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) {
141 /* Cache result before we open the pager */
150 static void agent_open_if_enabled(void) {
152 /* Open the password agent as a child process if necessary */
154 if (!arg_ask_password)
157 if (arg_scope != UNIT_FILE_SYSTEM)
163 static const char *ansi_highlight(bool b) {
168 return b ? ANSI_HIGHLIGHT_ON : ANSI_HIGHLIGHT_OFF;
171 static const char *ansi_highlight_green(bool b) {
176 return b ? ANSI_HIGHLIGHT_GREEN_ON : ANSI_HIGHLIGHT_OFF;
179 static bool error_is_no_service(const DBusError *error) {
182 if (!dbus_error_is_set(error))
185 if (dbus_error_has_name(error, DBUS_ERROR_NAME_HAS_NO_OWNER))
188 if (dbus_error_has_name(error, DBUS_ERROR_SERVICE_UNKNOWN))
191 return startswith(error->name, "org.freedesktop.DBus.Error.Spawn.");
194 static int translate_bus_error_to_exit_status(int r, const DBusError *error) {
197 if (!dbus_error_is_set(error))
200 if (dbus_error_has_name(error, DBUS_ERROR_ACCESS_DENIED) ||
201 dbus_error_has_name(error, BUS_ERROR_ONLY_BY_DEPENDENCY) ||
202 dbus_error_has_name(error, BUS_ERROR_NO_ISOLATION) ||
203 dbus_error_has_name(error, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE))
204 return EXIT_NOPERMISSION;
206 if (dbus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT))
207 return EXIT_NOTINSTALLED;
209 if (dbus_error_has_name(error, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE) ||
210 dbus_error_has_name(error, BUS_ERROR_NOT_SUPPORTED))
211 return EXIT_NOTIMPLEMENTED;
213 if (dbus_error_has_name(error, BUS_ERROR_LOAD_FAILED))
214 return EXIT_NOTCONFIGURED;
222 static void warn_wall(enum action action) {
223 static const char *table[_ACTION_MAX] = {
224 [ACTION_HALT] = "The system is going down for system halt NOW!",
225 [ACTION_REBOOT] = "The system is going down for reboot NOW!",
226 [ACTION_POWEROFF] = "The system is going down for power-off NOW!",
227 [ACTION_KEXEC] = "The system is going down for kexec reboot NOW!",
228 [ACTION_RESCUE] = "The system is going down to rescue mode NOW!",
229 [ACTION_EMERGENCY] = "The system is going down to emergency mode NOW!"
238 if (!(p = strv_join(arg_wall, " "))) {
239 log_error("Failed to join strings.");
255 utmp_wall(table[action], NULL);
258 static bool avoid_bus(void) {
260 if (running_in_chroot() > 0)
263 if (sd_booted() <= 0)
266 if (!isempty(arg_root))
269 if (arg_scope == UNIT_FILE_GLOBAL)
277 const char *description;
278 const char *load_state;
279 const char *active_state;
280 const char *sub_state;
281 const char *following;
282 const char *unit_path;
284 const char *job_type;
285 const char *job_path;
288 static int compare_unit_info(const void *a, const void *b) {
290 const struct unit_info *u = a, *v = b;
292 d1 = strrchr(u->id, '.');
293 d2 = strrchr(v->id, '.');
298 if ((r = strcasecmp(d1, d2)) != 0)
302 return strcasecmp(u->id, v->id);
305 static bool output_show_unit(const struct unit_info *u) {
309 return streq(u->active_state, "failed");
311 return (!arg_type || ((dot = strrchr(u->id, '.')) &&
312 streq(dot+1, arg_type))) &&
313 (arg_all || !(streq(u->active_state, "inactive") || u->following[0]) || u->job_id > 0);
316 static void output_units_list(const struct unit_info *unit_infos, unsigned c) {
317 unsigned active_len, sub_len, job_len, n_shown = 0;
318 const struct unit_info *u;
320 active_len = sizeof("ACTIVE")-1;
321 sub_len = sizeof("SUB")-1;
322 job_len = sizeof("JOB")-1;
324 for (u = unit_infos; u < unit_infos + c; u++) {
325 if (!output_show_unit(u))
328 active_len = MAX(active_len, strlen(u->active_state));
329 sub_len = MAX(sub_len, strlen(u->sub_state));
331 job_len = MAX(job_len, strlen(u->job_type));
335 printf("%-25s %-6s %-*s %-*s %-*s", "UNIT", "LOAD",
336 active_len, "ACTIVE", sub_len, "SUB", job_len, "JOB");
337 if (columns() >= 80+12 || arg_full || !arg_no_pager)
338 printf(" %s\n", "DESCRIPTION");
343 for (u = unit_infos; u < unit_infos + c; u++) {
346 const char *on_loaded, *off_loaded;
347 const char *on_active, *off_active;
349 if (!output_show_unit(u))
354 if (!streq(u->load_state, "loaded") &&
355 !streq(u->load_state, "banned")) {
356 on_loaded = ansi_highlight(true);
357 off_loaded = ansi_highlight(false);
359 on_loaded = off_loaded = "";
361 if (streq(u->active_state, "failed")) {
362 on_active = ansi_highlight(true);
363 off_active = ansi_highlight(false);
365 on_active = off_active = "";
367 e = arg_full ? NULL : ellipsize(u->id, 25, 33);
369 printf("%-25s %s%-6s%s %s%-*s %-*s%s%n",
371 on_loaded, u->load_state, off_loaded,
372 on_active, active_len, u->active_state,
373 sub_len, u->sub_state, off_active,
378 a -= strlen(on_loaded) + strlen(off_loaded);
379 a -= strlen(on_active) + strlen(off_active);
382 printf(" %-*s", job_len, u->job_type);
386 if (a + b + 1 < columns()) {
388 printf(" %-*s", job_len, "");
390 if (arg_full || !arg_no_pager)
391 printf(" %s", u->description);
393 printf(" %.*s", columns() - a - b - 1, u->description);
400 printf("\nLOAD = Reflects whether the unit definition was properly loaded.\n"
401 "ACTIVE = The high-level unit activation state, i.e. generalization of SUB.\n"
402 "SUB = The low-level unit activation state, values depend on unit type.\n"
403 "JOB = Pending job for the unit.\n");
406 printf("\n%u units listed.\n", n_shown);
408 printf("\n%u units listed. Pass --all to see inactive units, too.\n", n_shown);
412 static int list_units(DBusConnection *bus, char **args) {
413 DBusMessage *m = NULL, *reply = NULL;
416 DBusMessageIter iter, sub, sub2;
417 unsigned c = 0, n_units = 0;
418 struct unit_info *unit_infos = NULL;
420 dbus_error_init(&error);
424 pager_open_if_enabled();
426 if (!(m = dbus_message_new_method_call(
427 "org.freedesktop.systemd1",
428 "/org/freedesktop/systemd1",
429 "org.freedesktop.systemd1.Manager",
431 log_error("Could not allocate message.");
435 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
436 log_error("Failed to issue method call: %s", bus_error_message(&error));
441 if (!dbus_message_iter_init(reply, &iter) ||
442 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
443 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
444 log_error("Failed to parse reply.");
449 dbus_message_iter_recurse(&iter, &sub);
451 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
454 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
455 log_error("Failed to parse reply.");
463 n_units = MAX(2*c, 16);
464 w = realloc(unit_infos, sizeof(struct unit_info) * n_units);
467 log_error("Failed to allocate unit array.");
477 dbus_message_iter_recurse(&sub, &sub2);
479 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->id, true) < 0 ||
480 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->description, true) < 0 ||
481 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->load_state, true) < 0 ||
482 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->active_state, true) < 0 ||
483 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->sub_state, true) < 0 ||
484 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->following, true) < 0 ||
485 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &u->unit_path, true) < 0 ||
486 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &u->job_id, true) < 0 ||
487 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->job_type, true) < 0 ||
488 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &u->job_path, false) < 0) {
489 log_error("Failed to parse reply.");
494 dbus_message_iter_next(&sub);
499 qsort(unit_infos, c, sizeof(struct unit_info), compare_unit_info);
500 output_units_list(unit_infos, c);
507 dbus_message_unref(m);
510 dbus_message_unref(reply);
514 dbus_error_free(&error);
519 static int compare_unit_file_list(const void *a, const void *b) {
521 const UnitFileList *u = a, *v = b;
523 d1 = strrchr(u->path, '.');
524 d2 = strrchr(v->path, '.');
529 r = strcasecmp(d1, d2);
534 return strcasecmp(file_name_from_path(u->path), file_name_from_path(v->path));
537 static bool output_show_unit_file(const UnitFileList *u) {
540 return !arg_type || ((dot = strrchr(u->path, '.')) && streq(dot+1, arg_type));
543 static void output_unit_file_list(const UnitFileList *units, unsigned c) {
544 unsigned n_shown = 0;
545 const UnitFileList *u;
548 printf("%-25s %-6s\n", "UNIT FILE", "STATE");
550 for (u = units; u < units + c; u++) {
552 const char *on, *off;
555 if (!output_show_unit_file(u))
560 if (u->state == UNIT_FILE_MASKED ||
561 u->state == UNIT_FILE_MASKED_RUNTIME ||
562 u->state == UNIT_FILE_DISABLED) {
563 on = ansi_highlight(true);
564 off = ansi_highlight(false);
565 } else if (u->state == UNIT_FILE_ENABLED) {
566 on = ansi_highlight_green(true);
567 off = ansi_highlight_green(false);
571 id = file_name_from_path(u->path);
573 e = arg_full ? NULL : ellipsize(id, 25, 33);
575 printf("%-25s %s%-6s%s\n",
577 on, unit_file_state_to_string(u->state), off);
583 printf("\n%u unit files listed.\n", n_shown);
586 static int list_unit_files(DBusConnection *bus, char **args) {
587 DBusMessage *m = NULL, *reply = NULL;
590 DBusMessageIter iter, sub, sub2;
591 unsigned c = 0, n_units = 0;
592 UnitFileList *units = NULL;
594 dbus_error_init(&error);
598 pager_open_if_enabled();
605 h = hashmap_new(string_hash_func, string_compare_func);
607 log_error("Out of memory");
611 r = unit_file_get_list(arg_scope, arg_root, h);
613 unit_file_list_free(h);
614 log_error("Failed to get unit file list: %s", strerror(-r));
618 n_units = hashmap_size(h);
619 units = new(UnitFileList, n_units);
621 unit_file_list_free(h);
622 log_error("Out of memory");
626 HASHMAP_FOREACH(u, h, i) {
627 memcpy(units + c++, u, sizeof(UnitFileList));
633 m = dbus_message_new_method_call(
634 "org.freedesktop.systemd1",
635 "/org/freedesktop/systemd1",
636 "org.freedesktop.systemd1.Manager",
639 log_error("Could not allocate message.");
643 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
645 log_error("Failed to issue method call: %s", bus_error_message(&error));
650 if (!dbus_message_iter_init(reply, &iter) ||
651 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
652 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
653 log_error("Failed to parse reply.");
658 dbus_message_iter_recurse(&iter, &sub);
660 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
664 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
665 log_error("Failed to parse reply.");
673 n_units = MAX(2*c, 16);
674 w = realloc(units, sizeof(struct UnitFileList) * n_units);
677 log_error("Failed to allocate unit array.");
687 dbus_message_iter_recurse(&sub, &sub2);
689 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->path, true) < 0 ||
690 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, false) < 0) {
691 log_error("Failed to parse reply.");
696 u->state = unit_file_state_from_string(state);
698 dbus_message_iter_next(&sub);
704 qsort(units, c, sizeof(UnitFileList), compare_unit_file_list);
705 output_unit_file_list(units, c);
712 dbus_message_unref(m);
715 dbus_message_unref(reply);
719 dbus_error_free(&error);
724 static int dot_one_property(const char *name, const char *prop, DBusMessageIter *iter) {
725 static const char * const colors[] = {
726 "Requires", "[color=\"black\"]",
727 "RequiresOverridable", "[color=\"black\"]",
728 "Requisite", "[color=\"darkblue\"]",
729 "RequisiteOverridable", "[color=\"darkblue\"]",
730 "Wants", "[color=\"darkgrey\"]",
731 "Conflicts", "[color=\"red\"]",
732 "ConflictedBy", "[color=\"red\"]",
733 "After", "[color=\"green\"]"
736 const char *c = NULL;
743 for (i = 0; i < ELEMENTSOF(colors); i += 2)
744 if (streq(colors[i], prop)) {
752 if (arg_dot != DOT_ALL)
753 if ((arg_dot == DOT_ORDER) != streq(prop, "After"))
756 switch (dbus_message_iter_get_arg_type(iter)) {
758 case DBUS_TYPE_ARRAY:
760 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
763 dbus_message_iter_recurse(iter, &sub);
765 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
768 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
769 dbus_message_iter_get_basic(&sub, &s);
770 printf("\t\"%s\"->\"%s\" %s;\n", name, s, c);
772 dbus_message_iter_next(&sub);
782 static int dot_one(DBusConnection *bus, const char *name, const char *path) {
783 DBusMessage *m = NULL, *reply = NULL;
784 const char *interface = "org.freedesktop.systemd1.Unit";
787 DBusMessageIter iter, sub, sub2, sub3;
792 dbus_error_init(&error);
794 if (!(m = dbus_message_new_method_call(
795 "org.freedesktop.systemd1",
797 "org.freedesktop.DBus.Properties",
799 log_error("Could not allocate message.");
804 if (!dbus_message_append_args(m,
805 DBUS_TYPE_STRING, &interface,
806 DBUS_TYPE_INVALID)) {
807 log_error("Could not append arguments to message.");
812 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
813 log_error("Failed to issue method call: %s", bus_error_message(&error));
818 if (!dbus_message_iter_init(reply, &iter) ||
819 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
820 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
821 log_error("Failed to parse reply.");
826 dbus_message_iter_recurse(&iter, &sub);
828 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
831 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
832 log_error("Failed to parse reply.");
837 dbus_message_iter_recurse(&sub, &sub2);
839 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &prop, true) < 0) {
840 log_error("Failed to parse reply.");
845 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
846 log_error("Failed to parse reply.");
851 dbus_message_iter_recurse(&sub2, &sub3);
853 if (dot_one_property(name, prop, &sub3)) {
854 log_error("Failed to parse reply.");
859 dbus_message_iter_next(&sub);
866 dbus_message_unref(m);
869 dbus_message_unref(reply);
871 dbus_error_free(&error);
876 static int dot(DBusConnection *bus, char **args) {
877 DBusMessage *m = NULL, *reply = NULL;
880 DBusMessageIter iter, sub, sub2;
882 dbus_error_init(&error);
886 if (!(m = dbus_message_new_method_call(
887 "org.freedesktop.systemd1",
888 "/org/freedesktop/systemd1",
889 "org.freedesktop.systemd1.Manager",
891 log_error("Could not allocate message.");
895 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
896 log_error("Failed to issue method call: %s", bus_error_message(&error));
901 if (!dbus_message_iter_init(reply, &iter) ||
902 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
903 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
904 log_error("Failed to parse reply.");
909 printf("digraph systemd {\n");
911 dbus_message_iter_recurse(&iter, &sub);
912 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
913 const char *id, *description, *load_state, *active_state, *sub_state, *following, *unit_path;
915 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
916 log_error("Failed to parse reply.");
921 dbus_message_iter_recurse(&sub, &sub2);
923 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
924 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
925 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
926 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
927 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
928 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &following, true) < 0 ||
929 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, true) < 0) {
930 log_error("Failed to parse reply.");
935 if ((r = dot_one(bus, id, unit_path)) < 0)
938 /* printf("\t\"%s\";\n", id); */
939 dbus_message_iter_next(&sub);
944 log_info(" Color legend: black = Requires\n"
945 " dark blue = Requisite\n"
946 " dark grey = Wants\n"
951 log_notice("-- You probably want to process this output with graphviz' dot tool.\n"
952 "-- Try a shell pipeline like 'systemctl dot | dot -Tsvg > systemd.svg'!\n");
958 dbus_message_unref(m);
961 dbus_message_unref(reply);
963 dbus_error_free(&error);
968 static int list_jobs(DBusConnection *bus, char **args) {
969 DBusMessage *m = NULL, *reply = NULL;
972 DBusMessageIter iter, sub, sub2;
975 dbus_error_init(&error);
979 pager_open_if_enabled();
981 if (!(m = dbus_message_new_method_call(
982 "org.freedesktop.systemd1",
983 "/org/freedesktop/systemd1",
984 "org.freedesktop.systemd1.Manager",
986 log_error("Could not allocate message.");
990 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
991 log_error("Failed to issue method call: %s", bus_error_message(&error));
996 if (!dbus_message_iter_init(reply, &iter) ||
997 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
998 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
999 log_error("Failed to parse reply.");
1004 dbus_message_iter_recurse(&iter, &sub);
1007 printf("%4s %-25s %-15s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
1009 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1010 const char *name, *type, *state, *job_path, *unit_path;
1014 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
1015 log_error("Failed to parse reply.");
1020 dbus_message_iter_recurse(&sub, &sub2);
1022 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
1023 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
1024 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
1025 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
1026 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
1027 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
1028 log_error("Failed to parse reply.");
1033 e = arg_full ? NULL : ellipsize(name, 25, 33);
1034 printf("%4u %-25s %-15s %-7s\n", id, e ? e : name, type, state);
1039 dbus_message_iter_next(&sub);
1043 printf("\n%u jobs listed.\n", k);
1049 dbus_message_unref(m);
1052 dbus_message_unref(reply);
1054 dbus_error_free(&error);
1059 static int load_unit(DBusConnection *bus, char **args) {
1060 DBusMessage *m = NULL;
1065 dbus_error_init(&error);
1070 STRV_FOREACH(name, args+1) {
1073 if (!(m = dbus_message_new_method_call(
1074 "org.freedesktop.systemd1",
1075 "/org/freedesktop/systemd1",
1076 "org.freedesktop.systemd1.Manager",
1078 log_error("Could not allocate message.");
1083 if (!dbus_message_append_args(m,
1084 DBUS_TYPE_STRING, name,
1085 DBUS_TYPE_INVALID)) {
1086 log_error("Could not append arguments to message.");
1091 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1092 log_error("Failed to issue method call: %s", bus_error_message(&error));
1097 dbus_message_unref(m);
1098 dbus_message_unref(reply);
1107 dbus_message_unref(m);
1109 dbus_error_free(&error);
1114 static int cancel_job(DBusConnection *bus, char **args) {
1115 DBusMessage *m = NULL, *reply = NULL;
1120 dbus_error_init(&error);
1125 if (strv_length(args) <= 1)
1126 return daemon_reload(bus, args);
1128 STRV_FOREACH(name, args+1) {
1132 if (!(m = dbus_message_new_method_call(
1133 "org.freedesktop.systemd1",
1134 "/org/freedesktop/systemd1",
1135 "org.freedesktop.systemd1.Manager",
1137 log_error("Could not allocate message.");
1142 if ((r = safe_atou(*name, &id)) < 0) {
1143 log_error("Failed to parse job id: %s", strerror(-r));
1147 assert_cc(sizeof(uint32_t) == sizeof(id));
1148 if (!dbus_message_append_args(m,
1149 DBUS_TYPE_UINT32, &id,
1150 DBUS_TYPE_INVALID)) {
1151 log_error("Could not append arguments to message.");
1156 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1157 log_error("Failed to issue method call: %s", bus_error_message(&error));
1162 if (!dbus_message_get_args(reply, &error,
1163 DBUS_TYPE_OBJECT_PATH, &path,
1164 DBUS_TYPE_INVALID)) {
1165 log_error("Failed to parse reply: %s", bus_error_message(&error));
1170 dbus_message_unref(m);
1171 if (!(m = dbus_message_new_method_call(
1172 "org.freedesktop.systemd1",
1174 "org.freedesktop.systemd1.Job",
1176 log_error("Could not allocate message.");
1181 dbus_message_unref(reply);
1182 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1183 log_error("Failed to issue method call: %s", bus_error_message(&error));
1188 dbus_message_unref(m);
1189 dbus_message_unref(reply);
1197 dbus_message_unref(m);
1200 dbus_message_unref(reply);
1202 dbus_error_free(&error);
1207 static bool need_daemon_reload(DBusConnection *bus, const char *unit) {
1208 DBusMessage *m = NULL, *reply = NULL;
1209 dbus_bool_t b = FALSE;
1210 DBusMessageIter iter, sub;
1212 *interface = "org.freedesktop.systemd1.Unit",
1213 *property = "NeedDaemonReload",
1216 /* We ignore all errors here, since this is used to show a warning only */
1218 if (!(m = dbus_message_new_method_call(
1219 "org.freedesktop.systemd1",
1220 "/org/freedesktop/systemd1",
1221 "org.freedesktop.systemd1.Manager",
1225 if (!dbus_message_append_args(m,
1226 DBUS_TYPE_STRING, &unit,
1230 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
1233 if (!dbus_message_get_args(reply, NULL,
1234 DBUS_TYPE_OBJECT_PATH, &path,
1238 dbus_message_unref(m);
1239 if (!(m = dbus_message_new_method_call(
1240 "org.freedesktop.systemd1",
1242 "org.freedesktop.DBus.Properties",
1246 if (!dbus_message_append_args(m,
1247 DBUS_TYPE_STRING, &interface,
1248 DBUS_TYPE_STRING, &property,
1249 DBUS_TYPE_INVALID)) {
1253 dbus_message_unref(reply);
1254 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
1257 if (!dbus_message_iter_init(reply, &iter) ||
1258 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
1261 dbus_message_iter_recurse(&iter, &sub);
1263 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_BOOLEAN)
1266 dbus_message_iter_get_basic(&sub, &b);
1270 dbus_message_unref(m);
1273 dbus_message_unref(reply);
1278 typedef struct WaitData {
1283 static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
1291 dbus_error_init(&error);
1293 log_debug("Got D-Bus request: %s.%s() on %s",
1294 dbus_message_get_interface(message),
1295 dbus_message_get_member(message),
1296 dbus_message_get_path(message));
1298 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
1299 log_error("Warning! D-Bus connection terminated.");
1300 dbus_connection_close(connection);
1302 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
1304 const char *path, *result;
1305 dbus_bool_t success = true;
1307 if (dbus_message_get_args(message, &error,
1308 DBUS_TYPE_UINT32, &id,
1309 DBUS_TYPE_OBJECT_PATH, &path,
1310 DBUS_TYPE_STRING, &result,
1311 DBUS_TYPE_INVALID)) {
1314 if ((p = set_remove(d->set, (char*) path)))
1318 d->result = strdup(result);
1323 dbus_error_free(&error);
1325 if (dbus_message_get_args(message, &error,
1326 DBUS_TYPE_UINT32, &id,
1327 DBUS_TYPE_OBJECT_PATH, &path,
1328 DBUS_TYPE_BOOLEAN, &success,
1329 DBUS_TYPE_INVALID)) {
1332 /* Compatibility with older systemd versions <
1333 * 19 during upgrades. This should be dropped
1336 if ((p = set_remove(d->set, (char*) path)))
1340 d->result = strdup("failed");
1346 log_error("Failed to parse message: %s", bus_error_message(&error));
1350 dbus_error_free(&error);
1351 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1354 static int enable_wait_for_jobs(DBusConnection *bus) {
1362 dbus_error_init(&error);
1363 dbus_bus_add_match(bus,
1365 "sender='org.freedesktop.systemd1',"
1366 "interface='org.freedesktop.systemd1.Manager',"
1367 "member='JobRemoved',"
1368 "path='/org/freedesktop/systemd1'",
1371 if (dbus_error_is_set(&error)) {
1372 log_error("Failed to add match: %s", bus_error_message(&error));
1373 dbus_error_free(&error);
1377 /* This is slightly dirty, since we don't undo the match registrations. */
1381 static int wait_for_jobs(DBusConnection *bus, Set *s) {
1391 if (!dbus_connection_add_filter(bus, wait_filter, &d, NULL)) {
1392 log_error("Failed to add filter.");
1397 while (!set_isempty(s) &&
1398 dbus_connection_read_write_dispatch(bus, -1))
1401 if (!arg_quiet && d.result) {
1402 if (streq(d.result, "timeout"))
1403 log_error("Job timed out.");
1404 else if (streq(d.result, "canceled"))
1405 log_error("Job canceled.");
1406 else if (streq(d.result, "dependency"))
1407 log_error("A dependency job failed. See system logs for details.");
1408 else if (!streq(d.result, "done") && !streq(d.result, "skipped"))
1409 log_error("Job failed. See system logs and 'systemctl status' for details.");
1412 if (streq_ptr(d.result, "timeout"))
1414 else if (streq_ptr(d.result, "canceled"))
1416 else if (!streq_ptr(d.result, "done") && !streq_ptr(d.result, "skipped"))
1424 /* This is slightly dirty, since we don't undo the filter registration. */
1429 static int start_unit_one(
1430 DBusConnection *bus,
1437 DBusMessage *m = NULL, *reply = NULL;
1446 assert(arg_no_block || s);
1448 if (!(m = dbus_message_new_method_call(
1449 "org.freedesktop.systemd1",
1450 "/org/freedesktop/systemd1",
1451 "org.freedesktop.systemd1.Manager",
1453 log_error("Could not allocate message.");
1458 if (!dbus_message_append_args(m,
1459 DBUS_TYPE_STRING, &name,
1460 DBUS_TYPE_STRING, &mode,
1461 DBUS_TYPE_INVALID)) {
1462 log_error("Could not append arguments to message.");
1467 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, error))) {
1469 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(error)) {
1470 /* There's always a fallback possible for
1471 * legacy actions. */
1476 log_error("Failed to issue method call: %s", bus_error_message(error));
1481 if (!dbus_message_get_args(reply, error,
1482 DBUS_TYPE_OBJECT_PATH, &path,
1483 DBUS_TYPE_INVALID)) {
1484 log_error("Failed to parse reply: %s", bus_error_message(error));
1489 if (need_daemon_reload(bus, name))
1490 log_warning("Warning: Unit file of created job changed on disk, 'systemctl %s daemon-reload' recommended.",
1491 arg_scope == UNIT_FILE_SYSTEM ? "--system" : "--user");
1493 if (!arg_no_block) {
1496 if (!(p = strdup(path))) {
1497 log_error("Failed to duplicate path.");
1502 if ((r = set_put(s, p)) < 0) {
1504 log_error("Failed to add path to set.");
1513 dbus_message_unref(m);
1516 dbus_message_unref(reply);
1521 static enum action verb_to_action(const char *verb) {
1522 if (streq(verb, "halt"))
1524 else if (streq(verb, "poweroff"))
1525 return ACTION_POWEROFF;
1526 else if (streq(verb, "reboot"))
1527 return ACTION_REBOOT;
1528 else if (streq(verb, "kexec"))
1529 return ACTION_KEXEC;
1530 else if (streq(verb, "rescue"))
1531 return ACTION_RESCUE;
1532 else if (streq(verb, "emergency"))
1533 return ACTION_EMERGENCY;
1534 else if (streq(verb, "default"))
1535 return ACTION_DEFAULT;
1536 else if (streq(verb, "exit"))
1539 return ACTION_INVALID;
1542 static int start_unit(DBusConnection *bus, char **args) {
1544 static const char * const table[_ACTION_MAX] = {
1545 [ACTION_HALT] = SPECIAL_HALT_TARGET,
1546 [ACTION_POWEROFF] = SPECIAL_POWEROFF_TARGET,
1547 [ACTION_REBOOT] = SPECIAL_REBOOT_TARGET,
1548 [ACTION_KEXEC] = SPECIAL_KEXEC_TARGET,
1549 [ACTION_RUNLEVEL2] = SPECIAL_RUNLEVEL2_TARGET,
1550 [ACTION_RUNLEVEL3] = SPECIAL_RUNLEVEL3_TARGET,
1551 [ACTION_RUNLEVEL4] = SPECIAL_RUNLEVEL4_TARGET,
1552 [ACTION_RUNLEVEL5] = SPECIAL_RUNLEVEL5_TARGET,
1553 [ACTION_RESCUE] = SPECIAL_RESCUE_TARGET,
1554 [ACTION_EMERGENCY] = SPECIAL_EMERGENCY_TARGET,
1555 [ACTION_DEFAULT] = SPECIAL_DEFAULT_TARGET,
1556 [ACTION_EXIT] = SPECIAL_EXIT_TARGET
1560 const char *method, *mode, *one_name;
1565 dbus_error_init(&error);
1569 agent_open_if_enabled();
1571 if (arg_action == ACTION_SYSTEMCTL) {
1573 streq(args[0], "stop") ||
1574 streq(args[0], "condstop") ? "StopUnit" :
1575 streq(args[0], "reload") ? "ReloadUnit" :
1576 streq(args[0], "restart") ? "RestartUnit" :
1578 streq(args[0], "try-restart") ||
1579 streq(args[0], "condrestart") ? "TryRestartUnit" :
1581 streq(args[0], "reload-or-restart") ? "ReloadOrRestartUnit" :
1583 streq(args[0], "reload-or-try-restart") ||
1584 streq(args[0], "condreload") ||
1586 streq(args[0], "force-reload") ? "ReloadOrTryRestartUnit" :
1590 (streq(args[0], "isolate") ||
1591 streq(args[0], "rescue") ||
1592 streq(args[0], "emergency")) ? "isolate" : arg_job_mode;
1594 one_name = table[verb_to_action(args[0])];
1597 assert(arg_action < ELEMENTSOF(table));
1598 assert(table[arg_action]);
1600 method = "StartUnit";
1602 mode = (arg_action == ACTION_EMERGENCY ||
1603 arg_action == ACTION_RESCUE ||
1604 arg_action == ACTION_RUNLEVEL2 ||
1605 arg_action == ACTION_RUNLEVEL3 ||
1606 arg_action == ACTION_RUNLEVEL4 ||
1607 arg_action == ACTION_RUNLEVEL5) ? "isolate" : "replace";
1609 one_name = table[arg_action];
1612 if (!arg_no_block) {
1613 if ((ret = enable_wait_for_jobs(bus)) < 0) {
1614 log_error("Could not watch jobs: %s", strerror(-ret));
1618 if (!(s = set_new(string_hash_func, string_compare_func))) {
1619 log_error("Failed to allocate set.");
1626 if ((ret = start_unit_one(bus, method, one_name, mode, &error, s)) <= 0)
1629 STRV_FOREACH(name, args+1)
1630 if ((r = start_unit_one(bus, method, *name, mode, &error, s)) != 0) {
1631 ret = translate_bus_error_to_exit_status(r, &error);
1632 dbus_error_free(&error);
1637 if ((r = wait_for_jobs(bus, s)) < 0) {
1646 dbus_error_free(&error);
1651 static int start_special(DBusConnection *bus, char **args) {
1658 (streq(args[0], "halt") ||
1659 streq(args[0], "poweroff") ||
1660 streq(args[0], "reboot") ||
1661 streq(args[0], "kexec") ||
1662 streq(args[0], "exit")))
1663 return daemon_reload(bus, args);
1665 r = start_unit(bus, args);
1668 warn_wall(verb_to_action(args[0]));
1673 static int check_unit(DBusConnection *bus, char **args) {
1674 DBusMessage *m = NULL, *reply = NULL;
1676 *interface = "org.freedesktop.systemd1.Unit",
1677 *property = "ActiveState";
1678 int r = 3; /* According to LSB: "program is not running" */
1685 dbus_error_init(&error);
1687 STRV_FOREACH(name, args+1) {
1688 const char *path = NULL;
1690 DBusMessageIter iter, sub;
1692 if (!(m = dbus_message_new_method_call(
1693 "org.freedesktop.systemd1",
1694 "/org/freedesktop/systemd1",
1695 "org.freedesktop.systemd1.Manager",
1697 log_error("Could not allocate message.");
1702 if (!dbus_message_append_args(m,
1703 DBUS_TYPE_STRING, name,
1704 DBUS_TYPE_INVALID)) {
1705 log_error("Could not append arguments to message.");
1710 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1712 /* Hmm, cannot figure out anything about this unit... */
1716 dbus_error_free(&error);
1717 dbus_message_unref(m);
1722 if (!dbus_message_get_args(reply, &error,
1723 DBUS_TYPE_OBJECT_PATH, &path,
1724 DBUS_TYPE_INVALID)) {
1725 log_error("Failed to parse reply: %s", bus_error_message(&error));
1730 dbus_message_unref(m);
1731 if (!(m = dbus_message_new_method_call(
1732 "org.freedesktop.systemd1",
1734 "org.freedesktop.DBus.Properties",
1736 log_error("Could not allocate message.");
1741 if (!dbus_message_append_args(m,
1742 DBUS_TYPE_STRING, &interface,
1743 DBUS_TYPE_STRING, &property,
1744 DBUS_TYPE_INVALID)) {
1745 log_error("Could not append arguments to message.");
1750 dbus_message_unref(reply);
1751 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1752 log_error("Failed to issue method call: %s", bus_error_message(&error));
1757 if (!dbus_message_iter_init(reply, &iter) ||
1758 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1759 log_error("Failed to parse reply.");
1764 dbus_message_iter_recurse(&iter, &sub);
1766 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1767 log_error("Failed to parse reply.");
1772 dbus_message_iter_get_basic(&sub, &state);
1777 if (streq(state, "active") || streq(state, "reloading"))
1780 dbus_message_unref(m);
1781 dbus_message_unref(reply);
1787 dbus_message_unref(m);
1790 dbus_message_unref(reply);
1792 dbus_error_free(&error);
1797 static int kill_unit(DBusConnection *bus, char **args) {
1798 DBusMessage *m = NULL;
1806 dbus_error_init(&error);
1809 arg_kill_who = "all";
1812 arg_kill_mode = streq(arg_kill_who, "all") ? "control-group" : "process";
1814 STRV_FOREACH(name, args+1) {
1817 if (!(m = dbus_message_new_method_call(
1818 "org.freedesktop.systemd1",
1819 "/org/freedesktop/systemd1",
1820 "org.freedesktop.systemd1.Manager",
1822 log_error("Could not allocate message.");
1827 if (!dbus_message_append_args(m,
1828 DBUS_TYPE_STRING, name,
1829 DBUS_TYPE_STRING, &arg_kill_who,
1830 DBUS_TYPE_STRING, &arg_kill_mode,
1831 DBUS_TYPE_INT32, &arg_signal,
1832 DBUS_TYPE_INVALID)) {
1833 log_error("Could not append arguments to message.");
1838 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1839 log_error("Failed to issue method call: %s", bus_error_message(&error));
1840 dbus_error_free(&error);
1844 dbus_message_unref(m);
1847 dbus_message_unref(reply);
1853 dbus_message_unref(m);
1855 dbus_error_free(&error);
1860 typedef struct ExecStatusInfo {
1868 usec_t start_timestamp;
1869 usec_t exit_timestamp;
1874 LIST_FIELDS(struct ExecStatusInfo, exec);
1877 static void exec_status_info_free(ExecStatusInfo *i) {
1886 static int exec_status_info_deserialize(DBusMessageIter *sub, ExecStatusInfo *i) {
1887 uint64_t start_timestamp, exit_timestamp, start_timestamp_monotonic, exit_timestamp_monotonic;
1888 DBusMessageIter sub2, sub3;
1892 int32_t code, status;
1898 if (dbus_message_iter_get_arg_type(sub) != DBUS_TYPE_STRUCT)
1901 dbus_message_iter_recurse(sub, &sub2);
1903 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
1906 if (!(i->path = strdup(path)))
1909 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
1910 dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
1914 dbus_message_iter_recurse(&sub2, &sub3);
1915 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1916 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1917 dbus_message_iter_next(&sub3);
1922 if (!(i->argv = new0(char*, n+1)))
1926 dbus_message_iter_recurse(&sub2, &sub3);
1927 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1930 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1931 dbus_message_iter_get_basic(&sub3, &s);
1932 dbus_message_iter_next(&sub3);
1934 if (!(i->argv[n++] = strdup(s)))
1938 if (!dbus_message_iter_next(&sub2) ||
1939 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, true) < 0 ||
1940 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp, true) < 0 ||
1941 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp_monotonic, true) < 0 ||
1942 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp, true) < 0 ||
1943 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp_monotonic, true) < 0 ||
1944 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) < 0 ||
1945 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) < 0 ||
1946 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) < 0)
1950 i->start_timestamp = (usec_t) start_timestamp;
1951 i->exit_timestamp = (usec_t) exit_timestamp;
1952 i->pid = (pid_t) pid;
1959 typedef struct UnitStatusInfo {
1961 const char *load_state;
1962 const char *active_state;
1963 const char *sub_state;
1964 const char *unit_file_state;
1966 const char *description;
1967 const char *following;
1970 const char *default_control_group;
1972 const char *load_error;
1974 usec_t inactive_exit_timestamp;
1975 usec_t active_enter_timestamp;
1976 usec_t active_exit_timestamp;
1977 usec_t inactive_enter_timestamp;
1979 bool need_daemon_reload;
1984 const char *status_text;
1986 #ifdef HAVE_SYSV_COMPAT
1990 usec_t start_timestamp;
1991 usec_t exit_timestamp;
1993 int exit_code, exit_status;
1995 usec_t condition_timestamp;
1996 bool condition_result;
1999 unsigned n_accepted;
2000 unsigned n_connections;
2004 const char *sysfs_path;
2006 /* Mount, Automount */
2012 LIST_HEAD(ExecStatusInfo, exec);
2015 static void print_status_info(UnitStatusInfo *i) {
2017 const char *on, *off, *ss;
2019 char since1[FORMAT_TIMESTAMP_PRETTY_MAX], *s1;
2020 char since2[FORMAT_TIMESTAMP_MAX], *s2;
2024 /* This shows pretty information about a unit. See
2025 * print_property() for a low-level property printer */
2027 printf("%s", strna(i->id));
2029 if (i->description && !streq_ptr(i->id, i->description))
2030 printf(" - %s", i->description);
2035 printf("\t Follow: unit currently follows state of %s\n", i->following);
2037 if (streq_ptr(i->load_state, "failed") ||
2038 streq_ptr(i->load_state, "banned")) {
2039 on = ansi_highlight(true);
2040 off = ansi_highlight(false);
2045 printf("\t Loaded: %s%s%s (Reason: %s)\n", on, strna(i->load_state), off, i->load_error);
2046 else if (i->path && i->unit_file_state)
2047 printf("\t Loaded: %s%s%s (%s; %s)\n", on, strna(i->load_state), off, i->path, i->unit_file_state);
2049 printf("\t Loaded: %s%s%s (%s)\n", on, strna(i->load_state), off, i->path);
2051 printf("\t Loaded: %s%s%s\n", on, strna(i->load_state), off);
2053 ss = streq_ptr(i->active_state, i->sub_state) ? NULL : i->sub_state;
2055 if (streq_ptr(i->active_state, "failed")) {
2056 on = ansi_highlight(true);
2057 off = ansi_highlight(false);
2058 } else if (streq_ptr(i->active_state, "active") || streq_ptr(i->active_state, "reloading")) {
2059 on = ansi_highlight_green(true);
2060 off = ansi_highlight_green(false);
2065 printf("\t Active: %s%s (%s)%s",
2067 strna(i->active_state),
2071 printf("\t Active: %s%s%s",
2073 strna(i->active_state),
2076 timestamp = (streq_ptr(i->active_state, "active") ||
2077 streq_ptr(i->active_state, "reloading")) ? i->active_enter_timestamp :
2078 (streq_ptr(i->active_state, "inactive") ||
2079 streq_ptr(i->active_state, "failed")) ? i->inactive_enter_timestamp :
2080 streq_ptr(i->active_state, "activating") ? i->inactive_exit_timestamp :
2081 i->active_exit_timestamp;
2083 s1 = format_timestamp_pretty(since1, sizeof(since1), timestamp);
2084 s2 = format_timestamp(since2, sizeof(since2), timestamp);
2087 printf(" since %s; %s\n", s2, s1);
2089 printf(" since %s\n", s2);
2093 if (!i->condition_result && i->condition_timestamp > 0) {
2094 s1 = format_timestamp_pretty(since1, sizeof(since1), i->condition_timestamp);
2095 s2 = format_timestamp(since2, sizeof(since2), i->condition_timestamp);
2098 printf("\t start condition failed at %s; %s\n", s2, s1);
2100 printf("\t start condition failed at %s\n", s2);
2104 printf("\t Device: %s\n", i->sysfs_path);
2106 printf("\t Where: %s\n", i->where);
2108 printf("\t What: %s\n", i->what);
2111 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
2113 LIST_FOREACH(exec, p, i->exec) {
2117 /* Only show exited processes here */
2121 t = strv_join(p->argv, " ");
2122 printf("\t Process: %u %s=%s ", p->pid, p->name, strna(t));
2125 #ifdef HAVE_SYSV_COMPAT
2127 good = is_clean_exit_lsb(p->code, p->status);
2130 good = is_clean_exit(p->code, p->status);
2133 on = ansi_highlight(true);
2134 off = ansi_highlight(false);
2138 printf("%s(code=%s, ", on, sigchld_code_to_string(p->code));
2140 if (p->code == CLD_EXITED) {
2143 printf("status=%i", p->status);
2145 #ifdef HAVE_SYSV_COMPAT
2146 if ((c = exit_status_to_string(p->status, i->is_sysv ? EXIT_STATUS_LSB : EXIT_STATUS_SYSTEMD)))
2148 if ((c = exit_status_to_string(p->status, EXIT_STATUS_SYSTEMD)))
2153 printf("signal=%s", signal_to_string(p->status));
2155 printf(")%s\n", off);
2157 if (i->main_pid == p->pid &&
2158 i->start_timestamp == p->start_timestamp &&
2159 i->exit_timestamp == p->start_timestamp)
2160 /* Let's not show this twice */
2163 if (p->pid == i->control_pid)
2167 if (i->main_pid > 0 || i->control_pid > 0) {
2170 if (i->main_pid > 0) {
2171 printf("Main PID: %u", (unsigned) i->main_pid);
2175 get_process_name(i->main_pid, &t);
2180 } else if (i->exit_code > 0) {
2181 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
2183 if (i->exit_code == CLD_EXITED) {
2186 printf("status=%i", i->exit_status);
2188 #ifdef HAVE_SYSV_COMPAT
2189 if ((c = exit_status_to_string(i->exit_status, i->is_sysv ? EXIT_STATUS_LSB : EXIT_STATUS_SYSTEMD)))
2191 if ((c = exit_status_to_string(i->exit_status, EXIT_STATUS_SYSTEMD)))
2196 printf("signal=%s", signal_to_string(i->exit_status));
2201 if (i->main_pid > 0 && i->control_pid > 0)
2204 if (i->control_pid > 0) {
2207 printf(" Control: %u", (unsigned) i->control_pid);
2209 get_process_name(i->control_pid, &t);
2220 printf("\t Status: \"%s\"\n", i->status_text);
2222 if (i->default_control_group) {
2225 printf("\t CGroup: %s\n", i->default_control_group);
2227 if (arg_transport != TRANSPORT_SSH) {
2228 if ((c = columns()) > 18)
2233 show_cgroup_by_path(i->default_control_group, "\t\t ", c);
2237 if (i->need_daemon_reload)
2238 printf("\n%sWarning:%s Unit file changed on disk, 'systemctl %s daemon-reload' recommended.\n",
2239 ansi_highlight(true),
2240 ansi_highlight(false),
2241 arg_scope == UNIT_FILE_SYSTEM ? "--system" : "--user");
2244 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
2250 switch (dbus_message_iter_get_arg_type(iter)) {
2252 case DBUS_TYPE_STRING: {
2255 dbus_message_iter_get_basic(iter, &s);
2258 if (streq(name, "Id"))
2260 else if (streq(name, "LoadState"))
2262 else if (streq(name, "ActiveState"))
2263 i->active_state = s;
2264 else if (streq(name, "SubState"))
2266 else if (streq(name, "Description"))
2268 else if (streq(name, "FragmentPath"))
2270 #ifdef HAVE_SYSV_COMPAT
2271 else if (streq(name, "SysVPath")) {
2276 else if (streq(name, "DefaultControlGroup"))
2277 i->default_control_group = s;
2278 else if (streq(name, "StatusText"))
2280 else if (streq(name, "SysFSPath"))
2282 else if (streq(name, "Where"))
2284 else if (streq(name, "What"))
2286 else if (streq(name, "Following"))
2288 else if (streq(name, "UnitFileState"))
2289 i->unit_file_state = s;
2295 case DBUS_TYPE_BOOLEAN: {
2298 dbus_message_iter_get_basic(iter, &b);
2300 if (streq(name, "Accept"))
2302 else if (streq(name, "NeedDaemonReload"))
2303 i->need_daemon_reload = b;
2304 else if (streq(name, "ConditionResult"))
2305 i->condition_result = b;
2310 case DBUS_TYPE_UINT32: {
2313 dbus_message_iter_get_basic(iter, &u);
2315 if (streq(name, "MainPID")) {
2317 i->main_pid = (pid_t) u;
2320 } else if (streq(name, "ControlPID"))
2321 i->control_pid = (pid_t) u;
2322 else if (streq(name, "ExecMainPID")) {
2324 i->main_pid = (pid_t) u;
2325 } else if (streq(name, "NAccepted"))
2327 else if (streq(name, "NConnections"))
2328 i->n_connections = u;
2333 case DBUS_TYPE_INT32: {
2336 dbus_message_iter_get_basic(iter, &j);
2338 if (streq(name, "ExecMainCode"))
2339 i->exit_code = (int) j;
2340 else if (streq(name, "ExecMainStatus"))
2341 i->exit_status = (int) j;
2346 case DBUS_TYPE_UINT64: {
2349 dbus_message_iter_get_basic(iter, &u);
2351 if (streq(name, "ExecMainStartTimestamp"))
2352 i->start_timestamp = (usec_t) u;
2353 else if (streq(name, "ExecMainExitTimestamp"))
2354 i->exit_timestamp = (usec_t) u;
2355 else if (streq(name, "ActiveEnterTimestamp"))
2356 i->active_enter_timestamp = (usec_t) u;
2357 else if (streq(name, "InactiveEnterTimestamp"))
2358 i->inactive_enter_timestamp = (usec_t) u;
2359 else if (streq(name, "InactiveExitTimestamp"))
2360 i->inactive_exit_timestamp = (usec_t) u;
2361 else if (streq(name, "ActiveExitTimestamp"))
2362 i->active_exit_timestamp = (usec_t) u;
2363 else if (streq(name, "ConditionTimestamp"))
2364 i->condition_timestamp = (usec_t) u;
2369 case DBUS_TYPE_ARRAY: {
2371 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT &&
2372 startswith(name, "Exec")) {
2373 DBusMessageIter sub;
2375 dbus_message_iter_recurse(iter, &sub);
2376 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2377 ExecStatusInfo *info;
2380 if (!(info = new0(ExecStatusInfo, 1)))
2383 if (!(info->name = strdup(name))) {
2388 if ((r = exec_status_info_deserialize(&sub, info)) < 0) {
2393 LIST_PREPEND(ExecStatusInfo, exec, i->exec, info);
2395 dbus_message_iter_next(&sub);
2402 case DBUS_TYPE_STRUCT: {
2404 if (streq(name, "LoadError")) {
2405 DBusMessageIter sub;
2406 const char *n, *message;
2409 dbus_message_iter_recurse(iter, &sub);
2411 r = bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &n, true);
2415 r = bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &message, false);
2419 if (!isempty(message))
2420 i->load_error = message;
2430 static int print_property(const char *name, DBusMessageIter *iter) {
2434 /* This is a low-level property printer, see
2435 * print_status_info() for the nicer output */
2437 if (arg_property && !strv_find(arg_property, name))
2440 switch (dbus_message_iter_get_arg_type(iter)) {
2442 case DBUS_TYPE_STRUCT: {
2443 DBusMessageIter sub;
2444 dbus_message_iter_recurse(iter, &sub);
2446 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
2449 dbus_message_iter_get_basic(&sub, &u);
2452 printf("%s=%u\n", name, (unsigned) u);
2454 printf("%s=\n", name);
2457 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
2460 dbus_message_iter_get_basic(&sub, &s);
2462 if (arg_all || s[0])
2463 printf("%s=%s\n", name, s);
2466 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "LoadError")) {
2467 const char *a = NULL, *b = NULL;
2469 if (bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &a, true) >= 0)
2470 bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &b, false);
2472 if (arg_all || !isempty(a) || !isempty(b))
2473 printf("%s=%s \"%s\"\n", name, strempty(a), strempty(b));
2481 case DBUS_TYPE_ARRAY:
2483 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "EnvironmentFiles")) {
2484 DBusMessageIter sub, sub2;
2486 dbus_message_iter_recurse(iter, &sub);
2487 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2491 dbus_message_iter_recurse(&sub, &sub2);
2493 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) >= 0 &&
2494 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, false) >= 0)
2495 printf("EnvironmentFile=%s (ignore_errors=%s)\n", path, yes_no(ignore));
2497 dbus_message_iter_next(&sub);
2502 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
2503 DBusMessageIter sub, sub2;
2505 dbus_message_iter_recurse(iter, &sub);
2506 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2507 const char *type, *path;
2509 dbus_message_iter_recurse(&sub, &sub2);
2511 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
2512 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
2513 printf("%s=%s\n", type, path);
2515 dbus_message_iter_next(&sub);
2520 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
2521 DBusMessageIter sub, sub2;
2523 dbus_message_iter_recurse(iter, &sub);
2524 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2526 uint64_t value, next_elapse;
2528 dbus_message_iter_recurse(&sub, &sub2);
2530 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
2531 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
2532 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
2533 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
2535 printf("%s={ value=%s ; next_elapse=%s }\n",
2537 format_timespan(timespan1, sizeof(timespan1), value),
2538 format_timespan(timespan2, sizeof(timespan2), next_elapse));
2541 dbus_message_iter_next(&sub);
2546 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "ControlGroupAttributes")) {
2547 DBusMessageIter sub, sub2;
2549 dbus_message_iter_recurse(iter, &sub);
2550 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2551 const char *controller, *attr, *value;
2553 dbus_message_iter_recurse(&sub, &sub2);
2555 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &controller, true) >= 0 &&
2556 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &attr, true) >= 0 &&
2557 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &value, false) >= 0) {
2559 printf("ControlGroupAttribute={ controller=%s ; attribute=%s ; value=\"%s\" }\n",
2565 dbus_message_iter_next(&sub);
2570 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
2571 DBusMessageIter sub;
2573 dbus_message_iter_recurse(iter, &sub);
2574 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2575 ExecStatusInfo info;
2578 if (exec_status_info_deserialize(&sub, &info) >= 0) {
2579 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
2582 t = strv_join(info.argv, " ");
2584 printf("%s={ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n",
2588 yes_no(info.ignore),
2589 strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)),
2590 strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)),
2591 (unsigned) info. pid,
2592 sigchld_code_to_string(info.code),
2594 info.code == CLD_EXITED ? "" : "/",
2595 strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status)));
2601 strv_free(info.argv);
2603 dbus_message_iter_next(&sub);
2612 if (generic_print_property(name, iter, arg_all) > 0)
2616 printf("%s=[unprintable]\n", name);
2621 static int show_one(const char *verb, DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
2622 DBusMessage *m = NULL, *reply = NULL;
2623 const char *interface = "";
2626 DBusMessageIter iter, sub, sub2, sub3;
2627 UnitStatusInfo info;
2635 dbus_error_init(&error);
2637 if (!(m = dbus_message_new_method_call(
2638 "org.freedesktop.systemd1",
2640 "org.freedesktop.DBus.Properties",
2642 log_error("Could not allocate message.");
2647 if (!dbus_message_append_args(m,
2648 DBUS_TYPE_STRING, &interface,
2649 DBUS_TYPE_INVALID)) {
2650 log_error("Could not append arguments to message.");
2655 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2656 log_error("Failed to issue method call: %s", bus_error_message(&error));
2661 if (!dbus_message_iter_init(reply, &iter) ||
2662 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
2663 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
2664 log_error("Failed to parse reply.");
2669 dbus_message_iter_recurse(&iter, &sub);
2676 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2679 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
2680 log_error("Failed to parse reply.");
2685 dbus_message_iter_recurse(&sub, &sub2);
2687 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
2688 log_error("Failed to parse reply.");
2693 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
2694 log_error("Failed to parse reply.");
2699 dbus_message_iter_recurse(&sub2, &sub3);
2701 if (show_properties)
2702 r = print_property(name, &sub3);
2704 r = status_property(name, &sub3, &info);
2707 log_error("Failed to parse reply.");
2712 dbus_message_iter_next(&sub);
2717 if (!show_properties)
2718 print_status_info(&info);
2720 if (!streq_ptr(info.active_state, "active") &&
2721 !streq_ptr(info.active_state, "reloading") &&
2722 streq(verb, "status"))
2723 /* According to LSB: "program not running" */
2726 while ((p = info.exec)) {
2727 LIST_REMOVE(ExecStatusInfo, exec, info.exec, p);
2728 exec_status_info_free(p);
2733 dbus_message_unref(m);
2736 dbus_message_unref(reply);
2738 dbus_error_free(&error);
2743 static int show(DBusConnection *bus, char **args) {
2744 DBusMessage *m = NULL, *reply = NULL;
2747 bool show_properties, new_line = false;
2753 dbus_error_init(&error);
2755 show_properties = !streq(args[0], "status");
2757 if (show_properties)
2758 pager_open_if_enabled();
2760 if (show_properties && strv_length(args) <= 1) {
2761 /* If not argument is specified inspect the manager
2764 ret = show_one(args[0], bus, "/org/freedesktop/systemd1", show_properties, &new_line);
2768 STRV_FOREACH(name, args+1) {
2769 const char *path = NULL;
2772 if (safe_atou32(*name, &id) < 0) {
2774 /* Interpret as unit name */
2776 if (!(m = dbus_message_new_method_call(
2777 "org.freedesktop.systemd1",
2778 "/org/freedesktop/systemd1",
2779 "org.freedesktop.systemd1.Manager",
2781 log_error("Could not allocate message.");
2786 if (!dbus_message_append_args(m,
2787 DBUS_TYPE_STRING, name,
2788 DBUS_TYPE_INVALID)) {
2789 log_error("Could not append arguments to message.");
2794 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2796 if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
2797 log_error("Failed to issue method call: %s", bus_error_message(&error));
2802 dbus_error_free(&error);
2804 dbus_message_unref(m);
2805 if (!(m = dbus_message_new_method_call(
2806 "org.freedesktop.systemd1",
2807 "/org/freedesktop/systemd1",
2808 "org.freedesktop.systemd1.Manager",
2810 log_error("Could not allocate message.");
2815 if (!dbus_message_append_args(m,
2816 DBUS_TYPE_STRING, name,
2817 DBUS_TYPE_INVALID)) {
2818 log_error("Could not append arguments to message.");
2823 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2824 log_error("Failed to issue method call: %s", bus_error_message(&error));
2826 if (dbus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT))
2827 ret = 4; /* According to LSB: "program or service status is unknown" */
2834 } else if (show_properties) {
2836 /* Interpret as job id */
2838 if (!(m = dbus_message_new_method_call(
2839 "org.freedesktop.systemd1",
2840 "/org/freedesktop/systemd1",
2841 "org.freedesktop.systemd1.Manager",
2843 log_error("Could not allocate message.");
2848 if (!dbus_message_append_args(m,
2849 DBUS_TYPE_UINT32, &id,
2850 DBUS_TYPE_INVALID)) {
2851 log_error("Could not append arguments to message.");
2856 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2857 log_error("Failed to issue method call: %s", bus_error_message(&error));
2863 /* Interpret as PID */
2865 if (!(m = dbus_message_new_method_call(
2866 "org.freedesktop.systemd1",
2867 "/org/freedesktop/systemd1",
2868 "org.freedesktop.systemd1.Manager",
2870 log_error("Could not allocate message.");
2875 if (!dbus_message_append_args(m,
2876 DBUS_TYPE_UINT32, &id,
2877 DBUS_TYPE_INVALID)) {
2878 log_error("Could not append arguments to message.");
2883 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2884 log_error("Failed to issue method call: %s", bus_error_message(&error));
2890 if (!dbus_message_get_args(reply, &error,
2891 DBUS_TYPE_OBJECT_PATH, &path,
2892 DBUS_TYPE_INVALID)) {
2893 log_error("Failed to parse reply: %s", bus_error_message(&error));
2898 if ((r = show_one(args[0], bus, path, show_properties, &new_line)) != 0)
2901 dbus_message_unref(m);
2902 dbus_message_unref(reply);
2908 dbus_message_unref(m);
2911 dbus_message_unref(reply);
2913 dbus_error_free(&error);
2918 static int dump(DBusConnection *bus, char **args) {
2919 DBusMessage *m = NULL, *reply = NULL;
2924 dbus_error_init(&error);
2926 pager_open_if_enabled();
2928 if (!(m = dbus_message_new_method_call(
2929 "org.freedesktop.systemd1",
2930 "/org/freedesktop/systemd1",
2931 "org.freedesktop.systemd1.Manager",
2933 log_error("Could not allocate message.");
2937 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2938 log_error("Failed to issue method call: %s", bus_error_message(&error));
2943 if (!dbus_message_get_args(reply, &error,
2944 DBUS_TYPE_STRING, &text,
2945 DBUS_TYPE_INVALID)) {
2946 log_error("Failed to parse reply: %s", bus_error_message(&error));
2951 fputs(text, stdout);
2957 dbus_message_unref(m);
2960 dbus_message_unref(reply);
2962 dbus_error_free(&error);
2967 static int snapshot(DBusConnection *bus, char **args) {
2968 DBusMessage *m = NULL, *reply = NULL;
2971 const char *name = "", *path, *id;
2972 dbus_bool_t cleanup = FALSE;
2973 DBusMessageIter iter, sub;
2975 *interface = "org.freedesktop.systemd1.Unit",
2978 dbus_error_init(&error);
2980 if (!(m = dbus_message_new_method_call(
2981 "org.freedesktop.systemd1",
2982 "/org/freedesktop/systemd1",
2983 "org.freedesktop.systemd1.Manager",
2984 "CreateSnapshot"))) {
2985 log_error("Could not allocate message.");
2989 if (strv_length(args) > 1)
2992 if (!dbus_message_append_args(m,
2993 DBUS_TYPE_STRING, &name,
2994 DBUS_TYPE_BOOLEAN, &cleanup,
2995 DBUS_TYPE_INVALID)) {
2996 log_error("Could not append arguments to message.");
3001 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3002 log_error("Failed to issue method call: %s", bus_error_message(&error));
3007 if (!dbus_message_get_args(reply, &error,
3008 DBUS_TYPE_OBJECT_PATH, &path,
3009 DBUS_TYPE_INVALID)) {
3010 log_error("Failed to parse reply: %s", bus_error_message(&error));
3015 dbus_message_unref(m);
3016 if (!(m = dbus_message_new_method_call(
3017 "org.freedesktop.systemd1",
3019 "org.freedesktop.DBus.Properties",
3021 log_error("Could not allocate message.");
3025 if (!dbus_message_append_args(m,
3026 DBUS_TYPE_STRING, &interface,
3027 DBUS_TYPE_STRING, &property,
3028 DBUS_TYPE_INVALID)) {
3029 log_error("Could not append arguments to message.");
3034 dbus_message_unref(reply);
3035 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3036 log_error("Failed to issue method call: %s", bus_error_message(&error));
3041 if (!dbus_message_iter_init(reply, &iter) ||
3042 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
3043 log_error("Failed to parse reply.");
3048 dbus_message_iter_recurse(&iter, &sub);
3050 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
3051 log_error("Failed to parse reply.");
3056 dbus_message_iter_get_basic(&sub, &id);
3064 dbus_message_unref(m);
3067 dbus_message_unref(reply);
3069 dbus_error_free(&error);
3074 static int delete_snapshot(DBusConnection *bus, char **args) {
3075 DBusMessage *m = NULL, *reply = NULL;
3083 dbus_error_init(&error);
3085 STRV_FOREACH(name, args+1) {
3086 const char *path = NULL;
3088 if (!(m = dbus_message_new_method_call(
3089 "org.freedesktop.systemd1",
3090 "/org/freedesktop/systemd1",
3091 "org.freedesktop.systemd1.Manager",
3093 log_error("Could not allocate message.");
3098 if (!dbus_message_append_args(m,
3099 DBUS_TYPE_STRING, name,
3100 DBUS_TYPE_INVALID)) {
3101 log_error("Could not append arguments to message.");
3106 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3107 log_error("Failed to issue method call: %s", bus_error_message(&error));
3112 if (!dbus_message_get_args(reply, &error,
3113 DBUS_TYPE_OBJECT_PATH, &path,
3114 DBUS_TYPE_INVALID)) {
3115 log_error("Failed to parse reply: %s", bus_error_message(&error));
3120 dbus_message_unref(m);
3121 if (!(m = dbus_message_new_method_call(
3122 "org.freedesktop.systemd1",
3124 "org.freedesktop.systemd1.Snapshot",
3126 log_error("Could not allocate message.");
3131 dbus_message_unref(reply);
3132 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3133 log_error("Failed to issue method call: %s", bus_error_message(&error));
3138 dbus_message_unref(m);
3139 dbus_message_unref(reply);
3147 dbus_message_unref(m);
3150 dbus_message_unref(reply);
3152 dbus_error_free(&error);
3157 static int daemon_reload(DBusConnection *bus, char **args) {
3158 DBusMessage *m = NULL, *reply = NULL;
3163 dbus_error_init(&error);
3165 if (arg_action == ACTION_RELOAD)
3167 else if (arg_action == ACTION_REEXEC)
3168 method = "Reexecute";
3170 assert(arg_action == ACTION_SYSTEMCTL);
3173 streq(args[0], "clear-jobs") ||
3174 streq(args[0], "cancel") ? "ClearJobs" :
3175 streq(args[0], "daemon-reexec") ? "Reexecute" :
3176 streq(args[0], "reset-failed") ? "ResetFailed" :
3177 streq(args[0], "halt") ? "Halt" :
3178 streq(args[0], "poweroff") ? "PowerOff" :
3179 streq(args[0], "reboot") ? "Reboot" :
3180 streq(args[0], "kexec") ? "KExec" :
3181 streq(args[0], "exit") ? "Exit" :
3182 /* "daemon-reload" */ "Reload";
3185 if (!(m = dbus_message_new_method_call(
3186 "org.freedesktop.systemd1",
3187 "/org/freedesktop/systemd1",
3188 "org.freedesktop.systemd1.Manager",
3190 log_error("Could not allocate message.");
3194 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3196 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
3197 /* There's always a fallback possible for
3198 * legacy actions. */
3203 if (streq(method, "Reexecute") && dbus_error_has_name(&error, DBUS_ERROR_NO_REPLY)) {
3204 /* On reexecution, we expect a disconnect, not
3210 log_error("Failed to issue method call: %s", bus_error_message(&error));
3219 dbus_message_unref(m);
3222 dbus_message_unref(reply);
3224 dbus_error_free(&error);
3229 static int reset_failed(DBusConnection *bus, char **args) {
3230 DBusMessage *m = NULL;
3236 dbus_error_init(&error);
3238 if (strv_length(args) <= 1)
3239 return daemon_reload(bus, args);
3241 STRV_FOREACH(name, args+1) {
3244 if (!(m = dbus_message_new_method_call(
3245 "org.freedesktop.systemd1",
3246 "/org/freedesktop/systemd1",
3247 "org.freedesktop.systemd1.Manager",
3248 "ResetFailedUnit"))) {
3249 log_error("Could not allocate message.");
3254 if (!dbus_message_append_args(m,
3255 DBUS_TYPE_STRING, name,
3256 DBUS_TYPE_INVALID)) {
3257 log_error("Could not append arguments to message.");
3262 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3263 log_error("Failed to issue method call: %s", bus_error_message(&error));
3268 dbus_message_unref(m);
3269 dbus_message_unref(reply);
3277 dbus_message_unref(m);
3279 dbus_error_free(&error);
3284 static int show_enviroment(DBusConnection *bus, char **args) {
3285 DBusMessage *m = NULL, *reply = NULL;
3287 DBusMessageIter iter, sub, sub2;
3290 *interface = "org.freedesktop.systemd1.Manager",
3291 *property = "Environment";
3293 dbus_error_init(&error);
3295 pager_open_if_enabled();
3297 if (!(m = dbus_message_new_method_call(
3298 "org.freedesktop.systemd1",
3299 "/org/freedesktop/systemd1",
3300 "org.freedesktop.DBus.Properties",
3302 log_error("Could not allocate message.");
3306 if (!dbus_message_append_args(m,
3307 DBUS_TYPE_STRING, &interface,
3308 DBUS_TYPE_STRING, &property,
3309 DBUS_TYPE_INVALID)) {
3310 log_error("Could not append arguments to message.");
3315 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3316 log_error("Failed to issue method call: %s", bus_error_message(&error));
3321 if (!dbus_message_iter_init(reply, &iter) ||
3322 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
3323 log_error("Failed to parse reply.");
3328 dbus_message_iter_recurse(&iter, &sub);
3330 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
3331 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
3332 log_error("Failed to parse reply.");
3337 dbus_message_iter_recurse(&sub, &sub2);
3339 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
3342 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
3343 log_error("Failed to parse reply.");
3348 dbus_message_iter_get_basic(&sub2, &text);
3349 printf("%s\n", text);
3351 dbus_message_iter_next(&sub2);
3358 dbus_message_unref(m);
3361 dbus_message_unref(reply);
3363 dbus_error_free(&error);
3368 static int set_environment(DBusConnection *bus, char **args) {
3369 DBusMessage *m = NULL, *reply = NULL;
3373 DBusMessageIter iter, sub;
3376 dbus_error_init(&error);
3378 method = streq(args[0], "set-environment")
3380 : "UnsetEnvironment";
3382 if (!(m = dbus_message_new_method_call(
3383 "org.freedesktop.systemd1",
3384 "/org/freedesktop/systemd1",
3385 "org.freedesktop.systemd1.Manager",
3388 log_error("Could not allocate message.");
3392 dbus_message_iter_init_append(m, &iter);
3394 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
3395 log_error("Could not append arguments to message.");
3400 STRV_FOREACH(name, args+1)
3401 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, name)) {
3402 log_error("Could not append arguments to message.");
3407 if (!dbus_message_iter_close_container(&iter, &sub)) {
3408 log_error("Could not append arguments to message.");
3413 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3414 log_error("Failed to issue method call: %s", bus_error_message(&error));
3423 dbus_message_unref(m);
3426 dbus_message_unref(reply);
3428 dbus_error_free(&error);
3433 static int enable_sysv_units(char **args) {
3436 #if defined (HAVE_SYSV_COMPAT) && (defined(TARGET_FEDORA) || defined(TARGET_MANDRIVA) || defined(TARGET_SUSE) || defined(TARGET_MEEGO) || defined(TARGET_ALTLINUX))
3437 const char *verb = args[0];
3438 unsigned f = 1, t = 1;
3441 if (arg_scope != UNIT_FILE_SYSTEM)
3444 if (!streq(verb, "enable") &&
3445 !streq(verb, "disable") &&
3446 !streq(verb, "is-enabled"))
3449 /* Processes all SysV units, and reshuffles the array so that
3450 * afterwards only the native units remain */
3453 r = lookup_paths_init(&paths, MANAGER_SYSTEM, false);
3459 for (f = 1; args[f]; f++) {
3462 bool found_native = false, found_sysv;
3464 const char *argv[6] = { "/sbin/chkconfig", NULL, NULL, NULL, NULL };
3465 char **k, *l, *q = NULL;
3472 if (!endswith(name, ".service"))
3475 if (path_is_absolute(name))
3478 STRV_FOREACH(k, paths.unit_path) {
3481 if (!isempty(arg_root))
3482 asprintf(&p, "%s/%s/%s", arg_root, *k, name);
3484 asprintf(&p, "%s/%s", *k, name);
3487 log_error("No memory");
3492 found_native = access(p, F_OK) >= 0;
3503 if (!isempty(arg_root))
3504 asprintf(&p, "%s/" SYSTEM_SYSVINIT_PATH "/%s", arg_root, name);
3506 asprintf(&p, SYSTEM_SYSVINIT_PATH "/%s", name);
3508 log_error("No memory");
3513 p[strlen(p) - sizeof(".service") + 1] = 0;
3514 found_sysv = access(p, F_OK) >= 0;
3521 /* Mark this entry, so that we don't try enabling it as native unit */
3522 args[f] = (char*) "";
3524 log_info("%s is not a native service, redirecting to /sbin/chkconfig.", name);
3526 if (!isempty(arg_root))
3527 argv[c++] = q = strappend("--root=", arg_root);
3529 argv[c++] = file_name_from_path(p);
3531 streq(verb, "enable") ? "on" :
3532 streq(verb, "disable") ? "off" : "--level=5";
3535 l = strv_join((char**)argv, " ");
3537 log_error("No memory.");
3544 log_info("Executing %s", l);
3549 log_error("Failed to fork: %m");
3554 } else if (pid == 0) {
3557 execv(argv[0], (char**) argv);
3558 _exit(EXIT_FAILURE);
3564 j = wait_for_terminate(pid, &status);
3566 log_error("Failed to wait for child: %s", strerror(-r));
3571 if (status.si_code == CLD_EXITED) {
3572 if (streq(verb, "is-enabled")) {
3573 if (status.si_status == 0) {
3582 } else if (status.si_status != 0) {
3593 lookup_paths_free(&paths);
3595 /* Drop all SysV units */
3596 for (f = 1, t = 1; args[f]; f++) {
3598 if (isempty(args[f]))
3601 args[t++] = args[f];
3610 static int enable_unit(DBusConnection *bus, char **args) {
3611 const char *verb = args[0];
3612 UnitFileChange *changes = NULL;
3613 unsigned n_changes = 0, i;
3614 int carries_install_info = -1;
3615 DBusMessage *m = NULL, *reply = NULL;
3619 dbus_error_init(&error);
3621 r = enable_sysv_units(args);
3625 if (!bus || avoid_bus()) {
3626 if (streq(verb, "enable")) {
3627 r = unit_file_enable(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3628 carries_install_info = r;
3629 } else if (streq(verb, "disable"))
3630 r = unit_file_disable(arg_scope, arg_runtime, arg_root, args+1, &changes, &n_changes);
3631 else if (streq(verb, "reenable")) {
3632 r = unit_file_reenable(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3633 carries_install_info = r;
3634 } else if (streq(verb, "link"))
3635 r = unit_file_link(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3636 else if (streq(verb, "preset")) {
3637 r = unit_file_preset(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3638 carries_install_info = r;
3639 } else if (streq(verb, "mask"))
3640 r = unit_file_mask(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3641 else if (streq(verb, "unmask"))
3642 r = unit_file_unmask(arg_scope, arg_runtime, arg_root, args+1, &changes, &n_changes);
3644 assert_not_reached("Unknown verb");
3647 log_error("Operation failed: %s", strerror(-r));
3651 for (i = 0; i < n_changes; i++) {
3652 if (changes[i].type == UNIT_FILE_SYMLINK)
3653 log_info("ln -s '%s' '%s'", changes[i].source, changes[i].path);
3655 log_info("rm '%s'", changes[i].path);
3660 bool send_force = true, expect_carries_install_info = false;
3662 DBusMessageIter iter, sub, sub2;
3664 if (streq(verb, "enable")) {
3665 method = "EnableUnitFiles";
3666 expect_carries_install_info = true;
3667 } else if (streq(verb, "disable")) {
3668 method = "DisableUnitFiles";
3670 } else if (streq(verb, "reenable")) {
3671 method = "ReenableUnitFiles";
3672 expect_carries_install_info = true;
3673 } else if (streq(verb, "link"))
3674 method = "LinkUnitFiles";
3675 else if (streq(verb, "preset")) {
3676 method = "PresetUnitFiles";
3677 expect_carries_install_info = true;
3678 } else if (streq(verb, "mask"))
3679 method = "MaskUnitFiles";
3680 else if (streq(verb, "unmask")) {
3681 method = "UnmaskUnitFiles";
3684 assert_not_reached("Unknown verb");
3686 m = dbus_message_new_method_call(
3687 "org.freedesktop.systemd1",
3688 "/org/freedesktop/systemd1",
3689 "org.freedesktop.systemd1.Manager",
3692 log_error("Out of memory");
3697 dbus_message_iter_init_append(m, &iter);
3699 r = bus_append_strv_iter(&iter, args+1);
3701 log_error("Failed to append unit files.");
3706 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &a)) {
3707 log_error("Failed to append runtime boolean.");
3715 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b)) {
3716 log_error("Failed to append force boolean.");
3722 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
3724 log_error("Failed to issue method call: %s", bus_error_message(&error));
3729 if (!dbus_message_iter_init(reply, &iter)) {
3730 log_error("Failed to initialize iterator.");
3734 if (expect_carries_install_info) {
3735 r = bus_iter_get_basic_and_next(&iter, DBUS_TYPE_BOOLEAN, &b, true);
3737 log_error("Failed to parse reply.");
3741 carries_install_info = b;
3744 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
3745 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
3746 log_error("Failed to parse reply.");
3751 dbus_message_iter_recurse(&iter, &sub);
3752 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
3753 const char *type, *path, *source;
3755 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
3756 log_error("Failed to parse reply.");
3761 dbus_message_iter_recurse(&sub, &sub2);
3763 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
3764 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0 ||
3765 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &source, false) < 0) {
3766 log_error("Failed to parse reply.");
3771 if (streq(type, "symlink"))
3772 log_info("ln -s '%s' '%s'", source, path);
3774 log_info("rm '%s'", path);
3776 dbus_message_iter_next(&sub);
3779 /* Try to reload if enabeld */
3781 r = daemon_reload(bus, args);
3784 if (carries_install_info == 0)
3785 log_warning("Warning: unit files do not carry install information. No operation executed.");
3789 dbus_message_unref(m);
3792 dbus_message_unref(reply);
3794 unit_file_changes_free(changes, n_changes);
3796 dbus_error_free(&error);
3800 static int unit_is_enabled(DBusConnection *bus, char **args) {
3803 DBusMessage *m = NULL, *reply = NULL;
3807 dbus_error_init(&error);
3809 r = enable_sysv_units(args);
3815 if (!bus || avoid_bus()) {
3817 STRV_FOREACH(name, args+1) {
3818 UnitFileState state;
3820 state = unit_file_get_state(arg_scope, arg_root, *name);
3826 if (state == UNIT_FILE_ENABLED ||
3827 state == UNIT_FILE_ENABLED_RUNTIME ||
3828 state == UNIT_FILE_STATIC)
3832 puts(unit_file_state_to_string(state));
3836 STRV_FOREACH(name, args+1) {
3839 m = dbus_message_new_method_call(
3840 "org.freedesktop.systemd1",
3841 "/org/freedesktop/systemd1",
3842 "org.freedesktop.systemd1.Manager",
3843 "GetUnitFileState");
3845 log_error("Out of memory");
3850 if (!dbus_message_append_args(m,
3851 DBUS_TYPE_STRING, name,
3852 DBUS_TYPE_INVALID)) {
3853 log_error("Could not append arguments to message.");
3858 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
3860 log_error("Failed to issue method call: %s", bus_error_message(&error));
3865 if (!dbus_message_get_args(reply, &error,
3866 DBUS_TYPE_STRING, &s,
3867 DBUS_TYPE_INVALID)) {
3868 log_error("Failed to parse reply: %s", bus_error_message(&error));
3873 dbus_message_unref(m);
3874 dbus_message_unref(reply);
3877 if (streq(s, "enabled") ||
3878 streq(s, "enabled-runtime") ||
3887 r = enabled ? 0 : 1;
3891 dbus_message_unref(m);
3894 dbus_message_unref(reply);
3896 dbus_error_free(&error);
3900 static int systemctl_help(void) {
3902 pager_open_if_enabled();
3904 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
3905 "Query or send control commands to the systemd manager.\n\n"
3906 " -h --help Show this help\n"
3907 " --version Show package version\n"
3908 " -t --type=TYPE List only units of a particular type\n"
3909 " -p --property=NAME Show only properties by this name\n"
3910 " -a --all Show all units/properties, including dead/empty ones\n"
3911 " --failed Show only failed units\n"
3912 " --full Don't ellipsize unit names on output\n"
3913 " --fail When queueing a new job, fail if conflicting jobs are\n"
3915 " --ignore-dependencies\n"
3916 " When queueing a new job, ignore all its dependencies\n"
3917 " --kill-who=WHO Who to send signal to\n"
3918 " -s --signal=SIGNAL Which signal to send\n"
3919 " -H --host=[USER@]HOST\n"
3920 " Show information for remote host\n"
3921 " -P --privileged Acquire privileges before execution\n"
3922 " -q --quiet Suppress output\n"
3923 " --no-block Do not wait until operation finished\n"
3924 " --no-wall Don't send wall message before halt/power-off/reboot\n"
3925 " --no-reload When enabling/disabling unit files, don't reload daemon\n"
3927 " --no-pager Do not pipe output into a pager\n"
3928 " --no-ask-password\n"
3929 " Do not ask for system passwords\n"
3930 " --order When generating graph for dot, show only order\n"
3931 " --require When generating graph for dot, show only requirement\n"
3932 " --system Connect to system manager\n"
3933 " --user Connect to user service manager\n"
3934 " --global Enable/disable unit files globally\n"
3935 " -f --force When enabling unit files, override existing symlinks\n"
3936 " When shutting down, execute action immediately\n"
3937 " --root=PATH Enable unit files in the specified root directory\n"
3938 " --runtime Enable unit files only temporarily until next reboot\n\n"
3940 " list-units List loaded units\n"
3941 " start [NAME...] Start (activate) one or more units\n"
3942 " stop [NAME...] Stop (deactivate) one or more units\n"
3943 " reload [NAME...] Reload one or more units\n"
3944 " restart [NAME...] Start or restart one or more units\n"
3945 " try-restart [NAME...] Restart one or more units if active\n"
3946 " reload-or-restart [NAME...] Reload one or more units is possible,\n"
3947 " otherwise start or restart\n"
3948 " reload-or-try-restart [NAME...] Reload one or more units is possible,\n"
3949 " otherwise restart if active\n"
3950 " isolate [NAME] Start one unit and stop all others\n"
3951 " kill [NAME...] Send signal to processes of a unit\n"
3952 " is-active [NAME...] Check whether units are active\n"
3953 " status [NAME...|PID...] Show runtime status of one or more units\n"
3954 " show [NAME...|JOB...] Show properties of one or more\n"
3955 " units/jobs or the manager\n"
3956 " reset-failed [NAME...] Reset failed state for all, one, or more\n"
3958 " load [NAME...] Load one or more units\n\n"
3959 "Unit File Commands:\n"
3960 " list-unit-files List installed unit files\n"
3961 " enable [NAME...] Enable one or more unit files\n"
3962 " disable [NAME...] Disable one or more unit files\n"
3963 " reenable [NAME...] Reenable one or more unit files\n"
3964 " preset [NAME...] Enable/disable one or more unit files\n"
3965 " based on preset configuration\n"
3966 " mask [NAME...] Mask one or more units\n"
3967 " unmask [NAME...] Unmask one or more units\n"
3968 " link [PATH...] Link one or more units files into\n"
3969 " the search path\n"
3970 " is-enabled [NAME...] Check whether unit files are enabled\n\n"
3972 " list-jobs List jobs\n"
3973 " cancel [JOB...] Cancel all, one, or more jobs\n\n"
3974 "Status Commands:\n"
3975 " dump Dump server status\n"
3976 " dot Dump dependency graph for dot(1)\n\n"
3977 "Snapshot Commands:\n"
3978 " snapshot [NAME] Create a snapshot\n"
3979 " delete [NAME...] Remove one or more snapshots\n\n"
3980 "Environment Commands:\n"
3981 " show-environment Dump environment\n"
3982 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
3983 " unset-environment [NAME...] Unset one or more environment variables\n\n"
3984 "Manager Lifecycle Commands:\n"
3985 " daemon-reload Reload systemd manager configuration\n"
3986 " daemon-reexec Reexecute systemd manager\n\n"
3987 "System Commands:\n"
3988 " default Enter system default mode\n"
3989 " rescue Enter system rescue mode\n"
3990 " emergency Enter system emergency mode\n"
3991 " halt Shut down and halt the system\n"
3992 " poweroff Shut down and power-off the system\n"
3993 " reboot Shut down and reboot the system\n"
3994 " kexec Shut down and reboot the system with kexec\n"
3995 " exit Ask for user instance termination\n",
3996 program_invocation_short_name);
4001 static int halt_help(void) {
4003 printf("%s [OPTIONS...]\n\n"
4004 "%s the system.\n\n"
4005 " --help Show this help\n"
4006 " --halt Halt the machine\n"
4007 " -p --poweroff Switch off the machine\n"
4008 " --reboot Reboot the machine\n"
4009 " -f --force Force immediate halt/power-off/reboot\n"
4010 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
4011 " -d --no-wtmp Don't write wtmp record\n"
4012 " -n --no-sync Don't sync before halt/power-off/reboot\n"
4013 " --no-wall Don't send wall message before halt/power-off/reboot\n",
4014 program_invocation_short_name,
4015 arg_action == ACTION_REBOOT ? "Reboot" :
4016 arg_action == ACTION_POWEROFF ? "Power off" :
4022 static int shutdown_help(void) {
4024 printf("%s [OPTIONS...] [TIME] [WALL...]\n\n"
4025 "Shut down the system.\n\n"
4026 " --help Show this help\n"
4027 " -H --halt Halt the machine\n"
4028 " -P --poweroff Power-off the machine\n"
4029 " -r --reboot Reboot the machine\n"
4030 " -h Equivalent to --poweroff, overriden by --halt\n"
4031 " -k Don't halt/power-off/reboot, just send warnings\n"
4032 " --no-wall Don't send wall message before halt/power-off/reboot\n"
4033 " -c Cancel a pending shutdown\n",
4034 program_invocation_short_name);
4039 static int telinit_help(void) {
4041 printf("%s [OPTIONS...] {COMMAND}\n\n"
4042 "Send control commands to the init daemon.\n\n"
4043 " --help Show this help\n"
4044 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
4046 " 0 Power-off the machine\n"
4047 " 6 Reboot the machine\n"
4048 " 2, 3, 4, 5 Start runlevelX.target unit\n"
4049 " 1, s, S Enter rescue mode\n"
4050 " q, Q Reload init daemon configuration\n"
4051 " u, U Reexecute init daemon\n",
4052 program_invocation_short_name);
4057 static int runlevel_help(void) {
4059 printf("%s [OPTIONS...]\n\n"
4060 "Prints the previous and current runlevel of the init system.\n\n"
4061 " --help Show this help\n",
4062 program_invocation_short_name);
4067 static int systemctl_parse_argv(int argc, char *argv[]) {
4071 ARG_IGNORE_DEPENDENCIES,
4086 ARG_NO_ASK_PASSWORD,
4091 static const struct option options[] = {
4092 { "help", no_argument, NULL, 'h' },
4093 { "version", no_argument, NULL, ARG_VERSION },
4094 { "type", required_argument, NULL, 't' },
4095 { "property", required_argument, NULL, 'p' },
4096 { "all", no_argument, NULL, 'a' },
4097 { "failed", no_argument, NULL, ARG_FAILED },
4098 { "full", no_argument, NULL, ARG_FULL },
4099 { "fail", no_argument, NULL, ARG_FAIL },
4100 { "ignore-dependencies", no_argument, NULL, ARG_IGNORE_DEPENDENCIES },
4101 { "user", no_argument, NULL, ARG_USER },
4102 { "system", no_argument, NULL, ARG_SYSTEM },
4103 { "global", no_argument, NULL, ARG_GLOBAL },
4104 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
4105 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
4106 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4107 { "quiet", no_argument, NULL, 'q' },
4108 { "order", no_argument, NULL, ARG_ORDER },
4109 { "require", no_argument, NULL, ARG_REQUIRE },
4110 { "root", required_argument, NULL, ARG_ROOT },
4111 { "force", no_argument, NULL, 'f' },
4112 { "no-reload", no_argument, NULL, ARG_NO_RELOAD },
4113 { "kill-mode", required_argument, NULL, ARG_KILL_MODE }, /* undocumented on purpose */
4114 { "kill-who", required_argument, NULL, ARG_KILL_WHO },
4115 { "signal", required_argument, NULL, 's' },
4116 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
4117 { "host", required_argument, NULL, 'H' },
4118 { "privileged",no_argument, NULL, 'P' },
4119 { "runtime", no_argument, NULL, ARG_RUNTIME },
4120 { NULL, 0, NULL, 0 }
4128 /* Only when running as systemctl we ask for passwords */
4129 arg_ask_password = true;
4131 while ((c = getopt_long(argc, argv, "ht:p:aqfs:H:P", options, NULL)) >= 0) {
4140 puts(PACKAGE_STRING);
4142 puts(SYSTEMD_FEATURES);
4152 if (!(l = strv_append(arg_property, optarg)))
4155 strv_free(arg_property);
4158 /* If the user asked for a particular
4159 * property, show it to him, even if it is
4170 arg_job_mode = "fail";
4173 case ARG_IGNORE_DEPENDENCIES:
4174 arg_job_mode = "ignore-dependencies";
4178 arg_scope = UNIT_FILE_USER;
4182 arg_scope = UNIT_FILE_SYSTEM;
4186 arg_scope = UNIT_FILE_GLOBAL;
4190 arg_no_block = true;
4194 arg_no_pager = true;
4202 arg_dot = DOT_ORDER;
4206 arg_dot = DOT_REQUIRE;
4230 arg_no_reload = true;
4234 arg_kill_who = optarg;
4238 arg_kill_mode = optarg;
4242 if ((arg_signal = signal_from_string_try_harder(optarg)) < 0) {
4243 log_error("Failed to parse signal string %s.", optarg);
4248 case ARG_NO_ASK_PASSWORD:
4249 arg_ask_password = false;
4253 arg_transport = TRANSPORT_POLKIT;
4257 arg_transport = TRANSPORT_SSH;
4269 log_error("Unknown option code %c", c);
4274 if (arg_transport != TRANSPORT_NORMAL && arg_scope != UNIT_FILE_SYSTEM) {
4275 log_error("Cannot access user instance remotely.");
4282 static int halt_parse_argv(int argc, char *argv[]) {
4291 static const struct option options[] = {
4292 { "help", no_argument, NULL, ARG_HELP },
4293 { "halt", no_argument, NULL, ARG_HALT },
4294 { "poweroff", no_argument, NULL, 'p' },
4295 { "reboot", no_argument, NULL, ARG_REBOOT },
4296 { "force", no_argument, NULL, 'f' },
4297 { "wtmp-only", no_argument, NULL, 'w' },
4298 { "no-wtmp", no_argument, NULL, 'd' },
4299 { "no-sync", no_argument, NULL, 'n' },
4300 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4301 { NULL, 0, NULL, 0 }
4309 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
4310 if (runlevel == '0' || runlevel == '6')
4311 arg_immediate = true;
4313 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
4321 arg_action = ACTION_HALT;
4325 if (arg_action != ACTION_REBOOT)
4326 arg_action = ACTION_POWEROFF;
4330 arg_action = ACTION_REBOOT;
4334 arg_immediate = true;
4355 /* Compatibility nops */
4362 log_error("Unknown option code %c", c);
4367 if (optind < argc) {
4368 log_error("Too many arguments.");
4375 static int parse_time_spec(const char *t, usec_t *_u) {
4379 if (streq(t, "now"))
4381 else if (!strchr(t, ':')) {
4384 if (safe_atou64(t, &u) < 0)
4387 *_u = now(CLOCK_REALTIME) + USEC_PER_MINUTE * u;
4396 hour = strtol(t, &e, 10);
4397 if (errno != 0 || *e != ':' || hour < 0 || hour > 23)
4400 minute = strtol(e+1, &e, 10);
4401 if (errno != 0 || *e != 0 || minute < 0 || minute > 59)
4404 n = now(CLOCK_REALTIME);
4405 s = (time_t) (n / USEC_PER_SEC);
4408 assert_se(localtime_r(&s, &tm));
4410 tm.tm_hour = (int) hour;
4411 tm.tm_min = (int) minute;
4414 assert_se(s = mktime(&tm));
4416 *_u = (usec_t) s * USEC_PER_SEC;
4419 *_u += USEC_PER_DAY;
4425 static int shutdown_parse_argv(int argc, char *argv[]) {
4432 static const struct option options[] = {
4433 { "help", no_argument, NULL, ARG_HELP },
4434 { "halt", no_argument, NULL, 'H' },
4435 { "poweroff", no_argument, NULL, 'P' },
4436 { "reboot", no_argument, NULL, 'r' },
4437 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4438 { NULL, 0, NULL, 0 }
4446 while ((c = getopt_long(argc, argv, "HPrhkt:afFc", options, NULL)) >= 0) {
4454 arg_action = ACTION_HALT;
4458 arg_action = ACTION_POWEROFF;
4463 arg_action = ACTION_KEXEC;
4465 arg_action = ACTION_REBOOT;
4469 if (arg_action != ACTION_HALT)
4470 arg_action = ACTION_POWEROFF;
4483 /* Compatibility nops */
4487 arg_action = ACTION_CANCEL_SHUTDOWN;
4494 log_error("Unknown option code %c", c);
4499 if (argc > optind) {
4500 if ((r = parse_time_spec(argv[optind], &arg_when)) < 0) {
4501 log_error("Failed to parse time specification: %s", argv[optind]);
4505 arg_when = now(CLOCK_REALTIME) + USEC_PER_MINUTE;
4507 /* We skip the time argument */
4508 if (argc > optind + 1)
4509 arg_wall = argv + optind + 1;
4516 static int telinit_parse_argv(int argc, char *argv[]) {
4523 static const struct option options[] = {
4524 { "help", no_argument, NULL, ARG_HELP },
4525 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4526 { NULL, 0, NULL, 0 }
4529 static const struct {
4533 { '0', ACTION_POWEROFF },
4534 { '6', ACTION_REBOOT },
4535 { '1', ACTION_RESCUE },
4536 { '2', ACTION_RUNLEVEL2 },
4537 { '3', ACTION_RUNLEVEL3 },
4538 { '4', ACTION_RUNLEVEL4 },
4539 { '5', ACTION_RUNLEVEL5 },
4540 { 's', ACTION_RESCUE },
4541 { 'S', ACTION_RESCUE },
4542 { 'q', ACTION_RELOAD },
4543 { 'Q', ACTION_RELOAD },
4544 { 'u', ACTION_REEXEC },
4545 { 'U', ACTION_REEXEC }
4554 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4569 log_error("Unknown option code %c", c);
4574 if (optind >= argc) {
4579 if (optind + 1 < argc) {
4580 log_error("Too many arguments.");
4584 if (strlen(argv[optind]) != 1) {
4585 log_error("Expected single character argument.");
4589 for (i = 0; i < ELEMENTSOF(table); i++)
4590 if (table[i].from == argv[optind][0])
4593 if (i >= ELEMENTSOF(table)) {
4594 log_error("Unknown command %s.", argv[optind]);
4598 arg_action = table[i].to;
4605 static int runlevel_parse_argv(int argc, char *argv[]) {
4611 static const struct option options[] = {
4612 { "help", no_argument, NULL, ARG_HELP },
4613 { NULL, 0, NULL, 0 }
4621 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4632 log_error("Unknown option code %c", c);
4637 if (optind < argc) {
4638 log_error("Too many arguments.");
4645 static int parse_argv(int argc, char *argv[]) {
4649 if (program_invocation_short_name) {
4651 if (strstr(program_invocation_short_name, "halt")) {
4652 arg_action = ACTION_HALT;
4653 return halt_parse_argv(argc, argv);
4654 } else if (strstr(program_invocation_short_name, "poweroff")) {
4655 arg_action = ACTION_POWEROFF;
4656 return halt_parse_argv(argc, argv);
4657 } else if (strstr(program_invocation_short_name, "reboot")) {
4659 arg_action = ACTION_KEXEC;
4661 arg_action = ACTION_REBOOT;
4662 return halt_parse_argv(argc, argv);
4663 } else if (strstr(program_invocation_short_name, "shutdown")) {
4664 arg_action = ACTION_POWEROFF;
4665 return shutdown_parse_argv(argc, argv);
4666 } else if (strstr(program_invocation_short_name, "init")) {
4668 if (sd_booted() > 0) {
4669 arg_action = ACTION_INVALID;
4670 return telinit_parse_argv(argc, argv);
4672 /* Hmm, so some other init system is
4673 * running, we need to forward this
4674 * request to it. For now we simply
4675 * guess that it is Upstart. */
4677 execv("/lib/upstart/telinit", argv);
4679 log_error("Couldn't find an alternative telinit implementation to spawn.");
4683 } else if (strstr(program_invocation_short_name, "runlevel")) {
4684 arg_action = ACTION_RUNLEVEL;
4685 return runlevel_parse_argv(argc, argv);
4689 arg_action = ACTION_SYSTEMCTL;
4690 return systemctl_parse_argv(argc, argv);
4693 static int action_to_runlevel(void) {
4695 static const char table[_ACTION_MAX] = {
4696 [ACTION_HALT] = '0',
4697 [ACTION_POWEROFF] = '0',
4698 [ACTION_REBOOT] = '6',
4699 [ACTION_RUNLEVEL2] = '2',
4700 [ACTION_RUNLEVEL3] = '3',
4701 [ACTION_RUNLEVEL4] = '4',
4702 [ACTION_RUNLEVEL5] = '5',
4703 [ACTION_RESCUE] = '1'
4706 assert(arg_action < _ACTION_MAX);
4708 return table[arg_action];
4711 static int talk_upstart(void) {
4712 DBusMessage *m = NULL, *reply = NULL;
4714 int previous, rl, r;
4716 env1_buf[] = "RUNLEVEL=X",
4717 env2_buf[] = "PREVLEVEL=X";
4718 char *env1 = env1_buf, *env2 = env2_buf;
4719 const char *emit = "runlevel";
4720 dbus_bool_t b_false = FALSE;
4721 DBusMessageIter iter, sub;
4722 DBusConnection *bus;
4724 dbus_error_init(&error);
4726 if (!(rl = action_to_runlevel()))
4729 if (utmp_get_runlevel(&previous, NULL) < 0)
4732 if (!(bus = dbus_connection_open_private("unix:abstract=/com/ubuntu/upstart", &error))) {
4733 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
4738 log_error("Failed to connect to Upstart bus: %s", bus_error_message(&error));
4743 if ((r = bus_check_peercred(bus)) < 0) {
4744 log_error("Failed to verify owner of bus.");
4748 if (!(m = dbus_message_new_method_call(
4749 "com.ubuntu.Upstart",
4750 "/com/ubuntu/Upstart",
4751 "com.ubuntu.Upstart0_6",
4754 log_error("Could not allocate message.");
4759 dbus_message_iter_init_append(m, &iter);
4761 env1_buf[sizeof(env1_buf)-2] = rl;
4762 env2_buf[sizeof(env2_buf)-2] = previous;
4764 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
4765 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
4766 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
4767 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
4768 !dbus_message_iter_close_container(&iter, &sub) ||
4769 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
4770 log_error("Could not append arguments to message.");
4775 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
4777 if (error_is_no_service(&error)) {
4782 log_error("Failed to issue method call: %s", bus_error_message(&error));
4791 dbus_message_unref(m);
4794 dbus_message_unref(reply);
4797 dbus_connection_flush(bus);
4798 dbus_connection_close(bus);
4799 dbus_connection_unref(bus);
4802 dbus_error_free(&error);
4807 static int talk_initctl(void) {
4808 struct init_request request;
4812 if (!(rl = action_to_runlevel()))
4816 request.magic = INIT_MAGIC;
4817 request.sleeptime = 0;
4818 request.cmd = INIT_CMD_RUNLVL;
4819 request.runlevel = rl;
4821 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
4823 if (errno == ENOENT)
4826 log_error("Failed to open "INIT_FIFO": %m");
4831 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
4832 close_nointr_nofail(fd);
4835 log_error("Failed to write to "INIT_FIFO": %m");
4836 return errno ? -errno : -EIO;
4842 static int systemctl_main(DBusConnection *bus, int argc, char *argv[], DBusError *error) {
4844 static const struct {
4852 int (* const dispatch)(DBusConnection *bus, char **args);
4854 { "list-units", LESS, 1, list_units },
4855 { "list-unit-files", EQUAL, 1, list_unit_files },
4856 { "list-jobs", EQUAL, 1, list_jobs },
4857 { "clear-jobs", EQUAL, 1, daemon_reload },
4858 { "load", MORE, 2, load_unit },
4859 { "cancel", MORE, 2, cancel_job },
4860 { "start", MORE, 2, start_unit },
4861 { "stop", MORE, 2, start_unit },
4862 { "condstop", MORE, 2, start_unit }, /* For compatibility with ALTLinux */
4863 { "reload", MORE, 2, start_unit },
4864 { "restart", MORE, 2, start_unit },
4865 { "try-restart", MORE, 2, start_unit },
4866 { "reload-or-restart", MORE, 2, start_unit },
4867 { "reload-or-try-restart", MORE, 2, start_unit },
4868 { "force-reload", MORE, 2, start_unit }, /* For compatibility with SysV */
4869 { "condreload", MORE, 2, start_unit }, /* For compatibility with ALTLinux */
4870 { "condrestart", MORE, 2, start_unit }, /* For compatibility with RH */
4871 { "isolate", EQUAL, 2, start_unit },
4872 { "kill", MORE, 2, kill_unit },
4873 { "is-active", MORE, 2, check_unit },
4874 { "check", MORE, 2, check_unit },
4875 { "show", MORE, 1, show },
4876 { "status", MORE, 2, show },
4877 { "dump", EQUAL, 1, dump },
4878 { "dot", EQUAL, 1, dot },
4879 { "snapshot", LESS, 2, snapshot },
4880 { "delete", MORE, 2, delete_snapshot },
4881 { "daemon-reload", EQUAL, 1, daemon_reload },
4882 { "daemon-reexec", EQUAL, 1, daemon_reload },
4883 { "show-environment", EQUAL, 1, show_enviroment },
4884 { "set-environment", MORE, 2, set_environment },
4885 { "unset-environment", MORE, 2, set_environment },
4886 { "halt", EQUAL, 1, start_special },
4887 { "poweroff", EQUAL, 1, start_special },
4888 { "reboot", EQUAL, 1, start_special },
4889 { "kexec", EQUAL, 1, start_special },
4890 { "default", EQUAL, 1, start_special },
4891 { "rescue", EQUAL, 1, start_special },
4892 { "emergency", EQUAL, 1, start_special },
4893 { "exit", EQUAL, 1, start_special },
4894 { "reset-failed", MORE, 1, reset_failed },
4895 { "enable", MORE, 2, enable_unit },
4896 { "disable", MORE, 2, enable_unit },
4897 { "is-enabled", MORE, 2, unit_is_enabled },
4898 { "reenable", MORE, 2, enable_unit },
4899 { "preset", MORE, 2, enable_unit },
4900 { "mask", MORE, 2, enable_unit },
4901 { "unmask", MORE, 2, enable_unit },
4902 { "link", MORE, 2, enable_unit }
4912 left = argc - optind;
4915 /* Special rule: no arguments means "list-units" */
4918 if (streq(argv[optind], "help")) {
4923 for (i = 0; i < ELEMENTSOF(verbs); i++)
4924 if (streq(argv[optind], verbs[i].verb))
4927 if (i >= ELEMENTSOF(verbs)) {
4928 log_error("Unknown operation %s", argv[optind]);
4933 switch (verbs[i].argc_cmp) {
4936 if (left != verbs[i].argc) {
4937 log_error("Invalid number of arguments.");
4944 if (left < verbs[i].argc) {
4945 log_error("Too few arguments.");
4952 if (left > verbs[i].argc) {
4953 log_error("Too many arguments.");
4960 assert_not_reached("Unknown comparison operator.");
4963 /* Require a bus connection for all operations but
4965 if (!streq(verbs[i].verb, "enable") &&
4966 !streq(verbs[i].verb, "disable") &&
4967 !streq(verbs[i].verb, "is-enable") &&
4968 !streq(verbs[i].verb, "reenable") &&
4969 !streq(verbs[i].verb, "preset") &&
4970 !streq(verbs[i].verb, "mask") &&
4971 !streq(verbs[i].verb, "unmask") &&
4972 !streq(verbs[i].verb, "link")) {
4974 if (running_in_chroot() > 0) {
4975 log_info("Running in chroot, ignoring request.");
4979 if (!bus && !avoid_bus()) {
4980 log_error("Failed to get D-Bus connection: %s", error->message);
4985 return verbs[i].dispatch(bus, argv + optind);
4988 static int send_shutdownd(usec_t t, char mode, bool dry_run, bool warn, const char *message) {
4990 struct msghdr msghdr;
4992 union sockaddr_union sockaddr;
4993 struct shutdownd_command c;
4998 c.dry_run = dry_run;
5002 strncpy(c.wall_message, message, sizeof(c.wall_message));
5004 if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0)
5008 sockaddr.sa.sa_family = AF_UNIX;
5009 sockaddr.un.sun_path[0] = 0;
5010 strncpy(sockaddr.un.sun_path, "/run/systemd/shutdownd", sizeof(sockaddr.un.sun_path));
5013 iovec.iov_base = (char*) &c;
5014 iovec.iov_len = sizeof(c);
5017 msghdr.msg_name = &sockaddr;
5018 msghdr.msg_namelen = offsetof(struct sockaddr_un, sun_path) + sizeof("/run/systemd/shutdownd") - 1;
5020 msghdr.msg_iov = &iovec;
5021 msghdr.msg_iovlen = 1;
5023 if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) < 0) {
5024 close_nointr_nofail(fd);
5028 close_nointr_nofail(fd);
5032 static int reload_with_fallback(DBusConnection *bus) {
5035 /* First, try systemd via D-Bus. */
5036 if (daemon_reload(bus, NULL) >= 0)
5040 /* Nothing else worked, so let's try signals */
5041 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
5043 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
5044 log_error("kill() failed: %m");
5051 static int start_with_fallback(DBusConnection *bus) {
5054 /* First, try systemd via D-Bus. */
5055 if (start_unit(bus, NULL) >= 0)
5059 /* Hmm, talking to systemd via D-Bus didn't work. Then
5060 * let's try to talk to Upstart via D-Bus. */
5061 if (talk_upstart() > 0)
5064 /* Nothing else worked, so let's try
5066 if (talk_initctl() > 0)
5069 log_error("Failed to talk to init daemon.");
5073 warn_wall(arg_action);
5077 static int halt_main(DBusConnection *bus) {
5080 if (geteuid() != 0) {
5081 log_error("Must be root.");
5087 char date[FORMAT_TIMESTAMP_MAX];
5089 m = strv_join(arg_wall, " ");
5090 r = send_shutdownd(arg_when,
5091 arg_action == ACTION_HALT ? 'H' :
5092 arg_action == ACTION_POWEROFF ? 'P' :
5100 log_warning("Failed to talk to shutdownd, proceeding with immediate shutdown: %s", strerror(-r));
5102 log_info("Shutdown scheduled for %s, use 'shutdown -c' to cancel.",
5103 format_timestamp(date, sizeof(date), arg_when));
5108 if (!arg_dry && !arg_immediate)
5109 return start_with_fallback(bus);
5112 if (sd_booted() > 0)
5113 log_debug("Not writing utmp record, assuming that systemd-update-utmp is used.");
5114 else if ((r = utmp_put_shutdown(0)) < 0)
5115 log_warning("Failed to write utmp record: %s", strerror(-r));
5124 /* Make sure C-A-D is handled by the kernel from this
5126 reboot(RB_ENABLE_CAD);
5128 switch (arg_action) {
5131 log_info("Halting.");
5132 reboot(RB_HALT_SYSTEM);
5135 case ACTION_POWEROFF:
5136 log_info("Powering off.");
5137 reboot(RB_POWER_OFF);
5141 log_info("Rebooting.");
5142 reboot(RB_AUTOBOOT);
5146 assert_not_reached("Unknown halt action.");
5149 /* We should never reach this. */
5153 static int runlevel_main(void) {
5154 int r, runlevel, previous;
5156 r = utmp_get_runlevel(&runlevel, &previous);
5163 previous <= 0 ? 'N' : previous,
5164 runlevel <= 0 ? 'N' : runlevel);
5169 int main(int argc, char*argv[]) {
5170 int r, retval = EXIT_FAILURE;
5171 DBusConnection *bus = NULL;
5174 dbus_error_init(&error);
5176 log_parse_environment();
5179 if ((r = parse_argv(argc, argv)) < 0)
5182 retval = EXIT_SUCCESS;
5186 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
5187 * let's shortcut this */
5188 if (arg_action == ACTION_RUNLEVEL) {
5189 r = runlevel_main();
5190 retval = r < 0 ? EXIT_FAILURE : r;
5194 if (running_in_chroot() > 0 && arg_action != ACTION_SYSTEMCTL) {
5195 log_info("Running in chroot, ignoring request.");
5201 if (arg_transport == TRANSPORT_NORMAL)
5202 bus_connect(arg_scope == UNIT_FILE_SYSTEM ? DBUS_BUS_SYSTEM : DBUS_BUS_SESSION, &bus, &private_bus, &error);
5203 else if (arg_transport == TRANSPORT_POLKIT) {
5204 bus_connect_system_polkit(&bus, &error);
5205 private_bus = false;
5206 } else if (arg_transport == TRANSPORT_SSH) {
5207 bus_connect_system_ssh(NULL, arg_host, &bus, &error);
5208 private_bus = false;
5210 assert_not_reached("Uh, invalid transport...");
5213 switch (arg_action) {
5215 case ACTION_SYSTEMCTL:
5216 r = systemctl_main(bus, argc, argv, &error);
5220 case ACTION_POWEROFF:
5226 case ACTION_RUNLEVEL2:
5227 case ACTION_RUNLEVEL3:
5228 case ACTION_RUNLEVEL4:
5229 case ACTION_RUNLEVEL5:
5231 case ACTION_EMERGENCY:
5232 case ACTION_DEFAULT:
5233 r = start_with_fallback(bus);
5238 r = reload_with_fallback(bus);
5241 case ACTION_CANCEL_SHUTDOWN:
5242 r = send_shutdownd(0, 0, false, false, NULL);
5245 case ACTION_INVALID:
5246 case ACTION_RUNLEVEL:
5248 assert_not_reached("Unknown action");
5251 retval = r < 0 ? EXIT_FAILURE : r;
5255 dbus_connection_flush(bus);
5256 dbus_connection_close(bus);
5257 dbus_connection_unref(bus);
5260 dbus_error_free(&error);
5264 strv_free(arg_property);