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_wall = NULL;
79 } arg_action = ACTION_SYSTEMCTL;
86 static bool private_bus = false;
88 static bool error_is_no_service(DBusError *error) {
92 if (!dbus_error_is_set(error))
95 if (dbus_error_has_name(error, DBUS_ERROR_NAME_HAS_NO_OWNER))
98 if (dbus_error_has_name(error, DBUS_ERROR_SERVICE_UNKNOWN))
101 return startswith(error->name, "org.freedesktop.DBus.Error.Spawn.");
104 static int bus_iter_get_basic_and_next(DBusMessageIter *iter, int type, void *data, bool next) {
109 if (dbus_message_iter_get_arg_type(iter) != type)
112 dbus_message_iter_get_basic(iter, data);
114 if (!dbus_message_iter_next(iter) != !next)
120 static void warn_wall(enum action action) {
121 static const char *table[_ACTION_MAX] = {
122 [ACTION_HALT] = "The system is going down for system halt NOW!",
123 [ACTION_REBOOT] = "The system is going down for reboot NOW!",
124 [ACTION_POWEROFF] = "The system is going down for power-off NOW!",
125 [ACTION_RESCUE] = "The system is going down to rescue mode NOW!",
126 [ACTION_EMERGENCY] = "The system is going down to emergency mode NOW!"
135 if (!(p = strv_join(arg_wall, " "))) {
136 log_error("Failed to join strings.");
152 utmp_wall(table[action]);
155 static int list_units(DBusConnection *bus, char **args, unsigned n) {
156 DBusMessage *m = NULL, *reply = NULL;
159 DBusMessageIter iter, sub, sub2;
162 dbus_error_init(&error);
166 if (!(m = dbus_message_new_method_call(
167 "org.freedesktop.systemd1",
168 "/org/freedesktop/systemd1",
169 "org.freedesktop.systemd1.Manager",
171 log_error("Could not allocate message.");
175 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
176 log_error("Failed to issue method call: %s", error.message);
181 if (!dbus_message_iter_init(reply, &iter) ||
182 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
183 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
184 log_error("Failed to parse reply.");
189 dbus_message_iter_recurse(&iter, &sub);
191 printf("%-45s %-6s %-12s %-12s %-15s %s\n", "UNIT", "LOAD", "ACTIVE", "SUB", "JOB", "DESCRIPTION");
193 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
194 const char *id, *description, *load_state, *active_state, *sub_state, *unit_path, *job_type, *job_path, *dot;
197 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
198 log_error("Failed to parse reply.");
203 dbus_message_iter_recurse(&sub, &sub2);
205 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
206 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
207 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
208 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
209 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
210 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, true) < 0 ||
211 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &job_id, true) < 0 ||
212 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &job_type, true) < 0 ||
213 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, false) < 0) {
214 log_error("Failed to parse reply.");
219 if ((!arg_type || ((dot = strrchr(id, '.')) &&
220 streq(dot+1, arg_type))) &&
221 (arg_all || !streq(active_state, "inactive") || job_id > 0)) {
225 if (streq(active_state, "maintenance"))
226 fputs(ANSI_HIGHLIGHT_ON, stdout);
228 printf("%-45s %-6s %-12s %-12s%n", id, load_state, active_state, sub_state, &a);
231 printf(" => %-12s%n", job_type, &b);
235 if (a + b + 2 < columns()) {
239 printf(" %.*s", columns() - a - b - 2, description);
242 if (streq(active_state, "maintenance"))
243 fputs(ANSI_HIGHLIGHT_OFF, stdout);
249 dbus_message_iter_next(&sub);
252 printf("\nLOAD = Load State, reflects whether the unit configuration was properly loaded.\n"
253 "ACTIVE = Active State, the high-level unit activation state, i.e. generalization of the substate.\n"
254 "SUB = Substate, the low-level unit activation state, possible values depend on unit type.\n"
255 "JOB = Job, shows scheduled jobs for the unit.\n");
258 printf("\n%u units listed.\n", k);
260 printf("\n%u units listed. Pass --all to see inactive units, too.\n", k);
266 dbus_message_unref(m);
269 dbus_message_unref(reply);
271 dbus_error_free(&error);
276 static int dot_one_property(const char *name, const char *prop, DBusMessageIter *iter) {
277 static const char * const colors[] = {
278 "Requires", "[color=\"black\"]",
279 "RequiresOverridable", "[color=\"black\"]",
280 "Requisite", "[color=\"darkblue\"]",
281 "RequisiteOverridable", "[color=\"darkblue\"]",
282 "Wants", "[color=\"darkgrey\"]",
283 "Conflicts", "[color=\"red\"]",
284 "After", "[color=\"green\"]"
287 const char *c = NULL;
294 for (i = 0; i < ELEMENTSOF(colors); i += 2)
295 if (streq(colors[i], prop)) {
303 if (arg_dot != DOT_ALL)
304 if ((arg_dot == DOT_ORDER) != streq(prop, "After"))
307 switch (dbus_message_iter_get_arg_type(iter)) {
309 case DBUS_TYPE_ARRAY:
311 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
314 dbus_message_iter_recurse(iter, &sub);
316 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
319 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
320 dbus_message_iter_get_basic(&sub, &s);
321 printf("\t\"%s\"->\"%s\" %s;\n", name, s, c);
323 dbus_message_iter_next(&sub);
333 static int dot_one(DBusConnection *bus, const char *name, const char *path) {
334 DBusMessage *m = NULL, *reply = NULL;
335 const char *interface = "org.freedesktop.systemd1.Unit";
338 DBusMessageIter iter, sub, sub2, sub3;
343 dbus_error_init(&error);
345 if (!(m = dbus_message_new_method_call(
346 "org.freedesktop.systemd1",
348 "org.freedesktop.DBus.Properties",
350 log_error("Could not allocate message.");
355 if (!dbus_message_append_args(m,
356 DBUS_TYPE_STRING, &interface,
357 DBUS_TYPE_INVALID)) {
358 log_error("Could not append arguments to message.");
363 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
364 log_error("Failed to issue method call: %s", error.message);
369 if (!dbus_message_iter_init(reply, &iter) ||
370 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
371 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
372 log_error("Failed to parse reply.");
377 dbus_message_iter_recurse(&iter, &sub);
379 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
382 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
383 log_error("Failed to parse reply.");
388 dbus_message_iter_recurse(&sub, &sub2);
390 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &prop, true) < 0) {
391 log_error("Failed to parse reply.");
396 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
397 log_error("Failed to parse reply.");
402 dbus_message_iter_recurse(&sub2, &sub3);
404 if (dot_one_property(name, prop, &sub3)) {
405 log_error("Failed to parse reply.");
410 dbus_message_iter_next(&sub);
415 dbus_message_unref(m);
418 dbus_message_unref(reply);
420 dbus_error_free(&error);
425 static int dot(DBusConnection *bus, char **args, unsigned n) {
426 DBusMessage *m = NULL, *reply = NULL;
429 DBusMessageIter iter, sub, sub2;
431 dbus_error_init(&error);
435 if (!(m = dbus_message_new_method_call(
436 "org.freedesktop.systemd1",
437 "/org/freedesktop/systemd1",
438 "org.freedesktop.systemd1.Manager",
440 log_error("Could not allocate message.");
444 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
445 log_error("Failed to issue method call: %s", error.message);
450 if (!dbus_message_iter_init(reply, &iter) ||
451 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
452 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
453 log_error("Failed to parse reply.");
458 printf("digraph systemd {\n");
460 dbus_message_iter_recurse(&iter, &sub);
461 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
462 const char *id, *description, *load_state, *active_state, *sub_state, *unit_path;
464 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
465 log_error("Failed to parse reply.");
470 dbus_message_iter_recurse(&sub, &sub2);
472 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
473 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
474 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
475 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
476 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
477 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, true) < 0) {
478 log_error("Failed to parse reply.");
483 if ((r = dot_one(bus, id, unit_path)) < 0)
486 /* printf("\t\"%s\";\n", id); */
487 dbus_message_iter_next(&sub);
492 log_info(" Color legend: black = Requires\n"
493 " dark blue = Requisite\n"
494 " dark grey = Wants\n"
498 if (isatty(fileno(stdout)))
499 log_notice("-- You probably want to process this output with graphviz' dot tool.\n"
500 "-- Try a shell pipeline like 'systemctl dot | dot -Tsvg > systemd.svg'!\n");
506 dbus_message_unref(m);
509 dbus_message_unref(reply);
511 dbus_error_free(&error);
516 static int list_jobs(DBusConnection *bus, char **args, unsigned n) {
517 DBusMessage *m = NULL, *reply = NULL;
520 DBusMessageIter iter, sub, sub2;
523 dbus_error_init(&error);
527 if (!(m = dbus_message_new_method_call(
528 "org.freedesktop.systemd1",
529 "/org/freedesktop/systemd1",
530 "org.freedesktop.systemd1.Manager",
532 log_error("Could not allocate message.");
536 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
537 log_error("Failed to issue method call: %s", error.message);
542 if (!dbus_message_iter_init(reply, &iter) ||
543 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
544 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
545 log_error("Failed to parse reply.");
550 dbus_message_iter_recurse(&iter, &sub);
552 printf("%4s %-45s %-17s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
554 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
555 const char *name, *type, *state, *job_path, *unit_path;
558 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
559 log_error("Failed to parse reply.");
564 dbus_message_iter_recurse(&sub, &sub2);
566 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
567 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
568 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
569 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
570 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
571 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
572 log_error("Failed to parse reply.");
577 printf("%4u %-45s %-17s %-7s\n", id, name, type, state);
580 dbus_message_iter_next(&sub);
583 printf("\n%u jobs listed.\n", k);
588 dbus_message_unref(m);
591 dbus_message_unref(reply);
593 dbus_error_free(&error);
598 static int load_unit(DBusConnection *bus, char **args, unsigned n) {
599 DBusMessage *m = NULL, *reply = NULL;
604 dbus_error_init(&error);
609 for (i = 1; i < n; i++) {
611 if (!(m = dbus_message_new_method_call(
612 "org.freedesktop.systemd1",
613 "/org/freedesktop/systemd1",
614 "org.freedesktop.systemd1.Manager",
616 log_error("Could not allocate message.");
621 if (!dbus_message_append_args(m,
622 DBUS_TYPE_STRING, &args[i],
623 DBUS_TYPE_INVALID)) {
624 log_error("Could not append arguments to message.");
629 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
630 log_error("Failed to issue method call: %s", error.message);
635 dbus_message_unref(m);
636 dbus_message_unref(reply);
645 dbus_message_unref(m);
648 dbus_message_unref(reply);
650 dbus_error_free(&error);
655 static int cancel_job(DBusConnection *bus, char **args, unsigned n) {
656 DBusMessage *m = NULL, *reply = NULL;
661 dbus_error_init(&error);
666 for (i = 1; i < n; i++) {
670 if (!(m = dbus_message_new_method_call(
671 "org.freedesktop.systemd1",
672 "/org/freedesktop/systemd1",
673 "org.freedesktop.systemd1.Manager",
675 log_error("Could not allocate message.");
680 if ((r = safe_atou(args[i], &id)) < 0) {
681 log_error("Failed to parse job id: %s", strerror(-r));
685 assert_cc(sizeof(uint32_t) == sizeof(id));
686 if (!dbus_message_append_args(m,
687 DBUS_TYPE_UINT32, &id,
688 DBUS_TYPE_INVALID)) {
689 log_error("Could not append arguments to message.");
694 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
695 log_error("Failed to issue method call: %s", error.message);
700 if (!dbus_message_get_args(reply, &error,
701 DBUS_TYPE_OBJECT_PATH, &path,
702 DBUS_TYPE_INVALID)) {
703 log_error("Failed to parse reply: %s", error.message);
708 dbus_message_unref(m);
709 if (!(m = dbus_message_new_method_call(
710 "org.freedesktop.systemd1",
712 "org.freedesktop.systemd1.Job",
714 log_error("Could not allocate message.");
719 dbus_message_unref(reply);
720 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
721 log_error("Failed to issue method call: %s", error.message);
726 dbus_message_unref(m);
727 dbus_message_unref(reply);
735 dbus_message_unref(m);
738 dbus_message_unref(reply);
740 dbus_error_free(&error);
745 static bool unit_need_daemon_reload(DBusConnection *bus, const char *unit) {
746 DBusMessage *m, *reply;
747 dbus_bool_t b = FALSE;
748 DBusMessageIter iter, sub;
750 *interface = "org.freedesktop.systemd1.Unit",
751 *property = "NeedDaemonReload",
754 /* We ignore all errors here, since this is used to show a warning only */
756 if (!(m = dbus_message_new_method_call(
757 "org.freedesktop.systemd1",
758 "/org/freedesktop/systemd1",
759 "org.freedesktop.systemd1.Manager",
763 if (!dbus_message_append_args(m,
764 DBUS_TYPE_STRING, &unit,
768 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
771 if (!dbus_message_get_args(reply, NULL,
772 DBUS_TYPE_OBJECT_PATH, &path,
776 dbus_message_unref(m);
777 if (!(m = dbus_message_new_method_call(
778 "org.freedesktop.systemd1",
780 "org.freedesktop.DBus.Properties",
784 if (!dbus_message_append_args(m,
785 DBUS_TYPE_STRING, &interface,
786 DBUS_TYPE_STRING, &property,
787 DBUS_TYPE_INVALID)) {
791 dbus_message_unref(reply);
792 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
795 if (!dbus_message_iter_init(reply, &iter) ||
796 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
799 dbus_message_iter_recurse(&iter, &sub);
801 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_BOOLEAN)
804 dbus_message_iter_get_basic(&sub, &b);
808 dbus_message_unref(m);
811 dbus_message_unref(reply);
816 typedef struct WaitData {
821 static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
829 dbus_error_init(&error);
831 log_debug("Got D-Bus request: %s.%s() on %s",
832 dbus_message_get_interface(message),
833 dbus_message_get_member(message),
834 dbus_message_get_path(message));
836 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
837 log_error("Warning! D-Bus connection terminated.");
838 dbus_connection_close(connection);
840 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
843 dbus_bool_t success = true;
845 if (!dbus_message_get_args(message, &error,
846 DBUS_TYPE_UINT32, &id,
847 DBUS_TYPE_OBJECT_PATH, &path,
848 DBUS_TYPE_BOOLEAN, &success,
850 log_error("Failed to parse message: %s", error.message);
854 if ((p = set_remove(d->set, (char*) path)))
862 dbus_error_free(&error);
863 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
866 static int enable_wait_for_jobs(DBusConnection *bus) {
874 dbus_error_init(&error);
875 dbus_bus_add_match(bus,
877 "sender='org.freedesktop.systemd1',"
878 "interface='org.freedesktop.systemd1.Manager',"
879 "member='JobRemoved',"
880 "path='/org/freedesktop/systemd1'",
883 if (dbus_error_is_set(&error)) {
884 log_error("Failed to add match: %s", error.message);
885 dbus_error_free(&error);
889 /* This is slightly dirty, since we don't undo the match registrations. */
893 static int wait_for_jobs(DBusConnection *bus, Set *s) {
904 if (!dbus_connection_add_filter(bus, wait_filter, &d, NULL)) {
905 log_error("Failed to add filter.");
910 while (!set_isempty(s) &&
911 dbus_connection_read_write_dispatch(bus, -1))
914 if (!arg_quiet && d.failed)
915 log_error("Job failed, see logs for details.");
917 r = d.failed ? -EIO : 0;
920 /* This is slightly dirty, since we don't undo the filter registration. */
925 static int start_unit_one(
932 DBusMessage *m = NULL, *reply = NULL;
941 assert(arg_no_block || s);
943 dbus_error_init(&error);
945 if (!(m = dbus_message_new_method_call(
946 "org.freedesktop.systemd1",
947 "/org/freedesktop/systemd1",
948 "org.freedesktop.systemd1.Manager",
950 log_error("Could not allocate message.");
955 if (!dbus_message_append_args(m,
956 DBUS_TYPE_STRING, &name,
957 DBUS_TYPE_STRING, &mode,
958 DBUS_TYPE_INVALID)) {
959 log_error("Could not append arguments to message.");
964 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
966 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
967 /* There's always a fallback possible for
973 log_error("Failed to issue method call: %s", error.message);
978 if (!dbus_message_get_args(reply, &error,
979 DBUS_TYPE_OBJECT_PATH, &path,
980 DBUS_TYPE_INVALID)) {
981 log_error("Failed to parse reply: %s", error.message);
986 if (unit_need_daemon_reload(bus, name))
987 log_warning("Unit file of created job changed on disk, 'systemctl %s daemon-reload' recommended.",
988 arg_session ? "--session" : "--system");
993 if (!(p = strdup(path))) {
994 log_error("Failed to duplicate path.");
999 if ((r = set_put(s, p)) < 0) {
1001 log_error("Failed to add path to set.");
1010 dbus_message_unref(m);
1013 dbus_message_unref(reply);
1015 dbus_error_free(&error);
1020 static enum action verb_to_action(const char *verb) {
1021 if (streq(verb, "halt"))
1023 else if (streq(verb, "poweroff"))
1024 return ACTION_POWEROFF;
1025 else if (streq(verb, "reboot"))
1026 return ACTION_REBOOT;
1027 else if (streq(verb, "rescue"))
1028 return ACTION_RESCUE;
1029 else if (streq(verb, "emergency"))
1030 return ACTION_EMERGENCY;
1031 else if (streq(verb, "default"))
1032 return ACTION_DEFAULT;
1034 return ACTION_INVALID;
1037 static int start_unit(DBusConnection *bus, char **args, unsigned n) {
1039 static const char * const table[_ACTION_MAX] = {
1040 [ACTION_HALT] = SPECIAL_HALT_TARGET,
1041 [ACTION_POWEROFF] = SPECIAL_POWEROFF_TARGET,
1042 [ACTION_REBOOT] = SPECIAL_REBOOT_TARGET,
1043 [ACTION_RUNLEVEL2] = SPECIAL_RUNLEVEL2_TARGET,
1044 [ACTION_RUNLEVEL3] = SPECIAL_RUNLEVEL3_TARGET,
1045 [ACTION_RUNLEVEL4] = SPECIAL_RUNLEVEL4_TARGET,
1046 [ACTION_RUNLEVEL5] = SPECIAL_RUNLEVEL5_TARGET,
1047 [ACTION_RESCUE] = SPECIAL_RESCUE_TARGET,
1048 [ACTION_EMERGENCY] = SPECIAL_EMERGENCY_TARGET,
1049 [ACTION_DEFAULT] = SPECIAL_DEFAULT_TARGET
1054 const char *method, *mode, *one_name;
1059 if (arg_action == ACTION_SYSTEMCTL) {
1061 streq(args[0], "stop") ? "StopUnit" :
1062 streq(args[0], "reload") ? "ReloadUnit" :
1063 streq(args[0], "restart") ? "RestartUnit" :
1064 streq(args[0], "try-restart") ? "TryRestartUnit" :
1065 streq(args[0], "reload-or-restart") ? "ReloadOrRestartUnit" :
1066 streq(args[0], "reload-or-try-restart") ? "ReloadOrTryRestartUnit" :
1070 (streq(args[0], "isolate") ||
1071 streq(args[0], "rescue") ||
1072 streq(args[0], "emergency")) ? "isolate" :
1076 one_name = table[verb_to_action(args[0])];
1079 assert(arg_action < ELEMENTSOF(table));
1080 assert(table[arg_action]);
1082 method = "StartUnit";
1084 mode = (arg_action == ACTION_EMERGENCY ||
1085 arg_action == ACTION_RESCUE) ? "isolate" : "replace";
1087 one_name = table[arg_action];
1090 if (!arg_no_block) {
1091 if ((r = enable_wait_for_jobs(bus)) < 0) {
1092 log_error("Could not watch jobs: %s", strerror(-r));
1096 if (!(s = set_new(string_hash_func, string_compare_func))) {
1097 log_error("Failed to allocate set.");
1106 if ((r = start_unit_one(bus, method, one_name, mode, s)) <= 0)
1109 for (i = 1; i < n; i++)
1110 if ((r = start_unit_one(bus, method, args[i], mode, s)) < 0)
1115 r = wait_for_jobs(bus, s);
1124 static int start_special(DBusConnection *bus, char **args, unsigned n) {
1130 r = start_unit(bus, args, n);
1133 warn_wall(verb_to_action(args[0]));
1138 static int check_unit(DBusConnection *bus, char **args, unsigned n) {
1139 DBusMessage *m = NULL, *reply = NULL;
1141 *interface = "org.freedesktop.systemd1.Unit",
1142 *property = "ActiveState";
1143 int r = -EADDRNOTAVAIL;
1150 dbus_error_init(&error);
1152 for (i = 1; i < n; i++) {
1153 const char *path = NULL;
1155 DBusMessageIter iter, sub;
1157 if (!(m = dbus_message_new_method_call(
1158 "org.freedesktop.systemd1",
1159 "/org/freedesktop/systemd1",
1160 "org.freedesktop.systemd1.Manager",
1162 log_error("Could not allocate message.");
1167 if (!dbus_message_append_args(m,
1168 DBUS_TYPE_STRING, &args[i],
1169 DBUS_TYPE_INVALID)) {
1170 log_error("Could not append arguments to message.");
1175 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1177 /* Hmm, cannot figure out anything about this unit... */
1181 dbus_error_free(&error);
1185 if (!dbus_message_get_args(reply, &error,
1186 DBUS_TYPE_OBJECT_PATH, &path,
1187 DBUS_TYPE_INVALID)) {
1188 log_error("Failed to parse reply: %s", error.message);
1193 dbus_message_unref(m);
1194 if (!(m = dbus_message_new_method_call(
1195 "org.freedesktop.systemd1",
1197 "org.freedesktop.DBus.Properties",
1199 log_error("Could not allocate message.");
1204 if (!dbus_message_append_args(m,
1205 DBUS_TYPE_STRING, &interface,
1206 DBUS_TYPE_STRING, &property,
1207 DBUS_TYPE_INVALID)) {
1208 log_error("Could not append arguments to message.");
1213 dbus_message_unref(reply);
1214 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1215 log_error("Failed to issue method call: %s", error.message);
1220 if (!dbus_message_iter_init(reply, &iter) ||
1221 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1222 log_error("Failed to parse reply.");
1227 dbus_message_iter_recurse(&iter, &sub);
1229 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1230 log_error("Failed to parse reply.");
1235 dbus_message_iter_get_basic(&sub, &state);
1240 if (streq(state, "active") || startswith(state, "reloading"))
1243 dbus_message_unref(m);
1244 dbus_message_unref(reply);
1250 dbus_message_unref(m);
1253 dbus_message_unref(reply);
1255 dbus_error_free(&error);
1260 typedef struct ExecStatusInfo {
1266 usec_t start_timestamp;
1267 usec_t exit_timestamp;
1272 LIST_FIELDS(struct ExecStatusInfo, exec);
1275 static void exec_status_info_free(ExecStatusInfo *i) {
1283 static int exec_status_info_deserialize(DBusMessageIter *sub, ExecStatusInfo *i) {
1284 uint64_t start_timestamp, exit_timestamp;
1285 DBusMessageIter sub2, sub3;
1289 int32_t code, status;
1295 if (dbus_message_iter_get_arg_type(sub) != DBUS_TYPE_STRUCT)
1298 dbus_message_iter_recurse(sub, &sub2);
1300 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
1303 if (!(i->path = strdup(path)))
1306 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
1307 dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
1311 dbus_message_iter_recurse(&sub2, &sub3);
1312 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1313 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1314 dbus_message_iter_next(&sub3);
1319 if (!(i->argv = new0(char*, n+1)))
1323 dbus_message_iter_recurse(&sub2, &sub3);
1324 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1327 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1328 dbus_message_iter_get_basic(&sub3, &s);
1329 dbus_message_iter_next(&sub3);
1331 if (!(i->argv[n++] = strdup(s)))
1335 if (!dbus_message_iter_next(&sub2) ||
1336 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, true) < 0 ||
1337 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp, true) < 0 ||
1338 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp, true) < 0 ||
1339 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) < 0 ||
1340 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) < 0 ||
1341 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) < 0)
1345 i->start_timestamp = (usec_t) start_timestamp;
1346 i->exit_timestamp = (usec_t) exit_timestamp;
1347 i->pid = (pid_t) pid;
1354 typedef struct UnitStatusInfo {
1356 const char *load_state;
1357 const char *active_state;
1358 const char *sub_state;
1360 const char *description;
1362 const char *fragment_path;
1363 const char *default_control_group;
1365 bool need_daemon_reload;
1370 const char *status_text;
1373 usec_t start_timestamp;
1374 usec_t exit_timestamp;
1376 int exit_code, exit_status;
1379 unsigned n_accepted;
1380 unsigned n_connections;
1384 const char *sysfs_path;
1386 /* Mount, Automount */
1392 LIST_HEAD(ExecStatusInfo, exec);
1395 static void print_status_info(UnitStatusInfo *i) {
1400 /* This shows pretty information about a unit. See
1401 * print_property() for a low-level property printer */
1403 printf("%s", strna(i->id));
1405 if (i->description && !streq_ptr(i->id, i->description))
1406 printf(" - %s", i->description);
1410 if (i->fragment_path)
1411 printf("\t Loaded: %s (%s)\n", strna(i->load_state), i->fragment_path);
1412 else if (streq_ptr(i->load_state, "failed"))
1413 printf("\t Loaded: " ANSI_HIGHLIGHT_ON "%s" ANSI_HIGHLIGHT_OFF "\n", strna(i->load_state));
1415 printf("\t Loaded: %s\n", strna(i->load_state));
1417 if (streq_ptr(i->active_state, "maintenance")) {
1418 if (streq_ptr(i->active_state, i->sub_state))
1419 printf("\t Active: " ANSI_HIGHLIGHT_ON "%s" ANSI_HIGHLIGHT_OFF "\n",
1420 strna(i->active_state));
1422 printf("\t Active: " ANSI_HIGHLIGHT_ON "%s (%s)" ANSI_HIGHLIGHT_OFF "\n",
1423 strna(i->active_state),
1424 strna(i->sub_state));
1426 if (streq_ptr(i->active_state, i->sub_state))
1427 printf("\t Active: %s\n",
1428 strna(i->active_state));
1430 printf("\t Active: %s (%s)\n",
1431 strna(i->active_state),
1432 strna(i->sub_state));
1436 printf("\t Device: %s\n", i->sysfs_path);
1438 printf("\t Where: %s\n", i->where);
1440 printf("\t What: %s\n", i->what);
1443 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
1445 LIST_FOREACH(exec, p, i->exec) {
1448 /* Only show exited processes here */
1452 t = strv_join(p->argv, " ");
1453 printf("\t Exited: %u (%s, code=%s, ", p->pid, strna(t), sigchld_code_to_string(p->code));
1456 if (p->code == CLD_EXITED)
1457 printf("status=%i", p->status);
1459 printf("signal=%s", signal_to_string(p->status));
1462 if (i->main_pid == p->pid &&
1463 i->start_timestamp == p->start_timestamp &&
1464 i->exit_timestamp == p->start_timestamp)
1465 /* Let's not show this twice */
1468 if (p->pid == i->control_pid)
1472 if (i->main_pid > 0 || i->control_pid > 0) {
1475 if (i->main_pid > 0) {
1476 printf(" Main: %u", (unsigned) i->main_pid);
1480 get_process_name(i->main_pid, &t);
1486 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
1488 if (i->exit_code == CLD_EXITED)
1489 printf("status=%i", i->exit_status);
1491 printf("signal=%s", signal_to_string(i->exit_status));
1496 if (i->main_pid > 0 && i->control_pid > 0)
1499 if (i->control_pid > 0) {
1502 printf(" Control: %u", (unsigned) i->control_pid);
1504 get_process_name(i->control_pid, &t);
1515 printf("\t Status: \"%s\"\n", i->status_text);
1517 if (i->default_control_group) {
1520 printf("\t CGroup: %s\n", i->default_control_group);
1522 if ((c = columns()) > 18)
1527 show_cgroup_by_path(i->default_control_group, "\t\t ", c);
1530 if (i->need_daemon_reload)
1531 printf("\n" ANSI_HIGHLIGHT_ON "Warning:" ANSI_HIGHLIGHT_OFF " Unit file changed on disk, 'systemctl %s daemon-reload' recommended.\n",
1532 arg_session ? "--session" : "--system");
1535 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
1537 switch (dbus_message_iter_get_arg_type(iter)) {
1539 case DBUS_TYPE_STRING: {
1542 dbus_message_iter_get_basic(iter, &s);
1545 if (streq(name, "Id"))
1547 else if (streq(name, "LoadState"))
1549 else if (streq(name, "ActiveState"))
1550 i->active_state = s;
1551 else if (streq(name, "SubState"))
1553 else if (streq(name, "Description"))
1555 else if (streq(name, "FragmentPath"))
1556 i->fragment_path = s;
1557 else if (streq(name, "DefaultControlGroup"))
1558 i->default_control_group = s;
1559 else if (streq(name, "StatusText"))
1561 else if (streq(name, "SysFSPath"))
1563 else if (streq(name, "Where"))
1565 else if (streq(name, "What"))
1572 case DBUS_TYPE_BOOLEAN: {
1575 dbus_message_iter_get_basic(iter, &b);
1577 if (streq(name, "Accept"))
1579 else if (streq(name, "NeedDaemonReload"))
1580 i->need_daemon_reload = b;
1585 case DBUS_TYPE_UINT32: {
1588 dbus_message_iter_get_basic(iter, &u);
1590 if (streq(name, "MainPID")) {
1592 i->main_pid = (pid_t) u;
1595 } else if (streq(name, "ControlPID"))
1596 i->control_pid = (pid_t) u;
1597 else if (streq(name, "ExecMainPID")) {
1599 i->main_pid = (pid_t) u;
1600 } else if (streq(name, "NAccepted"))
1602 else if (streq(name, "NConnections"))
1603 i->n_connections = u;
1608 case DBUS_TYPE_INT32: {
1611 dbus_message_iter_get_basic(iter, &j);
1613 if (streq(name, "ExecMainCode"))
1614 i->exit_code = (int) j;
1615 else if (streq(name, "ExecMainStatus"))
1616 i->exit_status = (int) j;
1621 case DBUS_TYPE_UINT64: {
1624 dbus_message_iter_get_basic(iter, &u);
1626 if (streq(name, "ExecMainStartTimestamp"))
1627 i->start_timestamp = (usec_t) u;
1628 else if (streq(name, "ExecMainExitTimestamp"))
1629 i->exit_timestamp = (usec_t) u;
1634 case DBUS_TYPE_ARRAY: {
1636 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT &&
1637 startswith(name, "Exec")) {
1638 DBusMessageIter sub;
1640 dbus_message_iter_recurse(iter, &sub);
1641 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1642 ExecStatusInfo *info;
1645 if (!(info = new0(ExecStatusInfo, 1)))
1648 if ((r = exec_status_info_deserialize(&sub, info)) < 0) {
1653 LIST_PREPEND(ExecStatusInfo, exec, i->exec, info);
1655 dbus_message_iter_next(&sub);
1666 static int print_property(const char *name, DBusMessageIter *iter) {
1670 /* This is a low-level property printer, see
1671 * print_status_info() for the nicer output */
1673 if (arg_property && !streq(name, arg_property))
1676 switch (dbus_message_iter_get_arg_type(iter)) {
1678 case DBUS_TYPE_STRING: {
1680 dbus_message_iter_get_basic(iter, &s);
1682 if (arg_all || s[0])
1683 printf("%s=%s\n", name, s);
1688 case DBUS_TYPE_BOOLEAN: {
1690 dbus_message_iter_get_basic(iter, &b);
1691 printf("%s=%s\n", name, yes_no(b));
1696 case DBUS_TYPE_UINT64: {
1698 dbus_message_iter_get_basic(iter, &u);
1700 /* Yes, heuristics! But we can change this check
1701 * should it turn out to not be sufficient */
1703 if (strstr(name, "Timestamp")) {
1704 char timestamp[FORMAT_TIMESTAMP_MAX], *t;
1706 if ((t = format_timestamp(timestamp, sizeof(timestamp), u)) || arg_all)
1707 printf("%s=%s\n", name, strempty(t));
1708 } else if (strstr(name, "USec")) {
1709 char timespan[FORMAT_TIMESPAN_MAX];
1711 printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u));
1713 printf("%s=%llu\n", name, (unsigned long long) u);
1718 case DBUS_TYPE_UINT32: {
1720 dbus_message_iter_get_basic(iter, &u);
1722 if (strstr(name, "UMask") || strstr(name, "Mode"))
1723 printf("%s=%04o\n", name, u);
1725 printf("%s=%u\n", name, (unsigned) u);
1730 case DBUS_TYPE_INT32: {
1732 dbus_message_iter_get_basic(iter, &i);
1734 printf("%s=%i\n", name, (int) i);
1738 case DBUS_TYPE_STRUCT: {
1739 DBusMessageIter sub;
1740 dbus_message_iter_recurse(iter, &sub);
1742 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
1745 dbus_message_iter_get_basic(&sub, &u);
1748 printf("%s=%u\n", name, (unsigned) u);
1750 printf("%s=\n", name);
1753 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
1756 dbus_message_iter_get_basic(&sub, &s);
1758 if (arg_all || s[0])
1759 printf("%s=%s\n", name, s);
1767 case DBUS_TYPE_ARRAY:
1769 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
1770 DBusMessageIter sub;
1773 dbus_message_iter_recurse(iter, &sub);
1775 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1776 printf("%s=", name);
1778 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1781 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
1782 dbus_message_iter_get_basic(&sub, &s);
1783 printf("%s%s", space ? " " : "", s);
1786 dbus_message_iter_next(&sub);
1794 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_BYTE) {
1795 DBusMessageIter sub;
1797 dbus_message_iter_recurse(iter, &sub);
1799 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1800 printf("%s=", name);
1802 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1805 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_BYTE);
1806 dbus_message_iter_get_basic(&sub, &u);
1809 dbus_message_iter_next(&sub);
1817 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
1818 DBusMessageIter sub, sub2;
1820 dbus_message_iter_recurse(iter, &sub);
1821 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1822 const char *type, *path;
1824 dbus_message_iter_recurse(&sub, &sub2);
1826 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
1827 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
1828 printf("%s=%s\n", type, path);
1830 dbus_message_iter_next(&sub);
1835 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
1836 DBusMessageIter sub, sub2;
1838 dbus_message_iter_recurse(iter, &sub);
1839 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1841 uint64_t value, next_elapse;
1843 dbus_message_iter_recurse(&sub, &sub2);
1845 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
1846 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
1847 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
1848 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
1850 printf("%s={ value=%s ; next_elapse=%s }\n",
1852 format_timespan(timespan1, sizeof(timespan1), value),
1853 format_timespan(timespan2, sizeof(timespan2), next_elapse));
1856 dbus_message_iter_next(&sub);
1861 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
1862 DBusMessageIter sub;
1864 dbus_message_iter_recurse(iter, &sub);
1865 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1866 ExecStatusInfo info;
1869 if (exec_status_info_deserialize(&sub, &info) >= 0) {
1870 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
1873 t = strv_join(info.argv, " ");
1875 printf("%s={ path=%s ; argv[]=%s ; ignore=%s ; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n",
1879 yes_no(info.ignore),
1880 strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)),
1881 strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)),
1882 (unsigned) info. pid,
1883 sigchld_code_to_string(info.code),
1885 info.code == CLD_EXITED ? "" : "/",
1886 strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status)));
1892 strv_free(info.argv);
1894 dbus_message_iter_next(&sub);
1904 printf("%s=[unprintable]\n", name);
1909 static int show_one(DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
1910 DBusMessage *m = NULL, *reply = NULL;
1911 const char *interface = "";
1914 DBusMessageIter iter, sub, sub2, sub3;
1915 UnitStatusInfo info;
1923 dbus_error_init(&error);
1925 if (!(m = dbus_message_new_method_call(
1926 "org.freedesktop.systemd1",
1928 "org.freedesktop.DBus.Properties",
1930 log_error("Could not allocate message.");
1935 if (!dbus_message_append_args(m,
1936 DBUS_TYPE_STRING, &interface,
1937 DBUS_TYPE_INVALID)) {
1938 log_error("Could not append arguments to message.");
1943 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1944 log_error("Failed to issue method call: %s", error.message);
1949 if (!dbus_message_iter_init(reply, &iter) ||
1950 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
1951 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
1952 log_error("Failed to parse reply.");
1957 dbus_message_iter_recurse(&iter, &sub);
1964 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1967 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
1968 log_error("Failed to parse reply.");
1973 dbus_message_iter_recurse(&sub, &sub2);
1975 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
1976 log_error("Failed to parse reply.");
1981 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
1982 log_error("Failed to parse reply.");
1987 dbus_message_iter_recurse(&sub2, &sub3);
1989 if (show_properties)
1990 r = print_property(name, &sub3);
1992 r = status_property(name, &sub3, &info);
1995 log_error("Failed to parse reply.");
2000 dbus_message_iter_next(&sub);
2003 if (!show_properties)
2004 print_status_info(&info);
2006 while ((p = info.exec)) {
2007 LIST_REMOVE(ExecStatusInfo, exec, info.exec, p);
2008 exec_status_info_free(p);
2015 dbus_message_unref(m);
2018 dbus_message_unref(reply);
2020 dbus_error_free(&error);
2025 static int show(DBusConnection *bus, char **args, unsigned n) {
2026 DBusMessage *m = NULL, *reply = NULL;
2030 bool show_properties, new_line = false;
2035 dbus_error_init(&error);
2037 show_properties = !streq(args[0], "status");
2039 if (show_properties && n <= 1) {
2040 /* If not argument is specified inspect the manager
2043 r = show_one(bus, "/org/freedesktop/systemd1", show_properties, &new_line);
2047 for (i = 1; i < n; i++) {
2048 const char *path = NULL;
2051 if (!show_properties || safe_atou32(args[i], &id) < 0) {
2053 if (!(m = dbus_message_new_method_call(
2054 "org.freedesktop.systemd1",
2055 "/org/freedesktop/systemd1",
2056 "org.freedesktop.systemd1.Manager",
2058 log_error("Could not allocate message.");
2063 if (!dbus_message_append_args(m,
2064 DBUS_TYPE_STRING, &args[i],
2065 DBUS_TYPE_INVALID)) {
2066 log_error("Could not append arguments to message.");
2071 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2073 if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
2074 log_error("Failed to issue method call: %s", error.message);
2079 dbus_error_free(&error);
2081 dbus_message_unref(m);
2082 if (!(m = dbus_message_new_method_call(
2083 "org.freedesktop.systemd1",
2084 "/org/freedesktop/systemd1",
2085 "org.freedesktop.systemd1.Manager",
2087 log_error("Could not allocate message.");
2092 if (!dbus_message_append_args(m,
2093 DBUS_TYPE_STRING, &args[i],
2094 DBUS_TYPE_INVALID)) {
2095 log_error("Could not append arguments to message.");
2100 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2101 log_error("Failed to issue method call: %s", error.message);
2109 if (!(m = dbus_message_new_method_call(
2110 "org.freedesktop.systemd1",
2111 "/org/freedesktop/systemd1",
2112 "org.freedesktop.systemd1.Manager",
2114 log_error("Could not allocate message.");
2119 if (!dbus_message_append_args(m,
2120 DBUS_TYPE_UINT32, &id,
2121 DBUS_TYPE_INVALID)) {
2122 log_error("Could not append arguments to message.");
2127 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2128 log_error("Failed to issue method call: %s", error.message);
2134 if (!dbus_message_get_args(reply, &error,
2135 DBUS_TYPE_OBJECT_PATH, &path,
2136 DBUS_TYPE_INVALID)) {
2137 log_error("Failed to parse reply: %s", error.message);
2142 if ((r = show_one(bus, path, show_properties, &new_line)) < 0)
2145 dbus_message_unref(m);
2146 dbus_message_unref(reply);
2154 dbus_message_unref(m);
2157 dbus_message_unref(reply);
2159 dbus_error_free(&error);
2164 static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
2166 DBusMessage *m = NULL, *reply = NULL;
2171 dbus_error_init(&error);
2173 log_debug("Got D-Bus request: %s.%s() on %s",
2174 dbus_message_get_interface(message),
2175 dbus_message_get_member(message),
2176 dbus_message_get_path(message));
2178 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
2179 log_error("Warning! D-Bus connection terminated.");
2180 dbus_connection_close(connection);
2182 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") ||
2183 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
2184 const char *id, *path;
2186 if (!dbus_message_get_args(message, &error,
2187 DBUS_TYPE_STRING, &id,
2188 DBUS_TYPE_OBJECT_PATH, &path,
2190 log_error("Failed to parse message: %s", error.message);
2191 else if (streq(dbus_message_get_member(message), "UnitNew"))
2192 printf("Unit %s added.\n", id);
2194 printf("Unit %s removed.\n", id);
2196 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew") ||
2197 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
2201 if (!dbus_message_get_args(message, &error,
2202 DBUS_TYPE_UINT32, &id,
2203 DBUS_TYPE_OBJECT_PATH, &path,
2205 log_error("Failed to parse message: %s", error.message);
2206 else if (streq(dbus_message_get_member(message), "JobNew"))
2207 printf("Job %u added.\n", id);
2209 printf("Job %u removed.\n", id);
2212 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Unit", "Changed") ||
2213 dbus_message_is_signal(message, "org.freedesktop.systemd1.Job", "Changed")) {
2215 const char *path, *interface, *property = "Id";
2216 DBusMessageIter iter, sub;
2218 path = dbus_message_get_path(message);
2219 interface = dbus_message_get_interface(message);
2221 if (!(m = dbus_message_new_method_call(
2222 "org.freedesktop.systemd1",
2224 "org.freedesktop.DBus.Properties",
2226 log_error("Could not allocate message.");
2230 if (!dbus_message_append_args(m,
2231 DBUS_TYPE_STRING, &interface,
2232 DBUS_TYPE_STRING, &property,
2233 DBUS_TYPE_INVALID)) {
2234 log_error("Could not append arguments to message.");
2238 if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
2239 log_error("Failed to issue method call: %s", error.message);
2243 if (!dbus_message_iter_init(reply, &iter) ||
2244 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2245 log_error("Failed to parse reply.");
2249 dbus_message_iter_recurse(&iter, &sub);
2251 if (streq(interface, "org.freedesktop.systemd1.Unit")) {
2254 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2255 log_error("Failed to parse reply.");
2259 dbus_message_iter_get_basic(&sub, &id);
2260 printf("Unit %s changed.\n", id);
2264 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32) {
2265 log_error("Failed to parse reply.");
2269 dbus_message_iter_get_basic(&sub, &id);
2270 printf("Job %u changed.\n", id);
2276 dbus_message_unref(m);
2279 dbus_message_unref(reply);
2281 dbus_error_free(&error);
2282 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2286 dbus_message_unref(m);
2289 dbus_message_unref(reply);
2291 dbus_error_free(&error);
2292 return DBUS_HANDLER_RESULT_NEED_MEMORY;
2295 static int monitor(DBusConnection *bus, char **args, unsigned n) {
2296 DBusMessage *m = NULL, *reply = NULL;
2300 dbus_error_init(&error);
2303 dbus_bus_add_match(bus,
2305 "sender='org.freedesktop.systemd1',"
2306 "interface='org.freedesktop.systemd1.Manager',"
2307 "path='/org/freedesktop/systemd1'",
2310 if (dbus_error_is_set(&error)) {
2311 log_error("Failed to add match: %s", error.message);
2316 dbus_bus_add_match(bus,
2318 "sender='org.freedesktop.systemd1',"
2319 "interface='org.freedesktop.systemd1.Unit',"
2323 if (dbus_error_is_set(&error)) {
2324 log_error("Failed to add match: %s", error.message);
2329 dbus_bus_add_match(bus,
2331 "sender='org.freedesktop.systemd1',"
2332 "interface='org.freedesktop.systemd1.Job',"
2336 if (dbus_error_is_set(&error)) {
2337 log_error("Failed to add match: %s", error.message);
2343 if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
2344 log_error("Failed to add filter.");
2349 if (!(m = dbus_message_new_method_call(
2350 "org.freedesktop.systemd1",
2351 "/org/freedesktop/systemd1",
2352 "org.freedesktop.systemd1.Manager",
2354 log_error("Could not allocate message.");
2359 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2360 log_error("Failed to issue method call: %s", error.message);
2365 while (dbus_connection_read_write_dispatch(bus, -1))
2372 /* This is slightly dirty, since we don't undo the filter or the matches. */
2375 dbus_message_unref(m);
2378 dbus_message_unref(reply);
2380 dbus_error_free(&error);
2385 static int dump(DBusConnection *bus, char **args, unsigned n) {
2386 DBusMessage *m = NULL, *reply = NULL;
2391 dbus_error_init(&error);
2393 if (!(m = dbus_message_new_method_call(
2394 "org.freedesktop.systemd1",
2395 "/org/freedesktop/systemd1",
2396 "org.freedesktop.systemd1.Manager",
2398 log_error("Could not allocate message.");
2402 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2403 log_error("Failed to issue method call: %s", error.message);
2408 if (!dbus_message_get_args(reply, &error,
2409 DBUS_TYPE_STRING, &text,
2410 DBUS_TYPE_INVALID)) {
2411 log_error("Failed to parse reply: %s", error.message);
2416 fputs(text, stdout);
2422 dbus_message_unref(m);
2425 dbus_message_unref(reply);
2427 dbus_error_free(&error);
2432 static int snapshot(DBusConnection *bus, char **args, unsigned n) {
2433 DBusMessage *m = NULL, *reply = NULL;
2436 const char *name = "", *path, *id;
2437 dbus_bool_t cleanup = FALSE;
2438 DBusMessageIter iter, sub;
2440 *interface = "org.freedesktop.systemd1.Unit",
2443 dbus_error_init(&error);
2445 if (!(m = dbus_message_new_method_call(
2446 "org.freedesktop.systemd1",
2447 "/org/freedesktop/systemd1",
2448 "org.freedesktop.systemd1.Manager",
2449 "CreateSnapshot"))) {
2450 log_error("Could not allocate message.");
2457 if (!dbus_message_append_args(m,
2458 DBUS_TYPE_STRING, &name,
2459 DBUS_TYPE_BOOLEAN, &cleanup,
2460 DBUS_TYPE_INVALID)) {
2461 log_error("Could not append arguments to message.");
2466 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2467 log_error("Failed to issue method call: %s", error.message);
2472 if (!dbus_message_get_args(reply, &error,
2473 DBUS_TYPE_OBJECT_PATH, &path,
2474 DBUS_TYPE_INVALID)) {
2475 log_error("Failed to parse reply: %s", error.message);
2480 dbus_message_unref(m);
2481 if (!(m = dbus_message_new_method_call(
2482 "org.freedesktop.systemd1",
2484 "org.freedesktop.DBus.Properties",
2486 log_error("Could not allocate message.");
2490 if (!dbus_message_append_args(m,
2491 DBUS_TYPE_STRING, &interface,
2492 DBUS_TYPE_STRING, &property,
2493 DBUS_TYPE_INVALID)) {
2494 log_error("Could not append arguments to message.");
2499 dbus_message_unref(reply);
2500 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2501 log_error("Failed to issue method call: %s", error.message);
2506 if (!dbus_message_iter_init(reply, &iter) ||
2507 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2508 log_error("Failed to parse reply.");
2513 dbus_message_iter_recurse(&iter, &sub);
2515 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2516 log_error("Failed to parse reply.");
2521 dbus_message_iter_get_basic(&sub, &id);
2529 dbus_message_unref(m);
2532 dbus_message_unref(reply);
2534 dbus_error_free(&error);
2539 static int delete_snapshot(DBusConnection *bus, char **args, unsigned n) {
2540 DBusMessage *m = NULL, *reply = NULL;
2548 dbus_error_init(&error);
2550 for (i = 1; i < n; i++) {
2551 const char *path = NULL;
2553 if (!(m = dbus_message_new_method_call(
2554 "org.freedesktop.systemd1",
2555 "/org/freedesktop/systemd1",
2556 "org.freedesktop.systemd1.Manager",
2558 log_error("Could not allocate message.");
2563 if (!dbus_message_append_args(m,
2564 DBUS_TYPE_STRING, &args[i],
2565 DBUS_TYPE_INVALID)) {
2566 log_error("Could not append arguments to message.");
2571 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2572 log_error("Failed to issue method call: %s", error.message);
2577 if (!dbus_message_get_args(reply, &error,
2578 DBUS_TYPE_OBJECT_PATH, &path,
2579 DBUS_TYPE_INVALID)) {
2580 log_error("Failed to parse reply: %s", error.message);
2585 dbus_message_unref(m);
2586 if (!(m = dbus_message_new_method_call(
2587 "org.freedesktop.systemd1",
2589 "org.freedesktop.systemd1.Snapshot",
2591 log_error("Could not allocate message.");
2596 dbus_message_unref(reply);
2597 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2598 log_error("Failed to issue method call: %s", error.message);
2603 dbus_message_unref(m);
2604 dbus_message_unref(reply);
2612 dbus_message_unref(m);
2615 dbus_message_unref(reply);
2617 dbus_error_free(&error);
2622 static int clear_jobs(DBusConnection *bus, char **args, unsigned n) {
2623 DBusMessage *m = NULL, *reply = NULL;
2628 dbus_error_init(&error);
2630 if (arg_action == ACTION_RELOAD)
2632 else if (arg_action == ACTION_REEXEC)
2633 method = "Reexecute";
2635 assert(arg_action == ACTION_SYSTEMCTL);
2638 streq(args[0], "clear-jobs") ? "ClearJobs" :
2639 streq(args[0], "daemon-reload") ? "Reload" :
2640 streq(args[0], "daemon-reexec") ? "Reexecute" :
2644 if (!(m = dbus_message_new_method_call(
2645 "org.freedesktop.systemd1",
2646 "/org/freedesktop/systemd1",
2647 "org.freedesktop.systemd1.Manager",
2649 log_error("Could not allocate message.");
2653 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2655 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
2656 /* There's always a fallback possible for
2657 * legacy actions. */
2662 log_error("Failed to issue method call: %s", error.message);
2671 dbus_message_unref(m);
2674 dbus_message_unref(reply);
2676 dbus_error_free(&error);
2681 static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
2682 DBusMessage *m = NULL, *reply = NULL;
2684 DBusMessageIter iter, sub, sub2;
2687 *interface = "org.freedesktop.systemd1.Manager",
2688 *property = "Environment";
2690 dbus_error_init(&error);
2692 if (!(m = dbus_message_new_method_call(
2693 "org.freedesktop.systemd1",
2694 "/org/freedesktop/systemd1",
2695 "org.freedesktop.DBus.Properties",
2697 log_error("Could not allocate message.");
2701 if (!dbus_message_append_args(m,
2702 DBUS_TYPE_STRING, &interface,
2703 DBUS_TYPE_STRING, &property,
2704 DBUS_TYPE_INVALID)) {
2705 log_error("Could not append arguments to message.");
2710 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2711 log_error("Failed to issue method call: %s", error.message);
2716 if (!dbus_message_iter_init(reply, &iter) ||
2717 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2718 log_error("Failed to parse reply.");
2723 dbus_message_iter_recurse(&iter, &sub);
2725 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
2726 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
2727 log_error("Failed to parse reply.");
2732 dbus_message_iter_recurse(&sub, &sub2);
2734 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
2737 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
2738 log_error("Failed to parse reply.");
2743 dbus_message_iter_get_basic(&sub2, &text);
2744 printf("%s\n", text);
2746 dbus_message_iter_next(&sub2);
2753 dbus_message_unref(m);
2756 dbus_message_unref(reply);
2758 dbus_error_free(&error);
2763 static int set_environment(DBusConnection *bus, char **args, unsigned n) {
2764 DBusMessage *m = NULL, *reply = NULL;
2768 DBusMessageIter iter, sub;
2771 dbus_error_init(&error);
2773 method = streq(args[0], "set-environment")
2775 : "UnsetEnvironment";
2777 if (!(m = dbus_message_new_method_call(
2778 "org.freedesktop.systemd1",
2779 "/org/freedesktop/systemd1",
2780 "org.freedesktop.systemd1.Manager",
2783 log_error("Could not allocate message.");
2787 dbus_message_iter_init_append(m, &iter);
2789 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
2790 log_error("Could not append arguments to message.");
2795 for (i = 1; i < n; i++)
2796 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) {
2797 log_error("Could not append arguments to message.");
2802 if (!dbus_message_iter_close_container(&iter, &sub)) {
2803 log_error("Could not append arguments to message.");
2808 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2809 log_error("Failed to issue method call: %s", error.message);
2818 dbus_message_unref(m);
2821 dbus_message_unref(reply);
2823 dbus_error_free(&error);
2828 static int systemctl_help(void) {
2830 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
2831 "Send control commands to the systemd manager.\n\n"
2832 " -h --help Show this help\n"
2833 " -t --type=TYPE List only units of a particular type\n"
2834 " -p --property=NAME Show only properties by this name\n"
2835 " -a --all Show all units/properties, including dead/empty ones\n"
2836 " --fail When installing a new job, fail if conflicting jobs are\n"
2838 " --system Connect to system bus\n"
2839 " --session Connect to session bus\n"
2840 " --order When generating graph for dot, show only order\n"
2841 " --require When generating graph for dot, show only requirement\n"
2842 " -q --quiet Suppress output\n"
2843 " --no-block Do not wait until operation finished\n"
2844 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
2846 " list-units List units\n"
2847 " start [NAME...] Start one or more units\n"
2848 " stop [NAME...] Stop one or more units\n"
2849 " reload [NAME...] Reload one or more units\n"
2850 " restart [NAME...] Start or restart one or more units\n"
2851 " try-restart [NAME...] Restart one or more units if active\n"
2852 " reload-or-restart [NAME...] Reload one or more units is possible,\n"
2853 " otherwise start or restart\n"
2854 " reload-or-try-restart [NAME...] Reload one or more units is possible,\n"
2855 " otherwise restart if active\n"
2856 " isolate [NAME] Start one unit and stop all others\n"
2857 " check [NAME...] Check whether units are active\n"
2858 " status [NAME...] Show status of one or more units\n"
2859 " show [NAME...|JOB...] Show properties of one or more\n"
2860 " units/jobs/manager\n"
2861 " load [NAME...] Load one or more units\n"
2862 " list-jobs List jobs\n"
2863 " cancel [JOB...] Cancel one or more jobs\n"
2864 " clear-jobs Cancel all jobs\n"
2865 " monitor Monitor unit/job changes\n"
2866 " dump Dump server status\n"
2867 " dot Dump dependency graph for dot(1)\n"
2868 " snapshot [NAME] Create a snapshot\n"
2869 " delete [NAME...] Remove one or more snapshots\n"
2870 " daemon-reload Reload systemd manager configuration\n"
2871 " daemon-reexec Reexecute systemd manager\n"
2872 " daemon-exit Ask the systemd manager to quit\n"
2873 " show-environment Dump environment\n"
2874 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
2875 " unset-environment [NAME...] Unset one or more environment variables\n"
2876 " halt Shut down and halt the system\n"
2877 " poweroff Shut down and power-off the system\n"
2878 " reboot Shut down and reboot the system\n"
2879 " rescue Enter system rescue mode\n"
2880 " emergency Enter system emergency mode\n"
2881 " default Enter system default mode\n",
2882 program_invocation_short_name);
2887 static int halt_help(void) {
2889 printf("%s [OPTIONS...]\n\n"
2890 "%s the system.\n\n"
2891 " --help Show this help\n"
2892 " --halt Halt the machine\n"
2893 " -p --poweroff Switch off the machine\n"
2894 " --reboot Reboot the machine\n"
2895 " -f --force Force immediate halt/power-off/reboot\n"
2896 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
2897 " -d --no-wtmp Don't write wtmp record\n"
2898 " -n --no-sync Don't sync before halt/power-off/reboot\n"
2899 " --no-wall Don't send wall message before halt/power-off/reboot\n",
2900 program_invocation_short_name,
2901 arg_action == ACTION_REBOOT ? "Reboot" :
2902 arg_action == ACTION_POWEROFF ? "Power off" :
2908 static int shutdown_help(void) {
2910 printf("%s [OPTIONS...] [now] [WALL...]\n\n"
2911 "Shut down the system.\n\n"
2912 " --help Show this help\n"
2913 " -H --halt Halt the machine\n"
2914 " -P --poweroff Power-off the machine\n"
2915 " -r --reboot Reboot the machine\n"
2916 " -h Equivalent to --poweroff, overriden by --halt\n"
2917 " -k Don't halt/power-off/reboot, just send warnings\n"
2918 " --no-wall Don't send wall message before halt/power-off/reboot\n",
2919 program_invocation_short_name);
2924 static int telinit_help(void) {
2926 printf("%s [OPTIONS...] {COMMAND}\n\n"
2927 "Send control commands to the init daemon.\n\n"
2928 " --help Show this help\n"
2929 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
2931 " 0 Power-off the machine\n"
2932 " 6 Reboot the machine\n"
2933 " 2, 3, 4, 5 Start runlevelX.target unit\n"
2934 " 1, s, S Enter rescue mode\n"
2935 " q, Q Reload init daemon configuration\n"
2936 " u, U Reexecute init daemon\n",
2937 program_invocation_short_name);
2942 static int runlevel_help(void) {
2944 printf("%s [OPTIONS...]\n\n"
2945 "Prints the previous and current runlevel of the init system.\n\n"
2946 " --help Show this help\n",
2947 program_invocation_short_name);
2952 static int systemctl_parse_argv(int argc, char *argv[]) {
2964 static const struct option options[] = {
2965 { "help", no_argument, NULL, 'h' },
2966 { "type", required_argument, NULL, 't' },
2967 { "property", required_argument, NULL, 'p' },
2968 { "all", no_argument, NULL, 'a' },
2969 { "fail", no_argument, NULL, ARG_FAIL },
2970 { "session", no_argument, NULL, ARG_SESSION },
2971 { "system", no_argument, NULL, ARG_SYSTEM },
2972 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
2973 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2974 { "quiet", no_argument, NULL, 'q' },
2975 { "order", no_argument, NULL, ARG_ORDER },
2976 { "require", no_argument, NULL, ARG_REQUIRE },
2977 { NULL, 0, NULL, 0 }
2985 while ((c = getopt_long(argc, argv, "ht:p:aq", options, NULL)) >= 0) {
2998 arg_property = optarg;
3000 /* If the user asked for a particular
3001 * property, show it to him, even if it is
3019 arg_session = false;
3023 arg_no_block = true;
3031 arg_dot = DOT_ORDER;
3035 arg_dot = DOT_REQUIRE;
3046 log_error("Unknown option code %c", c);
3054 static int halt_parse_argv(int argc, char *argv[]) {
3063 static const struct option options[] = {
3064 { "help", no_argument, NULL, ARG_HELP },
3065 { "halt", no_argument, NULL, ARG_HALT },
3066 { "poweroff", no_argument, NULL, 'p' },
3067 { "reboot", no_argument, NULL, ARG_REBOOT },
3068 { "force", no_argument, NULL, 'f' },
3069 { "wtmp-only", no_argument, NULL, 'w' },
3070 { "no-wtmp", no_argument, NULL, 'd' },
3071 { "no-sync", no_argument, NULL, 'n' },
3072 { "no-wall", no_argument, NULL, ARG_NO_WALL },
3073 { NULL, 0, NULL, 0 }
3081 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
3082 if (runlevel == '0' || runlevel == '6')
3083 arg_immediate = true;
3085 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
3093 arg_action = ACTION_HALT;
3097 arg_action = ACTION_POWEROFF;
3101 arg_action = ACTION_REBOOT;
3105 arg_immediate = true;
3126 /* Compatibility nops */
3133 log_error("Unknown option code %c", c);
3138 if (optind < argc) {
3139 log_error("Too many arguments.");
3146 static int shutdown_parse_argv(int argc, char *argv[]) {
3153 static const struct option options[] = {
3154 { "help", no_argument, NULL, ARG_HELP },
3155 { "halt", no_argument, NULL, 'H' },
3156 { "poweroff", no_argument, NULL, 'P' },
3157 { "reboot", no_argument, NULL, 'r' },
3158 { "no-wall", no_argument, NULL, ARG_NO_WALL },
3159 { NULL, 0, NULL, 0 }
3167 while ((c = getopt_long(argc, argv, "HPrhkt:a", options, NULL)) >= 0) {
3175 arg_action = ACTION_HALT;
3179 arg_action = ACTION_POWEROFF;
3183 arg_action = ACTION_REBOOT;
3187 if (arg_action != ACTION_HALT)
3188 arg_action = ACTION_POWEROFF;
3201 /* Compatibility nops */
3208 log_error("Unknown option code %c", c);
3213 if (argc > optind && !streq(argv[optind], "now"))
3214 log_warning("First argument '%s' isn't 'now'. Ignoring.", argv[optind]);
3216 /* We ignore the time argument */
3217 if (argc > optind + 1)
3218 arg_wall = argv + optind + 1;
3225 static int telinit_parse_argv(int argc, char *argv[]) {
3232 static const struct option options[] = {
3233 { "help", no_argument, NULL, ARG_HELP },
3234 { "no-wall", no_argument, NULL, ARG_NO_WALL },
3235 { NULL, 0, NULL, 0 }
3238 static const struct {
3242 { '0', ACTION_POWEROFF },
3243 { '6', ACTION_REBOOT },
3244 { '1', ACTION_RESCUE },
3245 { '2', ACTION_RUNLEVEL2 },
3246 { '3', ACTION_RUNLEVEL3 },
3247 { '4', ACTION_RUNLEVEL4 },
3248 { '5', ACTION_RUNLEVEL5 },
3249 { 's', ACTION_RESCUE },
3250 { 'S', ACTION_RESCUE },
3251 { 'q', ACTION_RELOAD },
3252 { 'Q', ACTION_RELOAD },
3253 { 'u', ACTION_REEXEC },
3254 { 'U', ACTION_REEXEC }
3263 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
3278 log_error("Unknown option code %c", c);
3283 if (optind >= argc) {
3288 if (optind + 1 < argc) {
3289 log_error("Too many arguments.");
3293 if (strlen(argv[optind]) != 1) {
3294 log_error("Expected single character argument.");
3298 for (i = 0; i < ELEMENTSOF(table); i++)
3299 if (table[i].from == argv[optind][0])
3302 if (i >= ELEMENTSOF(table)) {
3303 log_error("Unknown command %s.", argv[optind]);
3307 arg_action = table[i].to;
3314 static int runlevel_parse_argv(int argc, char *argv[]) {
3320 static const struct option options[] = {
3321 { "help", no_argument, NULL, ARG_HELP },
3322 { NULL, 0, NULL, 0 }
3330 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
3341 log_error("Unknown option code %c", c);
3346 if (optind < argc) {
3347 log_error("Too many arguments.");
3354 static int parse_argv(int argc, char *argv[]) {
3358 if (program_invocation_short_name) {
3360 if (strstr(program_invocation_short_name, "halt")) {
3361 arg_action = ACTION_HALT;
3362 return halt_parse_argv(argc, argv);
3363 } else if (strstr(program_invocation_short_name, "poweroff")) {
3364 arg_action = ACTION_POWEROFF;
3365 return halt_parse_argv(argc, argv);
3366 } else if (strstr(program_invocation_short_name, "reboot")) {
3367 arg_action = ACTION_REBOOT;
3368 return halt_parse_argv(argc, argv);
3369 } else if (strstr(program_invocation_short_name, "shutdown")) {
3370 arg_action = ACTION_POWEROFF;
3371 return shutdown_parse_argv(argc, argv);
3372 } else if (strstr(program_invocation_short_name, "init")) {
3373 arg_action = ACTION_INVALID;
3374 return telinit_parse_argv(argc, argv);
3375 } else if (strstr(program_invocation_short_name, "runlevel")) {
3376 arg_action = ACTION_RUNLEVEL;
3377 return runlevel_parse_argv(argc, argv);
3381 arg_action = ACTION_SYSTEMCTL;
3382 return systemctl_parse_argv(argc, argv);
3385 static int action_to_runlevel(void) {
3387 static const char table[_ACTION_MAX] = {
3388 [ACTION_HALT] = '0',
3389 [ACTION_POWEROFF] = '0',
3390 [ACTION_REBOOT] = '6',
3391 [ACTION_RUNLEVEL2] = '2',
3392 [ACTION_RUNLEVEL3] = '3',
3393 [ACTION_RUNLEVEL4] = '4',
3394 [ACTION_RUNLEVEL5] = '5',
3395 [ACTION_RESCUE] = '1'
3398 assert(arg_action < _ACTION_MAX);
3400 return table[arg_action];
3403 static int talk_upstart(void) {
3404 DBusMessage *m = NULL, *reply = NULL;
3406 int previous, rl, r;
3408 env1_buf[] = "RUNLEVEL=X",
3409 env2_buf[] = "PREVLEVEL=X";
3410 char *env1 = env1_buf, *env2 = env2_buf;
3411 const char *emit = "runlevel";
3412 dbus_bool_t b_false = FALSE;
3413 DBusMessageIter iter, sub;
3414 DBusConnection *bus;
3416 dbus_error_init(&error);
3418 if (!(rl = action_to_runlevel()))
3421 if (utmp_get_runlevel(&previous, NULL) < 0)
3424 if (!(bus = dbus_connection_open_private("unix:abstract=/com/ubuntu/upstart", &error))) {
3425 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
3430 log_error("Failed to connect to Upstart bus: %s", error.message);
3435 if ((r = bus_check_peercred(bus)) < 0) {
3436 log_error("Failed to verify owner of bus.");
3440 if (!(m = dbus_message_new_method_call(
3441 "com.ubuntu.Upstart",
3442 "/com/ubuntu/Upstart",
3443 "com.ubuntu.Upstart0_6",
3446 log_error("Could not allocate message.");
3451 dbus_message_iter_init_append(m, &iter);
3453 env1_buf[sizeof(env1_buf)-2] = rl;
3454 env2_buf[sizeof(env2_buf)-2] = previous;
3456 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
3457 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
3458 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
3459 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
3460 !dbus_message_iter_close_container(&iter, &sub) ||
3461 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
3462 log_error("Could not append arguments to message.");
3467 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3469 if (error_is_no_service(&error)) {
3474 log_error("Failed to issue method call: %s", error.message);
3483 dbus_message_unref(m);
3486 dbus_message_unref(reply);
3489 dbus_connection_close(bus);
3490 dbus_connection_unref(bus);
3493 dbus_error_free(&error);
3498 static int talk_initctl(void) {
3499 struct init_request request;
3503 if (!(rl = action_to_runlevel()))
3507 request.magic = INIT_MAGIC;
3508 request.sleeptime = 0;
3509 request.cmd = INIT_CMD_RUNLVL;
3510 request.runlevel = rl;
3512 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
3514 if (errno == ENOENT)
3517 log_error("Failed to open "INIT_FIFO": %m");
3522 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
3523 close_nointr_nofail(fd);
3526 log_error("Failed to write to "INIT_FIFO": %m");
3527 return errno ? -errno : -EIO;
3533 static int systemctl_main(DBusConnection *bus, int argc, char *argv[]) {
3535 static const struct {
3543 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
3545 { "list-units", LESS, 1, list_units },
3546 { "list-jobs", EQUAL, 1, list_jobs },
3547 { "clear-jobs", EQUAL, 1, clear_jobs },
3548 { "load", MORE, 2, load_unit },
3549 { "cancel", MORE, 2, cancel_job },
3550 { "start", MORE, 2, start_unit },
3551 { "stop", MORE, 2, start_unit },
3552 { "reload", MORE, 2, start_unit },
3553 { "restart", MORE, 2, start_unit },
3554 { "try-restart", MORE, 2, start_unit },
3555 { "reload-or-restart", MORE, 2, start_unit },
3556 { "reload-or-try-restart", MORE, 2, start_unit },
3557 { "isolate", EQUAL, 2, start_unit },
3558 { "check", MORE, 2, check_unit },
3559 { "show", MORE, 1, show },
3560 { "status", MORE, 2, show },
3561 { "monitor", EQUAL, 1, monitor },
3562 { "dump", EQUAL, 1, dump },
3563 { "dot", EQUAL, 1, dot },
3564 { "snapshot", LESS, 2, snapshot },
3565 { "delete", MORE, 2, delete_snapshot },
3566 { "daemon-reload", EQUAL, 1, clear_jobs },
3567 { "daemon-reexec", EQUAL, 1, clear_jobs },
3568 { "daemon-exit", EQUAL, 1, clear_jobs },
3569 { "show-environment", EQUAL, 1, show_enviroment },
3570 { "set-environment", MORE, 2, set_environment },
3571 { "unset-environment", MORE, 2, set_environment },
3572 { "halt", EQUAL, 1, start_special },
3573 { "poweroff", EQUAL, 1, start_special },
3574 { "reboot", EQUAL, 1, start_special },
3575 { "default", EQUAL, 1, start_special },
3576 { "rescue", EQUAL, 1, start_special },
3577 { "emergency", EQUAL, 1, start_special },
3587 left = argc - optind;
3590 /* Special rule: no arguments means "list-units" */
3593 if (streq(argv[optind], "help")) {
3598 for (i = 0; i < ELEMENTSOF(verbs); i++)
3599 if (streq(argv[optind], verbs[i].verb))
3602 if (i >= ELEMENTSOF(verbs)) {
3603 log_error("Unknown operation %s", argv[optind]);
3608 switch (verbs[i].argc_cmp) {
3611 if (left != verbs[i].argc) {
3612 log_error("Invalid number of arguments.");
3619 if (left < verbs[i].argc) {
3620 log_error("Too few arguments.");
3627 if (left > verbs[i].argc) {
3628 log_error("Too many arguments.");
3635 assert_not_reached("Unknown comparison operator.");
3638 return verbs[i].dispatch(bus, argv + optind, left);
3641 static int reload_with_fallback(DBusConnection *bus) {
3645 /* First, try systemd via D-Bus. */
3646 if ((r = clear_jobs(bus, NULL, 0)) > 0)
3650 /* Nothing else worked, so let's try signals */
3651 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
3653 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
3654 log_error("kill() failed: %m");
3661 static int start_with_fallback(DBusConnection *bus) {
3666 /* First, try systemd via D-Bus. */
3667 if ((r = start_unit(bus, NULL, 0)) > 0)
3670 /* Hmm, talking to systemd via D-Bus didn't work. Then
3671 * let's try to talk to Upstart via D-Bus. */
3672 if ((r = talk_upstart()) > 0)
3676 /* Nothing else worked, so let's try
3678 if ((r = talk_initctl()) != 0)
3681 log_error("Failed to talk to init daemon.");
3685 warn_wall(arg_action);
3689 static int halt_main(DBusConnection *bus) {
3692 if (geteuid() != 0) {
3693 log_error("Must to be root.");
3697 if (!arg_dry && !arg_immediate)
3698 return start_with_fallback(bus);
3701 if ((r = utmp_put_shutdown(0)) < 0)
3702 log_warning("Failed to write utmp record: %s", strerror(-r));
3710 /* Make sure C-A-D is handled by the kernel from this
3712 reboot(RB_ENABLE_CAD);
3714 switch (arg_action) {
3717 log_info("Halting");
3718 reboot(RB_HALT_SYSTEM);
3721 case ACTION_POWEROFF:
3722 log_info("Powering off");
3723 reboot(RB_POWER_OFF);
3727 log_info("Rebooting");
3728 reboot(RB_AUTOBOOT);
3732 assert_not_reached("Unknown halt action.");
3735 /* We should never reach this. */
3739 static int runlevel_main(void) {
3740 int r, runlevel, previous;
3742 if ((r = utmp_get_runlevel(&runlevel, &previous)) < 0) {
3748 previous <= 0 ? 'N' : previous,
3749 runlevel <= 0 ? 'N' : runlevel);
3754 int main(int argc, char*argv[]) {
3756 DBusConnection *bus = NULL;
3759 dbus_error_init(&error);
3761 log_parse_environment();
3763 if ((r = parse_argv(argc, argv)) < 0)
3770 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
3771 * let's shortcut this */
3772 if (arg_action == ACTION_RUNLEVEL) {
3773 retval = runlevel_main() < 0;
3777 bus_connect(arg_session ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &bus, &private_bus, &error);
3779 switch (arg_action) {
3781 case ACTION_SYSTEMCTL: {
3784 log_error("Failed to get D-Bus connection: %s", error.message);
3788 retval = systemctl_main(bus, argc, argv) < 0;
3793 case ACTION_POWEROFF:
3795 retval = halt_main(bus) < 0;
3798 case ACTION_RUNLEVEL2:
3799 case ACTION_RUNLEVEL3:
3800 case ACTION_RUNLEVEL4:
3801 case ACTION_RUNLEVEL5:
3803 case ACTION_EMERGENCY:
3804 case ACTION_DEFAULT:
3805 retval = start_with_fallback(bus) < 0;
3810 retval = reload_with_fallback(bus) < 0;
3813 case ACTION_INVALID:
3814 case ACTION_RUNLEVEL:
3816 assert_not_reached("Unknown action");
3822 dbus_connection_close(bus);
3823 dbus_connection_unref(bus);
3826 dbus_error_free(&error);