1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/reboot.h>
28 #include <sys/ioctl.h>
32 #include <sys/socket.h>
34 #include <dbus/dbus.h>
40 #include "utmp-wtmp.h"
44 #include "dbus-common.h"
45 #include "cgroup-show.h"
46 #include "cgroup-util.h"
49 static const char *arg_type = NULL;
50 static const char *arg_property = NULL;
51 static bool arg_all = false;
52 static bool arg_fail = false;
53 static bool arg_session = false;
54 static bool arg_no_block = false;
55 static bool arg_immediate = false;
56 static bool arg_no_wtmp = false;
57 static bool arg_no_sync = false;
58 static bool arg_no_wall = false;
59 static bool arg_dry = false;
60 static bool arg_quiet = false;
61 static char arg_full = false;
62 static char **arg_wall = NULL;
80 } arg_action = ACTION_SYSTEMCTL;
87 static bool private_bus = false;
89 static bool error_is_no_service(DBusError *error) {
93 if (!dbus_error_is_set(error))
96 if (dbus_error_has_name(error, DBUS_ERROR_NAME_HAS_NO_OWNER))
99 if (dbus_error_has_name(error, DBUS_ERROR_SERVICE_UNKNOWN))
102 return startswith(error->name, "org.freedesktop.DBus.Error.Spawn.");
105 static int bus_iter_get_basic_and_next(DBusMessageIter *iter, int type, void *data, bool next) {
110 if (dbus_message_iter_get_arg_type(iter) != type)
113 dbus_message_iter_get_basic(iter, data);
115 if (!dbus_message_iter_next(iter) != !next)
121 static void warn_wall(enum action action) {
122 static const char *table[_ACTION_MAX] = {
123 [ACTION_HALT] = "The system is going down for system halt NOW!",
124 [ACTION_REBOOT] = "The system is going down for reboot NOW!",
125 [ACTION_POWEROFF] = "The system is going down for power-off NOW!",
126 [ACTION_RESCUE] = "The system is going down to rescue mode NOW!",
127 [ACTION_EMERGENCY] = "The system is going down to emergency mode NOW!"
136 if (!(p = strv_join(arg_wall, " "))) {
137 log_error("Failed to join strings.");
153 utmp_wall(table[action]);
156 static int list_units(DBusConnection *bus, char **args, unsigned n) {
157 DBusMessage *m = NULL, *reply = NULL;
160 DBusMessageIter iter, sub, sub2;
163 dbus_error_init(&error);
167 if (!(m = dbus_message_new_method_call(
168 "org.freedesktop.systemd1",
169 "/org/freedesktop/systemd1",
170 "org.freedesktop.systemd1.Manager",
172 log_error("Could not allocate message.");
176 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
177 log_error("Failed to issue method call: %s", error.message);
182 if (!dbus_message_iter_init(reply, &iter) ||
183 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
184 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
185 log_error("Failed to parse reply.");
190 dbus_message_iter_recurse(&iter, &sub);
192 if (isatty(STDOUT_FILENO))
193 printf("%-45s %-6s %-12s %-12s %-15s %s\n", "UNIT", "LOAD", "ACTIVE", "SUB", "JOB", "DESCRIPTION");
195 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
196 const char *id, *description, *load_state, *active_state, *sub_state, *following, *unit_path, *job_type, *job_path, *dot;
199 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
200 log_error("Failed to parse reply.");
205 dbus_message_iter_recurse(&sub, &sub2);
207 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
208 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
209 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
210 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
211 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
212 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &following, true) < 0 ||
213 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, true) < 0 ||
214 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &job_id, true) < 0 ||
215 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &job_type, true) < 0 ||
216 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, false) < 0) {
217 log_error("Failed to parse reply.");
222 if ((!arg_type || ((dot = strrchr(id, '.')) &&
223 streq(dot+1, arg_type))) &&
224 (arg_all || !(streq(active_state, "inactive") || following[0]) || job_id > 0)) {
228 if (streq(active_state, "maintenance"))
229 fputs(ANSI_HIGHLIGHT_ON, stdout);
231 e = arg_full ? NULL : ellipsize(id, 45, 33);
232 printf("%-45s %-6s %-12s %-12s%n", e ? e : id, load_state, active_state, sub_state, &a);
236 printf(" => %-12s%n", job_type, &b);
240 if (a + b + 2 < columns()) {
244 printf(" %.*s", columns() - a - b - 2, description);
247 if (streq(active_state, "maintenance"))
248 fputs(ANSI_HIGHLIGHT_OFF, stdout);
254 dbus_message_iter_next(&sub);
257 if (isatty(STDOUT_FILENO)) {
259 printf("\nLOAD = Load State, reflects whether the unit configuration was properly loaded.\n"
260 "ACTIVE = Active State, the high-level unit activation state, i.e. generalization of the substate.\n"
261 "SUB = Substate, the low-level unit activation state, possible values depend on unit type.\n"
262 "JOB = Job, shows pending jobs for the unit.\n");
265 printf("\n%u units listed.\n", k);
267 printf("\n%u units listed. Pass --all to see inactive units, too.\n", k);
274 dbus_message_unref(m);
277 dbus_message_unref(reply);
279 dbus_error_free(&error);
284 static int dot_one_property(const char *name, const char *prop, DBusMessageIter *iter) {
285 static const char * const colors[] = {
286 "Requires", "[color=\"black\"]",
287 "RequiresOverridable", "[color=\"black\"]",
288 "Requisite", "[color=\"darkblue\"]",
289 "RequisiteOverridable", "[color=\"darkblue\"]",
290 "Wants", "[color=\"darkgrey\"]",
291 "Conflicts", "[color=\"red\"]",
292 "After", "[color=\"green\"]"
295 const char *c = NULL;
302 for (i = 0; i < ELEMENTSOF(colors); i += 2)
303 if (streq(colors[i], prop)) {
311 if (arg_dot != DOT_ALL)
312 if ((arg_dot == DOT_ORDER) != streq(prop, "After"))
315 switch (dbus_message_iter_get_arg_type(iter)) {
317 case DBUS_TYPE_ARRAY:
319 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
322 dbus_message_iter_recurse(iter, &sub);
324 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
327 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
328 dbus_message_iter_get_basic(&sub, &s);
329 printf("\t\"%s\"->\"%s\" %s;\n", name, s, c);
331 dbus_message_iter_next(&sub);
341 static int dot_one(DBusConnection *bus, const char *name, const char *path) {
342 DBusMessage *m = NULL, *reply = NULL;
343 const char *interface = "org.freedesktop.systemd1.Unit";
346 DBusMessageIter iter, sub, sub2, sub3;
351 dbus_error_init(&error);
353 if (!(m = dbus_message_new_method_call(
354 "org.freedesktop.systemd1",
356 "org.freedesktop.DBus.Properties",
358 log_error("Could not allocate message.");
363 if (!dbus_message_append_args(m,
364 DBUS_TYPE_STRING, &interface,
365 DBUS_TYPE_INVALID)) {
366 log_error("Could not append arguments to message.");
371 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
372 log_error("Failed to issue method call: %s", error.message);
377 if (!dbus_message_iter_init(reply, &iter) ||
378 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
379 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
380 log_error("Failed to parse reply.");
385 dbus_message_iter_recurse(&iter, &sub);
387 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
390 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
391 log_error("Failed to parse reply.");
396 dbus_message_iter_recurse(&sub, &sub2);
398 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &prop, true) < 0) {
399 log_error("Failed to parse reply.");
404 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
405 log_error("Failed to parse reply.");
410 dbus_message_iter_recurse(&sub2, &sub3);
412 if (dot_one_property(name, prop, &sub3)) {
413 log_error("Failed to parse reply.");
418 dbus_message_iter_next(&sub);
423 dbus_message_unref(m);
426 dbus_message_unref(reply);
428 dbus_error_free(&error);
433 static int dot(DBusConnection *bus, char **args, unsigned n) {
434 DBusMessage *m = NULL, *reply = NULL;
437 DBusMessageIter iter, sub, sub2;
439 dbus_error_init(&error);
443 if (!(m = dbus_message_new_method_call(
444 "org.freedesktop.systemd1",
445 "/org/freedesktop/systemd1",
446 "org.freedesktop.systemd1.Manager",
448 log_error("Could not allocate message.");
452 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
453 log_error("Failed to issue method call: %s", error.message);
458 if (!dbus_message_iter_init(reply, &iter) ||
459 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
460 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
461 log_error("Failed to parse reply.");
466 printf("digraph systemd {\n");
468 dbus_message_iter_recurse(&iter, &sub);
469 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
470 const char *id, *description, *load_state, *active_state, *sub_state, *unit_path;
472 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
473 log_error("Failed to parse reply.");
478 dbus_message_iter_recurse(&sub, &sub2);
480 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
481 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
482 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
483 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
484 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
485 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, true) < 0) {
486 log_error("Failed to parse reply.");
491 if ((r = dot_one(bus, id, unit_path)) < 0)
494 /* printf("\t\"%s\";\n", id); */
495 dbus_message_iter_next(&sub);
500 log_info(" Color legend: black = Requires\n"
501 " dark blue = Requisite\n"
502 " dark grey = Wants\n"
506 if (isatty(fileno(stdout)))
507 log_notice("-- You probably want to process this output with graphviz' dot tool.\n"
508 "-- Try a shell pipeline like 'systemctl dot | dot -Tsvg > systemd.svg'!\n");
514 dbus_message_unref(m);
517 dbus_message_unref(reply);
519 dbus_error_free(&error);
524 static int list_jobs(DBusConnection *bus, char **args, unsigned n) {
525 DBusMessage *m = NULL, *reply = NULL;
528 DBusMessageIter iter, sub, sub2;
531 dbus_error_init(&error);
535 if (!(m = dbus_message_new_method_call(
536 "org.freedesktop.systemd1",
537 "/org/freedesktop/systemd1",
538 "org.freedesktop.systemd1.Manager",
540 log_error("Could not allocate message.");
544 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
545 log_error("Failed to issue method call: %s", error.message);
550 if (!dbus_message_iter_init(reply, &iter) ||
551 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
552 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
553 log_error("Failed to parse reply.");
558 dbus_message_iter_recurse(&iter, &sub);
560 printf("%4s %-45s %-17s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
562 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
563 const char *name, *type, *state, *job_path, *unit_path;
567 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
568 log_error("Failed to parse reply.");
573 dbus_message_iter_recurse(&sub, &sub2);
575 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
576 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
577 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
578 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
579 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
580 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
581 log_error("Failed to parse reply.");
586 e = arg_full ? NULL : ellipsize(name, 45, 33);
587 printf("%4u %-45s %-17s %-7s\n", id, e ? e : name, type, state);
592 dbus_message_iter_next(&sub);
595 printf("\n%u jobs listed.\n", k);
600 dbus_message_unref(m);
603 dbus_message_unref(reply);
605 dbus_error_free(&error);
610 static int load_unit(DBusConnection *bus, char **args, unsigned n) {
611 DBusMessage *m = NULL, *reply = NULL;
616 dbus_error_init(&error);
621 for (i = 1; i < n; i++) {
623 if (!(m = dbus_message_new_method_call(
624 "org.freedesktop.systemd1",
625 "/org/freedesktop/systemd1",
626 "org.freedesktop.systemd1.Manager",
628 log_error("Could not allocate message.");
633 if (!dbus_message_append_args(m,
634 DBUS_TYPE_STRING, &args[i],
635 DBUS_TYPE_INVALID)) {
636 log_error("Could not append arguments to message.");
641 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
642 log_error("Failed to issue method call: %s", error.message);
647 dbus_message_unref(m);
648 dbus_message_unref(reply);
657 dbus_message_unref(m);
660 dbus_message_unref(reply);
662 dbus_error_free(&error);
667 static int cancel_job(DBusConnection *bus, char **args, unsigned n) {
668 DBusMessage *m = NULL, *reply = NULL;
673 dbus_error_init(&error);
678 for (i = 1; i < n; i++) {
682 if (!(m = dbus_message_new_method_call(
683 "org.freedesktop.systemd1",
684 "/org/freedesktop/systemd1",
685 "org.freedesktop.systemd1.Manager",
687 log_error("Could not allocate message.");
692 if ((r = safe_atou(args[i], &id)) < 0) {
693 log_error("Failed to parse job id: %s", strerror(-r));
697 assert_cc(sizeof(uint32_t) == sizeof(id));
698 if (!dbus_message_append_args(m,
699 DBUS_TYPE_UINT32, &id,
700 DBUS_TYPE_INVALID)) {
701 log_error("Could not append arguments to message.");
706 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
707 log_error("Failed to issue method call: %s", error.message);
712 if (!dbus_message_get_args(reply, &error,
713 DBUS_TYPE_OBJECT_PATH, &path,
714 DBUS_TYPE_INVALID)) {
715 log_error("Failed to parse reply: %s", error.message);
720 dbus_message_unref(m);
721 if (!(m = dbus_message_new_method_call(
722 "org.freedesktop.systemd1",
724 "org.freedesktop.systemd1.Job",
726 log_error("Could not allocate message.");
731 dbus_message_unref(reply);
732 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
733 log_error("Failed to issue method call: %s", error.message);
738 dbus_message_unref(m);
739 dbus_message_unref(reply);
747 dbus_message_unref(m);
750 dbus_message_unref(reply);
752 dbus_error_free(&error);
757 static bool unit_need_daemon_reload(DBusConnection *bus, const char *unit) {
758 DBusMessage *m, *reply;
759 dbus_bool_t b = FALSE;
760 DBusMessageIter iter, sub;
762 *interface = "org.freedesktop.systemd1.Unit",
763 *property = "NeedDaemonReload",
766 /* We ignore all errors here, since this is used to show a warning only */
768 if (!(m = dbus_message_new_method_call(
769 "org.freedesktop.systemd1",
770 "/org/freedesktop/systemd1",
771 "org.freedesktop.systemd1.Manager",
775 if (!dbus_message_append_args(m,
776 DBUS_TYPE_STRING, &unit,
780 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
783 if (!dbus_message_get_args(reply, NULL,
784 DBUS_TYPE_OBJECT_PATH, &path,
788 dbus_message_unref(m);
789 if (!(m = dbus_message_new_method_call(
790 "org.freedesktop.systemd1",
792 "org.freedesktop.DBus.Properties",
796 if (!dbus_message_append_args(m,
797 DBUS_TYPE_STRING, &interface,
798 DBUS_TYPE_STRING, &property,
799 DBUS_TYPE_INVALID)) {
803 dbus_message_unref(reply);
804 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
807 if (!dbus_message_iter_init(reply, &iter) ||
808 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
811 dbus_message_iter_recurse(&iter, &sub);
813 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_BOOLEAN)
816 dbus_message_iter_get_basic(&sub, &b);
820 dbus_message_unref(m);
823 dbus_message_unref(reply);
828 typedef struct WaitData {
833 static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
841 dbus_error_init(&error);
843 log_debug("Got D-Bus request: %s.%s() on %s",
844 dbus_message_get_interface(message),
845 dbus_message_get_member(message),
846 dbus_message_get_path(message));
848 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
849 log_error("Warning! D-Bus connection terminated.");
850 dbus_connection_close(connection);
852 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
855 dbus_bool_t success = true;
857 if (!dbus_message_get_args(message, &error,
858 DBUS_TYPE_UINT32, &id,
859 DBUS_TYPE_OBJECT_PATH, &path,
860 DBUS_TYPE_BOOLEAN, &success,
862 log_error("Failed to parse message: %s", error.message);
866 if ((p = set_remove(d->set, (char*) path)))
874 dbus_error_free(&error);
875 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
878 static int enable_wait_for_jobs(DBusConnection *bus) {
886 dbus_error_init(&error);
887 dbus_bus_add_match(bus,
889 "sender='org.freedesktop.systemd1',"
890 "interface='org.freedesktop.systemd1.Manager',"
891 "member='JobRemoved',"
892 "path='/org/freedesktop/systemd1'",
895 if (dbus_error_is_set(&error)) {
896 log_error("Failed to add match: %s", error.message);
897 dbus_error_free(&error);
901 /* This is slightly dirty, since we don't undo the match registrations. */
905 static int wait_for_jobs(DBusConnection *bus, Set *s) {
916 if (!dbus_connection_add_filter(bus, wait_filter, &d, NULL)) {
917 log_error("Failed to add filter.");
922 while (!set_isempty(s) &&
923 dbus_connection_read_write_dispatch(bus, -1))
926 if (!arg_quiet && d.failed)
927 log_error("Job failed, see logs for details.");
929 r = d.failed ? -EIO : 0;
932 /* This is slightly dirty, since we don't undo the filter registration. */
937 static int start_unit_one(
944 DBusMessage *m = NULL, *reply = NULL;
953 assert(arg_no_block || s);
955 dbus_error_init(&error);
957 if (!(m = dbus_message_new_method_call(
958 "org.freedesktop.systemd1",
959 "/org/freedesktop/systemd1",
960 "org.freedesktop.systemd1.Manager",
962 log_error("Could not allocate message.");
967 if (!dbus_message_append_args(m,
968 DBUS_TYPE_STRING, &name,
969 DBUS_TYPE_STRING, &mode,
970 DBUS_TYPE_INVALID)) {
971 log_error("Could not append arguments to message.");
976 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
978 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
979 /* There's always a fallback possible for
985 log_error("Failed to issue method call: %s", error.message);
990 if (!dbus_message_get_args(reply, &error,
991 DBUS_TYPE_OBJECT_PATH, &path,
992 DBUS_TYPE_INVALID)) {
993 log_error("Failed to parse reply: %s", error.message);
998 if (unit_need_daemon_reload(bus, name))
999 log_warning("Unit file of created job changed on disk, 'systemctl %s daemon-reload' recommended.",
1000 arg_session ? "--session" : "--system");
1002 if (!arg_no_block) {
1005 if (!(p = strdup(path))) {
1006 log_error("Failed to duplicate path.");
1011 if ((r = set_put(s, p)) < 0) {
1013 log_error("Failed to add path to set.");
1022 dbus_message_unref(m);
1025 dbus_message_unref(reply);
1027 dbus_error_free(&error);
1032 static enum action verb_to_action(const char *verb) {
1033 if (streq(verb, "halt"))
1035 else if (streq(verb, "poweroff"))
1036 return ACTION_POWEROFF;
1037 else if (streq(verb, "reboot"))
1038 return ACTION_REBOOT;
1039 else if (streq(verb, "rescue"))
1040 return ACTION_RESCUE;
1041 else if (streq(verb, "emergency"))
1042 return ACTION_EMERGENCY;
1043 else if (streq(verb, "default"))
1044 return ACTION_DEFAULT;
1046 return ACTION_INVALID;
1049 static int start_unit(DBusConnection *bus, char **args, unsigned n) {
1051 static const char * const table[_ACTION_MAX] = {
1052 [ACTION_HALT] = SPECIAL_HALT_TARGET,
1053 [ACTION_POWEROFF] = SPECIAL_POWEROFF_TARGET,
1054 [ACTION_REBOOT] = SPECIAL_REBOOT_TARGET,
1055 [ACTION_RUNLEVEL2] = SPECIAL_RUNLEVEL2_TARGET,
1056 [ACTION_RUNLEVEL3] = SPECIAL_RUNLEVEL3_TARGET,
1057 [ACTION_RUNLEVEL4] = SPECIAL_RUNLEVEL4_TARGET,
1058 [ACTION_RUNLEVEL5] = SPECIAL_RUNLEVEL5_TARGET,
1059 [ACTION_RESCUE] = SPECIAL_RESCUE_TARGET,
1060 [ACTION_EMERGENCY] = SPECIAL_EMERGENCY_TARGET,
1061 [ACTION_DEFAULT] = SPECIAL_DEFAULT_TARGET
1066 const char *method, *mode, *one_name;
1071 if (arg_action == ACTION_SYSTEMCTL) {
1073 streq(args[0], "stop") ? "StopUnit" :
1074 streq(args[0], "reload") ? "ReloadUnit" :
1075 streq(args[0], "restart") ? "RestartUnit" :
1076 streq(args[0], "try-restart") ? "TryRestartUnit" :
1077 streq(args[0], "reload-or-restart") ? "ReloadOrRestartUnit" :
1078 streq(args[0], "reload-or-try-restart") ? "ReloadOrTryRestartUnit" :
1082 (streq(args[0], "isolate") ||
1083 streq(args[0], "rescue") ||
1084 streq(args[0], "emergency")) ? "isolate" :
1088 one_name = table[verb_to_action(args[0])];
1091 assert(arg_action < ELEMENTSOF(table));
1092 assert(table[arg_action]);
1094 method = "StartUnit";
1096 mode = (arg_action == ACTION_EMERGENCY ||
1097 arg_action == ACTION_RESCUE) ? "isolate" : "replace";
1099 one_name = table[arg_action];
1102 if (!arg_no_block) {
1103 if ((r = enable_wait_for_jobs(bus)) < 0) {
1104 log_error("Could not watch jobs: %s", strerror(-r));
1108 if (!(s = set_new(string_hash_func, string_compare_func))) {
1109 log_error("Failed to allocate set.");
1118 if ((r = start_unit_one(bus, method, one_name, mode, s)) <= 0)
1121 for (i = 1; i < n; i++)
1122 if ((r = start_unit_one(bus, method, args[i], mode, s)) < 0)
1127 r = wait_for_jobs(bus, s);
1136 static int start_special(DBusConnection *bus, char **args, unsigned n) {
1142 r = start_unit(bus, args, n);
1145 warn_wall(verb_to_action(args[0]));
1150 static int check_unit(DBusConnection *bus, char **args, unsigned n) {
1151 DBusMessage *m = NULL, *reply = NULL;
1153 *interface = "org.freedesktop.systemd1.Unit",
1154 *property = "ActiveState";
1155 int r = -EADDRNOTAVAIL;
1162 dbus_error_init(&error);
1164 for (i = 1; i < n; i++) {
1165 const char *path = NULL;
1167 DBusMessageIter iter, sub;
1169 if (!(m = dbus_message_new_method_call(
1170 "org.freedesktop.systemd1",
1171 "/org/freedesktop/systemd1",
1172 "org.freedesktop.systemd1.Manager",
1174 log_error("Could not allocate message.");
1179 if (!dbus_message_append_args(m,
1180 DBUS_TYPE_STRING, &args[i],
1181 DBUS_TYPE_INVALID)) {
1182 log_error("Could not append arguments to message.");
1187 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1189 /* Hmm, cannot figure out anything about this unit... */
1193 dbus_error_free(&error);
1197 if (!dbus_message_get_args(reply, &error,
1198 DBUS_TYPE_OBJECT_PATH, &path,
1199 DBUS_TYPE_INVALID)) {
1200 log_error("Failed to parse reply: %s", error.message);
1205 dbus_message_unref(m);
1206 if (!(m = dbus_message_new_method_call(
1207 "org.freedesktop.systemd1",
1209 "org.freedesktop.DBus.Properties",
1211 log_error("Could not allocate message.");
1216 if (!dbus_message_append_args(m,
1217 DBUS_TYPE_STRING, &interface,
1218 DBUS_TYPE_STRING, &property,
1219 DBUS_TYPE_INVALID)) {
1220 log_error("Could not append arguments to message.");
1225 dbus_message_unref(reply);
1226 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1227 log_error("Failed to issue method call: %s", error.message);
1232 if (!dbus_message_iter_init(reply, &iter) ||
1233 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1234 log_error("Failed to parse reply.");
1239 dbus_message_iter_recurse(&iter, &sub);
1241 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1242 log_error("Failed to parse reply.");
1247 dbus_message_iter_get_basic(&sub, &state);
1252 if (streq(state, "active") || startswith(state, "reloading"))
1255 dbus_message_unref(m);
1256 dbus_message_unref(reply);
1262 dbus_message_unref(m);
1265 dbus_message_unref(reply);
1267 dbus_error_free(&error);
1272 typedef struct ExecStatusInfo {
1278 usec_t start_timestamp;
1279 usec_t exit_timestamp;
1284 LIST_FIELDS(struct ExecStatusInfo, exec);
1287 static void exec_status_info_free(ExecStatusInfo *i) {
1295 static int exec_status_info_deserialize(DBusMessageIter *sub, ExecStatusInfo *i) {
1296 uint64_t start_timestamp, exit_timestamp;
1297 DBusMessageIter sub2, sub3;
1301 int32_t code, status;
1307 if (dbus_message_iter_get_arg_type(sub) != DBUS_TYPE_STRUCT)
1310 dbus_message_iter_recurse(sub, &sub2);
1312 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
1315 if (!(i->path = strdup(path)))
1318 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
1319 dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
1323 dbus_message_iter_recurse(&sub2, &sub3);
1324 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1325 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1326 dbus_message_iter_next(&sub3);
1331 if (!(i->argv = new0(char*, n+1)))
1335 dbus_message_iter_recurse(&sub2, &sub3);
1336 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1339 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1340 dbus_message_iter_get_basic(&sub3, &s);
1341 dbus_message_iter_next(&sub3);
1343 if (!(i->argv[n++] = strdup(s)))
1347 if (!dbus_message_iter_next(&sub2) ||
1348 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, true) < 0 ||
1349 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp, true) < 0 ||
1350 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp, true) < 0 ||
1351 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) < 0 ||
1352 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) < 0 ||
1353 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) < 0)
1357 i->start_timestamp = (usec_t) start_timestamp;
1358 i->exit_timestamp = (usec_t) exit_timestamp;
1359 i->pid = (pid_t) pid;
1366 typedef struct UnitStatusInfo {
1368 const char *load_state;
1369 const char *active_state;
1370 const char *sub_state;
1372 const char *description;
1374 const char *fragment_path;
1375 const char *default_control_group;
1377 bool need_daemon_reload;
1382 const char *status_text;
1385 usec_t start_timestamp;
1386 usec_t exit_timestamp;
1388 int exit_code, exit_status;
1391 unsigned n_accepted;
1392 unsigned n_connections;
1396 const char *sysfs_path;
1398 /* Mount, Automount */
1404 LIST_HEAD(ExecStatusInfo, exec);
1407 static void print_status_info(UnitStatusInfo *i) {
1412 /* This shows pretty information about a unit. See
1413 * print_property() for a low-level property printer */
1415 printf("%s", strna(i->id));
1417 if (i->description && !streq_ptr(i->id, i->description))
1418 printf(" - %s", i->description);
1422 if (i->fragment_path)
1423 printf("\t Loaded: %s (%s)\n", strna(i->load_state), i->fragment_path);
1424 else if (streq_ptr(i->load_state, "failed"))
1425 printf("\t Loaded: " ANSI_HIGHLIGHT_ON "%s" ANSI_HIGHLIGHT_OFF "\n", strna(i->load_state));
1427 printf("\t Loaded: %s\n", strna(i->load_state));
1429 if (streq_ptr(i->active_state, "maintenance")) {
1430 if (streq_ptr(i->active_state, i->sub_state))
1431 printf("\t Active: " ANSI_HIGHLIGHT_ON "%s" ANSI_HIGHLIGHT_OFF "\n",
1432 strna(i->active_state));
1434 printf("\t Active: " ANSI_HIGHLIGHT_ON "%s (%s)" ANSI_HIGHLIGHT_OFF "\n",
1435 strna(i->active_state),
1436 strna(i->sub_state));
1438 if (streq_ptr(i->active_state, i->sub_state))
1439 printf("\t Active: %s\n",
1440 strna(i->active_state));
1442 printf("\t Active: %s (%s)\n",
1443 strna(i->active_state),
1444 strna(i->sub_state));
1448 printf("\t Device: %s\n", i->sysfs_path);
1450 printf("\t Where: %s\n", i->where);
1452 printf("\t What: %s\n", i->what);
1455 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
1457 LIST_FOREACH(exec, p, i->exec) {
1460 /* Only show exited processes here */
1464 t = strv_join(p->argv, " ");
1465 printf("\t Exited: %u (%s, code=%s, ", p->pid, strna(t), sigchld_code_to_string(p->code));
1468 if (p->code == CLD_EXITED)
1469 printf("status=%i", p->status);
1471 printf("signal=%s", signal_to_string(p->status));
1474 if (i->main_pid == p->pid &&
1475 i->start_timestamp == p->start_timestamp &&
1476 i->exit_timestamp == p->start_timestamp)
1477 /* Let's not show this twice */
1480 if (p->pid == i->control_pid)
1484 if (i->main_pid > 0 || i->control_pid > 0) {
1487 if (i->main_pid > 0) {
1488 printf(" Main: %u", (unsigned) i->main_pid);
1492 get_process_name(i->main_pid, &t);
1498 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
1500 if (i->exit_code == CLD_EXITED)
1501 printf("status=%i", i->exit_status);
1503 printf("signal=%s", signal_to_string(i->exit_status));
1508 if (i->main_pid > 0 && i->control_pid > 0)
1511 if (i->control_pid > 0) {
1514 printf(" Control: %u", (unsigned) i->control_pid);
1516 get_process_name(i->control_pid, &t);
1527 printf("\t Status: \"%s\"\n", i->status_text);
1529 if (i->default_control_group) {
1532 printf("\t CGroup: %s\n", i->default_control_group);
1534 if ((c = columns()) > 18)
1539 show_cgroup_by_path(i->default_control_group, "\t\t ", c);
1542 if (i->need_daemon_reload)
1543 printf("\n" ANSI_HIGHLIGHT_ON "Warning:" ANSI_HIGHLIGHT_OFF " Unit file changed on disk, 'systemctl %s daemon-reload' recommended.\n",
1544 arg_session ? "--session" : "--system");
1547 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
1549 switch (dbus_message_iter_get_arg_type(iter)) {
1551 case DBUS_TYPE_STRING: {
1554 dbus_message_iter_get_basic(iter, &s);
1557 if (streq(name, "Id"))
1559 else if (streq(name, "LoadState"))
1561 else if (streq(name, "ActiveState"))
1562 i->active_state = s;
1563 else if (streq(name, "SubState"))
1565 else if (streq(name, "Description"))
1567 else if (streq(name, "FragmentPath"))
1568 i->fragment_path = s;
1569 else if (streq(name, "DefaultControlGroup"))
1570 i->default_control_group = s;
1571 else if (streq(name, "StatusText"))
1573 else if (streq(name, "SysFSPath"))
1575 else if (streq(name, "Where"))
1577 else if (streq(name, "What"))
1584 case DBUS_TYPE_BOOLEAN: {
1587 dbus_message_iter_get_basic(iter, &b);
1589 if (streq(name, "Accept"))
1591 else if (streq(name, "NeedDaemonReload"))
1592 i->need_daemon_reload = b;
1597 case DBUS_TYPE_UINT32: {
1600 dbus_message_iter_get_basic(iter, &u);
1602 if (streq(name, "MainPID")) {
1604 i->main_pid = (pid_t) u;
1607 } else if (streq(name, "ControlPID"))
1608 i->control_pid = (pid_t) u;
1609 else if (streq(name, "ExecMainPID")) {
1611 i->main_pid = (pid_t) u;
1612 } else if (streq(name, "NAccepted"))
1614 else if (streq(name, "NConnections"))
1615 i->n_connections = u;
1620 case DBUS_TYPE_INT32: {
1623 dbus_message_iter_get_basic(iter, &j);
1625 if (streq(name, "ExecMainCode"))
1626 i->exit_code = (int) j;
1627 else if (streq(name, "ExecMainStatus"))
1628 i->exit_status = (int) j;
1633 case DBUS_TYPE_UINT64: {
1636 dbus_message_iter_get_basic(iter, &u);
1638 if (streq(name, "ExecMainStartTimestamp"))
1639 i->start_timestamp = (usec_t) u;
1640 else if (streq(name, "ExecMainExitTimestamp"))
1641 i->exit_timestamp = (usec_t) u;
1646 case DBUS_TYPE_ARRAY: {
1648 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT &&
1649 startswith(name, "Exec")) {
1650 DBusMessageIter sub;
1652 dbus_message_iter_recurse(iter, &sub);
1653 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1654 ExecStatusInfo *info;
1657 if (!(info = new0(ExecStatusInfo, 1)))
1660 if ((r = exec_status_info_deserialize(&sub, info)) < 0) {
1665 LIST_PREPEND(ExecStatusInfo, exec, i->exec, info);
1667 dbus_message_iter_next(&sub);
1678 static int print_property(const char *name, DBusMessageIter *iter) {
1682 /* This is a low-level property printer, see
1683 * print_status_info() for the nicer output */
1685 if (arg_property && !streq(name, arg_property))
1688 switch (dbus_message_iter_get_arg_type(iter)) {
1690 case DBUS_TYPE_STRING: {
1692 dbus_message_iter_get_basic(iter, &s);
1694 if (arg_all || s[0])
1695 printf("%s=%s\n", name, s);
1700 case DBUS_TYPE_BOOLEAN: {
1702 dbus_message_iter_get_basic(iter, &b);
1703 printf("%s=%s\n", name, yes_no(b));
1708 case DBUS_TYPE_UINT64: {
1710 dbus_message_iter_get_basic(iter, &u);
1712 /* Yes, heuristics! But we can change this check
1713 * should it turn out to not be sufficient */
1715 if (strstr(name, "Timestamp")) {
1716 char timestamp[FORMAT_TIMESTAMP_MAX], *t;
1718 if ((t = format_timestamp(timestamp, sizeof(timestamp), u)) || arg_all)
1719 printf("%s=%s\n", name, strempty(t));
1720 } else if (strstr(name, "USec")) {
1721 char timespan[FORMAT_TIMESPAN_MAX];
1723 printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u));
1725 printf("%s=%llu\n", name, (unsigned long long) u);
1730 case DBUS_TYPE_UINT32: {
1732 dbus_message_iter_get_basic(iter, &u);
1734 if (strstr(name, "UMask") || strstr(name, "Mode"))
1735 printf("%s=%04o\n", name, u);
1737 printf("%s=%u\n", name, (unsigned) u);
1742 case DBUS_TYPE_INT32: {
1744 dbus_message_iter_get_basic(iter, &i);
1746 printf("%s=%i\n", name, (int) i);
1750 case DBUS_TYPE_STRUCT: {
1751 DBusMessageIter sub;
1752 dbus_message_iter_recurse(iter, &sub);
1754 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
1757 dbus_message_iter_get_basic(&sub, &u);
1760 printf("%s=%u\n", name, (unsigned) u);
1762 printf("%s=\n", name);
1765 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
1768 dbus_message_iter_get_basic(&sub, &s);
1770 if (arg_all || s[0])
1771 printf("%s=%s\n", name, s);
1779 case DBUS_TYPE_ARRAY:
1781 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
1782 DBusMessageIter sub;
1785 dbus_message_iter_recurse(iter, &sub);
1787 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1788 printf("%s=", name);
1790 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1793 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
1794 dbus_message_iter_get_basic(&sub, &s);
1795 printf("%s%s", space ? " " : "", s);
1798 dbus_message_iter_next(&sub);
1806 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_BYTE) {
1807 DBusMessageIter sub;
1809 dbus_message_iter_recurse(iter, &sub);
1811 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1812 printf("%s=", name);
1814 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1817 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_BYTE);
1818 dbus_message_iter_get_basic(&sub, &u);
1821 dbus_message_iter_next(&sub);
1829 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
1830 DBusMessageIter sub, sub2;
1832 dbus_message_iter_recurse(iter, &sub);
1833 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1834 const char *type, *path;
1836 dbus_message_iter_recurse(&sub, &sub2);
1838 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
1839 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
1840 printf("%s=%s\n", type, path);
1842 dbus_message_iter_next(&sub);
1847 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
1848 DBusMessageIter sub, sub2;
1850 dbus_message_iter_recurse(iter, &sub);
1851 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1853 uint64_t value, next_elapse;
1855 dbus_message_iter_recurse(&sub, &sub2);
1857 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
1858 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
1859 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
1860 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
1862 printf("%s={ value=%s ; next_elapse=%s }\n",
1864 format_timespan(timespan1, sizeof(timespan1), value),
1865 format_timespan(timespan2, sizeof(timespan2), next_elapse));
1868 dbus_message_iter_next(&sub);
1873 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
1874 DBusMessageIter sub;
1876 dbus_message_iter_recurse(iter, &sub);
1877 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1878 ExecStatusInfo info;
1881 if (exec_status_info_deserialize(&sub, &info) >= 0) {
1882 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
1885 t = strv_join(info.argv, " ");
1887 printf("%s={ path=%s ; argv[]=%s ; ignore=%s ; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n",
1891 yes_no(info.ignore),
1892 strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)),
1893 strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)),
1894 (unsigned) info. pid,
1895 sigchld_code_to_string(info.code),
1897 info.code == CLD_EXITED ? "" : "/",
1898 strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status)));
1904 strv_free(info.argv);
1906 dbus_message_iter_next(&sub);
1916 printf("%s=[unprintable]\n", name);
1921 static int show_one(DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
1922 DBusMessage *m = NULL, *reply = NULL;
1923 const char *interface = "";
1926 DBusMessageIter iter, sub, sub2, sub3;
1927 UnitStatusInfo info;
1935 dbus_error_init(&error);
1937 if (!(m = dbus_message_new_method_call(
1938 "org.freedesktop.systemd1",
1940 "org.freedesktop.DBus.Properties",
1942 log_error("Could not allocate message.");
1947 if (!dbus_message_append_args(m,
1948 DBUS_TYPE_STRING, &interface,
1949 DBUS_TYPE_INVALID)) {
1950 log_error("Could not append arguments to message.");
1955 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1956 log_error("Failed to issue method call: %s", error.message);
1961 if (!dbus_message_iter_init(reply, &iter) ||
1962 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
1963 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
1964 log_error("Failed to parse reply.");
1969 dbus_message_iter_recurse(&iter, &sub);
1976 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1979 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
1980 log_error("Failed to parse reply.");
1985 dbus_message_iter_recurse(&sub, &sub2);
1987 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
1988 log_error("Failed to parse reply.");
1993 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
1994 log_error("Failed to parse reply.");
1999 dbus_message_iter_recurse(&sub2, &sub3);
2001 if (show_properties)
2002 r = print_property(name, &sub3);
2004 r = status_property(name, &sub3, &info);
2007 log_error("Failed to parse reply.");
2012 dbus_message_iter_next(&sub);
2015 if (!show_properties)
2016 print_status_info(&info);
2018 while ((p = info.exec)) {
2019 LIST_REMOVE(ExecStatusInfo, exec, info.exec, p);
2020 exec_status_info_free(p);
2027 dbus_message_unref(m);
2030 dbus_message_unref(reply);
2032 dbus_error_free(&error);
2037 static int show(DBusConnection *bus, char **args, unsigned n) {
2038 DBusMessage *m = NULL, *reply = NULL;
2042 bool show_properties, new_line = false;
2047 dbus_error_init(&error);
2049 show_properties = !streq(args[0], "status");
2051 if (show_properties && n <= 1) {
2052 /* If not argument is specified inspect the manager
2055 r = show_one(bus, "/org/freedesktop/systemd1", show_properties, &new_line);
2059 for (i = 1; i < n; i++) {
2060 const char *path = NULL;
2063 if (!show_properties || safe_atou32(args[i], &id) < 0) {
2065 if (!(m = dbus_message_new_method_call(
2066 "org.freedesktop.systemd1",
2067 "/org/freedesktop/systemd1",
2068 "org.freedesktop.systemd1.Manager",
2070 log_error("Could not allocate message.");
2075 if (!dbus_message_append_args(m,
2076 DBUS_TYPE_STRING, &args[i],
2077 DBUS_TYPE_INVALID)) {
2078 log_error("Could not append arguments to message.");
2083 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2085 if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
2086 log_error("Failed to issue method call: %s", error.message);
2091 dbus_error_free(&error);
2093 dbus_message_unref(m);
2094 if (!(m = dbus_message_new_method_call(
2095 "org.freedesktop.systemd1",
2096 "/org/freedesktop/systemd1",
2097 "org.freedesktop.systemd1.Manager",
2099 log_error("Could not allocate message.");
2104 if (!dbus_message_append_args(m,
2105 DBUS_TYPE_STRING, &args[i],
2106 DBUS_TYPE_INVALID)) {
2107 log_error("Could not append arguments to message.");
2112 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2113 log_error("Failed to issue method call: %s", error.message);
2121 if (!(m = dbus_message_new_method_call(
2122 "org.freedesktop.systemd1",
2123 "/org/freedesktop/systemd1",
2124 "org.freedesktop.systemd1.Manager",
2126 log_error("Could not allocate message.");
2131 if (!dbus_message_append_args(m,
2132 DBUS_TYPE_UINT32, &id,
2133 DBUS_TYPE_INVALID)) {
2134 log_error("Could not append arguments to message.");
2139 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2140 log_error("Failed to issue method call: %s", error.message);
2146 if (!dbus_message_get_args(reply, &error,
2147 DBUS_TYPE_OBJECT_PATH, &path,
2148 DBUS_TYPE_INVALID)) {
2149 log_error("Failed to parse reply: %s", error.message);
2154 if ((r = show_one(bus, path, show_properties, &new_line)) < 0)
2157 dbus_message_unref(m);
2158 dbus_message_unref(reply);
2166 dbus_message_unref(m);
2169 dbus_message_unref(reply);
2171 dbus_error_free(&error);
2176 static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
2178 DBusMessage *m = NULL, *reply = NULL;
2183 dbus_error_init(&error);
2185 log_debug("Got D-Bus request: %s.%s() on %s",
2186 dbus_message_get_interface(message),
2187 dbus_message_get_member(message),
2188 dbus_message_get_path(message));
2190 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
2191 log_error("Warning! D-Bus connection terminated.");
2192 dbus_connection_close(connection);
2194 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") ||
2195 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
2196 const char *id, *path;
2198 if (!dbus_message_get_args(message, &error,
2199 DBUS_TYPE_STRING, &id,
2200 DBUS_TYPE_OBJECT_PATH, &path,
2202 log_error("Failed to parse message: %s", error.message);
2203 else if (streq(dbus_message_get_member(message), "UnitNew"))
2204 printf("Unit %s added.\n", id);
2206 printf("Unit %s removed.\n", id);
2208 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew") ||
2209 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
2213 if (!dbus_message_get_args(message, &error,
2214 DBUS_TYPE_UINT32, &id,
2215 DBUS_TYPE_OBJECT_PATH, &path,
2217 log_error("Failed to parse message: %s", error.message);
2218 else if (streq(dbus_message_get_member(message), "JobNew"))
2219 printf("Job %u added.\n", id);
2221 printf("Job %u removed.\n", id);
2224 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Unit", "Changed") ||
2225 dbus_message_is_signal(message, "org.freedesktop.systemd1.Job", "Changed")) {
2227 const char *path, *interface, *property = "Id";
2228 DBusMessageIter iter, sub;
2230 path = dbus_message_get_path(message);
2231 interface = dbus_message_get_interface(message);
2233 if (!(m = dbus_message_new_method_call(
2234 "org.freedesktop.systemd1",
2236 "org.freedesktop.DBus.Properties",
2238 log_error("Could not allocate message.");
2242 if (!dbus_message_append_args(m,
2243 DBUS_TYPE_STRING, &interface,
2244 DBUS_TYPE_STRING, &property,
2245 DBUS_TYPE_INVALID)) {
2246 log_error("Could not append arguments to message.");
2250 if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
2251 log_error("Failed to issue method call: %s", error.message);
2255 if (!dbus_message_iter_init(reply, &iter) ||
2256 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2257 log_error("Failed to parse reply.");
2261 dbus_message_iter_recurse(&iter, &sub);
2263 if (streq(interface, "org.freedesktop.systemd1.Unit")) {
2266 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2267 log_error("Failed to parse reply.");
2271 dbus_message_iter_get_basic(&sub, &id);
2272 printf("Unit %s changed.\n", id);
2276 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32) {
2277 log_error("Failed to parse reply.");
2281 dbus_message_iter_get_basic(&sub, &id);
2282 printf("Job %u changed.\n", id);
2288 dbus_message_unref(m);
2291 dbus_message_unref(reply);
2293 dbus_error_free(&error);
2294 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2298 dbus_message_unref(m);
2301 dbus_message_unref(reply);
2303 dbus_error_free(&error);
2304 return DBUS_HANDLER_RESULT_NEED_MEMORY;
2307 static int monitor(DBusConnection *bus, char **args, unsigned n) {
2308 DBusMessage *m = NULL, *reply = NULL;
2312 dbus_error_init(&error);
2315 dbus_bus_add_match(bus,
2317 "sender='org.freedesktop.systemd1',"
2318 "interface='org.freedesktop.systemd1.Manager',"
2319 "path='/org/freedesktop/systemd1'",
2322 if (dbus_error_is_set(&error)) {
2323 log_error("Failed to add match: %s", error.message);
2328 dbus_bus_add_match(bus,
2330 "sender='org.freedesktop.systemd1',"
2331 "interface='org.freedesktop.systemd1.Unit',"
2335 if (dbus_error_is_set(&error)) {
2336 log_error("Failed to add match: %s", error.message);
2341 dbus_bus_add_match(bus,
2343 "sender='org.freedesktop.systemd1',"
2344 "interface='org.freedesktop.systemd1.Job',"
2348 if (dbus_error_is_set(&error)) {
2349 log_error("Failed to add match: %s", error.message);
2355 if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
2356 log_error("Failed to add filter.");
2361 if (!(m = dbus_message_new_method_call(
2362 "org.freedesktop.systemd1",
2363 "/org/freedesktop/systemd1",
2364 "org.freedesktop.systemd1.Manager",
2366 log_error("Could not allocate message.");
2371 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2372 log_error("Failed to issue method call: %s", error.message);
2377 while (dbus_connection_read_write_dispatch(bus, -1))
2384 /* This is slightly dirty, since we don't undo the filter or the matches. */
2387 dbus_message_unref(m);
2390 dbus_message_unref(reply);
2392 dbus_error_free(&error);
2397 static int dump(DBusConnection *bus, char **args, unsigned n) {
2398 DBusMessage *m = NULL, *reply = NULL;
2403 dbus_error_init(&error);
2405 if (!(m = dbus_message_new_method_call(
2406 "org.freedesktop.systemd1",
2407 "/org/freedesktop/systemd1",
2408 "org.freedesktop.systemd1.Manager",
2410 log_error("Could not allocate message.");
2414 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2415 log_error("Failed to issue method call: %s", error.message);
2420 if (!dbus_message_get_args(reply, &error,
2421 DBUS_TYPE_STRING, &text,
2422 DBUS_TYPE_INVALID)) {
2423 log_error("Failed to parse reply: %s", error.message);
2428 fputs(text, stdout);
2434 dbus_message_unref(m);
2437 dbus_message_unref(reply);
2439 dbus_error_free(&error);
2444 static int snapshot(DBusConnection *bus, char **args, unsigned n) {
2445 DBusMessage *m = NULL, *reply = NULL;
2448 const char *name = "", *path, *id;
2449 dbus_bool_t cleanup = FALSE;
2450 DBusMessageIter iter, sub;
2452 *interface = "org.freedesktop.systemd1.Unit",
2455 dbus_error_init(&error);
2457 if (!(m = dbus_message_new_method_call(
2458 "org.freedesktop.systemd1",
2459 "/org/freedesktop/systemd1",
2460 "org.freedesktop.systemd1.Manager",
2461 "CreateSnapshot"))) {
2462 log_error("Could not allocate message.");
2469 if (!dbus_message_append_args(m,
2470 DBUS_TYPE_STRING, &name,
2471 DBUS_TYPE_BOOLEAN, &cleanup,
2472 DBUS_TYPE_INVALID)) {
2473 log_error("Could not append arguments to message.");
2478 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2479 log_error("Failed to issue method call: %s", error.message);
2484 if (!dbus_message_get_args(reply, &error,
2485 DBUS_TYPE_OBJECT_PATH, &path,
2486 DBUS_TYPE_INVALID)) {
2487 log_error("Failed to parse reply: %s", error.message);
2492 dbus_message_unref(m);
2493 if (!(m = dbus_message_new_method_call(
2494 "org.freedesktop.systemd1",
2496 "org.freedesktop.DBus.Properties",
2498 log_error("Could not allocate message.");
2502 if (!dbus_message_append_args(m,
2503 DBUS_TYPE_STRING, &interface,
2504 DBUS_TYPE_STRING, &property,
2505 DBUS_TYPE_INVALID)) {
2506 log_error("Could not append arguments to message.");
2511 dbus_message_unref(reply);
2512 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2513 log_error("Failed to issue method call: %s", error.message);
2518 if (!dbus_message_iter_init(reply, &iter) ||
2519 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2520 log_error("Failed to parse reply.");
2525 dbus_message_iter_recurse(&iter, &sub);
2527 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2528 log_error("Failed to parse reply.");
2533 dbus_message_iter_get_basic(&sub, &id);
2541 dbus_message_unref(m);
2544 dbus_message_unref(reply);
2546 dbus_error_free(&error);
2551 static int delete_snapshot(DBusConnection *bus, char **args, unsigned n) {
2552 DBusMessage *m = NULL, *reply = NULL;
2560 dbus_error_init(&error);
2562 for (i = 1; i < n; i++) {
2563 const char *path = NULL;
2565 if (!(m = dbus_message_new_method_call(
2566 "org.freedesktop.systemd1",
2567 "/org/freedesktop/systemd1",
2568 "org.freedesktop.systemd1.Manager",
2570 log_error("Could not allocate message.");
2575 if (!dbus_message_append_args(m,
2576 DBUS_TYPE_STRING, &args[i],
2577 DBUS_TYPE_INVALID)) {
2578 log_error("Could not append arguments to message.");
2583 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2584 log_error("Failed to issue method call: %s", error.message);
2589 if (!dbus_message_get_args(reply, &error,
2590 DBUS_TYPE_OBJECT_PATH, &path,
2591 DBUS_TYPE_INVALID)) {
2592 log_error("Failed to parse reply: %s", error.message);
2597 dbus_message_unref(m);
2598 if (!(m = dbus_message_new_method_call(
2599 "org.freedesktop.systemd1",
2601 "org.freedesktop.systemd1.Snapshot",
2603 log_error("Could not allocate message.");
2608 dbus_message_unref(reply);
2609 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2610 log_error("Failed to issue method call: %s", error.message);
2615 dbus_message_unref(m);
2616 dbus_message_unref(reply);
2624 dbus_message_unref(m);
2627 dbus_message_unref(reply);
2629 dbus_error_free(&error);
2634 static int clear_jobs(DBusConnection *bus, char **args, unsigned n) {
2635 DBusMessage *m = NULL, *reply = NULL;
2640 dbus_error_init(&error);
2642 if (arg_action == ACTION_RELOAD)
2644 else if (arg_action == ACTION_REEXEC)
2645 method = "Reexecute";
2647 assert(arg_action == ACTION_SYSTEMCTL);
2650 streq(args[0], "clear-jobs") ? "ClearJobs" :
2651 streq(args[0], "daemon-reload") ? "Reload" :
2652 streq(args[0], "daemon-reexec") ? "Reexecute" :
2653 streq(args[0], "reset-maintenance") ? "ResetMaintenance" :
2657 if (!(m = dbus_message_new_method_call(
2658 "org.freedesktop.systemd1",
2659 "/org/freedesktop/systemd1",
2660 "org.freedesktop.systemd1.Manager",
2662 log_error("Could not allocate message.");
2666 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2668 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
2669 /* There's always a fallback possible for
2670 * legacy actions. */
2675 log_error("Failed to issue method call: %s", error.message);
2684 dbus_message_unref(m);
2687 dbus_message_unref(reply);
2689 dbus_error_free(&error);
2694 static int reset_maintenance(DBusConnection *bus, char **args, unsigned n) {
2695 DBusMessage *m = NULL, *reply = NULL;
2701 dbus_error_init(&error);
2704 return clear_jobs(bus, args, n);
2706 for (i = 1; i < n; i++) {
2708 if (!(m = dbus_message_new_method_call(
2709 "org.freedesktop.systemd1",
2710 "/org/freedesktop/systemd1",
2711 "org.freedesktop.systemd1.Manager",
2712 "ResetMaintenanceUnit"))) {
2713 log_error("Could not allocate message.");
2718 if (!dbus_message_append_args(m,
2719 DBUS_TYPE_STRING, args + i,
2720 DBUS_TYPE_INVALID)) {
2721 log_error("Could not append arguments to message.");
2726 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2727 log_error("Failed to issue method call: %s", error.message);
2732 dbus_message_unref(m);
2733 dbus_message_unref(reply);
2741 dbus_message_unref(m);
2744 dbus_message_unref(reply);
2746 dbus_error_free(&error);
2751 static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
2752 DBusMessage *m = NULL, *reply = NULL;
2754 DBusMessageIter iter, sub, sub2;
2757 *interface = "org.freedesktop.systemd1.Manager",
2758 *property = "Environment";
2760 dbus_error_init(&error);
2762 if (!(m = dbus_message_new_method_call(
2763 "org.freedesktop.systemd1",
2764 "/org/freedesktop/systemd1",
2765 "org.freedesktop.DBus.Properties",
2767 log_error("Could not allocate message.");
2771 if (!dbus_message_append_args(m,
2772 DBUS_TYPE_STRING, &interface,
2773 DBUS_TYPE_STRING, &property,
2774 DBUS_TYPE_INVALID)) {
2775 log_error("Could not append arguments to message.");
2780 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2781 log_error("Failed to issue method call: %s", error.message);
2786 if (!dbus_message_iter_init(reply, &iter) ||
2787 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2788 log_error("Failed to parse reply.");
2793 dbus_message_iter_recurse(&iter, &sub);
2795 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
2796 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
2797 log_error("Failed to parse reply.");
2802 dbus_message_iter_recurse(&sub, &sub2);
2804 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
2807 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
2808 log_error("Failed to parse reply.");
2813 dbus_message_iter_get_basic(&sub2, &text);
2814 printf("%s\n", text);
2816 dbus_message_iter_next(&sub2);
2823 dbus_message_unref(m);
2826 dbus_message_unref(reply);
2828 dbus_error_free(&error);
2833 static int set_environment(DBusConnection *bus, char **args, unsigned n) {
2834 DBusMessage *m = NULL, *reply = NULL;
2838 DBusMessageIter iter, sub;
2841 dbus_error_init(&error);
2843 method = streq(args[0], "set-environment")
2845 : "UnsetEnvironment";
2847 if (!(m = dbus_message_new_method_call(
2848 "org.freedesktop.systemd1",
2849 "/org/freedesktop/systemd1",
2850 "org.freedesktop.systemd1.Manager",
2853 log_error("Could not allocate message.");
2857 dbus_message_iter_init_append(m, &iter);
2859 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
2860 log_error("Could not append arguments to message.");
2865 for (i = 1; i < n; i++)
2866 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) {
2867 log_error("Could not append arguments to message.");
2872 if (!dbus_message_iter_close_container(&iter, &sub)) {
2873 log_error("Could not append arguments to message.");
2878 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2879 log_error("Failed to issue method call: %s", error.message);
2888 dbus_message_unref(m);
2891 dbus_message_unref(reply);
2893 dbus_error_free(&error);
2898 static int systemctl_help(void) {
2900 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
2901 "Send control commands to or query the systemd manager.\n\n"
2902 " -h --help Show this help\n"
2903 " -t --type=TYPE List only units of a particular type\n"
2904 " -p --property=NAME Show only properties by this name\n"
2905 " -a --all Show all units/properties, including dead/empty ones\n"
2906 " --full Don't ellipsize unit names.\n"
2907 " --fail When installing a new job, fail if conflicting jobs are\n"
2909 " --system Connect to system bus\n"
2910 " --session Connect to session bus\n"
2911 " --order When generating graph for dot, show only order\n"
2912 " --require When generating graph for dot, show only requirement\n"
2913 " -q --quiet Suppress output\n"
2914 " --no-block Do not wait until operation finished\n"
2915 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
2917 " list-units List units\n"
2918 " start [NAME...] Start one or more units\n"
2919 " stop [NAME...] Stop one or more units\n"
2920 " reload [NAME...] Reload one or more units\n"
2921 " restart [NAME...] Start or restart one or more units\n"
2922 " try-restart [NAME...] Restart one or more units if active\n"
2923 " reload-or-restart [NAME...] Reload one or more units is possible,\n"
2924 " otherwise start or restart\n"
2925 " reload-or-try-restart [NAME...] Reload one or more units is possible,\n"
2926 " otherwise restart if active\n"
2927 " isolate [NAME] Start one unit and stop all others\n"
2928 " check [NAME...] Check whether units are active\n"
2929 " status [NAME...] Show status of one or more units\n"
2930 " show [NAME...|JOB...] Show properties of one or more\n"
2931 " units/jobs/manager\n"
2932 " reset-maintenance [NAME...] Reset maintenance state for all, one\n"
2934 " load [NAME...] Load one or more units\n"
2935 " list-jobs List jobs\n"
2936 " cancel [JOB...] Cancel one or more jobs\n"
2937 " clear-jobs Cancel all jobs\n"
2938 " monitor Monitor unit/job changes\n"
2939 " dump Dump server status\n"
2940 " dot Dump dependency graph for dot(1)\n"
2941 " snapshot [NAME] Create a snapshot\n"
2942 " delete [NAME...] Remove one or more snapshots\n"
2943 " daemon-reload Reload systemd manager configuration\n"
2944 " daemon-reexec Reexecute systemd manager\n"
2945 " daemon-exit Ask the systemd manager to quit\n"
2946 " show-environment Dump environment\n"
2947 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
2948 " unset-environment [NAME...] Unset one or more environment variables\n"
2949 " halt Shut down and halt the system\n"
2950 " poweroff Shut down and power-off the system\n"
2951 " reboot Shut down and reboot the system\n"
2952 " rescue Enter system rescue mode\n"
2953 " emergency Enter system emergency mode\n"
2954 " default Enter system default mode\n",
2955 program_invocation_short_name);
2960 static int halt_help(void) {
2962 printf("%s [OPTIONS...]\n\n"
2963 "%s the system.\n\n"
2964 " --help Show this help\n"
2965 " --halt Halt the machine\n"
2966 " -p --poweroff Switch off the machine\n"
2967 " --reboot Reboot the machine\n"
2968 " -f --force Force immediate halt/power-off/reboot\n"
2969 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
2970 " -d --no-wtmp Don't write wtmp record\n"
2971 " -n --no-sync Don't sync before halt/power-off/reboot\n"
2972 " --no-wall Don't send wall message before halt/power-off/reboot\n",
2973 program_invocation_short_name,
2974 arg_action == ACTION_REBOOT ? "Reboot" :
2975 arg_action == ACTION_POWEROFF ? "Power off" :
2981 static int shutdown_help(void) {
2983 printf("%s [OPTIONS...] [now] [WALL...]\n\n"
2984 "Shut down the system.\n\n"
2985 " --help Show this help\n"
2986 " -H --halt Halt the machine\n"
2987 " -P --poweroff Power-off the machine\n"
2988 " -r --reboot Reboot the machine\n"
2989 " -h Equivalent to --poweroff, overriden by --halt\n"
2990 " -k Don't halt/power-off/reboot, just send warnings\n"
2991 " --no-wall Don't send wall message before halt/power-off/reboot\n",
2992 program_invocation_short_name);
2997 static int telinit_help(void) {
2999 printf("%s [OPTIONS...] {COMMAND}\n\n"
3000 "Send control commands to the init daemon.\n\n"
3001 " --help Show this help\n"
3002 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
3004 " 0 Power-off the machine\n"
3005 " 6 Reboot the machine\n"
3006 " 2, 3, 4, 5 Start runlevelX.target unit\n"
3007 " 1, s, S Enter rescue mode\n"
3008 " q, Q Reload init daemon configuration\n"
3009 " u, U Reexecute init daemon\n",
3010 program_invocation_short_name);
3015 static int runlevel_help(void) {
3017 printf("%s [OPTIONS...]\n\n"
3018 "Prints the previous and current runlevel of the init system.\n\n"
3019 " --help Show this help\n",
3020 program_invocation_short_name);
3025 static int systemctl_parse_argv(int argc, char *argv[]) {
3038 static const struct option options[] = {
3039 { "help", no_argument, NULL, 'h' },
3040 { "type", required_argument, NULL, 't' },
3041 { "property", required_argument, NULL, 'p' },
3042 { "all", no_argument, NULL, 'a' },
3043 { "full", no_argument, NULL, ARG_FULL },
3044 { "fail", no_argument, NULL, ARG_FAIL },
3045 { "session", no_argument, NULL, ARG_SESSION },
3046 { "system", no_argument, NULL, ARG_SYSTEM },
3047 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
3048 { "no-wall", no_argument, NULL, ARG_NO_WALL },
3049 { "quiet", no_argument, NULL, 'q' },
3050 { "order", no_argument, NULL, ARG_ORDER },
3051 { "require", no_argument, NULL, ARG_REQUIRE },
3052 { NULL, 0, NULL, 0 }
3060 while ((c = getopt_long(argc, argv, "ht:p:aq", options, NULL)) >= 0) {
3073 arg_property = optarg;
3075 /* If the user asked for a particular
3076 * property, show it to him, even if it is
3094 arg_session = false;
3098 arg_no_block = true;
3106 arg_dot = DOT_ORDER;
3110 arg_dot = DOT_REQUIRE;
3125 log_error("Unknown option code %c", c);
3133 static int halt_parse_argv(int argc, char *argv[]) {
3142 static const struct option options[] = {
3143 { "help", no_argument, NULL, ARG_HELP },
3144 { "halt", no_argument, NULL, ARG_HALT },
3145 { "poweroff", no_argument, NULL, 'p' },
3146 { "reboot", no_argument, NULL, ARG_REBOOT },
3147 { "force", no_argument, NULL, 'f' },
3148 { "wtmp-only", no_argument, NULL, 'w' },
3149 { "no-wtmp", no_argument, NULL, 'd' },
3150 { "no-sync", no_argument, NULL, 'n' },
3151 { "no-wall", no_argument, NULL, ARG_NO_WALL },
3152 { NULL, 0, NULL, 0 }
3160 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
3161 if (runlevel == '0' || runlevel == '6')
3162 arg_immediate = true;
3164 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
3172 arg_action = ACTION_HALT;
3176 arg_action = ACTION_POWEROFF;
3180 arg_action = ACTION_REBOOT;
3184 arg_immediate = true;
3205 /* Compatibility nops */
3212 log_error("Unknown option code %c", c);
3217 if (optind < argc) {
3218 log_error("Too many arguments.");
3225 static int shutdown_parse_argv(int argc, char *argv[]) {
3232 static const struct option options[] = {
3233 { "help", no_argument, NULL, ARG_HELP },
3234 { "halt", no_argument, NULL, 'H' },
3235 { "poweroff", no_argument, NULL, 'P' },
3236 { "reboot", no_argument, NULL, 'r' },
3237 { "no-wall", no_argument, NULL, ARG_NO_WALL },
3238 { NULL, 0, NULL, 0 }
3246 while ((c = getopt_long(argc, argv, "HPrhkt:a", options, NULL)) >= 0) {
3254 arg_action = ACTION_HALT;
3258 arg_action = ACTION_POWEROFF;
3262 arg_action = ACTION_REBOOT;
3266 if (arg_action != ACTION_HALT)
3267 arg_action = ACTION_POWEROFF;
3280 /* Compatibility nops */
3287 log_error("Unknown option code %c", c);
3292 if (argc > optind && !streq(argv[optind], "now"))
3293 log_warning("First argument '%s' isn't 'now'. Ignoring.", argv[optind]);
3295 /* We ignore the time argument */
3296 if (argc > optind + 1)
3297 arg_wall = argv + optind + 1;
3304 static int telinit_parse_argv(int argc, char *argv[]) {
3311 static const struct option options[] = {
3312 { "help", no_argument, NULL, ARG_HELP },
3313 { "no-wall", no_argument, NULL, ARG_NO_WALL },
3314 { NULL, 0, NULL, 0 }
3317 static const struct {
3321 { '0', ACTION_POWEROFF },
3322 { '6', ACTION_REBOOT },
3323 { '1', ACTION_RESCUE },
3324 { '2', ACTION_RUNLEVEL2 },
3325 { '3', ACTION_RUNLEVEL3 },
3326 { '4', ACTION_RUNLEVEL4 },
3327 { '5', ACTION_RUNLEVEL5 },
3328 { 's', ACTION_RESCUE },
3329 { 'S', ACTION_RESCUE },
3330 { 'q', ACTION_RELOAD },
3331 { 'Q', ACTION_RELOAD },
3332 { 'u', ACTION_REEXEC },
3333 { 'U', ACTION_REEXEC }
3342 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
3357 log_error("Unknown option code %c", c);
3362 if (optind >= argc) {
3367 if (optind + 1 < argc) {
3368 log_error("Too many arguments.");
3372 if (strlen(argv[optind]) != 1) {
3373 log_error("Expected single character argument.");
3377 for (i = 0; i < ELEMENTSOF(table); i++)
3378 if (table[i].from == argv[optind][0])
3381 if (i >= ELEMENTSOF(table)) {
3382 log_error("Unknown command %s.", argv[optind]);
3386 arg_action = table[i].to;
3393 static int runlevel_parse_argv(int argc, char *argv[]) {
3399 static const struct option options[] = {
3400 { "help", no_argument, NULL, ARG_HELP },
3401 { NULL, 0, NULL, 0 }
3409 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
3420 log_error("Unknown option code %c", c);
3425 if (optind < argc) {
3426 log_error("Too many arguments.");
3433 static int parse_argv(int argc, char *argv[]) {
3437 if (program_invocation_short_name) {
3439 if (strstr(program_invocation_short_name, "halt")) {
3440 arg_action = ACTION_HALT;
3441 return halt_parse_argv(argc, argv);
3442 } else if (strstr(program_invocation_short_name, "poweroff")) {
3443 arg_action = ACTION_POWEROFF;
3444 return halt_parse_argv(argc, argv);
3445 } else if (strstr(program_invocation_short_name, "reboot")) {
3446 arg_action = ACTION_REBOOT;
3447 return halt_parse_argv(argc, argv);
3448 } else if (strstr(program_invocation_short_name, "shutdown")) {
3449 arg_action = ACTION_POWEROFF;
3450 return shutdown_parse_argv(argc, argv);
3451 } else if (strstr(program_invocation_short_name, "init")) {
3452 arg_action = ACTION_INVALID;
3453 return telinit_parse_argv(argc, argv);
3454 } else if (strstr(program_invocation_short_name, "runlevel")) {
3455 arg_action = ACTION_RUNLEVEL;
3456 return runlevel_parse_argv(argc, argv);
3460 arg_action = ACTION_SYSTEMCTL;
3461 return systemctl_parse_argv(argc, argv);
3464 static int action_to_runlevel(void) {
3466 static const char table[_ACTION_MAX] = {
3467 [ACTION_HALT] = '0',
3468 [ACTION_POWEROFF] = '0',
3469 [ACTION_REBOOT] = '6',
3470 [ACTION_RUNLEVEL2] = '2',
3471 [ACTION_RUNLEVEL3] = '3',
3472 [ACTION_RUNLEVEL4] = '4',
3473 [ACTION_RUNLEVEL5] = '5',
3474 [ACTION_RESCUE] = '1'
3477 assert(arg_action < _ACTION_MAX);
3479 return table[arg_action];
3482 static int talk_upstart(void) {
3483 DBusMessage *m = NULL, *reply = NULL;
3485 int previous, rl, r;
3487 env1_buf[] = "RUNLEVEL=X",
3488 env2_buf[] = "PREVLEVEL=X";
3489 char *env1 = env1_buf, *env2 = env2_buf;
3490 const char *emit = "runlevel";
3491 dbus_bool_t b_false = FALSE;
3492 DBusMessageIter iter, sub;
3493 DBusConnection *bus;
3495 dbus_error_init(&error);
3497 if (!(rl = action_to_runlevel()))
3500 if (utmp_get_runlevel(&previous, NULL) < 0)
3503 if (!(bus = dbus_connection_open_private("unix:abstract=/com/ubuntu/upstart", &error))) {
3504 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
3509 log_error("Failed to connect to Upstart bus: %s", error.message);
3514 if ((r = bus_check_peercred(bus)) < 0) {
3515 log_error("Failed to verify owner of bus.");
3519 if (!(m = dbus_message_new_method_call(
3520 "com.ubuntu.Upstart",
3521 "/com/ubuntu/Upstart",
3522 "com.ubuntu.Upstart0_6",
3525 log_error("Could not allocate message.");
3530 dbus_message_iter_init_append(m, &iter);
3532 env1_buf[sizeof(env1_buf)-2] = rl;
3533 env2_buf[sizeof(env2_buf)-2] = previous;
3535 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
3536 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
3537 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
3538 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
3539 !dbus_message_iter_close_container(&iter, &sub) ||
3540 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
3541 log_error("Could not append arguments to message.");
3546 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3548 if (error_is_no_service(&error)) {
3553 log_error("Failed to issue method call: %s", error.message);
3562 dbus_message_unref(m);
3565 dbus_message_unref(reply);
3568 dbus_connection_close(bus);
3569 dbus_connection_unref(bus);
3572 dbus_error_free(&error);
3577 static int talk_initctl(void) {
3578 struct init_request request;
3582 if (!(rl = action_to_runlevel()))
3586 request.magic = INIT_MAGIC;
3587 request.sleeptime = 0;
3588 request.cmd = INIT_CMD_RUNLVL;
3589 request.runlevel = rl;
3591 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
3593 if (errno == ENOENT)
3596 log_error("Failed to open "INIT_FIFO": %m");
3601 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
3602 close_nointr_nofail(fd);
3605 log_error("Failed to write to "INIT_FIFO": %m");
3606 return errno ? -errno : -EIO;
3612 static int systemctl_main(DBusConnection *bus, int argc, char *argv[]) {
3614 static const struct {
3622 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
3624 { "list-units", LESS, 1, list_units },
3625 { "list-jobs", EQUAL, 1, list_jobs },
3626 { "clear-jobs", EQUAL, 1, clear_jobs },
3627 { "load", MORE, 2, load_unit },
3628 { "cancel", MORE, 2, cancel_job },
3629 { "start", MORE, 2, start_unit },
3630 { "stop", MORE, 2, start_unit },
3631 { "reload", MORE, 2, start_unit },
3632 { "restart", MORE, 2, start_unit },
3633 { "try-restart", MORE, 2, start_unit },
3634 { "reload-or-restart", MORE, 2, start_unit },
3635 { "reload-or-try-restart", MORE, 2, start_unit },
3636 { "isolate", EQUAL, 2, start_unit },
3637 { "check", MORE, 2, check_unit },
3638 { "show", MORE, 1, show },
3639 { "status", MORE, 2, show },
3640 { "monitor", EQUAL, 1, monitor },
3641 { "dump", EQUAL, 1, dump },
3642 { "dot", EQUAL, 1, dot },
3643 { "snapshot", LESS, 2, snapshot },
3644 { "delete", MORE, 2, delete_snapshot },
3645 { "daemon-reload", EQUAL, 1, clear_jobs },
3646 { "daemon-reexec", EQUAL, 1, clear_jobs },
3647 { "daemon-exit", EQUAL, 1, clear_jobs },
3648 { "show-environment", EQUAL, 1, show_enviroment },
3649 { "set-environment", MORE, 2, set_environment },
3650 { "unset-environment", MORE, 2, set_environment },
3651 { "halt", EQUAL, 1, start_special },
3652 { "poweroff", EQUAL, 1, start_special },
3653 { "reboot", EQUAL, 1, start_special },
3654 { "default", EQUAL, 1, start_special },
3655 { "rescue", EQUAL, 1, start_special },
3656 { "emergency", EQUAL, 1, start_special },
3657 { "reset-maintenance", MORE, 1, reset_maintenance },
3667 left = argc - optind;
3670 /* Special rule: no arguments means "list-units" */
3673 if (streq(argv[optind], "help")) {
3678 for (i = 0; i < ELEMENTSOF(verbs); i++)
3679 if (streq(argv[optind], verbs[i].verb))
3682 if (i >= ELEMENTSOF(verbs)) {
3683 log_error("Unknown operation %s", argv[optind]);
3688 switch (verbs[i].argc_cmp) {
3691 if (left != verbs[i].argc) {
3692 log_error("Invalid number of arguments.");
3699 if (left < verbs[i].argc) {
3700 log_error("Too few arguments.");
3707 if (left > verbs[i].argc) {
3708 log_error("Too many arguments.");
3715 assert_not_reached("Unknown comparison operator.");
3718 return verbs[i].dispatch(bus, argv + optind, left);
3721 static int reload_with_fallback(DBusConnection *bus) {
3725 /* First, try systemd via D-Bus. */
3726 if ((r = clear_jobs(bus, NULL, 0)) > 0)
3730 /* Nothing else worked, so let's try signals */
3731 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
3733 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
3734 log_error("kill() failed: %m");
3741 static int start_with_fallback(DBusConnection *bus) {
3746 /* First, try systemd via D-Bus. */
3747 if ((r = start_unit(bus, NULL, 0)) > 0)
3750 /* Hmm, talking to systemd via D-Bus didn't work. Then
3751 * let's try to talk to Upstart via D-Bus. */
3752 if ((r = talk_upstart()) > 0)
3756 /* Nothing else worked, so let's try
3758 if ((r = talk_initctl()) != 0)
3761 log_error("Failed to talk to init daemon.");
3765 warn_wall(arg_action);
3769 static int halt_main(DBusConnection *bus) {
3772 if (geteuid() != 0) {
3773 log_error("Must to be root.");
3777 if (!arg_dry && !arg_immediate)
3778 return start_with_fallback(bus);
3781 if ((r = utmp_put_shutdown(0)) < 0)
3782 log_warning("Failed to write utmp record: %s", strerror(-r));
3790 /* Make sure C-A-D is handled by the kernel from this
3792 reboot(RB_ENABLE_CAD);
3794 switch (arg_action) {
3797 log_info("Halting");
3798 reboot(RB_HALT_SYSTEM);
3801 case ACTION_POWEROFF:
3802 log_info("Powering off");
3803 reboot(RB_POWER_OFF);
3807 log_info("Rebooting");
3808 reboot(RB_AUTOBOOT);
3812 assert_not_reached("Unknown halt action.");
3815 /* We should never reach this. */
3819 static int runlevel_main(void) {
3820 int r, runlevel, previous;
3822 if ((r = utmp_get_runlevel(&runlevel, &previous)) < 0) {
3828 previous <= 0 ? 'N' : previous,
3829 runlevel <= 0 ? 'N' : runlevel);
3834 int main(int argc, char*argv[]) {
3836 DBusConnection *bus = NULL;
3839 dbus_error_init(&error);
3841 log_parse_environment();
3843 if ((r = parse_argv(argc, argv)) < 0)
3850 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
3851 * let's shortcut this */
3852 if (arg_action == ACTION_RUNLEVEL) {
3853 retval = runlevel_main() < 0;
3857 bus_connect(arg_session ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &bus, &private_bus, &error);
3859 switch (arg_action) {
3861 case ACTION_SYSTEMCTL: {
3864 log_error("Failed to get D-Bus connection: %s", error.message);
3868 retval = systemctl_main(bus, argc, argv) < 0;
3873 case ACTION_POWEROFF:
3875 retval = halt_main(bus) < 0;
3878 case ACTION_RUNLEVEL2:
3879 case ACTION_RUNLEVEL3:
3880 case ACTION_RUNLEVEL4:
3881 case ACTION_RUNLEVEL5:
3883 case ACTION_EMERGENCY:
3884 case ACTION_DEFAULT:
3885 retval = start_with_fallback(bus) < 0;
3890 retval = reload_with_fallback(bus) < 0;
3893 case ACTION_INVALID:
3894 case ACTION_RUNLEVEL:
3896 assert_not_reached("Unknown action");
3902 dbus_connection_close(bus);
3903 dbus_connection_unref(bus);
3906 dbus_error_free(&error);