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 if (isatty(STDOUT_FILENO))
192 printf("%-45s %-6s %-12s %-12s %-15s %s\n", "UNIT", "LOAD", "ACTIVE", "SUB", "JOB", "DESCRIPTION");
194 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
195 const char *id, *description, *load_state, *active_state, *sub_state, *unit_path, *job_type, *job_path, *dot;
198 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
199 log_error("Failed to parse reply.");
204 dbus_message_iter_recurse(&sub, &sub2);
206 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
207 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
208 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
209 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
210 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
211 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, true) < 0 ||
212 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &job_id, true) < 0 ||
213 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &job_type, true) < 0 ||
214 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, false) < 0) {
215 log_error("Failed to parse reply.");
220 if ((!arg_type || ((dot = strrchr(id, '.')) &&
221 streq(dot+1, arg_type))) &&
222 (arg_all || !streq(active_state, "inactive") || job_id > 0)) {
226 if (streq(active_state, "maintenance"))
227 fputs(ANSI_HIGHLIGHT_ON, stdout);
229 printf("%-45s %-6s %-12s %-12s%n", id, load_state, active_state, sub_state, &a);
232 printf(" => %-12s%n", job_type, &b);
236 if (a + b + 2 < columns()) {
240 printf(" %.*s", columns() - a - b - 2, description);
243 if (streq(active_state, "maintenance"))
244 fputs(ANSI_HIGHLIGHT_OFF, stdout);
250 dbus_message_iter_next(&sub);
253 if (isatty(STDOUT_FILENO)) {
255 printf("\nLOAD = Load State, reflects whether the unit configuration was properly loaded.\n"
256 "ACTIVE = Active State, the high-level unit activation state, i.e. generalization of the substate.\n"
257 "SUB = Substate, the low-level unit activation state, possible values depend on unit type.\n"
258 "JOB = Job, shows pending jobs for the unit.\n");
261 printf("\n%u units listed.\n", k);
263 printf("\n%u units listed. Pass --all to see inactive units, too.\n", k);
270 dbus_message_unref(m);
273 dbus_message_unref(reply);
275 dbus_error_free(&error);
280 static int dot_one_property(const char *name, const char *prop, DBusMessageIter *iter) {
281 static const char * const colors[] = {
282 "Requires", "[color=\"black\"]",
283 "RequiresOverridable", "[color=\"black\"]",
284 "Requisite", "[color=\"darkblue\"]",
285 "RequisiteOverridable", "[color=\"darkblue\"]",
286 "Wants", "[color=\"darkgrey\"]",
287 "Conflicts", "[color=\"red\"]",
288 "After", "[color=\"green\"]"
291 const char *c = NULL;
298 for (i = 0; i < ELEMENTSOF(colors); i += 2)
299 if (streq(colors[i], prop)) {
307 if (arg_dot != DOT_ALL)
308 if ((arg_dot == DOT_ORDER) != streq(prop, "After"))
311 switch (dbus_message_iter_get_arg_type(iter)) {
313 case DBUS_TYPE_ARRAY:
315 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
318 dbus_message_iter_recurse(iter, &sub);
320 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
323 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
324 dbus_message_iter_get_basic(&sub, &s);
325 printf("\t\"%s\"->\"%s\" %s;\n", name, s, c);
327 dbus_message_iter_next(&sub);
337 static int dot_one(DBusConnection *bus, const char *name, const char *path) {
338 DBusMessage *m = NULL, *reply = NULL;
339 const char *interface = "org.freedesktop.systemd1.Unit";
342 DBusMessageIter iter, sub, sub2, sub3;
347 dbus_error_init(&error);
349 if (!(m = dbus_message_new_method_call(
350 "org.freedesktop.systemd1",
352 "org.freedesktop.DBus.Properties",
354 log_error("Could not allocate message.");
359 if (!dbus_message_append_args(m,
360 DBUS_TYPE_STRING, &interface,
361 DBUS_TYPE_INVALID)) {
362 log_error("Could not append arguments to message.");
367 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
368 log_error("Failed to issue method call: %s", error.message);
373 if (!dbus_message_iter_init(reply, &iter) ||
374 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
375 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
376 log_error("Failed to parse reply.");
381 dbus_message_iter_recurse(&iter, &sub);
383 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
386 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
387 log_error("Failed to parse reply.");
392 dbus_message_iter_recurse(&sub, &sub2);
394 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &prop, true) < 0) {
395 log_error("Failed to parse reply.");
400 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
401 log_error("Failed to parse reply.");
406 dbus_message_iter_recurse(&sub2, &sub3);
408 if (dot_one_property(name, prop, &sub3)) {
409 log_error("Failed to parse reply.");
414 dbus_message_iter_next(&sub);
419 dbus_message_unref(m);
422 dbus_message_unref(reply);
424 dbus_error_free(&error);
429 static int dot(DBusConnection *bus, char **args, unsigned n) {
430 DBusMessage *m = NULL, *reply = NULL;
433 DBusMessageIter iter, sub, sub2;
435 dbus_error_init(&error);
439 if (!(m = dbus_message_new_method_call(
440 "org.freedesktop.systemd1",
441 "/org/freedesktop/systemd1",
442 "org.freedesktop.systemd1.Manager",
444 log_error("Could not allocate message.");
448 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
449 log_error("Failed to issue method call: %s", error.message);
454 if (!dbus_message_iter_init(reply, &iter) ||
455 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
456 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
457 log_error("Failed to parse reply.");
462 printf("digraph systemd {\n");
464 dbus_message_iter_recurse(&iter, &sub);
465 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
466 const char *id, *description, *load_state, *active_state, *sub_state, *unit_path;
468 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
469 log_error("Failed to parse reply.");
474 dbus_message_iter_recurse(&sub, &sub2);
476 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
477 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
478 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
479 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
480 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
481 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, true) < 0) {
482 log_error("Failed to parse reply.");
487 if ((r = dot_one(bus, id, unit_path)) < 0)
490 /* printf("\t\"%s\";\n", id); */
491 dbus_message_iter_next(&sub);
496 log_info(" Color legend: black = Requires\n"
497 " dark blue = Requisite\n"
498 " dark grey = Wants\n"
502 if (isatty(fileno(stdout)))
503 log_notice("-- You probably want to process this output with graphviz' dot tool.\n"
504 "-- Try a shell pipeline like 'systemctl dot | dot -Tsvg > systemd.svg'!\n");
510 dbus_message_unref(m);
513 dbus_message_unref(reply);
515 dbus_error_free(&error);
520 static int list_jobs(DBusConnection *bus, char **args, unsigned n) {
521 DBusMessage *m = NULL, *reply = NULL;
524 DBusMessageIter iter, sub, sub2;
527 dbus_error_init(&error);
531 if (!(m = dbus_message_new_method_call(
532 "org.freedesktop.systemd1",
533 "/org/freedesktop/systemd1",
534 "org.freedesktop.systemd1.Manager",
536 log_error("Could not allocate message.");
540 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
541 log_error("Failed to issue method call: %s", error.message);
546 if (!dbus_message_iter_init(reply, &iter) ||
547 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
548 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
549 log_error("Failed to parse reply.");
554 dbus_message_iter_recurse(&iter, &sub);
556 printf("%4s %-45s %-17s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
558 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
559 const char *name, *type, *state, *job_path, *unit_path;
562 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
563 log_error("Failed to parse reply.");
568 dbus_message_iter_recurse(&sub, &sub2);
570 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
571 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
572 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
573 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
574 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
575 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
576 log_error("Failed to parse reply.");
581 printf("%4u %-45s %-17s %-7s\n", id, name, type, state);
584 dbus_message_iter_next(&sub);
587 printf("\n%u jobs listed.\n", k);
592 dbus_message_unref(m);
595 dbus_message_unref(reply);
597 dbus_error_free(&error);
602 static int load_unit(DBusConnection *bus, char **args, unsigned n) {
603 DBusMessage *m = NULL, *reply = NULL;
608 dbus_error_init(&error);
613 for (i = 1; i < n; i++) {
615 if (!(m = dbus_message_new_method_call(
616 "org.freedesktop.systemd1",
617 "/org/freedesktop/systemd1",
618 "org.freedesktop.systemd1.Manager",
620 log_error("Could not allocate message.");
625 if (!dbus_message_append_args(m,
626 DBUS_TYPE_STRING, &args[i],
627 DBUS_TYPE_INVALID)) {
628 log_error("Could not append arguments to message.");
633 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
634 log_error("Failed to issue method call: %s", error.message);
639 dbus_message_unref(m);
640 dbus_message_unref(reply);
649 dbus_message_unref(m);
652 dbus_message_unref(reply);
654 dbus_error_free(&error);
659 static int cancel_job(DBusConnection *bus, char **args, unsigned n) {
660 DBusMessage *m = NULL, *reply = NULL;
665 dbus_error_init(&error);
670 for (i = 1; i < n; i++) {
674 if (!(m = dbus_message_new_method_call(
675 "org.freedesktop.systemd1",
676 "/org/freedesktop/systemd1",
677 "org.freedesktop.systemd1.Manager",
679 log_error("Could not allocate message.");
684 if ((r = safe_atou(args[i], &id)) < 0) {
685 log_error("Failed to parse job id: %s", strerror(-r));
689 assert_cc(sizeof(uint32_t) == sizeof(id));
690 if (!dbus_message_append_args(m,
691 DBUS_TYPE_UINT32, &id,
692 DBUS_TYPE_INVALID)) {
693 log_error("Could not append arguments to message.");
698 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
699 log_error("Failed to issue method call: %s", error.message);
704 if (!dbus_message_get_args(reply, &error,
705 DBUS_TYPE_OBJECT_PATH, &path,
706 DBUS_TYPE_INVALID)) {
707 log_error("Failed to parse reply: %s", error.message);
712 dbus_message_unref(m);
713 if (!(m = dbus_message_new_method_call(
714 "org.freedesktop.systemd1",
716 "org.freedesktop.systemd1.Job",
718 log_error("Could not allocate message.");
723 dbus_message_unref(reply);
724 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
725 log_error("Failed to issue method call: %s", error.message);
730 dbus_message_unref(m);
731 dbus_message_unref(reply);
739 dbus_message_unref(m);
742 dbus_message_unref(reply);
744 dbus_error_free(&error);
749 static bool unit_need_daemon_reload(DBusConnection *bus, const char *unit) {
750 DBusMessage *m, *reply;
751 dbus_bool_t b = FALSE;
752 DBusMessageIter iter, sub;
754 *interface = "org.freedesktop.systemd1.Unit",
755 *property = "NeedDaemonReload",
758 /* We ignore all errors here, since this is used to show a warning only */
760 if (!(m = dbus_message_new_method_call(
761 "org.freedesktop.systemd1",
762 "/org/freedesktop/systemd1",
763 "org.freedesktop.systemd1.Manager",
767 if (!dbus_message_append_args(m,
768 DBUS_TYPE_STRING, &unit,
772 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
775 if (!dbus_message_get_args(reply, NULL,
776 DBUS_TYPE_OBJECT_PATH, &path,
780 dbus_message_unref(m);
781 if (!(m = dbus_message_new_method_call(
782 "org.freedesktop.systemd1",
784 "org.freedesktop.DBus.Properties",
788 if (!dbus_message_append_args(m,
789 DBUS_TYPE_STRING, &interface,
790 DBUS_TYPE_STRING, &property,
791 DBUS_TYPE_INVALID)) {
795 dbus_message_unref(reply);
796 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
799 if (!dbus_message_iter_init(reply, &iter) ||
800 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
803 dbus_message_iter_recurse(&iter, &sub);
805 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_BOOLEAN)
808 dbus_message_iter_get_basic(&sub, &b);
812 dbus_message_unref(m);
815 dbus_message_unref(reply);
820 typedef struct WaitData {
825 static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
833 dbus_error_init(&error);
835 log_debug("Got D-Bus request: %s.%s() on %s",
836 dbus_message_get_interface(message),
837 dbus_message_get_member(message),
838 dbus_message_get_path(message));
840 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
841 log_error("Warning! D-Bus connection terminated.");
842 dbus_connection_close(connection);
844 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
847 dbus_bool_t success = true;
849 if (!dbus_message_get_args(message, &error,
850 DBUS_TYPE_UINT32, &id,
851 DBUS_TYPE_OBJECT_PATH, &path,
852 DBUS_TYPE_BOOLEAN, &success,
854 log_error("Failed to parse message: %s", error.message);
858 if ((p = set_remove(d->set, (char*) path)))
866 dbus_error_free(&error);
867 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
870 static int enable_wait_for_jobs(DBusConnection *bus) {
878 dbus_error_init(&error);
879 dbus_bus_add_match(bus,
881 "sender='org.freedesktop.systemd1',"
882 "interface='org.freedesktop.systemd1.Manager',"
883 "member='JobRemoved',"
884 "path='/org/freedesktop/systemd1'",
887 if (dbus_error_is_set(&error)) {
888 log_error("Failed to add match: %s", error.message);
889 dbus_error_free(&error);
893 /* This is slightly dirty, since we don't undo the match registrations. */
897 static int wait_for_jobs(DBusConnection *bus, Set *s) {
908 if (!dbus_connection_add_filter(bus, wait_filter, &d, NULL)) {
909 log_error("Failed to add filter.");
914 while (!set_isempty(s) &&
915 dbus_connection_read_write_dispatch(bus, -1))
918 if (!arg_quiet && d.failed)
919 log_error("Job failed, see logs for details.");
921 r = d.failed ? -EIO : 0;
924 /* This is slightly dirty, since we don't undo the filter registration. */
929 static int start_unit_one(
936 DBusMessage *m = NULL, *reply = NULL;
945 assert(arg_no_block || s);
947 dbus_error_init(&error);
949 if (!(m = dbus_message_new_method_call(
950 "org.freedesktop.systemd1",
951 "/org/freedesktop/systemd1",
952 "org.freedesktop.systemd1.Manager",
954 log_error("Could not allocate message.");
959 if (!dbus_message_append_args(m,
960 DBUS_TYPE_STRING, &name,
961 DBUS_TYPE_STRING, &mode,
962 DBUS_TYPE_INVALID)) {
963 log_error("Could not append arguments to message.");
968 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
970 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
971 /* There's always a fallback possible for
977 log_error("Failed to issue method call: %s", error.message);
982 if (!dbus_message_get_args(reply, &error,
983 DBUS_TYPE_OBJECT_PATH, &path,
984 DBUS_TYPE_INVALID)) {
985 log_error("Failed to parse reply: %s", error.message);
990 if (unit_need_daemon_reload(bus, name))
991 log_warning("Unit file of created job changed on disk, 'systemctl %s daemon-reload' recommended.",
992 arg_session ? "--session" : "--system");
997 if (!(p = strdup(path))) {
998 log_error("Failed to duplicate path.");
1003 if ((r = set_put(s, p)) < 0) {
1005 log_error("Failed to add path to set.");
1014 dbus_message_unref(m);
1017 dbus_message_unref(reply);
1019 dbus_error_free(&error);
1024 static enum action verb_to_action(const char *verb) {
1025 if (streq(verb, "halt"))
1027 else if (streq(verb, "poweroff"))
1028 return ACTION_POWEROFF;
1029 else if (streq(verb, "reboot"))
1030 return ACTION_REBOOT;
1031 else if (streq(verb, "rescue"))
1032 return ACTION_RESCUE;
1033 else if (streq(verb, "emergency"))
1034 return ACTION_EMERGENCY;
1035 else if (streq(verb, "default"))
1036 return ACTION_DEFAULT;
1038 return ACTION_INVALID;
1041 static int start_unit(DBusConnection *bus, char **args, unsigned n) {
1043 static const char * const table[_ACTION_MAX] = {
1044 [ACTION_HALT] = SPECIAL_HALT_TARGET,
1045 [ACTION_POWEROFF] = SPECIAL_POWEROFF_TARGET,
1046 [ACTION_REBOOT] = SPECIAL_REBOOT_TARGET,
1047 [ACTION_RUNLEVEL2] = SPECIAL_RUNLEVEL2_TARGET,
1048 [ACTION_RUNLEVEL3] = SPECIAL_RUNLEVEL3_TARGET,
1049 [ACTION_RUNLEVEL4] = SPECIAL_RUNLEVEL4_TARGET,
1050 [ACTION_RUNLEVEL5] = SPECIAL_RUNLEVEL5_TARGET,
1051 [ACTION_RESCUE] = SPECIAL_RESCUE_TARGET,
1052 [ACTION_EMERGENCY] = SPECIAL_EMERGENCY_TARGET,
1053 [ACTION_DEFAULT] = SPECIAL_DEFAULT_TARGET
1058 const char *method, *mode, *one_name;
1063 if (arg_action == ACTION_SYSTEMCTL) {
1065 streq(args[0], "stop") ? "StopUnit" :
1066 streq(args[0], "reload") ? "ReloadUnit" :
1067 streq(args[0], "restart") ? "RestartUnit" :
1068 streq(args[0], "try-restart") ? "TryRestartUnit" :
1069 streq(args[0], "reload-or-restart") ? "ReloadOrRestartUnit" :
1070 streq(args[0], "reload-or-try-restart") ? "ReloadOrTryRestartUnit" :
1074 (streq(args[0], "isolate") ||
1075 streq(args[0], "rescue") ||
1076 streq(args[0], "emergency")) ? "isolate" :
1080 one_name = table[verb_to_action(args[0])];
1083 assert(arg_action < ELEMENTSOF(table));
1084 assert(table[arg_action]);
1086 method = "StartUnit";
1088 mode = (arg_action == ACTION_EMERGENCY ||
1089 arg_action == ACTION_RESCUE) ? "isolate" : "replace";
1091 one_name = table[arg_action];
1094 if (!arg_no_block) {
1095 if ((r = enable_wait_for_jobs(bus)) < 0) {
1096 log_error("Could not watch jobs: %s", strerror(-r));
1100 if (!(s = set_new(string_hash_func, string_compare_func))) {
1101 log_error("Failed to allocate set.");
1110 if ((r = start_unit_one(bus, method, one_name, mode, s)) <= 0)
1113 for (i = 1; i < n; i++)
1114 if ((r = start_unit_one(bus, method, args[i], mode, s)) < 0)
1119 r = wait_for_jobs(bus, s);
1128 static int start_special(DBusConnection *bus, char **args, unsigned n) {
1134 r = start_unit(bus, args, n);
1137 warn_wall(verb_to_action(args[0]));
1142 static int check_unit(DBusConnection *bus, char **args, unsigned n) {
1143 DBusMessage *m = NULL, *reply = NULL;
1145 *interface = "org.freedesktop.systemd1.Unit",
1146 *property = "ActiveState";
1147 int r = -EADDRNOTAVAIL;
1154 dbus_error_init(&error);
1156 for (i = 1; i < n; i++) {
1157 const char *path = NULL;
1159 DBusMessageIter iter, sub;
1161 if (!(m = dbus_message_new_method_call(
1162 "org.freedesktop.systemd1",
1163 "/org/freedesktop/systemd1",
1164 "org.freedesktop.systemd1.Manager",
1166 log_error("Could not allocate message.");
1171 if (!dbus_message_append_args(m,
1172 DBUS_TYPE_STRING, &args[i],
1173 DBUS_TYPE_INVALID)) {
1174 log_error("Could not append arguments to message.");
1179 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1181 /* Hmm, cannot figure out anything about this unit... */
1185 dbus_error_free(&error);
1189 if (!dbus_message_get_args(reply, &error,
1190 DBUS_TYPE_OBJECT_PATH, &path,
1191 DBUS_TYPE_INVALID)) {
1192 log_error("Failed to parse reply: %s", error.message);
1197 dbus_message_unref(m);
1198 if (!(m = dbus_message_new_method_call(
1199 "org.freedesktop.systemd1",
1201 "org.freedesktop.DBus.Properties",
1203 log_error("Could not allocate message.");
1208 if (!dbus_message_append_args(m,
1209 DBUS_TYPE_STRING, &interface,
1210 DBUS_TYPE_STRING, &property,
1211 DBUS_TYPE_INVALID)) {
1212 log_error("Could not append arguments to message.");
1217 dbus_message_unref(reply);
1218 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1219 log_error("Failed to issue method call: %s", error.message);
1224 if (!dbus_message_iter_init(reply, &iter) ||
1225 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1226 log_error("Failed to parse reply.");
1231 dbus_message_iter_recurse(&iter, &sub);
1233 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1234 log_error("Failed to parse reply.");
1239 dbus_message_iter_get_basic(&sub, &state);
1244 if (streq(state, "active") || startswith(state, "reloading"))
1247 dbus_message_unref(m);
1248 dbus_message_unref(reply);
1254 dbus_message_unref(m);
1257 dbus_message_unref(reply);
1259 dbus_error_free(&error);
1264 typedef struct ExecStatusInfo {
1270 usec_t start_timestamp;
1271 usec_t exit_timestamp;
1276 LIST_FIELDS(struct ExecStatusInfo, exec);
1279 static void exec_status_info_free(ExecStatusInfo *i) {
1287 static int exec_status_info_deserialize(DBusMessageIter *sub, ExecStatusInfo *i) {
1288 uint64_t start_timestamp, exit_timestamp;
1289 DBusMessageIter sub2, sub3;
1293 int32_t code, status;
1299 if (dbus_message_iter_get_arg_type(sub) != DBUS_TYPE_STRUCT)
1302 dbus_message_iter_recurse(sub, &sub2);
1304 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
1307 if (!(i->path = strdup(path)))
1310 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
1311 dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
1315 dbus_message_iter_recurse(&sub2, &sub3);
1316 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1317 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1318 dbus_message_iter_next(&sub3);
1323 if (!(i->argv = new0(char*, n+1)))
1327 dbus_message_iter_recurse(&sub2, &sub3);
1328 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1331 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1332 dbus_message_iter_get_basic(&sub3, &s);
1333 dbus_message_iter_next(&sub3);
1335 if (!(i->argv[n++] = strdup(s)))
1339 if (!dbus_message_iter_next(&sub2) ||
1340 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, true) < 0 ||
1341 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp, true) < 0 ||
1342 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp, true) < 0 ||
1343 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) < 0 ||
1344 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) < 0 ||
1345 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) < 0)
1349 i->start_timestamp = (usec_t) start_timestamp;
1350 i->exit_timestamp = (usec_t) exit_timestamp;
1351 i->pid = (pid_t) pid;
1358 typedef struct UnitStatusInfo {
1360 const char *load_state;
1361 const char *active_state;
1362 const char *sub_state;
1364 const char *description;
1366 const char *fragment_path;
1367 const char *default_control_group;
1369 bool need_daemon_reload;
1374 const char *status_text;
1377 usec_t start_timestamp;
1378 usec_t exit_timestamp;
1380 int exit_code, exit_status;
1383 unsigned n_accepted;
1384 unsigned n_connections;
1388 const char *sysfs_path;
1390 /* Mount, Automount */
1396 LIST_HEAD(ExecStatusInfo, exec);
1399 static void print_status_info(UnitStatusInfo *i) {
1404 /* This shows pretty information about a unit. See
1405 * print_property() for a low-level property printer */
1407 printf("%s", strna(i->id));
1409 if (i->description && !streq_ptr(i->id, i->description))
1410 printf(" - %s", i->description);
1414 if (i->fragment_path)
1415 printf("\t Loaded: %s (%s)\n", strna(i->load_state), i->fragment_path);
1416 else if (streq_ptr(i->load_state, "failed"))
1417 printf("\t Loaded: " ANSI_HIGHLIGHT_ON "%s" ANSI_HIGHLIGHT_OFF "\n", strna(i->load_state));
1419 printf("\t Loaded: %s\n", strna(i->load_state));
1421 if (streq_ptr(i->active_state, "maintenance")) {
1422 if (streq_ptr(i->active_state, i->sub_state))
1423 printf("\t Active: " ANSI_HIGHLIGHT_ON "%s" ANSI_HIGHLIGHT_OFF "\n",
1424 strna(i->active_state));
1426 printf("\t Active: " ANSI_HIGHLIGHT_ON "%s (%s)" ANSI_HIGHLIGHT_OFF "\n",
1427 strna(i->active_state),
1428 strna(i->sub_state));
1430 if (streq_ptr(i->active_state, i->sub_state))
1431 printf("\t Active: %s\n",
1432 strna(i->active_state));
1434 printf("\t Active: %s (%s)\n",
1435 strna(i->active_state),
1436 strna(i->sub_state));
1440 printf("\t Device: %s\n", i->sysfs_path);
1442 printf("\t Where: %s\n", i->where);
1444 printf("\t What: %s\n", i->what);
1447 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
1449 LIST_FOREACH(exec, p, i->exec) {
1452 /* Only show exited processes here */
1456 t = strv_join(p->argv, " ");
1457 printf("\t Exited: %u (%s, code=%s, ", p->pid, strna(t), sigchld_code_to_string(p->code));
1460 if (p->code == CLD_EXITED)
1461 printf("status=%i", p->status);
1463 printf("signal=%s", signal_to_string(p->status));
1466 if (i->main_pid == p->pid &&
1467 i->start_timestamp == p->start_timestamp &&
1468 i->exit_timestamp == p->start_timestamp)
1469 /* Let's not show this twice */
1472 if (p->pid == i->control_pid)
1476 if (i->main_pid > 0 || i->control_pid > 0) {
1479 if (i->main_pid > 0) {
1480 printf(" Main: %u", (unsigned) i->main_pid);
1484 get_process_name(i->main_pid, &t);
1490 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
1492 if (i->exit_code == CLD_EXITED)
1493 printf("status=%i", i->exit_status);
1495 printf("signal=%s", signal_to_string(i->exit_status));
1500 if (i->main_pid > 0 && i->control_pid > 0)
1503 if (i->control_pid > 0) {
1506 printf(" Control: %u", (unsigned) i->control_pid);
1508 get_process_name(i->control_pid, &t);
1519 printf("\t Status: \"%s\"\n", i->status_text);
1521 if (i->default_control_group) {
1524 printf("\t CGroup: %s\n", i->default_control_group);
1526 if ((c = columns()) > 18)
1531 show_cgroup_by_path(i->default_control_group, "\t\t ", c);
1534 if (i->need_daemon_reload)
1535 printf("\n" ANSI_HIGHLIGHT_ON "Warning:" ANSI_HIGHLIGHT_OFF " Unit file changed on disk, 'systemctl %s daemon-reload' recommended.\n",
1536 arg_session ? "--session" : "--system");
1539 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
1541 switch (dbus_message_iter_get_arg_type(iter)) {
1543 case DBUS_TYPE_STRING: {
1546 dbus_message_iter_get_basic(iter, &s);
1549 if (streq(name, "Id"))
1551 else if (streq(name, "LoadState"))
1553 else if (streq(name, "ActiveState"))
1554 i->active_state = s;
1555 else if (streq(name, "SubState"))
1557 else if (streq(name, "Description"))
1559 else if (streq(name, "FragmentPath"))
1560 i->fragment_path = s;
1561 else if (streq(name, "DefaultControlGroup"))
1562 i->default_control_group = s;
1563 else if (streq(name, "StatusText"))
1565 else if (streq(name, "SysFSPath"))
1567 else if (streq(name, "Where"))
1569 else if (streq(name, "What"))
1576 case DBUS_TYPE_BOOLEAN: {
1579 dbus_message_iter_get_basic(iter, &b);
1581 if (streq(name, "Accept"))
1583 else if (streq(name, "NeedDaemonReload"))
1584 i->need_daemon_reload = b;
1589 case DBUS_TYPE_UINT32: {
1592 dbus_message_iter_get_basic(iter, &u);
1594 if (streq(name, "MainPID")) {
1596 i->main_pid = (pid_t) u;
1599 } else if (streq(name, "ControlPID"))
1600 i->control_pid = (pid_t) u;
1601 else if (streq(name, "ExecMainPID")) {
1603 i->main_pid = (pid_t) u;
1604 } else if (streq(name, "NAccepted"))
1606 else if (streq(name, "NConnections"))
1607 i->n_connections = u;
1612 case DBUS_TYPE_INT32: {
1615 dbus_message_iter_get_basic(iter, &j);
1617 if (streq(name, "ExecMainCode"))
1618 i->exit_code = (int) j;
1619 else if (streq(name, "ExecMainStatus"))
1620 i->exit_status = (int) j;
1625 case DBUS_TYPE_UINT64: {
1628 dbus_message_iter_get_basic(iter, &u);
1630 if (streq(name, "ExecMainStartTimestamp"))
1631 i->start_timestamp = (usec_t) u;
1632 else if (streq(name, "ExecMainExitTimestamp"))
1633 i->exit_timestamp = (usec_t) u;
1638 case DBUS_TYPE_ARRAY: {
1640 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT &&
1641 startswith(name, "Exec")) {
1642 DBusMessageIter sub;
1644 dbus_message_iter_recurse(iter, &sub);
1645 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1646 ExecStatusInfo *info;
1649 if (!(info = new0(ExecStatusInfo, 1)))
1652 if ((r = exec_status_info_deserialize(&sub, info)) < 0) {
1657 LIST_PREPEND(ExecStatusInfo, exec, i->exec, info);
1659 dbus_message_iter_next(&sub);
1670 static int print_property(const char *name, DBusMessageIter *iter) {
1674 /* This is a low-level property printer, see
1675 * print_status_info() for the nicer output */
1677 if (arg_property && !streq(name, arg_property))
1680 switch (dbus_message_iter_get_arg_type(iter)) {
1682 case DBUS_TYPE_STRING: {
1684 dbus_message_iter_get_basic(iter, &s);
1686 if (arg_all || s[0])
1687 printf("%s=%s\n", name, s);
1692 case DBUS_TYPE_BOOLEAN: {
1694 dbus_message_iter_get_basic(iter, &b);
1695 printf("%s=%s\n", name, yes_no(b));
1700 case DBUS_TYPE_UINT64: {
1702 dbus_message_iter_get_basic(iter, &u);
1704 /* Yes, heuristics! But we can change this check
1705 * should it turn out to not be sufficient */
1707 if (strstr(name, "Timestamp")) {
1708 char timestamp[FORMAT_TIMESTAMP_MAX], *t;
1710 if ((t = format_timestamp(timestamp, sizeof(timestamp), u)) || arg_all)
1711 printf("%s=%s\n", name, strempty(t));
1712 } else if (strstr(name, "USec")) {
1713 char timespan[FORMAT_TIMESPAN_MAX];
1715 printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u));
1717 printf("%s=%llu\n", name, (unsigned long long) u);
1722 case DBUS_TYPE_UINT32: {
1724 dbus_message_iter_get_basic(iter, &u);
1726 if (strstr(name, "UMask") || strstr(name, "Mode"))
1727 printf("%s=%04o\n", name, u);
1729 printf("%s=%u\n", name, (unsigned) u);
1734 case DBUS_TYPE_INT32: {
1736 dbus_message_iter_get_basic(iter, &i);
1738 printf("%s=%i\n", name, (int) i);
1742 case DBUS_TYPE_STRUCT: {
1743 DBusMessageIter sub;
1744 dbus_message_iter_recurse(iter, &sub);
1746 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
1749 dbus_message_iter_get_basic(&sub, &u);
1752 printf("%s=%u\n", name, (unsigned) u);
1754 printf("%s=\n", name);
1757 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
1760 dbus_message_iter_get_basic(&sub, &s);
1762 if (arg_all || s[0])
1763 printf("%s=%s\n", name, s);
1771 case DBUS_TYPE_ARRAY:
1773 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
1774 DBusMessageIter sub;
1777 dbus_message_iter_recurse(iter, &sub);
1779 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1780 printf("%s=", name);
1782 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1785 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
1786 dbus_message_iter_get_basic(&sub, &s);
1787 printf("%s%s", space ? " " : "", s);
1790 dbus_message_iter_next(&sub);
1798 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_BYTE) {
1799 DBusMessageIter sub;
1801 dbus_message_iter_recurse(iter, &sub);
1803 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1804 printf("%s=", name);
1806 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1809 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_BYTE);
1810 dbus_message_iter_get_basic(&sub, &u);
1813 dbus_message_iter_next(&sub);
1821 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
1822 DBusMessageIter sub, sub2;
1824 dbus_message_iter_recurse(iter, &sub);
1825 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1826 const char *type, *path;
1828 dbus_message_iter_recurse(&sub, &sub2);
1830 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
1831 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
1832 printf("%s=%s\n", type, path);
1834 dbus_message_iter_next(&sub);
1839 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
1840 DBusMessageIter sub, sub2;
1842 dbus_message_iter_recurse(iter, &sub);
1843 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1845 uint64_t value, next_elapse;
1847 dbus_message_iter_recurse(&sub, &sub2);
1849 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
1850 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
1851 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
1852 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
1854 printf("%s={ value=%s ; next_elapse=%s }\n",
1856 format_timespan(timespan1, sizeof(timespan1), value),
1857 format_timespan(timespan2, sizeof(timespan2), next_elapse));
1860 dbus_message_iter_next(&sub);
1865 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
1866 DBusMessageIter sub;
1868 dbus_message_iter_recurse(iter, &sub);
1869 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1870 ExecStatusInfo info;
1873 if (exec_status_info_deserialize(&sub, &info) >= 0) {
1874 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
1877 t = strv_join(info.argv, " ");
1879 printf("%s={ path=%s ; argv[]=%s ; ignore=%s ; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n",
1883 yes_no(info.ignore),
1884 strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)),
1885 strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)),
1886 (unsigned) info. pid,
1887 sigchld_code_to_string(info.code),
1889 info.code == CLD_EXITED ? "" : "/",
1890 strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status)));
1896 strv_free(info.argv);
1898 dbus_message_iter_next(&sub);
1908 printf("%s=[unprintable]\n", name);
1913 static int show_one(DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
1914 DBusMessage *m = NULL, *reply = NULL;
1915 const char *interface = "";
1918 DBusMessageIter iter, sub, sub2, sub3;
1919 UnitStatusInfo info;
1927 dbus_error_init(&error);
1929 if (!(m = dbus_message_new_method_call(
1930 "org.freedesktop.systemd1",
1932 "org.freedesktop.DBus.Properties",
1934 log_error("Could not allocate message.");
1939 if (!dbus_message_append_args(m,
1940 DBUS_TYPE_STRING, &interface,
1941 DBUS_TYPE_INVALID)) {
1942 log_error("Could not append arguments to message.");
1947 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1948 log_error("Failed to issue method call: %s", error.message);
1953 if (!dbus_message_iter_init(reply, &iter) ||
1954 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
1955 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
1956 log_error("Failed to parse reply.");
1961 dbus_message_iter_recurse(&iter, &sub);
1968 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1971 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
1972 log_error("Failed to parse reply.");
1977 dbus_message_iter_recurse(&sub, &sub2);
1979 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
1980 log_error("Failed to parse reply.");
1985 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
1986 log_error("Failed to parse reply.");
1991 dbus_message_iter_recurse(&sub2, &sub3);
1993 if (show_properties)
1994 r = print_property(name, &sub3);
1996 r = status_property(name, &sub3, &info);
1999 log_error("Failed to parse reply.");
2004 dbus_message_iter_next(&sub);
2007 if (!show_properties)
2008 print_status_info(&info);
2010 while ((p = info.exec)) {
2011 LIST_REMOVE(ExecStatusInfo, exec, info.exec, p);
2012 exec_status_info_free(p);
2019 dbus_message_unref(m);
2022 dbus_message_unref(reply);
2024 dbus_error_free(&error);
2029 static int show(DBusConnection *bus, char **args, unsigned n) {
2030 DBusMessage *m = NULL, *reply = NULL;
2034 bool show_properties, new_line = false;
2039 dbus_error_init(&error);
2041 show_properties = !streq(args[0], "status");
2043 if (show_properties && n <= 1) {
2044 /* If not argument is specified inspect the manager
2047 r = show_one(bus, "/org/freedesktop/systemd1", show_properties, &new_line);
2051 for (i = 1; i < n; i++) {
2052 const char *path = NULL;
2055 if (!show_properties || safe_atou32(args[i], &id) < 0) {
2057 if (!(m = dbus_message_new_method_call(
2058 "org.freedesktop.systemd1",
2059 "/org/freedesktop/systemd1",
2060 "org.freedesktop.systemd1.Manager",
2062 log_error("Could not allocate message.");
2067 if (!dbus_message_append_args(m,
2068 DBUS_TYPE_STRING, &args[i],
2069 DBUS_TYPE_INVALID)) {
2070 log_error("Could not append arguments to message.");
2075 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2077 if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
2078 log_error("Failed to issue method call: %s", error.message);
2083 dbus_error_free(&error);
2085 dbus_message_unref(m);
2086 if (!(m = dbus_message_new_method_call(
2087 "org.freedesktop.systemd1",
2088 "/org/freedesktop/systemd1",
2089 "org.freedesktop.systemd1.Manager",
2091 log_error("Could not allocate message.");
2096 if (!dbus_message_append_args(m,
2097 DBUS_TYPE_STRING, &args[i],
2098 DBUS_TYPE_INVALID)) {
2099 log_error("Could not append arguments to message.");
2104 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2105 log_error("Failed to issue method call: %s", error.message);
2113 if (!(m = dbus_message_new_method_call(
2114 "org.freedesktop.systemd1",
2115 "/org/freedesktop/systemd1",
2116 "org.freedesktop.systemd1.Manager",
2118 log_error("Could not allocate message.");
2123 if (!dbus_message_append_args(m,
2124 DBUS_TYPE_UINT32, &id,
2125 DBUS_TYPE_INVALID)) {
2126 log_error("Could not append arguments to message.");
2131 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2132 log_error("Failed to issue method call: %s", error.message);
2138 if (!dbus_message_get_args(reply, &error,
2139 DBUS_TYPE_OBJECT_PATH, &path,
2140 DBUS_TYPE_INVALID)) {
2141 log_error("Failed to parse reply: %s", error.message);
2146 if ((r = show_one(bus, path, show_properties, &new_line)) < 0)
2149 dbus_message_unref(m);
2150 dbus_message_unref(reply);
2158 dbus_message_unref(m);
2161 dbus_message_unref(reply);
2163 dbus_error_free(&error);
2168 static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
2170 DBusMessage *m = NULL, *reply = NULL;
2175 dbus_error_init(&error);
2177 log_debug("Got D-Bus request: %s.%s() on %s",
2178 dbus_message_get_interface(message),
2179 dbus_message_get_member(message),
2180 dbus_message_get_path(message));
2182 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
2183 log_error("Warning! D-Bus connection terminated.");
2184 dbus_connection_close(connection);
2186 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") ||
2187 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
2188 const char *id, *path;
2190 if (!dbus_message_get_args(message, &error,
2191 DBUS_TYPE_STRING, &id,
2192 DBUS_TYPE_OBJECT_PATH, &path,
2194 log_error("Failed to parse message: %s", error.message);
2195 else if (streq(dbus_message_get_member(message), "UnitNew"))
2196 printf("Unit %s added.\n", id);
2198 printf("Unit %s removed.\n", id);
2200 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew") ||
2201 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
2205 if (!dbus_message_get_args(message, &error,
2206 DBUS_TYPE_UINT32, &id,
2207 DBUS_TYPE_OBJECT_PATH, &path,
2209 log_error("Failed to parse message: %s", error.message);
2210 else if (streq(dbus_message_get_member(message), "JobNew"))
2211 printf("Job %u added.\n", id);
2213 printf("Job %u removed.\n", id);
2216 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Unit", "Changed") ||
2217 dbus_message_is_signal(message, "org.freedesktop.systemd1.Job", "Changed")) {
2219 const char *path, *interface, *property = "Id";
2220 DBusMessageIter iter, sub;
2222 path = dbus_message_get_path(message);
2223 interface = dbus_message_get_interface(message);
2225 if (!(m = dbus_message_new_method_call(
2226 "org.freedesktop.systemd1",
2228 "org.freedesktop.DBus.Properties",
2230 log_error("Could not allocate message.");
2234 if (!dbus_message_append_args(m,
2235 DBUS_TYPE_STRING, &interface,
2236 DBUS_TYPE_STRING, &property,
2237 DBUS_TYPE_INVALID)) {
2238 log_error("Could not append arguments to message.");
2242 if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
2243 log_error("Failed to issue method call: %s", error.message);
2247 if (!dbus_message_iter_init(reply, &iter) ||
2248 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2249 log_error("Failed to parse reply.");
2253 dbus_message_iter_recurse(&iter, &sub);
2255 if (streq(interface, "org.freedesktop.systemd1.Unit")) {
2258 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2259 log_error("Failed to parse reply.");
2263 dbus_message_iter_get_basic(&sub, &id);
2264 printf("Unit %s changed.\n", id);
2268 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32) {
2269 log_error("Failed to parse reply.");
2273 dbus_message_iter_get_basic(&sub, &id);
2274 printf("Job %u changed.\n", id);
2280 dbus_message_unref(m);
2283 dbus_message_unref(reply);
2285 dbus_error_free(&error);
2286 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2290 dbus_message_unref(m);
2293 dbus_message_unref(reply);
2295 dbus_error_free(&error);
2296 return DBUS_HANDLER_RESULT_NEED_MEMORY;
2299 static int monitor(DBusConnection *bus, char **args, unsigned n) {
2300 DBusMessage *m = NULL, *reply = NULL;
2304 dbus_error_init(&error);
2307 dbus_bus_add_match(bus,
2309 "sender='org.freedesktop.systemd1',"
2310 "interface='org.freedesktop.systemd1.Manager',"
2311 "path='/org/freedesktop/systemd1'",
2314 if (dbus_error_is_set(&error)) {
2315 log_error("Failed to add match: %s", error.message);
2320 dbus_bus_add_match(bus,
2322 "sender='org.freedesktop.systemd1',"
2323 "interface='org.freedesktop.systemd1.Unit',"
2327 if (dbus_error_is_set(&error)) {
2328 log_error("Failed to add match: %s", error.message);
2333 dbus_bus_add_match(bus,
2335 "sender='org.freedesktop.systemd1',"
2336 "interface='org.freedesktop.systemd1.Job',"
2340 if (dbus_error_is_set(&error)) {
2341 log_error("Failed to add match: %s", error.message);
2347 if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
2348 log_error("Failed to add filter.");
2353 if (!(m = dbus_message_new_method_call(
2354 "org.freedesktop.systemd1",
2355 "/org/freedesktop/systemd1",
2356 "org.freedesktop.systemd1.Manager",
2358 log_error("Could not allocate message.");
2363 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2364 log_error("Failed to issue method call: %s", error.message);
2369 while (dbus_connection_read_write_dispatch(bus, -1))
2376 /* This is slightly dirty, since we don't undo the filter or the matches. */
2379 dbus_message_unref(m);
2382 dbus_message_unref(reply);
2384 dbus_error_free(&error);
2389 static int dump(DBusConnection *bus, char **args, unsigned n) {
2390 DBusMessage *m = NULL, *reply = NULL;
2395 dbus_error_init(&error);
2397 if (!(m = dbus_message_new_method_call(
2398 "org.freedesktop.systemd1",
2399 "/org/freedesktop/systemd1",
2400 "org.freedesktop.systemd1.Manager",
2402 log_error("Could not allocate message.");
2406 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2407 log_error("Failed to issue method call: %s", error.message);
2412 if (!dbus_message_get_args(reply, &error,
2413 DBUS_TYPE_STRING, &text,
2414 DBUS_TYPE_INVALID)) {
2415 log_error("Failed to parse reply: %s", error.message);
2420 fputs(text, stdout);
2426 dbus_message_unref(m);
2429 dbus_message_unref(reply);
2431 dbus_error_free(&error);
2436 static int snapshot(DBusConnection *bus, char **args, unsigned n) {
2437 DBusMessage *m = NULL, *reply = NULL;
2440 const char *name = "", *path, *id;
2441 dbus_bool_t cleanup = FALSE;
2442 DBusMessageIter iter, sub;
2444 *interface = "org.freedesktop.systemd1.Unit",
2447 dbus_error_init(&error);
2449 if (!(m = dbus_message_new_method_call(
2450 "org.freedesktop.systemd1",
2451 "/org/freedesktop/systemd1",
2452 "org.freedesktop.systemd1.Manager",
2453 "CreateSnapshot"))) {
2454 log_error("Could not allocate message.");
2461 if (!dbus_message_append_args(m,
2462 DBUS_TYPE_STRING, &name,
2463 DBUS_TYPE_BOOLEAN, &cleanup,
2464 DBUS_TYPE_INVALID)) {
2465 log_error("Could not append arguments to message.");
2470 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2471 log_error("Failed to issue method call: %s", error.message);
2476 if (!dbus_message_get_args(reply, &error,
2477 DBUS_TYPE_OBJECT_PATH, &path,
2478 DBUS_TYPE_INVALID)) {
2479 log_error("Failed to parse reply: %s", error.message);
2484 dbus_message_unref(m);
2485 if (!(m = dbus_message_new_method_call(
2486 "org.freedesktop.systemd1",
2488 "org.freedesktop.DBus.Properties",
2490 log_error("Could not allocate message.");
2494 if (!dbus_message_append_args(m,
2495 DBUS_TYPE_STRING, &interface,
2496 DBUS_TYPE_STRING, &property,
2497 DBUS_TYPE_INVALID)) {
2498 log_error("Could not append arguments to message.");
2503 dbus_message_unref(reply);
2504 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2505 log_error("Failed to issue method call: %s", error.message);
2510 if (!dbus_message_iter_init(reply, &iter) ||
2511 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2512 log_error("Failed to parse reply.");
2517 dbus_message_iter_recurse(&iter, &sub);
2519 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2520 log_error("Failed to parse reply.");
2525 dbus_message_iter_get_basic(&sub, &id);
2533 dbus_message_unref(m);
2536 dbus_message_unref(reply);
2538 dbus_error_free(&error);
2543 static int delete_snapshot(DBusConnection *bus, char **args, unsigned n) {
2544 DBusMessage *m = NULL, *reply = NULL;
2552 dbus_error_init(&error);
2554 for (i = 1; i < n; i++) {
2555 const char *path = NULL;
2557 if (!(m = dbus_message_new_method_call(
2558 "org.freedesktop.systemd1",
2559 "/org/freedesktop/systemd1",
2560 "org.freedesktop.systemd1.Manager",
2562 log_error("Could not allocate message.");
2567 if (!dbus_message_append_args(m,
2568 DBUS_TYPE_STRING, &args[i],
2569 DBUS_TYPE_INVALID)) {
2570 log_error("Could not append arguments to message.");
2575 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2576 log_error("Failed to issue method call: %s", error.message);
2581 if (!dbus_message_get_args(reply, &error,
2582 DBUS_TYPE_OBJECT_PATH, &path,
2583 DBUS_TYPE_INVALID)) {
2584 log_error("Failed to parse reply: %s", error.message);
2589 dbus_message_unref(m);
2590 if (!(m = dbus_message_new_method_call(
2591 "org.freedesktop.systemd1",
2593 "org.freedesktop.systemd1.Snapshot",
2595 log_error("Could not allocate message.");
2600 dbus_message_unref(reply);
2601 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2602 log_error("Failed to issue method call: %s", error.message);
2607 dbus_message_unref(m);
2608 dbus_message_unref(reply);
2616 dbus_message_unref(m);
2619 dbus_message_unref(reply);
2621 dbus_error_free(&error);
2626 static int clear_jobs(DBusConnection *bus, char **args, unsigned n) {
2627 DBusMessage *m = NULL, *reply = NULL;
2632 dbus_error_init(&error);
2634 if (arg_action == ACTION_RELOAD)
2636 else if (arg_action == ACTION_REEXEC)
2637 method = "Reexecute";
2639 assert(arg_action == ACTION_SYSTEMCTL);
2642 streq(args[0], "clear-jobs") ? "ClearJobs" :
2643 streq(args[0], "daemon-reload") ? "Reload" :
2644 streq(args[0], "daemon-reexec") ? "Reexecute" :
2645 streq(args[0], "reset-maintenance") ? "ResetMaintenance" :
2649 if (!(m = dbus_message_new_method_call(
2650 "org.freedesktop.systemd1",
2651 "/org/freedesktop/systemd1",
2652 "org.freedesktop.systemd1.Manager",
2654 log_error("Could not allocate message.");
2658 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2660 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
2661 /* There's always a fallback possible for
2662 * legacy actions. */
2667 log_error("Failed to issue method call: %s", error.message);
2676 dbus_message_unref(m);
2679 dbus_message_unref(reply);
2681 dbus_error_free(&error);
2686 static int reset_maintenance(DBusConnection *bus, char **args, unsigned n) {
2687 DBusMessage *m = NULL, *reply = NULL;
2693 dbus_error_init(&error);
2696 return clear_jobs(bus, args, n);
2698 for (i = 1; i < n; i++) {
2700 if (!(m = dbus_message_new_method_call(
2701 "org.freedesktop.systemd1",
2702 "/org/freedesktop/systemd1",
2703 "org.freedesktop.systemd1.Manager",
2704 "ResetMaintenanceUnit"))) {
2705 log_error("Could not allocate message.");
2710 if (!dbus_message_append_args(m,
2711 DBUS_TYPE_STRING, args + i,
2712 DBUS_TYPE_INVALID)) {
2713 log_error("Could not append arguments to message.");
2718 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2719 log_error("Failed to issue method call: %s", error.message);
2724 dbus_message_unref(m);
2725 dbus_message_unref(reply);
2733 dbus_message_unref(m);
2736 dbus_message_unref(reply);
2738 dbus_error_free(&error);
2743 static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
2744 DBusMessage *m = NULL, *reply = NULL;
2746 DBusMessageIter iter, sub, sub2;
2749 *interface = "org.freedesktop.systemd1.Manager",
2750 *property = "Environment";
2752 dbus_error_init(&error);
2754 if (!(m = dbus_message_new_method_call(
2755 "org.freedesktop.systemd1",
2756 "/org/freedesktop/systemd1",
2757 "org.freedesktop.DBus.Properties",
2759 log_error("Could not allocate message.");
2763 if (!dbus_message_append_args(m,
2764 DBUS_TYPE_STRING, &interface,
2765 DBUS_TYPE_STRING, &property,
2766 DBUS_TYPE_INVALID)) {
2767 log_error("Could not append arguments to message.");
2772 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2773 log_error("Failed to issue method call: %s", error.message);
2778 if (!dbus_message_iter_init(reply, &iter) ||
2779 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2780 log_error("Failed to parse reply.");
2785 dbus_message_iter_recurse(&iter, &sub);
2787 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
2788 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
2789 log_error("Failed to parse reply.");
2794 dbus_message_iter_recurse(&sub, &sub2);
2796 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
2799 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
2800 log_error("Failed to parse reply.");
2805 dbus_message_iter_get_basic(&sub2, &text);
2806 printf("%s\n", text);
2808 dbus_message_iter_next(&sub2);
2815 dbus_message_unref(m);
2818 dbus_message_unref(reply);
2820 dbus_error_free(&error);
2825 static int set_environment(DBusConnection *bus, char **args, unsigned n) {
2826 DBusMessage *m = NULL, *reply = NULL;
2830 DBusMessageIter iter, sub;
2833 dbus_error_init(&error);
2835 method = streq(args[0], "set-environment")
2837 : "UnsetEnvironment";
2839 if (!(m = dbus_message_new_method_call(
2840 "org.freedesktop.systemd1",
2841 "/org/freedesktop/systemd1",
2842 "org.freedesktop.systemd1.Manager",
2845 log_error("Could not allocate message.");
2849 dbus_message_iter_init_append(m, &iter);
2851 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
2852 log_error("Could not append arguments to message.");
2857 for (i = 1; i < n; i++)
2858 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) {
2859 log_error("Could not append arguments to message.");
2864 if (!dbus_message_iter_close_container(&iter, &sub)) {
2865 log_error("Could not append arguments to message.");
2870 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2871 log_error("Failed to issue method call: %s", error.message);
2880 dbus_message_unref(m);
2883 dbus_message_unref(reply);
2885 dbus_error_free(&error);
2890 static int systemctl_help(void) {
2892 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
2893 "Send control commands to or query the systemd manager.\n\n"
2894 " -h --help Show this help\n"
2895 " -t --type=TYPE List only units of a particular type\n"
2896 " -p --property=NAME Show only properties by this name\n"
2897 " -a --all Show all units/properties, including dead/empty ones\n"
2898 " --fail When installing a new job, fail if conflicting jobs are\n"
2900 " --system Connect to system bus\n"
2901 " --session Connect to session bus\n"
2902 " --order When generating graph for dot, show only order\n"
2903 " --require When generating graph for dot, show only requirement\n"
2904 " -q --quiet Suppress output\n"
2905 " --no-block Do not wait until operation finished\n"
2906 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
2908 " list-units List units\n"
2909 " start [NAME...] Start one or more units\n"
2910 " stop [NAME...] Stop one or more units\n"
2911 " reload [NAME...] Reload one or more units\n"
2912 " restart [NAME...] Start or restart one or more units\n"
2913 " try-restart [NAME...] Restart one or more units if active\n"
2914 " reload-or-restart [NAME...] Reload one or more units is possible,\n"
2915 " otherwise start or restart\n"
2916 " reload-or-try-restart [NAME...] Reload one or more units is possible,\n"
2917 " otherwise restart if active\n"
2918 " isolate [NAME] Start one unit and stop all others\n"
2919 " check [NAME...] Check whether units are active\n"
2920 " status [NAME...] Show status of one or more units\n"
2921 " show [NAME...|JOB...] Show properties of one or more\n"
2922 " units/jobs/manager\n"
2923 " reset-maintenance [NAME...] Reset maintenance state for all, one\n"
2925 " load [NAME...] Load one or more units\n"
2926 " list-jobs List jobs\n"
2927 " cancel [JOB...] Cancel one or more jobs\n"
2928 " clear-jobs Cancel all jobs\n"
2929 " monitor Monitor unit/job changes\n"
2930 " dump Dump server status\n"
2931 " dot Dump dependency graph for dot(1)\n"
2932 " snapshot [NAME] Create a snapshot\n"
2933 " delete [NAME...] Remove one or more snapshots\n"
2934 " daemon-reload Reload systemd manager configuration\n"
2935 " daemon-reexec Reexecute systemd manager\n"
2936 " daemon-exit Ask the systemd manager to quit\n"
2937 " show-environment Dump environment\n"
2938 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
2939 " unset-environment [NAME...] Unset one or more environment variables\n"
2940 " halt Shut down and halt the system\n"
2941 " poweroff Shut down and power-off the system\n"
2942 " reboot Shut down and reboot the system\n"
2943 " rescue Enter system rescue mode\n"
2944 " emergency Enter system emergency mode\n"
2945 " default Enter system default mode\n",
2946 program_invocation_short_name);
2951 static int halt_help(void) {
2953 printf("%s [OPTIONS...]\n\n"
2954 "%s the system.\n\n"
2955 " --help Show this help\n"
2956 " --halt Halt the machine\n"
2957 " -p --poweroff Switch off the machine\n"
2958 " --reboot Reboot the machine\n"
2959 " -f --force Force immediate halt/power-off/reboot\n"
2960 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
2961 " -d --no-wtmp Don't write wtmp record\n"
2962 " -n --no-sync Don't sync before halt/power-off/reboot\n"
2963 " --no-wall Don't send wall message before halt/power-off/reboot\n",
2964 program_invocation_short_name,
2965 arg_action == ACTION_REBOOT ? "Reboot" :
2966 arg_action == ACTION_POWEROFF ? "Power off" :
2972 static int shutdown_help(void) {
2974 printf("%s [OPTIONS...] [now] [WALL...]\n\n"
2975 "Shut down the system.\n\n"
2976 " --help Show this help\n"
2977 " -H --halt Halt the machine\n"
2978 " -P --poweroff Power-off the machine\n"
2979 " -r --reboot Reboot the machine\n"
2980 " -h Equivalent to --poweroff, overriden by --halt\n"
2981 " -k Don't halt/power-off/reboot, just send warnings\n"
2982 " --no-wall Don't send wall message before halt/power-off/reboot\n",
2983 program_invocation_short_name);
2988 static int telinit_help(void) {
2990 printf("%s [OPTIONS...] {COMMAND}\n\n"
2991 "Send control commands to the init daemon.\n\n"
2992 " --help Show this help\n"
2993 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
2995 " 0 Power-off the machine\n"
2996 " 6 Reboot the machine\n"
2997 " 2, 3, 4, 5 Start runlevelX.target unit\n"
2998 " 1, s, S Enter rescue mode\n"
2999 " q, Q Reload init daemon configuration\n"
3000 " u, U Reexecute init daemon\n",
3001 program_invocation_short_name);
3006 static int runlevel_help(void) {
3008 printf("%s [OPTIONS...]\n\n"
3009 "Prints the previous and current runlevel of the init system.\n\n"
3010 " --help Show this help\n",
3011 program_invocation_short_name);
3016 static int systemctl_parse_argv(int argc, char *argv[]) {
3028 static const struct option options[] = {
3029 { "help", no_argument, NULL, 'h' },
3030 { "type", required_argument, NULL, 't' },
3031 { "property", required_argument, NULL, 'p' },
3032 { "all", no_argument, NULL, 'a' },
3033 { "fail", no_argument, NULL, ARG_FAIL },
3034 { "session", no_argument, NULL, ARG_SESSION },
3035 { "system", no_argument, NULL, ARG_SYSTEM },
3036 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
3037 { "no-wall", no_argument, NULL, ARG_NO_WALL },
3038 { "quiet", no_argument, NULL, 'q' },
3039 { "order", no_argument, NULL, ARG_ORDER },
3040 { "require", no_argument, NULL, ARG_REQUIRE },
3041 { NULL, 0, NULL, 0 }
3049 while ((c = getopt_long(argc, argv, "ht:p:aq", options, NULL)) >= 0) {
3062 arg_property = optarg;
3064 /* If the user asked for a particular
3065 * property, show it to him, even if it is
3083 arg_session = false;
3087 arg_no_block = true;
3095 arg_dot = DOT_ORDER;
3099 arg_dot = DOT_REQUIRE;
3110 log_error("Unknown option code %c", c);
3118 static int halt_parse_argv(int argc, char *argv[]) {
3127 static const struct option options[] = {
3128 { "help", no_argument, NULL, ARG_HELP },
3129 { "halt", no_argument, NULL, ARG_HALT },
3130 { "poweroff", no_argument, NULL, 'p' },
3131 { "reboot", no_argument, NULL, ARG_REBOOT },
3132 { "force", no_argument, NULL, 'f' },
3133 { "wtmp-only", no_argument, NULL, 'w' },
3134 { "no-wtmp", no_argument, NULL, 'd' },
3135 { "no-sync", no_argument, NULL, 'n' },
3136 { "no-wall", no_argument, NULL, ARG_NO_WALL },
3137 { NULL, 0, NULL, 0 }
3145 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
3146 if (runlevel == '0' || runlevel == '6')
3147 arg_immediate = true;
3149 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
3157 arg_action = ACTION_HALT;
3161 arg_action = ACTION_POWEROFF;
3165 arg_action = ACTION_REBOOT;
3169 arg_immediate = true;
3190 /* Compatibility nops */
3197 log_error("Unknown option code %c", c);
3202 if (optind < argc) {
3203 log_error("Too many arguments.");
3210 static int shutdown_parse_argv(int argc, char *argv[]) {
3217 static const struct option options[] = {
3218 { "help", no_argument, NULL, ARG_HELP },
3219 { "halt", no_argument, NULL, 'H' },
3220 { "poweroff", no_argument, NULL, 'P' },
3221 { "reboot", no_argument, NULL, 'r' },
3222 { "no-wall", no_argument, NULL, ARG_NO_WALL },
3223 { NULL, 0, NULL, 0 }
3231 while ((c = getopt_long(argc, argv, "HPrhkt:a", options, NULL)) >= 0) {
3239 arg_action = ACTION_HALT;
3243 arg_action = ACTION_POWEROFF;
3247 arg_action = ACTION_REBOOT;
3251 if (arg_action != ACTION_HALT)
3252 arg_action = ACTION_POWEROFF;
3265 /* Compatibility nops */
3272 log_error("Unknown option code %c", c);
3277 if (argc > optind && !streq(argv[optind], "now"))
3278 log_warning("First argument '%s' isn't 'now'. Ignoring.", argv[optind]);
3280 /* We ignore the time argument */
3281 if (argc > optind + 1)
3282 arg_wall = argv + optind + 1;
3289 static int telinit_parse_argv(int argc, char *argv[]) {
3296 static const struct option options[] = {
3297 { "help", no_argument, NULL, ARG_HELP },
3298 { "no-wall", no_argument, NULL, ARG_NO_WALL },
3299 { NULL, 0, NULL, 0 }
3302 static const struct {
3306 { '0', ACTION_POWEROFF },
3307 { '6', ACTION_REBOOT },
3308 { '1', ACTION_RESCUE },
3309 { '2', ACTION_RUNLEVEL2 },
3310 { '3', ACTION_RUNLEVEL3 },
3311 { '4', ACTION_RUNLEVEL4 },
3312 { '5', ACTION_RUNLEVEL5 },
3313 { 's', ACTION_RESCUE },
3314 { 'S', ACTION_RESCUE },
3315 { 'q', ACTION_RELOAD },
3316 { 'Q', ACTION_RELOAD },
3317 { 'u', ACTION_REEXEC },
3318 { 'U', ACTION_REEXEC }
3327 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
3342 log_error("Unknown option code %c", c);
3347 if (optind >= argc) {
3352 if (optind + 1 < argc) {
3353 log_error("Too many arguments.");
3357 if (strlen(argv[optind]) != 1) {
3358 log_error("Expected single character argument.");
3362 for (i = 0; i < ELEMENTSOF(table); i++)
3363 if (table[i].from == argv[optind][0])
3366 if (i >= ELEMENTSOF(table)) {
3367 log_error("Unknown command %s.", argv[optind]);
3371 arg_action = table[i].to;
3378 static int runlevel_parse_argv(int argc, char *argv[]) {
3384 static const struct option options[] = {
3385 { "help", no_argument, NULL, ARG_HELP },
3386 { NULL, 0, NULL, 0 }
3394 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
3405 log_error("Unknown option code %c", c);
3410 if (optind < argc) {
3411 log_error("Too many arguments.");
3418 static int parse_argv(int argc, char *argv[]) {
3422 if (program_invocation_short_name) {
3424 if (strstr(program_invocation_short_name, "halt")) {
3425 arg_action = ACTION_HALT;
3426 return halt_parse_argv(argc, argv);
3427 } else if (strstr(program_invocation_short_name, "poweroff")) {
3428 arg_action = ACTION_POWEROFF;
3429 return halt_parse_argv(argc, argv);
3430 } else if (strstr(program_invocation_short_name, "reboot")) {
3431 arg_action = ACTION_REBOOT;
3432 return halt_parse_argv(argc, argv);
3433 } else if (strstr(program_invocation_short_name, "shutdown")) {
3434 arg_action = ACTION_POWEROFF;
3435 return shutdown_parse_argv(argc, argv);
3436 } else if (strstr(program_invocation_short_name, "init")) {
3437 arg_action = ACTION_INVALID;
3438 return telinit_parse_argv(argc, argv);
3439 } else if (strstr(program_invocation_short_name, "runlevel")) {
3440 arg_action = ACTION_RUNLEVEL;
3441 return runlevel_parse_argv(argc, argv);
3445 arg_action = ACTION_SYSTEMCTL;
3446 return systemctl_parse_argv(argc, argv);
3449 static int action_to_runlevel(void) {
3451 static const char table[_ACTION_MAX] = {
3452 [ACTION_HALT] = '0',
3453 [ACTION_POWEROFF] = '0',
3454 [ACTION_REBOOT] = '6',
3455 [ACTION_RUNLEVEL2] = '2',
3456 [ACTION_RUNLEVEL3] = '3',
3457 [ACTION_RUNLEVEL4] = '4',
3458 [ACTION_RUNLEVEL5] = '5',
3459 [ACTION_RESCUE] = '1'
3462 assert(arg_action < _ACTION_MAX);
3464 return table[arg_action];
3467 static int talk_upstart(void) {
3468 DBusMessage *m = NULL, *reply = NULL;
3470 int previous, rl, r;
3472 env1_buf[] = "RUNLEVEL=X",
3473 env2_buf[] = "PREVLEVEL=X";
3474 char *env1 = env1_buf, *env2 = env2_buf;
3475 const char *emit = "runlevel";
3476 dbus_bool_t b_false = FALSE;
3477 DBusMessageIter iter, sub;
3478 DBusConnection *bus;
3480 dbus_error_init(&error);
3482 if (!(rl = action_to_runlevel()))
3485 if (utmp_get_runlevel(&previous, NULL) < 0)
3488 if (!(bus = dbus_connection_open_private("unix:abstract=/com/ubuntu/upstart", &error))) {
3489 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
3494 log_error("Failed to connect to Upstart bus: %s", error.message);
3499 if ((r = bus_check_peercred(bus)) < 0) {
3500 log_error("Failed to verify owner of bus.");
3504 if (!(m = dbus_message_new_method_call(
3505 "com.ubuntu.Upstart",
3506 "/com/ubuntu/Upstart",
3507 "com.ubuntu.Upstart0_6",
3510 log_error("Could not allocate message.");
3515 dbus_message_iter_init_append(m, &iter);
3517 env1_buf[sizeof(env1_buf)-2] = rl;
3518 env2_buf[sizeof(env2_buf)-2] = previous;
3520 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
3521 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
3522 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
3523 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
3524 !dbus_message_iter_close_container(&iter, &sub) ||
3525 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
3526 log_error("Could not append arguments to message.");
3531 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3533 if (error_is_no_service(&error)) {
3538 log_error("Failed to issue method call: %s", error.message);
3547 dbus_message_unref(m);
3550 dbus_message_unref(reply);
3553 dbus_connection_close(bus);
3554 dbus_connection_unref(bus);
3557 dbus_error_free(&error);
3562 static int talk_initctl(void) {
3563 struct init_request request;
3567 if (!(rl = action_to_runlevel()))
3571 request.magic = INIT_MAGIC;
3572 request.sleeptime = 0;
3573 request.cmd = INIT_CMD_RUNLVL;
3574 request.runlevel = rl;
3576 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
3578 if (errno == ENOENT)
3581 log_error("Failed to open "INIT_FIFO": %m");
3586 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
3587 close_nointr_nofail(fd);
3590 log_error("Failed to write to "INIT_FIFO": %m");
3591 return errno ? -errno : -EIO;
3597 static int systemctl_main(DBusConnection *bus, int argc, char *argv[]) {
3599 static const struct {
3607 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
3609 { "list-units", LESS, 1, list_units },
3610 { "list-jobs", EQUAL, 1, list_jobs },
3611 { "clear-jobs", EQUAL, 1, clear_jobs },
3612 { "load", MORE, 2, load_unit },
3613 { "cancel", MORE, 2, cancel_job },
3614 { "start", MORE, 2, start_unit },
3615 { "stop", MORE, 2, start_unit },
3616 { "reload", MORE, 2, start_unit },
3617 { "restart", MORE, 2, start_unit },
3618 { "try-restart", MORE, 2, start_unit },
3619 { "reload-or-restart", MORE, 2, start_unit },
3620 { "reload-or-try-restart", MORE, 2, start_unit },
3621 { "isolate", EQUAL, 2, start_unit },
3622 { "check", MORE, 2, check_unit },
3623 { "show", MORE, 1, show },
3624 { "status", MORE, 2, show },
3625 { "monitor", EQUAL, 1, monitor },
3626 { "dump", EQUAL, 1, dump },
3627 { "dot", EQUAL, 1, dot },
3628 { "snapshot", LESS, 2, snapshot },
3629 { "delete", MORE, 2, delete_snapshot },
3630 { "daemon-reload", EQUAL, 1, clear_jobs },
3631 { "daemon-reexec", EQUAL, 1, clear_jobs },
3632 { "daemon-exit", EQUAL, 1, clear_jobs },
3633 { "show-environment", EQUAL, 1, show_enviroment },
3634 { "set-environment", MORE, 2, set_environment },
3635 { "unset-environment", MORE, 2, set_environment },
3636 { "halt", EQUAL, 1, start_special },
3637 { "poweroff", EQUAL, 1, start_special },
3638 { "reboot", EQUAL, 1, start_special },
3639 { "default", EQUAL, 1, start_special },
3640 { "rescue", EQUAL, 1, start_special },
3641 { "emergency", EQUAL, 1, start_special },
3642 { "reset-maintenance", MORE, 1, reset_maintenance },
3652 left = argc - optind;
3655 /* Special rule: no arguments means "list-units" */
3658 if (streq(argv[optind], "help")) {
3663 for (i = 0; i < ELEMENTSOF(verbs); i++)
3664 if (streq(argv[optind], verbs[i].verb))
3667 if (i >= ELEMENTSOF(verbs)) {
3668 log_error("Unknown operation %s", argv[optind]);
3673 switch (verbs[i].argc_cmp) {
3676 if (left != verbs[i].argc) {
3677 log_error("Invalid number of arguments.");
3684 if (left < verbs[i].argc) {
3685 log_error("Too few arguments.");
3692 if (left > verbs[i].argc) {
3693 log_error("Too many arguments.");
3700 assert_not_reached("Unknown comparison operator.");
3703 return verbs[i].dispatch(bus, argv + optind, left);
3706 static int reload_with_fallback(DBusConnection *bus) {
3710 /* First, try systemd via D-Bus. */
3711 if ((r = clear_jobs(bus, NULL, 0)) > 0)
3715 /* Nothing else worked, so let's try signals */
3716 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
3718 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
3719 log_error("kill() failed: %m");
3726 static int start_with_fallback(DBusConnection *bus) {
3731 /* First, try systemd via D-Bus. */
3732 if ((r = start_unit(bus, NULL, 0)) > 0)
3735 /* Hmm, talking to systemd via D-Bus didn't work. Then
3736 * let's try to talk to Upstart via D-Bus. */
3737 if ((r = talk_upstart()) > 0)
3741 /* Nothing else worked, so let's try
3743 if ((r = talk_initctl()) != 0)
3746 log_error("Failed to talk to init daemon.");
3750 warn_wall(arg_action);
3754 static int halt_main(DBusConnection *bus) {
3757 if (geteuid() != 0) {
3758 log_error("Must to be root.");
3762 if (!arg_dry && !arg_immediate)
3763 return start_with_fallback(bus);
3766 if ((r = utmp_put_shutdown(0)) < 0)
3767 log_warning("Failed to write utmp record: %s", strerror(-r));
3775 /* Make sure C-A-D is handled by the kernel from this
3777 reboot(RB_ENABLE_CAD);
3779 switch (arg_action) {
3782 log_info("Halting");
3783 reboot(RB_HALT_SYSTEM);
3786 case ACTION_POWEROFF:
3787 log_info("Powering off");
3788 reboot(RB_POWER_OFF);
3792 log_info("Rebooting");
3793 reboot(RB_AUTOBOOT);
3797 assert_not_reached("Unknown halt action.");
3800 /* We should never reach this. */
3804 static int runlevel_main(void) {
3805 int r, runlevel, previous;
3807 if ((r = utmp_get_runlevel(&runlevel, &previous)) < 0) {
3813 previous <= 0 ? 'N' : previous,
3814 runlevel <= 0 ? 'N' : runlevel);
3819 int main(int argc, char*argv[]) {
3821 DBusConnection *bus = NULL;
3824 dbus_error_init(&error);
3826 log_parse_environment();
3828 if ((r = parse_argv(argc, argv)) < 0)
3835 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
3836 * let's shortcut this */
3837 if (arg_action == ACTION_RUNLEVEL) {
3838 retval = runlevel_main() < 0;
3842 bus_connect(arg_session ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &bus, &private_bus, &error);
3844 switch (arg_action) {
3846 case ACTION_SYSTEMCTL: {
3849 log_error("Failed to get D-Bus connection: %s", error.message);
3853 retval = systemctl_main(bus, argc, argv) < 0;
3858 case ACTION_POWEROFF:
3860 retval = halt_main(bus) < 0;
3863 case ACTION_RUNLEVEL2:
3864 case ACTION_RUNLEVEL3:
3865 case ACTION_RUNLEVEL4:
3866 case ACTION_RUNLEVEL5:
3868 case ACTION_EMERGENCY:
3869 case ACTION_DEFAULT:
3870 retval = start_with_fallback(bus) < 0;
3875 retval = reload_with_fallback(bus) < 0;
3878 case ACTION_INVALID:
3879 case ACTION_RUNLEVEL:
3881 assert_not_reached("Unknown action");
3887 dbus_connection_close(bus);
3888 dbus_connection_unref(bus);
3891 dbus_error_free(&error);