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"))) {
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(" %-15s%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);
253 printf("\n%u units listed.\n", k);
255 printf("\n%u units listed. Pass --all to see inactive units, too.\n", k);
261 dbus_message_unref(m);
264 dbus_message_unref(reply);
266 dbus_error_free(&error);
271 static int dot_one_property(const char *name, const char *prop, DBusMessageIter *iter) {
272 static const char * const colors[] = {
273 "Requires", "[color=\"black\"]",
274 "RequiresOverridable", "[color=\"black\"]",
275 "Requisite", "[color=\"darkblue\"]",
276 "RequisiteOverridable", "[color=\"darkblue\"]",
277 "Wants", "[color=\"darkgrey\"]",
278 "Conflicts", "[color=\"red\"]",
279 "After", "[color=\"green\"]"
282 const char *c = NULL;
289 for (i = 0; i < ELEMENTSOF(colors); i += 2)
290 if (streq(colors[i], prop)) {
298 if (arg_dot != DOT_ALL)
299 if ((arg_dot == DOT_ORDER) != streq(prop, "After"))
302 switch (dbus_message_iter_get_arg_type(iter)) {
304 case DBUS_TYPE_ARRAY:
306 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
309 dbus_message_iter_recurse(iter, &sub);
311 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
314 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
315 dbus_message_iter_get_basic(&sub, &s);
316 printf("\t\"%s\"->\"%s\" %s;\n", name, s, c);
318 dbus_message_iter_next(&sub);
328 static int dot_one(DBusConnection *bus, const char *name, const char *path) {
329 DBusMessage *m = NULL, *reply = NULL;
330 const char *interface = "org.freedesktop.systemd1.Unit";
333 DBusMessageIter iter, sub, sub2, sub3;
338 dbus_error_init(&error);
340 if (!(m = dbus_message_new_method_call(
341 "org.freedesktop.systemd1",
343 "org.freedesktop.DBus.Properties",
345 log_error("Could not allocate message.");
350 if (!dbus_message_append_args(m,
351 DBUS_TYPE_STRING, &interface,
352 DBUS_TYPE_INVALID)) {
353 log_error("Could not append arguments to message.");
358 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
359 log_error("Failed to issue method call: %s", error.message);
364 if (!dbus_message_iter_init(reply, &iter) ||
365 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
366 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
367 log_error("Failed to parse reply.");
372 dbus_message_iter_recurse(&iter, &sub);
374 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
377 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
378 log_error("Failed to parse reply.");
383 dbus_message_iter_recurse(&sub, &sub2);
385 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &prop, true) < 0) {
386 log_error("Failed to parse reply.");
391 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
392 log_error("Failed to parse reply.");
397 dbus_message_iter_recurse(&sub2, &sub3);
399 if (dot_one_property(name, prop, &sub3)) {
400 log_error("Failed to parse reply.");
405 dbus_message_iter_next(&sub);
410 dbus_message_unref(m);
413 dbus_message_unref(reply);
415 dbus_error_free(&error);
420 static int dot(DBusConnection *bus, char **args, unsigned n) {
421 DBusMessage *m = NULL, *reply = NULL;
424 DBusMessageIter iter, sub, sub2;
426 dbus_error_init(&error);
430 if (!(m = dbus_message_new_method_call(
431 "org.freedesktop.systemd1",
432 "/org/freedesktop/systemd1",
433 "org.freedesktop.systemd1.Manager",
435 log_error("Could not allocate message.");
439 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
440 log_error("Failed to issue method call: %s", error.message);
445 if (!dbus_message_iter_init(reply, &iter) ||
446 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
447 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
448 log_error("Failed to parse reply.");
453 printf("digraph systemd {\n");
455 dbus_message_iter_recurse(&iter, &sub);
456 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
457 const char *id, *description, *load_state, *active_state, *sub_state, *unit_path;
459 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
460 log_error("Failed to parse reply.");
465 dbus_message_iter_recurse(&sub, &sub2);
467 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
468 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
469 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
470 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
471 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
472 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, true) < 0) {
473 log_error("Failed to parse reply.");
478 if ((r = dot_one(bus, id, unit_path)) < 0)
481 /* printf("\t\"%s\";\n", id); */
482 dbus_message_iter_next(&sub);
487 log_info(" Color legend: black = Requires\n"
488 " dark blue = Requisite\n"
489 " dark grey = Wants\n"
493 if (isatty(fileno(stdout)))
494 log_notice("-- You probably want to process this output with graphviz' dot tool.\n"
495 "-- Try a shell pipeline like 'systemctl dot | dot -Tsvg > systemd.svg'!\n");
501 dbus_message_unref(m);
504 dbus_message_unref(reply);
506 dbus_error_free(&error);
511 static int list_jobs(DBusConnection *bus, char **args, unsigned n) {
512 DBusMessage *m = NULL, *reply = NULL;
515 DBusMessageIter iter, sub, sub2;
518 dbus_error_init(&error);
522 if (!(m = dbus_message_new_method_call(
523 "org.freedesktop.systemd1",
524 "/org/freedesktop/systemd1",
525 "org.freedesktop.systemd1.Manager",
527 log_error("Could not allocate message.");
531 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
532 log_error("Failed to issue method call: %s", error.message);
537 if (!dbus_message_iter_init(reply, &iter) ||
538 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
539 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
540 log_error("Failed to parse reply.");
545 dbus_message_iter_recurse(&iter, &sub);
547 printf("%4s %-45s %-17s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
549 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
550 const char *name, *type, *state, *job_path, *unit_path;
553 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
554 log_error("Failed to parse reply.");
559 dbus_message_iter_recurse(&sub, &sub2);
561 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
562 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
563 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
564 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
565 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
566 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
567 log_error("Failed to parse reply.");
572 printf("%4u %-45s %-17s %-7s\n", id, name, type, state);
575 dbus_message_iter_next(&sub);
578 printf("\n%u jobs listed.\n", k);
583 dbus_message_unref(m);
586 dbus_message_unref(reply);
588 dbus_error_free(&error);
593 static int load_unit(DBusConnection *bus, char **args, unsigned n) {
594 DBusMessage *m = NULL, *reply = NULL;
599 dbus_error_init(&error);
604 for (i = 1; i < n; i++) {
606 if (!(m = dbus_message_new_method_call(
607 "org.freedesktop.systemd1",
608 "/org/freedesktop/systemd1",
609 "org.freedesktop.systemd1.Manager",
611 log_error("Could not allocate message.");
616 if (!dbus_message_append_args(m,
617 DBUS_TYPE_STRING, &args[i],
618 DBUS_TYPE_INVALID)) {
619 log_error("Could not append arguments to message.");
624 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
625 log_error("Failed to issue method call: %s", error.message);
630 dbus_message_unref(m);
631 dbus_message_unref(reply);
640 dbus_message_unref(m);
643 dbus_message_unref(reply);
645 dbus_error_free(&error);
650 static int cancel_job(DBusConnection *bus, char **args, unsigned n) {
651 DBusMessage *m = NULL, *reply = NULL;
656 dbus_error_init(&error);
661 for (i = 1; i < n; i++) {
665 if (!(m = dbus_message_new_method_call(
666 "org.freedesktop.systemd1",
667 "/org/freedesktop/systemd1",
668 "org.freedesktop.systemd1.Manager",
670 log_error("Could not allocate message.");
675 if ((r = safe_atou(args[i], &id)) < 0) {
676 log_error("Failed to parse job id: %s", strerror(-r));
680 assert_cc(sizeof(uint32_t) == sizeof(id));
681 if (!dbus_message_append_args(m,
682 DBUS_TYPE_UINT32, &id,
683 DBUS_TYPE_INVALID)) {
684 log_error("Could not append arguments to message.");
689 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
690 log_error("Failed to issue method call: %s", error.message);
695 if (!dbus_message_get_args(reply, &error,
696 DBUS_TYPE_OBJECT_PATH, &path,
697 DBUS_TYPE_INVALID)) {
698 log_error("Failed to parse reply: %s", error.message);
703 dbus_message_unref(m);
704 if (!(m = dbus_message_new_method_call(
705 "org.freedesktop.systemd1",
707 "org.freedesktop.systemd1.Job",
709 log_error("Could not allocate message.");
714 dbus_message_unref(reply);
715 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
716 log_error("Failed to issue method call: %s", error.message);
721 dbus_message_unref(m);
722 dbus_message_unref(reply);
730 dbus_message_unref(m);
733 dbus_message_unref(reply);
735 dbus_error_free(&error);
740 typedef struct WaitData {
745 static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
753 dbus_error_init(&error);
755 log_debug("Got D-Bus request: %s.%s() on %s",
756 dbus_message_get_interface(message),
757 dbus_message_get_member(message),
758 dbus_message_get_path(message));
760 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
761 log_error("Warning! D-Bus connection terminated.");
762 dbus_connection_close(connection);
764 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
767 dbus_bool_t success = true;
769 if (!dbus_message_get_args(message, &error,
770 DBUS_TYPE_UINT32, &id,
771 DBUS_TYPE_OBJECT_PATH, &path,
772 DBUS_TYPE_BOOLEAN, &success,
774 log_error("Failed to parse message: %s", error.message);
778 if ((p = set_remove(d->set, (char*) path)))
786 dbus_error_free(&error);
787 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
790 static int enable_wait_for_jobs(DBusConnection *bus) {
798 dbus_error_init(&error);
799 dbus_bus_add_match(bus,
801 "sender='org.freedesktop.systemd1',"
802 "interface='org.freedesktop.systemd1.Manager',"
803 "member='JobRemoved',"
804 "path='/org/freedesktop/systemd1'",
807 if (dbus_error_is_set(&error)) {
808 log_error("Failed to add match: %s", error.message);
809 dbus_error_free(&error);
813 /* This is slightly dirty, since we don't undo the match registrations. */
817 static int wait_for_jobs(DBusConnection *bus, Set *s) {
828 if (!dbus_connection_add_filter(bus, wait_filter, &d, NULL)) {
829 log_error("Failed to add filter.");
834 while (!set_isempty(s) &&
835 dbus_connection_read_write_dispatch(bus, -1))
838 if (!arg_quiet && d.failed)
839 log_error("Job failed, see logs for details.");
841 r = d.failed ? -EIO : 0;
844 /* This is slightly dirty, since we don't undo the filter registration. */
849 static int start_unit_one(
856 DBusMessage *m = NULL, *reply = NULL;
864 assert(arg_no_block || s);
866 dbus_error_init(&error);
868 if (!(m = dbus_message_new_method_call(
869 "org.freedesktop.systemd1",
870 "/org/freedesktop/systemd1",
871 "org.freedesktop.systemd1.Manager",
873 log_error("Could not allocate message.");
878 if (!dbus_message_append_args(m,
879 DBUS_TYPE_STRING, &name,
880 DBUS_TYPE_STRING, &mode,
881 DBUS_TYPE_INVALID)) {
882 log_error("Could not append arguments to message.");
887 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
889 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
890 /* There's always a fallback possible for
896 log_error("Failed to issue method call: %s", error.message);
905 if (!dbus_message_get_args(reply, &error,
906 DBUS_TYPE_OBJECT_PATH, &path,
907 DBUS_TYPE_INVALID)) {
908 log_error("Failed to parse reply: %s", error.message);
913 if (!(p = strdup(path))) {
914 log_error("Failed to duplicate path.");
919 if ((r = set_put(s, p)) < 0) {
921 log_error("Failed to add path to set.");
930 dbus_message_unref(m);
933 dbus_message_unref(reply);
935 dbus_error_free(&error);
940 static enum action verb_to_action(const char *verb) {
941 if (streq(verb, "halt"))
943 else if (streq(verb, "poweroff"))
944 return ACTION_POWEROFF;
945 else if (streq(verb, "reboot"))
946 return ACTION_REBOOT;
947 else if (streq(verb, "rescue"))
948 return ACTION_RESCUE;
949 else if (streq(verb, "emergency"))
950 return ACTION_EMERGENCY;
951 else if (streq(verb, "default"))
952 return ACTION_DEFAULT;
954 return ACTION_INVALID;
957 static int start_unit(DBusConnection *bus, char **args, unsigned n) {
959 static const char * const table[_ACTION_MAX] = {
960 [ACTION_HALT] = SPECIAL_HALT_TARGET,
961 [ACTION_POWEROFF] = SPECIAL_POWEROFF_TARGET,
962 [ACTION_REBOOT] = SPECIAL_REBOOT_TARGET,
963 [ACTION_RUNLEVEL2] = SPECIAL_RUNLEVEL2_TARGET,
964 [ACTION_RUNLEVEL3] = SPECIAL_RUNLEVEL3_TARGET,
965 [ACTION_RUNLEVEL4] = SPECIAL_RUNLEVEL4_TARGET,
966 [ACTION_RUNLEVEL5] = SPECIAL_RUNLEVEL5_TARGET,
967 [ACTION_RESCUE] = SPECIAL_RESCUE_TARGET,
968 [ACTION_EMERGENCY] = SPECIAL_EMERGENCY_TARGET,
969 [ACTION_DEFAULT] = SPECIAL_DEFAULT_TARGET
974 const char *method, *mode, *one_name;
979 if (arg_action == ACTION_SYSTEMCTL) {
981 streq(args[0], "stop") ? "StopUnit" :
982 streq(args[0], "reload") ? "ReloadUnit" :
983 streq(args[0], "restart") ? "RestartUnit" :
984 streq(args[0], "try-restart") ? "TryRestartUnit" :
985 streq(args[0], "reload-or-restart") ? "ReloadOrRestartUnit" :
986 streq(args[0], "reload-or-try-restart") ? "ReloadOrTryRestartUnit" :
990 (streq(args[0], "isolate") ||
991 streq(args[0], "rescue") ||
992 streq(args[0], "emergency")) ? "isolate" :
996 one_name = table[verb_to_action(args[0])];
999 assert(arg_action < ELEMENTSOF(table));
1000 assert(table[arg_action]);
1002 method = "StartUnit";
1004 mode = (arg_action == ACTION_EMERGENCY ||
1005 arg_action == ACTION_RESCUE) ? "isolate" : "replace";
1007 one_name = table[arg_action];
1010 if (!arg_no_block) {
1011 if ((r = enable_wait_for_jobs(bus)) < 0) {
1012 log_error("Could not watch jobs: %s", strerror(-r));
1016 if (!(s = set_new(string_hash_func, string_compare_func))) {
1017 log_error("Failed to allocate set.");
1026 if ((r = start_unit_one(bus, method, one_name, mode, s)) <= 0)
1029 for (i = 1; i < n; i++)
1030 if ((r = start_unit_one(bus, method, args[i], mode, s)) < 0)
1035 r = wait_for_jobs(bus, s);
1044 static int start_special(DBusConnection *bus, char **args, unsigned n) {
1050 r = start_unit(bus, args, n);
1053 warn_wall(verb_to_action(args[0]));
1058 static int check_unit(DBusConnection *bus, char **args, unsigned n) {
1059 DBusMessage *m = NULL, *reply = NULL;
1061 *interface = "org.freedesktop.systemd1.Unit",
1062 *property = "ActiveState";
1063 int r = -EADDRNOTAVAIL;
1070 dbus_error_init(&error);
1072 for (i = 1; i < n; i++) {
1073 const char *path = NULL;
1075 DBusMessageIter iter, sub;
1077 if (!(m = dbus_message_new_method_call(
1078 "org.freedesktop.systemd1",
1079 "/org/freedesktop/systemd1",
1080 "org.freedesktop.systemd1.Manager",
1082 log_error("Could not allocate message.");
1087 if (!dbus_message_append_args(m,
1088 DBUS_TYPE_STRING, &args[i],
1089 DBUS_TYPE_INVALID)) {
1090 log_error("Could not append arguments to message.");
1095 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1097 /* Hmm, cannot figure out anything about this unit... */
1101 dbus_error_free(&error);
1105 if (!dbus_message_get_args(reply, &error,
1106 DBUS_TYPE_OBJECT_PATH, &path,
1107 DBUS_TYPE_INVALID)) {
1108 log_error("Failed to parse reply: %s", error.message);
1113 dbus_message_unref(m);
1114 if (!(m = dbus_message_new_method_call(
1115 "org.freedesktop.systemd1",
1117 "org.freedesktop.DBus.Properties",
1119 log_error("Could not allocate message.");
1124 if (!dbus_message_append_args(m,
1125 DBUS_TYPE_STRING, &interface,
1126 DBUS_TYPE_STRING, &property,
1127 DBUS_TYPE_INVALID)) {
1128 log_error("Could not append arguments to message.");
1133 dbus_message_unref(reply);
1134 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1135 log_error("Failed to issue method call: %s", error.message);
1140 if (!dbus_message_iter_init(reply, &iter) ||
1141 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1142 log_error("Failed to parse reply.");
1147 dbus_message_iter_recurse(&iter, &sub);
1149 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1150 log_error("Failed to parse reply.");
1155 dbus_message_iter_get_basic(&sub, &state);
1160 if (streq(state, "active") || startswith(state, "reloading"))
1163 dbus_message_unref(m);
1164 dbus_message_unref(reply);
1170 dbus_message_unref(m);
1173 dbus_message_unref(reply);
1175 dbus_error_free(&error);
1180 typedef struct ExecStatusInfo {
1186 usec_t start_timestamp;
1187 usec_t exit_timestamp;
1192 LIST_FIELDS(struct ExecStatusInfo, exec);
1195 static void exec_status_info_free(ExecStatusInfo *i) {
1203 static int exec_status_info_deserialize(DBusMessageIter *sub, ExecStatusInfo *i) {
1204 uint64_t start_timestamp, exit_timestamp;
1205 DBusMessageIter sub2, sub3;
1209 int32_t code, status;
1215 if (dbus_message_iter_get_arg_type(sub) != DBUS_TYPE_STRUCT)
1218 dbus_message_iter_recurse(sub, &sub2);
1220 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
1223 if (!(i->path = strdup(path)))
1226 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
1227 dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
1231 dbus_message_iter_recurse(&sub2, &sub3);
1232 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1233 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1234 dbus_message_iter_next(&sub3);
1239 if (!(i->argv = new0(char*, n+1)))
1243 dbus_message_iter_recurse(&sub2, &sub3);
1244 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1247 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1248 dbus_message_iter_get_basic(&sub3, &s);
1249 dbus_message_iter_next(&sub3);
1251 if (!(i->argv[n++] = strdup(s)))
1255 if (!dbus_message_iter_next(&sub2) ||
1256 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, true) < 0 ||
1257 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp, true) < 0 ||
1258 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp, true) < 0 ||
1259 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) < 0 ||
1260 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) < 0 ||
1261 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) < 0)
1265 i->start_timestamp = (usec_t) start_timestamp;
1266 i->exit_timestamp = (usec_t) exit_timestamp;
1267 i->pid = (pid_t) pid;
1274 typedef struct UnitStatusInfo {
1276 const char *load_state;
1277 const char *active_state;
1278 const char *sub_state;
1280 const char *description;
1282 const char *fragment_path;
1283 const char *default_control_group;
1288 const char *status_text;
1291 usec_t start_timestamp;
1292 usec_t exit_timestamp;
1294 int exit_code, exit_status;
1297 unsigned n_accepted;
1298 unsigned n_connections;
1302 const char *sysfs_path;
1304 /* Mount, Automount */
1310 LIST_HEAD(ExecStatusInfo, exec);
1313 static void print_status_info(UnitStatusInfo *i) {
1318 /* This shows pretty information about a unit. See
1319 * print_property() for a low-level property printer */
1321 printf("%s", strna(i->id));
1323 if (i->description && !streq_ptr(i->id, i->description))
1324 printf(" - %s", i->description);
1328 if (i->fragment_path)
1329 printf("\t Loaded: %s (%s)\n", strna(i->load_state), i->fragment_path);
1330 else if (streq_ptr(i->load_state, "failed"))
1331 printf("\t Loaded: " ANSI_HIGHLIGHT_ON "%s" ANSI_HIGHLIGHT_OFF "\n", strna(i->load_state));
1333 printf("\t Loaded: %s\n", strna(i->load_state));
1335 if (streq_ptr(i->active_state, "maintenance")) {
1336 if (streq_ptr(i->active_state, i->sub_state))
1337 printf("\t Active: " ANSI_HIGHLIGHT_ON "%s" ANSI_HIGHLIGHT_OFF "\n",
1338 strna(i->active_state));
1340 printf("\t Active: " ANSI_HIGHLIGHT_ON "%s (%s)" ANSI_HIGHLIGHT_OFF "\n",
1341 strna(i->active_state),
1342 strna(i->sub_state));
1344 if (streq_ptr(i->active_state, i->sub_state))
1345 printf("\t Active: %s\n",
1346 strna(i->active_state));
1348 printf("\t Active: %s (%s)\n",
1349 strna(i->active_state),
1350 strna(i->sub_state));
1354 printf("\t Device: %s\n", i->sysfs_path);
1356 printf("\t Where: %s\n", i->where);
1358 printf("\t What: %s\n", i->what);
1361 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
1363 LIST_FOREACH(exec, p, i->exec) {
1366 /* Only show exited processes here */
1370 t = strv_join(p->argv, " ");
1371 printf("\t Exited: %u (%s, code=%s, ", p->pid, strna(t), sigchld_code_to_string(p->code));
1374 if (p->code == CLD_EXITED)
1375 printf("status=%i", p->status);
1377 printf("signal=%s", signal_to_string(p->status));
1380 if (i->main_pid == p->pid &&
1381 i->start_timestamp == p->start_timestamp &&
1382 i->exit_timestamp == p->start_timestamp)
1383 /* Let's not show this twice */
1386 if (p->pid == i->control_pid)
1390 if (i->main_pid > 0 || i->control_pid > 0) {
1393 if (i->main_pid > 0) {
1394 printf(" Main: %u", (unsigned) i->main_pid);
1398 get_process_name(i->main_pid, &t);
1404 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
1406 if (i->exit_code == CLD_EXITED)
1407 printf("status=%i", i->exit_status);
1409 printf("signal=%s", signal_to_string(i->exit_status));
1414 if (i->main_pid > 0 && i->control_pid > 0)
1417 if (i->control_pid > 0) {
1420 printf(" Control: %u", (unsigned) i->control_pid);
1422 get_process_name(i->control_pid, &t);
1433 printf("\t Status: \"%s\"\n", i->status_text);
1435 if (i->default_control_group) {
1438 printf("\t CGroup: %s\n", i->default_control_group);
1440 if ((c = columns()) > 18)
1445 show_cgroup_by_path(i->default_control_group, "\t\t ", c);
1449 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
1451 switch (dbus_message_iter_get_arg_type(iter)) {
1453 case DBUS_TYPE_STRING: {
1456 dbus_message_iter_get_basic(iter, &s);
1459 if (streq(name, "Id"))
1461 else if (streq(name, "LoadState"))
1463 else if (streq(name, "ActiveState"))
1464 i->active_state = s;
1465 else if (streq(name, "SubState"))
1467 else if (streq(name, "Description"))
1469 else if (streq(name, "FragmentPath"))
1470 i->fragment_path = s;
1471 else if (streq(name, "DefaultControlGroup"))
1472 i->default_control_group = s;
1473 else if (streq(name, "StatusText"))
1475 else if (streq(name, "SysFSPath"))
1477 else if (streq(name, "Where"))
1479 else if (streq(name, "What"))
1486 case DBUS_TYPE_BOOLEAN: {
1489 dbus_message_iter_get_basic(iter, &b);
1491 if (streq(name, "Accept"))
1497 case DBUS_TYPE_UINT32: {
1500 dbus_message_iter_get_basic(iter, &u);
1502 if (streq(name, "MainPID")) {
1504 i->main_pid = (pid_t) u;
1507 } else if (streq(name, "ControlPID"))
1508 i->control_pid = (pid_t) u;
1509 else if (streq(name, "ExecMainPID")) {
1511 i->main_pid = (pid_t) u;
1512 } else if (streq(name, "NAccepted"))
1514 else if (streq(name, "NConnections"))
1515 i->n_connections = u;
1520 case DBUS_TYPE_INT32: {
1523 dbus_message_iter_get_basic(iter, &j);
1525 if (streq(name, "ExecMainCode"))
1526 i->exit_code = (int) j;
1527 else if (streq(name, "ExecMainStatus"))
1528 i->exit_status = (int) j;
1533 case DBUS_TYPE_UINT64: {
1536 dbus_message_iter_get_basic(iter, &u);
1538 if (streq(name, "ExecMainStartTimestamp"))
1539 i->start_timestamp = (usec_t) u;
1540 else if (streq(name, "ExecMainExitTimestamp"))
1541 i->exit_timestamp = (usec_t) u;
1546 case DBUS_TYPE_ARRAY: {
1548 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT &&
1549 startswith(name, "Exec")) {
1550 DBusMessageIter sub;
1552 dbus_message_iter_recurse(iter, &sub);
1553 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1554 ExecStatusInfo *info;
1557 if (!(info = new0(ExecStatusInfo, 1)))
1560 if ((r = exec_status_info_deserialize(&sub, info)) < 0) {
1565 LIST_PREPEND(ExecStatusInfo, exec, i->exec, info);
1567 dbus_message_iter_next(&sub);
1578 static int print_property(const char *name, DBusMessageIter *iter) {
1582 /* This is a low-level property printer, see
1583 * print_status_info() for the nicer output */
1585 if (arg_property && !streq(name, arg_property))
1588 switch (dbus_message_iter_get_arg_type(iter)) {
1590 case DBUS_TYPE_STRING: {
1592 dbus_message_iter_get_basic(iter, &s);
1594 if (arg_all || s[0])
1595 printf("%s=%s\n", name, s);
1600 case DBUS_TYPE_BOOLEAN: {
1602 dbus_message_iter_get_basic(iter, &b);
1603 printf("%s=%s\n", name, yes_no(b));
1608 case DBUS_TYPE_UINT64: {
1610 dbus_message_iter_get_basic(iter, &u);
1612 /* Yes, heuristics! But we can change this check
1613 * should it turn out to not be sufficient */
1615 if (strstr(name, "Timestamp")) {
1616 char timestamp[FORMAT_TIMESTAMP_MAX], *t;
1618 if ((t = format_timestamp(timestamp, sizeof(timestamp), u)) || arg_all)
1619 printf("%s=%s\n", name, strempty(t));
1620 } else if (strstr(name, "USec")) {
1621 char timespan[FORMAT_TIMESPAN_MAX];
1623 printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u));
1625 printf("%s=%llu\n", name, (unsigned long long) u);
1630 case DBUS_TYPE_UINT32: {
1632 dbus_message_iter_get_basic(iter, &u);
1634 if (strstr(name, "UMask") || strstr(name, "Mode"))
1635 printf("%s=%04o\n", name, u);
1637 printf("%s=%u\n", name, (unsigned) u);
1642 case DBUS_TYPE_INT32: {
1644 dbus_message_iter_get_basic(iter, &i);
1646 printf("%s=%i\n", name, (int) i);
1650 case DBUS_TYPE_STRUCT: {
1651 DBusMessageIter sub;
1652 dbus_message_iter_recurse(iter, &sub);
1654 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
1657 dbus_message_iter_get_basic(&sub, &u);
1660 printf("%s=%u\n", name, (unsigned) u);
1662 printf("%s=\n", name);
1665 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
1668 dbus_message_iter_get_basic(&sub, &s);
1670 if (arg_all || s[0])
1671 printf("%s=%s\n", name, s);
1679 case DBUS_TYPE_ARRAY:
1681 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
1682 DBusMessageIter sub;
1685 dbus_message_iter_recurse(iter, &sub);
1687 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1688 printf("%s=", name);
1690 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1693 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
1694 dbus_message_iter_get_basic(&sub, &s);
1695 printf("%s%s", space ? " " : "", s);
1698 dbus_message_iter_next(&sub);
1706 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_BYTE) {
1707 DBusMessageIter sub;
1709 dbus_message_iter_recurse(iter, &sub);
1711 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1712 printf("%s=", name);
1714 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1717 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_BYTE);
1718 dbus_message_iter_get_basic(&sub, &u);
1721 dbus_message_iter_next(&sub);
1729 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
1730 DBusMessageIter sub, sub2;
1732 dbus_message_iter_recurse(iter, &sub);
1733 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1734 const char *type, *path;
1736 dbus_message_iter_recurse(&sub, &sub2);
1738 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
1739 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
1740 printf("%s=%s\n", type, path);
1742 dbus_message_iter_next(&sub);
1747 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
1748 DBusMessageIter sub, sub2;
1750 dbus_message_iter_recurse(iter, &sub);
1751 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1753 uint64_t value, next_elapse;
1755 dbus_message_iter_recurse(&sub, &sub2);
1757 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
1758 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
1759 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
1760 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
1762 printf("%s={ value=%s ; next_elapse=%s }\n",
1764 format_timespan(timespan1, sizeof(timespan1), value),
1765 format_timespan(timespan2, sizeof(timespan2), next_elapse));
1768 dbus_message_iter_next(&sub);
1773 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
1774 DBusMessageIter sub;
1776 dbus_message_iter_recurse(iter, &sub);
1777 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1778 ExecStatusInfo info;
1781 if (exec_status_info_deserialize(&sub, &info) >= 0) {
1782 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
1785 t = strv_join(info.argv, " ");
1787 printf("%s={ path=%s ; argv[]=%s ; ignore=%s ; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n",
1791 yes_no(info.ignore),
1792 strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)),
1793 strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)),
1794 (unsigned) info. pid,
1795 sigchld_code_to_string(info.code),
1797 info.code == CLD_EXITED ? "" : "/",
1798 strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status)));
1804 strv_free(info.argv);
1806 dbus_message_iter_next(&sub);
1816 printf("%s=[unprintable]\n", name);
1821 static int show_one(DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
1822 DBusMessage *m = NULL, *reply = NULL;
1823 const char *interface = "";
1826 DBusMessageIter iter, sub, sub2, sub3;
1827 UnitStatusInfo info;
1835 dbus_error_init(&error);
1837 if (!(m = dbus_message_new_method_call(
1838 "org.freedesktop.systemd1",
1840 "org.freedesktop.DBus.Properties",
1842 log_error("Could not allocate message.");
1847 if (!dbus_message_append_args(m,
1848 DBUS_TYPE_STRING, &interface,
1849 DBUS_TYPE_INVALID)) {
1850 log_error("Could not append arguments to message.");
1855 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1856 log_error("Failed to issue method call: %s", error.message);
1861 if (!dbus_message_iter_init(reply, &iter) ||
1862 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
1863 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
1864 log_error("Failed to parse reply.");
1869 dbus_message_iter_recurse(&iter, &sub);
1876 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1879 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
1880 log_error("Failed to parse reply.");
1885 dbus_message_iter_recurse(&sub, &sub2);
1887 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
1888 log_error("Failed to parse reply.");
1893 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
1894 log_error("Failed to parse reply.");
1899 dbus_message_iter_recurse(&sub2, &sub3);
1901 if (show_properties)
1902 r = print_property(name, &sub3);
1904 r = status_property(name, &sub3, &info);
1907 log_error("Failed to parse reply.");
1912 dbus_message_iter_next(&sub);
1915 if (!show_properties)
1916 print_status_info(&info);
1918 while ((p = info.exec)) {
1919 LIST_REMOVE(ExecStatusInfo, exec, info.exec, p);
1920 exec_status_info_free(p);
1927 dbus_message_unref(m);
1930 dbus_message_unref(reply);
1932 dbus_error_free(&error);
1937 static int show(DBusConnection *bus, char **args, unsigned n) {
1938 DBusMessage *m = NULL, *reply = NULL;
1942 bool show_properties, new_line = false;
1947 dbus_error_init(&error);
1949 show_properties = !streq(args[0], "status");
1951 if (show_properties && n <= 1) {
1952 /* If not argument is specified inspect the manager
1955 r = show_one(bus, "/org/freedesktop/systemd1", show_properties, &new_line);
1959 for (i = 1; i < n; i++) {
1960 const char *path = NULL;
1963 if (!show_properties || safe_atou32(args[i], &id) < 0) {
1965 if (!(m = dbus_message_new_method_call(
1966 "org.freedesktop.systemd1",
1967 "/org/freedesktop/systemd1",
1968 "org.freedesktop.systemd1.Manager",
1970 log_error("Could not allocate message.");
1975 if (!dbus_message_append_args(m,
1976 DBUS_TYPE_STRING, &args[i],
1977 DBUS_TYPE_INVALID)) {
1978 log_error("Could not append arguments to message.");
1983 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1985 if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
1986 log_error("Failed to issue method call: %s", error.message);
1991 dbus_error_free(&error);
1993 dbus_message_unref(m);
1994 if (!(m = dbus_message_new_method_call(
1995 "org.freedesktop.systemd1",
1996 "/org/freedesktop/systemd1",
1997 "org.freedesktop.systemd1.Manager",
1999 log_error("Could not allocate message.");
2004 if (!dbus_message_append_args(m,
2005 DBUS_TYPE_STRING, &args[i],
2006 DBUS_TYPE_INVALID)) {
2007 log_error("Could not append arguments to message.");
2012 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2013 log_error("Failed to issue method call: %s", error.message);
2021 if (!(m = dbus_message_new_method_call(
2022 "org.freedesktop.systemd1",
2023 "/org/freedesktop/systemd1",
2024 "org.freedesktop.systemd1.Manager",
2026 log_error("Could not allocate message.");
2031 if (!dbus_message_append_args(m,
2032 DBUS_TYPE_UINT32, &id,
2033 DBUS_TYPE_INVALID)) {
2034 log_error("Could not append arguments to message.");
2039 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2040 log_error("Failed to issue method call: %s", error.message);
2046 if (!dbus_message_get_args(reply, &error,
2047 DBUS_TYPE_OBJECT_PATH, &path,
2048 DBUS_TYPE_INVALID)) {
2049 log_error("Failed to parse reply: %s", error.message);
2054 if ((r = show_one(bus, path, show_properties, &new_line)) < 0)
2057 dbus_message_unref(m);
2058 dbus_message_unref(reply);
2066 dbus_message_unref(m);
2069 dbus_message_unref(reply);
2071 dbus_error_free(&error);
2076 static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
2078 DBusMessage *m = NULL, *reply = NULL;
2083 dbus_error_init(&error);
2085 log_debug("Got D-Bus request: %s.%s() on %s",
2086 dbus_message_get_interface(message),
2087 dbus_message_get_member(message),
2088 dbus_message_get_path(message));
2090 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
2091 log_error("Warning! D-Bus connection terminated.");
2092 dbus_connection_close(connection);
2094 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") ||
2095 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
2096 const char *id, *path;
2098 if (!dbus_message_get_args(message, &error,
2099 DBUS_TYPE_STRING, &id,
2100 DBUS_TYPE_OBJECT_PATH, &path,
2102 log_error("Failed to parse message: %s", error.message);
2103 else if (streq(dbus_message_get_member(message), "UnitNew"))
2104 printf("Unit %s added.\n", id);
2106 printf("Unit %s removed.\n", id);
2108 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew") ||
2109 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
2113 if (!dbus_message_get_args(message, &error,
2114 DBUS_TYPE_UINT32, &id,
2115 DBUS_TYPE_OBJECT_PATH, &path,
2117 log_error("Failed to parse message: %s", error.message);
2118 else if (streq(dbus_message_get_member(message), "JobNew"))
2119 printf("Job %u added.\n", id);
2121 printf("Job %u removed.\n", id);
2124 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Unit", "Changed") ||
2125 dbus_message_is_signal(message, "org.freedesktop.systemd1.Job", "Changed")) {
2127 const char *path, *interface, *property = "Id";
2128 DBusMessageIter iter, sub;
2130 path = dbus_message_get_path(message);
2131 interface = dbus_message_get_interface(message);
2133 if (!(m = dbus_message_new_method_call(
2134 "org.freedesktop.systemd1",
2136 "org.freedesktop.DBus.Properties",
2138 log_error("Could not allocate message.");
2142 if (!dbus_message_append_args(m,
2143 DBUS_TYPE_STRING, &interface,
2144 DBUS_TYPE_STRING, &property,
2145 DBUS_TYPE_INVALID)) {
2146 log_error("Could not append arguments to message.");
2150 if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
2151 log_error("Failed to issue method call: %s", error.message);
2155 if (!dbus_message_iter_init(reply, &iter) ||
2156 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2157 log_error("Failed to parse reply.");
2161 dbus_message_iter_recurse(&iter, &sub);
2163 if (streq(interface, "org.freedesktop.systemd1.Unit")) {
2166 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2167 log_error("Failed to parse reply.");
2171 dbus_message_iter_get_basic(&sub, &id);
2172 printf("Unit %s changed.\n", id);
2176 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32) {
2177 log_error("Failed to parse reply.");
2181 dbus_message_iter_get_basic(&sub, &id);
2182 printf("Job %u changed.\n", id);
2188 dbus_message_unref(m);
2191 dbus_message_unref(reply);
2193 dbus_error_free(&error);
2194 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2198 dbus_message_unref(m);
2201 dbus_message_unref(reply);
2203 dbus_error_free(&error);
2204 return DBUS_HANDLER_RESULT_NEED_MEMORY;
2207 static int monitor(DBusConnection *bus, char **args, unsigned n) {
2208 DBusMessage *m = NULL, *reply = NULL;
2212 dbus_error_init(&error);
2215 dbus_bus_add_match(bus,
2217 "sender='org.freedesktop.systemd1',"
2218 "interface='org.freedesktop.systemd1.Manager',"
2219 "path='/org/freedesktop/systemd1'",
2222 if (dbus_error_is_set(&error)) {
2223 log_error("Failed to add match: %s", error.message);
2228 dbus_bus_add_match(bus,
2230 "sender='org.freedesktop.systemd1',"
2231 "interface='org.freedesktop.systemd1.Unit',"
2235 if (dbus_error_is_set(&error)) {
2236 log_error("Failed to add match: %s", error.message);
2241 dbus_bus_add_match(bus,
2243 "sender='org.freedesktop.systemd1',"
2244 "interface='org.freedesktop.systemd1.Job',"
2248 if (dbus_error_is_set(&error)) {
2249 log_error("Failed to add match: %s", error.message);
2255 if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
2256 log_error("Failed to add filter.");
2261 if (!(m = dbus_message_new_method_call(
2262 "org.freedesktop.systemd1",
2263 "/org/freedesktop/systemd1",
2264 "org.freedesktop.systemd1.Manager",
2266 log_error("Could not allocate message.");
2271 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2272 log_error("Failed to issue method call: %s", error.message);
2277 while (dbus_connection_read_write_dispatch(bus, -1))
2284 /* This is slightly dirty, since we don't undo the filter or the matches. */
2287 dbus_message_unref(m);
2290 dbus_message_unref(reply);
2292 dbus_error_free(&error);
2297 static int dump(DBusConnection *bus, char **args, unsigned n) {
2298 DBusMessage *m = NULL, *reply = NULL;
2303 dbus_error_init(&error);
2305 if (!(m = dbus_message_new_method_call(
2306 "org.freedesktop.systemd1",
2307 "/org/freedesktop/systemd1",
2308 "org.freedesktop.systemd1.Manager",
2310 log_error("Could not allocate message.");
2314 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2315 log_error("Failed to issue method call: %s", error.message);
2320 if (!dbus_message_get_args(reply, &error,
2321 DBUS_TYPE_STRING, &text,
2322 DBUS_TYPE_INVALID)) {
2323 log_error("Failed to parse reply: %s", error.message);
2328 fputs(text, stdout);
2334 dbus_message_unref(m);
2337 dbus_message_unref(reply);
2339 dbus_error_free(&error);
2344 static int snapshot(DBusConnection *bus, char **args, unsigned n) {
2345 DBusMessage *m = NULL, *reply = NULL;
2348 const char *name = "", *path, *id;
2349 dbus_bool_t cleanup = FALSE;
2350 DBusMessageIter iter, sub;
2352 *interface = "org.freedesktop.systemd1.Unit",
2355 dbus_error_init(&error);
2357 if (!(m = dbus_message_new_method_call(
2358 "org.freedesktop.systemd1",
2359 "/org/freedesktop/systemd1",
2360 "org.freedesktop.systemd1.Manager",
2361 "CreateSnapshot"))) {
2362 log_error("Could not allocate message.");
2369 if (!dbus_message_append_args(m,
2370 DBUS_TYPE_STRING, &name,
2371 DBUS_TYPE_BOOLEAN, &cleanup,
2372 DBUS_TYPE_INVALID)) {
2373 log_error("Could not append arguments to message.");
2378 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2379 log_error("Failed to issue method call: %s", error.message);
2384 if (!dbus_message_get_args(reply, &error,
2385 DBUS_TYPE_OBJECT_PATH, &path,
2386 DBUS_TYPE_INVALID)) {
2387 log_error("Failed to parse reply: %s", error.message);
2392 dbus_message_unref(m);
2393 if (!(m = dbus_message_new_method_call(
2394 "org.freedesktop.systemd1",
2396 "org.freedesktop.DBus.Properties",
2398 log_error("Could not allocate message.");
2402 if (!dbus_message_append_args(m,
2403 DBUS_TYPE_STRING, &interface,
2404 DBUS_TYPE_STRING, &property,
2405 DBUS_TYPE_INVALID)) {
2406 log_error("Could not append arguments to message.");
2411 dbus_message_unref(reply);
2412 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2413 log_error("Failed to issue method call: %s", error.message);
2418 if (!dbus_message_iter_init(reply, &iter) ||
2419 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2420 log_error("Failed to parse reply.");
2425 dbus_message_iter_recurse(&iter, &sub);
2427 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2428 log_error("Failed to parse reply.");
2433 dbus_message_iter_get_basic(&sub, &id);
2441 dbus_message_unref(m);
2444 dbus_message_unref(reply);
2446 dbus_error_free(&error);
2451 static int delete_snapshot(DBusConnection *bus, char **args, unsigned n) {
2452 DBusMessage *m = NULL, *reply = NULL;
2460 dbus_error_init(&error);
2462 for (i = 1; i < n; i++) {
2463 const char *path = NULL;
2465 if (!(m = dbus_message_new_method_call(
2466 "org.freedesktop.systemd1",
2467 "/org/freedesktop/systemd1",
2468 "org.freedesktop.systemd1.Manager",
2470 log_error("Could not allocate message.");
2475 if (!dbus_message_append_args(m,
2476 DBUS_TYPE_STRING, &args[i],
2477 DBUS_TYPE_INVALID)) {
2478 log_error("Could not append arguments to message.");
2483 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2484 log_error("Failed to issue method call: %s", error.message);
2489 if (!dbus_message_get_args(reply, &error,
2490 DBUS_TYPE_OBJECT_PATH, &path,
2491 DBUS_TYPE_INVALID)) {
2492 log_error("Failed to parse reply: %s", error.message);
2497 dbus_message_unref(m);
2498 if (!(m = dbus_message_new_method_call(
2499 "org.freedesktop.systemd1",
2501 "org.freedesktop.systemd1.Snapshot",
2503 log_error("Could not allocate message.");
2508 dbus_message_unref(reply);
2509 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2510 log_error("Failed to issue method call: %s", error.message);
2515 dbus_message_unref(m);
2516 dbus_message_unref(reply);
2524 dbus_message_unref(m);
2527 dbus_message_unref(reply);
2529 dbus_error_free(&error);
2534 static int clear_jobs(DBusConnection *bus, char **args, unsigned n) {
2535 DBusMessage *m = NULL, *reply = NULL;
2540 dbus_error_init(&error);
2542 if (arg_action == ACTION_RELOAD)
2544 else if (arg_action == ACTION_REEXEC)
2545 method = "Reexecute";
2547 assert(arg_action == ACTION_SYSTEMCTL);
2550 streq(args[0], "clear-jobs") ? "ClearJobs" :
2551 streq(args[0], "daemon-reload") ? "Reload" :
2552 streq(args[0], "daemon-reexec") ? "Reexecute" :
2556 if (!(m = dbus_message_new_method_call(
2557 "org.freedesktop.systemd1",
2558 "/org/freedesktop/systemd1",
2559 "org.freedesktop.systemd1.Manager",
2561 log_error("Could not allocate message.");
2565 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2567 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
2568 /* There's always a fallback possible for
2569 * legacy actions. */
2574 log_error("Failed to issue method call: %s", error.message);
2583 dbus_message_unref(m);
2586 dbus_message_unref(reply);
2588 dbus_error_free(&error);
2593 static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
2594 DBusMessage *m = NULL, *reply = NULL;
2596 DBusMessageIter iter, sub, sub2;
2599 *interface = "org.freedesktop.systemd1.Manager",
2600 *property = "Environment";
2602 dbus_error_init(&error);
2604 if (!(m = dbus_message_new_method_call(
2605 "org.freedesktop.systemd1",
2606 "/org/freedesktop/systemd1",
2607 "org.freedesktop.DBus.Properties",
2609 log_error("Could not allocate message.");
2613 if (!dbus_message_append_args(m,
2614 DBUS_TYPE_STRING, &interface,
2615 DBUS_TYPE_STRING, &property,
2616 DBUS_TYPE_INVALID)) {
2617 log_error("Could not append arguments to message.");
2622 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2623 log_error("Failed to issue method call: %s", error.message);
2628 if (!dbus_message_iter_init(reply, &iter) ||
2629 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2630 log_error("Failed to parse reply.");
2635 dbus_message_iter_recurse(&iter, &sub);
2637 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
2638 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
2639 log_error("Failed to parse reply.");
2644 dbus_message_iter_recurse(&sub, &sub2);
2646 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
2649 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
2650 log_error("Failed to parse reply.");
2655 dbus_message_iter_get_basic(&sub2, &text);
2656 printf("%s\n", text);
2658 dbus_message_iter_next(&sub2);
2665 dbus_message_unref(m);
2668 dbus_message_unref(reply);
2670 dbus_error_free(&error);
2675 static int set_environment(DBusConnection *bus, char **args, unsigned n) {
2676 DBusMessage *m = NULL, *reply = NULL;
2680 DBusMessageIter iter, sub;
2683 dbus_error_init(&error);
2685 method = streq(args[0], "set-environment")
2687 : "UnsetEnvironment";
2689 if (!(m = dbus_message_new_method_call(
2690 "org.freedesktop.systemd1",
2691 "/org/freedesktop/systemd1",
2692 "org.freedesktop.systemd1.Manager",
2695 log_error("Could not allocate message.");
2699 dbus_message_iter_init_append(m, &iter);
2701 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
2702 log_error("Could not append arguments to message.");
2707 for (i = 1; i < n; i++)
2708 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) {
2709 log_error("Could not append arguments to message.");
2714 if (!dbus_message_iter_close_container(&iter, &sub)) {
2715 log_error("Could not append arguments to message.");
2720 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2721 log_error("Failed to issue method call: %s", error.message);
2730 dbus_message_unref(m);
2733 dbus_message_unref(reply);
2735 dbus_error_free(&error);
2740 static int systemctl_help(void) {
2742 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
2743 "Send control commands to the systemd manager.\n\n"
2744 " -h --help Show this help\n"
2745 " -t --type=TYPE List only units of a particular type\n"
2746 " -p --property=NAME Show only properties by this name\n"
2747 " -a --all Show all units/properties, including dead/empty ones\n"
2748 " --fail When installing a new job, fail if conflicting jobs are\n"
2750 " --system Connect to system bus\n"
2751 " --session Connect to session bus\n"
2752 " --order When generating graph for dot, show only order\n"
2753 " --require When generating graph for dot, show only requirement\n"
2754 " -q --quiet Suppress output\n"
2755 " --no-block Do not wait until operation finished\n"
2756 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
2758 " list-units List units\n"
2759 " start [NAME...] Start one or more units\n"
2760 " stop [NAME...] Stop one or more units\n"
2761 " reload [NAME...] Reload one or more units\n"
2762 " restart [NAME...] Start or restart one or more units\n"
2763 " try-restart [NAME...] Restart one or more units if active\n"
2764 " reload-or-restart [NAME...] Reload one or more units is possible,\n"
2765 " otherwise start or restart\n"
2766 " reload-or-try-restart [NAME...] Reload one or more units is possible,\n"
2767 " otherwise restart if active\n"
2768 " isolate [NAME] Start one unit and stop all others\n"
2769 " check [NAME...] Check whether units are active\n"
2770 " status [NAME...] Show status of one or more units\n"
2771 " show [NAME...|JOB...] Show properties of one or more\n"
2772 " units/jobs/manager\n"
2773 " load [NAME...] Load one or more units\n"
2774 " list-jobs List jobs\n"
2775 " cancel [JOB...] Cancel one or more jobs\n"
2776 " clear-jobs Cancel all jobs\n"
2777 " monitor Monitor unit/job changes\n"
2778 " dump Dump server status\n"
2779 " dot Dump dependency graph for dot(1)\n"
2780 " snapshot [NAME] Create a snapshot\n"
2781 " delete [NAME...] Remove one or more snapshots\n"
2782 " daemon-reload Reload systemd manager configuration\n"
2783 " daemon-reexec Reexecute systemd manager\n"
2784 " daemon-exit Ask the systemd manager to quit\n"
2785 " show-environment Dump environment\n"
2786 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
2787 " unset-environment [NAME...] Unset one or more environment variables\n"
2788 " halt Shut down and halt the system\n"
2789 " poweroff Shut down and power-off the system\n"
2790 " reboot Shut down and reboot the system\n"
2791 " rescue Enter system rescue mode\n"
2792 " emergency Enter system emergency mode\n"
2793 " default Enter system default mode\n",
2794 program_invocation_short_name);
2799 static int halt_help(void) {
2801 printf("%s [OPTIONS...]\n\n"
2802 "%s the system.\n\n"
2803 " --help Show this help\n"
2804 " --halt Halt the machine\n"
2805 " -p --poweroff Switch off the machine\n"
2806 " --reboot Reboot the machine\n"
2807 " -f --force Force immediate halt/power-off/reboot\n"
2808 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
2809 " -d --no-wtmp Don't write wtmp record\n"
2810 " -n --no-sync Don't sync before halt/power-off/reboot\n"
2811 " --no-wall Don't send wall message before halt/power-off/reboot\n",
2812 program_invocation_short_name,
2813 arg_action == ACTION_REBOOT ? "Reboot" :
2814 arg_action == ACTION_POWEROFF ? "Power off" :
2820 static int shutdown_help(void) {
2822 printf("%s [OPTIONS...] [now] [WALL...]\n\n"
2823 "Shut down the system.\n\n"
2824 " --help Show this help\n"
2825 " -H --halt Halt the machine\n"
2826 " -P --poweroff Power-off the machine\n"
2827 " -r --reboot Reboot the machine\n"
2828 " -h Equivalent to --poweroff, overriden by --halt\n"
2829 " -k Don't halt/power-off/reboot, just send warnings\n"
2830 " --no-wall Don't send wall message before halt/power-off/reboot\n",
2831 program_invocation_short_name);
2836 static int telinit_help(void) {
2838 printf("%s [OPTIONS...] {COMMAND}\n\n"
2839 "Send control commands to the init daemon.\n\n"
2840 " --help Show this help\n"
2841 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
2843 " 0 Power-off the machine\n"
2844 " 6 Reboot the machine\n"
2845 " 2, 3, 4, 5 Start runlevelX.target unit\n"
2846 " 1, s, S Enter rescue mode\n"
2847 " q, Q Reload init daemon configuration\n"
2848 " u, U Reexecute init daemon\n",
2849 program_invocation_short_name);
2854 static int runlevel_help(void) {
2856 printf("%s [OPTIONS...]\n\n"
2857 "Prints the previous and current runlevel of the init system.\n\n"
2858 " --help Show this help\n",
2859 program_invocation_short_name);
2864 static int systemctl_parse_argv(int argc, char *argv[]) {
2876 static const struct option options[] = {
2877 { "help", no_argument, NULL, 'h' },
2878 { "type", required_argument, NULL, 't' },
2879 { "property", required_argument, NULL, 'p' },
2880 { "all", no_argument, NULL, 'a' },
2881 { "fail", no_argument, NULL, ARG_FAIL },
2882 { "session", no_argument, NULL, ARG_SESSION },
2883 { "system", no_argument, NULL, ARG_SYSTEM },
2884 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
2885 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2886 { "quiet", no_argument, NULL, 'q' },
2887 { "order", no_argument, NULL, ARG_ORDER },
2888 { "require", no_argument, NULL, ARG_REQUIRE },
2889 { NULL, 0, NULL, 0 }
2897 while ((c = getopt_long(argc, argv, "ht:p:aq", options, NULL)) >= 0) {
2910 arg_property = optarg;
2912 /* If the user asked for a particular
2913 * property, show it to him, even if it is
2931 arg_session = false;
2935 arg_no_block = true;
2943 arg_dot = DOT_ORDER;
2947 arg_dot = DOT_REQUIRE;
2958 log_error("Unknown option code %c", c);
2966 static int halt_parse_argv(int argc, char *argv[]) {
2975 static const struct option options[] = {
2976 { "help", no_argument, NULL, ARG_HELP },
2977 { "halt", no_argument, NULL, ARG_HALT },
2978 { "poweroff", no_argument, NULL, 'p' },
2979 { "reboot", no_argument, NULL, ARG_REBOOT },
2980 { "force", no_argument, NULL, 'f' },
2981 { "wtmp-only", no_argument, NULL, 'w' },
2982 { "no-wtmp", no_argument, NULL, 'd' },
2983 { "no-sync", no_argument, NULL, 'n' },
2984 { "no-wall", no_argument, NULL, ARG_NO_WALL },
2985 { NULL, 0, NULL, 0 }
2993 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
2994 if (runlevel == '0' || runlevel == '6')
2995 arg_immediate = true;
2997 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
3005 arg_action = ACTION_HALT;
3009 arg_action = ACTION_POWEROFF;
3013 arg_action = ACTION_REBOOT;
3017 arg_immediate = true;
3038 /* Compatibility nops */
3045 log_error("Unknown option code %c", c);
3050 if (optind < argc) {
3051 log_error("Too many arguments.");
3058 static int shutdown_parse_argv(int argc, char *argv[]) {
3065 static const struct option options[] = {
3066 { "help", no_argument, NULL, ARG_HELP },
3067 { "halt", no_argument, NULL, 'H' },
3068 { "poweroff", no_argument, NULL, 'P' },
3069 { "reboot", no_argument, NULL, 'r' },
3070 { "no-wall", no_argument, NULL, ARG_NO_WALL },
3071 { NULL, 0, NULL, 0 }
3079 while ((c = getopt_long(argc, argv, "HPrhkt:a", options, NULL)) >= 0) {
3087 arg_action = ACTION_HALT;
3091 arg_action = ACTION_POWEROFF;
3095 arg_action = ACTION_REBOOT;
3099 if (arg_action != ACTION_HALT)
3100 arg_action = ACTION_POWEROFF;
3113 /* Compatibility nops */
3120 log_error("Unknown option code %c", c);
3125 if (argc > optind && !streq(argv[optind], "now"))
3126 log_warning("First argument '%s' isn't 'now'. Ignoring.", argv[optind]);
3128 /* We ignore the time argument */
3129 if (argc > optind + 1)
3130 arg_wall = argv + optind + 1;
3137 static int telinit_parse_argv(int argc, char *argv[]) {
3144 static const struct option options[] = {
3145 { "help", no_argument, NULL, ARG_HELP },
3146 { "no-wall", no_argument, NULL, ARG_NO_WALL },
3147 { NULL, 0, NULL, 0 }
3150 static const struct {
3154 { '0', ACTION_POWEROFF },
3155 { '6', ACTION_REBOOT },
3156 { '1', ACTION_RESCUE },
3157 { '2', ACTION_RUNLEVEL2 },
3158 { '3', ACTION_RUNLEVEL3 },
3159 { '4', ACTION_RUNLEVEL4 },
3160 { '5', ACTION_RUNLEVEL5 },
3161 { 's', ACTION_RESCUE },
3162 { 'S', ACTION_RESCUE },
3163 { 'q', ACTION_RELOAD },
3164 { 'Q', ACTION_RELOAD },
3165 { 'u', ACTION_REEXEC },
3166 { 'U', ACTION_REEXEC }
3175 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
3190 log_error("Unknown option code %c", c);
3195 if (optind >= argc) {
3200 if (optind + 1 < argc) {
3201 log_error("Too many arguments.");
3205 if (strlen(argv[optind]) != 1) {
3206 log_error("Expected single character argument.");
3210 for (i = 0; i < ELEMENTSOF(table); i++)
3211 if (table[i].from == argv[optind][0])
3214 if (i >= ELEMENTSOF(table)) {
3215 log_error("Unknown command %s.", argv[optind]);
3219 arg_action = table[i].to;
3226 static int runlevel_parse_argv(int argc, char *argv[]) {
3232 static const struct option options[] = {
3233 { "help", no_argument, NULL, ARG_HELP },
3234 { NULL, 0, NULL, 0 }
3242 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
3253 log_error("Unknown option code %c", c);
3258 if (optind < argc) {
3259 log_error("Too many arguments.");
3266 static int parse_argv(int argc, char *argv[]) {
3270 if (program_invocation_short_name) {
3272 if (strstr(program_invocation_short_name, "halt")) {
3273 arg_action = ACTION_HALT;
3274 return halt_parse_argv(argc, argv);
3275 } else if (strstr(program_invocation_short_name, "poweroff")) {
3276 arg_action = ACTION_POWEROFF;
3277 return halt_parse_argv(argc, argv);
3278 } else if (strstr(program_invocation_short_name, "reboot")) {
3279 arg_action = ACTION_REBOOT;
3280 return halt_parse_argv(argc, argv);
3281 } else if (strstr(program_invocation_short_name, "shutdown")) {
3282 arg_action = ACTION_POWEROFF;
3283 return shutdown_parse_argv(argc, argv);
3284 } else if (strstr(program_invocation_short_name, "init")) {
3285 arg_action = ACTION_INVALID;
3286 return telinit_parse_argv(argc, argv);
3287 } else if (strstr(program_invocation_short_name, "runlevel")) {
3288 arg_action = ACTION_RUNLEVEL;
3289 return runlevel_parse_argv(argc, argv);
3293 arg_action = ACTION_SYSTEMCTL;
3294 return systemctl_parse_argv(argc, argv);
3297 static int action_to_runlevel(void) {
3299 static const char table[_ACTION_MAX] = {
3300 [ACTION_HALT] = '0',
3301 [ACTION_POWEROFF] = '0',
3302 [ACTION_REBOOT] = '6',
3303 [ACTION_RUNLEVEL2] = '2',
3304 [ACTION_RUNLEVEL3] = '3',
3305 [ACTION_RUNLEVEL4] = '4',
3306 [ACTION_RUNLEVEL5] = '5',
3307 [ACTION_RESCUE] = '1'
3310 assert(arg_action < _ACTION_MAX);
3312 return table[arg_action];
3315 static int talk_upstart(void) {
3316 DBusMessage *m = NULL, *reply = NULL;
3318 int previous, rl, r;
3320 env1_buf[] = "RUNLEVEL=X",
3321 env2_buf[] = "PREVLEVEL=X";
3322 char *env1 = env1_buf, *env2 = env2_buf;
3323 const char *emit = "runlevel";
3324 dbus_bool_t b_false = FALSE;
3325 DBusMessageIter iter, sub;
3326 DBusConnection *bus;
3328 dbus_error_init(&error);
3330 if (!(rl = action_to_runlevel()))
3333 if (utmp_get_runlevel(&previous, NULL) < 0)
3336 if (!(bus = dbus_connection_open_private("unix:abstract=/com/ubuntu/upstart", &error))) {
3337 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
3342 log_error("Failed to connect to Upstart bus: %s", error.message);
3347 if ((r = bus_check_peercred(bus)) < 0) {
3348 log_error("Failed to verify owner of bus.");
3352 if (!(m = dbus_message_new_method_call(
3353 "com.ubuntu.Upstart",
3354 "/com/ubuntu/Upstart",
3355 "com.ubuntu.Upstart0_6",
3358 log_error("Could not allocate message.");
3363 dbus_message_iter_init_append(m, &iter);
3365 env1_buf[sizeof(env1_buf)-2] = rl;
3366 env2_buf[sizeof(env2_buf)-2] = previous;
3368 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
3369 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
3370 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
3371 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
3372 !dbus_message_iter_close_container(&iter, &sub) ||
3373 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
3374 log_error("Could not append arguments to message.");
3379 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3381 if (error_is_no_service(&error)) {
3386 log_error("Failed to issue method call: %s", error.message);
3395 dbus_message_unref(m);
3398 dbus_message_unref(reply);
3401 dbus_connection_close(bus);
3402 dbus_connection_unref(bus);
3405 dbus_error_free(&error);
3410 static int talk_initctl(void) {
3411 struct init_request request;
3415 if (!(rl = action_to_runlevel()))
3419 request.magic = INIT_MAGIC;
3420 request.sleeptime = 0;
3421 request.cmd = INIT_CMD_RUNLVL;
3422 request.runlevel = rl;
3424 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
3426 if (errno == ENOENT)
3429 log_error("Failed to open "INIT_FIFO": %m");
3434 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
3435 close_nointr_nofail(fd);
3438 log_error("Failed to write to "INIT_FIFO": %m");
3439 return errno ? -errno : -EIO;
3445 static int systemctl_main(DBusConnection *bus, int argc, char *argv[]) {
3447 static const struct {
3455 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
3457 { "list-units", LESS, 1, list_units },
3458 { "list-jobs", EQUAL, 1, list_jobs },
3459 { "clear-jobs", EQUAL, 1, clear_jobs },
3460 { "load", MORE, 2, load_unit },
3461 { "cancel", MORE, 2, cancel_job },
3462 { "start", MORE, 2, start_unit },
3463 { "stop", MORE, 2, start_unit },
3464 { "reload", MORE, 2, start_unit },
3465 { "restart", MORE, 2, start_unit },
3466 { "try-restart", MORE, 2, start_unit },
3467 { "reload-or-restart", MORE, 2, start_unit },
3468 { "reload-or-try-restart", MORE, 2, start_unit },
3469 { "isolate", EQUAL, 2, start_unit },
3470 { "check", MORE, 2, check_unit },
3471 { "show", MORE, 1, show },
3472 { "status", MORE, 2, show },
3473 { "monitor", EQUAL, 1, monitor },
3474 { "dump", EQUAL, 1, dump },
3475 { "dot", EQUAL, 1, dot },
3476 { "snapshot", LESS, 2, snapshot },
3477 { "delete", MORE, 2, delete_snapshot },
3478 { "daemon-reload", EQUAL, 1, clear_jobs },
3479 { "daemon-reexec", EQUAL, 1, clear_jobs },
3480 { "daemon-exit", EQUAL, 1, clear_jobs },
3481 { "show-environment", EQUAL, 1, show_enviroment },
3482 { "set-environment", MORE, 2, set_environment },
3483 { "unset-environment", MORE, 2, set_environment },
3484 { "halt", EQUAL, 1, start_special },
3485 { "poweroff", EQUAL, 1, start_special },
3486 { "reboot", EQUAL, 1, start_special },
3487 { "default", EQUAL, 1, start_special },
3488 { "rescue", EQUAL, 1, start_special },
3489 { "emergency", EQUAL, 1, start_special },
3499 left = argc - optind;
3502 /* Special rule: no arguments means "list-units" */
3505 if (streq(argv[optind], "help")) {
3510 for (i = 0; i < ELEMENTSOF(verbs); i++)
3511 if (streq(argv[optind], verbs[i].verb))
3514 if (i >= ELEMENTSOF(verbs)) {
3515 log_error("Unknown operation %s", argv[optind]);
3520 switch (verbs[i].argc_cmp) {
3523 if (left != verbs[i].argc) {
3524 log_error("Invalid number of arguments.");
3531 if (left < verbs[i].argc) {
3532 log_error("Too few arguments.");
3539 if (left > verbs[i].argc) {
3540 log_error("Too many arguments.");
3547 assert_not_reached("Unknown comparison operator.");
3550 return verbs[i].dispatch(bus, argv + optind, left);
3553 static int reload_with_fallback(DBusConnection *bus) {
3557 /* First, try systemd via D-Bus. */
3558 if ((r = clear_jobs(bus, NULL, 0)) > 0)
3562 /* Nothing else worked, so let's try signals */
3563 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
3565 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
3566 log_error("kill() failed: %m");
3573 static int start_with_fallback(DBusConnection *bus) {
3578 /* First, try systemd via D-Bus. */
3579 if ((r = start_unit(bus, NULL, 0)) > 0)
3582 /* Hmm, talking to systemd via D-Bus didn't work. Then
3583 * let's try to talk to Upstart via D-Bus. */
3584 if ((r = talk_upstart()) > 0)
3588 /* Nothing else worked, so let's try
3590 if ((r = talk_initctl()) != 0)
3593 log_error("Failed to talk to init daemon.");
3597 warn_wall(arg_action);
3601 static int halt_main(DBusConnection *bus) {
3604 if (geteuid() != 0) {
3605 log_error("Must to be root.");
3609 if (!arg_dry && !arg_immediate)
3610 return start_with_fallback(bus);
3613 if ((r = utmp_put_shutdown(0)) < 0)
3614 log_warning("Failed to write utmp record: %s", strerror(-r));
3622 /* Make sure C-A-D is handled by the kernel from this
3624 reboot(RB_ENABLE_CAD);
3626 switch (arg_action) {
3629 log_info("Halting");
3630 reboot(RB_HALT_SYSTEM);
3633 case ACTION_POWEROFF:
3634 log_info("Powering off");
3635 reboot(RB_POWER_OFF);
3639 log_info("Rebooting");
3640 reboot(RB_AUTOBOOT);
3644 assert_not_reached("Unknown halt action.");
3647 /* We should never reach this. */
3651 static int runlevel_main(void) {
3652 int r, runlevel, previous;
3654 if ((r = utmp_get_runlevel(&runlevel, &previous)) < 0) {
3660 previous <= 0 ? 'N' : previous,
3661 runlevel <= 0 ? 'N' : runlevel);
3666 int main(int argc, char*argv[]) {
3668 DBusConnection *bus = NULL;
3671 dbus_error_init(&error);
3673 log_parse_environment();
3675 if ((r = parse_argv(argc, argv)) < 0)
3682 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
3683 * let's shortcut this */
3684 if (arg_action == ACTION_RUNLEVEL) {
3685 retval = runlevel_main() < 0;
3689 bus_connect(arg_session ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &bus, &private_bus, &error);
3691 switch (arg_action) {
3693 case ACTION_SYSTEMCTL: {
3696 log_error("Failed to get D-Bus connection: %s", error.message);
3700 retval = systemctl_main(bus, argc, argv) < 0;
3705 case ACTION_POWEROFF:
3707 retval = halt_main(bus) < 0;
3710 case ACTION_RUNLEVEL2:
3711 case ACTION_RUNLEVEL3:
3712 case ACTION_RUNLEVEL4:
3713 case ACTION_RUNLEVEL5:
3715 case ACTION_EMERGENCY:
3716 case ACTION_DEFAULT:
3717 retval = start_with_fallback(bus) < 0;
3722 retval = reload_with_fallback(bus) < 0;
3725 case ACTION_INVALID:
3726 case ACTION_RUNLEVEL:
3728 assert_not_reached("Unknown action");
3734 dbus_connection_close(bus);
3735 dbus_connection_unref(bus);
3738 dbus_error_free(&error);