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, *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_OBJECT_PATH, &unit_path, true) < 0) {
508 log_error("Failed to parse reply.");
513 if ((r = dot_one(bus, id, unit_path)) < 0)
516 /* printf("\t\"%s\";\n", id); */
517 dbus_message_iter_next(&sub);
522 log_info(" Color legend: black = Requires\n"
523 " dark blue = Requisite\n"
524 " dark grey = Wants\n"
528 if (isatty(fileno(stdout)))
529 log_notice("-- You probably want to process this output with graphviz' dot tool.\n"
530 "-- Try a shell pipeline like 'systemctl dot | dot -Tsvg > systemd.svg'!\n");
536 dbus_message_unref(m);
539 dbus_message_unref(reply);
541 dbus_error_free(&error);
546 static int list_jobs(DBusConnection *bus, char **args, unsigned n) {
547 DBusMessage *m = NULL, *reply = NULL;
550 DBusMessageIter iter, sub, sub2;
553 dbus_error_init(&error);
557 if (!(m = dbus_message_new_method_call(
558 "org.freedesktop.systemd1",
559 "/org/freedesktop/systemd1",
560 "org.freedesktop.systemd1.Manager",
562 log_error("Could not allocate message.");
566 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
567 log_error("Failed to issue method call: %s", error.message);
572 if (!dbus_message_iter_init(reply, &iter) ||
573 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
574 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
575 log_error("Failed to parse reply.");
580 dbus_message_iter_recurse(&iter, &sub);
582 printf("%4s %-45s %-17s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
584 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
585 const char *name, *type, *state, *job_path, *unit_path;
589 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
590 log_error("Failed to parse reply.");
595 dbus_message_iter_recurse(&sub, &sub2);
597 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
598 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
599 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
600 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
601 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
602 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
603 log_error("Failed to parse reply.");
608 e = arg_full ? NULL : ellipsize(name, 45, 33);
609 printf("%4u %-45s %-17s %-7s\n", id, e ? e : name, type, state);
614 dbus_message_iter_next(&sub);
617 printf("\n%u jobs listed.\n", k);
622 dbus_message_unref(m);
625 dbus_message_unref(reply);
627 dbus_error_free(&error);
632 static int load_unit(DBusConnection *bus, char **args, unsigned n) {
633 DBusMessage *m = NULL, *reply = NULL;
638 dbus_error_init(&error);
643 for (i = 1; i < n; i++) {
645 if (!(m = dbus_message_new_method_call(
646 "org.freedesktop.systemd1",
647 "/org/freedesktop/systemd1",
648 "org.freedesktop.systemd1.Manager",
650 log_error("Could not allocate message.");
655 if (!dbus_message_append_args(m,
656 DBUS_TYPE_STRING, &args[i],
657 DBUS_TYPE_INVALID)) {
658 log_error("Could not append arguments to message.");
663 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
664 log_error("Failed to issue method call: %s", error.message);
669 dbus_message_unref(m);
670 dbus_message_unref(reply);
679 dbus_message_unref(m);
682 dbus_message_unref(reply);
684 dbus_error_free(&error);
689 static int cancel_job(DBusConnection *bus, char **args, unsigned n) {
690 DBusMessage *m = NULL, *reply = NULL;
695 dbus_error_init(&error);
701 return daemon_reload(bus, args, n);
703 for (i = 1; i < n; i++) {
707 if (!(m = dbus_message_new_method_call(
708 "org.freedesktop.systemd1",
709 "/org/freedesktop/systemd1",
710 "org.freedesktop.systemd1.Manager",
712 log_error("Could not allocate message.");
717 if ((r = safe_atou(args[i], &id)) < 0) {
718 log_error("Failed to parse job id: %s", strerror(-r));
722 assert_cc(sizeof(uint32_t) == sizeof(id));
723 if (!dbus_message_append_args(m,
724 DBUS_TYPE_UINT32, &id,
725 DBUS_TYPE_INVALID)) {
726 log_error("Could not append arguments to message.");
731 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
732 log_error("Failed to issue method call: %s", error.message);
737 if (!dbus_message_get_args(reply, &error,
738 DBUS_TYPE_OBJECT_PATH, &path,
739 DBUS_TYPE_INVALID)) {
740 log_error("Failed to parse reply: %s", error.message);
745 dbus_message_unref(m);
746 if (!(m = dbus_message_new_method_call(
747 "org.freedesktop.systemd1",
749 "org.freedesktop.systemd1.Job",
751 log_error("Could not allocate message.");
756 dbus_message_unref(reply);
757 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
758 log_error("Failed to issue method call: %s", error.message);
763 dbus_message_unref(m);
764 dbus_message_unref(reply);
772 dbus_message_unref(m);
775 dbus_message_unref(reply);
777 dbus_error_free(&error);
782 static bool need_daemon_reload(DBusConnection *bus, const char *unit) {
783 DBusMessage *m, *reply;
784 dbus_bool_t b = FALSE;
785 DBusMessageIter iter, sub;
787 *interface = "org.freedesktop.systemd1.Unit",
788 *property = "NeedDaemonReload",
791 /* We ignore all errors here, since this is used to show a warning only */
793 if (!(m = dbus_message_new_method_call(
794 "org.freedesktop.systemd1",
795 "/org/freedesktop/systemd1",
796 "org.freedesktop.systemd1.Manager",
800 if (!dbus_message_append_args(m,
801 DBUS_TYPE_STRING, &unit,
805 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
808 if (!dbus_message_get_args(reply, NULL,
809 DBUS_TYPE_OBJECT_PATH, &path,
813 dbus_message_unref(m);
814 if (!(m = dbus_message_new_method_call(
815 "org.freedesktop.systemd1",
817 "org.freedesktop.DBus.Properties",
821 if (!dbus_message_append_args(m,
822 DBUS_TYPE_STRING, &interface,
823 DBUS_TYPE_STRING, &property,
824 DBUS_TYPE_INVALID)) {
828 dbus_message_unref(reply);
829 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
832 if (!dbus_message_iter_init(reply, &iter) ||
833 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
836 dbus_message_iter_recurse(&iter, &sub);
838 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_BOOLEAN)
841 dbus_message_iter_get_basic(&sub, &b);
845 dbus_message_unref(m);
848 dbus_message_unref(reply);
853 typedef struct WaitData {
858 static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
866 dbus_error_init(&error);
868 log_debug("Got D-Bus request: %s.%s() on %s",
869 dbus_message_get_interface(message),
870 dbus_message_get_member(message),
871 dbus_message_get_path(message));
873 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
874 log_error("Warning! D-Bus connection terminated.");
875 dbus_connection_close(connection);
877 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
880 dbus_bool_t success = true;
882 if (!dbus_message_get_args(message, &error,
883 DBUS_TYPE_UINT32, &id,
884 DBUS_TYPE_OBJECT_PATH, &path,
885 DBUS_TYPE_BOOLEAN, &success,
887 log_error("Failed to parse message: %s", error.message);
891 if ((p = set_remove(d->set, (char*) path)))
899 dbus_error_free(&error);
900 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
903 static int enable_wait_for_jobs(DBusConnection *bus) {
911 dbus_error_init(&error);
912 dbus_bus_add_match(bus,
914 "sender='org.freedesktop.systemd1',"
915 "interface='org.freedesktop.systemd1.Manager',"
916 "member='JobRemoved',"
917 "path='/org/freedesktop/systemd1'",
920 if (dbus_error_is_set(&error)) {
921 log_error("Failed to add match: %s", error.message);
922 dbus_error_free(&error);
926 /* This is slightly dirty, since we don't undo the match registrations. */
930 static int wait_for_jobs(DBusConnection *bus, Set *s) {
941 if (!dbus_connection_add_filter(bus, wait_filter, &d, NULL)) {
942 log_error("Failed to add filter.");
947 while (!set_isempty(s) &&
948 dbus_connection_read_write_dispatch(bus, -1))
951 if (!arg_quiet && d.failed)
952 log_error("Job failed, see logs for details.");
954 r = d.failed ? -EIO : 0;
957 /* This is slightly dirty, since we don't undo the filter registration. */
962 static int start_unit_one(
969 DBusMessage *m = NULL, *reply = NULL;
978 assert(arg_no_block || s);
980 dbus_error_init(&error);
982 if (!(m = dbus_message_new_method_call(
983 "org.freedesktop.systemd1",
984 "/org/freedesktop/systemd1",
985 "org.freedesktop.systemd1.Manager",
987 log_error("Could not allocate message.");
992 if (!dbus_message_append_args(m,
993 DBUS_TYPE_STRING, &name,
994 DBUS_TYPE_STRING, &mode,
995 DBUS_TYPE_INVALID)) {
996 log_error("Could not append arguments to message.");
1001 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1003 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
1004 /* There's always a fallback possible for
1005 * legacy actions. */
1010 log_error("Failed to issue method call: %s", error.message);
1015 if (!dbus_message_get_args(reply, &error,
1016 DBUS_TYPE_OBJECT_PATH, &path,
1017 DBUS_TYPE_INVALID)) {
1018 log_error("Failed to parse reply: %s", error.message);
1023 if (need_daemon_reload(bus, name))
1024 log_warning("Unit file of created job changed on disk, 'systemctl %s daemon-reload' recommended.",
1025 arg_session ? "--session" : "--system");
1027 if (!arg_no_block) {
1030 if (!(p = strdup(path))) {
1031 log_error("Failed to duplicate path.");
1036 if ((r = set_put(s, p)) < 0) {
1038 log_error("Failed to add path to set.");
1047 dbus_message_unref(m);
1050 dbus_message_unref(reply);
1052 dbus_error_free(&error);
1057 static enum action verb_to_action(const char *verb) {
1058 if (streq(verb, "halt"))
1060 else if (streq(verb, "poweroff"))
1061 return ACTION_POWEROFF;
1062 else if (streq(verb, "reboot"))
1063 return ACTION_REBOOT;
1064 else if (streq(verb, "rescue"))
1065 return ACTION_RESCUE;
1066 else if (streq(verb, "emergency"))
1067 return ACTION_EMERGENCY;
1068 else if (streq(verb, "default"))
1069 return ACTION_DEFAULT;
1071 return ACTION_INVALID;
1074 static int start_unit(DBusConnection *bus, char **args, unsigned n) {
1076 static const char * const table[_ACTION_MAX] = {
1077 [ACTION_HALT] = SPECIAL_HALT_TARGET,
1078 [ACTION_POWEROFF] = SPECIAL_POWEROFF_TARGET,
1079 [ACTION_REBOOT] = SPECIAL_REBOOT_TARGET,
1080 [ACTION_RUNLEVEL2] = SPECIAL_RUNLEVEL2_TARGET,
1081 [ACTION_RUNLEVEL3] = SPECIAL_RUNLEVEL3_TARGET,
1082 [ACTION_RUNLEVEL4] = SPECIAL_RUNLEVEL4_TARGET,
1083 [ACTION_RUNLEVEL5] = SPECIAL_RUNLEVEL5_TARGET,
1084 [ACTION_RESCUE] = SPECIAL_RESCUE_TARGET,
1085 [ACTION_EMERGENCY] = SPECIAL_EMERGENCY_TARGET,
1086 [ACTION_DEFAULT] = SPECIAL_DEFAULT_TARGET
1091 const char *method, *mode, *one_name;
1096 if (arg_action == ACTION_SYSTEMCTL) {
1098 streq(args[0], "stop") ? "StopUnit" :
1099 streq(args[0], "reload") ? "ReloadUnit" :
1100 streq(args[0], "restart") ? "RestartUnit" :
1101 streq(args[0], "try-restart") ? "TryRestartUnit" :
1102 streq(args[0], "reload-or-restart") ? "ReloadOrRestartUnit" :
1103 streq(args[0], "reload-or-try-restart") ||
1104 streq(args[0], "force-reload") ||
1105 streq(args[0], "condrestart") ? "ReloadOrTryRestartUnit" :
1109 (streq(args[0], "isolate") ||
1110 streq(args[0], "rescue") ||
1111 streq(args[0], "emergency")) ? "isolate" :
1115 one_name = table[verb_to_action(args[0])];
1118 assert(arg_action < ELEMENTSOF(table));
1119 assert(table[arg_action]);
1121 method = "StartUnit";
1123 mode = (arg_action == ACTION_EMERGENCY ||
1124 arg_action == ACTION_RESCUE) ? "isolate" : "replace";
1126 one_name = table[arg_action];
1129 if (!arg_no_block) {
1130 if ((r = enable_wait_for_jobs(bus)) < 0) {
1131 log_error("Could not watch jobs: %s", strerror(-r));
1135 if (!(s = set_new(string_hash_func, string_compare_func))) {
1136 log_error("Failed to allocate set.");
1145 if ((r = start_unit_one(bus, method, one_name, mode, s)) <= 0)
1148 for (i = 1; i < n; i++)
1149 if ((r = start_unit_one(bus, method, args[i], mode, s)) < 0)
1154 r = wait_for_jobs(bus, s);
1163 static int start_special(DBusConnection *bus, char **args, unsigned n) {
1169 r = start_unit(bus, args, n);
1172 warn_wall(verb_to_action(args[0]));
1177 static int check_unit(DBusConnection *bus, char **args, unsigned n) {
1178 DBusMessage *m = NULL, *reply = NULL;
1180 *interface = "org.freedesktop.systemd1.Unit",
1181 *property = "ActiveState";
1182 int r = -EADDRNOTAVAIL;
1189 dbus_error_init(&error);
1191 for (i = 1; i < n; i++) {
1192 const char *path = NULL;
1194 DBusMessageIter iter, sub;
1196 if (!(m = dbus_message_new_method_call(
1197 "org.freedesktop.systemd1",
1198 "/org/freedesktop/systemd1",
1199 "org.freedesktop.systemd1.Manager",
1201 log_error("Could not allocate message.");
1206 if (!dbus_message_append_args(m,
1207 DBUS_TYPE_STRING, &args[i],
1208 DBUS_TYPE_INVALID)) {
1209 log_error("Could not append arguments to message.");
1214 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1216 /* Hmm, cannot figure out anything about this unit... */
1220 dbus_error_free(&error);
1224 if (!dbus_message_get_args(reply, &error,
1225 DBUS_TYPE_OBJECT_PATH, &path,
1226 DBUS_TYPE_INVALID)) {
1227 log_error("Failed to parse reply: %s", error.message);
1232 dbus_message_unref(m);
1233 if (!(m = dbus_message_new_method_call(
1234 "org.freedesktop.systemd1",
1236 "org.freedesktop.DBus.Properties",
1238 log_error("Could not allocate message.");
1243 if (!dbus_message_append_args(m,
1244 DBUS_TYPE_STRING, &interface,
1245 DBUS_TYPE_STRING, &property,
1246 DBUS_TYPE_INVALID)) {
1247 log_error("Could not append arguments to message.");
1252 dbus_message_unref(reply);
1253 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1254 log_error("Failed to issue method call: %s", error.message);
1259 if (!dbus_message_iter_init(reply, &iter) ||
1260 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1261 log_error("Failed to parse reply.");
1266 dbus_message_iter_recurse(&iter, &sub);
1268 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1269 log_error("Failed to parse reply.");
1274 dbus_message_iter_get_basic(&sub, &state);
1279 if (streq(state, "active") || startswith(state, "reloading"))
1282 dbus_message_unref(m);
1283 dbus_message_unref(reply);
1289 dbus_message_unref(m);
1292 dbus_message_unref(reply);
1294 dbus_error_free(&error);
1299 typedef struct ExecStatusInfo {
1305 usec_t start_timestamp;
1306 usec_t exit_timestamp;
1311 LIST_FIELDS(struct ExecStatusInfo, exec);
1314 static void exec_status_info_free(ExecStatusInfo *i) {
1322 static int exec_status_info_deserialize(DBusMessageIter *sub, ExecStatusInfo *i) {
1323 uint64_t start_timestamp, exit_timestamp;
1324 DBusMessageIter sub2, sub3;
1328 int32_t code, status;
1334 if (dbus_message_iter_get_arg_type(sub) != DBUS_TYPE_STRUCT)
1337 dbus_message_iter_recurse(sub, &sub2);
1339 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
1342 if (!(i->path = strdup(path)))
1345 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
1346 dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
1350 dbus_message_iter_recurse(&sub2, &sub3);
1351 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1352 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1353 dbus_message_iter_next(&sub3);
1358 if (!(i->argv = new0(char*, n+1)))
1362 dbus_message_iter_recurse(&sub2, &sub3);
1363 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1366 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1367 dbus_message_iter_get_basic(&sub3, &s);
1368 dbus_message_iter_next(&sub3);
1370 if (!(i->argv[n++] = strdup(s)))
1374 if (!dbus_message_iter_next(&sub2) ||
1375 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, true) < 0 ||
1376 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp, true) < 0 ||
1377 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp, true) < 0 ||
1378 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) < 0 ||
1379 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) < 0 ||
1380 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) < 0)
1384 i->start_timestamp = (usec_t) start_timestamp;
1385 i->exit_timestamp = (usec_t) exit_timestamp;
1386 i->pid = (pid_t) pid;
1393 typedef struct UnitStatusInfo {
1395 const char *load_state;
1396 const char *active_state;
1397 const char *sub_state;
1399 const char *description;
1401 const char *fragment_path;
1402 const char *default_control_group;
1404 bool need_daemon_reload;
1409 const char *status_text;
1412 usec_t start_timestamp;
1413 usec_t exit_timestamp;
1415 int exit_code, exit_status;
1418 unsigned n_accepted;
1419 unsigned n_connections;
1423 const char *sysfs_path;
1425 /* Mount, Automount */
1431 LIST_HEAD(ExecStatusInfo, exec);
1434 static void print_status_info(UnitStatusInfo *i) {
1439 /* This shows pretty information about a unit. See
1440 * print_property() for a low-level property printer */
1442 printf("%s", strna(i->id));
1444 if (i->description && !streq_ptr(i->id, i->description))
1445 printf(" - %s", i->description);
1449 if (i->fragment_path)
1450 printf("\t Loaded: %s (%s)\n", strna(i->load_state), i->fragment_path);
1451 else if (streq_ptr(i->load_state, "failed"))
1452 printf("\t Loaded: %s%s%s\n",
1453 ansi_highlight(true),
1454 strna(i->load_state),
1455 ansi_highlight(false));
1457 printf("\t Loaded: %s\n", strna(i->load_state));
1459 if (streq_ptr(i->active_state, "maintenance")) {
1460 if (streq_ptr(i->active_state, i->sub_state))
1461 printf("\t Active: %s%s%s\n",
1462 ansi_highlight(true),
1463 strna(i->active_state),
1464 ansi_highlight(false));
1466 printf("\t Active: %s%s (%s)%s\n",
1467 ansi_highlight(true),
1468 strna(i->active_state),
1469 strna(i->sub_state),
1470 ansi_highlight(false));
1472 if (streq_ptr(i->active_state, i->sub_state))
1473 printf("\t Active: %s\n",
1474 strna(i->active_state));
1476 printf("\t Active: %s (%s)\n",
1477 strna(i->active_state),
1478 strna(i->sub_state));
1482 printf("\t Device: %s\n", i->sysfs_path);
1484 printf("\t Where: %s\n", i->where);
1486 printf("\t What: %s\n", i->what);
1489 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
1491 LIST_FOREACH(exec, p, i->exec) {
1494 /* Only show exited processes here */
1498 t = strv_join(p->argv, " ");
1499 printf("\t Exited: %u (%s, code=%s, ", p->pid, strna(t), sigchld_code_to_string(p->code));
1502 if (p->code == CLD_EXITED)
1503 printf("status=%i", p->status);
1505 printf("signal=%s", signal_to_string(p->status));
1508 if (i->main_pid == p->pid &&
1509 i->start_timestamp == p->start_timestamp &&
1510 i->exit_timestamp == p->start_timestamp)
1511 /* Let's not show this twice */
1514 if (p->pid == i->control_pid)
1518 if (i->main_pid > 0 || i->control_pid > 0) {
1521 if (i->main_pid > 0) {
1522 printf(" Main: %u", (unsigned) i->main_pid);
1526 get_process_name(i->main_pid, &t);
1532 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
1534 if (i->exit_code == CLD_EXITED)
1535 printf("status=%i", i->exit_status);
1537 printf("signal=%s", signal_to_string(i->exit_status));
1541 if (i->main_pid > 0 && i->control_pid > 0)
1544 if (i->control_pid > 0) {
1547 printf(" Control: %u", (unsigned) i->control_pid);
1549 get_process_name(i->control_pid, &t);
1560 printf("\t Status: \"%s\"\n", i->status_text);
1562 if (i->default_control_group) {
1565 printf("\t CGroup: %s\n", i->default_control_group);
1567 if ((c = columns()) > 18)
1572 show_cgroup_by_path(i->default_control_group, "\t\t ", c);
1575 if (i->need_daemon_reload)
1576 printf("\n%sWarning:%s Unit file changed on disk, 'systemctl %s daemon-reload' recommended.\n",
1577 ansi_highlight(true),
1578 ansi_highlight(false),
1579 arg_session ? "--session" : "--system");
1582 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
1584 switch (dbus_message_iter_get_arg_type(iter)) {
1586 case DBUS_TYPE_STRING: {
1589 dbus_message_iter_get_basic(iter, &s);
1592 if (streq(name, "Id"))
1594 else if (streq(name, "LoadState"))
1596 else if (streq(name, "ActiveState"))
1597 i->active_state = s;
1598 else if (streq(name, "SubState"))
1600 else if (streq(name, "Description"))
1602 else if (streq(name, "FragmentPath"))
1603 i->fragment_path = s;
1604 else if (streq(name, "DefaultControlGroup"))
1605 i->default_control_group = s;
1606 else if (streq(name, "StatusText"))
1608 else if (streq(name, "SysFSPath"))
1610 else if (streq(name, "Where"))
1612 else if (streq(name, "What"))
1619 case DBUS_TYPE_BOOLEAN: {
1622 dbus_message_iter_get_basic(iter, &b);
1624 if (streq(name, "Accept"))
1626 else if (streq(name, "NeedDaemonReload"))
1627 i->need_daemon_reload = b;
1632 case DBUS_TYPE_UINT32: {
1635 dbus_message_iter_get_basic(iter, &u);
1637 if (streq(name, "MainPID")) {
1639 i->main_pid = (pid_t) u;
1642 } else if (streq(name, "ControlPID"))
1643 i->control_pid = (pid_t) u;
1644 else if (streq(name, "ExecMainPID")) {
1646 i->main_pid = (pid_t) u;
1647 } else if (streq(name, "NAccepted"))
1649 else if (streq(name, "NConnections"))
1650 i->n_connections = u;
1655 case DBUS_TYPE_INT32: {
1658 dbus_message_iter_get_basic(iter, &j);
1660 if (streq(name, "ExecMainCode"))
1661 i->exit_code = (int) j;
1662 else if (streq(name, "ExecMainStatus"))
1663 i->exit_status = (int) j;
1668 case DBUS_TYPE_UINT64: {
1671 dbus_message_iter_get_basic(iter, &u);
1673 if (streq(name, "ExecMainStartTimestamp"))
1674 i->start_timestamp = (usec_t) u;
1675 else if (streq(name, "ExecMainExitTimestamp"))
1676 i->exit_timestamp = (usec_t) u;
1681 case DBUS_TYPE_ARRAY: {
1683 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT &&
1684 startswith(name, "Exec")) {
1685 DBusMessageIter sub;
1687 dbus_message_iter_recurse(iter, &sub);
1688 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1689 ExecStatusInfo *info;
1692 if (!(info = new0(ExecStatusInfo, 1)))
1695 if ((r = exec_status_info_deserialize(&sub, info)) < 0) {
1700 LIST_PREPEND(ExecStatusInfo, exec, i->exec, info);
1702 dbus_message_iter_next(&sub);
1713 static int print_property(const char *name, DBusMessageIter *iter) {
1717 /* This is a low-level property printer, see
1718 * print_status_info() for the nicer output */
1720 if (arg_property && !strv_find(arg_property, name))
1723 switch (dbus_message_iter_get_arg_type(iter)) {
1725 case DBUS_TYPE_STRING: {
1727 dbus_message_iter_get_basic(iter, &s);
1729 if (arg_all || s[0])
1730 printf("%s=%s\n", name, s);
1735 case DBUS_TYPE_BOOLEAN: {
1737 dbus_message_iter_get_basic(iter, &b);
1738 printf("%s=%s\n", name, yes_no(b));
1743 case DBUS_TYPE_UINT64: {
1745 dbus_message_iter_get_basic(iter, &u);
1747 /* Yes, heuristics! But we can change this check
1748 * should it turn out to not be sufficient */
1750 if (strstr(name, "Timestamp")) {
1751 char timestamp[FORMAT_TIMESTAMP_MAX], *t;
1753 if ((t = format_timestamp(timestamp, sizeof(timestamp), u)) || arg_all)
1754 printf("%s=%s\n", name, strempty(t));
1755 } else if (strstr(name, "USec")) {
1756 char timespan[FORMAT_TIMESPAN_MAX];
1758 printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u));
1760 printf("%s=%llu\n", name, (unsigned long long) u);
1765 case DBUS_TYPE_UINT32: {
1767 dbus_message_iter_get_basic(iter, &u);
1769 if (strstr(name, "UMask") || strstr(name, "Mode"))
1770 printf("%s=%04o\n", name, u);
1772 printf("%s=%u\n", name, (unsigned) u);
1777 case DBUS_TYPE_INT32: {
1779 dbus_message_iter_get_basic(iter, &i);
1781 printf("%s=%i\n", name, (int) i);
1785 case DBUS_TYPE_STRUCT: {
1786 DBusMessageIter sub;
1787 dbus_message_iter_recurse(iter, &sub);
1789 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
1792 dbus_message_iter_get_basic(&sub, &u);
1795 printf("%s=%u\n", name, (unsigned) u);
1797 printf("%s=\n", name);
1800 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
1803 dbus_message_iter_get_basic(&sub, &s);
1805 if (arg_all || s[0])
1806 printf("%s=%s\n", name, s);
1814 case DBUS_TYPE_ARRAY:
1816 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
1817 DBusMessageIter sub;
1820 dbus_message_iter_recurse(iter, &sub);
1822 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1823 printf("%s=", name);
1825 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1828 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
1829 dbus_message_iter_get_basic(&sub, &s);
1830 printf("%s%s", space ? " " : "", s);
1833 dbus_message_iter_next(&sub);
1841 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_BYTE) {
1842 DBusMessageIter sub;
1844 dbus_message_iter_recurse(iter, &sub);
1846 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1847 printf("%s=", name);
1849 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1852 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_BYTE);
1853 dbus_message_iter_get_basic(&sub, &u);
1856 dbus_message_iter_next(&sub);
1864 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
1865 DBusMessageIter sub, sub2;
1867 dbus_message_iter_recurse(iter, &sub);
1868 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1869 const char *type, *path;
1871 dbus_message_iter_recurse(&sub, &sub2);
1873 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
1874 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
1875 printf("%s=%s\n", type, path);
1877 dbus_message_iter_next(&sub);
1882 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
1883 DBusMessageIter sub, sub2;
1885 dbus_message_iter_recurse(iter, &sub);
1886 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1888 uint64_t value, next_elapse;
1890 dbus_message_iter_recurse(&sub, &sub2);
1892 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
1893 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
1894 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
1895 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
1897 printf("%s={ value=%s ; next_elapse=%s }\n",
1899 format_timespan(timespan1, sizeof(timespan1), value),
1900 format_timespan(timespan2, sizeof(timespan2), next_elapse));
1903 dbus_message_iter_next(&sub);
1908 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
1909 DBusMessageIter sub;
1911 dbus_message_iter_recurse(iter, &sub);
1912 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1913 ExecStatusInfo info;
1916 if (exec_status_info_deserialize(&sub, &info) >= 0) {
1917 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
1920 t = strv_join(info.argv, " ");
1922 printf("%s={ path=%s ; argv[]=%s ; ignore=%s ; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n",
1926 yes_no(info.ignore),
1927 strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)),
1928 strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)),
1929 (unsigned) info. pid,
1930 sigchld_code_to_string(info.code),
1932 info.code == CLD_EXITED ? "" : "/",
1933 strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status)));
1939 strv_free(info.argv);
1941 dbus_message_iter_next(&sub);
1951 printf("%s=[unprintable]\n", name);
1956 static int show_one(DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
1957 DBusMessage *m = NULL, *reply = NULL;
1958 const char *interface = "";
1961 DBusMessageIter iter, sub, sub2, sub3;
1962 UnitStatusInfo info;
1970 dbus_error_init(&error);
1972 if (!(m = dbus_message_new_method_call(
1973 "org.freedesktop.systemd1",
1975 "org.freedesktop.DBus.Properties",
1977 log_error("Could not allocate message.");
1982 if (!dbus_message_append_args(m,
1983 DBUS_TYPE_STRING, &interface,
1984 DBUS_TYPE_INVALID)) {
1985 log_error("Could not append arguments to message.");
1990 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1991 log_error("Failed to issue method call: %s", error.message);
1996 if (!dbus_message_iter_init(reply, &iter) ||
1997 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
1998 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
1999 log_error("Failed to parse reply.");
2004 dbus_message_iter_recurse(&iter, &sub);
2011 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2014 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
2015 log_error("Failed to parse reply.");
2020 dbus_message_iter_recurse(&sub, &sub2);
2022 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
2023 log_error("Failed to parse reply.");
2028 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
2029 log_error("Failed to parse reply.");
2034 dbus_message_iter_recurse(&sub2, &sub3);
2036 if (show_properties)
2037 r = print_property(name, &sub3);
2039 r = status_property(name, &sub3, &info);
2042 log_error("Failed to parse reply.");
2047 dbus_message_iter_next(&sub);
2050 if (!show_properties)
2051 print_status_info(&info);
2053 while ((p = info.exec)) {
2054 LIST_REMOVE(ExecStatusInfo, exec, info.exec, p);
2055 exec_status_info_free(p);
2062 dbus_message_unref(m);
2065 dbus_message_unref(reply);
2067 dbus_error_free(&error);
2072 static int show(DBusConnection *bus, char **args, unsigned n) {
2073 DBusMessage *m = NULL, *reply = NULL;
2077 bool show_properties, new_line = false;
2082 dbus_error_init(&error);
2084 show_properties = !streq(args[0], "status");
2086 if (show_properties && n <= 1) {
2087 /* If not argument is specified inspect the manager
2090 r = show_one(bus, "/org/freedesktop/systemd1", show_properties, &new_line);
2094 for (i = 1; i < n; i++) {
2095 const char *path = NULL;
2098 if (!show_properties || safe_atou32(args[i], &id) < 0) {
2100 if (!(m = dbus_message_new_method_call(
2101 "org.freedesktop.systemd1",
2102 "/org/freedesktop/systemd1",
2103 "org.freedesktop.systemd1.Manager",
2105 log_error("Could not allocate message.");
2110 if (!dbus_message_append_args(m,
2111 DBUS_TYPE_STRING, &args[i],
2112 DBUS_TYPE_INVALID)) {
2113 log_error("Could not append arguments to message.");
2118 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2120 if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
2121 log_error("Failed to issue method call: %s", error.message);
2126 dbus_error_free(&error);
2128 dbus_message_unref(m);
2129 if (!(m = dbus_message_new_method_call(
2130 "org.freedesktop.systemd1",
2131 "/org/freedesktop/systemd1",
2132 "org.freedesktop.systemd1.Manager",
2134 log_error("Could not allocate message.");
2139 if (!dbus_message_append_args(m,
2140 DBUS_TYPE_STRING, &args[i],
2141 DBUS_TYPE_INVALID)) {
2142 log_error("Could not append arguments to message.");
2147 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2148 log_error("Failed to issue method call: %s", error.message);
2156 if (!(m = dbus_message_new_method_call(
2157 "org.freedesktop.systemd1",
2158 "/org/freedesktop/systemd1",
2159 "org.freedesktop.systemd1.Manager",
2161 log_error("Could not allocate message.");
2166 if (!dbus_message_append_args(m,
2167 DBUS_TYPE_UINT32, &id,
2168 DBUS_TYPE_INVALID)) {
2169 log_error("Could not append arguments to message.");
2174 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2175 log_error("Failed to issue method call: %s", error.message);
2181 if (!dbus_message_get_args(reply, &error,
2182 DBUS_TYPE_OBJECT_PATH, &path,
2183 DBUS_TYPE_INVALID)) {
2184 log_error("Failed to parse reply: %s", error.message);
2189 if ((r = show_one(bus, path, show_properties, &new_line)) < 0)
2192 dbus_message_unref(m);
2193 dbus_message_unref(reply);
2201 dbus_message_unref(m);
2204 dbus_message_unref(reply);
2206 dbus_error_free(&error);
2211 static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
2213 DBusMessage *m = NULL, *reply = NULL;
2218 dbus_error_init(&error);
2220 log_debug("Got D-Bus request: %s.%s() on %s",
2221 dbus_message_get_interface(message),
2222 dbus_message_get_member(message),
2223 dbus_message_get_path(message));
2225 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
2226 log_error("Warning! D-Bus connection terminated.");
2227 dbus_connection_close(connection);
2229 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") ||
2230 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
2231 const char *id, *path;
2233 if (!dbus_message_get_args(message, &error,
2234 DBUS_TYPE_STRING, &id,
2235 DBUS_TYPE_OBJECT_PATH, &path,
2237 log_error("Failed to parse message: %s", error.message);
2238 else if (streq(dbus_message_get_member(message), "UnitNew"))
2239 printf("Unit %s added.\n", id);
2241 printf("Unit %s removed.\n", id);
2243 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew") ||
2244 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
2248 if (!dbus_message_get_args(message, &error,
2249 DBUS_TYPE_UINT32, &id,
2250 DBUS_TYPE_OBJECT_PATH, &path,
2252 log_error("Failed to parse message: %s", error.message);
2253 else if (streq(dbus_message_get_member(message), "JobNew"))
2254 printf("Job %u added.\n", id);
2256 printf("Job %u removed.\n", id);
2259 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Unit", "Changed") ||
2260 dbus_message_is_signal(message, "org.freedesktop.systemd1.Job", "Changed")) {
2262 const char *path, *interface, *property = "Id";
2263 DBusMessageIter iter, sub;
2265 path = dbus_message_get_path(message);
2266 interface = dbus_message_get_interface(message);
2268 if (!(m = dbus_message_new_method_call(
2269 "org.freedesktop.systemd1",
2271 "org.freedesktop.DBus.Properties",
2273 log_error("Could not allocate message.");
2277 if (!dbus_message_append_args(m,
2278 DBUS_TYPE_STRING, &interface,
2279 DBUS_TYPE_STRING, &property,
2280 DBUS_TYPE_INVALID)) {
2281 log_error("Could not append arguments to message.");
2285 if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
2286 log_error("Failed to issue method call: %s", error.message);
2290 if (!dbus_message_iter_init(reply, &iter) ||
2291 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2292 log_error("Failed to parse reply.");
2296 dbus_message_iter_recurse(&iter, &sub);
2298 if (streq(interface, "org.freedesktop.systemd1.Unit")) {
2301 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2302 log_error("Failed to parse reply.");
2306 dbus_message_iter_get_basic(&sub, &id);
2307 printf("Unit %s changed.\n", id);
2311 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32) {
2312 log_error("Failed to parse reply.");
2316 dbus_message_iter_get_basic(&sub, &id);
2317 printf("Job %u changed.\n", id);
2323 dbus_message_unref(m);
2326 dbus_message_unref(reply);
2328 dbus_error_free(&error);
2329 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2333 dbus_message_unref(m);
2336 dbus_message_unref(reply);
2338 dbus_error_free(&error);
2339 return DBUS_HANDLER_RESULT_NEED_MEMORY;
2342 static int monitor(DBusConnection *bus, char **args, unsigned n) {
2343 DBusMessage *m = NULL, *reply = NULL;
2347 dbus_error_init(&error);
2350 dbus_bus_add_match(bus,
2352 "sender='org.freedesktop.systemd1',"
2353 "interface='org.freedesktop.systemd1.Manager',"
2354 "path='/org/freedesktop/systemd1'",
2357 if (dbus_error_is_set(&error)) {
2358 log_error("Failed to add match: %s", error.message);
2363 dbus_bus_add_match(bus,
2365 "sender='org.freedesktop.systemd1',"
2366 "interface='org.freedesktop.systemd1.Unit',"
2370 if (dbus_error_is_set(&error)) {
2371 log_error("Failed to add match: %s", error.message);
2376 dbus_bus_add_match(bus,
2378 "sender='org.freedesktop.systemd1',"
2379 "interface='org.freedesktop.systemd1.Job',"
2383 if (dbus_error_is_set(&error)) {
2384 log_error("Failed to add match: %s", error.message);
2390 if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
2391 log_error("Failed to add filter.");
2396 if (!(m = dbus_message_new_method_call(
2397 "org.freedesktop.systemd1",
2398 "/org/freedesktop/systemd1",
2399 "org.freedesktop.systemd1.Manager",
2401 log_error("Could not allocate message.");
2406 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2407 log_error("Failed to issue method call: %s", error.message);
2412 while (dbus_connection_read_write_dispatch(bus, -1))
2419 /* This is slightly dirty, since we don't undo the filter or the matches. */
2422 dbus_message_unref(m);
2425 dbus_message_unref(reply);
2427 dbus_error_free(&error);
2432 static int dump(DBusConnection *bus, char **args, unsigned n) {
2433 DBusMessage *m = NULL, *reply = NULL;
2438 dbus_error_init(&error);
2440 if (!(m = dbus_message_new_method_call(
2441 "org.freedesktop.systemd1",
2442 "/org/freedesktop/systemd1",
2443 "org.freedesktop.systemd1.Manager",
2445 log_error("Could not allocate message.");
2449 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2450 log_error("Failed to issue method call: %s", error.message);
2455 if (!dbus_message_get_args(reply, &error,
2456 DBUS_TYPE_STRING, &text,
2457 DBUS_TYPE_INVALID)) {
2458 log_error("Failed to parse reply: %s", error.message);
2463 fputs(text, stdout);
2469 dbus_message_unref(m);
2472 dbus_message_unref(reply);
2474 dbus_error_free(&error);
2479 static int snapshot(DBusConnection *bus, char **args, unsigned n) {
2480 DBusMessage *m = NULL, *reply = NULL;
2483 const char *name = "", *path, *id;
2484 dbus_bool_t cleanup = FALSE;
2485 DBusMessageIter iter, sub;
2487 *interface = "org.freedesktop.systemd1.Unit",
2490 dbus_error_init(&error);
2492 if (!(m = dbus_message_new_method_call(
2493 "org.freedesktop.systemd1",
2494 "/org/freedesktop/systemd1",
2495 "org.freedesktop.systemd1.Manager",
2496 "CreateSnapshot"))) {
2497 log_error("Could not allocate message.");
2504 if (!dbus_message_append_args(m,
2505 DBUS_TYPE_STRING, &name,
2506 DBUS_TYPE_BOOLEAN, &cleanup,
2507 DBUS_TYPE_INVALID)) {
2508 log_error("Could not append arguments to message.");
2513 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2514 log_error("Failed to issue method call: %s", error.message);
2519 if (!dbus_message_get_args(reply, &error,
2520 DBUS_TYPE_OBJECT_PATH, &path,
2521 DBUS_TYPE_INVALID)) {
2522 log_error("Failed to parse reply: %s", error.message);
2527 dbus_message_unref(m);
2528 if (!(m = dbus_message_new_method_call(
2529 "org.freedesktop.systemd1",
2531 "org.freedesktop.DBus.Properties",
2533 log_error("Could not allocate message.");
2537 if (!dbus_message_append_args(m,
2538 DBUS_TYPE_STRING, &interface,
2539 DBUS_TYPE_STRING, &property,
2540 DBUS_TYPE_INVALID)) {
2541 log_error("Could not append arguments to message.");
2546 dbus_message_unref(reply);
2547 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2548 log_error("Failed to issue method call: %s", error.message);
2553 if (!dbus_message_iter_init(reply, &iter) ||
2554 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2555 log_error("Failed to parse reply.");
2560 dbus_message_iter_recurse(&iter, &sub);
2562 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2563 log_error("Failed to parse reply.");
2568 dbus_message_iter_get_basic(&sub, &id);
2576 dbus_message_unref(m);
2579 dbus_message_unref(reply);
2581 dbus_error_free(&error);
2586 static int delete_snapshot(DBusConnection *bus, char **args, unsigned n) {
2587 DBusMessage *m = NULL, *reply = NULL;
2595 dbus_error_init(&error);
2597 for (i = 1; i < n; i++) {
2598 const char *path = NULL;
2600 if (!(m = dbus_message_new_method_call(
2601 "org.freedesktop.systemd1",
2602 "/org/freedesktop/systemd1",
2603 "org.freedesktop.systemd1.Manager",
2605 log_error("Could not allocate message.");
2610 if (!dbus_message_append_args(m,
2611 DBUS_TYPE_STRING, &args[i],
2612 DBUS_TYPE_INVALID)) {
2613 log_error("Could not append arguments to message.");
2618 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2619 log_error("Failed to issue method call: %s", error.message);
2624 if (!dbus_message_get_args(reply, &error,
2625 DBUS_TYPE_OBJECT_PATH, &path,
2626 DBUS_TYPE_INVALID)) {
2627 log_error("Failed to parse reply: %s", error.message);
2632 dbus_message_unref(m);
2633 if (!(m = dbus_message_new_method_call(
2634 "org.freedesktop.systemd1",
2636 "org.freedesktop.systemd1.Snapshot",
2638 log_error("Could not allocate message.");
2643 dbus_message_unref(reply);
2644 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2645 log_error("Failed to issue method call: %s", error.message);
2650 dbus_message_unref(m);
2651 dbus_message_unref(reply);
2659 dbus_message_unref(m);
2662 dbus_message_unref(reply);
2664 dbus_error_free(&error);
2669 static int daemon_reload(DBusConnection *bus, char **args, unsigned n) {
2670 DBusMessage *m = NULL, *reply = NULL;
2675 dbus_error_init(&error);
2677 if (arg_action == ACTION_RELOAD)
2679 else if (arg_action == ACTION_REEXEC)
2680 method = "Reexecute";
2682 assert(arg_action == ACTION_SYSTEMCTL);
2685 streq(args[0], "clear-jobs") ||
2686 streq(args[0], "cancel") ? "ClearJobs" :
2687 streq(args[0], "daemon-reexec") ? "Reexecute" :
2688 streq(args[0], "reset-maintenance") ? "ResetMaintenance" :
2689 streq(args[0], "daemon-exit") ? "Exit" :
2693 if (!(m = dbus_message_new_method_call(
2694 "org.freedesktop.systemd1",
2695 "/org/freedesktop/systemd1",
2696 "org.freedesktop.systemd1.Manager",
2698 log_error("Could not allocate message.");
2702 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2704 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
2705 /* There's always a fallback possible for
2706 * legacy actions. */
2711 log_error("Failed to issue method call: %s", error.message);
2720 dbus_message_unref(m);
2723 dbus_message_unref(reply);
2725 dbus_error_free(&error);
2730 static int reset_maintenance(DBusConnection *bus, char **args, unsigned n) {
2731 DBusMessage *m = NULL, *reply = NULL;
2737 dbus_error_init(&error);
2740 return daemon_reload(bus, args, n);
2742 for (i = 1; i < n; i++) {
2744 if (!(m = dbus_message_new_method_call(
2745 "org.freedesktop.systemd1",
2746 "/org/freedesktop/systemd1",
2747 "org.freedesktop.systemd1.Manager",
2748 "ResetMaintenanceUnit"))) {
2749 log_error("Could not allocate message.");
2754 if (!dbus_message_append_args(m,
2755 DBUS_TYPE_STRING, args + i,
2756 DBUS_TYPE_INVALID)) {
2757 log_error("Could not append arguments to message.");
2762 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2763 log_error("Failed to issue method call: %s", error.message);
2768 dbus_message_unref(m);
2769 dbus_message_unref(reply);
2777 dbus_message_unref(m);
2780 dbus_message_unref(reply);
2782 dbus_error_free(&error);
2787 static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
2788 DBusMessage *m = NULL, *reply = NULL;
2790 DBusMessageIter iter, sub, sub2;
2793 *interface = "org.freedesktop.systemd1.Manager",
2794 *property = "Environment";
2796 dbus_error_init(&error);
2798 if (!(m = dbus_message_new_method_call(
2799 "org.freedesktop.systemd1",
2800 "/org/freedesktop/systemd1",
2801 "org.freedesktop.DBus.Properties",
2803 log_error("Could not allocate message.");
2807 if (!dbus_message_append_args(m,
2808 DBUS_TYPE_STRING, &interface,
2809 DBUS_TYPE_STRING, &property,
2810 DBUS_TYPE_INVALID)) {
2811 log_error("Could not append arguments to message.");
2816 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2817 log_error("Failed to issue method call: %s", error.message);
2822 if (!dbus_message_iter_init(reply, &iter) ||
2823 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2824 log_error("Failed to parse reply.");
2829 dbus_message_iter_recurse(&iter, &sub);
2831 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
2832 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
2833 log_error("Failed to parse reply.");
2838 dbus_message_iter_recurse(&sub, &sub2);
2840 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
2843 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
2844 log_error("Failed to parse reply.");
2849 dbus_message_iter_get_basic(&sub2, &text);
2850 printf("%s\n", text);
2852 dbus_message_iter_next(&sub2);
2859 dbus_message_unref(m);
2862 dbus_message_unref(reply);
2864 dbus_error_free(&error);
2869 static int set_environment(DBusConnection *bus, char **args, unsigned n) {
2870 DBusMessage *m = NULL, *reply = NULL;
2874 DBusMessageIter iter, sub;
2877 dbus_error_init(&error);
2879 method = streq(args[0], "set-environment")
2881 : "UnsetEnvironment";
2883 if (!(m = dbus_message_new_method_call(
2884 "org.freedesktop.systemd1",
2885 "/org/freedesktop/systemd1",
2886 "org.freedesktop.systemd1.Manager",
2889 log_error("Could not allocate message.");
2893 dbus_message_iter_init_append(m, &iter);
2895 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
2896 log_error("Could not append arguments to message.");
2901 for (i = 1; i < n; i++)
2902 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) {
2903 log_error("Could not append arguments to message.");
2908 if (!dbus_message_iter_close_container(&iter, &sub)) {
2909 log_error("Could not append arguments to message.");
2914 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2915 log_error("Failed to issue method call: %s", error.message);
2924 dbus_message_unref(m);
2927 dbus_message_unref(reply);
2929 dbus_error_free(&error);
2942 static Hashmap *will_install = NULL, *have_installed = NULL;
2943 static Set *remove_symlinks_to = NULL;
2945 static void install_info_free(InstallInfo *i) {
2950 strv_free(i->aliases);
2951 strv_free(i->wanted_by);
2955 static void install_info_hashmap_free(Hashmap *m) {
2958 while ((i = hashmap_steal_first(m)))
2959 install_info_free(i);
2964 static bool unit_name_valid(const char *name) {
2966 /* This is a minimal version of unit_name_valid() from
2972 if (ignore_file(name))
2978 static int install_info_add(const char *name) {
2982 assert(will_install);
2984 if (!unit_name_valid(name))
2987 if (hashmap_get(have_installed, name) ||
2988 hashmap_get(will_install, name))
2991 if (!(i = new0(InstallInfo, 1))) {
2996 if (!(i->name = strdup(name))) {
3001 if ((r = hashmap_put(will_install, i->name, i)) < 0)
3008 install_info_free(i);
3013 static int config_parse_also(
3014 const char *filename,
3016 const char *section,
3030 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
3034 if (!(n = strndup(w, l)))
3037 r = install_info_add(n);
3047 static int mark_symlink_for_removal(const char *p) {
3052 assert(path_is_absolute(p));
3054 if (!remove_symlinks_to)
3057 if (!(n = strdup(p)))
3060 path_kill_slashes(n);
3062 if ((r = set_put(remove_symlinks_to, n)) < 0) {
3064 return r == -EEXIST ? 0 : r;
3070 static int remove_marked_symlinks_fd(int fd, const char *config_path, const char *root, bool *deleted) {
3079 if (!(d = fdopendir(fd))) {
3080 close_nointr_nofail(fd);
3086 while ((de = readdir(d))) {
3087 bool is_dir = false, is_link = false;
3089 if (ignore_file(de->d_name))
3092 if (de->d_type == DT_LNK)
3094 else if (de->d_type == DT_DIR)
3096 else if (de->d_type == DT_UNKNOWN) {
3099 if (fstatat(fd, de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) {
3100 log_error("Failed to stat %s/%s: %m", root, de->d_name);
3107 is_link = S_ISLNK(st.st_mode);
3108 is_dir = S_ISDIR(st.st_mode);
3116 if ((nfd = openat(fd, de->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW)) < 0) {
3117 log_error("Failed to open %s/%s: %m", root, de->d_name);
3124 if (asprintf(&p, "%s/%s", root, de->d_name) < 0) {
3125 log_error("Failed to allocate directory string.");
3126 close_nointr_nofail(nfd);
3131 /* This will close nfd, regardless whether it succeeds or not */
3132 q = remove_marked_symlinks_fd(nfd, config_path, p, deleted);
3138 } else if (is_link) {
3142 if (asprintf(&p, "%s/%s", root, de->d_name) < 0) {
3143 log_error("Failed to allocate symlink string.");
3148 if ((q = readlink_and_make_absolute(p, &dest)) < 0) {
3149 log_error("Cannot read symlink %s: %s", p, strerror(-q));
3157 if ((c = canonicalize_file_name(dest))) {
3158 /* This might fail if the destination
3159 * is already removed */
3165 path_kill_slashes(dest);
3166 if (set_get(remove_symlinks_to, dest)) {
3169 log_info("rm '%s'", p);
3171 if (unlink(p) < 0) {
3172 log_error("Cannot unlink symlink %s: %m", p);
3177 rmdir_parents(p, config_path);
3178 path_kill_slashes(p);
3180 if (!set_get(remove_symlinks_to, p)) {
3182 if ((r = mark_symlink_for_removal(p)) < 0) {
3201 static int remove_marked_symlinks(const char *config_path) {
3205 assert(config_path);
3207 if (set_size(remove_symlinks_to) <= 0)
3210 if ((fd = open(config_path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW)) < 0)
3217 if ((cfd = dup(fd)) < 0) {
3222 /* This takes possession of cfd and closes it */
3223 if ((q = remove_marked_symlinks_fd(cfd, config_path, config_path, &deleted)) < 0) {
3229 close_nointr_nofail(fd);
3234 static int create_symlink(const char *verb, const char *old_path, const char *new_path) {
3241 if (streq(verb, "enable")) {
3244 mkdir_parents(new_path, 0755);
3246 if (symlink(old_path, new_path) >= 0) {
3249 log_info("ln -s '%s' '%s'", old_path, new_path);
3254 if (errno != EEXIST) {
3255 log_error("Cannot link %s to %s: %m", old_path, new_path);
3259 if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
3261 if (errno == EINVAL) {
3262 log_error("Cannot link %s to %s, file exists already and is not a symlink.", old_path, new_path);
3266 log_error("readlink() failed: %s", strerror(-r));
3270 if (streq(dest, old_path)) {
3276 log_error("Cannot link %s to %s, symlink exists already and points to %s.", old_path, new_path, dest);
3285 log_info("ln -s '%s' '%s'", old_path, new_path);
3287 if (symlink(old_path, new_path) >= 0)
3290 log_error("Cannot link %s to %s: %m", old_path, new_path);
3293 } else if (streq(verb, "disable")) {
3296 if ((r = mark_symlink_for_removal(old_path)) < 0)
3299 if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
3300 if (errno == ENOENT)
3303 if (errno == EINVAL) {
3304 log_warning("File %s not a symlink, ignoring.", old_path);
3308 log_error("readlink() failed: %s", strerror(-r));
3312 if (!streq(dest, old_path)) {
3313 log_warning("File %s not a symlink to %s but points to %s, ignoring.", new_path, old_path, dest);
3320 if ((r = mark_symlink_for_removal(new_path)) < 0)
3324 log_info("rm '%s'", new_path);
3326 if (unlink(new_path) >= 0)
3329 log_error("Cannot unlink %s: %m", new_path);
3332 } else if (streq(verb, "is-enabled")) {
3335 if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
3337 if (errno == ENOENT || errno == EINVAL)
3340 log_error("readlink() failed: %s", strerror(-r));
3344 if (streq(dest, old_path)) {
3352 assert_not_reached("Unknown action.");
3355 static int install_info_symlink_alias(const char *verb, InstallInfo *i, const char *config_path) {
3357 char *alias_path = NULL;
3362 assert(config_path);
3364 STRV_FOREACH(s, i->aliases) {
3366 if (!unit_name_valid(*s)) {
3367 log_error("Invalid name %s.", *s);
3373 if (!(alias_path = path_make_absolute(*s, config_path))) {
3374 log_error("Out of memory");
3379 if ((r = create_symlink(verb, i->path, alias_path)) != 0)
3382 if (streq(verb, "disable"))
3383 rmdir_parents(alias_path, config_path);
3394 static int install_info_symlink_wants(const char *verb, InstallInfo *i, const char *config_path) {
3396 char *alias_path = NULL;
3401 assert(config_path);
3403 STRV_FOREACH(s, i->wanted_by) {
3404 if (!unit_name_valid(*s)) {
3405 log_error("Invalid name %s.", *s);
3413 if (asprintf(&alias_path, "%s/%s.wants/%s", config_path, *s, i->name) < 0) {
3414 log_error("Out of memory");
3419 if ((r = create_symlink(verb, i->path, alias_path)) != 0)
3422 if (streq(verb, "disable"))
3423 rmdir_parents(alias_path, config_path);
3434 static int install_info_apply(const char *verb, LookupPaths *paths, InstallInfo *i, const char *config_path) {
3436 const ConfigItem items[] = {
3437 { "Alias", config_parse_strv, &i->aliases, "Install" },
3438 { "WantedBy", config_parse_strv, &i->wanted_by, "Install" },
3439 { "Also", config_parse_also, NULL, "Install" },
3441 { NULL, NULL, NULL, NULL }
3445 char *filename = NULL;
3452 STRV_FOREACH(p, paths->unit_path) {
3455 if (!(filename = path_make_absolute(i->name, *p))) {
3456 log_error("Out of memory");
3460 /* Ensure that we don't follow symlinks */
3461 if ((fd = open(filename, O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NOCTTY)) >= 0)
3462 if ((f = fdopen(fd, "re")))
3465 if (errno == ELOOP) {
3466 log_error("Refusing to operate on symlinks, please pass unit names or absolute paths to unit files.");
3471 if (errno != ENOENT) {
3472 log_error("Failed to open %s: %m", filename);
3482 log_error("Couldn't find %s.", i->name);
3488 if ((r = config_parse(filename, f, NULL, items, true, i)) < 0) {
3495 if ((r = install_info_symlink_alias(verb, i, config_path)) != 0)
3498 if ((r = install_info_symlink_wants(verb, i, config_path)) != 0)
3501 if ((r = mark_symlink_for_removal(filename)) < 0)
3504 if ((r = remove_marked_symlinks(config_path)) < 0)
3510 static char *get_config_path(void) {
3512 if (arg_session && arg_global)
3513 return strdup(SESSION_CONFIG_UNIT_PATH);
3518 if (session_config_home(&p) < 0)
3524 return strdup(SYSTEM_CONFIG_UNIT_PATH);
3527 static int enable_unit(DBusConnection *bus, char **args, unsigned n) {
3531 char *config_path = NULL;
3534 const char *verb = args[0];
3536 dbus_error_init(&error);
3539 if ((r = lookup_paths_init(&paths, arg_session ? MANAGER_SESSION : MANAGER_SYSTEM)) < 0) {
3540 log_error("Failed to determine lookup paths: %s", strerror(-r));
3544 if (!(config_path = get_config_path())) {
3545 log_error("Failed to determine config path");
3550 will_install = hashmap_new(string_hash_func, string_compare_func);
3551 have_installed = hashmap_new(string_hash_func, string_compare_func);
3553 if (!will_install || !have_installed) {
3554 log_error("Failed to allocate unit sets.");
3559 if (!arg_defaults && streq(verb, "disable"))
3560 if (!(remove_symlinks_to = set_new(string_hash_func, string_compare_func))) {
3561 log_error("Failed to allocate symlink sets.");
3566 for (j = 1; j < n; j++)
3567 if ((r = install_info_add(args[j])) < 0)
3570 while ((i = hashmap_first(will_install))) {
3573 assert_se(hashmap_move_one(have_installed, will_install, i->name) == 0);
3575 if ((q = install_info_apply(verb, &paths, i, config_path)) != 0) {
3583 /* In test mode and found something */
3589 if (streq(verb, "is-enabled"))
3590 r = r > 0 ? 0 : -ENOENT;
3592 /* Don't try to reload anything if the user asked us to not do this */
3594 /* Don't try to reload anything when updating a unit globally */
3596 /* Don't try to reload anything if we are called for system changes but the system wasn't booted with systemd */
3597 (arg_session || sd_booted() > 0) &&
3598 /* Don't try to reload anything if we are running in a chroot environment */
3599 (arg_session || running_in_chroot() <= 0) ) {
3602 if ((q = daemon_reload(bus, args, n)) < 0)
3607 install_info_hashmap_free(will_install);
3608 install_info_hashmap_free(have_installed);
3610 set_free_free(remove_symlinks_to);
3612 lookup_paths_free(&paths);
3619 static int systemctl_help(void) {
3621 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
3622 "Send control commands to or query the systemd manager.\n\n"
3623 " -h --help Show this help\n"
3624 " -t --type=TYPE List only units of a particular type\n"
3625 " -p --property=NAME Show only properties by this name\n"
3626 " -a --all Show all units/properties, including dead/empty ones\n"
3627 " --full Don't ellipsize unit names on output\n"
3628 " --fail When queueing a new job, fail if conflicting jobs are\n"
3630 " -q --quiet Suppress output\n"
3631 " --no-block Do not wait until operation finished\n"
3632 " --system Connect to system bus\n"
3633 " --session Connect to session bus\n"
3634 " --order When generating graph for dot, show only order\n"
3635 " --require When generating graph for dot, show only requirement\n"
3636 " --no-wall Don't send wall message before halt/power-off/reboot\n"
3637 " --global Enable/disable unit files globally\n"
3638 " --no-reload When enabling/disabling unit files, don't reload daemon\n"
3640 " --force When enabling unit files, override existing symlinks\n"
3641 " --defaults When disabling unit files, remove default symlinks only\n\n"
3643 " list-units List units\n"
3644 " start [NAME...] Start (activate) one or more units\n"
3645 " stop [NAME...] Stop (deactivate) one or more units\n"
3646 " reload [NAME...] Reload one or more units\n"
3647 " restart [NAME...] Start or restart one or more units\n"
3648 " try-restart [NAME...] Restart one or more units if active\n"
3649 " reload-or-restart [NAME...] Reload one or more units is possible,\n"
3650 " otherwise start or restart\n"
3651 " reload-or-try-restart [NAME...] Reload one or more units is possible,\n"
3652 " otherwise restart if active\n"
3653 " isolate [NAME] Start one unit and stop all others\n"
3654 " is-active [NAME...] Check whether units are active\n"
3655 " status [NAME...] Show runtime status of one or more units\n"
3656 " show [NAME...|JOB...] Show properties of one or more\n"
3657 " units/jobs or the manager\n"
3658 " reset-maintenance [NAME...] Reset maintenance state for all, one,\n"
3660 " enable [NAME...] Enable one or more unit files\n"
3661 " disable [NAME...] Disable one or more unit files\n"
3662 " is-enabled [NAME...] Check whether unit files are enabled\n"
3663 " load [NAME...] Load one or more units\n"
3664 " list-jobs List jobs\n"
3665 " cancel [JOB...] Cancel all, one, or more jobs\n"
3666 " monitor Monitor unit/job changes\n"
3667 " dump Dump server status\n"
3668 " dot Dump dependency graph for dot(1)\n"
3669 " snapshot [NAME] Create a snapshot\n"
3670 " delete [NAME...] Remove one or more snapshots\n"
3671 " daemon-reload Reload systemd manager configuration\n"
3672 " daemon-reexec Reexecute systemd manager\n"
3673 " daemon-exit Ask the systemd manager to quit\n"
3674 " show-environment Dump environment\n"
3675 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
3676 " unset-environment [NAME...] Unset one or more environment variables\n"
3677 " halt Shut down and halt the system\n"
3678 " poweroff Shut down and power-off the system\n"
3679 " reboot Shut down and reboot the system\n"
3680 " rescue Enter system rescue mode\n"
3681 " emergency Enter system emergency mode\n"
3682 " default Enter system default mode\n",
3683 program_invocation_short_name);
3688 static int halt_help(void) {
3690 printf("%s [OPTIONS...]\n\n"
3691 "%s the system.\n\n"
3692 " --help Show this help\n"
3693 " --halt Halt the machine\n"
3694 " -p --poweroff Switch off the machine\n"
3695 " --reboot Reboot the machine\n"
3696 " -f --force Force immediate halt/power-off/reboot\n"
3697 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
3698 " -d --no-wtmp Don't write wtmp record\n"
3699 " -n --no-sync Don't sync before halt/power-off/reboot\n"
3700 " --no-wall Don't send wall message before halt/power-off/reboot\n",
3701 program_invocation_short_name,
3702 arg_action == ACTION_REBOOT ? "Reboot" :
3703 arg_action == ACTION_POWEROFF ? "Power off" :
3709 static int shutdown_help(void) {
3711 printf("%s [OPTIONS...] [now] [WALL...]\n\n"
3712 "Shut down the system.\n\n"
3713 " --help Show this help\n"
3714 " -H --halt Halt the machine\n"
3715 " -P --poweroff Power-off the machine\n"
3716 " -r --reboot Reboot the machine\n"
3717 " -h Equivalent to --poweroff, overriden by --halt\n"
3718 " -k Don't halt/power-off/reboot, just send warnings\n"
3719 " --no-wall Don't send wall message before halt/power-off/reboot\n",
3720 program_invocation_short_name);
3725 static int telinit_help(void) {
3727 printf("%s [OPTIONS...] {COMMAND}\n\n"
3728 "Send control commands to the init daemon.\n\n"
3729 " --help Show this help\n"
3730 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
3732 " 0 Power-off the machine\n"
3733 " 6 Reboot the machine\n"
3734 " 2, 3, 4, 5 Start runlevelX.target unit\n"
3735 " 1, s, S Enter rescue mode\n"
3736 " q, Q Reload init daemon configuration\n"
3737 " u, U Reexecute init daemon\n",
3738 program_invocation_short_name);
3743 static int runlevel_help(void) {
3745 printf("%s [OPTIONS...]\n\n"
3746 "Prints the previous and current runlevel of the init system.\n\n"
3747 " --help Show this help\n",
3748 program_invocation_short_name);
3753 static int systemctl_parse_argv(int argc, char *argv[]) {
3770 static const struct option options[] = {
3771 { "help", no_argument, NULL, 'h' },
3772 { "type", required_argument, NULL, 't' },
3773 { "property", required_argument, NULL, 'p' },
3774 { "all", no_argument, NULL, 'a' },
3775 { "full", no_argument, NULL, ARG_FULL },
3776 { "fail", no_argument, NULL, ARG_FAIL },
3777 { "session", no_argument, NULL, ARG_SESSION },
3778 { "system", no_argument, NULL, ARG_SYSTEM },
3779 { "global", no_argument, NULL, ARG_GLOBAL },
3780 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
3781 { "no-wall", no_argument, NULL, ARG_NO_WALL },
3782 { "quiet", no_argument, NULL, 'q' },
3783 { "order", no_argument, NULL, ARG_ORDER },
3784 { "require", no_argument, NULL, ARG_REQUIRE },
3785 { "force", no_argument, NULL, ARG_FORCE },
3786 { "no-reload", no_argument, NULL, ARG_NO_RELOAD },
3787 { "defaults", no_argument, NULL, ARG_DEFAULTS },
3788 { NULL, 0, NULL, 0 }
3796 while ((c = getopt_long(argc, argv, "ht:p:aq", options, NULL)) >= 0) {
3811 if (!(l = strv_append(arg_property, optarg)))
3814 strv_free(arg_property);
3817 /* If the user asked for a particular
3818 * property, show it to him, even if it is
3837 arg_session = false;
3841 arg_no_block = true;
3849 arg_dot = DOT_ORDER;
3853 arg_dot = DOT_REQUIRE;
3869 arg_no_reload = true;
3878 arg_defaults = true;
3885 log_error("Unknown option code %c", c);
3893 static int halt_parse_argv(int argc, char *argv[]) {
3902 static const struct option options[] = {
3903 { "help", no_argument, NULL, ARG_HELP },
3904 { "halt", no_argument, NULL, ARG_HALT },
3905 { "poweroff", no_argument, NULL, 'p' },
3906 { "reboot", no_argument, NULL, ARG_REBOOT },
3907 { "force", no_argument, NULL, 'f' },
3908 { "wtmp-only", no_argument, NULL, 'w' },
3909 { "no-wtmp", no_argument, NULL, 'd' },
3910 { "no-sync", no_argument, NULL, 'n' },
3911 { "no-wall", no_argument, NULL, ARG_NO_WALL },
3912 { NULL, 0, NULL, 0 }
3920 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
3921 if (runlevel == '0' || runlevel == '6')
3922 arg_immediate = true;
3924 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
3932 arg_action = ACTION_HALT;
3936 arg_action = ACTION_POWEROFF;
3940 arg_action = ACTION_REBOOT;
3944 arg_immediate = true;
3965 /* Compatibility nops */
3972 log_error("Unknown option code %c", c);
3977 if (optind < argc) {
3978 log_error("Too many arguments.");
3985 static int shutdown_parse_argv(int argc, char *argv[]) {
3992 static const struct option options[] = {
3993 { "help", no_argument, NULL, ARG_HELP },
3994 { "halt", no_argument, NULL, 'H' },
3995 { "poweroff", no_argument, NULL, 'P' },
3996 { "reboot", no_argument, NULL, 'r' },
3997 { "no-wall", no_argument, NULL, ARG_NO_WALL },
3998 { NULL, 0, NULL, 0 }
4006 while ((c = getopt_long(argc, argv, "HPrhkt:a", options, NULL)) >= 0) {
4014 arg_action = ACTION_HALT;
4018 arg_action = ACTION_POWEROFF;
4022 arg_action = ACTION_REBOOT;
4026 if (arg_action != ACTION_HALT)
4027 arg_action = ACTION_POWEROFF;
4040 /* Compatibility nops */
4047 log_error("Unknown option code %c", c);
4052 if (argc > optind && !streq(argv[optind], "now"))
4053 log_warning("First argument '%s' isn't 'now'. Ignoring.", argv[optind]);
4055 /* We ignore the time argument */
4056 if (argc > optind + 1)
4057 arg_wall = argv + optind + 1;
4064 static int telinit_parse_argv(int argc, char *argv[]) {
4071 static const struct option options[] = {
4072 { "help", no_argument, NULL, ARG_HELP },
4073 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4074 { NULL, 0, NULL, 0 }
4077 static const struct {
4081 { '0', ACTION_POWEROFF },
4082 { '6', ACTION_REBOOT },
4083 { '1', ACTION_RESCUE },
4084 { '2', ACTION_RUNLEVEL2 },
4085 { '3', ACTION_RUNLEVEL3 },
4086 { '4', ACTION_RUNLEVEL4 },
4087 { '5', ACTION_RUNLEVEL5 },
4088 { 's', ACTION_RESCUE },
4089 { 'S', ACTION_RESCUE },
4090 { 'q', ACTION_RELOAD },
4091 { 'Q', ACTION_RELOAD },
4092 { 'u', ACTION_REEXEC },
4093 { 'U', ACTION_REEXEC }
4102 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4117 log_error("Unknown option code %c", c);
4122 if (optind >= argc) {
4127 if (optind + 1 < argc) {
4128 log_error("Too many arguments.");
4132 if (strlen(argv[optind]) != 1) {
4133 log_error("Expected single character argument.");
4137 for (i = 0; i < ELEMENTSOF(table); i++)
4138 if (table[i].from == argv[optind][0])
4141 if (i >= ELEMENTSOF(table)) {
4142 log_error("Unknown command %s.", argv[optind]);
4146 arg_action = table[i].to;
4153 static int runlevel_parse_argv(int argc, char *argv[]) {
4159 static const struct option options[] = {
4160 { "help", no_argument, NULL, ARG_HELP },
4161 { NULL, 0, NULL, 0 }
4169 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4180 log_error("Unknown option code %c", c);
4185 if (optind < argc) {
4186 log_error("Too many arguments.");
4193 static int parse_argv(int argc, char *argv[]) {
4197 if (program_invocation_short_name) {
4199 if (strstr(program_invocation_short_name, "halt")) {
4200 arg_action = ACTION_HALT;
4201 return halt_parse_argv(argc, argv);
4202 } else if (strstr(program_invocation_short_name, "poweroff")) {
4203 arg_action = ACTION_POWEROFF;
4204 return halt_parse_argv(argc, argv);
4205 } else if (strstr(program_invocation_short_name, "reboot")) {
4206 arg_action = ACTION_REBOOT;
4207 return halt_parse_argv(argc, argv);
4208 } else if (strstr(program_invocation_short_name, "shutdown")) {
4209 arg_action = ACTION_POWEROFF;
4210 return shutdown_parse_argv(argc, argv);
4211 } else if (strstr(program_invocation_short_name, "init")) {
4213 if (sd_booted() > 0) {
4214 arg_action = ACTION_INVALID;
4215 return telinit_parse_argv(argc, argv);
4217 /* Hmm, so some other init system is
4218 * running, we need to forward this
4219 * request to it. For now we simply
4220 * guess that it is Upstart. */
4222 execv("/lib/upstart/telinit", argv);
4224 log_error("Couldn't find an alternative telinit implementation to spawn.");
4228 } else if (strstr(program_invocation_short_name, "runlevel")) {
4229 arg_action = ACTION_RUNLEVEL;
4230 return runlevel_parse_argv(argc, argv);
4234 arg_action = ACTION_SYSTEMCTL;
4235 return systemctl_parse_argv(argc, argv);
4238 static int action_to_runlevel(void) {
4240 static const char table[_ACTION_MAX] = {
4241 [ACTION_HALT] = '0',
4242 [ACTION_POWEROFF] = '0',
4243 [ACTION_REBOOT] = '6',
4244 [ACTION_RUNLEVEL2] = '2',
4245 [ACTION_RUNLEVEL3] = '3',
4246 [ACTION_RUNLEVEL4] = '4',
4247 [ACTION_RUNLEVEL5] = '5',
4248 [ACTION_RESCUE] = '1'
4251 assert(arg_action < _ACTION_MAX);
4253 return table[arg_action];
4256 static int talk_upstart(void) {
4257 DBusMessage *m = NULL, *reply = NULL;
4259 int previous, rl, r;
4261 env1_buf[] = "RUNLEVEL=X",
4262 env2_buf[] = "PREVLEVEL=X";
4263 char *env1 = env1_buf, *env2 = env2_buf;
4264 const char *emit = "runlevel";
4265 dbus_bool_t b_false = FALSE;
4266 DBusMessageIter iter, sub;
4267 DBusConnection *bus;
4269 dbus_error_init(&error);
4271 if (!(rl = action_to_runlevel()))
4274 if (utmp_get_runlevel(&previous, NULL) < 0)
4277 if (!(bus = dbus_connection_open_private("unix:abstract=/com/ubuntu/upstart", &error))) {
4278 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
4283 log_error("Failed to connect to Upstart bus: %s", error.message);
4288 if ((r = bus_check_peercred(bus)) < 0) {
4289 log_error("Failed to verify owner of bus.");
4293 if (!(m = dbus_message_new_method_call(
4294 "com.ubuntu.Upstart",
4295 "/com/ubuntu/Upstart",
4296 "com.ubuntu.Upstart0_6",
4299 log_error("Could not allocate message.");
4304 dbus_message_iter_init_append(m, &iter);
4306 env1_buf[sizeof(env1_buf)-2] = rl;
4307 env2_buf[sizeof(env2_buf)-2] = previous;
4309 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
4310 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
4311 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
4312 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
4313 !dbus_message_iter_close_container(&iter, &sub) ||
4314 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
4315 log_error("Could not append arguments to message.");
4320 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
4322 if (error_is_no_service(&error)) {
4327 log_error("Failed to issue method call: %s", error.message);
4336 dbus_message_unref(m);
4339 dbus_message_unref(reply);
4342 dbus_connection_close(bus);
4343 dbus_connection_unref(bus);
4346 dbus_error_free(&error);
4351 static int talk_initctl(void) {
4352 struct init_request request;
4356 if (!(rl = action_to_runlevel()))
4360 request.magic = INIT_MAGIC;
4361 request.sleeptime = 0;
4362 request.cmd = INIT_CMD_RUNLVL;
4363 request.runlevel = rl;
4365 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
4367 if (errno == ENOENT)
4370 log_error("Failed to open "INIT_FIFO": %m");
4375 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
4376 close_nointr_nofail(fd);
4379 log_error("Failed to write to "INIT_FIFO": %m");
4380 return errno ? -errno : -EIO;
4386 static int systemctl_main(DBusConnection *bus, int argc, char *argv[], DBusError *error) {
4388 static const struct {
4396 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
4398 { "list-units", LESS, 1, list_units },
4399 { "list-jobs", EQUAL, 1, list_jobs },
4400 { "clear-jobs", EQUAL, 1, daemon_reload },
4401 { "load", MORE, 2, load_unit },
4402 { "cancel", MORE, 2, cancel_job },
4403 { "start", MORE, 2, start_unit },
4404 { "stop", MORE, 2, start_unit },
4405 { "reload", MORE, 2, start_unit },
4406 { "restart", MORE, 2, start_unit },
4407 { "try-restart", MORE, 2, start_unit },
4408 { "reload-or-restart", MORE, 2, start_unit },
4409 { "reload-or-try-restart", MORE, 2, start_unit },
4410 { "force-reload", MORE, 2, start_unit }, /* For compatibility with SysV */
4411 { "condrestart", MORE, 2, start_unit }, /* For compatibility with RH */
4412 { "isolate", EQUAL, 2, start_unit },
4413 { "is-active", MORE, 2, check_unit },
4414 { "check", MORE, 2, check_unit },
4415 { "show", MORE, 1, show },
4416 { "status", MORE, 2, show },
4417 { "monitor", EQUAL, 1, monitor },
4418 { "dump", EQUAL, 1, dump },
4419 { "dot", EQUAL, 1, dot },
4420 { "snapshot", LESS, 2, snapshot },
4421 { "delete", MORE, 2, delete_snapshot },
4422 { "daemon-reload", EQUAL, 1, daemon_reload },
4423 { "daemon-reexec", EQUAL, 1, daemon_reload },
4424 { "daemon-exit", EQUAL, 1, daemon_reload },
4425 { "show-environment", EQUAL, 1, show_enviroment },
4426 { "set-environment", MORE, 2, set_environment },
4427 { "unset-environment", MORE, 2, set_environment },
4428 { "halt", EQUAL, 1, start_special },
4429 { "poweroff", EQUAL, 1, start_special },
4430 { "reboot", EQUAL, 1, start_special },
4431 { "default", EQUAL, 1, start_special },
4432 { "rescue", EQUAL, 1, start_special },
4433 { "emergency", EQUAL, 1, start_special },
4434 { "reset-maintenance", MORE, 1, reset_maintenance },
4435 { "enable", MORE, 2, enable_unit },
4436 { "disable", MORE, 2, enable_unit },
4437 { "is-enabled", MORE, 2, enable_unit }
4447 left = argc - optind;
4450 /* Special rule: no arguments means "list-units" */
4453 if (streq(argv[optind], "help")) {
4458 for (i = 0; i < ELEMENTSOF(verbs); i++)
4459 if (streq(argv[optind], verbs[i].verb))
4462 if (i >= ELEMENTSOF(verbs)) {
4463 log_error("Unknown operation %s", argv[optind]);
4468 switch (verbs[i].argc_cmp) {
4471 if (left != verbs[i].argc) {
4472 log_error("Invalid number of arguments.");
4479 if (left < verbs[i].argc) {
4480 log_error("Too few arguments.");
4487 if (left > verbs[i].argc) {
4488 log_error("Too many arguments.");
4495 assert_not_reached("Unknown comparison operator.");
4498 /* Require a bus connection for all operations but
4500 if (!streq(verbs[i].verb, "enable") &&
4501 !streq(verbs[i].verb, "disable") &&
4503 log_error("Failed to get D-Bus connection: %s", error->message);
4507 return verbs[i].dispatch(bus, argv + optind, left);
4510 static int reload_with_fallback(DBusConnection *bus) {
4514 /* First, try systemd via D-Bus. */
4515 if ((r = daemon_reload(bus, NULL, 0)) > 0)
4519 /* Nothing else worked, so let's try signals */
4520 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
4522 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
4523 log_error("kill() failed: %m");
4530 static int start_with_fallback(DBusConnection *bus) {
4534 /* First, try systemd via D-Bus. */
4535 if ((r = start_unit(bus, NULL, 0)) > 0)
4539 /* Hmm, talking to systemd via D-Bus didn't work. Then
4540 * let's try to talk to Upstart via D-Bus. */
4541 if ((r = talk_upstart()) > 0)
4544 /* Nothing else worked, so let's try
4546 if ((r = talk_initctl()) != 0)
4549 log_error("Failed to talk to init daemon.");
4553 warn_wall(arg_action);
4557 static int halt_main(DBusConnection *bus) {
4560 if (geteuid() != 0) {
4561 log_error("Must to be root.");
4565 if (!arg_dry && !arg_immediate)
4566 return start_with_fallback(bus);
4569 if ((r = utmp_put_shutdown(0)) < 0)
4570 log_warning("Failed to write utmp record: %s", strerror(-r));
4578 /* Make sure C-A-D is handled by the kernel from this
4580 reboot(RB_ENABLE_CAD);
4582 switch (arg_action) {
4585 log_info("Halting");
4586 reboot(RB_HALT_SYSTEM);
4589 case ACTION_POWEROFF:
4590 log_info("Powering off");
4591 reboot(RB_POWER_OFF);
4595 log_info("Rebooting");
4596 reboot(RB_AUTOBOOT);
4600 assert_not_reached("Unknown halt action.");
4603 /* We should never reach this. */
4607 static int runlevel_main(void) {
4608 int r, runlevel, previous;
4610 if ((r = utmp_get_runlevel(&runlevel, &previous)) < 0) {
4616 previous <= 0 ? 'N' : previous,
4617 runlevel <= 0 ? 'N' : runlevel);
4622 int main(int argc, char*argv[]) {
4624 DBusConnection *bus = NULL;
4627 dbus_error_init(&error);
4629 log_parse_environment();
4631 if ((r = parse_argv(argc, argv)) < 0)
4638 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
4639 * let's shortcut this */
4640 if (arg_action == ACTION_RUNLEVEL) {
4641 retval = runlevel_main() < 0;
4645 bus_connect(arg_session ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &bus, &private_bus, &error);
4647 switch (arg_action) {
4649 case ACTION_SYSTEMCTL: {
4650 retval = systemctl_main(bus, argc, argv, &error) < 0;
4655 case ACTION_POWEROFF:
4657 retval = halt_main(bus) < 0;
4660 case ACTION_RUNLEVEL2:
4661 case ACTION_RUNLEVEL3:
4662 case ACTION_RUNLEVEL4:
4663 case ACTION_RUNLEVEL5:
4665 case ACTION_EMERGENCY:
4666 case ACTION_DEFAULT:
4667 retval = start_with_fallback(bus) < 0;
4672 retval = reload_with_fallback(bus) < 0;
4675 case ACTION_INVALID:
4676 case ACTION_RUNLEVEL:
4678 assert_not_reached("Unknown action");
4684 dbus_connection_close(bus);
4685 dbus_connection_unref(bus);
4688 dbus_error_free(&error);
4692 strv_free(arg_property);