1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
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]);
193 const char *description;
194 const char *load_state;
195 const char *active_state;
196 const char *sub_state;
197 const char *following;
198 const char *unit_path;
200 const char *job_type;
201 const char *job_path;
204 static int compare_unit_info(const void *a, const void *b) {
206 const struct unit_info *u = a, *v = b;
208 d1 = strrchr(u->id, '.');
209 d2 = strrchr(v->id, '.');
214 if ((r = strcmp(d1, d2)) != 0)
218 return strcmp(u->id, v->id);
221 static int list_units(DBusConnection *bus, char **args, unsigned n) {
222 DBusMessage *m = NULL, *reply = NULL;
225 DBusMessageIter iter, sub, sub2;
226 unsigned c = 0, k, n_units = 0;
227 struct unit_info *unit_infos = NULL;
229 dbus_error_init(&error);
233 if (!(m = dbus_message_new_method_call(
234 "org.freedesktop.systemd1",
235 "/org/freedesktop/systemd1",
236 "org.freedesktop.systemd1.Manager",
238 log_error("Could not allocate message.");
242 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
243 log_error("Failed to issue method call: %s", error.message);
248 if (!dbus_message_iter_init(reply, &iter) ||
249 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
250 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
251 log_error("Failed to parse reply.");
256 dbus_message_iter_recurse(&iter, &sub);
258 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
261 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
262 log_error("Failed to parse reply.");
270 n_units = MAX(2*c, 16);
271 w = realloc(unit_infos, sizeof(struct unit_info) * n_units);
274 log_error("Failed to allocate unit array.");
284 dbus_message_iter_recurse(&sub, &sub2);
286 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->id, true) < 0 ||
287 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->description, true) < 0 ||
288 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->load_state, true) < 0 ||
289 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->active_state, true) < 0 ||
290 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->sub_state, true) < 0 ||
291 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->following, true) < 0 ||
292 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &u->unit_path, true) < 0 ||
293 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &u->job_id, true) < 0 ||
294 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->job_type, true) < 0 ||
295 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &u->job_path, false) < 0) {
296 log_error("Failed to parse reply.");
301 dbus_message_iter_next(&sub);
305 qsort(unit_infos, c, sizeof(struct unit_info), compare_unit_info);
307 if (isatty(STDOUT_FILENO))
308 printf("%-45s %-6s %-12s %-12s %-15s %s\n", "UNIT", "LOAD", "ACTIVE", "SUB", "JOB", "DESCRIPTION");
310 for (k = 0; k < c; k++) {
312 struct unit_info *u = unit_infos+k;
314 if ((!arg_type || ((dot = strrchr(u->id, '.')) &&
315 streq(dot+1, arg_type))) &&
316 (arg_all || !(streq(u->active_state, "inactive") || u->following[0]) || u->job_id > 0)) {
319 const char *on, *off;
321 if (streq(u->active_state, "maintenance")) {
322 on = ansi_highlight(true);
323 off = ansi_highlight(false);
327 e = arg_full ? NULL : ellipsize(u->id, 45, 33);
328 printf("%-45s %-6s %s%-12s %-12s%s%n", e ? e : u->id, u->load_state, on, u->active_state, u->sub_state, off, &a);
331 a -= strlen(on) + strlen(off);
334 printf(" => %-12s%n", u->job_type, &b);
338 if (a + b + 2 < columns()) {
342 printf(" %.*s", columns() - a - b - 2, u->description);
349 if (isatty(STDOUT_FILENO)) {
351 printf("\nLOAD = Load State, reflects whether the unit configuration was properly loaded.\n"
352 "ACTIVE = Active State, the high-level unit activation state, i.e. generalization of the substate.\n"
353 "SUB = Substate, the low-level unit activation state, possible values depend on unit type.\n"
354 "JOB = Job, shows pending jobs for the unit.\n");
357 printf("\n%u units listed.\n", c);
359 printf("\n%u units listed. Pass --all to see inactive units, too.\n", c);
366 dbus_message_unref(m);
369 dbus_message_unref(reply);
373 dbus_error_free(&error);
378 static int dot_one_property(const char *name, const char *prop, DBusMessageIter *iter) {
379 static const char * const colors[] = {
380 "Requires", "[color=\"black\"]",
381 "RequiresOverridable", "[color=\"black\"]",
382 "Requisite", "[color=\"darkblue\"]",
383 "RequisiteOverridable", "[color=\"darkblue\"]",
384 "Wants", "[color=\"darkgrey\"]",
385 "Conflicts", "[color=\"red\"]",
386 "ConflictedBy", "[color=\"red\"]",
387 "After", "[color=\"green\"]"
390 const char *c = NULL;
397 for (i = 0; i < ELEMENTSOF(colors); i += 2)
398 if (streq(colors[i], prop)) {
406 if (arg_dot != DOT_ALL)
407 if ((arg_dot == DOT_ORDER) != streq(prop, "After"))
410 switch (dbus_message_iter_get_arg_type(iter)) {
412 case DBUS_TYPE_ARRAY:
414 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
417 dbus_message_iter_recurse(iter, &sub);
419 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
422 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
423 dbus_message_iter_get_basic(&sub, &s);
424 printf("\t\"%s\"->\"%s\" %s;\n", name, s, c);
426 dbus_message_iter_next(&sub);
436 static int dot_one(DBusConnection *bus, const char *name, const char *path) {
437 DBusMessage *m = NULL, *reply = NULL;
438 const char *interface = "org.freedesktop.systemd1.Unit";
441 DBusMessageIter iter, sub, sub2, sub3;
446 dbus_error_init(&error);
448 if (!(m = dbus_message_new_method_call(
449 "org.freedesktop.systemd1",
451 "org.freedesktop.DBus.Properties",
453 log_error("Could not allocate message.");
458 if (!dbus_message_append_args(m,
459 DBUS_TYPE_STRING, &interface,
460 DBUS_TYPE_INVALID)) {
461 log_error("Could not append arguments to message.");
466 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
467 log_error("Failed to issue method call: %s", error.message);
472 if (!dbus_message_iter_init(reply, &iter) ||
473 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
474 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
475 log_error("Failed to parse reply.");
480 dbus_message_iter_recurse(&iter, &sub);
482 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
485 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
486 log_error("Failed to parse reply.");
491 dbus_message_iter_recurse(&sub, &sub2);
493 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &prop, true) < 0) {
494 log_error("Failed to parse reply.");
499 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
500 log_error("Failed to parse reply.");
505 dbus_message_iter_recurse(&sub2, &sub3);
507 if (dot_one_property(name, prop, &sub3)) {
508 log_error("Failed to parse reply.");
513 dbus_message_iter_next(&sub);
520 dbus_message_unref(m);
523 dbus_message_unref(reply);
525 dbus_error_free(&error);
530 static int dot(DBusConnection *bus, char **args, unsigned n) {
531 DBusMessage *m = NULL, *reply = NULL;
534 DBusMessageIter iter, sub, sub2;
536 dbus_error_init(&error);
540 if (!(m = dbus_message_new_method_call(
541 "org.freedesktop.systemd1",
542 "/org/freedesktop/systemd1",
543 "org.freedesktop.systemd1.Manager",
545 log_error("Could not allocate message.");
549 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
550 log_error("Failed to issue method call: %s", error.message);
555 if (!dbus_message_iter_init(reply, &iter) ||
556 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
557 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
558 log_error("Failed to parse reply.");
563 printf("digraph systemd {\n");
565 dbus_message_iter_recurse(&iter, &sub);
566 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
567 const char *id, *description, *load_state, *active_state, *sub_state, *following, *unit_path;
569 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
570 log_error("Failed to parse reply.");
575 dbus_message_iter_recurse(&sub, &sub2);
577 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
578 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
579 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
580 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
581 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
582 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &following, true) < 0 ||
583 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, true) < 0) {
584 log_error("Failed to parse reply.");
589 if ((r = dot_one(bus, id, unit_path)) < 0)
592 /* printf("\t\"%s\";\n", id); */
593 dbus_message_iter_next(&sub);
598 log_info(" Color legend: black = Requires\n"
599 " dark blue = Requisite\n"
600 " dark grey = Wants\n"
604 if (isatty(fileno(stdout)))
605 log_notice("-- You probably want to process this output with graphviz' dot tool.\n"
606 "-- Try a shell pipeline like 'systemctl dot | dot -Tsvg > systemd.svg'!\n");
612 dbus_message_unref(m);
615 dbus_message_unref(reply);
617 dbus_error_free(&error);
622 static int list_jobs(DBusConnection *bus, char **args, unsigned n) {
623 DBusMessage *m = NULL, *reply = NULL;
626 DBusMessageIter iter, sub, sub2;
629 dbus_error_init(&error);
633 if (!(m = dbus_message_new_method_call(
634 "org.freedesktop.systemd1",
635 "/org/freedesktop/systemd1",
636 "org.freedesktop.systemd1.Manager",
638 log_error("Could not allocate message.");
642 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
643 log_error("Failed to issue method call: %s", error.message);
648 if (!dbus_message_iter_init(reply, &iter) ||
649 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
650 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
651 log_error("Failed to parse reply.");
656 dbus_message_iter_recurse(&iter, &sub);
658 printf("%4s %-45s %-17s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
660 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
661 const char *name, *type, *state, *job_path, *unit_path;
665 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
666 log_error("Failed to parse reply.");
671 dbus_message_iter_recurse(&sub, &sub2);
673 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
674 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
675 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
676 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
677 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
678 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
679 log_error("Failed to parse reply.");
684 e = arg_full ? NULL : ellipsize(name, 45, 33);
685 printf("%4u %-45s %-17s %-7s\n", id, e ? e : name, type, state);
690 dbus_message_iter_next(&sub);
693 printf("\n%u jobs listed.\n", k);
698 dbus_message_unref(m);
701 dbus_message_unref(reply);
703 dbus_error_free(&error);
708 static int load_unit(DBusConnection *bus, char **args, unsigned n) {
709 DBusMessage *m = NULL, *reply = NULL;
714 dbus_error_init(&error);
719 for (i = 1; i < n; i++) {
721 if (!(m = dbus_message_new_method_call(
722 "org.freedesktop.systemd1",
723 "/org/freedesktop/systemd1",
724 "org.freedesktop.systemd1.Manager",
726 log_error("Could not allocate message.");
731 if (!dbus_message_append_args(m,
732 DBUS_TYPE_STRING, &args[i],
733 DBUS_TYPE_INVALID)) {
734 log_error("Could not append arguments to message.");
739 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
740 log_error("Failed to issue method call: %s", error.message);
745 dbus_message_unref(m);
746 dbus_message_unref(reply);
755 dbus_message_unref(m);
758 dbus_message_unref(reply);
760 dbus_error_free(&error);
765 static int cancel_job(DBusConnection *bus, char **args, unsigned n) {
766 DBusMessage *m = NULL, *reply = NULL;
771 dbus_error_init(&error);
777 return daemon_reload(bus, args, n);
779 for (i = 1; i < n; i++) {
783 if (!(m = dbus_message_new_method_call(
784 "org.freedesktop.systemd1",
785 "/org/freedesktop/systemd1",
786 "org.freedesktop.systemd1.Manager",
788 log_error("Could not allocate message.");
793 if ((r = safe_atou(args[i], &id)) < 0) {
794 log_error("Failed to parse job id: %s", strerror(-r));
798 assert_cc(sizeof(uint32_t) == sizeof(id));
799 if (!dbus_message_append_args(m,
800 DBUS_TYPE_UINT32, &id,
801 DBUS_TYPE_INVALID)) {
802 log_error("Could not append arguments to message.");
807 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
808 log_error("Failed to issue method call: %s", error.message);
813 if (!dbus_message_get_args(reply, &error,
814 DBUS_TYPE_OBJECT_PATH, &path,
815 DBUS_TYPE_INVALID)) {
816 log_error("Failed to parse reply: %s", error.message);
821 dbus_message_unref(m);
822 if (!(m = dbus_message_new_method_call(
823 "org.freedesktop.systemd1",
825 "org.freedesktop.systemd1.Job",
827 log_error("Could not allocate message.");
832 dbus_message_unref(reply);
833 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
834 log_error("Failed to issue method call: %s", error.message);
839 dbus_message_unref(m);
840 dbus_message_unref(reply);
848 dbus_message_unref(m);
851 dbus_message_unref(reply);
853 dbus_error_free(&error);
858 static bool need_daemon_reload(DBusConnection *bus, const char *unit) {
859 DBusMessage *m = NULL, *reply = NULL;
860 dbus_bool_t b = FALSE;
861 DBusMessageIter iter, sub;
863 *interface = "org.freedesktop.systemd1.Unit",
864 *property = "NeedDaemonReload",
867 /* We ignore all errors here, since this is used to show a warning only */
869 if (!(m = dbus_message_new_method_call(
870 "org.freedesktop.systemd1",
871 "/org/freedesktop/systemd1",
872 "org.freedesktop.systemd1.Manager",
876 if (!dbus_message_append_args(m,
877 DBUS_TYPE_STRING, &unit,
881 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
884 if (!dbus_message_get_args(reply, NULL,
885 DBUS_TYPE_OBJECT_PATH, &path,
889 dbus_message_unref(m);
890 if (!(m = dbus_message_new_method_call(
891 "org.freedesktop.systemd1",
893 "org.freedesktop.DBus.Properties",
897 if (!dbus_message_append_args(m,
898 DBUS_TYPE_STRING, &interface,
899 DBUS_TYPE_STRING, &property,
900 DBUS_TYPE_INVALID)) {
904 dbus_message_unref(reply);
905 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
908 if (!dbus_message_iter_init(reply, &iter) ||
909 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
912 dbus_message_iter_recurse(&iter, &sub);
914 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_BOOLEAN)
917 dbus_message_iter_get_basic(&sub, &b);
921 dbus_message_unref(m);
924 dbus_message_unref(reply);
929 typedef struct WaitData {
934 static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
942 dbus_error_init(&error);
944 log_debug("Got D-Bus request: %s.%s() on %s",
945 dbus_message_get_interface(message),
946 dbus_message_get_member(message),
947 dbus_message_get_path(message));
949 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
950 log_error("Warning! D-Bus connection terminated.");
951 dbus_connection_close(connection);
953 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
956 dbus_bool_t success = true;
958 if (!dbus_message_get_args(message, &error,
959 DBUS_TYPE_UINT32, &id,
960 DBUS_TYPE_OBJECT_PATH, &path,
961 DBUS_TYPE_BOOLEAN, &success,
963 log_error("Failed to parse message: %s", error.message);
967 if ((p = set_remove(d->set, (char*) path)))
975 dbus_error_free(&error);
976 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
979 static int enable_wait_for_jobs(DBusConnection *bus) {
987 dbus_error_init(&error);
988 dbus_bus_add_match(bus,
990 "sender='org.freedesktop.systemd1',"
991 "interface='org.freedesktop.systemd1.Manager',"
992 "member='JobRemoved',"
993 "path='/org/freedesktop/systemd1'",
996 if (dbus_error_is_set(&error)) {
997 log_error("Failed to add match: %s", error.message);
998 dbus_error_free(&error);
1002 /* This is slightly dirty, since we don't undo the match registrations. */
1006 static int wait_for_jobs(DBusConnection *bus, Set *s) {
1017 if (!dbus_connection_add_filter(bus, wait_filter, &d, NULL)) {
1018 log_error("Failed to add filter.");
1023 while (!set_isempty(s) &&
1024 dbus_connection_read_write_dispatch(bus, -1))
1027 if (!arg_quiet && d.failed)
1028 log_error("Job failed, see logs for details.");
1030 r = d.failed ? -EIO : 0;
1033 /* This is slightly dirty, since we don't undo the filter registration. */
1038 static int start_unit_one(
1039 DBusConnection *bus,
1045 DBusMessage *m = NULL, *reply = NULL;
1054 assert(arg_no_block || s);
1056 dbus_error_init(&error);
1058 if (!(m = dbus_message_new_method_call(
1059 "org.freedesktop.systemd1",
1060 "/org/freedesktop/systemd1",
1061 "org.freedesktop.systemd1.Manager",
1063 log_error("Could not allocate message.");
1068 if (!dbus_message_append_args(m,
1069 DBUS_TYPE_STRING, &name,
1070 DBUS_TYPE_STRING, &mode,
1071 DBUS_TYPE_INVALID)) {
1072 log_error("Could not append arguments to message.");
1077 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1079 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
1080 /* There's always a fallback possible for
1081 * legacy actions. */
1086 log_error("Failed to issue method call: %s", error.message);
1091 if (!dbus_message_get_args(reply, &error,
1092 DBUS_TYPE_OBJECT_PATH, &path,
1093 DBUS_TYPE_INVALID)) {
1094 log_error("Failed to parse reply: %s", error.message);
1099 if (need_daemon_reload(bus, name))
1100 log_warning("Unit file of created job changed on disk, 'systemctl %s daemon-reload' recommended.",
1101 arg_session ? "--session" : "--system");
1103 if (!arg_no_block) {
1106 if (!(p = strdup(path))) {
1107 log_error("Failed to duplicate path.");
1112 if ((r = set_put(s, p)) < 0) {
1114 log_error("Failed to add path to set.");
1123 dbus_message_unref(m);
1126 dbus_message_unref(reply);
1128 dbus_error_free(&error);
1133 static enum action verb_to_action(const char *verb) {
1134 if (streq(verb, "halt"))
1136 else if (streq(verb, "poweroff"))
1137 return ACTION_POWEROFF;
1138 else if (streq(verb, "reboot"))
1139 return ACTION_REBOOT;
1140 else if (streq(verb, "rescue"))
1141 return ACTION_RESCUE;
1142 else if (streq(verb, "emergency"))
1143 return ACTION_EMERGENCY;
1144 else if (streq(verb, "default"))
1145 return ACTION_DEFAULT;
1147 return ACTION_INVALID;
1150 static int start_unit(DBusConnection *bus, char **args, unsigned n) {
1152 static const char * const table[_ACTION_MAX] = {
1153 [ACTION_HALT] = SPECIAL_HALT_TARGET,
1154 [ACTION_POWEROFF] = SPECIAL_POWEROFF_TARGET,
1155 [ACTION_REBOOT] = SPECIAL_REBOOT_TARGET,
1156 [ACTION_RUNLEVEL2] = SPECIAL_RUNLEVEL2_TARGET,
1157 [ACTION_RUNLEVEL3] = SPECIAL_RUNLEVEL3_TARGET,
1158 [ACTION_RUNLEVEL4] = SPECIAL_RUNLEVEL4_TARGET,
1159 [ACTION_RUNLEVEL5] = SPECIAL_RUNLEVEL5_TARGET,
1160 [ACTION_RESCUE] = SPECIAL_RESCUE_TARGET,
1161 [ACTION_EMERGENCY] = SPECIAL_EMERGENCY_TARGET,
1162 [ACTION_DEFAULT] = SPECIAL_DEFAULT_TARGET
1167 const char *method, *mode, *one_name;
1172 if (arg_action == ACTION_SYSTEMCTL) {
1174 streq(args[0], "stop") ? "StopUnit" :
1175 streq(args[0], "reload") ? "ReloadUnit" :
1176 streq(args[0], "restart") ? "RestartUnit" :
1177 streq(args[0], "try-restart") ? "TryRestartUnit" :
1178 streq(args[0], "reload-or-restart") ? "ReloadOrRestartUnit" :
1179 streq(args[0], "reload-or-try-restart") ||
1180 streq(args[0], "force-reload") ||
1181 streq(args[0], "condrestart") ? "ReloadOrTryRestartUnit" :
1185 (streq(args[0], "isolate") ||
1186 streq(args[0], "rescue") ||
1187 streq(args[0], "emergency")) ? "isolate" :
1191 one_name = table[verb_to_action(args[0])];
1194 assert(arg_action < ELEMENTSOF(table));
1195 assert(table[arg_action]);
1197 method = "StartUnit";
1199 mode = (arg_action == ACTION_EMERGENCY ||
1200 arg_action == ACTION_RESCUE) ? "isolate" : "replace";
1202 one_name = table[arg_action];
1205 if (!arg_no_block) {
1206 if ((r = enable_wait_for_jobs(bus)) < 0) {
1207 log_error("Could not watch jobs: %s", strerror(-r));
1211 if (!(s = set_new(string_hash_func, string_compare_func))) {
1212 log_error("Failed to allocate set.");
1221 if ((r = start_unit_one(bus, method, one_name, mode, s)) <= 0)
1224 for (i = 1; i < n; i++)
1225 if ((r = start_unit_one(bus, method, args[i], mode, s)) < 0)
1230 if ((r = wait_for_jobs(bus, s)) < 0)
1242 static int start_special(DBusConnection *bus, char **args, unsigned n) {
1248 r = start_unit(bus, args, n);
1251 warn_wall(verb_to_action(args[0]));
1256 static int check_unit(DBusConnection *bus, char **args, unsigned n) {
1257 DBusMessage *m = NULL, *reply = NULL;
1259 *interface = "org.freedesktop.systemd1.Unit",
1260 *property = "ActiveState";
1261 int r = -EADDRNOTAVAIL;
1268 dbus_error_init(&error);
1270 for (i = 1; i < n; i++) {
1271 const char *path = NULL;
1273 DBusMessageIter iter, sub;
1275 if (!(m = dbus_message_new_method_call(
1276 "org.freedesktop.systemd1",
1277 "/org/freedesktop/systemd1",
1278 "org.freedesktop.systemd1.Manager",
1280 log_error("Could not allocate message.");
1285 if (!dbus_message_append_args(m,
1286 DBUS_TYPE_STRING, &args[i],
1287 DBUS_TYPE_INVALID)) {
1288 log_error("Could not append arguments to message.");
1293 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1295 /* Hmm, cannot figure out anything about this unit... */
1299 dbus_error_free(&error);
1303 if (!dbus_message_get_args(reply, &error,
1304 DBUS_TYPE_OBJECT_PATH, &path,
1305 DBUS_TYPE_INVALID)) {
1306 log_error("Failed to parse reply: %s", error.message);
1311 dbus_message_unref(m);
1312 if (!(m = dbus_message_new_method_call(
1313 "org.freedesktop.systemd1",
1315 "org.freedesktop.DBus.Properties",
1317 log_error("Could not allocate message.");
1322 if (!dbus_message_append_args(m,
1323 DBUS_TYPE_STRING, &interface,
1324 DBUS_TYPE_STRING, &property,
1325 DBUS_TYPE_INVALID)) {
1326 log_error("Could not append arguments to message.");
1331 dbus_message_unref(reply);
1332 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1333 log_error("Failed to issue method call: %s", error.message);
1338 if (!dbus_message_iter_init(reply, &iter) ||
1339 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1340 log_error("Failed to parse reply.");
1345 dbus_message_iter_recurse(&iter, &sub);
1347 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1348 log_error("Failed to parse reply.");
1353 dbus_message_iter_get_basic(&sub, &state);
1358 if (streq(state, "active") || startswith(state, "reloading"))
1361 dbus_message_unref(m);
1362 dbus_message_unref(reply);
1368 dbus_message_unref(m);
1371 dbus_message_unref(reply);
1373 dbus_error_free(&error);
1378 typedef struct ExecStatusInfo {
1384 usec_t start_timestamp;
1385 usec_t exit_timestamp;
1390 LIST_FIELDS(struct ExecStatusInfo, exec);
1393 static void exec_status_info_free(ExecStatusInfo *i) {
1401 static int exec_status_info_deserialize(DBusMessageIter *sub, ExecStatusInfo *i) {
1402 uint64_t start_timestamp, exit_timestamp;
1403 DBusMessageIter sub2, sub3;
1407 int32_t code, status;
1413 if (dbus_message_iter_get_arg_type(sub) != DBUS_TYPE_STRUCT)
1416 dbus_message_iter_recurse(sub, &sub2);
1418 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
1421 if (!(i->path = strdup(path)))
1424 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
1425 dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
1429 dbus_message_iter_recurse(&sub2, &sub3);
1430 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1431 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1432 dbus_message_iter_next(&sub3);
1437 if (!(i->argv = new0(char*, n+1)))
1441 dbus_message_iter_recurse(&sub2, &sub3);
1442 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1445 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1446 dbus_message_iter_get_basic(&sub3, &s);
1447 dbus_message_iter_next(&sub3);
1449 if (!(i->argv[n++] = strdup(s)))
1453 if (!dbus_message_iter_next(&sub2) ||
1454 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, true) < 0 ||
1455 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp, true) < 0 ||
1456 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp, true) < 0 ||
1457 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) < 0 ||
1458 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) < 0 ||
1459 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) < 0)
1463 i->start_timestamp = (usec_t) start_timestamp;
1464 i->exit_timestamp = (usec_t) exit_timestamp;
1465 i->pid = (pid_t) pid;
1472 typedef struct UnitStatusInfo {
1474 const char *load_state;
1475 const char *active_state;
1476 const char *sub_state;
1478 const char *description;
1481 const char *default_control_group;
1483 bool need_daemon_reload;
1488 const char *status_text;
1491 usec_t start_timestamp;
1492 usec_t exit_timestamp;
1494 int exit_code, exit_status;
1497 unsigned n_accepted;
1498 unsigned n_connections;
1502 const char *sysfs_path;
1504 /* Mount, Automount */
1510 LIST_HEAD(ExecStatusInfo, exec);
1513 static void print_status_info(UnitStatusInfo *i) {
1515 const char *on, *off, *ss;
1519 /* This shows pretty information about a unit. See
1520 * print_property() for a low-level property printer */
1522 printf("%s", strna(i->id));
1524 if (i->description && !streq_ptr(i->id, i->description))
1525 printf(" - %s", i->description);
1529 if (streq_ptr(i->load_state, "failed")) {
1530 on = ansi_highlight(true);
1531 off = ansi_highlight(false);
1536 printf("\t Loaded: %s%s%s (%s)\n", on, strna(i->load_state), off, i->path);
1538 printf("\t Loaded: %s%s%s\n", on, strna(i->load_state), off);
1540 ss = streq_ptr(i->active_state, i->sub_state) ? NULL : i->sub_state;
1542 if (streq_ptr(i->active_state, "maintenance")) {
1543 on = ansi_highlight(true);
1544 off = ansi_highlight(false);
1545 } else if (streq_ptr(i->active_state, "active") || streq_ptr(i->active_state, "reloading")) {
1546 on = ansi_highlight_green(true);
1547 off = ansi_highlight_green(false);
1552 printf("\t Active: %s%s (%s)%s\n",
1554 strna(i->active_state),
1558 printf("\t Active: %s%s%s\n",
1560 strna(i->active_state),
1564 printf("\t Device: %s\n", i->sysfs_path);
1566 printf("\t Where: %s\n", i->where);
1568 printf("\t What: %s\n", i->what);
1571 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
1573 LIST_FOREACH(exec, p, i->exec) {
1576 /* Only show exited processes here */
1580 t = strv_join(p->argv, " ");
1581 printf("\t Exited: %u (%s, code=%s, ", p->pid, strna(t), sigchld_code_to_string(p->code));
1584 if (p->code == CLD_EXITED)
1585 printf("status=%i", p->status);
1587 printf("signal=%s", signal_to_string(p->status));
1590 if (i->main_pid == p->pid &&
1591 i->start_timestamp == p->start_timestamp &&
1592 i->exit_timestamp == p->start_timestamp)
1593 /* Let's not show this twice */
1596 if (p->pid == i->control_pid)
1600 if (i->main_pid > 0 || i->control_pid > 0) {
1603 if (i->main_pid > 0) {
1604 printf(" Main: %u", (unsigned) i->main_pid);
1608 get_process_name(i->main_pid, &t);
1613 } else if (i->exit_code > 0) {
1614 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
1616 if (i->exit_code == CLD_EXITED)
1617 printf("status=%i", i->exit_status);
1619 printf("signal=%s", signal_to_string(i->exit_status));
1624 if (i->main_pid > 0 && i->control_pid > 0)
1627 if (i->control_pid > 0) {
1630 printf(" Control: %u", (unsigned) i->control_pid);
1632 get_process_name(i->control_pid, &t);
1643 printf("\t Status: \"%s\"\n", i->status_text);
1645 if (i->default_control_group) {
1648 printf("\t CGroup: %s\n", i->default_control_group);
1650 if ((c = columns()) > 18)
1655 show_cgroup_by_path(i->default_control_group, "\t\t ", c);
1658 if (i->need_daemon_reload)
1659 printf("\n%sWarning:%s Unit file changed on disk, 'systemctl %s daemon-reload' recommended.\n",
1660 ansi_highlight(true),
1661 ansi_highlight(false),
1662 arg_session ? "--session" : "--system");
1665 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
1667 switch (dbus_message_iter_get_arg_type(iter)) {
1669 case DBUS_TYPE_STRING: {
1672 dbus_message_iter_get_basic(iter, &s);
1675 if (streq(name, "Id"))
1677 else if (streq(name, "LoadState"))
1679 else if (streq(name, "ActiveState"))
1680 i->active_state = s;
1681 else if (streq(name, "SubState"))
1683 else if (streq(name, "Description"))
1685 else if (streq(name, "FragmentPath"))
1687 else if (streq(name, "SysVPath"))
1689 else if (streq(name, "DefaultControlGroup"))
1690 i->default_control_group = s;
1691 else if (streq(name, "StatusText"))
1693 else if (streq(name, "SysFSPath"))
1695 else if (streq(name, "Where"))
1697 else if (streq(name, "What"))
1704 case DBUS_TYPE_BOOLEAN: {
1707 dbus_message_iter_get_basic(iter, &b);
1709 if (streq(name, "Accept"))
1711 else if (streq(name, "NeedDaemonReload"))
1712 i->need_daemon_reload = b;
1717 case DBUS_TYPE_UINT32: {
1720 dbus_message_iter_get_basic(iter, &u);
1722 if (streq(name, "MainPID")) {
1724 i->main_pid = (pid_t) u;
1727 } else if (streq(name, "ControlPID"))
1728 i->control_pid = (pid_t) u;
1729 else if (streq(name, "ExecMainPID")) {
1731 i->main_pid = (pid_t) u;
1732 } else if (streq(name, "NAccepted"))
1734 else if (streq(name, "NConnections"))
1735 i->n_connections = u;
1740 case DBUS_TYPE_INT32: {
1743 dbus_message_iter_get_basic(iter, &j);
1745 if (streq(name, "ExecMainCode"))
1746 i->exit_code = (int) j;
1747 else if (streq(name, "ExecMainStatus"))
1748 i->exit_status = (int) j;
1753 case DBUS_TYPE_UINT64: {
1756 dbus_message_iter_get_basic(iter, &u);
1758 if (streq(name, "ExecMainStartTimestamp"))
1759 i->start_timestamp = (usec_t) u;
1760 else if (streq(name, "ExecMainExitTimestamp"))
1761 i->exit_timestamp = (usec_t) u;
1766 case DBUS_TYPE_ARRAY: {
1768 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT &&
1769 startswith(name, "Exec")) {
1770 DBusMessageIter sub;
1772 dbus_message_iter_recurse(iter, &sub);
1773 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1774 ExecStatusInfo *info;
1777 if (!(info = new0(ExecStatusInfo, 1)))
1780 if ((r = exec_status_info_deserialize(&sub, info)) < 0) {
1785 LIST_PREPEND(ExecStatusInfo, exec, i->exec, info);
1787 dbus_message_iter_next(&sub);
1798 static int print_property(const char *name, DBusMessageIter *iter) {
1802 /* This is a low-level property printer, see
1803 * print_status_info() for the nicer output */
1805 if (arg_property && !strv_find(arg_property, name))
1808 switch (dbus_message_iter_get_arg_type(iter)) {
1810 case DBUS_TYPE_STRING: {
1812 dbus_message_iter_get_basic(iter, &s);
1814 if (arg_all || s[0])
1815 printf("%s=%s\n", name, s);
1820 case DBUS_TYPE_BOOLEAN: {
1822 dbus_message_iter_get_basic(iter, &b);
1823 printf("%s=%s\n", name, yes_no(b));
1828 case DBUS_TYPE_UINT64: {
1830 dbus_message_iter_get_basic(iter, &u);
1832 /* Yes, heuristics! But we can change this check
1833 * should it turn out to not be sufficient */
1835 if (strstr(name, "Timestamp")) {
1836 char timestamp[FORMAT_TIMESTAMP_MAX], *t;
1838 if ((t = format_timestamp(timestamp, sizeof(timestamp), u)) || arg_all)
1839 printf("%s=%s\n", name, strempty(t));
1840 } else if (strstr(name, "USec")) {
1841 char timespan[FORMAT_TIMESPAN_MAX];
1843 printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u));
1845 printf("%s=%llu\n", name, (unsigned long long) u);
1850 case DBUS_TYPE_UINT32: {
1852 dbus_message_iter_get_basic(iter, &u);
1854 if (strstr(name, "UMask") || strstr(name, "Mode"))
1855 printf("%s=%04o\n", name, u);
1857 printf("%s=%u\n", name, (unsigned) u);
1862 case DBUS_TYPE_INT32: {
1864 dbus_message_iter_get_basic(iter, &i);
1866 printf("%s=%i\n", name, (int) i);
1870 case DBUS_TYPE_STRUCT: {
1871 DBusMessageIter sub;
1872 dbus_message_iter_recurse(iter, &sub);
1874 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
1877 dbus_message_iter_get_basic(&sub, &u);
1880 printf("%s=%u\n", name, (unsigned) u);
1882 printf("%s=\n", name);
1885 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
1888 dbus_message_iter_get_basic(&sub, &s);
1890 if (arg_all || s[0])
1891 printf("%s=%s\n", name, s);
1899 case DBUS_TYPE_ARRAY:
1901 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
1902 DBusMessageIter sub;
1905 dbus_message_iter_recurse(iter, &sub);
1907 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1908 printf("%s=", name);
1910 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1913 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
1914 dbus_message_iter_get_basic(&sub, &s);
1915 printf("%s%s", space ? " " : "", s);
1918 dbus_message_iter_next(&sub);
1926 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_BYTE) {
1927 DBusMessageIter sub;
1929 dbus_message_iter_recurse(iter, &sub);
1931 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1932 printf("%s=", name);
1934 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1937 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_BYTE);
1938 dbus_message_iter_get_basic(&sub, &u);
1941 dbus_message_iter_next(&sub);
1949 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
1950 DBusMessageIter sub, sub2;
1952 dbus_message_iter_recurse(iter, &sub);
1953 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1954 const char *type, *path;
1956 dbus_message_iter_recurse(&sub, &sub2);
1958 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
1959 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
1960 printf("%s=%s\n", type, path);
1962 dbus_message_iter_next(&sub);
1967 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
1968 DBusMessageIter sub, sub2;
1970 dbus_message_iter_recurse(iter, &sub);
1971 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1973 uint64_t value, next_elapse;
1975 dbus_message_iter_recurse(&sub, &sub2);
1977 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
1978 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
1979 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
1980 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
1982 printf("%s={ value=%s ; next_elapse=%s }\n",
1984 format_timespan(timespan1, sizeof(timespan1), value),
1985 format_timespan(timespan2, sizeof(timespan2), next_elapse));
1988 dbus_message_iter_next(&sub);
1993 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
1994 DBusMessageIter sub;
1996 dbus_message_iter_recurse(iter, &sub);
1997 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1998 ExecStatusInfo info;
2001 if (exec_status_info_deserialize(&sub, &info) >= 0) {
2002 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
2005 t = strv_join(info.argv, " ");
2007 printf("%s={ path=%s ; argv[]=%s ; ignore=%s ; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n",
2011 yes_no(info.ignore),
2012 strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)),
2013 strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)),
2014 (unsigned) info. pid,
2015 sigchld_code_to_string(info.code),
2017 info.code == CLD_EXITED ? "" : "/",
2018 strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status)));
2024 strv_free(info.argv);
2026 dbus_message_iter_next(&sub);
2036 printf("%s=[unprintable]\n", name);
2041 static int show_one(DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
2042 DBusMessage *m = NULL, *reply = NULL;
2043 const char *interface = "";
2046 DBusMessageIter iter, sub, sub2, sub3;
2047 UnitStatusInfo info;
2055 dbus_error_init(&error);
2057 if (!(m = dbus_message_new_method_call(
2058 "org.freedesktop.systemd1",
2060 "org.freedesktop.DBus.Properties",
2062 log_error("Could not allocate message.");
2067 if (!dbus_message_append_args(m,
2068 DBUS_TYPE_STRING, &interface,
2069 DBUS_TYPE_INVALID)) {
2070 log_error("Could not append arguments to message.");
2075 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2076 log_error("Failed to issue method call: %s", error.message);
2081 if (!dbus_message_iter_init(reply, &iter) ||
2082 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
2083 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
2084 log_error("Failed to parse reply.");
2089 dbus_message_iter_recurse(&iter, &sub);
2096 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2099 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
2100 log_error("Failed to parse reply.");
2105 dbus_message_iter_recurse(&sub, &sub2);
2107 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
2108 log_error("Failed to parse reply.");
2113 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
2114 log_error("Failed to parse reply.");
2119 dbus_message_iter_recurse(&sub2, &sub3);
2121 if (show_properties)
2122 r = print_property(name, &sub3);
2124 r = status_property(name, &sub3, &info);
2127 log_error("Failed to parse reply.");
2132 dbus_message_iter_next(&sub);
2135 if (!show_properties)
2136 print_status_info(&info);
2138 while ((p = info.exec)) {
2139 LIST_REMOVE(ExecStatusInfo, exec, info.exec, p);
2140 exec_status_info_free(p);
2147 dbus_message_unref(m);
2150 dbus_message_unref(reply);
2152 dbus_error_free(&error);
2157 static int show(DBusConnection *bus, char **args, unsigned n) {
2158 DBusMessage *m = NULL, *reply = NULL;
2162 bool show_properties, new_line = false;
2167 dbus_error_init(&error);
2169 show_properties = !streq(args[0], "status");
2171 if (show_properties && n <= 1) {
2172 /* If not argument is specified inspect the manager
2175 r = show_one(bus, "/org/freedesktop/systemd1", show_properties, &new_line);
2179 for (i = 1; i < n; i++) {
2180 const char *path = NULL;
2183 if (safe_atou32(args[i], &id) < 0) {
2185 /* Interpret as unit name */
2187 if (!(m = dbus_message_new_method_call(
2188 "org.freedesktop.systemd1",
2189 "/org/freedesktop/systemd1",
2190 "org.freedesktop.systemd1.Manager",
2192 log_error("Could not allocate message.");
2197 if (!dbus_message_append_args(m,
2198 DBUS_TYPE_STRING, &args[i],
2199 DBUS_TYPE_INVALID)) {
2200 log_error("Could not append arguments to message.");
2205 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2207 if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
2208 log_error("Failed to issue method call: %s", error.message);
2213 dbus_error_free(&error);
2215 dbus_message_unref(m);
2216 if (!(m = dbus_message_new_method_call(
2217 "org.freedesktop.systemd1",
2218 "/org/freedesktop/systemd1",
2219 "org.freedesktop.systemd1.Manager",
2221 log_error("Could not allocate message.");
2226 if (!dbus_message_append_args(m,
2227 DBUS_TYPE_STRING, &args[i],
2228 DBUS_TYPE_INVALID)) {
2229 log_error("Could not append arguments to message.");
2234 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2235 log_error("Failed to issue method call: %s", error.message);
2241 } else if (show_properties) {
2243 /* Interpret as job id */
2245 if (!(m = dbus_message_new_method_call(
2246 "org.freedesktop.systemd1",
2247 "/org/freedesktop/systemd1",
2248 "org.freedesktop.systemd1.Manager",
2250 log_error("Could not allocate message.");
2255 if (!dbus_message_append_args(m,
2256 DBUS_TYPE_UINT32, &id,
2257 DBUS_TYPE_INVALID)) {
2258 log_error("Could not append arguments to message.");
2263 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2264 log_error("Failed to issue method call: %s", error.message);
2270 /* Interpret as PID */
2272 if (!(m = dbus_message_new_method_call(
2273 "org.freedesktop.systemd1",
2274 "/org/freedesktop/systemd1",
2275 "org.freedesktop.systemd1.Manager",
2277 log_error("Could not allocate message.");
2282 if (!dbus_message_append_args(m,
2283 DBUS_TYPE_UINT32, &id,
2284 DBUS_TYPE_INVALID)) {
2285 log_error("Could not append arguments to message.");
2290 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2291 log_error("Failed to issue method call: %s", error.message);
2297 if (!dbus_message_get_args(reply, &error,
2298 DBUS_TYPE_OBJECT_PATH, &path,
2299 DBUS_TYPE_INVALID)) {
2300 log_error("Failed to parse reply: %s", error.message);
2305 if ((r = show_one(bus, path, show_properties, &new_line)) < 0)
2308 dbus_message_unref(m);
2309 dbus_message_unref(reply);
2317 dbus_message_unref(m);
2320 dbus_message_unref(reply);
2322 dbus_error_free(&error);
2327 static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
2329 DBusMessage *m = NULL, *reply = NULL;
2334 dbus_error_init(&error);
2336 log_debug("Got D-Bus request: %s.%s() on %s",
2337 dbus_message_get_interface(message),
2338 dbus_message_get_member(message),
2339 dbus_message_get_path(message));
2341 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
2342 log_error("Warning! D-Bus connection terminated.");
2343 dbus_connection_close(connection);
2345 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") ||
2346 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
2347 const char *id, *path;
2349 if (!dbus_message_get_args(message, &error,
2350 DBUS_TYPE_STRING, &id,
2351 DBUS_TYPE_OBJECT_PATH, &path,
2353 log_error("Failed to parse message: %s", error.message);
2354 else if (streq(dbus_message_get_member(message), "UnitNew"))
2355 printf("Unit %s added.\n", id);
2357 printf("Unit %s removed.\n", id);
2359 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew") ||
2360 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
2364 if (!dbus_message_get_args(message, &error,
2365 DBUS_TYPE_UINT32, &id,
2366 DBUS_TYPE_OBJECT_PATH, &path,
2368 log_error("Failed to parse message: %s", error.message);
2369 else if (streq(dbus_message_get_member(message), "JobNew"))
2370 printf("Job %u added.\n", id);
2372 printf("Job %u removed.\n", id);
2375 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Unit", "Changed") ||
2376 dbus_message_is_signal(message, "org.freedesktop.systemd1.Job", "Changed")) {
2378 const char *path, *interface, *property = "Id";
2379 DBusMessageIter iter, sub;
2381 path = dbus_message_get_path(message);
2382 interface = dbus_message_get_interface(message);
2384 if (!(m = dbus_message_new_method_call(
2385 "org.freedesktop.systemd1",
2387 "org.freedesktop.DBus.Properties",
2389 log_error("Could not allocate message.");
2393 if (!dbus_message_append_args(m,
2394 DBUS_TYPE_STRING, &interface,
2395 DBUS_TYPE_STRING, &property,
2396 DBUS_TYPE_INVALID)) {
2397 log_error("Could not append arguments to message.");
2401 if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
2402 log_error("Failed to issue method call: %s", error.message);
2406 if (!dbus_message_iter_init(reply, &iter) ||
2407 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2408 log_error("Failed to parse reply.");
2412 dbus_message_iter_recurse(&iter, &sub);
2414 if (streq(interface, "org.freedesktop.systemd1.Unit")) {
2417 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2418 log_error("Failed to parse reply.");
2422 dbus_message_iter_get_basic(&sub, &id);
2423 printf("Unit %s changed.\n", id);
2427 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32) {
2428 log_error("Failed to parse reply.");
2432 dbus_message_iter_get_basic(&sub, &id);
2433 printf("Job %u changed.\n", id);
2439 dbus_message_unref(m);
2442 dbus_message_unref(reply);
2444 dbus_error_free(&error);
2445 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2449 dbus_message_unref(m);
2452 dbus_message_unref(reply);
2454 dbus_error_free(&error);
2455 return DBUS_HANDLER_RESULT_NEED_MEMORY;
2458 static int monitor(DBusConnection *bus, char **args, unsigned n) {
2459 DBusMessage *m = NULL, *reply = NULL;
2463 dbus_error_init(&error);
2466 dbus_bus_add_match(bus,
2468 "sender='org.freedesktop.systemd1',"
2469 "interface='org.freedesktop.systemd1.Manager',"
2470 "path='/org/freedesktop/systemd1'",
2473 if (dbus_error_is_set(&error)) {
2474 log_error("Failed to add match: %s", error.message);
2479 dbus_bus_add_match(bus,
2481 "sender='org.freedesktop.systemd1',"
2482 "interface='org.freedesktop.systemd1.Unit',"
2486 if (dbus_error_is_set(&error)) {
2487 log_error("Failed to add match: %s", error.message);
2492 dbus_bus_add_match(bus,
2494 "sender='org.freedesktop.systemd1',"
2495 "interface='org.freedesktop.systemd1.Job',"
2499 if (dbus_error_is_set(&error)) {
2500 log_error("Failed to add match: %s", error.message);
2506 if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
2507 log_error("Failed to add filter.");
2512 if (!(m = dbus_message_new_method_call(
2513 "org.freedesktop.systemd1",
2514 "/org/freedesktop/systemd1",
2515 "org.freedesktop.systemd1.Manager",
2517 log_error("Could not allocate message.");
2522 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2523 log_error("Failed to issue method call: %s", error.message);
2528 while (dbus_connection_read_write_dispatch(bus, -1))
2535 /* This is slightly dirty, since we don't undo the filter or the matches. */
2538 dbus_message_unref(m);
2541 dbus_message_unref(reply);
2543 dbus_error_free(&error);
2548 static int dump(DBusConnection *bus, char **args, unsigned n) {
2549 DBusMessage *m = NULL, *reply = NULL;
2554 dbus_error_init(&error);
2556 if (!(m = dbus_message_new_method_call(
2557 "org.freedesktop.systemd1",
2558 "/org/freedesktop/systemd1",
2559 "org.freedesktop.systemd1.Manager",
2561 log_error("Could not allocate message.");
2565 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2566 log_error("Failed to issue method call: %s", error.message);
2571 if (!dbus_message_get_args(reply, &error,
2572 DBUS_TYPE_STRING, &text,
2573 DBUS_TYPE_INVALID)) {
2574 log_error("Failed to parse reply: %s", error.message);
2579 fputs(text, stdout);
2585 dbus_message_unref(m);
2588 dbus_message_unref(reply);
2590 dbus_error_free(&error);
2595 static int snapshot(DBusConnection *bus, char **args, unsigned n) {
2596 DBusMessage *m = NULL, *reply = NULL;
2599 const char *name = "", *path, *id;
2600 dbus_bool_t cleanup = FALSE;
2601 DBusMessageIter iter, sub;
2603 *interface = "org.freedesktop.systemd1.Unit",
2606 dbus_error_init(&error);
2608 if (!(m = dbus_message_new_method_call(
2609 "org.freedesktop.systemd1",
2610 "/org/freedesktop/systemd1",
2611 "org.freedesktop.systemd1.Manager",
2612 "CreateSnapshot"))) {
2613 log_error("Could not allocate message.");
2620 if (!dbus_message_append_args(m,
2621 DBUS_TYPE_STRING, &name,
2622 DBUS_TYPE_BOOLEAN, &cleanup,
2623 DBUS_TYPE_INVALID)) {
2624 log_error("Could not append arguments to message.");
2629 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2630 log_error("Failed to issue method call: %s", error.message);
2635 if (!dbus_message_get_args(reply, &error,
2636 DBUS_TYPE_OBJECT_PATH, &path,
2637 DBUS_TYPE_INVALID)) {
2638 log_error("Failed to parse reply: %s", error.message);
2643 dbus_message_unref(m);
2644 if (!(m = dbus_message_new_method_call(
2645 "org.freedesktop.systemd1",
2647 "org.freedesktop.DBus.Properties",
2649 log_error("Could not allocate message.");
2653 if (!dbus_message_append_args(m,
2654 DBUS_TYPE_STRING, &interface,
2655 DBUS_TYPE_STRING, &property,
2656 DBUS_TYPE_INVALID)) {
2657 log_error("Could not append arguments to message.");
2662 dbus_message_unref(reply);
2663 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2664 log_error("Failed to issue method call: %s", error.message);
2669 if (!dbus_message_iter_init(reply, &iter) ||
2670 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2671 log_error("Failed to parse reply.");
2676 dbus_message_iter_recurse(&iter, &sub);
2678 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2679 log_error("Failed to parse reply.");
2684 dbus_message_iter_get_basic(&sub, &id);
2692 dbus_message_unref(m);
2695 dbus_message_unref(reply);
2697 dbus_error_free(&error);
2702 static int delete_snapshot(DBusConnection *bus, char **args, unsigned n) {
2703 DBusMessage *m = NULL, *reply = NULL;
2711 dbus_error_init(&error);
2713 for (i = 1; i < n; i++) {
2714 const char *path = NULL;
2716 if (!(m = dbus_message_new_method_call(
2717 "org.freedesktop.systemd1",
2718 "/org/freedesktop/systemd1",
2719 "org.freedesktop.systemd1.Manager",
2721 log_error("Could not allocate message.");
2726 if (!dbus_message_append_args(m,
2727 DBUS_TYPE_STRING, &args[i],
2728 DBUS_TYPE_INVALID)) {
2729 log_error("Could not append arguments to message.");
2734 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2735 log_error("Failed to issue method call: %s", error.message);
2740 if (!dbus_message_get_args(reply, &error,
2741 DBUS_TYPE_OBJECT_PATH, &path,
2742 DBUS_TYPE_INVALID)) {
2743 log_error("Failed to parse reply: %s", error.message);
2748 dbus_message_unref(m);
2749 if (!(m = dbus_message_new_method_call(
2750 "org.freedesktop.systemd1",
2752 "org.freedesktop.systemd1.Snapshot",
2754 log_error("Could not allocate message.");
2759 dbus_message_unref(reply);
2760 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2761 log_error("Failed to issue method call: %s", error.message);
2766 dbus_message_unref(m);
2767 dbus_message_unref(reply);
2775 dbus_message_unref(m);
2778 dbus_message_unref(reply);
2780 dbus_error_free(&error);
2785 static int daemon_reload(DBusConnection *bus, char **args, unsigned n) {
2786 DBusMessage *m = NULL, *reply = NULL;
2791 dbus_error_init(&error);
2793 if (arg_action == ACTION_RELOAD)
2795 else if (arg_action == ACTION_REEXEC)
2796 method = "Reexecute";
2798 assert(arg_action == ACTION_SYSTEMCTL);
2801 streq(args[0], "clear-jobs") ||
2802 streq(args[0], "cancel") ? "ClearJobs" :
2803 streq(args[0], "daemon-reexec") ? "Reexecute" :
2804 streq(args[0], "reset-maintenance") ? "ResetMaintenance" :
2805 streq(args[0], "daemon-exit") ? "Exit" :
2809 if (!(m = dbus_message_new_method_call(
2810 "org.freedesktop.systemd1",
2811 "/org/freedesktop/systemd1",
2812 "org.freedesktop.systemd1.Manager",
2814 log_error("Could not allocate message.");
2818 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2820 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
2821 /* There's always a fallback possible for
2822 * legacy actions. */
2827 log_error("Failed to issue method call: %s", error.message);
2836 dbus_message_unref(m);
2839 dbus_message_unref(reply);
2841 dbus_error_free(&error);
2846 static int reset_maintenance(DBusConnection *bus, char **args, unsigned n) {
2847 DBusMessage *m = NULL, *reply = NULL;
2853 dbus_error_init(&error);
2856 return daemon_reload(bus, args, n);
2858 for (i = 1; i < n; i++) {
2860 if (!(m = dbus_message_new_method_call(
2861 "org.freedesktop.systemd1",
2862 "/org/freedesktop/systemd1",
2863 "org.freedesktop.systemd1.Manager",
2864 "ResetMaintenanceUnit"))) {
2865 log_error("Could not allocate message.");
2870 if (!dbus_message_append_args(m,
2871 DBUS_TYPE_STRING, args + i,
2872 DBUS_TYPE_INVALID)) {
2873 log_error("Could not append arguments to message.");
2878 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2879 log_error("Failed to issue method call: %s", error.message);
2884 dbus_message_unref(m);
2885 dbus_message_unref(reply);
2893 dbus_message_unref(m);
2896 dbus_message_unref(reply);
2898 dbus_error_free(&error);
2903 static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
2904 DBusMessage *m = NULL, *reply = NULL;
2906 DBusMessageIter iter, sub, sub2;
2909 *interface = "org.freedesktop.systemd1.Manager",
2910 *property = "Environment";
2912 dbus_error_init(&error);
2914 if (!(m = dbus_message_new_method_call(
2915 "org.freedesktop.systemd1",
2916 "/org/freedesktop/systemd1",
2917 "org.freedesktop.DBus.Properties",
2919 log_error("Could not allocate message.");
2923 if (!dbus_message_append_args(m,
2924 DBUS_TYPE_STRING, &interface,
2925 DBUS_TYPE_STRING, &property,
2926 DBUS_TYPE_INVALID)) {
2927 log_error("Could not append arguments to message.");
2932 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2933 log_error("Failed to issue method call: %s", error.message);
2938 if (!dbus_message_iter_init(reply, &iter) ||
2939 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2940 log_error("Failed to parse reply.");
2945 dbus_message_iter_recurse(&iter, &sub);
2947 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
2948 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
2949 log_error("Failed to parse reply.");
2954 dbus_message_iter_recurse(&sub, &sub2);
2956 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
2959 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
2960 log_error("Failed to parse reply.");
2965 dbus_message_iter_get_basic(&sub2, &text);
2966 printf("%s\n", text);
2968 dbus_message_iter_next(&sub2);
2975 dbus_message_unref(m);
2978 dbus_message_unref(reply);
2980 dbus_error_free(&error);
2985 static int set_environment(DBusConnection *bus, char **args, unsigned n) {
2986 DBusMessage *m = NULL, *reply = NULL;
2990 DBusMessageIter iter, sub;
2993 dbus_error_init(&error);
2995 method = streq(args[0], "set-environment")
2997 : "UnsetEnvironment";
2999 if (!(m = dbus_message_new_method_call(
3000 "org.freedesktop.systemd1",
3001 "/org/freedesktop/systemd1",
3002 "org.freedesktop.systemd1.Manager",
3005 log_error("Could not allocate message.");
3009 dbus_message_iter_init_append(m, &iter);
3011 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
3012 log_error("Could not append arguments to message.");
3017 for (i = 1; i < n; i++)
3018 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) {
3019 log_error("Could not append arguments to message.");
3024 if (!dbus_message_iter_close_container(&iter, &sub)) {
3025 log_error("Could not append arguments to message.");
3030 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3031 log_error("Failed to issue method call: %s", error.message);
3040 dbus_message_unref(m);
3043 dbus_message_unref(reply);
3045 dbus_error_free(&error);
3058 static Hashmap *will_install = NULL, *have_installed = NULL;
3059 static Set *remove_symlinks_to = NULL;
3061 static void install_info_free(InstallInfo *i) {
3066 strv_free(i->aliases);
3067 strv_free(i->wanted_by);
3071 static void install_info_hashmap_free(Hashmap *m) {
3074 while ((i = hashmap_steal_first(m)))
3075 install_info_free(i);
3080 static bool unit_name_valid(const char *name) {
3082 /* This is a minimal version of unit_name_valid() from
3088 if (ignore_file(name))
3094 static int install_info_add(const char *name) {
3098 assert(will_install);
3100 if (!unit_name_valid(name))
3103 if (hashmap_get(have_installed, name) ||
3104 hashmap_get(will_install, name))
3107 if (!(i = new0(InstallInfo, 1))) {
3112 if (!(i->name = strdup(name))) {
3117 if ((r = hashmap_put(will_install, i->name, i)) < 0)
3124 install_info_free(i);
3129 static int config_parse_also(
3130 const char *filename,
3132 const char *section,
3146 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
3150 if (!(n = strndup(w, l)))
3153 r = install_info_add(n);
3163 static int mark_symlink_for_removal(const char *p) {
3168 assert(path_is_absolute(p));
3170 if (!remove_symlinks_to)
3173 if (!(n = strdup(p)))
3176 path_kill_slashes(n);
3178 if ((r = set_put(remove_symlinks_to, n)) < 0) {
3180 return r == -EEXIST ? 0 : r;
3186 static int remove_marked_symlinks_fd(int fd, const char *config_path, const char *root, bool *deleted) {
3195 if (!(d = fdopendir(fd))) {
3196 close_nointr_nofail(fd);
3202 while ((de = readdir(d))) {
3203 bool is_dir = false, is_link = false;
3205 if (ignore_file(de->d_name))
3208 if (de->d_type == DT_LNK)
3210 else if (de->d_type == DT_DIR)
3212 else if (de->d_type == DT_UNKNOWN) {
3215 if (fstatat(fd, de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) {
3216 log_error("Failed to stat %s/%s: %m", root, de->d_name);
3223 is_link = S_ISLNK(st.st_mode);
3224 is_dir = S_ISDIR(st.st_mode);
3232 if ((nfd = openat(fd, de->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW)) < 0) {
3233 log_error("Failed to open %s/%s: %m", root, de->d_name);
3240 if (asprintf(&p, "%s/%s", root, de->d_name) < 0) {
3241 log_error("Failed to allocate directory string.");
3242 close_nointr_nofail(nfd);
3247 /* This will close nfd, regardless whether it succeeds or not */
3248 q = remove_marked_symlinks_fd(nfd, config_path, p, deleted);
3254 } else if (is_link) {
3258 if (asprintf(&p, "%s/%s", root, de->d_name) < 0) {
3259 log_error("Failed to allocate symlink string.");
3264 if ((q = readlink_and_make_absolute(p, &dest)) < 0) {
3265 log_error("Cannot read symlink %s: %s", p, strerror(-q));
3273 if ((c = canonicalize_file_name(dest))) {
3274 /* This might fail if the destination
3275 * is already removed */
3281 path_kill_slashes(dest);
3282 if (set_get(remove_symlinks_to, dest)) {
3285 log_info("rm '%s'", p);
3287 if (unlink(p) < 0) {
3288 log_error("Cannot unlink symlink %s: %m", p);
3293 rmdir_parents(p, config_path);
3294 path_kill_slashes(p);
3296 if (!set_get(remove_symlinks_to, p)) {
3298 if ((r = mark_symlink_for_removal(p)) < 0) {
3317 static int remove_marked_symlinks(const char *config_path) {
3321 assert(config_path);
3323 if (set_size(remove_symlinks_to) <= 0)
3326 if ((fd = open(config_path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW)) < 0)
3333 if ((cfd = dup(fd)) < 0) {
3338 /* This takes possession of cfd and closes it */
3339 if ((q = remove_marked_symlinks_fd(cfd, config_path, config_path, &deleted)) < 0) {
3345 close_nointr_nofail(fd);
3350 static int create_symlink(const char *verb, const char *old_path, const char *new_path) {
3357 if (streq(verb, "enable")) {
3360 mkdir_parents(new_path, 0755);
3362 if (symlink(old_path, new_path) >= 0) {
3365 log_info("ln -s '%s' '%s'", old_path, new_path);
3370 if (errno != EEXIST) {
3371 log_error("Cannot link %s to %s: %m", old_path, new_path);
3375 if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
3377 if (errno == EINVAL) {
3378 log_error("Cannot link %s to %s, file exists already and is not a symlink.", old_path, new_path);
3382 log_error("readlink() failed: %s", strerror(-r));
3386 if (streq(dest, old_path)) {
3392 log_error("Cannot link %s to %s, symlink exists already and points to %s.", old_path, new_path, dest);
3401 log_info("ln -s '%s' '%s'", old_path, new_path);
3403 if (symlink(old_path, new_path) >= 0)
3406 log_error("Cannot link %s to %s: %m", old_path, new_path);
3409 } else if (streq(verb, "disable")) {
3412 if ((r = mark_symlink_for_removal(old_path)) < 0)
3415 if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
3416 if (errno == ENOENT)
3419 if (errno == EINVAL) {
3420 log_warning("File %s not a symlink, ignoring.", old_path);
3424 log_error("readlink() failed: %s", strerror(-r));
3428 if (!streq(dest, old_path)) {
3429 log_warning("File %s not a symlink to %s but points to %s, ignoring.", new_path, old_path, dest);
3436 if ((r = mark_symlink_for_removal(new_path)) < 0)
3440 log_info("rm '%s'", new_path);
3442 if (unlink(new_path) >= 0)
3445 log_error("Cannot unlink %s: %m", new_path);
3448 } else if (streq(verb, "is-enabled")) {
3451 if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
3453 if (errno == ENOENT || errno == EINVAL)
3456 log_error("readlink() failed: %s", strerror(-r));
3460 if (streq(dest, old_path)) {
3468 assert_not_reached("Unknown action.");
3471 static int install_info_symlink_alias(const char *verb, InstallInfo *i, const char *config_path) {
3473 char *alias_path = NULL;
3478 assert(config_path);
3480 STRV_FOREACH(s, i->aliases) {
3482 if (!unit_name_valid(*s)) {
3483 log_error("Invalid name %s.", *s);
3489 if (!(alias_path = path_make_absolute(*s, config_path))) {
3490 log_error("Out of memory");
3495 if ((r = create_symlink(verb, i->path, alias_path)) != 0)
3498 if (streq(verb, "disable"))
3499 rmdir_parents(alias_path, config_path);
3510 static int install_info_symlink_wants(const char *verb, InstallInfo *i, const char *config_path) {
3512 char *alias_path = NULL;
3517 assert(config_path);
3519 STRV_FOREACH(s, i->wanted_by) {
3520 if (!unit_name_valid(*s)) {
3521 log_error("Invalid name %s.", *s);
3529 if (asprintf(&alias_path, "%s/%s.wants/%s", config_path, *s, i->name) < 0) {
3530 log_error("Out of memory");
3535 if ((r = create_symlink(verb, i->path, alias_path)) != 0)
3538 if (streq(verb, "disable"))
3539 rmdir_parents(alias_path, config_path);
3550 static int install_info_apply(const char *verb, LookupPaths *paths, InstallInfo *i, const char *config_path) {
3552 const ConfigItem items[] = {
3553 { "Alias", config_parse_strv, &i->aliases, "Install" },
3554 { "WantedBy", config_parse_strv, &i->wanted_by, "Install" },
3555 { "Also", config_parse_also, NULL, "Install" },
3557 { NULL, NULL, NULL, NULL }
3561 char *filename = NULL;
3568 STRV_FOREACH(p, paths->unit_path) {
3571 if (!(filename = path_make_absolute(i->name, *p))) {
3572 log_error("Out of memory");
3576 /* Ensure that we don't follow symlinks */
3577 if ((fd = open(filename, O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NOCTTY)) >= 0)
3578 if ((f = fdopen(fd, "re")))
3581 if (errno == ELOOP) {
3582 log_error("Refusing to operate on symlinks, please pass unit names or absolute paths to unit files.");
3587 if (errno != ENOENT) {
3588 log_error("Failed to open %s: %m", filename);
3598 log_error("Couldn't find %s.", i->name);
3604 if ((r = config_parse(filename, f, NULL, items, true, i)) < 0) {
3611 if ((r = install_info_symlink_alias(verb, i, config_path)) != 0)
3614 if ((r = install_info_symlink_wants(verb, i, config_path)) != 0)
3617 if ((r = mark_symlink_for_removal(filename)) < 0)
3620 if ((r = remove_marked_symlinks(config_path)) < 0)
3626 static char *get_config_path(void) {
3628 if (arg_session && arg_global)
3629 return strdup(SESSION_CONFIG_UNIT_PATH);
3634 if (session_config_home(&p) < 0)
3640 return strdup(SYSTEM_CONFIG_UNIT_PATH);
3643 static int enable_unit(DBusConnection *bus, char **args, unsigned n) {
3647 char *config_path = NULL;
3650 const char *verb = args[0];
3652 dbus_error_init(&error);
3655 if ((r = lookup_paths_init(&paths, arg_session ? MANAGER_SESSION : MANAGER_SYSTEM)) < 0) {
3656 log_error("Failed to determine lookup paths: %s", strerror(-r));
3660 if (!(config_path = get_config_path())) {
3661 log_error("Failed to determine config path");
3666 will_install = hashmap_new(string_hash_func, string_compare_func);
3667 have_installed = hashmap_new(string_hash_func, string_compare_func);
3669 if (!will_install || !have_installed) {
3670 log_error("Failed to allocate unit sets.");
3675 if (!arg_defaults && streq(verb, "disable"))
3676 if (!(remove_symlinks_to = set_new(string_hash_func, string_compare_func))) {
3677 log_error("Failed to allocate symlink sets.");
3682 for (j = 1; j < n; j++)
3683 if ((r = install_info_add(args[j])) < 0)
3686 while ((i = hashmap_first(will_install))) {
3689 assert_se(hashmap_move_one(have_installed, will_install, i->name) == 0);
3691 if ((q = install_info_apply(verb, &paths, i, config_path)) != 0) {
3699 /* In test mode and found something */
3705 if (streq(verb, "is-enabled"))
3706 r = r > 0 ? 0 : -ENOENT;
3708 /* Don't try to reload anything if the user asked us to not do this */
3710 /* Don't try to reload anything when updating a unit globally */
3712 /* Don't try to reload anything if we are called for system changes but the system wasn't booted with systemd */
3713 (arg_session || sd_booted() > 0) &&
3714 /* Don't try to reload anything if we are running in a chroot environment */
3715 (arg_session || running_in_chroot() <= 0) ) {
3718 if ((q = daemon_reload(bus, args, n)) < 0)
3723 install_info_hashmap_free(will_install);
3724 install_info_hashmap_free(have_installed);
3726 set_free_free(remove_symlinks_to);
3728 lookup_paths_free(&paths);
3735 static int systemctl_help(void) {
3737 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
3738 "Send control commands to or query the systemd manager.\n\n"
3739 " -h --help Show this help\n"
3740 " -t --type=TYPE List only units of a particular type\n"
3741 " -p --property=NAME Show only properties by this name\n"
3742 " -a --all Show all units/properties, including dead/empty ones\n"
3743 " --full Don't ellipsize unit names on output\n"
3744 " --fail When queueing a new job, fail if conflicting jobs are\n"
3746 " -q --quiet Suppress output\n"
3747 " --no-block Do not wait until operation finished\n"
3748 " --system Connect to system bus\n"
3749 " --session Connect to session bus\n"
3750 " --order When generating graph for dot, show only order\n"
3751 " --require When generating graph for dot, show only requirement\n"
3752 " --no-wall Don't send wall message before halt/power-off/reboot\n"
3753 " --global Enable/disable unit files globally\n"
3754 " --no-reload When enabling/disabling unit files, don't reload daemon\n"
3756 " --force When enabling unit files, override existing symlinks\n"
3757 " --defaults When disabling unit files, remove default symlinks only\n\n"
3759 " list-units List units\n"
3760 " start [NAME...] Start (activate) one or more units\n"
3761 " stop [NAME...] Stop (deactivate) one or more units\n"
3762 " reload [NAME...] Reload one or more units\n"
3763 " restart [NAME...] Start or restart one or more units\n"
3764 " try-restart [NAME...] Restart one or more units if active\n"
3765 " reload-or-restart [NAME...] Reload one or more units is possible,\n"
3766 " otherwise start or restart\n"
3767 " reload-or-try-restart [NAME...] Reload one or more units is possible,\n"
3768 " otherwise restart if active\n"
3769 " isolate [NAME] Start one unit and stop all others\n"
3770 " is-active [NAME...] Check whether units are active\n"
3771 " status [NAME...|PID...] Show runtime status of one or more units\n"
3772 " show [NAME...|JOB...] Show properties of one or more\n"
3773 " units/jobs or the manager\n"
3774 " reset-maintenance [NAME...] Reset maintenance state for all, one,\n"
3776 " enable [NAME...] Enable one or more unit files\n"
3777 " disable [NAME...] Disable one or more unit files\n"
3778 " is-enabled [NAME...] Check whether unit files are enabled\n"
3779 " load [NAME...] Load one or more units\n"
3780 " list-jobs List jobs\n"
3781 " cancel [JOB...] Cancel all, one, or more jobs\n"
3782 " monitor Monitor unit/job changes\n"
3783 " dump Dump server status\n"
3784 " dot Dump dependency graph for dot(1)\n"
3785 " snapshot [NAME] Create a snapshot\n"
3786 " delete [NAME...] Remove one or more snapshots\n"
3787 " daemon-reload Reload systemd manager configuration\n"
3788 " daemon-reexec Reexecute systemd manager\n"
3789 " daemon-exit Ask the systemd manager to quit\n"
3790 " show-environment Dump environment\n"
3791 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
3792 " unset-environment [NAME...] Unset one or more environment variables\n"
3793 " halt Shut down and halt the system\n"
3794 " poweroff Shut down and power-off the system\n"
3795 " reboot Shut down and reboot the system\n"
3796 " rescue Enter system rescue mode\n"
3797 " emergency Enter system emergency mode\n"
3798 " default Enter system default mode\n",
3799 program_invocation_short_name);
3804 static int halt_help(void) {
3806 printf("%s [OPTIONS...]\n\n"
3807 "%s the system.\n\n"
3808 " --help Show this help\n"
3809 " --halt Halt the machine\n"
3810 " -p --poweroff Switch off the machine\n"
3811 " --reboot Reboot the machine\n"
3812 " -f --force Force immediate halt/power-off/reboot\n"
3813 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
3814 " -d --no-wtmp Don't write wtmp record\n"
3815 " -n --no-sync Don't sync before halt/power-off/reboot\n"
3816 " --no-wall Don't send wall message before halt/power-off/reboot\n",
3817 program_invocation_short_name,
3818 arg_action == ACTION_REBOOT ? "Reboot" :
3819 arg_action == ACTION_POWEROFF ? "Power off" :
3825 static int shutdown_help(void) {
3827 printf("%s [OPTIONS...] [now] [WALL...]\n\n"
3828 "Shut down the system.\n\n"
3829 " --help Show this help\n"
3830 " -H --halt Halt the machine\n"
3831 " -P --poweroff Power-off the machine\n"
3832 " -r --reboot Reboot the machine\n"
3833 " -h Equivalent to --poweroff, overriden by --halt\n"
3834 " -k Don't halt/power-off/reboot, just send warnings\n"
3835 " --no-wall Don't send wall message before halt/power-off/reboot\n",
3836 program_invocation_short_name);
3841 static int telinit_help(void) {
3843 printf("%s [OPTIONS...] {COMMAND}\n\n"
3844 "Send control commands to the init daemon.\n\n"
3845 " --help Show this help\n"
3846 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
3848 " 0 Power-off the machine\n"
3849 " 6 Reboot the machine\n"
3850 " 2, 3, 4, 5 Start runlevelX.target unit\n"
3851 " 1, s, S Enter rescue mode\n"
3852 " q, Q Reload init daemon configuration\n"
3853 " u, U Reexecute init daemon\n",
3854 program_invocation_short_name);
3859 static int runlevel_help(void) {
3861 printf("%s [OPTIONS...]\n\n"
3862 "Prints the previous and current runlevel of the init system.\n\n"
3863 " --help Show this help\n",
3864 program_invocation_short_name);
3869 static int systemctl_parse_argv(int argc, char *argv[]) {
3886 static const struct option options[] = {
3887 { "help", no_argument, NULL, 'h' },
3888 { "type", required_argument, NULL, 't' },
3889 { "property", required_argument, NULL, 'p' },
3890 { "all", no_argument, NULL, 'a' },
3891 { "full", no_argument, NULL, ARG_FULL },
3892 { "fail", no_argument, NULL, ARG_FAIL },
3893 { "session", no_argument, NULL, ARG_SESSION },
3894 { "system", no_argument, NULL, ARG_SYSTEM },
3895 { "global", no_argument, NULL, ARG_GLOBAL },
3896 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
3897 { "no-wall", no_argument, NULL, ARG_NO_WALL },
3898 { "quiet", no_argument, NULL, 'q' },
3899 { "order", no_argument, NULL, ARG_ORDER },
3900 { "require", no_argument, NULL, ARG_REQUIRE },
3901 { "force", no_argument, NULL, ARG_FORCE },
3902 { "no-reload", no_argument, NULL, ARG_NO_RELOAD },
3903 { "defaults", no_argument, NULL, ARG_DEFAULTS },
3904 { NULL, 0, NULL, 0 }
3912 while ((c = getopt_long(argc, argv, "ht:p:aq", options, NULL)) >= 0) {
3927 if (!(l = strv_append(arg_property, optarg)))
3930 strv_free(arg_property);
3933 /* If the user asked for a particular
3934 * property, show it to him, even if it is
3953 arg_session = false;
3957 arg_no_block = true;
3965 arg_dot = DOT_ORDER;
3969 arg_dot = DOT_REQUIRE;
3985 arg_no_reload = true;
3994 arg_defaults = true;
4001 log_error("Unknown option code %c", c);
4009 static int halt_parse_argv(int argc, char *argv[]) {
4018 static const struct option options[] = {
4019 { "help", no_argument, NULL, ARG_HELP },
4020 { "halt", no_argument, NULL, ARG_HALT },
4021 { "poweroff", no_argument, NULL, 'p' },
4022 { "reboot", no_argument, NULL, ARG_REBOOT },
4023 { "force", no_argument, NULL, 'f' },
4024 { "wtmp-only", no_argument, NULL, 'w' },
4025 { "no-wtmp", no_argument, NULL, 'd' },
4026 { "no-sync", no_argument, NULL, 'n' },
4027 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4028 { NULL, 0, NULL, 0 }
4036 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
4037 if (runlevel == '0' || runlevel == '6')
4038 arg_immediate = true;
4040 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
4048 arg_action = ACTION_HALT;
4052 if (arg_action != ACTION_REBOOT)
4053 arg_action = ACTION_POWEROFF;
4057 arg_action = ACTION_REBOOT;
4061 arg_immediate = true;
4082 /* Compatibility nops */
4089 log_error("Unknown option code %c", c);
4094 if (optind < argc) {
4095 log_error("Too many arguments.");
4102 static int shutdown_parse_argv(int argc, char *argv[]) {
4109 static const struct option options[] = {
4110 { "help", no_argument, NULL, ARG_HELP },
4111 { "halt", no_argument, NULL, 'H' },
4112 { "poweroff", no_argument, NULL, 'P' },
4113 { "reboot", no_argument, NULL, 'r' },
4114 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4115 { NULL, 0, NULL, 0 }
4123 while ((c = getopt_long(argc, argv, "HPrhkt:a", options, NULL)) >= 0) {
4131 arg_action = ACTION_HALT;
4135 arg_action = ACTION_POWEROFF;
4139 arg_action = ACTION_REBOOT;
4143 if (arg_action != ACTION_HALT)
4144 arg_action = ACTION_POWEROFF;
4157 /* Compatibility nops */
4164 log_error("Unknown option code %c", c);
4169 if (argc > optind && !streq(argv[optind], "now"))
4170 log_warning("First argument '%s' isn't 'now'. Ignoring.", argv[optind]);
4172 /* We ignore the time argument */
4173 if (argc > optind + 1)
4174 arg_wall = argv + optind + 1;
4181 static int telinit_parse_argv(int argc, char *argv[]) {
4188 static const struct option options[] = {
4189 { "help", no_argument, NULL, ARG_HELP },
4190 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4191 { NULL, 0, NULL, 0 }
4194 static const struct {
4198 { '0', ACTION_POWEROFF },
4199 { '6', ACTION_REBOOT },
4200 { '1', ACTION_RESCUE },
4201 { '2', ACTION_RUNLEVEL2 },
4202 { '3', ACTION_RUNLEVEL3 },
4203 { '4', ACTION_RUNLEVEL4 },
4204 { '5', ACTION_RUNLEVEL5 },
4205 { 's', ACTION_RESCUE },
4206 { 'S', ACTION_RESCUE },
4207 { 'q', ACTION_RELOAD },
4208 { 'Q', ACTION_RELOAD },
4209 { 'u', ACTION_REEXEC },
4210 { 'U', ACTION_REEXEC }
4219 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4234 log_error("Unknown option code %c", c);
4239 if (optind >= argc) {
4244 if (optind + 1 < argc) {
4245 log_error("Too many arguments.");
4249 if (strlen(argv[optind]) != 1) {
4250 log_error("Expected single character argument.");
4254 for (i = 0; i < ELEMENTSOF(table); i++)
4255 if (table[i].from == argv[optind][0])
4258 if (i >= ELEMENTSOF(table)) {
4259 log_error("Unknown command %s.", argv[optind]);
4263 arg_action = table[i].to;
4270 static int runlevel_parse_argv(int argc, char *argv[]) {
4276 static const struct option options[] = {
4277 { "help", no_argument, NULL, ARG_HELP },
4278 { NULL, 0, NULL, 0 }
4286 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4297 log_error("Unknown option code %c", c);
4302 if (optind < argc) {
4303 log_error("Too many arguments.");
4310 static int parse_argv(int argc, char *argv[]) {
4314 if (program_invocation_short_name) {
4316 if (strstr(program_invocation_short_name, "halt")) {
4317 arg_action = ACTION_HALT;
4318 return halt_parse_argv(argc, argv);
4319 } else if (strstr(program_invocation_short_name, "poweroff")) {
4320 arg_action = ACTION_POWEROFF;
4321 return halt_parse_argv(argc, argv);
4322 } else if (strstr(program_invocation_short_name, "reboot")) {
4323 arg_action = ACTION_REBOOT;
4324 return halt_parse_argv(argc, argv);
4325 } else if (strstr(program_invocation_short_name, "shutdown")) {
4326 arg_action = ACTION_POWEROFF;
4327 return shutdown_parse_argv(argc, argv);
4328 } else if (strstr(program_invocation_short_name, "init")) {
4330 if (sd_booted() > 0) {
4331 arg_action = ACTION_INVALID;
4332 return telinit_parse_argv(argc, argv);
4334 /* Hmm, so some other init system is
4335 * running, we need to forward this
4336 * request to it. For now we simply
4337 * guess that it is Upstart. */
4339 execv("/lib/upstart/telinit", argv);
4341 log_error("Couldn't find an alternative telinit implementation to spawn.");
4345 } else if (strstr(program_invocation_short_name, "runlevel")) {
4346 arg_action = ACTION_RUNLEVEL;
4347 return runlevel_parse_argv(argc, argv);
4351 arg_action = ACTION_SYSTEMCTL;
4352 return systemctl_parse_argv(argc, argv);
4355 static int action_to_runlevel(void) {
4357 static const char table[_ACTION_MAX] = {
4358 [ACTION_HALT] = '0',
4359 [ACTION_POWEROFF] = '0',
4360 [ACTION_REBOOT] = '6',
4361 [ACTION_RUNLEVEL2] = '2',
4362 [ACTION_RUNLEVEL3] = '3',
4363 [ACTION_RUNLEVEL4] = '4',
4364 [ACTION_RUNLEVEL5] = '5',
4365 [ACTION_RESCUE] = '1'
4368 assert(arg_action < _ACTION_MAX);
4370 return table[arg_action];
4373 static int talk_upstart(void) {
4374 DBusMessage *m = NULL, *reply = NULL;
4376 int previous, rl, r;
4378 env1_buf[] = "RUNLEVEL=X",
4379 env2_buf[] = "PREVLEVEL=X";
4380 char *env1 = env1_buf, *env2 = env2_buf;
4381 const char *emit = "runlevel";
4382 dbus_bool_t b_false = FALSE;
4383 DBusMessageIter iter, sub;
4384 DBusConnection *bus;
4386 dbus_error_init(&error);
4388 if (!(rl = action_to_runlevel()))
4391 if (utmp_get_runlevel(&previous, NULL) < 0)
4394 if (!(bus = dbus_connection_open_private("unix:abstract=/com/ubuntu/upstart", &error))) {
4395 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
4400 log_error("Failed to connect to Upstart bus: %s", error.message);
4405 if ((r = bus_check_peercred(bus)) < 0) {
4406 log_error("Failed to verify owner of bus.");
4410 if (!(m = dbus_message_new_method_call(
4411 "com.ubuntu.Upstart",
4412 "/com/ubuntu/Upstart",
4413 "com.ubuntu.Upstart0_6",
4416 log_error("Could not allocate message.");
4421 dbus_message_iter_init_append(m, &iter);
4423 env1_buf[sizeof(env1_buf)-2] = rl;
4424 env2_buf[sizeof(env2_buf)-2] = previous;
4426 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
4427 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
4428 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
4429 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
4430 !dbus_message_iter_close_container(&iter, &sub) ||
4431 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
4432 log_error("Could not append arguments to message.");
4437 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
4439 if (error_is_no_service(&error)) {
4444 log_error("Failed to issue method call: %s", error.message);
4453 dbus_message_unref(m);
4456 dbus_message_unref(reply);
4459 dbus_connection_close(bus);
4460 dbus_connection_unref(bus);
4463 dbus_error_free(&error);
4468 static int talk_initctl(void) {
4469 struct init_request request;
4473 if (!(rl = action_to_runlevel()))
4477 request.magic = INIT_MAGIC;
4478 request.sleeptime = 0;
4479 request.cmd = INIT_CMD_RUNLVL;
4480 request.runlevel = rl;
4482 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
4484 if (errno == ENOENT)
4487 log_error("Failed to open "INIT_FIFO": %m");
4492 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
4493 close_nointr_nofail(fd);
4496 log_error("Failed to write to "INIT_FIFO": %m");
4497 return errno ? -errno : -EIO;
4503 static int systemctl_main(DBusConnection *bus, int argc, char *argv[], DBusError *error) {
4505 static const struct {
4513 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
4515 { "list-units", LESS, 1, list_units },
4516 { "list-jobs", EQUAL, 1, list_jobs },
4517 { "clear-jobs", EQUAL, 1, daemon_reload },
4518 { "load", MORE, 2, load_unit },
4519 { "cancel", MORE, 2, cancel_job },
4520 { "start", MORE, 2, start_unit },
4521 { "stop", MORE, 2, start_unit },
4522 { "reload", MORE, 2, start_unit },
4523 { "restart", MORE, 2, start_unit },
4524 { "try-restart", MORE, 2, start_unit },
4525 { "reload-or-restart", MORE, 2, start_unit },
4526 { "reload-or-try-restart", MORE, 2, start_unit },
4527 { "force-reload", MORE, 2, start_unit }, /* For compatibility with SysV */
4528 { "condrestart", MORE, 2, start_unit }, /* For compatibility with RH */
4529 { "isolate", EQUAL, 2, start_unit },
4530 { "is-active", MORE, 2, check_unit },
4531 { "check", MORE, 2, check_unit },
4532 { "show", MORE, 1, show },
4533 { "status", MORE, 2, show },
4534 { "monitor", EQUAL, 1, monitor },
4535 { "dump", EQUAL, 1, dump },
4536 { "dot", EQUAL, 1, dot },
4537 { "snapshot", LESS, 2, snapshot },
4538 { "delete", MORE, 2, delete_snapshot },
4539 { "daemon-reload", EQUAL, 1, daemon_reload },
4540 { "daemon-reexec", EQUAL, 1, daemon_reload },
4541 { "daemon-exit", EQUAL, 1, daemon_reload },
4542 { "show-environment", EQUAL, 1, show_enviroment },
4543 { "set-environment", MORE, 2, set_environment },
4544 { "unset-environment", MORE, 2, set_environment },
4545 { "halt", EQUAL, 1, start_special },
4546 { "poweroff", EQUAL, 1, start_special },
4547 { "reboot", EQUAL, 1, start_special },
4548 { "default", EQUAL, 1, start_special },
4549 { "rescue", EQUAL, 1, start_special },
4550 { "emergency", EQUAL, 1, start_special },
4551 { "reset-maintenance", MORE, 1, reset_maintenance },
4552 { "enable", MORE, 2, enable_unit },
4553 { "disable", MORE, 2, enable_unit },
4554 { "is-enabled", MORE, 2, enable_unit }
4564 left = argc - optind;
4567 /* Special rule: no arguments means "list-units" */
4570 if (streq(argv[optind], "help")) {
4575 for (i = 0; i < ELEMENTSOF(verbs); i++)
4576 if (streq(argv[optind], verbs[i].verb))
4579 if (i >= ELEMENTSOF(verbs)) {
4580 log_error("Unknown operation %s", argv[optind]);
4585 switch (verbs[i].argc_cmp) {
4588 if (left != verbs[i].argc) {
4589 log_error("Invalid number of arguments.");
4596 if (left < verbs[i].argc) {
4597 log_error("Too few arguments.");
4604 if (left > verbs[i].argc) {
4605 log_error("Too many arguments.");
4612 assert_not_reached("Unknown comparison operator.");
4615 /* Require a bus connection for all operations but
4617 if (!streq(verbs[i].verb, "enable") &&
4618 !streq(verbs[i].verb, "disable") &&
4620 log_error("Failed to get D-Bus connection: %s", error->message);
4624 return verbs[i].dispatch(bus, argv + optind, left);
4627 static int reload_with_fallback(DBusConnection *bus) {
4630 /* First, try systemd via D-Bus. */
4631 if (daemon_reload(bus, NULL, 0) > 0)
4635 /* Nothing else worked, so let's try signals */
4636 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
4638 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
4639 log_error("kill() failed: %m");
4646 static int start_with_fallback(DBusConnection *bus) {
4649 /* First, try systemd via D-Bus. */
4650 if (start_unit(bus, NULL, 0) > 0)
4654 /* Hmm, talking to systemd via D-Bus didn't work. Then
4655 * let's try to talk to Upstart via D-Bus. */
4656 if (talk_upstart() > 0)
4659 /* Nothing else worked, so let's try
4661 if (talk_initctl() > 0)
4664 log_error("Failed to talk to init daemon.");
4668 warn_wall(arg_action);
4672 static int halt_main(DBusConnection *bus) {
4675 if (geteuid() != 0) {
4676 log_error("Must to be root.");
4680 if (!arg_dry && !arg_immediate)
4681 return start_with_fallback(bus);
4684 if (sd_booted() > 0)
4685 log_debug("Not writing utmp record, assuming that systemd-update-utmp is used.");
4686 else if ((r = utmp_put_shutdown(0)) < 0)
4687 log_warning("Failed to write utmp record: %s", strerror(-r));
4696 /* Make sure C-A-D is handled by the kernel from this
4698 reboot(RB_ENABLE_CAD);
4700 switch (arg_action) {
4703 log_info("Halting.");
4704 reboot(RB_HALT_SYSTEM);
4707 case ACTION_POWEROFF:
4708 log_info("Powering off.");
4709 reboot(RB_POWER_OFF);
4713 log_info("Rebooting.");
4714 reboot(RB_AUTOBOOT);
4718 assert_not_reached("Unknown halt action.");
4721 /* We should never reach this. */
4725 static int runlevel_main(void) {
4726 int r, runlevel, previous;
4728 if ((r = utmp_get_runlevel(&runlevel, &previous)) < 0) {
4729 printf("unknown\n");
4734 previous <= 0 ? 'N' : previous,
4735 runlevel <= 0 ? 'N' : runlevel);
4740 int main(int argc, char*argv[]) {
4742 DBusConnection *bus = NULL;
4745 dbus_error_init(&error);
4747 log_parse_environment();
4749 if ((r = parse_argv(argc, argv)) < 0)
4756 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
4757 * let's shortcut this */
4758 if (arg_action == ACTION_RUNLEVEL) {
4759 retval = runlevel_main() < 0;
4763 bus_connect(arg_session ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &bus, &private_bus, &error);
4765 switch (arg_action) {
4767 case ACTION_SYSTEMCTL: {
4768 retval = systemctl_main(bus, argc, argv, &error) < 0;
4773 case ACTION_POWEROFF:
4775 retval = halt_main(bus) < 0;
4778 case ACTION_RUNLEVEL2:
4779 case ACTION_RUNLEVEL3:
4780 case ACTION_RUNLEVEL4:
4781 case ACTION_RUNLEVEL5:
4783 case ACTION_EMERGENCY:
4784 case ACTION_DEFAULT:
4785 retval = start_with_fallback(bus) < 0;
4790 retval = reload_with_fallback(bus) < 0;
4793 case ACTION_INVALID:
4794 case ACTION_RUNLEVEL:
4796 assert_not_reached("Unknown action");
4802 dbus_connection_close(bus);
4803 dbus_connection_unref(bus);
4806 dbus_error_free(&error);
4810 strv_free(arg_property);