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>
35 #include <dbus/dbus.h>
41 #include "utmp-wtmp.h"
45 #include "dbus-common.h"
46 #include "cgroup-show.h"
47 #include "cgroup-util.h"
49 #include "path-lookup.h"
50 #include "conf-parser.h"
51 #include "sd-daemon.h"
53 static const char *arg_type = NULL;
54 static char **arg_property = NULL;
55 static bool arg_all = false;
56 static bool arg_fail = false;
57 static bool arg_session = false;
58 static bool arg_global = false;
59 static bool arg_immediate = false;
60 static bool arg_no_block = false;
61 static bool arg_no_wtmp = false;
62 static bool arg_no_sync = false;
63 static bool arg_no_wall = false;
64 static bool arg_no_reload = false;
65 static bool arg_dry = false;
66 static bool arg_quiet = false;
67 static bool arg_full = false;
68 static bool arg_force = false;
69 static bool arg_defaults = false;
70 static char **arg_wall = NULL;
88 } arg_action = ACTION_SYSTEMCTL;
95 static bool private_bus = false;
97 static int daemon_reload(DBusConnection *bus, char **args, unsigned n);
99 static const char *ansi_highlight(bool b) {
102 if (_unlikely_(t < 0))
103 t = isatty(STDOUT_FILENO) > 0;
108 return b ? ANSI_HIGHLIGHT_ON : ANSI_HIGHLIGHT_OFF;
111 static bool error_is_no_service(DBusError *error) {
115 if (!dbus_error_is_set(error))
118 if (dbus_error_has_name(error, DBUS_ERROR_NAME_HAS_NO_OWNER))
121 if (dbus_error_has_name(error, DBUS_ERROR_SERVICE_UNKNOWN))
124 return startswith(error->name, "org.freedesktop.DBus.Error.Spawn.");
127 static int bus_iter_get_basic_and_next(DBusMessageIter *iter, int type, void *data, bool next) {
132 if (dbus_message_iter_get_arg_type(iter) != type)
135 dbus_message_iter_get_basic(iter, data);
137 if (!dbus_message_iter_next(iter) != !next)
143 static void warn_wall(enum action action) {
144 static const char *table[_ACTION_MAX] = {
145 [ACTION_HALT] = "The system is going down for system halt NOW!",
146 [ACTION_REBOOT] = "The system is going down for reboot NOW!",
147 [ACTION_POWEROFF] = "The system is going down for power-off NOW!",
148 [ACTION_RESCUE] = "The system is going down to rescue mode NOW!",
149 [ACTION_EMERGENCY] = "The system is going down to emergency mode NOW!"
158 if (!(p = strv_join(arg_wall, " "))) {
159 log_error("Failed to join strings.");
175 utmp_wall(table[action]);
178 static int list_units(DBusConnection *bus, char **args, unsigned n) {
179 DBusMessage *m = NULL, *reply = NULL;
182 DBusMessageIter iter, sub, sub2;
185 dbus_error_init(&error);
189 if (!(m = dbus_message_new_method_call(
190 "org.freedesktop.systemd1",
191 "/org/freedesktop/systemd1",
192 "org.freedesktop.systemd1.Manager",
194 log_error("Could not allocate message.");
198 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
199 log_error("Failed to issue method call: %s", error.message);
204 if (!dbus_message_iter_init(reply, &iter) ||
205 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
206 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
207 log_error("Failed to parse reply.");
212 dbus_message_iter_recurse(&iter, &sub);
214 if (isatty(STDOUT_FILENO))
215 printf("%-45s %-6s %-12s %-12s %-15s %s\n", "UNIT", "LOAD", "ACTIVE", "SUB", "JOB", "DESCRIPTION");
217 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
218 const char *id, *description, *load_state, *active_state, *sub_state, *following, *unit_path, *job_type, *job_path, *dot;
221 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
222 log_error("Failed to parse reply.");
227 dbus_message_iter_recurse(&sub, &sub2);
229 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
230 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
231 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
232 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
233 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
234 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &following, true) < 0 ||
235 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, true) < 0 ||
236 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &job_id, true) < 0 ||
237 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &job_type, true) < 0 ||
238 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, false) < 0) {
239 log_error("Failed to parse reply.");
244 if ((!arg_type || ((dot = strrchr(id, '.')) &&
245 streq(dot+1, arg_type))) &&
246 (arg_all || !(streq(active_state, "inactive") || following[0]) || job_id > 0)) {
250 if (streq(active_state, "maintenance"))
251 fputs(ansi_highlight(true), stdout);
253 e = arg_full ? NULL : ellipsize(id, 45, 33);
254 printf("%-45s %-6s %-12s %-12s%n", e ? e : id, load_state, active_state, sub_state, &a);
258 printf(" => %-12s%n", job_type, &b);
262 if (a + b + 2 < columns()) {
266 printf(" %.*s", columns() - a - b - 2, description);
269 if (streq(active_state, "maintenance"))
270 fputs(ansi_highlight(false), stdout);
276 dbus_message_iter_next(&sub);
279 if (isatty(STDOUT_FILENO)) {
281 printf("\nLOAD = Load State, reflects whether the unit configuration was properly loaded.\n"
282 "ACTIVE = Active State, the high-level unit activation state, i.e. generalization of the substate.\n"
283 "SUB = Substate, the low-level unit activation state, possible values depend on unit type.\n"
284 "JOB = Job, shows pending jobs for the unit.\n");
287 printf("\n%u units listed.\n", k);
289 printf("\n%u units listed. Pass --all to see inactive units, too.\n", k);
296 dbus_message_unref(m);
299 dbus_message_unref(reply);
301 dbus_error_free(&error);
306 static int dot_one_property(const char *name, const char *prop, DBusMessageIter *iter) {
307 static const char * const colors[] = {
308 "Requires", "[color=\"black\"]",
309 "RequiresOverridable", "[color=\"black\"]",
310 "Requisite", "[color=\"darkblue\"]",
311 "RequisiteOverridable", "[color=\"darkblue\"]",
312 "Wants", "[color=\"darkgrey\"]",
313 "Conflicts", "[color=\"red\"]",
314 "After", "[color=\"green\"]"
317 const char *c = NULL;
324 for (i = 0; i < ELEMENTSOF(colors); i += 2)
325 if (streq(colors[i], prop)) {
333 if (arg_dot != DOT_ALL)
334 if ((arg_dot == DOT_ORDER) != streq(prop, "After"))
337 switch (dbus_message_iter_get_arg_type(iter)) {
339 case DBUS_TYPE_ARRAY:
341 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
344 dbus_message_iter_recurse(iter, &sub);
346 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
349 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
350 dbus_message_iter_get_basic(&sub, &s);
351 printf("\t\"%s\"->\"%s\" %s;\n", name, s, c);
353 dbus_message_iter_next(&sub);
363 static int dot_one(DBusConnection *bus, const char *name, const char *path) {
364 DBusMessage *m = NULL, *reply = NULL;
365 const char *interface = "org.freedesktop.systemd1.Unit";
368 DBusMessageIter iter, sub, sub2, sub3;
373 dbus_error_init(&error);
375 if (!(m = dbus_message_new_method_call(
376 "org.freedesktop.systemd1",
378 "org.freedesktop.DBus.Properties",
380 log_error("Could not allocate message.");
385 if (!dbus_message_append_args(m,
386 DBUS_TYPE_STRING, &interface,
387 DBUS_TYPE_INVALID)) {
388 log_error("Could not append arguments to message.");
393 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
394 log_error("Failed to issue method call: %s", error.message);
399 if (!dbus_message_iter_init(reply, &iter) ||
400 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
401 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
402 log_error("Failed to parse reply.");
407 dbus_message_iter_recurse(&iter, &sub);
409 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
412 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
413 log_error("Failed to parse reply.");
418 dbus_message_iter_recurse(&sub, &sub2);
420 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &prop, true) < 0) {
421 log_error("Failed to parse reply.");
426 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
427 log_error("Failed to parse reply.");
432 dbus_message_iter_recurse(&sub2, &sub3);
434 if (dot_one_property(name, prop, &sub3)) {
435 log_error("Failed to parse reply.");
440 dbus_message_iter_next(&sub);
445 dbus_message_unref(m);
448 dbus_message_unref(reply);
450 dbus_error_free(&error);
455 static int dot(DBusConnection *bus, char **args, unsigned n) {
456 DBusMessage *m = NULL, *reply = NULL;
459 DBusMessageIter iter, sub, sub2;
461 dbus_error_init(&error);
465 if (!(m = dbus_message_new_method_call(
466 "org.freedesktop.systemd1",
467 "/org/freedesktop/systemd1",
468 "org.freedesktop.systemd1.Manager",
470 log_error("Could not allocate message.");
474 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
475 log_error("Failed to issue method call: %s", error.message);
480 if (!dbus_message_iter_init(reply, &iter) ||
481 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
482 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
483 log_error("Failed to parse reply.");
488 printf("digraph systemd {\n");
490 dbus_message_iter_recurse(&iter, &sub);
491 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
492 const char *id, *description, *load_state, *active_state, *sub_state, *following, *unit_path;
494 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
495 log_error("Failed to parse reply.");
500 dbus_message_iter_recurse(&sub, &sub2);
502 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
503 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
504 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
505 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
506 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
507 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &following, true) < 0 ||
508 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, true) < 0) {
509 log_error("Failed to parse reply.");
514 if ((r = dot_one(bus, id, unit_path)) < 0)
517 /* printf("\t\"%s\";\n", id); */
518 dbus_message_iter_next(&sub);
523 log_info(" Color legend: black = Requires\n"
524 " dark blue = Requisite\n"
525 " dark grey = Wants\n"
529 if (isatty(fileno(stdout)))
530 log_notice("-- You probably want to process this output with graphviz' dot tool.\n"
531 "-- Try a shell pipeline like 'systemctl dot | dot -Tsvg > systemd.svg'!\n");
537 dbus_message_unref(m);
540 dbus_message_unref(reply);
542 dbus_error_free(&error);
547 static int list_jobs(DBusConnection *bus, char **args, unsigned n) {
548 DBusMessage *m = NULL, *reply = NULL;
551 DBusMessageIter iter, sub, sub2;
554 dbus_error_init(&error);
558 if (!(m = dbus_message_new_method_call(
559 "org.freedesktop.systemd1",
560 "/org/freedesktop/systemd1",
561 "org.freedesktop.systemd1.Manager",
563 log_error("Could not allocate message.");
567 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
568 log_error("Failed to issue method call: %s", error.message);
573 if (!dbus_message_iter_init(reply, &iter) ||
574 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
575 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
576 log_error("Failed to parse reply.");
581 dbus_message_iter_recurse(&iter, &sub);
583 printf("%4s %-45s %-17s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
585 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
586 const char *name, *type, *state, *job_path, *unit_path;
590 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
591 log_error("Failed to parse reply.");
596 dbus_message_iter_recurse(&sub, &sub2);
598 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
599 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
600 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
601 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
602 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
603 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
604 log_error("Failed to parse reply.");
609 e = arg_full ? NULL : ellipsize(name, 45, 33);
610 printf("%4u %-45s %-17s %-7s\n", id, e ? e : name, type, state);
615 dbus_message_iter_next(&sub);
618 printf("\n%u jobs listed.\n", k);
623 dbus_message_unref(m);
626 dbus_message_unref(reply);
628 dbus_error_free(&error);
633 static int load_unit(DBusConnection *bus, char **args, unsigned n) {
634 DBusMessage *m = NULL, *reply = NULL;
639 dbus_error_init(&error);
644 for (i = 1; i < n; i++) {
646 if (!(m = dbus_message_new_method_call(
647 "org.freedesktop.systemd1",
648 "/org/freedesktop/systemd1",
649 "org.freedesktop.systemd1.Manager",
651 log_error("Could not allocate message.");
656 if (!dbus_message_append_args(m,
657 DBUS_TYPE_STRING, &args[i],
658 DBUS_TYPE_INVALID)) {
659 log_error("Could not append arguments to message.");
664 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
665 log_error("Failed to issue method call: %s", error.message);
670 dbus_message_unref(m);
671 dbus_message_unref(reply);
680 dbus_message_unref(m);
683 dbus_message_unref(reply);
685 dbus_error_free(&error);
690 static int cancel_job(DBusConnection *bus, char **args, unsigned n) {
691 DBusMessage *m = NULL, *reply = NULL;
696 dbus_error_init(&error);
702 return daemon_reload(bus, args, n);
704 for (i = 1; i < n; i++) {
708 if (!(m = dbus_message_new_method_call(
709 "org.freedesktop.systemd1",
710 "/org/freedesktop/systemd1",
711 "org.freedesktop.systemd1.Manager",
713 log_error("Could not allocate message.");
718 if ((r = safe_atou(args[i], &id)) < 0) {
719 log_error("Failed to parse job id: %s", strerror(-r));
723 assert_cc(sizeof(uint32_t) == sizeof(id));
724 if (!dbus_message_append_args(m,
725 DBUS_TYPE_UINT32, &id,
726 DBUS_TYPE_INVALID)) {
727 log_error("Could not append arguments to message.");
732 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
733 log_error("Failed to issue method call: %s", error.message);
738 if (!dbus_message_get_args(reply, &error,
739 DBUS_TYPE_OBJECT_PATH, &path,
740 DBUS_TYPE_INVALID)) {
741 log_error("Failed to parse reply: %s", error.message);
746 dbus_message_unref(m);
747 if (!(m = dbus_message_new_method_call(
748 "org.freedesktop.systemd1",
750 "org.freedesktop.systemd1.Job",
752 log_error("Could not allocate message.");
757 dbus_message_unref(reply);
758 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
759 log_error("Failed to issue method call: %s", error.message);
764 dbus_message_unref(m);
765 dbus_message_unref(reply);
773 dbus_message_unref(m);
776 dbus_message_unref(reply);
778 dbus_error_free(&error);
783 static bool need_daemon_reload(DBusConnection *bus, const char *unit) {
784 DBusMessage *m, *reply;
785 dbus_bool_t b = FALSE;
786 DBusMessageIter iter, sub;
788 *interface = "org.freedesktop.systemd1.Unit",
789 *property = "NeedDaemonReload",
792 /* We ignore all errors here, since this is used to show a warning only */
794 if (!(m = dbus_message_new_method_call(
795 "org.freedesktop.systemd1",
796 "/org/freedesktop/systemd1",
797 "org.freedesktop.systemd1.Manager",
801 if (!dbus_message_append_args(m,
802 DBUS_TYPE_STRING, &unit,
806 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
809 if (!dbus_message_get_args(reply, NULL,
810 DBUS_TYPE_OBJECT_PATH, &path,
814 dbus_message_unref(m);
815 if (!(m = dbus_message_new_method_call(
816 "org.freedesktop.systemd1",
818 "org.freedesktop.DBus.Properties",
822 if (!dbus_message_append_args(m,
823 DBUS_TYPE_STRING, &interface,
824 DBUS_TYPE_STRING, &property,
825 DBUS_TYPE_INVALID)) {
829 dbus_message_unref(reply);
830 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
833 if (!dbus_message_iter_init(reply, &iter) ||
834 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
837 dbus_message_iter_recurse(&iter, &sub);
839 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_BOOLEAN)
842 dbus_message_iter_get_basic(&sub, &b);
846 dbus_message_unref(m);
849 dbus_message_unref(reply);
854 typedef struct WaitData {
859 static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
867 dbus_error_init(&error);
869 log_debug("Got D-Bus request: %s.%s() on %s",
870 dbus_message_get_interface(message),
871 dbus_message_get_member(message),
872 dbus_message_get_path(message));
874 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
875 log_error("Warning! D-Bus connection terminated.");
876 dbus_connection_close(connection);
878 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
881 dbus_bool_t success = true;
883 if (!dbus_message_get_args(message, &error,
884 DBUS_TYPE_UINT32, &id,
885 DBUS_TYPE_OBJECT_PATH, &path,
886 DBUS_TYPE_BOOLEAN, &success,
888 log_error("Failed to parse message: %s", error.message);
892 if ((p = set_remove(d->set, (char*) path)))
900 dbus_error_free(&error);
901 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
904 static int enable_wait_for_jobs(DBusConnection *bus) {
912 dbus_error_init(&error);
913 dbus_bus_add_match(bus,
915 "sender='org.freedesktop.systemd1',"
916 "interface='org.freedesktop.systemd1.Manager',"
917 "member='JobRemoved',"
918 "path='/org/freedesktop/systemd1'",
921 if (dbus_error_is_set(&error)) {
922 log_error("Failed to add match: %s", error.message);
923 dbus_error_free(&error);
927 /* This is slightly dirty, since we don't undo the match registrations. */
931 static int wait_for_jobs(DBusConnection *bus, Set *s) {
942 if (!dbus_connection_add_filter(bus, wait_filter, &d, NULL)) {
943 log_error("Failed to add filter.");
948 while (!set_isempty(s) &&
949 dbus_connection_read_write_dispatch(bus, -1))
952 if (!arg_quiet && d.failed)
953 log_error("Job failed, see logs for details.");
955 r = d.failed ? -EIO : 0;
958 /* This is slightly dirty, since we don't undo the filter registration. */
963 static int start_unit_one(
970 DBusMessage *m = NULL, *reply = NULL;
979 assert(arg_no_block || s);
981 dbus_error_init(&error);
983 if (!(m = dbus_message_new_method_call(
984 "org.freedesktop.systemd1",
985 "/org/freedesktop/systemd1",
986 "org.freedesktop.systemd1.Manager",
988 log_error("Could not allocate message.");
993 if (!dbus_message_append_args(m,
994 DBUS_TYPE_STRING, &name,
995 DBUS_TYPE_STRING, &mode,
996 DBUS_TYPE_INVALID)) {
997 log_error("Could not append arguments to message.");
1002 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1004 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
1005 /* There's always a fallback possible for
1006 * legacy actions. */
1011 log_error("Failed to issue method call: %s", error.message);
1016 if (!dbus_message_get_args(reply, &error,
1017 DBUS_TYPE_OBJECT_PATH, &path,
1018 DBUS_TYPE_INVALID)) {
1019 log_error("Failed to parse reply: %s", error.message);
1024 if (need_daemon_reload(bus, name))
1025 log_warning("Unit file of created job changed on disk, 'systemctl %s daemon-reload' recommended.",
1026 arg_session ? "--session" : "--system");
1028 if (!arg_no_block) {
1031 if (!(p = strdup(path))) {
1032 log_error("Failed to duplicate path.");
1037 if ((r = set_put(s, p)) < 0) {
1039 log_error("Failed to add path to set.");
1048 dbus_message_unref(m);
1051 dbus_message_unref(reply);
1053 dbus_error_free(&error);
1058 static enum action verb_to_action(const char *verb) {
1059 if (streq(verb, "halt"))
1061 else if (streq(verb, "poweroff"))
1062 return ACTION_POWEROFF;
1063 else if (streq(verb, "reboot"))
1064 return ACTION_REBOOT;
1065 else if (streq(verb, "rescue"))
1066 return ACTION_RESCUE;
1067 else if (streq(verb, "emergency"))
1068 return ACTION_EMERGENCY;
1069 else if (streq(verb, "default"))
1070 return ACTION_DEFAULT;
1072 return ACTION_INVALID;
1075 static int start_unit(DBusConnection *bus, char **args, unsigned n) {
1077 static const char * const table[_ACTION_MAX] = {
1078 [ACTION_HALT] = SPECIAL_HALT_TARGET,
1079 [ACTION_POWEROFF] = SPECIAL_POWEROFF_TARGET,
1080 [ACTION_REBOOT] = SPECIAL_REBOOT_TARGET,
1081 [ACTION_RUNLEVEL2] = SPECIAL_RUNLEVEL2_TARGET,
1082 [ACTION_RUNLEVEL3] = SPECIAL_RUNLEVEL3_TARGET,
1083 [ACTION_RUNLEVEL4] = SPECIAL_RUNLEVEL4_TARGET,
1084 [ACTION_RUNLEVEL5] = SPECIAL_RUNLEVEL5_TARGET,
1085 [ACTION_RESCUE] = SPECIAL_RESCUE_TARGET,
1086 [ACTION_EMERGENCY] = SPECIAL_EMERGENCY_TARGET,
1087 [ACTION_DEFAULT] = SPECIAL_DEFAULT_TARGET
1092 const char *method, *mode, *one_name;
1097 if (arg_action == ACTION_SYSTEMCTL) {
1099 streq(args[0], "stop") ? "StopUnit" :
1100 streq(args[0], "reload") ? "ReloadUnit" :
1101 streq(args[0], "restart") ? "RestartUnit" :
1102 streq(args[0], "try-restart") ? "TryRestartUnit" :
1103 streq(args[0], "reload-or-restart") ? "ReloadOrRestartUnit" :
1104 streq(args[0], "reload-or-try-restart") ||
1105 streq(args[0], "force-reload") ||
1106 streq(args[0], "condrestart") ? "ReloadOrTryRestartUnit" :
1110 (streq(args[0], "isolate") ||
1111 streq(args[0], "rescue") ||
1112 streq(args[0], "emergency")) ? "isolate" :
1116 one_name = table[verb_to_action(args[0])];
1119 assert(arg_action < ELEMENTSOF(table));
1120 assert(table[arg_action]);
1122 method = "StartUnit";
1124 mode = (arg_action == ACTION_EMERGENCY ||
1125 arg_action == ACTION_RESCUE) ? "isolate" : "replace";
1127 one_name = table[arg_action];
1130 if (!arg_no_block) {
1131 if ((r = enable_wait_for_jobs(bus)) < 0) {
1132 log_error("Could not watch jobs: %s", strerror(-r));
1136 if (!(s = set_new(string_hash_func, string_compare_func))) {
1137 log_error("Failed to allocate set.");
1146 if ((r = start_unit_one(bus, method, one_name, mode, s)) <= 0)
1149 for (i = 1; i < n; i++)
1150 if ((r = start_unit_one(bus, method, args[i], mode, s)) < 0)
1155 r = wait_for_jobs(bus, s);
1164 static int start_special(DBusConnection *bus, char **args, unsigned n) {
1170 r = start_unit(bus, args, n);
1173 warn_wall(verb_to_action(args[0]));
1178 static int check_unit(DBusConnection *bus, char **args, unsigned n) {
1179 DBusMessage *m = NULL, *reply = NULL;
1181 *interface = "org.freedesktop.systemd1.Unit",
1182 *property = "ActiveState";
1183 int r = -EADDRNOTAVAIL;
1190 dbus_error_init(&error);
1192 for (i = 1; i < n; i++) {
1193 const char *path = NULL;
1195 DBusMessageIter iter, sub;
1197 if (!(m = dbus_message_new_method_call(
1198 "org.freedesktop.systemd1",
1199 "/org/freedesktop/systemd1",
1200 "org.freedesktop.systemd1.Manager",
1202 log_error("Could not allocate message.");
1207 if (!dbus_message_append_args(m,
1208 DBUS_TYPE_STRING, &args[i],
1209 DBUS_TYPE_INVALID)) {
1210 log_error("Could not append arguments to message.");
1215 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1217 /* Hmm, cannot figure out anything about this unit... */
1221 dbus_error_free(&error);
1225 if (!dbus_message_get_args(reply, &error,
1226 DBUS_TYPE_OBJECT_PATH, &path,
1227 DBUS_TYPE_INVALID)) {
1228 log_error("Failed to parse reply: %s", error.message);
1233 dbus_message_unref(m);
1234 if (!(m = dbus_message_new_method_call(
1235 "org.freedesktop.systemd1",
1237 "org.freedesktop.DBus.Properties",
1239 log_error("Could not allocate message.");
1244 if (!dbus_message_append_args(m,
1245 DBUS_TYPE_STRING, &interface,
1246 DBUS_TYPE_STRING, &property,
1247 DBUS_TYPE_INVALID)) {
1248 log_error("Could not append arguments to message.");
1253 dbus_message_unref(reply);
1254 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1255 log_error("Failed to issue method call: %s", error.message);
1260 if (!dbus_message_iter_init(reply, &iter) ||
1261 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1262 log_error("Failed to parse reply.");
1267 dbus_message_iter_recurse(&iter, &sub);
1269 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1270 log_error("Failed to parse reply.");
1275 dbus_message_iter_get_basic(&sub, &state);
1280 if (streq(state, "active") || startswith(state, "reloading"))
1283 dbus_message_unref(m);
1284 dbus_message_unref(reply);
1290 dbus_message_unref(m);
1293 dbus_message_unref(reply);
1295 dbus_error_free(&error);
1300 typedef struct ExecStatusInfo {
1306 usec_t start_timestamp;
1307 usec_t exit_timestamp;
1312 LIST_FIELDS(struct ExecStatusInfo, exec);
1315 static void exec_status_info_free(ExecStatusInfo *i) {
1323 static int exec_status_info_deserialize(DBusMessageIter *sub, ExecStatusInfo *i) {
1324 uint64_t start_timestamp, exit_timestamp;
1325 DBusMessageIter sub2, sub3;
1329 int32_t code, status;
1335 if (dbus_message_iter_get_arg_type(sub) != DBUS_TYPE_STRUCT)
1338 dbus_message_iter_recurse(sub, &sub2);
1340 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
1343 if (!(i->path = strdup(path)))
1346 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
1347 dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
1351 dbus_message_iter_recurse(&sub2, &sub3);
1352 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1353 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1354 dbus_message_iter_next(&sub3);
1359 if (!(i->argv = new0(char*, n+1)))
1363 dbus_message_iter_recurse(&sub2, &sub3);
1364 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1367 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1368 dbus_message_iter_get_basic(&sub3, &s);
1369 dbus_message_iter_next(&sub3);
1371 if (!(i->argv[n++] = strdup(s)))
1375 if (!dbus_message_iter_next(&sub2) ||
1376 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, true) < 0 ||
1377 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp, true) < 0 ||
1378 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp, true) < 0 ||
1379 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) < 0 ||
1380 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) < 0 ||
1381 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) < 0)
1385 i->start_timestamp = (usec_t) start_timestamp;
1386 i->exit_timestamp = (usec_t) exit_timestamp;
1387 i->pid = (pid_t) pid;
1394 typedef struct UnitStatusInfo {
1396 const char *load_state;
1397 const char *active_state;
1398 const char *sub_state;
1400 const char *description;
1402 const char *fragment_path;
1403 const char *default_control_group;
1405 bool need_daemon_reload;
1410 const char *status_text;
1413 usec_t start_timestamp;
1414 usec_t exit_timestamp;
1416 int exit_code, exit_status;
1419 unsigned n_accepted;
1420 unsigned n_connections;
1424 const char *sysfs_path;
1426 /* Mount, Automount */
1432 LIST_HEAD(ExecStatusInfo, exec);
1435 static void print_status_info(UnitStatusInfo *i) {
1440 /* This shows pretty information about a unit. See
1441 * print_property() for a low-level property printer */
1443 printf("%s", strna(i->id));
1445 if (i->description && !streq_ptr(i->id, i->description))
1446 printf(" - %s", i->description);
1450 if (i->fragment_path)
1451 printf("\t Loaded: %s (%s)\n", strna(i->load_state), i->fragment_path);
1452 else if (streq_ptr(i->load_state, "failed"))
1453 printf("\t Loaded: %s%s%s\n",
1454 ansi_highlight(true),
1455 strna(i->load_state),
1456 ansi_highlight(false));
1458 printf("\t Loaded: %s\n", strna(i->load_state));
1460 if (streq_ptr(i->active_state, "maintenance")) {
1461 if (streq_ptr(i->active_state, i->sub_state))
1462 printf("\t Active: %s%s%s\n",
1463 ansi_highlight(true),
1464 strna(i->active_state),
1465 ansi_highlight(false));
1467 printf("\t Active: %s%s (%s)%s\n",
1468 ansi_highlight(true),
1469 strna(i->active_state),
1470 strna(i->sub_state),
1471 ansi_highlight(false));
1473 if (streq_ptr(i->active_state, i->sub_state))
1474 printf("\t Active: %s\n",
1475 strna(i->active_state));
1477 printf("\t Active: %s (%s)\n",
1478 strna(i->active_state),
1479 strna(i->sub_state));
1483 printf("\t Device: %s\n", i->sysfs_path);
1485 printf("\t Where: %s\n", i->where);
1487 printf("\t What: %s\n", i->what);
1490 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
1492 LIST_FOREACH(exec, p, i->exec) {
1495 /* Only show exited processes here */
1499 t = strv_join(p->argv, " ");
1500 printf("\t Exited: %u (%s, code=%s, ", p->pid, strna(t), sigchld_code_to_string(p->code));
1503 if (p->code == CLD_EXITED)
1504 printf("status=%i", p->status);
1506 printf("signal=%s", signal_to_string(p->status));
1509 if (i->main_pid == p->pid &&
1510 i->start_timestamp == p->start_timestamp &&
1511 i->exit_timestamp == p->start_timestamp)
1512 /* Let's not show this twice */
1515 if (p->pid == i->control_pid)
1519 if (i->main_pid > 0 || i->control_pid > 0) {
1522 if (i->main_pid > 0) {
1523 printf(" Main: %u", (unsigned) i->main_pid);
1527 get_process_name(i->main_pid, &t);
1532 } else if (i->exit_code > 0) {
1533 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
1535 if (i->exit_code == CLD_EXITED)
1536 printf("status=%i", i->exit_status);
1538 printf("signal=%s", signal_to_string(i->exit_status));
1543 if (i->main_pid > 0 && i->control_pid > 0)
1546 if (i->control_pid > 0) {
1549 printf(" Control: %u", (unsigned) i->control_pid);
1551 get_process_name(i->control_pid, &t);
1562 printf("\t Status: \"%s\"\n", i->status_text);
1564 if (i->default_control_group) {
1567 printf("\t CGroup: %s\n", i->default_control_group);
1569 if ((c = columns()) > 18)
1574 show_cgroup_by_path(i->default_control_group, "\t\t ", c);
1577 if (i->need_daemon_reload)
1578 printf("\n%sWarning:%s Unit file changed on disk, 'systemctl %s daemon-reload' recommended.\n",
1579 ansi_highlight(true),
1580 ansi_highlight(false),
1581 arg_session ? "--session" : "--system");
1584 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
1586 switch (dbus_message_iter_get_arg_type(iter)) {
1588 case DBUS_TYPE_STRING: {
1591 dbus_message_iter_get_basic(iter, &s);
1594 if (streq(name, "Id"))
1596 else if (streq(name, "LoadState"))
1598 else if (streq(name, "ActiveState"))
1599 i->active_state = s;
1600 else if (streq(name, "SubState"))
1602 else if (streq(name, "Description"))
1604 else if (streq(name, "FragmentPath"))
1605 i->fragment_path = s;
1606 else if (streq(name, "DefaultControlGroup"))
1607 i->default_control_group = s;
1608 else if (streq(name, "StatusText"))
1610 else if (streq(name, "SysFSPath"))
1612 else if (streq(name, "Where"))
1614 else if (streq(name, "What"))
1621 case DBUS_TYPE_BOOLEAN: {
1624 dbus_message_iter_get_basic(iter, &b);
1626 if (streq(name, "Accept"))
1628 else if (streq(name, "NeedDaemonReload"))
1629 i->need_daemon_reload = b;
1634 case DBUS_TYPE_UINT32: {
1637 dbus_message_iter_get_basic(iter, &u);
1639 if (streq(name, "MainPID")) {
1641 i->main_pid = (pid_t) u;
1644 } else if (streq(name, "ControlPID"))
1645 i->control_pid = (pid_t) u;
1646 else if (streq(name, "ExecMainPID")) {
1648 i->main_pid = (pid_t) u;
1649 } else if (streq(name, "NAccepted"))
1651 else if (streq(name, "NConnections"))
1652 i->n_connections = u;
1657 case DBUS_TYPE_INT32: {
1660 dbus_message_iter_get_basic(iter, &j);
1662 if (streq(name, "ExecMainCode"))
1663 i->exit_code = (int) j;
1664 else if (streq(name, "ExecMainStatus"))
1665 i->exit_status = (int) j;
1670 case DBUS_TYPE_UINT64: {
1673 dbus_message_iter_get_basic(iter, &u);
1675 if (streq(name, "ExecMainStartTimestamp"))
1676 i->start_timestamp = (usec_t) u;
1677 else if (streq(name, "ExecMainExitTimestamp"))
1678 i->exit_timestamp = (usec_t) u;
1683 case DBUS_TYPE_ARRAY: {
1685 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT &&
1686 startswith(name, "Exec")) {
1687 DBusMessageIter sub;
1689 dbus_message_iter_recurse(iter, &sub);
1690 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1691 ExecStatusInfo *info;
1694 if (!(info = new0(ExecStatusInfo, 1)))
1697 if ((r = exec_status_info_deserialize(&sub, info)) < 0) {
1702 LIST_PREPEND(ExecStatusInfo, exec, i->exec, info);
1704 dbus_message_iter_next(&sub);
1715 static int print_property(const char *name, DBusMessageIter *iter) {
1719 /* This is a low-level property printer, see
1720 * print_status_info() for the nicer output */
1722 if (arg_property && !strv_find(arg_property, name))
1725 switch (dbus_message_iter_get_arg_type(iter)) {
1727 case DBUS_TYPE_STRING: {
1729 dbus_message_iter_get_basic(iter, &s);
1731 if (arg_all || s[0])
1732 printf("%s=%s\n", name, s);
1737 case DBUS_TYPE_BOOLEAN: {
1739 dbus_message_iter_get_basic(iter, &b);
1740 printf("%s=%s\n", name, yes_no(b));
1745 case DBUS_TYPE_UINT64: {
1747 dbus_message_iter_get_basic(iter, &u);
1749 /* Yes, heuristics! But we can change this check
1750 * should it turn out to not be sufficient */
1752 if (strstr(name, "Timestamp")) {
1753 char timestamp[FORMAT_TIMESTAMP_MAX], *t;
1755 if ((t = format_timestamp(timestamp, sizeof(timestamp), u)) || arg_all)
1756 printf("%s=%s\n", name, strempty(t));
1757 } else if (strstr(name, "USec")) {
1758 char timespan[FORMAT_TIMESPAN_MAX];
1760 printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u));
1762 printf("%s=%llu\n", name, (unsigned long long) u);
1767 case DBUS_TYPE_UINT32: {
1769 dbus_message_iter_get_basic(iter, &u);
1771 if (strstr(name, "UMask") || strstr(name, "Mode"))
1772 printf("%s=%04o\n", name, u);
1774 printf("%s=%u\n", name, (unsigned) u);
1779 case DBUS_TYPE_INT32: {
1781 dbus_message_iter_get_basic(iter, &i);
1783 printf("%s=%i\n", name, (int) i);
1787 case DBUS_TYPE_STRUCT: {
1788 DBusMessageIter sub;
1789 dbus_message_iter_recurse(iter, &sub);
1791 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
1794 dbus_message_iter_get_basic(&sub, &u);
1797 printf("%s=%u\n", name, (unsigned) u);
1799 printf("%s=\n", name);
1802 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
1805 dbus_message_iter_get_basic(&sub, &s);
1807 if (arg_all || s[0])
1808 printf("%s=%s\n", name, s);
1816 case DBUS_TYPE_ARRAY:
1818 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
1819 DBusMessageIter sub;
1822 dbus_message_iter_recurse(iter, &sub);
1824 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1825 printf("%s=", name);
1827 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1830 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
1831 dbus_message_iter_get_basic(&sub, &s);
1832 printf("%s%s", space ? " " : "", s);
1835 dbus_message_iter_next(&sub);
1843 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_BYTE) {
1844 DBusMessageIter sub;
1846 dbus_message_iter_recurse(iter, &sub);
1848 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1849 printf("%s=", name);
1851 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1854 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_BYTE);
1855 dbus_message_iter_get_basic(&sub, &u);
1858 dbus_message_iter_next(&sub);
1866 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
1867 DBusMessageIter sub, sub2;
1869 dbus_message_iter_recurse(iter, &sub);
1870 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1871 const char *type, *path;
1873 dbus_message_iter_recurse(&sub, &sub2);
1875 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
1876 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
1877 printf("%s=%s\n", type, path);
1879 dbus_message_iter_next(&sub);
1884 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
1885 DBusMessageIter sub, sub2;
1887 dbus_message_iter_recurse(iter, &sub);
1888 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1890 uint64_t value, next_elapse;
1892 dbus_message_iter_recurse(&sub, &sub2);
1894 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
1895 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
1896 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
1897 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
1899 printf("%s={ value=%s ; next_elapse=%s }\n",
1901 format_timespan(timespan1, sizeof(timespan1), value),
1902 format_timespan(timespan2, sizeof(timespan2), next_elapse));
1905 dbus_message_iter_next(&sub);
1910 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
1911 DBusMessageIter sub;
1913 dbus_message_iter_recurse(iter, &sub);
1914 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1915 ExecStatusInfo info;
1918 if (exec_status_info_deserialize(&sub, &info) >= 0) {
1919 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
1922 t = strv_join(info.argv, " ");
1924 printf("%s={ path=%s ; argv[]=%s ; ignore=%s ; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n",
1928 yes_no(info.ignore),
1929 strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)),
1930 strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)),
1931 (unsigned) info. pid,
1932 sigchld_code_to_string(info.code),
1934 info.code == CLD_EXITED ? "" : "/",
1935 strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status)));
1941 strv_free(info.argv);
1943 dbus_message_iter_next(&sub);
1953 printf("%s=[unprintable]\n", name);
1958 static int show_one(DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
1959 DBusMessage *m = NULL, *reply = NULL;
1960 const char *interface = "";
1963 DBusMessageIter iter, sub, sub2, sub3;
1964 UnitStatusInfo info;
1972 dbus_error_init(&error);
1974 if (!(m = dbus_message_new_method_call(
1975 "org.freedesktop.systemd1",
1977 "org.freedesktop.DBus.Properties",
1979 log_error("Could not allocate message.");
1984 if (!dbus_message_append_args(m,
1985 DBUS_TYPE_STRING, &interface,
1986 DBUS_TYPE_INVALID)) {
1987 log_error("Could not append arguments to message.");
1992 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1993 log_error("Failed to issue method call: %s", error.message);
1998 if (!dbus_message_iter_init(reply, &iter) ||
1999 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
2000 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
2001 log_error("Failed to parse reply.");
2006 dbus_message_iter_recurse(&iter, &sub);
2013 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2016 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
2017 log_error("Failed to parse reply.");
2022 dbus_message_iter_recurse(&sub, &sub2);
2024 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
2025 log_error("Failed to parse reply.");
2030 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
2031 log_error("Failed to parse reply.");
2036 dbus_message_iter_recurse(&sub2, &sub3);
2038 if (show_properties)
2039 r = print_property(name, &sub3);
2041 r = status_property(name, &sub3, &info);
2044 log_error("Failed to parse reply.");
2049 dbus_message_iter_next(&sub);
2052 if (!show_properties)
2053 print_status_info(&info);
2055 while ((p = info.exec)) {
2056 LIST_REMOVE(ExecStatusInfo, exec, info.exec, p);
2057 exec_status_info_free(p);
2064 dbus_message_unref(m);
2067 dbus_message_unref(reply);
2069 dbus_error_free(&error);
2074 static int show(DBusConnection *bus, char **args, unsigned n) {
2075 DBusMessage *m = NULL, *reply = NULL;
2079 bool show_properties, new_line = false;
2084 dbus_error_init(&error);
2086 show_properties = !streq(args[0], "status");
2088 if (show_properties && n <= 1) {
2089 /* If not argument is specified inspect the manager
2092 r = show_one(bus, "/org/freedesktop/systemd1", show_properties, &new_line);
2096 for (i = 1; i < n; i++) {
2097 const char *path = NULL;
2100 if (!show_properties || safe_atou32(args[i], &id) < 0) {
2102 if (!(m = dbus_message_new_method_call(
2103 "org.freedesktop.systemd1",
2104 "/org/freedesktop/systemd1",
2105 "org.freedesktop.systemd1.Manager",
2107 log_error("Could not allocate message.");
2112 if (!dbus_message_append_args(m,
2113 DBUS_TYPE_STRING, &args[i],
2114 DBUS_TYPE_INVALID)) {
2115 log_error("Could not append arguments to message.");
2120 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2122 if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
2123 log_error("Failed to issue method call: %s", error.message);
2128 dbus_error_free(&error);
2130 dbus_message_unref(m);
2131 if (!(m = dbus_message_new_method_call(
2132 "org.freedesktop.systemd1",
2133 "/org/freedesktop/systemd1",
2134 "org.freedesktop.systemd1.Manager",
2136 log_error("Could not allocate message.");
2141 if (!dbus_message_append_args(m,
2142 DBUS_TYPE_STRING, &args[i],
2143 DBUS_TYPE_INVALID)) {
2144 log_error("Could not append arguments to message.");
2149 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2150 log_error("Failed to issue method call: %s", error.message);
2158 if (!(m = dbus_message_new_method_call(
2159 "org.freedesktop.systemd1",
2160 "/org/freedesktop/systemd1",
2161 "org.freedesktop.systemd1.Manager",
2163 log_error("Could not allocate message.");
2168 if (!dbus_message_append_args(m,
2169 DBUS_TYPE_UINT32, &id,
2170 DBUS_TYPE_INVALID)) {
2171 log_error("Could not append arguments to message.");
2176 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2177 log_error("Failed to issue method call: %s", error.message);
2183 if (!dbus_message_get_args(reply, &error,
2184 DBUS_TYPE_OBJECT_PATH, &path,
2185 DBUS_TYPE_INVALID)) {
2186 log_error("Failed to parse reply: %s", error.message);
2191 if ((r = show_one(bus, path, show_properties, &new_line)) < 0)
2194 dbus_message_unref(m);
2195 dbus_message_unref(reply);
2203 dbus_message_unref(m);
2206 dbus_message_unref(reply);
2208 dbus_error_free(&error);
2213 static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
2215 DBusMessage *m = NULL, *reply = NULL;
2220 dbus_error_init(&error);
2222 log_debug("Got D-Bus request: %s.%s() on %s",
2223 dbus_message_get_interface(message),
2224 dbus_message_get_member(message),
2225 dbus_message_get_path(message));
2227 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
2228 log_error("Warning! D-Bus connection terminated.");
2229 dbus_connection_close(connection);
2231 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") ||
2232 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
2233 const char *id, *path;
2235 if (!dbus_message_get_args(message, &error,
2236 DBUS_TYPE_STRING, &id,
2237 DBUS_TYPE_OBJECT_PATH, &path,
2239 log_error("Failed to parse message: %s", error.message);
2240 else if (streq(dbus_message_get_member(message), "UnitNew"))
2241 printf("Unit %s added.\n", id);
2243 printf("Unit %s removed.\n", id);
2245 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew") ||
2246 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
2250 if (!dbus_message_get_args(message, &error,
2251 DBUS_TYPE_UINT32, &id,
2252 DBUS_TYPE_OBJECT_PATH, &path,
2254 log_error("Failed to parse message: %s", error.message);
2255 else if (streq(dbus_message_get_member(message), "JobNew"))
2256 printf("Job %u added.\n", id);
2258 printf("Job %u removed.\n", id);
2261 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Unit", "Changed") ||
2262 dbus_message_is_signal(message, "org.freedesktop.systemd1.Job", "Changed")) {
2264 const char *path, *interface, *property = "Id";
2265 DBusMessageIter iter, sub;
2267 path = dbus_message_get_path(message);
2268 interface = dbus_message_get_interface(message);
2270 if (!(m = dbus_message_new_method_call(
2271 "org.freedesktop.systemd1",
2273 "org.freedesktop.DBus.Properties",
2275 log_error("Could not allocate message.");
2279 if (!dbus_message_append_args(m,
2280 DBUS_TYPE_STRING, &interface,
2281 DBUS_TYPE_STRING, &property,
2282 DBUS_TYPE_INVALID)) {
2283 log_error("Could not append arguments to message.");
2287 if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
2288 log_error("Failed to issue method call: %s", error.message);
2292 if (!dbus_message_iter_init(reply, &iter) ||
2293 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2294 log_error("Failed to parse reply.");
2298 dbus_message_iter_recurse(&iter, &sub);
2300 if (streq(interface, "org.freedesktop.systemd1.Unit")) {
2303 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2304 log_error("Failed to parse reply.");
2308 dbus_message_iter_get_basic(&sub, &id);
2309 printf("Unit %s changed.\n", id);
2313 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32) {
2314 log_error("Failed to parse reply.");
2318 dbus_message_iter_get_basic(&sub, &id);
2319 printf("Job %u changed.\n", id);
2325 dbus_message_unref(m);
2328 dbus_message_unref(reply);
2330 dbus_error_free(&error);
2331 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2335 dbus_message_unref(m);
2338 dbus_message_unref(reply);
2340 dbus_error_free(&error);
2341 return DBUS_HANDLER_RESULT_NEED_MEMORY;
2344 static int monitor(DBusConnection *bus, char **args, unsigned n) {
2345 DBusMessage *m = NULL, *reply = NULL;
2349 dbus_error_init(&error);
2352 dbus_bus_add_match(bus,
2354 "sender='org.freedesktop.systemd1',"
2355 "interface='org.freedesktop.systemd1.Manager',"
2356 "path='/org/freedesktop/systemd1'",
2359 if (dbus_error_is_set(&error)) {
2360 log_error("Failed to add match: %s", error.message);
2365 dbus_bus_add_match(bus,
2367 "sender='org.freedesktop.systemd1',"
2368 "interface='org.freedesktop.systemd1.Unit',"
2372 if (dbus_error_is_set(&error)) {
2373 log_error("Failed to add match: %s", error.message);
2378 dbus_bus_add_match(bus,
2380 "sender='org.freedesktop.systemd1',"
2381 "interface='org.freedesktop.systemd1.Job',"
2385 if (dbus_error_is_set(&error)) {
2386 log_error("Failed to add match: %s", error.message);
2392 if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
2393 log_error("Failed to add filter.");
2398 if (!(m = dbus_message_new_method_call(
2399 "org.freedesktop.systemd1",
2400 "/org/freedesktop/systemd1",
2401 "org.freedesktop.systemd1.Manager",
2403 log_error("Could not allocate message.");
2408 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2409 log_error("Failed to issue method call: %s", error.message);
2414 while (dbus_connection_read_write_dispatch(bus, -1))
2421 /* This is slightly dirty, since we don't undo the filter or the matches. */
2424 dbus_message_unref(m);
2427 dbus_message_unref(reply);
2429 dbus_error_free(&error);
2434 static int dump(DBusConnection *bus, char **args, unsigned n) {
2435 DBusMessage *m = NULL, *reply = NULL;
2440 dbus_error_init(&error);
2442 if (!(m = dbus_message_new_method_call(
2443 "org.freedesktop.systemd1",
2444 "/org/freedesktop/systemd1",
2445 "org.freedesktop.systemd1.Manager",
2447 log_error("Could not allocate message.");
2451 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2452 log_error("Failed to issue method call: %s", error.message);
2457 if (!dbus_message_get_args(reply, &error,
2458 DBUS_TYPE_STRING, &text,
2459 DBUS_TYPE_INVALID)) {
2460 log_error("Failed to parse reply: %s", error.message);
2465 fputs(text, stdout);
2471 dbus_message_unref(m);
2474 dbus_message_unref(reply);
2476 dbus_error_free(&error);
2481 static int snapshot(DBusConnection *bus, char **args, unsigned n) {
2482 DBusMessage *m = NULL, *reply = NULL;
2485 const char *name = "", *path, *id;
2486 dbus_bool_t cleanup = FALSE;
2487 DBusMessageIter iter, sub;
2489 *interface = "org.freedesktop.systemd1.Unit",
2492 dbus_error_init(&error);
2494 if (!(m = dbus_message_new_method_call(
2495 "org.freedesktop.systemd1",
2496 "/org/freedesktop/systemd1",
2497 "org.freedesktop.systemd1.Manager",
2498 "CreateSnapshot"))) {
2499 log_error("Could not allocate message.");
2506 if (!dbus_message_append_args(m,
2507 DBUS_TYPE_STRING, &name,
2508 DBUS_TYPE_BOOLEAN, &cleanup,
2509 DBUS_TYPE_INVALID)) {
2510 log_error("Could not append arguments to message.");
2515 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2516 log_error("Failed to issue method call: %s", error.message);
2521 if (!dbus_message_get_args(reply, &error,
2522 DBUS_TYPE_OBJECT_PATH, &path,
2523 DBUS_TYPE_INVALID)) {
2524 log_error("Failed to parse reply: %s", error.message);
2529 dbus_message_unref(m);
2530 if (!(m = dbus_message_new_method_call(
2531 "org.freedesktop.systemd1",
2533 "org.freedesktop.DBus.Properties",
2535 log_error("Could not allocate message.");
2539 if (!dbus_message_append_args(m,
2540 DBUS_TYPE_STRING, &interface,
2541 DBUS_TYPE_STRING, &property,
2542 DBUS_TYPE_INVALID)) {
2543 log_error("Could not append arguments to message.");
2548 dbus_message_unref(reply);
2549 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2550 log_error("Failed to issue method call: %s", error.message);
2555 if (!dbus_message_iter_init(reply, &iter) ||
2556 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2557 log_error("Failed to parse reply.");
2562 dbus_message_iter_recurse(&iter, &sub);
2564 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2565 log_error("Failed to parse reply.");
2570 dbus_message_iter_get_basic(&sub, &id);
2578 dbus_message_unref(m);
2581 dbus_message_unref(reply);
2583 dbus_error_free(&error);
2588 static int delete_snapshot(DBusConnection *bus, char **args, unsigned n) {
2589 DBusMessage *m = NULL, *reply = NULL;
2597 dbus_error_init(&error);
2599 for (i = 1; i < n; i++) {
2600 const char *path = NULL;
2602 if (!(m = dbus_message_new_method_call(
2603 "org.freedesktop.systemd1",
2604 "/org/freedesktop/systemd1",
2605 "org.freedesktop.systemd1.Manager",
2607 log_error("Could not allocate message.");
2612 if (!dbus_message_append_args(m,
2613 DBUS_TYPE_STRING, &args[i],
2614 DBUS_TYPE_INVALID)) {
2615 log_error("Could not append arguments to message.");
2620 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2621 log_error("Failed to issue method call: %s", error.message);
2626 if (!dbus_message_get_args(reply, &error,
2627 DBUS_TYPE_OBJECT_PATH, &path,
2628 DBUS_TYPE_INVALID)) {
2629 log_error("Failed to parse reply: %s", error.message);
2634 dbus_message_unref(m);
2635 if (!(m = dbus_message_new_method_call(
2636 "org.freedesktop.systemd1",
2638 "org.freedesktop.systemd1.Snapshot",
2640 log_error("Could not allocate message.");
2645 dbus_message_unref(reply);
2646 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2647 log_error("Failed to issue method call: %s", error.message);
2652 dbus_message_unref(m);
2653 dbus_message_unref(reply);
2661 dbus_message_unref(m);
2664 dbus_message_unref(reply);
2666 dbus_error_free(&error);
2671 static int daemon_reload(DBusConnection *bus, char **args, unsigned n) {
2672 DBusMessage *m = NULL, *reply = NULL;
2677 dbus_error_init(&error);
2679 if (arg_action == ACTION_RELOAD)
2681 else if (arg_action == ACTION_REEXEC)
2682 method = "Reexecute";
2684 assert(arg_action == ACTION_SYSTEMCTL);
2687 streq(args[0], "clear-jobs") ||
2688 streq(args[0], "cancel") ? "ClearJobs" :
2689 streq(args[0], "daemon-reexec") ? "Reexecute" :
2690 streq(args[0], "reset-maintenance") ? "ResetMaintenance" :
2691 streq(args[0], "daemon-exit") ? "Exit" :
2695 if (!(m = dbus_message_new_method_call(
2696 "org.freedesktop.systemd1",
2697 "/org/freedesktop/systemd1",
2698 "org.freedesktop.systemd1.Manager",
2700 log_error("Could not allocate message.");
2704 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2706 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
2707 /* There's always a fallback possible for
2708 * legacy actions. */
2713 log_error("Failed to issue method call: %s", error.message);
2722 dbus_message_unref(m);
2725 dbus_message_unref(reply);
2727 dbus_error_free(&error);
2732 static int reset_maintenance(DBusConnection *bus, char **args, unsigned n) {
2733 DBusMessage *m = NULL, *reply = NULL;
2739 dbus_error_init(&error);
2742 return daemon_reload(bus, args, n);
2744 for (i = 1; i < n; i++) {
2746 if (!(m = dbus_message_new_method_call(
2747 "org.freedesktop.systemd1",
2748 "/org/freedesktop/systemd1",
2749 "org.freedesktop.systemd1.Manager",
2750 "ResetMaintenanceUnit"))) {
2751 log_error("Could not allocate message.");
2756 if (!dbus_message_append_args(m,
2757 DBUS_TYPE_STRING, args + i,
2758 DBUS_TYPE_INVALID)) {
2759 log_error("Could not append arguments to message.");
2764 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2765 log_error("Failed to issue method call: %s", error.message);
2770 dbus_message_unref(m);
2771 dbus_message_unref(reply);
2779 dbus_message_unref(m);
2782 dbus_message_unref(reply);
2784 dbus_error_free(&error);
2789 static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
2790 DBusMessage *m = NULL, *reply = NULL;
2792 DBusMessageIter iter, sub, sub2;
2795 *interface = "org.freedesktop.systemd1.Manager",
2796 *property = "Environment";
2798 dbus_error_init(&error);
2800 if (!(m = dbus_message_new_method_call(
2801 "org.freedesktop.systemd1",
2802 "/org/freedesktop/systemd1",
2803 "org.freedesktop.DBus.Properties",
2805 log_error("Could not allocate message.");
2809 if (!dbus_message_append_args(m,
2810 DBUS_TYPE_STRING, &interface,
2811 DBUS_TYPE_STRING, &property,
2812 DBUS_TYPE_INVALID)) {
2813 log_error("Could not append arguments to message.");
2818 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2819 log_error("Failed to issue method call: %s", error.message);
2824 if (!dbus_message_iter_init(reply, &iter) ||
2825 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2826 log_error("Failed to parse reply.");
2831 dbus_message_iter_recurse(&iter, &sub);
2833 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
2834 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
2835 log_error("Failed to parse reply.");
2840 dbus_message_iter_recurse(&sub, &sub2);
2842 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
2845 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
2846 log_error("Failed to parse reply.");
2851 dbus_message_iter_get_basic(&sub2, &text);
2852 printf("%s\n", text);
2854 dbus_message_iter_next(&sub2);
2861 dbus_message_unref(m);
2864 dbus_message_unref(reply);
2866 dbus_error_free(&error);
2871 static int set_environment(DBusConnection *bus, char **args, unsigned n) {
2872 DBusMessage *m = NULL, *reply = NULL;
2876 DBusMessageIter iter, sub;
2879 dbus_error_init(&error);
2881 method = streq(args[0], "set-environment")
2883 : "UnsetEnvironment";
2885 if (!(m = dbus_message_new_method_call(
2886 "org.freedesktop.systemd1",
2887 "/org/freedesktop/systemd1",
2888 "org.freedesktop.systemd1.Manager",
2891 log_error("Could not allocate message.");
2895 dbus_message_iter_init_append(m, &iter);
2897 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
2898 log_error("Could not append arguments to message.");
2903 for (i = 1; i < n; i++)
2904 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) {
2905 log_error("Could not append arguments to message.");
2910 if (!dbus_message_iter_close_container(&iter, &sub)) {
2911 log_error("Could not append arguments to message.");
2916 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2917 log_error("Failed to issue method call: %s", error.message);
2926 dbus_message_unref(m);
2929 dbus_message_unref(reply);
2931 dbus_error_free(&error);
2944 static Hashmap *will_install = NULL, *have_installed = NULL;
2945 static Set *remove_symlinks_to = NULL;
2947 static void install_info_free(InstallInfo *i) {
2952 strv_free(i->aliases);
2953 strv_free(i->wanted_by);
2957 static void install_info_hashmap_free(Hashmap *m) {
2960 while ((i = hashmap_steal_first(m)))
2961 install_info_free(i);
2966 static bool unit_name_valid(const char *name) {
2968 /* This is a minimal version of unit_name_valid() from
2974 if (ignore_file(name))
2980 static int install_info_add(const char *name) {
2984 assert(will_install);
2986 if (!unit_name_valid(name))
2989 if (hashmap_get(have_installed, name) ||
2990 hashmap_get(will_install, name))
2993 if (!(i = new0(InstallInfo, 1))) {
2998 if (!(i->name = strdup(name))) {
3003 if ((r = hashmap_put(will_install, i->name, i)) < 0)
3010 install_info_free(i);
3015 static int config_parse_also(
3016 const char *filename,
3018 const char *section,
3032 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
3036 if (!(n = strndup(w, l)))
3039 r = install_info_add(n);
3049 static int mark_symlink_for_removal(const char *p) {
3054 assert(path_is_absolute(p));
3056 if (!remove_symlinks_to)
3059 if (!(n = strdup(p)))
3062 path_kill_slashes(n);
3064 if ((r = set_put(remove_symlinks_to, n)) < 0) {
3066 return r == -EEXIST ? 0 : r;
3072 static int remove_marked_symlinks_fd(int fd, const char *config_path, const char *root, bool *deleted) {
3081 if (!(d = fdopendir(fd))) {
3082 close_nointr_nofail(fd);
3088 while ((de = readdir(d))) {
3089 bool is_dir = false, is_link = false;
3091 if (ignore_file(de->d_name))
3094 if (de->d_type == DT_LNK)
3096 else if (de->d_type == DT_DIR)
3098 else if (de->d_type == DT_UNKNOWN) {
3101 if (fstatat(fd, de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) {
3102 log_error("Failed to stat %s/%s: %m", root, de->d_name);
3109 is_link = S_ISLNK(st.st_mode);
3110 is_dir = S_ISDIR(st.st_mode);
3118 if ((nfd = openat(fd, de->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW)) < 0) {
3119 log_error("Failed to open %s/%s: %m", root, de->d_name);
3126 if (asprintf(&p, "%s/%s", root, de->d_name) < 0) {
3127 log_error("Failed to allocate directory string.");
3128 close_nointr_nofail(nfd);
3133 /* This will close nfd, regardless whether it succeeds or not */
3134 q = remove_marked_symlinks_fd(nfd, config_path, p, deleted);
3140 } else if (is_link) {
3144 if (asprintf(&p, "%s/%s", root, de->d_name) < 0) {
3145 log_error("Failed to allocate symlink string.");
3150 if ((q = readlink_and_make_absolute(p, &dest)) < 0) {
3151 log_error("Cannot read symlink %s: %s", p, strerror(-q));
3159 if ((c = canonicalize_file_name(dest))) {
3160 /* This might fail if the destination
3161 * is already removed */
3167 path_kill_slashes(dest);
3168 if (set_get(remove_symlinks_to, dest)) {
3171 log_info("rm '%s'", p);
3173 if (unlink(p) < 0) {
3174 log_error("Cannot unlink symlink %s: %m", p);
3179 rmdir_parents(p, config_path);
3180 path_kill_slashes(p);
3182 if (!set_get(remove_symlinks_to, p)) {
3184 if ((r = mark_symlink_for_removal(p)) < 0) {
3203 static int remove_marked_symlinks(const char *config_path) {
3207 assert(config_path);
3209 if (set_size(remove_symlinks_to) <= 0)
3212 if ((fd = open(config_path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW)) < 0)
3219 if ((cfd = dup(fd)) < 0) {
3224 /* This takes possession of cfd and closes it */
3225 if ((q = remove_marked_symlinks_fd(cfd, config_path, config_path, &deleted)) < 0) {
3231 close_nointr_nofail(fd);
3236 static int create_symlink(const char *verb, const char *old_path, const char *new_path) {
3243 if (streq(verb, "enable")) {
3246 mkdir_parents(new_path, 0755);
3248 if (symlink(old_path, new_path) >= 0) {
3251 log_info("ln -s '%s' '%s'", old_path, new_path);
3256 if (errno != EEXIST) {
3257 log_error("Cannot link %s to %s: %m", old_path, new_path);
3261 if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
3263 if (errno == EINVAL) {
3264 log_error("Cannot link %s to %s, file exists already and is not a symlink.", old_path, new_path);
3268 log_error("readlink() failed: %s", strerror(-r));
3272 if (streq(dest, old_path)) {
3278 log_error("Cannot link %s to %s, symlink exists already and points to %s.", old_path, new_path, dest);
3287 log_info("ln -s '%s' '%s'", old_path, new_path);
3289 if (symlink(old_path, new_path) >= 0)
3292 log_error("Cannot link %s to %s: %m", old_path, new_path);
3295 } else if (streq(verb, "disable")) {
3298 if ((r = mark_symlink_for_removal(old_path)) < 0)
3301 if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
3302 if (errno == ENOENT)
3305 if (errno == EINVAL) {
3306 log_warning("File %s not a symlink, ignoring.", old_path);
3310 log_error("readlink() failed: %s", strerror(-r));
3314 if (!streq(dest, old_path)) {
3315 log_warning("File %s not a symlink to %s but points to %s, ignoring.", new_path, old_path, dest);
3322 if ((r = mark_symlink_for_removal(new_path)) < 0)
3326 log_info("rm '%s'", new_path);
3328 if (unlink(new_path) >= 0)
3331 log_error("Cannot unlink %s: %m", new_path);
3334 } else if (streq(verb, "is-enabled")) {
3337 if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
3339 if (errno == ENOENT || errno == EINVAL)
3342 log_error("readlink() failed: %s", strerror(-r));
3346 if (streq(dest, old_path)) {
3354 assert_not_reached("Unknown action.");
3357 static int install_info_symlink_alias(const char *verb, InstallInfo *i, const char *config_path) {
3359 char *alias_path = NULL;
3364 assert(config_path);
3366 STRV_FOREACH(s, i->aliases) {
3368 if (!unit_name_valid(*s)) {
3369 log_error("Invalid name %s.", *s);
3375 if (!(alias_path = path_make_absolute(*s, config_path))) {
3376 log_error("Out of memory");
3381 if ((r = create_symlink(verb, i->path, alias_path)) != 0)
3384 if (streq(verb, "disable"))
3385 rmdir_parents(alias_path, config_path);
3396 static int install_info_symlink_wants(const char *verb, InstallInfo *i, const char *config_path) {
3398 char *alias_path = NULL;
3403 assert(config_path);
3405 STRV_FOREACH(s, i->wanted_by) {
3406 if (!unit_name_valid(*s)) {
3407 log_error("Invalid name %s.", *s);
3415 if (asprintf(&alias_path, "%s/%s.wants/%s", config_path, *s, i->name) < 0) {
3416 log_error("Out of memory");
3421 if ((r = create_symlink(verb, i->path, alias_path)) != 0)
3424 if (streq(verb, "disable"))
3425 rmdir_parents(alias_path, config_path);
3436 static int install_info_apply(const char *verb, LookupPaths *paths, InstallInfo *i, const char *config_path) {
3438 const ConfigItem items[] = {
3439 { "Alias", config_parse_strv, &i->aliases, "Install" },
3440 { "WantedBy", config_parse_strv, &i->wanted_by, "Install" },
3441 { "Also", config_parse_also, NULL, "Install" },
3443 { NULL, NULL, NULL, NULL }
3447 char *filename = NULL;
3454 STRV_FOREACH(p, paths->unit_path) {
3457 if (!(filename = path_make_absolute(i->name, *p))) {
3458 log_error("Out of memory");
3462 /* Ensure that we don't follow symlinks */
3463 if ((fd = open(filename, O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NOCTTY)) >= 0)
3464 if ((f = fdopen(fd, "re")))
3467 if (errno == ELOOP) {
3468 log_error("Refusing to operate on symlinks, please pass unit names or absolute paths to unit files.");
3473 if (errno != ENOENT) {
3474 log_error("Failed to open %s: %m", filename);
3484 log_error("Couldn't find %s.", i->name);
3490 if ((r = config_parse(filename, f, NULL, items, true, i)) < 0) {
3497 if ((r = install_info_symlink_alias(verb, i, config_path)) != 0)
3500 if ((r = install_info_symlink_wants(verb, i, config_path)) != 0)
3503 if ((r = mark_symlink_for_removal(filename)) < 0)
3506 if ((r = remove_marked_symlinks(config_path)) < 0)
3512 static char *get_config_path(void) {
3514 if (arg_session && arg_global)
3515 return strdup(SESSION_CONFIG_UNIT_PATH);
3520 if (session_config_home(&p) < 0)
3526 return strdup(SYSTEM_CONFIG_UNIT_PATH);
3529 static int enable_unit(DBusConnection *bus, char **args, unsigned n) {
3533 char *config_path = NULL;
3536 const char *verb = args[0];
3538 dbus_error_init(&error);
3541 if ((r = lookup_paths_init(&paths, arg_session ? MANAGER_SESSION : MANAGER_SYSTEM)) < 0) {
3542 log_error("Failed to determine lookup paths: %s", strerror(-r));
3546 if (!(config_path = get_config_path())) {
3547 log_error("Failed to determine config path");
3552 will_install = hashmap_new(string_hash_func, string_compare_func);
3553 have_installed = hashmap_new(string_hash_func, string_compare_func);
3555 if (!will_install || !have_installed) {
3556 log_error("Failed to allocate unit sets.");
3561 if (!arg_defaults && streq(verb, "disable"))
3562 if (!(remove_symlinks_to = set_new(string_hash_func, string_compare_func))) {
3563 log_error("Failed to allocate symlink sets.");
3568 for (j = 1; j < n; j++)
3569 if ((r = install_info_add(args[j])) < 0)
3572 while ((i = hashmap_first(will_install))) {
3575 assert_se(hashmap_move_one(have_installed, will_install, i->name) == 0);
3577 if ((q = install_info_apply(verb, &paths, i, config_path)) != 0) {
3585 /* In test mode and found something */
3591 if (streq(verb, "is-enabled"))
3592 r = r > 0 ? 0 : -ENOENT;
3594 /* Don't try to reload anything if the user asked us to not do this */
3596 /* Don't try to reload anything when updating a unit globally */
3598 /* Don't try to reload anything if we are called for system changes but the system wasn't booted with systemd */
3599 (arg_session || sd_booted() > 0) &&
3600 /* Don't try to reload anything if we are running in a chroot environment */
3601 (arg_session || running_in_chroot() <= 0) ) {
3604 if ((q = daemon_reload(bus, args, n)) < 0)
3609 install_info_hashmap_free(will_install);
3610 install_info_hashmap_free(have_installed);
3612 set_free_free(remove_symlinks_to);
3614 lookup_paths_free(&paths);
3621 static int systemctl_help(void) {
3623 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
3624 "Send control commands to or query the systemd manager.\n\n"
3625 " -h --help Show this help\n"
3626 " -t --type=TYPE List only units of a particular type\n"
3627 " -p --property=NAME Show only properties by this name\n"
3628 " -a --all Show all units/properties, including dead/empty ones\n"
3629 " --full Don't ellipsize unit names on output\n"
3630 " --fail When queueing a new job, fail if conflicting jobs are\n"
3632 " -q --quiet Suppress output\n"
3633 " --no-block Do not wait until operation finished\n"
3634 " --system Connect to system bus\n"
3635 " --session Connect to session bus\n"
3636 " --order When generating graph for dot, show only order\n"
3637 " --require When generating graph for dot, show only requirement\n"
3638 " --no-wall Don't send wall message before halt/power-off/reboot\n"
3639 " --global Enable/disable unit files globally\n"
3640 " --no-reload When enabling/disabling unit files, don't reload daemon\n"
3642 " --force When enabling unit files, override existing symlinks\n"
3643 " --defaults When disabling unit files, remove default symlinks only\n\n"
3645 " list-units List units\n"
3646 " start [NAME...] Start (activate) one or more units\n"
3647 " stop [NAME...] Stop (deactivate) one or more units\n"
3648 " reload [NAME...] Reload one or more units\n"
3649 " restart [NAME...] Start or restart one or more units\n"
3650 " try-restart [NAME...] Restart one or more units if active\n"
3651 " reload-or-restart [NAME...] Reload one or more units is possible,\n"
3652 " otherwise start or restart\n"
3653 " reload-or-try-restart [NAME...] Reload one or more units is possible,\n"
3654 " otherwise restart if active\n"
3655 " isolate [NAME] Start one unit and stop all others\n"
3656 " is-active [NAME...] Check whether units are active\n"
3657 " status [NAME...] Show runtime status of one or more units\n"
3658 " show [NAME...|JOB...] Show properties of one or more\n"
3659 " units/jobs or the manager\n"
3660 " reset-maintenance [NAME...] Reset maintenance state for all, one,\n"
3662 " enable [NAME...] Enable one or more unit files\n"
3663 " disable [NAME...] Disable one or more unit files\n"
3664 " is-enabled [NAME...] Check whether unit files are enabled\n"
3665 " load [NAME...] Load one or more units\n"
3666 " list-jobs List jobs\n"
3667 " cancel [JOB...] Cancel all, one, or more jobs\n"
3668 " monitor Monitor unit/job changes\n"
3669 " dump Dump server status\n"
3670 " dot Dump dependency graph for dot(1)\n"
3671 " snapshot [NAME] Create a snapshot\n"
3672 " delete [NAME...] Remove one or more snapshots\n"
3673 " daemon-reload Reload systemd manager configuration\n"
3674 " daemon-reexec Reexecute systemd manager\n"
3675 " daemon-exit Ask the systemd manager to quit\n"
3676 " show-environment Dump environment\n"
3677 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
3678 " unset-environment [NAME...] Unset one or more environment variables\n"
3679 " halt Shut down and halt the system\n"
3680 " poweroff Shut down and power-off the system\n"
3681 " reboot Shut down and reboot the system\n"
3682 " rescue Enter system rescue mode\n"
3683 " emergency Enter system emergency mode\n"
3684 " default Enter system default mode\n",
3685 program_invocation_short_name);
3690 static int halt_help(void) {
3692 printf("%s [OPTIONS...]\n\n"
3693 "%s the system.\n\n"
3694 " --help Show this help\n"
3695 " --halt Halt the machine\n"
3696 " -p --poweroff Switch off the machine\n"
3697 " --reboot Reboot the machine\n"
3698 " -f --force Force immediate halt/power-off/reboot\n"
3699 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
3700 " -d --no-wtmp Don't write wtmp record\n"
3701 " -n --no-sync Don't sync before halt/power-off/reboot\n"
3702 " --no-wall Don't send wall message before halt/power-off/reboot\n",
3703 program_invocation_short_name,
3704 arg_action == ACTION_REBOOT ? "Reboot" :
3705 arg_action == ACTION_POWEROFF ? "Power off" :
3711 static int shutdown_help(void) {
3713 printf("%s [OPTIONS...] [now] [WALL...]\n\n"
3714 "Shut down the system.\n\n"
3715 " --help Show this help\n"
3716 " -H --halt Halt the machine\n"
3717 " -P --poweroff Power-off the machine\n"
3718 " -r --reboot Reboot the machine\n"
3719 " -h Equivalent to --poweroff, overriden by --halt\n"
3720 " -k Don't halt/power-off/reboot, just send warnings\n"
3721 " --no-wall Don't send wall message before halt/power-off/reboot\n",
3722 program_invocation_short_name);
3727 static int telinit_help(void) {
3729 printf("%s [OPTIONS...] {COMMAND}\n\n"
3730 "Send control commands to the init daemon.\n\n"
3731 " --help Show this help\n"
3732 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
3734 " 0 Power-off the machine\n"
3735 " 6 Reboot the machine\n"
3736 " 2, 3, 4, 5 Start runlevelX.target unit\n"
3737 " 1, s, S Enter rescue mode\n"
3738 " q, Q Reload init daemon configuration\n"
3739 " u, U Reexecute init daemon\n",
3740 program_invocation_short_name);
3745 static int runlevel_help(void) {
3747 printf("%s [OPTIONS...]\n\n"
3748 "Prints the previous and current runlevel of the init system.\n\n"
3749 " --help Show this help\n",
3750 program_invocation_short_name);
3755 static int systemctl_parse_argv(int argc, char *argv[]) {
3772 static const struct option options[] = {
3773 { "help", no_argument, NULL, 'h' },
3774 { "type", required_argument, NULL, 't' },
3775 { "property", required_argument, NULL, 'p' },
3776 { "all", no_argument, NULL, 'a' },
3777 { "full", no_argument, NULL, ARG_FULL },
3778 { "fail", no_argument, NULL, ARG_FAIL },
3779 { "session", no_argument, NULL, ARG_SESSION },
3780 { "system", no_argument, NULL, ARG_SYSTEM },
3781 { "global", no_argument, NULL, ARG_GLOBAL },
3782 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
3783 { "no-wall", no_argument, NULL, ARG_NO_WALL },
3784 { "quiet", no_argument, NULL, 'q' },
3785 { "order", no_argument, NULL, ARG_ORDER },
3786 { "require", no_argument, NULL, ARG_REQUIRE },
3787 { "force", no_argument, NULL, ARG_FORCE },
3788 { "no-reload", no_argument, NULL, ARG_NO_RELOAD },
3789 { "defaults", no_argument, NULL, ARG_DEFAULTS },
3790 { NULL, 0, NULL, 0 }
3798 while ((c = getopt_long(argc, argv, "ht:p:aq", options, NULL)) >= 0) {
3813 if (!(l = strv_append(arg_property, optarg)))
3816 strv_free(arg_property);
3819 /* If the user asked for a particular
3820 * property, show it to him, even if it is
3839 arg_session = false;
3843 arg_no_block = true;
3851 arg_dot = DOT_ORDER;
3855 arg_dot = DOT_REQUIRE;
3871 arg_no_reload = true;
3880 arg_defaults = true;
3887 log_error("Unknown option code %c", c);
3895 static int halt_parse_argv(int argc, char *argv[]) {
3904 static const struct option options[] = {
3905 { "help", no_argument, NULL, ARG_HELP },
3906 { "halt", no_argument, NULL, ARG_HALT },
3907 { "poweroff", no_argument, NULL, 'p' },
3908 { "reboot", no_argument, NULL, ARG_REBOOT },
3909 { "force", no_argument, NULL, 'f' },
3910 { "wtmp-only", no_argument, NULL, 'w' },
3911 { "no-wtmp", no_argument, NULL, 'd' },
3912 { "no-sync", no_argument, NULL, 'n' },
3913 { "no-wall", no_argument, NULL, ARG_NO_WALL },
3914 { NULL, 0, NULL, 0 }
3922 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
3923 if (runlevel == '0' || runlevel == '6')
3924 arg_immediate = true;
3926 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
3934 arg_action = ACTION_HALT;
3938 if (arg_action != ACTION_REBOOT)
3939 arg_action = ACTION_POWEROFF;
3943 arg_action = ACTION_REBOOT;
3947 arg_immediate = true;
3968 /* Compatibility nops */
3975 log_error("Unknown option code %c", c);
3980 if (optind < argc) {
3981 log_error("Too many arguments.");
3988 static int shutdown_parse_argv(int argc, char *argv[]) {
3995 static const struct option options[] = {
3996 { "help", no_argument, NULL, ARG_HELP },
3997 { "halt", no_argument, NULL, 'H' },
3998 { "poweroff", no_argument, NULL, 'P' },
3999 { "reboot", no_argument, NULL, 'r' },
4000 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4001 { NULL, 0, NULL, 0 }
4009 while ((c = getopt_long(argc, argv, "HPrhkt:a", options, NULL)) >= 0) {
4017 arg_action = ACTION_HALT;
4021 arg_action = ACTION_POWEROFF;
4025 arg_action = ACTION_REBOOT;
4029 if (arg_action != ACTION_HALT)
4030 arg_action = ACTION_POWEROFF;
4043 /* Compatibility nops */
4050 log_error("Unknown option code %c", c);
4055 if (argc > optind && !streq(argv[optind], "now"))
4056 log_warning("First argument '%s' isn't 'now'. Ignoring.", argv[optind]);
4058 /* We ignore the time argument */
4059 if (argc > optind + 1)
4060 arg_wall = argv + optind + 1;
4067 static int telinit_parse_argv(int argc, char *argv[]) {
4074 static const struct option options[] = {
4075 { "help", no_argument, NULL, ARG_HELP },
4076 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4077 { NULL, 0, NULL, 0 }
4080 static const struct {
4084 { '0', ACTION_POWEROFF },
4085 { '6', ACTION_REBOOT },
4086 { '1', ACTION_RESCUE },
4087 { '2', ACTION_RUNLEVEL2 },
4088 { '3', ACTION_RUNLEVEL3 },
4089 { '4', ACTION_RUNLEVEL4 },
4090 { '5', ACTION_RUNLEVEL5 },
4091 { 's', ACTION_RESCUE },
4092 { 'S', ACTION_RESCUE },
4093 { 'q', ACTION_RELOAD },
4094 { 'Q', ACTION_RELOAD },
4095 { 'u', ACTION_REEXEC },
4096 { 'U', ACTION_REEXEC }
4105 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4120 log_error("Unknown option code %c", c);
4125 if (optind >= argc) {
4130 if (optind + 1 < argc) {
4131 log_error("Too many arguments.");
4135 if (strlen(argv[optind]) != 1) {
4136 log_error("Expected single character argument.");
4140 for (i = 0; i < ELEMENTSOF(table); i++)
4141 if (table[i].from == argv[optind][0])
4144 if (i >= ELEMENTSOF(table)) {
4145 log_error("Unknown command %s.", argv[optind]);
4149 arg_action = table[i].to;
4156 static int runlevel_parse_argv(int argc, char *argv[]) {
4162 static const struct option options[] = {
4163 { "help", no_argument, NULL, ARG_HELP },
4164 { NULL, 0, NULL, 0 }
4172 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4183 log_error("Unknown option code %c", c);
4188 if (optind < argc) {
4189 log_error("Too many arguments.");
4196 static int parse_argv(int argc, char *argv[]) {
4200 if (program_invocation_short_name) {
4202 if (strstr(program_invocation_short_name, "halt")) {
4203 arg_action = ACTION_HALT;
4204 return halt_parse_argv(argc, argv);
4205 } else if (strstr(program_invocation_short_name, "poweroff")) {
4206 arg_action = ACTION_POWEROFF;
4207 return halt_parse_argv(argc, argv);
4208 } else if (strstr(program_invocation_short_name, "reboot")) {
4209 arg_action = ACTION_REBOOT;
4210 return halt_parse_argv(argc, argv);
4211 } else if (strstr(program_invocation_short_name, "shutdown")) {
4212 arg_action = ACTION_POWEROFF;
4213 return shutdown_parse_argv(argc, argv);
4214 } else if (strstr(program_invocation_short_name, "init")) {
4216 if (sd_booted() > 0) {
4217 arg_action = ACTION_INVALID;
4218 return telinit_parse_argv(argc, argv);
4220 /* Hmm, so some other init system is
4221 * running, we need to forward this
4222 * request to it. For now we simply
4223 * guess that it is Upstart. */
4225 execv("/lib/upstart/telinit", argv);
4227 log_error("Couldn't find an alternative telinit implementation to spawn.");
4231 } else if (strstr(program_invocation_short_name, "runlevel")) {
4232 arg_action = ACTION_RUNLEVEL;
4233 return runlevel_parse_argv(argc, argv);
4237 arg_action = ACTION_SYSTEMCTL;
4238 return systemctl_parse_argv(argc, argv);
4241 static int action_to_runlevel(void) {
4243 static const char table[_ACTION_MAX] = {
4244 [ACTION_HALT] = '0',
4245 [ACTION_POWEROFF] = '0',
4246 [ACTION_REBOOT] = '6',
4247 [ACTION_RUNLEVEL2] = '2',
4248 [ACTION_RUNLEVEL3] = '3',
4249 [ACTION_RUNLEVEL4] = '4',
4250 [ACTION_RUNLEVEL5] = '5',
4251 [ACTION_RESCUE] = '1'
4254 assert(arg_action < _ACTION_MAX);
4256 return table[arg_action];
4259 static int talk_upstart(void) {
4260 DBusMessage *m = NULL, *reply = NULL;
4262 int previous, rl, r;
4264 env1_buf[] = "RUNLEVEL=X",
4265 env2_buf[] = "PREVLEVEL=X";
4266 char *env1 = env1_buf, *env2 = env2_buf;
4267 const char *emit = "runlevel";
4268 dbus_bool_t b_false = FALSE;
4269 DBusMessageIter iter, sub;
4270 DBusConnection *bus;
4272 dbus_error_init(&error);
4274 if (!(rl = action_to_runlevel()))
4277 if (utmp_get_runlevel(&previous, NULL) < 0)
4280 if (!(bus = dbus_connection_open_private("unix:abstract=/com/ubuntu/upstart", &error))) {
4281 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
4286 log_error("Failed to connect to Upstart bus: %s", error.message);
4291 if ((r = bus_check_peercred(bus)) < 0) {
4292 log_error("Failed to verify owner of bus.");
4296 if (!(m = dbus_message_new_method_call(
4297 "com.ubuntu.Upstart",
4298 "/com/ubuntu/Upstart",
4299 "com.ubuntu.Upstart0_6",
4302 log_error("Could not allocate message.");
4307 dbus_message_iter_init_append(m, &iter);
4309 env1_buf[sizeof(env1_buf)-2] = rl;
4310 env2_buf[sizeof(env2_buf)-2] = previous;
4312 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
4313 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
4314 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
4315 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
4316 !dbus_message_iter_close_container(&iter, &sub) ||
4317 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
4318 log_error("Could not append arguments to message.");
4323 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
4325 if (error_is_no_service(&error)) {
4330 log_error("Failed to issue method call: %s", error.message);
4339 dbus_message_unref(m);
4342 dbus_message_unref(reply);
4345 dbus_connection_close(bus);
4346 dbus_connection_unref(bus);
4349 dbus_error_free(&error);
4354 static int talk_initctl(void) {
4355 struct init_request request;
4359 if (!(rl = action_to_runlevel()))
4363 request.magic = INIT_MAGIC;
4364 request.sleeptime = 0;
4365 request.cmd = INIT_CMD_RUNLVL;
4366 request.runlevel = rl;
4368 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
4370 if (errno == ENOENT)
4373 log_error("Failed to open "INIT_FIFO": %m");
4378 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
4379 close_nointr_nofail(fd);
4382 log_error("Failed to write to "INIT_FIFO": %m");
4383 return errno ? -errno : -EIO;
4389 static int systemctl_main(DBusConnection *bus, int argc, char *argv[], DBusError *error) {
4391 static const struct {
4399 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
4401 { "list-units", LESS, 1, list_units },
4402 { "list-jobs", EQUAL, 1, list_jobs },
4403 { "clear-jobs", EQUAL, 1, daemon_reload },
4404 { "load", MORE, 2, load_unit },
4405 { "cancel", MORE, 2, cancel_job },
4406 { "start", MORE, 2, start_unit },
4407 { "stop", MORE, 2, start_unit },
4408 { "reload", MORE, 2, start_unit },
4409 { "restart", MORE, 2, start_unit },
4410 { "try-restart", MORE, 2, start_unit },
4411 { "reload-or-restart", MORE, 2, start_unit },
4412 { "reload-or-try-restart", MORE, 2, start_unit },
4413 { "force-reload", MORE, 2, start_unit }, /* For compatibility with SysV */
4414 { "condrestart", MORE, 2, start_unit }, /* For compatibility with RH */
4415 { "isolate", EQUAL, 2, start_unit },
4416 { "is-active", MORE, 2, check_unit },
4417 { "check", MORE, 2, check_unit },
4418 { "show", MORE, 1, show },
4419 { "status", MORE, 2, show },
4420 { "monitor", EQUAL, 1, monitor },
4421 { "dump", EQUAL, 1, dump },
4422 { "dot", EQUAL, 1, dot },
4423 { "snapshot", LESS, 2, snapshot },
4424 { "delete", MORE, 2, delete_snapshot },
4425 { "daemon-reload", EQUAL, 1, daemon_reload },
4426 { "daemon-reexec", EQUAL, 1, daemon_reload },
4427 { "daemon-exit", EQUAL, 1, daemon_reload },
4428 { "show-environment", EQUAL, 1, show_enviroment },
4429 { "set-environment", MORE, 2, set_environment },
4430 { "unset-environment", MORE, 2, set_environment },
4431 { "halt", EQUAL, 1, start_special },
4432 { "poweroff", EQUAL, 1, start_special },
4433 { "reboot", EQUAL, 1, start_special },
4434 { "default", EQUAL, 1, start_special },
4435 { "rescue", EQUAL, 1, start_special },
4436 { "emergency", EQUAL, 1, start_special },
4437 { "reset-maintenance", MORE, 1, reset_maintenance },
4438 { "enable", MORE, 2, enable_unit },
4439 { "disable", MORE, 2, enable_unit },
4440 { "is-enabled", MORE, 2, enable_unit }
4450 left = argc - optind;
4453 /* Special rule: no arguments means "list-units" */
4456 if (streq(argv[optind], "help")) {
4461 for (i = 0; i < ELEMENTSOF(verbs); i++)
4462 if (streq(argv[optind], verbs[i].verb))
4465 if (i >= ELEMENTSOF(verbs)) {
4466 log_error("Unknown operation %s", argv[optind]);
4471 switch (verbs[i].argc_cmp) {
4474 if (left != verbs[i].argc) {
4475 log_error("Invalid number of arguments.");
4482 if (left < verbs[i].argc) {
4483 log_error("Too few arguments.");
4490 if (left > verbs[i].argc) {
4491 log_error("Too many arguments.");
4498 assert_not_reached("Unknown comparison operator.");
4501 /* Require a bus connection for all operations but
4503 if (!streq(verbs[i].verb, "enable") &&
4504 !streq(verbs[i].verb, "disable") &&
4506 log_error("Failed to get D-Bus connection: %s", error->message);
4510 return verbs[i].dispatch(bus, argv + optind, left);
4513 static int reload_with_fallback(DBusConnection *bus) {
4517 /* First, try systemd via D-Bus. */
4518 if ((r = daemon_reload(bus, NULL, 0)) > 0)
4522 /* Nothing else worked, so let's try signals */
4523 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
4525 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
4526 log_error("kill() failed: %m");
4533 static int start_with_fallback(DBusConnection *bus) {
4537 /* First, try systemd via D-Bus. */
4538 if ((r = start_unit(bus, NULL, 0)) > 0)
4542 /* Hmm, talking to systemd via D-Bus didn't work. Then
4543 * let's try to talk to Upstart via D-Bus. */
4544 if ((r = talk_upstart()) > 0)
4547 /* Nothing else worked, so let's try
4549 if ((r = talk_initctl()) != 0)
4552 log_error("Failed to talk to init daemon.");
4556 warn_wall(arg_action);
4560 static int halt_main(DBusConnection *bus) {
4563 if (geteuid() != 0) {
4564 log_error("Must to be root.");
4568 if (!arg_dry && !arg_immediate)
4569 return start_with_fallback(bus);
4572 if ((r = utmp_put_shutdown(0)) < 0)
4573 log_warning("Failed to write utmp record: %s", strerror(-r));
4581 /* Make sure C-A-D is handled by the kernel from this
4583 reboot(RB_ENABLE_CAD);
4585 switch (arg_action) {
4588 log_info("Halting");
4589 reboot(RB_HALT_SYSTEM);
4592 case ACTION_POWEROFF:
4593 log_info("Powering off");
4594 reboot(RB_POWER_OFF);
4598 log_info("Rebooting");
4599 reboot(RB_AUTOBOOT);
4603 assert_not_reached("Unknown halt action.");
4606 /* We should never reach this. */
4610 static int runlevel_main(void) {
4611 int r, runlevel, previous;
4613 if ((r = utmp_get_runlevel(&runlevel, &previous)) < 0) {
4619 previous <= 0 ? 'N' : previous,
4620 runlevel <= 0 ? 'N' : runlevel);
4625 int main(int argc, char*argv[]) {
4627 DBusConnection *bus = NULL;
4630 dbus_error_init(&error);
4632 log_parse_environment();
4634 if ((r = parse_argv(argc, argv)) < 0)
4641 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
4642 * let's shortcut this */
4643 if (arg_action == ACTION_RUNLEVEL) {
4644 retval = runlevel_main() < 0;
4648 bus_connect(arg_session ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &bus, &private_bus, &error);
4650 switch (arg_action) {
4652 case ACTION_SYSTEMCTL: {
4653 retval = systemctl_main(bus, argc, argv, &error) < 0;
4658 case ACTION_POWEROFF:
4660 retval = halt_main(bus) < 0;
4663 case ACTION_RUNLEVEL2:
4664 case ACTION_RUNLEVEL3:
4665 case ACTION_RUNLEVEL4:
4666 case ACTION_RUNLEVEL5:
4668 case ACTION_EMERGENCY:
4669 case ACTION_DEFAULT:
4670 retval = start_with_fallback(bus) < 0;
4675 retval = reload_with_fallback(bus) < 0;
4678 case ACTION_INVALID:
4679 case ACTION_RUNLEVEL:
4681 assert_not_reached("Unknown action");
4687 dbus_connection_close(bus);
4688 dbus_connection_unref(bus);
4691 dbus_error_free(&error);
4695 strv_free(arg_property);