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 bool on_tty(void) {
102 if (_unlikely_(t < 0))
103 t = isatty(STDOUT_FILENO) > 0;
108 static const char *ansi_highlight(bool b) {
113 return b ? ANSI_HIGHLIGHT_ON : ANSI_HIGHLIGHT_OFF;
116 static const char *ansi_highlight_green(bool b) {
121 return b ? ANSI_HIGHLIGHT_GREEN_ON : ANSI_HIGHLIGHT_OFF;
124 static bool error_is_no_service(DBusError *error) {
128 if (!dbus_error_is_set(error))
131 if (dbus_error_has_name(error, DBUS_ERROR_NAME_HAS_NO_OWNER))
134 if (dbus_error_has_name(error, DBUS_ERROR_SERVICE_UNKNOWN))
137 return startswith(error->name, "org.freedesktop.DBus.Error.Spawn.");
140 static int bus_iter_get_basic_and_next(DBusMessageIter *iter, int type, void *data, bool next) {
145 if (dbus_message_iter_get_arg_type(iter) != type)
148 dbus_message_iter_get_basic(iter, data);
150 if (!dbus_message_iter_next(iter) != !next)
156 static void warn_wall(enum action action) {
157 static const char *table[_ACTION_MAX] = {
158 [ACTION_HALT] = "The system is going down for system halt NOW!",
159 [ACTION_REBOOT] = "The system is going down for reboot NOW!",
160 [ACTION_POWEROFF] = "The system is going down for power-off NOW!",
161 [ACTION_RESCUE] = "The system is going down to rescue mode NOW!",
162 [ACTION_EMERGENCY] = "The system is going down to emergency mode NOW!"
171 if (!(p = strv_join(arg_wall, " "))) {
172 log_error("Failed to join strings.");
188 utmp_wall(table[action]);
191 static int list_units(DBusConnection *bus, char **args, unsigned n) {
192 DBusMessage *m = NULL, *reply = NULL;
195 DBusMessageIter iter, sub, sub2;
198 dbus_error_init(&error);
202 if (!(m = dbus_message_new_method_call(
203 "org.freedesktop.systemd1",
204 "/org/freedesktop/systemd1",
205 "org.freedesktop.systemd1.Manager",
207 log_error("Could not allocate message.");
211 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
212 log_error("Failed to issue method call: %s", error.message);
217 if (!dbus_message_iter_init(reply, &iter) ||
218 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
219 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
220 log_error("Failed to parse reply.");
225 dbus_message_iter_recurse(&iter, &sub);
227 if (isatty(STDOUT_FILENO))
228 printf("%-45s %-6s %-12s %-12s %-15s %s\n", "UNIT", "LOAD", "ACTIVE", "SUB", "JOB", "DESCRIPTION");
230 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
231 const char *id, *description, *load_state, *active_state, *sub_state, *following, *unit_path, *job_type, *job_path, *dot;
234 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
235 log_error("Failed to parse reply.");
240 dbus_message_iter_recurse(&sub, &sub2);
242 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
243 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
244 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
245 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
246 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
247 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &following, true) < 0 ||
248 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, true) < 0 ||
249 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &job_id, true) < 0 ||
250 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &job_type, true) < 0 ||
251 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, false) < 0) {
252 log_error("Failed to parse reply.");
257 if ((!arg_type || ((dot = strrchr(id, '.')) &&
258 streq(dot+1, arg_type))) &&
259 (arg_all || !(streq(active_state, "inactive") || following[0]) || job_id > 0)) {
262 const char *on, *off;
264 if (streq(active_state, "maintenance")) {
265 on = ansi_highlight(true);
266 off = ansi_highlight(false);
270 e = arg_full ? NULL : ellipsize(id, 45, 33);
271 printf("%-45s %-6s %s%-12s %-12s%s%n", e ? e : id, load_state, on, active_state, sub_state, off, &a);
274 a -= strlen(on) + strlen(off);
277 printf(" => %-12s%n", job_type, &b);
281 if (a + b + 2 < columns()) {
285 printf(" %.*s", columns() - a - b - 2, description);
292 dbus_message_iter_next(&sub);
295 if (isatty(STDOUT_FILENO)) {
297 printf("\nLOAD = Load State, reflects whether the unit configuration was properly loaded.\n"
298 "ACTIVE = Active State, the high-level unit activation state, i.e. generalization of the substate.\n"
299 "SUB = Substate, the low-level unit activation state, possible values depend on unit type.\n"
300 "JOB = Job, shows pending jobs for the unit.\n");
303 printf("\n%u units listed.\n", k);
305 printf("\n%u units listed. Pass --all to see inactive units, too.\n", k);
312 dbus_message_unref(m);
315 dbus_message_unref(reply);
317 dbus_error_free(&error);
322 static int dot_one_property(const char *name, const char *prop, DBusMessageIter *iter) {
323 static const char * const colors[] = {
324 "Requires", "[color=\"black\"]",
325 "RequiresOverridable", "[color=\"black\"]",
326 "Requisite", "[color=\"darkblue\"]",
327 "RequisiteOverridable", "[color=\"darkblue\"]",
328 "Wants", "[color=\"darkgrey\"]",
329 "Conflicts", "[color=\"red\"]",
330 "ConflictedBy", "[color=\"red\"]",
331 "After", "[color=\"green\"]"
334 const char *c = NULL;
341 for (i = 0; i < ELEMENTSOF(colors); i += 2)
342 if (streq(colors[i], prop)) {
350 if (arg_dot != DOT_ALL)
351 if ((arg_dot == DOT_ORDER) != streq(prop, "After"))
354 switch (dbus_message_iter_get_arg_type(iter)) {
356 case DBUS_TYPE_ARRAY:
358 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
361 dbus_message_iter_recurse(iter, &sub);
363 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
366 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
367 dbus_message_iter_get_basic(&sub, &s);
368 printf("\t\"%s\"->\"%s\" %s;\n", name, s, c);
370 dbus_message_iter_next(&sub);
380 static int dot_one(DBusConnection *bus, const char *name, const char *path) {
381 DBusMessage *m = NULL, *reply = NULL;
382 const char *interface = "org.freedesktop.systemd1.Unit";
385 DBusMessageIter iter, sub, sub2, sub3;
390 dbus_error_init(&error);
392 if (!(m = dbus_message_new_method_call(
393 "org.freedesktop.systemd1",
395 "org.freedesktop.DBus.Properties",
397 log_error("Could not allocate message.");
402 if (!dbus_message_append_args(m,
403 DBUS_TYPE_STRING, &interface,
404 DBUS_TYPE_INVALID)) {
405 log_error("Could not append arguments to message.");
410 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
411 log_error("Failed to issue method call: %s", error.message);
416 if (!dbus_message_iter_init(reply, &iter) ||
417 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
418 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
419 log_error("Failed to parse reply.");
424 dbus_message_iter_recurse(&iter, &sub);
426 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
429 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
430 log_error("Failed to parse reply.");
435 dbus_message_iter_recurse(&sub, &sub2);
437 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &prop, true) < 0) {
438 log_error("Failed to parse reply.");
443 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
444 log_error("Failed to parse reply.");
449 dbus_message_iter_recurse(&sub2, &sub3);
451 if (dot_one_property(name, prop, &sub3)) {
452 log_error("Failed to parse reply.");
457 dbus_message_iter_next(&sub);
462 dbus_message_unref(m);
465 dbus_message_unref(reply);
467 dbus_error_free(&error);
472 static int dot(DBusConnection *bus, char **args, unsigned n) {
473 DBusMessage *m = NULL, *reply = NULL;
476 DBusMessageIter iter, sub, sub2;
478 dbus_error_init(&error);
482 if (!(m = dbus_message_new_method_call(
483 "org.freedesktop.systemd1",
484 "/org/freedesktop/systemd1",
485 "org.freedesktop.systemd1.Manager",
487 log_error("Could not allocate message.");
491 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
492 log_error("Failed to issue method call: %s", error.message);
497 if (!dbus_message_iter_init(reply, &iter) ||
498 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
499 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
500 log_error("Failed to parse reply.");
505 printf("digraph systemd {\n");
507 dbus_message_iter_recurse(&iter, &sub);
508 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
509 const char *id, *description, *load_state, *active_state, *sub_state, *following, *unit_path;
511 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
512 log_error("Failed to parse reply.");
517 dbus_message_iter_recurse(&sub, &sub2);
519 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
520 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
521 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
522 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
523 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
524 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &following, true) < 0 ||
525 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, true) < 0) {
526 log_error("Failed to parse reply.");
531 if ((r = dot_one(bus, id, unit_path)) < 0)
534 /* printf("\t\"%s\";\n", id); */
535 dbus_message_iter_next(&sub);
540 log_info(" Color legend: black = Requires\n"
541 " dark blue = Requisite\n"
542 " dark grey = Wants\n"
546 if (isatty(fileno(stdout)))
547 log_notice("-- You probably want to process this output with graphviz' dot tool.\n"
548 "-- Try a shell pipeline like 'systemctl dot | dot -Tsvg > systemd.svg'!\n");
554 dbus_message_unref(m);
557 dbus_message_unref(reply);
559 dbus_error_free(&error);
564 static int list_jobs(DBusConnection *bus, char **args, unsigned n) {
565 DBusMessage *m = NULL, *reply = NULL;
568 DBusMessageIter iter, sub, sub2;
571 dbus_error_init(&error);
575 if (!(m = dbus_message_new_method_call(
576 "org.freedesktop.systemd1",
577 "/org/freedesktop/systemd1",
578 "org.freedesktop.systemd1.Manager",
580 log_error("Could not allocate message.");
584 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
585 log_error("Failed to issue method call: %s", error.message);
590 if (!dbus_message_iter_init(reply, &iter) ||
591 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
592 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
593 log_error("Failed to parse reply.");
598 dbus_message_iter_recurse(&iter, &sub);
600 printf("%4s %-45s %-17s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
602 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
603 const char *name, *type, *state, *job_path, *unit_path;
607 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
608 log_error("Failed to parse reply.");
613 dbus_message_iter_recurse(&sub, &sub2);
615 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
616 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
617 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
618 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
619 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
620 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
621 log_error("Failed to parse reply.");
626 e = arg_full ? NULL : ellipsize(name, 45, 33);
627 printf("%4u %-45s %-17s %-7s\n", id, e ? e : name, type, state);
632 dbus_message_iter_next(&sub);
635 printf("\n%u jobs listed.\n", k);
640 dbus_message_unref(m);
643 dbus_message_unref(reply);
645 dbus_error_free(&error);
650 static int load_unit(DBusConnection *bus, char **args, unsigned n) {
651 DBusMessage *m = NULL, *reply = NULL;
656 dbus_error_init(&error);
661 for (i = 1; i < n; i++) {
663 if (!(m = dbus_message_new_method_call(
664 "org.freedesktop.systemd1",
665 "/org/freedesktop/systemd1",
666 "org.freedesktop.systemd1.Manager",
668 log_error("Could not allocate message.");
673 if (!dbus_message_append_args(m,
674 DBUS_TYPE_STRING, &args[i],
675 DBUS_TYPE_INVALID)) {
676 log_error("Could not append arguments to message.");
681 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
682 log_error("Failed to issue method call: %s", error.message);
687 dbus_message_unref(m);
688 dbus_message_unref(reply);
697 dbus_message_unref(m);
700 dbus_message_unref(reply);
702 dbus_error_free(&error);
707 static int cancel_job(DBusConnection *bus, char **args, unsigned n) {
708 DBusMessage *m = NULL, *reply = NULL;
713 dbus_error_init(&error);
719 return daemon_reload(bus, args, n);
721 for (i = 1; i < n; i++) {
725 if (!(m = dbus_message_new_method_call(
726 "org.freedesktop.systemd1",
727 "/org/freedesktop/systemd1",
728 "org.freedesktop.systemd1.Manager",
730 log_error("Could not allocate message.");
735 if ((r = safe_atou(args[i], &id)) < 0) {
736 log_error("Failed to parse job id: %s", strerror(-r));
740 assert_cc(sizeof(uint32_t) == sizeof(id));
741 if (!dbus_message_append_args(m,
742 DBUS_TYPE_UINT32, &id,
743 DBUS_TYPE_INVALID)) {
744 log_error("Could not append arguments to message.");
749 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
750 log_error("Failed to issue method call: %s", error.message);
755 if (!dbus_message_get_args(reply, &error,
756 DBUS_TYPE_OBJECT_PATH, &path,
757 DBUS_TYPE_INVALID)) {
758 log_error("Failed to parse reply: %s", error.message);
763 dbus_message_unref(m);
764 if (!(m = dbus_message_new_method_call(
765 "org.freedesktop.systemd1",
767 "org.freedesktop.systemd1.Job",
769 log_error("Could not allocate message.");
774 dbus_message_unref(reply);
775 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
776 log_error("Failed to issue method call: %s", error.message);
781 dbus_message_unref(m);
782 dbus_message_unref(reply);
790 dbus_message_unref(m);
793 dbus_message_unref(reply);
795 dbus_error_free(&error);
800 static bool need_daemon_reload(DBusConnection *bus, const char *unit) {
801 DBusMessage *m, *reply;
802 dbus_bool_t b = FALSE;
803 DBusMessageIter iter, sub;
805 *interface = "org.freedesktop.systemd1.Unit",
806 *property = "NeedDaemonReload",
809 /* We ignore all errors here, since this is used to show a warning only */
811 if (!(m = dbus_message_new_method_call(
812 "org.freedesktop.systemd1",
813 "/org/freedesktop/systemd1",
814 "org.freedesktop.systemd1.Manager",
818 if (!dbus_message_append_args(m,
819 DBUS_TYPE_STRING, &unit,
823 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
826 if (!dbus_message_get_args(reply, NULL,
827 DBUS_TYPE_OBJECT_PATH, &path,
831 dbus_message_unref(m);
832 if (!(m = dbus_message_new_method_call(
833 "org.freedesktop.systemd1",
835 "org.freedesktop.DBus.Properties",
839 if (!dbus_message_append_args(m,
840 DBUS_TYPE_STRING, &interface,
841 DBUS_TYPE_STRING, &property,
842 DBUS_TYPE_INVALID)) {
846 dbus_message_unref(reply);
847 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
850 if (!dbus_message_iter_init(reply, &iter) ||
851 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
854 dbus_message_iter_recurse(&iter, &sub);
856 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_BOOLEAN)
859 dbus_message_iter_get_basic(&sub, &b);
863 dbus_message_unref(m);
866 dbus_message_unref(reply);
871 typedef struct WaitData {
876 static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
884 dbus_error_init(&error);
886 log_debug("Got D-Bus request: %s.%s() on %s",
887 dbus_message_get_interface(message),
888 dbus_message_get_member(message),
889 dbus_message_get_path(message));
891 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
892 log_error("Warning! D-Bus connection terminated.");
893 dbus_connection_close(connection);
895 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
898 dbus_bool_t success = true;
900 if (!dbus_message_get_args(message, &error,
901 DBUS_TYPE_UINT32, &id,
902 DBUS_TYPE_OBJECT_PATH, &path,
903 DBUS_TYPE_BOOLEAN, &success,
905 log_error("Failed to parse message: %s", error.message);
909 if ((p = set_remove(d->set, (char*) path)))
917 dbus_error_free(&error);
918 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
921 static int enable_wait_for_jobs(DBusConnection *bus) {
929 dbus_error_init(&error);
930 dbus_bus_add_match(bus,
932 "sender='org.freedesktop.systemd1',"
933 "interface='org.freedesktop.systemd1.Manager',"
934 "member='JobRemoved',"
935 "path='/org/freedesktop/systemd1'",
938 if (dbus_error_is_set(&error)) {
939 log_error("Failed to add match: %s", error.message);
940 dbus_error_free(&error);
944 /* This is slightly dirty, since we don't undo the match registrations. */
948 static int wait_for_jobs(DBusConnection *bus, Set *s) {
959 if (!dbus_connection_add_filter(bus, wait_filter, &d, NULL)) {
960 log_error("Failed to add filter.");
965 while (!set_isempty(s) &&
966 dbus_connection_read_write_dispatch(bus, -1))
969 if (!arg_quiet && d.failed)
970 log_error("Job failed, see logs for details.");
972 r = d.failed ? -EIO : 0;
975 /* This is slightly dirty, since we don't undo the filter registration. */
980 static int start_unit_one(
987 DBusMessage *m = NULL, *reply = NULL;
996 assert(arg_no_block || s);
998 dbus_error_init(&error);
1000 if (!(m = dbus_message_new_method_call(
1001 "org.freedesktop.systemd1",
1002 "/org/freedesktop/systemd1",
1003 "org.freedesktop.systemd1.Manager",
1005 log_error("Could not allocate message.");
1010 if (!dbus_message_append_args(m,
1011 DBUS_TYPE_STRING, &name,
1012 DBUS_TYPE_STRING, &mode,
1013 DBUS_TYPE_INVALID)) {
1014 log_error("Could not append arguments to message.");
1019 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1021 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
1022 /* There's always a fallback possible for
1023 * legacy actions. */
1028 log_error("Failed to issue method call: %s", error.message);
1033 if (!dbus_message_get_args(reply, &error,
1034 DBUS_TYPE_OBJECT_PATH, &path,
1035 DBUS_TYPE_INVALID)) {
1036 log_error("Failed to parse reply: %s", error.message);
1041 if (need_daemon_reload(bus, name))
1042 log_warning("Unit file of created job changed on disk, 'systemctl %s daemon-reload' recommended.",
1043 arg_session ? "--session" : "--system");
1045 if (!arg_no_block) {
1048 if (!(p = strdup(path))) {
1049 log_error("Failed to duplicate path.");
1054 if ((r = set_put(s, p)) < 0) {
1056 log_error("Failed to add path to set.");
1065 dbus_message_unref(m);
1068 dbus_message_unref(reply);
1070 dbus_error_free(&error);
1075 static enum action verb_to_action(const char *verb) {
1076 if (streq(verb, "halt"))
1078 else if (streq(verb, "poweroff"))
1079 return ACTION_POWEROFF;
1080 else if (streq(verb, "reboot"))
1081 return ACTION_REBOOT;
1082 else if (streq(verb, "rescue"))
1083 return ACTION_RESCUE;
1084 else if (streq(verb, "emergency"))
1085 return ACTION_EMERGENCY;
1086 else if (streq(verb, "default"))
1087 return ACTION_DEFAULT;
1089 return ACTION_INVALID;
1092 static int start_unit(DBusConnection *bus, char **args, unsigned n) {
1094 static const char * const table[_ACTION_MAX] = {
1095 [ACTION_HALT] = SPECIAL_HALT_TARGET,
1096 [ACTION_POWEROFF] = SPECIAL_POWEROFF_TARGET,
1097 [ACTION_REBOOT] = SPECIAL_REBOOT_TARGET,
1098 [ACTION_RUNLEVEL2] = SPECIAL_RUNLEVEL2_TARGET,
1099 [ACTION_RUNLEVEL3] = SPECIAL_RUNLEVEL3_TARGET,
1100 [ACTION_RUNLEVEL4] = SPECIAL_RUNLEVEL4_TARGET,
1101 [ACTION_RUNLEVEL5] = SPECIAL_RUNLEVEL5_TARGET,
1102 [ACTION_RESCUE] = SPECIAL_RESCUE_TARGET,
1103 [ACTION_EMERGENCY] = SPECIAL_EMERGENCY_TARGET,
1104 [ACTION_DEFAULT] = SPECIAL_DEFAULT_TARGET
1109 const char *method, *mode, *one_name;
1114 if (arg_action == ACTION_SYSTEMCTL) {
1116 streq(args[0], "stop") ? "StopUnit" :
1117 streq(args[0], "reload") ? "ReloadUnit" :
1118 streq(args[0], "restart") ? "RestartUnit" :
1119 streq(args[0], "try-restart") ? "TryRestartUnit" :
1120 streq(args[0], "reload-or-restart") ? "ReloadOrRestartUnit" :
1121 streq(args[0], "reload-or-try-restart") ||
1122 streq(args[0], "force-reload") ||
1123 streq(args[0], "condrestart") ? "ReloadOrTryRestartUnit" :
1127 (streq(args[0], "isolate") ||
1128 streq(args[0], "rescue") ||
1129 streq(args[0], "emergency")) ? "isolate" :
1133 one_name = table[verb_to_action(args[0])];
1136 assert(arg_action < ELEMENTSOF(table));
1137 assert(table[arg_action]);
1139 method = "StartUnit";
1141 mode = (arg_action == ACTION_EMERGENCY ||
1142 arg_action == ACTION_RESCUE) ? "isolate" : "replace";
1144 one_name = table[arg_action];
1147 if (!arg_no_block) {
1148 if ((r = enable_wait_for_jobs(bus)) < 0) {
1149 log_error("Could not watch jobs: %s", strerror(-r));
1153 if (!(s = set_new(string_hash_func, string_compare_func))) {
1154 log_error("Failed to allocate set.");
1163 if ((r = start_unit_one(bus, method, one_name, mode, s)) <= 0)
1166 for (i = 1; i < n; i++)
1167 if ((r = start_unit_one(bus, method, args[i], mode, s)) < 0)
1172 r = wait_for_jobs(bus, s);
1181 static int start_special(DBusConnection *bus, char **args, unsigned n) {
1187 r = start_unit(bus, args, n);
1190 warn_wall(verb_to_action(args[0]));
1195 static int check_unit(DBusConnection *bus, char **args, unsigned n) {
1196 DBusMessage *m = NULL, *reply = NULL;
1198 *interface = "org.freedesktop.systemd1.Unit",
1199 *property = "ActiveState";
1200 int r = -EADDRNOTAVAIL;
1207 dbus_error_init(&error);
1209 for (i = 1; i < n; i++) {
1210 const char *path = NULL;
1212 DBusMessageIter iter, sub;
1214 if (!(m = dbus_message_new_method_call(
1215 "org.freedesktop.systemd1",
1216 "/org/freedesktop/systemd1",
1217 "org.freedesktop.systemd1.Manager",
1219 log_error("Could not allocate message.");
1224 if (!dbus_message_append_args(m,
1225 DBUS_TYPE_STRING, &args[i],
1226 DBUS_TYPE_INVALID)) {
1227 log_error("Could not append arguments to message.");
1232 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1234 /* Hmm, cannot figure out anything about this unit... */
1238 dbus_error_free(&error);
1242 if (!dbus_message_get_args(reply, &error,
1243 DBUS_TYPE_OBJECT_PATH, &path,
1244 DBUS_TYPE_INVALID)) {
1245 log_error("Failed to parse reply: %s", error.message);
1250 dbus_message_unref(m);
1251 if (!(m = dbus_message_new_method_call(
1252 "org.freedesktop.systemd1",
1254 "org.freedesktop.DBus.Properties",
1256 log_error("Could not allocate message.");
1261 if (!dbus_message_append_args(m,
1262 DBUS_TYPE_STRING, &interface,
1263 DBUS_TYPE_STRING, &property,
1264 DBUS_TYPE_INVALID)) {
1265 log_error("Could not append arguments to message.");
1270 dbus_message_unref(reply);
1271 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1272 log_error("Failed to issue method call: %s", error.message);
1277 if (!dbus_message_iter_init(reply, &iter) ||
1278 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1279 log_error("Failed to parse reply.");
1284 dbus_message_iter_recurse(&iter, &sub);
1286 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1287 log_error("Failed to parse reply.");
1292 dbus_message_iter_get_basic(&sub, &state);
1297 if (streq(state, "active") || startswith(state, "reloading"))
1300 dbus_message_unref(m);
1301 dbus_message_unref(reply);
1307 dbus_message_unref(m);
1310 dbus_message_unref(reply);
1312 dbus_error_free(&error);
1317 typedef struct ExecStatusInfo {
1323 usec_t start_timestamp;
1324 usec_t exit_timestamp;
1329 LIST_FIELDS(struct ExecStatusInfo, exec);
1332 static void exec_status_info_free(ExecStatusInfo *i) {
1340 static int exec_status_info_deserialize(DBusMessageIter *sub, ExecStatusInfo *i) {
1341 uint64_t start_timestamp, exit_timestamp;
1342 DBusMessageIter sub2, sub3;
1346 int32_t code, status;
1352 if (dbus_message_iter_get_arg_type(sub) != DBUS_TYPE_STRUCT)
1355 dbus_message_iter_recurse(sub, &sub2);
1357 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
1360 if (!(i->path = strdup(path)))
1363 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
1364 dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
1368 dbus_message_iter_recurse(&sub2, &sub3);
1369 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1370 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1371 dbus_message_iter_next(&sub3);
1376 if (!(i->argv = new0(char*, n+1)))
1380 dbus_message_iter_recurse(&sub2, &sub3);
1381 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1384 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1385 dbus_message_iter_get_basic(&sub3, &s);
1386 dbus_message_iter_next(&sub3);
1388 if (!(i->argv[n++] = strdup(s)))
1392 if (!dbus_message_iter_next(&sub2) ||
1393 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, true) < 0 ||
1394 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp, true) < 0 ||
1395 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp, true) < 0 ||
1396 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) < 0 ||
1397 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) < 0 ||
1398 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) < 0)
1402 i->start_timestamp = (usec_t) start_timestamp;
1403 i->exit_timestamp = (usec_t) exit_timestamp;
1404 i->pid = (pid_t) pid;
1411 typedef struct UnitStatusInfo {
1413 const char *load_state;
1414 const char *active_state;
1415 const char *sub_state;
1417 const char *description;
1419 const char *fragment_path;
1420 const char *default_control_group;
1422 bool need_daemon_reload;
1427 const char *status_text;
1430 usec_t start_timestamp;
1431 usec_t exit_timestamp;
1433 int exit_code, exit_status;
1436 unsigned n_accepted;
1437 unsigned n_connections;
1441 const char *sysfs_path;
1443 /* Mount, Automount */
1449 LIST_HEAD(ExecStatusInfo, exec);
1452 static void print_status_info(UnitStatusInfo *i) {
1454 const char *on, *off, *ss;
1458 /* This shows pretty information about a unit. See
1459 * print_property() for a low-level property printer */
1461 printf("%s", strna(i->id));
1463 if (i->description && !streq_ptr(i->id, i->description))
1464 printf(" - %s", i->description);
1468 if (i->fragment_path)
1469 printf("\t Loaded: %s (%s)\n", strna(i->load_state), i->fragment_path);
1470 else if (streq_ptr(i->load_state, "failed"))
1471 printf("\t Loaded: %s%s%s\n",
1472 ansi_highlight(true),
1473 strna(i->load_state),
1474 ansi_highlight(false));
1476 printf("\t Loaded: %s\n", strna(i->load_state));
1478 ss = streq_ptr(i->active_state, i->sub_state) ? NULL : i->sub_state;
1480 if (streq_ptr(i->active_state, "maintenance")) {
1481 on = ansi_highlight(true);
1482 off = ansi_highlight(false);
1483 } else if (streq_ptr(i->active_state, "active") || streq_ptr(i->active_state, "reloading")) {
1484 on = ansi_highlight_green(true);
1485 off = ansi_highlight_green(false);
1490 printf("\t Active: %s%s (%s)%s\n",
1492 strna(i->active_state),
1496 printf("\t Active: %s%s%s\n",
1498 strna(i->active_state),
1502 printf("\t Device: %s\n", i->sysfs_path);
1504 printf("\t Where: %s\n", i->where);
1506 printf("\t What: %s\n", i->what);
1509 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
1511 LIST_FOREACH(exec, p, i->exec) {
1514 /* Only show exited processes here */
1518 t = strv_join(p->argv, " ");
1519 printf("\t Exited: %u (%s, code=%s, ", p->pid, strna(t), sigchld_code_to_string(p->code));
1522 if (p->code == CLD_EXITED)
1523 printf("status=%i", p->status);
1525 printf("signal=%s", signal_to_string(p->status));
1528 if (i->main_pid == p->pid &&
1529 i->start_timestamp == p->start_timestamp &&
1530 i->exit_timestamp == p->start_timestamp)
1531 /* Let's not show this twice */
1534 if (p->pid == i->control_pid)
1538 if (i->main_pid > 0 || i->control_pid > 0) {
1541 if (i->main_pid > 0) {
1542 printf(" Main: %u", (unsigned) i->main_pid);
1546 get_process_name(i->main_pid, &t);
1551 } else if (i->exit_code > 0) {
1552 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
1554 if (i->exit_code == CLD_EXITED)
1555 printf("status=%i", i->exit_status);
1557 printf("signal=%s", signal_to_string(i->exit_status));
1562 if (i->main_pid > 0 && i->control_pid > 0)
1565 if (i->control_pid > 0) {
1568 printf(" Control: %u", (unsigned) i->control_pid);
1570 get_process_name(i->control_pid, &t);
1581 printf("\t Status: \"%s\"\n", i->status_text);
1583 if (i->default_control_group) {
1586 printf("\t CGroup: %s\n", i->default_control_group);
1588 if ((c = columns()) > 18)
1593 show_cgroup_by_path(i->default_control_group, "\t\t ", c);
1596 if (i->need_daemon_reload)
1597 printf("\n%sWarning:%s Unit file changed on disk, 'systemctl %s daemon-reload' recommended.\n",
1598 ansi_highlight(true),
1599 ansi_highlight(false),
1600 arg_session ? "--session" : "--system");
1603 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
1605 switch (dbus_message_iter_get_arg_type(iter)) {
1607 case DBUS_TYPE_STRING: {
1610 dbus_message_iter_get_basic(iter, &s);
1613 if (streq(name, "Id"))
1615 else if (streq(name, "LoadState"))
1617 else if (streq(name, "ActiveState"))
1618 i->active_state = s;
1619 else if (streq(name, "SubState"))
1621 else if (streq(name, "Description"))
1623 else if (streq(name, "FragmentPath"))
1624 i->fragment_path = s;
1625 else if (streq(name, "DefaultControlGroup"))
1626 i->default_control_group = s;
1627 else if (streq(name, "StatusText"))
1629 else if (streq(name, "SysFSPath"))
1631 else if (streq(name, "Where"))
1633 else if (streq(name, "What"))
1640 case DBUS_TYPE_BOOLEAN: {
1643 dbus_message_iter_get_basic(iter, &b);
1645 if (streq(name, "Accept"))
1647 else if (streq(name, "NeedDaemonReload"))
1648 i->need_daemon_reload = b;
1653 case DBUS_TYPE_UINT32: {
1656 dbus_message_iter_get_basic(iter, &u);
1658 if (streq(name, "MainPID")) {
1660 i->main_pid = (pid_t) u;
1663 } else if (streq(name, "ControlPID"))
1664 i->control_pid = (pid_t) u;
1665 else if (streq(name, "ExecMainPID")) {
1667 i->main_pid = (pid_t) u;
1668 } else if (streq(name, "NAccepted"))
1670 else if (streq(name, "NConnections"))
1671 i->n_connections = u;
1676 case DBUS_TYPE_INT32: {
1679 dbus_message_iter_get_basic(iter, &j);
1681 if (streq(name, "ExecMainCode"))
1682 i->exit_code = (int) j;
1683 else if (streq(name, "ExecMainStatus"))
1684 i->exit_status = (int) j;
1689 case DBUS_TYPE_UINT64: {
1692 dbus_message_iter_get_basic(iter, &u);
1694 if (streq(name, "ExecMainStartTimestamp"))
1695 i->start_timestamp = (usec_t) u;
1696 else if (streq(name, "ExecMainExitTimestamp"))
1697 i->exit_timestamp = (usec_t) u;
1702 case DBUS_TYPE_ARRAY: {
1704 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT &&
1705 startswith(name, "Exec")) {
1706 DBusMessageIter sub;
1708 dbus_message_iter_recurse(iter, &sub);
1709 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1710 ExecStatusInfo *info;
1713 if (!(info = new0(ExecStatusInfo, 1)))
1716 if ((r = exec_status_info_deserialize(&sub, info)) < 0) {
1721 LIST_PREPEND(ExecStatusInfo, exec, i->exec, info);
1723 dbus_message_iter_next(&sub);
1734 static int print_property(const char *name, DBusMessageIter *iter) {
1738 /* This is a low-level property printer, see
1739 * print_status_info() for the nicer output */
1741 if (arg_property && !strv_find(arg_property, name))
1744 switch (dbus_message_iter_get_arg_type(iter)) {
1746 case DBUS_TYPE_STRING: {
1748 dbus_message_iter_get_basic(iter, &s);
1750 if (arg_all || s[0])
1751 printf("%s=%s\n", name, s);
1756 case DBUS_TYPE_BOOLEAN: {
1758 dbus_message_iter_get_basic(iter, &b);
1759 printf("%s=%s\n", name, yes_no(b));
1764 case DBUS_TYPE_UINT64: {
1766 dbus_message_iter_get_basic(iter, &u);
1768 /* Yes, heuristics! But we can change this check
1769 * should it turn out to not be sufficient */
1771 if (strstr(name, "Timestamp")) {
1772 char timestamp[FORMAT_TIMESTAMP_MAX], *t;
1774 if ((t = format_timestamp(timestamp, sizeof(timestamp), u)) || arg_all)
1775 printf("%s=%s\n", name, strempty(t));
1776 } else if (strstr(name, "USec")) {
1777 char timespan[FORMAT_TIMESPAN_MAX];
1779 printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u));
1781 printf("%s=%llu\n", name, (unsigned long long) u);
1786 case DBUS_TYPE_UINT32: {
1788 dbus_message_iter_get_basic(iter, &u);
1790 if (strstr(name, "UMask") || strstr(name, "Mode"))
1791 printf("%s=%04o\n", name, u);
1793 printf("%s=%u\n", name, (unsigned) u);
1798 case DBUS_TYPE_INT32: {
1800 dbus_message_iter_get_basic(iter, &i);
1802 printf("%s=%i\n", name, (int) i);
1806 case DBUS_TYPE_STRUCT: {
1807 DBusMessageIter sub;
1808 dbus_message_iter_recurse(iter, &sub);
1810 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
1813 dbus_message_iter_get_basic(&sub, &u);
1816 printf("%s=%u\n", name, (unsigned) u);
1818 printf("%s=\n", name);
1821 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
1824 dbus_message_iter_get_basic(&sub, &s);
1826 if (arg_all || s[0])
1827 printf("%s=%s\n", name, s);
1835 case DBUS_TYPE_ARRAY:
1837 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
1838 DBusMessageIter sub;
1841 dbus_message_iter_recurse(iter, &sub);
1843 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1844 printf("%s=", name);
1846 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1849 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
1850 dbus_message_iter_get_basic(&sub, &s);
1851 printf("%s%s", space ? " " : "", s);
1854 dbus_message_iter_next(&sub);
1862 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_BYTE) {
1863 DBusMessageIter sub;
1865 dbus_message_iter_recurse(iter, &sub);
1867 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1868 printf("%s=", name);
1870 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1873 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_BYTE);
1874 dbus_message_iter_get_basic(&sub, &u);
1877 dbus_message_iter_next(&sub);
1885 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
1886 DBusMessageIter sub, sub2;
1888 dbus_message_iter_recurse(iter, &sub);
1889 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1890 const char *type, *path;
1892 dbus_message_iter_recurse(&sub, &sub2);
1894 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
1895 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
1896 printf("%s=%s\n", type, path);
1898 dbus_message_iter_next(&sub);
1903 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
1904 DBusMessageIter sub, sub2;
1906 dbus_message_iter_recurse(iter, &sub);
1907 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1909 uint64_t value, next_elapse;
1911 dbus_message_iter_recurse(&sub, &sub2);
1913 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
1914 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
1915 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
1916 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
1918 printf("%s={ value=%s ; next_elapse=%s }\n",
1920 format_timespan(timespan1, sizeof(timespan1), value),
1921 format_timespan(timespan2, sizeof(timespan2), next_elapse));
1924 dbus_message_iter_next(&sub);
1929 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
1930 DBusMessageIter sub;
1932 dbus_message_iter_recurse(iter, &sub);
1933 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1934 ExecStatusInfo info;
1937 if (exec_status_info_deserialize(&sub, &info) >= 0) {
1938 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
1941 t = strv_join(info.argv, " ");
1943 printf("%s={ path=%s ; argv[]=%s ; ignore=%s ; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n",
1947 yes_no(info.ignore),
1948 strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)),
1949 strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)),
1950 (unsigned) info. pid,
1951 sigchld_code_to_string(info.code),
1953 info.code == CLD_EXITED ? "" : "/",
1954 strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status)));
1960 strv_free(info.argv);
1962 dbus_message_iter_next(&sub);
1972 printf("%s=[unprintable]\n", name);
1977 static int show_one(DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
1978 DBusMessage *m = NULL, *reply = NULL;
1979 const char *interface = "";
1982 DBusMessageIter iter, sub, sub2, sub3;
1983 UnitStatusInfo info;
1991 dbus_error_init(&error);
1993 if (!(m = dbus_message_new_method_call(
1994 "org.freedesktop.systemd1",
1996 "org.freedesktop.DBus.Properties",
1998 log_error("Could not allocate message.");
2003 if (!dbus_message_append_args(m,
2004 DBUS_TYPE_STRING, &interface,
2005 DBUS_TYPE_INVALID)) {
2006 log_error("Could not append arguments to message.");
2011 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2012 log_error("Failed to issue method call: %s", error.message);
2017 if (!dbus_message_iter_init(reply, &iter) ||
2018 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
2019 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
2020 log_error("Failed to parse reply.");
2025 dbus_message_iter_recurse(&iter, &sub);
2032 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2035 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
2036 log_error("Failed to parse reply.");
2041 dbus_message_iter_recurse(&sub, &sub2);
2043 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
2044 log_error("Failed to parse reply.");
2049 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
2050 log_error("Failed to parse reply.");
2055 dbus_message_iter_recurse(&sub2, &sub3);
2057 if (show_properties)
2058 r = print_property(name, &sub3);
2060 r = status_property(name, &sub3, &info);
2063 log_error("Failed to parse reply.");
2068 dbus_message_iter_next(&sub);
2071 if (!show_properties)
2072 print_status_info(&info);
2074 while ((p = info.exec)) {
2075 LIST_REMOVE(ExecStatusInfo, exec, info.exec, p);
2076 exec_status_info_free(p);
2083 dbus_message_unref(m);
2086 dbus_message_unref(reply);
2088 dbus_error_free(&error);
2093 static int show(DBusConnection *bus, char **args, unsigned n) {
2094 DBusMessage *m = NULL, *reply = NULL;
2098 bool show_properties, new_line = false;
2103 dbus_error_init(&error);
2105 show_properties = !streq(args[0], "status");
2107 if (show_properties && n <= 1) {
2108 /* If not argument is specified inspect the manager
2111 r = show_one(bus, "/org/freedesktop/systemd1", show_properties, &new_line);
2115 for (i = 1; i < n; i++) {
2116 const char *path = NULL;
2119 if (!show_properties || safe_atou32(args[i], &id) < 0) {
2121 if (!(m = dbus_message_new_method_call(
2122 "org.freedesktop.systemd1",
2123 "/org/freedesktop/systemd1",
2124 "org.freedesktop.systemd1.Manager",
2126 log_error("Could not allocate message.");
2131 if (!dbus_message_append_args(m,
2132 DBUS_TYPE_STRING, &args[i],
2133 DBUS_TYPE_INVALID)) {
2134 log_error("Could not append arguments to message.");
2139 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2141 if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
2142 log_error("Failed to issue method call: %s", error.message);
2147 dbus_error_free(&error);
2149 dbus_message_unref(m);
2150 if (!(m = dbus_message_new_method_call(
2151 "org.freedesktop.systemd1",
2152 "/org/freedesktop/systemd1",
2153 "org.freedesktop.systemd1.Manager",
2155 log_error("Could not allocate message.");
2160 if (!dbus_message_append_args(m,
2161 DBUS_TYPE_STRING, &args[i],
2162 DBUS_TYPE_INVALID)) {
2163 log_error("Could not append arguments to message.");
2168 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2169 log_error("Failed to issue method call: %s", error.message);
2177 if (!(m = dbus_message_new_method_call(
2178 "org.freedesktop.systemd1",
2179 "/org/freedesktop/systemd1",
2180 "org.freedesktop.systemd1.Manager",
2182 log_error("Could not allocate message.");
2187 if (!dbus_message_append_args(m,
2188 DBUS_TYPE_UINT32, &id,
2189 DBUS_TYPE_INVALID)) {
2190 log_error("Could not append arguments to message.");
2195 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2196 log_error("Failed to issue method call: %s", error.message);
2202 if (!dbus_message_get_args(reply, &error,
2203 DBUS_TYPE_OBJECT_PATH, &path,
2204 DBUS_TYPE_INVALID)) {
2205 log_error("Failed to parse reply: %s", error.message);
2210 if ((r = show_one(bus, path, show_properties, &new_line)) < 0)
2213 dbus_message_unref(m);
2214 dbus_message_unref(reply);
2222 dbus_message_unref(m);
2225 dbus_message_unref(reply);
2227 dbus_error_free(&error);
2232 static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
2234 DBusMessage *m = NULL, *reply = NULL;
2239 dbus_error_init(&error);
2241 log_debug("Got D-Bus request: %s.%s() on %s",
2242 dbus_message_get_interface(message),
2243 dbus_message_get_member(message),
2244 dbus_message_get_path(message));
2246 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
2247 log_error("Warning! D-Bus connection terminated.");
2248 dbus_connection_close(connection);
2250 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") ||
2251 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
2252 const char *id, *path;
2254 if (!dbus_message_get_args(message, &error,
2255 DBUS_TYPE_STRING, &id,
2256 DBUS_TYPE_OBJECT_PATH, &path,
2258 log_error("Failed to parse message: %s", error.message);
2259 else if (streq(dbus_message_get_member(message), "UnitNew"))
2260 printf("Unit %s added.\n", id);
2262 printf("Unit %s removed.\n", id);
2264 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew") ||
2265 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
2269 if (!dbus_message_get_args(message, &error,
2270 DBUS_TYPE_UINT32, &id,
2271 DBUS_TYPE_OBJECT_PATH, &path,
2273 log_error("Failed to parse message: %s", error.message);
2274 else if (streq(dbus_message_get_member(message), "JobNew"))
2275 printf("Job %u added.\n", id);
2277 printf("Job %u removed.\n", id);
2280 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Unit", "Changed") ||
2281 dbus_message_is_signal(message, "org.freedesktop.systemd1.Job", "Changed")) {
2283 const char *path, *interface, *property = "Id";
2284 DBusMessageIter iter, sub;
2286 path = dbus_message_get_path(message);
2287 interface = dbus_message_get_interface(message);
2289 if (!(m = dbus_message_new_method_call(
2290 "org.freedesktop.systemd1",
2292 "org.freedesktop.DBus.Properties",
2294 log_error("Could not allocate message.");
2298 if (!dbus_message_append_args(m,
2299 DBUS_TYPE_STRING, &interface,
2300 DBUS_TYPE_STRING, &property,
2301 DBUS_TYPE_INVALID)) {
2302 log_error("Could not append arguments to message.");
2306 if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
2307 log_error("Failed to issue method call: %s", error.message);
2311 if (!dbus_message_iter_init(reply, &iter) ||
2312 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2313 log_error("Failed to parse reply.");
2317 dbus_message_iter_recurse(&iter, &sub);
2319 if (streq(interface, "org.freedesktop.systemd1.Unit")) {
2322 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2323 log_error("Failed to parse reply.");
2327 dbus_message_iter_get_basic(&sub, &id);
2328 printf("Unit %s changed.\n", id);
2332 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32) {
2333 log_error("Failed to parse reply.");
2337 dbus_message_iter_get_basic(&sub, &id);
2338 printf("Job %u changed.\n", id);
2344 dbus_message_unref(m);
2347 dbus_message_unref(reply);
2349 dbus_error_free(&error);
2350 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2354 dbus_message_unref(m);
2357 dbus_message_unref(reply);
2359 dbus_error_free(&error);
2360 return DBUS_HANDLER_RESULT_NEED_MEMORY;
2363 static int monitor(DBusConnection *bus, char **args, unsigned n) {
2364 DBusMessage *m = NULL, *reply = NULL;
2368 dbus_error_init(&error);
2371 dbus_bus_add_match(bus,
2373 "sender='org.freedesktop.systemd1',"
2374 "interface='org.freedesktop.systemd1.Manager',"
2375 "path='/org/freedesktop/systemd1'",
2378 if (dbus_error_is_set(&error)) {
2379 log_error("Failed to add match: %s", error.message);
2384 dbus_bus_add_match(bus,
2386 "sender='org.freedesktop.systemd1',"
2387 "interface='org.freedesktop.systemd1.Unit',"
2391 if (dbus_error_is_set(&error)) {
2392 log_error("Failed to add match: %s", error.message);
2397 dbus_bus_add_match(bus,
2399 "sender='org.freedesktop.systemd1',"
2400 "interface='org.freedesktop.systemd1.Job',"
2404 if (dbus_error_is_set(&error)) {
2405 log_error("Failed to add match: %s", error.message);
2411 if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
2412 log_error("Failed to add filter.");
2417 if (!(m = dbus_message_new_method_call(
2418 "org.freedesktop.systemd1",
2419 "/org/freedesktop/systemd1",
2420 "org.freedesktop.systemd1.Manager",
2422 log_error("Could not allocate message.");
2427 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2428 log_error("Failed to issue method call: %s", error.message);
2433 while (dbus_connection_read_write_dispatch(bus, -1))
2440 /* This is slightly dirty, since we don't undo the filter or the matches. */
2443 dbus_message_unref(m);
2446 dbus_message_unref(reply);
2448 dbus_error_free(&error);
2453 static int dump(DBusConnection *bus, char **args, unsigned n) {
2454 DBusMessage *m = NULL, *reply = NULL;
2459 dbus_error_init(&error);
2461 if (!(m = dbus_message_new_method_call(
2462 "org.freedesktop.systemd1",
2463 "/org/freedesktop/systemd1",
2464 "org.freedesktop.systemd1.Manager",
2466 log_error("Could not allocate message.");
2470 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2471 log_error("Failed to issue method call: %s", error.message);
2476 if (!dbus_message_get_args(reply, &error,
2477 DBUS_TYPE_STRING, &text,
2478 DBUS_TYPE_INVALID)) {
2479 log_error("Failed to parse reply: %s", error.message);
2484 fputs(text, stdout);
2490 dbus_message_unref(m);
2493 dbus_message_unref(reply);
2495 dbus_error_free(&error);
2500 static int snapshot(DBusConnection *bus, char **args, unsigned n) {
2501 DBusMessage *m = NULL, *reply = NULL;
2504 const char *name = "", *path, *id;
2505 dbus_bool_t cleanup = FALSE;
2506 DBusMessageIter iter, sub;
2508 *interface = "org.freedesktop.systemd1.Unit",
2511 dbus_error_init(&error);
2513 if (!(m = dbus_message_new_method_call(
2514 "org.freedesktop.systemd1",
2515 "/org/freedesktop/systemd1",
2516 "org.freedesktop.systemd1.Manager",
2517 "CreateSnapshot"))) {
2518 log_error("Could not allocate message.");
2525 if (!dbus_message_append_args(m,
2526 DBUS_TYPE_STRING, &name,
2527 DBUS_TYPE_BOOLEAN, &cleanup,
2528 DBUS_TYPE_INVALID)) {
2529 log_error("Could not append arguments to message.");
2534 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2535 log_error("Failed to issue method call: %s", error.message);
2540 if (!dbus_message_get_args(reply, &error,
2541 DBUS_TYPE_OBJECT_PATH, &path,
2542 DBUS_TYPE_INVALID)) {
2543 log_error("Failed to parse reply: %s", error.message);
2548 dbus_message_unref(m);
2549 if (!(m = dbus_message_new_method_call(
2550 "org.freedesktop.systemd1",
2552 "org.freedesktop.DBus.Properties",
2554 log_error("Could not allocate message.");
2558 if (!dbus_message_append_args(m,
2559 DBUS_TYPE_STRING, &interface,
2560 DBUS_TYPE_STRING, &property,
2561 DBUS_TYPE_INVALID)) {
2562 log_error("Could not append arguments to message.");
2567 dbus_message_unref(reply);
2568 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2569 log_error("Failed to issue method call: %s", error.message);
2574 if (!dbus_message_iter_init(reply, &iter) ||
2575 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2576 log_error("Failed to parse reply.");
2581 dbus_message_iter_recurse(&iter, &sub);
2583 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2584 log_error("Failed to parse reply.");
2589 dbus_message_iter_get_basic(&sub, &id);
2597 dbus_message_unref(m);
2600 dbus_message_unref(reply);
2602 dbus_error_free(&error);
2607 static int delete_snapshot(DBusConnection *bus, char **args, unsigned n) {
2608 DBusMessage *m = NULL, *reply = NULL;
2616 dbus_error_init(&error);
2618 for (i = 1; i < n; i++) {
2619 const char *path = NULL;
2621 if (!(m = dbus_message_new_method_call(
2622 "org.freedesktop.systemd1",
2623 "/org/freedesktop/systemd1",
2624 "org.freedesktop.systemd1.Manager",
2626 log_error("Could not allocate message.");
2631 if (!dbus_message_append_args(m,
2632 DBUS_TYPE_STRING, &args[i],
2633 DBUS_TYPE_INVALID)) {
2634 log_error("Could not append arguments to message.");
2639 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2640 log_error("Failed to issue method call: %s", error.message);
2645 if (!dbus_message_get_args(reply, &error,
2646 DBUS_TYPE_OBJECT_PATH, &path,
2647 DBUS_TYPE_INVALID)) {
2648 log_error("Failed to parse reply: %s", error.message);
2653 dbus_message_unref(m);
2654 if (!(m = dbus_message_new_method_call(
2655 "org.freedesktop.systemd1",
2657 "org.freedesktop.systemd1.Snapshot",
2659 log_error("Could not allocate message.");
2664 dbus_message_unref(reply);
2665 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2666 log_error("Failed to issue method call: %s", error.message);
2671 dbus_message_unref(m);
2672 dbus_message_unref(reply);
2680 dbus_message_unref(m);
2683 dbus_message_unref(reply);
2685 dbus_error_free(&error);
2690 static int daemon_reload(DBusConnection *bus, char **args, unsigned n) {
2691 DBusMessage *m = NULL, *reply = NULL;
2696 dbus_error_init(&error);
2698 if (arg_action == ACTION_RELOAD)
2700 else if (arg_action == ACTION_REEXEC)
2701 method = "Reexecute";
2703 assert(arg_action == ACTION_SYSTEMCTL);
2706 streq(args[0], "clear-jobs") ||
2707 streq(args[0], "cancel") ? "ClearJobs" :
2708 streq(args[0], "daemon-reexec") ? "Reexecute" :
2709 streq(args[0], "reset-maintenance") ? "ResetMaintenance" :
2710 streq(args[0], "daemon-exit") ? "Exit" :
2714 if (!(m = dbus_message_new_method_call(
2715 "org.freedesktop.systemd1",
2716 "/org/freedesktop/systemd1",
2717 "org.freedesktop.systemd1.Manager",
2719 log_error("Could not allocate message.");
2723 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2725 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
2726 /* There's always a fallback possible for
2727 * legacy actions. */
2732 log_error("Failed to issue method call: %s", error.message);
2741 dbus_message_unref(m);
2744 dbus_message_unref(reply);
2746 dbus_error_free(&error);
2751 static int reset_maintenance(DBusConnection *bus, char **args, unsigned n) {
2752 DBusMessage *m = NULL, *reply = NULL;
2758 dbus_error_init(&error);
2761 return daemon_reload(bus, args, n);
2763 for (i = 1; i < n; i++) {
2765 if (!(m = dbus_message_new_method_call(
2766 "org.freedesktop.systemd1",
2767 "/org/freedesktop/systemd1",
2768 "org.freedesktop.systemd1.Manager",
2769 "ResetMaintenanceUnit"))) {
2770 log_error("Could not allocate message.");
2775 if (!dbus_message_append_args(m,
2776 DBUS_TYPE_STRING, args + i,
2777 DBUS_TYPE_INVALID)) {
2778 log_error("Could not append arguments to message.");
2783 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2784 log_error("Failed to issue method call: %s", error.message);
2789 dbus_message_unref(m);
2790 dbus_message_unref(reply);
2798 dbus_message_unref(m);
2801 dbus_message_unref(reply);
2803 dbus_error_free(&error);
2808 static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
2809 DBusMessage *m = NULL, *reply = NULL;
2811 DBusMessageIter iter, sub, sub2;
2814 *interface = "org.freedesktop.systemd1.Manager",
2815 *property = "Environment";
2817 dbus_error_init(&error);
2819 if (!(m = dbus_message_new_method_call(
2820 "org.freedesktop.systemd1",
2821 "/org/freedesktop/systemd1",
2822 "org.freedesktop.DBus.Properties",
2824 log_error("Could not allocate message.");
2828 if (!dbus_message_append_args(m,
2829 DBUS_TYPE_STRING, &interface,
2830 DBUS_TYPE_STRING, &property,
2831 DBUS_TYPE_INVALID)) {
2832 log_error("Could not append arguments to message.");
2837 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2838 log_error("Failed to issue method call: %s", error.message);
2843 if (!dbus_message_iter_init(reply, &iter) ||
2844 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2845 log_error("Failed to parse reply.");
2850 dbus_message_iter_recurse(&iter, &sub);
2852 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
2853 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
2854 log_error("Failed to parse reply.");
2859 dbus_message_iter_recurse(&sub, &sub2);
2861 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
2864 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
2865 log_error("Failed to parse reply.");
2870 dbus_message_iter_get_basic(&sub2, &text);
2871 printf("%s\n", text);
2873 dbus_message_iter_next(&sub2);
2880 dbus_message_unref(m);
2883 dbus_message_unref(reply);
2885 dbus_error_free(&error);
2890 static int set_environment(DBusConnection *bus, char **args, unsigned n) {
2891 DBusMessage *m = NULL, *reply = NULL;
2895 DBusMessageIter iter, sub;
2898 dbus_error_init(&error);
2900 method = streq(args[0], "set-environment")
2902 : "UnsetEnvironment";
2904 if (!(m = dbus_message_new_method_call(
2905 "org.freedesktop.systemd1",
2906 "/org/freedesktop/systemd1",
2907 "org.freedesktop.systemd1.Manager",
2910 log_error("Could not allocate message.");
2914 dbus_message_iter_init_append(m, &iter);
2916 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
2917 log_error("Could not append arguments to message.");
2922 for (i = 1; i < n; i++)
2923 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) {
2924 log_error("Could not append arguments to message.");
2929 if (!dbus_message_iter_close_container(&iter, &sub)) {
2930 log_error("Could not append arguments to message.");
2935 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2936 log_error("Failed to issue method call: %s", error.message);
2945 dbus_message_unref(m);
2948 dbus_message_unref(reply);
2950 dbus_error_free(&error);
2963 static Hashmap *will_install = NULL, *have_installed = NULL;
2964 static Set *remove_symlinks_to = NULL;
2966 static void install_info_free(InstallInfo *i) {
2971 strv_free(i->aliases);
2972 strv_free(i->wanted_by);
2976 static void install_info_hashmap_free(Hashmap *m) {
2979 while ((i = hashmap_steal_first(m)))
2980 install_info_free(i);
2985 static bool unit_name_valid(const char *name) {
2987 /* This is a minimal version of unit_name_valid() from
2993 if (ignore_file(name))
2999 static int install_info_add(const char *name) {
3003 assert(will_install);
3005 if (!unit_name_valid(name))
3008 if (hashmap_get(have_installed, name) ||
3009 hashmap_get(will_install, name))
3012 if (!(i = new0(InstallInfo, 1))) {
3017 if (!(i->name = strdup(name))) {
3022 if ((r = hashmap_put(will_install, i->name, i)) < 0)
3029 install_info_free(i);
3034 static int config_parse_also(
3035 const char *filename,
3037 const char *section,
3051 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
3055 if (!(n = strndup(w, l)))
3058 r = install_info_add(n);
3068 static int mark_symlink_for_removal(const char *p) {
3073 assert(path_is_absolute(p));
3075 if (!remove_symlinks_to)
3078 if (!(n = strdup(p)))
3081 path_kill_slashes(n);
3083 if ((r = set_put(remove_symlinks_to, n)) < 0) {
3085 return r == -EEXIST ? 0 : r;
3091 static int remove_marked_symlinks_fd(int fd, const char *config_path, const char *root, bool *deleted) {
3100 if (!(d = fdopendir(fd))) {
3101 close_nointr_nofail(fd);
3107 while ((de = readdir(d))) {
3108 bool is_dir = false, is_link = false;
3110 if (ignore_file(de->d_name))
3113 if (de->d_type == DT_LNK)
3115 else if (de->d_type == DT_DIR)
3117 else if (de->d_type == DT_UNKNOWN) {
3120 if (fstatat(fd, de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) {
3121 log_error("Failed to stat %s/%s: %m", root, de->d_name);
3128 is_link = S_ISLNK(st.st_mode);
3129 is_dir = S_ISDIR(st.st_mode);
3137 if ((nfd = openat(fd, de->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW)) < 0) {
3138 log_error("Failed to open %s/%s: %m", root, de->d_name);
3145 if (asprintf(&p, "%s/%s", root, de->d_name) < 0) {
3146 log_error("Failed to allocate directory string.");
3147 close_nointr_nofail(nfd);
3152 /* This will close nfd, regardless whether it succeeds or not */
3153 q = remove_marked_symlinks_fd(nfd, config_path, p, deleted);
3159 } else if (is_link) {
3163 if (asprintf(&p, "%s/%s", root, de->d_name) < 0) {
3164 log_error("Failed to allocate symlink string.");
3169 if ((q = readlink_and_make_absolute(p, &dest)) < 0) {
3170 log_error("Cannot read symlink %s: %s", p, strerror(-q));
3178 if ((c = canonicalize_file_name(dest))) {
3179 /* This might fail if the destination
3180 * is already removed */
3186 path_kill_slashes(dest);
3187 if (set_get(remove_symlinks_to, dest)) {
3190 log_info("rm '%s'", p);
3192 if (unlink(p) < 0) {
3193 log_error("Cannot unlink symlink %s: %m", p);
3198 rmdir_parents(p, config_path);
3199 path_kill_slashes(p);
3201 if (!set_get(remove_symlinks_to, p)) {
3203 if ((r = mark_symlink_for_removal(p)) < 0) {
3222 static int remove_marked_symlinks(const char *config_path) {
3226 assert(config_path);
3228 if (set_size(remove_symlinks_to) <= 0)
3231 if ((fd = open(config_path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW)) < 0)
3238 if ((cfd = dup(fd)) < 0) {
3243 /* This takes possession of cfd and closes it */
3244 if ((q = remove_marked_symlinks_fd(cfd, config_path, config_path, &deleted)) < 0) {
3250 close_nointr_nofail(fd);
3255 static int create_symlink(const char *verb, const char *old_path, const char *new_path) {
3262 if (streq(verb, "enable")) {
3265 mkdir_parents(new_path, 0755);
3267 if (symlink(old_path, new_path) >= 0) {
3270 log_info("ln -s '%s' '%s'", old_path, new_path);
3275 if (errno != EEXIST) {
3276 log_error("Cannot link %s to %s: %m", old_path, new_path);
3280 if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
3282 if (errno == EINVAL) {
3283 log_error("Cannot link %s to %s, file exists already and is not a symlink.", old_path, new_path);
3287 log_error("readlink() failed: %s", strerror(-r));
3291 if (streq(dest, old_path)) {
3297 log_error("Cannot link %s to %s, symlink exists already and points to %s.", old_path, new_path, dest);
3306 log_info("ln -s '%s' '%s'", old_path, new_path);
3308 if (symlink(old_path, new_path) >= 0)
3311 log_error("Cannot link %s to %s: %m", old_path, new_path);
3314 } else if (streq(verb, "disable")) {
3317 if ((r = mark_symlink_for_removal(old_path)) < 0)
3320 if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
3321 if (errno == ENOENT)
3324 if (errno == EINVAL) {
3325 log_warning("File %s not a symlink, ignoring.", old_path);
3329 log_error("readlink() failed: %s", strerror(-r));
3333 if (!streq(dest, old_path)) {
3334 log_warning("File %s not a symlink to %s but points to %s, ignoring.", new_path, old_path, dest);
3341 if ((r = mark_symlink_for_removal(new_path)) < 0)
3345 log_info("rm '%s'", new_path);
3347 if (unlink(new_path) >= 0)
3350 log_error("Cannot unlink %s: %m", new_path);
3353 } else if (streq(verb, "is-enabled")) {
3356 if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
3358 if (errno == ENOENT || errno == EINVAL)
3361 log_error("readlink() failed: %s", strerror(-r));
3365 if (streq(dest, old_path)) {
3373 assert_not_reached("Unknown action.");
3376 static int install_info_symlink_alias(const char *verb, InstallInfo *i, const char *config_path) {
3378 char *alias_path = NULL;
3383 assert(config_path);
3385 STRV_FOREACH(s, i->aliases) {
3387 if (!unit_name_valid(*s)) {
3388 log_error("Invalid name %s.", *s);
3394 if (!(alias_path = path_make_absolute(*s, config_path))) {
3395 log_error("Out of memory");
3400 if ((r = create_symlink(verb, i->path, alias_path)) != 0)
3403 if (streq(verb, "disable"))
3404 rmdir_parents(alias_path, config_path);
3415 static int install_info_symlink_wants(const char *verb, InstallInfo *i, const char *config_path) {
3417 char *alias_path = NULL;
3422 assert(config_path);
3424 STRV_FOREACH(s, i->wanted_by) {
3425 if (!unit_name_valid(*s)) {
3426 log_error("Invalid name %s.", *s);
3434 if (asprintf(&alias_path, "%s/%s.wants/%s", config_path, *s, i->name) < 0) {
3435 log_error("Out of memory");
3440 if ((r = create_symlink(verb, i->path, alias_path)) != 0)
3443 if (streq(verb, "disable"))
3444 rmdir_parents(alias_path, config_path);
3455 static int install_info_apply(const char *verb, LookupPaths *paths, InstallInfo *i, const char *config_path) {
3457 const ConfigItem items[] = {
3458 { "Alias", config_parse_strv, &i->aliases, "Install" },
3459 { "WantedBy", config_parse_strv, &i->wanted_by, "Install" },
3460 { "Also", config_parse_also, NULL, "Install" },
3462 { NULL, NULL, NULL, NULL }
3466 char *filename = NULL;
3473 STRV_FOREACH(p, paths->unit_path) {
3476 if (!(filename = path_make_absolute(i->name, *p))) {
3477 log_error("Out of memory");
3481 /* Ensure that we don't follow symlinks */
3482 if ((fd = open(filename, O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NOCTTY)) >= 0)
3483 if ((f = fdopen(fd, "re")))
3486 if (errno == ELOOP) {
3487 log_error("Refusing to operate on symlinks, please pass unit names or absolute paths to unit files.");
3492 if (errno != ENOENT) {
3493 log_error("Failed to open %s: %m", filename);
3503 log_error("Couldn't find %s.", i->name);
3509 if ((r = config_parse(filename, f, NULL, items, true, i)) < 0) {
3516 if ((r = install_info_symlink_alias(verb, i, config_path)) != 0)
3519 if ((r = install_info_symlink_wants(verb, i, config_path)) != 0)
3522 if ((r = mark_symlink_for_removal(filename)) < 0)
3525 if ((r = remove_marked_symlinks(config_path)) < 0)
3531 static char *get_config_path(void) {
3533 if (arg_session && arg_global)
3534 return strdup(SESSION_CONFIG_UNIT_PATH);
3539 if (session_config_home(&p) < 0)
3545 return strdup(SYSTEM_CONFIG_UNIT_PATH);
3548 static int enable_unit(DBusConnection *bus, char **args, unsigned n) {
3552 char *config_path = NULL;
3555 const char *verb = args[0];
3557 dbus_error_init(&error);
3560 if ((r = lookup_paths_init(&paths, arg_session ? MANAGER_SESSION : MANAGER_SYSTEM)) < 0) {
3561 log_error("Failed to determine lookup paths: %s", strerror(-r));
3565 if (!(config_path = get_config_path())) {
3566 log_error("Failed to determine config path");
3571 will_install = hashmap_new(string_hash_func, string_compare_func);
3572 have_installed = hashmap_new(string_hash_func, string_compare_func);
3574 if (!will_install || !have_installed) {
3575 log_error("Failed to allocate unit sets.");
3580 if (!arg_defaults && streq(verb, "disable"))
3581 if (!(remove_symlinks_to = set_new(string_hash_func, string_compare_func))) {
3582 log_error("Failed to allocate symlink sets.");
3587 for (j = 1; j < n; j++)
3588 if ((r = install_info_add(args[j])) < 0)
3591 while ((i = hashmap_first(will_install))) {
3594 assert_se(hashmap_move_one(have_installed, will_install, i->name) == 0);
3596 if ((q = install_info_apply(verb, &paths, i, config_path)) != 0) {
3604 /* In test mode and found something */
3610 if (streq(verb, "is-enabled"))
3611 r = r > 0 ? 0 : -ENOENT;
3613 /* Don't try to reload anything if the user asked us to not do this */
3615 /* Don't try to reload anything when updating a unit globally */
3617 /* Don't try to reload anything if we are called for system changes but the system wasn't booted with systemd */
3618 (arg_session || sd_booted() > 0) &&
3619 /* Don't try to reload anything if we are running in a chroot environment */
3620 (arg_session || running_in_chroot() <= 0) ) {
3623 if ((q = daemon_reload(bus, args, n)) < 0)
3628 install_info_hashmap_free(will_install);
3629 install_info_hashmap_free(have_installed);
3631 set_free_free(remove_symlinks_to);
3633 lookup_paths_free(&paths);
3640 static int systemctl_help(void) {
3642 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
3643 "Send control commands to or query the systemd manager.\n\n"
3644 " -h --help Show this help\n"
3645 " -t --type=TYPE List only units of a particular type\n"
3646 " -p --property=NAME Show only properties by this name\n"
3647 " -a --all Show all units/properties, including dead/empty ones\n"
3648 " --full Don't ellipsize unit names on output\n"
3649 " --fail When queueing a new job, fail if conflicting jobs are\n"
3651 " -q --quiet Suppress output\n"
3652 " --no-block Do not wait until operation finished\n"
3653 " --system Connect to system bus\n"
3654 " --session Connect to session bus\n"
3655 " --order When generating graph for dot, show only order\n"
3656 " --require When generating graph for dot, show only requirement\n"
3657 " --no-wall Don't send wall message before halt/power-off/reboot\n"
3658 " --global Enable/disable unit files globally\n"
3659 " --no-reload When enabling/disabling unit files, don't reload daemon\n"
3661 " --force When enabling unit files, override existing symlinks\n"
3662 " --defaults When disabling unit files, remove default symlinks only\n\n"
3664 " list-units List units\n"
3665 " start [NAME...] Start (activate) one or more units\n"
3666 " stop [NAME...] Stop (deactivate) one or more units\n"
3667 " reload [NAME...] Reload one or more units\n"
3668 " restart [NAME...] Start or restart one or more units\n"
3669 " try-restart [NAME...] Restart one or more units if active\n"
3670 " reload-or-restart [NAME...] Reload one or more units is possible,\n"
3671 " otherwise start or restart\n"
3672 " reload-or-try-restart [NAME...] Reload one or more units is possible,\n"
3673 " otherwise restart if active\n"
3674 " isolate [NAME] Start one unit and stop all others\n"
3675 " is-active [NAME...] Check whether units are active\n"
3676 " status [NAME...] Show runtime status of one or more units\n"
3677 " show [NAME...|JOB...] Show properties of one or more\n"
3678 " units/jobs or the manager\n"
3679 " reset-maintenance [NAME...] Reset maintenance state for all, one,\n"
3681 " enable [NAME...] Enable one or more unit files\n"
3682 " disable [NAME...] Disable one or more unit files\n"
3683 " is-enabled [NAME...] Check whether unit files are enabled\n"
3684 " load [NAME...] Load one or more units\n"
3685 " list-jobs List jobs\n"
3686 " cancel [JOB...] Cancel all, one, or more jobs\n"
3687 " monitor Monitor unit/job changes\n"
3688 " dump Dump server status\n"
3689 " dot Dump dependency graph for dot(1)\n"
3690 " snapshot [NAME] Create a snapshot\n"
3691 " delete [NAME...] Remove one or more snapshots\n"
3692 " daemon-reload Reload systemd manager configuration\n"
3693 " daemon-reexec Reexecute systemd manager\n"
3694 " daemon-exit Ask the systemd manager to quit\n"
3695 " show-environment Dump environment\n"
3696 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
3697 " unset-environment [NAME...] Unset one or more environment variables\n"
3698 " halt Shut down and halt the system\n"
3699 " poweroff Shut down and power-off the system\n"
3700 " reboot Shut down and reboot the system\n"
3701 " rescue Enter system rescue mode\n"
3702 " emergency Enter system emergency mode\n"
3703 " default Enter system default mode\n",
3704 program_invocation_short_name);
3709 static int halt_help(void) {
3711 printf("%s [OPTIONS...]\n\n"
3712 "%s the system.\n\n"
3713 " --help Show this help\n"
3714 " --halt Halt the machine\n"
3715 " -p --poweroff Switch off the machine\n"
3716 " --reboot Reboot the machine\n"
3717 " -f --force Force immediate halt/power-off/reboot\n"
3718 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
3719 " -d --no-wtmp Don't write wtmp record\n"
3720 " -n --no-sync Don't sync before halt/power-off/reboot\n"
3721 " --no-wall Don't send wall message before halt/power-off/reboot\n",
3722 program_invocation_short_name,
3723 arg_action == ACTION_REBOOT ? "Reboot" :
3724 arg_action == ACTION_POWEROFF ? "Power off" :
3730 static int shutdown_help(void) {
3732 printf("%s [OPTIONS...] [now] [WALL...]\n\n"
3733 "Shut down the system.\n\n"
3734 " --help Show this help\n"
3735 " -H --halt Halt the machine\n"
3736 " -P --poweroff Power-off the machine\n"
3737 " -r --reboot Reboot the machine\n"
3738 " -h Equivalent to --poweroff, overriden by --halt\n"
3739 " -k Don't halt/power-off/reboot, just send warnings\n"
3740 " --no-wall Don't send wall message before halt/power-off/reboot\n",
3741 program_invocation_short_name);
3746 static int telinit_help(void) {
3748 printf("%s [OPTIONS...] {COMMAND}\n\n"
3749 "Send control commands to the init daemon.\n\n"
3750 " --help Show this help\n"
3751 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
3753 " 0 Power-off the machine\n"
3754 " 6 Reboot the machine\n"
3755 " 2, 3, 4, 5 Start runlevelX.target unit\n"
3756 " 1, s, S Enter rescue mode\n"
3757 " q, Q Reload init daemon configuration\n"
3758 " u, U Reexecute init daemon\n",
3759 program_invocation_short_name);
3764 static int runlevel_help(void) {
3766 printf("%s [OPTIONS...]\n\n"
3767 "Prints the previous and current runlevel of the init system.\n\n"
3768 " --help Show this help\n",
3769 program_invocation_short_name);
3774 static int systemctl_parse_argv(int argc, char *argv[]) {
3791 static const struct option options[] = {
3792 { "help", no_argument, NULL, 'h' },
3793 { "type", required_argument, NULL, 't' },
3794 { "property", required_argument, NULL, 'p' },
3795 { "all", no_argument, NULL, 'a' },
3796 { "full", no_argument, NULL, ARG_FULL },
3797 { "fail", no_argument, NULL, ARG_FAIL },
3798 { "session", no_argument, NULL, ARG_SESSION },
3799 { "system", no_argument, NULL, ARG_SYSTEM },
3800 { "global", no_argument, NULL, ARG_GLOBAL },
3801 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
3802 { "no-wall", no_argument, NULL, ARG_NO_WALL },
3803 { "quiet", no_argument, NULL, 'q' },
3804 { "order", no_argument, NULL, ARG_ORDER },
3805 { "require", no_argument, NULL, ARG_REQUIRE },
3806 { "force", no_argument, NULL, ARG_FORCE },
3807 { "no-reload", no_argument, NULL, ARG_NO_RELOAD },
3808 { "defaults", no_argument, NULL, ARG_DEFAULTS },
3809 { NULL, 0, NULL, 0 }
3817 while ((c = getopt_long(argc, argv, "ht:p:aq", options, NULL)) >= 0) {
3832 if (!(l = strv_append(arg_property, optarg)))
3835 strv_free(arg_property);
3838 /* If the user asked for a particular
3839 * property, show it to him, even if it is
3858 arg_session = false;
3862 arg_no_block = true;
3870 arg_dot = DOT_ORDER;
3874 arg_dot = DOT_REQUIRE;
3890 arg_no_reload = true;
3899 arg_defaults = true;
3906 log_error("Unknown option code %c", c);
3914 static int halt_parse_argv(int argc, char *argv[]) {
3923 static const struct option options[] = {
3924 { "help", no_argument, NULL, ARG_HELP },
3925 { "halt", no_argument, NULL, ARG_HALT },
3926 { "poweroff", no_argument, NULL, 'p' },
3927 { "reboot", no_argument, NULL, ARG_REBOOT },
3928 { "force", no_argument, NULL, 'f' },
3929 { "wtmp-only", no_argument, NULL, 'w' },
3930 { "no-wtmp", no_argument, NULL, 'd' },
3931 { "no-sync", no_argument, NULL, 'n' },
3932 { "no-wall", no_argument, NULL, ARG_NO_WALL },
3933 { NULL, 0, NULL, 0 }
3941 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
3942 if (runlevel == '0' || runlevel == '6')
3943 arg_immediate = true;
3945 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
3953 arg_action = ACTION_HALT;
3957 if (arg_action != ACTION_REBOOT)
3958 arg_action = ACTION_POWEROFF;
3962 arg_action = ACTION_REBOOT;
3966 arg_immediate = true;
3987 /* Compatibility nops */
3994 log_error("Unknown option code %c", c);
3999 if (optind < argc) {
4000 log_error("Too many arguments.");
4007 static int shutdown_parse_argv(int argc, char *argv[]) {
4014 static const struct option options[] = {
4015 { "help", no_argument, NULL, ARG_HELP },
4016 { "halt", no_argument, NULL, 'H' },
4017 { "poweroff", no_argument, NULL, 'P' },
4018 { "reboot", no_argument, NULL, 'r' },
4019 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4020 { NULL, 0, NULL, 0 }
4028 while ((c = getopt_long(argc, argv, "HPrhkt:a", options, NULL)) >= 0) {
4036 arg_action = ACTION_HALT;
4040 arg_action = ACTION_POWEROFF;
4044 arg_action = ACTION_REBOOT;
4048 if (arg_action != ACTION_HALT)
4049 arg_action = ACTION_POWEROFF;
4062 /* Compatibility nops */
4069 log_error("Unknown option code %c", c);
4074 if (argc > optind && !streq(argv[optind], "now"))
4075 log_warning("First argument '%s' isn't 'now'. Ignoring.", argv[optind]);
4077 /* We ignore the time argument */
4078 if (argc > optind + 1)
4079 arg_wall = argv + optind + 1;
4086 static int telinit_parse_argv(int argc, char *argv[]) {
4093 static const struct option options[] = {
4094 { "help", no_argument, NULL, ARG_HELP },
4095 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4096 { NULL, 0, NULL, 0 }
4099 static const struct {
4103 { '0', ACTION_POWEROFF },
4104 { '6', ACTION_REBOOT },
4105 { '1', ACTION_RESCUE },
4106 { '2', ACTION_RUNLEVEL2 },
4107 { '3', ACTION_RUNLEVEL3 },
4108 { '4', ACTION_RUNLEVEL4 },
4109 { '5', ACTION_RUNLEVEL5 },
4110 { 's', ACTION_RESCUE },
4111 { 'S', ACTION_RESCUE },
4112 { 'q', ACTION_RELOAD },
4113 { 'Q', ACTION_RELOAD },
4114 { 'u', ACTION_REEXEC },
4115 { 'U', ACTION_REEXEC }
4124 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4139 log_error("Unknown option code %c", c);
4144 if (optind >= argc) {
4149 if (optind + 1 < argc) {
4150 log_error("Too many arguments.");
4154 if (strlen(argv[optind]) != 1) {
4155 log_error("Expected single character argument.");
4159 for (i = 0; i < ELEMENTSOF(table); i++)
4160 if (table[i].from == argv[optind][0])
4163 if (i >= ELEMENTSOF(table)) {
4164 log_error("Unknown command %s.", argv[optind]);
4168 arg_action = table[i].to;
4175 static int runlevel_parse_argv(int argc, char *argv[]) {
4181 static const struct option options[] = {
4182 { "help", no_argument, NULL, ARG_HELP },
4183 { NULL, 0, NULL, 0 }
4191 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4202 log_error("Unknown option code %c", c);
4207 if (optind < argc) {
4208 log_error("Too many arguments.");
4215 static int parse_argv(int argc, char *argv[]) {
4219 if (program_invocation_short_name) {
4221 if (strstr(program_invocation_short_name, "halt")) {
4222 arg_action = ACTION_HALT;
4223 return halt_parse_argv(argc, argv);
4224 } else if (strstr(program_invocation_short_name, "poweroff")) {
4225 arg_action = ACTION_POWEROFF;
4226 return halt_parse_argv(argc, argv);
4227 } else if (strstr(program_invocation_short_name, "reboot")) {
4228 arg_action = ACTION_REBOOT;
4229 return halt_parse_argv(argc, argv);
4230 } else if (strstr(program_invocation_short_name, "shutdown")) {
4231 arg_action = ACTION_POWEROFF;
4232 return shutdown_parse_argv(argc, argv);
4233 } else if (strstr(program_invocation_short_name, "init")) {
4235 if (sd_booted() > 0) {
4236 arg_action = ACTION_INVALID;
4237 return telinit_parse_argv(argc, argv);
4239 /* Hmm, so some other init system is
4240 * running, we need to forward this
4241 * request to it. For now we simply
4242 * guess that it is Upstart. */
4244 execv("/lib/upstart/telinit", argv);
4246 log_error("Couldn't find an alternative telinit implementation to spawn.");
4250 } else if (strstr(program_invocation_short_name, "runlevel")) {
4251 arg_action = ACTION_RUNLEVEL;
4252 return runlevel_parse_argv(argc, argv);
4256 arg_action = ACTION_SYSTEMCTL;
4257 return systemctl_parse_argv(argc, argv);
4260 static int action_to_runlevel(void) {
4262 static const char table[_ACTION_MAX] = {
4263 [ACTION_HALT] = '0',
4264 [ACTION_POWEROFF] = '0',
4265 [ACTION_REBOOT] = '6',
4266 [ACTION_RUNLEVEL2] = '2',
4267 [ACTION_RUNLEVEL3] = '3',
4268 [ACTION_RUNLEVEL4] = '4',
4269 [ACTION_RUNLEVEL5] = '5',
4270 [ACTION_RESCUE] = '1'
4273 assert(arg_action < _ACTION_MAX);
4275 return table[arg_action];
4278 static int talk_upstart(void) {
4279 DBusMessage *m = NULL, *reply = NULL;
4281 int previous, rl, r;
4283 env1_buf[] = "RUNLEVEL=X",
4284 env2_buf[] = "PREVLEVEL=X";
4285 char *env1 = env1_buf, *env2 = env2_buf;
4286 const char *emit = "runlevel";
4287 dbus_bool_t b_false = FALSE;
4288 DBusMessageIter iter, sub;
4289 DBusConnection *bus;
4291 dbus_error_init(&error);
4293 if (!(rl = action_to_runlevel()))
4296 if (utmp_get_runlevel(&previous, NULL) < 0)
4299 if (!(bus = dbus_connection_open_private("unix:abstract=/com/ubuntu/upstart", &error))) {
4300 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
4305 log_error("Failed to connect to Upstart bus: %s", error.message);
4310 if ((r = bus_check_peercred(bus)) < 0) {
4311 log_error("Failed to verify owner of bus.");
4315 if (!(m = dbus_message_new_method_call(
4316 "com.ubuntu.Upstart",
4317 "/com/ubuntu/Upstart",
4318 "com.ubuntu.Upstart0_6",
4321 log_error("Could not allocate message.");
4326 dbus_message_iter_init_append(m, &iter);
4328 env1_buf[sizeof(env1_buf)-2] = rl;
4329 env2_buf[sizeof(env2_buf)-2] = previous;
4331 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
4332 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
4333 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
4334 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
4335 !dbus_message_iter_close_container(&iter, &sub) ||
4336 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
4337 log_error("Could not append arguments to message.");
4342 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
4344 if (error_is_no_service(&error)) {
4349 log_error("Failed to issue method call: %s", error.message);
4358 dbus_message_unref(m);
4361 dbus_message_unref(reply);
4364 dbus_connection_close(bus);
4365 dbus_connection_unref(bus);
4368 dbus_error_free(&error);
4373 static int talk_initctl(void) {
4374 struct init_request request;
4378 if (!(rl = action_to_runlevel()))
4382 request.magic = INIT_MAGIC;
4383 request.sleeptime = 0;
4384 request.cmd = INIT_CMD_RUNLVL;
4385 request.runlevel = rl;
4387 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
4389 if (errno == ENOENT)
4392 log_error("Failed to open "INIT_FIFO": %m");
4397 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
4398 close_nointr_nofail(fd);
4401 log_error("Failed to write to "INIT_FIFO": %m");
4402 return errno ? -errno : -EIO;
4408 static int systemctl_main(DBusConnection *bus, int argc, char *argv[], DBusError *error) {
4410 static const struct {
4418 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
4420 { "list-units", LESS, 1, list_units },
4421 { "list-jobs", EQUAL, 1, list_jobs },
4422 { "clear-jobs", EQUAL, 1, daemon_reload },
4423 { "load", MORE, 2, load_unit },
4424 { "cancel", MORE, 2, cancel_job },
4425 { "start", MORE, 2, start_unit },
4426 { "stop", MORE, 2, start_unit },
4427 { "reload", MORE, 2, start_unit },
4428 { "restart", MORE, 2, start_unit },
4429 { "try-restart", MORE, 2, start_unit },
4430 { "reload-or-restart", MORE, 2, start_unit },
4431 { "reload-or-try-restart", MORE, 2, start_unit },
4432 { "force-reload", MORE, 2, start_unit }, /* For compatibility with SysV */
4433 { "condrestart", MORE, 2, start_unit }, /* For compatibility with RH */
4434 { "isolate", EQUAL, 2, start_unit },
4435 { "is-active", MORE, 2, check_unit },
4436 { "check", MORE, 2, check_unit },
4437 { "show", MORE, 1, show },
4438 { "status", MORE, 2, show },
4439 { "monitor", EQUAL, 1, monitor },
4440 { "dump", EQUAL, 1, dump },
4441 { "dot", EQUAL, 1, dot },
4442 { "snapshot", LESS, 2, snapshot },
4443 { "delete", MORE, 2, delete_snapshot },
4444 { "daemon-reload", EQUAL, 1, daemon_reload },
4445 { "daemon-reexec", EQUAL, 1, daemon_reload },
4446 { "daemon-exit", EQUAL, 1, daemon_reload },
4447 { "show-environment", EQUAL, 1, show_enviroment },
4448 { "set-environment", MORE, 2, set_environment },
4449 { "unset-environment", MORE, 2, set_environment },
4450 { "halt", EQUAL, 1, start_special },
4451 { "poweroff", EQUAL, 1, start_special },
4452 { "reboot", EQUAL, 1, start_special },
4453 { "default", EQUAL, 1, start_special },
4454 { "rescue", EQUAL, 1, start_special },
4455 { "emergency", EQUAL, 1, start_special },
4456 { "reset-maintenance", MORE, 1, reset_maintenance },
4457 { "enable", MORE, 2, enable_unit },
4458 { "disable", MORE, 2, enable_unit },
4459 { "is-enabled", MORE, 2, enable_unit }
4469 left = argc - optind;
4472 /* Special rule: no arguments means "list-units" */
4475 if (streq(argv[optind], "help")) {
4480 for (i = 0; i < ELEMENTSOF(verbs); i++)
4481 if (streq(argv[optind], verbs[i].verb))
4484 if (i >= ELEMENTSOF(verbs)) {
4485 log_error("Unknown operation %s", argv[optind]);
4490 switch (verbs[i].argc_cmp) {
4493 if (left != verbs[i].argc) {
4494 log_error("Invalid number of arguments.");
4501 if (left < verbs[i].argc) {
4502 log_error("Too few arguments.");
4509 if (left > verbs[i].argc) {
4510 log_error("Too many arguments.");
4517 assert_not_reached("Unknown comparison operator.");
4520 /* Require a bus connection for all operations but
4522 if (!streq(verbs[i].verb, "enable") &&
4523 !streq(verbs[i].verb, "disable") &&
4525 log_error("Failed to get D-Bus connection: %s", error->message);
4529 return verbs[i].dispatch(bus, argv + optind, left);
4532 static int reload_with_fallback(DBusConnection *bus) {
4536 /* First, try systemd via D-Bus. */
4537 if ((r = daemon_reload(bus, NULL, 0)) > 0)
4541 /* Nothing else worked, so let's try signals */
4542 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
4544 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
4545 log_error("kill() failed: %m");
4552 static int start_with_fallback(DBusConnection *bus) {
4556 /* First, try systemd via D-Bus. */
4557 if ((r = start_unit(bus, NULL, 0)) > 0)
4561 /* Hmm, talking to systemd via D-Bus didn't work. Then
4562 * let's try to talk to Upstart via D-Bus. */
4563 if ((r = talk_upstart()) > 0)
4566 /* Nothing else worked, so let's try
4568 if ((r = talk_initctl()) != 0)
4571 log_error("Failed to talk to init daemon.");
4575 warn_wall(arg_action);
4579 static int halt_main(DBusConnection *bus) {
4582 if (geteuid() != 0) {
4583 log_error("Must to be root.");
4587 if (!arg_dry && !arg_immediate)
4588 return start_with_fallback(bus);
4591 if ((r = utmp_put_shutdown(0)) < 0)
4592 log_warning("Failed to write utmp record: %s", strerror(-r));
4600 /* Make sure C-A-D is handled by the kernel from this
4602 reboot(RB_ENABLE_CAD);
4604 switch (arg_action) {
4607 log_info("Halting");
4608 reboot(RB_HALT_SYSTEM);
4611 case ACTION_POWEROFF:
4612 log_info("Powering off");
4613 reboot(RB_POWER_OFF);
4617 log_info("Rebooting");
4618 reboot(RB_AUTOBOOT);
4622 assert_not_reached("Unknown halt action.");
4625 /* We should never reach this. */
4629 static int runlevel_main(void) {
4630 int r, runlevel, previous;
4632 if ((r = utmp_get_runlevel(&runlevel, &previous)) < 0) {
4633 printf("unknown\n");
4638 previous <= 0 ? 'N' : previous,
4639 runlevel <= 0 ? 'N' : runlevel);
4644 int main(int argc, char*argv[]) {
4646 DBusConnection *bus = NULL;
4649 dbus_error_init(&error);
4651 log_parse_environment();
4653 if ((r = parse_argv(argc, argv)) < 0)
4660 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
4661 * let's shortcut this */
4662 if (arg_action == ACTION_RUNLEVEL) {
4663 retval = runlevel_main() < 0;
4667 bus_connect(arg_session ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &bus, &private_bus, &error);
4669 switch (arg_action) {
4671 case ACTION_SYSTEMCTL: {
4672 retval = systemctl_main(bus, argc, argv, &error) < 0;
4677 case ACTION_POWEROFF:
4679 retval = halt_main(bus) < 0;
4682 case ACTION_RUNLEVEL2:
4683 case ACTION_RUNLEVEL3:
4684 case ACTION_RUNLEVEL4:
4685 case ACTION_RUNLEVEL5:
4687 case ACTION_EMERGENCY:
4688 case ACTION_DEFAULT:
4689 retval = start_with_fallback(bus) < 0;
4694 retval = reload_with_fallback(bus) < 0;
4697 case ACTION_INVALID:
4698 case ACTION_RUNLEVEL:
4700 assert_not_reached("Unknown action");
4706 dbus_connection_close(bus);
4707 dbus_connection_unref(bus);
4710 dbus_error_free(&error);
4714 strv_free(arg_property);