1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/reboot.h>
28 #include <sys/ioctl.h>
32 #include <sys/socket.h>
35 #include <sys/prctl.h>
36 #include <dbus/dbus.h>
42 #include "utmp-wtmp.h"
46 #include "dbus-common.h"
47 #include "cgroup-show.h"
48 #include "cgroup-util.h"
50 #include "path-lookup.h"
51 #include "conf-parser.h"
52 #include "sd-daemon.h"
53 #include "shutdownd.h"
54 #include "exit-status.h"
55 #include "bus-errors.h"
57 #include "unit-name.h"
59 #include "spawn-agent.h"
62 static const char *arg_type = NULL;
63 static char **arg_property = NULL;
64 static bool arg_all = false;
65 static const char *arg_job_mode = "replace";
66 static UnitFileScope arg_scope = UNIT_FILE_SYSTEM;
67 static bool arg_immediate = false;
68 static bool arg_no_block = false;
69 static bool arg_no_pager = false;
70 static bool arg_no_wtmp = false;
71 static bool arg_no_sync = false;
72 static bool arg_no_wall = false;
73 static bool arg_no_reload = false;
74 static bool arg_dry = false;
75 static bool arg_quiet = false;
76 static bool arg_full = false;
77 static bool arg_force = false;
78 static bool arg_ask_password = false;
79 static bool arg_failed = false;
80 static bool arg_runtime = false;
81 static char **arg_wall = NULL;
82 static const char *arg_kill_who = NULL;
83 static const char *arg_kill_mode = NULL;
84 static int arg_signal = SIGTERM;
85 static const char *arg_root = NULL;
86 static usec_t arg_when = 0;
105 ACTION_CANCEL_SHUTDOWN,
107 } arg_action = ACTION_SYSTEMCTL;
113 static enum transport {
117 } arg_transport = TRANSPORT_NORMAL;
118 static const char *arg_host = NULL;
120 static bool private_bus = false;
122 static int daemon_reload(DBusConnection *bus, char **args);
124 static bool on_tty(void) {
127 /* Note that this is invoked relatively early, before we start
128 * the pager. That means the value we return reflects whether
129 * we originally were started on a tty, not if we currently
130 * are. But this is intended, since we want colour and so on
131 * when run in our own pager. */
133 if (_unlikely_(t < 0))
134 t = isatty(STDOUT_FILENO) > 0;
139 static void pager_open_if_enabled(void) {
148 static void agent_open_if_enabled(void) {
150 /* Open the password agent as a child process if necessary */
152 if (!arg_ask_password)
155 if (arg_scope != UNIT_FILE_SYSTEM)
161 static const char *ansi_highlight(bool b) {
166 return b ? ANSI_HIGHLIGHT_ON : ANSI_HIGHLIGHT_OFF;
169 static const char *ansi_highlight_green(bool b) {
174 return b ? ANSI_HIGHLIGHT_GREEN_ON : ANSI_HIGHLIGHT_OFF;
177 static bool error_is_no_service(const DBusError *error) {
180 if (!dbus_error_is_set(error))
183 if (dbus_error_has_name(error, DBUS_ERROR_NAME_HAS_NO_OWNER))
186 if (dbus_error_has_name(error, DBUS_ERROR_SERVICE_UNKNOWN))
189 return startswith(error->name, "org.freedesktop.DBus.Error.Spawn.");
192 static int translate_bus_error_to_exit_status(int r, const DBusError *error) {
195 if (!dbus_error_is_set(error))
198 if (dbus_error_has_name(error, DBUS_ERROR_ACCESS_DENIED) ||
199 dbus_error_has_name(error, BUS_ERROR_ONLY_BY_DEPENDENCY) ||
200 dbus_error_has_name(error, BUS_ERROR_NO_ISOLATION) ||
201 dbus_error_has_name(error, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE))
202 return EXIT_NOPERMISSION;
204 if (dbus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT))
205 return EXIT_NOTINSTALLED;
207 if (dbus_error_has_name(error, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE) ||
208 dbus_error_has_name(error, BUS_ERROR_NOT_SUPPORTED))
209 return EXIT_NOTIMPLEMENTED;
211 if (dbus_error_has_name(error, BUS_ERROR_LOAD_FAILED))
212 return EXIT_NOTCONFIGURED;
220 static void warn_wall(enum action action) {
221 static const char *table[_ACTION_MAX] = {
222 [ACTION_HALT] = "The system is going down for system halt NOW!",
223 [ACTION_REBOOT] = "The system is going down for reboot NOW!",
224 [ACTION_POWEROFF] = "The system is going down for power-off NOW!",
225 [ACTION_KEXEC] = "The system is going down for kexec reboot NOW!",
226 [ACTION_RESCUE] = "The system is going down to rescue mode NOW!",
227 [ACTION_EMERGENCY] = "The system is going down to emergency mode NOW!"
236 if (!(p = strv_join(arg_wall, " "))) {
237 log_error("Failed to join strings.");
253 utmp_wall(table[action], NULL);
256 static bool avoid_bus(void) {
258 if (running_in_chroot() > 0)
261 if (sd_booted() <= 0)
264 if (!isempty(arg_root))
267 if (arg_scope == UNIT_FILE_GLOBAL)
275 const char *description;
276 const char *load_state;
277 const char *active_state;
278 const char *sub_state;
279 const char *following;
280 const char *unit_path;
282 const char *job_type;
283 const char *job_path;
286 static int compare_unit_info(const void *a, const void *b) {
288 const struct unit_info *u = a, *v = b;
290 d1 = strrchr(u->id, '.');
291 d2 = strrchr(v->id, '.');
296 if ((r = strcasecmp(d1, d2)) != 0)
300 return strcasecmp(u->id, v->id);
303 static bool output_show_unit(const struct unit_info *u) {
307 return streq(u->active_state, "failed");
309 return (!arg_type || ((dot = strrchr(u->id, '.')) &&
310 streq(dot+1, arg_type))) &&
311 (arg_all || !(streq(u->active_state, "inactive") || u->following[0]) || u->job_id > 0);
314 static void output_units_list(const struct unit_info *unit_infos, unsigned c) {
315 unsigned active_len, sub_len, job_len, n_shown = 0;
316 const struct unit_info *u;
318 active_len = sizeof("ACTIVE")-1;
319 sub_len = sizeof("SUB")-1;
320 job_len = sizeof("JOB")-1;
322 for (u = unit_infos; u < unit_infos + c; u++) {
323 if (!output_show_unit(u))
326 active_len = MAX(active_len, strlen(u->active_state));
327 sub_len = MAX(sub_len, strlen(u->sub_state));
329 job_len = MAX(job_len, strlen(u->job_type));
333 printf("%-25s %-6s %-*s %-*s %-*s", "UNIT", "LOAD",
334 active_len, "ACTIVE", sub_len, "SUB", job_len, "JOB");
335 if (columns() >= 80+12 || arg_full || !arg_no_pager)
336 printf(" %s\n", "DESCRIPTION");
341 for (u = unit_infos; u < unit_infos + c; u++) {
344 const char *on_loaded, *off_loaded;
345 const char *on_active, *off_active;
347 if (!output_show_unit(u))
352 if (!streq(u->load_state, "loaded") &&
353 !streq(u->load_state, "banned")) {
354 on_loaded = ansi_highlight(true);
355 off_loaded = ansi_highlight(false);
357 on_loaded = off_loaded = "";
359 if (streq(u->active_state, "failed")) {
360 on_active = ansi_highlight(true);
361 off_active = ansi_highlight(false);
363 on_active = off_active = "";
365 e = arg_full ? NULL : ellipsize(u->id, 25, 33);
367 printf("%-25s %s%-6s%s %s%-*s %-*s%s%n",
369 on_loaded, u->load_state, off_loaded,
370 on_active, active_len, u->active_state,
371 sub_len, u->sub_state, off_active,
376 a -= strlen(on_loaded) + strlen(off_loaded);
377 a -= strlen(on_active) + strlen(off_active);
380 printf(" %-*s", job_len, u->job_type);
384 if (a + b + 1 < columns()) {
386 printf(" %-*s", job_len, "");
388 if (arg_full || !arg_no_pager)
389 printf(" %s", u->description);
391 printf(" %.*s", columns() - a - b - 1, u->description);
398 printf("\nLOAD = Reflects whether the unit definition was properly loaded.\n"
399 "ACTIVE = The high-level unit activation state, i.e. generalization of SUB.\n"
400 "SUB = The low-level unit activation state, values depend on unit type.\n"
401 "JOB = Pending job for the unit.\n");
404 printf("\n%u units listed.\n", n_shown);
406 printf("\n%u units listed. Pass --all to see inactive units, too.\n", n_shown);
410 static int list_units(DBusConnection *bus, char **args) {
411 DBusMessage *m = NULL, *reply = NULL;
414 DBusMessageIter iter, sub, sub2;
415 unsigned c = 0, n_units = 0;
416 struct unit_info *unit_infos = NULL;
418 dbus_error_init(&error);
422 pager_open_if_enabled();
424 if (!(m = dbus_message_new_method_call(
425 "org.freedesktop.systemd1",
426 "/org/freedesktop/systemd1",
427 "org.freedesktop.systemd1.Manager",
429 log_error("Could not allocate message.");
433 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
434 log_error("Failed to issue method call: %s", bus_error_message(&error));
439 if (!dbus_message_iter_init(reply, &iter) ||
440 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
441 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
442 log_error("Failed to parse reply.");
447 dbus_message_iter_recurse(&iter, &sub);
449 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
452 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
453 log_error("Failed to parse reply.");
461 n_units = MAX(2*c, 16);
462 w = realloc(unit_infos, sizeof(struct unit_info) * n_units);
465 log_error("Failed to allocate unit array.");
475 dbus_message_iter_recurse(&sub, &sub2);
477 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->id, true) < 0 ||
478 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->description, true) < 0 ||
479 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->load_state, true) < 0 ||
480 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->active_state, true) < 0 ||
481 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->sub_state, true) < 0 ||
482 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->following, true) < 0 ||
483 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &u->unit_path, true) < 0 ||
484 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &u->job_id, true) < 0 ||
485 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->job_type, true) < 0 ||
486 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &u->job_path, false) < 0) {
487 log_error("Failed to parse reply.");
492 dbus_message_iter_next(&sub);
497 qsort(unit_infos, c, sizeof(struct unit_info), compare_unit_info);
498 output_units_list(unit_infos, c);
505 dbus_message_unref(m);
508 dbus_message_unref(reply);
512 dbus_error_free(&error);
517 static int compare_unit_file_list(const void *a, const void *b) {
519 const UnitFileList *u = a, *v = b;
521 d1 = strrchr(u->path, '.');
522 d2 = strrchr(v->path, '.');
527 r = strcasecmp(d1, d2);
532 return strcasecmp(file_name_from_path(u->path), file_name_from_path(v->path));
535 static bool output_show_unit_file(const UnitFileList *u) {
538 return !arg_type || ((dot = strrchr(u->path, '.')) && streq(dot+1, arg_type));
541 static void output_unit_file_list(const UnitFileList *units, unsigned c) {
542 unsigned n_shown = 0;
543 const UnitFileList *u;
546 printf("%-25s %-6s\n", "UNIT FILE", "STATE");
548 for (u = units; u < units + c; u++) {
550 const char *on, *off;
553 if (!output_show_unit_file(u))
558 if (u->state == UNIT_FILE_MASKED ||
559 u->state == UNIT_FILE_MASKED_RUNTIME ||
560 u->state == UNIT_FILE_DISABLED) {
561 on = ansi_highlight(true);
562 off = ansi_highlight(false);
563 } else if (u->state == UNIT_FILE_ENABLED) {
564 on = ansi_highlight_green(true);
565 off = ansi_highlight_green(false);
569 id = file_name_from_path(u->path);
571 e = arg_full ? NULL : ellipsize(id, 25, 33);
573 printf("%-25s %s%-6s%s\n",
575 on, unit_file_state_to_string(u->state), off);
581 printf("\n%u unit files listed.\n", n_shown);
584 static int list_unit_files(DBusConnection *bus, char **args) {
585 DBusMessage *m = NULL, *reply = NULL;
588 DBusMessageIter iter, sub, sub2;
589 unsigned c = 0, n_units = 0;
590 UnitFileList *units = NULL;
592 dbus_error_init(&error);
596 pager_open_if_enabled();
603 h = hashmap_new(string_hash_func, string_compare_func);
605 log_error("Out of memory");
609 r = unit_file_get_list(arg_scope, arg_root, h);
611 log_error("Failed to get unit file list: %s", strerror(-r));
615 n_units = hashmap_size(h);
616 units = new(UnitFileList, n_units);
618 unit_file_list_free(h);
619 log_error("Out of memory");
623 HASHMAP_FOREACH(u, h, i) {
624 memcpy(units + c++, u, sizeof(UnitFileList));
630 m = dbus_message_new_method_call(
631 "org.freedesktop.systemd1",
632 "/org/freedesktop/systemd1",
633 "org.freedesktop.systemd1.Manager",
636 log_error("Could not allocate message.");
640 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
642 log_error("Failed to issue method call: %s", bus_error_message(&error));
647 if (!dbus_message_iter_init(reply, &iter) ||
648 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
649 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
650 log_error("Failed to parse reply.");
655 dbus_message_iter_recurse(&iter, &sub);
657 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
661 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
662 log_error("Failed to parse reply.");
670 n_units = MAX(2*c, 16);
671 w = realloc(units, sizeof(struct UnitFileList) * n_units);
674 log_error("Failed to allocate unit array.");
684 dbus_message_iter_recurse(&sub, &sub2);
686 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->path, true) < 0 ||
687 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, false) < 0) {
688 log_error("Failed to parse reply.");
693 u->state = unit_file_state_from_string(state);
695 dbus_message_iter_next(&sub);
701 qsort(units, c, sizeof(UnitFileList), compare_unit_file_list);
702 output_unit_file_list(units, c);
709 dbus_message_unref(m);
712 dbus_message_unref(reply);
716 dbus_error_free(&error);
721 static int dot_one_property(const char *name, const char *prop, DBusMessageIter *iter) {
722 static const char * const colors[] = {
723 "Requires", "[color=\"black\"]",
724 "RequiresOverridable", "[color=\"black\"]",
725 "Requisite", "[color=\"darkblue\"]",
726 "RequisiteOverridable", "[color=\"darkblue\"]",
727 "Wants", "[color=\"darkgrey\"]",
728 "Conflicts", "[color=\"red\"]",
729 "ConflictedBy", "[color=\"red\"]",
730 "After", "[color=\"green\"]"
733 const char *c = NULL;
740 for (i = 0; i < ELEMENTSOF(colors); i += 2)
741 if (streq(colors[i], prop)) {
749 if (arg_dot != DOT_ALL)
750 if ((arg_dot == DOT_ORDER) != streq(prop, "After"))
753 switch (dbus_message_iter_get_arg_type(iter)) {
755 case DBUS_TYPE_ARRAY:
757 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
760 dbus_message_iter_recurse(iter, &sub);
762 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
765 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
766 dbus_message_iter_get_basic(&sub, &s);
767 printf("\t\"%s\"->\"%s\" %s;\n", name, s, c);
769 dbus_message_iter_next(&sub);
779 static int dot_one(DBusConnection *bus, const char *name, const char *path) {
780 DBusMessage *m = NULL, *reply = NULL;
781 const char *interface = "org.freedesktop.systemd1.Unit";
784 DBusMessageIter iter, sub, sub2, sub3;
789 dbus_error_init(&error);
791 if (!(m = dbus_message_new_method_call(
792 "org.freedesktop.systemd1",
794 "org.freedesktop.DBus.Properties",
796 log_error("Could not allocate message.");
801 if (!dbus_message_append_args(m,
802 DBUS_TYPE_STRING, &interface,
803 DBUS_TYPE_INVALID)) {
804 log_error("Could not append arguments to message.");
809 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
810 log_error("Failed to issue method call: %s", bus_error_message(&error));
815 if (!dbus_message_iter_init(reply, &iter) ||
816 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
817 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
818 log_error("Failed to parse reply.");
823 dbus_message_iter_recurse(&iter, &sub);
825 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
828 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
829 log_error("Failed to parse reply.");
834 dbus_message_iter_recurse(&sub, &sub2);
836 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &prop, true) < 0) {
837 log_error("Failed to parse reply.");
842 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
843 log_error("Failed to parse reply.");
848 dbus_message_iter_recurse(&sub2, &sub3);
850 if (dot_one_property(name, prop, &sub3)) {
851 log_error("Failed to parse reply.");
856 dbus_message_iter_next(&sub);
863 dbus_message_unref(m);
866 dbus_message_unref(reply);
868 dbus_error_free(&error);
873 static int dot(DBusConnection *bus, char **args) {
874 DBusMessage *m = NULL, *reply = NULL;
877 DBusMessageIter iter, sub, sub2;
879 dbus_error_init(&error);
883 if (!(m = dbus_message_new_method_call(
884 "org.freedesktop.systemd1",
885 "/org/freedesktop/systemd1",
886 "org.freedesktop.systemd1.Manager",
888 log_error("Could not allocate message.");
892 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
893 log_error("Failed to issue method call: %s", bus_error_message(&error));
898 if (!dbus_message_iter_init(reply, &iter) ||
899 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
900 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
901 log_error("Failed to parse reply.");
906 printf("digraph systemd {\n");
908 dbus_message_iter_recurse(&iter, &sub);
909 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
910 const char *id, *description, *load_state, *active_state, *sub_state, *following, *unit_path;
912 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
913 log_error("Failed to parse reply.");
918 dbus_message_iter_recurse(&sub, &sub2);
920 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
921 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
922 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
923 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
924 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
925 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &following, true) < 0 ||
926 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, true) < 0) {
927 log_error("Failed to parse reply.");
932 if ((r = dot_one(bus, id, unit_path)) < 0)
935 /* printf("\t\"%s\";\n", id); */
936 dbus_message_iter_next(&sub);
941 log_info(" Color legend: black = Requires\n"
942 " dark blue = Requisite\n"
943 " dark grey = Wants\n"
948 log_notice("-- You probably want to process this output with graphviz' dot tool.\n"
949 "-- Try a shell pipeline like 'systemctl dot | dot -Tsvg > systemd.svg'!\n");
955 dbus_message_unref(m);
958 dbus_message_unref(reply);
960 dbus_error_free(&error);
965 static int list_jobs(DBusConnection *bus, char **args) {
966 DBusMessage *m = NULL, *reply = NULL;
969 DBusMessageIter iter, sub, sub2;
972 dbus_error_init(&error);
976 pager_open_if_enabled();
978 if (!(m = dbus_message_new_method_call(
979 "org.freedesktop.systemd1",
980 "/org/freedesktop/systemd1",
981 "org.freedesktop.systemd1.Manager",
983 log_error("Could not allocate message.");
987 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
988 log_error("Failed to issue method call: %s", bus_error_message(&error));
993 if (!dbus_message_iter_init(reply, &iter) ||
994 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
995 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
996 log_error("Failed to parse reply.");
1001 dbus_message_iter_recurse(&iter, &sub);
1004 printf("%4s %-25s %-15s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
1006 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1007 const char *name, *type, *state, *job_path, *unit_path;
1011 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
1012 log_error("Failed to parse reply.");
1017 dbus_message_iter_recurse(&sub, &sub2);
1019 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
1020 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
1021 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
1022 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
1023 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
1024 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
1025 log_error("Failed to parse reply.");
1030 e = arg_full ? NULL : ellipsize(name, 25, 33);
1031 printf("%4u %-25s %-15s %-7s\n", id, e ? e : name, type, state);
1036 dbus_message_iter_next(&sub);
1040 printf("\n%u jobs listed.\n", k);
1046 dbus_message_unref(m);
1049 dbus_message_unref(reply);
1051 dbus_error_free(&error);
1056 static int load_unit(DBusConnection *bus, char **args) {
1057 DBusMessage *m = NULL, *reply = NULL;
1062 dbus_error_init(&error);
1067 STRV_FOREACH(name, args+1) {
1069 if (!(m = dbus_message_new_method_call(
1070 "org.freedesktop.systemd1",
1071 "/org/freedesktop/systemd1",
1072 "org.freedesktop.systemd1.Manager",
1074 log_error("Could not allocate message.");
1079 if (!dbus_message_append_args(m,
1080 DBUS_TYPE_STRING, name,
1081 DBUS_TYPE_INVALID)) {
1082 log_error("Could not append arguments to message.");
1087 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1088 log_error("Failed to issue method call: %s", bus_error_message(&error));
1093 dbus_message_unref(m);
1094 dbus_message_unref(reply);
1103 dbus_message_unref(m);
1106 dbus_message_unref(reply);
1108 dbus_error_free(&error);
1113 static int cancel_job(DBusConnection *bus, char **args) {
1114 DBusMessage *m = NULL, *reply = NULL;
1119 dbus_error_init(&error);
1124 if (strv_length(args) <= 1)
1125 return daemon_reload(bus, args);
1127 STRV_FOREACH(name, args+1) {
1131 if (!(m = dbus_message_new_method_call(
1132 "org.freedesktop.systemd1",
1133 "/org/freedesktop/systemd1",
1134 "org.freedesktop.systemd1.Manager",
1136 log_error("Could not allocate message.");
1141 if ((r = safe_atou(*name, &id)) < 0) {
1142 log_error("Failed to parse job id: %s", strerror(-r));
1146 assert_cc(sizeof(uint32_t) == sizeof(id));
1147 if (!dbus_message_append_args(m,
1148 DBUS_TYPE_UINT32, &id,
1149 DBUS_TYPE_INVALID)) {
1150 log_error("Could not append arguments to message.");
1155 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1156 log_error("Failed to issue method call: %s", bus_error_message(&error));
1161 if (!dbus_message_get_args(reply, &error,
1162 DBUS_TYPE_OBJECT_PATH, &path,
1163 DBUS_TYPE_INVALID)) {
1164 log_error("Failed to parse reply: %s", bus_error_message(&error));
1169 dbus_message_unref(m);
1170 if (!(m = dbus_message_new_method_call(
1171 "org.freedesktop.systemd1",
1173 "org.freedesktop.systemd1.Job",
1175 log_error("Could not allocate message.");
1180 dbus_message_unref(reply);
1181 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1182 log_error("Failed to issue method call: %s", bus_error_message(&error));
1187 dbus_message_unref(m);
1188 dbus_message_unref(reply);
1196 dbus_message_unref(m);
1199 dbus_message_unref(reply);
1201 dbus_error_free(&error);
1206 static bool need_daemon_reload(DBusConnection *bus, const char *unit) {
1207 DBusMessage *m = NULL, *reply = NULL;
1208 dbus_bool_t b = FALSE;
1209 DBusMessageIter iter, sub;
1211 *interface = "org.freedesktop.systemd1.Unit",
1212 *property = "NeedDaemonReload",
1215 /* We ignore all errors here, since this is used to show a warning only */
1217 if (!(m = dbus_message_new_method_call(
1218 "org.freedesktop.systemd1",
1219 "/org/freedesktop/systemd1",
1220 "org.freedesktop.systemd1.Manager",
1224 if (!dbus_message_append_args(m,
1225 DBUS_TYPE_STRING, &unit,
1229 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
1232 if (!dbus_message_get_args(reply, NULL,
1233 DBUS_TYPE_OBJECT_PATH, &path,
1237 dbus_message_unref(m);
1238 if (!(m = dbus_message_new_method_call(
1239 "org.freedesktop.systemd1",
1241 "org.freedesktop.DBus.Properties",
1245 if (!dbus_message_append_args(m,
1246 DBUS_TYPE_STRING, &interface,
1247 DBUS_TYPE_STRING, &property,
1248 DBUS_TYPE_INVALID)) {
1252 dbus_message_unref(reply);
1253 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
1256 if (!dbus_message_iter_init(reply, &iter) ||
1257 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
1260 dbus_message_iter_recurse(&iter, &sub);
1262 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_BOOLEAN)
1265 dbus_message_iter_get_basic(&sub, &b);
1269 dbus_message_unref(m);
1272 dbus_message_unref(reply);
1277 typedef struct WaitData {
1282 static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
1290 dbus_error_init(&error);
1292 log_debug("Got D-Bus request: %s.%s() on %s",
1293 dbus_message_get_interface(message),
1294 dbus_message_get_member(message),
1295 dbus_message_get_path(message));
1297 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
1298 log_error("Warning! D-Bus connection terminated.");
1299 dbus_connection_close(connection);
1301 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
1303 const char *path, *result;
1304 dbus_bool_t success = true;
1306 if (dbus_message_get_args(message, &error,
1307 DBUS_TYPE_UINT32, &id,
1308 DBUS_TYPE_OBJECT_PATH, &path,
1309 DBUS_TYPE_STRING, &result,
1310 DBUS_TYPE_INVALID)) {
1313 if ((p = set_remove(d->set, (char*) path)))
1317 d->result = strdup(result);
1322 dbus_error_free(&error);
1324 if (dbus_message_get_args(message, &error,
1325 DBUS_TYPE_UINT32, &id,
1326 DBUS_TYPE_OBJECT_PATH, &path,
1327 DBUS_TYPE_BOOLEAN, &success,
1328 DBUS_TYPE_INVALID)) {
1331 /* Compatibility with older systemd versions <
1332 * 19 during upgrades. This should be dropped
1335 if ((p = set_remove(d->set, (char*) path)))
1339 d->result = strdup("failed");
1345 log_error("Failed to parse message: %s", bus_error_message(&error));
1349 dbus_error_free(&error);
1350 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1353 static int enable_wait_for_jobs(DBusConnection *bus) {
1361 dbus_error_init(&error);
1362 dbus_bus_add_match(bus,
1364 "sender='org.freedesktop.systemd1',"
1365 "interface='org.freedesktop.systemd1.Manager',"
1366 "member='JobRemoved',"
1367 "path='/org/freedesktop/systemd1'",
1370 if (dbus_error_is_set(&error)) {
1371 log_error("Failed to add match: %s", bus_error_message(&error));
1372 dbus_error_free(&error);
1376 /* This is slightly dirty, since we don't undo the match registrations. */
1380 static int wait_for_jobs(DBusConnection *bus, Set *s) {
1390 if (!dbus_connection_add_filter(bus, wait_filter, &d, NULL)) {
1391 log_error("Failed to add filter.");
1396 while (!set_isempty(s) &&
1397 dbus_connection_read_write_dispatch(bus, -1))
1400 if (!arg_quiet && d.result) {
1401 if (streq(d.result, "timeout"))
1402 log_error("Job timed out.");
1403 else if (streq(d.result, "canceled"))
1404 log_error("Job canceled.");
1405 else if (streq(d.result, "dependency"))
1406 log_error("A dependency job failed. See system logs for details.");
1407 else if (!streq(d.result, "done") && !streq(d.result, "skipped"))
1408 log_error("Job failed. See system logs and 'systemctl status' for details.");
1411 if (streq_ptr(d.result, "timeout"))
1413 else if (streq_ptr(d.result, "canceled"))
1415 else if (!streq_ptr(d.result, "done") && !streq_ptr(d.result, "skipped"))
1423 /* This is slightly dirty, since we don't undo the filter registration. */
1428 static int start_unit_one(
1429 DBusConnection *bus,
1436 DBusMessage *m = NULL, *reply = NULL;
1445 assert(arg_no_block || s);
1447 if (!(m = dbus_message_new_method_call(
1448 "org.freedesktop.systemd1",
1449 "/org/freedesktop/systemd1",
1450 "org.freedesktop.systemd1.Manager",
1452 log_error("Could not allocate message.");
1457 if (!dbus_message_append_args(m,
1458 DBUS_TYPE_STRING, &name,
1459 DBUS_TYPE_STRING, &mode,
1460 DBUS_TYPE_INVALID)) {
1461 log_error("Could not append arguments to message.");
1466 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, error))) {
1468 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(error)) {
1469 /* There's always a fallback possible for
1470 * legacy actions. */
1475 log_error("Failed to issue method call: %s", bus_error_message(error));
1480 if (!dbus_message_get_args(reply, error,
1481 DBUS_TYPE_OBJECT_PATH, &path,
1482 DBUS_TYPE_INVALID)) {
1483 log_error("Failed to parse reply: %s", bus_error_message(error));
1488 if (need_daemon_reload(bus, name))
1489 log_warning("Warning: Unit file of created job changed on disk, 'systemctl %s daemon-reload' recommended.",
1490 arg_scope == UNIT_FILE_SYSTEM ? "--system" : "--user");
1492 if (!arg_no_block) {
1495 if (!(p = strdup(path))) {
1496 log_error("Failed to duplicate path.");
1501 if ((r = set_put(s, p)) < 0) {
1503 log_error("Failed to add path to set.");
1512 dbus_message_unref(m);
1515 dbus_message_unref(reply);
1520 static enum action verb_to_action(const char *verb) {
1521 if (streq(verb, "halt"))
1523 else if (streq(verb, "poweroff"))
1524 return ACTION_POWEROFF;
1525 else if (streq(verb, "reboot"))
1526 return ACTION_REBOOT;
1527 else if (streq(verb, "kexec"))
1528 return ACTION_KEXEC;
1529 else if (streq(verb, "rescue"))
1530 return ACTION_RESCUE;
1531 else if (streq(verb, "emergency"))
1532 return ACTION_EMERGENCY;
1533 else if (streq(verb, "default"))
1534 return ACTION_DEFAULT;
1535 else if (streq(verb, "exit"))
1538 return ACTION_INVALID;
1541 static int start_unit(DBusConnection *bus, char **args) {
1543 static const char * const table[_ACTION_MAX] = {
1544 [ACTION_HALT] = SPECIAL_HALT_TARGET,
1545 [ACTION_POWEROFF] = SPECIAL_POWEROFF_TARGET,
1546 [ACTION_REBOOT] = SPECIAL_REBOOT_TARGET,
1547 [ACTION_KEXEC] = SPECIAL_KEXEC_TARGET,
1548 [ACTION_RUNLEVEL2] = SPECIAL_RUNLEVEL2_TARGET,
1549 [ACTION_RUNLEVEL3] = SPECIAL_RUNLEVEL3_TARGET,
1550 [ACTION_RUNLEVEL4] = SPECIAL_RUNLEVEL4_TARGET,
1551 [ACTION_RUNLEVEL5] = SPECIAL_RUNLEVEL5_TARGET,
1552 [ACTION_RESCUE] = SPECIAL_RESCUE_TARGET,
1553 [ACTION_EMERGENCY] = SPECIAL_EMERGENCY_TARGET,
1554 [ACTION_DEFAULT] = SPECIAL_DEFAULT_TARGET,
1555 [ACTION_EXIT] = SPECIAL_EXIT_TARGET
1559 const char *method, *mode, *one_name;
1564 dbus_error_init(&error);
1568 agent_open_if_enabled();
1570 if (arg_action == ACTION_SYSTEMCTL) {
1572 streq(args[0], "stop") ||
1573 streq(args[0], "condstop") ? "StopUnit" :
1574 streq(args[0], "reload") ? "ReloadUnit" :
1575 streq(args[0], "restart") ? "RestartUnit" :
1577 streq(args[0], "try-restart") ||
1578 streq(args[0], "condrestart") ? "TryRestartUnit" :
1580 streq(args[0], "reload-or-restart") ? "ReloadOrRestartUnit" :
1582 streq(args[0], "reload-or-try-restart") ||
1583 streq(args[0], "condreload") ||
1585 streq(args[0], "force-reload") ? "ReloadOrTryRestartUnit" :
1589 (streq(args[0], "isolate") ||
1590 streq(args[0], "rescue") ||
1591 streq(args[0], "emergency")) ? "isolate" : arg_job_mode;
1593 one_name = table[verb_to_action(args[0])];
1596 assert(arg_action < ELEMENTSOF(table));
1597 assert(table[arg_action]);
1599 method = "StartUnit";
1601 mode = (arg_action == ACTION_EMERGENCY ||
1602 arg_action == ACTION_RESCUE ||
1603 arg_action == ACTION_RUNLEVEL2 ||
1604 arg_action == ACTION_RUNLEVEL3 ||
1605 arg_action == ACTION_RUNLEVEL4 ||
1606 arg_action == ACTION_RUNLEVEL5) ? "isolate" : "replace";
1608 one_name = table[arg_action];
1611 if (!arg_no_block) {
1612 if ((ret = enable_wait_for_jobs(bus)) < 0) {
1613 log_error("Could not watch jobs: %s", strerror(-ret));
1617 if (!(s = set_new(string_hash_func, string_compare_func))) {
1618 log_error("Failed to allocate set.");
1625 if ((ret = start_unit_one(bus, method, one_name, mode, &error, s)) <= 0)
1628 STRV_FOREACH(name, args+1)
1629 if ((r = start_unit_one(bus, method, *name, mode, &error, s)) != 0) {
1630 ret = translate_bus_error_to_exit_status(r, &error);
1631 dbus_error_free(&error);
1636 if ((r = wait_for_jobs(bus, s)) < 0) {
1645 dbus_error_free(&error);
1650 static int start_special(DBusConnection *bus, char **args) {
1657 (streq(args[0], "halt") ||
1658 streq(args[0], "poweroff") ||
1659 streq(args[0], "reboot") ||
1660 streq(args[0], "kexec") ||
1661 streq(args[0], "exit")))
1662 return daemon_reload(bus, args);
1664 r = start_unit(bus, args);
1667 warn_wall(verb_to_action(args[0]));
1672 static int check_unit(DBusConnection *bus, char **args) {
1673 DBusMessage *m = NULL, *reply = NULL;
1675 *interface = "org.freedesktop.systemd1.Unit",
1676 *property = "ActiveState";
1677 int r = 3; /* According to LSB: "program is not running" */
1684 dbus_error_init(&error);
1686 STRV_FOREACH(name, args+1) {
1687 const char *path = NULL;
1689 DBusMessageIter iter, sub;
1691 if (!(m = dbus_message_new_method_call(
1692 "org.freedesktop.systemd1",
1693 "/org/freedesktop/systemd1",
1694 "org.freedesktop.systemd1.Manager",
1696 log_error("Could not allocate message.");
1701 if (!dbus_message_append_args(m,
1702 DBUS_TYPE_STRING, name,
1703 DBUS_TYPE_INVALID)) {
1704 log_error("Could not append arguments to message.");
1709 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1711 /* Hmm, cannot figure out anything about this unit... */
1715 dbus_error_free(&error);
1716 dbus_message_unref(m);
1721 if (!dbus_message_get_args(reply, &error,
1722 DBUS_TYPE_OBJECT_PATH, &path,
1723 DBUS_TYPE_INVALID)) {
1724 log_error("Failed to parse reply: %s", bus_error_message(&error));
1729 dbus_message_unref(m);
1730 if (!(m = dbus_message_new_method_call(
1731 "org.freedesktop.systemd1",
1733 "org.freedesktop.DBus.Properties",
1735 log_error("Could not allocate message.");
1740 if (!dbus_message_append_args(m,
1741 DBUS_TYPE_STRING, &interface,
1742 DBUS_TYPE_STRING, &property,
1743 DBUS_TYPE_INVALID)) {
1744 log_error("Could not append arguments to message.");
1749 dbus_message_unref(reply);
1750 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1751 log_error("Failed to issue method call: %s", bus_error_message(&error));
1756 if (!dbus_message_iter_init(reply, &iter) ||
1757 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1758 log_error("Failed to parse reply.");
1763 dbus_message_iter_recurse(&iter, &sub);
1765 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1766 log_error("Failed to parse reply.");
1771 dbus_message_iter_get_basic(&sub, &state);
1776 if (streq(state, "active") || streq(state, "reloading"))
1779 dbus_message_unref(m);
1780 dbus_message_unref(reply);
1786 dbus_message_unref(m);
1789 dbus_message_unref(reply);
1791 dbus_error_free(&error);
1796 static int kill_unit(DBusConnection *bus, char **args) {
1797 DBusMessage *m = NULL, *reply = NULL;
1805 dbus_error_init(&error);
1808 arg_kill_who = "all";
1811 arg_kill_mode = streq(arg_kill_who, "all") ? "control-group" : "process";
1813 STRV_FOREACH(name, args+1) {
1815 if (!(m = dbus_message_new_method_call(
1816 "org.freedesktop.systemd1",
1817 "/org/freedesktop/systemd1",
1818 "org.freedesktop.systemd1.Manager",
1820 log_error("Could not allocate message.");
1825 if (!dbus_message_append_args(m,
1826 DBUS_TYPE_STRING, name,
1827 DBUS_TYPE_STRING, &arg_kill_who,
1828 DBUS_TYPE_STRING, &arg_kill_mode,
1829 DBUS_TYPE_INT32, &arg_signal,
1830 DBUS_TYPE_INVALID)) {
1831 log_error("Could not append arguments to message.");
1836 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1837 log_error("Failed to issue method call: %s", bus_error_message(&error));
1838 dbus_error_free(&error);
1842 dbus_message_unref(m);
1845 dbus_message_unref(reply);
1851 dbus_message_unref(m);
1854 dbus_message_unref(reply);
1856 dbus_error_free(&error);
1861 typedef struct ExecStatusInfo {
1869 usec_t start_timestamp;
1870 usec_t exit_timestamp;
1875 LIST_FIELDS(struct ExecStatusInfo, exec);
1878 static void exec_status_info_free(ExecStatusInfo *i) {
1887 static int exec_status_info_deserialize(DBusMessageIter *sub, ExecStatusInfo *i) {
1888 uint64_t start_timestamp, exit_timestamp, start_timestamp_monotonic, exit_timestamp_monotonic;
1889 DBusMessageIter sub2, sub3;
1893 int32_t code, status;
1899 if (dbus_message_iter_get_arg_type(sub) != DBUS_TYPE_STRUCT)
1902 dbus_message_iter_recurse(sub, &sub2);
1904 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
1907 if (!(i->path = strdup(path)))
1910 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
1911 dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
1915 dbus_message_iter_recurse(&sub2, &sub3);
1916 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1917 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1918 dbus_message_iter_next(&sub3);
1923 if (!(i->argv = new0(char*, n+1)))
1927 dbus_message_iter_recurse(&sub2, &sub3);
1928 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1931 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1932 dbus_message_iter_get_basic(&sub3, &s);
1933 dbus_message_iter_next(&sub3);
1935 if (!(i->argv[n++] = strdup(s)))
1939 if (!dbus_message_iter_next(&sub2) ||
1940 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, true) < 0 ||
1941 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp, true) < 0 ||
1942 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp_monotonic, true) < 0 ||
1943 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp, true) < 0 ||
1944 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp_monotonic, true) < 0 ||
1945 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) < 0 ||
1946 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) < 0 ||
1947 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) < 0)
1951 i->start_timestamp = (usec_t) start_timestamp;
1952 i->exit_timestamp = (usec_t) exit_timestamp;
1953 i->pid = (pid_t) pid;
1960 typedef struct UnitStatusInfo {
1962 const char *load_state;
1963 const char *active_state;
1964 const char *sub_state;
1965 const char *unit_file_state;
1967 const char *description;
1968 const char *following;
1971 const char *default_control_group;
1973 const char *load_error;
1975 usec_t inactive_exit_timestamp;
1976 usec_t active_enter_timestamp;
1977 usec_t active_exit_timestamp;
1978 usec_t inactive_enter_timestamp;
1980 bool need_daemon_reload;
1985 const char *status_text;
1987 #ifdef HAVE_SYSV_COMPAT
1991 usec_t start_timestamp;
1992 usec_t exit_timestamp;
1994 int exit_code, exit_status;
1996 usec_t condition_timestamp;
1997 bool condition_result;
2000 unsigned n_accepted;
2001 unsigned n_connections;
2005 const char *sysfs_path;
2007 /* Mount, Automount */
2013 LIST_HEAD(ExecStatusInfo, exec);
2016 static void print_status_info(UnitStatusInfo *i) {
2018 const char *on, *off, *ss;
2020 char since1[FORMAT_TIMESTAMP_PRETTY_MAX], *s1;
2021 char since2[FORMAT_TIMESTAMP_MAX], *s2;
2025 /* This shows pretty information about a unit. See
2026 * print_property() for a low-level property printer */
2028 printf("%s", strna(i->id));
2030 if (i->description && !streq_ptr(i->id, i->description))
2031 printf(" - %s", i->description);
2036 printf("\t Follow: unit currently follows state of %s\n", i->following);
2038 if (streq_ptr(i->load_state, "failed") ||
2039 streq_ptr(i->load_state, "banned")) {
2040 on = ansi_highlight(true);
2041 off = ansi_highlight(false);
2046 printf("\t Loaded: %s%s%s (Reason: %s)\n", on, strna(i->load_state), off, i->load_error);
2047 else if (i->path && i->unit_file_state)
2048 printf("\t Loaded: %s%s%s (%s; %s)\n", on, strna(i->load_state), off, i->path, i->unit_file_state);
2050 printf("\t Loaded: %s%s%s (%s)\n", on, strna(i->load_state), off, i->path);
2052 printf("\t Loaded: %s%s%s\n", on, strna(i->load_state), off);
2054 ss = streq_ptr(i->active_state, i->sub_state) ? NULL : i->sub_state;
2056 if (streq_ptr(i->active_state, "failed")) {
2057 on = ansi_highlight(true);
2058 off = ansi_highlight(false);
2059 } else if (streq_ptr(i->active_state, "active") || streq_ptr(i->active_state, "reloading")) {
2060 on = ansi_highlight_green(true);
2061 off = ansi_highlight_green(false);
2066 printf("\t Active: %s%s (%s)%s",
2068 strna(i->active_state),
2072 printf("\t Active: %s%s%s",
2074 strna(i->active_state),
2077 timestamp = (streq_ptr(i->active_state, "active") ||
2078 streq_ptr(i->active_state, "reloading")) ? i->active_enter_timestamp :
2079 (streq_ptr(i->active_state, "inactive") ||
2080 streq_ptr(i->active_state, "failed")) ? i->inactive_enter_timestamp :
2081 streq_ptr(i->active_state, "activating") ? i->inactive_exit_timestamp :
2082 i->active_exit_timestamp;
2084 s1 = format_timestamp_pretty(since1, sizeof(since1), timestamp);
2085 s2 = format_timestamp(since2, sizeof(since2), timestamp);
2088 printf(" since %s; %s\n", s2, s1);
2090 printf(" since %s\n", s2);
2094 if (!i->condition_result && i->condition_timestamp > 0) {
2095 s1 = format_timestamp_pretty(since1, sizeof(since1), i->condition_timestamp);
2096 s2 = format_timestamp(since2, sizeof(since2), i->condition_timestamp);
2099 printf("\t start condition failed at %s; %s\n", s2, s1);
2101 printf("\t start condition failed at %s\n", s2);
2105 printf("\t Device: %s\n", i->sysfs_path);
2107 printf("\t Where: %s\n", i->where);
2109 printf("\t What: %s\n", i->what);
2112 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
2114 LIST_FOREACH(exec, p, i->exec) {
2118 /* Only show exited processes here */
2122 t = strv_join(p->argv, " ");
2123 printf("\t Process: %u %s=%s ", p->pid, p->name, strna(t));
2126 #ifdef HAVE_SYSV_COMPAT
2128 good = is_clean_exit_lsb(p->code, p->status);
2131 good = is_clean_exit(p->code, p->status);
2134 on = ansi_highlight(true);
2135 off = ansi_highlight(false);
2139 printf("%s(code=%s, ", on, sigchld_code_to_string(p->code));
2141 if (p->code == CLD_EXITED) {
2144 printf("status=%i", p->status);
2146 #ifdef HAVE_SYSV_COMPAT
2147 if ((c = exit_status_to_string(p->status, i->is_sysv ? EXIT_STATUS_LSB : EXIT_STATUS_SYSTEMD)))
2149 if ((c = exit_status_to_string(p->status, EXIT_STATUS_SYSTEMD)))
2154 printf("signal=%s", signal_to_string(p->status));
2156 printf(")%s\n", off);
2160 if (i->main_pid == p->pid &&
2161 i->start_timestamp == p->start_timestamp &&
2162 i->exit_timestamp == p->start_timestamp)
2163 /* Let's not show this twice */
2166 if (p->pid == i->control_pid)
2170 if (i->main_pid > 0 || i->control_pid > 0) {
2173 if (i->main_pid > 0) {
2174 printf("Main PID: %u", (unsigned) i->main_pid);
2178 get_process_name(i->main_pid, &t);
2183 } else if (i->exit_code > 0) {
2184 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
2186 if (i->exit_code == CLD_EXITED) {
2189 printf("status=%i", i->exit_status);
2191 #ifdef HAVE_SYSV_COMPAT
2192 if ((c = exit_status_to_string(i->exit_status, i->is_sysv ? EXIT_STATUS_LSB : EXIT_STATUS_SYSTEMD)))
2194 if ((c = exit_status_to_string(i->exit_status, EXIT_STATUS_SYSTEMD)))
2199 printf("signal=%s", signal_to_string(i->exit_status));
2204 if (i->main_pid > 0 && i->control_pid > 0)
2207 if (i->control_pid > 0) {
2210 printf(" Control: %u", (unsigned) i->control_pid);
2212 get_process_name(i->control_pid, &t);
2223 printf("\t Status: \"%s\"\n", i->status_text);
2225 if (i->default_control_group) {
2228 printf("\t CGroup: %s\n", i->default_control_group);
2230 if (arg_transport != TRANSPORT_SSH) {
2231 if ((c = columns()) > 18)
2236 show_cgroup_by_path(i->default_control_group, "\t\t ", c);
2240 if (i->need_daemon_reload)
2241 printf("\n%sWarning:%s Unit file changed on disk, 'systemctl %s daemon-reload' recommended.\n",
2242 ansi_highlight(true),
2243 ansi_highlight(false),
2244 arg_scope == UNIT_FILE_SYSTEM ? "--system" : "--user");
2247 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
2253 switch (dbus_message_iter_get_arg_type(iter)) {
2255 case DBUS_TYPE_STRING: {
2258 dbus_message_iter_get_basic(iter, &s);
2261 if (streq(name, "Id"))
2263 else if (streq(name, "LoadState"))
2265 else if (streq(name, "ActiveState"))
2266 i->active_state = s;
2267 else if (streq(name, "SubState"))
2269 else if (streq(name, "Description"))
2271 else if (streq(name, "FragmentPath"))
2273 #ifdef HAVE_SYSV_COMPAT
2274 else if (streq(name, "SysVPath")) {
2279 else if (streq(name, "DefaultControlGroup"))
2280 i->default_control_group = s;
2281 else if (streq(name, "StatusText"))
2283 else if (streq(name, "SysFSPath"))
2285 else if (streq(name, "Where"))
2287 else if (streq(name, "What"))
2289 else if (streq(name, "Following"))
2291 else if (streq(name, "UnitFileState"))
2292 i->unit_file_state = s;
2298 case DBUS_TYPE_BOOLEAN: {
2301 dbus_message_iter_get_basic(iter, &b);
2303 if (streq(name, "Accept"))
2305 else if (streq(name, "NeedDaemonReload"))
2306 i->need_daemon_reload = b;
2307 else if (streq(name, "ConditionResult"))
2308 i->condition_result = b;
2313 case DBUS_TYPE_UINT32: {
2316 dbus_message_iter_get_basic(iter, &u);
2318 if (streq(name, "MainPID")) {
2320 i->main_pid = (pid_t) u;
2323 } else if (streq(name, "ControlPID"))
2324 i->control_pid = (pid_t) u;
2325 else if (streq(name, "ExecMainPID")) {
2327 i->main_pid = (pid_t) u;
2328 } else if (streq(name, "NAccepted"))
2330 else if (streq(name, "NConnections"))
2331 i->n_connections = u;
2336 case DBUS_TYPE_INT32: {
2339 dbus_message_iter_get_basic(iter, &j);
2341 if (streq(name, "ExecMainCode"))
2342 i->exit_code = (int) j;
2343 else if (streq(name, "ExecMainStatus"))
2344 i->exit_status = (int) j;
2349 case DBUS_TYPE_UINT64: {
2352 dbus_message_iter_get_basic(iter, &u);
2354 if (streq(name, "ExecMainStartTimestamp"))
2355 i->start_timestamp = (usec_t) u;
2356 else if (streq(name, "ExecMainExitTimestamp"))
2357 i->exit_timestamp = (usec_t) u;
2358 else if (streq(name, "ActiveEnterTimestamp"))
2359 i->active_enter_timestamp = (usec_t) u;
2360 else if (streq(name, "InactiveEnterTimestamp"))
2361 i->inactive_enter_timestamp = (usec_t) u;
2362 else if (streq(name, "InactiveExitTimestamp"))
2363 i->inactive_exit_timestamp = (usec_t) u;
2364 else if (streq(name, "ActiveExitTimestamp"))
2365 i->active_exit_timestamp = (usec_t) u;
2366 else if (streq(name, "ConditionTimestamp"))
2367 i->condition_timestamp = (usec_t) u;
2372 case DBUS_TYPE_ARRAY: {
2374 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT &&
2375 startswith(name, "Exec")) {
2376 DBusMessageIter sub;
2378 dbus_message_iter_recurse(iter, &sub);
2379 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2380 ExecStatusInfo *info;
2383 if (!(info = new0(ExecStatusInfo, 1)))
2386 if (!(info->name = strdup(name))) {
2391 if ((r = exec_status_info_deserialize(&sub, info)) < 0) {
2396 LIST_PREPEND(ExecStatusInfo, exec, i->exec, info);
2398 dbus_message_iter_next(&sub);
2405 case DBUS_TYPE_STRUCT: {
2407 if (streq(name, "LoadError")) {
2408 DBusMessageIter sub;
2409 const char *n, *message;
2412 dbus_message_iter_recurse(iter, &sub);
2414 r = bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &n, true);
2418 r = bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &message, false);
2422 if (!isempty(message))
2423 i->load_error = message;
2433 static int print_property(const char *name, DBusMessageIter *iter) {
2437 /* This is a low-level property printer, see
2438 * print_status_info() for the nicer output */
2440 if (arg_property && !strv_find(arg_property, name))
2443 switch (dbus_message_iter_get_arg_type(iter)) {
2445 case DBUS_TYPE_STRUCT: {
2446 DBusMessageIter sub;
2447 dbus_message_iter_recurse(iter, &sub);
2449 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
2452 dbus_message_iter_get_basic(&sub, &u);
2455 printf("%s=%u\n", name, (unsigned) u);
2457 printf("%s=\n", name);
2460 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
2463 dbus_message_iter_get_basic(&sub, &s);
2465 if (arg_all || s[0])
2466 printf("%s=%s\n", name, s);
2469 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "LoadError")) {
2470 const char *a = NULL, *b = NULL;
2472 if (bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &a, true) >= 0)
2473 bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &b, false);
2475 if (arg_all || !isempty(a) || !isempty(b))
2476 printf("%s=%s \"%s\"\n", name, strempty(a), strempty(b));
2484 case DBUS_TYPE_ARRAY:
2486 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "EnvironmentFiles")) {
2487 DBusMessageIter sub, sub2;
2489 dbus_message_iter_recurse(iter, &sub);
2490 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2494 dbus_message_iter_recurse(&sub, &sub2);
2496 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) >= 0 &&
2497 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, false) >= 0)
2498 printf("EnvironmentFile=%s (ignore_errors=%s)\n", path, yes_no(ignore));
2500 dbus_message_iter_next(&sub);
2505 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
2506 DBusMessageIter sub, sub2;
2508 dbus_message_iter_recurse(iter, &sub);
2509 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2510 const char *type, *path;
2512 dbus_message_iter_recurse(&sub, &sub2);
2514 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
2515 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
2516 printf("%s=%s\n", type, path);
2518 dbus_message_iter_next(&sub);
2523 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
2524 DBusMessageIter sub, sub2;
2526 dbus_message_iter_recurse(iter, &sub);
2527 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2529 uint64_t value, next_elapse;
2531 dbus_message_iter_recurse(&sub, &sub2);
2533 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
2534 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
2535 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
2536 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
2538 printf("%s={ value=%s ; next_elapse=%s }\n",
2540 format_timespan(timespan1, sizeof(timespan1), value),
2541 format_timespan(timespan2, sizeof(timespan2), next_elapse));
2544 dbus_message_iter_next(&sub);
2549 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "ControlGroupAttributes")) {
2550 DBusMessageIter sub, sub2;
2552 dbus_message_iter_recurse(iter, &sub);
2553 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2554 const char *controller, *attr, *value;
2556 dbus_message_iter_recurse(&sub, &sub2);
2558 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &controller, true) >= 0 &&
2559 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &attr, true) >= 0 &&
2560 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &value, false) >= 0) {
2562 printf("ControlGroupAttribute={ controller=%s ; attribute=%s ; value=\"%s\" }\n",
2568 dbus_message_iter_next(&sub);
2573 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
2574 DBusMessageIter sub;
2576 dbus_message_iter_recurse(iter, &sub);
2577 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2578 ExecStatusInfo info;
2581 if (exec_status_info_deserialize(&sub, &info) >= 0) {
2582 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
2585 t = strv_join(info.argv, " ");
2587 printf("%s={ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n",
2591 yes_no(info.ignore),
2592 strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)),
2593 strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)),
2594 (unsigned) info. pid,
2595 sigchld_code_to_string(info.code),
2597 info.code == CLD_EXITED ? "" : "/",
2598 strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status)));
2604 strv_free(info.argv);
2606 dbus_message_iter_next(&sub);
2615 if (generic_print_property(name, iter, arg_all) > 0)
2619 printf("%s=[unprintable]\n", name);
2624 static int show_one(const char *verb, DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
2625 DBusMessage *m = NULL, *reply = NULL;
2626 const char *interface = "";
2629 DBusMessageIter iter, sub, sub2, sub3;
2630 UnitStatusInfo info;
2638 dbus_error_init(&error);
2640 if (!(m = dbus_message_new_method_call(
2641 "org.freedesktop.systemd1",
2643 "org.freedesktop.DBus.Properties",
2645 log_error("Could not allocate message.");
2650 if (!dbus_message_append_args(m,
2651 DBUS_TYPE_STRING, &interface,
2652 DBUS_TYPE_INVALID)) {
2653 log_error("Could not append arguments to message.");
2658 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2659 log_error("Failed to issue method call: %s", bus_error_message(&error));
2664 if (!dbus_message_iter_init(reply, &iter) ||
2665 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
2666 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
2667 log_error("Failed to parse reply.");
2672 dbus_message_iter_recurse(&iter, &sub);
2679 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2682 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
2683 log_error("Failed to parse reply.");
2688 dbus_message_iter_recurse(&sub, &sub2);
2690 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
2691 log_error("Failed to parse reply.");
2696 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
2697 log_error("Failed to parse reply.");
2702 dbus_message_iter_recurse(&sub2, &sub3);
2704 if (show_properties)
2705 r = print_property(name, &sub3);
2707 r = status_property(name, &sub3, &info);
2710 log_error("Failed to parse reply.");
2715 dbus_message_iter_next(&sub);
2720 if (!show_properties)
2721 print_status_info(&info);
2723 if (!streq_ptr(info.active_state, "active") &&
2724 !streq_ptr(info.active_state, "reloading") &&
2725 streq(verb, "status"))
2726 /* According to LSB: "program not running" */
2729 while ((p = info.exec)) {
2730 LIST_REMOVE(ExecStatusInfo, exec, info.exec, p);
2731 exec_status_info_free(p);
2736 dbus_message_unref(m);
2739 dbus_message_unref(reply);
2741 dbus_error_free(&error);
2746 static int show(DBusConnection *bus, char **args) {
2747 DBusMessage *m = NULL, *reply = NULL;
2750 bool show_properties, new_line = false;
2756 dbus_error_init(&error);
2758 show_properties = !streq(args[0], "status");
2760 if (show_properties)
2761 pager_open_if_enabled();
2763 if (show_properties && strv_length(args) <= 1) {
2764 /* If not argument is specified inspect the manager
2767 ret = show_one(args[0], bus, "/org/freedesktop/systemd1", show_properties, &new_line);
2771 STRV_FOREACH(name, args+1) {
2772 const char *path = NULL;
2775 if (safe_atou32(*name, &id) < 0) {
2777 /* Interpret as unit name */
2779 if (!(m = dbus_message_new_method_call(
2780 "org.freedesktop.systemd1",
2781 "/org/freedesktop/systemd1",
2782 "org.freedesktop.systemd1.Manager",
2784 log_error("Could not allocate message.");
2789 if (!dbus_message_append_args(m,
2790 DBUS_TYPE_STRING, name,
2791 DBUS_TYPE_INVALID)) {
2792 log_error("Could not append arguments to message.");
2797 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2799 if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
2800 log_error("Failed to issue method call: %s", bus_error_message(&error));
2805 dbus_error_free(&error);
2807 dbus_message_unref(m);
2808 if (!(m = dbus_message_new_method_call(
2809 "org.freedesktop.systemd1",
2810 "/org/freedesktop/systemd1",
2811 "org.freedesktop.systemd1.Manager",
2813 log_error("Could not allocate message.");
2818 if (!dbus_message_append_args(m,
2819 DBUS_TYPE_STRING, name,
2820 DBUS_TYPE_INVALID)) {
2821 log_error("Could not append arguments to message.");
2826 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2827 log_error("Failed to issue method call: %s", bus_error_message(&error));
2829 if (dbus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT))
2830 ret = 4; /* According to LSB: "program or service status is unknown" */
2837 } else if (show_properties) {
2839 /* Interpret as job id */
2841 if (!(m = dbus_message_new_method_call(
2842 "org.freedesktop.systemd1",
2843 "/org/freedesktop/systemd1",
2844 "org.freedesktop.systemd1.Manager",
2846 log_error("Could not allocate message.");
2851 if (!dbus_message_append_args(m,
2852 DBUS_TYPE_UINT32, &id,
2853 DBUS_TYPE_INVALID)) {
2854 log_error("Could not append arguments to message.");
2859 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2860 log_error("Failed to issue method call: %s", bus_error_message(&error));
2866 /* Interpret as PID */
2868 if (!(m = dbus_message_new_method_call(
2869 "org.freedesktop.systemd1",
2870 "/org/freedesktop/systemd1",
2871 "org.freedesktop.systemd1.Manager",
2873 log_error("Could not allocate message.");
2878 if (!dbus_message_append_args(m,
2879 DBUS_TYPE_UINT32, &id,
2880 DBUS_TYPE_INVALID)) {
2881 log_error("Could not append arguments to message.");
2886 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2887 log_error("Failed to issue method call: %s", bus_error_message(&error));
2893 if (!dbus_message_get_args(reply, &error,
2894 DBUS_TYPE_OBJECT_PATH, &path,
2895 DBUS_TYPE_INVALID)) {
2896 log_error("Failed to parse reply: %s", bus_error_message(&error));
2901 if ((r = show_one(args[0], bus, path, show_properties, &new_line)) != 0)
2904 dbus_message_unref(m);
2905 dbus_message_unref(reply);
2911 dbus_message_unref(m);
2914 dbus_message_unref(reply);
2916 dbus_error_free(&error);
2921 static int dump(DBusConnection *bus, char **args) {
2922 DBusMessage *m = NULL, *reply = NULL;
2927 dbus_error_init(&error);
2929 pager_open_if_enabled();
2931 if (!(m = dbus_message_new_method_call(
2932 "org.freedesktop.systemd1",
2933 "/org/freedesktop/systemd1",
2934 "org.freedesktop.systemd1.Manager",
2936 log_error("Could not allocate message.");
2940 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2941 log_error("Failed to issue method call: %s", bus_error_message(&error));
2946 if (!dbus_message_get_args(reply, &error,
2947 DBUS_TYPE_STRING, &text,
2948 DBUS_TYPE_INVALID)) {
2949 log_error("Failed to parse reply: %s", bus_error_message(&error));
2954 fputs(text, stdout);
2960 dbus_message_unref(m);
2963 dbus_message_unref(reply);
2965 dbus_error_free(&error);
2970 static int snapshot(DBusConnection *bus, char **args) {
2971 DBusMessage *m = NULL, *reply = NULL;
2974 const char *name = "", *path, *id;
2975 dbus_bool_t cleanup = FALSE;
2976 DBusMessageIter iter, sub;
2978 *interface = "org.freedesktop.systemd1.Unit",
2981 dbus_error_init(&error);
2983 if (!(m = dbus_message_new_method_call(
2984 "org.freedesktop.systemd1",
2985 "/org/freedesktop/systemd1",
2986 "org.freedesktop.systemd1.Manager",
2987 "CreateSnapshot"))) {
2988 log_error("Could not allocate message.");
2992 if (strv_length(args) > 1)
2995 if (!dbus_message_append_args(m,
2996 DBUS_TYPE_STRING, &name,
2997 DBUS_TYPE_BOOLEAN, &cleanup,
2998 DBUS_TYPE_INVALID)) {
2999 log_error("Could not append arguments to message.");
3004 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3005 log_error("Failed to issue method call: %s", bus_error_message(&error));
3010 if (!dbus_message_get_args(reply, &error,
3011 DBUS_TYPE_OBJECT_PATH, &path,
3012 DBUS_TYPE_INVALID)) {
3013 log_error("Failed to parse reply: %s", bus_error_message(&error));
3018 dbus_message_unref(m);
3019 if (!(m = dbus_message_new_method_call(
3020 "org.freedesktop.systemd1",
3022 "org.freedesktop.DBus.Properties",
3024 log_error("Could not allocate message.");
3028 if (!dbus_message_append_args(m,
3029 DBUS_TYPE_STRING, &interface,
3030 DBUS_TYPE_STRING, &property,
3031 DBUS_TYPE_INVALID)) {
3032 log_error("Could not append arguments to message.");
3037 dbus_message_unref(reply);
3038 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3039 log_error("Failed to issue method call: %s", bus_error_message(&error));
3044 if (!dbus_message_iter_init(reply, &iter) ||
3045 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
3046 log_error("Failed to parse reply.");
3051 dbus_message_iter_recurse(&iter, &sub);
3053 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
3054 log_error("Failed to parse reply.");
3059 dbus_message_iter_get_basic(&sub, &id);
3067 dbus_message_unref(m);
3070 dbus_message_unref(reply);
3072 dbus_error_free(&error);
3077 static int delete_snapshot(DBusConnection *bus, char **args) {
3078 DBusMessage *m = NULL, *reply = NULL;
3086 dbus_error_init(&error);
3088 STRV_FOREACH(name, args+1) {
3089 const char *path = NULL;
3091 if (!(m = dbus_message_new_method_call(
3092 "org.freedesktop.systemd1",
3093 "/org/freedesktop/systemd1",
3094 "org.freedesktop.systemd1.Manager",
3096 log_error("Could not allocate message.");
3101 if (!dbus_message_append_args(m,
3102 DBUS_TYPE_STRING, name,
3103 DBUS_TYPE_INVALID)) {
3104 log_error("Could not append arguments to message.");
3109 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3110 log_error("Failed to issue method call: %s", bus_error_message(&error));
3115 if (!dbus_message_get_args(reply, &error,
3116 DBUS_TYPE_OBJECT_PATH, &path,
3117 DBUS_TYPE_INVALID)) {
3118 log_error("Failed to parse reply: %s", bus_error_message(&error));
3123 dbus_message_unref(m);
3124 if (!(m = dbus_message_new_method_call(
3125 "org.freedesktop.systemd1",
3127 "org.freedesktop.systemd1.Snapshot",
3129 log_error("Could not allocate message.");
3134 dbus_message_unref(reply);
3135 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3136 log_error("Failed to issue method call: %s", bus_error_message(&error));
3141 dbus_message_unref(m);
3142 dbus_message_unref(reply);
3150 dbus_message_unref(m);
3153 dbus_message_unref(reply);
3155 dbus_error_free(&error);
3160 static int daemon_reload(DBusConnection *bus, char **args) {
3161 DBusMessage *m = NULL, *reply = NULL;
3166 dbus_error_init(&error);
3168 if (arg_action == ACTION_RELOAD)
3170 else if (arg_action == ACTION_REEXEC)
3171 method = "Reexecute";
3173 assert(arg_action == ACTION_SYSTEMCTL);
3176 streq(args[0], "clear-jobs") ||
3177 streq(args[0], "cancel") ? "ClearJobs" :
3178 streq(args[0], "daemon-reexec") ? "Reexecute" :
3179 streq(args[0], "reset-failed") ? "ResetFailed" :
3180 streq(args[0], "halt") ? "Halt" :
3181 streq(args[0], "poweroff") ? "PowerOff" :
3182 streq(args[0], "reboot") ? "Reboot" :
3183 streq(args[0], "kexec") ? "KExec" :
3184 streq(args[0], "exit") ? "Exit" :
3185 /* "daemon-reload" */ "Reload";
3188 if (!(m = dbus_message_new_method_call(
3189 "org.freedesktop.systemd1",
3190 "/org/freedesktop/systemd1",
3191 "org.freedesktop.systemd1.Manager",
3193 log_error("Could not allocate message.");
3197 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3199 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
3200 /* There's always a fallback possible for
3201 * legacy actions. */
3206 if (streq(method, "Reexecute") && dbus_error_has_name(&error, DBUS_ERROR_NO_REPLY)) {
3207 /* On reexecution, we expect a disconnect, not
3213 log_error("Failed to issue method call: %s", bus_error_message(&error));
3222 dbus_message_unref(m);
3225 dbus_message_unref(reply);
3227 dbus_error_free(&error);
3232 static int reset_failed(DBusConnection *bus, char **args) {
3233 DBusMessage *m = NULL, *reply = NULL;
3239 dbus_error_init(&error);
3241 if (strv_length(args) <= 1)
3242 return daemon_reload(bus, args);
3244 STRV_FOREACH(name, args+1) {
3246 if (!(m = dbus_message_new_method_call(
3247 "org.freedesktop.systemd1",
3248 "/org/freedesktop/systemd1",
3249 "org.freedesktop.systemd1.Manager",
3250 "ResetFailedUnit"))) {
3251 log_error("Could not allocate message.");
3256 if (!dbus_message_append_args(m,
3257 DBUS_TYPE_STRING, name,
3258 DBUS_TYPE_INVALID)) {
3259 log_error("Could not append arguments to message.");
3264 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3265 log_error("Failed to issue method call: %s", bus_error_message(&error));
3270 dbus_message_unref(m);
3271 dbus_message_unref(reply);
3279 dbus_message_unref(m);
3282 dbus_message_unref(reply);
3284 dbus_error_free(&error);
3289 static int show_enviroment(DBusConnection *bus, char **args) {
3290 DBusMessage *m = NULL, *reply = NULL;
3292 DBusMessageIter iter, sub, sub2;
3295 *interface = "org.freedesktop.systemd1.Manager",
3296 *property = "Environment";
3298 dbus_error_init(&error);
3300 pager_open_if_enabled();
3302 if (!(m = dbus_message_new_method_call(
3303 "org.freedesktop.systemd1",
3304 "/org/freedesktop/systemd1",
3305 "org.freedesktop.DBus.Properties",
3307 log_error("Could not allocate message.");
3311 if (!dbus_message_append_args(m,
3312 DBUS_TYPE_STRING, &interface,
3313 DBUS_TYPE_STRING, &property,
3314 DBUS_TYPE_INVALID)) {
3315 log_error("Could not append arguments to message.");
3320 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3321 log_error("Failed to issue method call: %s", bus_error_message(&error));
3326 if (!dbus_message_iter_init(reply, &iter) ||
3327 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
3328 log_error("Failed to parse reply.");
3333 dbus_message_iter_recurse(&iter, &sub);
3335 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
3336 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
3337 log_error("Failed to parse reply.");
3342 dbus_message_iter_recurse(&sub, &sub2);
3344 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
3347 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
3348 log_error("Failed to parse reply.");
3353 dbus_message_iter_get_basic(&sub2, &text);
3354 printf("%s\n", text);
3356 dbus_message_iter_next(&sub2);
3363 dbus_message_unref(m);
3366 dbus_message_unref(reply);
3368 dbus_error_free(&error);
3373 static int set_environment(DBusConnection *bus, char **args) {
3374 DBusMessage *m = NULL, *reply = NULL;
3378 DBusMessageIter iter, sub;
3381 dbus_error_init(&error);
3383 method = streq(args[0], "set-environment")
3385 : "UnsetEnvironment";
3387 if (!(m = dbus_message_new_method_call(
3388 "org.freedesktop.systemd1",
3389 "/org/freedesktop/systemd1",
3390 "org.freedesktop.systemd1.Manager",
3393 log_error("Could not allocate message.");
3397 dbus_message_iter_init_append(m, &iter);
3399 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
3400 log_error("Could not append arguments to message.");
3405 STRV_FOREACH(name, args+1)
3406 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, name)) {
3407 log_error("Could not append arguments to message.");
3412 if (!dbus_message_iter_close_container(&iter, &sub)) {
3413 log_error("Could not append arguments to message.");
3418 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3419 log_error("Failed to issue method call: %s", bus_error_message(&error));
3428 dbus_message_unref(m);
3431 dbus_message_unref(reply);
3433 dbus_error_free(&error);
3438 static int enable_sysv_units(char **args) {
3441 #if defined (HAVE_SYSV_COMPAT) && (defined(TARGET_FEDORA) || defined(TARGET_MANDRIVA) || defined(TARGET_SUSE) || defined(TARGET_MEEGO) || defined(TARGET_ALTLINUX))
3442 const char *verb = args[0];
3443 unsigned f = 1, t = 1;
3446 if (arg_scope != UNIT_FILE_SYSTEM)
3449 if (!streq(verb, "enable") &&
3450 !streq(verb, "disable") &&
3451 !streq(verb, "is-enabled"))
3454 /* Processes all SysV units, and reshuffles the array so that
3455 * afterwards only the native units remain */
3458 r = lookup_paths_init(&paths, MANAGER_SYSTEM, false);
3464 for (f = 1; args[f]; f++) {
3467 bool found_native = false, found_sysv;
3469 const char *argv[6] = { "/sbin/chkconfig", NULL, NULL, NULL, NULL };
3470 char **k, *l, *q = NULL;
3477 if (!endswith(name, ".service"))
3480 if (path_is_absolute(name))
3483 STRV_FOREACH(k, paths.unit_path) {
3486 if (!isempty(arg_root))
3487 asprintf(&p, "%s/%s/%s", arg_root, *k, name);
3489 asprintf(&p, "%s/%s", *k, name);
3492 log_error("No memory");
3497 found_native = access(p, F_OK) >= 0;
3508 if (!isempty(arg_root))
3509 asprintf(&p, "%s/" SYSTEM_SYSVINIT_PATH "/%s", arg_root, name);
3511 asprintf(&p, SYSTEM_SYSVINIT_PATH "/%s", name);
3513 log_error("No memory");
3518 p[strlen(p) - sizeof(".service") + 1] = 0;
3519 found_sysv = access(p, F_OK) >= 0;
3526 /* Mark this entry, so that we don't try enabling it as native unit */
3527 args[f] = (char*) "";
3529 log_info("%s is not a native service, redirecting to /sbin/chkconfig.", name);
3531 if (!isempty(arg_root))
3532 argv[c++] = q = strappend("--root=", arg_root);
3534 argv[c++] = file_name_from_path(p);
3536 streq(verb, "enable") ? "on" :
3537 streq(verb, "disable") ? "off" : "--level=5";
3540 l = strv_join((char**)argv, " ");
3542 log_error("No memory.");
3549 log_info("Executing %s", l);
3554 log_error("Failed to fork: %m");
3559 } else if (pid == 0) {
3562 execv(argv[0], (char**) argv);
3563 _exit(EXIT_FAILURE);
3569 j = wait_for_terminate(pid, &status);
3571 log_error("Failed to wait for child: %s", strerror(-r));
3576 if (status.si_code == CLD_EXITED) {
3577 if (streq(verb, "is-enabled")) {
3578 if (status.si_status == 0) {
3587 } else if (status.si_status != 0) {
3598 lookup_paths_free(&paths);
3600 /* Drop all SysV units */
3601 for (f = 1, t = 1; args[f]; f++) {
3603 if (isempty(args[f]))
3606 args[t++] = args[f];
3615 static int enable_unit(DBusConnection *bus, char **args) {
3616 const char *verb = args[0];
3617 UnitFileChange *changes = NULL;
3618 unsigned n_changes = 0, i;
3619 int carries_install_info = -1;
3620 DBusMessage *m = NULL, *reply = NULL;
3624 dbus_error_init(&error);
3626 r = enable_sysv_units(args);
3630 if (!bus || avoid_bus()) {
3631 if (streq(verb, "enable")) {
3632 r = unit_file_enable(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3633 carries_install_info = r;
3634 } else if (streq(verb, "disable"))
3635 r = unit_file_disable(arg_scope, arg_runtime, arg_root, args+1, &changes, &n_changes);
3636 else if (streq(verb, "reenable")) {
3637 r = unit_file_reenable(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3638 carries_install_info = r;
3639 } else if (streq(verb, "link"))
3640 r = unit_file_link(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3641 else if (streq(verb, "preset")) {
3642 r = unit_file_preset(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3643 carries_install_info = r;
3644 } else if (streq(verb, "mask"))
3645 r = unit_file_mask(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3646 else if (streq(verb, "unmask"))
3647 r = unit_file_unmask(arg_scope, arg_runtime, arg_root, args+1, &changes, &n_changes);
3649 assert_not_reached("Unknown verb");
3652 log_error("Operation failed: %s", strerror(-r));
3656 for (i = 0; i < n_changes; i++) {
3657 if (changes[i].type == UNIT_FILE_SYMLINK)
3658 log_info("ln -s '%s' '%s'", changes[i].source, changes[i].path);
3660 log_info("rm '%s'", changes[i].path);
3665 bool send_force = true, expect_carries_install_info = false;
3667 DBusMessageIter iter, sub, sub2;
3669 if (streq(verb, "enable")) {
3670 method = "EnableUnitFiles";
3671 expect_carries_install_info = true;
3672 } else if (streq(verb, "disable")) {
3673 method = "DisableUnitFiles";
3675 } else if (streq(verb, "reenable")) {
3676 method = "ReenableUnitFiles";
3677 expect_carries_install_info = true;
3678 } else if (streq(verb, "link"))
3679 method = "LinkUnitFiles";
3680 else if (streq(verb, "preset")) {
3681 method = "PresetUnitFiles";
3682 expect_carries_install_info = true;
3683 } else if (streq(verb, "mask"))
3684 method = "MaskUnitFiles";
3685 else if (streq(verb, "unmask")) {
3686 method = "UnmaskUnitFiles";
3689 assert_not_reached("Unknown verb");
3691 m = dbus_message_new_method_call(
3692 "org.freedesktop.systemd1",
3693 "/org/freedesktop/systemd1",
3694 "org.freedesktop.systemd1.Manager",
3697 log_error("Out of memory");
3702 dbus_message_iter_init_append(m, &iter);
3704 r = bus_append_strv_iter(&iter, args+1);
3706 log_error("Failed to append unit files.");
3711 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &a)) {
3712 log_error("Failed to append runtime boolean.");
3720 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b)) {
3721 log_error("Failed to append force boolean.");
3727 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
3729 log_error("Failed to issue method call: %s", bus_error_message(&error));
3734 if (!dbus_message_iter_init(reply, &iter)) {
3735 log_error("Failed to initialize iterator.");
3739 if (expect_carries_install_info) {
3740 r = bus_iter_get_basic_and_next(&iter, DBUS_TYPE_BOOLEAN, &b, true);
3742 log_error("Failed to parse reply.");
3746 carries_install_info = b;
3749 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
3750 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
3751 log_error("Failed to parse reply.");
3756 dbus_message_iter_recurse(&iter, &sub);
3757 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
3758 const char *type, *path, *source;
3760 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
3761 log_error("Failed to parse reply.");
3766 dbus_message_iter_recurse(&sub, &sub2);
3768 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
3769 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0 ||
3770 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &source, false) < 0) {
3771 log_error("Failed to parse reply.");
3776 if (streq(type, "symlink"))
3777 log_info("ln -s '%s' '%s'", source, path);
3779 log_info("rm '%s'", path);
3781 dbus_message_iter_next(&sub);
3784 /* Try to reload if enabeld */
3786 r = daemon_reload(bus, args);
3789 if (carries_install_info == 0)
3790 log_warning("Warning: unit files do not carry install information. No operation executed.");
3794 dbus_message_unref(m);
3797 dbus_message_unref(reply);
3799 unit_file_changes_free(changes, n_changes);
3801 dbus_error_free(&error);
3805 static int unit_is_enabled(DBusConnection *bus, char **args) {
3808 DBusMessage *m = NULL, *reply = NULL;
3812 dbus_error_init(&error);
3814 r = enable_sysv_units(args);
3820 if (!bus || avoid_bus()) {
3822 STRV_FOREACH(name, args+1) {
3823 UnitFileState state;
3825 state = unit_file_get_state(arg_scope, arg_root, *name);
3831 if (state == UNIT_FILE_ENABLED ||
3832 state == UNIT_FILE_ENABLED_RUNTIME ||
3833 state == UNIT_FILE_STATIC)
3837 puts(unit_file_state_to_string(state));
3841 STRV_FOREACH(name, args+1) {
3844 m = dbus_message_new_method_call(
3845 "org.freedesktop.systemd1",
3846 "/org/freedesktop/systemd1",
3847 "org.freedesktop.systemd1.Manager",
3848 "GetUnitFileState");
3850 log_error("Out of memory");
3855 if (!dbus_message_append_args(m,
3856 DBUS_TYPE_STRING, name,
3857 DBUS_TYPE_INVALID)) {
3858 log_error("Could not append arguments to message.");
3863 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
3865 log_error("Failed to issue method call: %s", bus_error_message(&error));
3870 if (!dbus_message_get_args(reply, &error,
3871 DBUS_TYPE_STRING, &s,
3872 DBUS_TYPE_INVALID)) {
3873 log_error("Failed to parse reply: %s", bus_error_message(&error));
3878 dbus_message_unref(m);
3879 dbus_message_unref(reply);
3882 if (streq(s, "enabled") ||
3883 streq(s, "enabled-runtime") ||
3892 r = enabled ? 0 : 1;
3896 dbus_message_unref(m);
3899 dbus_message_unref(reply);
3901 dbus_error_free(&error);
3905 static int systemctl_help(void) {
3907 pager_open_if_enabled();
3909 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
3910 "Query or send control commands to the systemd manager.\n\n"
3911 " -h --help Show this help\n"
3912 " --version Show package version\n"
3913 " -t --type=TYPE List only units of a particular type\n"
3914 " -p --property=NAME Show only properties by this name\n"
3915 " -a --all Show all units/properties, including dead/empty ones\n"
3916 " --failed Show only failed units\n"
3917 " --full Don't ellipsize unit names on output\n"
3918 " --fail When queueing a new job, fail if conflicting jobs are\n"
3920 " --ignore-dependencies\n"
3921 " When queueing a new job, ignore all its dependencies\n"
3922 " --kill-who=WHO Who to send signal to\n"
3923 " -s --signal=SIGNAL Which signal to send\n"
3924 " -H --host=[USER@]HOST\n"
3925 " Show information for remote host\n"
3926 " -P --privileged Acquire privileges before execution\n"
3927 " -q --quiet Suppress output\n"
3928 " --no-block Do not wait until operation finished\n"
3929 " --no-wall Don't send wall message before halt/power-off/reboot\n"
3930 " --no-reload When enabling/disabling unit files, don't reload daemon\n"
3932 " --no-pager Do not pipe output into a pager\n"
3933 " --no-ask-password\n"
3934 " Do not ask for system passwords\n"
3935 " --order When generating graph for dot, show only order\n"
3936 " --require When generating graph for dot, show only requirement\n"
3937 " --system Connect to system manager\n"
3938 " --user Connect to user service manager\n"
3939 " --global Enable/disable unit files globally\n"
3940 " -f --force When enabling unit files, override existing symlinks\n"
3941 " When shutting down, execute action immediately\n"
3942 " --root=PATH Enable unit files in the specified root directory\n"
3943 " --runtime Enable unit files only temporarily until next reboot\n\n"
3945 " list-units List loaded units\n"
3946 " start [NAME...] Start (activate) one or more units\n"
3947 " stop [NAME...] Stop (deactivate) one or more units\n"
3948 " reload [NAME...] Reload one or more units\n"
3949 " restart [NAME...] Start or restart one or more units\n"
3950 " try-restart [NAME...] Restart one or more units if active\n"
3951 " reload-or-restart [NAME...] Reload one or more units is possible,\n"
3952 " otherwise start or restart\n"
3953 " reload-or-try-restart [NAME...] Reload one or more units is possible,\n"
3954 " otherwise restart if active\n"
3955 " isolate [NAME] Start one unit and stop all others\n"
3956 " kill [NAME...] Send signal to processes of a unit\n"
3957 " is-active [NAME...] Check whether units are active\n"
3958 " status [NAME...|PID...] Show runtime status of one or more units\n"
3959 " show [NAME...|JOB...] Show properties of one or more\n"
3960 " units/jobs or the manager\n"
3961 " reset-failed [NAME...] Reset failed state for all, one, or more\n"
3963 " load [NAME...] Load one or more units\n\n"
3964 "Unit File Commands:\n"
3965 " list-unit-files List installed unit files\n"
3966 " enable [NAME...] Enable one or more unit files\n"
3967 " disable [NAME...] Disable one or more unit files\n"
3968 " reenable [NAME...] Reenable one or more unit files\n"
3969 " preset [NAME...] Enable/disable one or more unit files\n"
3970 " based on preset configuration\n"
3971 " mask [NAME...] Mask one or more units\n"
3972 " unmask [NAME...] Unmask one or more units\n"
3973 " link [PATH...] Link one or more units files into\n"
3974 " the search path\n"
3975 " is-enabled [NAME...] Check whether unit files are enabled\n\n"
3977 " list-jobs List jobs\n"
3978 " cancel [JOB...] Cancel all, one, or more jobs\n\n"
3979 "Status Commands:\n"
3980 " dump Dump server status\n"
3981 " dot Dump dependency graph for dot(1)\n\n"
3982 "Snapshot Commands:\n"
3983 " snapshot [NAME] Create a snapshot\n"
3984 " delete [NAME...] Remove one or more snapshots\n\n"
3985 "Environment Commands:\n"
3986 " show-environment Dump environment\n"
3987 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
3988 " unset-environment [NAME...] Unset one or more environment variables\n\n"
3989 "Manager Lifecycle Commands:\n"
3990 " daemon-reload Reload systemd manager configuration\n"
3991 " daemon-reexec Reexecute systemd manager\n\n"
3992 "System Commands:\n"
3993 " default Enter system default mode\n"
3994 " rescue Enter system rescue mode\n"
3995 " emergency Enter system emergency mode\n"
3996 " halt Shut down and halt the system\n"
3997 " poweroff Shut down and power-off the system\n"
3998 " reboot Shut down and reboot the system\n"
3999 " kexec Shut down and reboot the system with kexec\n"
4000 " exit Ask for user instance termination\n",
4001 program_invocation_short_name);
4006 static int halt_help(void) {
4008 printf("%s [OPTIONS...]\n\n"
4009 "%s the system.\n\n"
4010 " --help Show this help\n"
4011 " --halt Halt the machine\n"
4012 " -p --poweroff Switch off the machine\n"
4013 " --reboot Reboot the machine\n"
4014 " -f --force Force immediate halt/power-off/reboot\n"
4015 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
4016 " -d --no-wtmp Don't write wtmp record\n"
4017 " -n --no-sync Don't sync before halt/power-off/reboot\n"
4018 " --no-wall Don't send wall message before halt/power-off/reboot\n",
4019 program_invocation_short_name,
4020 arg_action == ACTION_REBOOT ? "Reboot" :
4021 arg_action == ACTION_POWEROFF ? "Power off" :
4027 static int shutdown_help(void) {
4029 printf("%s [OPTIONS...] [TIME] [WALL...]\n\n"
4030 "Shut down the system.\n\n"
4031 " --help Show this help\n"
4032 " -H --halt Halt the machine\n"
4033 " -P --poweroff Power-off the machine\n"
4034 " -r --reboot Reboot the machine\n"
4035 " -h Equivalent to --poweroff, overriden by --halt\n"
4036 " -k Don't halt/power-off/reboot, just send warnings\n"
4037 " --no-wall Don't send wall message before halt/power-off/reboot\n"
4038 " -c Cancel a pending shutdown\n",
4039 program_invocation_short_name);
4044 static int telinit_help(void) {
4046 printf("%s [OPTIONS...] {COMMAND}\n\n"
4047 "Send control commands to the init daemon.\n\n"
4048 " --help Show this help\n"
4049 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
4051 " 0 Power-off the machine\n"
4052 " 6 Reboot the machine\n"
4053 " 2, 3, 4, 5 Start runlevelX.target unit\n"
4054 " 1, s, S Enter rescue mode\n"
4055 " q, Q Reload init daemon configuration\n"
4056 " u, U Reexecute init daemon\n",
4057 program_invocation_short_name);
4062 static int runlevel_help(void) {
4064 printf("%s [OPTIONS...]\n\n"
4065 "Prints the previous and current runlevel of the init system.\n\n"
4066 " --help Show this help\n",
4067 program_invocation_short_name);
4072 static int systemctl_parse_argv(int argc, char *argv[]) {
4076 ARG_IGNORE_DEPENDENCIES,
4091 ARG_NO_ASK_PASSWORD,
4096 static const struct option options[] = {
4097 { "help", no_argument, NULL, 'h' },
4098 { "version", no_argument, NULL, ARG_VERSION },
4099 { "type", required_argument, NULL, 't' },
4100 { "property", required_argument, NULL, 'p' },
4101 { "all", no_argument, NULL, 'a' },
4102 { "failed", no_argument, NULL, ARG_FAILED },
4103 { "full", no_argument, NULL, ARG_FULL },
4104 { "fail", no_argument, NULL, ARG_FAIL },
4105 { "ignore-dependencies", no_argument, NULL, ARG_IGNORE_DEPENDENCIES },
4106 { "user", no_argument, NULL, ARG_USER },
4107 { "system", no_argument, NULL, ARG_SYSTEM },
4108 { "global", no_argument, NULL, ARG_GLOBAL },
4109 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
4110 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
4111 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4112 { "quiet", no_argument, NULL, 'q' },
4113 { "order", no_argument, NULL, ARG_ORDER },
4114 { "require", no_argument, NULL, ARG_REQUIRE },
4115 { "root", required_argument, NULL, ARG_ROOT },
4116 { "force", no_argument, NULL, 'f' },
4117 { "no-reload", no_argument, NULL, ARG_NO_RELOAD },
4118 { "kill-mode", required_argument, NULL, ARG_KILL_MODE }, /* undocumented on purpose */
4119 { "kill-who", required_argument, NULL, ARG_KILL_WHO },
4120 { "signal", required_argument, NULL, 's' },
4121 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
4122 { "host", required_argument, NULL, 'H' },
4123 { "privileged",no_argument, NULL, 'P' },
4124 { "runtime", no_argument, NULL, ARG_RUNTIME },
4125 { NULL, 0, NULL, 0 }
4133 /* Only when running as systemctl we ask for passwords */
4134 arg_ask_password = true;
4136 while ((c = getopt_long(argc, argv, "ht:p:aqfs:H:P", options, NULL)) >= 0) {
4145 puts(PACKAGE_STRING);
4147 puts(SYSTEMD_FEATURES);
4157 if (!(l = strv_append(arg_property, optarg)))
4160 strv_free(arg_property);
4163 /* If the user asked for a particular
4164 * property, show it to him, even if it is
4175 arg_job_mode = "fail";
4178 case ARG_IGNORE_DEPENDENCIES:
4179 arg_job_mode = "ignore-dependencies";
4183 arg_scope = UNIT_FILE_USER;
4187 arg_scope = UNIT_FILE_SYSTEM;
4191 arg_scope = UNIT_FILE_GLOBAL;
4195 arg_no_block = true;
4199 arg_no_pager = true;
4207 arg_dot = DOT_ORDER;
4211 arg_dot = DOT_REQUIRE;
4235 arg_no_reload = true;
4239 arg_kill_who = optarg;
4243 arg_kill_mode = optarg;
4247 if ((arg_signal = signal_from_string_try_harder(optarg)) < 0) {
4248 log_error("Failed to parse signal string %s.", optarg);
4253 case ARG_NO_ASK_PASSWORD:
4254 arg_ask_password = false;
4258 arg_transport = TRANSPORT_POLKIT;
4262 arg_transport = TRANSPORT_SSH;
4274 log_error("Unknown option code %c", c);
4279 if (arg_transport != TRANSPORT_NORMAL && arg_scope != UNIT_FILE_SYSTEM) {
4280 log_error("Cannot access user instance remotely.");
4287 static int halt_parse_argv(int argc, char *argv[]) {
4296 static const struct option options[] = {
4297 { "help", no_argument, NULL, ARG_HELP },
4298 { "halt", no_argument, NULL, ARG_HALT },
4299 { "poweroff", no_argument, NULL, 'p' },
4300 { "reboot", no_argument, NULL, ARG_REBOOT },
4301 { "force", no_argument, NULL, 'f' },
4302 { "wtmp-only", no_argument, NULL, 'w' },
4303 { "no-wtmp", no_argument, NULL, 'd' },
4304 { "no-sync", no_argument, NULL, 'n' },
4305 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4306 { NULL, 0, NULL, 0 }
4314 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
4315 if (runlevel == '0' || runlevel == '6')
4316 arg_immediate = true;
4318 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
4326 arg_action = ACTION_HALT;
4330 if (arg_action != ACTION_REBOOT)
4331 arg_action = ACTION_POWEROFF;
4335 arg_action = ACTION_REBOOT;
4339 arg_immediate = true;
4360 /* Compatibility nops */
4367 log_error("Unknown option code %c", c);
4372 if (optind < argc) {
4373 log_error("Too many arguments.");
4380 static int parse_time_spec(const char *t, usec_t *_u) {
4384 if (streq(t, "now"))
4386 else if (!strchr(t, ':')) {
4389 if (safe_atou64(t, &u) < 0)
4392 *_u = now(CLOCK_REALTIME) + USEC_PER_MINUTE * u;
4401 hour = strtol(t, &e, 10);
4402 if (errno != 0 || *e != ':' || hour < 0 || hour > 23)
4405 minute = strtol(e+1, &e, 10);
4406 if (errno != 0 || *e != 0 || minute < 0 || minute > 59)
4409 n = now(CLOCK_REALTIME);
4410 s = (time_t) (n / USEC_PER_SEC);
4413 assert_se(localtime_r(&s, &tm));
4415 tm.tm_hour = (int) hour;
4416 tm.tm_min = (int) minute;
4419 assert_se(s = mktime(&tm));
4421 *_u = (usec_t) s * USEC_PER_SEC;
4424 *_u += USEC_PER_DAY;
4430 static bool kexec_loaded(void) {
4431 bool loaded = false;
4434 if (read_one_line_file("/sys/kernel/kexec_loaded", &s) >= 0) {
4442 static int shutdown_parse_argv(int argc, char *argv[]) {
4449 static const struct option options[] = {
4450 { "help", no_argument, NULL, ARG_HELP },
4451 { "halt", no_argument, NULL, 'H' },
4452 { "poweroff", no_argument, NULL, 'P' },
4453 { "reboot", no_argument, NULL, 'r' },
4454 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4455 { NULL, 0, NULL, 0 }
4463 while ((c = getopt_long(argc, argv, "HPrhkt:afFc", options, NULL)) >= 0) {
4471 arg_action = ACTION_HALT;
4475 arg_action = ACTION_POWEROFF;
4480 arg_action = ACTION_KEXEC;
4482 arg_action = ACTION_REBOOT;
4486 if (arg_action != ACTION_HALT)
4487 arg_action = ACTION_POWEROFF;
4500 /* Compatibility nops */
4504 arg_action = ACTION_CANCEL_SHUTDOWN;
4511 log_error("Unknown option code %c", c);
4516 if (argc > optind) {
4517 if ((r = parse_time_spec(argv[optind], &arg_when)) < 0) {
4518 log_error("Failed to parse time specification: %s", argv[optind]);
4522 arg_when = now(CLOCK_REALTIME) + USEC_PER_MINUTE;
4524 /* We skip the time argument */
4525 if (argc > optind + 1)
4526 arg_wall = argv + optind + 1;
4533 static int telinit_parse_argv(int argc, char *argv[]) {
4540 static const struct option options[] = {
4541 { "help", no_argument, NULL, ARG_HELP },
4542 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4543 { NULL, 0, NULL, 0 }
4546 static const struct {
4550 { '0', ACTION_POWEROFF },
4551 { '6', ACTION_REBOOT },
4552 { '1', ACTION_RESCUE },
4553 { '2', ACTION_RUNLEVEL2 },
4554 { '3', ACTION_RUNLEVEL3 },
4555 { '4', ACTION_RUNLEVEL4 },
4556 { '5', ACTION_RUNLEVEL5 },
4557 { 's', ACTION_RESCUE },
4558 { 'S', ACTION_RESCUE },
4559 { 'q', ACTION_RELOAD },
4560 { 'Q', ACTION_RELOAD },
4561 { 'u', ACTION_REEXEC },
4562 { 'U', ACTION_REEXEC }
4571 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4586 log_error("Unknown option code %c", c);
4591 if (optind >= argc) {
4596 if (optind + 1 < argc) {
4597 log_error("Too many arguments.");
4601 if (strlen(argv[optind]) != 1) {
4602 log_error("Expected single character argument.");
4606 for (i = 0; i < ELEMENTSOF(table); i++)
4607 if (table[i].from == argv[optind][0])
4610 if (i >= ELEMENTSOF(table)) {
4611 log_error("Unknown command %s.", argv[optind]);
4615 arg_action = table[i].to;
4622 static int runlevel_parse_argv(int argc, char *argv[]) {
4628 static const struct option options[] = {
4629 { "help", no_argument, NULL, ARG_HELP },
4630 { NULL, 0, NULL, 0 }
4638 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4649 log_error("Unknown option code %c", c);
4654 if (optind < argc) {
4655 log_error("Too many arguments.");
4662 static int parse_argv(int argc, char *argv[]) {
4666 if (program_invocation_short_name) {
4668 if (strstr(program_invocation_short_name, "halt")) {
4669 arg_action = ACTION_HALT;
4670 return halt_parse_argv(argc, argv);
4671 } else if (strstr(program_invocation_short_name, "poweroff")) {
4672 arg_action = ACTION_POWEROFF;
4673 return halt_parse_argv(argc, argv);
4674 } else if (strstr(program_invocation_short_name, "reboot")) {
4676 arg_action = ACTION_KEXEC;
4678 arg_action = ACTION_REBOOT;
4679 return halt_parse_argv(argc, argv);
4680 } else if (strstr(program_invocation_short_name, "shutdown")) {
4681 arg_action = ACTION_POWEROFF;
4682 return shutdown_parse_argv(argc, argv);
4683 } else if (strstr(program_invocation_short_name, "init")) {
4685 if (sd_booted() > 0) {
4686 arg_action = ACTION_INVALID;
4687 return telinit_parse_argv(argc, argv);
4689 /* Hmm, so some other init system is
4690 * running, we need to forward this
4691 * request to it. For now we simply
4692 * guess that it is Upstart. */
4694 execv("/lib/upstart/telinit", argv);
4696 log_error("Couldn't find an alternative telinit implementation to spawn.");
4700 } else if (strstr(program_invocation_short_name, "runlevel")) {
4701 arg_action = ACTION_RUNLEVEL;
4702 return runlevel_parse_argv(argc, argv);
4706 arg_action = ACTION_SYSTEMCTL;
4707 return systemctl_parse_argv(argc, argv);
4710 static int action_to_runlevel(void) {
4712 static const char table[_ACTION_MAX] = {
4713 [ACTION_HALT] = '0',
4714 [ACTION_POWEROFF] = '0',
4715 [ACTION_REBOOT] = '6',
4716 [ACTION_RUNLEVEL2] = '2',
4717 [ACTION_RUNLEVEL3] = '3',
4718 [ACTION_RUNLEVEL4] = '4',
4719 [ACTION_RUNLEVEL5] = '5',
4720 [ACTION_RESCUE] = '1'
4723 assert(arg_action < _ACTION_MAX);
4725 return table[arg_action];
4728 static int talk_upstart(void) {
4729 DBusMessage *m = NULL, *reply = NULL;
4731 int previous, rl, r;
4733 env1_buf[] = "RUNLEVEL=X",
4734 env2_buf[] = "PREVLEVEL=X";
4735 char *env1 = env1_buf, *env2 = env2_buf;
4736 const char *emit = "runlevel";
4737 dbus_bool_t b_false = FALSE;
4738 DBusMessageIter iter, sub;
4739 DBusConnection *bus;
4741 dbus_error_init(&error);
4743 if (!(rl = action_to_runlevel()))
4746 if (utmp_get_runlevel(&previous, NULL) < 0)
4749 if (!(bus = dbus_connection_open_private("unix:abstract=/com/ubuntu/upstart", &error))) {
4750 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
4755 log_error("Failed to connect to Upstart bus: %s", bus_error_message(&error));
4760 if ((r = bus_check_peercred(bus)) < 0) {
4761 log_error("Failed to verify owner of bus.");
4765 if (!(m = dbus_message_new_method_call(
4766 "com.ubuntu.Upstart",
4767 "/com/ubuntu/Upstart",
4768 "com.ubuntu.Upstart0_6",
4771 log_error("Could not allocate message.");
4776 dbus_message_iter_init_append(m, &iter);
4778 env1_buf[sizeof(env1_buf)-2] = rl;
4779 env2_buf[sizeof(env2_buf)-2] = previous;
4781 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
4782 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
4783 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
4784 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
4785 !dbus_message_iter_close_container(&iter, &sub) ||
4786 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
4787 log_error("Could not append arguments to message.");
4792 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
4794 if (error_is_no_service(&error)) {
4799 log_error("Failed to issue method call: %s", bus_error_message(&error));
4808 dbus_message_unref(m);
4811 dbus_message_unref(reply);
4814 dbus_connection_flush(bus);
4815 dbus_connection_close(bus);
4816 dbus_connection_unref(bus);
4819 dbus_error_free(&error);
4824 static int talk_initctl(void) {
4825 struct init_request request;
4829 if (!(rl = action_to_runlevel()))
4833 request.magic = INIT_MAGIC;
4834 request.sleeptime = 0;
4835 request.cmd = INIT_CMD_RUNLVL;
4836 request.runlevel = rl;
4838 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
4840 if (errno == ENOENT)
4843 log_error("Failed to open "INIT_FIFO": %m");
4848 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
4849 close_nointr_nofail(fd);
4852 log_error("Failed to write to "INIT_FIFO": %m");
4853 return errno ? -errno : -EIO;
4859 static int systemctl_main(DBusConnection *bus, int argc, char *argv[], DBusError *error) {
4861 static const struct {
4869 int (* const dispatch)(DBusConnection *bus, char **args);
4871 { "list-units", LESS, 1, list_units },
4872 { "list-unit-files", EQUAL, 1, list_unit_files },
4873 { "list-jobs", EQUAL, 1, list_jobs },
4874 { "clear-jobs", EQUAL, 1, daemon_reload },
4875 { "load", MORE, 2, load_unit },
4876 { "cancel", MORE, 2, cancel_job },
4877 { "start", MORE, 2, start_unit },
4878 { "stop", MORE, 2, start_unit },
4879 { "condstop", MORE, 2, start_unit }, /* For compatibility with ALTLinux */
4880 { "reload", MORE, 2, start_unit },
4881 { "restart", MORE, 2, start_unit },
4882 { "try-restart", MORE, 2, start_unit },
4883 { "reload-or-restart", MORE, 2, start_unit },
4884 { "reload-or-try-restart", MORE, 2, start_unit },
4885 { "force-reload", MORE, 2, start_unit }, /* For compatibility with SysV */
4886 { "condreload", MORE, 2, start_unit }, /* For compatibility with ALTLinux */
4887 { "condrestart", MORE, 2, start_unit }, /* For compatibility with RH */
4888 { "isolate", EQUAL, 2, start_unit },
4889 { "kill", MORE, 2, kill_unit },
4890 { "is-active", MORE, 2, check_unit },
4891 { "check", MORE, 2, check_unit },
4892 { "show", MORE, 1, show },
4893 { "status", MORE, 2, show },
4894 { "dump", EQUAL, 1, dump },
4895 { "dot", EQUAL, 1, dot },
4896 { "snapshot", LESS, 2, snapshot },
4897 { "delete", MORE, 2, delete_snapshot },
4898 { "daemon-reload", EQUAL, 1, daemon_reload },
4899 { "daemon-reexec", EQUAL, 1, daemon_reload },
4900 { "show-environment", EQUAL, 1, show_enviroment },
4901 { "set-environment", MORE, 2, set_environment },
4902 { "unset-environment", MORE, 2, set_environment },
4903 { "halt", EQUAL, 1, start_special },
4904 { "poweroff", EQUAL, 1, start_special },
4905 { "reboot", EQUAL, 1, start_special },
4906 { "kexec", EQUAL, 1, start_special },
4907 { "default", EQUAL, 1, start_special },
4908 { "rescue", EQUAL, 1, start_special },
4909 { "emergency", EQUAL, 1, start_special },
4910 { "exit", EQUAL, 1, start_special },
4911 { "reset-failed", MORE, 1, reset_failed },
4912 { "enable", MORE, 2, enable_unit },
4913 { "disable", MORE, 2, enable_unit },
4914 { "is-enabled", MORE, 2, unit_is_enabled },
4915 { "reenable", MORE, 2, enable_unit },
4916 { "preset", MORE, 2, enable_unit },
4917 { "mask", MORE, 2, enable_unit },
4918 { "unmask", MORE, 2, enable_unit },
4919 { "link", MORE, 2, enable_unit }
4929 left = argc - optind;
4932 /* Special rule: no arguments means "list-units" */
4935 if (streq(argv[optind], "help")) {
4940 for (i = 0; i < ELEMENTSOF(verbs); i++)
4941 if (streq(argv[optind], verbs[i].verb))
4944 if (i >= ELEMENTSOF(verbs)) {
4945 log_error("Unknown operation %s", argv[optind]);
4950 switch (verbs[i].argc_cmp) {
4953 if (left != verbs[i].argc) {
4954 log_error("Invalid number of arguments.");
4961 if (left < verbs[i].argc) {
4962 log_error("Too few arguments.");
4969 if (left > verbs[i].argc) {
4970 log_error("Too many arguments.");
4977 assert_not_reached("Unknown comparison operator.");
4980 /* Require a bus connection for all operations but
4982 if (!streq(verbs[i].verb, "enable") &&
4983 !streq(verbs[i].verb, "disable") &&
4984 !streq(verbs[i].verb, "is-enable") &&
4985 !streq(verbs[i].verb, "reenable") &&
4986 !streq(verbs[i].verb, "preset") &&
4987 !streq(verbs[i].verb, "mask") &&
4988 !streq(verbs[i].verb, "unmask") &&
4989 !streq(verbs[i].verb, "link")) {
4991 if (running_in_chroot() > 0) {
4992 log_info("Running in chroot, ignoring request.");
4996 if (!bus && !avoid_bus()) {
4997 log_error("Failed to get D-Bus connection: %s", error->message);
5002 return verbs[i].dispatch(bus, argv + optind);
5005 static int send_shutdownd(usec_t t, char mode, bool dry_run, bool warn, const char *message) {
5007 struct msghdr msghdr;
5009 union sockaddr_union sockaddr;
5010 struct shutdownd_command c;
5015 c.dry_run = dry_run;
5019 strncpy(c.wall_message, message, sizeof(c.wall_message));
5021 if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0)
5025 sockaddr.sa.sa_family = AF_UNIX;
5026 sockaddr.un.sun_path[0] = 0;
5027 strncpy(sockaddr.un.sun_path, "/run/systemd/shutdownd", sizeof(sockaddr.un.sun_path));
5030 iovec.iov_base = (char*) &c;
5031 iovec.iov_len = sizeof(c);
5034 msghdr.msg_name = &sockaddr;
5035 msghdr.msg_namelen = offsetof(struct sockaddr_un, sun_path) + sizeof("/run/systemd/shutdownd") - 1;
5037 msghdr.msg_iov = &iovec;
5038 msghdr.msg_iovlen = 1;
5040 if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) < 0) {
5041 close_nointr_nofail(fd);
5045 close_nointr_nofail(fd);
5049 static int reload_with_fallback(DBusConnection *bus) {
5052 /* First, try systemd via D-Bus. */
5053 if (daemon_reload(bus, NULL) > 0)
5057 /* Nothing else worked, so let's try signals */
5058 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
5060 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
5061 log_error("kill() failed: %m");
5068 static int start_with_fallback(DBusConnection *bus) {
5071 /* First, try systemd via D-Bus. */
5072 if (start_unit(bus, NULL) >= 0)
5076 /* Hmm, talking to systemd via D-Bus didn't work. Then
5077 * let's try to talk to Upstart via D-Bus. */
5078 if (talk_upstart() > 0)
5081 /* Nothing else worked, so let's try
5083 if (talk_initctl() > 0)
5086 log_error("Failed to talk to init daemon.");
5090 warn_wall(arg_action);
5094 static int halt_main(DBusConnection *bus) {
5097 if (geteuid() != 0) {
5098 log_error("Must be root.");
5104 char date[FORMAT_TIMESTAMP_MAX];
5106 m = strv_join(arg_wall, " ");
5107 r = send_shutdownd(arg_when,
5108 arg_action == ACTION_HALT ? 'H' :
5109 arg_action == ACTION_POWEROFF ? 'P' :
5117 log_warning("Failed to talk to shutdownd, proceeding with immediate shutdown: %s", strerror(-r));
5119 log_info("Shutdown scheduled for %s, use 'shutdown -c' to cancel.",
5120 format_timestamp(date, sizeof(date), arg_when));
5125 if (!arg_dry && !arg_immediate)
5126 return start_with_fallback(bus);
5129 if (sd_booted() > 0)
5130 log_debug("Not writing utmp record, assuming that systemd-update-utmp is used.");
5131 else if ((r = utmp_put_shutdown(0)) < 0)
5132 log_warning("Failed to write utmp record: %s", strerror(-r));
5141 /* Make sure C-A-D is handled by the kernel from this
5143 reboot(RB_ENABLE_CAD);
5145 switch (arg_action) {
5148 log_info("Halting.");
5149 reboot(RB_HALT_SYSTEM);
5152 case ACTION_POWEROFF:
5153 log_info("Powering off.");
5154 reboot(RB_POWER_OFF);
5158 log_info("Rebooting.");
5159 reboot(RB_AUTOBOOT);
5163 assert_not_reached("Unknown halt action.");
5166 /* We should never reach this. */
5170 static int runlevel_main(void) {
5171 int r, runlevel, previous;
5173 r = utmp_get_runlevel(&runlevel, &previous);
5180 previous <= 0 ? 'N' : previous,
5181 runlevel <= 0 ? 'N' : runlevel);
5186 int main(int argc, char*argv[]) {
5187 int r, retval = EXIT_FAILURE;
5188 DBusConnection *bus = NULL;
5191 dbus_error_init(&error);
5193 log_parse_environment();
5196 if ((r = parse_argv(argc, argv)) < 0)
5199 retval = EXIT_SUCCESS;
5203 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
5204 * let's shortcut this */
5205 if (arg_action == ACTION_RUNLEVEL) {
5206 r = runlevel_main();
5207 retval = r < 0 ? EXIT_FAILURE : r;
5211 if (running_in_chroot() > 0 && arg_action != ACTION_SYSTEMCTL) {
5212 log_info("Running in chroot, ignoring request.");
5218 if (arg_transport == TRANSPORT_NORMAL)
5219 bus_connect(arg_scope == UNIT_FILE_SYSTEM ? DBUS_BUS_SYSTEM : DBUS_BUS_SESSION, &bus, &private_bus, &error);
5220 else if (arg_transport == TRANSPORT_POLKIT) {
5221 bus_connect_system_polkit(&bus, &error);
5222 private_bus = false;
5223 } else if (arg_transport == TRANSPORT_SSH) {
5224 bus_connect_system_ssh(NULL, arg_host, &bus, &error);
5225 private_bus = false;
5227 assert_not_reached("Uh, invalid transport...");
5230 switch (arg_action) {
5232 case ACTION_SYSTEMCTL:
5233 r = systemctl_main(bus, argc, argv, &error);
5237 case ACTION_POWEROFF:
5243 case ACTION_RUNLEVEL2:
5244 case ACTION_RUNLEVEL3:
5245 case ACTION_RUNLEVEL4:
5246 case ACTION_RUNLEVEL5:
5248 case ACTION_EMERGENCY:
5249 case ACTION_DEFAULT:
5250 r = start_with_fallback(bus);
5255 r = reload_with_fallback(bus);
5258 case ACTION_CANCEL_SHUTDOWN:
5259 r = send_shutdownd(0, 0, false, false, NULL);
5262 case ACTION_INVALID:
5263 case ACTION_RUNLEVEL:
5265 assert_not_reached("Unknown action");
5268 retval = r < 0 ? EXIT_FAILURE : r;
5272 dbus_connection_flush(bus);
5273 dbus_connection_close(bus);
5274 dbus_connection_unref(bus);
5277 dbus_error_free(&error);
5281 strv_free(arg_property);