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 {
1623 usec_t start_timestamp;
1624 usec_t exit_timestamp;
1629 LIST_FIELDS(struct ExecStatusInfo, exec);
1632 static void exec_status_info_free(ExecStatusInfo *i) {
1640 static int exec_status_info_deserialize(DBusMessageIter *sub, ExecStatusInfo *i) {
1641 uint64_t start_timestamp, exit_timestamp;
1642 DBusMessageIter sub2, sub3;
1646 int32_t code, status;
1652 if (dbus_message_iter_get_arg_type(sub) != DBUS_TYPE_STRUCT)
1655 dbus_message_iter_recurse(sub, &sub2);
1657 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
1660 if (!(i->path = strdup(path)))
1663 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
1664 dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
1668 dbus_message_iter_recurse(&sub2, &sub3);
1669 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1670 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1671 dbus_message_iter_next(&sub3);
1676 if (!(i->argv = new0(char*, n+1)))
1680 dbus_message_iter_recurse(&sub2, &sub3);
1681 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1684 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1685 dbus_message_iter_get_basic(&sub3, &s);
1686 dbus_message_iter_next(&sub3);
1688 if (!(i->argv[n++] = strdup(s)))
1692 if (!dbus_message_iter_next(&sub2) ||
1693 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, true) < 0 ||
1694 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp, true) < 0 ||
1695 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp, true) < 0 ||
1696 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) < 0 ||
1697 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) < 0 ||
1698 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) < 0)
1702 i->start_timestamp = (usec_t) start_timestamp;
1703 i->exit_timestamp = (usec_t) exit_timestamp;
1704 i->pid = (pid_t) pid;
1711 typedef struct UnitStatusInfo {
1713 const char *load_state;
1714 const char *active_state;
1715 const char *sub_state;
1717 const char *description;
1718 const char *following;
1721 const char *default_control_group;
1723 usec_t inactive_exit_timestamp;
1724 usec_t active_enter_timestamp;
1725 usec_t active_exit_timestamp;
1726 usec_t inactive_enter_timestamp;
1728 bool need_daemon_reload;
1733 const char *status_text;
1735 #ifdef HAVE_SYSV_COMPAT
1739 usec_t start_timestamp;
1740 usec_t exit_timestamp;
1742 int exit_code, exit_status;
1745 unsigned n_accepted;
1746 unsigned n_connections;
1750 const char *sysfs_path;
1752 /* Mount, Automount */
1758 LIST_HEAD(ExecStatusInfo, exec);
1761 static void print_status_info(UnitStatusInfo *i) {
1763 const char *on, *off, *ss;
1765 char since1[FORMAT_TIMESTAMP_PRETTY_MAX], *s1;
1766 char since2[FORMAT_TIMESTAMP_MAX], *s2;
1770 /* This shows pretty information about a unit. See
1771 * print_property() for a low-level property printer */
1773 printf("%s", strna(i->id));
1775 if (i->description && !streq_ptr(i->id, i->description))
1776 printf(" - %s", i->description);
1781 printf("\t Follow: unit currently follows state of %s\n", i->following);
1783 if (streq_ptr(i->load_state, "failed") ||
1784 streq_ptr(i->load_state, "banned")) {
1785 on = ansi_highlight(true);
1786 off = ansi_highlight(false);
1791 printf("\t Loaded: %s%s%s (%s)\n", on, strna(i->load_state), off, i->path);
1793 printf("\t Loaded: %s%s%s\n", on, strna(i->load_state), off);
1795 ss = streq_ptr(i->active_state, i->sub_state) ? NULL : i->sub_state;
1797 if (streq_ptr(i->active_state, "failed")) {
1798 on = ansi_highlight(true);
1799 off = ansi_highlight(false);
1800 } else if (streq_ptr(i->active_state, "active") || streq_ptr(i->active_state, "reloading")) {
1801 on = ansi_highlight_green(true);
1802 off = ansi_highlight_green(false);
1807 printf("\t Active: %s%s (%s)%s",
1809 strna(i->active_state),
1813 printf("\t Active: %s%s%s",
1815 strna(i->active_state),
1818 timestamp = (streq_ptr(i->active_state, "active") ||
1819 streq_ptr(i->active_state, "reloading")) ? i->active_enter_timestamp :
1820 (streq_ptr(i->active_state, "inactive") ||
1821 streq_ptr(i->active_state, "failed")) ? i->inactive_enter_timestamp :
1822 streq_ptr(i->active_state, "activating") ? i->inactive_exit_timestamp :
1823 i->active_exit_timestamp;
1825 s1 = format_timestamp_pretty(since1, sizeof(since1), timestamp);
1826 s2 = format_timestamp(since2, sizeof(since2), timestamp);
1829 printf(" since %s; %s\n", s2, s1);
1831 printf(" since %s\n", s2);
1836 printf("\t Device: %s\n", i->sysfs_path);
1838 printf("\t Where: %s\n", i->where);
1840 printf("\t What: %s\n", i->what);
1843 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
1845 LIST_FOREACH(exec, p, i->exec) {
1848 /* Only show exited processes here */
1852 t = strv_join(p->argv, " ");
1853 printf("\t Process: %u (%s, code=%s, ", p->pid, strna(t), sigchld_code_to_string(p->code));
1856 if (p->code == CLD_EXITED) {
1859 printf("status=%i", p->status);
1861 #ifdef HAVE_SYSV_COMPAT
1862 if ((c = exit_status_to_string(p->status, i->is_sysv ? EXIT_STATUS_LSB : EXIT_STATUS_SYSTEMD)))
1864 if ((c = exit_status_to_string(p->status, EXIT_STATUS_SYSTEMD)))
1869 printf("signal=%s", signal_to_string(p->status));
1872 if (i->main_pid == p->pid &&
1873 i->start_timestamp == p->start_timestamp &&
1874 i->exit_timestamp == p->start_timestamp)
1875 /* Let's not show this twice */
1878 if (p->pid == i->control_pid)
1882 if (i->main_pid > 0 || i->control_pid > 0) {
1885 if (i->main_pid > 0) {
1886 printf("Main PID: %u", (unsigned) i->main_pid);
1890 get_process_name(i->main_pid, &t);
1895 } else if (i->exit_code > 0) {
1896 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
1898 if (i->exit_code == CLD_EXITED) {
1901 printf("status=%i", i->exit_status);
1903 #ifdef HAVE_SYSV_COMPAT
1904 if ((c = exit_status_to_string(i->exit_status, i->is_sysv ? EXIT_STATUS_LSB : EXIT_STATUS_SYSTEMD)))
1906 if ((c = exit_status_to_string(i->exit_status, EXIT_STATUS_SYSTEMD)))
1911 printf("signal=%s", signal_to_string(i->exit_status));
1916 if (i->main_pid > 0 && i->control_pid > 0)
1919 if (i->control_pid > 0) {
1922 printf(" Control: %u", (unsigned) i->control_pid);
1924 get_process_name(i->control_pid, &t);
1935 printf("\t Status: \"%s\"\n", i->status_text);
1937 if (i->default_control_group) {
1940 printf("\t CGroup: %s\n", i->default_control_group);
1942 if ((c = columns()) > 18)
1947 show_cgroup_by_path(i->default_control_group, "\t\t ", c);
1950 if (i->need_daemon_reload)
1951 printf("\n%sWarning:%s Unit file changed on disk, 'systemctl %s daemon-reload' recommended.\n",
1952 ansi_highlight(true),
1953 ansi_highlight(false),
1954 arg_user ? "--user" : "--system");
1957 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
1959 switch (dbus_message_iter_get_arg_type(iter)) {
1961 case DBUS_TYPE_STRING: {
1964 dbus_message_iter_get_basic(iter, &s);
1967 if (streq(name, "Id"))
1969 else if (streq(name, "LoadState"))
1971 else if (streq(name, "ActiveState"))
1972 i->active_state = s;
1973 else if (streq(name, "SubState"))
1975 else if (streq(name, "Description"))
1977 else if (streq(name, "FragmentPath"))
1979 #ifdef HAVE_SYSV_COMPAT
1980 else if (streq(name, "SysVPath")) {
1985 else if (streq(name, "DefaultControlGroup"))
1986 i->default_control_group = s;
1987 else if (streq(name, "StatusText"))
1989 else if (streq(name, "SysFSPath"))
1991 else if (streq(name, "Where"))
1993 else if (streq(name, "What"))
1995 else if (streq(name, "Following"))
2002 case DBUS_TYPE_BOOLEAN: {
2005 dbus_message_iter_get_basic(iter, &b);
2007 if (streq(name, "Accept"))
2009 else if (streq(name, "NeedDaemonReload"))
2010 i->need_daemon_reload = b;
2015 case DBUS_TYPE_UINT32: {
2018 dbus_message_iter_get_basic(iter, &u);
2020 if (streq(name, "MainPID")) {
2022 i->main_pid = (pid_t) u;
2025 } else if (streq(name, "ControlPID"))
2026 i->control_pid = (pid_t) u;
2027 else if (streq(name, "ExecMainPID")) {
2029 i->main_pid = (pid_t) u;
2030 } else if (streq(name, "NAccepted"))
2032 else if (streq(name, "NConnections"))
2033 i->n_connections = u;
2038 case DBUS_TYPE_INT32: {
2041 dbus_message_iter_get_basic(iter, &j);
2043 if (streq(name, "ExecMainCode"))
2044 i->exit_code = (int) j;
2045 else if (streq(name, "ExecMainStatus"))
2046 i->exit_status = (int) j;
2051 case DBUS_TYPE_UINT64: {
2054 dbus_message_iter_get_basic(iter, &u);
2056 if (streq(name, "ExecMainStartTimestamp"))
2057 i->start_timestamp = (usec_t) u;
2058 else if (streq(name, "ExecMainExitTimestamp"))
2059 i->exit_timestamp = (usec_t) u;
2060 else if (streq(name, "ActiveEnterTimestamp"))
2061 i->active_enter_timestamp = (usec_t) u;
2062 else if (streq(name, "InactiveEnterTimestamp"))
2063 i->inactive_enter_timestamp = (usec_t) u;
2064 else if (streq(name, "InactiveExitTimestamp"))
2065 i->inactive_exit_timestamp = (usec_t) u;
2066 else if (streq(name, "ActiveExitTimestamp"))
2067 i->active_exit_timestamp = (usec_t) u;
2072 case DBUS_TYPE_ARRAY: {
2074 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT &&
2075 startswith(name, "Exec")) {
2076 DBusMessageIter sub;
2078 dbus_message_iter_recurse(iter, &sub);
2079 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2080 ExecStatusInfo *info;
2083 if (!(info = new0(ExecStatusInfo, 1)))
2086 if ((r = exec_status_info_deserialize(&sub, info)) < 0) {
2091 LIST_PREPEND(ExecStatusInfo, exec, i->exec, info);
2093 dbus_message_iter_next(&sub);
2104 static int print_property(const char *name, DBusMessageIter *iter) {
2108 /* This is a low-level property printer, see
2109 * print_status_info() for the nicer output */
2111 if (arg_property && !strv_find(arg_property, name))
2114 switch (dbus_message_iter_get_arg_type(iter)) {
2116 case DBUS_TYPE_STRING: {
2118 dbus_message_iter_get_basic(iter, &s);
2120 if (arg_all || s[0])
2121 printf("%s=%s\n", name, s);
2126 case DBUS_TYPE_BOOLEAN: {
2128 dbus_message_iter_get_basic(iter, &b);
2129 printf("%s=%s\n", name, yes_no(b));
2134 case DBUS_TYPE_UINT64: {
2136 dbus_message_iter_get_basic(iter, &u);
2138 /* Yes, heuristics! But we can change this check
2139 * should it turn out to not be sufficient */
2141 if (strstr(name, "Timestamp")) {
2142 char timestamp[FORMAT_TIMESTAMP_MAX], *t;
2144 if ((t = format_timestamp(timestamp, sizeof(timestamp), u)) || arg_all)
2145 printf("%s=%s\n", name, strempty(t));
2146 } else if (strstr(name, "USec")) {
2147 char timespan[FORMAT_TIMESPAN_MAX];
2149 printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u));
2151 printf("%s=%llu\n", name, (unsigned long long) u);
2156 case DBUS_TYPE_UINT32: {
2158 dbus_message_iter_get_basic(iter, &u);
2160 if (strstr(name, "UMask") || strstr(name, "Mode"))
2161 printf("%s=%04o\n", name, u);
2163 printf("%s=%u\n", name, (unsigned) u);
2168 case DBUS_TYPE_INT32: {
2170 dbus_message_iter_get_basic(iter, &i);
2172 printf("%s=%i\n", name, (int) i);
2176 case DBUS_TYPE_DOUBLE: {
2178 dbus_message_iter_get_basic(iter, &d);
2180 printf("%s=%g\n", name, d);
2184 case DBUS_TYPE_STRUCT: {
2185 DBusMessageIter sub;
2186 dbus_message_iter_recurse(iter, &sub);
2188 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
2191 dbus_message_iter_get_basic(&sub, &u);
2194 printf("%s=%u\n", name, (unsigned) u);
2196 printf("%s=\n", name);
2199 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
2202 dbus_message_iter_get_basic(&sub, &s);
2204 if (arg_all || s[0])
2205 printf("%s=%s\n", name, s);
2213 case DBUS_TYPE_ARRAY:
2215 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
2216 DBusMessageIter sub;
2219 dbus_message_iter_recurse(iter, &sub);
2221 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2222 printf("%s=", name);
2224 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2227 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
2228 dbus_message_iter_get_basic(&sub, &s);
2229 printf("%s%s", space ? " " : "", s);
2232 dbus_message_iter_next(&sub);
2240 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_BYTE) {
2241 DBusMessageIter sub;
2243 dbus_message_iter_recurse(iter, &sub);
2245 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2246 printf("%s=", name);
2248 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2251 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_BYTE);
2252 dbus_message_iter_get_basic(&sub, &u);
2255 dbus_message_iter_next(&sub);
2263 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
2264 DBusMessageIter sub, sub2;
2266 dbus_message_iter_recurse(iter, &sub);
2267 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2268 const char *type, *path;
2270 dbus_message_iter_recurse(&sub, &sub2);
2272 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
2273 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
2274 printf("%s=%s\n", type, path);
2276 dbus_message_iter_next(&sub);
2281 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
2282 DBusMessageIter sub, sub2;
2284 dbus_message_iter_recurse(iter, &sub);
2285 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2287 uint64_t value, next_elapse;
2289 dbus_message_iter_recurse(&sub, &sub2);
2291 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
2292 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
2293 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
2294 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
2296 printf("%s={ value=%s ; next_elapse=%s }\n",
2298 format_timespan(timespan1, sizeof(timespan1), value),
2299 format_timespan(timespan2, sizeof(timespan2), next_elapse));
2302 dbus_message_iter_next(&sub);
2307 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
2308 DBusMessageIter sub;
2310 dbus_message_iter_recurse(iter, &sub);
2311 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2312 ExecStatusInfo info;
2315 if (exec_status_info_deserialize(&sub, &info) >= 0) {
2316 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
2319 t = strv_join(info.argv, " ");
2321 printf("%s={ path=%s ; argv[]=%s ; ignore=%s ; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n",
2325 yes_no(info.ignore),
2326 strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)),
2327 strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)),
2328 (unsigned) info. pid,
2329 sigchld_code_to_string(info.code),
2331 info.code == CLD_EXITED ? "" : "/",
2332 strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status)));
2338 strv_free(info.argv);
2340 dbus_message_iter_next(&sub);
2350 printf("%s=[unprintable]\n", name);
2355 static int show_one(const char *verb, DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
2356 DBusMessage *m = NULL, *reply = NULL;
2357 const char *interface = "";
2360 DBusMessageIter iter, sub, sub2, sub3;
2361 UnitStatusInfo info;
2369 dbus_error_init(&error);
2371 if (!(m = dbus_message_new_method_call(
2372 "org.freedesktop.systemd1",
2374 "org.freedesktop.DBus.Properties",
2376 log_error("Could not allocate message.");
2381 if (!dbus_message_append_args(m,
2382 DBUS_TYPE_STRING, &interface,
2383 DBUS_TYPE_INVALID)) {
2384 log_error("Could not append arguments to message.");
2389 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2390 log_error("Failed to issue method call: %s", bus_error_message(&error));
2395 if (!dbus_message_iter_init(reply, &iter) ||
2396 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
2397 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
2398 log_error("Failed to parse reply.");
2403 dbus_message_iter_recurse(&iter, &sub);
2410 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2413 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
2414 log_error("Failed to parse reply.");
2419 dbus_message_iter_recurse(&sub, &sub2);
2421 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
2422 log_error("Failed to parse reply.");
2427 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
2428 log_error("Failed to parse reply.");
2433 dbus_message_iter_recurse(&sub2, &sub3);
2435 if (show_properties)
2436 r = print_property(name, &sub3);
2438 r = status_property(name, &sub3, &info);
2441 log_error("Failed to parse reply.");
2446 dbus_message_iter_next(&sub);
2451 if (!show_properties)
2452 print_status_info(&info);
2454 if (!streq_ptr(info.active_state, "active") &&
2455 !streq_ptr(info.active_state, "reloading") &&
2456 streq(verb, "status"))
2457 /* According to LSB: "program not running" */
2460 while ((p = info.exec)) {
2461 LIST_REMOVE(ExecStatusInfo, exec, info.exec, p);
2462 exec_status_info_free(p);
2467 dbus_message_unref(m);
2470 dbus_message_unref(reply);
2472 dbus_error_free(&error);
2477 static int show(DBusConnection *bus, char **args, unsigned n) {
2478 DBusMessage *m = NULL, *reply = NULL;
2482 bool show_properties, new_line = false;
2487 dbus_error_init(&error);
2489 show_properties = !streq(args[0], "status");
2491 if (show_properties)
2494 if (show_properties && n <= 1) {
2495 /* If not argument is specified inspect the manager
2498 ret = show_one(args[0], bus, "/org/freedesktop/systemd1", show_properties, &new_line);
2502 for (i = 1; i < n; i++) {
2503 const char *path = NULL;
2506 if (safe_atou32(args[i], &id) < 0) {
2508 /* Interpret as unit name */
2510 if (!(m = dbus_message_new_method_call(
2511 "org.freedesktop.systemd1",
2512 "/org/freedesktop/systemd1",
2513 "org.freedesktop.systemd1.Manager",
2515 log_error("Could not allocate message.");
2520 if (!dbus_message_append_args(m,
2521 DBUS_TYPE_STRING, &args[i],
2522 DBUS_TYPE_INVALID)) {
2523 log_error("Could not append arguments to message.");
2528 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2530 if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
2531 log_error("Failed to issue method call: %s", bus_error_message(&error));
2536 dbus_error_free(&error);
2538 dbus_message_unref(m);
2539 if (!(m = dbus_message_new_method_call(
2540 "org.freedesktop.systemd1",
2541 "/org/freedesktop/systemd1",
2542 "org.freedesktop.systemd1.Manager",
2544 log_error("Could not allocate message.");
2549 if (!dbus_message_append_args(m,
2550 DBUS_TYPE_STRING, &args[i],
2551 DBUS_TYPE_INVALID)) {
2552 log_error("Could not append arguments to message.");
2557 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2558 log_error("Failed to issue method call: %s", bus_error_message(&error));
2560 if (dbus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT))
2561 ret = 4; /* According to LSB: "program or service status is unknown" */
2568 } else if (show_properties) {
2570 /* Interpret as job id */
2572 if (!(m = dbus_message_new_method_call(
2573 "org.freedesktop.systemd1",
2574 "/org/freedesktop/systemd1",
2575 "org.freedesktop.systemd1.Manager",
2577 log_error("Could not allocate message.");
2582 if (!dbus_message_append_args(m,
2583 DBUS_TYPE_UINT32, &id,
2584 DBUS_TYPE_INVALID)) {
2585 log_error("Could not append arguments to message.");
2590 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2591 log_error("Failed to issue method call: %s", bus_error_message(&error));
2597 /* Interpret as PID */
2599 if (!(m = dbus_message_new_method_call(
2600 "org.freedesktop.systemd1",
2601 "/org/freedesktop/systemd1",
2602 "org.freedesktop.systemd1.Manager",
2604 log_error("Could not allocate message.");
2609 if (!dbus_message_append_args(m,
2610 DBUS_TYPE_UINT32, &id,
2611 DBUS_TYPE_INVALID)) {
2612 log_error("Could not append arguments to message.");
2617 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2618 log_error("Failed to issue method call: %s", bus_error_message(&error));
2624 if (!dbus_message_get_args(reply, &error,
2625 DBUS_TYPE_OBJECT_PATH, &path,
2626 DBUS_TYPE_INVALID)) {
2627 log_error("Failed to parse reply: %s", bus_error_message(&error));
2632 if ((r = show_one(args[0], bus, path, show_properties, &new_line)) != 0)
2635 dbus_message_unref(m);
2636 dbus_message_unref(reply);
2642 dbus_message_unref(m);
2645 dbus_message_unref(reply);
2647 dbus_error_free(&error);
2652 static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
2654 DBusMessage *m = NULL, *reply = NULL;
2659 dbus_error_init(&error);
2661 log_debug("Got D-Bus request: %s.%s() on %s",
2662 dbus_message_get_interface(message),
2663 dbus_message_get_member(message),
2664 dbus_message_get_path(message));
2666 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
2667 log_error("Warning! D-Bus connection terminated.");
2668 dbus_connection_close(connection);
2670 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") ||
2671 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
2672 const char *id, *path;
2674 if (!dbus_message_get_args(message, &error,
2675 DBUS_TYPE_STRING, &id,
2676 DBUS_TYPE_OBJECT_PATH, &path,
2678 log_error("Failed to parse message: %s", bus_error_message(&error));
2679 else if (streq(dbus_message_get_member(message), "UnitNew"))
2680 printf("Unit %s added.\n", id);
2682 printf("Unit %s removed.\n", id);
2684 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew") ||
2685 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
2689 if (!dbus_message_get_args(message, &error,
2690 DBUS_TYPE_UINT32, &id,
2691 DBUS_TYPE_OBJECT_PATH, &path,
2693 log_error("Failed to parse message: %s", bus_error_message(&error));
2694 else if (streq(dbus_message_get_member(message), "JobNew"))
2695 printf("Job %u added.\n", id);
2697 printf("Job %u removed.\n", id);
2700 } else if (dbus_message_is_signal(message, "org.freedesktop.DBus.Properties", "PropertiesChanged")) {
2702 const char *path, *interface, *property = "Id";
2703 DBusMessageIter iter, sub;
2705 path = dbus_message_get_path(message);
2707 if (!dbus_message_get_args(message, &error,
2708 DBUS_TYPE_STRING, &interface,
2709 DBUS_TYPE_INVALID)) {
2710 log_error("Failed to parse message: %s", bus_error_message(&error));
2714 if (!streq(interface, "org.freedesktop.systemd1.Job") &&
2715 !streq(interface, "org.freedesktop.systemd1.Unit"))
2718 if (!(m = dbus_message_new_method_call(
2719 "org.freedesktop.systemd1",
2721 "org.freedesktop.DBus.Properties",
2723 log_error("Could not allocate message.");
2727 if (!dbus_message_append_args(m,
2728 DBUS_TYPE_STRING, &interface,
2729 DBUS_TYPE_STRING, &property,
2730 DBUS_TYPE_INVALID)) {
2731 log_error("Could not append arguments to message.");
2735 if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
2736 log_error("Failed to issue method call: %s", bus_error_message(&error));
2740 if (!dbus_message_iter_init(reply, &iter) ||
2741 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2742 log_error("Failed to parse reply.");
2746 dbus_message_iter_recurse(&iter, &sub);
2748 if (streq(interface, "org.freedesktop.systemd1.Unit")) {
2751 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2752 log_error("Failed to parse reply.");
2756 dbus_message_iter_get_basic(&sub, &id);
2757 printf("Unit %s changed.\n", id);
2761 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32) {
2762 log_error("Failed to parse reply.");
2766 dbus_message_iter_get_basic(&sub, &id);
2767 printf("Job %u changed.\n", id);
2773 dbus_message_unref(m);
2776 dbus_message_unref(reply);
2778 dbus_error_free(&error);
2779 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2783 dbus_message_unref(m);
2786 dbus_message_unref(reply);
2788 dbus_error_free(&error);
2789 return DBUS_HANDLER_RESULT_NEED_MEMORY;
2792 static int monitor(DBusConnection *bus, char **args, unsigned n) {
2793 DBusMessage *m = NULL, *reply = NULL;
2797 dbus_error_init(&error);
2800 dbus_bus_add_match(bus,
2802 "sender='org.freedesktop.systemd1',"
2803 "interface='org.freedesktop.systemd1.Manager',"
2804 "path='/org/freedesktop/systemd1'",
2807 if (dbus_error_is_set(&error)) {
2808 log_error("Failed to add match: %s", bus_error_message(&error));
2813 dbus_bus_add_match(bus,
2815 "sender='org.freedesktop.systemd1',"
2816 "interface='org.freedesktop.DBus.Properties',"
2817 "member='PropertiesChanged'",
2820 if (dbus_error_is_set(&error)) {
2821 log_error("Failed to add match: %s", bus_error_message(&error));
2827 if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
2828 log_error("Failed to add filter.");
2833 if (!(m = dbus_message_new_method_call(
2834 "org.freedesktop.systemd1",
2835 "/org/freedesktop/systemd1",
2836 "org.freedesktop.systemd1.Manager",
2838 log_error("Could not allocate message.");
2843 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2844 log_error("Failed to issue method call: %s", bus_error_message(&error));
2849 while (dbus_connection_read_write_dispatch(bus, -1))
2856 /* This is slightly dirty, since we don't undo the filter or the matches. */
2859 dbus_message_unref(m);
2862 dbus_message_unref(reply);
2864 dbus_error_free(&error);
2869 static int dump(DBusConnection *bus, char **args, unsigned n) {
2870 DBusMessage *m = NULL, *reply = NULL;
2875 dbus_error_init(&error);
2879 if (!(m = dbus_message_new_method_call(
2880 "org.freedesktop.systemd1",
2881 "/org/freedesktop/systemd1",
2882 "org.freedesktop.systemd1.Manager",
2884 log_error("Could not allocate message.");
2888 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2889 log_error("Failed to issue method call: %s", bus_error_message(&error));
2894 if (!dbus_message_get_args(reply, &error,
2895 DBUS_TYPE_STRING, &text,
2896 DBUS_TYPE_INVALID)) {
2897 log_error("Failed to parse reply: %s", bus_error_message(&error));
2902 fputs(text, stdout);
2908 dbus_message_unref(m);
2911 dbus_message_unref(reply);
2913 dbus_error_free(&error);
2918 static int snapshot(DBusConnection *bus, char **args, unsigned n) {
2919 DBusMessage *m = NULL, *reply = NULL;
2922 const char *name = "", *path, *id;
2923 dbus_bool_t cleanup = FALSE;
2924 DBusMessageIter iter, sub;
2926 *interface = "org.freedesktop.systemd1.Unit",
2929 dbus_error_init(&error);
2931 if (!(m = dbus_message_new_method_call(
2932 "org.freedesktop.systemd1",
2933 "/org/freedesktop/systemd1",
2934 "org.freedesktop.systemd1.Manager",
2935 "CreateSnapshot"))) {
2936 log_error("Could not allocate message.");
2943 if (!dbus_message_append_args(m,
2944 DBUS_TYPE_STRING, &name,
2945 DBUS_TYPE_BOOLEAN, &cleanup,
2946 DBUS_TYPE_INVALID)) {
2947 log_error("Could not append arguments to message.");
2952 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2953 log_error("Failed to issue method call: %s", bus_error_message(&error));
2958 if (!dbus_message_get_args(reply, &error,
2959 DBUS_TYPE_OBJECT_PATH, &path,
2960 DBUS_TYPE_INVALID)) {
2961 log_error("Failed to parse reply: %s", bus_error_message(&error));
2966 dbus_message_unref(m);
2967 if (!(m = dbus_message_new_method_call(
2968 "org.freedesktop.systemd1",
2970 "org.freedesktop.DBus.Properties",
2972 log_error("Could not allocate message.");
2976 if (!dbus_message_append_args(m,
2977 DBUS_TYPE_STRING, &interface,
2978 DBUS_TYPE_STRING, &property,
2979 DBUS_TYPE_INVALID)) {
2980 log_error("Could not append arguments to message.");
2985 dbus_message_unref(reply);
2986 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2987 log_error("Failed to issue method call: %s", bus_error_message(&error));
2992 if (!dbus_message_iter_init(reply, &iter) ||
2993 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2994 log_error("Failed to parse reply.");
2999 dbus_message_iter_recurse(&iter, &sub);
3001 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
3002 log_error("Failed to parse reply.");
3007 dbus_message_iter_get_basic(&sub, &id);
3015 dbus_message_unref(m);
3018 dbus_message_unref(reply);
3020 dbus_error_free(&error);
3025 static int delete_snapshot(DBusConnection *bus, char **args, unsigned n) {
3026 DBusMessage *m = NULL, *reply = NULL;
3034 dbus_error_init(&error);
3036 for (i = 1; i < n; i++) {
3037 const char *path = NULL;
3039 if (!(m = dbus_message_new_method_call(
3040 "org.freedesktop.systemd1",
3041 "/org/freedesktop/systemd1",
3042 "org.freedesktop.systemd1.Manager",
3044 log_error("Could not allocate message.");
3049 if (!dbus_message_append_args(m,
3050 DBUS_TYPE_STRING, &args[i],
3051 DBUS_TYPE_INVALID)) {
3052 log_error("Could not append arguments to message.");
3057 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3058 log_error("Failed to issue method call: %s", bus_error_message(&error));
3063 if (!dbus_message_get_args(reply, &error,
3064 DBUS_TYPE_OBJECT_PATH, &path,
3065 DBUS_TYPE_INVALID)) {
3066 log_error("Failed to parse reply: %s", bus_error_message(&error));
3071 dbus_message_unref(m);
3072 if (!(m = dbus_message_new_method_call(
3073 "org.freedesktop.systemd1",
3075 "org.freedesktop.systemd1.Snapshot",
3077 log_error("Could not allocate message.");
3082 dbus_message_unref(reply);
3083 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3084 log_error("Failed to issue method call: %s", bus_error_message(&error));
3089 dbus_message_unref(m);
3090 dbus_message_unref(reply);
3098 dbus_message_unref(m);
3101 dbus_message_unref(reply);
3103 dbus_error_free(&error);
3108 static int daemon_reload(DBusConnection *bus, char **args, unsigned n) {
3109 DBusMessage *m = NULL, *reply = NULL;
3114 dbus_error_init(&error);
3116 if (arg_action == ACTION_RELOAD)
3118 else if (arg_action == ACTION_REEXEC)
3119 method = "Reexecute";
3121 assert(arg_action == ACTION_SYSTEMCTL);
3124 streq(args[0], "clear-jobs") ||
3125 streq(args[0], "cancel") ? "ClearJobs" :
3126 streq(args[0], "daemon-reexec") ? "Reexecute" :
3127 streq(args[0], "reset-failed") ? "ResetFailed" :
3128 streq(args[0], "halt") ? "Halt" :
3129 streq(args[0], "poweroff") ? "PowerOff" :
3130 streq(args[0], "reboot") ? "Reboot" :
3131 streq(args[0], "kexec") ? "KExec" :
3132 streq(args[0], "exit") ? "Exit" :
3133 /* "daemon-reload" */ "Reload";
3136 if (!(m = dbus_message_new_method_call(
3137 "org.freedesktop.systemd1",
3138 "/org/freedesktop/systemd1",
3139 "org.freedesktop.systemd1.Manager",
3141 log_error("Could not allocate message.");
3145 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3147 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
3148 /* There's always a fallback possible for
3149 * legacy actions. */
3154 log_error("Failed to issue method call: %s", bus_error_message(&error));
3163 dbus_message_unref(m);
3166 dbus_message_unref(reply);
3168 dbus_error_free(&error);
3173 static int reset_failed(DBusConnection *bus, char **args, unsigned n) {
3174 DBusMessage *m = NULL, *reply = NULL;
3180 dbus_error_init(&error);
3183 return daemon_reload(bus, args, n);
3185 for (i = 1; i < n; i++) {
3187 if (!(m = dbus_message_new_method_call(
3188 "org.freedesktop.systemd1",
3189 "/org/freedesktop/systemd1",
3190 "org.freedesktop.systemd1.Manager",
3191 "ResetFailedUnit"))) {
3192 log_error("Could not allocate message.");
3197 if (!dbus_message_append_args(m,
3198 DBUS_TYPE_STRING, args + i,
3199 DBUS_TYPE_INVALID)) {
3200 log_error("Could not append arguments to message.");
3205 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3206 log_error("Failed to issue method call: %s", bus_error_message(&error));
3211 dbus_message_unref(m);
3212 dbus_message_unref(reply);
3220 dbus_message_unref(m);
3223 dbus_message_unref(reply);
3225 dbus_error_free(&error);
3230 static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
3231 DBusMessage *m = NULL, *reply = NULL;
3233 DBusMessageIter iter, sub, sub2;
3236 *interface = "org.freedesktop.systemd1.Manager",
3237 *property = "Environment";
3239 dbus_error_init(&error);
3243 if (!(m = dbus_message_new_method_call(
3244 "org.freedesktop.systemd1",
3245 "/org/freedesktop/systemd1",
3246 "org.freedesktop.DBus.Properties",
3248 log_error("Could not allocate message.");
3252 if (!dbus_message_append_args(m,
3253 DBUS_TYPE_STRING, &interface,
3254 DBUS_TYPE_STRING, &property,
3255 DBUS_TYPE_INVALID)) {
3256 log_error("Could not append arguments to message.");
3261 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3262 log_error("Failed to issue method call: %s", bus_error_message(&error));
3267 if (!dbus_message_iter_init(reply, &iter) ||
3268 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
3269 log_error("Failed to parse reply.");
3274 dbus_message_iter_recurse(&iter, &sub);
3276 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
3277 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
3278 log_error("Failed to parse reply.");
3283 dbus_message_iter_recurse(&sub, &sub2);
3285 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
3288 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
3289 log_error("Failed to parse reply.");
3294 dbus_message_iter_get_basic(&sub2, &text);
3295 printf("%s\n", text);
3297 dbus_message_iter_next(&sub2);
3304 dbus_message_unref(m);
3307 dbus_message_unref(reply);
3309 dbus_error_free(&error);
3314 static int set_environment(DBusConnection *bus, char **args, unsigned n) {
3315 DBusMessage *m = NULL, *reply = NULL;
3319 DBusMessageIter iter, sub;
3322 dbus_error_init(&error);
3324 method = streq(args[0], "set-environment")
3326 : "UnsetEnvironment";
3328 if (!(m = dbus_message_new_method_call(
3329 "org.freedesktop.systemd1",
3330 "/org/freedesktop/systemd1",
3331 "org.freedesktop.systemd1.Manager",
3334 log_error("Could not allocate message.");
3338 dbus_message_iter_init_append(m, &iter);
3340 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
3341 log_error("Could not append arguments to message.");
3346 for (i = 1; i < n; i++)
3347 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) {
3348 log_error("Could not append arguments to message.");
3353 if (!dbus_message_iter_close_container(&iter, &sub)) {
3354 log_error("Could not append arguments to message.");
3359 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3360 log_error("Failed to issue method call: %s", bus_error_message(&error));
3369 dbus_message_unref(m);
3372 dbus_message_unref(reply);
3374 dbus_error_free(&error);
3387 static Hashmap *will_install = NULL, *have_installed = NULL;
3388 static Set *remove_symlinks_to = NULL;
3389 static unsigned n_symlinks = 0;
3391 static void install_info_free(InstallInfo *i) {
3396 strv_free(i->aliases);
3397 strv_free(i->wanted_by);
3401 static void install_info_hashmap_free(Hashmap *m) {
3404 while ((i = hashmap_steal_first(m)))
3405 install_info_free(i);
3410 static int install_info_add(const char *name) {
3414 assert(will_install);
3416 if (!unit_name_is_valid_no_type(name, true)) {
3417 log_warning("Unit name %s is not a valid unit name.", name);
3421 if (hashmap_get(have_installed, name) ||
3422 hashmap_get(will_install, name))
3425 if (!(i = new0(InstallInfo, 1))) {
3430 if (!(i->name = strdup(name))) {
3435 if ((r = hashmap_put(will_install, i->name, i)) < 0)
3442 install_info_free(i);
3447 static int config_parse_also(
3448 const char *filename,
3450 const char *section,
3464 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
3468 if (!(n = strndup(w, l)))
3471 if ((r = install_info_add(n)) < 0) {
3472 log_warning("Cannot install unit %s: %s", n, strerror(-r));
3483 static int mark_symlink_for_removal(const char *p) {
3488 assert(path_is_absolute(p));
3490 if (!remove_symlinks_to)
3493 if (!(n = strdup(p)))
3496 path_kill_slashes(n);
3498 if ((r = set_put(remove_symlinks_to, n)) < 0) {
3500 return r == -EEXIST ? 0 : r;
3506 static int remove_marked_symlinks_fd(int fd, const char *config_path, const char *root, bool *deleted) {
3515 if (!(d = fdopendir(fd))) {
3516 close_nointr_nofail(fd);
3522 while ((de = readdir(d))) {
3523 bool is_dir = false, is_link = false;
3525 if (ignore_file(de->d_name))
3528 if (de->d_type == DT_LNK)
3530 else if (de->d_type == DT_DIR)
3532 else if (de->d_type == DT_UNKNOWN) {
3535 if (fstatat(fd, de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) {
3536 log_error("Failed to stat %s/%s: %m", root, de->d_name);
3543 is_link = S_ISLNK(st.st_mode);
3544 is_dir = S_ISDIR(st.st_mode);
3552 if ((nfd = openat(fd, de->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW)) < 0) {
3553 log_error("Failed to open %s/%s: %m", root, de->d_name);
3560 if (asprintf(&p, "%s/%s", root, de->d_name) < 0) {
3561 log_error("Failed to allocate directory string.");
3562 close_nointr_nofail(nfd);
3567 /* This will close nfd, regardless whether it succeeds or not */
3568 q = remove_marked_symlinks_fd(nfd, config_path, p, deleted);
3574 } else if (is_link) {
3578 if (asprintf(&p, "%s/%s", root, de->d_name) < 0) {
3579 log_error("Failed to allocate symlink string.");
3584 if ((q = readlink_and_make_absolute(p, &dest)) < 0) {
3585 log_error("Cannot read symlink %s: %s", p, strerror(-q));
3593 if ((c = canonicalize_file_name(dest))) {
3594 /* This might fail if the destination
3595 * is already removed */
3601 path_kill_slashes(dest);
3602 if (set_get(remove_symlinks_to, dest)) {
3605 log_info("rm '%s'", p);
3607 if (unlink(p) < 0) {
3608 log_error("Cannot unlink symlink %s: %m", p);
3613 rmdir_parents(p, config_path);
3614 path_kill_slashes(p);
3616 if (!set_get(remove_symlinks_to, p)) {
3618 if ((r = mark_symlink_for_removal(p)) < 0) {
3637 static int remove_marked_symlinks(const char *config_path) {
3641 assert(config_path);
3643 if (set_size(remove_symlinks_to) <= 0)
3646 if ((fd = open(config_path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW)) < 0)
3653 if ((cfd = dup(fd)) < 0) {
3658 /* This takes possession of cfd and closes it */
3659 if ((q = remove_marked_symlinks_fd(cfd, config_path, config_path, &deleted)) < 0) {
3665 close_nointr_nofail(fd);
3670 static int create_symlink(const char *verb, const char *old_path, const char *new_path) {
3677 if (streq(verb, "enable")) {
3680 mkdir_parents(new_path, 0755);
3682 if (symlink(old_path, new_path) >= 0) {
3685 log_info("ln -s '%s' '%s'", old_path, new_path);
3690 if (errno != EEXIST) {
3691 log_error("Cannot link %s to %s: %m", old_path, new_path);
3695 if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
3697 if (errno == EINVAL) {
3698 log_error("Cannot link %s to %s, file exists already and is not a symlink.", old_path, new_path);
3702 log_error("readlink() failed: %s", strerror(-r));
3706 if (streq(dest, old_path)) {
3712 log_error("Cannot link %s to %s, symlink exists already and points to %s.", old_path, new_path, dest);
3721 log_info("ln -s '%s' '%s'", old_path, new_path);
3723 if (symlink(old_path, new_path) >= 0)
3726 log_error("Cannot link %s to %s: %m", old_path, new_path);
3729 } else if (streq(verb, "disable")) {
3732 if ((r = mark_symlink_for_removal(old_path)) < 0)
3735 if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
3736 if (errno == ENOENT)
3739 if (errno == EINVAL) {
3740 log_warning("File %s not a symlink, ignoring.", old_path);
3744 log_error("readlink() failed: %s", strerror(-r));
3748 if (!streq(dest, old_path)) {
3749 log_warning("File %s not a symlink to %s but points to %s, ignoring.", new_path, old_path, dest);
3756 if ((r = mark_symlink_for_removal(new_path)) < 0)
3760 log_info("rm '%s'", new_path);
3762 if (unlink(new_path) >= 0)
3765 log_error("Cannot unlink %s: %m", new_path);
3768 } else if (streq(verb, "is-enabled")) {
3771 if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
3773 if (errno == ENOENT || errno == EINVAL)
3776 log_error("readlink() failed: %s", strerror(-r));
3780 if (streq(dest, old_path)) {
3788 assert_not_reached("Unknown action.");
3791 static int install_info_symlink_alias(const char *verb, InstallInfo *i, const char *config_path) {
3793 char *alias_path = NULL;
3798 assert(config_path);
3800 STRV_FOREACH(s, i->aliases) {
3803 if (!(alias_path = path_make_absolute(*s, config_path))) {
3804 log_error("Out of memory");
3809 if ((r = create_symlink(verb, i->path, alias_path)) != 0)
3812 if (streq(verb, "disable"))
3813 rmdir_parents(alias_path, config_path);
3823 static int install_info_symlink_wants(const char *verb, InstallInfo *i, const char *config_path) {
3825 char *alias_path = NULL;
3830 assert(config_path);
3832 STRV_FOREACH(s, i->wanted_by) {
3833 if (!unit_name_is_valid_no_type(*s, true)) {
3834 log_error("Invalid name %s.", *s);
3842 if (asprintf(&alias_path, "%s/%s.wants/%s", config_path, *s, i->name) < 0) {
3843 log_error("Out of memory");
3848 if ((r = create_symlink(verb, i->path, alias_path)) != 0)
3851 if (streq(verb, "disable"))
3852 rmdir_parents(alias_path, config_path);
3863 static int install_info_apply(const char *verb, LookupPaths *paths, InstallInfo *i, const char *config_path) {
3865 const ConfigItem items[] = {
3866 { "Alias", config_parse_strv, &i->aliases, "Install" },
3867 { "WantedBy", config_parse_strv, &i->wanted_by, "Install" },
3868 { "Also", config_parse_also, NULL, "Install" },
3870 { NULL, NULL, NULL, NULL }
3874 char *filename = NULL;
3881 STRV_FOREACH(p, paths->unit_path) {
3884 if (!(filename = path_make_absolute(i->name, *p))) {
3885 log_error("Out of memory");
3889 /* Ensure that we don't follow symlinks */
3890 if ((fd = open(filename, O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NOCTTY)) >= 0)
3891 if ((f = fdopen(fd, "re")))
3894 if (errno == ELOOP) {
3895 log_error("Refusing to operate on symlinks, please pass unit names or absolute paths to unit files.");
3900 if (errno != ENOENT) {
3901 log_error("Failed to open %s: %m", filename);
3911 #if defined(TARGET_FEDORA) && defined (HAVE_SYSV_COMPAT)
3913 if (endswith(i->name, ".service")) {
3917 if (asprintf(&sysv, SYSTEM_SYSVINIT_PATH "/%s", i->name) < 0) {
3918 log_error("Out of memory");
3922 sysv[strlen(sysv) - sizeof(".service") + 1] = 0;
3923 exists = access(sysv, F_OK) >= 0;
3929 const char *argv[] = {
3936 log_info("%s is not a native service, redirecting to /sbin/chkconfig.", i->name);
3938 argv[1] = file_name_from_path(sysv);
3940 streq(verb, "enable") ? "on" :
3941 streq(verb, "disable") ? "off" : NULL;
3943 log_info("Executing %s %s %s", argv[0], argv[1], strempty(argv[2]));
3945 if ((pid = fork()) < 0) {
3946 log_error("Failed to fork: %m");
3949 } else if (pid == 0) {
3950 execv(argv[0], (char**) argv);
3951 _exit(EXIT_FAILURE);
3956 if ((r = wait_for_terminate(pid, &status)) < 0)
3959 if (status.si_code == CLD_EXITED) {
3960 if (status.si_status == 0 && (streq(verb, "enable") || streq(verb, "disable")))
3963 return status.si_status == 0 ? 0 : -EINVAL;
3973 log_error("Couldn't find %s.", i->name);
3979 if ((r = config_parse(filename, f, NULL, items, true, i)) < 0) {
3984 n_symlinks += strv_length(i->aliases);
3985 n_symlinks += strv_length(i->wanted_by);
3989 if ((r = install_info_symlink_alias(verb, i, config_path)) != 0)
3992 if ((r = install_info_symlink_wants(verb, i, config_path)) != 0)
3995 if ((r = mark_symlink_for_removal(filename)) < 0)
3998 if ((r = remove_marked_symlinks(config_path)) < 0)
4004 static char *get_config_path(void) {
4006 if (arg_user && arg_global)
4007 return strdup(USER_CONFIG_UNIT_PATH);
4012 if (user_config_home(&p) < 0)
4018 return strdup(SYSTEM_CONFIG_UNIT_PATH);
4021 static int enable_unit(DBusConnection *bus, char **args, unsigned n) {
4025 char *config_path = NULL;
4028 const char *verb = args[0];
4030 dbus_error_init(&error);
4033 if ((r = lookup_paths_init(&paths, arg_user ? MANAGER_USER : MANAGER_SYSTEM)) < 0) {
4034 log_error("Failed to determine lookup paths: %s", strerror(-r));
4038 if (!(config_path = get_config_path())) {
4039 log_error("Failed to determine config path");
4044 will_install = hashmap_new(string_hash_func, string_compare_func);
4045 have_installed = hashmap_new(string_hash_func, string_compare_func);
4047 if (!will_install || !have_installed) {
4048 log_error("Failed to allocate unit sets.");
4053 if (!arg_defaults && streq(verb, "disable"))
4054 if (!(remove_symlinks_to = set_new(string_hash_func, string_compare_func))) {
4055 log_error("Failed to allocate symlink sets.");
4060 for (j = 1; j < n; j++)
4061 if ((r = install_info_add(args[j])) < 0) {
4062 log_warning("Cannot install unit %s: %s", args[j], strerror(-r));
4066 while ((i = hashmap_first(will_install))) {
4069 assert_se(hashmap_move_one(have_installed, will_install, i->name) == 0);
4071 if ((q = install_info_apply(verb, &paths, i, config_path)) != 0) {
4079 /* In test mode and found something */
4085 if (streq(verb, "is-enabled"))
4086 r = r > 0 ? 0 : -ENOENT;
4088 if (n_symlinks <= 0)
4089 log_warning("Unit files contain no applicable installation information. Ignoring.");
4092 /* Don't try to reload anything if the user asked us to not do this */
4094 /* Don't try to reload anything when updating a unit globally */
4096 /* Don't try to reload anything if we are called for system changes but the system wasn't booted with systemd */
4097 (arg_user || sd_booted() > 0) &&
4098 /* Don't try to reload anything if we are running in a chroot environment */
4099 (arg_user || running_in_chroot() <= 0) ) {
4102 if ((q = daemon_reload(bus, args, n)) < 0)
4108 install_info_hashmap_free(will_install);
4109 install_info_hashmap_free(have_installed);
4111 set_free_free(remove_symlinks_to);
4113 lookup_paths_free(&paths);
4120 static int systemctl_help(void) {
4122 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
4123 "Send control commands to or query the systemd manager.\n\n"
4124 " -h --help Show this help\n"
4125 " --version Show package version\n"
4126 " -t --type=TYPE List only units of a particular type\n"
4127 " -p --property=NAME Show only properties by this name\n"
4128 " -a --all Show all units/properties, including dead/empty ones\n"
4129 " --full Don't ellipsize unit names on output\n"
4130 " --fail When queueing a new job, fail if conflicting jobs are\n"
4132 " -q --quiet Suppress output\n"
4133 " --no-block Do not wait until operation finished\n"
4134 " --no-pager Do not pipe output into a pager.\n"
4135 " --system Connect to system manager\n"
4136 " --user Connect to user service manager\n"
4137 " --order When generating graph for dot, show only order\n"
4138 " --require When generating graph for dot, show only requirement\n"
4139 " --no-wall Don't send wall message before halt/power-off/reboot\n"
4140 " --global Enable/disable unit files globally\n"
4141 " --no-reload When enabling/disabling unit files, don't reload daemon\n"
4143 " --no-ask-password\n"
4144 " Do not ask for system passwords\n"
4145 " --kill-mode=MODE How to send signal\n"
4146 " --kill-who=WHO Who to send signal to\n"
4147 " -s --signal=SIGNAL Which signal to send\n"
4148 " -f --force When enabling unit files, override existing symlinks\n"
4149 " When shutting down, execute action immediately\n"
4150 " --defaults When disabling unit files, remove default symlinks only\n\n"
4152 " list-units List units\n"
4153 " start [NAME...] Start (activate) one or more units\n"
4154 " stop [NAME...] Stop (deactivate) one or more units\n"
4155 " reload [NAME...] Reload one or more units\n"
4156 " restart [NAME...] Start or restart one or more units\n"
4157 " try-restart [NAME...] Restart one or more units if active\n"
4158 " reload-or-restart [NAME...] Reload one or more units is possible,\n"
4159 " otherwise start or restart\n"
4160 " reload-or-try-restart [NAME...] Reload one or more units is possible,\n"
4161 " otherwise restart if active\n"
4162 " isolate [NAME] Start one unit and stop all others\n"
4163 " kill [NAME...] Send signal to processes of a unit\n"
4164 " is-active [NAME...] Check whether units are active\n"
4165 " status [NAME...|PID...] Show runtime status of one or more units\n"
4166 " show [NAME...|JOB...] Show properties of one or more\n"
4167 " units/jobs or the manager\n"
4168 " reset-failed [NAME...] Reset failed state for all, one, or more\n"
4170 " enable [NAME...] Enable one or more unit files\n"
4171 " disable [NAME...] Disable one or more unit files\n"
4172 " is-enabled [NAME...] Check whether unit files are enabled\n"
4173 " load [NAME...] Load one or more units\n"
4174 " list-jobs List jobs\n"
4175 " cancel [JOB...] Cancel all, one, or more jobs\n"
4176 " monitor Monitor unit/job changes\n"
4177 " dump Dump server status\n"
4178 " dot Dump dependency graph for dot(1)\n"
4179 " snapshot [NAME] Create a snapshot\n"
4180 " delete [NAME...] Remove one or more snapshots\n"
4181 " daemon-reload Reload systemd manager configuration\n"
4182 " daemon-reexec Reexecute systemd manager\n"
4183 " show-environment Dump environment\n"
4184 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
4185 " unset-environment [NAME...] Unset one or more environment variables\n"
4186 " default Enter system default mode\n"
4187 " rescue Enter system rescue mode\n"
4188 " emergency Enter system emergency mode\n"
4189 " halt Shut down and halt the system\n"
4190 " poweroff Shut down and power-off the system\n"
4191 " reboot Shut down and reboot the system\n"
4192 " kexec Shut down and reboot the system with kexec\n"
4193 " exit Ask for user instance termination\n",
4194 program_invocation_short_name);
4199 static int halt_help(void) {
4201 printf("%s [OPTIONS...]\n\n"
4202 "%s the system.\n\n"
4203 " --help Show this help\n"
4204 " --halt Halt the machine\n"
4205 " -p --poweroff Switch off the machine\n"
4206 " --reboot Reboot the machine\n"
4207 " -f --force Force immediate halt/power-off/reboot\n"
4208 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
4209 " -d --no-wtmp Don't write wtmp record\n"
4210 " -n --no-sync Don't sync before halt/power-off/reboot\n"
4211 " --no-wall Don't send wall message before halt/power-off/reboot\n",
4212 program_invocation_short_name,
4213 arg_action == ACTION_REBOOT ? "Reboot" :
4214 arg_action == ACTION_POWEROFF ? "Power off" :
4220 static int shutdown_help(void) {
4222 printf("%s [OPTIONS...] [TIME] [WALL...]\n\n"
4223 "Shut down the system.\n\n"
4224 " --help Show this help\n"
4225 " -H --halt Halt the machine\n"
4226 " -P --poweroff Power-off the machine\n"
4227 " -r --reboot Reboot the machine\n"
4228 " -h Equivalent to --poweroff, overriden by --halt\n"
4229 " -k Don't halt/power-off/reboot, just send warnings\n"
4230 " --no-wall Don't send wall message before halt/power-off/reboot\n"
4231 " -c Cancel a pending shutdown\n",
4232 program_invocation_short_name);
4237 static int telinit_help(void) {
4239 printf("%s [OPTIONS...] {COMMAND}\n\n"
4240 "Send control commands to the init daemon.\n\n"
4241 " --help Show this help\n"
4242 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
4244 " 0 Power-off the machine\n"
4245 " 6 Reboot the machine\n"
4246 " 2, 3, 4, 5 Start runlevelX.target unit\n"
4247 " 1, s, S Enter rescue mode\n"
4248 " q, Q Reload init daemon configuration\n"
4249 " u, U Reexecute init daemon\n",
4250 program_invocation_short_name);
4255 static int runlevel_help(void) {
4257 printf("%s [OPTIONS...]\n\n"
4258 "Prints the previous and current runlevel of the init system.\n\n"
4259 " --help Show this help\n",
4260 program_invocation_short_name);
4265 static int systemctl_parse_argv(int argc, char *argv[]) {
4286 static const struct option options[] = {
4287 { "help", no_argument, NULL, 'h' },
4288 { "version", no_argument, NULL, ARG_VERSION },
4289 { "type", required_argument, NULL, 't' },
4290 { "property", required_argument, NULL, 'p' },
4291 { "all", no_argument, NULL, 'a' },
4292 { "full", no_argument, NULL, ARG_FULL },
4293 { "fail", no_argument, NULL, ARG_FAIL },
4294 { "user", no_argument, NULL, ARG_USER },
4295 { "system", no_argument, NULL, ARG_SYSTEM },
4296 { "global", no_argument, NULL, ARG_GLOBAL },
4297 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
4298 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
4299 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4300 { "quiet", no_argument, NULL, 'q' },
4301 { "order", no_argument, NULL, ARG_ORDER },
4302 { "require", no_argument, NULL, ARG_REQUIRE },
4303 { "force", no_argument, NULL, 'f' },
4304 { "no-reload", no_argument, NULL, ARG_NO_RELOAD },
4305 { "defaults", no_argument, NULL, ARG_DEFAULTS },
4306 { "kill-mode", required_argument, NULL, ARG_KILL_MODE },
4307 { "kill-who", required_argument, NULL, ARG_KILL_WHO },
4308 { "signal", required_argument, NULL, 's' },
4309 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
4310 { NULL, 0, NULL, 0 }
4318 /* Only when running as systemctl we ask for passwords */
4319 arg_ask_password = true;
4321 while ((c = getopt_long(argc, argv, "ht:p:aqfs:", options, NULL)) >= 0) {
4330 puts(PACKAGE_STRING);
4332 puts(SYSTEMD_FEATURES);
4342 if (!(l = strv_append(arg_property, optarg)))
4345 strv_free(arg_property);
4348 /* If the user asked for a particular
4349 * property, show it to him, even if it is
4372 arg_no_block = true;
4376 arg_no_pager = true;
4384 arg_dot = DOT_ORDER;
4388 arg_dot = DOT_REQUIRE;
4404 arg_no_reload = true;
4413 arg_defaults = true;
4417 arg_kill_who = optarg;
4421 arg_kill_mode = optarg;
4425 if ((arg_signal = signal_from_string_try_harder(optarg)) < 0) {
4426 log_error("Failed to parse signal string %s.", optarg);
4431 case ARG_NO_ASK_PASSWORD:
4432 arg_ask_password = false;
4439 log_error("Unknown option code %c", c);
4447 static int halt_parse_argv(int argc, char *argv[]) {
4456 static const struct option options[] = {
4457 { "help", no_argument, NULL, ARG_HELP },
4458 { "halt", no_argument, NULL, ARG_HALT },
4459 { "poweroff", no_argument, NULL, 'p' },
4460 { "reboot", no_argument, NULL, ARG_REBOOT },
4461 { "force", no_argument, NULL, 'f' },
4462 { "wtmp-only", no_argument, NULL, 'w' },
4463 { "no-wtmp", no_argument, NULL, 'd' },
4464 { "no-sync", no_argument, NULL, 'n' },
4465 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4466 { NULL, 0, NULL, 0 }
4474 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
4475 if (runlevel == '0' || runlevel == '6')
4476 arg_immediate = true;
4478 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
4486 arg_action = ACTION_HALT;
4490 if (arg_action != ACTION_REBOOT)
4491 arg_action = ACTION_POWEROFF;
4495 arg_action = ACTION_REBOOT;
4499 arg_immediate = true;
4520 /* Compatibility nops */
4527 log_error("Unknown option code %c", c);
4532 if (optind < argc) {
4533 log_error("Too many arguments.");
4540 static int parse_time_spec(const char *t, usec_t *_u) {
4544 if (streq(t, "now"))
4546 else if (t[0] == '+') {
4549 if (safe_atou64(t + 1, &u) < 0)
4552 *_u = now(CLOCK_REALTIME) + USEC_PER_MINUTE * u;
4561 hour = strtol(t, &e, 10);
4562 if (errno != 0 || *e != ':' || hour < 0 || hour > 23)
4565 minute = strtol(e+1, &e, 10);
4566 if (errno != 0 || *e != 0 || minute < 0 || minute > 59)
4569 n = now(CLOCK_REALTIME);
4570 s = (time_t) (n / USEC_PER_SEC);
4573 assert_se(localtime_r(&s, &tm));
4575 tm.tm_hour = (int) hour;
4576 tm.tm_min = (int) minute;
4579 assert_se(s = mktime(&tm));
4581 *_u = (usec_t) s * USEC_PER_SEC;
4584 *_u += USEC_PER_DAY;
4590 static int shutdown_parse_argv(int argc, char *argv[]) {
4597 static const struct option options[] = {
4598 { "help", no_argument, NULL, ARG_HELP },
4599 { "halt", no_argument, NULL, 'H' },
4600 { "poweroff", no_argument, NULL, 'P' },
4601 { "reboot", no_argument, NULL, 'r' },
4602 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4603 { NULL, 0, NULL, 0 }
4611 while ((c = getopt_long(argc, argv, "HPrhkt:afFc", options, NULL)) >= 0) {
4619 arg_action = ACTION_HALT;
4623 arg_action = ACTION_POWEROFF;
4627 arg_action = ACTION_REBOOT;
4631 if (arg_action != ACTION_HALT)
4632 arg_action = ACTION_POWEROFF;
4645 /* Compatibility nops */
4649 arg_action = ACTION_CANCEL_SHUTDOWN;
4656 log_error("Unknown option code %c", c);
4661 if (argc > optind) {
4662 if ((r = parse_time_spec(argv[optind], &arg_when)) < 0) {
4663 log_error("Failed to parse time specification: %s", argv[optind]);
4667 arg_when = now(CLOCK_REALTIME) + USEC_PER_MINUTE;
4669 /* We skip the time argument */
4670 if (argc > optind + 1)
4671 arg_wall = argv + optind + 1;
4678 static int telinit_parse_argv(int argc, char *argv[]) {
4685 static const struct option options[] = {
4686 { "help", no_argument, NULL, ARG_HELP },
4687 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4688 { NULL, 0, NULL, 0 }
4691 static const struct {
4695 { '0', ACTION_POWEROFF },
4696 { '6', ACTION_REBOOT },
4697 { '1', ACTION_RESCUE },
4698 { '2', ACTION_RUNLEVEL2 },
4699 { '3', ACTION_RUNLEVEL3 },
4700 { '4', ACTION_RUNLEVEL4 },
4701 { '5', ACTION_RUNLEVEL5 },
4702 { 's', ACTION_RESCUE },
4703 { 'S', ACTION_RESCUE },
4704 { 'q', ACTION_RELOAD },
4705 { 'Q', ACTION_RELOAD },
4706 { 'u', ACTION_REEXEC },
4707 { 'U', ACTION_REEXEC }
4716 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4731 log_error("Unknown option code %c", c);
4736 if (optind >= argc) {
4741 if (optind + 1 < argc) {
4742 log_error("Too many arguments.");
4746 if (strlen(argv[optind]) != 1) {
4747 log_error("Expected single character argument.");
4751 for (i = 0; i < ELEMENTSOF(table); i++)
4752 if (table[i].from == argv[optind][0])
4755 if (i >= ELEMENTSOF(table)) {
4756 log_error("Unknown command %s.", argv[optind]);
4760 arg_action = table[i].to;
4767 static int runlevel_parse_argv(int argc, char *argv[]) {
4773 static const struct option options[] = {
4774 { "help", no_argument, NULL, ARG_HELP },
4775 { NULL, 0, NULL, 0 }
4783 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4794 log_error("Unknown option code %c", c);
4799 if (optind < argc) {
4800 log_error("Too many arguments.");
4807 static int parse_argv(int argc, char *argv[]) {
4811 if (program_invocation_short_name) {
4813 if (strstr(program_invocation_short_name, "halt")) {
4814 arg_action = ACTION_HALT;
4815 return halt_parse_argv(argc, argv);
4816 } else if (strstr(program_invocation_short_name, "poweroff")) {
4817 arg_action = ACTION_POWEROFF;
4818 return halt_parse_argv(argc, argv);
4819 } else if (strstr(program_invocation_short_name, "reboot")) {
4820 arg_action = ACTION_REBOOT;
4821 return halt_parse_argv(argc, argv);
4822 } else if (strstr(program_invocation_short_name, "shutdown")) {
4823 arg_action = ACTION_POWEROFF;
4824 return shutdown_parse_argv(argc, argv);
4825 } else if (strstr(program_invocation_short_name, "init")) {
4827 if (sd_booted() > 0) {
4828 arg_action = ACTION_INVALID;
4829 return telinit_parse_argv(argc, argv);
4831 /* Hmm, so some other init system is
4832 * running, we need to forward this
4833 * request to it. For now we simply
4834 * guess that it is Upstart. */
4836 execv("/lib/upstart/telinit", argv);
4838 log_error("Couldn't find an alternative telinit implementation to spawn.");
4842 } else if (strstr(program_invocation_short_name, "runlevel")) {
4843 arg_action = ACTION_RUNLEVEL;
4844 return runlevel_parse_argv(argc, argv);
4848 arg_action = ACTION_SYSTEMCTL;
4849 return systemctl_parse_argv(argc, argv);
4852 static int action_to_runlevel(void) {
4854 static const char table[_ACTION_MAX] = {
4855 [ACTION_HALT] = '0',
4856 [ACTION_POWEROFF] = '0',
4857 [ACTION_REBOOT] = '6',
4858 [ACTION_RUNLEVEL2] = '2',
4859 [ACTION_RUNLEVEL3] = '3',
4860 [ACTION_RUNLEVEL4] = '4',
4861 [ACTION_RUNLEVEL5] = '5',
4862 [ACTION_RESCUE] = '1'
4865 assert(arg_action < _ACTION_MAX);
4867 return table[arg_action];
4870 static int talk_upstart(void) {
4871 DBusMessage *m = NULL, *reply = NULL;
4873 int previous, rl, r;
4875 env1_buf[] = "RUNLEVEL=X",
4876 env2_buf[] = "PREVLEVEL=X";
4877 char *env1 = env1_buf, *env2 = env2_buf;
4878 const char *emit = "runlevel";
4879 dbus_bool_t b_false = FALSE;
4880 DBusMessageIter iter, sub;
4881 DBusConnection *bus;
4883 dbus_error_init(&error);
4885 if (!(rl = action_to_runlevel()))
4888 if (utmp_get_runlevel(&previous, NULL) < 0)
4891 if (!(bus = dbus_connection_open_private("unix:abstract=/com/ubuntu/upstart", &error))) {
4892 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
4897 log_error("Failed to connect to Upstart bus: %s", bus_error_message(&error));
4902 if ((r = bus_check_peercred(bus)) < 0) {
4903 log_error("Failed to verify owner of bus.");
4907 if (!(m = dbus_message_new_method_call(
4908 "com.ubuntu.Upstart",
4909 "/com/ubuntu/Upstart",
4910 "com.ubuntu.Upstart0_6",
4913 log_error("Could not allocate message.");
4918 dbus_message_iter_init_append(m, &iter);
4920 env1_buf[sizeof(env1_buf)-2] = rl;
4921 env2_buf[sizeof(env2_buf)-2] = previous;
4923 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
4924 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
4925 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
4926 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
4927 !dbus_message_iter_close_container(&iter, &sub) ||
4928 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
4929 log_error("Could not append arguments to message.");
4934 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
4936 if (error_is_no_service(&error)) {
4941 log_error("Failed to issue method call: %s", bus_error_message(&error));
4950 dbus_message_unref(m);
4953 dbus_message_unref(reply);
4956 dbus_connection_flush(bus);
4957 dbus_connection_close(bus);
4958 dbus_connection_unref(bus);
4961 dbus_error_free(&error);
4966 static int talk_initctl(void) {
4967 struct init_request request;
4971 if (!(rl = action_to_runlevel()))
4975 request.magic = INIT_MAGIC;
4976 request.sleeptime = 0;
4977 request.cmd = INIT_CMD_RUNLVL;
4978 request.runlevel = rl;
4980 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
4982 if (errno == ENOENT)
4985 log_error("Failed to open "INIT_FIFO": %m");
4990 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
4991 close_nointr_nofail(fd);
4994 log_error("Failed to write to "INIT_FIFO": %m");
4995 return errno ? -errno : -EIO;
5001 static int systemctl_main(DBusConnection *bus, int argc, char *argv[], DBusError *error) {
5003 static const struct {
5011 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
5013 { "list-units", LESS, 1, list_units },
5014 { "list-jobs", EQUAL, 1, list_jobs },
5015 { "clear-jobs", EQUAL, 1, daemon_reload },
5016 { "load", MORE, 2, load_unit },
5017 { "cancel", MORE, 2, cancel_job },
5018 { "start", MORE, 2, start_unit },
5019 { "stop", MORE, 2, start_unit },
5020 { "reload", MORE, 2, start_unit },
5021 { "restart", MORE, 2, start_unit },
5022 { "try-restart", MORE, 2, start_unit },
5023 { "reload-or-restart", MORE, 2, start_unit },
5024 { "reload-or-try-restart", MORE, 2, start_unit },
5025 { "force-reload", MORE, 2, start_unit }, /* For compatibility with SysV */
5026 { "condrestart", MORE, 2, start_unit }, /* For compatibility with RH */
5027 { "isolate", EQUAL, 2, start_unit },
5028 { "kill", MORE, 2, kill_unit },
5029 { "is-active", MORE, 2, check_unit },
5030 { "check", MORE, 2, check_unit },
5031 { "show", MORE, 1, show },
5032 { "status", MORE, 2, show },
5033 { "monitor", EQUAL, 1, monitor },
5034 { "dump", EQUAL, 1, dump },
5035 { "dot", EQUAL, 1, dot },
5036 { "snapshot", LESS, 2, snapshot },
5037 { "delete", MORE, 2, delete_snapshot },
5038 { "daemon-reload", EQUAL, 1, daemon_reload },
5039 { "daemon-reexec", EQUAL, 1, daemon_reload },
5040 { "show-environment", EQUAL, 1, show_enviroment },
5041 { "set-environment", MORE, 2, set_environment },
5042 { "unset-environment", MORE, 2, set_environment },
5043 { "halt", EQUAL, 1, start_special },
5044 { "poweroff", EQUAL, 1, start_special },
5045 { "reboot", EQUAL, 1, start_special },
5046 { "kexec", EQUAL, 1, start_special },
5047 { "default", EQUAL, 1, start_special },
5048 { "rescue", EQUAL, 1, start_special },
5049 { "emergency", EQUAL, 1, start_special },
5050 { "exit", EQUAL, 1, start_special },
5051 { "reset-failed", MORE, 1, reset_failed },
5052 { "enable", MORE, 2, enable_unit },
5053 { "disable", MORE, 2, enable_unit },
5054 { "is-enabled", MORE, 2, enable_unit }
5064 left = argc - optind;
5067 /* Special rule: no arguments means "list-units" */
5070 if (streq(argv[optind], "help")) {
5075 for (i = 0; i < ELEMENTSOF(verbs); i++)
5076 if (streq(argv[optind], verbs[i].verb))
5079 if (i >= ELEMENTSOF(verbs)) {
5080 log_error("Unknown operation %s", argv[optind]);
5085 switch (verbs[i].argc_cmp) {
5088 if (left != verbs[i].argc) {
5089 log_error("Invalid number of arguments.");
5096 if (left < verbs[i].argc) {
5097 log_error("Too few arguments.");
5104 if (left > verbs[i].argc) {
5105 log_error("Too many arguments.");
5112 assert_not_reached("Unknown comparison operator.");
5115 /* Require a bus connection for all operations but
5117 if (!streq(verbs[i].verb, "enable") &&
5118 !streq(verbs[i].verb, "disable") &&
5120 log_error("Failed to get D-Bus connection: %s", error->message);
5124 return verbs[i].dispatch(bus, argv + optind, left);
5127 static int send_shutdownd(usec_t t, char mode, bool warn, const char *message) {
5129 struct msghdr msghdr;
5131 union sockaddr_union sockaddr;
5132 struct shutdownd_command c;
5140 strncpy(c.wall_message, message, sizeof(c.wall_message));
5142 if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0)
5146 sockaddr.sa.sa_family = AF_UNIX;
5147 sockaddr.un.sun_path[0] = 0;
5148 strncpy(sockaddr.un.sun_path+1, "/org/freedesktop/systemd1/shutdownd", sizeof(sockaddr.un.sun_path)-1);
5151 iovec.iov_base = (char*) &c;
5152 iovec.iov_len = sizeof(c);
5155 msghdr.msg_name = &sockaddr;
5156 msghdr.msg_namelen = offsetof(struct sockaddr_un, sun_path) + 1 + sizeof("/org/freedesktop/systemd1/shutdownd") - 1;
5158 msghdr.msg_iov = &iovec;
5159 msghdr.msg_iovlen = 1;
5161 if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) < 0) {
5162 close_nointr_nofail(fd);
5166 close_nointr_nofail(fd);
5170 static int reload_with_fallback(DBusConnection *bus) {
5173 /* First, try systemd via D-Bus. */
5174 if (daemon_reload(bus, NULL, 0) > 0)
5178 /* Nothing else worked, so let's try signals */
5179 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
5181 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
5182 log_error("kill() failed: %m");
5189 static int start_with_fallback(DBusConnection *bus) {
5192 /* First, try systemd via D-Bus. */
5193 if (start_unit(bus, NULL, 0) >= 0)
5197 /* Hmm, talking to systemd via D-Bus didn't work. Then
5198 * let's try to talk to Upstart via D-Bus. */
5199 if (talk_upstart() > 0)
5202 /* Nothing else worked, so let's try
5204 if (talk_initctl() > 0)
5207 log_error("Failed to talk to init daemon.");
5211 warn_wall(arg_action);
5215 static int halt_main(DBusConnection *bus) {
5218 if (geteuid() != 0) {
5219 log_error("Must be root.");
5225 char date[FORMAT_TIMESTAMP_MAX];
5227 m = strv_join(arg_wall, " ");
5228 r = send_shutdownd(arg_when,
5229 arg_action == ACTION_HALT ? 'H' :
5230 arg_action == ACTION_POWEROFF ? 'P' :
5237 log_warning("Failed to talk to shutdownd, proceeding with immediate shutdown: %s", strerror(-r));
5239 log_info("Shutdown scheduled for %s, use 'shutdown -c' to cancel.",
5240 format_timestamp(date, sizeof(date), arg_when));
5245 if (!arg_dry && !arg_immediate)
5246 return start_with_fallback(bus);
5249 if (sd_booted() > 0)
5250 log_debug("Not writing utmp record, assuming that systemd-update-utmp is used.");
5251 else if ((r = utmp_put_shutdown(0)) < 0)
5252 log_warning("Failed to write utmp record: %s", strerror(-r));
5261 /* Make sure C-A-D is handled by the kernel from this
5263 reboot(RB_ENABLE_CAD);
5265 switch (arg_action) {
5268 log_info("Halting.");
5269 reboot(RB_HALT_SYSTEM);
5272 case ACTION_POWEROFF:
5273 log_info("Powering off.");
5274 reboot(RB_POWER_OFF);
5278 log_info("Rebooting.");
5279 reboot(RB_AUTOBOOT);
5283 assert_not_reached("Unknown halt action.");
5286 /* We should never reach this. */
5290 static int runlevel_main(void) {
5291 int r, runlevel, previous;
5293 if ((r = utmp_get_runlevel(&runlevel, &previous)) < 0) {
5294 printf("unknown\n");
5299 previous <= 0 ? 'N' : previous,
5300 runlevel <= 0 ? 'N' : runlevel);
5305 static void pager_open(void) {
5312 if (!on_tty() || arg_no_pager)
5315 if ((pager = getenv("PAGER")))
5316 if (!*pager || streq(pager, "cat"))
5320 log_error("Failed to create pager pipe: %m");
5325 if (pager_pid < 0) {
5326 log_error("Failed to fork pager: %m");
5331 /* In the child start the pager */
5332 if (pager_pid == 0) {
5334 dup2(fd[0], STDIN_FILENO);
5337 setenv("LESS", "FRSX", 0);
5339 prctl(PR_SET_PDEATHSIG, SIGTERM);
5342 execlp(pager, pager, NULL);
5343 execl("/bin/sh", "sh", "-c", pager, NULL);
5345 /* Debian's alternatives command for pagers is
5346 * called 'pager'. Note that we do not call
5347 * sensible-pagers here, since that is just a
5348 * shell script that implements a logic that
5349 * is similar to this one anyway, but is
5350 * Debian-specific. */
5351 execlp("pager", "pager", NULL);
5353 execlp("less", "less", NULL);
5354 execlp("more", "more", NULL);
5357 log_error("Unable to execute pager: %m");
5358 _exit(EXIT_FAILURE);
5361 /* Return in the parent */
5362 if (dup2(fd[1], STDOUT_FILENO) < 0)
5363 log_error("Failed to duplicate pager pipe: %m");
5368 static void pager_close(void) {
5374 /* Inform pager that we are done */
5376 wait_for_terminate(pager_pid, &dummy);
5380 int main(int argc, char*argv[]) {
5381 int r, retval = EXIT_FAILURE;
5382 DBusConnection *bus = NULL;
5385 dbus_error_init(&error);
5387 log_parse_environment();
5390 if ((r = parse_argv(argc, argv)) < 0)
5393 retval = EXIT_SUCCESS;
5397 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
5398 * let's shortcut this */
5399 if (arg_action == ACTION_RUNLEVEL) {
5400 r = runlevel_main();
5401 retval = r < 0 ? EXIT_FAILURE : r;
5405 bus_connect(arg_user ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &bus, &private_bus, &error);
5407 switch (arg_action) {
5409 case ACTION_SYSTEMCTL:
5410 r = systemctl_main(bus, argc, argv, &error);
5414 case ACTION_POWEROFF:
5419 case ACTION_RUNLEVEL2:
5420 case ACTION_RUNLEVEL3:
5421 case ACTION_RUNLEVEL4:
5422 case ACTION_RUNLEVEL5:
5424 case ACTION_EMERGENCY:
5425 case ACTION_DEFAULT:
5426 r = start_with_fallback(bus);
5431 r = reload_with_fallback(bus);
5434 case ACTION_CANCEL_SHUTDOWN:
5435 r = send_shutdownd(0, 0, false, NULL);
5438 case ACTION_INVALID:
5439 case ACTION_RUNLEVEL:
5441 assert_not_reached("Unknown action");
5444 retval = r < 0 ? EXIT_FAILURE : r;
5449 dbus_connection_flush(bus);
5450 dbus_connection_close(bus);
5451 dbus_connection_unref(bus);
5454 dbus_error_free(&error);
5458 strv_free(arg_property);