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 <sys/prctl.h>
37 #include <dbus/dbus.h>
43 #include "utmp-wtmp.h"
47 #include "dbus-common.h"
48 #include "cgroup-show.h"
49 #include "cgroup-util.h"
51 #include "path-lookup.h"
52 #include "conf-parser.h"
53 #include "sd-daemon.h"
54 #include "shutdownd.h"
55 #include "exit-status.h"
56 #include "bus-errors.h"
58 #include "unit-name.h"
60 static const char *arg_type = NULL;
61 static char **arg_property = NULL;
62 static bool arg_all = false;
63 static bool arg_fail = false;
64 static bool arg_user = false;
65 static bool arg_global = false;
66 static bool arg_immediate = false;
67 static bool arg_no_block = false;
68 static bool arg_no_pager = false;
69 static bool arg_no_wtmp = false;
70 static bool arg_no_sync = false;
71 static bool arg_no_wall = false;
72 static bool arg_no_reload = false;
73 static bool arg_dry = false;
74 static bool arg_quiet = false;
75 static bool arg_full = false;
76 static bool arg_force = false;
77 static bool arg_defaults = false;
78 static bool arg_ask_password = false;
79 static char **arg_wall = NULL;
80 static const char *arg_kill_who = NULL;
81 static const char *arg_kill_mode = NULL;
82 static int arg_signal = SIGTERM;
83 static usec_t arg_when = 0;
102 ACTION_CANCEL_SHUTDOWN,
104 } arg_action = ACTION_SYSTEMCTL;
111 static bool private_bus = false;
113 static pid_t pager_pid = 0;
115 static int daemon_reload(DBusConnection *bus, char **args, unsigned n);
116 static void pager_open(void);
118 static bool on_tty(void) {
121 /* Note that this is invoked relatively early, before we start
122 * the pager. That means the value we return reflects whether
123 * we originally were started on a tty, not if we currently
124 * are. But this is intended, since we want color, and so on
125 * when run in our own pager. */
127 if (_unlikely_(t < 0))
128 t = isatty(STDOUT_FILENO) > 0;
133 static void spawn_ask_password_agent(void) {
136 /* We check STDIN here, not STDOUT, since this is about input,
138 if (!isatty(STDIN_FILENO))
141 if (!arg_ask_password)
146 /* Spawns a temporary TTY agent, making sure it goes away when
149 if ((child = fork()) < 0)
155 const char * const args[] = {
156 SYSTEMD_TTY_ASK_PASSWORD_AGENT_BINARY_PATH,
161 if (prctl(PR_SET_PDEATHSIG, SIGTERM) < 0)
164 /* Check whether our parent died before we were able
165 * to set the death signal */
166 if (getppid() != parent)
169 execv(args[0], (char **) args);
174 static const char *ansi_highlight(bool b) {
179 return b ? ANSI_HIGHLIGHT_ON : ANSI_HIGHLIGHT_OFF;
182 static const char *ansi_highlight_green(bool b) {
187 return b ? ANSI_HIGHLIGHT_GREEN_ON : ANSI_HIGHLIGHT_OFF;
190 static bool error_is_no_service(const DBusError *error) {
193 if (!dbus_error_is_set(error))
196 if (dbus_error_has_name(error, DBUS_ERROR_NAME_HAS_NO_OWNER))
199 if (dbus_error_has_name(error, DBUS_ERROR_SERVICE_UNKNOWN))
202 return startswith(error->name, "org.freedesktop.DBus.Error.Spawn.");
205 static int translate_bus_error_to_exit_status(int r, const DBusError *error) {
208 if (!dbus_error_is_set(error))
211 if (dbus_error_has_name(error, DBUS_ERROR_ACCESS_DENIED) ||
212 dbus_error_has_name(error, BUS_ERROR_ONLY_BY_DEPENDENCY) ||
213 dbus_error_has_name(error, BUS_ERROR_NO_ISOLATION) ||
214 dbus_error_has_name(error, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE))
215 return EXIT_NOPERMISSION;
217 if (dbus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT))
218 return EXIT_NOTINSTALLED;
220 if (dbus_error_has_name(error, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE) ||
221 dbus_error_has_name(error, BUS_ERROR_NOT_SUPPORTED))
222 return EXIT_NOTIMPLEMENTED;
224 if (dbus_error_has_name(error, BUS_ERROR_LOAD_FAILED))
225 return EXIT_NOTCONFIGURED;
233 static int bus_iter_get_basic_and_next(DBusMessageIter *iter, int type, void *data, bool next) {
238 if (dbus_message_iter_get_arg_type(iter) != type)
241 dbus_message_iter_get_basic(iter, data);
243 if (!dbus_message_iter_next(iter) != !next)
249 static void warn_wall(enum action action) {
250 static const char *table[_ACTION_MAX] = {
251 [ACTION_HALT] = "The system is going down for system halt NOW!",
252 [ACTION_REBOOT] = "The system is going down for reboot NOW!",
253 [ACTION_POWEROFF] = "The system is going down for power-off NOW!",
254 [ACTION_KEXEC] = "The system is going down for kexec reboot NOW!",
255 [ACTION_RESCUE] = "The system is going down to rescue mode NOW!",
256 [ACTION_EMERGENCY] = "The system is going down to emergency mode NOW!"
265 if (!(p = strv_join(arg_wall, " "))) {
266 log_error("Failed to join strings.");
282 utmp_wall(table[action], NULL);
287 const char *description;
288 const char *load_state;
289 const char *active_state;
290 const char *sub_state;
291 const char *following;
292 const char *unit_path;
294 const char *job_type;
295 const char *job_path;
298 static int compare_unit_info(const void *a, const void *b) {
300 const struct unit_info *u = a, *v = b;
302 d1 = strrchr(u->id, '.');
303 d2 = strrchr(v->id, '.');
308 if ((r = strcasecmp(d1, d2)) != 0)
312 return strcasecmp(u->id, v->id);
315 static bool output_show_job(const struct unit_info *u) {
318 return (!arg_type || ((dot = strrchr(u->id, '.')) &&
319 streq(dot+1, arg_type))) &&
320 (arg_all || !(streq(u->active_state, "inactive") || u->following[0]) || u->job_id > 0);
323 static void output_units_list(const struct unit_info *unit_infos, unsigned c) {
324 unsigned active_len, sub_len, job_len, n_shown = 0;
325 const struct unit_info *u;
327 active_len = sizeof("ACTIVE")-1;
328 sub_len = sizeof("SUB")-1;
329 job_len = sizeof("JOB")-1;
331 for (u = unit_infos; u < unit_infos + c; u++) {
332 if (!output_show_job(u))
335 active_len = MAX(active_len, strlen(u->active_state));
336 sub_len = MAX(sub_len, strlen(u->sub_state));
338 job_len = MAX(job_len, strlen(u->job_type));
342 printf("%-25s %-6s %-*s %-*s %-*s", "UNIT", "LOAD",
343 active_len, "ACTIVE", sub_len, "SUB", job_len, "JOB");
344 if (columns() >= 80+12 || arg_full)
345 printf(" %s\n", "DESCRIPTION");
350 for (u = unit_infos; u < unit_infos + c; u++) {
353 const char *on_loaded, *off_loaded;
354 const char *on_active, *off_active;
356 if (!output_show_job(u))
361 if (!streq(u->load_state, "loaded") &&
362 !streq(u->load_state, "banned")) {
363 on_loaded = ansi_highlight(true);
364 off_loaded = ansi_highlight(false);
366 on_loaded = off_loaded = "";
368 if (streq(u->active_state, "failed")) {
369 on_active = ansi_highlight(true);
370 off_active = ansi_highlight(false);
372 on_active = off_active = "";
374 e = arg_full ? NULL : ellipsize(u->id, 25, 33);
376 printf("%-25s %s%-6s%s %s%-*s %-*s%s%n",
378 on_loaded, u->load_state, off_loaded,
379 on_active, active_len, u->active_state,
380 sub_len, u->sub_state, off_active,
385 a -= strlen(on_loaded) + strlen(off_loaded);
386 a -= strlen(on_active) + strlen(off_active);
389 printf(" %-*s", job_len, u->job_type);
393 if (a + b + 1 < columns()) {
395 printf(" %-*s", job_len, "");
398 printf(" %s", u->description);
400 printf(" %.*s", columns() - a - b - 1, u->description);
407 printf("\nLOAD = Reflects whether the unit definition was properly loaded.\n"
408 "ACTIVE = The high-level unit activation state, i.e. generalization of SUB.\n"
409 "SUB = The low-level unit activation state, values depend on unit type.\n"
410 "JOB = Pending job for the unit.\n");
413 printf("\n%u units listed.\n", n_shown);
415 printf("\n%u units listed. Pass --all to see inactive units, too.\n", n_shown);
419 static int list_units(DBusConnection *bus, char **args, unsigned n) {
420 DBusMessage *m = NULL, *reply = NULL;
423 DBusMessageIter iter, sub, sub2;
424 unsigned c = 0, n_units = 0;
425 struct unit_info *unit_infos = NULL;
427 dbus_error_init(&error);
433 if (!(m = dbus_message_new_method_call(
434 "org.freedesktop.systemd1",
435 "/org/freedesktop/systemd1",
436 "org.freedesktop.systemd1.Manager",
438 log_error("Could not allocate message.");
442 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
443 log_error("Failed to issue method call: %s", bus_error_message(&error));
448 if (!dbus_message_iter_init(reply, &iter) ||
449 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
450 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
451 log_error("Failed to parse reply.");
456 dbus_message_iter_recurse(&iter, &sub);
458 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
461 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
462 log_error("Failed to parse reply.");
470 n_units = MAX(2*c, 16);
471 w = realloc(unit_infos, sizeof(struct unit_info) * n_units);
474 log_error("Failed to allocate unit array.");
484 dbus_message_iter_recurse(&sub, &sub2);
486 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->id, true) < 0 ||
487 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->description, true) < 0 ||
488 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->load_state, true) < 0 ||
489 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->active_state, true) < 0 ||
490 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->sub_state, true) < 0 ||
491 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->following, true) < 0 ||
492 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &u->unit_path, true) < 0 ||
493 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &u->job_id, true) < 0 ||
494 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->job_type, true) < 0 ||
495 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &u->job_path, false) < 0) {
496 log_error("Failed to parse reply.");
501 dbus_message_iter_next(&sub);
505 qsort(unit_infos, c, sizeof(struct unit_info), compare_unit_info);
506 output_units_list(unit_infos, c);
512 dbus_message_unref(m);
515 dbus_message_unref(reply);
519 dbus_error_free(&error);
524 static int dot_one_property(const char *name, const char *prop, DBusMessageIter *iter) {
525 static const char * const colors[] = {
526 "Requires", "[color=\"black\"]",
527 "RequiresOverridable", "[color=\"black\"]",
528 "Requisite", "[color=\"darkblue\"]",
529 "RequisiteOverridable", "[color=\"darkblue\"]",
530 "Wants", "[color=\"darkgrey\"]",
531 "Conflicts", "[color=\"red\"]",
532 "ConflictedBy", "[color=\"red\"]",
533 "After", "[color=\"green\"]"
536 const char *c = NULL;
543 for (i = 0; i < ELEMENTSOF(colors); i += 2)
544 if (streq(colors[i], prop)) {
552 if (arg_dot != DOT_ALL)
553 if ((arg_dot == DOT_ORDER) != streq(prop, "After"))
556 switch (dbus_message_iter_get_arg_type(iter)) {
558 case DBUS_TYPE_ARRAY:
560 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
563 dbus_message_iter_recurse(iter, &sub);
565 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
568 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
569 dbus_message_iter_get_basic(&sub, &s);
570 printf("\t\"%s\"->\"%s\" %s;\n", name, s, c);
572 dbus_message_iter_next(&sub);
582 static int dot_one(DBusConnection *bus, const char *name, const char *path) {
583 DBusMessage *m = NULL, *reply = NULL;
584 const char *interface = "org.freedesktop.systemd1.Unit";
587 DBusMessageIter iter, sub, sub2, sub3;
592 dbus_error_init(&error);
594 if (!(m = dbus_message_new_method_call(
595 "org.freedesktop.systemd1",
597 "org.freedesktop.DBus.Properties",
599 log_error("Could not allocate message.");
604 if (!dbus_message_append_args(m,
605 DBUS_TYPE_STRING, &interface,
606 DBUS_TYPE_INVALID)) {
607 log_error("Could not append arguments to message.");
612 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
613 log_error("Failed to issue method call: %s", bus_error_message(&error));
618 if (!dbus_message_iter_init(reply, &iter) ||
619 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
620 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
621 log_error("Failed to parse reply.");
626 dbus_message_iter_recurse(&iter, &sub);
628 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
631 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
632 log_error("Failed to parse reply.");
637 dbus_message_iter_recurse(&sub, &sub2);
639 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &prop, true) < 0) {
640 log_error("Failed to parse reply.");
645 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
646 log_error("Failed to parse reply.");
651 dbus_message_iter_recurse(&sub2, &sub3);
653 if (dot_one_property(name, prop, &sub3)) {
654 log_error("Failed to parse reply.");
659 dbus_message_iter_next(&sub);
666 dbus_message_unref(m);
669 dbus_message_unref(reply);
671 dbus_error_free(&error);
676 static int dot(DBusConnection *bus, char **args, unsigned n) {
677 DBusMessage *m = NULL, *reply = NULL;
680 DBusMessageIter iter, sub, sub2;
682 dbus_error_init(&error);
686 if (!(m = dbus_message_new_method_call(
687 "org.freedesktop.systemd1",
688 "/org/freedesktop/systemd1",
689 "org.freedesktop.systemd1.Manager",
691 log_error("Could not allocate message.");
695 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
696 log_error("Failed to issue method call: %s", bus_error_message(&error));
701 if (!dbus_message_iter_init(reply, &iter) ||
702 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
703 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
704 log_error("Failed to parse reply.");
709 printf("digraph systemd {\n");
711 dbus_message_iter_recurse(&iter, &sub);
712 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
713 const char *id, *description, *load_state, *active_state, *sub_state, *following, *unit_path;
715 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
716 log_error("Failed to parse reply.");
721 dbus_message_iter_recurse(&sub, &sub2);
723 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
724 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
725 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
726 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
727 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
728 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &following, true) < 0 ||
729 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, true) < 0) {
730 log_error("Failed to parse reply.");
735 if ((r = dot_one(bus, id, unit_path)) < 0)
738 /* printf("\t\"%s\";\n", id); */
739 dbus_message_iter_next(&sub);
744 log_info(" Color legend: black = Requires\n"
745 " dark blue = Requisite\n"
746 " dark grey = Wants\n"
750 if (isatty(fileno(stdout)))
751 log_notice("-- You probably want to process this output with graphviz' dot tool.\n"
752 "-- Try a shell pipeline like 'systemctl dot | dot -Tsvg > systemd.svg'!\n");
758 dbus_message_unref(m);
761 dbus_message_unref(reply);
763 dbus_error_free(&error);
768 static int list_jobs(DBusConnection *bus, char **args, unsigned n) {
769 DBusMessage *m = NULL, *reply = NULL;
772 DBusMessageIter iter, sub, sub2;
775 dbus_error_init(&error);
781 if (!(m = dbus_message_new_method_call(
782 "org.freedesktop.systemd1",
783 "/org/freedesktop/systemd1",
784 "org.freedesktop.systemd1.Manager",
786 log_error("Could not allocate message.");
790 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
791 log_error("Failed to issue method call: %s", bus_error_message(&error));
796 if (!dbus_message_iter_init(reply, &iter) ||
797 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
798 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
799 log_error("Failed to parse reply.");
804 dbus_message_iter_recurse(&iter, &sub);
806 if (isatty(STDOUT_FILENO))
807 printf("%4s %-25s %-15s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
809 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
810 const char *name, *type, *state, *job_path, *unit_path;
814 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
815 log_error("Failed to parse reply.");
820 dbus_message_iter_recurse(&sub, &sub2);
822 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
823 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
824 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
825 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
826 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
827 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
828 log_error("Failed to parse reply.");
833 e = arg_full ? NULL : ellipsize(name, 25, 33);
834 printf("%4u %-25s %-15s %-7s\n", id, e ? e : name, type, state);
839 dbus_message_iter_next(&sub);
842 if (isatty(STDOUT_FILENO))
843 printf("\n%u jobs listed.\n", k);
849 dbus_message_unref(m);
852 dbus_message_unref(reply);
854 dbus_error_free(&error);
859 static int load_unit(DBusConnection *bus, char **args, unsigned n) {
860 DBusMessage *m = NULL, *reply = NULL;
865 dbus_error_init(&error);
870 for (i = 1; i < n; i++) {
872 if (!(m = dbus_message_new_method_call(
873 "org.freedesktop.systemd1",
874 "/org/freedesktop/systemd1",
875 "org.freedesktop.systemd1.Manager",
877 log_error("Could not allocate message.");
882 if (!dbus_message_append_args(m,
883 DBUS_TYPE_STRING, &args[i],
884 DBUS_TYPE_INVALID)) {
885 log_error("Could not append arguments to message.");
890 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
891 log_error("Failed to issue method call: %s", bus_error_message(&error));
896 dbus_message_unref(m);
897 dbus_message_unref(reply);
906 dbus_message_unref(m);
909 dbus_message_unref(reply);
911 dbus_error_free(&error);
916 static int cancel_job(DBusConnection *bus, char **args, unsigned n) {
917 DBusMessage *m = NULL, *reply = NULL;
922 dbus_error_init(&error);
928 return daemon_reload(bus, args, n);
930 for (i = 1; i < n; i++) {
934 if (!(m = dbus_message_new_method_call(
935 "org.freedesktop.systemd1",
936 "/org/freedesktop/systemd1",
937 "org.freedesktop.systemd1.Manager",
939 log_error("Could not allocate message.");
944 if ((r = safe_atou(args[i], &id)) < 0) {
945 log_error("Failed to parse job id: %s", strerror(-r));
949 assert_cc(sizeof(uint32_t) == sizeof(id));
950 if (!dbus_message_append_args(m,
951 DBUS_TYPE_UINT32, &id,
952 DBUS_TYPE_INVALID)) {
953 log_error("Could not append arguments to message.");
958 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
959 log_error("Failed to issue method call: %s", bus_error_message(&error));
964 if (!dbus_message_get_args(reply, &error,
965 DBUS_TYPE_OBJECT_PATH, &path,
966 DBUS_TYPE_INVALID)) {
967 log_error("Failed to parse reply: %s", bus_error_message(&error));
972 dbus_message_unref(m);
973 if (!(m = dbus_message_new_method_call(
974 "org.freedesktop.systemd1",
976 "org.freedesktop.systemd1.Job",
978 log_error("Could not allocate message.");
983 dbus_message_unref(reply);
984 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
985 log_error("Failed to issue method call: %s", bus_error_message(&error));
990 dbus_message_unref(m);
991 dbus_message_unref(reply);
999 dbus_message_unref(m);
1002 dbus_message_unref(reply);
1004 dbus_error_free(&error);
1009 static bool need_daemon_reload(DBusConnection *bus, const char *unit) {
1010 DBusMessage *m = NULL, *reply = NULL;
1011 dbus_bool_t b = FALSE;
1012 DBusMessageIter iter, sub;
1014 *interface = "org.freedesktop.systemd1.Unit",
1015 *property = "NeedDaemonReload",
1018 /* We ignore all errors here, since this is used to show a warning only */
1020 if (!(m = dbus_message_new_method_call(
1021 "org.freedesktop.systemd1",
1022 "/org/freedesktop/systemd1",
1023 "org.freedesktop.systemd1.Manager",
1027 if (!dbus_message_append_args(m,
1028 DBUS_TYPE_STRING, &unit,
1032 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
1035 if (!dbus_message_get_args(reply, NULL,
1036 DBUS_TYPE_OBJECT_PATH, &path,
1040 dbus_message_unref(m);
1041 if (!(m = dbus_message_new_method_call(
1042 "org.freedesktop.systemd1",
1044 "org.freedesktop.DBus.Properties",
1048 if (!dbus_message_append_args(m,
1049 DBUS_TYPE_STRING, &interface,
1050 DBUS_TYPE_STRING, &property,
1051 DBUS_TYPE_INVALID)) {
1055 dbus_message_unref(reply);
1056 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
1059 if (!dbus_message_iter_init(reply, &iter) ||
1060 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
1063 dbus_message_iter_recurse(&iter, &sub);
1065 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_BOOLEAN)
1068 dbus_message_iter_get_basic(&sub, &b);
1072 dbus_message_unref(m);
1075 dbus_message_unref(reply);
1080 typedef struct WaitData {
1085 static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
1093 dbus_error_init(&error);
1095 log_debug("Got D-Bus request: %s.%s() on %s",
1096 dbus_message_get_interface(message),
1097 dbus_message_get_member(message),
1098 dbus_message_get_path(message));
1100 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
1101 log_error("Warning! D-Bus connection terminated.");
1102 dbus_connection_close(connection);
1104 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
1107 dbus_bool_t success = true;
1109 if (!dbus_message_get_args(message, &error,
1110 DBUS_TYPE_UINT32, &id,
1111 DBUS_TYPE_OBJECT_PATH, &path,
1112 DBUS_TYPE_BOOLEAN, &success,
1114 log_error("Failed to parse message: %s", bus_error_message(&error));
1118 if ((p = set_remove(d->set, (char*) path)))
1126 dbus_error_free(&error);
1127 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1130 static int enable_wait_for_jobs(DBusConnection *bus) {
1138 dbus_error_init(&error);
1139 dbus_bus_add_match(bus,
1141 "sender='org.freedesktop.systemd1',"
1142 "interface='org.freedesktop.systemd1.Manager',"
1143 "member='JobRemoved',"
1144 "path='/org/freedesktop/systemd1'",
1147 if (dbus_error_is_set(&error)) {
1148 log_error("Failed to add match: %s", bus_error_message(&error));
1149 dbus_error_free(&error);
1153 /* This is slightly dirty, since we don't undo the match registrations. */
1157 static int wait_for_jobs(DBusConnection *bus, Set *s) {
1168 if (!dbus_connection_add_filter(bus, wait_filter, &d, NULL)) {
1169 log_error("Failed to add filter.");
1174 while (!set_isempty(s) &&
1175 dbus_connection_read_write_dispatch(bus, -1))
1178 if (!arg_quiet && d.failed)
1179 log_error("Job failed. See system logs and 'systemctl status' for details.");
1181 r = d.failed ? -EIO : 0;
1184 /* This is slightly dirty, since we don't undo the filter registration. */
1189 static int start_unit_one(
1190 DBusConnection *bus,
1197 DBusMessage *m = NULL, *reply = NULL;
1206 assert(arg_no_block || s);
1208 if (!(m = dbus_message_new_method_call(
1209 "org.freedesktop.systemd1",
1210 "/org/freedesktop/systemd1",
1211 "org.freedesktop.systemd1.Manager",
1213 log_error("Could not allocate message.");
1218 if (!dbus_message_append_args(m,
1219 DBUS_TYPE_STRING, &name,
1220 DBUS_TYPE_STRING, &mode,
1221 DBUS_TYPE_INVALID)) {
1222 log_error("Could not append arguments to message.");
1227 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, error))) {
1229 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(error)) {
1230 /* There's always a fallback possible for
1231 * legacy actions. */
1236 log_error("Failed to issue method call: %s", bus_error_message(error));
1241 if (!dbus_message_get_args(reply, error,
1242 DBUS_TYPE_OBJECT_PATH, &path,
1243 DBUS_TYPE_INVALID)) {
1244 log_error("Failed to parse reply: %s", bus_error_message(error));
1249 if (need_daemon_reload(bus, name))
1250 log_warning("Unit file of created job changed on disk, 'systemctl %s daemon-reload' recommended.",
1251 arg_user ? "--user" : "--system");
1253 if (!arg_no_block) {
1256 if (!(p = strdup(path))) {
1257 log_error("Failed to duplicate path.");
1262 if ((r = set_put(s, p)) < 0) {
1264 log_error("Failed to add path to set.");
1273 dbus_message_unref(m);
1276 dbus_message_unref(reply);
1281 static enum action verb_to_action(const char *verb) {
1282 if (streq(verb, "halt"))
1284 else if (streq(verb, "poweroff"))
1285 return ACTION_POWEROFF;
1286 else if (streq(verb, "reboot"))
1287 return ACTION_REBOOT;
1288 else if (streq(verb, "kexec"))
1289 return ACTION_KEXEC;
1290 else if (streq(verb, "rescue"))
1291 return ACTION_RESCUE;
1292 else if (streq(verb, "emergency"))
1293 return ACTION_EMERGENCY;
1294 else if (streq(verb, "default"))
1295 return ACTION_DEFAULT;
1296 else if (streq(verb, "exit"))
1299 return ACTION_INVALID;
1302 static int start_unit(DBusConnection *bus, char **args, unsigned n) {
1304 static const char * const table[_ACTION_MAX] = {
1305 [ACTION_HALT] = SPECIAL_HALT_TARGET,
1306 [ACTION_POWEROFF] = SPECIAL_POWEROFF_TARGET,
1307 [ACTION_REBOOT] = SPECIAL_REBOOT_TARGET,
1308 [ACTION_KEXEC] = SPECIAL_KEXEC_TARGET,
1309 [ACTION_RUNLEVEL2] = SPECIAL_RUNLEVEL2_TARGET,
1310 [ACTION_RUNLEVEL3] = SPECIAL_RUNLEVEL3_TARGET,
1311 [ACTION_RUNLEVEL4] = SPECIAL_RUNLEVEL4_TARGET,
1312 [ACTION_RUNLEVEL5] = SPECIAL_RUNLEVEL5_TARGET,
1313 [ACTION_RESCUE] = SPECIAL_RESCUE_TARGET,
1314 [ACTION_EMERGENCY] = SPECIAL_EMERGENCY_TARGET,
1315 [ACTION_DEFAULT] = SPECIAL_DEFAULT_TARGET,
1316 [ACTION_EXIT] = SPECIAL_EXIT_TARGET
1321 const char *method, *mode, *one_name;
1325 dbus_error_init(&error);
1329 spawn_ask_password_agent();
1331 if (arg_action == ACTION_SYSTEMCTL) {
1333 streq(args[0], "stop") ? "StopUnit" :
1334 streq(args[0], "reload") ? "ReloadUnit" :
1335 streq(args[0], "restart") ? "RestartUnit" :
1336 streq(args[0], "try-restart") ||
1337 streq(args[0], "condrestart") ? "TryRestartUnit" :
1338 streq(args[0], "reload-or-restart") ? "ReloadOrRestartUnit" :
1339 streq(args[0], "reload-or-try-restart") ||
1340 streq(args[0], "force-reload") ? "ReloadOrTryRestartUnit" :
1344 (streq(args[0], "isolate") ||
1345 streq(args[0], "rescue") ||
1346 streq(args[0], "emergency")) ? "isolate" :
1350 one_name = table[verb_to_action(args[0])];
1353 assert(arg_action < ELEMENTSOF(table));
1354 assert(table[arg_action]);
1356 method = "StartUnit";
1358 mode = (arg_action == ACTION_EMERGENCY ||
1359 arg_action == ACTION_RESCUE ||
1360 arg_action == ACTION_RUNLEVEL2 ||
1361 arg_action == ACTION_RUNLEVEL3 ||
1362 arg_action == ACTION_RUNLEVEL4 ||
1363 arg_action == ACTION_RUNLEVEL5) ? "isolate" : "replace";
1365 one_name = table[arg_action];
1368 if (!arg_no_block) {
1369 if ((ret = enable_wait_for_jobs(bus)) < 0) {
1370 log_error("Could not watch jobs: %s", strerror(-ret));
1374 if (!(s = set_new(string_hash_func, string_compare_func))) {
1375 log_error("Failed to allocate set.");
1382 if ((ret = start_unit_one(bus, method, one_name, mode, &error, s)) <= 0)
1385 for (i = 1; i < n; i++)
1386 if ((r = start_unit_one(bus, method, args[i], mode, &error, s)) != 0) {
1387 ret = translate_bus_error_to_exit_status(r, &error);
1388 dbus_error_free(&error);
1393 if ((r = wait_for_jobs(bus, s)) < 0) {
1402 dbus_error_free(&error);
1407 static int start_special(DBusConnection *bus, char **args, unsigned n) {
1414 (streq(args[0], "halt") ||
1415 streq(args[0], "poweroff") ||
1416 streq(args[0], "reboot") ||
1417 streq(args[0], "kexec") ||
1418 streq(args[0], "exit")))
1419 return daemon_reload(bus, args, n);
1421 r = start_unit(bus, args, n);
1424 warn_wall(verb_to_action(args[0]));
1429 static int check_unit(DBusConnection *bus, char **args, unsigned n) {
1430 DBusMessage *m = NULL, *reply = NULL;
1432 *interface = "org.freedesktop.systemd1.Unit",
1433 *property = "ActiveState";
1434 int r = 3; /* According to LSB: "program is not running" */
1441 dbus_error_init(&error);
1443 for (i = 1; i < n; i++) {
1444 const char *path = NULL;
1446 DBusMessageIter iter, sub;
1448 if (!(m = dbus_message_new_method_call(
1449 "org.freedesktop.systemd1",
1450 "/org/freedesktop/systemd1",
1451 "org.freedesktop.systemd1.Manager",
1453 log_error("Could not allocate message.");
1458 if (!dbus_message_append_args(m,
1459 DBUS_TYPE_STRING, &args[i],
1460 DBUS_TYPE_INVALID)) {
1461 log_error("Could not append arguments to message.");
1466 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1468 /* Hmm, cannot figure out anything about this unit... */
1472 dbus_error_free(&error);
1473 dbus_message_unref(m);
1477 if (!dbus_message_get_args(reply, &error,
1478 DBUS_TYPE_OBJECT_PATH, &path,
1479 DBUS_TYPE_INVALID)) {
1480 log_error("Failed to parse reply: %s", bus_error_message(&error));
1485 dbus_message_unref(m);
1486 if (!(m = dbus_message_new_method_call(
1487 "org.freedesktop.systemd1",
1489 "org.freedesktop.DBus.Properties",
1491 log_error("Could not allocate message.");
1496 if (!dbus_message_append_args(m,
1497 DBUS_TYPE_STRING, &interface,
1498 DBUS_TYPE_STRING, &property,
1499 DBUS_TYPE_INVALID)) {
1500 log_error("Could not append arguments to message.");
1505 dbus_message_unref(reply);
1506 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1507 log_error("Failed to issue method call: %s", bus_error_message(&error));
1512 if (!dbus_message_iter_init(reply, &iter) ||
1513 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1514 log_error("Failed to parse reply.");
1519 dbus_message_iter_recurse(&iter, &sub);
1521 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1522 log_error("Failed to parse reply.");
1527 dbus_message_iter_get_basic(&sub, &state);
1532 if (streq(state, "active") || streq(state, "reloading"))
1535 dbus_message_unref(m);
1536 dbus_message_unref(reply);
1542 dbus_message_unref(m);
1545 dbus_message_unref(reply);
1547 dbus_error_free(&error);
1552 static int kill_unit(DBusConnection *bus, char **args, unsigned n) {
1553 DBusMessage *m = NULL, *reply = NULL;
1561 dbus_error_init(&error);
1564 arg_kill_who = "all";
1567 arg_kill_mode = streq(arg_kill_who, "all") ? "control-group" : "process";
1569 for (i = 1; i < n; i++) {
1571 if (!(m = dbus_message_new_method_call(
1572 "org.freedesktop.systemd1",
1573 "/org/freedesktop/systemd1",
1574 "org.freedesktop.systemd1.Manager",
1576 log_error("Could not allocate message.");
1581 if (!dbus_message_append_args(m,
1582 DBUS_TYPE_STRING, &args[i],
1583 DBUS_TYPE_STRING, &arg_kill_who,
1584 DBUS_TYPE_STRING, &arg_kill_mode,
1585 DBUS_TYPE_INT32, &arg_signal,
1586 DBUS_TYPE_INVALID)) {
1587 log_error("Could not append arguments to message.");
1592 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1593 log_error("Failed to issue method call: %s", bus_error_message(&error));
1594 dbus_error_free(&error);
1598 dbus_message_unref(m);
1601 dbus_message_unref(reply);
1607 dbus_message_unref(m);
1610 dbus_message_unref(reply);
1612 dbus_error_free(&error);
1617 typedef struct ExecStatusInfo {
1625 usec_t start_timestamp;
1626 usec_t exit_timestamp;
1631 LIST_FIELDS(struct ExecStatusInfo, exec);
1634 static void exec_status_info_free(ExecStatusInfo *i) {
1643 static int exec_status_info_deserialize(DBusMessageIter *sub, ExecStatusInfo *i) {
1644 uint64_t start_timestamp, exit_timestamp;
1645 DBusMessageIter sub2, sub3;
1649 int32_t code, status;
1655 if (dbus_message_iter_get_arg_type(sub) != DBUS_TYPE_STRUCT)
1658 dbus_message_iter_recurse(sub, &sub2);
1660 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
1663 if (!(i->path = strdup(path)))
1666 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
1667 dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
1671 dbus_message_iter_recurse(&sub2, &sub3);
1672 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1673 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1674 dbus_message_iter_next(&sub3);
1679 if (!(i->argv = new0(char*, n+1)))
1683 dbus_message_iter_recurse(&sub2, &sub3);
1684 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1687 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1688 dbus_message_iter_get_basic(&sub3, &s);
1689 dbus_message_iter_next(&sub3);
1691 if (!(i->argv[n++] = strdup(s)))
1695 if (!dbus_message_iter_next(&sub2) ||
1696 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, true) < 0 ||
1697 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp, true) < 0 ||
1698 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp, true) < 0 ||
1699 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) < 0 ||
1700 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) < 0 ||
1701 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) < 0)
1705 i->start_timestamp = (usec_t) start_timestamp;
1706 i->exit_timestamp = (usec_t) exit_timestamp;
1707 i->pid = (pid_t) pid;
1714 typedef struct UnitStatusInfo {
1716 const char *load_state;
1717 const char *active_state;
1718 const char *sub_state;
1720 const char *description;
1721 const char *following;
1724 const char *default_control_group;
1726 usec_t inactive_exit_timestamp;
1727 usec_t active_enter_timestamp;
1728 usec_t active_exit_timestamp;
1729 usec_t inactive_enter_timestamp;
1731 bool need_daemon_reload;
1736 const char *status_text;
1738 #ifdef HAVE_SYSV_COMPAT
1742 usec_t start_timestamp;
1743 usec_t exit_timestamp;
1745 int exit_code, exit_status;
1748 unsigned n_accepted;
1749 unsigned n_connections;
1753 const char *sysfs_path;
1755 /* Mount, Automount */
1761 LIST_HEAD(ExecStatusInfo, exec);
1764 static void print_status_info(UnitStatusInfo *i) {
1766 const char *on, *off, *ss;
1768 char since1[FORMAT_TIMESTAMP_PRETTY_MAX], *s1;
1769 char since2[FORMAT_TIMESTAMP_MAX], *s2;
1773 /* This shows pretty information about a unit. See
1774 * print_property() for a low-level property printer */
1776 printf("%s", strna(i->id));
1778 if (i->description && !streq_ptr(i->id, i->description))
1779 printf(" - %s", i->description);
1784 printf("\t Follow: unit currently follows state of %s\n", i->following);
1786 if (streq_ptr(i->load_state, "failed") ||
1787 streq_ptr(i->load_state, "banned")) {
1788 on = ansi_highlight(true);
1789 off = ansi_highlight(false);
1794 printf("\t Loaded: %s%s%s (%s)\n", on, strna(i->load_state), off, i->path);
1796 printf("\t Loaded: %s%s%s\n", on, strna(i->load_state), off);
1798 ss = streq_ptr(i->active_state, i->sub_state) ? NULL : i->sub_state;
1800 if (streq_ptr(i->active_state, "failed")) {
1801 on = ansi_highlight(true);
1802 off = ansi_highlight(false);
1803 } else if (streq_ptr(i->active_state, "active") || streq_ptr(i->active_state, "reloading")) {
1804 on = ansi_highlight_green(true);
1805 off = ansi_highlight_green(false);
1810 printf("\t Active: %s%s (%s)%s",
1812 strna(i->active_state),
1816 printf("\t Active: %s%s%s",
1818 strna(i->active_state),
1821 timestamp = (streq_ptr(i->active_state, "active") ||
1822 streq_ptr(i->active_state, "reloading")) ? i->active_enter_timestamp :
1823 (streq_ptr(i->active_state, "inactive") ||
1824 streq_ptr(i->active_state, "failed")) ? i->inactive_enter_timestamp :
1825 streq_ptr(i->active_state, "activating") ? i->inactive_exit_timestamp :
1826 i->active_exit_timestamp;
1828 s1 = format_timestamp_pretty(since1, sizeof(since1), timestamp);
1829 s2 = format_timestamp(since2, sizeof(since2), timestamp);
1832 printf(" since %s; %s\n", s2, s1);
1834 printf(" since %s\n", s2);
1839 printf("\t Device: %s\n", i->sysfs_path);
1841 printf("\t Where: %s\n", i->where);
1843 printf("\t What: %s\n", i->what);
1846 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
1848 LIST_FOREACH(exec, p, i->exec) {
1851 /* Only show exited processes here */
1855 t = strv_join(p->argv, " ");
1856 printf("\t Process: %u %s=%s (code=%s, ", p->pid, p->name, strna(t), sigchld_code_to_string(p->code));
1859 if (p->code == CLD_EXITED) {
1862 printf("status=%i", p->status);
1864 #ifdef HAVE_SYSV_COMPAT
1865 if ((c = exit_status_to_string(p->status, i->is_sysv ? EXIT_STATUS_LSB : EXIT_STATUS_SYSTEMD)))
1867 if ((c = exit_status_to_string(p->status, EXIT_STATUS_SYSTEMD)))
1872 printf("signal=%s", signal_to_string(p->status));
1875 if (i->main_pid == p->pid &&
1876 i->start_timestamp == p->start_timestamp &&
1877 i->exit_timestamp == p->start_timestamp)
1878 /* Let's not show this twice */
1881 if (p->pid == i->control_pid)
1885 if (i->main_pid > 0 || i->control_pid > 0) {
1888 if (i->main_pid > 0) {
1889 printf("Main PID: %u", (unsigned) i->main_pid);
1893 get_process_name(i->main_pid, &t);
1898 } else if (i->exit_code > 0) {
1899 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
1901 if (i->exit_code == CLD_EXITED) {
1904 printf("status=%i", i->exit_status);
1906 #ifdef HAVE_SYSV_COMPAT
1907 if ((c = exit_status_to_string(i->exit_status, i->is_sysv ? EXIT_STATUS_LSB : EXIT_STATUS_SYSTEMD)))
1909 if ((c = exit_status_to_string(i->exit_status, EXIT_STATUS_SYSTEMD)))
1914 printf("signal=%s", signal_to_string(i->exit_status));
1919 if (i->main_pid > 0 && i->control_pid > 0)
1922 if (i->control_pid > 0) {
1925 printf(" Control: %u", (unsigned) i->control_pid);
1927 get_process_name(i->control_pid, &t);
1938 printf("\t Status: \"%s\"\n", i->status_text);
1940 if (i->default_control_group) {
1943 printf("\t CGroup: %s\n", i->default_control_group);
1945 if ((c = columns()) > 18)
1950 show_cgroup_by_path(i->default_control_group, "\t\t ", c);
1953 if (i->need_daemon_reload)
1954 printf("\n%sWarning:%s Unit file changed on disk, 'systemctl %s daemon-reload' recommended.\n",
1955 ansi_highlight(true),
1956 ansi_highlight(false),
1957 arg_user ? "--user" : "--system");
1960 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
1962 switch (dbus_message_iter_get_arg_type(iter)) {
1964 case DBUS_TYPE_STRING: {
1967 dbus_message_iter_get_basic(iter, &s);
1970 if (streq(name, "Id"))
1972 else if (streq(name, "LoadState"))
1974 else if (streq(name, "ActiveState"))
1975 i->active_state = s;
1976 else if (streq(name, "SubState"))
1978 else if (streq(name, "Description"))
1980 else if (streq(name, "FragmentPath"))
1982 #ifdef HAVE_SYSV_COMPAT
1983 else if (streq(name, "SysVPath")) {
1988 else if (streq(name, "DefaultControlGroup"))
1989 i->default_control_group = s;
1990 else if (streq(name, "StatusText"))
1992 else if (streq(name, "SysFSPath"))
1994 else if (streq(name, "Where"))
1996 else if (streq(name, "What"))
1998 else if (streq(name, "Following"))
2005 case DBUS_TYPE_BOOLEAN: {
2008 dbus_message_iter_get_basic(iter, &b);
2010 if (streq(name, "Accept"))
2012 else if (streq(name, "NeedDaemonReload"))
2013 i->need_daemon_reload = b;
2018 case DBUS_TYPE_UINT32: {
2021 dbus_message_iter_get_basic(iter, &u);
2023 if (streq(name, "MainPID")) {
2025 i->main_pid = (pid_t) u;
2028 } else if (streq(name, "ControlPID"))
2029 i->control_pid = (pid_t) u;
2030 else if (streq(name, "ExecMainPID")) {
2032 i->main_pid = (pid_t) u;
2033 } else if (streq(name, "NAccepted"))
2035 else if (streq(name, "NConnections"))
2036 i->n_connections = u;
2041 case DBUS_TYPE_INT32: {
2044 dbus_message_iter_get_basic(iter, &j);
2046 if (streq(name, "ExecMainCode"))
2047 i->exit_code = (int) j;
2048 else if (streq(name, "ExecMainStatus"))
2049 i->exit_status = (int) j;
2054 case DBUS_TYPE_UINT64: {
2057 dbus_message_iter_get_basic(iter, &u);
2059 if (streq(name, "ExecMainStartTimestamp"))
2060 i->start_timestamp = (usec_t) u;
2061 else if (streq(name, "ExecMainExitTimestamp"))
2062 i->exit_timestamp = (usec_t) u;
2063 else if (streq(name, "ActiveEnterTimestamp"))
2064 i->active_enter_timestamp = (usec_t) u;
2065 else if (streq(name, "InactiveEnterTimestamp"))
2066 i->inactive_enter_timestamp = (usec_t) u;
2067 else if (streq(name, "InactiveExitTimestamp"))
2068 i->inactive_exit_timestamp = (usec_t) u;
2069 else if (streq(name, "ActiveExitTimestamp"))
2070 i->active_exit_timestamp = (usec_t) u;
2075 case DBUS_TYPE_ARRAY: {
2077 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT &&
2078 startswith(name, "Exec")) {
2079 DBusMessageIter sub;
2081 dbus_message_iter_recurse(iter, &sub);
2082 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2083 ExecStatusInfo *info;
2086 if (!(info = new0(ExecStatusInfo, 1)))
2089 if (!(info->name = strdup(name))) {
2094 if ((r = exec_status_info_deserialize(&sub, info)) < 0) {
2099 LIST_PREPEND(ExecStatusInfo, exec, i->exec, info);
2101 dbus_message_iter_next(&sub);
2112 static int print_property(const char *name, DBusMessageIter *iter) {
2116 /* This is a low-level property printer, see
2117 * print_status_info() for the nicer output */
2119 if (arg_property && !strv_find(arg_property, name))
2122 switch (dbus_message_iter_get_arg_type(iter)) {
2124 case DBUS_TYPE_STRING: {
2126 dbus_message_iter_get_basic(iter, &s);
2128 if (arg_all || s[0])
2129 printf("%s=%s\n", name, s);
2134 case DBUS_TYPE_BOOLEAN: {
2136 dbus_message_iter_get_basic(iter, &b);
2137 printf("%s=%s\n", name, yes_no(b));
2142 case DBUS_TYPE_UINT64: {
2144 dbus_message_iter_get_basic(iter, &u);
2146 /* Yes, heuristics! But we can change this check
2147 * should it turn out to not be sufficient */
2149 if (strstr(name, "Timestamp")) {
2150 char timestamp[FORMAT_TIMESTAMP_MAX], *t;
2152 if ((t = format_timestamp(timestamp, sizeof(timestamp), u)) || arg_all)
2153 printf("%s=%s\n", name, strempty(t));
2154 } else if (strstr(name, "USec")) {
2155 char timespan[FORMAT_TIMESPAN_MAX];
2157 printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u));
2159 printf("%s=%llu\n", name, (unsigned long long) u);
2164 case DBUS_TYPE_UINT32: {
2166 dbus_message_iter_get_basic(iter, &u);
2168 if (strstr(name, "UMask") || strstr(name, "Mode"))
2169 printf("%s=%04o\n", name, u);
2171 printf("%s=%u\n", name, (unsigned) u);
2176 case DBUS_TYPE_INT32: {
2178 dbus_message_iter_get_basic(iter, &i);
2180 printf("%s=%i\n", name, (int) i);
2184 case DBUS_TYPE_DOUBLE: {
2186 dbus_message_iter_get_basic(iter, &d);
2188 printf("%s=%g\n", name, d);
2192 case DBUS_TYPE_STRUCT: {
2193 DBusMessageIter sub;
2194 dbus_message_iter_recurse(iter, &sub);
2196 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
2199 dbus_message_iter_get_basic(&sub, &u);
2202 printf("%s=%u\n", name, (unsigned) u);
2204 printf("%s=\n", name);
2207 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
2210 dbus_message_iter_get_basic(&sub, &s);
2212 if (arg_all || s[0])
2213 printf("%s=%s\n", name, s);
2221 case DBUS_TYPE_ARRAY:
2223 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
2224 DBusMessageIter sub;
2227 dbus_message_iter_recurse(iter, &sub);
2229 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2230 printf("%s=", name);
2232 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2235 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
2236 dbus_message_iter_get_basic(&sub, &s);
2237 printf("%s%s", space ? " " : "", s);
2240 dbus_message_iter_next(&sub);
2248 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_BYTE) {
2249 DBusMessageIter sub;
2251 dbus_message_iter_recurse(iter, &sub);
2253 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2254 printf("%s=", name);
2256 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2259 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_BYTE);
2260 dbus_message_iter_get_basic(&sub, &u);
2263 dbus_message_iter_next(&sub);
2271 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
2272 DBusMessageIter sub, sub2;
2274 dbus_message_iter_recurse(iter, &sub);
2275 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2276 const char *type, *path;
2278 dbus_message_iter_recurse(&sub, &sub2);
2280 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
2281 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
2282 printf("%s=%s\n", type, path);
2284 dbus_message_iter_next(&sub);
2289 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
2290 DBusMessageIter sub, sub2;
2292 dbus_message_iter_recurse(iter, &sub);
2293 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2295 uint64_t value, next_elapse;
2297 dbus_message_iter_recurse(&sub, &sub2);
2299 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
2300 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
2301 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
2302 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
2304 printf("%s={ value=%s ; next_elapse=%s }\n",
2306 format_timespan(timespan1, sizeof(timespan1), value),
2307 format_timespan(timespan2, sizeof(timespan2), next_elapse));
2310 dbus_message_iter_next(&sub);
2315 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
2316 DBusMessageIter sub;
2318 dbus_message_iter_recurse(iter, &sub);
2319 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2320 ExecStatusInfo info;
2323 if (exec_status_info_deserialize(&sub, &info) >= 0) {
2324 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
2327 t = strv_join(info.argv, " ");
2329 printf("%s={ path=%s ; argv[]=%s ; ignore=%s ; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n",
2333 yes_no(info.ignore),
2334 strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)),
2335 strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)),
2336 (unsigned) info. pid,
2337 sigchld_code_to_string(info.code),
2339 info.code == CLD_EXITED ? "" : "/",
2340 strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status)));
2346 strv_free(info.argv);
2348 dbus_message_iter_next(&sub);
2358 printf("%s=[unprintable]\n", name);
2363 static int show_one(const char *verb, DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
2364 DBusMessage *m = NULL, *reply = NULL;
2365 const char *interface = "";
2368 DBusMessageIter iter, sub, sub2, sub3;
2369 UnitStatusInfo info;
2377 dbus_error_init(&error);
2379 if (!(m = dbus_message_new_method_call(
2380 "org.freedesktop.systemd1",
2382 "org.freedesktop.DBus.Properties",
2384 log_error("Could not allocate message.");
2389 if (!dbus_message_append_args(m,
2390 DBUS_TYPE_STRING, &interface,
2391 DBUS_TYPE_INVALID)) {
2392 log_error("Could not append arguments to message.");
2397 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2398 log_error("Failed to issue method call: %s", bus_error_message(&error));
2403 if (!dbus_message_iter_init(reply, &iter) ||
2404 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
2405 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
2406 log_error("Failed to parse reply.");
2411 dbus_message_iter_recurse(&iter, &sub);
2418 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2421 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
2422 log_error("Failed to parse reply.");
2427 dbus_message_iter_recurse(&sub, &sub2);
2429 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
2430 log_error("Failed to parse reply.");
2435 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
2436 log_error("Failed to parse reply.");
2441 dbus_message_iter_recurse(&sub2, &sub3);
2443 if (show_properties)
2444 r = print_property(name, &sub3);
2446 r = status_property(name, &sub3, &info);
2449 log_error("Failed to parse reply.");
2454 dbus_message_iter_next(&sub);
2459 if (!show_properties)
2460 print_status_info(&info);
2462 if (!streq_ptr(info.active_state, "active") &&
2463 !streq_ptr(info.active_state, "reloading") &&
2464 streq(verb, "status"))
2465 /* According to LSB: "program not running" */
2468 while ((p = info.exec)) {
2469 LIST_REMOVE(ExecStatusInfo, exec, info.exec, p);
2470 exec_status_info_free(p);
2475 dbus_message_unref(m);
2478 dbus_message_unref(reply);
2480 dbus_error_free(&error);
2485 static int show(DBusConnection *bus, char **args, unsigned n) {
2486 DBusMessage *m = NULL, *reply = NULL;
2490 bool show_properties, new_line = false;
2495 dbus_error_init(&error);
2497 show_properties = !streq(args[0], "status");
2499 if (show_properties)
2502 if (show_properties && n <= 1) {
2503 /* If not argument is specified inspect the manager
2506 ret = show_one(args[0], bus, "/org/freedesktop/systemd1", show_properties, &new_line);
2510 for (i = 1; i < n; i++) {
2511 const char *path = NULL;
2514 if (safe_atou32(args[i], &id) < 0) {
2516 /* Interpret as unit name */
2518 if (!(m = dbus_message_new_method_call(
2519 "org.freedesktop.systemd1",
2520 "/org/freedesktop/systemd1",
2521 "org.freedesktop.systemd1.Manager",
2523 log_error("Could not allocate message.");
2528 if (!dbus_message_append_args(m,
2529 DBUS_TYPE_STRING, &args[i],
2530 DBUS_TYPE_INVALID)) {
2531 log_error("Could not append arguments to message.");
2536 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2538 if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
2539 log_error("Failed to issue method call: %s", bus_error_message(&error));
2544 dbus_error_free(&error);
2546 dbus_message_unref(m);
2547 if (!(m = dbus_message_new_method_call(
2548 "org.freedesktop.systemd1",
2549 "/org/freedesktop/systemd1",
2550 "org.freedesktop.systemd1.Manager",
2552 log_error("Could not allocate message.");
2557 if (!dbus_message_append_args(m,
2558 DBUS_TYPE_STRING, &args[i],
2559 DBUS_TYPE_INVALID)) {
2560 log_error("Could not append arguments to message.");
2565 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2566 log_error("Failed to issue method call: %s", bus_error_message(&error));
2568 if (dbus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT))
2569 ret = 4; /* According to LSB: "program or service status is unknown" */
2576 } else if (show_properties) {
2578 /* Interpret as job id */
2580 if (!(m = dbus_message_new_method_call(
2581 "org.freedesktop.systemd1",
2582 "/org/freedesktop/systemd1",
2583 "org.freedesktop.systemd1.Manager",
2585 log_error("Could not allocate message.");
2590 if (!dbus_message_append_args(m,
2591 DBUS_TYPE_UINT32, &id,
2592 DBUS_TYPE_INVALID)) {
2593 log_error("Could not append arguments to message.");
2598 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2599 log_error("Failed to issue method call: %s", bus_error_message(&error));
2605 /* Interpret as PID */
2607 if (!(m = dbus_message_new_method_call(
2608 "org.freedesktop.systemd1",
2609 "/org/freedesktop/systemd1",
2610 "org.freedesktop.systemd1.Manager",
2612 log_error("Could not allocate message.");
2617 if (!dbus_message_append_args(m,
2618 DBUS_TYPE_UINT32, &id,
2619 DBUS_TYPE_INVALID)) {
2620 log_error("Could not append arguments to message.");
2625 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2626 log_error("Failed to issue method call: %s", bus_error_message(&error));
2632 if (!dbus_message_get_args(reply, &error,
2633 DBUS_TYPE_OBJECT_PATH, &path,
2634 DBUS_TYPE_INVALID)) {
2635 log_error("Failed to parse reply: %s", bus_error_message(&error));
2640 if ((r = show_one(args[0], bus, path, show_properties, &new_line)) != 0)
2643 dbus_message_unref(m);
2644 dbus_message_unref(reply);
2650 dbus_message_unref(m);
2653 dbus_message_unref(reply);
2655 dbus_error_free(&error);
2660 static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
2662 DBusMessage *m = NULL, *reply = NULL;
2667 dbus_error_init(&error);
2669 log_debug("Got D-Bus request: %s.%s() on %s",
2670 dbus_message_get_interface(message),
2671 dbus_message_get_member(message),
2672 dbus_message_get_path(message));
2674 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
2675 log_error("Warning! D-Bus connection terminated.");
2676 dbus_connection_close(connection);
2678 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") ||
2679 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
2680 const char *id, *path;
2682 if (!dbus_message_get_args(message, &error,
2683 DBUS_TYPE_STRING, &id,
2684 DBUS_TYPE_OBJECT_PATH, &path,
2686 log_error("Failed to parse message: %s", bus_error_message(&error));
2687 else if (streq(dbus_message_get_member(message), "UnitNew"))
2688 printf("Unit %s added.\n", id);
2690 printf("Unit %s removed.\n", id);
2692 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew") ||
2693 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
2697 if (!dbus_message_get_args(message, &error,
2698 DBUS_TYPE_UINT32, &id,
2699 DBUS_TYPE_OBJECT_PATH, &path,
2701 log_error("Failed to parse message: %s", bus_error_message(&error));
2702 else if (streq(dbus_message_get_member(message), "JobNew"))
2703 printf("Job %u added.\n", id);
2705 printf("Job %u removed.\n", id);
2708 } else if (dbus_message_is_signal(message, "org.freedesktop.DBus.Properties", "PropertiesChanged")) {
2710 const char *path, *interface, *property = "Id";
2711 DBusMessageIter iter, sub;
2713 path = dbus_message_get_path(message);
2715 if (!dbus_message_get_args(message, &error,
2716 DBUS_TYPE_STRING, &interface,
2717 DBUS_TYPE_INVALID)) {
2718 log_error("Failed to parse message: %s", bus_error_message(&error));
2722 if (!streq(interface, "org.freedesktop.systemd1.Job") &&
2723 !streq(interface, "org.freedesktop.systemd1.Unit"))
2726 if (!(m = dbus_message_new_method_call(
2727 "org.freedesktop.systemd1",
2729 "org.freedesktop.DBus.Properties",
2731 log_error("Could not allocate message.");
2735 if (!dbus_message_append_args(m,
2736 DBUS_TYPE_STRING, &interface,
2737 DBUS_TYPE_STRING, &property,
2738 DBUS_TYPE_INVALID)) {
2739 log_error("Could not append arguments to message.");
2743 if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
2744 log_error("Failed to issue method call: %s", bus_error_message(&error));
2748 if (!dbus_message_iter_init(reply, &iter) ||
2749 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2750 log_error("Failed to parse reply.");
2754 dbus_message_iter_recurse(&iter, &sub);
2756 if (streq(interface, "org.freedesktop.systemd1.Unit")) {
2759 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2760 log_error("Failed to parse reply.");
2764 dbus_message_iter_get_basic(&sub, &id);
2765 printf("Unit %s changed.\n", id);
2769 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32) {
2770 log_error("Failed to parse reply.");
2774 dbus_message_iter_get_basic(&sub, &id);
2775 printf("Job %u changed.\n", id);
2781 dbus_message_unref(m);
2784 dbus_message_unref(reply);
2786 dbus_error_free(&error);
2787 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2791 dbus_message_unref(m);
2794 dbus_message_unref(reply);
2796 dbus_error_free(&error);
2797 return DBUS_HANDLER_RESULT_NEED_MEMORY;
2800 static int monitor(DBusConnection *bus, char **args, unsigned n) {
2801 DBusMessage *m = NULL, *reply = NULL;
2805 dbus_error_init(&error);
2808 dbus_bus_add_match(bus,
2810 "sender='org.freedesktop.systemd1',"
2811 "interface='org.freedesktop.systemd1.Manager',"
2812 "path='/org/freedesktop/systemd1'",
2815 if (dbus_error_is_set(&error)) {
2816 log_error("Failed to add match: %s", bus_error_message(&error));
2821 dbus_bus_add_match(bus,
2823 "sender='org.freedesktop.systemd1',"
2824 "interface='org.freedesktop.DBus.Properties',"
2825 "member='PropertiesChanged'",
2828 if (dbus_error_is_set(&error)) {
2829 log_error("Failed to add match: %s", bus_error_message(&error));
2835 if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
2836 log_error("Failed to add filter.");
2841 if (!(m = dbus_message_new_method_call(
2842 "org.freedesktop.systemd1",
2843 "/org/freedesktop/systemd1",
2844 "org.freedesktop.systemd1.Manager",
2846 log_error("Could not allocate message.");
2851 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2852 log_error("Failed to issue method call: %s", bus_error_message(&error));
2857 while (dbus_connection_read_write_dispatch(bus, -1))
2864 /* This is slightly dirty, since we don't undo the filter or the matches. */
2867 dbus_message_unref(m);
2870 dbus_message_unref(reply);
2872 dbus_error_free(&error);
2877 static int dump(DBusConnection *bus, char **args, unsigned n) {
2878 DBusMessage *m = NULL, *reply = NULL;
2883 dbus_error_init(&error);
2887 if (!(m = dbus_message_new_method_call(
2888 "org.freedesktop.systemd1",
2889 "/org/freedesktop/systemd1",
2890 "org.freedesktop.systemd1.Manager",
2892 log_error("Could not allocate message.");
2896 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2897 log_error("Failed to issue method call: %s", bus_error_message(&error));
2902 if (!dbus_message_get_args(reply, &error,
2903 DBUS_TYPE_STRING, &text,
2904 DBUS_TYPE_INVALID)) {
2905 log_error("Failed to parse reply: %s", bus_error_message(&error));
2910 fputs(text, stdout);
2916 dbus_message_unref(m);
2919 dbus_message_unref(reply);
2921 dbus_error_free(&error);
2926 static int snapshot(DBusConnection *bus, char **args, unsigned n) {
2927 DBusMessage *m = NULL, *reply = NULL;
2930 const char *name = "", *path, *id;
2931 dbus_bool_t cleanup = FALSE;
2932 DBusMessageIter iter, sub;
2934 *interface = "org.freedesktop.systemd1.Unit",
2937 dbus_error_init(&error);
2939 if (!(m = dbus_message_new_method_call(
2940 "org.freedesktop.systemd1",
2941 "/org/freedesktop/systemd1",
2942 "org.freedesktop.systemd1.Manager",
2943 "CreateSnapshot"))) {
2944 log_error("Could not allocate message.");
2951 if (!dbus_message_append_args(m,
2952 DBUS_TYPE_STRING, &name,
2953 DBUS_TYPE_BOOLEAN, &cleanup,
2954 DBUS_TYPE_INVALID)) {
2955 log_error("Could not append arguments to message.");
2960 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2961 log_error("Failed to issue method call: %s", bus_error_message(&error));
2966 if (!dbus_message_get_args(reply, &error,
2967 DBUS_TYPE_OBJECT_PATH, &path,
2968 DBUS_TYPE_INVALID)) {
2969 log_error("Failed to parse reply: %s", bus_error_message(&error));
2974 dbus_message_unref(m);
2975 if (!(m = dbus_message_new_method_call(
2976 "org.freedesktop.systemd1",
2978 "org.freedesktop.DBus.Properties",
2980 log_error("Could not allocate message.");
2984 if (!dbus_message_append_args(m,
2985 DBUS_TYPE_STRING, &interface,
2986 DBUS_TYPE_STRING, &property,
2987 DBUS_TYPE_INVALID)) {
2988 log_error("Could not append arguments to message.");
2993 dbus_message_unref(reply);
2994 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2995 log_error("Failed to issue method call: %s", bus_error_message(&error));
3000 if (!dbus_message_iter_init(reply, &iter) ||
3001 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
3002 log_error("Failed to parse reply.");
3007 dbus_message_iter_recurse(&iter, &sub);
3009 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
3010 log_error("Failed to parse reply.");
3015 dbus_message_iter_get_basic(&sub, &id);
3023 dbus_message_unref(m);
3026 dbus_message_unref(reply);
3028 dbus_error_free(&error);
3033 static int delete_snapshot(DBusConnection *bus, char **args, unsigned n) {
3034 DBusMessage *m = NULL, *reply = NULL;
3042 dbus_error_init(&error);
3044 for (i = 1; i < n; i++) {
3045 const char *path = NULL;
3047 if (!(m = dbus_message_new_method_call(
3048 "org.freedesktop.systemd1",
3049 "/org/freedesktop/systemd1",
3050 "org.freedesktop.systemd1.Manager",
3052 log_error("Could not allocate message.");
3057 if (!dbus_message_append_args(m,
3058 DBUS_TYPE_STRING, &args[i],
3059 DBUS_TYPE_INVALID)) {
3060 log_error("Could not append arguments to message.");
3065 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3066 log_error("Failed to issue method call: %s", bus_error_message(&error));
3071 if (!dbus_message_get_args(reply, &error,
3072 DBUS_TYPE_OBJECT_PATH, &path,
3073 DBUS_TYPE_INVALID)) {
3074 log_error("Failed to parse reply: %s", bus_error_message(&error));
3079 dbus_message_unref(m);
3080 if (!(m = dbus_message_new_method_call(
3081 "org.freedesktop.systemd1",
3083 "org.freedesktop.systemd1.Snapshot",
3085 log_error("Could not allocate message.");
3090 dbus_message_unref(reply);
3091 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3092 log_error("Failed to issue method call: %s", bus_error_message(&error));
3097 dbus_message_unref(m);
3098 dbus_message_unref(reply);
3106 dbus_message_unref(m);
3109 dbus_message_unref(reply);
3111 dbus_error_free(&error);
3116 static int daemon_reload(DBusConnection *bus, char **args, unsigned n) {
3117 DBusMessage *m = NULL, *reply = NULL;
3122 dbus_error_init(&error);
3124 if (arg_action == ACTION_RELOAD)
3126 else if (arg_action == ACTION_REEXEC)
3127 method = "Reexecute";
3129 assert(arg_action == ACTION_SYSTEMCTL);
3132 streq(args[0], "clear-jobs") ||
3133 streq(args[0], "cancel") ? "ClearJobs" :
3134 streq(args[0], "daemon-reexec") ? "Reexecute" :
3135 streq(args[0], "reset-failed") ? "ResetFailed" :
3136 streq(args[0], "halt") ? "Halt" :
3137 streq(args[0], "poweroff") ? "PowerOff" :
3138 streq(args[0], "reboot") ? "Reboot" :
3139 streq(args[0], "kexec") ? "KExec" :
3140 streq(args[0], "exit") ? "Exit" :
3141 /* "daemon-reload" */ "Reload";
3144 if (!(m = dbus_message_new_method_call(
3145 "org.freedesktop.systemd1",
3146 "/org/freedesktop/systemd1",
3147 "org.freedesktop.systemd1.Manager",
3149 log_error("Could not allocate message.");
3153 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3155 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
3156 /* There's always a fallback possible for
3157 * legacy actions. */
3162 log_error("Failed to issue method call: %s", bus_error_message(&error));
3171 dbus_message_unref(m);
3174 dbus_message_unref(reply);
3176 dbus_error_free(&error);
3181 static int reset_failed(DBusConnection *bus, char **args, unsigned n) {
3182 DBusMessage *m = NULL, *reply = NULL;
3188 dbus_error_init(&error);
3191 return daemon_reload(bus, args, n);
3193 for (i = 1; i < n; i++) {
3195 if (!(m = dbus_message_new_method_call(
3196 "org.freedesktop.systemd1",
3197 "/org/freedesktop/systemd1",
3198 "org.freedesktop.systemd1.Manager",
3199 "ResetFailedUnit"))) {
3200 log_error("Could not allocate message.");
3205 if (!dbus_message_append_args(m,
3206 DBUS_TYPE_STRING, args + i,
3207 DBUS_TYPE_INVALID)) {
3208 log_error("Could not append arguments to message.");
3213 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3214 log_error("Failed to issue method call: %s", bus_error_message(&error));
3219 dbus_message_unref(m);
3220 dbus_message_unref(reply);
3228 dbus_message_unref(m);
3231 dbus_message_unref(reply);
3233 dbus_error_free(&error);
3238 static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
3239 DBusMessage *m = NULL, *reply = NULL;
3241 DBusMessageIter iter, sub, sub2;
3244 *interface = "org.freedesktop.systemd1.Manager",
3245 *property = "Environment";
3247 dbus_error_init(&error);
3251 if (!(m = dbus_message_new_method_call(
3252 "org.freedesktop.systemd1",
3253 "/org/freedesktop/systemd1",
3254 "org.freedesktop.DBus.Properties",
3256 log_error("Could not allocate message.");
3260 if (!dbus_message_append_args(m,
3261 DBUS_TYPE_STRING, &interface,
3262 DBUS_TYPE_STRING, &property,
3263 DBUS_TYPE_INVALID)) {
3264 log_error("Could not append arguments to message.");
3269 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3270 log_error("Failed to issue method call: %s", bus_error_message(&error));
3275 if (!dbus_message_iter_init(reply, &iter) ||
3276 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
3277 log_error("Failed to parse reply.");
3282 dbus_message_iter_recurse(&iter, &sub);
3284 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
3285 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
3286 log_error("Failed to parse reply.");
3291 dbus_message_iter_recurse(&sub, &sub2);
3293 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
3296 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
3297 log_error("Failed to parse reply.");
3302 dbus_message_iter_get_basic(&sub2, &text);
3303 printf("%s\n", text);
3305 dbus_message_iter_next(&sub2);
3312 dbus_message_unref(m);
3315 dbus_message_unref(reply);
3317 dbus_error_free(&error);
3322 static int set_environment(DBusConnection *bus, char **args, unsigned n) {
3323 DBusMessage *m = NULL, *reply = NULL;
3327 DBusMessageIter iter, sub;
3330 dbus_error_init(&error);
3332 method = streq(args[0], "set-environment")
3334 : "UnsetEnvironment";
3336 if (!(m = dbus_message_new_method_call(
3337 "org.freedesktop.systemd1",
3338 "/org/freedesktop/systemd1",
3339 "org.freedesktop.systemd1.Manager",
3342 log_error("Could not allocate message.");
3346 dbus_message_iter_init_append(m, &iter);
3348 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
3349 log_error("Could not append arguments to message.");
3354 for (i = 1; i < n; i++)
3355 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) {
3356 log_error("Could not append arguments to message.");
3361 if (!dbus_message_iter_close_container(&iter, &sub)) {
3362 log_error("Could not append arguments to message.");
3367 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3368 log_error("Failed to issue method call: %s", bus_error_message(&error));
3377 dbus_message_unref(m);
3380 dbus_message_unref(reply);
3382 dbus_error_free(&error);
3395 static Hashmap *will_install = NULL, *have_installed = NULL;
3396 static Set *remove_symlinks_to = NULL;
3397 static unsigned n_symlinks = 0;
3399 static void install_info_free(InstallInfo *i) {
3404 strv_free(i->aliases);
3405 strv_free(i->wanted_by);
3409 static void install_info_hashmap_free(Hashmap *m) {
3412 while ((i = hashmap_steal_first(m)))
3413 install_info_free(i);
3418 static int install_info_add(const char *name) {
3422 assert(will_install);
3424 if (!unit_name_is_valid_no_type(name, true)) {
3425 log_warning("Unit name %s is not a valid unit name.", name);
3429 if (hashmap_get(have_installed, name) ||
3430 hashmap_get(will_install, name))
3433 if (!(i = new0(InstallInfo, 1))) {
3438 if (!(i->name = strdup(name))) {
3443 if ((r = hashmap_put(will_install, i->name, i)) < 0)
3450 install_info_free(i);
3455 static int config_parse_also(
3456 const char *filename,
3458 const char *section,
3472 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
3476 if (!(n = strndup(w, l)))
3479 if ((r = install_info_add(n)) < 0) {
3480 log_warning("Cannot install unit %s: %s", n, strerror(-r));
3491 static int mark_symlink_for_removal(const char *p) {
3496 assert(path_is_absolute(p));
3498 if (!remove_symlinks_to)
3501 if (!(n = strdup(p)))
3504 path_kill_slashes(n);
3506 if ((r = set_put(remove_symlinks_to, n)) < 0) {
3508 return r == -EEXIST ? 0 : r;
3514 static int remove_marked_symlinks_fd(int fd, const char *config_path, const char *root, bool *deleted) {
3523 if (!(d = fdopendir(fd))) {
3524 close_nointr_nofail(fd);
3530 while ((de = readdir(d))) {
3531 bool is_dir = false, is_link = false;
3533 if (ignore_file(de->d_name))
3536 if (de->d_type == DT_LNK)
3538 else if (de->d_type == DT_DIR)
3540 else if (de->d_type == DT_UNKNOWN) {
3543 if (fstatat(fd, de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) {
3544 log_error("Failed to stat %s/%s: %m", root, de->d_name);
3551 is_link = S_ISLNK(st.st_mode);
3552 is_dir = S_ISDIR(st.st_mode);
3560 if ((nfd = openat(fd, de->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW)) < 0) {
3561 log_error("Failed to open %s/%s: %m", root, de->d_name);
3568 if (asprintf(&p, "%s/%s", root, de->d_name) < 0) {
3569 log_error("Failed to allocate directory string.");
3570 close_nointr_nofail(nfd);
3575 /* This will close nfd, regardless whether it succeeds or not */
3576 q = remove_marked_symlinks_fd(nfd, config_path, p, deleted);
3582 } else if (is_link) {
3586 if (asprintf(&p, "%s/%s", root, de->d_name) < 0) {
3587 log_error("Failed to allocate symlink string.");
3592 if ((q = readlink_and_make_absolute(p, &dest)) < 0) {
3593 log_error("Cannot read symlink %s: %s", p, strerror(-q));
3601 if ((c = canonicalize_file_name(dest))) {
3602 /* This might fail if the destination
3603 * is already removed */
3609 path_kill_slashes(dest);
3610 if (set_get(remove_symlinks_to, dest)) {
3613 log_info("rm '%s'", p);
3615 if (unlink(p) < 0) {
3616 log_error("Cannot unlink symlink %s: %m", p);
3621 rmdir_parents(p, config_path);
3622 path_kill_slashes(p);
3624 if (!set_get(remove_symlinks_to, p)) {
3626 if ((r = mark_symlink_for_removal(p)) < 0) {
3645 static int remove_marked_symlinks(const char *config_path) {
3649 assert(config_path);
3651 if (set_size(remove_symlinks_to) <= 0)
3654 if ((fd = open(config_path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW)) < 0)
3661 if ((cfd = dup(fd)) < 0) {
3666 /* This takes possession of cfd and closes it */
3667 if ((q = remove_marked_symlinks_fd(cfd, config_path, config_path, &deleted)) < 0) {
3673 close_nointr_nofail(fd);
3678 static int create_symlink(const char *verb, const char *old_path, const char *new_path) {
3685 if (streq(verb, "enable")) {
3688 mkdir_parents(new_path, 0755);
3690 if (symlink(old_path, new_path) >= 0) {
3693 log_info("ln -s '%s' '%s'", old_path, new_path);
3698 if (errno != EEXIST) {
3699 log_error("Cannot link %s to %s: %m", old_path, new_path);
3703 if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
3705 if (errno == EINVAL) {
3706 log_error("Cannot link %s to %s, file exists already and is not a symlink.", old_path, new_path);
3710 log_error("readlink() failed: %s", strerror(-r));
3714 if (streq(dest, old_path)) {
3720 log_error("Cannot link %s to %s, symlink exists already and points to %s.", old_path, new_path, dest);
3729 log_info("ln -s '%s' '%s'", old_path, new_path);
3731 if (symlink(old_path, new_path) >= 0)
3734 log_error("Cannot link %s to %s: %m", old_path, new_path);
3737 } else if (streq(verb, "disable")) {
3740 if ((r = mark_symlink_for_removal(old_path)) < 0)
3743 if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
3744 if (errno == ENOENT)
3747 if (errno == EINVAL) {
3748 log_warning("File %s not a symlink, ignoring.", old_path);
3752 log_error("readlink() failed: %s", strerror(-r));
3756 if (!streq(dest, old_path)) {
3757 log_warning("File %s not a symlink to %s but points to %s, ignoring.", new_path, old_path, dest);
3764 if ((r = mark_symlink_for_removal(new_path)) < 0)
3768 log_info("rm '%s'", new_path);
3770 if (unlink(new_path) >= 0)
3773 log_error("Cannot unlink %s: %m", new_path);
3776 } else if (streq(verb, "is-enabled")) {
3779 if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
3781 if (errno == ENOENT || errno == EINVAL)
3784 log_error("readlink() failed: %s", strerror(-r));
3788 if (streq(dest, old_path)) {
3796 assert_not_reached("Unknown action.");
3799 static int install_info_symlink_alias(const char *verb, InstallInfo *i, const char *config_path) {
3801 char *alias_path = NULL;
3806 assert(config_path);
3808 STRV_FOREACH(s, i->aliases) {
3811 if (!(alias_path = path_make_absolute(*s, config_path))) {
3812 log_error("Out of memory");
3817 if ((r = create_symlink(verb, i->path, alias_path)) != 0)
3820 if (streq(verb, "disable"))
3821 rmdir_parents(alias_path, config_path);
3831 static int install_info_symlink_wants(const char *verb, InstallInfo *i, const char *config_path) {
3833 char *alias_path = NULL;
3838 assert(config_path);
3840 STRV_FOREACH(s, i->wanted_by) {
3841 if (!unit_name_is_valid_no_type(*s, true)) {
3842 log_error("Invalid name %s.", *s);
3850 if (asprintf(&alias_path, "%s/%s.wants/%s", config_path, *s, i->name) < 0) {
3851 log_error("Out of memory");
3856 if ((r = create_symlink(verb, i->path, alias_path)) != 0)
3859 if (streq(verb, "disable"))
3860 rmdir_parents(alias_path, config_path);
3871 static int install_info_apply(const char *verb, LookupPaths *paths, InstallInfo *i, const char *config_path) {
3873 const ConfigItem items[] = {
3874 { "Alias", config_parse_strv, &i->aliases, "Install" },
3875 { "WantedBy", config_parse_strv, &i->wanted_by, "Install" },
3876 { "Also", config_parse_also, NULL, "Install" },
3878 { NULL, NULL, NULL, NULL }
3882 char *filename = NULL;
3889 STRV_FOREACH(p, paths->unit_path) {
3892 if (!(filename = path_make_absolute(i->name, *p))) {
3893 log_error("Out of memory");
3897 /* Ensure that we don't follow symlinks */
3898 if ((fd = open(filename, O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NOCTTY)) >= 0)
3899 if ((f = fdopen(fd, "re")))
3902 if (errno == ELOOP) {
3903 log_error("Refusing to operate on symlinks, please pass unit names or absolute paths to unit files.");
3908 if (errno != ENOENT) {
3909 log_error("Failed to open %s: %m", filename);
3919 #if defined(TARGET_FEDORA) && defined (HAVE_SYSV_COMPAT)
3921 if (endswith(i->name, ".service")) {
3925 if (asprintf(&sysv, SYSTEM_SYSVINIT_PATH "/%s", i->name) < 0) {
3926 log_error("Out of memory");
3930 sysv[strlen(sysv) - sizeof(".service") + 1] = 0;
3931 exists = access(sysv, F_OK) >= 0;
3937 const char *argv[] = {
3944 log_info("%s is not a native service, redirecting to /sbin/chkconfig.", i->name);
3946 argv[1] = file_name_from_path(sysv);
3948 streq(verb, "enable") ? "on" :
3949 streq(verb, "disable") ? "off" : NULL;
3951 log_info("Executing %s %s %s", argv[0], argv[1], strempty(argv[2]));
3953 if ((pid = fork()) < 0) {
3954 log_error("Failed to fork: %m");
3957 } else if (pid == 0) {
3958 execv(argv[0], (char**) argv);
3959 _exit(EXIT_FAILURE);
3964 if ((r = wait_for_terminate(pid, &status)) < 0)
3967 if (status.si_code == CLD_EXITED) {
3968 if (status.si_status == 0 && (streq(verb, "enable") || streq(verb, "disable")))
3971 return status.si_status == 0 ? 0 : -EINVAL;
3981 log_error("Couldn't find %s.", i->name);
3987 if ((r = config_parse(filename, f, NULL, items, true, i)) < 0) {
3992 n_symlinks += strv_length(i->aliases);
3993 n_symlinks += strv_length(i->wanted_by);
3997 if ((r = install_info_symlink_alias(verb, i, config_path)) != 0)
4000 if ((r = install_info_symlink_wants(verb, i, config_path)) != 0)
4003 if ((r = mark_symlink_for_removal(filename)) < 0)
4006 if ((r = remove_marked_symlinks(config_path)) < 0)
4012 static char *get_config_path(void) {
4014 if (arg_user && arg_global)
4015 return strdup(USER_CONFIG_UNIT_PATH);
4020 if (user_config_home(&p) < 0)
4026 return strdup(SYSTEM_CONFIG_UNIT_PATH);
4029 static int enable_unit(DBusConnection *bus, char **args, unsigned n) {
4033 char *config_path = NULL;
4036 const char *verb = args[0];
4038 dbus_error_init(&error);
4041 if ((r = lookup_paths_init(&paths, arg_user ? MANAGER_USER : MANAGER_SYSTEM)) < 0) {
4042 log_error("Failed to determine lookup paths: %s", strerror(-r));
4046 if (!(config_path = get_config_path())) {
4047 log_error("Failed to determine config path");
4052 will_install = hashmap_new(string_hash_func, string_compare_func);
4053 have_installed = hashmap_new(string_hash_func, string_compare_func);
4055 if (!will_install || !have_installed) {
4056 log_error("Failed to allocate unit sets.");
4061 if (!arg_defaults && streq(verb, "disable"))
4062 if (!(remove_symlinks_to = set_new(string_hash_func, string_compare_func))) {
4063 log_error("Failed to allocate symlink sets.");
4068 for (j = 1; j < n; j++)
4069 if ((r = install_info_add(args[j])) < 0) {
4070 log_warning("Cannot install unit %s: %s", args[j], strerror(-r));
4074 while ((i = hashmap_first(will_install))) {
4077 assert_se(hashmap_move_one(have_installed, will_install, i->name) == 0);
4079 if ((q = install_info_apply(verb, &paths, i, config_path)) != 0) {
4087 /* In test mode and found something */
4093 if (streq(verb, "is-enabled"))
4094 r = r > 0 ? 0 : -ENOENT;
4096 if (n_symlinks <= 0)
4097 log_warning("Unit files contain no applicable installation information. Ignoring.");
4100 /* Don't try to reload anything if the user asked us to not do this */
4102 /* Don't try to reload anything when updating a unit globally */
4104 /* Don't try to reload anything if we are called for system changes but the system wasn't booted with systemd */
4105 (arg_user || sd_booted() > 0) &&
4106 /* Don't try to reload anything if we are running in a chroot environment */
4107 (arg_user || running_in_chroot() <= 0) ) {
4110 if ((q = daemon_reload(bus, args, n)) < 0)
4116 install_info_hashmap_free(will_install);
4117 install_info_hashmap_free(have_installed);
4119 set_free_free(remove_symlinks_to);
4121 lookup_paths_free(&paths);
4128 static int systemctl_help(void) {
4130 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
4131 "Send control commands to or query the systemd manager.\n\n"
4132 " -h --help Show this help\n"
4133 " --version Show package version\n"
4134 " -t --type=TYPE List only units of a particular type\n"
4135 " -p --property=NAME Show only properties by this name\n"
4136 " -a --all Show all units/properties, including dead/empty ones\n"
4137 " --full Don't ellipsize unit names on output\n"
4138 " --fail When queueing a new job, fail if conflicting jobs are\n"
4140 " -q --quiet Suppress output\n"
4141 " --no-block Do not wait until operation finished\n"
4142 " --no-pager Do not pipe output into a pager.\n"
4143 " --system Connect to system manager\n"
4144 " --user Connect to user service manager\n"
4145 " --order When generating graph for dot, show only order\n"
4146 " --require When generating graph for dot, show only requirement\n"
4147 " --no-wall Don't send wall message before halt/power-off/reboot\n"
4148 " --global Enable/disable unit files globally\n"
4149 " --no-reload When enabling/disabling unit files, don't reload daemon\n"
4151 " --no-ask-password\n"
4152 " Do not ask for system passwords\n"
4153 " --kill-mode=MODE How to send signal\n"
4154 " --kill-who=WHO Who to send signal to\n"
4155 " -s --signal=SIGNAL Which signal to send\n"
4156 " -f --force When enabling unit files, override existing symlinks\n"
4157 " When shutting down, execute action immediately\n"
4158 " --defaults When disabling unit files, remove default symlinks only\n\n"
4160 " list-units List units\n"
4161 " start [NAME...] Start (activate) one or more units\n"
4162 " stop [NAME...] Stop (deactivate) one or more units\n"
4163 " reload [NAME...] Reload one or more units\n"
4164 " restart [NAME...] Start or restart one or more units\n"
4165 " try-restart [NAME...] Restart one or more units if active\n"
4166 " reload-or-restart [NAME...] Reload one or more units is possible,\n"
4167 " otherwise start or restart\n"
4168 " reload-or-try-restart [NAME...] Reload one or more units is possible,\n"
4169 " otherwise restart if active\n"
4170 " isolate [NAME] Start one unit and stop all others\n"
4171 " kill [NAME...] Send signal to processes of a unit\n"
4172 " is-active [NAME...] Check whether units are active\n"
4173 " status [NAME...|PID...] Show runtime status of one or more units\n"
4174 " show [NAME...|JOB...] Show properties of one or more\n"
4175 " units/jobs or the manager\n"
4176 " reset-failed [NAME...] Reset failed state for all, one, or more\n"
4178 " enable [NAME...] Enable one or more unit files\n"
4179 " disable [NAME...] Disable one or more unit files\n"
4180 " is-enabled [NAME...] Check whether unit files are enabled\n"
4181 " load [NAME...] Load one or more units\n"
4182 " list-jobs List jobs\n"
4183 " cancel [JOB...] Cancel all, one, or more jobs\n"
4184 " monitor Monitor unit/job changes\n"
4185 " dump Dump server status\n"
4186 " dot Dump dependency graph for dot(1)\n"
4187 " snapshot [NAME] Create a snapshot\n"
4188 " delete [NAME...] Remove one or more snapshots\n"
4189 " daemon-reload Reload systemd manager configuration\n"
4190 " daemon-reexec Reexecute systemd manager\n"
4191 " show-environment Dump environment\n"
4192 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
4193 " unset-environment [NAME...] Unset one or more environment variables\n"
4194 " default Enter system default mode\n"
4195 " rescue Enter system rescue mode\n"
4196 " emergency Enter system emergency mode\n"
4197 " halt Shut down and halt the system\n"
4198 " poweroff Shut down and power-off the system\n"
4199 " reboot Shut down and reboot the system\n"
4200 " kexec Shut down and reboot the system with kexec\n"
4201 " exit Ask for user instance termination\n",
4202 program_invocation_short_name);
4207 static int halt_help(void) {
4209 printf("%s [OPTIONS...]\n\n"
4210 "%s the system.\n\n"
4211 " --help Show this help\n"
4212 " --halt Halt the machine\n"
4213 " -p --poweroff Switch off the machine\n"
4214 " --reboot Reboot the machine\n"
4215 " -f --force Force immediate halt/power-off/reboot\n"
4216 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
4217 " -d --no-wtmp Don't write wtmp record\n"
4218 " -n --no-sync Don't sync before halt/power-off/reboot\n"
4219 " --no-wall Don't send wall message before halt/power-off/reboot\n",
4220 program_invocation_short_name,
4221 arg_action == ACTION_REBOOT ? "Reboot" :
4222 arg_action == ACTION_POWEROFF ? "Power off" :
4228 static int shutdown_help(void) {
4230 printf("%s [OPTIONS...] [TIME] [WALL...]\n\n"
4231 "Shut down the system.\n\n"
4232 " --help Show this help\n"
4233 " -H --halt Halt the machine\n"
4234 " -P --poweroff Power-off the machine\n"
4235 " -r --reboot Reboot the machine\n"
4236 " -h Equivalent to --poweroff, overriden by --halt\n"
4237 " -k Don't halt/power-off/reboot, just send warnings\n"
4238 " --no-wall Don't send wall message before halt/power-off/reboot\n"
4239 " -c Cancel a pending shutdown\n",
4240 program_invocation_short_name);
4245 static int telinit_help(void) {
4247 printf("%s [OPTIONS...] {COMMAND}\n\n"
4248 "Send control commands to the init daemon.\n\n"
4249 " --help Show this help\n"
4250 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
4252 " 0 Power-off the machine\n"
4253 " 6 Reboot the machine\n"
4254 " 2, 3, 4, 5 Start runlevelX.target unit\n"
4255 " 1, s, S Enter rescue mode\n"
4256 " q, Q Reload init daemon configuration\n"
4257 " u, U Reexecute init daemon\n",
4258 program_invocation_short_name);
4263 static int runlevel_help(void) {
4265 printf("%s [OPTIONS...]\n\n"
4266 "Prints the previous and current runlevel of the init system.\n\n"
4267 " --help Show this help\n",
4268 program_invocation_short_name);
4273 static int systemctl_parse_argv(int argc, char *argv[]) {
4294 static const struct option options[] = {
4295 { "help", no_argument, NULL, 'h' },
4296 { "version", no_argument, NULL, ARG_VERSION },
4297 { "type", required_argument, NULL, 't' },
4298 { "property", required_argument, NULL, 'p' },
4299 { "all", no_argument, NULL, 'a' },
4300 { "full", no_argument, NULL, ARG_FULL },
4301 { "fail", no_argument, NULL, ARG_FAIL },
4302 { "user", no_argument, NULL, ARG_USER },
4303 { "system", no_argument, NULL, ARG_SYSTEM },
4304 { "global", no_argument, NULL, ARG_GLOBAL },
4305 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
4306 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
4307 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4308 { "quiet", no_argument, NULL, 'q' },
4309 { "order", no_argument, NULL, ARG_ORDER },
4310 { "require", no_argument, NULL, ARG_REQUIRE },
4311 { "force", no_argument, NULL, 'f' },
4312 { "no-reload", no_argument, NULL, ARG_NO_RELOAD },
4313 { "defaults", no_argument, NULL, ARG_DEFAULTS },
4314 { "kill-mode", required_argument, NULL, ARG_KILL_MODE },
4315 { "kill-who", required_argument, NULL, ARG_KILL_WHO },
4316 { "signal", required_argument, NULL, 's' },
4317 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
4318 { NULL, 0, NULL, 0 }
4326 /* Only when running as systemctl we ask for passwords */
4327 arg_ask_password = true;
4329 while ((c = getopt_long(argc, argv, "ht:p:aqfs:", options, NULL)) >= 0) {
4338 puts(PACKAGE_STRING);
4340 puts(SYSTEMD_FEATURES);
4350 if (!(l = strv_append(arg_property, optarg)))
4353 strv_free(arg_property);
4356 /* If the user asked for a particular
4357 * property, show it to him, even if it is
4380 arg_no_block = true;
4384 arg_no_pager = true;
4392 arg_dot = DOT_ORDER;
4396 arg_dot = DOT_REQUIRE;
4412 arg_no_reload = true;
4421 arg_defaults = true;
4425 arg_kill_who = optarg;
4429 arg_kill_mode = optarg;
4433 if ((arg_signal = signal_from_string_try_harder(optarg)) < 0) {
4434 log_error("Failed to parse signal string %s.", optarg);
4439 case ARG_NO_ASK_PASSWORD:
4440 arg_ask_password = false;
4447 log_error("Unknown option code %c", c);
4455 static int halt_parse_argv(int argc, char *argv[]) {
4464 static const struct option options[] = {
4465 { "help", no_argument, NULL, ARG_HELP },
4466 { "halt", no_argument, NULL, ARG_HALT },
4467 { "poweroff", no_argument, NULL, 'p' },
4468 { "reboot", no_argument, NULL, ARG_REBOOT },
4469 { "force", no_argument, NULL, 'f' },
4470 { "wtmp-only", no_argument, NULL, 'w' },
4471 { "no-wtmp", no_argument, NULL, 'd' },
4472 { "no-sync", no_argument, NULL, 'n' },
4473 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4474 { NULL, 0, NULL, 0 }
4482 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
4483 if (runlevel == '0' || runlevel == '6')
4484 arg_immediate = true;
4486 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
4494 arg_action = ACTION_HALT;
4498 if (arg_action != ACTION_REBOOT)
4499 arg_action = ACTION_POWEROFF;
4503 arg_action = ACTION_REBOOT;
4507 arg_immediate = true;
4528 /* Compatibility nops */
4535 log_error("Unknown option code %c", c);
4540 if (optind < argc) {
4541 log_error("Too many arguments.");
4548 static int parse_time_spec(const char *t, usec_t *_u) {
4552 if (streq(t, "now"))
4554 else if (t[0] == '+') {
4557 if (safe_atou64(t + 1, &u) < 0)
4560 *_u = now(CLOCK_REALTIME) + USEC_PER_MINUTE * u;
4569 hour = strtol(t, &e, 10);
4570 if (errno != 0 || *e != ':' || hour < 0 || hour > 23)
4573 minute = strtol(e+1, &e, 10);
4574 if (errno != 0 || *e != 0 || minute < 0 || minute > 59)
4577 n = now(CLOCK_REALTIME);
4578 s = (time_t) (n / USEC_PER_SEC);
4581 assert_se(localtime_r(&s, &tm));
4583 tm.tm_hour = (int) hour;
4584 tm.tm_min = (int) minute;
4587 assert_se(s = mktime(&tm));
4589 *_u = (usec_t) s * USEC_PER_SEC;
4592 *_u += USEC_PER_DAY;
4598 static int shutdown_parse_argv(int argc, char *argv[]) {
4605 static const struct option options[] = {
4606 { "help", no_argument, NULL, ARG_HELP },
4607 { "halt", no_argument, NULL, 'H' },
4608 { "poweroff", no_argument, NULL, 'P' },
4609 { "reboot", no_argument, NULL, 'r' },
4610 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4611 { NULL, 0, NULL, 0 }
4619 while ((c = getopt_long(argc, argv, "HPrhkt:afFc", options, NULL)) >= 0) {
4627 arg_action = ACTION_HALT;
4631 arg_action = ACTION_POWEROFF;
4635 arg_action = ACTION_REBOOT;
4639 if (arg_action != ACTION_HALT)
4640 arg_action = ACTION_POWEROFF;
4653 /* Compatibility nops */
4657 arg_action = ACTION_CANCEL_SHUTDOWN;
4664 log_error("Unknown option code %c", c);
4669 if (argc > optind) {
4670 if ((r = parse_time_spec(argv[optind], &arg_when)) < 0) {
4671 log_error("Failed to parse time specification: %s", argv[optind]);
4675 arg_when = now(CLOCK_REALTIME) + USEC_PER_MINUTE;
4677 /* We skip the time argument */
4678 if (argc > optind + 1)
4679 arg_wall = argv + optind + 1;
4686 static int telinit_parse_argv(int argc, char *argv[]) {
4693 static const struct option options[] = {
4694 { "help", no_argument, NULL, ARG_HELP },
4695 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4696 { NULL, 0, NULL, 0 }
4699 static const struct {
4703 { '0', ACTION_POWEROFF },
4704 { '6', ACTION_REBOOT },
4705 { '1', ACTION_RESCUE },
4706 { '2', ACTION_RUNLEVEL2 },
4707 { '3', ACTION_RUNLEVEL3 },
4708 { '4', ACTION_RUNLEVEL4 },
4709 { '5', ACTION_RUNLEVEL5 },
4710 { 's', ACTION_RESCUE },
4711 { 'S', ACTION_RESCUE },
4712 { 'q', ACTION_RELOAD },
4713 { 'Q', ACTION_RELOAD },
4714 { 'u', ACTION_REEXEC },
4715 { 'U', ACTION_REEXEC }
4724 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4739 log_error("Unknown option code %c", c);
4744 if (optind >= argc) {
4749 if (optind + 1 < argc) {
4750 log_error("Too many arguments.");
4754 if (strlen(argv[optind]) != 1) {
4755 log_error("Expected single character argument.");
4759 for (i = 0; i < ELEMENTSOF(table); i++)
4760 if (table[i].from == argv[optind][0])
4763 if (i >= ELEMENTSOF(table)) {
4764 log_error("Unknown command %s.", argv[optind]);
4768 arg_action = table[i].to;
4775 static int runlevel_parse_argv(int argc, char *argv[]) {
4781 static const struct option options[] = {
4782 { "help", no_argument, NULL, ARG_HELP },
4783 { NULL, 0, NULL, 0 }
4791 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4802 log_error("Unknown option code %c", c);
4807 if (optind < argc) {
4808 log_error("Too many arguments.");
4815 static int parse_argv(int argc, char *argv[]) {
4819 if (program_invocation_short_name) {
4821 if (strstr(program_invocation_short_name, "halt")) {
4822 arg_action = ACTION_HALT;
4823 return halt_parse_argv(argc, argv);
4824 } else if (strstr(program_invocation_short_name, "poweroff")) {
4825 arg_action = ACTION_POWEROFF;
4826 return halt_parse_argv(argc, argv);
4827 } else if (strstr(program_invocation_short_name, "reboot")) {
4828 arg_action = ACTION_REBOOT;
4829 return halt_parse_argv(argc, argv);
4830 } else if (strstr(program_invocation_short_name, "shutdown")) {
4831 arg_action = ACTION_POWEROFF;
4832 return shutdown_parse_argv(argc, argv);
4833 } else if (strstr(program_invocation_short_name, "init")) {
4835 if (sd_booted() > 0) {
4836 arg_action = ACTION_INVALID;
4837 return telinit_parse_argv(argc, argv);
4839 /* Hmm, so some other init system is
4840 * running, we need to forward this
4841 * request to it. For now we simply
4842 * guess that it is Upstart. */
4844 execv("/lib/upstart/telinit", argv);
4846 log_error("Couldn't find an alternative telinit implementation to spawn.");
4850 } else if (strstr(program_invocation_short_name, "runlevel")) {
4851 arg_action = ACTION_RUNLEVEL;
4852 return runlevel_parse_argv(argc, argv);
4856 arg_action = ACTION_SYSTEMCTL;
4857 return systemctl_parse_argv(argc, argv);
4860 static int action_to_runlevel(void) {
4862 static const char table[_ACTION_MAX] = {
4863 [ACTION_HALT] = '0',
4864 [ACTION_POWEROFF] = '0',
4865 [ACTION_REBOOT] = '6',
4866 [ACTION_RUNLEVEL2] = '2',
4867 [ACTION_RUNLEVEL3] = '3',
4868 [ACTION_RUNLEVEL4] = '4',
4869 [ACTION_RUNLEVEL5] = '5',
4870 [ACTION_RESCUE] = '1'
4873 assert(arg_action < _ACTION_MAX);
4875 return table[arg_action];
4878 static int talk_upstart(void) {
4879 DBusMessage *m = NULL, *reply = NULL;
4881 int previous, rl, r;
4883 env1_buf[] = "RUNLEVEL=X",
4884 env2_buf[] = "PREVLEVEL=X";
4885 char *env1 = env1_buf, *env2 = env2_buf;
4886 const char *emit = "runlevel";
4887 dbus_bool_t b_false = FALSE;
4888 DBusMessageIter iter, sub;
4889 DBusConnection *bus;
4891 dbus_error_init(&error);
4893 if (!(rl = action_to_runlevel()))
4896 if (utmp_get_runlevel(&previous, NULL) < 0)
4899 if (!(bus = dbus_connection_open_private("unix:abstract=/com/ubuntu/upstart", &error))) {
4900 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
4905 log_error("Failed to connect to Upstart bus: %s", bus_error_message(&error));
4910 if ((r = bus_check_peercred(bus)) < 0) {
4911 log_error("Failed to verify owner of bus.");
4915 if (!(m = dbus_message_new_method_call(
4916 "com.ubuntu.Upstart",
4917 "/com/ubuntu/Upstart",
4918 "com.ubuntu.Upstart0_6",
4921 log_error("Could not allocate message.");
4926 dbus_message_iter_init_append(m, &iter);
4928 env1_buf[sizeof(env1_buf)-2] = rl;
4929 env2_buf[sizeof(env2_buf)-2] = previous;
4931 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
4932 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
4933 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
4934 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
4935 !dbus_message_iter_close_container(&iter, &sub) ||
4936 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
4937 log_error("Could not append arguments to message.");
4942 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
4944 if (error_is_no_service(&error)) {
4949 log_error("Failed to issue method call: %s", bus_error_message(&error));
4958 dbus_message_unref(m);
4961 dbus_message_unref(reply);
4964 dbus_connection_flush(bus);
4965 dbus_connection_close(bus);
4966 dbus_connection_unref(bus);
4969 dbus_error_free(&error);
4974 static int talk_initctl(void) {
4975 struct init_request request;
4979 if (!(rl = action_to_runlevel()))
4983 request.magic = INIT_MAGIC;
4984 request.sleeptime = 0;
4985 request.cmd = INIT_CMD_RUNLVL;
4986 request.runlevel = rl;
4988 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
4990 if (errno == ENOENT)
4993 log_error("Failed to open "INIT_FIFO": %m");
4998 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
4999 close_nointr_nofail(fd);
5002 log_error("Failed to write to "INIT_FIFO": %m");
5003 return errno ? -errno : -EIO;
5009 static int systemctl_main(DBusConnection *bus, int argc, char *argv[], DBusError *error) {
5011 static const struct {
5019 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
5021 { "list-units", LESS, 1, list_units },
5022 { "list-jobs", EQUAL, 1, list_jobs },
5023 { "clear-jobs", EQUAL, 1, daemon_reload },
5024 { "load", MORE, 2, load_unit },
5025 { "cancel", MORE, 2, cancel_job },
5026 { "start", MORE, 2, start_unit },
5027 { "stop", MORE, 2, start_unit },
5028 { "reload", MORE, 2, start_unit },
5029 { "restart", MORE, 2, start_unit },
5030 { "try-restart", MORE, 2, start_unit },
5031 { "reload-or-restart", MORE, 2, start_unit },
5032 { "reload-or-try-restart", MORE, 2, start_unit },
5033 { "force-reload", MORE, 2, start_unit }, /* For compatibility with SysV */
5034 { "condrestart", MORE, 2, start_unit }, /* For compatibility with RH */
5035 { "isolate", EQUAL, 2, start_unit },
5036 { "kill", MORE, 2, kill_unit },
5037 { "is-active", MORE, 2, check_unit },
5038 { "check", MORE, 2, check_unit },
5039 { "show", MORE, 1, show },
5040 { "status", MORE, 2, show },
5041 { "monitor", EQUAL, 1, monitor },
5042 { "dump", EQUAL, 1, dump },
5043 { "dot", EQUAL, 1, dot },
5044 { "snapshot", LESS, 2, snapshot },
5045 { "delete", MORE, 2, delete_snapshot },
5046 { "daemon-reload", EQUAL, 1, daemon_reload },
5047 { "daemon-reexec", EQUAL, 1, daemon_reload },
5048 { "show-environment", EQUAL, 1, show_enviroment },
5049 { "set-environment", MORE, 2, set_environment },
5050 { "unset-environment", MORE, 2, set_environment },
5051 { "halt", EQUAL, 1, start_special },
5052 { "poweroff", EQUAL, 1, start_special },
5053 { "reboot", EQUAL, 1, start_special },
5054 { "kexec", EQUAL, 1, start_special },
5055 { "default", EQUAL, 1, start_special },
5056 { "rescue", EQUAL, 1, start_special },
5057 { "emergency", EQUAL, 1, start_special },
5058 { "exit", EQUAL, 1, start_special },
5059 { "reset-failed", MORE, 1, reset_failed },
5060 { "enable", MORE, 2, enable_unit },
5061 { "disable", MORE, 2, enable_unit },
5062 { "is-enabled", MORE, 2, enable_unit }
5072 left = argc - optind;
5075 /* Special rule: no arguments means "list-units" */
5078 if (streq(argv[optind], "help")) {
5083 for (i = 0; i < ELEMENTSOF(verbs); i++)
5084 if (streq(argv[optind], verbs[i].verb))
5087 if (i >= ELEMENTSOF(verbs)) {
5088 log_error("Unknown operation %s", argv[optind]);
5093 switch (verbs[i].argc_cmp) {
5096 if (left != verbs[i].argc) {
5097 log_error("Invalid number of arguments.");
5104 if (left < verbs[i].argc) {
5105 log_error("Too few arguments.");
5112 if (left > verbs[i].argc) {
5113 log_error("Too many arguments.");
5120 assert_not_reached("Unknown comparison operator.");
5123 /* Require a bus connection for all operations but
5125 if (!streq(verbs[i].verb, "enable") &&
5126 !streq(verbs[i].verb, "disable") &&
5128 log_error("Failed to get D-Bus connection: %s", error->message);
5132 return verbs[i].dispatch(bus, argv + optind, left);
5135 static int send_shutdownd(usec_t t, char mode, bool warn, const char *message) {
5137 struct msghdr msghdr;
5139 union sockaddr_union sockaddr;
5140 struct shutdownd_command c;
5148 strncpy(c.wall_message, message, sizeof(c.wall_message));
5150 if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0)
5154 sockaddr.sa.sa_family = AF_UNIX;
5155 sockaddr.un.sun_path[0] = 0;
5156 strncpy(sockaddr.un.sun_path+1, "/org/freedesktop/systemd1/shutdownd", sizeof(sockaddr.un.sun_path)-1);
5159 iovec.iov_base = (char*) &c;
5160 iovec.iov_len = sizeof(c);
5163 msghdr.msg_name = &sockaddr;
5164 msghdr.msg_namelen = offsetof(struct sockaddr_un, sun_path) + 1 + sizeof("/org/freedesktop/systemd1/shutdownd") - 1;
5166 msghdr.msg_iov = &iovec;
5167 msghdr.msg_iovlen = 1;
5169 if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) < 0) {
5170 close_nointr_nofail(fd);
5174 close_nointr_nofail(fd);
5178 static int reload_with_fallback(DBusConnection *bus) {
5181 /* First, try systemd via D-Bus. */
5182 if (daemon_reload(bus, NULL, 0) > 0)
5186 /* Nothing else worked, so let's try signals */
5187 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
5189 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
5190 log_error("kill() failed: %m");
5197 static int start_with_fallback(DBusConnection *bus) {
5200 /* First, try systemd via D-Bus. */
5201 if (start_unit(bus, NULL, 0) >= 0)
5205 /* Hmm, talking to systemd via D-Bus didn't work. Then
5206 * let's try to talk to Upstart via D-Bus. */
5207 if (talk_upstart() > 0)
5210 /* Nothing else worked, so let's try
5212 if (talk_initctl() > 0)
5215 log_error("Failed to talk to init daemon.");
5219 warn_wall(arg_action);
5223 static int halt_main(DBusConnection *bus) {
5226 if (geteuid() != 0) {
5227 log_error("Must be root.");
5233 char date[FORMAT_TIMESTAMP_MAX];
5235 m = strv_join(arg_wall, " ");
5236 r = send_shutdownd(arg_when,
5237 arg_action == ACTION_HALT ? 'H' :
5238 arg_action == ACTION_POWEROFF ? 'P' :
5245 log_warning("Failed to talk to shutdownd, proceeding with immediate shutdown: %s", strerror(-r));
5247 log_info("Shutdown scheduled for %s, use 'shutdown -c' to cancel.",
5248 format_timestamp(date, sizeof(date), arg_when));
5253 if (!arg_dry && !arg_immediate)
5254 return start_with_fallback(bus);
5257 if (sd_booted() > 0)
5258 log_debug("Not writing utmp record, assuming that systemd-update-utmp is used.");
5259 else if ((r = utmp_put_shutdown(0)) < 0)
5260 log_warning("Failed to write utmp record: %s", strerror(-r));
5269 /* Make sure C-A-D is handled by the kernel from this
5271 reboot(RB_ENABLE_CAD);
5273 switch (arg_action) {
5276 log_info("Halting.");
5277 reboot(RB_HALT_SYSTEM);
5280 case ACTION_POWEROFF:
5281 log_info("Powering off.");
5282 reboot(RB_POWER_OFF);
5286 log_info("Rebooting.");
5287 reboot(RB_AUTOBOOT);
5291 assert_not_reached("Unknown halt action.");
5294 /* We should never reach this. */
5298 static int runlevel_main(void) {
5299 int r, runlevel, previous;
5301 if ((r = utmp_get_runlevel(&runlevel, &previous)) < 0) {
5302 printf("unknown\n");
5307 previous <= 0 ? 'N' : previous,
5308 runlevel <= 0 ? 'N' : runlevel);
5313 static void pager_open(void) {
5320 if (!on_tty() || arg_no_pager)
5323 if ((pager = getenv("PAGER")))
5324 if (!*pager || streq(pager, "cat"))
5327 /* Determine and cache number of columns before we spawn the
5328 * pager so that we get the value from the actual tty */
5332 log_error("Failed to create pager pipe: %m");
5337 if (pager_pid < 0) {
5338 log_error("Failed to fork pager: %m");
5343 /* In the child start the pager */
5344 if (pager_pid == 0) {
5346 dup2(fd[0], STDIN_FILENO);
5349 setenv("LESS", "FRSX", 0);
5351 prctl(PR_SET_PDEATHSIG, SIGTERM);
5354 execlp(pager, pager, NULL);
5355 execl("/bin/sh", "sh", "-c", pager, NULL);
5357 /* Debian's alternatives command for pagers is
5358 * called 'pager'. Note that we do not call
5359 * sensible-pagers here, since that is just a
5360 * shell script that implements a logic that
5361 * is similar to this one anyway, but is
5362 * Debian-specific. */
5363 execlp("pager", "pager", NULL);
5365 execlp("less", "less", NULL);
5366 execlp("more", "more", NULL);
5369 log_error("Unable to execute pager: %m");
5370 _exit(EXIT_FAILURE);
5373 /* Return in the parent */
5374 if (dup2(fd[1], STDOUT_FILENO) < 0)
5375 log_error("Failed to duplicate pager pipe: %m");
5380 static void pager_close(void) {
5386 /* Inform pager that we are done */
5388 wait_for_terminate(pager_pid, &dummy);
5392 int main(int argc, char*argv[]) {
5393 int r, retval = EXIT_FAILURE;
5394 DBusConnection *bus = NULL;
5397 dbus_error_init(&error);
5399 log_parse_environment();
5402 if ((r = parse_argv(argc, argv)) < 0)
5405 retval = EXIT_SUCCESS;
5409 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
5410 * let's shortcut this */
5411 if (arg_action == ACTION_RUNLEVEL) {
5412 r = runlevel_main();
5413 retval = r < 0 ? EXIT_FAILURE : r;
5417 bus_connect(arg_user ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &bus, &private_bus, &error);
5419 switch (arg_action) {
5421 case ACTION_SYSTEMCTL:
5422 r = systemctl_main(bus, argc, argv, &error);
5426 case ACTION_POWEROFF:
5431 case ACTION_RUNLEVEL2:
5432 case ACTION_RUNLEVEL3:
5433 case ACTION_RUNLEVEL4:
5434 case ACTION_RUNLEVEL5:
5436 case ACTION_EMERGENCY:
5437 case ACTION_DEFAULT:
5438 r = start_with_fallback(bus);
5443 r = reload_with_fallback(bus);
5446 case ACTION_CANCEL_SHUTDOWN:
5447 r = send_shutdownd(0, 0, false, NULL);
5450 case ACTION_INVALID:
5451 case ACTION_RUNLEVEL:
5453 assert_not_reached("Unknown action");
5456 retval = r < 0 ? EXIT_FAILURE : r;
5461 dbus_connection_flush(bus);
5462 dbus_connection_close(bus);
5463 dbus_connection_unref(bus);
5466 dbus_error_free(&error);
5470 strv_free(arg_property);