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" :
2648 if (!(m = dbus_message_new_method_call(
2649 "org.freedesktop.systemd1",
2650 "/org/freedesktop/systemd1",
2651 "org.freedesktop.systemd1.Manager",
2653 log_error("Could not allocate message.");
2657 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2659 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
2660 /* There's always a fallback possible for
2661 * legacy actions. */
2666 log_error("Failed to issue method call: %s", error.message);
2675 dbus_message_unref(m);
2678 dbus_message_unref(reply);
2680 dbus_error_free(&error);
2685 static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
2686 DBusMessage *m = NULL, *reply = NULL;
2688 DBusMessageIter iter, sub, sub2;
2691 *interface = "org.freedesktop.systemd1.Manager",
2692 *property = "Environment";
2694 dbus_error_init(&error);
2696 if (!(m = dbus_message_new_method_call(
2697 "org.freedesktop.systemd1",
2698 "/org/freedesktop/systemd1",
2699 "org.freedesktop.DBus.Properties",
2701 log_error("Could not allocate message.");
2705 if (!dbus_message_append_args(m,
2706 DBUS_TYPE_STRING, &interface,
2707 DBUS_TYPE_STRING, &property,
2708 DBUS_TYPE_INVALID)) {
2709 log_error("Could not append arguments to message.");
2714 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2715 log_error("Failed to issue method call: %s", error.message);
2720 if (!dbus_message_iter_init(reply, &iter) ||
2721 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2722 log_error("Failed to parse reply.");
2727 dbus_message_iter_recurse(&iter, &sub);
2729 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
2730 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
2731 log_error("Failed to parse reply.");
2736 dbus_message_iter_recurse(&sub, &sub2);
2738 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
2741 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
2742 log_error("Failed to parse reply.");
2747 dbus_message_iter_get_basic(&sub2, &text);
2748 printf("%s\n", text);
2750 dbus_message_iter_next(&sub2);
2757 dbus_message_unref(m);
2760 dbus_message_unref(reply);
2762 dbus_error_free(&error);
2767 static int set_environment(DBusConnection *bus, char **args, unsigned n) {
2768 DBusMessage *m = NULL, *reply = NULL;
2772 DBusMessageIter iter, sub;
2775 dbus_error_init(&error);
2777 method = streq(args[0], "set-environment")
2779 : "UnsetEnvironment";
2781 if (!(m = dbus_message_new_method_call(
2782 "org.freedesktop.systemd1",
2783 "/org/freedesktop/systemd1",
2784 "org.freedesktop.systemd1.Manager",
2787 log_error("Could not allocate message.");
2791 dbus_message_iter_init_append(m, &iter);
2793 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
2794 log_error("Could not append arguments to message.");
2799 for (i = 1; i < n; i++)
2800 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) {
2801 log_error("Could not append arguments to message.");
2806 if (!dbus_message_iter_close_container(&iter, &sub)) {
2807 log_error("Could not append arguments to message.");
2812 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2813 log_error("Failed to issue method call: %s", error.message);
2822 dbus_message_unref(m);
2825 dbus_message_unref(reply);
2827 dbus_error_free(&error);
2832 static int systemctl_help(void) {
2834 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
2835 "Send control commands to the systemd manager.\n\n"
2836 " -h --help Show this help\n"
2837 " -t --type=TYPE List only units of a particular type\n"
2838 " -p --property=NAME Show only properties by this name\n"
2839 " -a --all Show all units/properties, including dead/empty ones\n"
2840 " --fail When installing a new job, fail if conflicting jobs are\n"
2842 " --system Connect to system bus\n"
2843 " --session Connect to session bus\n"
2844 " --order When generating graph for dot, show only order\n"
2845 " --require When generating graph for dot, show only requirement\n"
2846 " -q --quiet Suppress output\n"
2847 " --no-block Do not wait until operation finished\n"
2848 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
2850 " list-units List units\n"
2851 " start [NAME...] Start one or more units\n"
2852 " stop [NAME...] Stop one or more units\n"
2853 " reload [NAME...] Reload one or more units\n"
2854 " restart [NAME...] Start or restart one or more units\n"
2855 " try-restart [NAME...] Restart one or more units if active\n"
2856 " reload-or-restart [NAME...] Reload one or more units is possible,\n"
2857 " otherwise start or restart\n"
2858 " reload-or-try-restart [NAME...] Reload one or more units is possible,\n"
2859 " otherwise restart if active\n"
2860 " isolate [NAME] Start one unit and stop all others\n"
2861 " check [NAME...] Check whether units are active\n"
2862 " status [NAME...] Show status of one or more units\n"
2863 " show [NAME...|JOB...] Show properties of one or more\n"
2864 " units/jobs/manager\n"
2865 " load [NAME...] Load one or more units\n"
2866 " list-jobs List jobs\n"
2867 " cancel [JOB...] Cancel one or more jobs\n"
2868 " clear-jobs Cancel all jobs\n"
2869 " monitor Monitor unit/job changes\n"
2870 " dump Dump server status\n"
2871 " dot Dump dependency graph for dot(1)\n"
2872 " snapshot [NAME] Create a snapshot\n"
2873 " delete [NAME...] Remove one or more snapshots\n"
2874 " daemon-reload Reload systemd manager configuration\n"
2875 " daemon-reexec Reexecute systemd manager\n"
2876 " daemon-exit Ask the systemd manager to quit\n"
2877 " show-environment Dump environment\n"
2878 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
2879 " unset-environment [NAME...] Unset one or more environment variables\n"
2880 " halt Shut down and halt the system\n"
2881 " poweroff Shut down and power-off the system\n"
2882 " reboot Shut down and reboot the system\n"
2883 " rescue Enter system rescue mode\n"
2884 " emergency Enter system emergency mode\n"
2885 " default Enter system default mode\n",
2886 program_invocation_short_name);
2891 static int halt_help(void) {
2893 printf("%s [OPTIONS...]\n\n"
2894 "%s the system.\n\n"
2895 " --help Show this help\n"
2896 " --halt Halt the machine\n"
2897 " -p --poweroff Switch off the machine\n"
2898 " --reboot Reboot the machine\n"
2899 " -f --force Force immediate halt/power-off/reboot\n"
2900 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
2901 " -d --no-wtmp Don't write wtmp record\n"
2902 " -n --no-sync Don't sync before halt/power-off/reboot\n"
2903 " --no-wall Don't send wall message before halt/power-off/reboot\n",
2904 program_invocation_short_name,
2905 arg_action == ACTION_REBOOT ? "Reboot" :
2906 arg_action == ACTION_POWEROFF ? "Power off" :
2912 static int shutdown_help(void) {
2914 printf("%s [OPTIONS...] [now] [WALL...]\n\n"
2915 "Shut down the system.\n\n"
2916 " --help Show this help\n"
2917 " -H --halt Halt the machine\n"
2918 " -P --poweroff Power-off the machine\n"
2919 " -r --reboot Reboot the machine\n"
2920 " -h Equivalent to --poweroff, overriden by --halt\n"
2921 " -k Don't halt/power-off/reboot, just send warnings\n"
2922 " --no-wall Don't send wall message before halt/power-off/reboot\n",
2923 program_invocation_short_name);
2928 static int telinit_help(void) {
2930 printf("%s [OPTIONS...] {COMMAND}\n\n"
2931 "Send control commands to the init daemon.\n\n"
2932 " --help Show this help\n"
2933 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
2935 " 0 Power-off the machine\n"
2936 " 6 Reboot the machine\n"
2937 " 2, 3, 4, 5 Start runlevelX.target unit\n"
2938 " 1, s, S Enter rescue mode\n"
2939 " q, Q Reload init daemon configuration\n"
2940 " u, U Reexecute init daemon\n",
2941 program_invocation_short_name);
2946 static int runlevel_help(void) {
2948 printf("%s [OPTIONS...]\n\n"
2949 "Prints the previous and current runlevel of the init system.\n\n"
2950 " --help Show this help\n",
2951 program_invocation_short_name);
2956 static int systemctl_parse_argv(int argc, char *argv[]) {
2968 static const struct option options[] = {
2969 { "help", no_argument, NULL, 'h' },
2970 { "type", required_argument, NULL, 't' },
2971 { "property", required_argument, NULL, 'p' },
2972 { "all", no_argument, NULL, 'a' },
2973 { "fail", no_argument, NULL, ARG_FAIL },
2974 { "session", no_argument, NULL, ARG_SESSION },
2975 { "system", no_argument, NULL, ARG_SYSTEM },
2976 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
2977 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2978 { "quiet", no_argument, NULL, 'q' },
2979 { "order", no_argument, NULL, ARG_ORDER },
2980 { "require", no_argument, NULL, ARG_REQUIRE },
2981 { NULL, 0, NULL, 0 }
2989 while ((c = getopt_long(argc, argv, "ht:p:aq", options, NULL)) >= 0) {
3002 arg_property = optarg;
3004 /* If the user asked for a particular
3005 * property, show it to him, even if it is
3023 arg_session = false;
3027 arg_no_block = true;
3035 arg_dot = DOT_ORDER;
3039 arg_dot = DOT_REQUIRE;
3050 log_error("Unknown option code %c", c);
3058 static int halt_parse_argv(int argc, char *argv[]) {
3067 static const struct option options[] = {
3068 { "help", no_argument, NULL, ARG_HELP },
3069 { "halt", no_argument, NULL, ARG_HALT },
3070 { "poweroff", no_argument, NULL, 'p' },
3071 { "reboot", no_argument, NULL, ARG_REBOOT },
3072 { "force", no_argument, NULL, 'f' },
3073 { "wtmp-only", no_argument, NULL, 'w' },
3074 { "no-wtmp", no_argument, NULL, 'd' },
3075 { "no-sync", no_argument, NULL, 'n' },
3076 { "no-wall", no_argument, NULL, ARG_NO_WALL },
3077 { NULL, 0, NULL, 0 }
3085 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
3086 if (runlevel == '0' || runlevel == '6')
3087 arg_immediate = true;
3089 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
3097 arg_action = ACTION_HALT;
3101 arg_action = ACTION_POWEROFF;
3105 arg_action = ACTION_REBOOT;
3109 arg_immediate = true;
3130 /* Compatibility nops */
3137 log_error("Unknown option code %c", c);
3142 if (optind < argc) {
3143 log_error("Too many arguments.");
3150 static int shutdown_parse_argv(int argc, char *argv[]) {
3157 static const struct option options[] = {
3158 { "help", no_argument, NULL, ARG_HELP },
3159 { "halt", no_argument, NULL, 'H' },
3160 { "poweroff", no_argument, NULL, 'P' },
3161 { "reboot", no_argument, NULL, 'r' },
3162 { "no-wall", no_argument, NULL, ARG_NO_WALL },
3163 { NULL, 0, NULL, 0 }
3171 while ((c = getopt_long(argc, argv, "HPrhkt:a", options, NULL)) >= 0) {
3179 arg_action = ACTION_HALT;
3183 arg_action = ACTION_POWEROFF;
3187 arg_action = ACTION_REBOOT;
3191 if (arg_action != ACTION_HALT)
3192 arg_action = ACTION_POWEROFF;
3205 /* Compatibility nops */
3212 log_error("Unknown option code %c", c);
3217 if (argc > optind && !streq(argv[optind], "now"))
3218 log_warning("First argument '%s' isn't 'now'. Ignoring.", argv[optind]);
3220 /* We ignore the time argument */
3221 if (argc > optind + 1)
3222 arg_wall = argv + optind + 1;
3229 static int telinit_parse_argv(int argc, char *argv[]) {
3236 static const struct option options[] = {
3237 { "help", no_argument, NULL, ARG_HELP },
3238 { "no-wall", no_argument, NULL, ARG_NO_WALL },
3239 { NULL, 0, NULL, 0 }
3242 static const struct {
3246 { '0', ACTION_POWEROFF },
3247 { '6', ACTION_REBOOT },
3248 { '1', ACTION_RESCUE },
3249 { '2', ACTION_RUNLEVEL2 },
3250 { '3', ACTION_RUNLEVEL3 },
3251 { '4', ACTION_RUNLEVEL4 },
3252 { '5', ACTION_RUNLEVEL5 },
3253 { 's', ACTION_RESCUE },
3254 { 'S', ACTION_RESCUE },
3255 { 'q', ACTION_RELOAD },
3256 { 'Q', ACTION_RELOAD },
3257 { 'u', ACTION_REEXEC },
3258 { 'U', ACTION_REEXEC }
3267 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
3282 log_error("Unknown option code %c", c);
3287 if (optind >= argc) {
3292 if (optind + 1 < argc) {
3293 log_error("Too many arguments.");
3297 if (strlen(argv[optind]) != 1) {
3298 log_error("Expected single character argument.");
3302 for (i = 0; i < ELEMENTSOF(table); i++)
3303 if (table[i].from == argv[optind][0])
3306 if (i >= ELEMENTSOF(table)) {
3307 log_error("Unknown command %s.", argv[optind]);
3311 arg_action = table[i].to;
3318 static int runlevel_parse_argv(int argc, char *argv[]) {
3324 static const struct option options[] = {
3325 { "help", no_argument, NULL, ARG_HELP },
3326 { NULL, 0, NULL, 0 }
3334 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
3345 log_error("Unknown option code %c", c);
3350 if (optind < argc) {
3351 log_error("Too many arguments.");
3358 static int parse_argv(int argc, char *argv[]) {
3362 if (program_invocation_short_name) {
3364 if (strstr(program_invocation_short_name, "halt")) {
3365 arg_action = ACTION_HALT;
3366 return halt_parse_argv(argc, argv);
3367 } else if (strstr(program_invocation_short_name, "poweroff")) {
3368 arg_action = ACTION_POWEROFF;
3369 return halt_parse_argv(argc, argv);
3370 } else if (strstr(program_invocation_short_name, "reboot")) {
3371 arg_action = ACTION_REBOOT;
3372 return halt_parse_argv(argc, argv);
3373 } else if (strstr(program_invocation_short_name, "shutdown")) {
3374 arg_action = ACTION_POWEROFF;
3375 return shutdown_parse_argv(argc, argv);
3376 } else if (strstr(program_invocation_short_name, "init")) {
3377 arg_action = ACTION_INVALID;
3378 return telinit_parse_argv(argc, argv);
3379 } else if (strstr(program_invocation_short_name, "runlevel")) {
3380 arg_action = ACTION_RUNLEVEL;
3381 return runlevel_parse_argv(argc, argv);
3385 arg_action = ACTION_SYSTEMCTL;
3386 return systemctl_parse_argv(argc, argv);
3389 static int action_to_runlevel(void) {
3391 static const char table[_ACTION_MAX] = {
3392 [ACTION_HALT] = '0',
3393 [ACTION_POWEROFF] = '0',
3394 [ACTION_REBOOT] = '6',
3395 [ACTION_RUNLEVEL2] = '2',
3396 [ACTION_RUNLEVEL3] = '3',
3397 [ACTION_RUNLEVEL4] = '4',
3398 [ACTION_RUNLEVEL5] = '5',
3399 [ACTION_RESCUE] = '1'
3402 assert(arg_action < _ACTION_MAX);
3404 return table[arg_action];
3407 static int talk_upstart(void) {
3408 DBusMessage *m = NULL, *reply = NULL;
3410 int previous, rl, r;
3412 env1_buf[] = "RUNLEVEL=X",
3413 env2_buf[] = "PREVLEVEL=X";
3414 char *env1 = env1_buf, *env2 = env2_buf;
3415 const char *emit = "runlevel";
3416 dbus_bool_t b_false = FALSE;
3417 DBusMessageIter iter, sub;
3418 DBusConnection *bus;
3420 dbus_error_init(&error);
3422 if (!(rl = action_to_runlevel()))
3425 if (utmp_get_runlevel(&previous, NULL) < 0)
3428 if (!(bus = dbus_connection_open_private("unix:abstract=/com/ubuntu/upstart", &error))) {
3429 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
3434 log_error("Failed to connect to Upstart bus: %s", error.message);
3439 if ((r = bus_check_peercred(bus)) < 0) {
3440 log_error("Failed to verify owner of bus.");
3444 if (!(m = dbus_message_new_method_call(
3445 "com.ubuntu.Upstart",
3446 "/com/ubuntu/Upstart",
3447 "com.ubuntu.Upstart0_6",
3450 log_error("Could not allocate message.");
3455 dbus_message_iter_init_append(m, &iter);
3457 env1_buf[sizeof(env1_buf)-2] = rl;
3458 env2_buf[sizeof(env2_buf)-2] = previous;
3460 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
3461 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
3462 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
3463 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
3464 !dbus_message_iter_close_container(&iter, &sub) ||
3465 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
3466 log_error("Could not append arguments to message.");
3471 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3473 if (error_is_no_service(&error)) {
3478 log_error("Failed to issue method call: %s", error.message);
3487 dbus_message_unref(m);
3490 dbus_message_unref(reply);
3493 dbus_connection_close(bus);
3494 dbus_connection_unref(bus);
3497 dbus_error_free(&error);
3502 static int talk_initctl(void) {
3503 struct init_request request;
3507 if (!(rl = action_to_runlevel()))
3511 request.magic = INIT_MAGIC;
3512 request.sleeptime = 0;
3513 request.cmd = INIT_CMD_RUNLVL;
3514 request.runlevel = rl;
3516 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
3518 if (errno == ENOENT)
3521 log_error("Failed to open "INIT_FIFO": %m");
3526 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
3527 close_nointr_nofail(fd);
3530 log_error("Failed to write to "INIT_FIFO": %m");
3531 return errno ? -errno : -EIO;
3537 static int systemctl_main(DBusConnection *bus, int argc, char *argv[]) {
3539 static const struct {
3547 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
3549 { "list-units", LESS, 1, list_units },
3550 { "list-jobs", EQUAL, 1, list_jobs },
3551 { "clear-jobs", EQUAL, 1, clear_jobs },
3552 { "load", MORE, 2, load_unit },
3553 { "cancel", MORE, 2, cancel_job },
3554 { "start", MORE, 2, start_unit },
3555 { "stop", MORE, 2, start_unit },
3556 { "reload", MORE, 2, start_unit },
3557 { "restart", MORE, 2, start_unit },
3558 { "try-restart", MORE, 2, start_unit },
3559 { "reload-or-restart", MORE, 2, start_unit },
3560 { "reload-or-try-restart", MORE, 2, start_unit },
3561 { "isolate", EQUAL, 2, start_unit },
3562 { "check", MORE, 2, check_unit },
3563 { "show", MORE, 1, show },
3564 { "status", MORE, 2, show },
3565 { "monitor", EQUAL, 1, monitor },
3566 { "dump", EQUAL, 1, dump },
3567 { "dot", EQUAL, 1, dot },
3568 { "snapshot", LESS, 2, snapshot },
3569 { "delete", MORE, 2, delete_snapshot },
3570 { "daemon-reload", EQUAL, 1, clear_jobs },
3571 { "daemon-reexec", EQUAL, 1, clear_jobs },
3572 { "daemon-exit", EQUAL, 1, clear_jobs },
3573 { "show-environment", EQUAL, 1, show_enviroment },
3574 { "set-environment", MORE, 2, set_environment },
3575 { "unset-environment", MORE, 2, set_environment },
3576 { "halt", EQUAL, 1, start_special },
3577 { "poweroff", EQUAL, 1, start_special },
3578 { "reboot", EQUAL, 1, start_special },
3579 { "default", EQUAL, 1, start_special },
3580 { "rescue", EQUAL, 1, start_special },
3581 { "emergency", EQUAL, 1, start_special },
3591 left = argc - optind;
3594 /* Special rule: no arguments means "list-units" */
3597 if (streq(argv[optind], "help")) {
3602 for (i = 0; i < ELEMENTSOF(verbs); i++)
3603 if (streq(argv[optind], verbs[i].verb))
3606 if (i >= ELEMENTSOF(verbs)) {
3607 log_error("Unknown operation %s", argv[optind]);
3612 switch (verbs[i].argc_cmp) {
3615 if (left != verbs[i].argc) {
3616 log_error("Invalid number of arguments.");
3623 if (left < verbs[i].argc) {
3624 log_error("Too few arguments.");
3631 if (left > verbs[i].argc) {
3632 log_error("Too many arguments.");
3639 assert_not_reached("Unknown comparison operator.");
3642 return verbs[i].dispatch(bus, argv + optind, left);
3645 static int reload_with_fallback(DBusConnection *bus) {
3649 /* First, try systemd via D-Bus. */
3650 if ((r = clear_jobs(bus, NULL, 0)) > 0)
3654 /* Nothing else worked, so let's try signals */
3655 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
3657 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
3658 log_error("kill() failed: %m");
3665 static int start_with_fallback(DBusConnection *bus) {
3670 /* First, try systemd via D-Bus. */
3671 if ((r = start_unit(bus, NULL, 0)) > 0)
3674 /* Hmm, talking to systemd via D-Bus didn't work. Then
3675 * let's try to talk to Upstart via D-Bus. */
3676 if ((r = talk_upstart()) > 0)
3680 /* Nothing else worked, so let's try
3682 if ((r = talk_initctl()) != 0)
3685 log_error("Failed to talk to init daemon.");
3689 warn_wall(arg_action);
3693 static int halt_main(DBusConnection *bus) {
3696 if (geteuid() != 0) {
3697 log_error("Must to be root.");
3701 if (!arg_dry && !arg_immediate)
3702 return start_with_fallback(bus);
3705 if ((r = utmp_put_shutdown(0)) < 0)
3706 log_warning("Failed to write utmp record: %s", strerror(-r));
3714 /* Make sure C-A-D is handled by the kernel from this
3716 reboot(RB_ENABLE_CAD);
3718 switch (arg_action) {
3721 log_info("Halting");
3722 reboot(RB_HALT_SYSTEM);
3725 case ACTION_POWEROFF:
3726 log_info("Powering off");
3727 reboot(RB_POWER_OFF);
3731 log_info("Rebooting");
3732 reboot(RB_AUTOBOOT);
3736 assert_not_reached("Unknown halt action.");
3739 /* We should never reach this. */
3743 static int runlevel_main(void) {
3744 int r, runlevel, previous;
3746 if ((r = utmp_get_runlevel(&runlevel, &previous)) < 0) {
3752 previous <= 0 ? 'N' : previous,
3753 runlevel <= 0 ? 'N' : runlevel);
3758 int main(int argc, char*argv[]) {
3760 DBusConnection *bus = NULL;
3763 dbus_error_init(&error);
3765 log_parse_environment();
3767 if ((r = parse_argv(argc, argv)) < 0)
3774 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
3775 * let's shortcut this */
3776 if (arg_action == ACTION_RUNLEVEL) {
3777 retval = runlevel_main() < 0;
3781 bus_connect(arg_session ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &bus, &private_bus, &error);
3783 switch (arg_action) {
3785 case ACTION_SYSTEMCTL: {
3788 log_error("Failed to get D-Bus connection: %s", error.message);
3792 retval = systemctl_main(bus, argc, argv) < 0;
3797 case ACTION_POWEROFF:
3799 retval = halt_main(bus) < 0;
3802 case ACTION_RUNLEVEL2:
3803 case ACTION_RUNLEVEL3:
3804 case ACTION_RUNLEVEL4:
3805 case ACTION_RUNLEVEL5:
3807 case ACTION_EMERGENCY:
3808 case ACTION_DEFAULT:
3809 retval = start_with_fallback(bus) < 0;
3814 retval = reload_with_fallback(bus) < 0;
3817 case ACTION_INVALID:
3818 case ACTION_RUNLEVEL:
3820 assert_not_reached("Unknown action");
3826 dbus_connection_close(bus);
3827 dbus_connection_unref(bus);
3830 dbus_error_free(&error);