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;
1966 const char *description;
1967 const char *following;
1970 const char *default_control_group;
1972 const char *load_error;
1974 usec_t inactive_exit_timestamp;
1975 usec_t active_enter_timestamp;
1976 usec_t active_exit_timestamp;
1977 usec_t inactive_enter_timestamp;
1979 bool need_daemon_reload;
1984 const char *status_text;
1986 #ifdef HAVE_SYSV_COMPAT
1990 usec_t start_timestamp;
1991 usec_t exit_timestamp;
1993 int exit_code, exit_status;
1995 usec_t condition_timestamp;
1996 bool condition_result;
1999 unsigned n_accepted;
2000 unsigned n_connections;
2004 const char *sysfs_path;
2006 /* Mount, Automount */
2012 LIST_HEAD(ExecStatusInfo, exec);
2015 static void print_status_info(UnitStatusInfo *i) {
2017 const char *on, *off, *ss;
2019 char since1[FORMAT_TIMESTAMP_PRETTY_MAX], *s1;
2020 char since2[FORMAT_TIMESTAMP_MAX], *s2;
2024 /* This shows pretty information about a unit. See
2025 * print_property() for a low-level property printer */
2027 printf("%s", strna(i->id));
2029 if (i->description && !streq_ptr(i->id, i->description))
2030 printf(" - %s", i->description);
2035 printf("\t Follow: unit currently follows state of %s\n", i->following);
2037 if (streq_ptr(i->load_state, "failed") ||
2038 streq_ptr(i->load_state, "banned")) {
2039 on = ansi_highlight(true);
2040 off = ansi_highlight(false);
2045 printf("\t Loaded: %s%s%s (Reason: %s)\n", on, strna(i->load_state), off, i->load_error);
2047 printf("\t Loaded: %s%s%s (%s)\n", on, strna(i->load_state), off, i->path);
2049 printf("\t Loaded: %s%s%s\n", on, strna(i->load_state), off);
2051 ss = streq_ptr(i->active_state, i->sub_state) ? NULL : i->sub_state;
2053 if (streq_ptr(i->active_state, "failed")) {
2054 on = ansi_highlight(true);
2055 off = ansi_highlight(false);
2056 } else if (streq_ptr(i->active_state, "active") || streq_ptr(i->active_state, "reloading")) {
2057 on = ansi_highlight_green(true);
2058 off = ansi_highlight_green(false);
2063 printf("\t Active: %s%s (%s)%s",
2065 strna(i->active_state),
2069 printf("\t Active: %s%s%s",
2071 strna(i->active_state),
2074 timestamp = (streq_ptr(i->active_state, "active") ||
2075 streq_ptr(i->active_state, "reloading")) ? i->active_enter_timestamp :
2076 (streq_ptr(i->active_state, "inactive") ||
2077 streq_ptr(i->active_state, "failed")) ? i->inactive_enter_timestamp :
2078 streq_ptr(i->active_state, "activating") ? i->inactive_exit_timestamp :
2079 i->active_exit_timestamp;
2081 s1 = format_timestamp_pretty(since1, sizeof(since1), timestamp);
2082 s2 = format_timestamp(since2, sizeof(since2), timestamp);
2085 printf(" since %s; %s\n", s2, s1);
2087 printf(" since %s\n", s2);
2091 if (!i->condition_result && i->condition_timestamp > 0) {
2092 s1 = format_timestamp_pretty(since1, sizeof(since1), i->condition_timestamp);
2093 s2 = format_timestamp(since2, sizeof(since2), i->condition_timestamp);
2096 printf("\t start condition failed at %s; %s\n", s2, s1);
2098 printf("\t start condition failed at %s\n", s2);
2102 printf("\t Device: %s\n", i->sysfs_path);
2104 printf("\t Where: %s\n", i->where);
2106 printf("\t What: %s\n", i->what);
2109 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
2111 LIST_FOREACH(exec, p, i->exec) {
2115 /* Only show exited processes here */
2119 t = strv_join(p->argv, " ");
2120 printf("\t Process: %u %s=%s ", p->pid, p->name, strna(t));
2123 #ifdef HAVE_SYSV_COMPAT
2125 good = is_clean_exit_lsb(p->code, p->status);
2128 good = is_clean_exit(p->code, p->status);
2131 on = ansi_highlight(true);
2132 off = ansi_highlight(false);
2136 printf("%s(code=%s, ", on, sigchld_code_to_string(p->code));
2138 if (p->code == CLD_EXITED) {
2141 printf("status=%i", p->status);
2143 #ifdef HAVE_SYSV_COMPAT
2144 if ((c = exit_status_to_string(p->status, i->is_sysv ? EXIT_STATUS_LSB : EXIT_STATUS_SYSTEMD)))
2146 if ((c = exit_status_to_string(p->status, EXIT_STATUS_SYSTEMD)))
2151 printf("signal=%s", signal_to_string(p->status));
2153 printf(")%s\n", off);
2157 if (i->main_pid == p->pid &&
2158 i->start_timestamp == p->start_timestamp &&
2159 i->exit_timestamp == p->start_timestamp)
2160 /* Let's not show this twice */
2163 if (p->pid == i->control_pid)
2167 if (i->main_pid > 0 || i->control_pid > 0) {
2170 if (i->main_pid > 0) {
2171 printf("Main PID: %u", (unsigned) i->main_pid);
2175 get_process_name(i->main_pid, &t);
2180 } else if (i->exit_code > 0) {
2181 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
2183 if (i->exit_code == CLD_EXITED) {
2186 printf("status=%i", i->exit_status);
2188 #ifdef HAVE_SYSV_COMPAT
2189 if ((c = exit_status_to_string(i->exit_status, i->is_sysv ? EXIT_STATUS_LSB : EXIT_STATUS_SYSTEMD)))
2191 if ((c = exit_status_to_string(i->exit_status, EXIT_STATUS_SYSTEMD)))
2196 printf("signal=%s", signal_to_string(i->exit_status));
2201 if (i->main_pid > 0 && i->control_pid > 0)
2204 if (i->control_pid > 0) {
2207 printf(" Control: %u", (unsigned) i->control_pid);
2209 get_process_name(i->control_pid, &t);
2220 printf("\t Status: \"%s\"\n", i->status_text);
2222 if (i->default_control_group) {
2225 printf("\t CGroup: %s\n", i->default_control_group);
2227 if (arg_transport != TRANSPORT_SSH) {
2228 if ((c = columns()) > 18)
2233 show_cgroup_by_path(i->default_control_group, "\t\t ", c);
2237 if (i->need_daemon_reload)
2238 printf("\n%sWarning:%s Unit file changed on disk, 'systemctl %s daemon-reload' recommended.\n",
2239 ansi_highlight(true),
2240 ansi_highlight(false),
2241 arg_scope == UNIT_FILE_SYSTEM ? "--system" : "--user");
2244 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
2250 switch (dbus_message_iter_get_arg_type(iter)) {
2252 case DBUS_TYPE_STRING: {
2255 dbus_message_iter_get_basic(iter, &s);
2258 if (streq(name, "Id"))
2260 else if (streq(name, "LoadState"))
2262 else if (streq(name, "ActiveState"))
2263 i->active_state = s;
2264 else if (streq(name, "SubState"))
2266 else if (streq(name, "Description"))
2268 else if (streq(name, "FragmentPath"))
2270 #ifdef HAVE_SYSV_COMPAT
2271 else if (streq(name, "SysVPath")) {
2276 else if (streq(name, "DefaultControlGroup"))
2277 i->default_control_group = s;
2278 else if (streq(name, "StatusText"))
2280 else if (streq(name, "SysFSPath"))
2282 else if (streq(name, "Where"))
2284 else if (streq(name, "What"))
2286 else if (streq(name, "Following"))
2293 case DBUS_TYPE_BOOLEAN: {
2296 dbus_message_iter_get_basic(iter, &b);
2298 if (streq(name, "Accept"))
2300 else if (streq(name, "NeedDaemonReload"))
2301 i->need_daemon_reload = b;
2302 else if (streq(name, "ConditionResult"))
2303 i->condition_result = b;
2308 case DBUS_TYPE_UINT32: {
2311 dbus_message_iter_get_basic(iter, &u);
2313 if (streq(name, "MainPID")) {
2315 i->main_pid = (pid_t) u;
2318 } else if (streq(name, "ControlPID"))
2319 i->control_pid = (pid_t) u;
2320 else if (streq(name, "ExecMainPID")) {
2322 i->main_pid = (pid_t) u;
2323 } else if (streq(name, "NAccepted"))
2325 else if (streq(name, "NConnections"))
2326 i->n_connections = u;
2331 case DBUS_TYPE_INT32: {
2334 dbus_message_iter_get_basic(iter, &j);
2336 if (streq(name, "ExecMainCode"))
2337 i->exit_code = (int) j;
2338 else if (streq(name, "ExecMainStatus"))
2339 i->exit_status = (int) j;
2344 case DBUS_TYPE_UINT64: {
2347 dbus_message_iter_get_basic(iter, &u);
2349 if (streq(name, "ExecMainStartTimestamp"))
2350 i->start_timestamp = (usec_t) u;
2351 else if (streq(name, "ExecMainExitTimestamp"))
2352 i->exit_timestamp = (usec_t) u;
2353 else if (streq(name, "ActiveEnterTimestamp"))
2354 i->active_enter_timestamp = (usec_t) u;
2355 else if (streq(name, "InactiveEnterTimestamp"))
2356 i->inactive_enter_timestamp = (usec_t) u;
2357 else if (streq(name, "InactiveExitTimestamp"))
2358 i->inactive_exit_timestamp = (usec_t) u;
2359 else if (streq(name, "ActiveExitTimestamp"))
2360 i->active_exit_timestamp = (usec_t) u;
2361 else if (streq(name, "ConditionTimestamp"))
2362 i->condition_timestamp = (usec_t) u;
2367 case DBUS_TYPE_ARRAY: {
2369 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT &&
2370 startswith(name, "Exec")) {
2371 DBusMessageIter sub;
2373 dbus_message_iter_recurse(iter, &sub);
2374 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2375 ExecStatusInfo *info;
2378 if (!(info = new0(ExecStatusInfo, 1)))
2381 if (!(info->name = strdup(name))) {
2386 if ((r = exec_status_info_deserialize(&sub, info)) < 0) {
2391 LIST_PREPEND(ExecStatusInfo, exec, i->exec, info);
2393 dbus_message_iter_next(&sub);
2400 case DBUS_TYPE_STRUCT: {
2402 if (streq(name, "LoadError")) {
2403 DBusMessageIter sub;
2404 const char *n, *message;
2407 dbus_message_iter_recurse(iter, &sub);
2409 r = bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &n, true);
2413 r = bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &message, false);
2417 if (!isempty(message))
2418 i->load_error = message;
2428 static int print_property(const char *name, DBusMessageIter *iter) {
2432 /* This is a low-level property printer, see
2433 * print_status_info() for the nicer output */
2435 if (arg_property && !strv_find(arg_property, name))
2438 switch (dbus_message_iter_get_arg_type(iter)) {
2440 case DBUS_TYPE_STRUCT: {
2441 DBusMessageIter sub;
2442 dbus_message_iter_recurse(iter, &sub);
2444 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
2447 dbus_message_iter_get_basic(&sub, &u);
2450 printf("%s=%u\n", name, (unsigned) u);
2452 printf("%s=\n", name);
2455 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
2458 dbus_message_iter_get_basic(&sub, &s);
2460 if (arg_all || s[0])
2461 printf("%s=%s\n", name, s);
2464 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "LoadError")) {
2465 const char *a = NULL, *b = NULL;
2467 if (bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &a, true) > 0)
2468 bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &b, false);
2470 if (arg_all || !isempty(a) || !isempty(b))
2471 printf("%s=%s \"%s\"\n", name, strempty(a), strempty(b));
2477 case DBUS_TYPE_ARRAY:
2479 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "EnvironmentFiles")) {
2480 DBusMessageIter sub, sub2;
2482 dbus_message_iter_recurse(iter, &sub);
2483 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2487 dbus_message_iter_recurse(&sub, &sub2);
2489 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) >= 0 &&
2490 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, false) >= 0)
2491 printf("EnvironmentFile=%s (ignore_errors=%s)\n", path, yes_no(ignore));
2493 dbus_message_iter_next(&sub);
2498 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
2499 DBusMessageIter sub, sub2;
2501 dbus_message_iter_recurse(iter, &sub);
2502 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2503 const char *type, *path;
2505 dbus_message_iter_recurse(&sub, &sub2);
2507 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
2508 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
2509 printf("%s=%s\n", type, path);
2511 dbus_message_iter_next(&sub);
2516 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
2517 DBusMessageIter sub, sub2;
2519 dbus_message_iter_recurse(iter, &sub);
2520 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2522 uint64_t value, next_elapse;
2524 dbus_message_iter_recurse(&sub, &sub2);
2526 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
2527 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
2528 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
2529 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
2531 printf("%s={ value=%s ; next_elapse=%s }\n",
2533 format_timespan(timespan1, sizeof(timespan1), value),
2534 format_timespan(timespan2, sizeof(timespan2), next_elapse));
2537 dbus_message_iter_next(&sub);
2542 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
2543 DBusMessageIter sub;
2545 dbus_message_iter_recurse(iter, &sub);
2546 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2547 ExecStatusInfo info;
2550 if (exec_status_info_deserialize(&sub, &info) >= 0) {
2551 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
2554 t = strv_join(info.argv, " ");
2556 printf("%s={ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n",
2560 yes_no(info.ignore),
2561 strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)),
2562 strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)),
2563 (unsigned) info. pid,
2564 sigchld_code_to_string(info.code),
2566 info.code == CLD_EXITED ? "" : "/",
2567 strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status)));
2573 strv_free(info.argv);
2575 dbus_message_iter_next(&sub);
2584 if (generic_print_property(name, iter, arg_all) > 0)
2588 printf("%s=[unprintable]\n", name);
2593 static int show_one(const char *verb, DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
2594 DBusMessage *m = NULL, *reply = NULL;
2595 const char *interface = "";
2598 DBusMessageIter iter, sub, sub2, sub3;
2599 UnitStatusInfo info;
2607 dbus_error_init(&error);
2609 if (!(m = dbus_message_new_method_call(
2610 "org.freedesktop.systemd1",
2612 "org.freedesktop.DBus.Properties",
2614 log_error("Could not allocate message.");
2619 if (!dbus_message_append_args(m,
2620 DBUS_TYPE_STRING, &interface,
2621 DBUS_TYPE_INVALID)) {
2622 log_error("Could not append arguments to message.");
2627 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2628 log_error("Failed to issue method call: %s", bus_error_message(&error));
2633 if (!dbus_message_iter_init(reply, &iter) ||
2634 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
2635 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
2636 log_error("Failed to parse reply.");
2641 dbus_message_iter_recurse(&iter, &sub);
2648 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2651 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
2652 log_error("Failed to parse reply.");
2657 dbus_message_iter_recurse(&sub, &sub2);
2659 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
2660 log_error("Failed to parse reply.");
2665 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
2666 log_error("Failed to parse reply.");
2671 dbus_message_iter_recurse(&sub2, &sub3);
2673 if (show_properties)
2674 r = print_property(name, &sub3);
2676 r = status_property(name, &sub3, &info);
2679 log_error("Failed to parse reply.");
2684 dbus_message_iter_next(&sub);
2689 if (!show_properties)
2690 print_status_info(&info);
2692 if (!streq_ptr(info.active_state, "active") &&
2693 !streq_ptr(info.active_state, "reloading") &&
2694 streq(verb, "status"))
2695 /* According to LSB: "program not running" */
2698 while ((p = info.exec)) {
2699 LIST_REMOVE(ExecStatusInfo, exec, info.exec, p);
2700 exec_status_info_free(p);
2705 dbus_message_unref(m);
2708 dbus_message_unref(reply);
2710 dbus_error_free(&error);
2715 static int show(DBusConnection *bus, char **args) {
2716 DBusMessage *m = NULL, *reply = NULL;
2719 bool show_properties, new_line = false;
2725 dbus_error_init(&error);
2727 show_properties = !streq(args[0], "status");
2729 if (show_properties)
2730 pager_open_if_enabled();
2732 if (show_properties && strv_length(args) <= 1) {
2733 /* If not argument is specified inspect the manager
2736 ret = show_one(args[0], bus, "/org/freedesktop/systemd1", show_properties, &new_line);
2740 STRV_FOREACH(name, args+1) {
2741 const char *path = NULL;
2744 if (safe_atou32(*name, &id) < 0) {
2746 /* Interpret as unit name */
2748 if (!(m = dbus_message_new_method_call(
2749 "org.freedesktop.systemd1",
2750 "/org/freedesktop/systemd1",
2751 "org.freedesktop.systemd1.Manager",
2753 log_error("Could not allocate message.");
2758 if (!dbus_message_append_args(m,
2759 DBUS_TYPE_STRING, name,
2760 DBUS_TYPE_INVALID)) {
2761 log_error("Could not append arguments to message.");
2766 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2768 if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
2769 log_error("Failed to issue method call: %s", bus_error_message(&error));
2774 dbus_error_free(&error);
2776 dbus_message_unref(m);
2777 if (!(m = dbus_message_new_method_call(
2778 "org.freedesktop.systemd1",
2779 "/org/freedesktop/systemd1",
2780 "org.freedesktop.systemd1.Manager",
2782 log_error("Could not allocate message.");
2787 if (!dbus_message_append_args(m,
2788 DBUS_TYPE_STRING, name,
2789 DBUS_TYPE_INVALID)) {
2790 log_error("Could not append arguments to message.");
2795 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2796 log_error("Failed to issue method call: %s", bus_error_message(&error));
2798 if (dbus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT))
2799 ret = 4; /* According to LSB: "program or service status is unknown" */
2806 } else if (show_properties) {
2808 /* Interpret as job id */
2810 if (!(m = dbus_message_new_method_call(
2811 "org.freedesktop.systemd1",
2812 "/org/freedesktop/systemd1",
2813 "org.freedesktop.systemd1.Manager",
2815 log_error("Could not allocate message.");
2820 if (!dbus_message_append_args(m,
2821 DBUS_TYPE_UINT32, &id,
2822 DBUS_TYPE_INVALID)) {
2823 log_error("Could not append arguments to message.");
2828 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2829 log_error("Failed to issue method call: %s", bus_error_message(&error));
2835 /* Interpret as PID */
2837 if (!(m = dbus_message_new_method_call(
2838 "org.freedesktop.systemd1",
2839 "/org/freedesktop/systemd1",
2840 "org.freedesktop.systemd1.Manager",
2842 log_error("Could not allocate message.");
2847 if (!dbus_message_append_args(m,
2848 DBUS_TYPE_UINT32, &id,
2849 DBUS_TYPE_INVALID)) {
2850 log_error("Could not append arguments to message.");
2855 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2856 log_error("Failed to issue method call: %s", bus_error_message(&error));
2862 if (!dbus_message_get_args(reply, &error,
2863 DBUS_TYPE_OBJECT_PATH, &path,
2864 DBUS_TYPE_INVALID)) {
2865 log_error("Failed to parse reply: %s", bus_error_message(&error));
2870 if ((r = show_one(args[0], bus, path, show_properties, &new_line)) != 0)
2873 dbus_message_unref(m);
2874 dbus_message_unref(reply);
2880 dbus_message_unref(m);
2883 dbus_message_unref(reply);
2885 dbus_error_free(&error);
2890 static int dump(DBusConnection *bus, char **args) {
2891 DBusMessage *m = NULL, *reply = NULL;
2896 dbus_error_init(&error);
2898 pager_open_if_enabled();
2900 if (!(m = dbus_message_new_method_call(
2901 "org.freedesktop.systemd1",
2902 "/org/freedesktop/systemd1",
2903 "org.freedesktop.systemd1.Manager",
2905 log_error("Could not allocate message.");
2909 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2910 log_error("Failed to issue method call: %s", bus_error_message(&error));
2915 if (!dbus_message_get_args(reply, &error,
2916 DBUS_TYPE_STRING, &text,
2917 DBUS_TYPE_INVALID)) {
2918 log_error("Failed to parse reply: %s", bus_error_message(&error));
2923 fputs(text, stdout);
2929 dbus_message_unref(m);
2932 dbus_message_unref(reply);
2934 dbus_error_free(&error);
2939 static int snapshot(DBusConnection *bus, char **args) {
2940 DBusMessage *m = NULL, *reply = NULL;
2943 const char *name = "", *path, *id;
2944 dbus_bool_t cleanup = FALSE;
2945 DBusMessageIter iter, sub;
2947 *interface = "org.freedesktop.systemd1.Unit",
2950 dbus_error_init(&error);
2952 if (!(m = dbus_message_new_method_call(
2953 "org.freedesktop.systemd1",
2954 "/org/freedesktop/systemd1",
2955 "org.freedesktop.systemd1.Manager",
2956 "CreateSnapshot"))) {
2957 log_error("Could not allocate message.");
2961 if (strv_length(args) > 1)
2964 if (!dbus_message_append_args(m,
2965 DBUS_TYPE_STRING, &name,
2966 DBUS_TYPE_BOOLEAN, &cleanup,
2967 DBUS_TYPE_INVALID)) {
2968 log_error("Could not append arguments to message.");
2973 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2974 log_error("Failed to issue method call: %s", bus_error_message(&error));
2979 if (!dbus_message_get_args(reply, &error,
2980 DBUS_TYPE_OBJECT_PATH, &path,
2981 DBUS_TYPE_INVALID)) {
2982 log_error("Failed to parse reply: %s", bus_error_message(&error));
2987 dbus_message_unref(m);
2988 if (!(m = dbus_message_new_method_call(
2989 "org.freedesktop.systemd1",
2991 "org.freedesktop.DBus.Properties",
2993 log_error("Could not allocate message.");
2997 if (!dbus_message_append_args(m,
2998 DBUS_TYPE_STRING, &interface,
2999 DBUS_TYPE_STRING, &property,
3000 DBUS_TYPE_INVALID)) {
3001 log_error("Could not append arguments to message.");
3006 dbus_message_unref(reply);
3007 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3008 log_error("Failed to issue method call: %s", bus_error_message(&error));
3013 if (!dbus_message_iter_init(reply, &iter) ||
3014 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
3015 log_error("Failed to parse reply.");
3020 dbus_message_iter_recurse(&iter, &sub);
3022 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
3023 log_error("Failed to parse reply.");
3028 dbus_message_iter_get_basic(&sub, &id);
3036 dbus_message_unref(m);
3039 dbus_message_unref(reply);
3041 dbus_error_free(&error);
3046 static int delete_snapshot(DBusConnection *bus, char **args) {
3047 DBusMessage *m = NULL, *reply = NULL;
3055 dbus_error_init(&error);
3057 STRV_FOREACH(name, args+1) {
3058 const char *path = NULL;
3060 if (!(m = dbus_message_new_method_call(
3061 "org.freedesktop.systemd1",
3062 "/org/freedesktop/systemd1",
3063 "org.freedesktop.systemd1.Manager",
3065 log_error("Could not allocate message.");
3070 if (!dbus_message_append_args(m,
3071 DBUS_TYPE_STRING, name,
3072 DBUS_TYPE_INVALID)) {
3073 log_error("Could not append arguments to message.");
3078 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3079 log_error("Failed to issue method call: %s", bus_error_message(&error));
3084 if (!dbus_message_get_args(reply, &error,
3085 DBUS_TYPE_OBJECT_PATH, &path,
3086 DBUS_TYPE_INVALID)) {
3087 log_error("Failed to parse reply: %s", bus_error_message(&error));
3092 dbus_message_unref(m);
3093 if (!(m = dbus_message_new_method_call(
3094 "org.freedesktop.systemd1",
3096 "org.freedesktop.systemd1.Snapshot",
3098 log_error("Could not allocate message.");
3103 dbus_message_unref(reply);
3104 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3105 log_error("Failed to issue method call: %s", bus_error_message(&error));
3110 dbus_message_unref(m);
3111 dbus_message_unref(reply);
3119 dbus_message_unref(m);
3122 dbus_message_unref(reply);
3124 dbus_error_free(&error);
3129 static int daemon_reload(DBusConnection *bus, char **args) {
3130 DBusMessage *m = NULL, *reply = NULL;
3135 dbus_error_init(&error);
3137 if (arg_action == ACTION_RELOAD)
3139 else if (arg_action == ACTION_REEXEC)
3140 method = "Reexecute";
3142 assert(arg_action == ACTION_SYSTEMCTL);
3145 streq(args[0], "clear-jobs") ||
3146 streq(args[0], "cancel") ? "ClearJobs" :
3147 streq(args[0], "daemon-reexec") ? "Reexecute" :
3148 streq(args[0], "reset-failed") ? "ResetFailed" :
3149 streq(args[0], "halt") ? "Halt" :
3150 streq(args[0], "poweroff") ? "PowerOff" :
3151 streq(args[0], "reboot") ? "Reboot" :
3152 streq(args[0], "kexec") ? "KExec" :
3153 streq(args[0], "exit") ? "Exit" :
3154 /* "daemon-reload" */ "Reload";
3157 if (!(m = dbus_message_new_method_call(
3158 "org.freedesktop.systemd1",
3159 "/org/freedesktop/systemd1",
3160 "org.freedesktop.systemd1.Manager",
3162 log_error("Could not allocate message.");
3166 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3168 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
3169 /* There's always a fallback possible for
3170 * legacy actions. */
3175 if (streq(method, "Reexecute") && dbus_error_has_name(&error, DBUS_ERROR_NO_REPLY)) {
3176 /* On reexecution, we expect a disconnect, not
3182 log_error("Failed to issue method call: %s", bus_error_message(&error));
3191 dbus_message_unref(m);
3194 dbus_message_unref(reply);
3196 dbus_error_free(&error);
3201 static int reset_failed(DBusConnection *bus, char **args) {
3202 DBusMessage *m = NULL, *reply = NULL;
3208 dbus_error_init(&error);
3210 if (strv_length(args) <= 1)
3211 return daemon_reload(bus, args);
3213 STRV_FOREACH(name, args+1) {
3215 if (!(m = dbus_message_new_method_call(
3216 "org.freedesktop.systemd1",
3217 "/org/freedesktop/systemd1",
3218 "org.freedesktop.systemd1.Manager",
3219 "ResetFailedUnit"))) {
3220 log_error("Could not allocate message.");
3225 if (!dbus_message_append_args(m,
3226 DBUS_TYPE_STRING, name,
3227 DBUS_TYPE_INVALID)) {
3228 log_error("Could not append arguments to message.");
3233 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3234 log_error("Failed to issue method call: %s", bus_error_message(&error));
3239 dbus_message_unref(m);
3240 dbus_message_unref(reply);
3248 dbus_message_unref(m);
3251 dbus_message_unref(reply);
3253 dbus_error_free(&error);
3258 static int show_enviroment(DBusConnection *bus, char **args) {
3259 DBusMessage *m = NULL, *reply = NULL;
3261 DBusMessageIter iter, sub, sub2;
3264 *interface = "org.freedesktop.systemd1.Manager",
3265 *property = "Environment";
3267 dbus_error_init(&error);
3269 pager_open_if_enabled();
3271 if (!(m = dbus_message_new_method_call(
3272 "org.freedesktop.systemd1",
3273 "/org/freedesktop/systemd1",
3274 "org.freedesktop.DBus.Properties",
3276 log_error("Could not allocate message.");
3280 if (!dbus_message_append_args(m,
3281 DBUS_TYPE_STRING, &interface,
3282 DBUS_TYPE_STRING, &property,
3283 DBUS_TYPE_INVALID)) {
3284 log_error("Could not append arguments to message.");
3289 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3290 log_error("Failed to issue method call: %s", bus_error_message(&error));
3295 if (!dbus_message_iter_init(reply, &iter) ||
3296 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
3297 log_error("Failed to parse reply.");
3302 dbus_message_iter_recurse(&iter, &sub);
3304 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
3305 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
3306 log_error("Failed to parse reply.");
3311 dbus_message_iter_recurse(&sub, &sub2);
3313 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
3316 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
3317 log_error("Failed to parse reply.");
3322 dbus_message_iter_get_basic(&sub2, &text);
3323 printf("%s\n", text);
3325 dbus_message_iter_next(&sub2);
3332 dbus_message_unref(m);
3335 dbus_message_unref(reply);
3337 dbus_error_free(&error);
3342 static int set_environment(DBusConnection *bus, char **args) {
3343 DBusMessage *m = NULL, *reply = NULL;
3347 DBusMessageIter iter, sub;
3350 dbus_error_init(&error);
3352 method = streq(args[0], "set-environment")
3354 : "UnsetEnvironment";
3356 if (!(m = dbus_message_new_method_call(
3357 "org.freedesktop.systemd1",
3358 "/org/freedesktop/systemd1",
3359 "org.freedesktop.systemd1.Manager",
3362 log_error("Could not allocate message.");
3366 dbus_message_iter_init_append(m, &iter);
3368 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
3369 log_error("Could not append arguments to message.");
3374 STRV_FOREACH(name, args+1)
3375 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, name)) {
3376 log_error("Could not append arguments to message.");
3381 if (!dbus_message_iter_close_container(&iter, &sub)) {
3382 log_error("Could not append arguments to message.");
3387 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3388 log_error("Failed to issue method call: %s", bus_error_message(&error));
3397 dbus_message_unref(m);
3400 dbus_message_unref(reply);
3402 dbus_error_free(&error);
3407 static int enable_sysv_units(char **args) {
3410 #if defined (HAVE_SYSV_COMPAT) && (defined(TARGET_FEDORA) || defined(TARGET_MANDRIVA) || defined(TARGET_SUSE) || defined(TARGET_MEEGO) || defined(TARGET_ALTLINUX))
3411 const char *verb = args[0];
3412 unsigned f = 1, t = 1;
3415 if (arg_scope != UNIT_FILE_SYSTEM)
3418 if (!streq(verb, "enable") &&
3419 !streq(verb, "disable") &&
3420 !streq(verb, "is-enabled"))
3423 /* Processes all SysV units, and reshuffles the array so that
3424 * afterwards only the native units remain */
3427 r = lookup_paths_init(&paths, MANAGER_SYSTEM, false);
3433 for (f = 1; args[f]; f++) {
3436 bool found_native = false, found_sysv;
3438 const char *argv[6] = { "/sbin/chkconfig", NULL, NULL, NULL, NULL };
3439 char **k, *l, *q = NULL;
3446 if (!endswith(name, ".service"))
3449 if (path_is_absolute(name))
3452 STRV_FOREACH(k, paths.unit_path) {
3455 if (!isempty(arg_root))
3456 asprintf(&p, "%s/%s/%s", arg_root, *k, name);
3458 asprintf(&p, "%s/%s", *k, name);
3461 log_error("No memory");
3466 found_native = access(p, F_OK) >= 0;
3477 if (!isempty(arg_root))
3478 asprintf(&p, "%s/" SYSTEM_SYSVINIT_PATH "/%s", arg_root, name);
3480 asprintf(&p, SYSTEM_SYSVINIT_PATH "/%s", name);
3482 log_error("No memory");
3487 p[strlen(p) - sizeof(".service") + 1] = 0;
3488 found_sysv = access(p, F_OK) >= 0;
3495 /* Mark this entry, so that we don't try enabling it as native unit */
3496 args[f] = (char*) "";
3498 log_info("%s is not a native service, redirecting to /sbin/chkconfig.", name);
3500 if (!isempty(arg_root))
3501 argv[c++] = q = strappend("--root=", arg_root);
3503 argv[c++] = file_name_from_path(p);
3505 streq(verb, "enable") ? "on" :
3506 streq(verb, "disable") ? "off" : "--level=5";
3509 l = strv_join((char**)argv, " ");
3511 log_error("No memory.");
3518 log_info("Executing %s", l);
3523 log_error("Failed to fork: %m");
3528 } else if (pid == 0) {
3531 execv(argv[0], (char**) argv);
3532 _exit(EXIT_FAILURE);
3538 j = wait_for_terminate(pid, &status);
3540 log_error("Failed to wait for child: %s", strerror(-r));
3545 if (status.si_code == CLD_EXITED) {
3546 if (streq(verb, "is-enabled")) {
3547 if (status.si_status == 0) {
3556 } else if (status.si_status != 0) {
3567 lookup_paths_free(&paths);
3569 /* Drop all SysV units */
3570 for (f = 1, t = 1; args[f]; f++) {
3572 if (isempty(args[f]))
3575 args[t++] = args[f];
3584 static int enable_unit(DBusConnection *bus, char **args) {
3585 const char *verb = args[0];
3586 UnitFileChange *changes = NULL;
3587 unsigned n_changes = 0, i;
3588 int carries_install_info = -1;
3589 DBusMessage *m = NULL, *reply = NULL;
3593 dbus_error_init(&error);
3595 r = enable_sysv_units(args);
3599 if (!bus || avoid_bus()) {
3600 if (streq(verb, "enable")) {
3601 r = unit_file_enable(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3602 carries_install_info = r;
3603 } else if (streq(verb, "disable"))
3604 r = unit_file_disable(arg_scope, arg_runtime, arg_root, args+1, &changes, &n_changes);
3605 else if (streq(verb, "reenable")) {
3606 r = unit_file_reenable(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3607 carries_install_info = r;
3608 } else if (streq(verb, "link"))
3609 r = unit_file_link(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3610 else if (streq(verb, "preset")) {
3611 r = unit_file_preset(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3612 carries_install_info = r;
3613 } else if (streq(verb, "mask"))
3614 r = unit_file_mask(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3615 else if (streq(verb, "unmask"))
3616 r = unit_file_unmask(arg_scope, arg_runtime, arg_root, args+1, &changes, &n_changes);
3618 assert_not_reached("Unknown verb");
3621 log_error("Operation failed: %s", strerror(-r));
3625 for (i = 0; i < n_changes; i++) {
3626 if (changes[i].type == UNIT_FILE_SYMLINK)
3627 log_info("ln -s '%s' '%s'", changes[i].source, changes[i].path);
3629 log_info("rm '%s'", changes[i].path);
3634 bool send_force = true, expect_carries_install_info = false;
3636 DBusMessageIter iter, sub, sub2;
3638 if (streq(verb, "enable")) {
3639 method = "EnableUnitFiles";
3640 expect_carries_install_info = true;
3641 } else if (streq(verb, "disable")) {
3642 method = "DisableUnitFiles";
3644 } else if (streq(verb, "reenable")) {
3645 method = "ReenableUnitFiles";
3646 expect_carries_install_info = true;
3647 } else if (streq(verb, "link"))
3648 method = "LinkUnitFiles";
3649 else if (streq(verb, "preset")) {
3650 method = "PresetUnitFiles";
3651 expect_carries_install_info = true;
3652 } else if (streq(verb, "mask"))
3653 method = "MaskUnitFiles";
3654 else if (streq(verb, "unmask")) {
3655 method = "UnmaskUnitFiles";
3658 assert_not_reached("Unknown verb");
3660 m = dbus_message_new_method_call(
3661 "org.freedesktop.systemd1",
3662 "/org/freedesktop/systemd1",
3663 "org.freedesktop.systemd1.Manager",
3666 log_error("Out of memory");
3671 dbus_message_iter_init_append(m, &iter);
3673 r = bus_append_strv_iter(&iter, args+1);
3675 log_error("Failed to append unit files.");
3680 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &a)) {
3681 log_error("Failed to append runtime boolean.");
3689 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b)) {
3690 log_error("Failed to append force boolean.");
3696 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
3698 log_error("Failed to issue method call: %s", bus_error_message(&error));
3703 if (!dbus_message_iter_init(reply, &iter)) {
3704 log_error("Failed to initialize iterator.");
3708 if (expect_carries_install_info) {
3709 r = bus_iter_get_basic_and_next(&iter, DBUS_TYPE_BOOLEAN, &b, true);
3711 log_error("Failed to parse reply.");
3715 carries_install_info = b;
3718 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
3719 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
3720 log_error("Failed to parse reply.");
3725 dbus_message_iter_recurse(&iter, &sub);
3726 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
3727 const char *type, *path, *source;
3729 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
3730 log_error("Failed to parse reply.");
3735 dbus_message_iter_recurse(&sub, &sub2);
3737 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
3738 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0 ||
3739 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &source, false) < 0) {
3740 log_error("Failed to parse reply.");
3745 if (streq(type, "symlink"))
3746 log_info("ln -s '%s' '%s'", source, path);
3748 log_info("rm '%s'", path);
3750 dbus_message_iter_next(&sub);
3753 /* Try to reload if enabeld */
3755 r = daemon_reload(bus, args);
3758 if (carries_install_info == 0)
3759 log_warning("Warning: unit files do not carry install information. No operation executed.");
3763 dbus_message_unref(m);
3766 dbus_message_unref(reply);
3768 unit_file_changes_free(changes, n_changes);
3770 dbus_error_free(&error);
3774 static int unit_is_enabled(DBusConnection *bus, char **args) {
3777 DBusMessage *m = NULL, *reply = NULL;
3781 dbus_error_init(&error);
3783 r = enable_sysv_units(args);
3789 if (!bus || avoid_bus()) {
3791 STRV_FOREACH(name, args+1) {
3792 UnitFileState state;
3794 state = unit_file_get_state(arg_scope, arg_root, *name);
3800 if (state == UNIT_FILE_ENABLED ||
3801 state == UNIT_FILE_ENABLED_RUNTIME ||
3802 state == UNIT_FILE_STATIC)
3806 puts(unit_file_state_to_string(state));
3810 STRV_FOREACH(name, args+1) {
3813 m = dbus_message_new_method_call(
3814 "org.freedesktop.systemd1",
3815 "/org/freedesktop/systemd1",
3816 "org.freedesktop.systemd1.Manager",
3817 "GetUnitFileState");
3819 log_error("Out of memory");
3824 if (!dbus_message_append_args(m,
3825 DBUS_TYPE_STRING, name,
3826 DBUS_TYPE_INVALID)) {
3827 log_error("Could not append arguments to message.");
3832 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
3834 log_error("Failed to issue method call: %s", bus_error_message(&error));
3839 if (!dbus_message_get_args(reply, &error,
3840 DBUS_TYPE_STRING, &s,
3841 DBUS_TYPE_INVALID)) {
3842 log_error("Failed to parse reply: %s", bus_error_message(&error));
3847 dbus_message_unref(m);
3848 dbus_message_unref(reply);
3851 if (streq(s, "enabled") ||
3852 streq(s, "enabled-runtime") ||
3861 r = enabled ? 0 : 1;
3865 dbus_message_unref(m);
3868 dbus_message_unref(reply);
3870 dbus_error_free(&error);
3874 static int systemctl_help(void) {
3876 pager_open_if_enabled();
3878 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
3879 "Query or send control commands to the systemd manager.\n\n"
3880 " -h --help Show this help\n"
3881 " --version Show package version\n"
3882 " -t --type=TYPE List only units of a particular type\n"
3883 " -p --property=NAME Show only properties by this name\n"
3884 " -a --all Show all units/properties, including dead/empty ones\n"
3885 " --failed Show only failed units\n"
3886 " --full Don't ellipsize unit names on output\n"
3887 " --fail When queueing a new job, fail if conflicting jobs are\n"
3889 " --ignore-dependencies\n"
3890 " When queueing a new job, ignore all its dependencies\n"
3891 " --kill-who=WHO Who to send signal to\n"
3892 " -s --signal=SIGNAL Which signal to send\n"
3893 " -H --host=[USER@]HOST\n"
3894 " Show information for remote host\n"
3895 " -P --privileged Acquire privileges before execution\n"
3896 " -q --quiet Suppress output\n"
3897 " --no-block Do not wait until operation finished\n"
3898 " --no-wall Don't send wall message before halt/power-off/reboot\n"
3899 " --no-reload When enabling/disabling unit files, don't reload daemon\n"
3901 " --no-pager Do not pipe output into a pager\n"
3902 " --no-ask-password\n"
3903 " Do not ask for system passwords\n"
3904 " --order When generating graph for dot, show only order\n"
3905 " --require When generating graph for dot, show only requirement\n"
3906 " --system Connect to system manager\n"
3907 " --user Connect to user service manager\n"
3908 " --global Enable/disable unit files globally\n"
3909 " -f --force When enabling unit files, override existing symlinks\n"
3910 " When shutting down, execute action immediately\n"
3911 " --root=PATH Enable unit files in the specified root directory\n"
3912 " --runtime Enable unit files only temporarily until next reboot\n\n"
3914 " list-units List loaded units\n"
3915 " start [NAME...] Start (activate) one or more units\n"
3916 " stop [NAME...] Stop (deactivate) one or more units\n"
3917 " reload [NAME...] Reload one or more units\n"
3918 " restart [NAME...] Start or restart one or more units\n"
3919 " try-restart [NAME...] Restart one or more units if active\n"
3920 " reload-or-restart [NAME...] Reload one or more units is possible,\n"
3921 " otherwise start or restart\n"
3922 " reload-or-try-restart [NAME...] Reload one or more units is possible,\n"
3923 " otherwise restart if active\n"
3924 " isolate [NAME] Start one unit and stop all others\n"
3925 " kill [NAME...] Send signal to processes of a unit\n"
3926 " is-active [NAME...] Check whether units are active\n"
3927 " status [NAME...|PID...] Show runtime status of one or more units\n"
3928 " show [NAME...|JOB...] Show properties of one or more\n"
3929 " units/jobs or the manager\n"
3930 " reset-failed [NAME...] Reset failed state for all, one, or more\n"
3932 " load [NAME...] Load one or more units\n\n"
3933 "Unit File Commands:\n"
3934 " list-unit-files List installed unit files\n"
3935 " enable [NAME...] Enable one or more unit files\n"
3936 " disable [NAME...] Disable one or more unit files\n"
3937 " reenable [NAME...] Reenable one or more unit files\n"
3938 " preset [NAME...] Enable/disable one or more unit files\n"
3939 " based on preset configuration\n"
3940 " mask [NAME...] Mask one or more units\n"
3941 " unmask [NAME...] Unmask one or more units\n"
3942 " link [PATH...] Link one or more units files into\n"
3943 " the search path\n"
3944 " is-enabled [NAME...] Check whether unit files are enabled\n\n"
3946 " list-jobs List jobs\n"
3947 " cancel [JOB...] Cancel all, one, or more jobs\n\n"
3948 "Status Commands:\n"
3949 " dump Dump server status\n"
3950 " dot Dump dependency graph for dot(1)\n\n"
3951 "Snapshot Commands:\n"
3952 " snapshot [NAME] Create a snapshot\n"
3953 " delete [NAME...] Remove one or more snapshots\n\n"
3954 "Environment Commands:\n"
3955 " show-environment Dump environment\n"
3956 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
3957 " unset-environment [NAME...] Unset one or more environment variables\n\n"
3958 "Manager Lifecycle Commands:\n"
3959 " daemon-reload Reload systemd manager configuration\n"
3960 " daemon-reexec Reexecute systemd manager\n\n"
3961 "System Commands:\n"
3962 " default Enter system default mode\n"
3963 " rescue Enter system rescue mode\n"
3964 " emergency Enter system emergency mode\n"
3965 " halt Shut down and halt the system\n"
3966 " poweroff Shut down and power-off the system\n"
3967 " reboot Shut down and reboot the system\n"
3968 " kexec Shut down and reboot the system with kexec\n"
3969 " exit Ask for user instance termination\n",
3970 program_invocation_short_name);
3975 static int halt_help(void) {
3977 printf("%s [OPTIONS...]\n\n"
3978 "%s the system.\n\n"
3979 " --help Show this help\n"
3980 " --halt Halt the machine\n"
3981 " -p --poweroff Switch off the machine\n"
3982 " --reboot Reboot the machine\n"
3983 " -f --force Force immediate halt/power-off/reboot\n"
3984 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
3985 " -d --no-wtmp Don't write wtmp record\n"
3986 " -n --no-sync Don't sync before halt/power-off/reboot\n"
3987 " --no-wall Don't send wall message before halt/power-off/reboot\n",
3988 program_invocation_short_name,
3989 arg_action == ACTION_REBOOT ? "Reboot" :
3990 arg_action == ACTION_POWEROFF ? "Power off" :
3996 static int shutdown_help(void) {
3998 printf("%s [OPTIONS...] [TIME] [WALL...]\n\n"
3999 "Shut down the system.\n\n"
4000 " --help Show this help\n"
4001 " -H --halt Halt the machine\n"
4002 " -P --poweroff Power-off the machine\n"
4003 " -r --reboot Reboot the machine\n"
4004 " -h Equivalent to --poweroff, overriden by --halt\n"
4005 " -k Don't halt/power-off/reboot, just send warnings\n"
4006 " --no-wall Don't send wall message before halt/power-off/reboot\n"
4007 " -c Cancel a pending shutdown\n",
4008 program_invocation_short_name);
4013 static int telinit_help(void) {
4015 printf("%s [OPTIONS...] {COMMAND}\n\n"
4016 "Send control commands to the init daemon.\n\n"
4017 " --help Show this help\n"
4018 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
4020 " 0 Power-off the machine\n"
4021 " 6 Reboot the machine\n"
4022 " 2, 3, 4, 5 Start runlevelX.target unit\n"
4023 " 1, s, S Enter rescue mode\n"
4024 " q, Q Reload init daemon configuration\n"
4025 " u, U Reexecute init daemon\n",
4026 program_invocation_short_name);
4031 static int runlevel_help(void) {
4033 printf("%s [OPTIONS...]\n\n"
4034 "Prints the previous and current runlevel of the init system.\n\n"
4035 " --help Show this help\n",
4036 program_invocation_short_name);
4041 static int systemctl_parse_argv(int argc, char *argv[]) {
4045 ARG_IGNORE_DEPENDENCIES,
4060 ARG_NO_ASK_PASSWORD,
4065 static const struct option options[] = {
4066 { "help", no_argument, NULL, 'h' },
4067 { "version", no_argument, NULL, ARG_VERSION },
4068 { "type", required_argument, NULL, 't' },
4069 { "property", required_argument, NULL, 'p' },
4070 { "all", no_argument, NULL, 'a' },
4071 { "failed", no_argument, NULL, ARG_FAILED },
4072 { "full", no_argument, NULL, ARG_FULL },
4073 { "fail", no_argument, NULL, ARG_FAIL },
4074 { "ignore-dependencies", no_argument, NULL, ARG_IGNORE_DEPENDENCIES },
4075 { "user", no_argument, NULL, ARG_USER },
4076 { "system", no_argument, NULL, ARG_SYSTEM },
4077 { "global", no_argument, NULL, ARG_GLOBAL },
4078 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
4079 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
4080 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4081 { "quiet", no_argument, NULL, 'q' },
4082 { "order", no_argument, NULL, ARG_ORDER },
4083 { "require", no_argument, NULL, ARG_REQUIRE },
4084 { "root", required_argument, NULL, ARG_ROOT },
4085 { "force", no_argument, NULL, 'f' },
4086 { "no-reload", no_argument, NULL, ARG_NO_RELOAD },
4087 { "kill-mode", required_argument, NULL, ARG_KILL_MODE }, /* undocumented on purpose */
4088 { "kill-who", required_argument, NULL, ARG_KILL_WHO },
4089 { "signal", required_argument, NULL, 's' },
4090 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
4091 { "host", required_argument, NULL, 'H' },
4092 { "privileged",no_argument, NULL, 'P' },
4093 { "runtime", no_argument, NULL, ARG_RUNTIME },
4094 { NULL, 0, NULL, 0 }
4102 /* Only when running as systemctl we ask for passwords */
4103 arg_ask_password = true;
4105 while ((c = getopt_long(argc, argv, "ht:p:aqfs:H:P", options, NULL)) >= 0) {
4114 puts(PACKAGE_STRING);
4116 puts(SYSTEMD_FEATURES);
4126 if (!(l = strv_append(arg_property, optarg)))
4129 strv_free(arg_property);
4132 /* If the user asked for a particular
4133 * property, show it to him, even if it is
4144 arg_job_mode = "fail";
4147 case ARG_IGNORE_DEPENDENCIES:
4148 arg_job_mode = "ignore-dependencies";
4152 arg_scope = UNIT_FILE_USER;
4156 arg_scope = UNIT_FILE_SYSTEM;
4160 arg_scope = UNIT_FILE_GLOBAL;
4164 arg_no_block = true;
4168 arg_no_pager = true;
4176 arg_dot = DOT_ORDER;
4180 arg_dot = DOT_REQUIRE;
4204 arg_no_reload = true;
4208 arg_kill_who = optarg;
4212 arg_kill_mode = optarg;
4216 if ((arg_signal = signal_from_string_try_harder(optarg)) < 0) {
4217 log_error("Failed to parse signal string %s.", optarg);
4222 case ARG_NO_ASK_PASSWORD:
4223 arg_ask_password = false;
4227 arg_transport = TRANSPORT_POLKIT;
4231 arg_transport = TRANSPORT_SSH;
4243 log_error("Unknown option code %c", c);
4248 if (arg_transport != TRANSPORT_NORMAL && arg_scope != UNIT_FILE_SYSTEM) {
4249 log_error("Cannot access user instance remotely.");
4256 static int halt_parse_argv(int argc, char *argv[]) {
4265 static const struct option options[] = {
4266 { "help", no_argument, NULL, ARG_HELP },
4267 { "halt", no_argument, NULL, ARG_HALT },
4268 { "poweroff", no_argument, NULL, 'p' },
4269 { "reboot", no_argument, NULL, ARG_REBOOT },
4270 { "force", no_argument, NULL, 'f' },
4271 { "wtmp-only", no_argument, NULL, 'w' },
4272 { "no-wtmp", no_argument, NULL, 'd' },
4273 { "no-sync", no_argument, NULL, 'n' },
4274 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4275 { NULL, 0, NULL, 0 }
4283 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
4284 if (runlevel == '0' || runlevel == '6')
4285 arg_immediate = true;
4287 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
4295 arg_action = ACTION_HALT;
4299 if (arg_action != ACTION_REBOOT)
4300 arg_action = ACTION_POWEROFF;
4304 arg_action = ACTION_REBOOT;
4308 arg_immediate = true;
4329 /* Compatibility nops */
4336 log_error("Unknown option code %c", c);
4341 if (optind < argc) {
4342 log_error("Too many arguments.");
4349 static int parse_time_spec(const char *t, usec_t *_u) {
4353 if (streq(t, "now"))
4355 else if (!strchr(t, ':')) {
4358 if (safe_atou64(t, &u) < 0)
4361 *_u = now(CLOCK_REALTIME) + USEC_PER_MINUTE * u;
4370 hour = strtol(t, &e, 10);
4371 if (errno != 0 || *e != ':' || hour < 0 || hour > 23)
4374 minute = strtol(e+1, &e, 10);
4375 if (errno != 0 || *e != 0 || minute < 0 || minute > 59)
4378 n = now(CLOCK_REALTIME);
4379 s = (time_t) (n / USEC_PER_SEC);
4382 assert_se(localtime_r(&s, &tm));
4384 tm.tm_hour = (int) hour;
4385 tm.tm_min = (int) minute;
4388 assert_se(s = mktime(&tm));
4390 *_u = (usec_t) s * USEC_PER_SEC;
4393 *_u += USEC_PER_DAY;
4399 static bool kexec_loaded(void) {
4400 bool loaded = false;
4403 if (read_one_line_file("/sys/kernel/kexec_loaded", &s) >= 0) {
4411 static int shutdown_parse_argv(int argc, char *argv[]) {
4418 static const struct option options[] = {
4419 { "help", no_argument, NULL, ARG_HELP },
4420 { "halt", no_argument, NULL, 'H' },
4421 { "poweroff", no_argument, NULL, 'P' },
4422 { "reboot", no_argument, NULL, 'r' },
4423 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4424 { NULL, 0, NULL, 0 }
4432 while ((c = getopt_long(argc, argv, "HPrhkt:afFc", options, NULL)) >= 0) {
4440 arg_action = ACTION_HALT;
4444 arg_action = ACTION_POWEROFF;
4449 arg_action = ACTION_KEXEC;
4451 arg_action = ACTION_REBOOT;
4455 if (arg_action != ACTION_HALT)
4456 arg_action = ACTION_POWEROFF;
4469 /* Compatibility nops */
4473 arg_action = ACTION_CANCEL_SHUTDOWN;
4480 log_error("Unknown option code %c", c);
4485 if (argc > optind) {
4486 if ((r = parse_time_spec(argv[optind], &arg_when)) < 0) {
4487 log_error("Failed to parse time specification: %s", argv[optind]);
4491 arg_when = now(CLOCK_REALTIME) + USEC_PER_MINUTE;
4493 /* We skip the time argument */
4494 if (argc > optind + 1)
4495 arg_wall = argv + optind + 1;
4502 static int telinit_parse_argv(int argc, char *argv[]) {
4509 static const struct option options[] = {
4510 { "help", no_argument, NULL, ARG_HELP },
4511 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4512 { NULL, 0, NULL, 0 }
4515 static const struct {
4519 { '0', ACTION_POWEROFF },
4520 { '6', ACTION_REBOOT },
4521 { '1', ACTION_RESCUE },
4522 { '2', ACTION_RUNLEVEL2 },
4523 { '3', ACTION_RUNLEVEL3 },
4524 { '4', ACTION_RUNLEVEL4 },
4525 { '5', ACTION_RUNLEVEL5 },
4526 { 's', ACTION_RESCUE },
4527 { 'S', ACTION_RESCUE },
4528 { 'q', ACTION_RELOAD },
4529 { 'Q', ACTION_RELOAD },
4530 { 'u', ACTION_REEXEC },
4531 { 'U', ACTION_REEXEC }
4540 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4555 log_error("Unknown option code %c", c);
4560 if (optind >= argc) {
4565 if (optind + 1 < argc) {
4566 log_error("Too many arguments.");
4570 if (strlen(argv[optind]) != 1) {
4571 log_error("Expected single character argument.");
4575 for (i = 0; i < ELEMENTSOF(table); i++)
4576 if (table[i].from == argv[optind][0])
4579 if (i >= ELEMENTSOF(table)) {
4580 log_error("Unknown command %s.", argv[optind]);
4584 arg_action = table[i].to;
4591 static int runlevel_parse_argv(int argc, char *argv[]) {
4597 static const struct option options[] = {
4598 { "help", no_argument, NULL, ARG_HELP },
4599 { NULL, 0, NULL, 0 }
4607 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4618 log_error("Unknown option code %c", c);
4623 if (optind < argc) {
4624 log_error("Too many arguments.");
4631 static int parse_argv(int argc, char *argv[]) {
4635 if (program_invocation_short_name) {
4637 if (strstr(program_invocation_short_name, "halt")) {
4638 arg_action = ACTION_HALT;
4639 return halt_parse_argv(argc, argv);
4640 } else if (strstr(program_invocation_short_name, "poweroff")) {
4641 arg_action = ACTION_POWEROFF;
4642 return halt_parse_argv(argc, argv);
4643 } else if (strstr(program_invocation_short_name, "reboot")) {
4645 arg_action = ACTION_KEXEC;
4647 arg_action = ACTION_REBOOT;
4648 return halt_parse_argv(argc, argv);
4649 } else if (strstr(program_invocation_short_name, "shutdown")) {
4650 arg_action = ACTION_POWEROFF;
4651 return shutdown_parse_argv(argc, argv);
4652 } else if (strstr(program_invocation_short_name, "init")) {
4654 if (sd_booted() > 0) {
4655 arg_action = ACTION_INVALID;
4656 return telinit_parse_argv(argc, argv);
4658 /* Hmm, so some other init system is
4659 * running, we need to forward this
4660 * request to it. For now we simply
4661 * guess that it is Upstart. */
4663 execv("/lib/upstart/telinit", argv);
4665 log_error("Couldn't find an alternative telinit implementation to spawn.");
4669 } else if (strstr(program_invocation_short_name, "runlevel")) {
4670 arg_action = ACTION_RUNLEVEL;
4671 return runlevel_parse_argv(argc, argv);
4675 arg_action = ACTION_SYSTEMCTL;
4676 return systemctl_parse_argv(argc, argv);
4679 static int action_to_runlevel(void) {
4681 static const char table[_ACTION_MAX] = {
4682 [ACTION_HALT] = '0',
4683 [ACTION_POWEROFF] = '0',
4684 [ACTION_REBOOT] = '6',
4685 [ACTION_RUNLEVEL2] = '2',
4686 [ACTION_RUNLEVEL3] = '3',
4687 [ACTION_RUNLEVEL4] = '4',
4688 [ACTION_RUNLEVEL5] = '5',
4689 [ACTION_RESCUE] = '1'
4692 assert(arg_action < _ACTION_MAX);
4694 return table[arg_action];
4697 static int talk_upstart(void) {
4698 DBusMessage *m = NULL, *reply = NULL;
4700 int previous, rl, r;
4702 env1_buf[] = "RUNLEVEL=X",
4703 env2_buf[] = "PREVLEVEL=X";
4704 char *env1 = env1_buf, *env2 = env2_buf;
4705 const char *emit = "runlevel";
4706 dbus_bool_t b_false = FALSE;
4707 DBusMessageIter iter, sub;
4708 DBusConnection *bus;
4710 dbus_error_init(&error);
4712 if (!(rl = action_to_runlevel()))
4715 if (utmp_get_runlevel(&previous, NULL) < 0)
4718 if (!(bus = dbus_connection_open_private("unix:abstract=/com/ubuntu/upstart", &error))) {
4719 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
4724 log_error("Failed to connect to Upstart bus: %s", bus_error_message(&error));
4729 if ((r = bus_check_peercred(bus)) < 0) {
4730 log_error("Failed to verify owner of bus.");
4734 if (!(m = dbus_message_new_method_call(
4735 "com.ubuntu.Upstart",
4736 "/com/ubuntu/Upstart",
4737 "com.ubuntu.Upstart0_6",
4740 log_error("Could not allocate message.");
4745 dbus_message_iter_init_append(m, &iter);
4747 env1_buf[sizeof(env1_buf)-2] = rl;
4748 env2_buf[sizeof(env2_buf)-2] = previous;
4750 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
4751 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
4752 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
4753 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
4754 !dbus_message_iter_close_container(&iter, &sub) ||
4755 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
4756 log_error("Could not append arguments to message.");
4761 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
4763 if (error_is_no_service(&error)) {
4768 log_error("Failed to issue method call: %s", bus_error_message(&error));
4777 dbus_message_unref(m);
4780 dbus_message_unref(reply);
4783 dbus_connection_flush(bus);
4784 dbus_connection_close(bus);
4785 dbus_connection_unref(bus);
4788 dbus_error_free(&error);
4793 static int talk_initctl(void) {
4794 struct init_request request;
4798 if (!(rl = action_to_runlevel()))
4802 request.magic = INIT_MAGIC;
4803 request.sleeptime = 0;
4804 request.cmd = INIT_CMD_RUNLVL;
4805 request.runlevel = rl;
4807 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
4809 if (errno == ENOENT)
4812 log_error("Failed to open "INIT_FIFO": %m");
4817 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
4818 close_nointr_nofail(fd);
4821 log_error("Failed to write to "INIT_FIFO": %m");
4822 return errno ? -errno : -EIO;
4828 static int systemctl_main(DBusConnection *bus, int argc, char *argv[], DBusError *error) {
4830 static const struct {
4838 int (* const dispatch)(DBusConnection *bus, char **args);
4840 { "list-units", LESS, 1, list_units },
4841 { "list-unit-files", EQUAL, 1, list_unit_files },
4842 { "list-jobs", EQUAL, 1, list_jobs },
4843 { "clear-jobs", EQUAL, 1, daemon_reload },
4844 { "load", MORE, 2, load_unit },
4845 { "cancel", MORE, 2, cancel_job },
4846 { "start", MORE, 2, start_unit },
4847 { "stop", MORE, 2, start_unit },
4848 { "condstop", MORE, 2, start_unit }, /* For compatibility with ALTLinux */
4849 { "reload", MORE, 2, start_unit },
4850 { "restart", MORE, 2, start_unit },
4851 { "try-restart", MORE, 2, start_unit },
4852 { "reload-or-restart", MORE, 2, start_unit },
4853 { "reload-or-try-restart", MORE, 2, start_unit },
4854 { "force-reload", MORE, 2, start_unit }, /* For compatibility with SysV */
4855 { "condreload", MORE, 2, start_unit }, /* For compatibility with ALTLinux */
4856 { "condrestart", MORE, 2, start_unit }, /* For compatibility with RH */
4857 { "isolate", EQUAL, 2, start_unit },
4858 { "kill", MORE, 2, kill_unit },
4859 { "is-active", MORE, 2, check_unit },
4860 { "check", MORE, 2, check_unit },
4861 { "show", MORE, 1, show },
4862 { "status", MORE, 2, show },
4863 { "dump", EQUAL, 1, dump },
4864 { "dot", EQUAL, 1, dot },
4865 { "snapshot", LESS, 2, snapshot },
4866 { "delete", MORE, 2, delete_snapshot },
4867 { "daemon-reload", EQUAL, 1, daemon_reload },
4868 { "daemon-reexec", EQUAL, 1, daemon_reload },
4869 { "show-environment", EQUAL, 1, show_enviroment },
4870 { "set-environment", MORE, 2, set_environment },
4871 { "unset-environment", MORE, 2, set_environment },
4872 { "halt", EQUAL, 1, start_special },
4873 { "poweroff", EQUAL, 1, start_special },
4874 { "reboot", EQUAL, 1, start_special },
4875 { "kexec", EQUAL, 1, start_special },
4876 { "default", EQUAL, 1, start_special },
4877 { "rescue", EQUAL, 1, start_special },
4878 { "emergency", EQUAL, 1, start_special },
4879 { "exit", EQUAL, 1, start_special },
4880 { "reset-failed", MORE, 1, reset_failed },
4881 { "enable", MORE, 2, enable_unit },
4882 { "disable", MORE, 2, enable_unit },
4883 { "is-enabled", MORE, 2, unit_is_enabled },
4884 { "reenable", MORE, 2, enable_unit },
4885 { "preset", MORE, 2, enable_unit },
4886 { "mask", MORE, 2, enable_unit },
4887 { "unmask", MORE, 2, enable_unit },
4888 { "link", MORE, 2, enable_unit }
4898 left = argc - optind;
4901 /* Special rule: no arguments means "list-units" */
4904 if (streq(argv[optind], "help")) {
4909 for (i = 0; i < ELEMENTSOF(verbs); i++)
4910 if (streq(argv[optind], verbs[i].verb))
4913 if (i >= ELEMENTSOF(verbs)) {
4914 log_error("Unknown operation %s", argv[optind]);
4919 switch (verbs[i].argc_cmp) {
4922 if (left != verbs[i].argc) {
4923 log_error("Invalid number of arguments.");
4930 if (left < verbs[i].argc) {
4931 log_error("Too few arguments.");
4938 if (left > verbs[i].argc) {
4939 log_error("Too many arguments.");
4946 assert_not_reached("Unknown comparison operator.");
4949 /* Require a bus connection for all operations but
4951 if (!streq(verbs[i].verb, "enable") &&
4952 !streq(verbs[i].verb, "disable") &&
4953 !streq(verbs[i].verb, "is-enable") &&
4954 !streq(verbs[i].verb, "reenable") &&
4955 !streq(verbs[i].verb, "preset") &&
4956 !streq(verbs[i].verb, "mask") &&
4957 !streq(verbs[i].verb, "unmask") &&
4958 !streq(verbs[i].verb, "link")) {
4960 if (running_in_chroot() > 0) {
4961 log_info("Running in chroot, ignoring request.");
4965 if (!bus && !avoid_bus()) {
4966 log_error("Failed to get D-Bus connection: %s", error->message);
4971 return verbs[i].dispatch(bus, argv + optind);
4974 static int send_shutdownd(usec_t t, char mode, bool dry_run, bool warn, const char *message) {
4976 struct msghdr msghdr;
4978 union sockaddr_union sockaddr;
4979 struct shutdownd_command c;
4984 c.dry_run = dry_run;
4988 strncpy(c.wall_message, message, sizeof(c.wall_message));
4990 if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0)
4994 sockaddr.sa.sa_family = AF_UNIX;
4995 sockaddr.un.sun_path[0] = 0;
4996 strncpy(sockaddr.un.sun_path, "/run/systemd/shutdownd", sizeof(sockaddr.un.sun_path));
4999 iovec.iov_base = (char*) &c;
5000 iovec.iov_len = sizeof(c);
5003 msghdr.msg_name = &sockaddr;
5004 msghdr.msg_namelen = offsetof(struct sockaddr_un, sun_path) + sizeof("/run/systemd/shutdownd") - 1;
5006 msghdr.msg_iov = &iovec;
5007 msghdr.msg_iovlen = 1;
5009 if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) < 0) {
5010 close_nointr_nofail(fd);
5014 close_nointr_nofail(fd);
5018 static int reload_with_fallback(DBusConnection *bus) {
5021 /* First, try systemd via D-Bus. */
5022 if (daemon_reload(bus, NULL) > 0)
5026 /* Nothing else worked, so let's try signals */
5027 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
5029 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
5030 log_error("kill() failed: %m");
5037 static int start_with_fallback(DBusConnection *bus) {
5040 /* First, try systemd via D-Bus. */
5041 if (start_unit(bus, NULL) >= 0)
5045 /* Hmm, talking to systemd via D-Bus didn't work. Then
5046 * let's try to talk to Upstart via D-Bus. */
5047 if (talk_upstart() > 0)
5050 /* Nothing else worked, so let's try
5052 if (talk_initctl() > 0)
5055 log_error("Failed to talk to init daemon.");
5059 warn_wall(arg_action);
5063 static int halt_main(DBusConnection *bus) {
5066 if (geteuid() != 0) {
5067 log_error("Must be root.");
5073 char date[FORMAT_TIMESTAMP_MAX];
5075 m = strv_join(arg_wall, " ");
5076 r = send_shutdownd(arg_when,
5077 arg_action == ACTION_HALT ? 'H' :
5078 arg_action == ACTION_POWEROFF ? 'P' :
5086 log_warning("Failed to talk to shutdownd, proceeding with immediate shutdown: %s", strerror(-r));
5088 log_info("Shutdown scheduled for %s, use 'shutdown -c' to cancel.",
5089 format_timestamp(date, sizeof(date), arg_when));
5094 if (!arg_dry && !arg_immediate)
5095 return start_with_fallback(bus);
5098 if (sd_booted() > 0)
5099 log_debug("Not writing utmp record, assuming that systemd-update-utmp is used.");
5100 else if ((r = utmp_put_shutdown(0)) < 0)
5101 log_warning("Failed to write utmp record: %s", strerror(-r));
5110 /* Make sure C-A-D is handled by the kernel from this
5112 reboot(RB_ENABLE_CAD);
5114 switch (arg_action) {
5117 log_info("Halting.");
5118 reboot(RB_HALT_SYSTEM);
5121 case ACTION_POWEROFF:
5122 log_info("Powering off.");
5123 reboot(RB_POWER_OFF);
5127 log_info("Rebooting.");
5128 reboot(RB_AUTOBOOT);
5132 assert_not_reached("Unknown halt action.");
5135 /* We should never reach this. */
5139 static int runlevel_main(void) {
5140 int r, runlevel, previous;
5142 r = utmp_get_runlevel(&runlevel, &previous);
5149 previous <= 0 ? 'N' : previous,
5150 runlevel <= 0 ? 'N' : runlevel);
5155 int main(int argc, char*argv[]) {
5156 int r, retval = EXIT_FAILURE;
5157 DBusConnection *bus = NULL;
5160 dbus_error_init(&error);
5162 log_parse_environment();
5165 if ((r = parse_argv(argc, argv)) < 0)
5168 retval = EXIT_SUCCESS;
5172 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
5173 * let's shortcut this */
5174 if (arg_action == ACTION_RUNLEVEL) {
5175 r = runlevel_main();
5176 retval = r < 0 ? EXIT_FAILURE : r;
5180 if (running_in_chroot() > 0 && arg_action != ACTION_SYSTEMCTL) {
5181 log_info("Running in chroot, ignoring request.");
5187 if (arg_transport == TRANSPORT_NORMAL)
5188 bus_connect(arg_scope == UNIT_FILE_SYSTEM ? DBUS_BUS_SYSTEM : DBUS_BUS_SESSION, &bus, &private_bus, &error);
5189 else if (arg_transport == TRANSPORT_POLKIT) {
5190 bus_connect_system_polkit(&bus, &error);
5191 private_bus = false;
5192 } else if (arg_transport == TRANSPORT_SSH) {
5193 bus_connect_system_ssh(NULL, arg_host, &bus, &error);
5194 private_bus = false;
5196 assert_not_reached("Uh, invalid transport...");
5199 switch (arg_action) {
5201 case ACTION_SYSTEMCTL:
5202 r = systemctl_main(bus, argc, argv, &error);
5206 case ACTION_POWEROFF:
5212 case ACTION_RUNLEVEL2:
5213 case ACTION_RUNLEVEL3:
5214 case ACTION_RUNLEVEL4:
5215 case ACTION_RUNLEVEL5:
5217 case ACTION_EMERGENCY:
5218 case ACTION_DEFAULT:
5219 r = start_with_fallback(bus);
5224 r = reload_with_fallback(bus);
5227 case ACTION_CANCEL_SHUTDOWN:
5228 r = send_shutdownd(0, 0, false, false, NULL);
5231 case ACTION_INVALID:
5232 case ACTION_RUNLEVEL:
5234 assert_not_reached("Unknown action");
5237 retval = r < 0 ? EXIT_FAILURE : r;
5241 dbus_connection_flush(bus);
5242 dbus_connection_close(bus);
5243 dbus_connection_unref(bus);
5246 dbus_error_free(&error);
5250 strv_free(arg_property);