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>
36 #include <dbus/dbus.h>
42 #include "utmp-wtmp.h"
46 #include "dbus-common.h"
47 #include "cgroup-show.h"
48 #include "cgroup-util.h"
50 #include "path-lookup.h"
51 #include "conf-parser.h"
52 #include "sd-daemon.h"
53 #include "shutdownd.h"
54 #include "exit-status.h"
55 #include "bus-errors.h"
57 #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 const char *arg_job_mode = "replace";
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 bool arg_failed = false;
80 static char **arg_wall = NULL;
81 static const char *arg_kill_who = NULL;
82 static const char *arg_kill_mode = NULL;
83 static int arg_signal = SIGTERM;
84 static const char *arg_root = NULL;
85 static usec_t arg_when = 0;
104 ACTION_CANCEL_SHUTDOWN,
106 } arg_action = ACTION_SYSTEMCTL;
112 static enum transport {
116 } arg_transport = TRANSPORT_NORMAL;
117 static const char *arg_host = NULL;
119 static bool private_bus = false;
121 static pid_t agent_pid = 0;
123 static int daemon_reload(DBusConnection *bus, char **args, unsigned n);
125 static bool on_tty(void) {
128 /* Note that this is invoked relatively early, before we start
129 * the pager. That means the value we return reflects whether
130 * we originally were started on a tty, not if we currently
131 * are. But this is intended, since we want colour and so on
132 * when run in our own pager. */
134 if (_unlikely_(t < 0))
135 t = isatty(STDOUT_FILENO) > 0;
140 static void pager_open_if_enabled(void) {
147 static void spawn_ask_password_agent(void) {
153 /* We check STDIN here, not STDOUT, since this is about input,
155 if (!isatty(STDIN_FILENO))
158 if (!arg_ask_password)
166 /* Spawns a temporary TTY agent, making sure it goes away when
169 if ((agent_pid = fork()) < 0)
172 if (agent_pid == 0) {
176 bool stdout_is_tty, stderr_is_tty;
178 /* Make sure the agent goes away when the parent dies */
179 if (prctl(PR_SET_PDEATHSIG, SIGTERM) < 0)
182 /* Check whether our parent died before we were able
183 * to set the death signal */
184 if (getppid() != parent)
187 /* Don't leak fds to the agent */
188 close_all_fds(NULL, 0);
190 stdout_is_tty = isatty(STDOUT_FILENO);
191 stderr_is_tty = isatty(STDERR_FILENO);
193 if (!stdout_is_tty || !stderr_is_tty) {
194 /* Detach from stdout/stderr. and reopen
195 * /dev/tty for them. This is important to
196 * ensure that when systemctl is started via
197 * popen() or a similar call that expects to
198 * read EOF we actually do generate EOF and
199 * not delay this indefinitely by because we
200 * keep an unused copy of stdin around. */
201 if ((fd = open("/dev/tty", O_WRONLY)) < 0) {
202 log_error("Failed to open /dev/tty: %m");
207 dup2(fd, STDOUT_FILENO);
210 dup2(fd, STDERR_FILENO);
216 execl(SYSTEMD_TTY_ASK_PASSWORD_AGENT_BINARY_PATH, SYSTEMD_TTY_ASK_PASSWORD_AGENT_BINARY_PATH, "--watch", NULL);
218 log_error("Unable to execute agent: %m");
223 static const char *ansi_highlight(bool b) {
228 return b ? ANSI_HIGHLIGHT_ON : ANSI_HIGHLIGHT_OFF;
231 static const char *ansi_highlight_green(bool b) {
236 return b ? ANSI_HIGHLIGHT_GREEN_ON : ANSI_HIGHLIGHT_OFF;
239 static bool error_is_no_service(const DBusError *error) {
242 if (!dbus_error_is_set(error))
245 if (dbus_error_has_name(error, DBUS_ERROR_NAME_HAS_NO_OWNER))
248 if (dbus_error_has_name(error, DBUS_ERROR_SERVICE_UNKNOWN))
251 return startswith(error->name, "org.freedesktop.DBus.Error.Spawn.");
254 static int translate_bus_error_to_exit_status(int r, const DBusError *error) {
257 if (!dbus_error_is_set(error))
260 if (dbus_error_has_name(error, DBUS_ERROR_ACCESS_DENIED) ||
261 dbus_error_has_name(error, BUS_ERROR_ONLY_BY_DEPENDENCY) ||
262 dbus_error_has_name(error, BUS_ERROR_NO_ISOLATION) ||
263 dbus_error_has_name(error, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE))
264 return EXIT_NOPERMISSION;
266 if (dbus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT))
267 return EXIT_NOTINSTALLED;
269 if (dbus_error_has_name(error, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE) ||
270 dbus_error_has_name(error, BUS_ERROR_NOT_SUPPORTED))
271 return EXIT_NOTIMPLEMENTED;
273 if (dbus_error_has_name(error, BUS_ERROR_LOAD_FAILED))
274 return EXIT_NOTCONFIGURED;
282 static void warn_wall(enum action action) {
283 static const char *table[_ACTION_MAX] = {
284 [ACTION_HALT] = "The system is going down for system halt NOW!",
285 [ACTION_REBOOT] = "The system is going down for reboot NOW!",
286 [ACTION_POWEROFF] = "The system is going down for power-off NOW!",
287 [ACTION_KEXEC] = "The system is going down for kexec reboot NOW!",
288 [ACTION_RESCUE] = "The system is going down to rescue mode NOW!",
289 [ACTION_EMERGENCY] = "The system is going down to emergency mode NOW!"
298 if (!(p = strv_join(arg_wall, " "))) {
299 log_error("Failed to join strings.");
315 utmp_wall(table[action], NULL);
320 const char *description;
321 const char *load_state;
322 const char *active_state;
323 const char *sub_state;
324 const char *following;
325 const char *unit_path;
327 const char *job_type;
328 const char *job_path;
331 static int compare_unit_info(const void *a, const void *b) {
333 const struct unit_info *u = a, *v = b;
335 d1 = strrchr(u->id, '.');
336 d2 = strrchr(v->id, '.');
341 if ((r = strcasecmp(d1, d2)) != 0)
345 return strcasecmp(u->id, v->id);
348 static bool output_show_unit(const struct unit_info *u) {
352 return streq(u->active_state, "failed");
354 return (!arg_type || ((dot = strrchr(u->id, '.')) &&
355 streq(dot+1, arg_type))) &&
356 (arg_all || !(streq(u->active_state, "inactive") || u->following[0]) || u->job_id > 0);
359 static void output_units_list(const struct unit_info *unit_infos, unsigned c) {
360 unsigned active_len, sub_len, job_len, n_shown = 0;
361 const struct unit_info *u;
363 active_len = sizeof("ACTIVE")-1;
364 sub_len = sizeof("SUB")-1;
365 job_len = sizeof("JOB")-1;
367 for (u = unit_infos; u < unit_infos + c; u++) {
368 if (!output_show_unit(u))
371 active_len = MAX(active_len, strlen(u->active_state));
372 sub_len = MAX(sub_len, strlen(u->sub_state));
374 job_len = MAX(job_len, strlen(u->job_type));
378 printf("%-25s %-6s %-*s %-*s %-*s", "UNIT", "LOAD",
379 active_len, "ACTIVE", sub_len, "SUB", job_len, "JOB");
380 if (columns() >= 80+12 || arg_full || !arg_no_pager)
381 printf(" %s\n", "DESCRIPTION");
386 for (u = unit_infos; u < unit_infos + c; u++) {
389 const char *on_loaded, *off_loaded;
390 const char *on_active, *off_active;
392 if (!output_show_unit(u))
397 if (!streq(u->load_state, "loaded") &&
398 !streq(u->load_state, "banned")) {
399 on_loaded = ansi_highlight(true);
400 off_loaded = ansi_highlight(false);
402 on_loaded = off_loaded = "";
404 if (streq(u->active_state, "failed")) {
405 on_active = ansi_highlight(true);
406 off_active = ansi_highlight(false);
408 on_active = off_active = "";
410 e = arg_full ? NULL : ellipsize(u->id, 25, 33);
412 printf("%-25s %s%-6s%s %s%-*s %-*s%s%n",
414 on_loaded, u->load_state, off_loaded,
415 on_active, active_len, u->active_state,
416 sub_len, u->sub_state, off_active,
421 a -= strlen(on_loaded) + strlen(off_loaded);
422 a -= strlen(on_active) + strlen(off_active);
425 printf(" %-*s", job_len, u->job_type);
429 if (a + b + 1 < columns()) {
431 printf(" %-*s", job_len, "");
433 if (arg_full || !arg_no_pager)
434 printf(" %s", u->description);
436 printf(" %.*s", columns() - a - b - 1, u->description);
443 printf("\nLOAD = Reflects whether the unit definition was properly loaded.\n"
444 "ACTIVE = The high-level unit activation state, i.e. generalization of SUB.\n"
445 "SUB = The low-level unit activation state, values depend on unit type.\n"
446 "JOB = Pending job for the unit.\n");
449 printf("\n%u units listed.\n", n_shown);
451 printf("\n%u units listed. Pass --all to see inactive units, too.\n", n_shown);
455 static int list_units(DBusConnection *bus, char **args, unsigned n) {
456 DBusMessage *m = NULL, *reply = NULL;
459 DBusMessageIter iter, sub, sub2;
460 unsigned c = 0, n_units = 0;
461 struct unit_info *unit_infos = NULL;
463 dbus_error_init(&error);
467 pager_open_if_enabled();
469 if (!(m = dbus_message_new_method_call(
470 "org.freedesktop.systemd1",
471 "/org/freedesktop/systemd1",
472 "org.freedesktop.systemd1.Manager",
474 log_error("Could not allocate message.");
478 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
479 log_error("Failed to issue method call: %s", bus_error_message(&error));
484 if (!dbus_message_iter_init(reply, &iter) ||
485 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
486 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
487 log_error("Failed to parse reply.");
492 dbus_message_iter_recurse(&iter, &sub);
494 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
497 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
498 log_error("Failed to parse reply.");
506 n_units = MAX(2*c, 16);
507 w = realloc(unit_infos, sizeof(struct unit_info) * n_units);
510 log_error("Failed to allocate unit array.");
520 dbus_message_iter_recurse(&sub, &sub2);
522 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->id, true) < 0 ||
523 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->description, true) < 0 ||
524 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->load_state, true) < 0 ||
525 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->active_state, true) < 0 ||
526 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->sub_state, true) < 0 ||
527 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->following, true) < 0 ||
528 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &u->unit_path, true) < 0 ||
529 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &u->job_id, true) < 0 ||
530 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->job_type, true) < 0 ||
531 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &u->job_path, false) < 0) {
532 log_error("Failed to parse reply.");
537 dbus_message_iter_next(&sub);
542 qsort(unit_infos, c, sizeof(struct unit_info), compare_unit_info);
543 output_units_list(unit_infos, c);
550 dbus_message_unref(m);
553 dbus_message_unref(reply);
557 dbus_error_free(&error);
562 static int dot_one_property(const char *name, const char *prop, DBusMessageIter *iter) {
563 static const char * const colors[] = {
564 "Requires", "[color=\"black\"]",
565 "RequiresOverridable", "[color=\"black\"]",
566 "Requisite", "[color=\"darkblue\"]",
567 "RequisiteOverridable", "[color=\"darkblue\"]",
568 "Wants", "[color=\"darkgrey\"]",
569 "Conflicts", "[color=\"red\"]",
570 "ConflictedBy", "[color=\"red\"]",
571 "After", "[color=\"green\"]"
574 const char *c = NULL;
581 for (i = 0; i < ELEMENTSOF(colors); i += 2)
582 if (streq(colors[i], prop)) {
590 if (arg_dot != DOT_ALL)
591 if ((arg_dot == DOT_ORDER) != streq(prop, "After"))
594 switch (dbus_message_iter_get_arg_type(iter)) {
596 case DBUS_TYPE_ARRAY:
598 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
601 dbus_message_iter_recurse(iter, &sub);
603 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
606 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
607 dbus_message_iter_get_basic(&sub, &s);
608 printf("\t\"%s\"->\"%s\" %s;\n", name, s, c);
610 dbus_message_iter_next(&sub);
620 static int dot_one(DBusConnection *bus, const char *name, const char *path) {
621 DBusMessage *m = NULL, *reply = NULL;
622 const char *interface = "org.freedesktop.systemd1.Unit";
625 DBusMessageIter iter, sub, sub2, sub3;
630 dbus_error_init(&error);
632 if (!(m = dbus_message_new_method_call(
633 "org.freedesktop.systemd1",
635 "org.freedesktop.DBus.Properties",
637 log_error("Could not allocate message.");
642 if (!dbus_message_append_args(m,
643 DBUS_TYPE_STRING, &interface,
644 DBUS_TYPE_INVALID)) {
645 log_error("Could not append arguments to message.");
650 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
651 log_error("Failed to issue method call: %s", bus_error_message(&error));
656 if (!dbus_message_iter_init(reply, &iter) ||
657 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
658 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
659 log_error("Failed to parse reply.");
664 dbus_message_iter_recurse(&iter, &sub);
666 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
669 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
670 log_error("Failed to parse reply.");
675 dbus_message_iter_recurse(&sub, &sub2);
677 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &prop, true) < 0) {
678 log_error("Failed to parse reply.");
683 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
684 log_error("Failed to parse reply.");
689 dbus_message_iter_recurse(&sub2, &sub3);
691 if (dot_one_property(name, prop, &sub3)) {
692 log_error("Failed to parse reply.");
697 dbus_message_iter_next(&sub);
704 dbus_message_unref(m);
707 dbus_message_unref(reply);
709 dbus_error_free(&error);
714 static int dot(DBusConnection *bus, char **args, unsigned n) {
715 DBusMessage *m = NULL, *reply = NULL;
718 DBusMessageIter iter, sub, sub2;
720 dbus_error_init(&error);
724 if (!(m = dbus_message_new_method_call(
725 "org.freedesktop.systemd1",
726 "/org/freedesktop/systemd1",
727 "org.freedesktop.systemd1.Manager",
729 log_error("Could not allocate message.");
733 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
734 log_error("Failed to issue method call: %s", bus_error_message(&error));
739 if (!dbus_message_iter_init(reply, &iter) ||
740 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
741 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
742 log_error("Failed to parse reply.");
747 printf("digraph systemd {\n");
749 dbus_message_iter_recurse(&iter, &sub);
750 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
751 const char *id, *description, *load_state, *active_state, *sub_state, *following, *unit_path;
753 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
754 log_error("Failed to parse reply.");
759 dbus_message_iter_recurse(&sub, &sub2);
761 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
762 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
763 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
764 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
765 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
766 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &following, true) < 0 ||
767 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, true) < 0) {
768 log_error("Failed to parse reply.");
773 if ((r = dot_one(bus, id, unit_path)) < 0)
776 /* printf("\t\"%s\";\n", id); */
777 dbus_message_iter_next(&sub);
782 log_info(" Color legend: black = Requires\n"
783 " dark blue = Requisite\n"
784 " dark grey = Wants\n"
789 log_notice("-- You probably want to process this output with graphviz' dot tool.\n"
790 "-- Try a shell pipeline like 'systemctl dot | dot -Tsvg > systemd.svg'!\n");
796 dbus_message_unref(m);
799 dbus_message_unref(reply);
801 dbus_error_free(&error);
806 static int list_jobs(DBusConnection *bus, char **args, unsigned n) {
807 DBusMessage *m = NULL, *reply = NULL;
810 DBusMessageIter iter, sub, sub2;
813 dbus_error_init(&error);
817 pager_open_if_enabled();
819 if (!(m = dbus_message_new_method_call(
820 "org.freedesktop.systemd1",
821 "/org/freedesktop/systemd1",
822 "org.freedesktop.systemd1.Manager",
824 log_error("Could not allocate message.");
828 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
829 log_error("Failed to issue method call: %s", bus_error_message(&error));
834 if (!dbus_message_iter_init(reply, &iter) ||
835 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
836 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
837 log_error("Failed to parse reply.");
842 dbus_message_iter_recurse(&iter, &sub);
845 printf("%4s %-25s %-15s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
847 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
848 const char *name, *type, *state, *job_path, *unit_path;
852 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
853 log_error("Failed to parse reply.");
858 dbus_message_iter_recurse(&sub, &sub2);
860 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
861 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
862 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
863 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
864 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
865 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
866 log_error("Failed to parse reply.");
871 e = arg_full ? NULL : ellipsize(name, 25, 33);
872 printf("%4u %-25s %-15s %-7s\n", id, e ? e : name, type, state);
877 dbus_message_iter_next(&sub);
881 printf("\n%u jobs listed.\n", k);
887 dbus_message_unref(m);
890 dbus_message_unref(reply);
892 dbus_error_free(&error);
897 static int load_unit(DBusConnection *bus, char **args, unsigned n) {
898 DBusMessage *m = NULL, *reply = NULL;
903 dbus_error_init(&error);
908 for (i = 1; i < n; i++) {
910 if (!(m = dbus_message_new_method_call(
911 "org.freedesktop.systemd1",
912 "/org/freedesktop/systemd1",
913 "org.freedesktop.systemd1.Manager",
915 log_error("Could not allocate message.");
920 if (!dbus_message_append_args(m,
921 DBUS_TYPE_STRING, &args[i],
922 DBUS_TYPE_INVALID)) {
923 log_error("Could not append arguments to message.");
928 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
929 log_error("Failed to issue method call: %s", bus_error_message(&error));
934 dbus_message_unref(m);
935 dbus_message_unref(reply);
944 dbus_message_unref(m);
947 dbus_message_unref(reply);
949 dbus_error_free(&error);
954 static int cancel_job(DBusConnection *bus, char **args, unsigned n) {
955 DBusMessage *m = NULL, *reply = NULL;
960 dbus_error_init(&error);
966 return daemon_reload(bus, args, n);
968 for (i = 1; i < n; i++) {
972 if (!(m = dbus_message_new_method_call(
973 "org.freedesktop.systemd1",
974 "/org/freedesktop/systemd1",
975 "org.freedesktop.systemd1.Manager",
977 log_error("Could not allocate message.");
982 if ((r = safe_atou(args[i], &id)) < 0) {
983 log_error("Failed to parse job id: %s", strerror(-r));
987 assert_cc(sizeof(uint32_t) == sizeof(id));
988 if (!dbus_message_append_args(m,
989 DBUS_TYPE_UINT32, &id,
990 DBUS_TYPE_INVALID)) {
991 log_error("Could not append arguments to message.");
996 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
997 log_error("Failed to issue method call: %s", bus_error_message(&error));
1002 if (!dbus_message_get_args(reply, &error,
1003 DBUS_TYPE_OBJECT_PATH, &path,
1004 DBUS_TYPE_INVALID)) {
1005 log_error("Failed to parse reply: %s", bus_error_message(&error));
1010 dbus_message_unref(m);
1011 if (!(m = dbus_message_new_method_call(
1012 "org.freedesktop.systemd1",
1014 "org.freedesktop.systemd1.Job",
1016 log_error("Could not allocate message.");
1021 dbus_message_unref(reply);
1022 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1023 log_error("Failed to issue method call: %s", bus_error_message(&error));
1028 dbus_message_unref(m);
1029 dbus_message_unref(reply);
1037 dbus_message_unref(m);
1040 dbus_message_unref(reply);
1042 dbus_error_free(&error);
1047 static bool need_daemon_reload(DBusConnection *bus, const char *unit) {
1048 DBusMessage *m = NULL, *reply = NULL;
1049 dbus_bool_t b = FALSE;
1050 DBusMessageIter iter, sub;
1052 *interface = "org.freedesktop.systemd1.Unit",
1053 *property = "NeedDaemonReload",
1056 /* We ignore all errors here, since this is used to show a warning only */
1058 if (!(m = dbus_message_new_method_call(
1059 "org.freedesktop.systemd1",
1060 "/org/freedesktop/systemd1",
1061 "org.freedesktop.systemd1.Manager",
1065 if (!dbus_message_append_args(m,
1066 DBUS_TYPE_STRING, &unit,
1070 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
1073 if (!dbus_message_get_args(reply, NULL,
1074 DBUS_TYPE_OBJECT_PATH, &path,
1078 dbus_message_unref(m);
1079 if (!(m = dbus_message_new_method_call(
1080 "org.freedesktop.systemd1",
1082 "org.freedesktop.DBus.Properties",
1086 if (!dbus_message_append_args(m,
1087 DBUS_TYPE_STRING, &interface,
1088 DBUS_TYPE_STRING, &property,
1089 DBUS_TYPE_INVALID)) {
1093 dbus_message_unref(reply);
1094 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
1097 if (!dbus_message_iter_init(reply, &iter) ||
1098 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
1101 dbus_message_iter_recurse(&iter, &sub);
1103 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_BOOLEAN)
1106 dbus_message_iter_get_basic(&sub, &b);
1110 dbus_message_unref(m);
1113 dbus_message_unref(reply);
1118 typedef struct WaitData {
1123 static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
1131 dbus_error_init(&error);
1133 log_debug("Got D-Bus request: %s.%s() on %s",
1134 dbus_message_get_interface(message),
1135 dbus_message_get_member(message),
1136 dbus_message_get_path(message));
1138 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
1139 log_error("Warning! D-Bus connection terminated.");
1140 dbus_connection_close(connection);
1142 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
1144 const char *path, *result;
1145 dbus_bool_t success = true;
1147 if (dbus_message_get_args(message, &error,
1148 DBUS_TYPE_UINT32, &id,
1149 DBUS_TYPE_OBJECT_PATH, &path,
1150 DBUS_TYPE_STRING, &result,
1151 DBUS_TYPE_INVALID)) {
1154 if ((p = set_remove(d->set, (char*) path)))
1158 d->result = strdup(result);
1163 dbus_error_free(&error);
1165 if (dbus_message_get_args(message, &error,
1166 DBUS_TYPE_UINT32, &id,
1167 DBUS_TYPE_OBJECT_PATH, &path,
1168 DBUS_TYPE_BOOLEAN, &success,
1169 DBUS_TYPE_INVALID)) {
1172 /* Compatibility with older systemd versions <
1173 * 19 during upgrades. This should be dropped
1176 if ((p = set_remove(d->set, (char*) path)))
1180 d->result = strdup("failed");
1186 log_error("Failed to parse message: %s", bus_error_message(&error));
1190 dbus_error_free(&error);
1191 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1194 static int enable_wait_for_jobs(DBusConnection *bus) {
1202 dbus_error_init(&error);
1203 dbus_bus_add_match(bus,
1205 "sender='org.freedesktop.systemd1',"
1206 "interface='org.freedesktop.systemd1.Manager',"
1207 "member='JobRemoved',"
1208 "path='/org/freedesktop/systemd1'",
1211 if (dbus_error_is_set(&error)) {
1212 log_error("Failed to add match: %s", bus_error_message(&error));
1213 dbus_error_free(&error);
1217 /* This is slightly dirty, since we don't undo the match registrations. */
1221 static int wait_for_jobs(DBusConnection *bus, Set *s) {
1231 if (!dbus_connection_add_filter(bus, wait_filter, &d, NULL)) {
1232 log_error("Failed to add filter.");
1237 while (!set_isempty(s) &&
1238 dbus_connection_read_write_dispatch(bus, -1))
1241 if (!arg_quiet && d.result) {
1242 if (streq(d.result, "timeout"))
1243 log_error("Job timed out.");
1244 else if (streq(d.result, "canceled"))
1245 log_error("Job canceled.");
1246 else if (streq(d.result, "dependency"))
1247 log_error("A dependency job failed. See system logs for details.");
1248 else if (!streq(d.result, "done") && !streq(d.result, "skipped"))
1249 log_error("Job failed. See system logs and 'systemctl status' for details.");
1252 if (streq_ptr(d.result, "timeout"))
1254 else if (streq_ptr(d.result, "canceled"))
1256 else if (!streq_ptr(d.result, "done") && !streq_ptr(d.result, "skipped"))
1264 /* This is slightly dirty, since we don't undo the filter registration. */
1269 static int start_unit_one(
1270 DBusConnection *bus,
1277 DBusMessage *m = NULL, *reply = NULL;
1286 assert(arg_no_block || s);
1288 if (!(m = dbus_message_new_method_call(
1289 "org.freedesktop.systemd1",
1290 "/org/freedesktop/systemd1",
1291 "org.freedesktop.systemd1.Manager",
1293 log_error("Could not allocate message.");
1298 if (!dbus_message_append_args(m,
1299 DBUS_TYPE_STRING, &name,
1300 DBUS_TYPE_STRING, &mode,
1301 DBUS_TYPE_INVALID)) {
1302 log_error("Could not append arguments to message.");
1307 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, error))) {
1309 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(error)) {
1310 /* There's always a fallback possible for
1311 * legacy actions. */
1316 log_error("Failed to issue method call: %s", bus_error_message(error));
1321 if (!dbus_message_get_args(reply, error,
1322 DBUS_TYPE_OBJECT_PATH, &path,
1323 DBUS_TYPE_INVALID)) {
1324 log_error("Failed to parse reply: %s", bus_error_message(error));
1329 if (need_daemon_reload(bus, name))
1330 log_warning("Unit file of created job changed on disk, 'systemctl %s daemon-reload' recommended.",
1331 arg_user ? "--user" : "--system");
1333 if (!arg_no_block) {
1336 if (!(p = strdup(path))) {
1337 log_error("Failed to duplicate path.");
1342 if ((r = set_put(s, p)) < 0) {
1344 log_error("Failed to add path to set.");
1353 dbus_message_unref(m);
1356 dbus_message_unref(reply);
1361 static enum action verb_to_action(const char *verb) {
1362 if (streq(verb, "halt"))
1364 else if (streq(verb, "poweroff"))
1365 return ACTION_POWEROFF;
1366 else if (streq(verb, "reboot"))
1367 return ACTION_REBOOT;
1368 else if (streq(verb, "kexec"))
1369 return ACTION_KEXEC;
1370 else if (streq(verb, "rescue"))
1371 return ACTION_RESCUE;
1372 else if (streq(verb, "emergency"))
1373 return ACTION_EMERGENCY;
1374 else if (streq(verb, "default"))
1375 return ACTION_DEFAULT;
1376 else if (streq(verb, "exit"))
1379 return ACTION_INVALID;
1382 static int start_unit(DBusConnection *bus, char **args, unsigned n) {
1384 static const char * const table[_ACTION_MAX] = {
1385 [ACTION_HALT] = SPECIAL_HALT_TARGET,
1386 [ACTION_POWEROFF] = SPECIAL_POWEROFF_TARGET,
1387 [ACTION_REBOOT] = SPECIAL_REBOOT_TARGET,
1388 [ACTION_KEXEC] = SPECIAL_KEXEC_TARGET,
1389 [ACTION_RUNLEVEL2] = SPECIAL_RUNLEVEL2_TARGET,
1390 [ACTION_RUNLEVEL3] = SPECIAL_RUNLEVEL3_TARGET,
1391 [ACTION_RUNLEVEL4] = SPECIAL_RUNLEVEL4_TARGET,
1392 [ACTION_RUNLEVEL5] = SPECIAL_RUNLEVEL5_TARGET,
1393 [ACTION_RESCUE] = SPECIAL_RESCUE_TARGET,
1394 [ACTION_EMERGENCY] = SPECIAL_EMERGENCY_TARGET,
1395 [ACTION_DEFAULT] = SPECIAL_DEFAULT_TARGET,
1396 [ACTION_EXIT] = SPECIAL_EXIT_TARGET
1401 const char *method, *mode, *one_name;
1405 dbus_error_init(&error);
1409 spawn_ask_password_agent();
1411 if (arg_action == ACTION_SYSTEMCTL) {
1413 streq(args[0], "stop") ||
1414 streq(args[0], "condstop") ? "StopUnit" :
1415 streq(args[0], "reload") ? "ReloadUnit" :
1416 streq(args[0], "restart") ? "RestartUnit" :
1418 streq(args[0], "try-restart") ||
1419 streq(args[0], "condrestart") ? "TryRestartUnit" :
1421 streq(args[0], "reload-or-restart") ? "ReloadOrRestartUnit" :
1423 streq(args[0], "reload-or-try-restart") ||
1424 streq(args[0], "condreload") ||
1426 streq(args[0], "force-reload") ? "ReloadOrTryRestartUnit" :
1430 (streq(args[0], "isolate") ||
1431 streq(args[0], "rescue") ||
1432 streq(args[0], "emergency")) ? "isolate" : arg_job_mode;
1434 one_name = table[verb_to_action(args[0])];
1437 assert(arg_action < ELEMENTSOF(table));
1438 assert(table[arg_action]);
1440 method = "StartUnit";
1442 mode = (arg_action == ACTION_EMERGENCY ||
1443 arg_action == ACTION_RESCUE ||
1444 arg_action == ACTION_RUNLEVEL2 ||
1445 arg_action == ACTION_RUNLEVEL3 ||
1446 arg_action == ACTION_RUNLEVEL4 ||
1447 arg_action == ACTION_RUNLEVEL5) ? "isolate" : "replace";
1449 one_name = table[arg_action];
1452 if (!arg_no_block) {
1453 if ((ret = enable_wait_for_jobs(bus)) < 0) {
1454 log_error("Could not watch jobs: %s", strerror(-ret));
1458 if (!(s = set_new(string_hash_func, string_compare_func))) {
1459 log_error("Failed to allocate set.");
1466 if ((ret = start_unit_one(bus, method, one_name, mode, &error, s)) <= 0)
1469 for (i = 1; i < n; i++)
1470 if ((r = start_unit_one(bus, method, args[i], mode, &error, s)) != 0) {
1471 ret = translate_bus_error_to_exit_status(r, &error);
1472 dbus_error_free(&error);
1477 if ((r = wait_for_jobs(bus, s)) < 0) {
1486 dbus_error_free(&error);
1491 static int start_special(DBusConnection *bus, char **args, unsigned n) {
1498 (streq(args[0], "halt") ||
1499 streq(args[0], "poweroff") ||
1500 streq(args[0], "reboot") ||
1501 streq(args[0], "kexec") ||
1502 streq(args[0], "exit")))
1503 return daemon_reload(bus, args, n);
1505 r = start_unit(bus, args, n);
1508 warn_wall(verb_to_action(args[0]));
1513 static int check_unit(DBusConnection *bus, char **args, unsigned n) {
1514 DBusMessage *m = NULL, *reply = NULL;
1516 *interface = "org.freedesktop.systemd1.Unit",
1517 *property = "ActiveState";
1518 int r = 3; /* According to LSB: "program is not running" */
1525 dbus_error_init(&error);
1527 for (i = 1; i < n; i++) {
1528 const char *path = NULL;
1530 DBusMessageIter iter, sub;
1532 if (!(m = dbus_message_new_method_call(
1533 "org.freedesktop.systemd1",
1534 "/org/freedesktop/systemd1",
1535 "org.freedesktop.systemd1.Manager",
1537 log_error("Could not allocate message.");
1542 if (!dbus_message_append_args(m,
1543 DBUS_TYPE_STRING, &args[i],
1544 DBUS_TYPE_INVALID)) {
1545 log_error("Could not append arguments to message.");
1550 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1552 /* Hmm, cannot figure out anything about this unit... */
1556 dbus_error_free(&error);
1557 dbus_message_unref(m);
1562 if (!dbus_message_get_args(reply, &error,
1563 DBUS_TYPE_OBJECT_PATH, &path,
1564 DBUS_TYPE_INVALID)) {
1565 log_error("Failed to parse reply: %s", bus_error_message(&error));
1570 dbus_message_unref(m);
1571 if (!(m = dbus_message_new_method_call(
1572 "org.freedesktop.systemd1",
1574 "org.freedesktop.DBus.Properties",
1576 log_error("Could not allocate message.");
1581 if (!dbus_message_append_args(m,
1582 DBUS_TYPE_STRING, &interface,
1583 DBUS_TYPE_STRING, &property,
1584 DBUS_TYPE_INVALID)) {
1585 log_error("Could not append arguments to message.");
1590 dbus_message_unref(reply);
1591 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1592 log_error("Failed to issue method call: %s", bus_error_message(&error));
1597 if (!dbus_message_iter_init(reply, &iter) ||
1598 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1599 log_error("Failed to parse reply.");
1604 dbus_message_iter_recurse(&iter, &sub);
1606 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1607 log_error("Failed to parse reply.");
1612 dbus_message_iter_get_basic(&sub, &state);
1617 if (streq(state, "active") || streq(state, "reloading"))
1620 dbus_message_unref(m);
1621 dbus_message_unref(reply);
1627 dbus_message_unref(m);
1630 dbus_message_unref(reply);
1632 dbus_error_free(&error);
1637 static int kill_unit(DBusConnection *bus, char **args, unsigned n) {
1638 DBusMessage *m = NULL, *reply = NULL;
1646 dbus_error_init(&error);
1649 arg_kill_who = "all";
1652 arg_kill_mode = streq(arg_kill_who, "all") ? "control-group" : "process";
1654 for (i = 1; i < n; i++) {
1656 if (!(m = dbus_message_new_method_call(
1657 "org.freedesktop.systemd1",
1658 "/org/freedesktop/systemd1",
1659 "org.freedesktop.systemd1.Manager",
1661 log_error("Could not allocate message.");
1666 if (!dbus_message_append_args(m,
1667 DBUS_TYPE_STRING, &args[i],
1668 DBUS_TYPE_STRING, &arg_kill_who,
1669 DBUS_TYPE_STRING, &arg_kill_mode,
1670 DBUS_TYPE_INT32, &arg_signal,
1671 DBUS_TYPE_INVALID)) {
1672 log_error("Could not append arguments to message.");
1677 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1678 log_error("Failed to issue method call: %s", bus_error_message(&error));
1679 dbus_error_free(&error);
1683 dbus_message_unref(m);
1686 dbus_message_unref(reply);
1692 dbus_message_unref(m);
1695 dbus_message_unref(reply);
1697 dbus_error_free(&error);
1702 typedef struct ExecStatusInfo {
1710 usec_t start_timestamp;
1711 usec_t exit_timestamp;
1716 LIST_FIELDS(struct ExecStatusInfo, exec);
1719 static void exec_status_info_free(ExecStatusInfo *i) {
1728 static int exec_status_info_deserialize(DBusMessageIter *sub, ExecStatusInfo *i) {
1729 uint64_t start_timestamp, exit_timestamp, start_timestamp_monotonic, exit_timestamp_monotonic;
1730 DBusMessageIter sub2, sub3;
1734 int32_t code, status;
1740 if (dbus_message_iter_get_arg_type(sub) != DBUS_TYPE_STRUCT)
1743 dbus_message_iter_recurse(sub, &sub2);
1745 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
1748 if (!(i->path = strdup(path)))
1751 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
1752 dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
1756 dbus_message_iter_recurse(&sub2, &sub3);
1757 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1758 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1759 dbus_message_iter_next(&sub3);
1764 if (!(i->argv = new0(char*, n+1)))
1768 dbus_message_iter_recurse(&sub2, &sub3);
1769 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1772 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1773 dbus_message_iter_get_basic(&sub3, &s);
1774 dbus_message_iter_next(&sub3);
1776 if (!(i->argv[n++] = strdup(s)))
1780 if (!dbus_message_iter_next(&sub2) ||
1781 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, true) < 0 ||
1782 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp, true) < 0 ||
1783 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp_monotonic, true) < 0 ||
1784 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp, true) < 0 ||
1785 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp_monotonic, true) < 0 ||
1786 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) < 0 ||
1787 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) < 0 ||
1788 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) < 0)
1792 i->start_timestamp = (usec_t) start_timestamp;
1793 i->exit_timestamp = (usec_t) exit_timestamp;
1794 i->pid = (pid_t) pid;
1801 typedef struct UnitStatusInfo {
1803 const char *load_state;
1804 const char *active_state;
1805 const char *sub_state;
1807 const char *description;
1808 const char *following;
1811 const char *default_control_group;
1813 usec_t inactive_exit_timestamp;
1814 usec_t active_enter_timestamp;
1815 usec_t active_exit_timestamp;
1816 usec_t inactive_enter_timestamp;
1818 bool need_daemon_reload;
1823 const char *status_text;
1825 #ifdef HAVE_SYSV_COMPAT
1829 usec_t start_timestamp;
1830 usec_t exit_timestamp;
1832 int exit_code, exit_status;
1834 usec_t condition_timestamp;
1835 bool condition_result;
1838 unsigned n_accepted;
1839 unsigned n_connections;
1843 const char *sysfs_path;
1845 /* Mount, Automount */
1851 LIST_HEAD(ExecStatusInfo, exec);
1854 static void print_status_info(UnitStatusInfo *i) {
1856 const char *on, *off, *ss;
1858 char since1[FORMAT_TIMESTAMP_PRETTY_MAX], *s1;
1859 char since2[FORMAT_TIMESTAMP_MAX], *s2;
1863 /* This shows pretty information about a unit. See
1864 * print_property() for a low-level property printer */
1866 printf("%s", strna(i->id));
1868 if (i->description && !streq_ptr(i->id, i->description))
1869 printf(" - %s", i->description);
1874 printf("\t Follow: unit currently follows state of %s\n", i->following);
1876 if (streq_ptr(i->load_state, "failed") ||
1877 streq_ptr(i->load_state, "banned")) {
1878 on = ansi_highlight(true);
1879 off = ansi_highlight(false);
1884 printf("\t Loaded: %s%s%s (%s)\n", on, strna(i->load_state), off, i->path);
1886 printf("\t Loaded: %s%s%s\n", on, strna(i->load_state), off);
1888 ss = streq_ptr(i->active_state, i->sub_state) ? NULL : i->sub_state;
1890 if (streq_ptr(i->active_state, "failed")) {
1891 on = ansi_highlight(true);
1892 off = ansi_highlight(false);
1893 } else if (streq_ptr(i->active_state, "active") || streq_ptr(i->active_state, "reloading")) {
1894 on = ansi_highlight_green(true);
1895 off = ansi_highlight_green(false);
1900 printf("\t Active: %s%s (%s)%s",
1902 strna(i->active_state),
1906 printf("\t Active: %s%s%s",
1908 strna(i->active_state),
1911 timestamp = (streq_ptr(i->active_state, "active") ||
1912 streq_ptr(i->active_state, "reloading")) ? i->active_enter_timestamp :
1913 (streq_ptr(i->active_state, "inactive") ||
1914 streq_ptr(i->active_state, "failed")) ? i->inactive_enter_timestamp :
1915 streq_ptr(i->active_state, "activating") ? i->inactive_exit_timestamp :
1916 i->active_exit_timestamp;
1918 s1 = format_timestamp_pretty(since1, sizeof(since1), timestamp);
1919 s2 = format_timestamp(since2, sizeof(since2), timestamp);
1922 printf(" since %s; %s\n", s2, s1);
1924 printf(" since %s\n", s2);
1928 if (!i->condition_result && i->condition_timestamp > 0) {
1929 s1 = format_timestamp_pretty(since1, sizeof(since1), i->condition_timestamp);
1930 s2 = format_timestamp(since2, sizeof(since2), i->condition_timestamp);
1933 printf("\t start condition failed at %s; %s\n", s2, s1);
1935 printf("\t start condition failed at %s\n", s2);
1939 printf("\t Device: %s\n", i->sysfs_path);
1941 printf("\t Where: %s\n", i->where);
1943 printf("\t What: %s\n", i->what);
1946 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
1948 LIST_FOREACH(exec, p, i->exec) {
1952 /* Only show exited processes here */
1956 t = strv_join(p->argv, " ");
1957 printf("\t Process: %u %s=%s ", p->pid, p->name, strna(t));
1960 #ifdef HAVE_SYSV_COMPAT
1962 good = is_clean_exit_lsb(p->code, p->status);
1965 good = is_clean_exit(p->code, p->status);
1968 on = ansi_highlight(true);
1969 off = ansi_highlight(false);
1973 printf("%s(code=%s, ", on, sigchld_code_to_string(p->code));
1975 if (p->code == CLD_EXITED) {
1978 printf("status=%i", p->status);
1980 #ifdef HAVE_SYSV_COMPAT
1981 if ((c = exit_status_to_string(p->status, i->is_sysv ? EXIT_STATUS_LSB : EXIT_STATUS_SYSTEMD)))
1983 if ((c = exit_status_to_string(p->status, EXIT_STATUS_SYSTEMD)))
1988 printf("signal=%s", signal_to_string(p->status));
1990 printf(")%s\n", off);
1994 if (i->main_pid == p->pid &&
1995 i->start_timestamp == p->start_timestamp &&
1996 i->exit_timestamp == p->start_timestamp)
1997 /* Let's not show this twice */
2000 if (p->pid == i->control_pid)
2004 if (i->main_pid > 0 || i->control_pid > 0) {
2007 if (i->main_pid > 0) {
2008 printf("Main PID: %u", (unsigned) i->main_pid);
2012 get_process_name(i->main_pid, &t);
2017 } else if (i->exit_code > 0) {
2018 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
2020 if (i->exit_code == CLD_EXITED) {
2023 printf("status=%i", i->exit_status);
2025 #ifdef HAVE_SYSV_COMPAT
2026 if ((c = exit_status_to_string(i->exit_status, i->is_sysv ? EXIT_STATUS_LSB : EXIT_STATUS_SYSTEMD)))
2028 if ((c = exit_status_to_string(i->exit_status, EXIT_STATUS_SYSTEMD)))
2033 printf("signal=%s", signal_to_string(i->exit_status));
2038 if (i->main_pid > 0 && i->control_pid > 0)
2041 if (i->control_pid > 0) {
2044 printf(" Control: %u", (unsigned) i->control_pid);
2046 get_process_name(i->control_pid, &t);
2057 printf("\t Status: \"%s\"\n", i->status_text);
2059 if (i->default_control_group) {
2062 printf("\t CGroup: %s\n", i->default_control_group);
2064 if (arg_transport != TRANSPORT_SSH) {
2065 if ((c = columns()) > 18)
2070 show_cgroup_by_path(i->default_control_group, "\t\t ", c);
2074 if (i->need_daemon_reload)
2075 printf("\n%sWarning:%s Unit file changed on disk, 'systemctl %s daemon-reload' recommended.\n",
2076 ansi_highlight(true),
2077 ansi_highlight(false),
2078 arg_user ? "--user" : "--system");
2081 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
2087 switch (dbus_message_iter_get_arg_type(iter)) {
2089 case DBUS_TYPE_STRING: {
2092 dbus_message_iter_get_basic(iter, &s);
2095 if (streq(name, "Id"))
2097 else if (streq(name, "LoadState"))
2099 else if (streq(name, "ActiveState"))
2100 i->active_state = s;
2101 else if (streq(name, "SubState"))
2103 else if (streq(name, "Description"))
2105 else if (streq(name, "FragmentPath"))
2107 #ifdef HAVE_SYSV_COMPAT
2108 else if (streq(name, "SysVPath")) {
2113 else if (streq(name, "DefaultControlGroup"))
2114 i->default_control_group = s;
2115 else if (streq(name, "StatusText"))
2117 else if (streq(name, "SysFSPath"))
2119 else if (streq(name, "Where"))
2121 else if (streq(name, "What"))
2123 else if (streq(name, "Following"))
2130 case DBUS_TYPE_BOOLEAN: {
2133 dbus_message_iter_get_basic(iter, &b);
2135 if (streq(name, "Accept"))
2137 else if (streq(name, "NeedDaemonReload"))
2138 i->need_daemon_reload = b;
2139 else if (streq(name, "ConditionResult"))
2140 i->condition_result = b;
2145 case DBUS_TYPE_UINT32: {
2148 dbus_message_iter_get_basic(iter, &u);
2150 if (streq(name, "MainPID")) {
2152 i->main_pid = (pid_t) u;
2155 } else if (streq(name, "ControlPID"))
2156 i->control_pid = (pid_t) u;
2157 else if (streq(name, "ExecMainPID")) {
2159 i->main_pid = (pid_t) u;
2160 } else if (streq(name, "NAccepted"))
2162 else if (streq(name, "NConnections"))
2163 i->n_connections = u;
2168 case DBUS_TYPE_INT32: {
2171 dbus_message_iter_get_basic(iter, &j);
2173 if (streq(name, "ExecMainCode"))
2174 i->exit_code = (int) j;
2175 else if (streq(name, "ExecMainStatus"))
2176 i->exit_status = (int) j;
2181 case DBUS_TYPE_UINT64: {
2184 dbus_message_iter_get_basic(iter, &u);
2186 if (streq(name, "ExecMainStartTimestamp"))
2187 i->start_timestamp = (usec_t) u;
2188 else if (streq(name, "ExecMainExitTimestamp"))
2189 i->exit_timestamp = (usec_t) u;
2190 else if (streq(name, "ActiveEnterTimestamp"))
2191 i->active_enter_timestamp = (usec_t) u;
2192 else if (streq(name, "InactiveEnterTimestamp"))
2193 i->inactive_enter_timestamp = (usec_t) u;
2194 else if (streq(name, "InactiveExitTimestamp"))
2195 i->inactive_exit_timestamp = (usec_t) u;
2196 else if (streq(name, "ActiveExitTimestamp"))
2197 i->active_exit_timestamp = (usec_t) u;
2198 else if (streq(name, "ConditionTimestamp"))
2199 i->condition_timestamp = (usec_t) u;
2204 case DBUS_TYPE_ARRAY: {
2206 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT &&
2207 startswith(name, "Exec")) {
2208 DBusMessageIter sub;
2210 dbus_message_iter_recurse(iter, &sub);
2211 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2212 ExecStatusInfo *info;
2215 if (!(info = new0(ExecStatusInfo, 1)))
2218 if (!(info->name = strdup(name))) {
2223 if ((r = exec_status_info_deserialize(&sub, info)) < 0) {
2228 LIST_PREPEND(ExecStatusInfo, exec, i->exec, info);
2230 dbus_message_iter_next(&sub);
2241 static int print_property(const char *name, DBusMessageIter *iter) {
2245 /* This is a low-level property printer, see
2246 * print_status_info() for the nicer output */
2248 if (arg_property && !strv_find(arg_property, name))
2251 switch (dbus_message_iter_get_arg_type(iter)) {
2253 case DBUS_TYPE_STRUCT: {
2254 DBusMessageIter sub;
2255 dbus_message_iter_recurse(iter, &sub);
2257 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
2260 dbus_message_iter_get_basic(&sub, &u);
2263 printf("%s=%u\n", name, (unsigned) u);
2265 printf("%s=\n", name);
2268 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
2271 dbus_message_iter_get_basic(&sub, &s);
2273 if (arg_all || s[0])
2274 printf("%s=%s\n", name, s);
2282 case DBUS_TYPE_ARRAY:
2284 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "EnvironmentFiles")) {
2285 DBusMessageIter sub, sub2;
2287 dbus_message_iter_recurse(iter, &sub);
2288 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2292 dbus_message_iter_recurse(&sub, &sub2);
2294 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) >= 0 &&
2295 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, false) >= 0)
2296 printf("EnvironmentFile=%s (ignore=%s)\n", path, yes_no(ignore));
2298 dbus_message_iter_next(&sub);
2303 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
2304 DBusMessageIter sub, sub2;
2306 dbus_message_iter_recurse(iter, &sub);
2307 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2308 const char *type, *path;
2310 dbus_message_iter_recurse(&sub, &sub2);
2312 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
2313 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
2314 printf("%s=%s\n", type, path);
2316 dbus_message_iter_next(&sub);
2321 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
2322 DBusMessageIter sub, sub2;
2324 dbus_message_iter_recurse(iter, &sub);
2325 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2327 uint64_t value, next_elapse;
2329 dbus_message_iter_recurse(&sub, &sub2);
2331 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
2332 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
2333 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
2334 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
2336 printf("%s={ value=%s ; next_elapse=%s }\n",
2338 format_timespan(timespan1, sizeof(timespan1), value),
2339 format_timespan(timespan2, sizeof(timespan2), next_elapse));
2342 dbus_message_iter_next(&sub);
2347 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
2348 DBusMessageIter sub;
2350 dbus_message_iter_recurse(iter, &sub);
2351 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2352 ExecStatusInfo info;
2355 if (exec_status_info_deserialize(&sub, &info) >= 0) {
2356 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
2359 t = strv_join(info.argv, " ");
2361 printf("%s={ path=%s ; argv[]=%s ; ignore=%s ; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n",
2365 yes_no(info.ignore),
2366 strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)),
2367 strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)),
2368 (unsigned) info. pid,
2369 sigchld_code_to_string(info.code),
2371 info.code == CLD_EXITED ? "" : "/",
2372 strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status)));
2378 strv_free(info.argv);
2380 dbus_message_iter_next(&sub);
2389 if (generic_print_property(name, iter, arg_all) > 0)
2393 printf("%s=[unprintable]\n", name);
2398 static int show_one(const char *verb, DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
2399 DBusMessage *m = NULL, *reply = NULL;
2400 const char *interface = "";
2403 DBusMessageIter iter, sub, sub2, sub3;
2404 UnitStatusInfo info;
2412 dbus_error_init(&error);
2414 if (!(m = dbus_message_new_method_call(
2415 "org.freedesktop.systemd1",
2417 "org.freedesktop.DBus.Properties",
2419 log_error("Could not allocate message.");
2424 if (!dbus_message_append_args(m,
2425 DBUS_TYPE_STRING, &interface,
2426 DBUS_TYPE_INVALID)) {
2427 log_error("Could not append arguments to message.");
2432 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2433 log_error("Failed to issue method call: %s", bus_error_message(&error));
2438 if (!dbus_message_iter_init(reply, &iter) ||
2439 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
2440 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
2441 log_error("Failed to parse reply.");
2446 dbus_message_iter_recurse(&iter, &sub);
2453 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2456 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
2457 log_error("Failed to parse reply.");
2462 dbus_message_iter_recurse(&sub, &sub2);
2464 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
2465 log_error("Failed to parse reply.");
2470 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
2471 log_error("Failed to parse reply.");
2476 dbus_message_iter_recurse(&sub2, &sub3);
2478 if (show_properties)
2479 r = print_property(name, &sub3);
2481 r = status_property(name, &sub3, &info);
2484 log_error("Failed to parse reply.");
2489 dbus_message_iter_next(&sub);
2494 if (!show_properties)
2495 print_status_info(&info);
2497 if (!streq_ptr(info.active_state, "active") &&
2498 !streq_ptr(info.active_state, "reloading") &&
2499 streq(verb, "status"))
2500 /* According to LSB: "program not running" */
2503 while ((p = info.exec)) {
2504 LIST_REMOVE(ExecStatusInfo, exec, info.exec, p);
2505 exec_status_info_free(p);
2510 dbus_message_unref(m);
2513 dbus_message_unref(reply);
2515 dbus_error_free(&error);
2520 static int show(DBusConnection *bus, char **args, unsigned n) {
2521 DBusMessage *m = NULL, *reply = NULL;
2525 bool show_properties, new_line = false;
2530 dbus_error_init(&error);
2532 show_properties = !streq(args[0], "status");
2534 if (show_properties)
2535 pager_open_if_enabled();
2537 if (show_properties && n <= 1) {
2538 /* If not argument is specified inspect the manager
2541 ret = show_one(args[0], bus, "/org/freedesktop/systemd1", show_properties, &new_line);
2545 for (i = 1; i < n; i++) {
2546 const char *path = NULL;
2549 if (safe_atou32(args[i], &id) < 0) {
2551 /* Interpret as unit name */
2553 if (!(m = dbus_message_new_method_call(
2554 "org.freedesktop.systemd1",
2555 "/org/freedesktop/systemd1",
2556 "org.freedesktop.systemd1.Manager",
2558 log_error("Could not allocate message.");
2563 if (!dbus_message_append_args(m,
2564 DBUS_TYPE_STRING, &args[i],
2565 DBUS_TYPE_INVALID)) {
2566 log_error("Could not append arguments to message.");
2571 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2573 if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
2574 log_error("Failed to issue method call: %s", bus_error_message(&error));
2579 dbus_error_free(&error);
2581 dbus_message_unref(m);
2582 if (!(m = dbus_message_new_method_call(
2583 "org.freedesktop.systemd1",
2584 "/org/freedesktop/systemd1",
2585 "org.freedesktop.systemd1.Manager",
2587 log_error("Could not allocate message.");
2592 if (!dbus_message_append_args(m,
2593 DBUS_TYPE_STRING, &args[i],
2594 DBUS_TYPE_INVALID)) {
2595 log_error("Could not append arguments to message.");
2600 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2601 log_error("Failed to issue method call: %s", bus_error_message(&error));
2603 if (dbus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT))
2604 ret = 4; /* According to LSB: "program or service status is unknown" */
2611 } else if (show_properties) {
2613 /* Interpret as job id */
2615 if (!(m = dbus_message_new_method_call(
2616 "org.freedesktop.systemd1",
2617 "/org/freedesktop/systemd1",
2618 "org.freedesktop.systemd1.Manager",
2620 log_error("Could not allocate message.");
2625 if (!dbus_message_append_args(m,
2626 DBUS_TYPE_UINT32, &id,
2627 DBUS_TYPE_INVALID)) {
2628 log_error("Could not append arguments to message.");
2633 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2634 log_error("Failed to issue method call: %s", bus_error_message(&error));
2640 /* Interpret as PID */
2642 if (!(m = dbus_message_new_method_call(
2643 "org.freedesktop.systemd1",
2644 "/org/freedesktop/systemd1",
2645 "org.freedesktop.systemd1.Manager",
2647 log_error("Could not allocate message.");
2652 if (!dbus_message_append_args(m,
2653 DBUS_TYPE_UINT32, &id,
2654 DBUS_TYPE_INVALID)) {
2655 log_error("Could not append arguments to message.");
2660 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2661 log_error("Failed to issue method call: %s", bus_error_message(&error));
2667 if (!dbus_message_get_args(reply, &error,
2668 DBUS_TYPE_OBJECT_PATH, &path,
2669 DBUS_TYPE_INVALID)) {
2670 log_error("Failed to parse reply: %s", bus_error_message(&error));
2675 if ((r = show_one(args[0], bus, path, show_properties, &new_line)) != 0)
2678 dbus_message_unref(m);
2679 dbus_message_unref(reply);
2685 dbus_message_unref(m);
2688 dbus_message_unref(reply);
2690 dbus_error_free(&error);
2695 static int dump(DBusConnection *bus, char **args, unsigned n) {
2696 DBusMessage *m = NULL, *reply = NULL;
2701 dbus_error_init(&error);
2703 pager_open_if_enabled();
2705 if (!(m = dbus_message_new_method_call(
2706 "org.freedesktop.systemd1",
2707 "/org/freedesktop/systemd1",
2708 "org.freedesktop.systemd1.Manager",
2710 log_error("Could not allocate message.");
2714 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2715 log_error("Failed to issue method call: %s", bus_error_message(&error));
2720 if (!dbus_message_get_args(reply, &error,
2721 DBUS_TYPE_STRING, &text,
2722 DBUS_TYPE_INVALID)) {
2723 log_error("Failed to parse reply: %s", bus_error_message(&error));
2728 fputs(text, stdout);
2734 dbus_message_unref(m);
2737 dbus_message_unref(reply);
2739 dbus_error_free(&error);
2744 static int snapshot(DBusConnection *bus, char **args, unsigned n) {
2745 DBusMessage *m = NULL, *reply = NULL;
2748 const char *name = "", *path, *id;
2749 dbus_bool_t cleanup = FALSE;
2750 DBusMessageIter iter, sub;
2752 *interface = "org.freedesktop.systemd1.Unit",
2755 dbus_error_init(&error);
2757 if (!(m = dbus_message_new_method_call(
2758 "org.freedesktop.systemd1",
2759 "/org/freedesktop/systemd1",
2760 "org.freedesktop.systemd1.Manager",
2761 "CreateSnapshot"))) {
2762 log_error("Could not allocate message.");
2769 if (!dbus_message_append_args(m,
2770 DBUS_TYPE_STRING, &name,
2771 DBUS_TYPE_BOOLEAN, &cleanup,
2772 DBUS_TYPE_INVALID)) {
2773 log_error("Could not append arguments to message.");
2778 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2779 log_error("Failed to issue method call: %s", bus_error_message(&error));
2784 if (!dbus_message_get_args(reply, &error,
2785 DBUS_TYPE_OBJECT_PATH, &path,
2786 DBUS_TYPE_INVALID)) {
2787 log_error("Failed to parse reply: %s", bus_error_message(&error));
2792 dbus_message_unref(m);
2793 if (!(m = dbus_message_new_method_call(
2794 "org.freedesktop.systemd1",
2796 "org.freedesktop.DBus.Properties",
2798 log_error("Could not allocate message.");
2802 if (!dbus_message_append_args(m,
2803 DBUS_TYPE_STRING, &interface,
2804 DBUS_TYPE_STRING, &property,
2805 DBUS_TYPE_INVALID)) {
2806 log_error("Could not append arguments to message.");
2811 dbus_message_unref(reply);
2812 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2813 log_error("Failed to issue method call: %s", bus_error_message(&error));
2818 if (!dbus_message_iter_init(reply, &iter) ||
2819 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2820 log_error("Failed to parse reply.");
2825 dbus_message_iter_recurse(&iter, &sub);
2827 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2828 log_error("Failed to parse reply.");
2833 dbus_message_iter_get_basic(&sub, &id);
2841 dbus_message_unref(m);
2844 dbus_message_unref(reply);
2846 dbus_error_free(&error);
2851 static int delete_snapshot(DBusConnection *bus, char **args, unsigned n) {
2852 DBusMessage *m = NULL, *reply = NULL;
2860 dbus_error_init(&error);
2862 for (i = 1; i < n; i++) {
2863 const char *path = NULL;
2865 if (!(m = dbus_message_new_method_call(
2866 "org.freedesktop.systemd1",
2867 "/org/freedesktop/systemd1",
2868 "org.freedesktop.systemd1.Manager",
2870 log_error("Could not allocate message.");
2875 if (!dbus_message_append_args(m,
2876 DBUS_TYPE_STRING, &args[i],
2877 DBUS_TYPE_INVALID)) {
2878 log_error("Could not append arguments to message.");
2883 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2884 log_error("Failed to issue method call: %s", bus_error_message(&error));
2889 if (!dbus_message_get_args(reply, &error,
2890 DBUS_TYPE_OBJECT_PATH, &path,
2891 DBUS_TYPE_INVALID)) {
2892 log_error("Failed to parse reply: %s", bus_error_message(&error));
2897 dbus_message_unref(m);
2898 if (!(m = dbus_message_new_method_call(
2899 "org.freedesktop.systemd1",
2901 "org.freedesktop.systemd1.Snapshot",
2903 log_error("Could not allocate message.");
2908 dbus_message_unref(reply);
2909 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2910 log_error("Failed to issue method call: %s", bus_error_message(&error));
2915 dbus_message_unref(m);
2916 dbus_message_unref(reply);
2924 dbus_message_unref(m);
2927 dbus_message_unref(reply);
2929 dbus_error_free(&error);
2934 static int daemon_reload(DBusConnection *bus, char **args, unsigned n) {
2935 DBusMessage *m = NULL, *reply = NULL;
2940 dbus_error_init(&error);
2942 if (arg_action == ACTION_RELOAD)
2944 else if (arg_action == ACTION_REEXEC)
2945 method = "Reexecute";
2947 assert(arg_action == ACTION_SYSTEMCTL);
2950 streq(args[0], "clear-jobs") ||
2951 streq(args[0], "cancel") ? "ClearJobs" :
2952 streq(args[0], "daemon-reexec") ? "Reexecute" :
2953 streq(args[0], "reset-failed") ? "ResetFailed" :
2954 streq(args[0], "halt") ? "Halt" :
2955 streq(args[0], "poweroff") ? "PowerOff" :
2956 streq(args[0], "reboot") ? "Reboot" :
2957 streq(args[0], "kexec") ? "KExec" :
2958 streq(args[0], "exit") ? "Exit" :
2959 /* "daemon-reload" */ "Reload";
2962 if (!(m = dbus_message_new_method_call(
2963 "org.freedesktop.systemd1",
2964 "/org/freedesktop/systemd1",
2965 "org.freedesktop.systemd1.Manager",
2967 log_error("Could not allocate message.");
2971 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2973 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
2974 /* There's always a fallback possible for
2975 * legacy actions. */
2980 if (streq(method, "Reexecute") && dbus_error_has_name(&error, DBUS_ERROR_NO_REPLY)) {
2981 /* On reexecution, we expect a disconnect, not
2987 log_error("Failed to issue method call: %s", bus_error_message(&error));
2996 dbus_message_unref(m);
2999 dbus_message_unref(reply);
3001 dbus_error_free(&error);
3006 static int reset_failed(DBusConnection *bus, char **args, unsigned n) {
3007 DBusMessage *m = NULL, *reply = NULL;
3013 dbus_error_init(&error);
3016 return daemon_reload(bus, args, n);
3018 for (i = 1; i < n; i++) {
3020 if (!(m = dbus_message_new_method_call(
3021 "org.freedesktop.systemd1",
3022 "/org/freedesktop/systemd1",
3023 "org.freedesktop.systemd1.Manager",
3024 "ResetFailedUnit"))) {
3025 log_error("Could not allocate message.");
3030 if (!dbus_message_append_args(m,
3031 DBUS_TYPE_STRING, args + i,
3032 DBUS_TYPE_INVALID)) {
3033 log_error("Could not append arguments to message.");
3038 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3039 log_error("Failed to issue method call: %s", bus_error_message(&error));
3044 dbus_message_unref(m);
3045 dbus_message_unref(reply);
3053 dbus_message_unref(m);
3056 dbus_message_unref(reply);
3058 dbus_error_free(&error);
3063 static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
3064 DBusMessage *m = NULL, *reply = NULL;
3066 DBusMessageIter iter, sub, sub2;
3069 *interface = "org.freedesktop.systemd1.Manager",
3070 *property = "Environment";
3072 dbus_error_init(&error);
3074 pager_open_if_enabled();
3076 if (!(m = dbus_message_new_method_call(
3077 "org.freedesktop.systemd1",
3078 "/org/freedesktop/systemd1",
3079 "org.freedesktop.DBus.Properties",
3081 log_error("Could not allocate message.");
3085 if (!dbus_message_append_args(m,
3086 DBUS_TYPE_STRING, &interface,
3087 DBUS_TYPE_STRING, &property,
3088 DBUS_TYPE_INVALID)) {
3089 log_error("Could not append arguments to message.");
3094 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3095 log_error("Failed to issue method call: %s", bus_error_message(&error));
3100 if (!dbus_message_iter_init(reply, &iter) ||
3101 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
3102 log_error("Failed to parse reply.");
3107 dbus_message_iter_recurse(&iter, &sub);
3109 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
3110 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
3111 log_error("Failed to parse reply.");
3116 dbus_message_iter_recurse(&sub, &sub2);
3118 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
3121 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
3122 log_error("Failed to parse reply.");
3127 dbus_message_iter_get_basic(&sub2, &text);
3128 printf("%s\n", text);
3130 dbus_message_iter_next(&sub2);
3137 dbus_message_unref(m);
3140 dbus_message_unref(reply);
3142 dbus_error_free(&error);
3147 static int set_environment(DBusConnection *bus, char **args, unsigned n) {
3148 DBusMessage *m = NULL, *reply = NULL;
3152 DBusMessageIter iter, sub;
3155 dbus_error_init(&error);
3157 method = streq(args[0], "set-environment")
3159 : "UnsetEnvironment";
3161 if (!(m = dbus_message_new_method_call(
3162 "org.freedesktop.systemd1",
3163 "/org/freedesktop/systemd1",
3164 "org.freedesktop.systemd1.Manager",
3167 log_error("Could not allocate message.");
3171 dbus_message_iter_init_append(m, &iter);
3173 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
3174 log_error("Could not append arguments to message.");
3179 for (i = 1; i < n; i++)
3180 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) {
3181 log_error("Could not append arguments to message.");
3186 if (!dbus_message_iter_close_container(&iter, &sub)) {
3187 log_error("Could not append arguments to message.");
3192 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3193 log_error("Failed to issue method call: %s", bus_error_message(&error));
3202 dbus_message_unref(m);
3205 dbus_message_unref(reply);
3207 dbus_error_free(&error);
3220 static Hashmap *will_install = NULL, *have_installed = NULL;
3221 static Set *remove_symlinks_to = NULL;
3222 static unsigned n_symlinks = 0;
3224 static void install_info_free(InstallInfo *i) {
3229 strv_free(i->aliases);
3230 strv_free(i->wanted_by);
3234 static void install_info_hashmap_free(Hashmap *m) {
3237 while ((i = hashmap_steal_first(m)))
3238 install_info_free(i);
3243 static int install_info_add(const char *name) {
3247 assert(will_install);
3249 if (!unit_name_is_valid_no_type(name, true)) {
3250 log_warning("Unit name %s is not a valid unit name.", name);
3254 if (hashmap_get(have_installed, name) ||
3255 hashmap_get(will_install, name))
3258 if (!(i = new0(InstallInfo, 1))) {
3263 if (!(i->name = strdup(name))) {
3268 if ((r = hashmap_put(will_install, i->name, i)) < 0)
3275 install_info_free(i);
3280 static int config_parse_also(
3281 const char *filename,
3283 const char *section,
3298 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
3302 if (!(n = strndup(w, l)))
3305 if ((r = install_info_add(n)) < 0) {
3306 log_warning("Cannot install unit %s: %s", n, strerror(-r));
3317 static int mark_symlink_for_removal(const char *p) {
3322 assert(path_is_absolute(p));
3324 if (!remove_symlinks_to)
3327 if (!(n = strdup(p)))
3330 path_kill_slashes(n);
3332 if ((r = set_put(remove_symlinks_to, n)) < 0) {
3334 return r == -EEXIST ? 0 : r;
3340 static int remove_marked_symlinks_fd(int fd, const char *config_path, const char *root, bool *deleted) {
3349 if (!(d = fdopendir(fd))) {
3350 close_nointr_nofail(fd);
3356 while ((de = readdir(d))) {
3357 bool is_dir = false, is_link = false;
3359 if (ignore_file(de->d_name))
3362 if (de->d_type == DT_LNK)
3364 else if (de->d_type == DT_DIR)
3366 else if (de->d_type == DT_UNKNOWN) {
3369 if (fstatat(fd, de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) {
3370 log_error("Failed to stat %s/%s: %m", root, de->d_name);
3377 is_link = S_ISLNK(st.st_mode);
3378 is_dir = S_ISDIR(st.st_mode);
3386 if ((nfd = openat(fd, de->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW)) < 0) {
3387 log_error("Failed to open %s/%s: %m", root, de->d_name);
3394 if (asprintf(&p, "%s/%s", root, de->d_name) < 0) {
3395 log_error("Failed to allocate directory string.");
3396 close_nointr_nofail(nfd);
3401 /* This will close nfd, regardless whether it succeeds or not */
3402 q = remove_marked_symlinks_fd(nfd, config_path, p, deleted);
3408 } else if (is_link) {
3412 if (asprintf(&p, "%s/%s", root, de->d_name) < 0) {
3413 log_error("Failed to allocate symlink string.");
3418 if ((q = readlink_and_make_absolute(p, &dest)) < 0) {
3419 log_error("Cannot read symlink %s: %s", p, strerror(-q));
3427 if ((c = canonicalize_file_name(dest))) {
3428 /* This might fail if the destination
3429 * is already removed */
3435 path_kill_slashes(dest);
3436 if (set_get(remove_symlinks_to, dest)) {
3439 log_info("rm '%s'", p);
3441 if (unlink(p) < 0) {
3442 log_error("Cannot unlink symlink %s: %m", p);
3447 rmdir_parents(p, config_path);
3448 path_kill_slashes(p);
3450 if (!set_get(remove_symlinks_to, p)) {
3452 if ((r = mark_symlink_for_removal(p)) < 0) {
3471 static int remove_marked_symlinks(const char *config_path) {
3475 assert(config_path);
3477 if (set_size(remove_symlinks_to) <= 0)
3480 if ((fd = open(config_path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW)) < 0)
3487 if ((cfd = dup(fd)) < 0) {
3492 /* This takes possession of cfd and closes it */
3493 if ((q = remove_marked_symlinks_fd(cfd, config_path, config_path, &deleted)) < 0) {
3499 close_nointr_nofail(fd);
3504 static int create_symlink(const char *verb, const char *orig_old_path, const char *new_path) {
3506 const char *old_path;
3509 old_path = orig_old_path+strlen(arg_root);
3511 old_path = orig_old_path;
3517 if (streq(verb, "enable")) {
3520 mkdir_parents(new_path, 0755);
3522 if (symlink(old_path, new_path) >= 0) {
3525 log_info("ln -s '%s' '%s'", old_path, new_path);
3530 if (errno != EEXIST) {
3531 log_error("Cannot link %s to %s: %m", old_path, new_path);
3535 if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
3537 if (errno == EINVAL) {
3538 log_error("Cannot link %s to %s, file exists already and is not a symlink.", old_path, new_path);
3542 log_error("readlink() failed: %s", strerror(-r));
3546 if (streq(dest, old_path)) {
3552 log_error("Cannot link %s to %s, symlink exists already and points to %s.", old_path, new_path, dest);
3561 log_info("ln -s '%s' '%s'", old_path, new_path);
3563 if (symlink(old_path, new_path) >= 0)
3566 log_error("Cannot link %s to %s: %m", old_path, new_path);
3569 } else if (streq(verb, "disable")) {
3572 if ((r = mark_symlink_for_removal(old_path)) < 0)
3575 if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
3576 if (errno == ENOENT)
3579 if (errno == EINVAL) {
3580 log_warning("File %s not a symlink, ignoring.", old_path);
3584 log_error("readlink() failed: %s", strerror(-r));
3588 if (!streq(dest, old_path)) {
3589 log_warning("File %s not a symlink to %s but points to %s, ignoring.", new_path, old_path, dest);
3596 if ((r = mark_symlink_for_removal(new_path)) < 0)
3600 log_info("rm '%s'", new_path);
3602 if (unlink(new_path) >= 0)
3605 log_error("Cannot unlink %s: %m", new_path);
3608 } else if (streq(verb, "is-enabled")) {
3611 if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
3613 if (errno == ENOENT || errno == EINVAL)
3616 log_error("readlink() failed: %s", strerror(-r));
3620 if (streq(dest, old_path)) {
3629 assert_not_reached("Unknown action.");
3632 static int install_info_symlink_alias(const char *verb, InstallInfo *i, const char *config_path) {
3634 char *alias_path = NULL;
3639 assert(config_path);
3641 STRV_FOREACH(s, i->aliases) {
3644 if (!(alias_path = path_make_absolute(*s, config_path))) {
3645 log_error("Out of memory");
3650 if ((r = create_symlink(verb, i->path, alias_path)) != 0)
3653 if (streq(verb, "disable"))
3654 rmdir_parents(alias_path, config_path);
3664 static int install_info_symlink_wants(const char *verb, InstallInfo *i, const char *config_path) {
3666 char *alias_path = NULL;
3671 assert(config_path);
3673 STRV_FOREACH(s, i->wanted_by) {
3674 if (!unit_name_is_valid_no_type(*s, true)) {
3675 log_error("Invalid name %s.", *s);
3683 if (asprintf(&alias_path, "%s/%s.wants/%s", config_path, *s, i->name) < 0) {
3684 log_error("Out of memory");
3689 if ((r = create_symlink(verb, i->path, alias_path)) != 0)
3692 if (streq(verb, "disable"))
3693 rmdir_parents(alias_path, config_path);
3704 static int install_info_apply(const char *verb, LookupPaths *paths, InstallInfo *i, const char *config_path) {
3706 const ConfigItem items[] = {
3707 { "Alias", config_parse_strv, 0, &i->aliases, "Install" },
3708 { "WantedBy", config_parse_strv, 0, &i->wanted_by, "Install" },
3709 { "Also", config_parse_also, 0, NULL, "Install" },
3711 { NULL, NULL, 0, NULL, NULL }
3715 char *filename = NULL;
3722 STRV_FOREACH(p, paths->unit_path) {
3724 char *path, *should_free;
3727 should_free = path = strappend(arg_root, *p);
3733 if (!(filename = path_make_absolute(i->name, path))) {
3734 log_error("Out of memory");
3741 /* Ensure that we don't follow symlinks */
3742 if ((fd = open(filename, O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NOCTTY)) >= 0)
3743 if ((f = fdopen(fd, "re")))
3746 if (errno == ELOOP) {
3747 log_error("Refusing to operate on symlinks, please pass unit names or absolute paths to unit files.");
3752 if (errno != ENOENT) {
3753 log_error("Failed to open %s: %m", filename);
3763 #if (defined(TARGET_FEDORA) || defined(TARGET_MANDRIVA) || defined(TARGET_SUSE) || defined(TARGET_MEEGO) || defined(TARGET_ALTLINUX)) && defined (HAVE_SYSV_COMPAT)
3765 if (endswith(i->name, ".service")) {
3769 if (asprintf(&sysv, SYSTEM_SYSVINIT_PATH "/%s", i->name) < 0) {
3770 log_error("Out of memory");
3774 sysv[strlen(sysv) - sizeof(".service") + 1] = 0;
3778 tmp_path = strappend (arg_root, sysv);
3779 exists = access (tmp_path, F_OK) >= 0;
3782 exists = access(sysv, F_OK) >= 0;
3788 const char *argv[] = {
3796 log_info("%s is not a native service, redirecting to /sbin/chkconfig.", i->name);
3798 argv[1] = file_name_from_path(sysv);
3800 streq(verb, "enable") ? "on" :
3801 streq(verb, "disable") ? "off" : "--level=5";
3803 argv[3] = strappend("--root=", arg_root);
3805 log_info("Executing %s %s %s %s", argv[0], argv[1], strempty(argv[2]), strempty(argv[3]));
3807 if ((pid = fork()) < 0) {
3808 log_error("Failed to fork: %m");
3811 } else if (pid == 0) {
3812 execv(argv[0], (char**) argv);
3813 _exit(EXIT_FAILURE);
3818 if ((r = wait_for_terminate(pid, &status)) < 0)
3821 if (status.si_code == CLD_EXITED) {
3823 if (streq(verb, "is-enabled"))
3824 return status.si_status == 0 ? 1 : 0;
3826 if (status.si_status == 0)
3829 return status.si_status == 0 ? 0 : -EINVAL;
3840 log_error("Couldn't find %s.", i->name);
3846 if ((r = config_parse(filename, f, NULL, items, true, i)) < 0) {
3851 /* Consider unit files stored in /lib and /usr always enabled
3852 * if they have no [Install] data. */
3853 if (streq(verb, "is-enabled") &&
3854 strv_isempty(i->aliases) &&
3855 strv_isempty(i->wanted_by) &&
3856 !path_startswith(filename, "/etc")) {
3861 n_symlinks += strv_length(i->aliases);
3862 n_symlinks += strv_length(i->wanted_by);
3866 if ((r = install_info_symlink_alias(verb, i, config_path)) != 0)
3869 if ((r = install_info_symlink_wants(verb, i, config_path)) != 0)
3872 if ((r = mark_symlink_for_removal(filename)) < 0)
3875 if ((r = remove_marked_symlinks(config_path)) < 0)
3881 static char *get_config_path(void) {
3884 if (arg_user && arg_global)
3885 ret = strdup(USER_CONFIG_UNIT_PATH);
3888 if (user_config_home(&ret) < 0)
3892 ret = strdup(SYSTEM_CONFIG_UNIT_PATH);
3895 p = strappend (arg_root, ret);
3902 static int enable_unit(DBusConnection *bus, char **args, unsigned n) {
3906 char *config_path = NULL;
3909 const char *verb = args[0];
3911 dbus_error_init(&error);
3914 if ((r = lookup_paths_init(&paths, arg_user ? MANAGER_USER : MANAGER_SYSTEM, true)) < 0) {
3915 log_error("Failed to determine lookup paths: %s", strerror(-r));
3919 if (!(config_path = get_config_path())) {
3920 log_error("Failed to determine config path");
3925 will_install = hashmap_new(string_hash_func, string_compare_func);
3926 have_installed = hashmap_new(string_hash_func, string_compare_func);
3928 if (!will_install || !have_installed) {
3929 log_error("Failed to allocate unit sets.");
3934 if (!arg_defaults && streq(verb, "disable"))
3935 if (!(remove_symlinks_to = set_new(string_hash_func, string_compare_func))) {
3936 log_error("Failed to allocate symlink sets.");
3941 for (j = 1; j < n; j++)
3942 if ((r = install_info_add(args[j])) < 0) {
3943 log_warning("Cannot install unit %s: %s", args[j], strerror(-r));
3949 while ((i = hashmap_first(will_install))) {
3952 assert_se(hashmap_move_one(have_installed, will_install, i->name) == 0);
3954 if ((q = install_info_apply(verb, &paths, i, config_path)) != 0) {
3962 /* In test mode and found something */
3968 if (streq(verb, "is-enabled"))
3969 r = r > 0 ? 0 : -ENOENT;
3971 if (n_symlinks <= 0)
3972 log_warning("Unit files contain no applicable installation information. Ignoring.");
3975 /* Don't try to reload anything if the user asked us to not do this */
3977 /* Don't try to reload anything when updating a unit globally */
3979 /* Don't try to reload anything if we are called for system changes but the system wasn't booted with systemd */
3980 (arg_user || sd_booted() > 0) &&
3981 /* Don't try to reload anything if we are running in a chroot environment */
3982 (arg_user || running_in_chroot() <= 0 || arg_root) ) {
3985 if ((q = daemon_reload(bus, args, n)) < 0)
3991 install_info_hashmap_free(will_install);
3992 install_info_hashmap_free(have_installed);
3994 set_free_free(remove_symlinks_to);
3996 lookup_paths_free(&paths);
4003 static int systemctl_help(void) {
4005 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
4006 "Send control commands to or query the systemd manager.\n\n"
4007 " -h --help Show this help\n"
4008 " --version Show package version\n"
4009 " -t --type=TYPE List only units of a particular type\n"
4010 " -p --property=NAME Show only properties by this name\n"
4011 " -a --all Show all units/properties, including dead/empty ones\n"
4012 " --failed Show only failed units\n"
4013 " --full Don't ellipsize unit names on output\n"
4014 " --fail When queueing a new job, fail if conflicting jobs are\n"
4016 " --ignore-dependencies\n"
4017 " When queueing a new job, ignore all its dependencies\n"
4018 " --kill-who=WHO Who to send signal to\n"
4019 " -s --signal=SIGNAL Which signal to send\n"
4020 " -H --host=[USER@]HOST\n"
4021 " Show information for remote host\n"
4022 " -P --privileged Acquire privileges before execution\n"
4023 " -q --quiet Suppress output\n"
4024 " --no-block Do not wait until operation finished\n"
4025 " --no-wall Don't send wall message before halt/power-off/reboot\n"
4026 " --no-reload When enabling/disabling unit files, don't reload daemon\n"
4028 " --no-pager Do not pipe output into a pager\n"
4029 " --no-ask-password\n"
4030 " Do not ask for system passwords\n"
4031 " --order When generating graph for dot, show only order\n"
4032 " --require When generating graph for dot, show only requirement\n"
4033 " --root=path Use <root> as the root file system\n"
4034 " --system Connect to system manager\n"
4035 " --user Connect to user service manager\n"
4036 " --global Enable/disable unit files globally\n"
4037 " -f --force When enabling unit files, override existing symlinks\n"
4038 " When shutting down, execute action immediately\n"
4039 " --defaults When disabling unit files, remove default symlinks only\n\n"
4041 " list-units List units\n"
4042 " start [NAME...] Start (activate) one or more units\n"
4043 " stop [NAME...] Stop (deactivate) one or more units\n"
4044 " reload [NAME...] Reload one or more units\n"
4045 " restart [NAME...] Start or restart one or more units\n"
4046 " try-restart [NAME...] Restart one or more units if active\n"
4047 " reload-or-restart [NAME...] Reload one or more units is possible,\n"
4048 " otherwise start or restart\n"
4049 " reload-or-try-restart [NAME...] Reload one or more units is possible,\n"
4050 " otherwise restart if active\n"
4051 " isolate [NAME] Start one unit and stop all others\n"
4052 " kill [NAME...] Send signal to processes of a unit\n"
4053 " is-active [NAME...] Check whether units are active\n"
4054 " status [NAME...|PID...] Show runtime status of one or more units\n"
4055 " show [NAME...|JOB...] Show properties of one or more\n"
4056 " units/jobs or the manager\n"
4057 " reset-failed [NAME...] Reset failed state for all, one, or more\n"
4059 " load [NAME...] Load one or more units\n\n"
4060 "Unit File Commands:\n"
4061 " enable [NAME...] Enable one or more unit files\n"
4062 " disable [NAME...] Disable one or more unit files\n"
4063 " is-enabled [NAME...] Check whether unit files are enabled\n\n"
4065 " list-jobs List jobs\n"
4066 " cancel [JOB...] Cancel all, one, or more jobs\n\n"
4067 "Status Commands:\n"
4068 " dump Dump server status\n"
4069 " dot Dump dependency graph for dot(1)\n\n"
4070 "Snapshot Commands:\n"
4071 " snapshot [NAME] Create a snapshot\n"
4072 " delete [NAME...] Remove one or more snapshots\n\n"
4073 "Environment Commands:\n"
4074 " show-environment Dump environment\n"
4075 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
4076 " unset-environment [NAME...] Unset one or more environment variables\n\n"
4077 "Manager Lifecycle Commands:\n"
4078 " daemon-reload Reload systemd manager configuration\n"
4079 " daemon-reexec Reexecute systemd manager\n\n"
4080 "System Commands:\n"
4081 " default Enter system default mode\n"
4082 " rescue Enter system rescue mode\n"
4083 " emergency Enter system emergency mode\n"
4084 " halt Shut down and halt the system\n"
4085 " poweroff Shut down and power-off the system\n"
4086 " reboot Shut down and reboot the system\n"
4087 " kexec Shut down and reboot the system with kexec\n"
4088 " exit Ask for user instance termination\n",
4089 program_invocation_short_name);
4094 static int halt_help(void) {
4096 printf("%s [OPTIONS...]\n\n"
4097 "%s the system.\n\n"
4098 " --help Show this help\n"
4099 " --halt Halt the machine\n"
4100 " -p --poweroff Switch off the machine\n"
4101 " --reboot Reboot the machine\n"
4102 " -f --force Force immediate halt/power-off/reboot\n"
4103 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
4104 " -d --no-wtmp Don't write wtmp record\n"
4105 " -n --no-sync Don't sync before halt/power-off/reboot\n"
4106 " --no-wall Don't send wall message before halt/power-off/reboot\n",
4107 program_invocation_short_name,
4108 arg_action == ACTION_REBOOT ? "Reboot" :
4109 arg_action == ACTION_POWEROFF ? "Power off" :
4115 static int shutdown_help(void) {
4117 printf("%s [OPTIONS...] [TIME] [WALL...]\n\n"
4118 "Shut down the system.\n\n"
4119 " --help Show this help\n"
4120 " -H --halt Halt the machine\n"
4121 " -P --poweroff Power-off the machine\n"
4122 " -r --reboot Reboot the machine\n"
4123 " -h Equivalent to --poweroff, overriden by --halt\n"
4124 " -k Don't halt/power-off/reboot, just send warnings\n"
4125 " --no-wall Don't send wall message before halt/power-off/reboot\n"
4126 " -c Cancel a pending shutdown\n",
4127 program_invocation_short_name);
4132 static int telinit_help(void) {
4134 printf("%s [OPTIONS...] {COMMAND}\n\n"
4135 "Send control commands to the init daemon.\n\n"
4136 " --help Show this help\n"
4137 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
4139 " 0 Power-off the machine\n"
4140 " 6 Reboot the machine\n"
4141 " 2, 3, 4, 5 Start runlevelX.target unit\n"
4142 " 1, s, S Enter rescue mode\n"
4143 " q, Q Reload init daemon configuration\n"
4144 " u, U Reexecute init daemon\n",
4145 program_invocation_short_name);
4150 static int runlevel_help(void) {
4152 printf("%s [OPTIONS...]\n\n"
4153 "Prints the previous and current runlevel of the init system.\n\n"
4154 " --help Show this help\n",
4155 program_invocation_short_name);
4160 static int systemctl_parse_argv(int argc, char *argv[]) {
4164 ARG_IGNORE_DEPENDENCIES,
4180 ARG_NO_ASK_PASSWORD,
4184 static const struct option options[] = {
4185 { "help", no_argument, NULL, 'h' },
4186 { "version", no_argument, NULL, ARG_VERSION },
4187 { "type", required_argument, NULL, 't' },
4188 { "property", required_argument, NULL, 'p' },
4189 { "all", no_argument, NULL, 'a' },
4190 { "failed", no_argument, NULL, ARG_FAILED },
4191 { "full", no_argument, NULL, ARG_FULL },
4192 { "fail", no_argument, NULL, ARG_FAIL },
4193 { "ignore-dependencies", no_argument, NULL, ARG_IGNORE_DEPENDENCIES },
4194 { "user", no_argument, NULL, ARG_USER },
4195 { "system", no_argument, NULL, ARG_SYSTEM },
4196 { "global", no_argument, NULL, ARG_GLOBAL },
4197 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
4198 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
4199 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4200 { "quiet", no_argument, NULL, 'q' },
4201 { "order", no_argument, NULL, ARG_ORDER },
4202 { "require", no_argument, NULL, ARG_REQUIRE },
4203 { "root", required_argument, NULL, ARG_ROOT },
4204 { "force", no_argument, NULL, 'f' },
4205 { "no-reload", no_argument, NULL, ARG_NO_RELOAD },
4206 { "defaults", no_argument, NULL, ARG_DEFAULTS },
4207 { "kill-mode", required_argument, NULL, ARG_KILL_MODE }, /* undocumented on purpose */
4208 { "kill-who", required_argument, NULL, ARG_KILL_WHO },
4209 { "signal", required_argument, NULL, 's' },
4210 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
4211 { "host", required_argument, NULL, 'H' },
4212 { "privileged",no_argument, NULL, 'P' },
4213 { NULL, 0, NULL, 0 }
4221 /* Only when running as systemctl we ask for passwords */
4222 arg_ask_password = true;
4224 while ((c = getopt_long(argc, argv, "ht:p:aqfs:H:P", options, NULL)) >= 0) {
4233 puts(PACKAGE_STRING);
4235 puts(SYSTEMD_FEATURES);
4245 if (!(l = strv_append(arg_property, optarg)))
4248 strv_free(arg_property);
4251 /* If the user asked for a particular
4252 * property, show it to him, even if it is
4263 arg_job_mode = "fail";
4266 case ARG_IGNORE_DEPENDENCIES:
4267 arg_job_mode = "ignore-dependencies";
4279 arg_no_block = true;
4283 arg_no_pager = true;
4291 arg_dot = DOT_ORDER;
4295 arg_dot = DOT_REQUIRE;
4319 arg_no_reload = true;
4328 arg_defaults = true;
4332 arg_kill_who = optarg;
4336 arg_kill_mode = optarg;
4340 if ((arg_signal = signal_from_string_try_harder(optarg)) < 0) {
4341 log_error("Failed to parse signal string %s.", optarg);
4346 case ARG_NO_ASK_PASSWORD:
4347 arg_ask_password = false;
4351 arg_transport = TRANSPORT_POLKIT;
4355 arg_transport = TRANSPORT_SSH;
4363 log_error("Unknown option code %c", c);
4368 if (arg_transport != TRANSPORT_NORMAL && arg_user) {
4369 log_error("Cannot access user instance remotely.");
4376 static int halt_parse_argv(int argc, char *argv[]) {
4385 static const struct option options[] = {
4386 { "help", no_argument, NULL, ARG_HELP },
4387 { "halt", no_argument, NULL, ARG_HALT },
4388 { "poweroff", no_argument, NULL, 'p' },
4389 { "reboot", no_argument, NULL, ARG_REBOOT },
4390 { "force", no_argument, NULL, 'f' },
4391 { "wtmp-only", no_argument, NULL, 'w' },
4392 { "no-wtmp", no_argument, NULL, 'd' },
4393 { "no-sync", no_argument, NULL, 'n' },
4394 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4395 { NULL, 0, NULL, 0 }
4403 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
4404 if (runlevel == '0' || runlevel == '6')
4405 arg_immediate = true;
4407 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
4415 arg_action = ACTION_HALT;
4419 if (arg_action != ACTION_REBOOT)
4420 arg_action = ACTION_POWEROFF;
4424 arg_action = ACTION_REBOOT;
4428 arg_immediate = true;
4449 /* Compatibility nops */
4456 log_error("Unknown option code %c", c);
4461 if (optind < argc) {
4462 log_error("Too many arguments.");
4469 static int parse_time_spec(const char *t, usec_t *_u) {
4473 if (streq(t, "now"))
4475 else if (!strchr(t, ':')) {
4478 if (safe_atou64(t, &u) < 0)
4481 *_u = now(CLOCK_REALTIME) + USEC_PER_MINUTE * u;
4490 hour = strtol(t, &e, 10);
4491 if (errno != 0 || *e != ':' || hour < 0 || hour > 23)
4494 minute = strtol(e+1, &e, 10);
4495 if (errno != 0 || *e != 0 || minute < 0 || minute > 59)
4498 n = now(CLOCK_REALTIME);
4499 s = (time_t) (n / USEC_PER_SEC);
4502 assert_se(localtime_r(&s, &tm));
4504 tm.tm_hour = (int) hour;
4505 tm.tm_min = (int) minute;
4508 assert_se(s = mktime(&tm));
4510 *_u = (usec_t) s * USEC_PER_SEC;
4513 *_u += USEC_PER_DAY;
4519 static bool kexec_loaded(void) {
4520 bool loaded = false;
4523 if (read_one_line_file("/sys/kernel/kexec_loaded", &s) >= 0) {
4531 static int shutdown_parse_argv(int argc, char *argv[]) {
4538 static const struct option options[] = {
4539 { "help", no_argument, NULL, ARG_HELP },
4540 { "halt", no_argument, NULL, 'H' },
4541 { "poweroff", no_argument, NULL, 'P' },
4542 { "reboot", no_argument, NULL, 'r' },
4543 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4544 { NULL, 0, NULL, 0 }
4552 while ((c = getopt_long(argc, argv, "HPrhkt:afFc", options, NULL)) >= 0) {
4560 arg_action = ACTION_HALT;
4564 arg_action = ACTION_POWEROFF;
4569 arg_action = ACTION_KEXEC;
4571 arg_action = ACTION_REBOOT;
4575 if (arg_action != ACTION_HALT)
4576 arg_action = ACTION_POWEROFF;
4589 /* Compatibility nops */
4593 arg_action = ACTION_CANCEL_SHUTDOWN;
4600 log_error("Unknown option code %c", c);
4605 if (argc > optind) {
4606 if ((r = parse_time_spec(argv[optind], &arg_when)) < 0) {
4607 log_error("Failed to parse time specification: %s", argv[optind]);
4611 arg_when = now(CLOCK_REALTIME) + USEC_PER_MINUTE;
4613 /* We skip the time argument */
4614 if (argc > optind + 1)
4615 arg_wall = argv + optind + 1;
4622 static int telinit_parse_argv(int argc, char *argv[]) {
4629 static const struct option options[] = {
4630 { "help", no_argument, NULL, ARG_HELP },
4631 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4632 { NULL, 0, NULL, 0 }
4635 static const struct {
4639 { '0', ACTION_POWEROFF },
4640 { '6', ACTION_REBOOT },
4641 { '1', ACTION_RESCUE },
4642 { '2', ACTION_RUNLEVEL2 },
4643 { '3', ACTION_RUNLEVEL3 },
4644 { '4', ACTION_RUNLEVEL4 },
4645 { '5', ACTION_RUNLEVEL5 },
4646 { 's', ACTION_RESCUE },
4647 { 'S', ACTION_RESCUE },
4648 { 'q', ACTION_RELOAD },
4649 { 'Q', ACTION_RELOAD },
4650 { 'u', ACTION_REEXEC },
4651 { 'U', ACTION_REEXEC }
4660 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4675 log_error("Unknown option code %c", c);
4680 if (optind >= argc) {
4685 if (optind + 1 < argc) {
4686 log_error("Too many arguments.");
4690 if (strlen(argv[optind]) != 1) {
4691 log_error("Expected single character argument.");
4695 for (i = 0; i < ELEMENTSOF(table); i++)
4696 if (table[i].from == argv[optind][0])
4699 if (i >= ELEMENTSOF(table)) {
4700 log_error("Unknown command %s.", argv[optind]);
4704 arg_action = table[i].to;
4711 static int runlevel_parse_argv(int argc, char *argv[]) {
4717 static const struct option options[] = {
4718 { "help", no_argument, NULL, ARG_HELP },
4719 { NULL, 0, NULL, 0 }
4727 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4738 log_error("Unknown option code %c", c);
4743 if (optind < argc) {
4744 log_error("Too many arguments.");
4751 static int parse_argv(int argc, char *argv[]) {
4755 if (program_invocation_short_name) {
4757 if (strstr(program_invocation_short_name, "halt")) {
4758 arg_action = ACTION_HALT;
4759 return halt_parse_argv(argc, argv);
4760 } else if (strstr(program_invocation_short_name, "poweroff")) {
4761 arg_action = ACTION_POWEROFF;
4762 return halt_parse_argv(argc, argv);
4763 } else if (strstr(program_invocation_short_name, "reboot")) {
4765 arg_action = ACTION_KEXEC;
4767 arg_action = ACTION_REBOOT;
4768 return halt_parse_argv(argc, argv);
4769 } else if (strstr(program_invocation_short_name, "shutdown")) {
4770 arg_action = ACTION_POWEROFF;
4771 return shutdown_parse_argv(argc, argv);
4772 } else if (strstr(program_invocation_short_name, "init")) {
4774 if (sd_booted() > 0) {
4775 arg_action = ACTION_INVALID;
4776 return telinit_parse_argv(argc, argv);
4778 /* Hmm, so some other init system is
4779 * running, we need to forward this
4780 * request to it. For now we simply
4781 * guess that it is Upstart. */
4783 execv("/lib/upstart/telinit", argv);
4785 log_error("Couldn't find an alternative telinit implementation to spawn.");
4789 } else if (strstr(program_invocation_short_name, "runlevel")) {
4790 arg_action = ACTION_RUNLEVEL;
4791 return runlevel_parse_argv(argc, argv);
4795 arg_action = ACTION_SYSTEMCTL;
4796 return systemctl_parse_argv(argc, argv);
4799 static int action_to_runlevel(void) {
4801 static const char table[_ACTION_MAX] = {
4802 [ACTION_HALT] = '0',
4803 [ACTION_POWEROFF] = '0',
4804 [ACTION_REBOOT] = '6',
4805 [ACTION_RUNLEVEL2] = '2',
4806 [ACTION_RUNLEVEL3] = '3',
4807 [ACTION_RUNLEVEL4] = '4',
4808 [ACTION_RUNLEVEL5] = '5',
4809 [ACTION_RESCUE] = '1'
4812 assert(arg_action < _ACTION_MAX);
4814 return table[arg_action];
4817 static int talk_upstart(void) {
4818 DBusMessage *m = NULL, *reply = NULL;
4820 int previous, rl, r;
4822 env1_buf[] = "RUNLEVEL=X",
4823 env2_buf[] = "PREVLEVEL=X";
4824 char *env1 = env1_buf, *env2 = env2_buf;
4825 const char *emit = "runlevel";
4826 dbus_bool_t b_false = FALSE;
4827 DBusMessageIter iter, sub;
4828 DBusConnection *bus;
4830 dbus_error_init(&error);
4832 if (!(rl = action_to_runlevel()))
4835 if (utmp_get_runlevel(&previous, NULL) < 0)
4838 if (!(bus = dbus_connection_open_private("unix:abstract=/com/ubuntu/upstart", &error))) {
4839 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
4844 log_error("Failed to connect to Upstart bus: %s", bus_error_message(&error));
4849 if ((r = bus_check_peercred(bus)) < 0) {
4850 log_error("Failed to verify owner of bus.");
4854 if (!(m = dbus_message_new_method_call(
4855 "com.ubuntu.Upstart",
4856 "/com/ubuntu/Upstart",
4857 "com.ubuntu.Upstart0_6",
4860 log_error("Could not allocate message.");
4865 dbus_message_iter_init_append(m, &iter);
4867 env1_buf[sizeof(env1_buf)-2] = rl;
4868 env2_buf[sizeof(env2_buf)-2] = previous;
4870 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
4871 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
4872 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
4873 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
4874 !dbus_message_iter_close_container(&iter, &sub) ||
4875 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
4876 log_error("Could not append arguments to message.");
4881 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
4883 if (error_is_no_service(&error)) {
4888 log_error("Failed to issue method call: %s", bus_error_message(&error));
4897 dbus_message_unref(m);
4900 dbus_message_unref(reply);
4903 dbus_connection_flush(bus);
4904 dbus_connection_close(bus);
4905 dbus_connection_unref(bus);
4908 dbus_error_free(&error);
4913 static int talk_initctl(void) {
4914 struct init_request request;
4918 if (!(rl = action_to_runlevel()))
4922 request.magic = INIT_MAGIC;
4923 request.sleeptime = 0;
4924 request.cmd = INIT_CMD_RUNLVL;
4925 request.runlevel = rl;
4927 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
4929 if (errno == ENOENT)
4932 log_error("Failed to open "INIT_FIFO": %m");
4937 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
4938 close_nointr_nofail(fd);
4941 log_error("Failed to write to "INIT_FIFO": %m");
4942 return errno ? -errno : -EIO;
4948 static int systemctl_main(DBusConnection *bus, int argc, char *argv[], DBusError *error) {
4950 static const struct {
4958 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
4960 { "list-units", LESS, 1, list_units },
4961 { "list-jobs", EQUAL, 1, list_jobs },
4962 { "clear-jobs", EQUAL, 1, daemon_reload },
4963 { "load", MORE, 2, load_unit },
4964 { "cancel", MORE, 2, cancel_job },
4965 { "start", MORE, 2, start_unit },
4966 { "stop", MORE, 2, start_unit },
4967 { "condstop", MORE, 2, start_unit }, /* For compatibility with ALTLinux */
4968 { "reload", MORE, 2, start_unit },
4969 { "restart", MORE, 2, start_unit },
4970 { "try-restart", MORE, 2, start_unit },
4971 { "reload-or-restart", MORE, 2, start_unit },
4972 { "reload-or-try-restart", MORE, 2, start_unit },
4973 { "force-reload", MORE, 2, start_unit }, /* For compatibility with SysV */
4974 { "condreload", MORE, 2, start_unit }, /* For compatibility with ALTLinux */
4975 { "condrestart", MORE, 2, start_unit }, /* For compatibility with RH */
4976 { "isolate", EQUAL, 2, start_unit },
4977 { "kill", MORE, 2, kill_unit },
4978 { "is-active", MORE, 2, check_unit },
4979 { "check", MORE, 2, check_unit },
4980 { "show", MORE, 1, show },
4981 { "status", MORE, 2, show },
4982 { "dump", EQUAL, 1, dump },
4983 { "dot", EQUAL, 1, dot },
4984 { "snapshot", LESS, 2, snapshot },
4985 { "delete", MORE, 2, delete_snapshot },
4986 { "daemon-reload", EQUAL, 1, daemon_reload },
4987 { "daemon-reexec", EQUAL, 1, daemon_reload },
4988 { "show-environment", EQUAL, 1, show_enviroment },
4989 { "set-environment", MORE, 2, set_environment },
4990 { "unset-environment", MORE, 2, set_environment },
4991 { "halt", EQUAL, 1, start_special },
4992 { "poweroff", EQUAL, 1, start_special },
4993 { "reboot", EQUAL, 1, start_special },
4994 { "kexec", EQUAL, 1, start_special },
4995 { "default", EQUAL, 1, start_special },
4996 { "rescue", EQUAL, 1, start_special },
4997 { "emergency", EQUAL, 1, start_special },
4998 { "exit", EQUAL, 1, start_special },
4999 { "reset-failed", MORE, 1, reset_failed },
5000 { "enable", MORE, 2, enable_unit },
5001 { "disable", MORE, 2, enable_unit },
5002 { "is-enabled", MORE, 2, enable_unit }
5012 left = argc - optind;
5015 /* Special rule: no arguments means "list-units" */
5018 if (streq(argv[optind], "help")) {
5023 for (i = 0; i < ELEMENTSOF(verbs); i++)
5024 if (streq(argv[optind], verbs[i].verb))
5027 if (i >= ELEMENTSOF(verbs)) {
5028 log_error("Unknown operation %s", argv[optind]);
5033 switch (verbs[i].argc_cmp) {
5036 if (left != verbs[i].argc) {
5037 log_error("Invalid number of arguments.");
5044 if (left < verbs[i].argc) {
5045 log_error("Too few arguments.");
5052 if (left > verbs[i].argc) {
5053 log_error("Too many arguments.");
5060 assert_not_reached("Unknown comparison operator.");
5063 /* Require a bus connection for all operations but
5065 if (!streq(verbs[i].verb, "enable") && !streq(verbs[i].verb, "disable")) {
5067 if (running_in_chroot() > 0) {
5068 log_info("Running in chroot, ignoring request.");
5073 log_error("Failed to get D-Bus connection: %s", error->message);
5078 return verbs[i].dispatch(bus, argv + optind, left);
5081 static int send_shutdownd(usec_t t, char mode, bool dry_run, bool warn, const char *message) {
5083 struct msghdr msghdr;
5085 union sockaddr_union sockaddr;
5086 struct shutdownd_command c;
5091 c.dry_run = dry_run;
5095 strncpy(c.wall_message, message, sizeof(c.wall_message));
5097 if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0)
5101 sockaddr.sa.sa_family = AF_UNIX;
5102 sockaddr.un.sun_path[0] = 0;
5103 strncpy(sockaddr.un.sun_path, "/run/systemd/shutdownd", sizeof(sockaddr.un.sun_path));
5106 iovec.iov_base = (char*) &c;
5107 iovec.iov_len = sizeof(c);
5110 msghdr.msg_name = &sockaddr;
5111 msghdr.msg_namelen = offsetof(struct sockaddr_un, sun_path) + sizeof("/run/systemd/shutdownd") - 1;
5113 msghdr.msg_iov = &iovec;
5114 msghdr.msg_iovlen = 1;
5116 if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) < 0) {
5117 close_nointr_nofail(fd);
5121 close_nointr_nofail(fd);
5125 static int reload_with_fallback(DBusConnection *bus) {
5128 /* First, try systemd via D-Bus. */
5129 if (daemon_reload(bus, NULL, 0) > 0)
5133 /* Nothing else worked, so let's try signals */
5134 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
5136 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
5137 log_error("kill() failed: %m");
5144 static int start_with_fallback(DBusConnection *bus) {
5147 /* First, try systemd via D-Bus. */
5148 if (start_unit(bus, NULL, 0) >= 0)
5152 /* Hmm, talking to systemd via D-Bus didn't work. Then
5153 * let's try to talk to Upstart via D-Bus. */
5154 if (talk_upstart() > 0)
5157 /* Nothing else worked, so let's try
5159 if (talk_initctl() > 0)
5162 log_error("Failed to talk to init daemon.");
5166 warn_wall(arg_action);
5170 static int halt_main(DBusConnection *bus) {
5173 if (geteuid() != 0) {
5174 log_error("Must be root.");
5180 char date[FORMAT_TIMESTAMP_MAX];
5182 m = strv_join(arg_wall, " ");
5183 r = send_shutdownd(arg_when,
5184 arg_action == ACTION_HALT ? 'H' :
5185 arg_action == ACTION_POWEROFF ? 'P' :
5193 log_warning("Failed to talk to shutdownd, proceeding with immediate shutdown: %s", strerror(-r));
5195 log_info("Shutdown scheduled for %s, use 'shutdown -c' to cancel.",
5196 format_timestamp(date, sizeof(date), arg_when));
5201 if (!arg_dry && !arg_immediate)
5202 return start_with_fallback(bus);
5205 if (sd_booted() > 0)
5206 log_debug("Not writing utmp record, assuming that systemd-update-utmp is used.");
5207 else if ((r = utmp_put_shutdown(0)) < 0)
5208 log_warning("Failed to write utmp record: %s", strerror(-r));
5217 /* Make sure C-A-D is handled by the kernel from this
5219 reboot(RB_ENABLE_CAD);
5221 switch (arg_action) {
5224 log_info("Halting.");
5225 reboot(RB_HALT_SYSTEM);
5228 case ACTION_POWEROFF:
5229 log_info("Powering off.");
5230 reboot(RB_POWER_OFF);
5234 log_info("Rebooting.");
5235 reboot(RB_AUTOBOOT);
5239 assert_not_reached("Unknown halt action.");
5242 /* We should never reach this. */
5246 static int runlevel_main(void) {
5247 int r, runlevel, previous;
5249 if ((r = utmp_get_runlevel(&runlevel, &previous)) < 0) {
5250 printf("unknown\n");
5255 previous <= 0 ? 'N' : previous,
5256 runlevel <= 0 ? 'N' : runlevel);
5261 static void agent_close(void) {
5267 /* Inform agent that we are done */
5268 kill(agent_pid, SIGTERM);
5269 kill(agent_pid, SIGCONT);
5270 wait_for_terminate(agent_pid, &dummy);
5274 int main(int argc, char*argv[]) {
5275 int r, retval = EXIT_FAILURE;
5276 DBusConnection *bus = NULL;
5279 dbus_error_init(&error);
5281 log_parse_environment();
5284 if ((r = parse_argv(argc, argv)) < 0)
5287 retval = EXIT_SUCCESS;
5291 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
5292 * let's shortcut this */
5293 if (arg_action == ACTION_RUNLEVEL) {
5294 r = runlevel_main();
5295 retval = r < 0 ? EXIT_FAILURE : r;
5299 if (running_in_chroot() > 0 && arg_action != ACTION_SYSTEMCTL) {
5300 log_info("Running in chroot, ignoring request.");
5305 if (arg_transport == TRANSPORT_NORMAL)
5306 bus_connect(arg_user ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &bus, &private_bus, &error);
5307 else if (arg_transport == TRANSPORT_POLKIT) {
5308 bus_connect_system_polkit(&bus, &error);
5309 private_bus = false;
5310 } else if (arg_transport == TRANSPORT_SSH) {
5311 bus_connect_system_ssh(NULL, arg_host, &bus, &error);
5312 private_bus = false;
5314 assert_not_reached("Uh, invalid transport...");
5316 switch (arg_action) {
5318 case ACTION_SYSTEMCTL:
5319 r = systemctl_main(bus, argc, argv, &error);
5323 case ACTION_POWEROFF:
5329 case ACTION_RUNLEVEL2:
5330 case ACTION_RUNLEVEL3:
5331 case ACTION_RUNLEVEL4:
5332 case ACTION_RUNLEVEL5:
5334 case ACTION_EMERGENCY:
5335 case ACTION_DEFAULT:
5336 r = start_with_fallback(bus);
5341 r = reload_with_fallback(bus);
5344 case ACTION_CANCEL_SHUTDOWN:
5345 r = send_shutdownd(0, 0, false, false, NULL);
5348 case ACTION_INVALID:
5349 case ACTION_RUNLEVEL:
5351 assert_not_reached("Unknown action");
5354 retval = r < 0 ? EXIT_FAILURE : r;
5359 dbus_connection_flush(bus);
5360 dbus_connection_close(bus);
5361 dbus_connection_unref(bus);
5364 dbus_error_free(&error);
5368 strv_free(arg_property);