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"
59 #include "spawn-agent.h"
62 static const char *arg_type = NULL;
63 static char **arg_property = NULL;
64 static bool arg_all = false;
65 static const char *arg_job_mode = "replace";
66 static UnitFileScope arg_scope = UNIT_FILE_SYSTEM;
67 static bool arg_immediate = false;
68 static bool arg_no_block = false;
69 static bool arg_no_pager = false;
70 static bool arg_no_wtmp = false;
71 static bool arg_no_sync = false;
72 static bool arg_no_wall = false;
73 static bool arg_no_reload = false;
74 static bool arg_dry = false;
75 static bool arg_quiet = false;
76 static bool arg_full = false;
77 static bool arg_force = false;
78 static bool arg_ask_password = false;
79 static bool arg_failed = false;
80 static bool arg_runtime = false;
81 static char **arg_wall = NULL;
82 static const char *arg_kill_who = NULL;
83 static const char *arg_kill_mode = NULL;
84 static int arg_signal = SIGTERM;
85 static const char *arg_root = NULL;
86 static usec_t arg_when = 0;
105 ACTION_CANCEL_SHUTDOWN,
107 } arg_action = ACTION_SYSTEMCTL;
113 static enum transport {
117 } arg_transport = TRANSPORT_NORMAL;
118 static const char *arg_host = NULL;
120 static bool private_bus = false;
122 static int daemon_reload(DBusConnection *bus, char **args);
124 static bool on_tty(void) {
127 /* Note that this is invoked relatively early, before we start
128 * the pager. That means the value we return reflects whether
129 * we originally were started on a tty, not if we currently
130 * are. But this is intended, since we want colour and so on
131 * when run in our own pager. */
133 if (_unlikely_(t < 0))
134 t = isatty(STDOUT_FILENO) > 0;
139 static void pager_open_if_enabled(void) {
148 static void agent_open_if_enabled(void) {
150 /* Open the password agent as a child process if necessary */
152 if (!arg_ask_password)
155 if (arg_scope != UNIT_FILE_SYSTEM)
161 static const char *ansi_highlight(bool b) {
166 return b ? ANSI_HIGHLIGHT_ON : ANSI_HIGHLIGHT_OFF;
169 static const char *ansi_highlight_green(bool b) {
174 return b ? ANSI_HIGHLIGHT_GREEN_ON : ANSI_HIGHLIGHT_OFF;
177 static bool error_is_no_service(const DBusError *error) {
180 if (!dbus_error_is_set(error))
183 if (dbus_error_has_name(error, DBUS_ERROR_NAME_HAS_NO_OWNER))
186 if (dbus_error_has_name(error, DBUS_ERROR_SERVICE_UNKNOWN))
189 return startswith(error->name, "org.freedesktop.DBus.Error.Spawn.");
192 static int translate_bus_error_to_exit_status(int r, const DBusError *error) {
195 if (!dbus_error_is_set(error))
198 if (dbus_error_has_name(error, DBUS_ERROR_ACCESS_DENIED) ||
199 dbus_error_has_name(error, BUS_ERROR_ONLY_BY_DEPENDENCY) ||
200 dbus_error_has_name(error, BUS_ERROR_NO_ISOLATION) ||
201 dbus_error_has_name(error, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE))
202 return EXIT_NOPERMISSION;
204 if (dbus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT))
205 return EXIT_NOTINSTALLED;
207 if (dbus_error_has_name(error, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE) ||
208 dbus_error_has_name(error, BUS_ERROR_NOT_SUPPORTED))
209 return EXIT_NOTIMPLEMENTED;
211 if (dbus_error_has_name(error, BUS_ERROR_LOAD_FAILED))
212 return EXIT_NOTCONFIGURED;
220 static void warn_wall(enum action action) {
221 static const char *table[_ACTION_MAX] = {
222 [ACTION_HALT] = "The system is going down for system halt NOW!",
223 [ACTION_REBOOT] = "The system is going down for reboot NOW!",
224 [ACTION_POWEROFF] = "The system is going down for power-off NOW!",
225 [ACTION_KEXEC] = "The system is going down for kexec reboot NOW!",
226 [ACTION_RESCUE] = "The system is going down to rescue mode NOW!",
227 [ACTION_EMERGENCY] = "The system is going down to emergency mode NOW!"
236 if (!(p = strv_join(arg_wall, " "))) {
237 log_error("Failed to join strings.");
253 utmp_wall(table[action], NULL);
256 static bool avoid_bus(void) {
258 if (running_in_chroot() > 0)
261 if (sd_booted() <= 0)
264 if (!isempty(arg_root))
267 if (arg_scope == UNIT_FILE_GLOBAL)
275 const char *description;
276 const char *load_state;
277 const char *active_state;
278 const char *sub_state;
279 const char *following;
280 const char *unit_path;
282 const char *job_type;
283 const char *job_path;
286 static int compare_unit_info(const void *a, const void *b) {
288 const struct unit_info *u = a, *v = b;
290 d1 = strrchr(u->id, '.');
291 d2 = strrchr(v->id, '.');
296 if ((r = strcasecmp(d1, d2)) != 0)
300 return strcasecmp(u->id, v->id);
303 static bool output_show_unit(const struct unit_info *u) {
307 return streq(u->active_state, "failed");
309 return (!arg_type || ((dot = strrchr(u->id, '.')) &&
310 streq(dot+1, arg_type))) &&
311 (arg_all || !(streq(u->active_state, "inactive") || u->following[0]) || u->job_id > 0);
314 static void output_units_list(const struct unit_info *unit_infos, unsigned c) {
315 unsigned active_len, sub_len, job_len, n_shown = 0;
316 const struct unit_info *u;
318 active_len = sizeof("ACTIVE")-1;
319 sub_len = sizeof("SUB")-1;
320 job_len = sizeof("JOB")-1;
322 for (u = unit_infos; u < unit_infos + c; u++) {
323 if (!output_show_unit(u))
326 active_len = MAX(active_len, strlen(u->active_state));
327 sub_len = MAX(sub_len, strlen(u->sub_state));
329 job_len = MAX(job_len, strlen(u->job_type));
333 printf("%-25s %-6s %-*s %-*s %-*s", "UNIT", "LOAD",
334 active_len, "ACTIVE", sub_len, "SUB", job_len, "JOB");
335 if (columns() >= 80+12 || arg_full || !arg_no_pager)
336 printf(" %s\n", "DESCRIPTION");
341 for (u = unit_infos; u < unit_infos + c; u++) {
344 const char *on_loaded, *off_loaded;
345 const char *on_active, *off_active;
347 if (!output_show_unit(u))
352 if (!streq(u->load_state, "loaded") &&
353 !streq(u->load_state, "banned")) {
354 on_loaded = ansi_highlight(true);
355 off_loaded = ansi_highlight(false);
357 on_loaded = off_loaded = "";
359 if (streq(u->active_state, "failed")) {
360 on_active = ansi_highlight(true);
361 off_active = ansi_highlight(false);
363 on_active = off_active = "";
365 e = arg_full ? NULL : ellipsize(u->id, 25, 33);
367 printf("%-25s %s%-6s%s %s%-*s %-*s%s%n",
369 on_loaded, u->load_state, off_loaded,
370 on_active, active_len, u->active_state,
371 sub_len, u->sub_state, off_active,
376 a -= strlen(on_loaded) + strlen(off_loaded);
377 a -= strlen(on_active) + strlen(off_active);
380 printf(" %-*s", job_len, u->job_type);
384 if (a + b + 1 < columns()) {
386 printf(" %-*s", job_len, "");
388 if (arg_full || !arg_no_pager)
389 printf(" %s", u->description);
391 printf(" %.*s", columns() - a - b - 1, u->description);
398 printf("\nLOAD = Reflects whether the unit definition was properly loaded.\n"
399 "ACTIVE = The high-level unit activation state, i.e. generalization of SUB.\n"
400 "SUB = The low-level unit activation state, values depend on unit type.\n"
401 "JOB = Pending job for the unit.\n");
404 printf("\n%u units listed.\n", n_shown);
406 printf("\n%u units listed. Pass --all to see inactive units, too.\n", n_shown);
410 static int list_units(DBusConnection *bus, char **args) {
411 DBusMessage *m = NULL, *reply = NULL;
414 DBusMessageIter iter, sub, sub2;
415 unsigned c = 0, n_units = 0;
416 struct unit_info *unit_infos = NULL;
418 dbus_error_init(&error);
422 pager_open_if_enabled();
424 if (!(m = dbus_message_new_method_call(
425 "org.freedesktop.systemd1",
426 "/org/freedesktop/systemd1",
427 "org.freedesktop.systemd1.Manager",
429 log_error("Could not allocate message.");
433 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
434 log_error("Failed to issue method call: %s", bus_error_message(&error));
439 if (!dbus_message_iter_init(reply, &iter) ||
440 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
441 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
442 log_error("Failed to parse reply.");
447 dbus_message_iter_recurse(&iter, &sub);
449 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
452 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
453 log_error("Failed to parse reply.");
461 n_units = MAX(2*c, 16);
462 w = realloc(unit_infos, sizeof(struct unit_info) * n_units);
465 log_error("Failed to allocate unit array.");
475 dbus_message_iter_recurse(&sub, &sub2);
477 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->id, true) < 0 ||
478 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->description, true) < 0 ||
479 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->load_state, true) < 0 ||
480 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->active_state, true) < 0 ||
481 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->sub_state, true) < 0 ||
482 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->following, true) < 0 ||
483 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &u->unit_path, true) < 0 ||
484 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &u->job_id, true) < 0 ||
485 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->job_type, true) < 0 ||
486 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &u->job_path, false) < 0) {
487 log_error("Failed to parse reply.");
492 dbus_message_iter_next(&sub);
497 qsort(unit_infos, c, sizeof(struct unit_info), compare_unit_info);
498 output_units_list(unit_infos, c);
505 dbus_message_unref(m);
508 dbus_message_unref(reply);
512 dbus_error_free(&error);
517 static int compare_unit_file_list(const void *a, const void *b) {
519 const UnitFileList *u = a, *v = b;
521 d1 = strrchr(u->path, '.');
522 d2 = strrchr(v->path, '.');
527 r = strcasecmp(d1, d2);
532 return strcasecmp(file_name_from_path(u->path), file_name_from_path(v->path));
535 static bool output_show_unit_file(const UnitFileList *u) {
538 return !arg_type || ((dot = strrchr(u->path, '.')) && streq(dot+1, arg_type));
541 static void output_unit_file_list(const UnitFileList *units, unsigned c) {
542 unsigned n_shown = 0;
543 const UnitFileList *u;
546 printf("%-25s %-6s\n", "UNIT FILE", "STATE");
548 for (u = units; u < units + c; u++) {
550 const char *on, *off;
553 if (!output_show_unit_file(u))
558 if (u->state == UNIT_FILE_MASKED ||
559 u->state == UNIT_FILE_MASKED_RUNTIME ||
560 u->state == UNIT_FILE_DISABLED) {
561 on = ansi_highlight(true);
562 off = ansi_highlight(false);
563 } else if (u->state == UNIT_FILE_ENABLED) {
564 on = ansi_highlight_green(true);
565 off = ansi_highlight_green(false);
569 id = file_name_from_path(u->path);
571 e = arg_full ? NULL : ellipsize(id, 25, 33);
573 printf("%-25s %s%-6s%s\n",
575 on, unit_file_state_to_string(u->state), off);
581 printf("\n%u unit files listed.\n", n_shown);
584 static int list_unit_files(DBusConnection *bus, char **args) {
585 DBusMessage *m = NULL, *reply = NULL;
588 DBusMessageIter iter, sub, sub2;
589 unsigned c = 0, n_units = 0;
590 UnitFileList *units = NULL;
592 dbus_error_init(&error);
596 pager_open_if_enabled();
603 h = hashmap_new(string_hash_func, string_compare_func);
605 log_error("Out of memory");
609 r = unit_file_get_list(arg_scope, arg_root, h);
611 log_error("Failed to get unit file list: %s", strerror(-r));
615 n_units = hashmap_size(h);
616 units = new(UnitFileList, n_units);
618 unit_file_list_free(h);
619 log_error("Out of memory");
623 HASHMAP_FOREACH(u, h, i) {
624 memcpy(units + c++, u, sizeof(UnitFileList));
630 m = dbus_message_new_method_call(
631 "org.freedesktop.systemd1",
632 "/org/freedesktop/systemd1",
633 "org.freedesktop.systemd1.Manager",
636 log_error("Could not allocate message.");
640 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
642 log_error("Failed to issue method call: %s", bus_error_message(&error));
647 if (!dbus_message_iter_init(reply, &iter) ||
648 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
649 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
650 log_error("Failed to parse reply.");
655 dbus_message_iter_recurse(&iter, &sub);
657 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
661 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
662 log_error("Failed to parse reply.");
670 n_units = MAX(2*c, 16);
671 w = realloc(units, sizeof(struct UnitFileList) * n_units);
674 log_error("Failed to allocate unit array.");
684 dbus_message_iter_recurse(&sub, &sub2);
686 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->path, true) < 0 ||
687 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, false) < 0) {
688 log_error("Failed to parse reply.");
693 u->state = unit_file_state_from_string(state);
695 dbus_message_iter_next(&sub);
701 qsort(units, c, sizeof(UnitFileList), compare_unit_file_list);
702 output_unit_file_list(units, c);
709 dbus_message_unref(m);
712 dbus_message_unref(reply);
716 dbus_error_free(&error);
721 static int dot_one_property(const char *name, const char *prop, DBusMessageIter *iter) {
722 static const char * const colors[] = {
723 "Requires", "[color=\"black\"]",
724 "RequiresOverridable", "[color=\"black\"]",
725 "Requisite", "[color=\"darkblue\"]",
726 "RequisiteOverridable", "[color=\"darkblue\"]",
727 "Wants", "[color=\"darkgrey\"]",
728 "Conflicts", "[color=\"red\"]",
729 "ConflictedBy", "[color=\"red\"]",
730 "After", "[color=\"green\"]"
733 const char *c = NULL;
740 for (i = 0; i < ELEMENTSOF(colors); i += 2)
741 if (streq(colors[i], prop)) {
749 if (arg_dot != DOT_ALL)
750 if ((arg_dot == DOT_ORDER) != streq(prop, "After"))
753 switch (dbus_message_iter_get_arg_type(iter)) {
755 case DBUS_TYPE_ARRAY:
757 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
760 dbus_message_iter_recurse(iter, &sub);
762 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
765 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
766 dbus_message_iter_get_basic(&sub, &s);
767 printf("\t\"%s\"->\"%s\" %s;\n", name, s, c);
769 dbus_message_iter_next(&sub);
779 static int dot_one(DBusConnection *bus, const char *name, const char *path) {
780 DBusMessage *m = NULL, *reply = NULL;
781 const char *interface = "org.freedesktop.systemd1.Unit";
784 DBusMessageIter iter, sub, sub2, sub3;
789 dbus_error_init(&error);
791 if (!(m = dbus_message_new_method_call(
792 "org.freedesktop.systemd1",
794 "org.freedesktop.DBus.Properties",
796 log_error("Could not allocate message.");
801 if (!dbus_message_append_args(m,
802 DBUS_TYPE_STRING, &interface,
803 DBUS_TYPE_INVALID)) {
804 log_error("Could not append arguments to message.");
809 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
810 log_error("Failed to issue method call: %s", bus_error_message(&error));
815 if (!dbus_message_iter_init(reply, &iter) ||
816 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
817 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
818 log_error("Failed to parse reply.");
823 dbus_message_iter_recurse(&iter, &sub);
825 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
828 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
829 log_error("Failed to parse reply.");
834 dbus_message_iter_recurse(&sub, &sub2);
836 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &prop, true) < 0) {
837 log_error("Failed to parse reply.");
842 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
843 log_error("Failed to parse reply.");
848 dbus_message_iter_recurse(&sub2, &sub3);
850 if (dot_one_property(name, prop, &sub3)) {
851 log_error("Failed to parse reply.");
856 dbus_message_iter_next(&sub);
863 dbus_message_unref(m);
866 dbus_message_unref(reply);
868 dbus_error_free(&error);
873 static int dot(DBusConnection *bus, char **args) {
874 DBusMessage *m = NULL, *reply = NULL;
877 DBusMessageIter iter, sub, sub2;
879 dbus_error_init(&error);
883 if (!(m = dbus_message_new_method_call(
884 "org.freedesktop.systemd1",
885 "/org/freedesktop/systemd1",
886 "org.freedesktop.systemd1.Manager",
888 log_error("Could not allocate message.");
892 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
893 log_error("Failed to issue method call: %s", bus_error_message(&error));
898 if (!dbus_message_iter_init(reply, &iter) ||
899 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
900 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
901 log_error("Failed to parse reply.");
906 printf("digraph systemd {\n");
908 dbus_message_iter_recurse(&iter, &sub);
909 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
910 const char *id, *description, *load_state, *active_state, *sub_state, *following, *unit_path;
912 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
913 log_error("Failed to parse reply.");
918 dbus_message_iter_recurse(&sub, &sub2);
920 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
921 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
922 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
923 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
924 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
925 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &following, true) < 0 ||
926 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, true) < 0) {
927 log_error("Failed to parse reply.");
932 if ((r = dot_one(bus, id, unit_path)) < 0)
935 /* printf("\t\"%s\";\n", id); */
936 dbus_message_iter_next(&sub);
941 log_info(" Color legend: black = Requires\n"
942 " dark blue = Requisite\n"
943 " dark grey = Wants\n"
948 log_notice("-- You probably want to process this output with graphviz' dot tool.\n"
949 "-- Try a shell pipeline like 'systemctl dot | dot -Tsvg > systemd.svg'!\n");
955 dbus_message_unref(m);
958 dbus_message_unref(reply);
960 dbus_error_free(&error);
965 static int list_jobs(DBusConnection *bus, char **args) {
966 DBusMessage *m = NULL, *reply = NULL;
969 DBusMessageIter iter, sub, sub2;
972 dbus_error_init(&error);
976 pager_open_if_enabled();
978 if (!(m = dbus_message_new_method_call(
979 "org.freedesktop.systemd1",
980 "/org/freedesktop/systemd1",
981 "org.freedesktop.systemd1.Manager",
983 log_error("Could not allocate message.");
987 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
988 log_error("Failed to issue method call: %s", bus_error_message(&error));
993 if (!dbus_message_iter_init(reply, &iter) ||
994 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
995 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
996 log_error("Failed to parse reply.");
1001 dbus_message_iter_recurse(&iter, &sub);
1004 printf("%4s %-25s %-15s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
1006 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1007 const char *name, *type, *state, *job_path, *unit_path;
1011 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
1012 log_error("Failed to parse reply.");
1017 dbus_message_iter_recurse(&sub, &sub2);
1019 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
1020 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
1021 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
1022 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
1023 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
1024 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
1025 log_error("Failed to parse reply.");
1030 e = arg_full ? NULL : ellipsize(name, 25, 33);
1031 printf("%4u %-25s %-15s %-7s\n", id, e ? e : name, type, state);
1036 dbus_message_iter_next(&sub);
1040 printf("\n%u jobs listed.\n", k);
1046 dbus_message_unref(m);
1049 dbus_message_unref(reply);
1051 dbus_error_free(&error);
1056 static int load_unit(DBusConnection *bus, char **args) {
1057 DBusMessage *m = NULL, *reply = NULL;
1062 dbus_error_init(&error);
1067 STRV_FOREACH(name, args+1) {
1069 if (!(m = dbus_message_new_method_call(
1070 "org.freedesktop.systemd1",
1071 "/org/freedesktop/systemd1",
1072 "org.freedesktop.systemd1.Manager",
1074 log_error("Could not allocate message.");
1079 if (!dbus_message_append_args(m,
1080 DBUS_TYPE_STRING, name,
1081 DBUS_TYPE_INVALID)) {
1082 log_error("Could not append arguments to message.");
1087 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1088 log_error("Failed to issue method call: %s", bus_error_message(&error));
1093 dbus_message_unref(m);
1094 dbus_message_unref(reply);
1103 dbus_message_unref(m);
1106 dbus_message_unref(reply);
1108 dbus_error_free(&error);
1113 static int cancel_job(DBusConnection *bus, char **args) {
1114 DBusMessage *m = NULL, *reply = NULL;
1119 dbus_error_init(&error);
1124 if (strv_length(args) <= 1)
1125 return daemon_reload(bus, args);
1127 STRV_FOREACH(name, args+1) {
1131 if (!(m = dbus_message_new_method_call(
1132 "org.freedesktop.systemd1",
1133 "/org/freedesktop/systemd1",
1134 "org.freedesktop.systemd1.Manager",
1136 log_error("Could not allocate message.");
1141 if ((r = safe_atou(*name, &id)) < 0) {
1142 log_error("Failed to parse job id: %s", strerror(-r));
1146 assert_cc(sizeof(uint32_t) == sizeof(id));
1147 if (!dbus_message_append_args(m,
1148 DBUS_TYPE_UINT32, &id,
1149 DBUS_TYPE_INVALID)) {
1150 log_error("Could not append arguments to message.");
1155 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1156 log_error("Failed to issue method call: %s", bus_error_message(&error));
1161 if (!dbus_message_get_args(reply, &error,
1162 DBUS_TYPE_OBJECT_PATH, &path,
1163 DBUS_TYPE_INVALID)) {
1164 log_error("Failed to parse reply: %s", bus_error_message(&error));
1169 dbus_message_unref(m);
1170 if (!(m = dbus_message_new_method_call(
1171 "org.freedesktop.systemd1",
1173 "org.freedesktop.systemd1.Job",
1175 log_error("Could not allocate message.");
1180 dbus_message_unref(reply);
1181 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1182 log_error("Failed to issue method call: %s", bus_error_message(&error));
1187 dbus_message_unref(m);
1188 dbus_message_unref(reply);
1196 dbus_message_unref(m);
1199 dbus_message_unref(reply);
1201 dbus_error_free(&error);
1206 static bool need_daemon_reload(DBusConnection *bus, const char *unit) {
1207 DBusMessage *m = NULL, *reply = NULL;
1208 dbus_bool_t b = FALSE;
1209 DBusMessageIter iter, sub;
1211 *interface = "org.freedesktop.systemd1.Unit",
1212 *property = "NeedDaemonReload",
1215 /* We ignore all errors here, since this is used to show a warning only */
1217 if (!(m = dbus_message_new_method_call(
1218 "org.freedesktop.systemd1",
1219 "/org/freedesktop/systemd1",
1220 "org.freedesktop.systemd1.Manager",
1224 if (!dbus_message_append_args(m,
1225 DBUS_TYPE_STRING, &unit,
1229 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
1232 if (!dbus_message_get_args(reply, NULL,
1233 DBUS_TYPE_OBJECT_PATH, &path,
1237 dbus_message_unref(m);
1238 if (!(m = dbus_message_new_method_call(
1239 "org.freedesktop.systemd1",
1241 "org.freedesktop.DBus.Properties",
1245 if (!dbus_message_append_args(m,
1246 DBUS_TYPE_STRING, &interface,
1247 DBUS_TYPE_STRING, &property,
1248 DBUS_TYPE_INVALID)) {
1252 dbus_message_unref(reply);
1253 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
1256 if (!dbus_message_iter_init(reply, &iter) ||
1257 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
1260 dbus_message_iter_recurse(&iter, &sub);
1262 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_BOOLEAN)
1265 dbus_message_iter_get_basic(&sub, &b);
1269 dbus_message_unref(m);
1272 dbus_message_unref(reply);
1277 typedef struct WaitData {
1282 static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
1290 dbus_error_init(&error);
1292 log_debug("Got D-Bus request: %s.%s() on %s",
1293 dbus_message_get_interface(message),
1294 dbus_message_get_member(message),
1295 dbus_message_get_path(message));
1297 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
1298 log_error("Warning! D-Bus connection terminated.");
1299 dbus_connection_close(connection);
1301 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
1303 const char *path, *result;
1304 dbus_bool_t success = true;
1306 if (dbus_message_get_args(message, &error,
1307 DBUS_TYPE_UINT32, &id,
1308 DBUS_TYPE_OBJECT_PATH, &path,
1309 DBUS_TYPE_STRING, &result,
1310 DBUS_TYPE_INVALID)) {
1313 if ((p = set_remove(d->set, (char*) path)))
1317 d->result = strdup(result);
1322 dbus_error_free(&error);
1324 if (dbus_message_get_args(message, &error,
1325 DBUS_TYPE_UINT32, &id,
1326 DBUS_TYPE_OBJECT_PATH, &path,
1327 DBUS_TYPE_BOOLEAN, &success,
1328 DBUS_TYPE_INVALID)) {
1331 /* Compatibility with older systemd versions <
1332 * 19 during upgrades. This should be dropped
1335 if ((p = set_remove(d->set, (char*) path)))
1339 d->result = strdup("failed");
1345 log_error("Failed to parse message: %s", bus_error_message(&error));
1349 dbus_error_free(&error);
1350 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1353 static int enable_wait_for_jobs(DBusConnection *bus) {
1361 dbus_error_init(&error);
1362 dbus_bus_add_match(bus,
1364 "sender='org.freedesktop.systemd1',"
1365 "interface='org.freedesktop.systemd1.Manager',"
1366 "member='JobRemoved',"
1367 "path='/org/freedesktop/systemd1'",
1370 if (dbus_error_is_set(&error)) {
1371 log_error("Failed to add match: %s", bus_error_message(&error));
1372 dbus_error_free(&error);
1376 /* This is slightly dirty, since we don't undo the match registrations. */
1380 static int wait_for_jobs(DBusConnection *bus, Set *s) {
1390 if (!dbus_connection_add_filter(bus, wait_filter, &d, NULL)) {
1391 log_error("Failed to add filter.");
1396 while (!set_isempty(s) &&
1397 dbus_connection_read_write_dispatch(bus, -1))
1400 if (!arg_quiet && d.result) {
1401 if (streq(d.result, "timeout"))
1402 log_error("Job timed out.");
1403 else if (streq(d.result, "canceled"))
1404 log_error("Job canceled.");
1405 else if (streq(d.result, "dependency"))
1406 log_error("A dependency job failed. See system logs for details.");
1407 else if (!streq(d.result, "done") && !streq(d.result, "skipped"))
1408 log_error("Job failed. See system logs and 'systemctl status' for details.");
1411 if (streq_ptr(d.result, "timeout"))
1413 else if (streq_ptr(d.result, "canceled"))
1415 else if (!streq_ptr(d.result, "done") && !streq_ptr(d.result, "skipped"))
1423 /* This is slightly dirty, since we don't undo the filter registration. */
1428 static int start_unit_one(
1429 DBusConnection *bus,
1436 DBusMessage *m = NULL, *reply = NULL;
1445 assert(arg_no_block || s);
1447 if (!(m = dbus_message_new_method_call(
1448 "org.freedesktop.systemd1",
1449 "/org/freedesktop/systemd1",
1450 "org.freedesktop.systemd1.Manager",
1452 log_error("Could not allocate message.");
1457 if (!dbus_message_append_args(m,
1458 DBUS_TYPE_STRING, &name,
1459 DBUS_TYPE_STRING, &mode,
1460 DBUS_TYPE_INVALID)) {
1461 log_error("Could not append arguments to message.");
1466 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, error))) {
1468 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(error)) {
1469 /* There's always a fallback possible for
1470 * legacy actions. */
1475 log_error("Failed to issue method call: %s", bus_error_message(error));
1480 if (!dbus_message_get_args(reply, error,
1481 DBUS_TYPE_OBJECT_PATH, &path,
1482 DBUS_TYPE_INVALID)) {
1483 log_error("Failed to parse reply: %s", bus_error_message(error));
1488 if (need_daemon_reload(bus, name))
1489 log_warning("Warning: Unit file of created job changed on disk, 'systemctl %s daemon-reload' recommended.",
1490 arg_scope == UNIT_FILE_SYSTEM ? "--system" : "--user");
1492 if (!arg_no_block) {
1495 if (!(p = strdup(path))) {
1496 log_error("Failed to duplicate path.");
1501 if ((r = set_put(s, p)) < 0) {
1503 log_error("Failed to add path to set.");
1512 dbus_message_unref(m);
1515 dbus_message_unref(reply);
1520 static enum action verb_to_action(const char *verb) {
1521 if (streq(verb, "halt"))
1523 else if (streq(verb, "poweroff"))
1524 return ACTION_POWEROFF;
1525 else if (streq(verb, "reboot"))
1526 return ACTION_REBOOT;
1527 else if (streq(verb, "kexec"))
1528 return ACTION_KEXEC;
1529 else if (streq(verb, "rescue"))
1530 return ACTION_RESCUE;
1531 else if (streq(verb, "emergency"))
1532 return ACTION_EMERGENCY;
1533 else if (streq(verb, "default"))
1534 return ACTION_DEFAULT;
1535 else if (streq(verb, "exit"))
1538 return ACTION_INVALID;
1541 static int start_unit(DBusConnection *bus, char **args) {
1543 static const char * const table[_ACTION_MAX] = {
1544 [ACTION_HALT] = SPECIAL_HALT_TARGET,
1545 [ACTION_POWEROFF] = SPECIAL_POWEROFF_TARGET,
1546 [ACTION_REBOOT] = SPECIAL_REBOOT_TARGET,
1547 [ACTION_KEXEC] = SPECIAL_KEXEC_TARGET,
1548 [ACTION_RUNLEVEL2] = SPECIAL_RUNLEVEL2_TARGET,
1549 [ACTION_RUNLEVEL3] = SPECIAL_RUNLEVEL3_TARGET,
1550 [ACTION_RUNLEVEL4] = SPECIAL_RUNLEVEL4_TARGET,
1551 [ACTION_RUNLEVEL5] = SPECIAL_RUNLEVEL5_TARGET,
1552 [ACTION_RESCUE] = SPECIAL_RESCUE_TARGET,
1553 [ACTION_EMERGENCY] = SPECIAL_EMERGENCY_TARGET,
1554 [ACTION_DEFAULT] = SPECIAL_DEFAULT_TARGET,
1555 [ACTION_EXIT] = SPECIAL_EXIT_TARGET
1559 const char *method, *mode, *one_name;
1564 dbus_error_init(&error);
1568 agent_open_if_enabled();
1570 if (arg_action == ACTION_SYSTEMCTL) {
1572 streq(args[0], "stop") ||
1573 streq(args[0], "condstop") ? "StopUnit" :
1574 streq(args[0], "reload") ? "ReloadUnit" :
1575 streq(args[0], "restart") ? "RestartUnit" :
1577 streq(args[0], "try-restart") ||
1578 streq(args[0], "condrestart") ? "TryRestartUnit" :
1580 streq(args[0], "reload-or-restart") ? "ReloadOrRestartUnit" :
1582 streq(args[0], "reload-or-try-restart") ||
1583 streq(args[0], "condreload") ||
1585 streq(args[0], "force-reload") ? "ReloadOrTryRestartUnit" :
1589 (streq(args[0], "isolate") ||
1590 streq(args[0], "rescue") ||
1591 streq(args[0], "emergency")) ? "isolate" : arg_job_mode;
1593 one_name = table[verb_to_action(args[0])];
1596 assert(arg_action < ELEMENTSOF(table));
1597 assert(table[arg_action]);
1599 method = "StartUnit";
1601 mode = (arg_action == ACTION_EMERGENCY ||
1602 arg_action == ACTION_RESCUE ||
1603 arg_action == ACTION_RUNLEVEL2 ||
1604 arg_action == ACTION_RUNLEVEL3 ||
1605 arg_action == ACTION_RUNLEVEL4 ||
1606 arg_action == ACTION_RUNLEVEL5) ? "isolate" : "replace";
1608 one_name = table[arg_action];
1611 if (!arg_no_block) {
1612 if ((ret = enable_wait_for_jobs(bus)) < 0) {
1613 log_error("Could not watch jobs: %s", strerror(-ret));
1617 if (!(s = set_new(string_hash_func, string_compare_func))) {
1618 log_error("Failed to allocate set.");
1625 if ((ret = start_unit_one(bus, method, one_name, mode, &error, s)) <= 0)
1628 STRV_FOREACH(name, args+1)
1629 if ((r = start_unit_one(bus, method, *name, mode, &error, s)) != 0) {
1630 ret = translate_bus_error_to_exit_status(r, &error);
1631 dbus_error_free(&error);
1636 if ((r = wait_for_jobs(bus, s)) < 0) {
1645 dbus_error_free(&error);
1650 static int start_special(DBusConnection *bus, char **args) {
1657 (streq(args[0], "halt") ||
1658 streq(args[0], "poweroff") ||
1659 streq(args[0], "reboot") ||
1660 streq(args[0], "kexec") ||
1661 streq(args[0], "exit")))
1662 return daemon_reload(bus, args);
1664 r = start_unit(bus, args);
1667 warn_wall(verb_to_action(args[0]));
1672 static int check_unit(DBusConnection *bus, char **args) {
1673 DBusMessage *m = NULL, *reply = NULL;
1675 *interface = "org.freedesktop.systemd1.Unit",
1676 *property = "ActiveState";
1677 int r = 3; /* According to LSB: "program is not running" */
1684 dbus_error_init(&error);
1686 STRV_FOREACH(name, args+1) {
1687 const char *path = NULL;
1689 DBusMessageIter iter, sub;
1691 if (!(m = dbus_message_new_method_call(
1692 "org.freedesktop.systemd1",
1693 "/org/freedesktop/systemd1",
1694 "org.freedesktop.systemd1.Manager",
1696 log_error("Could not allocate message.");
1701 if (!dbus_message_append_args(m,
1702 DBUS_TYPE_STRING, name,
1703 DBUS_TYPE_INVALID)) {
1704 log_error("Could not append arguments to message.");
1709 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1711 /* Hmm, cannot figure out anything about this unit... */
1715 dbus_error_free(&error);
1716 dbus_message_unref(m);
1721 if (!dbus_message_get_args(reply, &error,
1722 DBUS_TYPE_OBJECT_PATH, &path,
1723 DBUS_TYPE_INVALID)) {
1724 log_error("Failed to parse reply: %s", bus_error_message(&error));
1729 dbus_message_unref(m);
1730 if (!(m = dbus_message_new_method_call(
1731 "org.freedesktop.systemd1",
1733 "org.freedesktop.DBus.Properties",
1735 log_error("Could not allocate message.");
1740 if (!dbus_message_append_args(m,
1741 DBUS_TYPE_STRING, &interface,
1742 DBUS_TYPE_STRING, &property,
1743 DBUS_TYPE_INVALID)) {
1744 log_error("Could not append arguments to message.");
1749 dbus_message_unref(reply);
1750 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1751 log_error("Failed to issue method call: %s", bus_error_message(&error));
1756 if (!dbus_message_iter_init(reply, &iter) ||
1757 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1758 log_error("Failed to parse reply.");
1763 dbus_message_iter_recurse(&iter, &sub);
1765 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1766 log_error("Failed to parse reply.");
1771 dbus_message_iter_get_basic(&sub, &state);
1776 if (streq(state, "active") || streq(state, "reloading"))
1779 dbus_message_unref(m);
1780 dbus_message_unref(reply);
1786 dbus_message_unref(m);
1789 dbus_message_unref(reply);
1791 dbus_error_free(&error);
1796 static int kill_unit(DBusConnection *bus, char **args) {
1797 DBusMessage *m = NULL, *reply = NULL;
1805 dbus_error_init(&error);
1808 arg_kill_who = "all";
1811 arg_kill_mode = streq(arg_kill_who, "all") ? "control-group" : "process";
1813 STRV_FOREACH(name, args+1) {
1815 if (!(m = dbus_message_new_method_call(
1816 "org.freedesktop.systemd1",
1817 "/org/freedesktop/systemd1",
1818 "org.freedesktop.systemd1.Manager",
1820 log_error("Could not allocate message.");
1825 if (!dbus_message_append_args(m,
1826 DBUS_TYPE_STRING, name,
1827 DBUS_TYPE_STRING, &arg_kill_who,
1828 DBUS_TYPE_STRING, &arg_kill_mode,
1829 DBUS_TYPE_INT32, &arg_signal,
1830 DBUS_TYPE_INVALID)) {
1831 log_error("Could not append arguments to message.");
1836 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1837 log_error("Failed to issue method call: %s", bus_error_message(&error));
1838 dbus_error_free(&error);
1842 dbus_message_unref(m);
1845 dbus_message_unref(reply);
1851 dbus_message_unref(m);
1854 dbus_message_unref(reply);
1856 dbus_error_free(&error);
1861 typedef struct ExecStatusInfo {
1869 usec_t start_timestamp;
1870 usec_t exit_timestamp;
1875 LIST_FIELDS(struct ExecStatusInfo, exec);
1878 static void exec_status_info_free(ExecStatusInfo *i) {
1887 static int exec_status_info_deserialize(DBusMessageIter *sub, ExecStatusInfo *i) {
1888 uint64_t start_timestamp, exit_timestamp, start_timestamp_monotonic, exit_timestamp_monotonic;
1889 DBusMessageIter sub2, sub3;
1893 int32_t code, status;
1899 if (dbus_message_iter_get_arg_type(sub) != DBUS_TYPE_STRUCT)
1902 dbus_message_iter_recurse(sub, &sub2);
1904 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
1907 if (!(i->path = strdup(path)))
1910 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
1911 dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
1915 dbus_message_iter_recurse(&sub2, &sub3);
1916 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1917 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1918 dbus_message_iter_next(&sub3);
1923 if (!(i->argv = new0(char*, n+1)))
1927 dbus_message_iter_recurse(&sub2, &sub3);
1928 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1931 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1932 dbus_message_iter_get_basic(&sub3, &s);
1933 dbus_message_iter_next(&sub3);
1935 if (!(i->argv[n++] = strdup(s)))
1939 if (!dbus_message_iter_next(&sub2) ||
1940 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, true) < 0 ||
1941 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp, true) < 0 ||
1942 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp_monotonic, true) < 0 ||
1943 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp, true) < 0 ||
1944 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp_monotonic, true) < 0 ||
1945 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) < 0 ||
1946 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) < 0 ||
1947 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) < 0)
1951 i->start_timestamp = (usec_t) start_timestamp;
1952 i->exit_timestamp = (usec_t) exit_timestamp;
1953 i->pid = (pid_t) pid;
1960 typedef struct UnitStatusInfo {
1962 const char *load_state;
1963 const char *active_state;
1964 const char *sub_state;
1965 const char *unit_file_state;
1967 const char *description;
1968 const char *following;
1971 const char *default_control_group;
1973 const char *load_error;
1975 usec_t inactive_exit_timestamp;
1976 usec_t active_enter_timestamp;
1977 usec_t active_exit_timestamp;
1978 usec_t inactive_enter_timestamp;
1980 bool need_daemon_reload;
1985 const char *status_text;
1987 #ifdef HAVE_SYSV_COMPAT
1991 usec_t start_timestamp;
1992 usec_t exit_timestamp;
1994 int exit_code, exit_status;
1996 usec_t condition_timestamp;
1997 bool condition_result;
2000 unsigned n_accepted;
2001 unsigned n_connections;
2005 const char *sysfs_path;
2007 /* Mount, Automount */
2013 LIST_HEAD(ExecStatusInfo, exec);
2016 static void print_status_info(UnitStatusInfo *i) {
2018 const char *on, *off, *ss;
2020 char since1[FORMAT_TIMESTAMP_PRETTY_MAX], *s1;
2021 char since2[FORMAT_TIMESTAMP_MAX], *s2;
2025 /* This shows pretty information about a unit. See
2026 * print_property() for a low-level property printer */
2028 printf("%s", strna(i->id));
2030 if (i->description && !streq_ptr(i->id, i->description))
2031 printf(" - %s", i->description);
2036 printf("\t Follow: unit currently follows state of %s\n", i->following);
2038 if (streq_ptr(i->load_state, "failed") ||
2039 streq_ptr(i->load_state, "banned")) {
2040 on = ansi_highlight(true);
2041 off = ansi_highlight(false);
2046 printf("\t Loaded: %s%s%s (Reason: %s)\n", on, strna(i->load_state), off, i->load_error);
2047 else if (i->path && i->unit_file_state)
2048 printf("\t Loaded: %s%s%s (%s; %s)\n", on, strna(i->load_state), off, i->path, i->unit_file_state);
2050 printf("\t Loaded: %s%s%s (%s)\n", on, strna(i->load_state), off, i->path);
2052 printf("\t Loaded: %s%s%s\n", on, strna(i->load_state), off);
2054 ss = streq_ptr(i->active_state, i->sub_state) ? NULL : i->sub_state;
2056 if (streq_ptr(i->active_state, "failed")) {
2057 on = ansi_highlight(true);
2058 off = ansi_highlight(false);
2059 } else if (streq_ptr(i->active_state, "active") || streq_ptr(i->active_state, "reloading")) {
2060 on = ansi_highlight_green(true);
2061 off = ansi_highlight_green(false);
2066 printf("\t Active: %s%s (%s)%s",
2068 strna(i->active_state),
2072 printf("\t Active: %s%s%s",
2074 strna(i->active_state),
2077 timestamp = (streq_ptr(i->active_state, "active") ||
2078 streq_ptr(i->active_state, "reloading")) ? i->active_enter_timestamp :
2079 (streq_ptr(i->active_state, "inactive") ||
2080 streq_ptr(i->active_state, "failed")) ? i->inactive_enter_timestamp :
2081 streq_ptr(i->active_state, "activating") ? i->inactive_exit_timestamp :
2082 i->active_exit_timestamp;
2084 s1 = format_timestamp_pretty(since1, sizeof(since1), timestamp);
2085 s2 = format_timestamp(since2, sizeof(since2), timestamp);
2088 printf(" since %s; %s\n", s2, s1);
2090 printf(" since %s\n", s2);
2094 if (!i->condition_result && i->condition_timestamp > 0) {
2095 s1 = format_timestamp_pretty(since1, sizeof(since1), i->condition_timestamp);
2096 s2 = format_timestamp(since2, sizeof(since2), i->condition_timestamp);
2099 printf("\t start condition failed at %s; %s\n", s2, s1);
2101 printf("\t start condition failed at %s\n", s2);
2105 printf("\t Device: %s\n", i->sysfs_path);
2107 printf("\t Where: %s\n", i->where);
2109 printf("\t What: %s\n", i->what);
2112 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
2114 LIST_FOREACH(exec, p, i->exec) {
2118 /* Only show exited processes here */
2122 t = strv_join(p->argv, " ");
2123 printf("\t Process: %u %s=%s ", p->pid, p->name, strna(t));
2126 #ifdef HAVE_SYSV_COMPAT
2128 good = is_clean_exit_lsb(p->code, p->status);
2131 good = is_clean_exit(p->code, p->status);
2134 on = ansi_highlight(true);
2135 off = ansi_highlight(false);
2139 printf("%s(code=%s, ", on, sigchld_code_to_string(p->code));
2141 if (p->code == CLD_EXITED) {
2144 printf("status=%i", p->status);
2146 #ifdef HAVE_SYSV_COMPAT
2147 if ((c = exit_status_to_string(p->status, i->is_sysv ? EXIT_STATUS_LSB : EXIT_STATUS_SYSTEMD)))
2149 if ((c = exit_status_to_string(p->status, EXIT_STATUS_SYSTEMD)))
2154 printf("signal=%s", signal_to_string(p->status));
2156 printf(")%s\n", off);
2160 if (i->main_pid == p->pid &&
2161 i->start_timestamp == p->start_timestamp &&
2162 i->exit_timestamp == p->start_timestamp)
2163 /* Let's not show this twice */
2166 if (p->pid == i->control_pid)
2170 if (i->main_pid > 0 || i->control_pid > 0) {
2173 if (i->main_pid > 0) {
2174 printf("Main PID: %u", (unsigned) i->main_pid);
2178 get_process_name(i->main_pid, &t);
2183 } else if (i->exit_code > 0) {
2184 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
2186 if (i->exit_code == CLD_EXITED) {
2189 printf("status=%i", i->exit_status);
2191 #ifdef HAVE_SYSV_COMPAT
2192 if ((c = exit_status_to_string(i->exit_status, i->is_sysv ? EXIT_STATUS_LSB : EXIT_STATUS_SYSTEMD)))
2194 if ((c = exit_status_to_string(i->exit_status, EXIT_STATUS_SYSTEMD)))
2199 printf("signal=%s", signal_to_string(i->exit_status));
2204 if (i->main_pid > 0 && i->control_pid > 0)
2207 if (i->control_pid > 0) {
2210 printf(" Control: %u", (unsigned) i->control_pid);
2212 get_process_name(i->control_pid, &t);
2223 printf("\t Status: \"%s\"\n", i->status_text);
2225 if (i->default_control_group) {
2228 printf("\t CGroup: %s\n", i->default_control_group);
2230 if (arg_transport != TRANSPORT_SSH) {
2231 if ((c = columns()) > 18)
2236 show_cgroup_by_path(i->default_control_group, "\t\t ", c);
2240 if (i->need_daemon_reload)
2241 printf("\n%sWarning:%s Unit file changed on disk, 'systemctl %s daemon-reload' recommended.\n",
2242 ansi_highlight(true),
2243 ansi_highlight(false),
2244 arg_scope == UNIT_FILE_SYSTEM ? "--system" : "--user");
2247 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
2253 switch (dbus_message_iter_get_arg_type(iter)) {
2255 case DBUS_TYPE_STRING: {
2258 dbus_message_iter_get_basic(iter, &s);
2261 if (streq(name, "Id"))
2263 else if (streq(name, "LoadState"))
2265 else if (streq(name, "ActiveState"))
2266 i->active_state = s;
2267 else if (streq(name, "SubState"))
2269 else if (streq(name, "Description"))
2271 else if (streq(name, "FragmentPath"))
2273 #ifdef HAVE_SYSV_COMPAT
2274 else if (streq(name, "SysVPath")) {
2279 else if (streq(name, "DefaultControlGroup"))
2280 i->default_control_group = s;
2281 else if (streq(name, "StatusText"))
2283 else if (streq(name, "SysFSPath"))
2285 else if (streq(name, "Where"))
2287 else if (streq(name, "What"))
2289 else if (streq(name, "Following"))
2291 else if (streq(name, "UnitFileState"))
2292 i->unit_file_state = s;
2298 case DBUS_TYPE_BOOLEAN: {
2301 dbus_message_iter_get_basic(iter, &b);
2303 if (streq(name, "Accept"))
2305 else if (streq(name, "NeedDaemonReload"))
2306 i->need_daemon_reload = b;
2307 else if (streq(name, "ConditionResult"))
2308 i->condition_result = b;
2313 case DBUS_TYPE_UINT32: {
2316 dbus_message_iter_get_basic(iter, &u);
2318 if (streq(name, "MainPID")) {
2320 i->main_pid = (pid_t) u;
2323 } else if (streq(name, "ControlPID"))
2324 i->control_pid = (pid_t) u;
2325 else if (streq(name, "ExecMainPID")) {
2327 i->main_pid = (pid_t) u;
2328 } else if (streq(name, "NAccepted"))
2330 else if (streq(name, "NConnections"))
2331 i->n_connections = u;
2336 case DBUS_TYPE_INT32: {
2339 dbus_message_iter_get_basic(iter, &j);
2341 if (streq(name, "ExecMainCode"))
2342 i->exit_code = (int) j;
2343 else if (streq(name, "ExecMainStatus"))
2344 i->exit_status = (int) j;
2349 case DBUS_TYPE_UINT64: {
2352 dbus_message_iter_get_basic(iter, &u);
2354 if (streq(name, "ExecMainStartTimestamp"))
2355 i->start_timestamp = (usec_t) u;
2356 else if (streq(name, "ExecMainExitTimestamp"))
2357 i->exit_timestamp = (usec_t) u;
2358 else if (streq(name, "ActiveEnterTimestamp"))
2359 i->active_enter_timestamp = (usec_t) u;
2360 else if (streq(name, "InactiveEnterTimestamp"))
2361 i->inactive_enter_timestamp = (usec_t) u;
2362 else if (streq(name, "InactiveExitTimestamp"))
2363 i->inactive_exit_timestamp = (usec_t) u;
2364 else if (streq(name, "ActiveExitTimestamp"))
2365 i->active_exit_timestamp = (usec_t) u;
2366 else if (streq(name, "ConditionTimestamp"))
2367 i->condition_timestamp = (usec_t) u;
2372 case DBUS_TYPE_ARRAY: {
2374 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT &&
2375 startswith(name, "Exec")) {
2376 DBusMessageIter sub;
2378 dbus_message_iter_recurse(iter, &sub);
2379 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2380 ExecStatusInfo *info;
2383 if (!(info = new0(ExecStatusInfo, 1)))
2386 if (!(info->name = strdup(name))) {
2391 if ((r = exec_status_info_deserialize(&sub, info)) < 0) {
2396 LIST_PREPEND(ExecStatusInfo, exec, i->exec, info);
2398 dbus_message_iter_next(&sub);
2405 case DBUS_TYPE_STRUCT: {
2407 if (streq(name, "LoadError")) {
2408 DBusMessageIter sub;
2409 const char *n, *message;
2412 dbus_message_iter_recurse(iter, &sub);
2414 r = bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &n, true);
2418 r = bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &message, false);
2422 if (!isempty(message))
2423 i->load_error = message;
2433 static int print_property(const char *name, DBusMessageIter *iter) {
2437 /* This is a low-level property printer, see
2438 * print_status_info() for the nicer output */
2440 if (arg_property && !strv_find(arg_property, name))
2443 switch (dbus_message_iter_get_arg_type(iter)) {
2445 case DBUS_TYPE_STRUCT: {
2446 DBusMessageIter sub;
2447 dbus_message_iter_recurse(iter, &sub);
2449 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
2452 dbus_message_iter_get_basic(&sub, &u);
2455 printf("%s=%u\n", name, (unsigned) u);
2457 printf("%s=\n", name);
2460 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
2463 dbus_message_iter_get_basic(&sub, &s);
2465 if (arg_all || s[0])
2466 printf("%s=%s\n", name, s);
2469 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "LoadError")) {
2470 const char *a = NULL, *b = NULL;
2472 if (bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &a, true) >= 0)
2473 bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &b, false);
2475 if (arg_all || !isempty(a) || !isempty(b))
2476 printf("%s=%s \"%s\"\n", name, strempty(a), strempty(b));
2484 case DBUS_TYPE_ARRAY:
2486 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "EnvironmentFiles")) {
2487 DBusMessageIter sub, sub2;
2489 dbus_message_iter_recurse(iter, &sub);
2490 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2494 dbus_message_iter_recurse(&sub, &sub2);
2496 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) >= 0 &&
2497 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, false) >= 0)
2498 printf("EnvironmentFile=%s (ignore_errors=%s)\n", path, yes_no(ignore));
2500 dbus_message_iter_next(&sub);
2505 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
2506 DBusMessageIter sub, sub2;
2508 dbus_message_iter_recurse(iter, &sub);
2509 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2510 const char *type, *path;
2512 dbus_message_iter_recurse(&sub, &sub2);
2514 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
2515 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
2516 printf("%s=%s\n", type, path);
2518 dbus_message_iter_next(&sub);
2523 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
2524 DBusMessageIter sub, sub2;
2526 dbus_message_iter_recurse(iter, &sub);
2527 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2529 uint64_t value, next_elapse;
2531 dbus_message_iter_recurse(&sub, &sub2);
2533 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
2534 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
2535 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
2536 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
2538 printf("%s={ value=%s ; next_elapse=%s }\n",
2540 format_timespan(timespan1, sizeof(timespan1), value),
2541 format_timespan(timespan2, sizeof(timespan2), next_elapse));
2544 dbus_message_iter_next(&sub);
2549 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
2550 DBusMessageIter sub;
2552 dbus_message_iter_recurse(iter, &sub);
2553 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2554 ExecStatusInfo info;
2557 if (exec_status_info_deserialize(&sub, &info) >= 0) {
2558 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
2561 t = strv_join(info.argv, " ");
2563 printf("%s={ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n",
2567 yes_no(info.ignore),
2568 strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)),
2569 strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)),
2570 (unsigned) info. pid,
2571 sigchld_code_to_string(info.code),
2573 info.code == CLD_EXITED ? "" : "/",
2574 strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status)));
2580 strv_free(info.argv);
2582 dbus_message_iter_next(&sub);
2591 if (generic_print_property(name, iter, arg_all) > 0)
2595 printf("%s=[unprintable]\n", name);
2600 static int show_one(const char *verb, DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
2601 DBusMessage *m = NULL, *reply = NULL;
2602 const char *interface = "";
2605 DBusMessageIter iter, sub, sub2, sub3;
2606 UnitStatusInfo info;
2614 dbus_error_init(&error);
2616 if (!(m = dbus_message_new_method_call(
2617 "org.freedesktop.systemd1",
2619 "org.freedesktop.DBus.Properties",
2621 log_error("Could not allocate message.");
2626 if (!dbus_message_append_args(m,
2627 DBUS_TYPE_STRING, &interface,
2628 DBUS_TYPE_INVALID)) {
2629 log_error("Could not append arguments to message.");
2634 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2635 log_error("Failed to issue method call: %s", bus_error_message(&error));
2640 if (!dbus_message_iter_init(reply, &iter) ||
2641 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
2642 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
2643 log_error("Failed to parse reply.");
2648 dbus_message_iter_recurse(&iter, &sub);
2655 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2658 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
2659 log_error("Failed to parse reply.");
2664 dbus_message_iter_recurse(&sub, &sub2);
2666 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
2667 log_error("Failed to parse reply.");
2672 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
2673 log_error("Failed to parse reply.");
2678 dbus_message_iter_recurse(&sub2, &sub3);
2680 if (show_properties)
2681 r = print_property(name, &sub3);
2683 r = status_property(name, &sub3, &info);
2686 log_error("Failed to parse reply.");
2691 dbus_message_iter_next(&sub);
2696 if (!show_properties)
2697 print_status_info(&info);
2699 if (!streq_ptr(info.active_state, "active") &&
2700 !streq_ptr(info.active_state, "reloading") &&
2701 streq(verb, "status"))
2702 /* According to LSB: "program not running" */
2705 while ((p = info.exec)) {
2706 LIST_REMOVE(ExecStatusInfo, exec, info.exec, p);
2707 exec_status_info_free(p);
2712 dbus_message_unref(m);
2715 dbus_message_unref(reply);
2717 dbus_error_free(&error);
2722 static int show(DBusConnection *bus, char **args) {
2723 DBusMessage *m = NULL, *reply = NULL;
2726 bool show_properties, new_line = false;
2732 dbus_error_init(&error);
2734 show_properties = !streq(args[0], "status");
2736 if (show_properties)
2737 pager_open_if_enabled();
2739 if (show_properties && strv_length(args) <= 1) {
2740 /* If not argument is specified inspect the manager
2743 ret = show_one(args[0], bus, "/org/freedesktop/systemd1", show_properties, &new_line);
2747 STRV_FOREACH(name, args+1) {
2748 const char *path = NULL;
2751 if (safe_atou32(*name, &id) < 0) {
2753 /* Interpret as unit name */
2755 if (!(m = dbus_message_new_method_call(
2756 "org.freedesktop.systemd1",
2757 "/org/freedesktop/systemd1",
2758 "org.freedesktop.systemd1.Manager",
2760 log_error("Could not allocate message.");
2765 if (!dbus_message_append_args(m,
2766 DBUS_TYPE_STRING, name,
2767 DBUS_TYPE_INVALID)) {
2768 log_error("Could not append arguments to message.");
2773 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2775 if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
2776 log_error("Failed to issue method call: %s", bus_error_message(&error));
2781 dbus_error_free(&error);
2783 dbus_message_unref(m);
2784 if (!(m = dbus_message_new_method_call(
2785 "org.freedesktop.systemd1",
2786 "/org/freedesktop/systemd1",
2787 "org.freedesktop.systemd1.Manager",
2789 log_error("Could not allocate message.");
2794 if (!dbus_message_append_args(m,
2795 DBUS_TYPE_STRING, name,
2796 DBUS_TYPE_INVALID)) {
2797 log_error("Could not append arguments to message.");
2802 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2803 log_error("Failed to issue method call: %s", bus_error_message(&error));
2805 if (dbus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT))
2806 ret = 4; /* According to LSB: "program or service status is unknown" */
2813 } else if (show_properties) {
2815 /* Interpret as job id */
2817 if (!(m = dbus_message_new_method_call(
2818 "org.freedesktop.systemd1",
2819 "/org/freedesktop/systemd1",
2820 "org.freedesktop.systemd1.Manager",
2822 log_error("Could not allocate message.");
2827 if (!dbus_message_append_args(m,
2828 DBUS_TYPE_UINT32, &id,
2829 DBUS_TYPE_INVALID)) {
2830 log_error("Could not append arguments to message.");
2835 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2836 log_error("Failed to issue method call: %s", bus_error_message(&error));
2842 /* Interpret as PID */
2844 if (!(m = dbus_message_new_method_call(
2845 "org.freedesktop.systemd1",
2846 "/org/freedesktop/systemd1",
2847 "org.freedesktop.systemd1.Manager",
2849 log_error("Could not allocate message.");
2854 if (!dbus_message_append_args(m,
2855 DBUS_TYPE_UINT32, &id,
2856 DBUS_TYPE_INVALID)) {
2857 log_error("Could not append arguments to message.");
2862 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2863 log_error("Failed to issue method call: %s", bus_error_message(&error));
2869 if (!dbus_message_get_args(reply, &error,
2870 DBUS_TYPE_OBJECT_PATH, &path,
2871 DBUS_TYPE_INVALID)) {
2872 log_error("Failed to parse reply: %s", bus_error_message(&error));
2877 if ((r = show_one(args[0], bus, path, show_properties, &new_line)) != 0)
2880 dbus_message_unref(m);
2881 dbus_message_unref(reply);
2887 dbus_message_unref(m);
2890 dbus_message_unref(reply);
2892 dbus_error_free(&error);
2897 static int dump(DBusConnection *bus, char **args) {
2898 DBusMessage *m = NULL, *reply = NULL;
2903 dbus_error_init(&error);
2905 pager_open_if_enabled();
2907 if (!(m = dbus_message_new_method_call(
2908 "org.freedesktop.systemd1",
2909 "/org/freedesktop/systemd1",
2910 "org.freedesktop.systemd1.Manager",
2912 log_error("Could not allocate message.");
2916 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2917 log_error("Failed to issue method call: %s", bus_error_message(&error));
2922 if (!dbus_message_get_args(reply, &error,
2923 DBUS_TYPE_STRING, &text,
2924 DBUS_TYPE_INVALID)) {
2925 log_error("Failed to parse reply: %s", bus_error_message(&error));
2930 fputs(text, stdout);
2936 dbus_message_unref(m);
2939 dbus_message_unref(reply);
2941 dbus_error_free(&error);
2946 static int snapshot(DBusConnection *bus, char **args) {
2947 DBusMessage *m = NULL, *reply = NULL;
2950 const char *name = "", *path, *id;
2951 dbus_bool_t cleanup = FALSE;
2952 DBusMessageIter iter, sub;
2954 *interface = "org.freedesktop.systemd1.Unit",
2957 dbus_error_init(&error);
2959 if (!(m = dbus_message_new_method_call(
2960 "org.freedesktop.systemd1",
2961 "/org/freedesktop/systemd1",
2962 "org.freedesktop.systemd1.Manager",
2963 "CreateSnapshot"))) {
2964 log_error("Could not allocate message.");
2968 if (strv_length(args) > 1)
2971 if (!dbus_message_append_args(m,
2972 DBUS_TYPE_STRING, &name,
2973 DBUS_TYPE_BOOLEAN, &cleanup,
2974 DBUS_TYPE_INVALID)) {
2975 log_error("Could not append arguments to message.");
2980 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2981 log_error("Failed to issue method call: %s", bus_error_message(&error));
2986 if (!dbus_message_get_args(reply, &error,
2987 DBUS_TYPE_OBJECT_PATH, &path,
2988 DBUS_TYPE_INVALID)) {
2989 log_error("Failed to parse reply: %s", bus_error_message(&error));
2994 dbus_message_unref(m);
2995 if (!(m = dbus_message_new_method_call(
2996 "org.freedesktop.systemd1",
2998 "org.freedesktop.DBus.Properties",
3000 log_error("Could not allocate message.");
3004 if (!dbus_message_append_args(m,
3005 DBUS_TYPE_STRING, &interface,
3006 DBUS_TYPE_STRING, &property,
3007 DBUS_TYPE_INVALID)) {
3008 log_error("Could not append arguments to message.");
3013 dbus_message_unref(reply);
3014 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3015 log_error("Failed to issue method call: %s", bus_error_message(&error));
3020 if (!dbus_message_iter_init(reply, &iter) ||
3021 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
3022 log_error("Failed to parse reply.");
3027 dbus_message_iter_recurse(&iter, &sub);
3029 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
3030 log_error("Failed to parse reply.");
3035 dbus_message_iter_get_basic(&sub, &id);
3043 dbus_message_unref(m);
3046 dbus_message_unref(reply);
3048 dbus_error_free(&error);
3053 static int delete_snapshot(DBusConnection *bus, char **args) {
3054 DBusMessage *m = NULL, *reply = NULL;
3062 dbus_error_init(&error);
3064 STRV_FOREACH(name, args+1) {
3065 const char *path = NULL;
3067 if (!(m = dbus_message_new_method_call(
3068 "org.freedesktop.systemd1",
3069 "/org/freedesktop/systemd1",
3070 "org.freedesktop.systemd1.Manager",
3072 log_error("Could not allocate message.");
3077 if (!dbus_message_append_args(m,
3078 DBUS_TYPE_STRING, name,
3079 DBUS_TYPE_INVALID)) {
3080 log_error("Could not append arguments to message.");
3085 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3086 log_error("Failed to issue method call: %s", bus_error_message(&error));
3091 if (!dbus_message_get_args(reply, &error,
3092 DBUS_TYPE_OBJECT_PATH, &path,
3093 DBUS_TYPE_INVALID)) {
3094 log_error("Failed to parse reply: %s", bus_error_message(&error));
3099 dbus_message_unref(m);
3100 if (!(m = dbus_message_new_method_call(
3101 "org.freedesktop.systemd1",
3103 "org.freedesktop.systemd1.Snapshot",
3105 log_error("Could not allocate message.");
3110 dbus_message_unref(reply);
3111 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3112 log_error("Failed to issue method call: %s", bus_error_message(&error));
3117 dbus_message_unref(m);
3118 dbus_message_unref(reply);
3126 dbus_message_unref(m);
3129 dbus_message_unref(reply);
3131 dbus_error_free(&error);
3136 static int daemon_reload(DBusConnection *bus, char **args) {
3137 DBusMessage *m = NULL, *reply = NULL;
3142 dbus_error_init(&error);
3144 if (arg_action == ACTION_RELOAD)
3146 else if (arg_action == ACTION_REEXEC)
3147 method = "Reexecute";
3149 assert(arg_action == ACTION_SYSTEMCTL);
3152 streq(args[0], "clear-jobs") ||
3153 streq(args[0], "cancel") ? "ClearJobs" :
3154 streq(args[0], "daemon-reexec") ? "Reexecute" :
3155 streq(args[0], "reset-failed") ? "ResetFailed" :
3156 streq(args[0], "halt") ? "Halt" :
3157 streq(args[0], "poweroff") ? "PowerOff" :
3158 streq(args[0], "reboot") ? "Reboot" :
3159 streq(args[0], "kexec") ? "KExec" :
3160 streq(args[0], "exit") ? "Exit" :
3161 /* "daemon-reload" */ "Reload";
3164 if (!(m = dbus_message_new_method_call(
3165 "org.freedesktop.systemd1",
3166 "/org/freedesktop/systemd1",
3167 "org.freedesktop.systemd1.Manager",
3169 log_error("Could not allocate message.");
3173 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3175 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
3176 /* There's always a fallback possible for
3177 * legacy actions. */
3182 if (streq(method, "Reexecute") && dbus_error_has_name(&error, DBUS_ERROR_NO_REPLY)) {
3183 /* On reexecution, we expect a disconnect, not
3189 log_error("Failed to issue method call: %s", bus_error_message(&error));
3198 dbus_message_unref(m);
3201 dbus_message_unref(reply);
3203 dbus_error_free(&error);
3208 static int reset_failed(DBusConnection *bus, char **args) {
3209 DBusMessage *m = NULL, *reply = NULL;
3215 dbus_error_init(&error);
3217 if (strv_length(args) <= 1)
3218 return daemon_reload(bus, args);
3220 STRV_FOREACH(name, args+1) {
3222 if (!(m = dbus_message_new_method_call(
3223 "org.freedesktop.systemd1",
3224 "/org/freedesktop/systemd1",
3225 "org.freedesktop.systemd1.Manager",
3226 "ResetFailedUnit"))) {
3227 log_error("Could not allocate message.");
3232 if (!dbus_message_append_args(m,
3233 DBUS_TYPE_STRING, name,
3234 DBUS_TYPE_INVALID)) {
3235 log_error("Could not append arguments to message.");
3240 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3241 log_error("Failed to issue method call: %s", bus_error_message(&error));
3246 dbus_message_unref(m);
3247 dbus_message_unref(reply);
3255 dbus_message_unref(m);
3258 dbus_message_unref(reply);
3260 dbus_error_free(&error);
3265 static int show_enviroment(DBusConnection *bus, char **args) {
3266 DBusMessage *m = NULL, *reply = NULL;
3268 DBusMessageIter iter, sub, sub2;
3271 *interface = "org.freedesktop.systemd1.Manager",
3272 *property = "Environment";
3274 dbus_error_init(&error);
3276 pager_open_if_enabled();
3278 if (!(m = dbus_message_new_method_call(
3279 "org.freedesktop.systemd1",
3280 "/org/freedesktop/systemd1",
3281 "org.freedesktop.DBus.Properties",
3283 log_error("Could not allocate message.");
3287 if (!dbus_message_append_args(m,
3288 DBUS_TYPE_STRING, &interface,
3289 DBUS_TYPE_STRING, &property,
3290 DBUS_TYPE_INVALID)) {
3291 log_error("Could not append arguments to message.");
3296 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3297 log_error("Failed to issue method call: %s", bus_error_message(&error));
3302 if (!dbus_message_iter_init(reply, &iter) ||
3303 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
3304 log_error("Failed to parse reply.");
3309 dbus_message_iter_recurse(&iter, &sub);
3311 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
3312 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
3313 log_error("Failed to parse reply.");
3318 dbus_message_iter_recurse(&sub, &sub2);
3320 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
3323 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
3324 log_error("Failed to parse reply.");
3329 dbus_message_iter_get_basic(&sub2, &text);
3330 printf("%s\n", text);
3332 dbus_message_iter_next(&sub2);
3339 dbus_message_unref(m);
3342 dbus_message_unref(reply);
3344 dbus_error_free(&error);
3349 static int set_environment(DBusConnection *bus, char **args) {
3350 DBusMessage *m = NULL, *reply = NULL;
3354 DBusMessageIter iter, sub;
3357 dbus_error_init(&error);
3359 method = streq(args[0], "set-environment")
3361 : "UnsetEnvironment";
3363 if (!(m = dbus_message_new_method_call(
3364 "org.freedesktop.systemd1",
3365 "/org/freedesktop/systemd1",
3366 "org.freedesktop.systemd1.Manager",
3369 log_error("Could not allocate message.");
3373 dbus_message_iter_init_append(m, &iter);
3375 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
3376 log_error("Could not append arguments to message.");
3381 STRV_FOREACH(name, args+1)
3382 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, name)) {
3383 log_error("Could not append arguments to message.");
3388 if (!dbus_message_iter_close_container(&iter, &sub)) {
3389 log_error("Could not append arguments to message.");
3394 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3395 log_error("Failed to issue method call: %s", bus_error_message(&error));
3404 dbus_message_unref(m);
3407 dbus_message_unref(reply);
3409 dbus_error_free(&error);
3414 static int enable_sysv_units(char **args) {
3417 #if defined (HAVE_SYSV_COMPAT) && (defined(TARGET_FEDORA) || defined(TARGET_MANDRIVA) || defined(TARGET_SUSE) || defined(TARGET_MEEGO) || defined(TARGET_ALTLINUX))
3418 const char *verb = args[0];
3419 unsigned f = 1, t = 1;
3422 if (arg_scope != UNIT_FILE_SYSTEM)
3425 if (!streq(verb, "enable") &&
3426 !streq(verb, "disable") &&
3427 !streq(verb, "is-enabled"))
3430 /* Processes all SysV units, and reshuffles the array so that
3431 * afterwards only the native units remain */
3434 r = lookup_paths_init(&paths, MANAGER_SYSTEM, false);
3440 for (f = 1; args[f]; f++) {
3443 bool found_native = false, found_sysv;
3445 const char *argv[6] = { "/sbin/chkconfig", NULL, NULL, NULL, NULL };
3446 char **k, *l, *q = NULL;
3453 if (!endswith(name, ".service"))
3456 if (path_is_absolute(name))
3459 STRV_FOREACH(k, paths.unit_path) {
3462 if (!isempty(arg_root))
3463 asprintf(&p, "%s/%s/%s", arg_root, *k, name);
3465 asprintf(&p, "%s/%s", *k, name);
3468 log_error("No memory");
3473 found_native = access(p, F_OK) >= 0;
3484 if (!isempty(arg_root))
3485 asprintf(&p, "%s/" SYSTEM_SYSVINIT_PATH "/%s", arg_root, name);
3487 asprintf(&p, SYSTEM_SYSVINIT_PATH "/%s", name);
3489 log_error("No memory");
3494 p[strlen(p) - sizeof(".service") + 1] = 0;
3495 found_sysv = access(p, F_OK) >= 0;
3502 /* Mark this entry, so that we don't try enabling it as native unit */
3503 args[f] = (char*) "";
3505 log_info("%s is not a native service, redirecting to /sbin/chkconfig.", name);
3507 if (!isempty(arg_root))
3508 argv[c++] = q = strappend("--root=", arg_root);
3510 argv[c++] = file_name_from_path(p);
3512 streq(verb, "enable") ? "on" :
3513 streq(verb, "disable") ? "off" : "--level=5";
3516 l = strv_join((char**)argv, " ");
3518 log_error("No memory.");
3525 log_info("Executing %s", l);
3530 log_error("Failed to fork: %m");
3535 } else if (pid == 0) {
3538 execv(argv[0], (char**) argv);
3539 _exit(EXIT_FAILURE);
3545 j = wait_for_terminate(pid, &status);
3547 log_error("Failed to wait for child: %s", strerror(-r));
3552 if (status.si_code == CLD_EXITED) {
3553 if (streq(verb, "is-enabled")) {
3554 if (status.si_status == 0) {
3563 } else if (status.si_status != 0) {
3574 lookup_paths_free(&paths);
3576 /* Drop all SysV units */
3577 for (f = 1, t = 1; args[f]; f++) {
3579 if (isempty(args[f]))
3582 args[t++] = args[f];
3591 static int enable_unit(DBusConnection *bus, char **args) {
3592 const char *verb = args[0];
3593 UnitFileChange *changes = NULL;
3594 unsigned n_changes = 0, i;
3595 int carries_install_info = -1;
3596 DBusMessage *m = NULL, *reply = NULL;
3600 dbus_error_init(&error);
3602 r = enable_sysv_units(args);
3606 if (!bus || avoid_bus()) {
3607 if (streq(verb, "enable")) {
3608 r = unit_file_enable(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3609 carries_install_info = r;
3610 } else if (streq(verb, "disable"))
3611 r = unit_file_disable(arg_scope, arg_runtime, arg_root, args+1, &changes, &n_changes);
3612 else if (streq(verb, "reenable")) {
3613 r = unit_file_reenable(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3614 carries_install_info = r;
3615 } else if (streq(verb, "link"))
3616 r = unit_file_link(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3617 else if (streq(verb, "preset")) {
3618 r = unit_file_preset(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3619 carries_install_info = r;
3620 } else if (streq(verb, "mask"))
3621 r = unit_file_mask(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3622 else if (streq(verb, "unmask"))
3623 r = unit_file_unmask(arg_scope, arg_runtime, arg_root, args+1, &changes, &n_changes);
3625 assert_not_reached("Unknown verb");
3628 log_error("Operation failed: %s", strerror(-r));
3632 for (i = 0; i < n_changes; i++) {
3633 if (changes[i].type == UNIT_FILE_SYMLINK)
3634 log_info("ln -s '%s' '%s'", changes[i].source, changes[i].path);
3636 log_info("rm '%s'", changes[i].path);
3641 bool send_force = true, expect_carries_install_info = false;
3643 DBusMessageIter iter, sub, sub2;
3645 if (streq(verb, "enable")) {
3646 method = "EnableUnitFiles";
3647 expect_carries_install_info = true;
3648 } else if (streq(verb, "disable")) {
3649 method = "DisableUnitFiles";
3651 } else if (streq(verb, "reenable")) {
3652 method = "ReenableUnitFiles";
3653 expect_carries_install_info = true;
3654 } else if (streq(verb, "link"))
3655 method = "LinkUnitFiles";
3656 else if (streq(verb, "preset")) {
3657 method = "PresetUnitFiles";
3658 expect_carries_install_info = true;
3659 } else if (streq(verb, "mask"))
3660 method = "MaskUnitFiles";
3661 else if (streq(verb, "unmask")) {
3662 method = "UnmaskUnitFiles";
3665 assert_not_reached("Unknown verb");
3667 m = dbus_message_new_method_call(
3668 "org.freedesktop.systemd1",
3669 "/org/freedesktop/systemd1",
3670 "org.freedesktop.systemd1.Manager",
3673 log_error("Out of memory");
3678 dbus_message_iter_init_append(m, &iter);
3680 r = bus_append_strv_iter(&iter, args+1);
3682 log_error("Failed to append unit files.");
3687 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &a)) {
3688 log_error("Failed to append runtime boolean.");
3696 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b)) {
3697 log_error("Failed to append force boolean.");
3703 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
3705 log_error("Failed to issue method call: %s", bus_error_message(&error));
3710 if (!dbus_message_iter_init(reply, &iter)) {
3711 log_error("Failed to initialize iterator.");
3715 if (expect_carries_install_info) {
3716 r = bus_iter_get_basic_and_next(&iter, DBUS_TYPE_BOOLEAN, &b, true);
3718 log_error("Failed to parse reply.");
3722 carries_install_info = b;
3725 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
3726 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
3727 log_error("Failed to parse reply.");
3732 dbus_message_iter_recurse(&iter, &sub);
3733 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
3734 const char *type, *path, *source;
3736 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
3737 log_error("Failed to parse reply.");
3742 dbus_message_iter_recurse(&sub, &sub2);
3744 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
3745 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0 ||
3746 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &source, false) < 0) {
3747 log_error("Failed to parse reply.");
3752 if (streq(type, "symlink"))
3753 log_info("ln -s '%s' '%s'", source, path);
3755 log_info("rm '%s'", path);
3757 dbus_message_iter_next(&sub);
3760 /* Try to reload if enabeld */
3762 r = daemon_reload(bus, args);
3765 if (carries_install_info == 0)
3766 log_warning("Warning: unit files do not carry install information. No operation executed.");
3770 dbus_message_unref(m);
3773 dbus_message_unref(reply);
3775 unit_file_changes_free(changes, n_changes);
3777 dbus_error_free(&error);
3781 static int unit_is_enabled(DBusConnection *bus, char **args) {
3784 DBusMessage *m = NULL, *reply = NULL;
3788 dbus_error_init(&error);
3790 r = enable_sysv_units(args);
3796 if (!bus || avoid_bus()) {
3798 STRV_FOREACH(name, args+1) {
3799 UnitFileState state;
3801 state = unit_file_get_state(arg_scope, arg_root, *name);
3807 if (state == UNIT_FILE_ENABLED ||
3808 state == UNIT_FILE_ENABLED_RUNTIME ||
3809 state == UNIT_FILE_STATIC)
3813 puts(unit_file_state_to_string(state));
3817 STRV_FOREACH(name, args+1) {
3820 m = dbus_message_new_method_call(
3821 "org.freedesktop.systemd1",
3822 "/org/freedesktop/systemd1",
3823 "org.freedesktop.systemd1.Manager",
3824 "GetUnitFileState");
3826 log_error("Out of memory");
3831 if (!dbus_message_append_args(m,
3832 DBUS_TYPE_STRING, name,
3833 DBUS_TYPE_INVALID)) {
3834 log_error("Could not append arguments to message.");
3839 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
3841 log_error("Failed to issue method call: %s", bus_error_message(&error));
3846 if (!dbus_message_get_args(reply, &error,
3847 DBUS_TYPE_STRING, &s,
3848 DBUS_TYPE_INVALID)) {
3849 log_error("Failed to parse reply: %s", bus_error_message(&error));
3854 dbus_message_unref(m);
3855 dbus_message_unref(reply);
3858 if (streq(s, "enabled") ||
3859 streq(s, "enabled-runtime") ||
3868 r = enabled ? 0 : 1;
3872 dbus_message_unref(m);
3875 dbus_message_unref(reply);
3877 dbus_error_free(&error);
3881 static int systemctl_help(void) {
3883 pager_open_if_enabled();
3885 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
3886 "Query or send control commands to the systemd manager.\n\n"
3887 " -h --help Show this help\n"
3888 " --version Show package version\n"
3889 " -t --type=TYPE List only units of a particular type\n"
3890 " -p --property=NAME Show only properties by this name\n"
3891 " -a --all Show all units/properties, including dead/empty ones\n"
3892 " --failed Show only failed units\n"
3893 " --full Don't ellipsize unit names on output\n"
3894 " --fail When queueing a new job, fail if conflicting jobs are\n"
3896 " --ignore-dependencies\n"
3897 " When queueing a new job, ignore all its dependencies\n"
3898 " --kill-who=WHO Who to send signal to\n"
3899 " -s --signal=SIGNAL Which signal to send\n"
3900 " -H --host=[USER@]HOST\n"
3901 " Show information for remote host\n"
3902 " -P --privileged Acquire privileges before execution\n"
3903 " -q --quiet Suppress output\n"
3904 " --no-block Do not wait until operation finished\n"
3905 " --no-wall Don't send wall message before halt/power-off/reboot\n"
3906 " --no-reload When enabling/disabling unit files, don't reload daemon\n"
3908 " --no-pager Do not pipe output into a pager\n"
3909 " --no-ask-password\n"
3910 " Do not ask for system passwords\n"
3911 " --order When generating graph for dot, show only order\n"
3912 " --require When generating graph for dot, show only requirement\n"
3913 " --system Connect to system manager\n"
3914 " --user Connect to user service manager\n"
3915 " --global Enable/disable unit files globally\n"
3916 " -f --force When enabling unit files, override existing symlinks\n"
3917 " When shutting down, execute action immediately\n"
3918 " --root=PATH Enable unit files in the specified root directory\n"
3919 " --runtime Enable unit files only temporarily until next reboot\n\n"
3921 " list-units List loaded units\n"
3922 " start [NAME...] Start (activate) one or more units\n"
3923 " stop [NAME...] Stop (deactivate) one or more units\n"
3924 " reload [NAME...] Reload one or more units\n"
3925 " restart [NAME...] Start or restart one or more units\n"
3926 " try-restart [NAME...] Restart one or more units if active\n"
3927 " reload-or-restart [NAME...] Reload one or more units is possible,\n"
3928 " otherwise start or restart\n"
3929 " reload-or-try-restart [NAME...] Reload one or more units is possible,\n"
3930 " otherwise restart if active\n"
3931 " isolate [NAME] Start one unit and stop all others\n"
3932 " kill [NAME...] Send signal to processes of a unit\n"
3933 " is-active [NAME...] Check whether units are active\n"
3934 " status [NAME...|PID...] Show runtime status of one or more units\n"
3935 " show [NAME...|JOB...] Show properties of one or more\n"
3936 " units/jobs or the manager\n"
3937 " reset-failed [NAME...] Reset failed state for all, one, or more\n"
3939 " load [NAME...] Load one or more units\n\n"
3940 "Unit File Commands:\n"
3941 " list-unit-files List installed unit files\n"
3942 " enable [NAME...] Enable one or more unit files\n"
3943 " disable [NAME...] Disable one or more unit files\n"
3944 " reenable [NAME...] Reenable one or more unit files\n"
3945 " preset [NAME...] Enable/disable one or more unit files\n"
3946 " based on preset configuration\n"
3947 " mask [NAME...] Mask one or more units\n"
3948 " unmask [NAME...] Unmask one or more units\n"
3949 " link [PATH...] Link one or more units files into\n"
3950 " the search path\n"
3951 " is-enabled [NAME...] Check whether unit files are enabled\n\n"
3953 " list-jobs List jobs\n"
3954 " cancel [JOB...] Cancel all, one, or more jobs\n\n"
3955 "Status Commands:\n"
3956 " dump Dump server status\n"
3957 " dot Dump dependency graph for dot(1)\n\n"
3958 "Snapshot Commands:\n"
3959 " snapshot [NAME] Create a snapshot\n"
3960 " delete [NAME...] Remove one or more snapshots\n\n"
3961 "Environment Commands:\n"
3962 " show-environment Dump environment\n"
3963 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
3964 " unset-environment [NAME...] Unset one or more environment variables\n\n"
3965 "Manager Lifecycle Commands:\n"
3966 " daemon-reload Reload systemd manager configuration\n"
3967 " daemon-reexec Reexecute systemd manager\n\n"
3968 "System Commands:\n"
3969 " default Enter system default mode\n"
3970 " rescue Enter system rescue mode\n"
3971 " emergency Enter system emergency mode\n"
3972 " halt Shut down and halt the system\n"
3973 " poweroff Shut down and power-off the system\n"
3974 " reboot Shut down and reboot the system\n"
3975 " kexec Shut down and reboot the system with kexec\n"
3976 " exit Ask for user instance termination\n",
3977 program_invocation_short_name);
3982 static int halt_help(void) {
3984 printf("%s [OPTIONS...]\n\n"
3985 "%s the system.\n\n"
3986 " --help Show this help\n"
3987 " --halt Halt the machine\n"
3988 " -p --poweroff Switch off the machine\n"
3989 " --reboot Reboot the machine\n"
3990 " -f --force Force immediate halt/power-off/reboot\n"
3991 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
3992 " -d --no-wtmp Don't write wtmp record\n"
3993 " -n --no-sync Don't sync before halt/power-off/reboot\n"
3994 " --no-wall Don't send wall message before halt/power-off/reboot\n",
3995 program_invocation_short_name,
3996 arg_action == ACTION_REBOOT ? "Reboot" :
3997 arg_action == ACTION_POWEROFF ? "Power off" :
4003 static int shutdown_help(void) {
4005 printf("%s [OPTIONS...] [TIME] [WALL...]\n\n"
4006 "Shut down the system.\n\n"
4007 " --help Show this help\n"
4008 " -H --halt Halt the machine\n"
4009 " -P --poweroff Power-off the machine\n"
4010 " -r --reboot Reboot the machine\n"
4011 " -h Equivalent to --poweroff, overriden by --halt\n"
4012 " -k Don't halt/power-off/reboot, just send warnings\n"
4013 " --no-wall Don't send wall message before halt/power-off/reboot\n"
4014 " -c Cancel a pending shutdown\n",
4015 program_invocation_short_name);
4020 static int telinit_help(void) {
4022 printf("%s [OPTIONS...] {COMMAND}\n\n"
4023 "Send control commands to the init daemon.\n\n"
4024 " --help Show this help\n"
4025 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
4027 " 0 Power-off the machine\n"
4028 " 6 Reboot the machine\n"
4029 " 2, 3, 4, 5 Start runlevelX.target unit\n"
4030 " 1, s, S Enter rescue mode\n"
4031 " q, Q Reload init daemon configuration\n"
4032 " u, U Reexecute init daemon\n",
4033 program_invocation_short_name);
4038 static int runlevel_help(void) {
4040 printf("%s [OPTIONS...]\n\n"
4041 "Prints the previous and current runlevel of the init system.\n\n"
4042 " --help Show this help\n",
4043 program_invocation_short_name);
4048 static int systemctl_parse_argv(int argc, char *argv[]) {
4052 ARG_IGNORE_DEPENDENCIES,
4067 ARG_NO_ASK_PASSWORD,
4072 static const struct option options[] = {
4073 { "help", no_argument, NULL, 'h' },
4074 { "version", no_argument, NULL, ARG_VERSION },
4075 { "type", required_argument, NULL, 't' },
4076 { "property", required_argument, NULL, 'p' },
4077 { "all", no_argument, NULL, 'a' },
4078 { "failed", no_argument, NULL, ARG_FAILED },
4079 { "full", no_argument, NULL, ARG_FULL },
4080 { "fail", no_argument, NULL, ARG_FAIL },
4081 { "ignore-dependencies", no_argument, NULL, ARG_IGNORE_DEPENDENCIES },
4082 { "user", no_argument, NULL, ARG_USER },
4083 { "system", no_argument, NULL, ARG_SYSTEM },
4084 { "global", no_argument, NULL, ARG_GLOBAL },
4085 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
4086 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
4087 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4088 { "quiet", no_argument, NULL, 'q' },
4089 { "order", no_argument, NULL, ARG_ORDER },
4090 { "require", no_argument, NULL, ARG_REQUIRE },
4091 { "root", required_argument, NULL, ARG_ROOT },
4092 { "force", no_argument, NULL, 'f' },
4093 { "no-reload", no_argument, NULL, ARG_NO_RELOAD },
4094 { "kill-mode", required_argument, NULL, ARG_KILL_MODE }, /* undocumented on purpose */
4095 { "kill-who", required_argument, NULL, ARG_KILL_WHO },
4096 { "signal", required_argument, NULL, 's' },
4097 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
4098 { "host", required_argument, NULL, 'H' },
4099 { "privileged",no_argument, NULL, 'P' },
4100 { "runtime", no_argument, NULL, ARG_RUNTIME },
4101 { NULL, 0, NULL, 0 }
4109 /* Only when running as systemctl we ask for passwords */
4110 arg_ask_password = true;
4112 while ((c = getopt_long(argc, argv, "ht:p:aqfs:H:P", options, NULL)) >= 0) {
4121 puts(PACKAGE_STRING);
4123 puts(SYSTEMD_FEATURES);
4133 if (!(l = strv_append(arg_property, optarg)))
4136 strv_free(arg_property);
4139 /* If the user asked for a particular
4140 * property, show it to him, even if it is
4151 arg_job_mode = "fail";
4154 case ARG_IGNORE_DEPENDENCIES:
4155 arg_job_mode = "ignore-dependencies";
4159 arg_scope = UNIT_FILE_USER;
4163 arg_scope = UNIT_FILE_SYSTEM;
4167 arg_scope = UNIT_FILE_GLOBAL;
4171 arg_no_block = true;
4175 arg_no_pager = true;
4183 arg_dot = DOT_ORDER;
4187 arg_dot = DOT_REQUIRE;
4211 arg_no_reload = true;
4215 arg_kill_who = optarg;
4219 arg_kill_mode = optarg;
4223 if ((arg_signal = signal_from_string_try_harder(optarg)) < 0) {
4224 log_error("Failed to parse signal string %s.", optarg);
4229 case ARG_NO_ASK_PASSWORD:
4230 arg_ask_password = false;
4234 arg_transport = TRANSPORT_POLKIT;
4238 arg_transport = TRANSPORT_SSH;
4250 log_error("Unknown option code %c", c);
4255 if (arg_transport != TRANSPORT_NORMAL && arg_scope != UNIT_FILE_SYSTEM) {
4256 log_error("Cannot access user instance remotely.");
4263 static int halt_parse_argv(int argc, char *argv[]) {
4272 static const struct option options[] = {
4273 { "help", no_argument, NULL, ARG_HELP },
4274 { "halt", no_argument, NULL, ARG_HALT },
4275 { "poweroff", no_argument, NULL, 'p' },
4276 { "reboot", no_argument, NULL, ARG_REBOOT },
4277 { "force", no_argument, NULL, 'f' },
4278 { "wtmp-only", no_argument, NULL, 'w' },
4279 { "no-wtmp", no_argument, NULL, 'd' },
4280 { "no-sync", no_argument, NULL, 'n' },
4281 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4282 { NULL, 0, NULL, 0 }
4290 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
4291 if (runlevel == '0' || runlevel == '6')
4292 arg_immediate = true;
4294 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
4302 arg_action = ACTION_HALT;
4306 if (arg_action != ACTION_REBOOT)
4307 arg_action = ACTION_POWEROFF;
4311 arg_action = ACTION_REBOOT;
4315 arg_immediate = true;
4336 /* Compatibility nops */
4343 log_error("Unknown option code %c", c);
4348 if (optind < argc) {
4349 log_error("Too many arguments.");
4356 static int parse_time_spec(const char *t, usec_t *_u) {
4360 if (streq(t, "now"))
4362 else if (!strchr(t, ':')) {
4365 if (safe_atou64(t, &u) < 0)
4368 *_u = now(CLOCK_REALTIME) + USEC_PER_MINUTE * u;
4377 hour = strtol(t, &e, 10);
4378 if (errno != 0 || *e != ':' || hour < 0 || hour > 23)
4381 minute = strtol(e+1, &e, 10);
4382 if (errno != 0 || *e != 0 || minute < 0 || minute > 59)
4385 n = now(CLOCK_REALTIME);
4386 s = (time_t) (n / USEC_PER_SEC);
4389 assert_se(localtime_r(&s, &tm));
4391 tm.tm_hour = (int) hour;
4392 tm.tm_min = (int) minute;
4395 assert_se(s = mktime(&tm));
4397 *_u = (usec_t) s * USEC_PER_SEC;
4400 *_u += USEC_PER_DAY;
4406 static bool kexec_loaded(void) {
4407 bool loaded = false;
4410 if (read_one_line_file("/sys/kernel/kexec_loaded", &s) >= 0) {
4418 static int shutdown_parse_argv(int argc, char *argv[]) {
4425 static const struct option options[] = {
4426 { "help", no_argument, NULL, ARG_HELP },
4427 { "halt", no_argument, NULL, 'H' },
4428 { "poweroff", no_argument, NULL, 'P' },
4429 { "reboot", no_argument, NULL, 'r' },
4430 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4431 { NULL, 0, NULL, 0 }
4439 while ((c = getopt_long(argc, argv, "HPrhkt:afFc", options, NULL)) >= 0) {
4447 arg_action = ACTION_HALT;
4451 arg_action = ACTION_POWEROFF;
4456 arg_action = ACTION_KEXEC;
4458 arg_action = ACTION_REBOOT;
4462 if (arg_action != ACTION_HALT)
4463 arg_action = ACTION_POWEROFF;
4476 /* Compatibility nops */
4480 arg_action = ACTION_CANCEL_SHUTDOWN;
4487 log_error("Unknown option code %c", c);
4492 if (argc > optind) {
4493 if ((r = parse_time_spec(argv[optind], &arg_when)) < 0) {
4494 log_error("Failed to parse time specification: %s", argv[optind]);
4498 arg_when = now(CLOCK_REALTIME) + USEC_PER_MINUTE;
4500 /* We skip the time argument */
4501 if (argc > optind + 1)
4502 arg_wall = argv + optind + 1;
4509 static int telinit_parse_argv(int argc, char *argv[]) {
4516 static const struct option options[] = {
4517 { "help", no_argument, NULL, ARG_HELP },
4518 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4519 { NULL, 0, NULL, 0 }
4522 static const struct {
4526 { '0', ACTION_POWEROFF },
4527 { '6', ACTION_REBOOT },
4528 { '1', ACTION_RESCUE },
4529 { '2', ACTION_RUNLEVEL2 },
4530 { '3', ACTION_RUNLEVEL3 },
4531 { '4', ACTION_RUNLEVEL4 },
4532 { '5', ACTION_RUNLEVEL5 },
4533 { 's', ACTION_RESCUE },
4534 { 'S', ACTION_RESCUE },
4535 { 'q', ACTION_RELOAD },
4536 { 'Q', ACTION_RELOAD },
4537 { 'u', ACTION_REEXEC },
4538 { 'U', ACTION_REEXEC }
4547 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4562 log_error("Unknown option code %c", c);
4567 if (optind >= argc) {
4572 if (optind + 1 < argc) {
4573 log_error("Too many arguments.");
4577 if (strlen(argv[optind]) != 1) {
4578 log_error("Expected single character argument.");
4582 for (i = 0; i < ELEMENTSOF(table); i++)
4583 if (table[i].from == argv[optind][0])
4586 if (i >= ELEMENTSOF(table)) {
4587 log_error("Unknown command %s.", argv[optind]);
4591 arg_action = table[i].to;
4598 static int runlevel_parse_argv(int argc, char *argv[]) {
4604 static const struct option options[] = {
4605 { "help", no_argument, NULL, ARG_HELP },
4606 { NULL, 0, NULL, 0 }
4614 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4625 log_error("Unknown option code %c", c);
4630 if (optind < argc) {
4631 log_error("Too many arguments.");
4638 static int parse_argv(int argc, char *argv[]) {
4642 if (program_invocation_short_name) {
4644 if (strstr(program_invocation_short_name, "halt")) {
4645 arg_action = ACTION_HALT;
4646 return halt_parse_argv(argc, argv);
4647 } else if (strstr(program_invocation_short_name, "poweroff")) {
4648 arg_action = ACTION_POWEROFF;
4649 return halt_parse_argv(argc, argv);
4650 } else if (strstr(program_invocation_short_name, "reboot")) {
4652 arg_action = ACTION_KEXEC;
4654 arg_action = ACTION_REBOOT;
4655 return halt_parse_argv(argc, argv);
4656 } else if (strstr(program_invocation_short_name, "shutdown")) {
4657 arg_action = ACTION_POWEROFF;
4658 return shutdown_parse_argv(argc, argv);
4659 } else if (strstr(program_invocation_short_name, "init")) {
4661 if (sd_booted() > 0) {
4662 arg_action = ACTION_INVALID;
4663 return telinit_parse_argv(argc, argv);
4665 /* Hmm, so some other init system is
4666 * running, we need to forward this
4667 * request to it. For now we simply
4668 * guess that it is Upstart. */
4670 execv("/lib/upstart/telinit", argv);
4672 log_error("Couldn't find an alternative telinit implementation to spawn.");
4676 } else if (strstr(program_invocation_short_name, "runlevel")) {
4677 arg_action = ACTION_RUNLEVEL;
4678 return runlevel_parse_argv(argc, argv);
4682 arg_action = ACTION_SYSTEMCTL;
4683 return systemctl_parse_argv(argc, argv);
4686 static int action_to_runlevel(void) {
4688 static const char table[_ACTION_MAX] = {
4689 [ACTION_HALT] = '0',
4690 [ACTION_POWEROFF] = '0',
4691 [ACTION_REBOOT] = '6',
4692 [ACTION_RUNLEVEL2] = '2',
4693 [ACTION_RUNLEVEL3] = '3',
4694 [ACTION_RUNLEVEL4] = '4',
4695 [ACTION_RUNLEVEL5] = '5',
4696 [ACTION_RESCUE] = '1'
4699 assert(arg_action < _ACTION_MAX);
4701 return table[arg_action];
4704 static int talk_upstart(void) {
4705 DBusMessage *m = NULL, *reply = NULL;
4707 int previous, rl, r;
4709 env1_buf[] = "RUNLEVEL=X",
4710 env2_buf[] = "PREVLEVEL=X";
4711 char *env1 = env1_buf, *env2 = env2_buf;
4712 const char *emit = "runlevel";
4713 dbus_bool_t b_false = FALSE;
4714 DBusMessageIter iter, sub;
4715 DBusConnection *bus;
4717 dbus_error_init(&error);
4719 if (!(rl = action_to_runlevel()))
4722 if (utmp_get_runlevel(&previous, NULL) < 0)
4725 if (!(bus = dbus_connection_open_private("unix:abstract=/com/ubuntu/upstart", &error))) {
4726 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
4731 log_error("Failed to connect to Upstart bus: %s", bus_error_message(&error));
4736 if ((r = bus_check_peercred(bus)) < 0) {
4737 log_error("Failed to verify owner of bus.");
4741 if (!(m = dbus_message_new_method_call(
4742 "com.ubuntu.Upstart",
4743 "/com/ubuntu/Upstart",
4744 "com.ubuntu.Upstart0_6",
4747 log_error("Could not allocate message.");
4752 dbus_message_iter_init_append(m, &iter);
4754 env1_buf[sizeof(env1_buf)-2] = rl;
4755 env2_buf[sizeof(env2_buf)-2] = previous;
4757 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
4758 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
4759 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
4760 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
4761 !dbus_message_iter_close_container(&iter, &sub) ||
4762 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
4763 log_error("Could not append arguments to message.");
4768 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
4770 if (error_is_no_service(&error)) {
4775 log_error("Failed to issue method call: %s", bus_error_message(&error));
4784 dbus_message_unref(m);
4787 dbus_message_unref(reply);
4790 dbus_connection_flush(bus);
4791 dbus_connection_close(bus);
4792 dbus_connection_unref(bus);
4795 dbus_error_free(&error);
4800 static int talk_initctl(void) {
4801 struct init_request request;
4805 if (!(rl = action_to_runlevel()))
4809 request.magic = INIT_MAGIC;
4810 request.sleeptime = 0;
4811 request.cmd = INIT_CMD_RUNLVL;
4812 request.runlevel = rl;
4814 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
4816 if (errno == ENOENT)
4819 log_error("Failed to open "INIT_FIFO": %m");
4824 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
4825 close_nointr_nofail(fd);
4828 log_error("Failed to write to "INIT_FIFO": %m");
4829 return errno ? -errno : -EIO;
4835 static int systemctl_main(DBusConnection *bus, int argc, char *argv[], DBusError *error) {
4837 static const struct {
4845 int (* const dispatch)(DBusConnection *bus, char **args);
4847 { "list-units", LESS, 1, list_units },
4848 { "list-unit-files", EQUAL, 1, list_unit_files },
4849 { "list-jobs", EQUAL, 1, list_jobs },
4850 { "clear-jobs", EQUAL, 1, daemon_reload },
4851 { "load", MORE, 2, load_unit },
4852 { "cancel", MORE, 2, cancel_job },
4853 { "start", MORE, 2, start_unit },
4854 { "stop", MORE, 2, start_unit },
4855 { "condstop", MORE, 2, start_unit }, /* For compatibility with ALTLinux */
4856 { "reload", MORE, 2, start_unit },
4857 { "restart", MORE, 2, start_unit },
4858 { "try-restart", MORE, 2, start_unit },
4859 { "reload-or-restart", MORE, 2, start_unit },
4860 { "reload-or-try-restart", MORE, 2, start_unit },
4861 { "force-reload", MORE, 2, start_unit }, /* For compatibility with SysV */
4862 { "condreload", MORE, 2, start_unit }, /* For compatibility with ALTLinux */
4863 { "condrestart", MORE, 2, start_unit }, /* For compatibility with RH */
4864 { "isolate", EQUAL, 2, start_unit },
4865 { "kill", MORE, 2, kill_unit },
4866 { "is-active", MORE, 2, check_unit },
4867 { "check", MORE, 2, check_unit },
4868 { "show", MORE, 1, show },
4869 { "status", MORE, 2, show },
4870 { "dump", EQUAL, 1, dump },
4871 { "dot", EQUAL, 1, dot },
4872 { "snapshot", LESS, 2, snapshot },
4873 { "delete", MORE, 2, delete_snapshot },
4874 { "daemon-reload", EQUAL, 1, daemon_reload },
4875 { "daemon-reexec", EQUAL, 1, daemon_reload },
4876 { "show-environment", EQUAL, 1, show_enviroment },
4877 { "set-environment", MORE, 2, set_environment },
4878 { "unset-environment", MORE, 2, set_environment },
4879 { "halt", EQUAL, 1, start_special },
4880 { "poweroff", EQUAL, 1, start_special },
4881 { "reboot", EQUAL, 1, start_special },
4882 { "kexec", EQUAL, 1, start_special },
4883 { "default", EQUAL, 1, start_special },
4884 { "rescue", EQUAL, 1, start_special },
4885 { "emergency", EQUAL, 1, start_special },
4886 { "exit", EQUAL, 1, start_special },
4887 { "reset-failed", MORE, 1, reset_failed },
4888 { "enable", MORE, 2, enable_unit },
4889 { "disable", MORE, 2, enable_unit },
4890 { "is-enabled", MORE, 2, unit_is_enabled },
4891 { "reenable", MORE, 2, enable_unit },
4892 { "preset", MORE, 2, enable_unit },
4893 { "mask", MORE, 2, enable_unit },
4894 { "unmask", MORE, 2, enable_unit },
4895 { "link", MORE, 2, enable_unit }
4905 left = argc - optind;
4908 /* Special rule: no arguments means "list-units" */
4911 if (streq(argv[optind], "help")) {
4916 for (i = 0; i < ELEMENTSOF(verbs); i++)
4917 if (streq(argv[optind], verbs[i].verb))
4920 if (i >= ELEMENTSOF(verbs)) {
4921 log_error("Unknown operation %s", argv[optind]);
4926 switch (verbs[i].argc_cmp) {
4929 if (left != verbs[i].argc) {
4930 log_error("Invalid number of arguments.");
4937 if (left < verbs[i].argc) {
4938 log_error("Too few arguments.");
4945 if (left > verbs[i].argc) {
4946 log_error("Too many arguments.");
4953 assert_not_reached("Unknown comparison operator.");
4956 /* Require a bus connection for all operations but
4958 if (!streq(verbs[i].verb, "enable") &&
4959 !streq(verbs[i].verb, "disable") &&
4960 !streq(verbs[i].verb, "is-enable") &&
4961 !streq(verbs[i].verb, "reenable") &&
4962 !streq(verbs[i].verb, "preset") &&
4963 !streq(verbs[i].verb, "mask") &&
4964 !streq(verbs[i].verb, "unmask") &&
4965 !streq(verbs[i].verb, "link")) {
4967 if (running_in_chroot() > 0) {
4968 log_info("Running in chroot, ignoring request.");
4972 if (!bus && !avoid_bus()) {
4973 log_error("Failed to get D-Bus connection: %s", error->message);
4978 return verbs[i].dispatch(bus, argv + optind);
4981 static int send_shutdownd(usec_t t, char mode, bool dry_run, bool warn, const char *message) {
4983 struct msghdr msghdr;
4985 union sockaddr_union sockaddr;
4986 struct shutdownd_command c;
4991 c.dry_run = dry_run;
4995 strncpy(c.wall_message, message, sizeof(c.wall_message));
4997 if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0)
5001 sockaddr.sa.sa_family = AF_UNIX;
5002 sockaddr.un.sun_path[0] = 0;
5003 strncpy(sockaddr.un.sun_path, "/run/systemd/shutdownd", sizeof(sockaddr.un.sun_path));
5006 iovec.iov_base = (char*) &c;
5007 iovec.iov_len = sizeof(c);
5010 msghdr.msg_name = &sockaddr;
5011 msghdr.msg_namelen = offsetof(struct sockaddr_un, sun_path) + sizeof("/run/systemd/shutdownd") - 1;
5013 msghdr.msg_iov = &iovec;
5014 msghdr.msg_iovlen = 1;
5016 if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) < 0) {
5017 close_nointr_nofail(fd);
5021 close_nointr_nofail(fd);
5025 static int reload_with_fallback(DBusConnection *bus) {
5028 /* First, try systemd via D-Bus. */
5029 if (daemon_reload(bus, NULL) > 0)
5033 /* Nothing else worked, so let's try signals */
5034 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
5036 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
5037 log_error("kill() failed: %m");
5044 static int start_with_fallback(DBusConnection *bus) {
5047 /* First, try systemd via D-Bus. */
5048 if (start_unit(bus, NULL) >= 0)
5052 /* Hmm, talking to systemd via D-Bus didn't work. Then
5053 * let's try to talk to Upstart via D-Bus. */
5054 if (talk_upstart() > 0)
5057 /* Nothing else worked, so let's try
5059 if (talk_initctl() > 0)
5062 log_error("Failed to talk to init daemon.");
5066 warn_wall(arg_action);
5070 static int halt_main(DBusConnection *bus) {
5073 if (geteuid() != 0) {
5074 log_error("Must be root.");
5080 char date[FORMAT_TIMESTAMP_MAX];
5082 m = strv_join(arg_wall, " ");
5083 r = send_shutdownd(arg_when,
5084 arg_action == ACTION_HALT ? 'H' :
5085 arg_action == ACTION_POWEROFF ? 'P' :
5093 log_warning("Failed to talk to shutdownd, proceeding with immediate shutdown: %s", strerror(-r));
5095 log_info("Shutdown scheduled for %s, use 'shutdown -c' to cancel.",
5096 format_timestamp(date, sizeof(date), arg_when));
5101 if (!arg_dry && !arg_immediate)
5102 return start_with_fallback(bus);
5105 if (sd_booted() > 0)
5106 log_debug("Not writing utmp record, assuming that systemd-update-utmp is used.");
5107 else if ((r = utmp_put_shutdown(0)) < 0)
5108 log_warning("Failed to write utmp record: %s", strerror(-r));
5117 /* Make sure C-A-D is handled by the kernel from this
5119 reboot(RB_ENABLE_CAD);
5121 switch (arg_action) {
5124 log_info("Halting.");
5125 reboot(RB_HALT_SYSTEM);
5128 case ACTION_POWEROFF:
5129 log_info("Powering off.");
5130 reboot(RB_POWER_OFF);
5134 log_info("Rebooting.");
5135 reboot(RB_AUTOBOOT);
5139 assert_not_reached("Unknown halt action.");
5142 /* We should never reach this. */
5146 static int runlevel_main(void) {
5147 int r, runlevel, previous;
5149 r = utmp_get_runlevel(&runlevel, &previous);
5156 previous <= 0 ? 'N' : previous,
5157 runlevel <= 0 ? 'N' : runlevel);
5162 int main(int argc, char*argv[]) {
5163 int r, retval = EXIT_FAILURE;
5164 DBusConnection *bus = NULL;
5167 dbus_error_init(&error);
5169 log_parse_environment();
5172 if ((r = parse_argv(argc, argv)) < 0)
5175 retval = EXIT_SUCCESS;
5179 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
5180 * let's shortcut this */
5181 if (arg_action == ACTION_RUNLEVEL) {
5182 r = runlevel_main();
5183 retval = r < 0 ? EXIT_FAILURE : r;
5187 if (running_in_chroot() > 0 && arg_action != ACTION_SYSTEMCTL) {
5188 log_info("Running in chroot, ignoring request.");
5194 if (arg_transport == TRANSPORT_NORMAL)
5195 bus_connect(arg_scope == UNIT_FILE_SYSTEM ? DBUS_BUS_SYSTEM : DBUS_BUS_SESSION, &bus, &private_bus, &error);
5196 else if (arg_transport == TRANSPORT_POLKIT) {
5197 bus_connect_system_polkit(&bus, &error);
5198 private_bus = false;
5199 } else if (arg_transport == TRANSPORT_SSH) {
5200 bus_connect_system_ssh(NULL, arg_host, &bus, &error);
5201 private_bus = false;
5203 assert_not_reached("Uh, invalid transport...");
5206 switch (arg_action) {
5208 case ACTION_SYSTEMCTL:
5209 r = systemctl_main(bus, argc, argv, &error);
5213 case ACTION_POWEROFF:
5219 case ACTION_RUNLEVEL2:
5220 case ACTION_RUNLEVEL3:
5221 case ACTION_RUNLEVEL4:
5222 case ACTION_RUNLEVEL5:
5224 case ACTION_EMERGENCY:
5225 case ACTION_DEFAULT:
5226 r = start_with_fallback(bus);
5231 r = reload_with_fallback(bus);
5234 case ACTION_CANCEL_SHUTDOWN:
5235 r = send_shutdownd(0, 0, false, false, NULL);
5238 case ACTION_INVALID:
5239 case ACTION_RUNLEVEL:
5241 assert_not_reached("Unknown action");
5244 retval = r < 0 ? EXIT_FAILURE : r;
5248 dbus_connection_flush(bus);
5249 dbus_connection_close(bus);
5250 dbus_connection_unref(bus);
5253 dbus_error_free(&error);
5257 strv_free(arg_property);