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 "ConflictedBy", "[color=\"red\"]",
315 "After", "[color=\"green\"]"
318 const char *c = NULL;
325 for (i = 0; i < ELEMENTSOF(colors); i += 2)
326 if (streq(colors[i], prop)) {
334 if (arg_dot != DOT_ALL)
335 if ((arg_dot == DOT_ORDER) != streq(prop, "After"))
338 switch (dbus_message_iter_get_arg_type(iter)) {
340 case DBUS_TYPE_ARRAY:
342 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
345 dbus_message_iter_recurse(iter, &sub);
347 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
350 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
351 dbus_message_iter_get_basic(&sub, &s);
352 printf("\t\"%s\"->\"%s\" %s;\n", name, s, c);
354 dbus_message_iter_next(&sub);
364 static int dot_one(DBusConnection *bus, const char *name, const char *path) {
365 DBusMessage *m = NULL, *reply = NULL;
366 const char *interface = "org.freedesktop.systemd1.Unit";
369 DBusMessageIter iter, sub, sub2, sub3;
374 dbus_error_init(&error);
376 if (!(m = dbus_message_new_method_call(
377 "org.freedesktop.systemd1",
379 "org.freedesktop.DBus.Properties",
381 log_error("Could not allocate message.");
386 if (!dbus_message_append_args(m,
387 DBUS_TYPE_STRING, &interface,
388 DBUS_TYPE_INVALID)) {
389 log_error("Could not append arguments to message.");
394 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
395 log_error("Failed to issue method call: %s", error.message);
400 if (!dbus_message_iter_init(reply, &iter) ||
401 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
402 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
403 log_error("Failed to parse reply.");
408 dbus_message_iter_recurse(&iter, &sub);
410 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
413 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
414 log_error("Failed to parse reply.");
419 dbus_message_iter_recurse(&sub, &sub2);
421 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &prop, true) < 0) {
422 log_error("Failed to parse reply.");
427 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
428 log_error("Failed to parse reply.");
433 dbus_message_iter_recurse(&sub2, &sub3);
435 if (dot_one_property(name, prop, &sub3)) {
436 log_error("Failed to parse reply.");
441 dbus_message_iter_next(&sub);
446 dbus_message_unref(m);
449 dbus_message_unref(reply);
451 dbus_error_free(&error);
456 static int dot(DBusConnection *bus, char **args, unsigned n) {
457 DBusMessage *m = NULL, *reply = NULL;
460 DBusMessageIter iter, sub, sub2;
462 dbus_error_init(&error);
466 if (!(m = dbus_message_new_method_call(
467 "org.freedesktop.systemd1",
468 "/org/freedesktop/systemd1",
469 "org.freedesktop.systemd1.Manager",
471 log_error("Could not allocate message.");
475 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
476 log_error("Failed to issue method call: %s", error.message);
481 if (!dbus_message_iter_init(reply, &iter) ||
482 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
483 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
484 log_error("Failed to parse reply.");
489 printf("digraph systemd {\n");
491 dbus_message_iter_recurse(&iter, &sub);
492 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
493 const char *id, *description, *load_state, *active_state, *sub_state, *following, *unit_path;
495 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
496 log_error("Failed to parse reply.");
501 dbus_message_iter_recurse(&sub, &sub2);
503 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
504 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
505 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
506 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
507 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
508 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &following, true) < 0 ||
509 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, true) < 0) {
510 log_error("Failed to parse reply.");
515 if ((r = dot_one(bus, id, unit_path)) < 0)
518 /* printf("\t\"%s\";\n", id); */
519 dbus_message_iter_next(&sub);
524 log_info(" Color legend: black = Requires\n"
525 " dark blue = Requisite\n"
526 " dark grey = Wants\n"
530 if (isatty(fileno(stdout)))
531 log_notice("-- You probably want to process this output with graphviz' dot tool.\n"
532 "-- Try a shell pipeline like 'systemctl dot | dot -Tsvg > systemd.svg'!\n");
538 dbus_message_unref(m);
541 dbus_message_unref(reply);
543 dbus_error_free(&error);
548 static int list_jobs(DBusConnection *bus, char **args, unsigned n) {
549 DBusMessage *m = NULL, *reply = NULL;
552 DBusMessageIter iter, sub, sub2;
555 dbus_error_init(&error);
559 if (!(m = dbus_message_new_method_call(
560 "org.freedesktop.systemd1",
561 "/org/freedesktop/systemd1",
562 "org.freedesktop.systemd1.Manager",
564 log_error("Could not allocate message.");
568 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
569 log_error("Failed to issue method call: %s", error.message);
574 if (!dbus_message_iter_init(reply, &iter) ||
575 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
576 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
577 log_error("Failed to parse reply.");
582 dbus_message_iter_recurse(&iter, &sub);
584 printf("%4s %-45s %-17s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
586 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
587 const char *name, *type, *state, *job_path, *unit_path;
591 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
592 log_error("Failed to parse reply.");
597 dbus_message_iter_recurse(&sub, &sub2);
599 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
600 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
601 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
602 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
603 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
604 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
605 log_error("Failed to parse reply.");
610 e = arg_full ? NULL : ellipsize(name, 45, 33);
611 printf("%4u %-45s %-17s %-7s\n", id, e ? e : name, type, state);
616 dbus_message_iter_next(&sub);
619 printf("\n%u jobs listed.\n", k);
624 dbus_message_unref(m);
627 dbus_message_unref(reply);
629 dbus_error_free(&error);
634 static int load_unit(DBusConnection *bus, char **args, unsigned n) {
635 DBusMessage *m = NULL, *reply = NULL;
640 dbus_error_init(&error);
645 for (i = 1; i < n; i++) {
647 if (!(m = dbus_message_new_method_call(
648 "org.freedesktop.systemd1",
649 "/org/freedesktop/systemd1",
650 "org.freedesktop.systemd1.Manager",
652 log_error("Could not allocate message.");
657 if (!dbus_message_append_args(m,
658 DBUS_TYPE_STRING, &args[i],
659 DBUS_TYPE_INVALID)) {
660 log_error("Could not append arguments to message.");
665 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
666 log_error("Failed to issue method call: %s", error.message);
671 dbus_message_unref(m);
672 dbus_message_unref(reply);
681 dbus_message_unref(m);
684 dbus_message_unref(reply);
686 dbus_error_free(&error);
691 static int cancel_job(DBusConnection *bus, char **args, unsigned n) {
692 DBusMessage *m = NULL, *reply = NULL;
697 dbus_error_init(&error);
703 return daemon_reload(bus, args, n);
705 for (i = 1; i < n; i++) {
709 if (!(m = dbus_message_new_method_call(
710 "org.freedesktop.systemd1",
711 "/org/freedesktop/systemd1",
712 "org.freedesktop.systemd1.Manager",
714 log_error("Could not allocate message.");
719 if ((r = safe_atou(args[i], &id)) < 0) {
720 log_error("Failed to parse job id: %s", strerror(-r));
724 assert_cc(sizeof(uint32_t) == sizeof(id));
725 if (!dbus_message_append_args(m,
726 DBUS_TYPE_UINT32, &id,
727 DBUS_TYPE_INVALID)) {
728 log_error("Could not append arguments to message.");
733 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
734 log_error("Failed to issue method call: %s", error.message);
739 if (!dbus_message_get_args(reply, &error,
740 DBUS_TYPE_OBJECT_PATH, &path,
741 DBUS_TYPE_INVALID)) {
742 log_error("Failed to parse reply: %s", error.message);
747 dbus_message_unref(m);
748 if (!(m = dbus_message_new_method_call(
749 "org.freedesktop.systemd1",
751 "org.freedesktop.systemd1.Job",
753 log_error("Could not allocate message.");
758 dbus_message_unref(reply);
759 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
760 log_error("Failed to issue method call: %s", error.message);
765 dbus_message_unref(m);
766 dbus_message_unref(reply);
774 dbus_message_unref(m);
777 dbus_message_unref(reply);
779 dbus_error_free(&error);
784 static bool need_daemon_reload(DBusConnection *bus, const char *unit) {
785 DBusMessage *m, *reply;
786 dbus_bool_t b = FALSE;
787 DBusMessageIter iter, sub;
789 *interface = "org.freedesktop.systemd1.Unit",
790 *property = "NeedDaemonReload",
793 /* We ignore all errors here, since this is used to show a warning only */
795 if (!(m = dbus_message_new_method_call(
796 "org.freedesktop.systemd1",
797 "/org/freedesktop/systemd1",
798 "org.freedesktop.systemd1.Manager",
802 if (!dbus_message_append_args(m,
803 DBUS_TYPE_STRING, &unit,
807 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
810 if (!dbus_message_get_args(reply, NULL,
811 DBUS_TYPE_OBJECT_PATH, &path,
815 dbus_message_unref(m);
816 if (!(m = dbus_message_new_method_call(
817 "org.freedesktop.systemd1",
819 "org.freedesktop.DBus.Properties",
823 if (!dbus_message_append_args(m,
824 DBUS_TYPE_STRING, &interface,
825 DBUS_TYPE_STRING, &property,
826 DBUS_TYPE_INVALID)) {
830 dbus_message_unref(reply);
831 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
834 if (!dbus_message_iter_init(reply, &iter) ||
835 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
838 dbus_message_iter_recurse(&iter, &sub);
840 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_BOOLEAN)
843 dbus_message_iter_get_basic(&sub, &b);
847 dbus_message_unref(m);
850 dbus_message_unref(reply);
855 typedef struct WaitData {
860 static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
868 dbus_error_init(&error);
870 log_debug("Got D-Bus request: %s.%s() on %s",
871 dbus_message_get_interface(message),
872 dbus_message_get_member(message),
873 dbus_message_get_path(message));
875 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
876 log_error("Warning! D-Bus connection terminated.");
877 dbus_connection_close(connection);
879 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
882 dbus_bool_t success = true;
884 if (!dbus_message_get_args(message, &error,
885 DBUS_TYPE_UINT32, &id,
886 DBUS_TYPE_OBJECT_PATH, &path,
887 DBUS_TYPE_BOOLEAN, &success,
889 log_error("Failed to parse message: %s", error.message);
893 if ((p = set_remove(d->set, (char*) path)))
901 dbus_error_free(&error);
902 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
905 static int enable_wait_for_jobs(DBusConnection *bus) {
913 dbus_error_init(&error);
914 dbus_bus_add_match(bus,
916 "sender='org.freedesktop.systemd1',"
917 "interface='org.freedesktop.systemd1.Manager',"
918 "member='JobRemoved',"
919 "path='/org/freedesktop/systemd1'",
922 if (dbus_error_is_set(&error)) {
923 log_error("Failed to add match: %s", error.message);
924 dbus_error_free(&error);
928 /* This is slightly dirty, since we don't undo the match registrations. */
932 static int wait_for_jobs(DBusConnection *bus, Set *s) {
943 if (!dbus_connection_add_filter(bus, wait_filter, &d, NULL)) {
944 log_error("Failed to add filter.");
949 while (!set_isempty(s) &&
950 dbus_connection_read_write_dispatch(bus, -1))
953 if (!arg_quiet && d.failed)
954 log_error("Job failed, see logs for details.");
956 r = d.failed ? -EIO : 0;
959 /* This is slightly dirty, since we don't undo the filter registration. */
964 static int start_unit_one(
971 DBusMessage *m = NULL, *reply = NULL;
980 assert(arg_no_block || s);
982 dbus_error_init(&error);
984 if (!(m = dbus_message_new_method_call(
985 "org.freedesktop.systemd1",
986 "/org/freedesktop/systemd1",
987 "org.freedesktop.systemd1.Manager",
989 log_error("Could not allocate message.");
994 if (!dbus_message_append_args(m,
995 DBUS_TYPE_STRING, &name,
996 DBUS_TYPE_STRING, &mode,
997 DBUS_TYPE_INVALID)) {
998 log_error("Could not append arguments to message.");
1003 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1005 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
1006 /* There's always a fallback possible for
1007 * legacy actions. */
1012 log_error("Failed to issue method call: %s", error.message);
1017 if (!dbus_message_get_args(reply, &error,
1018 DBUS_TYPE_OBJECT_PATH, &path,
1019 DBUS_TYPE_INVALID)) {
1020 log_error("Failed to parse reply: %s", error.message);
1025 if (need_daemon_reload(bus, name))
1026 log_warning("Unit file of created job changed on disk, 'systemctl %s daemon-reload' recommended.",
1027 arg_session ? "--session" : "--system");
1029 if (!arg_no_block) {
1032 if (!(p = strdup(path))) {
1033 log_error("Failed to duplicate path.");
1038 if ((r = set_put(s, p)) < 0) {
1040 log_error("Failed to add path to set.");
1049 dbus_message_unref(m);
1052 dbus_message_unref(reply);
1054 dbus_error_free(&error);
1059 static enum action verb_to_action(const char *verb) {
1060 if (streq(verb, "halt"))
1062 else if (streq(verb, "poweroff"))
1063 return ACTION_POWEROFF;
1064 else if (streq(verb, "reboot"))
1065 return ACTION_REBOOT;
1066 else if (streq(verb, "rescue"))
1067 return ACTION_RESCUE;
1068 else if (streq(verb, "emergency"))
1069 return ACTION_EMERGENCY;
1070 else if (streq(verb, "default"))
1071 return ACTION_DEFAULT;
1073 return ACTION_INVALID;
1076 static int start_unit(DBusConnection *bus, char **args, unsigned n) {
1078 static const char * const table[_ACTION_MAX] = {
1079 [ACTION_HALT] = SPECIAL_HALT_TARGET,
1080 [ACTION_POWEROFF] = SPECIAL_POWEROFF_TARGET,
1081 [ACTION_REBOOT] = SPECIAL_REBOOT_TARGET,
1082 [ACTION_RUNLEVEL2] = SPECIAL_RUNLEVEL2_TARGET,
1083 [ACTION_RUNLEVEL3] = SPECIAL_RUNLEVEL3_TARGET,
1084 [ACTION_RUNLEVEL4] = SPECIAL_RUNLEVEL4_TARGET,
1085 [ACTION_RUNLEVEL5] = SPECIAL_RUNLEVEL5_TARGET,
1086 [ACTION_RESCUE] = SPECIAL_RESCUE_TARGET,
1087 [ACTION_EMERGENCY] = SPECIAL_EMERGENCY_TARGET,
1088 [ACTION_DEFAULT] = SPECIAL_DEFAULT_TARGET
1093 const char *method, *mode, *one_name;
1098 if (arg_action == ACTION_SYSTEMCTL) {
1100 streq(args[0], "stop") ? "StopUnit" :
1101 streq(args[0], "reload") ? "ReloadUnit" :
1102 streq(args[0], "restart") ? "RestartUnit" :
1103 streq(args[0], "try-restart") ? "TryRestartUnit" :
1104 streq(args[0], "reload-or-restart") ? "ReloadOrRestartUnit" :
1105 streq(args[0], "reload-or-try-restart") ||
1106 streq(args[0], "force-reload") ||
1107 streq(args[0], "condrestart") ? "ReloadOrTryRestartUnit" :
1111 (streq(args[0], "isolate") ||
1112 streq(args[0], "rescue") ||
1113 streq(args[0], "emergency")) ? "isolate" :
1117 one_name = table[verb_to_action(args[0])];
1120 assert(arg_action < ELEMENTSOF(table));
1121 assert(table[arg_action]);
1123 method = "StartUnit";
1125 mode = (arg_action == ACTION_EMERGENCY ||
1126 arg_action == ACTION_RESCUE) ? "isolate" : "replace";
1128 one_name = table[arg_action];
1131 if (!arg_no_block) {
1132 if ((r = enable_wait_for_jobs(bus)) < 0) {
1133 log_error("Could not watch jobs: %s", strerror(-r));
1137 if (!(s = set_new(string_hash_func, string_compare_func))) {
1138 log_error("Failed to allocate set.");
1147 if ((r = start_unit_one(bus, method, one_name, mode, s)) <= 0)
1150 for (i = 1; i < n; i++)
1151 if ((r = start_unit_one(bus, method, args[i], mode, s)) < 0)
1156 r = wait_for_jobs(bus, s);
1165 static int start_special(DBusConnection *bus, char **args, unsigned n) {
1171 r = start_unit(bus, args, n);
1174 warn_wall(verb_to_action(args[0]));
1179 static int check_unit(DBusConnection *bus, char **args, unsigned n) {
1180 DBusMessage *m = NULL, *reply = NULL;
1182 *interface = "org.freedesktop.systemd1.Unit",
1183 *property = "ActiveState";
1184 int r = -EADDRNOTAVAIL;
1191 dbus_error_init(&error);
1193 for (i = 1; i < n; i++) {
1194 const char *path = NULL;
1196 DBusMessageIter iter, sub;
1198 if (!(m = dbus_message_new_method_call(
1199 "org.freedesktop.systemd1",
1200 "/org/freedesktop/systemd1",
1201 "org.freedesktop.systemd1.Manager",
1203 log_error("Could not allocate message.");
1208 if (!dbus_message_append_args(m,
1209 DBUS_TYPE_STRING, &args[i],
1210 DBUS_TYPE_INVALID)) {
1211 log_error("Could not append arguments to message.");
1216 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1218 /* Hmm, cannot figure out anything about this unit... */
1222 dbus_error_free(&error);
1226 if (!dbus_message_get_args(reply, &error,
1227 DBUS_TYPE_OBJECT_PATH, &path,
1228 DBUS_TYPE_INVALID)) {
1229 log_error("Failed to parse reply: %s", error.message);
1234 dbus_message_unref(m);
1235 if (!(m = dbus_message_new_method_call(
1236 "org.freedesktop.systemd1",
1238 "org.freedesktop.DBus.Properties",
1240 log_error("Could not allocate message.");
1245 if (!dbus_message_append_args(m,
1246 DBUS_TYPE_STRING, &interface,
1247 DBUS_TYPE_STRING, &property,
1248 DBUS_TYPE_INVALID)) {
1249 log_error("Could not append arguments to message.");
1254 dbus_message_unref(reply);
1255 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1256 log_error("Failed to issue method call: %s", error.message);
1261 if (!dbus_message_iter_init(reply, &iter) ||
1262 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1263 log_error("Failed to parse reply.");
1268 dbus_message_iter_recurse(&iter, &sub);
1270 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1271 log_error("Failed to parse reply.");
1276 dbus_message_iter_get_basic(&sub, &state);
1281 if (streq(state, "active") || startswith(state, "reloading"))
1284 dbus_message_unref(m);
1285 dbus_message_unref(reply);
1291 dbus_message_unref(m);
1294 dbus_message_unref(reply);
1296 dbus_error_free(&error);
1301 typedef struct ExecStatusInfo {
1307 usec_t start_timestamp;
1308 usec_t exit_timestamp;
1313 LIST_FIELDS(struct ExecStatusInfo, exec);
1316 static void exec_status_info_free(ExecStatusInfo *i) {
1324 static int exec_status_info_deserialize(DBusMessageIter *sub, ExecStatusInfo *i) {
1325 uint64_t start_timestamp, exit_timestamp;
1326 DBusMessageIter sub2, sub3;
1330 int32_t code, status;
1336 if (dbus_message_iter_get_arg_type(sub) != DBUS_TYPE_STRUCT)
1339 dbus_message_iter_recurse(sub, &sub2);
1341 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
1344 if (!(i->path = strdup(path)))
1347 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
1348 dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
1352 dbus_message_iter_recurse(&sub2, &sub3);
1353 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1354 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1355 dbus_message_iter_next(&sub3);
1360 if (!(i->argv = new0(char*, n+1)))
1364 dbus_message_iter_recurse(&sub2, &sub3);
1365 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1368 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1369 dbus_message_iter_get_basic(&sub3, &s);
1370 dbus_message_iter_next(&sub3);
1372 if (!(i->argv[n++] = strdup(s)))
1376 if (!dbus_message_iter_next(&sub2) ||
1377 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, true) < 0 ||
1378 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp, true) < 0 ||
1379 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp, true) < 0 ||
1380 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) < 0 ||
1381 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) < 0 ||
1382 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) < 0)
1386 i->start_timestamp = (usec_t) start_timestamp;
1387 i->exit_timestamp = (usec_t) exit_timestamp;
1388 i->pid = (pid_t) pid;
1395 typedef struct UnitStatusInfo {
1397 const char *load_state;
1398 const char *active_state;
1399 const char *sub_state;
1401 const char *description;
1403 const char *fragment_path;
1404 const char *default_control_group;
1406 bool need_daemon_reload;
1411 const char *status_text;
1414 usec_t start_timestamp;
1415 usec_t exit_timestamp;
1417 int exit_code, exit_status;
1420 unsigned n_accepted;
1421 unsigned n_connections;
1425 const char *sysfs_path;
1427 /* Mount, Automount */
1433 LIST_HEAD(ExecStatusInfo, exec);
1436 static void print_status_info(UnitStatusInfo *i) {
1441 /* This shows pretty information about a unit. See
1442 * print_property() for a low-level property printer */
1444 printf("%s", strna(i->id));
1446 if (i->description && !streq_ptr(i->id, i->description))
1447 printf(" - %s", i->description);
1451 if (i->fragment_path)
1452 printf("\t Loaded: %s (%s)\n", strna(i->load_state), i->fragment_path);
1453 else if (streq_ptr(i->load_state, "failed"))
1454 printf("\t Loaded: %s%s%s\n",
1455 ansi_highlight(true),
1456 strna(i->load_state),
1457 ansi_highlight(false));
1459 printf("\t Loaded: %s\n", strna(i->load_state));
1461 if (streq_ptr(i->active_state, "maintenance")) {
1462 if (streq_ptr(i->active_state, i->sub_state))
1463 printf("\t Active: %s%s%s\n",
1464 ansi_highlight(true),
1465 strna(i->active_state),
1466 ansi_highlight(false));
1468 printf("\t Active: %s%s (%s)%s\n",
1469 ansi_highlight(true),
1470 strna(i->active_state),
1471 strna(i->sub_state),
1472 ansi_highlight(false));
1474 if (streq_ptr(i->active_state, i->sub_state))
1475 printf("\t Active: %s\n",
1476 strna(i->active_state));
1478 printf("\t Active: %s (%s)\n",
1479 strna(i->active_state),
1480 strna(i->sub_state));
1484 printf("\t Device: %s\n", i->sysfs_path);
1486 printf("\t Where: %s\n", i->where);
1488 printf("\t What: %s\n", i->what);
1491 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
1493 LIST_FOREACH(exec, p, i->exec) {
1496 /* Only show exited processes here */
1500 t = strv_join(p->argv, " ");
1501 printf("\t Exited: %u (%s, code=%s, ", p->pid, strna(t), sigchld_code_to_string(p->code));
1504 if (p->code == CLD_EXITED)
1505 printf("status=%i", p->status);
1507 printf("signal=%s", signal_to_string(p->status));
1510 if (i->main_pid == p->pid &&
1511 i->start_timestamp == p->start_timestamp &&
1512 i->exit_timestamp == p->start_timestamp)
1513 /* Let's not show this twice */
1516 if (p->pid == i->control_pid)
1520 if (i->main_pid > 0 || i->control_pid > 0) {
1523 if (i->main_pid > 0) {
1524 printf(" Main: %u", (unsigned) i->main_pid);
1528 get_process_name(i->main_pid, &t);
1533 } else if (i->exit_code > 0) {
1534 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
1536 if (i->exit_code == CLD_EXITED)
1537 printf("status=%i", i->exit_status);
1539 printf("signal=%s", signal_to_string(i->exit_status));
1544 if (i->main_pid > 0 && i->control_pid > 0)
1547 if (i->control_pid > 0) {
1550 printf(" Control: %u", (unsigned) i->control_pid);
1552 get_process_name(i->control_pid, &t);
1563 printf("\t Status: \"%s\"\n", i->status_text);
1565 if (i->default_control_group) {
1568 printf("\t CGroup: %s\n", i->default_control_group);
1570 if ((c = columns()) > 18)
1575 show_cgroup_by_path(i->default_control_group, "\t\t ", c);
1578 if (i->need_daemon_reload)
1579 printf("\n%sWarning:%s Unit file changed on disk, 'systemctl %s daemon-reload' recommended.\n",
1580 ansi_highlight(true),
1581 ansi_highlight(false),
1582 arg_session ? "--session" : "--system");
1585 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
1587 switch (dbus_message_iter_get_arg_type(iter)) {
1589 case DBUS_TYPE_STRING: {
1592 dbus_message_iter_get_basic(iter, &s);
1595 if (streq(name, "Id"))
1597 else if (streq(name, "LoadState"))
1599 else if (streq(name, "ActiveState"))
1600 i->active_state = s;
1601 else if (streq(name, "SubState"))
1603 else if (streq(name, "Description"))
1605 else if (streq(name, "FragmentPath"))
1606 i->fragment_path = s;
1607 else if (streq(name, "DefaultControlGroup"))
1608 i->default_control_group = s;
1609 else if (streq(name, "StatusText"))
1611 else if (streq(name, "SysFSPath"))
1613 else if (streq(name, "Where"))
1615 else if (streq(name, "What"))
1622 case DBUS_TYPE_BOOLEAN: {
1625 dbus_message_iter_get_basic(iter, &b);
1627 if (streq(name, "Accept"))
1629 else if (streq(name, "NeedDaemonReload"))
1630 i->need_daemon_reload = b;
1635 case DBUS_TYPE_UINT32: {
1638 dbus_message_iter_get_basic(iter, &u);
1640 if (streq(name, "MainPID")) {
1642 i->main_pid = (pid_t) u;
1645 } else if (streq(name, "ControlPID"))
1646 i->control_pid = (pid_t) u;
1647 else if (streq(name, "ExecMainPID")) {
1649 i->main_pid = (pid_t) u;
1650 } else if (streq(name, "NAccepted"))
1652 else if (streq(name, "NConnections"))
1653 i->n_connections = u;
1658 case DBUS_TYPE_INT32: {
1661 dbus_message_iter_get_basic(iter, &j);
1663 if (streq(name, "ExecMainCode"))
1664 i->exit_code = (int) j;
1665 else if (streq(name, "ExecMainStatus"))
1666 i->exit_status = (int) j;
1671 case DBUS_TYPE_UINT64: {
1674 dbus_message_iter_get_basic(iter, &u);
1676 if (streq(name, "ExecMainStartTimestamp"))
1677 i->start_timestamp = (usec_t) u;
1678 else if (streq(name, "ExecMainExitTimestamp"))
1679 i->exit_timestamp = (usec_t) u;
1684 case DBUS_TYPE_ARRAY: {
1686 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT &&
1687 startswith(name, "Exec")) {
1688 DBusMessageIter sub;
1690 dbus_message_iter_recurse(iter, &sub);
1691 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1692 ExecStatusInfo *info;
1695 if (!(info = new0(ExecStatusInfo, 1)))
1698 if ((r = exec_status_info_deserialize(&sub, info)) < 0) {
1703 LIST_PREPEND(ExecStatusInfo, exec, i->exec, info);
1705 dbus_message_iter_next(&sub);
1716 static int print_property(const char *name, DBusMessageIter *iter) {
1720 /* This is a low-level property printer, see
1721 * print_status_info() for the nicer output */
1723 if (arg_property && !strv_find(arg_property, name))
1726 switch (dbus_message_iter_get_arg_type(iter)) {
1728 case DBUS_TYPE_STRING: {
1730 dbus_message_iter_get_basic(iter, &s);
1732 if (arg_all || s[0])
1733 printf("%s=%s\n", name, s);
1738 case DBUS_TYPE_BOOLEAN: {
1740 dbus_message_iter_get_basic(iter, &b);
1741 printf("%s=%s\n", name, yes_no(b));
1746 case DBUS_TYPE_UINT64: {
1748 dbus_message_iter_get_basic(iter, &u);
1750 /* Yes, heuristics! But we can change this check
1751 * should it turn out to not be sufficient */
1753 if (strstr(name, "Timestamp")) {
1754 char timestamp[FORMAT_TIMESTAMP_MAX], *t;
1756 if ((t = format_timestamp(timestamp, sizeof(timestamp), u)) || arg_all)
1757 printf("%s=%s\n", name, strempty(t));
1758 } else if (strstr(name, "USec")) {
1759 char timespan[FORMAT_TIMESPAN_MAX];
1761 printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u));
1763 printf("%s=%llu\n", name, (unsigned long long) u);
1768 case DBUS_TYPE_UINT32: {
1770 dbus_message_iter_get_basic(iter, &u);
1772 if (strstr(name, "UMask") || strstr(name, "Mode"))
1773 printf("%s=%04o\n", name, u);
1775 printf("%s=%u\n", name, (unsigned) u);
1780 case DBUS_TYPE_INT32: {
1782 dbus_message_iter_get_basic(iter, &i);
1784 printf("%s=%i\n", name, (int) i);
1788 case DBUS_TYPE_STRUCT: {
1789 DBusMessageIter sub;
1790 dbus_message_iter_recurse(iter, &sub);
1792 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
1795 dbus_message_iter_get_basic(&sub, &u);
1798 printf("%s=%u\n", name, (unsigned) u);
1800 printf("%s=\n", name);
1803 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
1806 dbus_message_iter_get_basic(&sub, &s);
1808 if (arg_all || s[0])
1809 printf("%s=%s\n", name, s);
1817 case DBUS_TYPE_ARRAY:
1819 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
1820 DBusMessageIter sub;
1823 dbus_message_iter_recurse(iter, &sub);
1825 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1826 printf("%s=", name);
1828 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1831 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
1832 dbus_message_iter_get_basic(&sub, &s);
1833 printf("%s%s", space ? " " : "", s);
1836 dbus_message_iter_next(&sub);
1844 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_BYTE) {
1845 DBusMessageIter sub;
1847 dbus_message_iter_recurse(iter, &sub);
1849 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1850 printf("%s=", name);
1852 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1855 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_BYTE);
1856 dbus_message_iter_get_basic(&sub, &u);
1859 dbus_message_iter_next(&sub);
1867 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
1868 DBusMessageIter sub, sub2;
1870 dbus_message_iter_recurse(iter, &sub);
1871 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1872 const char *type, *path;
1874 dbus_message_iter_recurse(&sub, &sub2);
1876 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
1877 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
1878 printf("%s=%s\n", type, path);
1880 dbus_message_iter_next(&sub);
1885 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
1886 DBusMessageIter sub, sub2;
1888 dbus_message_iter_recurse(iter, &sub);
1889 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1891 uint64_t value, next_elapse;
1893 dbus_message_iter_recurse(&sub, &sub2);
1895 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
1896 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
1897 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
1898 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
1900 printf("%s={ value=%s ; next_elapse=%s }\n",
1902 format_timespan(timespan1, sizeof(timespan1), value),
1903 format_timespan(timespan2, sizeof(timespan2), next_elapse));
1906 dbus_message_iter_next(&sub);
1911 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
1912 DBusMessageIter sub;
1914 dbus_message_iter_recurse(iter, &sub);
1915 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1916 ExecStatusInfo info;
1919 if (exec_status_info_deserialize(&sub, &info) >= 0) {
1920 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
1923 t = strv_join(info.argv, " ");
1925 printf("%s={ path=%s ; argv[]=%s ; ignore=%s ; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n",
1929 yes_no(info.ignore),
1930 strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)),
1931 strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)),
1932 (unsigned) info. pid,
1933 sigchld_code_to_string(info.code),
1935 info.code == CLD_EXITED ? "" : "/",
1936 strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status)));
1942 strv_free(info.argv);
1944 dbus_message_iter_next(&sub);
1954 printf("%s=[unprintable]\n", name);
1959 static int show_one(DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
1960 DBusMessage *m = NULL, *reply = NULL;
1961 const char *interface = "";
1964 DBusMessageIter iter, sub, sub2, sub3;
1965 UnitStatusInfo info;
1973 dbus_error_init(&error);
1975 if (!(m = dbus_message_new_method_call(
1976 "org.freedesktop.systemd1",
1978 "org.freedesktop.DBus.Properties",
1980 log_error("Could not allocate message.");
1985 if (!dbus_message_append_args(m,
1986 DBUS_TYPE_STRING, &interface,
1987 DBUS_TYPE_INVALID)) {
1988 log_error("Could not append arguments to message.");
1993 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1994 log_error("Failed to issue method call: %s", error.message);
1999 if (!dbus_message_iter_init(reply, &iter) ||
2000 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
2001 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
2002 log_error("Failed to parse reply.");
2007 dbus_message_iter_recurse(&iter, &sub);
2014 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2017 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
2018 log_error("Failed to parse reply.");
2023 dbus_message_iter_recurse(&sub, &sub2);
2025 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
2026 log_error("Failed to parse reply.");
2031 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
2032 log_error("Failed to parse reply.");
2037 dbus_message_iter_recurse(&sub2, &sub3);
2039 if (show_properties)
2040 r = print_property(name, &sub3);
2042 r = status_property(name, &sub3, &info);
2045 log_error("Failed to parse reply.");
2050 dbus_message_iter_next(&sub);
2053 if (!show_properties)
2054 print_status_info(&info);
2056 while ((p = info.exec)) {
2057 LIST_REMOVE(ExecStatusInfo, exec, info.exec, p);
2058 exec_status_info_free(p);
2065 dbus_message_unref(m);
2068 dbus_message_unref(reply);
2070 dbus_error_free(&error);
2075 static int show(DBusConnection *bus, char **args, unsigned n) {
2076 DBusMessage *m = NULL, *reply = NULL;
2080 bool show_properties, new_line = false;
2085 dbus_error_init(&error);
2087 show_properties = !streq(args[0], "status");
2089 if (show_properties && n <= 1) {
2090 /* If not argument is specified inspect the manager
2093 r = show_one(bus, "/org/freedesktop/systemd1", show_properties, &new_line);
2097 for (i = 1; i < n; i++) {
2098 const char *path = NULL;
2101 if (!show_properties || safe_atou32(args[i], &id) < 0) {
2103 if (!(m = dbus_message_new_method_call(
2104 "org.freedesktop.systemd1",
2105 "/org/freedesktop/systemd1",
2106 "org.freedesktop.systemd1.Manager",
2108 log_error("Could not allocate message.");
2113 if (!dbus_message_append_args(m,
2114 DBUS_TYPE_STRING, &args[i],
2115 DBUS_TYPE_INVALID)) {
2116 log_error("Could not append arguments to message.");
2121 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2123 if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
2124 log_error("Failed to issue method call: %s", error.message);
2129 dbus_error_free(&error);
2131 dbus_message_unref(m);
2132 if (!(m = dbus_message_new_method_call(
2133 "org.freedesktop.systemd1",
2134 "/org/freedesktop/systemd1",
2135 "org.freedesktop.systemd1.Manager",
2137 log_error("Could not allocate message.");
2142 if (!dbus_message_append_args(m,
2143 DBUS_TYPE_STRING, &args[i],
2144 DBUS_TYPE_INVALID)) {
2145 log_error("Could not append arguments to message.");
2150 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2151 log_error("Failed to issue method call: %s", error.message);
2159 if (!(m = dbus_message_new_method_call(
2160 "org.freedesktop.systemd1",
2161 "/org/freedesktop/systemd1",
2162 "org.freedesktop.systemd1.Manager",
2164 log_error("Could not allocate message.");
2169 if (!dbus_message_append_args(m,
2170 DBUS_TYPE_UINT32, &id,
2171 DBUS_TYPE_INVALID)) {
2172 log_error("Could not append arguments to message.");
2177 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2178 log_error("Failed to issue method call: %s", error.message);
2184 if (!dbus_message_get_args(reply, &error,
2185 DBUS_TYPE_OBJECT_PATH, &path,
2186 DBUS_TYPE_INVALID)) {
2187 log_error("Failed to parse reply: %s", error.message);
2192 if ((r = show_one(bus, path, show_properties, &new_line)) < 0)
2195 dbus_message_unref(m);
2196 dbus_message_unref(reply);
2204 dbus_message_unref(m);
2207 dbus_message_unref(reply);
2209 dbus_error_free(&error);
2214 static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
2216 DBusMessage *m = NULL, *reply = NULL;
2221 dbus_error_init(&error);
2223 log_debug("Got D-Bus request: %s.%s() on %s",
2224 dbus_message_get_interface(message),
2225 dbus_message_get_member(message),
2226 dbus_message_get_path(message));
2228 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
2229 log_error("Warning! D-Bus connection terminated.");
2230 dbus_connection_close(connection);
2232 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") ||
2233 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
2234 const char *id, *path;
2236 if (!dbus_message_get_args(message, &error,
2237 DBUS_TYPE_STRING, &id,
2238 DBUS_TYPE_OBJECT_PATH, &path,
2240 log_error("Failed to parse message: %s", error.message);
2241 else if (streq(dbus_message_get_member(message), "UnitNew"))
2242 printf("Unit %s added.\n", id);
2244 printf("Unit %s removed.\n", id);
2246 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew") ||
2247 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
2251 if (!dbus_message_get_args(message, &error,
2252 DBUS_TYPE_UINT32, &id,
2253 DBUS_TYPE_OBJECT_PATH, &path,
2255 log_error("Failed to parse message: %s", error.message);
2256 else if (streq(dbus_message_get_member(message), "JobNew"))
2257 printf("Job %u added.\n", id);
2259 printf("Job %u removed.\n", id);
2262 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Unit", "Changed") ||
2263 dbus_message_is_signal(message, "org.freedesktop.systemd1.Job", "Changed")) {
2265 const char *path, *interface, *property = "Id";
2266 DBusMessageIter iter, sub;
2268 path = dbus_message_get_path(message);
2269 interface = dbus_message_get_interface(message);
2271 if (!(m = dbus_message_new_method_call(
2272 "org.freedesktop.systemd1",
2274 "org.freedesktop.DBus.Properties",
2276 log_error("Could not allocate message.");
2280 if (!dbus_message_append_args(m,
2281 DBUS_TYPE_STRING, &interface,
2282 DBUS_TYPE_STRING, &property,
2283 DBUS_TYPE_INVALID)) {
2284 log_error("Could not append arguments to message.");
2288 if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
2289 log_error("Failed to issue method call: %s", error.message);
2293 if (!dbus_message_iter_init(reply, &iter) ||
2294 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2295 log_error("Failed to parse reply.");
2299 dbus_message_iter_recurse(&iter, &sub);
2301 if (streq(interface, "org.freedesktop.systemd1.Unit")) {
2304 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2305 log_error("Failed to parse reply.");
2309 dbus_message_iter_get_basic(&sub, &id);
2310 printf("Unit %s changed.\n", id);
2314 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32) {
2315 log_error("Failed to parse reply.");
2319 dbus_message_iter_get_basic(&sub, &id);
2320 printf("Job %u changed.\n", id);
2326 dbus_message_unref(m);
2329 dbus_message_unref(reply);
2331 dbus_error_free(&error);
2332 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2336 dbus_message_unref(m);
2339 dbus_message_unref(reply);
2341 dbus_error_free(&error);
2342 return DBUS_HANDLER_RESULT_NEED_MEMORY;
2345 static int monitor(DBusConnection *bus, char **args, unsigned n) {
2346 DBusMessage *m = NULL, *reply = NULL;
2350 dbus_error_init(&error);
2353 dbus_bus_add_match(bus,
2355 "sender='org.freedesktop.systemd1',"
2356 "interface='org.freedesktop.systemd1.Manager',"
2357 "path='/org/freedesktop/systemd1'",
2360 if (dbus_error_is_set(&error)) {
2361 log_error("Failed to add match: %s", error.message);
2366 dbus_bus_add_match(bus,
2368 "sender='org.freedesktop.systemd1',"
2369 "interface='org.freedesktop.systemd1.Unit',"
2373 if (dbus_error_is_set(&error)) {
2374 log_error("Failed to add match: %s", error.message);
2379 dbus_bus_add_match(bus,
2381 "sender='org.freedesktop.systemd1',"
2382 "interface='org.freedesktop.systemd1.Job',"
2386 if (dbus_error_is_set(&error)) {
2387 log_error("Failed to add match: %s", error.message);
2393 if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
2394 log_error("Failed to add filter.");
2399 if (!(m = dbus_message_new_method_call(
2400 "org.freedesktop.systemd1",
2401 "/org/freedesktop/systemd1",
2402 "org.freedesktop.systemd1.Manager",
2404 log_error("Could not allocate message.");
2409 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2410 log_error("Failed to issue method call: %s", error.message);
2415 while (dbus_connection_read_write_dispatch(bus, -1))
2422 /* This is slightly dirty, since we don't undo the filter or the matches. */
2425 dbus_message_unref(m);
2428 dbus_message_unref(reply);
2430 dbus_error_free(&error);
2435 static int dump(DBusConnection *bus, char **args, unsigned n) {
2436 DBusMessage *m = NULL, *reply = NULL;
2441 dbus_error_init(&error);
2443 if (!(m = dbus_message_new_method_call(
2444 "org.freedesktop.systemd1",
2445 "/org/freedesktop/systemd1",
2446 "org.freedesktop.systemd1.Manager",
2448 log_error("Could not allocate message.");
2452 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2453 log_error("Failed to issue method call: %s", error.message);
2458 if (!dbus_message_get_args(reply, &error,
2459 DBUS_TYPE_STRING, &text,
2460 DBUS_TYPE_INVALID)) {
2461 log_error("Failed to parse reply: %s", error.message);
2466 fputs(text, stdout);
2472 dbus_message_unref(m);
2475 dbus_message_unref(reply);
2477 dbus_error_free(&error);
2482 static int snapshot(DBusConnection *bus, char **args, unsigned n) {
2483 DBusMessage *m = NULL, *reply = NULL;
2486 const char *name = "", *path, *id;
2487 dbus_bool_t cleanup = FALSE;
2488 DBusMessageIter iter, sub;
2490 *interface = "org.freedesktop.systemd1.Unit",
2493 dbus_error_init(&error);
2495 if (!(m = dbus_message_new_method_call(
2496 "org.freedesktop.systemd1",
2497 "/org/freedesktop/systemd1",
2498 "org.freedesktop.systemd1.Manager",
2499 "CreateSnapshot"))) {
2500 log_error("Could not allocate message.");
2507 if (!dbus_message_append_args(m,
2508 DBUS_TYPE_STRING, &name,
2509 DBUS_TYPE_BOOLEAN, &cleanup,
2510 DBUS_TYPE_INVALID)) {
2511 log_error("Could not append arguments to message.");
2516 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2517 log_error("Failed to issue method call: %s", error.message);
2522 if (!dbus_message_get_args(reply, &error,
2523 DBUS_TYPE_OBJECT_PATH, &path,
2524 DBUS_TYPE_INVALID)) {
2525 log_error("Failed to parse reply: %s", error.message);
2530 dbus_message_unref(m);
2531 if (!(m = dbus_message_new_method_call(
2532 "org.freedesktop.systemd1",
2534 "org.freedesktop.DBus.Properties",
2536 log_error("Could not allocate message.");
2540 if (!dbus_message_append_args(m,
2541 DBUS_TYPE_STRING, &interface,
2542 DBUS_TYPE_STRING, &property,
2543 DBUS_TYPE_INVALID)) {
2544 log_error("Could not append arguments to message.");
2549 dbus_message_unref(reply);
2550 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2551 log_error("Failed to issue method call: %s", error.message);
2556 if (!dbus_message_iter_init(reply, &iter) ||
2557 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2558 log_error("Failed to parse reply.");
2563 dbus_message_iter_recurse(&iter, &sub);
2565 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2566 log_error("Failed to parse reply.");
2571 dbus_message_iter_get_basic(&sub, &id);
2579 dbus_message_unref(m);
2582 dbus_message_unref(reply);
2584 dbus_error_free(&error);
2589 static int delete_snapshot(DBusConnection *bus, char **args, unsigned n) {
2590 DBusMessage *m = NULL, *reply = NULL;
2598 dbus_error_init(&error);
2600 for (i = 1; i < n; i++) {
2601 const char *path = NULL;
2603 if (!(m = dbus_message_new_method_call(
2604 "org.freedesktop.systemd1",
2605 "/org/freedesktop/systemd1",
2606 "org.freedesktop.systemd1.Manager",
2608 log_error("Could not allocate message.");
2613 if (!dbus_message_append_args(m,
2614 DBUS_TYPE_STRING, &args[i],
2615 DBUS_TYPE_INVALID)) {
2616 log_error("Could not append arguments to message.");
2621 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2622 log_error("Failed to issue method call: %s", error.message);
2627 if (!dbus_message_get_args(reply, &error,
2628 DBUS_TYPE_OBJECT_PATH, &path,
2629 DBUS_TYPE_INVALID)) {
2630 log_error("Failed to parse reply: %s", error.message);
2635 dbus_message_unref(m);
2636 if (!(m = dbus_message_new_method_call(
2637 "org.freedesktop.systemd1",
2639 "org.freedesktop.systemd1.Snapshot",
2641 log_error("Could not allocate message.");
2646 dbus_message_unref(reply);
2647 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2648 log_error("Failed to issue method call: %s", error.message);
2653 dbus_message_unref(m);
2654 dbus_message_unref(reply);
2662 dbus_message_unref(m);
2665 dbus_message_unref(reply);
2667 dbus_error_free(&error);
2672 static int daemon_reload(DBusConnection *bus, char **args, unsigned n) {
2673 DBusMessage *m = NULL, *reply = NULL;
2678 dbus_error_init(&error);
2680 if (arg_action == ACTION_RELOAD)
2682 else if (arg_action == ACTION_REEXEC)
2683 method = "Reexecute";
2685 assert(arg_action == ACTION_SYSTEMCTL);
2688 streq(args[0], "clear-jobs") ||
2689 streq(args[0], "cancel") ? "ClearJobs" :
2690 streq(args[0], "daemon-reexec") ? "Reexecute" :
2691 streq(args[0], "reset-maintenance") ? "ResetMaintenance" :
2692 streq(args[0], "daemon-exit") ? "Exit" :
2696 if (!(m = dbus_message_new_method_call(
2697 "org.freedesktop.systemd1",
2698 "/org/freedesktop/systemd1",
2699 "org.freedesktop.systemd1.Manager",
2701 log_error("Could not allocate message.");
2705 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2707 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
2708 /* There's always a fallback possible for
2709 * legacy actions. */
2714 log_error("Failed to issue method call: %s", error.message);
2723 dbus_message_unref(m);
2726 dbus_message_unref(reply);
2728 dbus_error_free(&error);
2733 static int reset_maintenance(DBusConnection *bus, char **args, unsigned n) {
2734 DBusMessage *m = NULL, *reply = NULL;
2740 dbus_error_init(&error);
2743 return daemon_reload(bus, args, n);
2745 for (i = 1; i < n; i++) {
2747 if (!(m = dbus_message_new_method_call(
2748 "org.freedesktop.systemd1",
2749 "/org/freedesktop/systemd1",
2750 "org.freedesktop.systemd1.Manager",
2751 "ResetMaintenanceUnit"))) {
2752 log_error("Could not allocate message.");
2757 if (!dbus_message_append_args(m,
2758 DBUS_TYPE_STRING, args + i,
2759 DBUS_TYPE_INVALID)) {
2760 log_error("Could not append arguments to message.");
2765 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2766 log_error("Failed to issue method call: %s", error.message);
2771 dbus_message_unref(m);
2772 dbus_message_unref(reply);
2780 dbus_message_unref(m);
2783 dbus_message_unref(reply);
2785 dbus_error_free(&error);
2790 static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
2791 DBusMessage *m = NULL, *reply = NULL;
2793 DBusMessageIter iter, sub, sub2;
2796 *interface = "org.freedesktop.systemd1.Manager",
2797 *property = "Environment";
2799 dbus_error_init(&error);
2801 if (!(m = dbus_message_new_method_call(
2802 "org.freedesktop.systemd1",
2803 "/org/freedesktop/systemd1",
2804 "org.freedesktop.DBus.Properties",
2806 log_error("Could not allocate message.");
2810 if (!dbus_message_append_args(m,
2811 DBUS_TYPE_STRING, &interface,
2812 DBUS_TYPE_STRING, &property,
2813 DBUS_TYPE_INVALID)) {
2814 log_error("Could not append arguments to message.");
2819 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2820 log_error("Failed to issue method call: %s", error.message);
2825 if (!dbus_message_iter_init(reply, &iter) ||
2826 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2827 log_error("Failed to parse reply.");
2832 dbus_message_iter_recurse(&iter, &sub);
2834 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
2835 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
2836 log_error("Failed to parse reply.");
2841 dbus_message_iter_recurse(&sub, &sub2);
2843 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
2846 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
2847 log_error("Failed to parse reply.");
2852 dbus_message_iter_get_basic(&sub2, &text);
2853 printf("%s\n", text);
2855 dbus_message_iter_next(&sub2);
2862 dbus_message_unref(m);
2865 dbus_message_unref(reply);
2867 dbus_error_free(&error);
2872 static int set_environment(DBusConnection *bus, char **args, unsigned n) {
2873 DBusMessage *m = NULL, *reply = NULL;
2877 DBusMessageIter iter, sub;
2880 dbus_error_init(&error);
2882 method = streq(args[0], "set-environment")
2884 : "UnsetEnvironment";
2886 if (!(m = dbus_message_new_method_call(
2887 "org.freedesktop.systemd1",
2888 "/org/freedesktop/systemd1",
2889 "org.freedesktop.systemd1.Manager",
2892 log_error("Could not allocate message.");
2896 dbus_message_iter_init_append(m, &iter);
2898 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
2899 log_error("Could not append arguments to message.");
2904 for (i = 1; i < n; i++)
2905 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) {
2906 log_error("Could not append arguments to message.");
2911 if (!dbus_message_iter_close_container(&iter, &sub)) {
2912 log_error("Could not append arguments to message.");
2917 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2918 log_error("Failed to issue method call: %s", error.message);
2927 dbus_message_unref(m);
2930 dbus_message_unref(reply);
2932 dbus_error_free(&error);
2945 static Hashmap *will_install = NULL, *have_installed = NULL;
2946 static Set *remove_symlinks_to = NULL;
2948 static void install_info_free(InstallInfo *i) {
2953 strv_free(i->aliases);
2954 strv_free(i->wanted_by);
2958 static void install_info_hashmap_free(Hashmap *m) {
2961 while ((i = hashmap_steal_first(m)))
2962 install_info_free(i);
2967 static bool unit_name_valid(const char *name) {
2969 /* This is a minimal version of unit_name_valid() from
2975 if (ignore_file(name))
2981 static int install_info_add(const char *name) {
2985 assert(will_install);
2987 if (!unit_name_valid(name))
2990 if (hashmap_get(have_installed, name) ||
2991 hashmap_get(will_install, name))
2994 if (!(i = new0(InstallInfo, 1))) {
2999 if (!(i->name = strdup(name))) {
3004 if ((r = hashmap_put(will_install, i->name, i)) < 0)
3011 install_info_free(i);
3016 static int config_parse_also(
3017 const char *filename,
3019 const char *section,
3033 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
3037 if (!(n = strndup(w, l)))
3040 r = install_info_add(n);
3050 static int mark_symlink_for_removal(const char *p) {
3055 assert(path_is_absolute(p));
3057 if (!remove_symlinks_to)
3060 if (!(n = strdup(p)))
3063 path_kill_slashes(n);
3065 if ((r = set_put(remove_symlinks_to, n)) < 0) {
3067 return r == -EEXIST ? 0 : r;
3073 static int remove_marked_symlinks_fd(int fd, const char *config_path, const char *root, bool *deleted) {
3082 if (!(d = fdopendir(fd))) {
3083 close_nointr_nofail(fd);
3089 while ((de = readdir(d))) {
3090 bool is_dir = false, is_link = false;
3092 if (ignore_file(de->d_name))
3095 if (de->d_type == DT_LNK)
3097 else if (de->d_type == DT_DIR)
3099 else if (de->d_type == DT_UNKNOWN) {
3102 if (fstatat(fd, de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) {
3103 log_error("Failed to stat %s/%s: %m", root, de->d_name);
3110 is_link = S_ISLNK(st.st_mode);
3111 is_dir = S_ISDIR(st.st_mode);
3119 if ((nfd = openat(fd, de->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW)) < 0) {
3120 log_error("Failed to open %s/%s: %m", root, de->d_name);
3127 if (asprintf(&p, "%s/%s", root, de->d_name) < 0) {
3128 log_error("Failed to allocate directory string.");
3129 close_nointr_nofail(nfd);
3134 /* This will close nfd, regardless whether it succeeds or not */
3135 q = remove_marked_symlinks_fd(nfd, config_path, p, deleted);
3141 } else if (is_link) {
3145 if (asprintf(&p, "%s/%s", root, de->d_name) < 0) {
3146 log_error("Failed to allocate symlink string.");
3151 if ((q = readlink_and_make_absolute(p, &dest)) < 0) {
3152 log_error("Cannot read symlink %s: %s", p, strerror(-q));
3160 if ((c = canonicalize_file_name(dest))) {
3161 /* This might fail if the destination
3162 * is already removed */
3168 path_kill_slashes(dest);
3169 if (set_get(remove_symlinks_to, dest)) {
3172 log_info("rm '%s'", p);
3174 if (unlink(p) < 0) {
3175 log_error("Cannot unlink symlink %s: %m", p);
3180 rmdir_parents(p, config_path);
3181 path_kill_slashes(p);
3183 if (!set_get(remove_symlinks_to, p)) {
3185 if ((r = mark_symlink_for_removal(p)) < 0) {
3204 static int remove_marked_symlinks(const char *config_path) {
3208 assert(config_path);
3210 if (set_size(remove_symlinks_to) <= 0)
3213 if ((fd = open(config_path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW)) < 0)
3220 if ((cfd = dup(fd)) < 0) {
3225 /* This takes possession of cfd and closes it */
3226 if ((q = remove_marked_symlinks_fd(cfd, config_path, config_path, &deleted)) < 0) {
3232 close_nointr_nofail(fd);
3237 static int create_symlink(const char *verb, const char *old_path, const char *new_path) {
3244 if (streq(verb, "enable")) {
3247 mkdir_parents(new_path, 0755);
3249 if (symlink(old_path, new_path) >= 0) {
3252 log_info("ln -s '%s' '%s'", old_path, new_path);
3257 if (errno != EEXIST) {
3258 log_error("Cannot link %s to %s: %m", old_path, new_path);
3262 if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
3264 if (errno == EINVAL) {
3265 log_error("Cannot link %s to %s, file exists already and is not a symlink.", old_path, new_path);
3269 log_error("readlink() failed: %s", strerror(-r));
3273 if (streq(dest, old_path)) {
3279 log_error("Cannot link %s to %s, symlink exists already and points to %s.", old_path, new_path, dest);
3288 log_info("ln -s '%s' '%s'", old_path, new_path);
3290 if (symlink(old_path, new_path) >= 0)
3293 log_error("Cannot link %s to %s: %m", old_path, new_path);
3296 } else if (streq(verb, "disable")) {
3299 if ((r = mark_symlink_for_removal(old_path)) < 0)
3302 if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
3303 if (errno == ENOENT)
3306 if (errno == EINVAL) {
3307 log_warning("File %s not a symlink, ignoring.", old_path);
3311 log_error("readlink() failed: %s", strerror(-r));
3315 if (!streq(dest, old_path)) {
3316 log_warning("File %s not a symlink to %s but points to %s, ignoring.", new_path, old_path, dest);
3323 if ((r = mark_symlink_for_removal(new_path)) < 0)
3327 log_info("rm '%s'", new_path);
3329 if (unlink(new_path) >= 0)
3332 log_error("Cannot unlink %s: %m", new_path);
3335 } else if (streq(verb, "is-enabled")) {
3338 if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
3340 if (errno == ENOENT || errno == EINVAL)
3343 log_error("readlink() failed: %s", strerror(-r));
3347 if (streq(dest, old_path)) {
3355 assert_not_reached("Unknown action.");
3358 static int install_info_symlink_alias(const char *verb, InstallInfo *i, const char *config_path) {
3360 char *alias_path = NULL;
3365 assert(config_path);
3367 STRV_FOREACH(s, i->aliases) {
3369 if (!unit_name_valid(*s)) {
3370 log_error("Invalid name %s.", *s);
3376 if (!(alias_path = path_make_absolute(*s, config_path))) {
3377 log_error("Out of memory");
3382 if ((r = create_symlink(verb, i->path, alias_path)) != 0)
3385 if (streq(verb, "disable"))
3386 rmdir_parents(alias_path, config_path);
3397 static int install_info_symlink_wants(const char *verb, InstallInfo *i, const char *config_path) {
3399 char *alias_path = NULL;
3404 assert(config_path);
3406 STRV_FOREACH(s, i->wanted_by) {
3407 if (!unit_name_valid(*s)) {
3408 log_error("Invalid name %s.", *s);
3416 if (asprintf(&alias_path, "%s/%s.wants/%s", config_path, *s, i->name) < 0) {
3417 log_error("Out of memory");
3422 if ((r = create_symlink(verb, i->path, alias_path)) != 0)
3425 if (streq(verb, "disable"))
3426 rmdir_parents(alias_path, config_path);
3437 static int install_info_apply(const char *verb, LookupPaths *paths, InstallInfo *i, const char *config_path) {
3439 const ConfigItem items[] = {
3440 { "Alias", config_parse_strv, &i->aliases, "Install" },
3441 { "WantedBy", config_parse_strv, &i->wanted_by, "Install" },
3442 { "Also", config_parse_also, NULL, "Install" },
3444 { NULL, NULL, NULL, NULL }
3448 char *filename = NULL;
3455 STRV_FOREACH(p, paths->unit_path) {
3458 if (!(filename = path_make_absolute(i->name, *p))) {
3459 log_error("Out of memory");
3463 /* Ensure that we don't follow symlinks */
3464 if ((fd = open(filename, O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NOCTTY)) >= 0)
3465 if ((f = fdopen(fd, "re")))
3468 if (errno == ELOOP) {
3469 log_error("Refusing to operate on symlinks, please pass unit names or absolute paths to unit files.");
3474 if (errno != ENOENT) {
3475 log_error("Failed to open %s: %m", filename);
3485 log_error("Couldn't find %s.", i->name);
3491 if ((r = config_parse(filename, f, NULL, items, true, i)) < 0) {
3498 if ((r = install_info_symlink_alias(verb, i, config_path)) != 0)
3501 if ((r = install_info_symlink_wants(verb, i, config_path)) != 0)
3504 if ((r = mark_symlink_for_removal(filename)) < 0)
3507 if ((r = remove_marked_symlinks(config_path)) < 0)
3513 static char *get_config_path(void) {
3515 if (arg_session && arg_global)
3516 return strdup(SESSION_CONFIG_UNIT_PATH);
3521 if (session_config_home(&p) < 0)
3527 return strdup(SYSTEM_CONFIG_UNIT_PATH);
3530 static int enable_unit(DBusConnection *bus, char **args, unsigned n) {
3534 char *config_path = NULL;
3537 const char *verb = args[0];
3539 dbus_error_init(&error);
3542 if ((r = lookup_paths_init(&paths, arg_session ? MANAGER_SESSION : MANAGER_SYSTEM)) < 0) {
3543 log_error("Failed to determine lookup paths: %s", strerror(-r));
3547 if (!(config_path = get_config_path())) {
3548 log_error("Failed to determine config path");
3553 will_install = hashmap_new(string_hash_func, string_compare_func);
3554 have_installed = hashmap_new(string_hash_func, string_compare_func);
3556 if (!will_install || !have_installed) {
3557 log_error("Failed to allocate unit sets.");
3562 if (!arg_defaults && streq(verb, "disable"))
3563 if (!(remove_symlinks_to = set_new(string_hash_func, string_compare_func))) {
3564 log_error("Failed to allocate symlink sets.");
3569 for (j = 1; j < n; j++)
3570 if ((r = install_info_add(args[j])) < 0)
3573 while ((i = hashmap_first(will_install))) {
3576 assert_se(hashmap_move_one(have_installed, will_install, i->name) == 0);
3578 if ((q = install_info_apply(verb, &paths, i, config_path)) != 0) {
3586 /* In test mode and found something */
3592 if (streq(verb, "is-enabled"))
3593 r = r > 0 ? 0 : -ENOENT;
3595 /* Don't try to reload anything if the user asked us to not do this */
3597 /* Don't try to reload anything when updating a unit globally */
3599 /* Don't try to reload anything if we are called for system changes but the system wasn't booted with systemd */
3600 (arg_session || sd_booted() > 0) &&
3601 /* Don't try to reload anything if we are running in a chroot environment */
3602 (arg_session || running_in_chroot() <= 0) ) {
3605 if ((q = daemon_reload(bus, args, n)) < 0)
3610 install_info_hashmap_free(will_install);
3611 install_info_hashmap_free(have_installed);
3613 set_free_free(remove_symlinks_to);
3615 lookup_paths_free(&paths);
3622 static int systemctl_help(void) {
3624 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
3625 "Send control commands to or query the systemd manager.\n\n"
3626 " -h --help Show this help\n"
3627 " -t --type=TYPE List only units of a particular type\n"
3628 " -p --property=NAME Show only properties by this name\n"
3629 " -a --all Show all units/properties, including dead/empty ones\n"
3630 " --full Don't ellipsize unit names on output\n"
3631 " --fail When queueing a new job, fail if conflicting jobs are\n"
3633 " -q --quiet Suppress output\n"
3634 " --no-block Do not wait until operation finished\n"
3635 " --system Connect to system bus\n"
3636 " --session Connect to session bus\n"
3637 " --order When generating graph for dot, show only order\n"
3638 " --require When generating graph for dot, show only requirement\n"
3639 " --no-wall Don't send wall message before halt/power-off/reboot\n"
3640 " --global Enable/disable unit files globally\n"
3641 " --no-reload When enabling/disabling unit files, don't reload daemon\n"
3643 " --force When enabling unit files, override existing symlinks\n"
3644 " --defaults When disabling unit files, remove default symlinks only\n\n"
3646 " list-units List units\n"
3647 " start [NAME...] Start (activate) one or more units\n"
3648 " stop [NAME...] Stop (deactivate) one or more units\n"
3649 " reload [NAME...] Reload one or more units\n"
3650 " restart [NAME...] Start or restart one or more units\n"
3651 " try-restart [NAME...] Restart one or more units if active\n"
3652 " reload-or-restart [NAME...] Reload one or more units is possible,\n"
3653 " otherwise start or restart\n"
3654 " reload-or-try-restart [NAME...] Reload one or more units is possible,\n"
3655 " otherwise restart if active\n"
3656 " isolate [NAME] Start one unit and stop all others\n"
3657 " is-active [NAME...] Check whether units are active\n"
3658 " status [NAME...] Show runtime status of one or more units\n"
3659 " show [NAME...|JOB...] Show properties of one or more\n"
3660 " units/jobs or the manager\n"
3661 " reset-maintenance [NAME...] Reset maintenance state for all, one,\n"
3663 " enable [NAME...] Enable one or more unit files\n"
3664 " disable [NAME...] Disable one or more unit files\n"
3665 " is-enabled [NAME...] Check whether unit files are enabled\n"
3666 " load [NAME...] Load one or more units\n"
3667 " list-jobs List jobs\n"
3668 " cancel [JOB...] Cancel all, one, or more jobs\n"
3669 " monitor Monitor unit/job changes\n"
3670 " dump Dump server status\n"
3671 " dot Dump dependency graph for dot(1)\n"
3672 " snapshot [NAME] Create a snapshot\n"
3673 " delete [NAME...] Remove one or more snapshots\n"
3674 " daemon-reload Reload systemd manager configuration\n"
3675 " daemon-reexec Reexecute systemd manager\n"
3676 " daemon-exit Ask the systemd manager to quit\n"
3677 " show-environment Dump environment\n"
3678 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
3679 " unset-environment [NAME...] Unset one or more environment variables\n"
3680 " halt Shut down and halt the system\n"
3681 " poweroff Shut down and power-off the system\n"
3682 " reboot Shut down and reboot the system\n"
3683 " rescue Enter system rescue mode\n"
3684 " emergency Enter system emergency mode\n"
3685 " default Enter system default mode\n",
3686 program_invocation_short_name);
3691 static int halt_help(void) {
3693 printf("%s [OPTIONS...]\n\n"
3694 "%s the system.\n\n"
3695 " --help Show this help\n"
3696 " --halt Halt the machine\n"
3697 " -p --poweroff Switch off the machine\n"
3698 " --reboot Reboot the machine\n"
3699 " -f --force Force immediate halt/power-off/reboot\n"
3700 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
3701 " -d --no-wtmp Don't write wtmp record\n"
3702 " -n --no-sync Don't sync before halt/power-off/reboot\n"
3703 " --no-wall Don't send wall message before halt/power-off/reboot\n",
3704 program_invocation_short_name,
3705 arg_action == ACTION_REBOOT ? "Reboot" :
3706 arg_action == ACTION_POWEROFF ? "Power off" :
3712 static int shutdown_help(void) {
3714 printf("%s [OPTIONS...] [now] [WALL...]\n\n"
3715 "Shut down the system.\n\n"
3716 " --help Show this help\n"
3717 " -H --halt Halt the machine\n"
3718 " -P --poweroff Power-off the machine\n"
3719 " -r --reboot Reboot the machine\n"
3720 " -h Equivalent to --poweroff, overriden by --halt\n"
3721 " -k Don't halt/power-off/reboot, just send warnings\n"
3722 " --no-wall Don't send wall message before halt/power-off/reboot\n",
3723 program_invocation_short_name);
3728 static int telinit_help(void) {
3730 printf("%s [OPTIONS...] {COMMAND}\n\n"
3731 "Send control commands to the init daemon.\n\n"
3732 " --help Show this help\n"
3733 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
3735 " 0 Power-off the machine\n"
3736 " 6 Reboot the machine\n"
3737 " 2, 3, 4, 5 Start runlevelX.target unit\n"
3738 " 1, s, S Enter rescue mode\n"
3739 " q, Q Reload init daemon configuration\n"
3740 " u, U Reexecute init daemon\n",
3741 program_invocation_short_name);
3746 static int runlevel_help(void) {
3748 printf("%s [OPTIONS...]\n\n"
3749 "Prints the previous and current runlevel of the init system.\n\n"
3750 " --help Show this help\n",
3751 program_invocation_short_name);
3756 static int systemctl_parse_argv(int argc, char *argv[]) {
3773 static const struct option options[] = {
3774 { "help", no_argument, NULL, 'h' },
3775 { "type", required_argument, NULL, 't' },
3776 { "property", required_argument, NULL, 'p' },
3777 { "all", no_argument, NULL, 'a' },
3778 { "full", no_argument, NULL, ARG_FULL },
3779 { "fail", no_argument, NULL, ARG_FAIL },
3780 { "session", no_argument, NULL, ARG_SESSION },
3781 { "system", no_argument, NULL, ARG_SYSTEM },
3782 { "global", no_argument, NULL, ARG_GLOBAL },
3783 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
3784 { "no-wall", no_argument, NULL, ARG_NO_WALL },
3785 { "quiet", no_argument, NULL, 'q' },
3786 { "order", no_argument, NULL, ARG_ORDER },
3787 { "require", no_argument, NULL, ARG_REQUIRE },
3788 { "force", no_argument, NULL, ARG_FORCE },
3789 { "no-reload", no_argument, NULL, ARG_NO_RELOAD },
3790 { "defaults", no_argument, NULL, ARG_DEFAULTS },
3791 { NULL, 0, NULL, 0 }
3799 while ((c = getopt_long(argc, argv, "ht:p:aq", options, NULL)) >= 0) {
3814 if (!(l = strv_append(arg_property, optarg)))
3817 strv_free(arg_property);
3820 /* If the user asked for a particular
3821 * property, show it to him, even if it is
3840 arg_session = false;
3844 arg_no_block = true;
3852 arg_dot = DOT_ORDER;
3856 arg_dot = DOT_REQUIRE;
3872 arg_no_reload = true;
3881 arg_defaults = true;
3888 log_error("Unknown option code %c", c);
3896 static int halt_parse_argv(int argc, char *argv[]) {
3905 static const struct option options[] = {
3906 { "help", no_argument, NULL, ARG_HELP },
3907 { "halt", no_argument, NULL, ARG_HALT },
3908 { "poweroff", no_argument, NULL, 'p' },
3909 { "reboot", no_argument, NULL, ARG_REBOOT },
3910 { "force", no_argument, NULL, 'f' },
3911 { "wtmp-only", no_argument, NULL, 'w' },
3912 { "no-wtmp", no_argument, NULL, 'd' },
3913 { "no-sync", no_argument, NULL, 'n' },
3914 { "no-wall", no_argument, NULL, ARG_NO_WALL },
3915 { NULL, 0, NULL, 0 }
3923 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
3924 if (runlevel == '0' || runlevel == '6')
3925 arg_immediate = true;
3927 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
3935 arg_action = ACTION_HALT;
3939 if (arg_action != ACTION_REBOOT)
3940 arg_action = ACTION_POWEROFF;
3944 arg_action = ACTION_REBOOT;
3948 arg_immediate = true;
3969 /* Compatibility nops */
3976 log_error("Unknown option code %c", c);
3981 if (optind < argc) {
3982 log_error("Too many arguments.");
3989 static int shutdown_parse_argv(int argc, char *argv[]) {
3996 static const struct option options[] = {
3997 { "help", no_argument, NULL, ARG_HELP },
3998 { "halt", no_argument, NULL, 'H' },
3999 { "poweroff", no_argument, NULL, 'P' },
4000 { "reboot", no_argument, NULL, 'r' },
4001 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4002 { NULL, 0, NULL, 0 }
4010 while ((c = getopt_long(argc, argv, "HPrhkt:a", options, NULL)) >= 0) {
4018 arg_action = ACTION_HALT;
4022 arg_action = ACTION_POWEROFF;
4026 arg_action = ACTION_REBOOT;
4030 if (arg_action != ACTION_HALT)
4031 arg_action = ACTION_POWEROFF;
4044 /* Compatibility nops */
4051 log_error("Unknown option code %c", c);
4056 if (argc > optind && !streq(argv[optind], "now"))
4057 log_warning("First argument '%s' isn't 'now'. Ignoring.", argv[optind]);
4059 /* We ignore the time argument */
4060 if (argc > optind + 1)
4061 arg_wall = argv + optind + 1;
4068 static int telinit_parse_argv(int argc, char *argv[]) {
4075 static const struct option options[] = {
4076 { "help", no_argument, NULL, ARG_HELP },
4077 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4078 { NULL, 0, NULL, 0 }
4081 static const struct {
4085 { '0', ACTION_POWEROFF },
4086 { '6', ACTION_REBOOT },
4087 { '1', ACTION_RESCUE },
4088 { '2', ACTION_RUNLEVEL2 },
4089 { '3', ACTION_RUNLEVEL3 },
4090 { '4', ACTION_RUNLEVEL4 },
4091 { '5', ACTION_RUNLEVEL5 },
4092 { 's', ACTION_RESCUE },
4093 { 'S', ACTION_RESCUE },
4094 { 'q', ACTION_RELOAD },
4095 { 'Q', ACTION_RELOAD },
4096 { 'u', ACTION_REEXEC },
4097 { 'U', ACTION_REEXEC }
4106 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4121 log_error("Unknown option code %c", c);
4126 if (optind >= argc) {
4131 if (optind + 1 < argc) {
4132 log_error("Too many arguments.");
4136 if (strlen(argv[optind]) != 1) {
4137 log_error("Expected single character argument.");
4141 for (i = 0; i < ELEMENTSOF(table); i++)
4142 if (table[i].from == argv[optind][0])
4145 if (i >= ELEMENTSOF(table)) {
4146 log_error("Unknown command %s.", argv[optind]);
4150 arg_action = table[i].to;
4157 static int runlevel_parse_argv(int argc, char *argv[]) {
4163 static const struct option options[] = {
4164 { "help", no_argument, NULL, ARG_HELP },
4165 { NULL, 0, NULL, 0 }
4173 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4184 log_error("Unknown option code %c", c);
4189 if (optind < argc) {
4190 log_error("Too many arguments.");
4197 static int parse_argv(int argc, char *argv[]) {
4201 if (program_invocation_short_name) {
4203 if (strstr(program_invocation_short_name, "halt")) {
4204 arg_action = ACTION_HALT;
4205 return halt_parse_argv(argc, argv);
4206 } else if (strstr(program_invocation_short_name, "poweroff")) {
4207 arg_action = ACTION_POWEROFF;
4208 return halt_parse_argv(argc, argv);
4209 } else if (strstr(program_invocation_short_name, "reboot")) {
4210 arg_action = ACTION_REBOOT;
4211 return halt_parse_argv(argc, argv);
4212 } else if (strstr(program_invocation_short_name, "shutdown")) {
4213 arg_action = ACTION_POWEROFF;
4214 return shutdown_parse_argv(argc, argv);
4215 } else if (strstr(program_invocation_short_name, "init")) {
4217 if (sd_booted() > 0) {
4218 arg_action = ACTION_INVALID;
4219 return telinit_parse_argv(argc, argv);
4221 /* Hmm, so some other init system is
4222 * running, we need to forward this
4223 * request to it. For now we simply
4224 * guess that it is Upstart. */
4226 execv("/lib/upstart/telinit", argv);
4228 log_error("Couldn't find an alternative telinit implementation to spawn.");
4232 } else if (strstr(program_invocation_short_name, "runlevel")) {
4233 arg_action = ACTION_RUNLEVEL;
4234 return runlevel_parse_argv(argc, argv);
4238 arg_action = ACTION_SYSTEMCTL;
4239 return systemctl_parse_argv(argc, argv);
4242 static int action_to_runlevel(void) {
4244 static const char table[_ACTION_MAX] = {
4245 [ACTION_HALT] = '0',
4246 [ACTION_POWEROFF] = '0',
4247 [ACTION_REBOOT] = '6',
4248 [ACTION_RUNLEVEL2] = '2',
4249 [ACTION_RUNLEVEL3] = '3',
4250 [ACTION_RUNLEVEL4] = '4',
4251 [ACTION_RUNLEVEL5] = '5',
4252 [ACTION_RESCUE] = '1'
4255 assert(arg_action < _ACTION_MAX);
4257 return table[arg_action];
4260 static int talk_upstart(void) {
4261 DBusMessage *m = NULL, *reply = NULL;
4263 int previous, rl, r;
4265 env1_buf[] = "RUNLEVEL=X",
4266 env2_buf[] = "PREVLEVEL=X";
4267 char *env1 = env1_buf, *env2 = env2_buf;
4268 const char *emit = "runlevel";
4269 dbus_bool_t b_false = FALSE;
4270 DBusMessageIter iter, sub;
4271 DBusConnection *bus;
4273 dbus_error_init(&error);
4275 if (!(rl = action_to_runlevel()))
4278 if (utmp_get_runlevel(&previous, NULL) < 0)
4281 if (!(bus = dbus_connection_open_private("unix:abstract=/com/ubuntu/upstart", &error))) {
4282 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
4287 log_error("Failed to connect to Upstart bus: %s", error.message);
4292 if ((r = bus_check_peercred(bus)) < 0) {
4293 log_error("Failed to verify owner of bus.");
4297 if (!(m = dbus_message_new_method_call(
4298 "com.ubuntu.Upstart",
4299 "/com/ubuntu/Upstart",
4300 "com.ubuntu.Upstart0_6",
4303 log_error("Could not allocate message.");
4308 dbus_message_iter_init_append(m, &iter);
4310 env1_buf[sizeof(env1_buf)-2] = rl;
4311 env2_buf[sizeof(env2_buf)-2] = previous;
4313 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
4314 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
4315 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
4316 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
4317 !dbus_message_iter_close_container(&iter, &sub) ||
4318 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
4319 log_error("Could not append arguments to message.");
4324 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
4326 if (error_is_no_service(&error)) {
4331 log_error("Failed to issue method call: %s", error.message);
4340 dbus_message_unref(m);
4343 dbus_message_unref(reply);
4346 dbus_connection_close(bus);
4347 dbus_connection_unref(bus);
4350 dbus_error_free(&error);
4355 static int talk_initctl(void) {
4356 struct init_request request;
4360 if (!(rl = action_to_runlevel()))
4364 request.magic = INIT_MAGIC;
4365 request.sleeptime = 0;
4366 request.cmd = INIT_CMD_RUNLVL;
4367 request.runlevel = rl;
4369 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
4371 if (errno == ENOENT)
4374 log_error("Failed to open "INIT_FIFO": %m");
4379 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
4380 close_nointr_nofail(fd);
4383 log_error("Failed to write to "INIT_FIFO": %m");
4384 return errno ? -errno : -EIO;
4390 static int systemctl_main(DBusConnection *bus, int argc, char *argv[], DBusError *error) {
4392 static const struct {
4400 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
4402 { "list-units", LESS, 1, list_units },
4403 { "list-jobs", EQUAL, 1, list_jobs },
4404 { "clear-jobs", EQUAL, 1, daemon_reload },
4405 { "load", MORE, 2, load_unit },
4406 { "cancel", MORE, 2, cancel_job },
4407 { "start", MORE, 2, start_unit },
4408 { "stop", MORE, 2, start_unit },
4409 { "reload", MORE, 2, start_unit },
4410 { "restart", MORE, 2, start_unit },
4411 { "try-restart", MORE, 2, start_unit },
4412 { "reload-or-restart", MORE, 2, start_unit },
4413 { "reload-or-try-restart", MORE, 2, start_unit },
4414 { "force-reload", MORE, 2, start_unit }, /* For compatibility with SysV */
4415 { "condrestart", MORE, 2, start_unit }, /* For compatibility with RH */
4416 { "isolate", EQUAL, 2, start_unit },
4417 { "is-active", MORE, 2, check_unit },
4418 { "check", MORE, 2, check_unit },
4419 { "show", MORE, 1, show },
4420 { "status", MORE, 2, show },
4421 { "monitor", EQUAL, 1, monitor },
4422 { "dump", EQUAL, 1, dump },
4423 { "dot", EQUAL, 1, dot },
4424 { "snapshot", LESS, 2, snapshot },
4425 { "delete", MORE, 2, delete_snapshot },
4426 { "daemon-reload", EQUAL, 1, daemon_reload },
4427 { "daemon-reexec", EQUAL, 1, daemon_reload },
4428 { "daemon-exit", EQUAL, 1, daemon_reload },
4429 { "show-environment", EQUAL, 1, show_enviroment },
4430 { "set-environment", MORE, 2, set_environment },
4431 { "unset-environment", MORE, 2, set_environment },
4432 { "halt", EQUAL, 1, start_special },
4433 { "poweroff", EQUAL, 1, start_special },
4434 { "reboot", EQUAL, 1, start_special },
4435 { "default", EQUAL, 1, start_special },
4436 { "rescue", EQUAL, 1, start_special },
4437 { "emergency", EQUAL, 1, start_special },
4438 { "reset-maintenance", MORE, 1, reset_maintenance },
4439 { "enable", MORE, 2, enable_unit },
4440 { "disable", MORE, 2, enable_unit },
4441 { "is-enabled", MORE, 2, enable_unit }
4451 left = argc - optind;
4454 /* Special rule: no arguments means "list-units" */
4457 if (streq(argv[optind], "help")) {
4462 for (i = 0; i < ELEMENTSOF(verbs); i++)
4463 if (streq(argv[optind], verbs[i].verb))
4466 if (i >= ELEMENTSOF(verbs)) {
4467 log_error("Unknown operation %s", argv[optind]);
4472 switch (verbs[i].argc_cmp) {
4475 if (left != verbs[i].argc) {
4476 log_error("Invalid number of arguments.");
4483 if (left < verbs[i].argc) {
4484 log_error("Too few arguments.");
4491 if (left > verbs[i].argc) {
4492 log_error("Too many arguments.");
4499 assert_not_reached("Unknown comparison operator.");
4502 /* Require a bus connection for all operations but
4504 if (!streq(verbs[i].verb, "enable") &&
4505 !streq(verbs[i].verb, "disable") &&
4507 log_error("Failed to get D-Bus connection: %s", error->message);
4511 return verbs[i].dispatch(bus, argv + optind, left);
4514 static int reload_with_fallback(DBusConnection *bus) {
4518 /* First, try systemd via D-Bus. */
4519 if ((r = daemon_reload(bus, NULL, 0)) > 0)
4523 /* Nothing else worked, so let's try signals */
4524 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
4526 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
4527 log_error("kill() failed: %m");
4534 static int start_with_fallback(DBusConnection *bus) {
4538 /* First, try systemd via D-Bus. */
4539 if ((r = start_unit(bus, NULL, 0)) > 0)
4543 /* Hmm, talking to systemd via D-Bus didn't work. Then
4544 * let's try to talk to Upstart via D-Bus. */
4545 if ((r = talk_upstart()) > 0)
4548 /* Nothing else worked, so let's try
4550 if ((r = talk_initctl()) != 0)
4553 log_error("Failed to talk to init daemon.");
4557 warn_wall(arg_action);
4561 static int halt_main(DBusConnection *bus) {
4564 if (geteuid() != 0) {
4565 log_error("Must to be root.");
4569 if (!arg_dry && !arg_immediate)
4570 return start_with_fallback(bus);
4573 if ((r = utmp_put_shutdown(0)) < 0)
4574 log_warning("Failed to write utmp record: %s", strerror(-r));
4582 /* Make sure C-A-D is handled by the kernel from this
4584 reboot(RB_ENABLE_CAD);
4586 switch (arg_action) {
4589 log_info("Halting");
4590 reboot(RB_HALT_SYSTEM);
4593 case ACTION_POWEROFF:
4594 log_info("Powering off");
4595 reboot(RB_POWER_OFF);
4599 log_info("Rebooting");
4600 reboot(RB_AUTOBOOT);
4604 assert_not_reached("Unknown halt action.");
4607 /* We should never reach this. */
4611 static int runlevel_main(void) {
4612 int r, runlevel, previous;
4614 if ((r = utmp_get_runlevel(&runlevel, &previous)) < 0) {
4615 printf("unknown\n");
4620 previous <= 0 ? 'N' : previous,
4621 runlevel <= 0 ? 'N' : runlevel);
4626 int main(int argc, char*argv[]) {
4628 DBusConnection *bus = NULL;
4631 dbus_error_init(&error);
4633 log_parse_environment();
4635 if ((r = parse_argv(argc, argv)) < 0)
4642 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
4643 * let's shortcut this */
4644 if (arg_action == ACTION_RUNLEVEL) {
4645 retval = runlevel_main() < 0;
4649 bus_connect(arg_session ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &bus, &private_bus, &error);
4651 switch (arg_action) {
4653 case ACTION_SYSTEMCTL: {
4654 retval = systemctl_main(bus, argc, argv, &error) < 0;
4659 case ACTION_POWEROFF:
4661 retval = halt_main(bus) < 0;
4664 case ACTION_RUNLEVEL2:
4665 case ACTION_RUNLEVEL3:
4666 case ACTION_RUNLEVEL4:
4667 case ACTION_RUNLEVEL5:
4669 case ACTION_EMERGENCY:
4670 case ACTION_DEFAULT:
4671 retval = start_with_fallback(bus) < 0;
4676 retval = reload_with_fallback(bus) < 0;
4679 case ACTION_INVALID:
4680 case ACTION_RUNLEVEL:
4682 assert_not_reached("Unknown action");
4688 dbus_connection_close(bus);
4689 dbus_connection_unref(bus);
4692 dbus_error_free(&error);
4696 strv_free(arg_property);