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 usec_t inactive_exit_timestamp;
1973 usec_t active_enter_timestamp;
1974 usec_t active_exit_timestamp;
1975 usec_t inactive_enter_timestamp;
1977 bool need_daemon_reload;
1982 const char *status_text;
1984 #ifdef HAVE_SYSV_COMPAT
1988 usec_t start_timestamp;
1989 usec_t exit_timestamp;
1991 int exit_code, exit_status;
1993 usec_t condition_timestamp;
1994 bool condition_result;
1997 unsigned n_accepted;
1998 unsigned n_connections;
2002 const char *sysfs_path;
2004 /* Mount, Automount */
2010 LIST_HEAD(ExecStatusInfo, exec);
2013 static void print_status_info(UnitStatusInfo *i) {
2015 const char *on, *off, *ss;
2017 char since1[FORMAT_TIMESTAMP_PRETTY_MAX], *s1;
2018 char since2[FORMAT_TIMESTAMP_MAX], *s2;
2022 /* This shows pretty information about a unit. See
2023 * print_property() for a low-level property printer */
2025 printf("%s", strna(i->id));
2027 if (i->description && !streq_ptr(i->id, i->description))
2028 printf(" - %s", i->description);
2033 printf("\t Follow: unit currently follows state of %s\n", i->following);
2035 if (streq_ptr(i->load_state, "failed") ||
2036 streq_ptr(i->load_state, "banned")) {
2037 on = ansi_highlight(true);
2038 off = ansi_highlight(false);
2043 printf("\t Loaded: %s%s%s (%s)\n", on, strna(i->load_state), off, i->path);
2045 printf("\t Loaded: %s%s%s\n", on, strna(i->load_state), off);
2047 ss = streq_ptr(i->active_state, i->sub_state) ? NULL : i->sub_state;
2049 if (streq_ptr(i->active_state, "failed")) {
2050 on = ansi_highlight(true);
2051 off = ansi_highlight(false);
2052 } else if (streq_ptr(i->active_state, "active") || streq_ptr(i->active_state, "reloading")) {
2053 on = ansi_highlight_green(true);
2054 off = ansi_highlight_green(false);
2059 printf("\t Active: %s%s (%s)%s",
2061 strna(i->active_state),
2065 printf("\t Active: %s%s%s",
2067 strna(i->active_state),
2070 timestamp = (streq_ptr(i->active_state, "active") ||
2071 streq_ptr(i->active_state, "reloading")) ? i->active_enter_timestamp :
2072 (streq_ptr(i->active_state, "inactive") ||
2073 streq_ptr(i->active_state, "failed")) ? i->inactive_enter_timestamp :
2074 streq_ptr(i->active_state, "activating") ? i->inactive_exit_timestamp :
2075 i->active_exit_timestamp;
2077 s1 = format_timestamp_pretty(since1, sizeof(since1), timestamp);
2078 s2 = format_timestamp(since2, sizeof(since2), timestamp);
2081 printf(" since %s; %s\n", s2, s1);
2083 printf(" since %s\n", s2);
2087 if (!i->condition_result && i->condition_timestamp > 0) {
2088 s1 = format_timestamp_pretty(since1, sizeof(since1), i->condition_timestamp);
2089 s2 = format_timestamp(since2, sizeof(since2), i->condition_timestamp);
2092 printf("\t start condition failed at %s; %s\n", s2, s1);
2094 printf("\t start condition failed at %s\n", s2);
2098 printf("\t Device: %s\n", i->sysfs_path);
2100 printf("\t Where: %s\n", i->where);
2102 printf("\t What: %s\n", i->what);
2105 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
2107 LIST_FOREACH(exec, p, i->exec) {
2111 /* Only show exited processes here */
2115 t = strv_join(p->argv, " ");
2116 printf("\t Process: %u %s=%s ", p->pid, p->name, strna(t));
2119 #ifdef HAVE_SYSV_COMPAT
2121 good = is_clean_exit_lsb(p->code, p->status);
2124 good = is_clean_exit(p->code, p->status);
2127 on = ansi_highlight(true);
2128 off = ansi_highlight(false);
2132 printf("%s(code=%s, ", on, sigchld_code_to_string(p->code));
2134 if (p->code == CLD_EXITED) {
2137 printf("status=%i", p->status);
2139 #ifdef HAVE_SYSV_COMPAT
2140 if ((c = exit_status_to_string(p->status, i->is_sysv ? EXIT_STATUS_LSB : EXIT_STATUS_SYSTEMD)))
2142 if ((c = exit_status_to_string(p->status, EXIT_STATUS_SYSTEMD)))
2147 printf("signal=%s", signal_to_string(p->status));
2149 printf(")%s\n", off);
2153 if (i->main_pid == p->pid &&
2154 i->start_timestamp == p->start_timestamp &&
2155 i->exit_timestamp == p->start_timestamp)
2156 /* Let's not show this twice */
2159 if (p->pid == i->control_pid)
2163 if (i->main_pid > 0 || i->control_pid > 0) {
2166 if (i->main_pid > 0) {
2167 printf("Main PID: %u", (unsigned) i->main_pid);
2171 get_process_name(i->main_pid, &t);
2176 } else if (i->exit_code > 0) {
2177 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
2179 if (i->exit_code == CLD_EXITED) {
2182 printf("status=%i", i->exit_status);
2184 #ifdef HAVE_SYSV_COMPAT
2185 if ((c = exit_status_to_string(i->exit_status, i->is_sysv ? EXIT_STATUS_LSB : EXIT_STATUS_SYSTEMD)))
2187 if ((c = exit_status_to_string(i->exit_status, EXIT_STATUS_SYSTEMD)))
2192 printf("signal=%s", signal_to_string(i->exit_status));
2197 if (i->main_pid > 0 && i->control_pid > 0)
2200 if (i->control_pid > 0) {
2203 printf(" Control: %u", (unsigned) i->control_pid);
2205 get_process_name(i->control_pid, &t);
2216 printf("\t Status: \"%s\"\n", i->status_text);
2218 if (i->default_control_group) {
2221 printf("\t CGroup: %s\n", i->default_control_group);
2223 if (arg_transport != TRANSPORT_SSH) {
2224 if ((c = columns()) > 18)
2229 show_cgroup_by_path(i->default_control_group, "\t\t ", c);
2233 if (i->need_daemon_reload)
2234 printf("\n%sWarning:%s Unit file changed on disk, 'systemctl %s daemon-reload' recommended.\n",
2235 ansi_highlight(true),
2236 ansi_highlight(false),
2237 arg_scope == UNIT_FILE_SYSTEM ? "--system" : "--user");
2240 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
2246 switch (dbus_message_iter_get_arg_type(iter)) {
2248 case DBUS_TYPE_STRING: {
2251 dbus_message_iter_get_basic(iter, &s);
2254 if (streq(name, "Id"))
2256 else if (streq(name, "LoadState"))
2258 else if (streq(name, "ActiveState"))
2259 i->active_state = s;
2260 else if (streq(name, "SubState"))
2262 else if (streq(name, "Description"))
2264 else if (streq(name, "FragmentPath"))
2266 #ifdef HAVE_SYSV_COMPAT
2267 else if (streq(name, "SysVPath")) {
2272 else if (streq(name, "DefaultControlGroup"))
2273 i->default_control_group = s;
2274 else if (streq(name, "StatusText"))
2276 else if (streq(name, "SysFSPath"))
2278 else if (streq(name, "Where"))
2280 else if (streq(name, "What"))
2282 else if (streq(name, "Following"))
2289 case DBUS_TYPE_BOOLEAN: {
2292 dbus_message_iter_get_basic(iter, &b);
2294 if (streq(name, "Accept"))
2296 else if (streq(name, "NeedDaemonReload"))
2297 i->need_daemon_reload = b;
2298 else if (streq(name, "ConditionResult"))
2299 i->condition_result = b;
2304 case DBUS_TYPE_UINT32: {
2307 dbus_message_iter_get_basic(iter, &u);
2309 if (streq(name, "MainPID")) {
2311 i->main_pid = (pid_t) u;
2314 } else if (streq(name, "ControlPID"))
2315 i->control_pid = (pid_t) u;
2316 else if (streq(name, "ExecMainPID")) {
2318 i->main_pid = (pid_t) u;
2319 } else if (streq(name, "NAccepted"))
2321 else if (streq(name, "NConnections"))
2322 i->n_connections = u;
2327 case DBUS_TYPE_INT32: {
2330 dbus_message_iter_get_basic(iter, &j);
2332 if (streq(name, "ExecMainCode"))
2333 i->exit_code = (int) j;
2334 else if (streq(name, "ExecMainStatus"))
2335 i->exit_status = (int) j;
2340 case DBUS_TYPE_UINT64: {
2343 dbus_message_iter_get_basic(iter, &u);
2345 if (streq(name, "ExecMainStartTimestamp"))
2346 i->start_timestamp = (usec_t) u;
2347 else if (streq(name, "ExecMainExitTimestamp"))
2348 i->exit_timestamp = (usec_t) u;
2349 else if (streq(name, "ActiveEnterTimestamp"))
2350 i->active_enter_timestamp = (usec_t) u;
2351 else if (streq(name, "InactiveEnterTimestamp"))
2352 i->inactive_enter_timestamp = (usec_t) u;
2353 else if (streq(name, "InactiveExitTimestamp"))
2354 i->inactive_exit_timestamp = (usec_t) u;
2355 else if (streq(name, "ActiveExitTimestamp"))
2356 i->active_exit_timestamp = (usec_t) u;
2357 else if (streq(name, "ConditionTimestamp"))
2358 i->condition_timestamp = (usec_t) u;
2363 case DBUS_TYPE_ARRAY: {
2365 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT &&
2366 startswith(name, "Exec")) {
2367 DBusMessageIter sub;
2369 dbus_message_iter_recurse(iter, &sub);
2370 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2371 ExecStatusInfo *info;
2374 if (!(info = new0(ExecStatusInfo, 1)))
2377 if (!(info->name = strdup(name))) {
2382 if ((r = exec_status_info_deserialize(&sub, info)) < 0) {
2387 LIST_PREPEND(ExecStatusInfo, exec, i->exec, info);
2389 dbus_message_iter_next(&sub);
2400 static int print_property(const char *name, DBusMessageIter *iter) {
2404 /* This is a low-level property printer, see
2405 * print_status_info() for the nicer output */
2407 if (arg_property && !strv_find(arg_property, name))
2410 switch (dbus_message_iter_get_arg_type(iter)) {
2412 case DBUS_TYPE_STRUCT: {
2413 DBusMessageIter sub;
2414 dbus_message_iter_recurse(iter, &sub);
2416 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
2419 dbus_message_iter_get_basic(&sub, &u);
2422 printf("%s=%u\n", name, (unsigned) u);
2424 printf("%s=\n", name);
2427 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
2430 dbus_message_iter_get_basic(&sub, &s);
2432 if (arg_all || s[0])
2433 printf("%s=%s\n", name, s);
2441 case DBUS_TYPE_ARRAY:
2443 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "EnvironmentFiles")) {
2444 DBusMessageIter sub, sub2;
2446 dbus_message_iter_recurse(iter, &sub);
2447 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2451 dbus_message_iter_recurse(&sub, &sub2);
2453 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) >= 0 &&
2454 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, false) >= 0)
2455 printf("EnvironmentFile=%s (ignore_errors=%s)\n", path, yes_no(ignore));
2457 dbus_message_iter_next(&sub);
2462 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
2463 DBusMessageIter sub, sub2;
2465 dbus_message_iter_recurse(iter, &sub);
2466 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2467 const char *type, *path;
2469 dbus_message_iter_recurse(&sub, &sub2);
2471 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
2472 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
2473 printf("%s=%s\n", type, path);
2475 dbus_message_iter_next(&sub);
2480 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
2481 DBusMessageIter sub, sub2;
2483 dbus_message_iter_recurse(iter, &sub);
2484 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2486 uint64_t value, next_elapse;
2488 dbus_message_iter_recurse(&sub, &sub2);
2490 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
2491 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
2492 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
2493 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
2495 printf("%s={ value=%s ; next_elapse=%s }\n",
2497 format_timespan(timespan1, sizeof(timespan1), value),
2498 format_timespan(timespan2, sizeof(timespan2), next_elapse));
2501 dbus_message_iter_next(&sub);
2506 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
2507 DBusMessageIter sub;
2509 dbus_message_iter_recurse(iter, &sub);
2510 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2511 ExecStatusInfo info;
2514 if (exec_status_info_deserialize(&sub, &info) >= 0) {
2515 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
2518 t = strv_join(info.argv, " ");
2520 printf("%s={ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n",
2524 yes_no(info.ignore),
2525 strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)),
2526 strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)),
2527 (unsigned) info. pid,
2528 sigchld_code_to_string(info.code),
2530 info.code == CLD_EXITED ? "" : "/",
2531 strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status)));
2537 strv_free(info.argv);
2539 dbus_message_iter_next(&sub);
2548 if (generic_print_property(name, iter, arg_all) > 0)
2552 printf("%s=[unprintable]\n", name);
2557 static int show_one(const char *verb, DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
2558 DBusMessage *m = NULL, *reply = NULL;
2559 const char *interface = "";
2562 DBusMessageIter iter, sub, sub2, sub3;
2563 UnitStatusInfo info;
2571 dbus_error_init(&error);
2573 if (!(m = dbus_message_new_method_call(
2574 "org.freedesktop.systemd1",
2576 "org.freedesktop.DBus.Properties",
2578 log_error("Could not allocate message.");
2583 if (!dbus_message_append_args(m,
2584 DBUS_TYPE_STRING, &interface,
2585 DBUS_TYPE_INVALID)) {
2586 log_error("Could not append arguments to message.");
2591 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2592 log_error("Failed to issue method call: %s", bus_error_message(&error));
2597 if (!dbus_message_iter_init(reply, &iter) ||
2598 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
2599 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
2600 log_error("Failed to parse reply.");
2605 dbus_message_iter_recurse(&iter, &sub);
2612 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2615 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
2616 log_error("Failed to parse reply.");
2621 dbus_message_iter_recurse(&sub, &sub2);
2623 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
2624 log_error("Failed to parse reply.");
2629 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
2630 log_error("Failed to parse reply.");
2635 dbus_message_iter_recurse(&sub2, &sub3);
2637 if (show_properties)
2638 r = print_property(name, &sub3);
2640 r = status_property(name, &sub3, &info);
2643 log_error("Failed to parse reply.");
2648 dbus_message_iter_next(&sub);
2653 if (!show_properties)
2654 print_status_info(&info);
2656 if (!streq_ptr(info.active_state, "active") &&
2657 !streq_ptr(info.active_state, "reloading") &&
2658 streq(verb, "status"))
2659 /* According to LSB: "program not running" */
2662 while ((p = info.exec)) {
2663 LIST_REMOVE(ExecStatusInfo, exec, info.exec, p);
2664 exec_status_info_free(p);
2669 dbus_message_unref(m);
2672 dbus_message_unref(reply);
2674 dbus_error_free(&error);
2679 static int show(DBusConnection *bus, char **args) {
2680 DBusMessage *m = NULL, *reply = NULL;
2683 bool show_properties, new_line = false;
2689 dbus_error_init(&error);
2691 show_properties = !streq(args[0], "status");
2693 if (show_properties)
2694 pager_open_if_enabled();
2696 if (show_properties && strv_length(args) <= 1) {
2697 /* If not argument is specified inspect the manager
2700 ret = show_one(args[0], bus, "/org/freedesktop/systemd1", show_properties, &new_line);
2704 STRV_FOREACH(name, args+1) {
2705 const char *path = NULL;
2708 if (safe_atou32(*name, &id) < 0) {
2710 /* Interpret as unit name */
2712 if (!(m = dbus_message_new_method_call(
2713 "org.freedesktop.systemd1",
2714 "/org/freedesktop/systemd1",
2715 "org.freedesktop.systemd1.Manager",
2717 log_error("Could not allocate message.");
2722 if (!dbus_message_append_args(m,
2723 DBUS_TYPE_STRING, name,
2724 DBUS_TYPE_INVALID)) {
2725 log_error("Could not append arguments to message.");
2730 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2732 if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
2733 log_error("Failed to issue method call: %s", bus_error_message(&error));
2738 dbus_error_free(&error);
2740 dbus_message_unref(m);
2741 if (!(m = dbus_message_new_method_call(
2742 "org.freedesktop.systemd1",
2743 "/org/freedesktop/systemd1",
2744 "org.freedesktop.systemd1.Manager",
2746 log_error("Could not allocate message.");
2751 if (!dbus_message_append_args(m,
2752 DBUS_TYPE_STRING, name,
2753 DBUS_TYPE_INVALID)) {
2754 log_error("Could not append arguments to message.");
2759 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2760 log_error("Failed to issue method call: %s", bus_error_message(&error));
2762 if (dbus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT))
2763 ret = 4; /* According to LSB: "program or service status is unknown" */
2770 } else if (show_properties) {
2772 /* Interpret as job id */
2774 if (!(m = dbus_message_new_method_call(
2775 "org.freedesktop.systemd1",
2776 "/org/freedesktop/systemd1",
2777 "org.freedesktop.systemd1.Manager",
2779 log_error("Could not allocate message.");
2784 if (!dbus_message_append_args(m,
2785 DBUS_TYPE_UINT32, &id,
2786 DBUS_TYPE_INVALID)) {
2787 log_error("Could not append arguments to message.");
2792 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2793 log_error("Failed to issue method call: %s", bus_error_message(&error));
2799 /* Interpret as PID */
2801 if (!(m = dbus_message_new_method_call(
2802 "org.freedesktop.systemd1",
2803 "/org/freedesktop/systemd1",
2804 "org.freedesktop.systemd1.Manager",
2806 log_error("Could not allocate message.");
2811 if (!dbus_message_append_args(m,
2812 DBUS_TYPE_UINT32, &id,
2813 DBUS_TYPE_INVALID)) {
2814 log_error("Could not append arguments to message.");
2819 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2820 log_error("Failed to issue method call: %s", bus_error_message(&error));
2826 if (!dbus_message_get_args(reply, &error,
2827 DBUS_TYPE_OBJECT_PATH, &path,
2828 DBUS_TYPE_INVALID)) {
2829 log_error("Failed to parse reply: %s", bus_error_message(&error));
2834 if ((r = show_one(args[0], bus, path, show_properties, &new_line)) != 0)
2837 dbus_message_unref(m);
2838 dbus_message_unref(reply);
2844 dbus_message_unref(m);
2847 dbus_message_unref(reply);
2849 dbus_error_free(&error);
2854 static int dump(DBusConnection *bus, char **args) {
2855 DBusMessage *m = NULL, *reply = NULL;
2860 dbus_error_init(&error);
2862 pager_open_if_enabled();
2864 if (!(m = dbus_message_new_method_call(
2865 "org.freedesktop.systemd1",
2866 "/org/freedesktop/systemd1",
2867 "org.freedesktop.systemd1.Manager",
2869 log_error("Could not allocate message.");
2873 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2874 log_error("Failed to issue method call: %s", bus_error_message(&error));
2879 if (!dbus_message_get_args(reply, &error,
2880 DBUS_TYPE_STRING, &text,
2881 DBUS_TYPE_INVALID)) {
2882 log_error("Failed to parse reply: %s", bus_error_message(&error));
2887 fputs(text, stdout);
2893 dbus_message_unref(m);
2896 dbus_message_unref(reply);
2898 dbus_error_free(&error);
2903 static int snapshot(DBusConnection *bus, char **args) {
2904 DBusMessage *m = NULL, *reply = NULL;
2907 const char *name = "", *path, *id;
2908 dbus_bool_t cleanup = FALSE;
2909 DBusMessageIter iter, sub;
2911 *interface = "org.freedesktop.systemd1.Unit",
2914 dbus_error_init(&error);
2916 if (!(m = dbus_message_new_method_call(
2917 "org.freedesktop.systemd1",
2918 "/org/freedesktop/systemd1",
2919 "org.freedesktop.systemd1.Manager",
2920 "CreateSnapshot"))) {
2921 log_error("Could not allocate message.");
2925 if (strv_length(args) > 1)
2928 if (!dbus_message_append_args(m,
2929 DBUS_TYPE_STRING, &name,
2930 DBUS_TYPE_BOOLEAN, &cleanup,
2931 DBUS_TYPE_INVALID)) {
2932 log_error("Could not append arguments to message.");
2937 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2938 log_error("Failed to issue method call: %s", bus_error_message(&error));
2943 if (!dbus_message_get_args(reply, &error,
2944 DBUS_TYPE_OBJECT_PATH, &path,
2945 DBUS_TYPE_INVALID)) {
2946 log_error("Failed to parse reply: %s", bus_error_message(&error));
2951 dbus_message_unref(m);
2952 if (!(m = dbus_message_new_method_call(
2953 "org.freedesktop.systemd1",
2955 "org.freedesktop.DBus.Properties",
2957 log_error("Could not allocate message.");
2961 if (!dbus_message_append_args(m,
2962 DBUS_TYPE_STRING, &interface,
2963 DBUS_TYPE_STRING, &property,
2964 DBUS_TYPE_INVALID)) {
2965 log_error("Could not append arguments to message.");
2970 dbus_message_unref(reply);
2971 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2972 log_error("Failed to issue method call: %s", bus_error_message(&error));
2977 if (!dbus_message_iter_init(reply, &iter) ||
2978 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2979 log_error("Failed to parse reply.");
2984 dbus_message_iter_recurse(&iter, &sub);
2986 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2987 log_error("Failed to parse reply.");
2992 dbus_message_iter_get_basic(&sub, &id);
3000 dbus_message_unref(m);
3003 dbus_message_unref(reply);
3005 dbus_error_free(&error);
3010 static int delete_snapshot(DBusConnection *bus, char **args) {
3011 DBusMessage *m = NULL, *reply = NULL;
3019 dbus_error_init(&error);
3021 STRV_FOREACH(name, args+1) {
3022 const char *path = NULL;
3024 if (!(m = dbus_message_new_method_call(
3025 "org.freedesktop.systemd1",
3026 "/org/freedesktop/systemd1",
3027 "org.freedesktop.systemd1.Manager",
3029 log_error("Could not allocate message.");
3034 if (!dbus_message_append_args(m,
3035 DBUS_TYPE_STRING, name,
3036 DBUS_TYPE_INVALID)) {
3037 log_error("Could not append arguments to message.");
3042 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3043 log_error("Failed to issue method call: %s", bus_error_message(&error));
3048 if (!dbus_message_get_args(reply, &error,
3049 DBUS_TYPE_OBJECT_PATH, &path,
3050 DBUS_TYPE_INVALID)) {
3051 log_error("Failed to parse reply: %s", bus_error_message(&error));
3056 dbus_message_unref(m);
3057 if (!(m = dbus_message_new_method_call(
3058 "org.freedesktop.systemd1",
3060 "org.freedesktop.systemd1.Snapshot",
3062 log_error("Could not allocate message.");
3067 dbus_message_unref(reply);
3068 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3069 log_error("Failed to issue method call: %s", bus_error_message(&error));
3074 dbus_message_unref(m);
3075 dbus_message_unref(reply);
3083 dbus_message_unref(m);
3086 dbus_message_unref(reply);
3088 dbus_error_free(&error);
3093 static int daemon_reload(DBusConnection *bus, char **args) {
3094 DBusMessage *m = NULL, *reply = NULL;
3099 dbus_error_init(&error);
3101 if (arg_action == ACTION_RELOAD)
3103 else if (arg_action == ACTION_REEXEC)
3104 method = "Reexecute";
3106 assert(arg_action == ACTION_SYSTEMCTL);
3109 streq(args[0], "clear-jobs") ||
3110 streq(args[0], "cancel") ? "ClearJobs" :
3111 streq(args[0], "daemon-reexec") ? "Reexecute" :
3112 streq(args[0], "reset-failed") ? "ResetFailed" :
3113 streq(args[0], "halt") ? "Halt" :
3114 streq(args[0], "poweroff") ? "PowerOff" :
3115 streq(args[0], "reboot") ? "Reboot" :
3116 streq(args[0], "kexec") ? "KExec" :
3117 streq(args[0], "exit") ? "Exit" :
3118 /* "daemon-reload" */ "Reload";
3121 if (!(m = dbus_message_new_method_call(
3122 "org.freedesktop.systemd1",
3123 "/org/freedesktop/systemd1",
3124 "org.freedesktop.systemd1.Manager",
3126 log_error("Could not allocate message.");
3130 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3132 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
3133 /* There's always a fallback possible for
3134 * legacy actions. */
3139 if (streq(method, "Reexecute") && dbus_error_has_name(&error, DBUS_ERROR_NO_REPLY)) {
3140 /* On reexecution, we expect a disconnect, not
3146 log_error("Failed to issue method call: %s", bus_error_message(&error));
3155 dbus_message_unref(m);
3158 dbus_message_unref(reply);
3160 dbus_error_free(&error);
3165 static int reset_failed(DBusConnection *bus, char **args) {
3166 DBusMessage *m = NULL, *reply = NULL;
3172 dbus_error_init(&error);
3174 if (strv_length(args) <= 1)
3175 return daemon_reload(bus, args);
3177 STRV_FOREACH(name, args+1) {
3179 if (!(m = dbus_message_new_method_call(
3180 "org.freedesktop.systemd1",
3181 "/org/freedesktop/systemd1",
3182 "org.freedesktop.systemd1.Manager",
3183 "ResetFailedUnit"))) {
3184 log_error("Could not allocate message.");
3189 if (!dbus_message_append_args(m,
3190 DBUS_TYPE_STRING, name,
3191 DBUS_TYPE_INVALID)) {
3192 log_error("Could not append arguments to message.");
3197 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3198 log_error("Failed to issue method call: %s", bus_error_message(&error));
3203 dbus_message_unref(m);
3204 dbus_message_unref(reply);
3212 dbus_message_unref(m);
3215 dbus_message_unref(reply);
3217 dbus_error_free(&error);
3222 static int show_enviroment(DBusConnection *bus, char **args) {
3223 DBusMessage *m = NULL, *reply = NULL;
3225 DBusMessageIter iter, sub, sub2;
3228 *interface = "org.freedesktop.systemd1.Manager",
3229 *property = "Environment";
3231 dbus_error_init(&error);
3233 pager_open_if_enabled();
3235 if (!(m = dbus_message_new_method_call(
3236 "org.freedesktop.systemd1",
3237 "/org/freedesktop/systemd1",
3238 "org.freedesktop.DBus.Properties",
3240 log_error("Could not allocate message.");
3244 if (!dbus_message_append_args(m,
3245 DBUS_TYPE_STRING, &interface,
3246 DBUS_TYPE_STRING, &property,
3247 DBUS_TYPE_INVALID)) {
3248 log_error("Could not append arguments to message.");
3253 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3254 log_error("Failed to issue method call: %s", bus_error_message(&error));
3259 if (!dbus_message_iter_init(reply, &iter) ||
3260 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
3261 log_error("Failed to parse reply.");
3266 dbus_message_iter_recurse(&iter, &sub);
3268 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
3269 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
3270 log_error("Failed to parse reply.");
3275 dbus_message_iter_recurse(&sub, &sub2);
3277 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
3280 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
3281 log_error("Failed to parse reply.");
3286 dbus_message_iter_get_basic(&sub2, &text);
3287 printf("%s\n", text);
3289 dbus_message_iter_next(&sub2);
3296 dbus_message_unref(m);
3299 dbus_message_unref(reply);
3301 dbus_error_free(&error);
3306 static int set_environment(DBusConnection *bus, char **args) {
3307 DBusMessage *m = NULL, *reply = NULL;
3311 DBusMessageIter iter, sub;
3314 dbus_error_init(&error);
3316 method = streq(args[0], "set-environment")
3318 : "UnsetEnvironment";
3320 if (!(m = dbus_message_new_method_call(
3321 "org.freedesktop.systemd1",
3322 "/org/freedesktop/systemd1",
3323 "org.freedesktop.systemd1.Manager",
3326 log_error("Could not allocate message.");
3330 dbus_message_iter_init_append(m, &iter);
3332 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
3333 log_error("Could not append arguments to message.");
3338 STRV_FOREACH(name, args+1)
3339 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, name)) {
3340 log_error("Could not append arguments to message.");
3345 if (!dbus_message_iter_close_container(&iter, &sub)) {
3346 log_error("Could not append arguments to message.");
3351 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3352 log_error("Failed to issue method call: %s", bus_error_message(&error));
3361 dbus_message_unref(m);
3364 dbus_message_unref(reply);
3366 dbus_error_free(&error);
3371 static int enable_sysv_units(char **args) {
3374 #if defined (HAVE_SYSV_COMPAT) && (defined(TARGET_FEDORA) || defined(TARGET_MANDRIVA) || defined(TARGET_SUSE) || defined(TARGET_MEEGO) || defined(TARGET_ALTLINUX))
3375 const char *verb = args[0];
3376 unsigned f = 1, t = 1;
3379 if (arg_scope != UNIT_FILE_SYSTEM)
3382 if (!streq(verb, "enable") &&
3383 !streq(verb, "disable") &&
3384 !streq(verb, "is-enabled"))
3387 /* Processes all SysV units, and reshuffles the array so that
3388 * afterwards only the native units remain */
3391 r = lookup_paths_init(&paths, MANAGER_SYSTEM, false);
3397 for (f = 1; args[f]; f++) {
3400 bool found_native = false, found_sysv;
3402 const char *argv[6] = { "/sbin/chkconfig", NULL, NULL, NULL, NULL };
3403 char **k, *l, *q = NULL;
3410 if (!endswith(name, ".service"))
3413 if (path_is_absolute(name))
3416 STRV_FOREACH(k, paths.unit_path) {
3419 if (!isempty(arg_root))
3420 asprintf(&p, "%s/%s/%s", arg_root, *k, name);
3422 asprintf(&p, "%s/%s", *k, name);
3425 log_error("No memory");
3430 found_native = access(p, F_OK) >= 0;
3441 if (!isempty(arg_root))
3442 asprintf(&p, "%s/" SYSTEM_SYSVINIT_PATH "/%s", arg_root, name);
3444 asprintf(&p, SYSTEM_SYSVINIT_PATH "/%s", name);
3446 log_error("No memory");
3451 p[strlen(p) - sizeof(".service") + 1] = 0;
3452 found_sysv = access(p, F_OK) >= 0;
3459 /* Mark this entry, so that we don't try enabling it as native unit */
3460 args[f] = (char*) "";
3462 log_info("%s is not a native service, redirecting to /sbin/chkconfig.", name);
3464 if (!isempty(arg_root))
3465 argv[c++] = q = strappend("--root=", arg_root);
3467 argv[c++] = file_name_from_path(p);
3469 streq(verb, "enable") ? "on" :
3470 streq(verb, "disable") ? "off" : "--level=5";
3473 l = strv_join((char**)argv, " ");
3475 log_error("No memory.");
3482 log_info("Executing %s", l);
3487 log_error("Failed to fork: %m");
3492 } else if (pid == 0) {
3495 execv(argv[0], (char**) argv);
3496 _exit(EXIT_FAILURE);
3502 j = wait_for_terminate(pid, &status);
3504 log_error("Failed to wait for child: %s", strerror(-r));
3509 if (status.si_code == CLD_EXITED) {
3510 if (streq(verb, "is-enabled")) {
3511 if (status.si_status == 0) {
3520 } else if (status.si_status != 0) {
3531 lookup_paths_free(&paths);
3533 /* Drop all SysV units */
3534 for (f = 1, t = 1; args[f]; f++) {
3536 if (isempty(args[f]))
3539 args[t++] = args[f];
3548 static int enable_unit(DBusConnection *bus, char **args) {
3549 const char *verb = args[0];
3550 UnitFileChange *changes = NULL;
3551 unsigned n_changes = 0, i;
3552 int carries_install_info = -1;
3553 DBusMessage *m = NULL, *reply = NULL;
3557 dbus_error_init(&error);
3559 r = enable_sysv_units(args);
3563 if (!bus || avoid_bus()) {
3564 if (streq(verb, "enable")) {
3565 r = unit_file_enable(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3566 carries_install_info = r;
3567 } else if (streq(verb, "disable"))
3568 r = unit_file_disable(arg_scope, arg_runtime, arg_root, args+1, &changes, &n_changes);
3569 else if (streq(verb, "reenable")) {
3570 r = unit_file_reenable(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3571 carries_install_info = r;
3572 } else if (streq(verb, "link"))
3573 r = unit_file_link(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3574 else if (streq(verb, "preset")) {
3575 r = unit_file_preset(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3576 carries_install_info = r;
3577 } else if (streq(verb, "mask"))
3578 r = unit_file_mask(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3579 else if (streq(verb, "unmask"))
3580 r = unit_file_unmask(arg_scope, arg_runtime, arg_root, args+1, &changes, &n_changes);
3582 assert_not_reached("Unknown verb");
3585 log_error("Operation failed: %s", strerror(-r));
3589 for (i = 0; i < n_changes; i++) {
3590 if (changes[i].type == UNIT_FILE_SYMLINK)
3591 log_info("ln -s '%s' '%s'", changes[i].source, changes[i].path);
3593 log_info("rm '%s'", changes[i].path);
3598 bool send_force = true, expect_carries_install_info = false;
3600 DBusMessageIter iter, sub, sub2;
3602 if (streq(verb, "enable")) {
3603 method = "EnableUnitFiles";
3604 expect_carries_install_info = true;
3605 } else if (streq(verb, "disable")) {
3606 method = "DisableUnitFiles";
3608 } else if (streq(verb, "reenable")) {
3609 method = "ReenableUnitFiles";
3610 expect_carries_install_info = true;
3611 } else if (streq(verb, "link"))
3612 method = "LinkUnitFiles";
3613 else if (streq(verb, "preset")) {
3614 method = "PresetUnitFiles";
3615 expect_carries_install_info = true;
3616 } else if (streq(verb, "mask"))
3617 method = "MaskUnitFiles";
3618 else if (streq(verb, "unmask")) {
3619 method = "UnmaskUnitFiles";
3622 assert_not_reached("Unknown verb");
3624 m = dbus_message_new_method_call(
3625 "org.freedesktop.systemd1",
3626 "/org/freedesktop/systemd1",
3627 "org.freedesktop.systemd1.Manager",
3630 log_error("Out of memory");
3635 dbus_message_iter_init_append(m, &iter);
3637 r = bus_append_strv_iter(&iter, args+1);
3639 log_error("Failed to append unit files.");
3644 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &a)) {
3645 log_error("Failed to append runtime boolean.");
3653 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b)) {
3654 log_error("Failed to append force boolean.");
3660 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
3662 log_error("Failed to issue method call: %s", bus_error_message(&error));
3667 if (!dbus_message_iter_init(reply, &iter)) {
3668 log_error("Failed to initialize iterator.");
3672 if (expect_carries_install_info) {
3673 r = bus_iter_get_basic_and_next(&iter, DBUS_TYPE_BOOLEAN, &b, true);
3675 log_error("Failed to parse reply.");
3679 carries_install_info = b;
3682 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
3683 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
3684 log_error("Failed to parse reply.");
3689 dbus_message_iter_recurse(&iter, &sub);
3690 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
3691 const char *type, *path, *source;
3693 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
3694 log_error("Failed to parse reply.");
3699 dbus_message_iter_recurse(&sub, &sub2);
3701 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
3702 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0 ||
3703 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &source, false) < 0) {
3704 log_error("Failed to parse reply.");
3709 if (streq(type, "symlink"))
3710 log_info("ln -s '%s' '%s'", source, path);
3712 log_info("rm '%s'", path);
3714 dbus_message_iter_next(&sub);
3717 /* Try to reload if enabeld */
3719 r = daemon_reload(bus, args);
3722 if (carries_install_info == 0)
3723 log_warning("Warning: unit files do not carry install information. No operation executed.");
3727 dbus_message_unref(m);
3730 dbus_message_unref(reply);
3732 unit_file_changes_free(changes, n_changes);
3734 dbus_error_free(&error);
3738 static int unit_is_enabled(DBusConnection *bus, char **args) {
3741 DBusMessage *m = NULL, *reply = NULL;
3745 dbus_error_init(&error);
3747 r = enable_sysv_units(args);
3753 if (!bus || avoid_bus()) {
3755 STRV_FOREACH(name, args+1) {
3756 UnitFileState state;
3758 state = unit_file_get_state(arg_scope, arg_root, *name);
3764 if (state == UNIT_FILE_ENABLED ||
3765 state == UNIT_FILE_ENABLED_RUNTIME ||
3766 state == UNIT_FILE_STATIC)
3770 puts(unit_file_state_to_string(state));
3774 STRV_FOREACH(name, args+1) {
3777 m = dbus_message_new_method_call(
3778 "org.freedesktop.systemd1",
3779 "/org/freedesktop/systemd1",
3780 "org.freedesktop.systemd1.Manager",
3781 "GetUnitFileState");
3783 log_error("Out of memory");
3788 if (!dbus_message_append_args(m,
3789 DBUS_TYPE_STRING, name,
3790 DBUS_TYPE_INVALID)) {
3791 log_error("Could not append arguments to message.");
3796 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
3798 log_error("Failed to issue method call: %s", bus_error_message(&error));
3803 if (!dbus_message_get_args(reply, &error,
3804 DBUS_TYPE_STRING, &s,
3805 DBUS_TYPE_INVALID)) {
3806 log_error("Failed to parse reply: %s", bus_error_message(&error));
3811 dbus_message_unref(m);
3812 dbus_message_unref(reply);
3815 if (streq(s, "enabled") ||
3816 streq(s, "enabled-runtime") ||
3825 r = enabled ? 0 : 1;
3829 dbus_message_unref(m);
3832 dbus_message_unref(reply);
3834 dbus_error_free(&error);
3838 static int systemctl_help(void) {
3840 pager_open_if_enabled();
3842 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
3843 "Query or send control commands to the systemd manager.\n\n"
3844 " -h --help Show this help\n"
3845 " --version Show package version\n"
3846 " -t --type=TYPE List only units of a particular type\n"
3847 " -p --property=NAME Show only properties by this name\n"
3848 " -a --all Show all units/properties, including dead/empty ones\n"
3849 " --failed Show only failed units\n"
3850 " --full Don't ellipsize unit names on output\n"
3851 " --fail When queueing a new job, fail if conflicting jobs are\n"
3853 " --ignore-dependencies\n"
3854 " When queueing a new job, ignore all its dependencies\n"
3855 " --kill-who=WHO Who to send signal to\n"
3856 " -s --signal=SIGNAL Which signal to send\n"
3857 " -H --host=[USER@]HOST\n"
3858 " Show information for remote host\n"
3859 " -P --privileged Acquire privileges before execution\n"
3860 " -q --quiet Suppress output\n"
3861 " --no-block Do not wait until operation finished\n"
3862 " --no-wall Don't send wall message before halt/power-off/reboot\n"
3863 " --no-reload When enabling/disabling unit files, don't reload daemon\n"
3865 " --no-pager Do not pipe output into a pager\n"
3866 " --no-ask-password\n"
3867 " Do not ask for system passwords\n"
3868 " --order When generating graph for dot, show only order\n"
3869 " --require When generating graph for dot, show only requirement\n"
3870 " --system Connect to system manager\n"
3871 " --user Connect to user service manager\n"
3872 " --global Enable/disable unit files globally\n"
3873 " -f --force When enabling unit files, override existing symlinks\n"
3874 " When shutting down, execute action immediately\n"
3875 " --root=PATH Enable unit files in the specified root directory\n"
3876 " --runtime Enable unit files only temporarily until next reboot\n\n"
3878 " list-units List loaded units\n"
3879 " start [NAME...] Start (activate) one or more units\n"
3880 " stop [NAME...] Stop (deactivate) one or more units\n"
3881 " reload [NAME...] Reload one or more units\n"
3882 " restart [NAME...] Start or restart one or more units\n"
3883 " try-restart [NAME...] Restart one or more units if active\n"
3884 " reload-or-restart [NAME...] Reload one or more units is possible,\n"
3885 " otherwise start or restart\n"
3886 " reload-or-try-restart [NAME...] Reload one or more units is possible,\n"
3887 " otherwise restart if active\n"
3888 " isolate [NAME] Start one unit and stop all others\n"
3889 " kill [NAME...] Send signal to processes of a unit\n"
3890 " is-active [NAME...] Check whether units are active\n"
3891 " status [NAME...|PID...] Show runtime status of one or more units\n"
3892 " show [NAME...|JOB...] Show properties of one or more\n"
3893 " units/jobs or the manager\n"
3894 " reset-failed [NAME...] Reset failed state for all, one, or more\n"
3896 " load [NAME...] Load one or more units\n\n"
3897 "Unit File Commands:\n"
3898 " list-unit-files List installed unit files\n"
3899 " enable [NAME...] Enable one or more unit files\n"
3900 " disable [NAME...] Disable one or more unit files\n"
3901 " reenable [NAME...] Reenable one or more unit files\n"
3902 " preset [NAME...] Enable/disable one or more unit files\n"
3903 " based on preset configuration\n"
3904 " mask [NAME...] Mask one or more units\n"
3905 " unmask [NAME...] Unmask one or more units\n"
3906 " link [PATH...] Link one or more units files into\n"
3907 " the search path\n"
3908 " is-enabled [NAME...] Check whether unit files are enabled\n\n"
3910 " list-jobs List jobs\n"
3911 " cancel [JOB...] Cancel all, one, or more jobs\n\n"
3912 "Status Commands:\n"
3913 " dump Dump server status\n"
3914 " dot Dump dependency graph for dot(1)\n\n"
3915 "Snapshot Commands:\n"
3916 " snapshot [NAME] Create a snapshot\n"
3917 " delete [NAME...] Remove one or more snapshots\n\n"
3918 "Environment Commands:\n"
3919 " show-environment Dump environment\n"
3920 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
3921 " unset-environment [NAME...] Unset one or more environment variables\n\n"
3922 "Manager Lifecycle Commands:\n"
3923 " daemon-reload Reload systemd manager configuration\n"
3924 " daemon-reexec Reexecute systemd manager\n\n"
3925 "System Commands:\n"
3926 " default Enter system default mode\n"
3927 " rescue Enter system rescue mode\n"
3928 " emergency Enter system emergency mode\n"
3929 " halt Shut down and halt the system\n"
3930 " poweroff Shut down and power-off the system\n"
3931 " reboot Shut down and reboot the system\n"
3932 " kexec Shut down and reboot the system with kexec\n"
3933 " exit Ask for user instance termination\n",
3934 program_invocation_short_name);
3939 static int halt_help(void) {
3941 printf("%s [OPTIONS...]\n\n"
3942 "%s the system.\n\n"
3943 " --help Show this help\n"
3944 " --halt Halt the machine\n"
3945 " -p --poweroff Switch off the machine\n"
3946 " --reboot Reboot the machine\n"
3947 " -f --force Force immediate halt/power-off/reboot\n"
3948 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
3949 " -d --no-wtmp Don't write wtmp record\n"
3950 " -n --no-sync Don't sync before halt/power-off/reboot\n"
3951 " --no-wall Don't send wall message before halt/power-off/reboot\n",
3952 program_invocation_short_name,
3953 arg_action == ACTION_REBOOT ? "Reboot" :
3954 arg_action == ACTION_POWEROFF ? "Power off" :
3960 static int shutdown_help(void) {
3962 printf("%s [OPTIONS...] [TIME] [WALL...]\n\n"
3963 "Shut down the system.\n\n"
3964 " --help Show this help\n"
3965 " -H --halt Halt the machine\n"
3966 " -P --poweroff Power-off the machine\n"
3967 " -r --reboot Reboot the machine\n"
3968 " -h Equivalent to --poweroff, overriden by --halt\n"
3969 " -k Don't halt/power-off/reboot, just send warnings\n"
3970 " --no-wall Don't send wall message before halt/power-off/reboot\n"
3971 " -c Cancel a pending shutdown\n",
3972 program_invocation_short_name);
3977 static int telinit_help(void) {
3979 printf("%s [OPTIONS...] {COMMAND}\n\n"
3980 "Send control commands to the init daemon.\n\n"
3981 " --help Show this help\n"
3982 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
3984 " 0 Power-off the machine\n"
3985 " 6 Reboot the machine\n"
3986 " 2, 3, 4, 5 Start runlevelX.target unit\n"
3987 " 1, s, S Enter rescue mode\n"
3988 " q, Q Reload init daemon configuration\n"
3989 " u, U Reexecute init daemon\n",
3990 program_invocation_short_name);
3995 static int runlevel_help(void) {
3997 printf("%s [OPTIONS...]\n\n"
3998 "Prints the previous and current runlevel of the init system.\n\n"
3999 " --help Show this help\n",
4000 program_invocation_short_name);
4005 static int systemctl_parse_argv(int argc, char *argv[]) {
4009 ARG_IGNORE_DEPENDENCIES,
4024 ARG_NO_ASK_PASSWORD,
4029 static const struct option options[] = {
4030 { "help", no_argument, NULL, 'h' },
4031 { "version", no_argument, NULL, ARG_VERSION },
4032 { "type", required_argument, NULL, 't' },
4033 { "property", required_argument, NULL, 'p' },
4034 { "all", no_argument, NULL, 'a' },
4035 { "failed", no_argument, NULL, ARG_FAILED },
4036 { "full", no_argument, NULL, ARG_FULL },
4037 { "fail", no_argument, NULL, ARG_FAIL },
4038 { "ignore-dependencies", no_argument, NULL, ARG_IGNORE_DEPENDENCIES },
4039 { "user", no_argument, NULL, ARG_USER },
4040 { "system", no_argument, NULL, ARG_SYSTEM },
4041 { "global", no_argument, NULL, ARG_GLOBAL },
4042 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
4043 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
4044 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4045 { "quiet", no_argument, NULL, 'q' },
4046 { "order", no_argument, NULL, ARG_ORDER },
4047 { "require", no_argument, NULL, ARG_REQUIRE },
4048 { "root", required_argument, NULL, ARG_ROOT },
4049 { "force", no_argument, NULL, 'f' },
4050 { "no-reload", no_argument, NULL, ARG_NO_RELOAD },
4051 { "kill-mode", required_argument, NULL, ARG_KILL_MODE }, /* undocumented on purpose */
4052 { "kill-who", required_argument, NULL, ARG_KILL_WHO },
4053 { "signal", required_argument, NULL, 's' },
4054 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
4055 { "host", required_argument, NULL, 'H' },
4056 { "privileged",no_argument, NULL, 'P' },
4057 { "runtime", no_argument, NULL, ARG_RUNTIME },
4058 { NULL, 0, NULL, 0 }
4066 /* Only when running as systemctl we ask for passwords */
4067 arg_ask_password = true;
4069 while ((c = getopt_long(argc, argv, "ht:p:aqfs:H:P", options, NULL)) >= 0) {
4078 puts(PACKAGE_STRING);
4080 puts(SYSTEMD_FEATURES);
4090 if (!(l = strv_append(arg_property, optarg)))
4093 strv_free(arg_property);
4096 /* If the user asked for a particular
4097 * property, show it to him, even if it is
4108 arg_job_mode = "fail";
4111 case ARG_IGNORE_DEPENDENCIES:
4112 arg_job_mode = "ignore-dependencies";
4116 arg_scope = UNIT_FILE_USER;
4120 arg_scope = UNIT_FILE_SYSTEM;
4124 arg_scope = UNIT_FILE_GLOBAL;
4128 arg_no_block = true;
4132 arg_no_pager = true;
4140 arg_dot = DOT_ORDER;
4144 arg_dot = DOT_REQUIRE;
4168 arg_no_reload = true;
4172 arg_kill_who = optarg;
4176 arg_kill_mode = optarg;
4180 if ((arg_signal = signal_from_string_try_harder(optarg)) < 0) {
4181 log_error("Failed to parse signal string %s.", optarg);
4186 case ARG_NO_ASK_PASSWORD:
4187 arg_ask_password = false;
4191 arg_transport = TRANSPORT_POLKIT;
4195 arg_transport = TRANSPORT_SSH;
4207 log_error("Unknown option code %c", c);
4212 if (arg_transport != TRANSPORT_NORMAL && arg_scope != UNIT_FILE_SYSTEM) {
4213 log_error("Cannot access user instance remotely.");
4220 static int halt_parse_argv(int argc, char *argv[]) {
4229 static const struct option options[] = {
4230 { "help", no_argument, NULL, ARG_HELP },
4231 { "halt", no_argument, NULL, ARG_HALT },
4232 { "poweroff", no_argument, NULL, 'p' },
4233 { "reboot", no_argument, NULL, ARG_REBOOT },
4234 { "force", no_argument, NULL, 'f' },
4235 { "wtmp-only", no_argument, NULL, 'w' },
4236 { "no-wtmp", no_argument, NULL, 'd' },
4237 { "no-sync", no_argument, NULL, 'n' },
4238 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4239 { NULL, 0, NULL, 0 }
4247 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
4248 if (runlevel == '0' || runlevel == '6')
4249 arg_immediate = true;
4251 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
4259 arg_action = ACTION_HALT;
4263 if (arg_action != ACTION_REBOOT)
4264 arg_action = ACTION_POWEROFF;
4268 arg_action = ACTION_REBOOT;
4272 arg_immediate = true;
4293 /* Compatibility nops */
4300 log_error("Unknown option code %c", c);
4305 if (optind < argc) {
4306 log_error("Too many arguments.");
4313 static int parse_time_spec(const char *t, usec_t *_u) {
4317 if (streq(t, "now"))
4319 else if (!strchr(t, ':')) {
4322 if (safe_atou64(t, &u) < 0)
4325 *_u = now(CLOCK_REALTIME) + USEC_PER_MINUTE * u;
4334 hour = strtol(t, &e, 10);
4335 if (errno != 0 || *e != ':' || hour < 0 || hour > 23)
4338 minute = strtol(e+1, &e, 10);
4339 if (errno != 0 || *e != 0 || minute < 0 || minute > 59)
4342 n = now(CLOCK_REALTIME);
4343 s = (time_t) (n / USEC_PER_SEC);
4346 assert_se(localtime_r(&s, &tm));
4348 tm.tm_hour = (int) hour;
4349 tm.tm_min = (int) minute;
4352 assert_se(s = mktime(&tm));
4354 *_u = (usec_t) s * USEC_PER_SEC;
4357 *_u += USEC_PER_DAY;
4363 static bool kexec_loaded(void) {
4364 bool loaded = false;
4367 if (read_one_line_file("/sys/kernel/kexec_loaded", &s) >= 0) {
4375 static int shutdown_parse_argv(int argc, char *argv[]) {
4382 static const struct option options[] = {
4383 { "help", no_argument, NULL, ARG_HELP },
4384 { "halt", no_argument, NULL, 'H' },
4385 { "poweroff", no_argument, NULL, 'P' },
4386 { "reboot", no_argument, NULL, 'r' },
4387 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4388 { NULL, 0, NULL, 0 }
4396 while ((c = getopt_long(argc, argv, "HPrhkt:afFc", options, NULL)) >= 0) {
4404 arg_action = ACTION_HALT;
4408 arg_action = ACTION_POWEROFF;
4413 arg_action = ACTION_KEXEC;
4415 arg_action = ACTION_REBOOT;
4419 if (arg_action != ACTION_HALT)
4420 arg_action = ACTION_POWEROFF;
4433 /* Compatibility nops */
4437 arg_action = ACTION_CANCEL_SHUTDOWN;
4444 log_error("Unknown option code %c", c);
4449 if (argc > optind) {
4450 if ((r = parse_time_spec(argv[optind], &arg_when)) < 0) {
4451 log_error("Failed to parse time specification: %s", argv[optind]);
4455 arg_when = now(CLOCK_REALTIME) + USEC_PER_MINUTE;
4457 /* We skip the time argument */
4458 if (argc > optind + 1)
4459 arg_wall = argv + optind + 1;
4466 static int telinit_parse_argv(int argc, char *argv[]) {
4473 static const struct option options[] = {
4474 { "help", no_argument, NULL, ARG_HELP },
4475 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4476 { NULL, 0, NULL, 0 }
4479 static const struct {
4483 { '0', ACTION_POWEROFF },
4484 { '6', ACTION_REBOOT },
4485 { '1', ACTION_RESCUE },
4486 { '2', ACTION_RUNLEVEL2 },
4487 { '3', ACTION_RUNLEVEL3 },
4488 { '4', ACTION_RUNLEVEL4 },
4489 { '5', ACTION_RUNLEVEL5 },
4490 { 's', ACTION_RESCUE },
4491 { 'S', ACTION_RESCUE },
4492 { 'q', ACTION_RELOAD },
4493 { 'Q', ACTION_RELOAD },
4494 { 'u', ACTION_REEXEC },
4495 { 'U', ACTION_REEXEC }
4504 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4519 log_error("Unknown option code %c", c);
4524 if (optind >= argc) {
4529 if (optind + 1 < argc) {
4530 log_error("Too many arguments.");
4534 if (strlen(argv[optind]) != 1) {
4535 log_error("Expected single character argument.");
4539 for (i = 0; i < ELEMENTSOF(table); i++)
4540 if (table[i].from == argv[optind][0])
4543 if (i >= ELEMENTSOF(table)) {
4544 log_error("Unknown command %s.", argv[optind]);
4548 arg_action = table[i].to;
4555 static int runlevel_parse_argv(int argc, char *argv[]) {
4561 static const struct option options[] = {
4562 { "help", no_argument, NULL, ARG_HELP },
4563 { NULL, 0, NULL, 0 }
4571 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4582 log_error("Unknown option code %c", c);
4587 if (optind < argc) {
4588 log_error("Too many arguments.");
4595 static int parse_argv(int argc, char *argv[]) {
4599 if (program_invocation_short_name) {
4601 if (strstr(program_invocation_short_name, "halt")) {
4602 arg_action = ACTION_HALT;
4603 return halt_parse_argv(argc, argv);
4604 } else if (strstr(program_invocation_short_name, "poweroff")) {
4605 arg_action = ACTION_POWEROFF;
4606 return halt_parse_argv(argc, argv);
4607 } else if (strstr(program_invocation_short_name, "reboot")) {
4609 arg_action = ACTION_KEXEC;
4611 arg_action = ACTION_REBOOT;
4612 return halt_parse_argv(argc, argv);
4613 } else if (strstr(program_invocation_short_name, "shutdown")) {
4614 arg_action = ACTION_POWEROFF;
4615 return shutdown_parse_argv(argc, argv);
4616 } else if (strstr(program_invocation_short_name, "init")) {
4618 if (sd_booted() > 0) {
4619 arg_action = ACTION_INVALID;
4620 return telinit_parse_argv(argc, argv);
4622 /* Hmm, so some other init system is
4623 * running, we need to forward this
4624 * request to it. For now we simply
4625 * guess that it is Upstart. */
4627 execv("/lib/upstart/telinit", argv);
4629 log_error("Couldn't find an alternative telinit implementation to spawn.");
4633 } else if (strstr(program_invocation_short_name, "runlevel")) {
4634 arg_action = ACTION_RUNLEVEL;
4635 return runlevel_parse_argv(argc, argv);
4639 arg_action = ACTION_SYSTEMCTL;
4640 return systemctl_parse_argv(argc, argv);
4643 static int action_to_runlevel(void) {
4645 static const char table[_ACTION_MAX] = {
4646 [ACTION_HALT] = '0',
4647 [ACTION_POWEROFF] = '0',
4648 [ACTION_REBOOT] = '6',
4649 [ACTION_RUNLEVEL2] = '2',
4650 [ACTION_RUNLEVEL3] = '3',
4651 [ACTION_RUNLEVEL4] = '4',
4652 [ACTION_RUNLEVEL5] = '5',
4653 [ACTION_RESCUE] = '1'
4656 assert(arg_action < _ACTION_MAX);
4658 return table[arg_action];
4661 static int talk_upstart(void) {
4662 DBusMessage *m = NULL, *reply = NULL;
4664 int previous, rl, r;
4666 env1_buf[] = "RUNLEVEL=X",
4667 env2_buf[] = "PREVLEVEL=X";
4668 char *env1 = env1_buf, *env2 = env2_buf;
4669 const char *emit = "runlevel";
4670 dbus_bool_t b_false = FALSE;
4671 DBusMessageIter iter, sub;
4672 DBusConnection *bus;
4674 dbus_error_init(&error);
4676 if (!(rl = action_to_runlevel()))
4679 if (utmp_get_runlevel(&previous, NULL) < 0)
4682 if (!(bus = dbus_connection_open_private("unix:abstract=/com/ubuntu/upstart", &error))) {
4683 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
4688 log_error("Failed to connect to Upstart bus: %s", bus_error_message(&error));
4693 if ((r = bus_check_peercred(bus)) < 0) {
4694 log_error("Failed to verify owner of bus.");
4698 if (!(m = dbus_message_new_method_call(
4699 "com.ubuntu.Upstart",
4700 "/com/ubuntu/Upstart",
4701 "com.ubuntu.Upstart0_6",
4704 log_error("Could not allocate message.");
4709 dbus_message_iter_init_append(m, &iter);
4711 env1_buf[sizeof(env1_buf)-2] = rl;
4712 env2_buf[sizeof(env2_buf)-2] = previous;
4714 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
4715 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
4716 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
4717 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
4718 !dbus_message_iter_close_container(&iter, &sub) ||
4719 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
4720 log_error("Could not append arguments to message.");
4725 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
4727 if (error_is_no_service(&error)) {
4732 log_error("Failed to issue method call: %s", bus_error_message(&error));
4741 dbus_message_unref(m);
4744 dbus_message_unref(reply);
4747 dbus_connection_flush(bus);
4748 dbus_connection_close(bus);
4749 dbus_connection_unref(bus);
4752 dbus_error_free(&error);
4757 static int talk_initctl(void) {
4758 struct init_request request;
4762 if (!(rl = action_to_runlevel()))
4766 request.magic = INIT_MAGIC;
4767 request.sleeptime = 0;
4768 request.cmd = INIT_CMD_RUNLVL;
4769 request.runlevel = rl;
4771 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
4773 if (errno == ENOENT)
4776 log_error("Failed to open "INIT_FIFO": %m");
4781 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
4782 close_nointr_nofail(fd);
4785 log_error("Failed to write to "INIT_FIFO": %m");
4786 return errno ? -errno : -EIO;
4792 static int systemctl_main(DBusConnection *bus, int argc, char *argv[], DBusError *error) {
4794 static const struct {
4802 int (* const dispatch)(DBusConnection *bus, char **args);
4804 { "list-units", LESS, 1, list_units },
4805 { "list-unit-files", EQUAL, 1, list_unit_files },
4806 { "list-jobs", EQUAL, 1, list_jobs },
4807 { "clear-jobs", EQUAL, 1, daemon_reload },
4808 { "load", MORE, 2, load_unit },
4809 { "cancel", MORE, 2, cancel_job },
4810 { "start", MORE, 2, start_unit },
4811 { "stop", MORE, 2, start_unit },
4812 { "condstop", MORE, 2, start_unit }, /* For compatibility with ALTLinux */
4813 { "reload", MORE, 2, start_unit },
4814 { "restart", MORE, 2, start_unit },
4815 { "try-restart", MORE, 2, start_unit },
4816 { "reload-or-restart", MORE, 2, start_unit },
4817 { "reload-or-try-restart", MORE, 2, start_unit },
4818 { "force-reload", MORE, 2, start_unit }, /* For compatibility with SysV */
4819 { "condreload", MORE, 2, start_unit }, /* For compatibility with ALTLinux */
4820 { "condrestart", MORE, 2, start_unit }, /* For compatibility with RH */
4821 { "isolate", EQUAL, 2, start_unit },
4822 { "kill", MORE, 2, kill_unit },
4823 { "is-active", MORE, 2, check_unit },
4824 { "check", MORE, 2, check_unit },
4825 { "show", MORE, 1, show },
4826 { "status", MORE, 2, show },
4827 { "dump", EQUAL, 1, dump },
4828 { "dot", EQUAL, 1, dot },
4829 { "snapshot", LESS, 2, snapshot },
4830 { "delete", MORE, 2, delete_snapshot },
4831 { "daemon-reload", EQUAL, 1, daemon_reload },
4832 { "daemon-reexec", EQUAL, 1, daemon_reload },
4833 { "show-environment", EQUAL, 1, show_enviroment },
4834 { "set-environment", MORE, 2, set_environment },
4835 { "unset-environment", MORE, 2, set_environment },
4836 { "halt", EQUAL, 1, start_special },
4837 { "poweroff", EQUAL, 1, start_special },
4838 { "reboot", EQUAL, 1, start_special },
4839 { "kexec", EQUAL, 1, start_special },
4840 { "default", EQUAL, 1, start_special },
4841 { "rescue", EQUAL, 1, start_special },
4842 { "emergency", EQUAL, 1, start_special },
4843 { "exit", EQUAL, 1, start_special },
4844 { "reset-failed", MORE, 1, reset_failed },
4845 { "enable", MORE, 2, enable_unit },
4846 { "disable", MORE, 2, enable_unit },
4847 { "is-enabled", MORE, 2, unit_is_enabled },
4848 { "reenable", MORE, 2, enable_unit },
4849 { "preset", MORE, 2, enable_unit },
4850 { "mask", MORE, 2, enable_unit },
4851 { "unmask", MORE, 2, enable_unit },
4852 { "link", MORE, 2, enable_unit }
4862 left = argc - optind;
4865 /* Special rule: no arguments means "list-units" */
4868 if (streq(argv[optind], "help")) {
4873 for (i = 0; i < ELEMENTSOF(verbs); i++)
4874 if (streq(argv[optind], verbs[i].verb))
4877 if (i >= ELEMENTSOF(verbs)) {
4878 log_error("Unknown operation %s", argv[optind]);
4883 switch (verbs[i].argc_cmp) {
4886 if (left != verbs[i].argc) {
4887 log_error("Invalid number of arguments.");
4894 if (left < verbs[i].argc) {
4895 log_error("Too few arguments.");
4902 if (left > verbs[i].argc) {
4903 log_error("Too many arguments.");
4910 assert_not_reached("Unknown comparison operator.");
4913 /* Require a bus connection for all operations but
4915 if (!streq(verbs[i].verb, "enable") &&
4916 !streq(verbs[i].verb, "disable") &&
4917 !streq(verbs[i].verb, "is-enable") &&
4918 !streq(verbs[i].verb, "reenable") &&
4919 !streq(verbs[i].verb, "preset") &&
4920 !streq(verbs[i].verb, "mask") &&
4921 !streq(verbs[i].verb, "unmask") &&
4922 !streq(verbs[i].verb, "link")) {
4924 if (running_in_chroot() > 0) {
4925 log_info("Running in chroot, ignoring request.");
4929 if (!bus && !avoid_bus()) {
4930 log_error("Failed to get D-Bus connection: %s", error->message);
4935 return verbs[i].dispatch(bus, argv + optind);
4938 static int send_shutdownd(usec_t t, char mode, bool dry_run, bool warn, const char *message) {
4940 struct msghdr msghdr;
4942 union sockaddr_union sockaddr;
4943 struct shutdownd_command c;
4948 c.dry_run = dry_run;
4952 strncpy(c.wall_message, message, sizeof(c.wall_message));
4954 if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0)
4958 sockaddr.sa.sa_family = AF_UNIX;
4959 sockaddr.un.sun_path[0] = 0;
4960 strncpy(sockaddr.un.sun_path, "/run/systemd/shutdownd", sizeof(sockaddr.un.sun_path));
4963 iovec.iov_base = (char*) &c;
4964 iovec.iov_len = sizeof(c);
4967 msghdr.msg_name = &sockaddr;
4968 msghdr.msg_namelen = offsetof(struct sockaddr_un, sun_path) + sizeof("/run/systemd/shutdownd") - 1;
4970 msghdr.msg_iov = &iovec;
4971 msghdr.msg_iovlen = 1;
4973 if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) < 0) {
4974 close_nointr_nofail(fd);
4978 close_nointr_nofail(fd);
4982 static int reload_with_fallback(DBusConnection *bus) {
4985 /* First, try systemd via D-Bus. */
4986 if (daemon_reload(bus, NULL) > 0)
4990 /* Nothing else worked, so let's try signals */
4991 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
4993 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
4994 log_error("kill() failed: %m");
5001 static int start_with_fallback(DBusConnection *bus) {
5004 /* First, try systemd via D-Bus. */
5005 if (start_unit(bus, NULL) >= 0)
5009 /* Hmm, talking to systemd via D-Bus didn't work. Then
5010 * let's try to talk to Upstart via D-Bus. */
5011 if (talk_upstart() > 0)
5014 /* Nothing else worked, so let's try
5016 if (talk_initctl() > 0)
5019 log_error("Failed to talk to init daemon.");
5023 warn_wall(arg_action);
5027 static int halt_main(DBusConnection *bus) {
5030 if (geteuid() != 0) {
5031 log_error("Must be root.");
5037 char date[FORMAT_TIMESTAMP_MAX];
5039 m = strv_join(arg_wall, " ");
5040 r = send_shutdownd(arg_when,
5041 arg_action == ACTION_HALT ? 'H' :
5042 arg_action == ACTION_POWEROFF ? 'P' :
5050 log_warning("Failed to talk to shutdownd, proceeding with immediate shutdown: %s", strerror(-r));
5052 log_info("Shutdown scheduled for %s, use 'shutdown -c' to cancel.",
5053 format_timestamp(date, sizeof(date), arg_when));
5058 if (!arg_dry && !arg_immediate)
5059 return start_with_fallback(bus);
5062 if (sd_booted() > 0)
5063 log_debug("Not writing utmp record, assuming that systemd-update-utmp is used.");
5064 else if ((r = utmp_put_shutdown(0)) < 0)
5065 log_warning("Failed to write utmp record: %s", strerror(-r));
5074 /* Make sure C-A-D is handled by the kernel from this
5076 reboot(RB_ENABLE_CAD);
5078 switch (arg_action) {
5081 log_info("Halting.");
5082 reboot(RB_HALT_SYSTEM);
5085 case ACTION_POWEROFF:
5086 log_info("Powering off.");
5087 reboot(RB_POWER_OFF);
5091 log_info("Rebooting.");
5092 reboot(RB_AUTOBOOT);
5096 assert_not_reached("Unknown halt action.");
5099 /* We should never reach this. */
5103 static int runlevel_main(void) {
5104 int r, runlevel, previous;
5106 r = utmp_get_runlevel(&runlevel, &previous);
5113 previous <= 0 ? 'N' : previous,
5114 runlevel <= 0 ? 'N' : runlevel);
5119 int main(int argc, char*argv[]) {
5120 int r, retval = EXIT_FAILURE;
5121 DBusConnection *bus = NULL;
5124 dbus_error_init(&error);
5126 log_parse_environment();
5129 if ((r = parse_argv(argc, argv)) < 0)
5132 retval = EXIT_SUCCESS;
5136 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
5137 * let's shortcut this */
5138 if (arg_action == ACTION_RUNLEVEL) {
5139 r = runlevel_main();
5140 retval = r < 0 ? EXIT_FAILURE : r;
5144 if (running_in_chroot() > 0 && arg_action != ACTION_SYSTEMCTL) {
5145 log_info("Running in chroot, ignoring request.");
5151 if (arg_transport == TRANSPORT_NORMAL)
5152 bus_connect(arg_scope == UNIT_FILE_SYSTEM ? DBUS_BUS_SYSTEM : DBUS_BUS_SESSION, &bus, &private_bus, &error);
5153 else if (arg_transport == TRANSPORT_POLKIT) {
5154 bus_connect_system_polkit(&bus, &error);
5155 private_bus = false;
5156 } else if (arg_transport == TRANSPORT_SSH) {
5157 bus_connect_system_ssh(NULL, arg_host, &bus, &error);
5158 private_bus = false;
5160 assert_not_reached("Uh, invalid transport...");
5163 switch (arg_action) {
5165 case ACTION_SYSTEMCTL:
5166 r = systemctl_main(bus, argc, argv, &error);
5170 case ACTION_POWEROFF:
5176 case ACTION_RUNLEVEL2:
5177 case ACTION_RUNLEVEL3:
5178 case ACTION_RUNLEVEL4:
5179 case ACTION_RUNLEVEL5:
5181 case ACTION_EMERGENCY:
5182 case ACTION_DEFAULT:
5183 r = start_with_fallback(bus);
5188 r = reload_with_fallback(bus);
5191 case ACTION_CANCEL_SHUTDOWN:
5192 r = send_shutdownd(0, 0, false, false, NULL);
5195 case ACTION_INVALID:
5196 case ACTION_RUNLEVEL:
5198 assert_not_reached("Unknown action");
5201 retval = r < 0 ? EXIT_FAILURE : r;
5205 dbus_connection_flush(bus);
5206 dbus_connection_close(bus);
5207 dbus_connection_unref(bus);
5210 dbus_error_free(&error);
5214 strv_free(arg_property);