1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/reboot.h>
28 #include <sys/ioctl.h>
32 #include <sys/socket.h>
34 #include <dbus/dbus.h>
40 #include "utmp-wtmp.h"
44 #include "dbus-common.h"
45 #include "cgroup-show.h"
46 #include "cgroup-util.h"
49 static const char *arg_type = NULL;
50 static const char *arg_property = NULL;
51 static bool arg_all = false;
52 static bool arg_fail = false;
53 static bool arg_session = false;
54 static bool arg_no_block = false;
55 static bool arg_immediate = false;
56 static bool arg_no_wtmp = false;
57 static bool arg_no_sync = false;
58 static bool arg_no_wall = false;
59 static bool arg_dry = false;
60 static bool arg_quiet = false;
61 static char **arg_wall = NULL;
79 } arg_action = ACTION_SYSTEMCTL;
86 static bool private_bus = false;
88 static bool error_is_no_service(DBusError *error) {
92 if (!dbus_error_is_set(error))
95 if (dbus_error_has_name(error, DBUS_ERROR_NAME_HAS_NO_OWNER))
98 if (dbus_error_has_name(error, DBUS_ERROR_SERVICE_UNKNOWN))
101 return startswith(error->name, "org.freedesktop.DBus.Error.Spawn.");
104 static int bus_iter_get_basic_and_next(DBusMessageIter *iter, int type, void *data, bool next) {
109 if (dbus_message_iter_get_arg_type(iter) != type)
112 dbus_message_iter_get_basic(iter, data);
114 if (!dbus_message_iter_next(iter) != !next)
120 static void warn_wall(enum action action) {
121 static const char *table[_ACTION_MAX] = {
122 [ACTION_HALT] = "The system is going down for system halt NOW!",
123 [ACTION_REBOOT] = "The system is going down for reboot NOW!",
124 [ACTION_POWEROFF] = "The system is going down for power-off NOW!",
125 [ACTION_RESCUE] = "The system is going down to rescue mode NOW!",
126 [ACTION_EMERGENCY] = "The system is going down to emergency mode NOW!"
135 if (!(p = strv_join(arg_wall, " "))) {
136 log_error("Failed to join strings.");
152 utmp_wall(table[action]);
155 static int list_units(DBusConnection *bus, char **args, unsigned n) {
156 DBusMessage *m = NULL, *reply = NULL;
159 DBusMessageIter iter, sub, sub2;
162 dbus_error_init(&error);
166 if (!(m = dbus_message_new_method_call(
167 "org.freedesktop.systemd1",
168 "/org/freedesktop/systemd1",
169 "org.freedesktop.systemd1.Manager",
171 log_error("Could not allocate message.");
175 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
176 log_error("Failed to issue method call: %s", error.message);
181 if (!dbus_message_iter_init(reply, &iter) ||
182 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
183 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
184 log_error("Failed to parse reply.");
189 dbus_message_iter_recurse(&iter, &sub);
191 printf("%-45s %-6s %-12s %-12s %-15s %s\n", "UNIT", "LOAD", "ACTIVE", "SUB", "JOB", "DESCRIPTION");
193 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
194 const char *id, *description, *load_state, *active_state, *sub_state, *unit_path, *job_type, *job_path, *dot;
197 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
198 log_error("Failed to parse reply.");
203 dbus_message_iter_recurse(&sub, &sub2);
205 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
206 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
207 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
208 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
209 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
210 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, true) < 0 ||
211 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &job_id, true) < 0 ||
212 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &job_type, true) < 0 ||
213 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, false) < 0) {
214 log_error("Failed to parse reply.");
219 if ((!arg_type || ((dot = strrchr(id, '.')) &&
220 streq(dot+1, arg_type))) &&
221 (arg_all || !streq(active_state, "inactive") || job_id > 0)) {
225 if (streq(active_state, "maintenance"))
226 fputs(ANSI_HIGHLIGHT_ON, stdout);
228 printf("%-45s %-6s %-12s %-12s%n", id, load_state, active_state, sub_state, &a);
231 printf(" => %-12s%n", job_type, &b);
235 if (a + b + 2 < columns()) {
239 printf(" %.*s", columns() - a - b - 2, description);
242 if (streq(active_state, "maintenance"))
243 fputs(ANSI_HIGHLIGHT_OFF, stdout);
249 dbus_message_iter_next(&sub);
252 printf("\nLOAD = Load State, reflects whether the unit configuration was properly loaded.\n"
253 "ACTIVE = Active State, the high-level unit activation state, i.e. generalization of the substate.\n"
254 "SUB = Substate, the low-level unit activation state, possible values depend on unit type.\n"
255 "JOB = Job, shows scheduled jobs for the unit.\n");
258 printf("\n%u units listed.\n", k);
260 printf("\n%u units listed. Pass --all to see inactive units, too.\n", k);
266 dbus_message_unref(m);
269 dbus_message_unref(reply);
271 dbus_error_free(&error);
276 static int dot_one_property(const char *name, const char *prop, DBusMessageIter *iter) {
277 static const char * const colors[] = {
278 "Requires", "[color=\"black\"]",
279 "RequiresOverridable", "[color=\"black\"]",
280 "Requisite", "[color=\"darkblue\"]",
281 "RequisiteOverridable", "[color=\"darkblue\"]",
282 "Wants", "[color=\"darkgrey\"]",
283 "Conflicts", "[color=\"red\"]",
284 "After", "[color=\"green\"]"
287 const char *c = NULL;
294 for (i = 0; i < ELEMENTSOF(colors); i += 2)
295 if (streq(colors[i], prop)) {
303 if (arg_dot != DOT_ALL)
304 if ((arg_dot == DOT_ORDER) != streq(prop, "After"))
307 switch (dbus_message_iter_get_arg_type(iter)) {
309 case DBUS_TYPE_ARRAY:
311 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
314 dbus_message_iter_recurse(iter, &sub);
316 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
319 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
320 dbus_message_iter_get_basic(&sub, &s);
321 printf("\t\"%s\"->\"%s\" %s;\n", name, s, c);
323 dbus_message_iter_next(&sub);
333 static int dot_one(DBusConnection *bus, const char *name, const char *path) {
334 DBusMessage *m = NULL, *reply = NULL;
335 const char *interface = "org.freedesktop.systemd1.Unit";
338 DBusMessageIter iter, sub, sub2, sub3;
343 dbus_error_init(&error);
345 if (!(m = dbus_message_new_method_call(
346 "org.freedesktop.systemd1",
348 "org.freedesktop.DBus.Properties",
350 log_error("Could not allocate message.");
355 if (!dbus_message_append_args(m,
356 DBUS_TYPE_STRING, &interface,
357 DBUS_TYPE_INVALID)) {
358 log_error("Could not append arguments to message.");
363 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
364 log_error("Failed to issue method call: %s", error.message);
369 if (!dbus_message_iter_init(reply, &iter) ||
370 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
371 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
372 log_error("Failed to parse reply.");
377 dbus_message_iter_recurse(&iter, &sub);
379 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
382 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
383 log_error("Failed to parse reply.");
388 dbus_message_iter_recurse(&sub, &sub2);
390 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &prop, true) < 0) {
391 log_error("Failed to parse reply.");
396 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
397 log_error("Failed to parse reply.");
402 dbus_message_iter_recurse(&sub2, &sub3);
404 if (dot_one_property(name, prop, &sub3)) {
405 log_error("Failed to parse reply.");
410 dbus_message_iter_next(&sub);
415 dbus_message_unref(m);
418 dbus_message_unref(reply);
420 dbus_error_free(&error);
425 static int dot(DBusConnection *bus, char **args, unsigned n) {
426 DBusMessage *m = NULL, *reply = NULL;
429 DBusMessageIter iter, sub, sub2;
431 dbus_error_init(&error);
435 if (!(m = dbus_message_new_method_call(
436 "org.freedesktop.systemd1",
437 "/org/freedesktop/systemd1",
438 "org.freedesktop.systemd1.Manager",
440 log_error("Could not allocate message.");
444 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
445 log_error("Failed to issue method call: %s", error.message);
450 if (!dbus_message_iter_init(reply, &iter) ||
451 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
452 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
453 log_error("Failed to parse reply.");
458 printf("digraph systemd {\n");
460 dbus_message_iter_recurse(&iter, &sub);
461 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
462 const char *id, *description, *load_state, *active_state, *sub_state, *unit_path;
464 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
465 log_error("Failed to parse reply.");
470 dbus_message_iter_recurse(&sub, &sub2);
472 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
473 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
474 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
475 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
476 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
477 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, true) < 0) {
478 log_error("Failed to parse reply.");
483 if ((r = dot_one(bus, id, unit_path)) < 0)
486 /* printf("\t\"%s\";\n", id); */
487 dbus_message_iter_next(&sub);
492 log_info(" Color legend: black = Requires\n"
493 " dark blue = Requisite\n"
494 " dark grey = Wants\n"
498 if (isatty(fileno(stdout)))
499 log_notice("-- You probably want to process this output with graphviz' dot tool.\n"
500 "-- Try a shell pipeline like 'systemctl dot | dot -Tsvg > systemd.svg'!\n");
506 dbus_message_unref(m);
509 dbus_message_unref(reply);
511 dbus_error_free(&error);
516 static int list_jobs(DBusConnection *bus, char **args, unsigned n) {
517 DBusMessage *m = NULL, *reply = NULL;
520 DBusMessageIter iter, sub, sub2;
523 dbus_error_init(&error);
527 if (!(m = dbus_message_new_method_call(
528 "org.freedesktop.systemd1",
529 "/org/freedesktop/systemd1",
530 "org.freedesktop.systemd1.Manager",
532 log_error("Could not allocate message.");
536 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
537 log_error("Failed to issue method call: %s", error.message);
542 if (!dbus_message_iter_init(reply, &iter) ||
543 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
544 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
545 log_error("Failed to parse reply.");
550 dbus_message_iter_recurse(&iter, &sub);
552 printf("%4s %-45s %-17s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
554 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
555 const char *name, *type, *state, *job_path, *unit_path;
558 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
559 log_error("Failed to parse reply.");
564 dbus_message_iter_recurse(&sub, &sub2);
566 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
567 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
568 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
569 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
570 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
571 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
572 log_error("Failed to parse reply.");
577 printf("%4u %-45s %-17s %-7s\n", id, name, type, state);
580 dbus_message_iter_next(&sub);
583 printf("\n%u jobs listed.\n", k);
588 dbus_message_unref(m);
591 dbus_message_unref(reply);
593 dbus_error_free(&error);
598 static int load_unit(DBusConnection *bus, char **args, unsigned n) {
599 DBusMessage *m = NULL, *reply = NULL;
604 dbus_error_init(&error);
609 for (i = 1; i < n; i++) {
611 if (!(m = dbus_message_new_method_call(
612 "org.freedesktop.systemd1",
613 "/org/freedesktop/systemd1",
614 "org.freedesktop.systemd1.Manager",
616 log_error("Could not allocate message.");
621 if (!dbus_message_append_args(m,
622 DBUS_TYPE_STRING, &args[i],
623 DBUS_TYPE_INVALID)) {
624 log_error("Could not append arguments to message.");
629 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
630 log_error("Failed to issue method call: %s", error.message);
635 dbus_message_unref(m);
636 dbus_message_unref(reply);
645 dbus_message_unref(m);
648 dbus_message_unref(reply);
650 dbus_error_free(&error);
655 static int cancel_job(DBusConnection *bus, char **args, unsigned n) {
656 DBusMessage *m = NULL, *reply = NULL;
661 dbus_error_init(&error);
666 for (i = 1; i < n; i++) {
670 if (!(m = dbus_message_new_method_call(
671 "org.freedesktop.systemd1",
672 "/org/freedesktop/systemd1",
673 "org.freedesktop.systemd1.Manager",
675 log_error("Could not allocate message.");
680 if ((r = safe_atou(args[i], &id)) < 0) {
681 log_error("Failed to parse job id: %s", strerror(-r));
685 assert_cc(sizeof(uint32_t) == sizeof(id));
686 if (!dbus_message_append_args(m,
687 DBUS_TYPE_UINT32, &id,
688 DBUS_TYPE_INVALID)) {
689 log_error("Could not append arguments to message.");
694 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
695 log_error("Failed to issue method call: %s", error.message);
700 if (!dbus_message_get_args(reply, &error,
701 DBUS_TYPE_OBJECT_PATH, &path,
702 DBUS_TYPE_INVALID)) {
703 log_error("Failed to parse reply: %s", error.message);
708 dbus_message_unref(m);
709 if (!(m = dbus_message_new_method_call(
710 "org.freedesktop.systemd1",
712 "org.freedesktop.systemd1.Job",
714 log_error("Could not allocate message.");
719 dbus_message_unref(reply);
720 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
721 log_error("Failed to issue method call: %s", error.message);
726 dbus_message_unref(m);
727 dbus_message_unref(reply);
735 dbus_message_unref(m);
738 dbus_message_unref(reply);
740 dbus_error_free(&error);
745 typedef struct WaitData {
750 static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
758 dbus_error_init(&error);
760 log_debug("Got D-Bus request: %s.%s() on %s",
761 dbus_message_get_interface(message),
762 dbus_message_get_member(message),
763 dbus_message_get_path(message));
765 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
766 log_error("Warning! D-Bus connection terminated.");
767 dbus_connection_close(connection);
769 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
772 dbus_bool_t success = true;
774 if (!dbus_message_get_args(message, &error,
775 DBUS_TYPE_UINT32, &id,
776 DBUS_TYPE_OBJECT_PATH, &path,
777 DBUS_TYPE_BOOLEAN, &success,
779 log_error("Failed to parse message: %s", error.message);
783 if ((p = set_remove(d->set, (char*) path)))
791 dbus_error_free(&error);
792 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
795 static int enable_wait_for_jobs(DBusConnection *bus) {
803 dbus_error_init(&error);
804 dbus_bus_add_match(bus,
806 "sender='org.freedesktop.systemd1',"
807 "interface='org.freedesktop.systemd1.Manager',"
808 "member='JobRemoved',"
809 "path='/org/freedesktop/systemd1'",
812 if (dbus_error_is_set(&error)) {
813 log_error("Failed to add match: %s", error.message);
814 dbus_error_free(&error);
818 /* This is slightly dirty, since we don't undo the match registrations. */
822 static int wait_for_jobs(DBusConnection *bus, Set *s) {
833 if (!dbus_connection_add_filter(bus, wait_filter, &d, NULL)) {
834 log_error("Failed to add filter.");
839 while (!set_isempty(s) &&
840 dbus_connection_read_write_dispatch(bus, -1))
843 if (!arg_quiet && d.failed)
844 log_error("Job failed, see logs for details.");
846 r = d.failed ? -EIO : 0;
849 /* This is slightly dirty, since we don't undo the filter registration. */
854 static int start_unit_one(
861 DBusMessage *m = NULL, *reply = NULL;
869 assert(arg_no_block || s);
871 dbus_error_init(&error);
873 if (!(m = dbus_message_new_method_call(
874 "org.freedesktop.systemd1",
875 "/org/freedesktop/systemd1",
876 "org.freedesktop.systemd1.Manager",
878 log_error("Could not allocate message.");
883 if (!dbus_message_append_args(m,
884 DBUS_TYPE_STRING, &name,
885 DBUS_TYPE_STRING, &mode,
886 DBUS_TYPE_INVALID)) {
887 log_error("Could not append arguments to message.");
892 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
894 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
895 /* There's always a fallback possible for
901 log_error("Failed to issue method call: %s", error.message);
910 if (!dbus_message_get_args(reply, &error,
911 DBUS_TYPE_OBJECT_PATH, &path,
912 DBUS_TYPE_INVALID)) {
913 log_error("Failed to parse reply: %s", error.message);
918 if (!(p = strdup(path))) {
919 log_error("Failed to duplicate path.");
924 if ((r = set_put(s, p)) < 0) {
926 log_error("Failed to add path to set.");
935 dbus_message_unref(m);
938 dbus_message_unref(reply);
940 dbus_error_free(&error);
945 static enum action verb_to_action(const char *verb) {
946 if (streq(verb, "halt"))
948 else if (streq(verb, "poweroff"))
949 return ACTION_POWEROFF;
950 else if (streq(verb, "reboot"))
951 return ACTION_REBOOT;
952 else if (streq(verb, "rescue"))
953 return ACTION_RESCUE;
954 else if (streq(verb, "emergency"))
955 return ACTION_EMERGENCY;
956 else if (streq(verb, "default"))
957 return ACTION_DEFAULT;
959 return ACTION_INVALID;
962 static int start_unit(DBusConnection *bus, char **args, unsigned n) {
964 static const char * const table[_ACTION_MAX] = {
965 [ACTION_HALT] = SPECIAL_HALT_TARGET,
966 [ACTION_POWEROFF] = SPECIAL_POWEROFF_TARGET,
967 [ACTION_REBOOT] = SPECIAL_REBOOT_TARGET,
968 [ACTION_RUNLEVEL2] = SPECIAL_RUNLEVEL2_TARGET,
969 [ACTION_RUNLEVEL3] = SPECIAL_RUNLEVEL3_TARGET,
970 [ACTION_RUNLEVEL4] = SPECIAL_RUNLEVEL4_TARGET,
971 [ACTION_RUNLEVEL5] = SPECIAL_RUNLEVEL5_TARGET,
972 [ACTION_RESCUE] = SPECIAL_RESCUE_TARGET,
973 [ACTION_EMERGENCY] = SPECIAL_EMERGENCY_TARGET,
974 [ACTION_DEFAULT] = SPECIAL_DEFAULT_TARGET
979 const char *method, *mode, *one_name;
984 if (arg_action == ACTION_SYSTEMCTL) {
986 streq(args[0], "stop") ? "StopUnit" :
987 streq(args[0], "reload") ? "ReloadUnit" :
988 streq(args[0], "restart") ? "RestartUnit" :
989 streq(args[0], "try-restart") ? "TryRestartUnit" :
990 streq(args[0], "reload-or-restart") ? "ReloadOrRestartUnit" :
991 streq(args[0], "reload-or-try-restart") ? "ReloadOrTryRestartUnit" :
995 (streq(args[0], "isolate") ||
996 streq(args[0], "rescue") ||
997 streq(args[0], "emergency")) ? "isolate" :
1001 one_name = table[verb_to_action(args[0])];
1004 assert(arg_action < ELEMENTSOF(table));
1005 assert(table[arg_action]);
1007 method = "StartUnit";
1009 mode = (arg_action == ACTION_EMERGENCY ||
1010 arg_action == ACTION_RESCUE) ? "isolate" : "replace";
1012 one_name = table[arg_action];
1015 if (!arg_no_block) {
1016 if ((r = enable_wait_for_jobs(bus)) < 0) {
1017 log_error("Could not watch jobs: %s", strerror(-r));
1021 if (!(s = set_new(string_hash_func, string_compare_func))) {
1022 log_error("Failed to allocate set.");
1031 if ((r = start_unit_one(bus, method, one_name, mode, s)) <= 0)
1034 for (i = 1; i < n; i++)
1035 if ((r = start_unit_one(bus, method, args[i], mode, s)) < 0)
1040 r = wait_for_jobs(bus, s);
1049 static int start_special(DBusConnection *bus, char **args, unsigned n) {
1055 r = start_unit(bus, args, n);
1058 warn_wall(verb_to_action(args[0]));
1063 static int check_unit(DBusConnection *bus, char **args, unsigned n) {
1064 DBusMessage *m = NULL, *reply = NULL;
1066 *interface = "org.freedesktop.systemd1.Unit",
1067 *property = "ActiveState";
1068 int r = -EADDRNOTAVAIL;
1075 dbus_error_init(&error);
1077 for (i = 1; i < n; i++) {
1078 const char *path = NULL;
1080 DBusMessageIter iter, sub;
1082 if (!(m = dbus_message_new_method_call(
1083 "org.freedesktop.systemd1",
1084 "/org/freedesktop/systemd1",
1085 "org.freedesktop.systemd1.Manager",
1087 log_error("Could not allocate message.");
1092 if (!dbus_message_append_args(m,
1093 DBUS_TYPE_STRING, &args[i],
1094 DBUS_TYPE_INVALID)) {
1095 log_error("Could not append arguments to message.");
1100 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1102 /* Hmm, cannot figure out anything about this unit... */
1106 dbus_error_free(&error);
1110 if (!dbus_message_get_args(reply, &error,
1111 DBUS_TYPE_OBJECT_PATH, &path,
1112 DBUS_TYPE_INVALID)) {
1113 log_error("Failed to parse reply: %s", error.message);
1118 dbus_message_unref(m);
1119 if (!(m = dbus_message_new_method_call(
1120 "org.freedesktop.systemd1",
1122 "org.freedesktop.DBus.Properties",
1124 log_error("Could not allocate message.");
1129 if (!dbus_message_append_args(m,
1130 DBUS_TYPE_STRING, &interface,
1131 DBUS_TYPE_STRING, &property,
1132 DBUS_TYPE_INVALID)) {
1133 log_error("Could not append arguments to message.");
1138 dbus_message_unref(reply);
1139 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1140 log_error("Failed to issue method call: %s", error.message);
1145 if (!dbus_message_iter_init(reply, &iter) ||
1146 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1147 log_error("Failed to parse reply.");
1152 dbus_message_iter_recurse(&iter, &sub);
1154 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1155 log_error("Failed to parse reply.");
1160 dbus_message_iter_get_basic(&sub, &state);
1165 if (streq(state, "active") || startswith(state, "reloading"))
1168 dbus_message_unref(m);
1169 dbus_message_unref(reply);
1175 dbus_message_unref(m);
1178 dbus_message_unref(reply);
1180 dbus_error_free(&error);
1185 typedef struct ExecStatusInfo {
1191 usec_t start_timestamp;
1192 usec_t exit_timestamp;
1197 LIST_FIELDS(struct ExecStatusInfo, exec);
1200 static void exec_status_info_free(ExecStatusInfo *i) {
1208 static int exec_status_info_deserialize(DBusMessageIter *sub, ExecStatusInfo *i) {
1209 uint64_t start_timestamp, exit_timestamp;
1210 DBusMessageIter sub2, sub3;
1214 int32_t code, status;
1220 if (dbus_message_iter_get_arg_type(sub) != DBUS_TYPE_STRUCT)
1223 dbus_message_iter_recurse(sub, &sub2);
1225 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
1228 if (!(i->path = strdup(path)))
1231 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
1232 dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
1236 dbus_message_iter_recurse(&sub2, &sub3);
1237 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1238 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1239 dbus_message_iter_next(&sub3);
1244 if (!(i->argv = new0(char*, n+1)))
1248 dbus_message_iter_recurse(&sub2, &sub3);
1249 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1252 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1253 dbus_message_iter_get_basic(&sub3, &s);
1254 dbus_message_iter_next(&sub3);
1256 if (!(i->argv[n++] = strdup(s)))
1260 if (!dbus_message_iter_next(&sub2) ||
1261 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, true) < 0 ||
1262 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp, true) < 0 ||
1263 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp, true) < 0 ||
1264 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) < 0 ||
1265 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) < 0 ||
1266 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) < 0)
1270 i->start_timestamp = (usec_t) start_timestamp;
1271 i->exit_timestamp = (usec_t) exit_timestamp;
1272 i->pid = (pid_t) pid;
1279 typedef struct UnitStatusInfo {
1281 const char *load_state;
1282 const char *active_state;
1283 const char *sub_state;
1285 const char *description;
1287 const char *fragment_path;
1288 const char *default_control_group;
1293 const char *status_text;
1296 usec_t start_timestamp;
1297 usec_t exit_timestamp;
1299 int exit_code, exit_status;
1302 unsigned n_accepted;
1303 unsigned n_connections;
1307 const char *sysfs_path;
1309 /* Mount, Automount */
1315 LIST_HEAD(ExecStatusInfo, exec);
1318 static void print_status_info(UnitStatusInfo *i) {
1323 /* This shows pretty information about a unit. See
1324 * print_property() for a low-level property printer */
1326 printf("%s", strna(i->id));
1328 if (i->description && !streq_ptr(i->id, i->description))
1329 printf(" - %s", i->description);
1333 if (i->fragment_path)
1334 printf("\t Loaded: %s (%s)\n", strna(i->load_state), i->fragment_path);
1335 else if (streq_ptr(i->load_state, "failed"))
1336 printf("\t Loaded: " ANSI_HIGHLIGHT_ON "%s" ANSI_HIGHLIGHT_OFF "\n", strna(i->load_state));
1338 printf("\t Loaded: %s\n", strna(i->load_state));
1340 if (streq_ptr(i->active_state, "maintenance")) {
1341 if (streq_ptr(i->active_state, i->sub_state))
1342 printf("\t Active: " ANSI_HIGHLIGHT_ON "%s" ANSI_HIGHLIGHT_OFF "\n",
1343 strna(i->active_state));
1345 printf("\t Active: " ANSI_HIGHLIGHT_ON "%s (%s)" ANSI_HIGHLIGHT_OFF "\n",
1346 strna(i->active_state),
1347 strna(i->sub_state));
1349 if (streq_ptr(i->active_state, i->sub_state))
1350 printf("\t Active: %s\n",
1351 strna(i->active_state));
1353 printf("\t Active: %s (%s)\n",
1354 strna(i->active_state),
1355 strna(i->sub_state));
1359 printf("\t Device: %s\n", i->sysfs_path);
1361 printf("\t Where: %s\n", i->where);
1363 printf("\t What: %s\n", i->what);
1366 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
1368 LIST_FOREACH(exec, p, i->exec) {
1371 /* Only show exited processes here */
1375 t = strv_join(p->argv, " ");
1376 printf("\t Exited: %u (%s, code=%s, ", p->pid, strna(t), sigchld_code_to_string(p->code));
1379 if (p->code == CLD_EXITED)
1380 printf("status=%i", p->status);
1382 printf("signal=%s", signal_to_string(p->status));
1385 if (i->main_pid == p->pid &&
1386 i->start_timestamp == p->start_timestamp &&
1387 i->exit_timestamp == p->start_timestamp)
1388 /* Let's not show this twice */
1391 if (p->pid == i->control_pid)
1395 if (i->main_pid > 0 || i->control_pid > 0) {
1398 if (i->main_pid > 0) {
1399 printf(" Main: %u", (unsigned) i->main_pid);
1403 get_process_name(i->main_pid, &t);
1409 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
1411 if (i->exit_code == CLD_EXITED)
1412 printf("status=%i", i->exit_status);
1414 printf("signal=%s", signal_to_string(i->exit_status));
1419 if (i->main_pid > 0 && i->control_pid > 0)
1422 if (i->control_pid > 0) {
1425 printf(" Control: %u", (unsigned) i->control_pid);
1427 get_process_name(i->control_pid, &t);
1438 printf("\t Status: \"%s\"\n", i->status_text);
1440 if (i->default_control_group) {
1443 printf("\t CGroup: %s\n", i->default_control_group);
1445 if ((c = columns()) > 18)
1450 show_cgroup_by_path(i->default_control_group, "\t\t ", c);
1454 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
1456 switch (dbus_message_iter_get_arg_type(iter)) {
1458 case DBUS_TYPE_STRING: {
1461 dbus_message_iter_get_basic(iter, &s);
1464 if (streq(name, "Id"))
1466 else if (streq(name, "LoadState"))
1468 else if (streq(name, "ActiveState"))
1469 i->active_state = s;
1470 else if (streq(name, "SubState"))
1472 else if (streq(name, "Description"))
1474 else if (streq(name, "FragmentPath"))
1475 i->fragment_path = s;
1476 else if (streq(name, "DefaultControlGroup"))
1477 i->default_control_group = s;
1478 else if (streq(name, "StatusText"))
1480 else if (streq(name, "SysFSPath"))
1482 else if (streq(name, "Where"))
1484 else if (streq(name, "What"))
1491 case DBUS_TYPE_BOOLEAN: {
1494 dbus_message_iter_get_basic(iter, &b);
1496 if (streq(name, "Accept"))
1502 case DBUS_TYPE_UINT32: {
1505 dbus_message_iter_get_basic(iter, &u);
1507 if (streq(name, "MainPID")) {
1509 i->main_pid = (pid_t) u;
1512 } else if (streq(name, "ControlPID"))
1513 i->control_pid = (pid_t) u;
1514 else if (streq(name, "ExecMainPID")) {
1516 i->main_pid = (pid_t) u;
1517 } else if (streq(name, "NAccepted"))
1519 else if (streq(name, "NConnections"))
1520 i->n_connections = u;
1525 case DBUS_TYPE_INT32: {
1528 dbus_message_iter_get_basic(iter, &j);
1530 if (streq(name, "ExecMainCode"))
1531 i->exit_code = (int) j;
1532 else if (streq(name, "ExecMainStatus"))
1533 i->exit_status = (int) j;
1538 case DBUS_TYPE_UINT64: {
1541 dbus_message_iter_get_basic(iter, &u);
1543 if (streq(name, "ExecMainStartTimestamp"))
1544 i->start_timestamp = (usec_t) u;
1545 else if (streq(name, "ExecMainExitTimestamp"))
1546 i->exit_timestamp = (usec_t) u;
1551 case DBUS_TYPE_ARRAY: {
1553 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT &&
1554 startswith(name, "Exec")) {
1555 DBusMessageIter sub;
1557 dbus_message_iter_recurse(iter, &sub);
1558 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1559 ExecStatusInfo *info;
1562 if (!(info = new0(ExecStatusInfo, 1)))
1565 if ((r = exec_status_info_deserialize(&sub, info)) < 0) {
1570 LIST_PREPEND(ExecStatusInfo, exec, i->exec, info);
1572 dbus_message_iter_next(&sub);
1583 static int print_property(const char *name, DBusMessageIter *iter) {
1587 /* This is a low-level property printer, see
1588 * print_status_info() for the nicer output */
1590 if (arg_property && !streq(name, arg_property))
1593 switch (dbus_message_iter_get_arg_type(iter)) {
1595 case DBUS_TYPE_STRING: {
1597 dbus_message_iter_get_basic(iter, &s);
1599 if (arg_all || s[0])
1600 printf("%s=%s\n", name, s);
1605 case DBUS_TYPE_BOOLEAN: {
1607 dbus_message_iter_get_basic(iter, &b);
1608 printf("%s=%s\n", name, yes_no(b));
1613 case DBUS_TYPE_UINT64: {
1615 dbus_message_iter_get_basic(iter, &u);
1617 /* Yes, heuristics! But we can change this check
1618 * should it turn out to not be sufficient */
1620 if (strstr(name, "Timestamp")) {
1621 char timestamp[FORMAT_TIMESTAMP_MAX], *t;
1623 if ((t = format_timestamp(timestamp, sizeof(timestamp), u)) || arg_all)
1624 printf("%s=%s\n", name, strempty(t));
1625 } else if (strstr(name, "USec")) {
1626 char timespan[FORMAT_TIMESPAN_MAX];
1628 printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u));
1630 printf("%s=%llu\n", name, (unsigned long long) u);
1635 case DBUS_TYPE_UINT32: {
1637 dbus_message_iter_get_basic(iter, &u);
1639 if (strstr(name, "UMask") || strstr(name, "Mode"))
1640 printf("%s=%04o\n", name, u);
1642 printf("%s=%u\n", name, (unsigned) u);
1647 case DBUS_TYPE_INT32: {
1649 dbus_message_iter_get_basic(iter, &i);
1651 printf("%s=%i\n", name, (int) i);
1655 case DBUS_TYPE_STRUCT: {
1656 DBusMessageIter sub;
1657 dbus_message_iter_recurse(iter, &sub);
1659 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
1662 dbus_message_iter_get_basic(&sub, &u);
1665 printf("%s=%u\n", name, (unsigned) u);
1667 printf("%s=\n", name);
1670 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
1673 dbus_message_iter_get_basic(&sub, &s);
1675 if (arg_all || s[0])
1676 printf("%s=%s\n", name, s);
1684 case DBUS_TYPE_ARRAY:
1686 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
1687 DBusMessageIter sub;
1690 dbus_message_iter_recurse(iter, &sub);
1692 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1693 printf("%s=", name);
1695 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1698 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
1699 dbus_message_iter_get_basic(&sub, &s);
1700 printf("%s%s", space ? " " : "", s);
1703 dbus_message_iter_next(&sub);
1711 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_BYTE) {
1712 DBusMessageIter sub;
1714 dbus_message_iter_recurse(iter, &sub);
1716 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1717 printf("%s=", name);
1719 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1722 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_BYTE);
1723 dbus_message_iter_get_basic(&sub, &u);
1726 dbus_message_iter_next(&sub);
1734 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
1735 DBusMessageIter sub, sub2;
1737 dbus_message_iter_recurse(iter, &sub);
1738 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1739 const char *type, *path;
1741 dbus_message_iter_recurse(&sub, &sub2);
1743 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
1744 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
1745 printf("%s=%s\n", type, path);
1747 dbus_message_iter_next(&sub);
1752 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
1753 DBusMessageIter sub, sub2;
1755 dbus_message_iter_recurse(iter, &sub);
1756 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1758 uint64_t value, next_elapse;
1760 dbus_message_iter_recurse(&sub, &sub2);
1762 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
1763 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
1764 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
1765 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
1767 printf("%s={ value=%s ; next_elapse=%s }\n",
1769 format_timespan(timespan1, sizeof(timespan1), value),
1770 format_timespan(timespan2, sizeof(timespan2), next_elapse));
1773 dbus_message_iter_next(&sub);
1778 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
1779 DBusMessageIter sub;
1781 dbus_message_iter_recurse(iter, &sub);
1782 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1783 ExecStatusInfo info;
1786 if (exec_status_info_deserialize(&sub, &info) >= 0) {
1787 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
1790 t = strv_join(info.argv, " ");
1792 printf("%s={ path=%s ; argv[]=%s ; ignore=%s ; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n",
1796 yes_no(info.ignore),
1797 strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)),
1798 strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)),
1799 (unsigned) info. pid,
1800 sigchld_code_to_string(info.code),
1802 info.code == CLD_EXITED ? "" : "/",
1803 strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status)));
1809 strv_free(info.argv);
1811 dbus_message_iter_next(&sub);
1821 printf("%s=[unprintable]\n", name);
1826 static int show_one(DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
1827 DBusMessage *m = NULL, *reply = NULL;
1828 const char *interface = "";
1831 DBusMessageIter iter, sub, sub2, sub3;
1832 UnitStatusInfo info;
1840 dbus_error_init(&error);
1842 if (!(m = dbus_message_new_method_call(
1843 "org.freedesktop.systemd1",
1845 "org.freedesktop.DBus.Properties",
1847 log_error("Could not allocate message.");
1852 if (!dbus_message_append_args(m,
1853 DBUS_TYPE_STRING, &interface,
1854 DBUS_TYPE_INVALID)) {
1855 log_error("Could not append arguments to message.");
1860 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1861 log_error("Failed to issue method call: %s", error.message);
1866 if (!dbus_message_iter_init(reply, &iter) ||
1867 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
1868 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
1869 log_error("Failed to parse reply.");
1874 dbus_message_iter_recurse(&iter, &sub);
1881 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1884 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
1885 log_error("Failed to parse reply.");
1890 dbus_message_iter_recurse(&sub, &sub2);
1892 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
1893 log_error("Failed to parse reply.");
1898 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
1899 log_error("Failed to parse reply.");
1904 dbus_message_iter_recurse(&sub2, &sub3);
1906 if (show_properties)
1907 r = print_property(name, &sub3);
1909 r = status_property(name, &sub3, &info);
1912 log_error("Failed to parse reply.");
1917 dbus_message_iter_next(&sub);
1920 if (!show_properties)
1921 print_status_info(&info);
1923 while ((p = info.exec)) {
1924 LIST_REMOVE(ExecStatusInfo, exec, info.exec, p);
1925 exec_status_info_free(p);
1932 dbus_message_unref(m);
1935 dbus_message_unref(reply);
1937 dbus_error_free(&error);
1942 static int show(DBusConnection *bus, char **args, unsigned n) {
1943 DBusMessage *m = NULL, *reply = NULL;
1947 bool show_properties, new_line = false;
1952 dbus_error_init(&error);
1954 show_properties = !streq(args[0], "status");
1956 if (show_properties && n <= 1) {
1957 /* If not argument is specified inspect the manager
1960 r = show_one(bus, "/org/freedesktop/systemd1", show_properties, &new_line);
1964 for (i = 1; i < n; i++) {
1965 const char *path = NULL;
1968 if (!show_properties || safe_atou32(args[i], &id) < 0) {
1970 if (!(m = dbus_message_new_method_call(
1971 "org.freedesktop.systemd1",
1972 "/org/freedesktop/systemd1",
1973 "org.freedesktop.systemd1.Manager",
1975 log_error("Could not allocate message.");
1980 if (!dbus_message_append_args(m,
1981 DBUS_TYPE_STRING, &args[i],
1982 DBUS_TYPE_INVALID)) {
1983 log_error("Could not append arguments to message.");
1988 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1990 if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
1991 log_error("Failed to issue method call: %s", error.message);
1996 dbus_error_free(&error);
1998 dbus_message_unref(m);
1999 if (!(m = dbus_message_new_method_call(
2000 "org.freedesktop.systemd1",
2001 "/org/freedesktop/systemd1",
2002 "org.freedesktop.systemd1.Manager",
2004 log_error("Could not allocate message.");
2009 if (!dbus_message_append_args(m,
2010 DBUS_TYPE_STRING, &args[i],
2011 DBUS_TYPE_INVALID)) {
2012 log_error("Could not append arguments to message.");
2017 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2018 log_error("Failed to issue method call: %s", error.message);
2026 if (!(m = dbus_message_new_method_call(
2027 "org.freedesktop.systemd1",
2028 "/org/freedesktop/systemd1",
2029 "org.freedesktop.systemd1.Manager",
2031 log_error("Could not allocate message.");
2036 if (!dbus_message_append_args(m,
2037 DBUS_TYPE_UINT32, &id,
2038 DBUS_TYPE_INVALID)) {
2039 log_error("Could not append arguments to message.");
2044 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2045 log_error("Failed to issue method call: %s", error.message);
2051 if (!dbus_message_get_args(reply, &error,
2052 DBUS_TYPE_OBJECT_PATH, &path,
2053 DBUS_TYPE_INVALID)) {
2054 log_error("Failed to parse reply: %s", error.message);
2059 if ((r = show_one(bus, path, show_properties, &new_line)) < 0)
2062 dbus_message_unref(m);
2063 dbus_message_unref(reply);
2071 dbus_message_unref(m);
2074 dbus_message_unref(reply);
2076 dbus_error_free(&error);
2081 static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
2083 DBusMessage *m = NULL, *reply = NULL;
2088 dbus_error_init(&error);
2090 log_debug("Got D-Bus request: %s.%s() on %s",
2091 dbus_message_get_interface(message),
2092 dbus_message_get_member(message),
2093 dbus_message_get_path(message));
2095 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
2096 log_error("Warning! D-Bus connection terminated.");
2097 dbus_connection_close(connection);
2099 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") ||
2100 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
2101 const char *id, *path;
2103 if (!dbus_message_get_args(message, &error,
2104 DBUS_TYPE_STRING, &id,
2105 DBUS_TYPE_OBJECT_PATH, &path,
2107 log_error("Failed to parse message: %s", error.message);
2108 else if (streq(dbus_message_get_member(message), "UnitNew"))
2109 printf("Unit %s added.\n", id);
2111 printf("Unit %s removed.\n", id);
2113 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew") ||
2114 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
2118 if (!dbus_message_get_args(message, &error,
2119 DBUS_TYPE_UINT32, &id,
2120 DBUS_TYPE_OBJECT_PATH, &path,
2122 log_error("Failed to parse message: %s", error.message);
2123 else if (streq(dbus_message_get_member(message), "JobNew"))
2124 printf("Job %u added.\n", id);
2126 printf("Job %u removed.\n", id);
2129 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Unit", "Changed") ||
2130 dbus_message_is_signal(message, "org.freedesktop.systemd1.Job", "Changed")) {
2132 const char *path, *interface, *property = "Id";
2133 DBusMessageIter iter, sub;
2135 path = dbus_message_get_path(message);
2136 interface = dbus_message_get_interface(message);
2138 if (!(m = dbus_message_new_method_call(
2139 "org.freedesktop.systemd1",
2141 "org.freedesktop.DBus.Properties",
2143 log_error("Could not allocate message.");
2147 if (!dbus_message_append_args(m,
2148 DBUS_TYPE_STRING, &interface,
2149 DBUS_TYPE_STRING, &property,
2150 DBUS_TYPE_INVALID)) {
2151 log_error("Could not append arguments to message.");
2155 if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
2156 log_error("Failed to issue method call: %s", error.message);
2160 if (!dbus_message_iter_init(reply, &iter) ||
2161 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2162 log_error("Failed to parse reply.");
2166 dbus_message_iter_recurse(&iter, &sub);
2168 if (streq(interface, "org.freedesktop.systemd1.Unit")) {
2171 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2172 log_error("Failed to parse reply.");
2176 dbus_message_iter_get_basic(&sub, &id);
2177 printf("Unit %s changed.\n", id);
2181 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32) {
2182 log_error("Failed to parse reply.");
2186 dbus_message_iter_get_basic(&sub, &id);
2187 printf("Job %u changed.\n", id);
2193 dbus_message_unref(m);
2196 dbus_message_unref(reply);
2198 dbus_error_free(&error);
2199 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2203 dbus_message_unref(m);
2206 dbus_message_unref(reply);
2208 dbus_error_free(&error);
2209 return DBUS_HANDLER_RESULT_NEED_MEMORY;
2212 static int monitor(DBusConnection *bus, char **args, unsigned n) {
2213 DBusMessage *m = NULL, *reply = NULL;
2217 dbus_error_init(&error);
2220 dbus_bus_add_match(bus,
2222 "sender='org.freedesktop.systemd1',"
2223 "interface='org.freedesktop.systemd1.Manager',"
2224 "path='/org/freedesktop/systemd1'",
2227 if (dbus_error_is_set(&error)) {
2228 log_error("Failed to add match: %s", error.message);
2233 dbus_bus_add_match(bus,
2235 "sender='org.freedesktop.systemd1',"
2236 "interface='org.freedesktop.systemd1.Unit',"
2240 if (dbus_error_is_set(&error)) {
2241 log_error("Failed to add match: %s", error.message);
2246 dbus_bus_add_match(bus,
2248 "sender='org.freedesktop.systemd1',"
2249 "interface='org.freedesktop.systemd1.Job',"
2253 if (dbus_error_is_set(&error)) {
2254 log_error("Failed to add match: %s", error.message);
2260 if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
2261 log_error("Failed to add filter.");
2266 if (!(m = dbus_message_new_method_call(
2267 "org.freedesktop.systemd1",
2268 "/org/freedesktop/systemd1",
2269 "org.freedesktop.systemd1.Manager",
2271 log_error("Could not allocate message.");
2276 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2277 log_error("Failed to issue method call: %s", error.message);
2282 while (dbus_connection_read_write_dispatch(bus, -1))
2289 /* This is slightly dirty, since we don't undo the filter or the matches. */
2292 dbus_message_unref(m);
2295 dbus_message_unref(reply);
2297 dbus_error_free(&error);
2302 static int dump(DBusConnection *bus, char **args, unsigned n) {
2303 DBusMessage *m = NULL, *reply = NULL;
2308 dbus_error_init(&error);
2310 if (!(m = dbus_message_new_method_call(
2311 "org.freedesktop.systemd1",
2312 "/org/freedesktop/systemd1",
2313 "org.freedesktop.systemd1.Manager",
2315 log_error("Could not allocate message.");
2319 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2320 log_error("Failed to issue method call: %s", error.message);
2325 if (!dbus_message_get_args(reply, &error,
2326 DBUS_TYPE_STRING, &text,
2327 DBUS_TYPE_INVALID)) {
2328 log_error("Failed to parse reply: %s", error.message);
2333 fputs(text, stdout);
2339 dbus_message_unref(m);
2342 dbus_message_unref(reply);
2344 dbus_error_free(&error);
2349 static int snapshot(DBusConnection *bus, char **args, unsigned n) {
2350 DBusMessage *m = NULL, *reply = NULL;
2353 const char *name = "", *path, *id;
2354 dbus_bool_t cleanup = FALSE;
2355 DBusMessageIter iter, sub;
2357 *interface = "org.freedesktop.systemd1.Unit",
2360 dbus_error_init(&error);
2362 if (!(m = dbus_message_new_method_call(
2363 "org.freedesktop.systemd1",
2364 "/org/freedesktop/systemd1",
2365 "org.freedesktop.systemd1.Manager",
2366 "CreateSnapshot"))) {
2367 log_error("Could not allocate message.");
2374 if (!dbus_message_append_args(m,
2375 DBUS_TYPE_STRING, &name,
2376 DBUS_TYPE_BOOLEAN, &cleanup,
2377 DBUS_TYPE_INVALID)) {
2378 log_error("Could not append arguments to message.");
2383 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2384 log_error("Failed to issue method call: %s", error.message);
2389 if (!dbus_message_get_args(reply, &error,
2390 DBUS_TYPE_OBJECT_PATH, &path,
2391 DBUS_TYPE_INVALID)) {
2392 log_error("Failed to parse reply: %s", error.message);
2397 dbus_message_unref(m);
2398 if (!(m = dbus_message_new_method_call(
2399 "org.freedesktop.systemd1",
2401 "org.freedesktop.DBus.Properties",
2403 log_error("Could not allocate message.");
2407 if (!dbus_message_append_args(m,
2408 DBUS_TYPE_STRING, &interface,
2409 DBUS_TYPE_STRING, &property,
2410 DBUS_TYPE_INVALID)) {
2411 log_error("Could not append arguments to message.");
2416 dbus_message_unref(reply);
2417 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2418 log_error("Failed to issue method call: %s", error.message);
2423 if (!dbus_message_iter_init(reply, &iter) ||
2424 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2425 log_error("Failed to parse reply.");
2430 dbus_message_iter_recurse(&iter, &sub);
2432 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2433 log_error("Failed to parse reply.");
2438 dbus_message_iter_get_basic(&sub, &id);
2446 dbus_message_unref(m);
2449 dbus_message_unref(reply);
2451 dbus_error_free(&error);
2456 static int delete_snapshot(DBusConnection *bus, char **args, unsigned n) {
2457 DBusMessage *m = NULL, *reply = NULL;
2465 dbus_error_init(&error);
2467 for (i = 1; i < n; i++) {
2468 const char *path = NULL;
2470 if (!(m = dbus_message_new_method_call(
2471 "org.freedesktop.systemd1",
2472 "/org/freedesktop/systemd1",
2473 "org.freedesktop.systemd1.Manager",
2475 log_error("Could not allocate message.");
2480 if (!dbus_message_append_args(m,
2481 DBUS_TYPE_STRING, &args[i],
2482 DBUS_TYPE_INVALID)) {
2483 log_error("Could not append arguments to message.");
2488 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2489 log_error("Failed to issue method call: %s", error.message);
2494 if (!dbus_message_get_args(reply, &error,
2495 DBUS_TYPE_OBJECT_PATH, &path,
2496 DBUS_TYPE_INVALID)) {
2497 log_error("Failed to parse reply: %s", error.message);
2502 dbus_message_unref(m);
2503 if (!(m = dbus_message_new_method_call(
2504 "org.freedesktop.systemd1",
2506 "org.freedesktop.systemd1.Snapshot",
2508 log_error("Could not allocate message.");
2513 dbus_message_unref(reply);
2514 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2515 log_error("Failed to issue method call: %s", error.message);
2520 dbus_message_unref(m);
2521 dbus_message_unref(reply);
2529 dbus_message_unref(m);
2532 dbus_message_unref(reply);
2534 dbus_error_free(&error);
2539 static int clear_jobs(DBusConnection *bus, char **args, unsigned n) {
2540 DBusMessage *m = NULL, *reply = NULL;
2545 dbus_error_init(&error);
2547 if (arg_action == ACTION_RELOAD)
2549 else if (arg_action == ACTION_REEXEC)
2550 method = "Reexecute";
2552 assert(arg_action == ACTION_SYSTEMCTL);
2555 streq(args[0], "clear-jobs") ? "ClearJobs" :
2556 streq(args[0], "daemon-reload") ? "Reload" :
2557 streq(args[0], "daemon-reexec") ? "Reexecute" :
2561 if (!(m = dbus_message_new_method_call(
2562 "org.freedesktop.systemd1",
2563 "/org/freedesktop/systemd1",
2564 "org.freedesktop.systemd1.Manager",
2566 log_error("Could not allocate message.");
2570 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2572 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
2573 /* There's always a fallback possible for
2574 * legacy actions. */
2579 log_error("Failed to issue method call: %s", error.message);
2588 dbus_message_unref(m);
2591 dbus_message_unref(reply);
2593 dbus_error_free(&error);
2598 static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
2599 DBusMessage *m = NULL, *reply = NULL;
2601 DBusMessageIter iter, sub, sub2;
2604 *interface = "org.freedesktop.systemd1.Manager",
2605 *property = "Environment";
2607 dbus_error_init(&error);
2609 if (!(m = dbus_message_new_method_call(
2610 "org.freedesktop.systemd1",
2611 "/org/freedesktop/systemd1",
2612 "org.freedesktop.DBus.Properties",
2614 log_error("Could not allocate message.");
2618 if (!dbus_message_append_args(m,
2619 DBUS_TYPE_STRING, &interface,
2620 DBUS_TYPE_STRING, &property,
2621 DBUS_TYPE_INVALID)) {
2622 log_error("Could not append arguments to message.");
2627 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2628 log_error("Failed to issue method call: %s", error.message);
2633 if (!dbus_message_iter_init(reply, &iter) ||
2634 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2635 log_error("Failed to parse reply.");
2640 dbus_message_iter_recurse(&iter, &sub);
2642 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
2643 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
2644 log_error("Failed to parse reply.");
2649 dbus_message_iter_recurse(&sub, &sub2);
2651 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
2654 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
2655 log_error("Failed to parse reply.");
2660 dbus_message_iter_get_basic(&sub2, &text);
2661 printf("%s\n", text);
2663 dbus_message_iter_next(&sub2);
2670 dbus_message_unref(m);
2673 dbus_message_unref(reply);
2675 dbus_error_free(&error);
2680 static int set_environment(DBusConnection *bus, char **args, unsigned n) {
2681 DBusMessage *m = NULL, *reply = NULL;
2685 DBusMessageIter iter, sub;
2688 dbus_error_init(&error);
2690 method = streq(args[0], "set-environment")
2692 : "UnsetEnvironment";
2694 if (!(m = dbus_message_new_method_call(
2695 "org.freedesktop.systemd1",
2696 "/org/freedesktop/systemd1",
2697 "org.freedesktop.systemd1.Manager",
2700 log_error("Could not allocate message.");
2704 dbus_message_iter_init_append(m, &iter);
2706 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
2707 log_error("Could not append arguments to message.");
2712 for (i = 1; i < n; i++)
2713 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) {
2714 log_error("Could not append arguments to message.");
2719 if (!dbus_message_iter_close_container(&iter, &sub)) {
2720 log_error("Could not append arguments to message.");
2725 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2726 log_error("Failed to issue method call: %s", error.message);
2735 dbus_message_unref(m);
2738 dbus_message_unref(reply);
2740 dbus_error_free(&error);
2745 static int systemctl_help(void) {
2747 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
2748 "Send control commands to the systemd manager.\n\n"
2749 " -h --help Show this help\n"
2750 " -t --type=TYPE List only units of a particular type\n"
2751 " -p --property=NAME Show only properties by this name\n"
2752 " -a --all Show all units/properties, including dead/empty ones\n"
2753 " --fail When installing a new job, fail if conflicting jobs are\n"
2755 " --system Connect to system bus\n"
2756 " --session Connect to session bus\n"
2757 " --order When generating graph for dot, show only order\n"
2758 " --require When generating graph for dot, show only requirement\n"
2759 " -q --quiet Suppress output\n"
2760 " --no-block Do not wait until operation finished\n"
2761 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
2763 " list-units List units\n"
2764 " start [NAME...] Start one or more units\n"
2765 " stop [NAME...] Stop one or more units\n"
2766 " reload [NAME...] Reload one or more units\n"
2767 " restart [NAME...] Start or restart one or more units\n"
2768 " try-restart [NAME...] Restart one or more units if active\n"
2769 " reload-or-restart [NAME...] Reload one or more units is possible,\n"
2770 " otherwise start or restart\n"
2771 " reload-or-try-restart [NAME...] Reload one or more units is possible,\n"
2772 " otherwise restart if active\n"
2773 " isolate [NAME] Start one unit and stop all others\n"
2774 " check [NAME...] Check whether units are active\n"
2775 " status [NAME...] Show status of one or more units\n"
2776 " show [NAME...|JOB...] Show properties of one or more\n"
2777 " units/jobs/manager\n"
2778 " load [NAME...] Load one or more units\n"
2779 " list-jobs List jobs\n"
2780 " cancel [JOB...] Cancel one or more jobs\n"
2781 " clear-jobs Cancel all jobs\n"
2782 " monitor Monitor unit/job changes\n"
2783 " dump Dump server status\n"
2784 " dot Dump dependency graph for dot(1)\n"
2785 " snapshot [NAME] Create a snapshot\n"
2786 " delete [NAME...] Remove one or more snapshots\n"
2787 " daemon-reload Reload systemd manager configuration\n"
2788 " daemon-reexec Reexecute systemd manager\n"
2789 " daemon-exit Ask the systemd manager to quit\n"
2790 " show-environment Dump environment\n"
2791 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
2792 " unset-environment [NAME...] Unset one or more environment variables\n"
2793 " halt Shut down and halt the system\n"
2794 " poweroff Shut down and power-off the system\n"
2795 " reboot Shut down and reboot the system\n"
2796 " rescue Enter system rescue mode\n"
2797 " emergency Enter system emergency mode\n"
2798 " default Enter system default mode\n",
2799 program_invocation_short_name);
2804 static int halt_help(void) {
2806 printf("%s [OPTIONS...]\n\n"
2807 "%s the system.\n\n"
2808 " --help Show this help\n"
2809 " --halt Halt the machine\n"
2810 " -p --poweroff Switch off the machine\n"
2811 " --reboot Reboot the machine\n"
2812 " -f --force Force immediate halt/power-off/reboot\n"
2813 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
2814 " -d --no-wtmp Don't write wtmp record\n"
2815 " -n --no-sync Don't sync before halt/power-off/reboot\n"
2816 " --no-wall Don't send wall message before halt/power-off/reboot\n",
2817 program_invocation_short_name,
2818 arg_action == ACTION_REBOOT ? "Reboot" :
2819 arg_action == ACTION_POWEROFF ? "Power off" :
2825 static int shutdown_help(void) {
2827 printf("%s [OPTIONS...] [now] [WALL...]\n\n"
2828 "Shut down the system.\n\n"
2829 " --help Show this help\n"
2830 " -H --halt Halt the machine\n"
2831 " -P --poweroff Power-off the machine\n"
2832 " -r --reboot Reboot the machine\n"
2833 " -h Equivalent to --poweroff, overriden by --halt\n"
2834 " -k Don't halt/power-off/reboot, just send warnings\n"
2835 " --no-wall Don't send wall message before halt/power-off/reboot\n",
2836 program_invocation_short_name);
2841 static int telinit_help(void) {
2843 printf("%s [OPTIONS...] {COMMAND}\n\n"
2844 "Send control commands to the init daemon.\n\n"
2845 " --help Show this help\n"
2846 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
2848 " 0 Power-off the machine\n"
2849 " 6 Reboot the machine\n"
2850 " 2, 3, 4, 5 Start runlevelX.target unit\n"
2851 " 1, s, S Enter rescue mode\n"
2852 " q, Q Reload init daemon configuration\n"
2853 " u, U Reexecute init daemon\n",
2854 program_invocation_short_name);
2859 static int runlevel_help(void) {
2861 printf("%s [OPTIONS...]\n\n"
2862 "Prints the previous and current runlevel of the init system.\n\n"
2863 " --help Show this help\n",
2864 program_invocation_short_name);
2869 static int systemctl_parse_argv(int argc, char *argv[]) {
2881 static const struct option options[] = {
2882 { "help", no_argument, NULL, 'h' },
2883 { "type", required_argument, NULL, 't' },
2884 { "property", required_argument, NULL, 'p' },
2885 { "all", no_argument, NULL, 'a' },
2886 { "fail", no_argument, NULL, ARG_FAIL },
2887 { "session", no_argument, NULL, ARG_SESSION },
2888 { "system", no_argument, NULL, ARG_SYSTEM },
2889 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
2890 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2891 { "quiet", no_argument, NULL, 'q' },
2892 { "order", no_argument, NULL, ARG_ORDER },
2893 { "require", no_argument, NULL, ARG_REQUIRE },
2894 { NULL, 0, NULL, 0 }
2902 while ((c = getopt_long(argc, argv, "ht:p:aq", options, NULL)) >= 0) {
2915 arg_property = optarg;
2917 /* If the user asked for a particular
2918 * property, show it to him, even if it is
2936 arg_session = false;
2940 arg_no_block = true;
2948 arg_dot = DOT_ORDER;
2952 arg_dot = DOT_REQUIRE;
2963 log_error("Unknown option code %c", c);
2971 static int halt_parse_argv(int argc, char *argv[]) {
2980 static const struct option options[] = {
2981 { "help", no_argument, NULL, ARG_HELP },
2982 { "halt", no_argument, NULL, ARG_HALT },
2983 { "poweroff", no_argument, NULL, 'p' },
2984 { "reboot", no_argument, NULL, ARG_REBOOT },
2985 { "force", no_argument, NULL, 'f' },
2986 { "wtmp-only", no_argument, NULL, 'w' },
2987 { "no-wtmp", no_argument, NULL, 'd' },
2988 { "no-sync", no_argument, NULL, 'n' },
2989 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2990 { NULL, 0, NULL, 0 }
2998 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
2999 if (runlevel == '0' || runlevel == '6')
3000 arg_immediate = true;
3002 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
3010 arg_action = ACTION_HALT;
3014 arg_action = ACTION_POWEROFF;
3018 arg_action = ACTION_REBOOT;
3022 arg_immediate = true;
3043 /* Compatibility nops */
3050 log_error("Unknown option code %c", c);
3055 if (optind < argc) {
3056 log_error("Too many arguments.");
3063 static int shutdown_parse_argv(int argc, char *argv[]) {
3070 static const struct option options[] = {
3071 { "help", no_argument, NULL, ARG_HELP },
3072 { "halt", no_argument, NULL, 'H' },
3073 { "poweroff", no_argument, NULL, 'P' },
3074 { "reboot", no_argument, NULL, 'r' },
3075 { "no-wall", no_argument, NULL, ARG_NO_WALL },
3076 { NULL, 0, NULL, 0 }
3084 while ((c = getopt_long(argc, argv, "HPrhkt:a", options, NULL)) >= 0) {
3092 arg_action = ACTION_HALT;
3096 arg_action = ACTION_POWEROFF;
3100 arg_action = ACTION_REBOOT;
3104 if (arg_action != ACTION_HALT)
3105 arg_action = ACTION_POWEROFF;
3118 /* Compatibility nops */
3125 log_error("Unknown option code %c", c);
3130 if (argc > optind && !streq(argv[optind], "now"))
3131 log_warning("First argument '%s' isn't 'now'. Ignoring.", argv[optind]);
3133 /* We ignore the time argument */
3134 if (argc > optind + 1)
3135 arg_wall = argv + optind + 1;
3142 static int telinit_parse_argv(int argc, char *argv[]) {
3149 static const struct option options[] = {
3150 { "help", no_argument, NULL, ARG_HELP },
3151 { "no-wall", no_argument, NULL, ARG_NO_WALL },
3152 { NULL, 0, NULL, 0 }
3155 static const struct {
3159 { '0', ACTION_POWEROFF },
3160 { '6', ACTION_REBOOT },
3161 { '1', ACTION_RESCUE },
3162 { '2', ACTION_RUNLEVEL2 },
3163 { '3', ACTION_RUNLEVEL3 },
3164 { '4', ACTION_RUNLEVEL4 },
3165 { '5', ACTION_RUNLEVEL5 },
3166 { 's', ACTION_RESCUE },
3167 { 'S', ACTION_RESCUE },
3168 { 'q', ACTION_RELOAD },
3169 { 'Q', ACTION_RELOAD },
3170 { 'u', ACTION_REEXEC },
3171 { 'U', ACTION_REEXEC }
3180 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
3195 log_error("Unknown option code %c", c);
3200 if (optind >= argc) {
3205 if (optind + 1 < argc) {
3206 log_error("Too many arguments.");
3210 if (strlen(argv[optind]) != 1) {
3211 log_error("Expected single character argument.");
3215 for (i = 0; i < ELEMENTSOF(table); i++)
3216 if (table[i].from == argv[optind][0])
3219 if (i >= ELEMENTSOF(table)) {
3220 log_error("Unknown command %s.", argv[optind]);
3224 arg_action = table[i].to;
3231 static int runlevel_parse_argv(int argc, char *argv[]) {
3237 static const struct option options[] = {
3238 { "help", no_argument, NULL, ARG_HELP },
3239 { NULL, 0, NULL, 0 }
3247 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
3258 log_error("Unknown option code %c", c);
3263 if (optind < argc) {
3264 log_error("Too many arguments.");
3271 static int parse_argv(int argc, char *argv[]) {
3275 if (program_invocation_short_name) {
3277 if (strstr(program_invocation_short_name, "halt")) {
3278 arg_action = ACTION_HALT;
3279 return halt_parse_argv(argc, argv);
3280 } else if (strstr(program_invocation_short_name, "poweroff")) {
3281 arg_action = ACTION_POWEROFF;
3282 return halt_parse_argv(argc, argv);
3283 } else if (strstr(program_invocation_short_name, "reboot")) {
3284 arg_action = ACTION_REBOOT;
3285 return halt_parse_argv(argc, argv);
3286 } else if (strstr(program_invocation_short_name, "shutdown")) {
3287 arg_action = ACTION_POWEROFF;
3288 return shutdown_parse_argv(argc, argv);
3289 } else if (strstr(program_invocation_short_name, "init")) {
3290 arg_action = ACTION_INVALID;
3291 return telinit_parse_argv(argc, argv);
3292 } else if (strstr(program_invocation_short_name, "runlevel")) {
3293 arg_action = ACTION_RUNLEVEL;
3294 return runlevel_parse_argv(argc, argv);
3298 arg_action = ACTION_SYSTEMCTL;
3299 return systemctl_parse_argv(argc, argv);
3302 static int action_to_runlevel(void) {
3304 static const char table[_ACTION_MAX] = {
3305 [ACTION_HALT] = '0',
3306 [ACTION_POWEROFF] = '0',
3307 [ACTION_REBOOT] = '6',
3308 [ACTION_RUNLEVEL2] = '2',
3309 [ACTION_RUNLEVEL3] = '3',
3310 [ACTION_RUNLEVEL4] = '4',
3311 [ACTION_RUNLEVEL5] = '5',
3312 [ACTION_RESCUE] = '1'
3315 assert(arg_action < _ACTION_MAX);
3317 return table[arg_action];
3320 static int talk_upstart(void) {
3321 DBusMessage *m = NULL, *reply = NULL;
3323 int previous, rl, r;
3325 env1_buf[] = "RUNLEVEL=X",
3326 env2_buf[] = "PREVLEVEL=X";
3327 char *env1 = env1_buf, *env2 = env2_buf;
3328 const char *emit = "runlevel";
3329 dbus_bool_t b_false = FALSE;
3330 DBusMessageIter iter, sub;
3331 DBusConnection *bus;
3333 dbus_error_init(&error);
3335 if (!(rl = action_to_runlevel()))
3338 if (utmp_get_runlevel(&previous, NULL) < 0)
3341 if (!(bus = dbus_connection_open_private("unix:abstract=/com/ubuntu/upstart", &error))) {
3342 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
3347 log_error("Failed to connect to Upstart bus: %s", error.message);
3352 if ((r = bus_check_peercred(bus)) < 0) {
3353 log_error("Failed to verify owner of bus.");
3357 if (!(m = dbus_message_new_method_call(
3358 "com.ubuntu.Upstart",
3359 "/com/ubuntu/Upstart",
3360 "com.ubuntu.Upstart0_6",
3363 log_error("Could not allocate message.");
3368 dbus_message_iter_init_append(m, &iter);
3370 env1_buf[sizeof(env1_buf)-2] = rl;
3371 env2_buf[sizeof(env2_buf)-2] = previous;
3373 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
3374 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
3375 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
3376 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
3377 !dbus_message_iter_close_container(&iter, &sub) ||
3378 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
3379 log_error("Could not append arguments to message.");
3384 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3386 if (error_is_no_service(&error)) {
3391 log_error("Failed to issue method call: %s", error.message);
3400 dbus_message_unref(m);
3403 dbus_message_unref(reply);
3406 dbus_connection_close(bus);
3407 dbus_connection_unref(bus);
3410 dbus_error_free(&error);
3415 static int talk_initctl(void) {
3416 struct init_request request;
3420 if (!(rl = action_to_runlevel()))
3424 request.magic = INIT_MAGIC;
3425 request.sleeptime = 0;
3426 request.cmd = INIT_CMD_RUNLVL;
3427 request.runlevel = rl;
3429 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
3431 if (errno == ENOENT)
3434 log_error("Failed to open "INIT_FIFO": %m");
3439 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
3440 close_nointr_nofail(fd);
3443 log_error("Failed to write to "INIT_FIFO": %m");
3444 return errno ? -errno : -EIO;
3450 static int systemctl_main(DBusConnection *bus, int argc, char *argv[]) {
3452 static const struct {
3460 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
3462 { "list-units", LESS, 1, list_units },
3463 { "list-jobs", EQUAL, 1, list_jobs },
3464 { "clear-jobs", EQUAL, 1, clear_jobs },
3465 { "load", MORE, 2, load_unit },
3466 { "cancel", MORE, 2, cancel_job },
3467 { "start", MORE, 2, start_unit },
3468 { "stop", MORE, 2, start_unit },
3469 { "reload", MORE, 2, start_unit },
3470 { "restart", MORE, 2, start_unit },
3471 { "try-restart", MORE, 2, start_unit },
3472 { "reload-or-restart", MORE, 2, start_unit },
3473 { "reload-or-try-restart", MORE, 2, start_unit },
3474 { "isolate", EQUAL, 2, start_unit },
3475 { "check", MORE, 2, check_unit },
3476 { "show", MORE, 1, show },
3477 { "status", MORE, 2, show },
3478 { "monitor", EQUAL, 1, monitor },
3479 { "dump", EQUAL, 1, dump },
3480 { "dot", EQUAL, 1, dot },
3481 { "snapshot", LESS, 2, snapshot },
3482 { "delete", MORE, 2, delete_snapshot },
3483 { "daemon-reload", EQUAL, 1, clear_jobs },
3484 { "daemon-reexec", EQUAL, 1, clear_jobs },
3485 { "daemon-exit", EQUAL, 1, clear_jobs },
3486 { "show-environment", EQUAL, 1, show_enviroment },
3487 { "set-environment", MORE, 2, set_environment },
3488 { "unset-environment", MORE, 2, set_environment },
3489 { "halt", EQUAL, 1, start_special },
3490 { "poweroff", EQUAL, 1, start_special },
3491 { "reboot", EQUAL, 1, start_special },
3492 { "default", EQUAL, 1, start_special },
3493 { "rescue", EQUAL, 1, start_special },
3494 { "emergency", EQUAL, 1, start_special },
3504 left = argc - optind;
3507 /* Special rule: no arguments means "list-units" */
3510 if (streq(argv[optind], "help")) {
3515 for (i = 0; i < ELEMENTSOF(verbs); i++)
3516 if (streq(argv[optind], verbs[i].verb))
3519 if (i >= ELEMENTSOF(verbs)) {
3520 log_error("Unknown operation %s", argv[optind]);
3525 switch (verbs[i].argc_cmp) {
3528 if (left != verbs[i].argc) {
3529 log_error("Invalid number of arguments.");
3536 if (left < verbs[i].argc) {
3537 log_error("Too few arguments.");
3544 if (left > verbs[i].argc) {
3545 log_error("Too many arguments.");
3552 assert_not_reached("Unknown comparison operator.");
3555 return verbs[i].dispatch(bus, argv + optind, left);
3558 static int reload_with_fallback(DBusConnection *bus) {
3562 /* First, try systemd via D-Bus. */
3563 if ((r = clear_jobs(bus, NULL, 0)) > 0)
3567 /* Nothing else worked, so let's try signals */
3568 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
3570 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
3571 log_error("kill() failed: %m");
3578 static int start_with_fallback(DBusConnection *bus) {
3583 /* First, try systemd via D-Bus. */
3584 if ((r = start_unit(bus, NULL, 0)) > 0)
3587 /* Hmm, talking to systemd via D-Bus didn't work. Then
3588 * let's try to talk to Upstart via D-Bus. */
3589 if ((r = talk_upstart()) > 0)
3593 /* Nothing else worked, so let's try
3595 if ((r = talk_initctl()) != 0)
3598 log_error("Failed to talk to init daemon.");
3602 warn_wall(arg_action);
3606 static int halt_main(DBusConnection *bus) {
3609 if (geteuid() != 0) {
3610 log_error("Must to be root.");
3614 if (!arg_dry && !arg_immediate)
3615 return start_with_fallback(bus);
3618 if ((r = utmp_put_shutdown(0)) < 0)
3619 log_warning("Failed to write utmp record: %s", strerror(-r));
3627 /* Make sure C-A-D is handled by the kernel from this
3629 reboot(RB_ENABLE_CAD);
3631 switch (arg_action) {
3634 log_info("Halting");
3635 reboot(RB_HALT_SYSTEM);
3638 case ACTION_POWEROFF:
3639 log_info("Powering off");
3640 reboot(RB_POWER_OFF);
3644 log_info("Rebooting");
3645 reboot(RB_AUTOBOOT);
3649 assert_not_reached("Unknown halt action.");
3652 /* We should never reach this. */
3656 static int runlevel_main(void) {
3657 int r, runlevel, previous;
3659 if ((r = utmp_get_runlevel(&runlevel, &previous)) < 0) {
3665 previous <= 0 ? 'N' : previous,
3666 runlevel <= 0 ? 'N' : runlevel);
3671 int main(int argc, char*argv[]) {
3673 DBusConnection *bus = NULL;
3676 dbus_error_init(&error);
3678 log_parse_environment();
3680 if ((r = parse_argv(argc, argv)) < 0)
3687 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
3688 * let's shortcut this */
3689 if (arg_action == ACTION_RUNLEVEL) {
3690 retval = runlevel_main() < 0;
3694 bus_connect(arg_session ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &bus, &private_bus, &error);
3696 switch (arg_action) {
3698 case ACTION_SYSTEMCTL: {
3701 log_error("Failed to get D-Bus connection: %s", error.message);
3705 retval = systemctl_main(bus, argc, argv) < 0;
3710 case ACTION_POWEROFF:
3712 retval = halt_main(bus) < 0;
3715 case ACTION_RUNLEVEL2:
3716 case ACTION_RUNLEVEL3:
3717 case ACTION_RUNLEVEL4:
3718 case ACTION_RUNLEVEL5:
3720 case ACTION_EMERGENCY:
3721 case ACTION_DEFAULT:
3722 retval = start_with_fallback(bus) < 0;
3727 retval = reload_with_fallback(bus) < 0;
3730 case ACTION_INVALID:
3731 case ACTION_RUNLEVEL:
3733 assert_not_reached("Unknown action");
3739 dbus_connection_close(bus);
3740 dbus_connection_unref(bus);
3743 dbus_error_free(&error);