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);
2159 if (i->main_pid == p->pid &&
2160 i->start_timestamp == p->start_timestamp &&
2161 i->exit_timestamp == p->start_timestamp)
2162 /* Let's not show this twice */
2165 if (p->pid == i->control_pid)
2169 if (i->main_pid > 0 || i->control_pid > 0) {
2172 if (i->main_pid > 0) {
2173 printf("Main PID: %u", (unsigned) i->main_pid);
2177 get_process_name(i->main_pid, &t);
2182 } else if (i->exit_code > 0) {
2183 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
2185 if (i->exit_code == CLD_EXITED) {
2188 printf("status=%i", i->exit_status);
2190 #ifdef HAVE_SYSV_COMPAT
2191 if ((c = exit_status_to_string(i->exit_status, i->is_sysv ? EXIT_STATUS_LSB : EXIT_STATUS_SYSTEMD)))
2193 if ((c = exit_status_to_string(i->exit_status, EXIT_STATUS_SYSTEMD)))
2198 printf("signal=%s", signal_to_string(i->exit_status));
2203 if (i->main_pid > 0 && i->control_pid > 0)
2206 if (i->control_pid > 0) {
2209 printf(" Control: %u", (unsigned) i->control_pid);
2211 get_process_name(i->control_pid, &t);
2222 printf("\t Status: \"%s\"\n", i->status_text);
2224 if (i->default_control_group) {
2227 printf("\t CGroup: %s\n", i->default_control_group);
2229 if (arg_transport != TRANSPORT_SSH) {
2230 if ((c = columns()) > 18)
2235 show_cgroup_by_path(i->default_control_group, "\t\t ", c);
2239 if (i->need_daemon_reload)
2240 printf("\n%sWarning:%s Unit file changed on disk, 'systemctl %s daemon-reload' recommended.\n",
2241 ansi_highlight(true),
2242 ansi_highlight(false),
2243 arg_scope == UNIT_FILE_SYSTEM ? "--system" : "--user");
2246 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
2252 switch (dbus_message_iter_get_arg_type(iter)) {
2254 case DBUS_TYPE_STRING: {
2257 dbus_message_iter_get_basic(iter, &s);
2260 if (streq(name, "Id"))
2262 else if (streq(name, "LoadState"))
2264 else if (streq(name, "ActiveState"))
2265 i->active_state = s;
2266 else if (streq(name, "SubState"))
2268 else if (streq(name, "Description"))
2270 else if (streq(name, "FragmentPath"))
2272 #ifdef HAVE_SYSV_COMPAT
2273 else if (streq(name, "SysVPath")) {
2278 else if (streq(name, "DefaultControlGroup"))
2279 i->default_control_group = s;
2280 else if (streq(name, "StatusText"))
2282 else if (streq(name, "SysFSPath"))
2284 else if (streq(name, "Where"))
2286 else if (streq(name, "What"))
2288 else if (streq(name, "Following"))
2290 else if (streq(name, "UnitFileState"))
2291 i->unit_file_state = s;
2297 case DBUS_TYPE_BOOLEAN: {
2300 dbus_message_iter_get_basic(iter, &b);
2302 if (streq(name, "Accept"))
2304 else if (streq(name, "NeedDaemonReload"))
2305 i->need_daemon_reload = b;
2306 else if (streq(name, "ConditionResult"))
2307 i->condition_result = b;
2312 case DBUS_TYPE_UINT32: {
2315 dbus_message_iter_get_basic(iter, &u);
2317 if (streq(name, "MainPID")) {
2319 i->main_pid = (pid_t) u;
2322 } else if (streq(name, "ControlPID"))
2323 i->control_pid = (pid_t) u;
2324 else if (streq(name, "ExecMainPID")) {
2326 i->main_pid = (pid_t) u;
2327 } else if (streq(name, "NAccepted"))
2329 else if (streq(name, "NConnections"))
2330 i->n_connections = u;
2335 case DBUS_TYPE_INT32: {
2338 dbus_message_iter_get_basic(iter, &j);
2340 if (streq(name, "ExecMainCode"))
2341 i->exit_code = (int) j;
2342 else if (streq(name, "ExecMainStatus"))
2343 i->exit_status = (int) j;
2348 case DBUS_TYPE_UINT64: {
2351 dbus_message_iter_get_basic(iter, &u);
2353 if (streq(name, "ExecMainStartTimestamp"))
2354 i->start_timestamp = (usec_t) u;
2355 else if (streq(name, "ExecMainExitTimestamp"))
2356 i->exit_timestamp = (usec_t) u;
2357 else if (streq(name, "ActiveEnterTimestamp"))
2358 i->active_enter_timestamp = (usec_t) u;
2359 else if (streq(name, "InactiveEnterTimestamp"))
2360 i->inactive_enter_timestamp = (usec_t) u;
2361 else if (streq(name, "InactiveExitTimestamp"))
2362 i->inactive_exit_timestamp = (usec_t) u;
2363 else if (streq(name, "ActiveExitTimestamp"))
2364 i->active_exit_timestamp = (usec_t) u;
2365 else if (streq(name, "ConditionTimestamp"))
2366 i->condition_timestamp = (usec_t) u;
2371 case DBUS_TYPE_ARRAY: {
2373 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT &&
2374 startswith(name, "Exec")) {
2375 DBusMessageIter sub;
2377 dbus_message_iter_recurse(iter, &sub);
2378 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2379 ExecStatusInfo *info;
2382 if (!(info = new0(ExecStatusInfo, 1)))
2385 if (!(info->name = strdup(name))) {
2390 if ((r = exec_status_info_deserialize(&sub, info)) < 0) {
2395 LIST_PREPEND(ExecStatusInfo, exec, i->exec, info);
2397 dbus_message_iter_next(&sub);
2404 case DBUS_TYPE_STRUCT: {
2406 if (streq(name, "LoadError")) {
2407 DBusMessageIter sub;
2408 const char *n, *message;
2411 dbus_message_iter_recurse(iter, &sub);
2413 r = bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &n, true);
2417 r = bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &message, false);
2421 if (!isempty(message))
2422 i->load_error = message;
2432 static int print_property(const char *name, DBusMessageIter *iter) {
2436 /* This is a low-level property printer, see
2437 * print_status_info() for the nicer output */
2439 if (arg_property && !strv_find(arg_property, name))
2442 switch (dbus_message_iter_get_arg_type(iter)) {
2444 case DBUS_TYPE_STRUCT: {
2445 DBusMessageIter sub;
2446 dbus_message_iter_recurse(iter, &sub);
2448 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
2451 dbus_message_iter_get_basic(&sub, &u);
2454 printf("%s=%u\n", name, (unsigned) u);
2456 printf("%s=\n", name);
2459 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
2462 dbus_message_iter_get_basic(&sub, &s);
2464 if (arg_all || s[0])
2465 printf("%s=%s\n", name, s);
2468 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "LoadError")) {
2469 const char *a = NULL, *b = NULL;
2471 if (bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &a, true) >= 0)
2472 bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &b, false);
2474 if (arg_all || !isempty(a) || !isempty(b))
2475 printf("%s=%s \"%s\"\n", name, strempty(a), strempty(b));
2483 case DBUS_TYPE_ARRAY:
2485 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "EnvironmentFiles")) {
2486 DBusMessageIter sub, sub2;
2488 dbus_message_iter_recurse(iter, &sub);
2489 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2493 dbus_message_iter_recurse(&sub, &sub2);
2495 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) >= 0 &&
2496 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, false) >= 0)
2497 printf("EnvironmentFile=%s (ignore_errors=%s)\n", path, yes_no(ignore));
2499 dbus_message_iter_next(&sub);
2504 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
2505 DBusMessageIter sub, sub2;
2507 dbus_message_iter_recurse(iter, &sub);
2508 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2509 const char *type, *path;
2511 dbus_message_iter_recurse(&sub, &sub2);
2513 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
2514 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
2515 printf("%s=%s\n", type, path);
2517 dbus_message_iter_next(&sub);
2522 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
2523 DBusMessageIter sub, sub2;
2525 dbus_message_iter_recurse(iter, &sub);
2526 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2528 uint64_t value, next_elapse;
2530 dbus_message_iter_recurse(&sub, &sub2);
2532 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
2533 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
2534 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
2535 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
2537 printf("%s={ value=%s ; next_elapse=%s }\n",
2539 format_timespan(timespan1, sizeof(timespan1), value),
2540 format_timespan(timespan2, sizeof(timespan2), next_elapse));
2543 dbus_message_iter_next(&sub);
2548 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "ControlGroupAttributes")) {
2549 DBusMessageIter sub, sub2;
2551 dbus_message_iter_recurse(iter, &sub);
2552 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2553 const char *controller, *attr, *value;
2555 dbus_message_iter_recurse(&sub, &sub2);
2557 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &controller, true) >= 0 &&
2558 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &attr, true) >= 0 &&
2559 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &value, false) >= 0) {
2561 printf("ControlGroupAttribute={ controller=%s ; attribute=%s ; value=\"%s\" }\n",
2567 dbus_message_iter_next(&sub);
2572 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
2573 DBusMessageIter sub;
2575 dbus_message_iter_recurse(iter, &sub);
2576 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2577 ExecStatusInfo info;
2580 if (exec_status_info_deserialize(&sub, &info) >= 0) {
2581 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
2584 t = strv_join(info.argv, " ");
2586 printf("%s={ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n",
2590 yes_no(info.ignore),
2591 strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)),
2592 strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)),
2593 (unsigned) info. pid,
2594 sigchld_code_to_string(info.code),
2596 info.code == CLD_EXITED ? "" : "/",
2597 strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status)));
2603 strv_free(info.argv);
2605 dbus_message_iter_next(&sub);
2614 if (generic_print_property(name, iter, arg_all) > 0)
2618 printf("%s=[unprintable]\n", name);
2623 static int show_one(const char *verb, DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
2624 DBusMessage *m = NULL, *reply = NULL;
2625 const char *interface = "";
2628 DBusMessageIter iter, sub, sub2, sub3;
2629 UnitStatusInfo info;
2637 dbus_error_init(&error);
2639 if (!(m = dbus_message_new_method_call(
2640 "org.freedesktop.systemd1",
2642 "org.freedesktop.DBus.Properties",
2644 log_error("Could not allocate message.");
2649 if (!dbus_message_append_args(m,
2650 DBUS_TYPE_STRING, &interface,
2651 DBUS_TYPE_INVALID)) {
2652 log_error("Could not append arguments to message.");
2657 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2658 log_error("Failed to issue method call: %s", bus_error_message(&error));
2663 if (!dbus_message_iter_init(reply, &iter) ||
2664 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
2665 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
2666 log_error("Failed to parse reply.");
2671 dbus_message_iter_recurse(&iter, &sub);
2678 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2681 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
2682 log_error("Failed to parse reply.");
2687 dbus_message_iter_recurse(&sub, &sub2);
2689 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
2690 log_error("Failed to parse reply.");
2695 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
2696 log_error("Failed to parse reply.");
2701 dbus_message_iter_recurse(&sub2, &sub3);
2703 if (show_properties)
2704 r = print_property(name, &sub3);
2706 r = status_property(name, &sub3, &info);
2709 log_error("Failed to parse reply.");
2714 dbus_message_iter_next(&sub);
2719 if (!show_properties)
2720 print_status_info(&info);
2722 if (!streq_ptr(info.active_state, "active") &&
2723 !streq_ptr(info.active_state, "reloading") &&
2724 streq(verb, "status"))
2725 /* According to LSB: "program not running" */
2728 while ((p = info.exec)) {
2729 LIST_REMOVE(ExecStatusInfo, exec, info.exec, p);
2730 exec_status_info_free(p);
2735 dbus_message_unref(m);
2738 dbus_message_unref(reply);
2740 dbus_error_free(&error);
2745 static int show(DBusConnection *bus, char **args) {
2746 DBusMessage *m = NULL, *reply = NULL;
2749 bool show_properties, new_line = false;
2755 dbus_error_init(&error);
2757 show_properties = !streq(args[0], "status");
2759 if (show_properties)
2760 pager_open_if_enabled();
2762 if (show_properties && strv_length(args) <= 1) {
2763 /* If not argument is specified inspect the manager
2766 ret = show_one(args[0], bus, "/org/freedesktop/systemd1", show_properties, &new_line);
2770 STRV_FOREACH(name, args+1) {
2771 const char *path = NULL;
2774 if (safe_atou32(*name, &id) < 0) {
2776 /* Interpret as unit name */
2778 if (!(m = dbus_message_new_method_call(
2779 "org.freedesktop.systemd1",
2780 "/org/freedesktop/systemd1",
2781 "org.freedesktop.systemd1.Manager",
2783 log_error("Could not allocate message.");
2788 if (!dbus_message_append_args(m,
2789 DBUS_TYPE_STRING, name,
2790 DBUS_TYPE_INVALID)) {
2791 log_error("Could not append arguments to message.");
2796 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2798 if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
2799 log_error("Failed to issue method call: %s", bus_error_message(&error));
2804 dbus_error_free(&error);
2806 dbus_message_unref(m);
2807 if (!(m = dbus_message_new_method_call(
2808 "org.freedesktop.systemd1",
2809 "/org/freedesktop/systemd1",
2810 "org.freedesktop.systemd1.Manager",
2812 log_error("Could not allocate message.");
2817 if (!dbus_message_append_args(m,
2818 DBUS_TYPE_STRING, name,
2819 DBUS_TYPE_INVALID)) {
2820 log_error("Could not append arguments to message.");
2825 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2826 log_error("Failed to issue method call: %s", bus_error_message(&error));
2828 if (dbus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT))
2829 ret = 4; /* According to LSB: "program or service status is unknown" */
2836 } else if (show_properties) {
2838 /* Interpret as job id */
2840 if (!(m = dbus_message_new_method_call(
2841 "org.freedesktop.systemd1",
2842 "/org/freedesktop/systemd1",
2843 "org.freedesktop.systemd1.Manager",
2845 log_error("Could not allocate message.");
2850 if (!dbus_message_append_args(m,
2851 DBUS_TYPE_UINT32, &id,
2852 DBUS_TYPE_INVALID)) {
2853 log_error("Could not append arguments to message.");
2858 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2859 log_error("Failed to issue method call: %s", bus_error_message(&error));
2865 /* Interpret as PID */
2867 if (!(m = dbus_message_new_method_call(
2868 "org.freedesktop.systemd1",
2869 "/org/freedesktop/systemd1",
2870 "org.freedesktop.systemd1.Manager",
2872 log_error("Could not allocate message.");
2877 if (!dbus_message_append_args(m,
2878 DBUS_TYPE_UINT32, &id,
2879 DBUS_TYPE_INVALID)) {
2880 log_error("Could not append arguments to message.");
2885 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2886 log_error("Failed to issue method call: %s", bus_error_message(&error));
2892 if (!dbus_message_get_args(reply, &error,
2893 DBUS_TYPE_OBJECT_PATH, &path,
2894 DBUS_TYPE_INVALID)) {
2895 log_error("Failed to parse reply: %s", bus_error_message(&error));
2900 if ((r = show_one(args[0], bus, path, show_properties, &new_line)) != 0)
2903 dbus_message_unref(m);
2904 dbus_message_unref(reply);
2910 dbus_message_unref(m);
2913 dbus_message_unref(reply);
2915 dbus_error_free(&error);
2920 static int dump(DBusConnection *bus, char **args) {
2921 DBusMessage *m = NULL, *reply = NULL;
2926 dbus_error_init(&error);
2928 pager_open_if_enabled();
2930 if (!(m = dbus_message_new_method_call(
2931 "org.freedesktop.systemd1",
2932 "/org/freedesktop/systemd1",
2933 "org.freedesktop.systemd1.Manager",
2935 log_error("Could not allocate message.");
2939 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2940 log_error("Failed to issue method call: %s", bus_error_message(&error));
2945 if (!dbus_message_get_args(reply, &error,
2946 DBUS_TYPE_STRING, &text,
2947 DBUS_TYPE_INVALID)) {
2948 log_error("Failed to parse reply: %s", bus_error_message(&error));
2953 fputs(text, stdout);
2959 dbus_message_unref(m);
2962 dbus_message_unref(reply);
2964 dbus_error_free(&error);
2969 static int snapshot(DBusConnection *bus, char **args) {
2970 DBusMessage *m = NULL, *reply = NULL;
2973 const char *name = "", *path, *id;
2974 dbus_bool_t cleanup = FALSE;
2975 DBusMessageIter iter, sub;
2977 *interface = "org.freedesktop.systemd1.Unit",
2980 dbus_error_init(&error);
2982 if (!(m = dbus_message_new_method_call(
2983 "org.freedesktop.systemd1",
2984 "/org/freedesktop/systemd1",
2985 "org.freedesktop.systemd1.Manager",
2986 "CreateSnapshot"))) {
2987 log_error("Could not allocate message.");
2991 if (strv_length(args) > 1)
2994 if (!dbus_message_append_args(m,
2995 DBUS_TYPE_STRING, &name,
2996 DBUS_TYPE_BOOLEAN, &cleanup,
2997 DBUS_TYPE_INVALID)) {
2998 log_error("Could not append arguments to message.");
3003 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3004 log_error("Failed to issue method call: %s", bus_error_message(&error));
3009 if (!dbus_message_get_args(reply, &error,
3010 DBUS_TYPE_OBJECT_PATH, &path,
3011 DBUS_TYPE_INVALID)) {
3012 log_error("Failed to parse reply: %s", bus_error_message(&error));
3017 dbus_message_unref(m);
3018 if (!(m = dbus_message_new_method_call(
3019 "org.freedesktop.systemd1",
3021 "org.freedesktop.DBus.Properties",
3023 log_error("Could not allocate message.");
3027 if (!dbus_message_append_args(m,
3028 DBUS_TYPE_STRING, &interface,
3029 DBUS_TYPE_STRING, &property,
3030 DBUS_TYPE_INVALID)) {
3031 log_error("Could not append arguments to message.");
3036 dbus_message_unref(reply);
3037 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3038 log_error("Failed to issue method call: %s", bus_error_message(&error));
3043 if (!dbus_message_iter_init(reply, &iter) ||
3044 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
3045 log_error("Failed to parse reply.");
3050 dbus_message_iter_recurse(&iter, &sub);
3052 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
3053 log_error("Failed to parse reply.");
3058 dbus_message_iter_get_basic(&sub, &id);
3066 dbus_message_unref(m);
3069 dbus_message_unref(reply);
3071 dbus_error_free(&error);
3076 static int delete_snapshot(DBusConnection *bus, char **args) {
3077 DBusMessage *m = NULL, *reply = NULL;
3085 dbus_error_init(&error);
3087 STRV_FOREACH(name, args+1) {
3088 const char *path = NULL;
3090 if (!(m = dbus_message_new_method_call(
3091 "org.freedesktop.systemd1",
3092 "/org/freedesktop/systemd1",
3093 "org.freedesktop.systemd1.Manager",
3095 log_error("Could not allocate message.");
3100 if (!dbus_message_append_args(m,
3101 DBUS_TYPE_STRING, name,
3102 DBUS_TYPE_INVALID)) {
3103 log_error("Could not append arguments to message.");
3108 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3109 log_error("Failed to issue method call: %s", bus_error_message(&error));
3114 if (!dbus_message_get_args(reply, &error,
3115 DBUS_TYPE_OBJECT_PATH, &path,
3116 DBUS_TYPE_INVALID)) {
3117 log_error("Failed to parse reply: %s", bus_error_message(&error));
3122 dbus_message_unref(m);
3123 if (!(m = dbus_message_new_method_call(
3124 "org.freedesktop.systemd1",
3126 "org.freedesktop.systemd1.Snapshot",
3128 log_error("Could not allocate message.");
3133 dbus_message_unref(reply);
3134 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3135 log_error("Failed to issue method call: %s", bus_error_message(&error));
3140 dbus_message_unref(m);
3141 dbus_message_unref(reply);
3149 dbus_message_unref(m);
3152 dbus_message_unref(reply);
3154 dbus_error_free(&error);
3159 static int daemon_reload(DBusConnection *bus, char **args) {
3160 DBusMessage *m = NULL, *reply = NULL;
3165 dbus_error_init(&error);
3167 if (arg_action == ACTION_RELOAD)
3169 else if (arg_action == ACTION_REEXEC)
3170 method = "Reexecute";
3172 assert(arg_action == ACTION_SYSTEMCTL);
3175 streq(args[0], "clear-jobs") ||
3176 streq(args[0], "cancel") ? "ClearJobs" :
3177 streq(args[0], "daemon-reexec") ? "Reexecute" :
3178 streq(args[0], "reset-failed") ? "ResetFailed" :
3179 streq(args[0], "halt") ? "Halt" :
3180 streq(args[0], "poweroff") ? "PowerOff" :
3181 streq(args[0], "reboot") ? "Reboot" :
3182 streq(args[0], "kexec") ? "KExec" :
3183 streq(args[0], "exit") ? "Exit" :
3184 /* "daemon-reload" */ "Reload";
3187 if (!(m = dbus_message_new_method_call(
3188 "org.freedesktop.systemd1",
3189 "/org/freedesktop/systemd1",
3190 "org.freedesktop.systemd1.Manager",
3192 log_error("Could not allocate message.");
3196 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3198 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
3199 /* There's always a fallback possible for
3200 * legacy actions. */
3205 if (streq(method, "Reexecute") && dbus_error_has_name(&error, DBUS_ERROR_NO_REPLY)) {
3206 /* On reexecution, we expect a disconnect, not
3212 log_error("Failed to issue method call: %s", bus_error_message(&error));
3221 dbus_message_unref(m);
3224 dbus_message_unref(reply);
3226 dbus_error_free(&error);
3231 static int reset_failed(DBusConnection *bus, char **args) {
3232 DBusMessage *m = NULL;
3238 dbus_error_init(&error);
3240 if (strv_length(args) <= 1)
3241 return daemon_reload(bus, args);
3243 STRV_FOREACH(name, args+1) {
3246 if (!(m = dbus_message_new_method_call(
3247 "org.freedesktop.systemd1",
3248 "/org/freedesktop/systemd1",
3249 "org.freedesktop.systemd1.Manager",
3250 "ResetFailedUnit"))) {
3251 log_error("Could not allocate message.");
3256 if (!dbus_message_append_args(m,
3257 DBUS_TYPE_STRING, name,
3258 DBUS_TYPE_INVALID)) {
3259 log_error("Could not append arguments to message.");
3264 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3265 log_error("Failed to issue method call: %s", bus_error_message(&error));
3270 dbus_message_unref(m);
3271 dbus_message_unref(reply);
3279 dbus_message_unref(m);
3281 dbus_error_free(&error);
3286 static int show_enviroment(DBusConnection *bus, char **args) {
3287 DBusMessage *m = NULL, *reply = NULL;
3289 DBusMessageIter iter, sub, sub2;
3292 *interface = "org.freedesktop.systemd1.Manager",
3293 *property = "Environment";
3295 dbus_error_init(&error);
3297 pager_open_if_enabled();
3299 if (!(m = dbus_message_new_method_call(
3300 "org.freedesktop.systemd1",
3301 "/org/freedesktop/systemd1",
3302 "org.freedesktop.DBus.Properties",
3304 log_error("Could not allocate message.");
3308 if (!dbus_message_append_args(m,
3309 DBUS_TYPE_STRING, &interface,
3310 DBUS_TYPE_STRING, &property,
3311 DBUS_TYPE_INVALID)) {
3312 log_error("Could not append arguments to message.");
3317 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3318 log_error("Failed to issue method call: %s", bus_error_message(&error));
3323 if (!dbus_message_iter_init(reply, &iter) ||
3324 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
3325 log_error("Failed to parse reply.");
3330 dbus_message_iter_recurse(&iter, &sub);
3332 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
3333 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
3334 log_error("Failed to parse reply.");
3339 dbus_message_iter_recurse(&sub, &sub2);
3341 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
3344 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
3345 log_error("Failed to parse reply.");
3350 dbus_message_iter_get_basic(&sub2, &text);
3351 printf("%s\n", text);
3353 dbus_message_iter_next(&sub2);
3360 dbus_message_unref(m);
3363 dbus_message_unref(reply);
3365 dbus_error_free(&error);
3370 static int set_environment(DBusConnection *bus, char **args) {
3371 DBusMessage *m = NULL, *reply = NULL;
3375 DBusMessageIter iter, sub;
3378 dbus_error_init(&error);
3380 method = streq(args[0], "set-environment")
3382 : "UnsetEnvironment";
3384 if (!(m = dbus_message_new_method_call(
3385 "org.freedesktop.systemd1",
3386 "/org/freedesktop/systemd1",
3387 "org.freedesktop.systemd1.Manager",
3390 log_error("Could not allocate message.");
3394 dbus_message_iter_init_append(m, &iter);
3396 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
3397 log_error("Could not append arguments to message.");
3402 STRV_FOREACH(name, args+1)
3403 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, name)) {
3404 log_error("Could not append arguments to message.");
3409 if (!dbus_message_iter_close_container(&iter, &sub)) {
3410 log_error("Could not append arguments to message.");
3415 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3416 log_error("Failed to issue method call: %s", bus_error_message(&error));
3425 dbus_message_unref(m);
3428 dbus_message_unref(reply);
3430 dbus_error_free(&error);
3435 static int enable_sysv_units(char **args) {
3438 #if defined (HAVE_SYSV_COMPAT) && (defined(TARGET_FEDORA) || defined(TARGET_MANDRIVA) || defined(TARGET_SUSE) || defined(TARGET_MEEGO) || defined(TARGET_ALTLINUX))
3439 const char *verb = args[0];
3440 unsigned f = 1, t = 1;
3443 if (arg_scope != UNIT_FILE_SYSTEM)
3446 if (!streq(verb, "enable") &&
3447 !streq(verb, "disable") &&
3448 !streq(verb, "is-enabled"))
3451 /* Processes all SysV units, and reshuffles the array so that
3452 * afterwards only the native units remain */
3455 r = lookup_paths_init(&paths, MANAGER_SYSTEM, false);
3461 for (f = 1; args[f]; f++) {
3464 bool found_native = false, found_sysv;
3466 const char *argv[6] = { "/sbin/chkconfig", NULL, NULL, NULL, NULL };
3467 char **k, *l, *q = NULL;
3474 if (!endswith(name, ".service"))
3477 if (path_is_absolute(name))
3480 STRV_FOREACH(k, paths.unit_path) {
3483 if (!isempty(arg_root))
3484 asprintf(&p, "%s/%s/%s", arg_root, *k, name);
3486 asprintf(&p, "%s/%s", *k, name);
3489 log_error("No memory");
3494 found_native = access(p, F_OK) >= 0;
3505 if (!isempty(arg_root))
3506 asprintf(&p, "%s/" SYSTEM_SYSVINIT_PATH "/%s", arg_root, name);
3508 asprintf(&p, SYSTEM_SYSVINIT_PATH "/%s", name);
3510 log_error("No memory");
3515 p[strlen(p) - sizeof(".service") + 1] = 0;
3516 found_sysv = access(p, F_OK) >= 0;
3523 /* Mark this entry, so that we don't try enabling it as native unit */
3524 args[f] = (char*) "";
3526 log_info("%s is not a native service, redirecting to /sbin/chkconfig.", name);
3528 if (!isempty(arg_root))
3529 argv[c++] = q = strappend("--root=", arg_root);
3531 argv[c++] = file_name_from_path(p);
3533 streq(verb, "enable") ? "on" :
3534 streq(verb, "disable") ? "off" : "--level=5";
3537 l = strv_join((char**)argv, " ");
3539 log_error("No memory.");
3546 log_info("Executing %s", l);
3551 log_error("Failed to fork: %m");
3556 } else if (pid == 0) {
3559 execv(argv[0], (char**) argv);
3560 _exit(EXIT_FAILURE);
3566 j = wait_for_terminate(pid, &status);
3568 log_error("Failed to wait for child: %s", strerror(-r));
3573 if (status.si_code == CLD_EXITED) {
3574 if (streq(verb, "is-enabled")) {
3575 if (status.si_status == 0) {
3584 } else if (status.si_status != 0) {
3595 lookup_paths_free(&paths);
3597 /* Drop all SysV units */
3598 for (f = 1, t = 1; args[f]; f++) {
3600 if (isempty(args[f]))
3603 args[t++] = args[f];
3612 static int enable_unit(DBusConnection *bus, char **args) {
3613 const char *verb = args[0];
3614 UnitFileChange *changes = NULL;
3615 unsigned n_changes = 0, i;
3616 int carries_install_info = -1;
3617 DBusMessage *m = NULL, *reply = NULL;
3621 dbus_error_init(&error);
3623 r = enable_sysv_units(args);
3627 if (!bus || avoid_bus()) {
3628 if (streq(verb, "enable")) {
3629 r = unit_file_enable(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3630 carries_install_info = r;
3631 } else if (streq(verb, "disable"))
3632 r = unit_file_disable(arg_scope, arg_runtime, arg_root, args+1, &changes, &n_changes);
3633 else if (streq(verb, "reenable")) {
3634 r = unit_file_reenable(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3635 carries_install_info = r;
3636 } else if (streq(verb, "link"))
3637 r = unit_file_link(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3638 else if (streq(verb, "preset")) {
3639 r = unit_file_preset(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3640 carries_install_info = r;
3641 } else if (streq(verb, "mask"))
3642 r = unit_file_mask(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3643 else if (streq(verb, "unmask"))
3644 r = unit_file_unmask(arg_scope, arg_runtime, arg_root, args+1, &changes, &n_changes);
3646 assert_not_reached("Unknown verb");
3649 log_error("Operation failed: %s", strerror(-r));
3653 for (i = 0; i < n_changes; i++) {
3654 if (changes[i].type == UNIT_FILE_SYMLINK)
3655 log_info("ln -s '%s' '%s'", changes[i].source, changes[i].path);
3657 log_info("rm '%s'", changes[i].path);
3662 bool send_force = true, expect_carries_install_info = false;
3664 DBusMessageIter iter, sub, sub2;
3666 if (streq(verb, "enable")) {
3667 method = "EnableUnitFiles";
3668 expect_carries_install_info = true;
3669 } else if (streq(verb, "disable")) {
3670 method = "DisableUnitFiles";
3672 } else if (streq(verb, "reenable")) {
3673 method = "ReenableUnitFiles";
3674 expect_carries_install_info = true;
3675 } else if (streq(verb, "link"))
3676 method = "LinkUnitFiles";
3677 else if (streq(verb, "preset")) {
3678 method = "PresetUnitFiles";
3679 expect_carries_install_info = true;
3680 } else if (streq(verb, "mask"))
3681 method = "MaskUnitFiles";
3682 else if (streq(verb, "unmask")) {
3683 method = "UnmaskUnitFiles";
3686 assert_not_reached("Unknown verb");
3688 m = dbus_message_new_method_call(
3689 "org.freedesktop.systemd1",
3690 "/org/freedesktop/systemd1",
3691 "org.freedesktop.systemd1.Manager",
3694 log_error("Out of memory");
3699 dbus_message_iter_init_append(m, &iter);
3701 r = bus_append_strv_iter(&iter, args+1);
3703 log_error("Failed to append unit files.");
3708 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &a)) {
3709 log_error("Failed to append runtime boolean.");
3717 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b)) {
3718 log_error("Failed to append force boolean.");
3724 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
3726 log_error("Failed to issue method call: %s", bus_error_message(&error));
3731 if (!dbus_message_iter_init(reply, &iter)) {
3732 log_error("Failed to initialize iterator.");
3736 if (expect_carries_install_info) {
3737 r = bus_iter_get_basic_and_next(&iter, DBUS_TYPE_BOOLEAN, &b, true);
3739 log_error("Failed to parse reply.");
3743 carries_install_info = b;
3746 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
3747 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
3748 log_error("Failed to parse reply.");
3753 dbus_message_iter_recurse(&iter, &sub);
3754 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
3755 const char *type, *path, *source;
3757 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
3758 log_error("Failed to parse reply.");
3763 dbus_message_iter_recurse(&sub, &sub2);
3765 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
3766 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0 ||
3767 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &source, false) < 0) {
3768 log_error("Failed to parse reply.");
3773 if (streq(type, "symlink"))
3774 log_info("ln -s '%s' '%s'", source, path);
3776 log_info("rm '%s'", path);
3778 dbus_message_iter_next(&sub);
3781 /* Try to reload if enabeld */
3783 r = daemon_reload(bus, args);
3786 if (carries_install_info == 0)
3787 log_warning("Warning: unit files do not carry install information. No operation executed.");
3791 dbus_message_unref(m);
3794 dbus_message_unref(reply);
3796 unit_file_changes_free(changes, n_changes);
3798 dbus_error_free(&error);
3802 static int unit_is_enabled(DBusConnection *bus, char **args) {
3805 DBusMessage *m = NULL, *reply = NULL;
3809 dbus_error_init(&error);
3811 r = enable_sysv_units(args);
3817 if (!bus || avoid_bus()) {
3819 STRV_FOREACH(name, args+1) {
3820 UnitFileState state;
3822 state = unit_file_get_state(arg_scope, arg_root, *name);
3828 if (state == UNIT_FILE_ENABLED ||
3829 state == UNIT_FILE_ENABLED_RUNTIME ||
3830 state == UNIT_FILE_STATIC)
3834 puts(unit_file_state_to_string(state));
3838 STRV_FOREACH(name, args+1) {
3841 m = dbus_message_new_method_call(
3842 "org.freedesktop.systemd1",
3843 "/org/freedesktop/systemd1",
3844 "org.freedesktop.systemd1.Manager",
3845 "GetUnitFileState");
3847 log_error("Out of memory");
3852 if (!dbus_message_append_args(m,
3853 DBUS_TYPE_STRING, name,
3854 DBUS_TYPE_INVALID)) {
3855 log_error("Could not append arguments to message.");
3860 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
3862 log_error("Failed to issue method call: %s", bus_error_message(&error));
3867 if (!dbus_message_get_args(reply, &error,
3868 DBUS_TYPE_STRING, &s,
3869 DBUS_TYPE_INVALID)) {
3870 log_error("Failed to parse reply: %s", bus_error_message(&error));
3875 dbus_message_unref(m);
3876 dbus_message_unref(reply);
3879 if (streq(s, "enabled") ||
3880 streq(s, "enabled-runtime") ||
3889 r = enabled ? 0 : 1;
3893 dbus_message_unref(m);
3896 dbus_message_unref(reply);
3898 dbus_error_free(&error);
3902 static int systemctl_help(void) {
3904 pager_open_if_enabled();
3906 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
3907 "Query or send control commands to the systemd manager.\n\n"
3908 " -h --help Show this help\n"
3909 " --version Show package version\n"
3910 " -t --type=TYPE List only units of a particular type\n"
3911 " -p --property=NAME Show only properties by this name\n"
3912 " -a --all Show all units/properties, including dead/empty ones\n"
3913 " --failed Show only failed units\n"
3914 " --full Don't ellipsize unit names on output\n"
3915 " --fail When queueing a new job, fail if conflicting jobs are\n"
3917 " --ignore-dependencies\n"
3918 " When queueing a new job, ignore all its dependencies\n"
3919 " --kill-who=WHO Who to send signal to\n"
3920 " -s --signal=SIGNAL Which signal to send\n"
3921 " -H --host=[USER@]HOST\n"
3922 " Show information for remote host\n"
3923 " -P --privileged Acquire privileges before execution\n"
3924 " -q --quiet Suppress output\n"
3925 " --no-block Do not wait until operation finished\n"
3926 " --no-wall Don't send wall message before halt/power-off/reboot\n"
3927 " --no-reload When enabling/disabling unit files, don't reload daemon\n"
3929 " --no-pager Do not pipe output into a pager\n"
3930 " --no-ask-password\n"
3931 " Do not ask for system passwords\n"
3932 " --order When generating graph for dot, show only order\n"
3933 " --require When generating graph for dot, show only requirement\n"
3934 " --system Connect to system manager\n"
3935 " --user Connect to user service manager\n"
3936 " --global Enable/disable unit files globally\n"
3937 " -f --force When enabling unit files, override existing symlinks\n"
3938 " When shutting down, execute action immediately\n"
3939 " --root=PATH Enable unit files in the specified root directory\n"
3940 " --runtime Enable unit files only temporarily until next reboot\n\n"
3942 " list-units List loaded units\n"
3943 " start [NAME...] Start (activate) one or more units\n"
3944 " stop [NAME...] Stop (deactivate) one or more units\n"
3945 " reload [NAME...] Reload one or more units\n"
3946 " restart [NAME...] Start or restart one or more units\n"
3947 " try-restart [NAME...] Restart one or more units if active\n"
3948 " reload-or-restart [NAME...] Reload one or more units is possible,\n"
3949 " otherwise start or restart\n"
3950 " reload-or-try-restart [NAME...] Reload one or more units is possible,\n"
3951 " otherwise restart if active\n"
3952 " isolate [NAME] Start one unit and stop all others\n"
3953 " kill [NAME...] Send signal to processes of a unit\n"
3954 " is-active [NAME...] Check whether units are active\n"
3955 " status [NAME...|PID...] Show runtime status of one or more units\n"
3956 " show [NAME...|JOB...] Show properties of one or more\n"
3957 " units/jobs or the manager\n"
3958 " reset-failed [NAME...] Reset failed state for all, one, or more\n"
3960 " load [NAME...] Load one or more units\n\n"
3961 "Unit File Commands:\n"
3962 " list-unit-files List installed unit files\n"
3963 " enable [NAME...] Enable one or more unit files\n"
3964 " disable [NAME...] Disable one or more unit files\n"
3965 " reenable [NAME...] Reenable one or more unit files\n"
3966 " preset [NAME...] Enable/disable one or more unit files\n"
3967 " based on preset configuration\n"
3968 " mask [NAME...] Mask one or more units\n"
3969 " unmask [NAME...] Unmask one or more units\n"
3970 " link [PATH...] Link one or more units files into\n"
3971 " the search path\n"
3972 " is-enabled [NAME...] Check whether unit files are enabled\n\n"
3974 " list-jobs List jobs\n"
3975 " cancel [JOB...] Cancel all, one, or more jobs\n\n"
3976 "Status Commands:\n"
3977 " dump Dump server status\n"
3978 " dot Dump dependency graph for dot(1)\n\n"
3979 "Snapshot Commands:\n"
3980 " snapshot [NAME] Create a snapshot\n"
3981 " delete [NAME...] Remove one or more snapshots\n\n"
3982 "Environment Commands:\n"
3983 " show-environment Dump environment\n"
3984 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
3985 " unset-environment [NAME...] Unset one or more environment variables\n\n"
3986 "Manager Lifecycle Commands:\n"
3987 " daemon-reload Reload systemd manager configuration\n"
3988 " daemon-reexec Reexecute systemd manager\n\n"
3989 "System Commands:\n"
3990 " default Enter system default mode\n"
3991 " rescue Enter system rescue mode\n"
3992 " emergency Enter system emergency mode\n"
3993 " halt Shut down and halt the system\n"
3994 " poweroff Shut down and power-off the system\n"
3995 " reboot Shut down and reboot the system\n"
3996 " kexec Shut down and reboot the system with kexec\n"
3997 " exit Ask for user instance termination\n",
3998 program_invocation_short_name);
4003 static int halt_help(void) {
4005 printf("%s [OPTIONS...]\n\n"
4006 "%s the system.\n\n"
4007 " --help Show this help\n"
4008 " --halt Halt the machine\n"
4009 " -p --poweroff Switch off the machine\n"
4010 " --reboot Reboot the machine\n"
4011 " -f --force Force immediate halt/power-off/reboot\n"
4012 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
4013 " -d --no-wtmp Don't write wtmp record\n"
4014 " -n --no-sync Don't sync before halt/power-off/reboot\n"
4015 " --no-wall Don't send wall message before halt/power-off/reboot\n",
4016 program_invocation_short_name,
4017 arg_action == ACTION_REBOOT ? "Reboot" :
4018 arg_action == ACTION_POWEROFF ? "Power off" :
4024 static int shutdown_help(void) {
4026 printf("%s [OPTIONS...] [TIME] [WALL...]\n\n"
4027 "Shut down the system.\n\n"
4028 " --help Show this help\n"
4029 " -H --halt Halt the machine\n"
4030 " -P --poweroff Power-off the machine\n"
4031 " -r --reboot Reboot the machine\n"
4032 " -h Equivalent to --poweroff, overriden by --halt\n"
4033 " -k Don't halt/power-off/reboot, just send warnings\n"
4034 " --no-wall Don't send wall message before halt/power-off/reboot\n"
4035 " -c Cancel a pending shutdown\n",
4036 program_invocation_short_name);
4041 static int telinit_help(void) {
4043 printf("%s [OPTIONS...] {COMMAND}\n\n"
4044 "Send control commands to the init daemon.\n\n"
4045 " --help Show this help\n"
4046 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
4048 " 0 Power-off the machine\n"
4049 " 6 Reboot the machine\n"
4050 " 2, 3, 4, 5 Start runlevelX.target unit\n"
4051 " 1, s, S Enter rescue mode\n"
4052 " q, Q Reload init daemon configuration\n"
4053 " u, U Reexecute init daemon\n",
4054 program_invocation_short_name);
4059 static int runlevel_help(void) {
4061 printf("%s [OPTIONS...]\n\n"
4062 "Prints the previous and current runlevel of the init system.\n\n"
4063 " --help Show this help\n",
4064 program_invocation_short_name);
4069 static int systemctl_parse_argv(int argc, char *argv[]) {
4073 ARG_IGNORE_DEPENDENCIES,
4088 ARG_NO_ASK_PASSWORD,
4093 static const struct option options[] = {
4094 { "help", no_argument, NULL, 'h' },
4095 { "version", no_argument, NULL, ARG_VERSION },
4096 { "type", required_argument, NULL, 't' },
4097 { "property", required_argument, NULL, 'p' },
4098 { "all", no_argument, NULL, 'a' },
4099 { "failed", no_argument, NULL, ARG_FAILED },
4100 { "full", no_argument, NULL, ARG_FULL },
4101 { "fail", no_argument, NULL, ARG_FAIL },
4102 { "ignore-dependencies", no_argument, NULL, ARG_IGNORE_DEPENDENCIES },
4103 { "user", no_argument, NULL, ARG_USER },
4104 { "system", no_argument, NULL, ARG_SYSTEM },
4105 { "global", no_argument, NULL, ARG_GLOBAL },
4106 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
4107 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
4108 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4109 { "quiet", no_argument, NULL, 'q' },
4110 { "order", no_argument, NULL, ARG_ORDER },
4111 { "require", no_argument, NULL, ARG_REQUIRE },
4112 { "root", required_argument, NULL, ARG_ROOT },
4113 { "force", no_argument, NULL, 'f' },
4114 { "no-reload", no_argument, NULL, ARG_NO_RELOAD },
4115 { "kill-mode", required_argument, NULL, ARG_KILL_MODE }, /* undocumented on purpose */
4116 { "kill-who", required_argument, NULL, ARG_KILL_WHO },
4117 { "signal", required_argument, NULL, 's' },
4118 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
4119 { "host", required_argument, NULL, 'H' },
4120 { "privileged",no_argument, NULL, 'P' },
4121 { "runtime", no_argument, NULL, ARG_RUNTIME },
4122 { NULL, 0, NULL, 0 }
4130 /* Only when running as systemctl we ask for passwords */
4131 arg_ask_password = true;
4133 while ((c = getopt_long(argc, argv, "ht:p:aqfs:H:P", options, NULL)) >= 0) {
4142 puts(PACKAGE_STRING);
4144 puts(SYSTEMD_FEATURES);
4154 if (!(l = strv_append(arg_property, optarg)))
4157 strv_free(arg_property);
4160 /* If the user asked for a particular
4161 * property, show it to him, even if it is
4172 arg_job_mode = "fail";
4175 case ARG_IGNORE_DEPENDENCIES:
4176 arg_job_mode = "ignore-dependencies";
4180 arg_scope = UNIT_FILE_USER;
4184 arg_scope = UNIT_FILE_SYSTEM;
4188 arg_scope = UNIT_FILE_GLOBAL;
4192 arg_no_block = true;
4196 arg_no_pager = true;
4204 arg_dot = DOT_ORDER;
4208 arg_dot = DOT_REQUIRE;
4232 arg_no_reload = true;
4236 arg_kill_who = optarg;
4240 arg_kill_mode = optarg;
4244 if ((arg_signal = signal_from_string_try_harder(optarg)) < 0) {
4245 log_error("Failed to parse signal string %s.", optarg);
4250 case ARG_NO_ASK_PASSWORD:
4251 arg_ask_password = false;
4255 arg_transport = TRANSPORT_POLKIT;
4259 arg_transport = TRANSPORT_SSH;
4271 log_error("Unknown option code %c", c);
4276 if (arg_transport != TRANSPORT_NORMAL && arg_scope != UNIT_FILE_SYSTEM) {
4277 log_error("Cannot access user instance remotely.");
4284 static int halt_parse_argv(int argc, char *argv[]) {
4293 static const struct option options[] = {
4294 { "help", no_argument, NULL, ARG_HELP },
4295 { "halt", no_argument, NULL, ARG_HALT },
4296 { "poweroff", no_argument, NULL, 'p' },
4297 { "reboot", no_argument, NULL, ARG_REBOOT },
4298 { "force", no_argument, NULL, 'f' },
4299 { "wtmp-only", no_argument, NULL, 'w' },
4300 { "no-wtmp", no_argument, NULL, 'd' },
4301 { "no-sync", no_argument, NULL, 'n' },
4302 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4303 { NULL, 0, NULL, 0 }
4311 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
4312 if (runlevel == '0' || runlevel == '6')
4313 arg_immediate = true;
4315 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
4323 arg_action = ACTION_HALT;
4327 if (arg_action != ACTION_REBOOT)
4328 arg_action = ACTION_POWEROFF;
4332 arg_action = ACTION_REBOOT;
4336 arg_immediate = true;
4357 /* Compatibility nops */
4364 log_error("Unknown option code %c", c);
4369 if (optind < argc) {
4370 log_error("Too many arguments.");
4377 static int parse_time_spec(const char *t, usec_t *_u) {
4381 if (streq(t, "now"))
4383 else if (!strchr(t, ':')) {
4386 if (safe_atou64(t, &u) < 0)
4389 *_u = now(CLOCK_REALTIME) + USEC_PER_MINUTE * u;
4398 hour = strtol(t, &e, 10);
4399 if (errno != 0 || *e != ':' || hour < 0 || hour > 23)
4402 minute = strtol(e+1, &e, 10);
4403 if (errno != 0 || *e != 0 || minute < 0 || minute > 59)
4406 n = now(CLOCK_REALTIME);
4407 s = (time_t) (n / USEC_PER_SEC);
4410 assert_se(localtime_r(&s, &tm));
4412 tm.tm_hour = (int) hour;
4413 tm.tm_min = (int) minute;
4416 assert_se(s = mktime(&tm));
4418 *_u = (usec_t) s * USEC_PER_SEC;
4421 *_u += USEC_PER_DAY;
4427 static int shutdown_parse_argv(int argc, char *argv[]) {
4434 static const struct option options[] = {
4435 { "help", no_argument, NULL, ARG_HELP },
4436 { "halt", no_argument, NULL, 'H' },
4437 { "poweroff", no_argument, NULL, 'P' },
4438 { "reboot", no_argument, NULL, 'r' },
4439 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4440 { NULL, 0, NULL, 0 }
4448 while ((c = getopt_long(argc, argv, "HPrhkt:afFc", options, NULL)) >= 0) {
4456 arg_action = ACTION_HALT;
4460 arg_action = ACTION_POWEROFF;
4465 arg_action = ACTION_KEXEC;
4467 arg_action = ACTION_REBOOT;
4471 if (arg_action != ACTION_HALT)
4472 arg_action = ACTION_POWEROFF;
4485 /* Compatibility nops */
4489 arg_action = ACTION_CANCEL_SHUTDOWN;
4496 log_error("Unknown option code %c", c);
4501 if (argc > optind) {
4502 if ((r = parse_time_spec(argv[optind], &arg_when)) < 0) {
4503 log_error("Failed to parse time specification: %s", argv[optind]);
4507 arg_when = now(CLOCK_REALTIME) + USEC_PER_MINUTE;
4509 /* We skip the time argument */
4510 if (argc > optind + 1)
4511 arg_wall = argv + optind + 1;
4518 static int telinit_parse_argv(int argc, char *argv[]) {
4525 static const struct option options[] = {
4526 { "help", no_argument, NULL, ARG_HELP },
4527 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4528 { NULL, 0, NULL, 0 }
4531 static const struct {
4535 { '0', ACTION_POWEROFF },
4536 { '6', ACTION_REBOOT },
4537 { '1', ACTION_RESCUE },
4538 { '2', ACTION_RUNLEVEL2 },
4539 { '3', ACTION_RUNLEVEL3 },
4540 { '4', ACTION_RUNLEVEL4 },
4541 { '5', ACTION_RUNLEVEL5 },
4542 { 's', ACTION_RESCUE },
4543 { 'S', ACTION_RESCUE },
4544 { 'q', ACTION_RELOAD },
4545 { 'Q', ACTION_RELOAD },
4546 { 'u', ACTION_REEXEC },
4547 { 'U', ACTION_REEXEC }
4556 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4571 log_error("Unknown option code %c", c);
4576 if (optind >= argc) {
4581 if (optind + 1 < argc) {
4582 log_error("Too many arguments.");
4586 if (strlen(argv[optind]) != 1) {
4587 log_error("Expected single character argument.");
4591 for (i = 0; i < ELEMENTSOF(table); i++)
4592 if (table[i].from == argv[optind][0])
4595 if (i >= ELEMENTSOF(table)) {
4596 log_error("Unknown command %s.", argv[optind]);
4600 arg_action = table[i].to;
4607 static int runlevel_parse_argv(int argc, char *argv[]) {
4613 static const struct option options[] = {
4614 { "help", no_argument, NULL, ARG_HELP },
4615 { NULL, 0, NULL, 0 }
4623 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4634 log_error("Unknown option code %c", c);
4639 if (optind < argc) {
4640 log_error("Too many arguments.");
4647 static int parse_argv(int argc, char *argv[]) {
4651 if (program_invocation_short_name) {
4653 if (strstr(program_invocation_short_name, "halt")) {
4654 arg_action = ACTION_HALT;
4655 return halt_parse_argv(argc, argv);
4656 } else if (strstr(program_invocation_short_name, "poweroff")) {
4657 arg_action = ACTION_POWEROFF;
4658 return halt_parse_argv(argc, argv);
4659 } else if (strstr(program_invocation_short_name, "reboot")) {
4661 arg_action = ACTION_KEXEC;
4663 arg_action = ACTION_REBOOT;
4664 return halt_parse_argv(argc, argv);
4665 } else if (strstr(program_invocation_short_name, "shutdown")) {
4666 arg_action = ACTION_POWEROFF;
4667 return shutdown_parse_argv(argc, argv);
4668 } else if (strstr(program_invocation_short_name, "init")) {
4670 if (sd_booted() > 0) {
4671 arg_action = ACTION_INVALID;
4672 return telinit_parse_argv(argc, argv);
4674 /* Hmm, so some other init system is
4675 * running, we need to forward this
4676 * request to it. For now we simply
4677 * guess that it is Upstart. */
4679 execv("/lib/upstart/telinit", argv);
4681 log_error("Couldn't find an alternative telinit implementation to spawn.");
4685 } else if (strstr(program_invocation_short_name, "runlevel")) {
4686 arg_action = ACTION_RUNLEVEL;
4687 return runlevel_parse_argv(argc, argv);
4691 arg_action = ACTION_SYSTEMCTL;
4692 return systemctl_parse_argv(argc, argv);
4695 static int action_to_runlevel(void) {
4697 static const char table[_ACTION_MAX] = {
4698 [ACTION_HALT] = '0',
4699 [ACTION_POWEROFF] = '0',
4700 [ACTION_REBOOT] = '6',
4701 [ACTION_RUNLEVEL2] = '2',
4702 [ACTION_RUNLEVEL3] = '3',
4703 [ACTION_RUNLEVEL4] = '4',
4704 [ACTION_RUNLEVEL5] = '5',
4705 [ACTION_RESCUE] = '1'
4708 assert(arg_action < _ACTION_MAX);
4710 return table[arg_action];
4713 static int talk_upstart(void) {
4714 DBusMessage *m = NULL, *reply = NULL;
4716 int previous, rl, r;
4718 env1_buf[] = "RUNLEVEL=X",
4719 env2_buf[] = "PREVLEVEL=X";
4720 char *env1 = env1_buf, *env2 = env2_buf;
4721 const char *emit = "runlevel";
4722 dbus_bool_t b_false = FALSE;
4723 DBusMessageIter iter, sub;
4724 DBusConnection *bus;
4726 dbus_error_init(&error);
4728 if (!(rl = action_to_runlevel()))
4731 if (utmp_get_runlevel(&previous, NULL) < 0)
4734 if (!(bus = dbus_connection_open_private("unix:abstract=/com/ubuntu/upstart", &error))) {
4735 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
4740 log_error("Failed to connect to Upstart bus: %s", bus_error_message(&error));
4745 if ((r = bus_check_peercred(bus)) < 0) {
4746 log_error("Failed to verify owner of bus.");
4750 if (!(m = dbus_message_new_method_call(
4751 "com.ubuntu.Upstart",
4752 "/com/ubuntu/Upstart",
4753 "com.ubuntu.Upstart0_6",
4756 log_error("Could not allocate message.");
4761 dbus_message_iter_init_append(m, &iter);
4763 env1_buf[sizeof(env1_buf)-2] = rl;
4764 env2_buf[sizeof(env2_buf)-2] = previous;
4766 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
4767 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
4768 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
4769 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
4770 !dbus_message_iter_close_container(&iter, &sub) ||
4771 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
4772 log_error("Could not append arguments to message.");
4777 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
4779 if (error_is_no_service(&error)) {
4784 log_error("Failed to issue method call: %s", bus_error_message(&error));
4793 dbus_message_unref(m);
4796 dbus_message_unref(reply);
4799 dbus_connection_flush(bus);
4800 dbus_connection_close(bus);
4801 dbus_connection_unref(bus);
4804 dbus_error_free(&error);
4809 static int talk_initctl(void) {
4810 struct init_request request;
4814 if (!(rl = action_to_runlevel()))
4818 request.magic = INIT_MAGIC;
4819 request.sleeptime = 0;
4820 request.cmd = INIT_CMD_RUNLVL;
4821 request.runlevel = rl;
4823 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
4825 if (errno == ENOENT)
4828 log_error("Failed to open "INIT_FIFO": %m");
4833 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
4834 close_nointr_nofail(fd);
4837 log_error("Failed to write to "INIT_FIFO": %m");
4838 return errno ? -errno : -EIO;
4844 static int systemctl_main(DBusConnection *bus, int argc, char *argv[], DBusError *error) {
4846 static const struct {
4854 int (* const dispatch)(DBusConnection *bus, char **args);
4856 { "list-units", LESS, 1, list_units },
4857 { "list-unit-files", EQUAL, 1, list_unit_files },
4858 { "list-jobs", EQUAL, 1, list_jobs },
4859 { "clear-jobs", EQUAL, 1, daemon_reload },
4860 { "load", MORE, 2, load_unit },
4861 { "cancel", MORE, 2, cancel_job },
4862 { "start", MORE, 2, start_unit },
4863 { "stop", MORE, 2, start_unit },
4864 { "condstop", MORE, 2, start_unit }, /* For compatibility with ALTLinux */
4865 { "reload", MORE, 2, start_unit },
4866 { "restart", MORE, 2, start_unit },
4867 { "try-restart", MORE, 2, start_unit },
4868 { "reload-or-restart", MORE, 2, start_unit },
4869 { "reload-or-try-restart", MORE, 2, start_unit },
4870 { "force-reload", MORE, 2, start_unit }, /* For compatibility with SysV */
4871 { "condreload", MORE, 2, start_unit }, /* For compatibility with ALTLinux */
4872 { "condrestart", MORE, 2, start_unit }, /* For compatibility with RH */
4873 { "isolate", EQUAL, 2, start_unit },
4874 { "kill", MORE, 2, kill_unit },
4875 { "is-active", MORE, 2, check_unit },
4876 { "check", MORE, 2, check_unit },
4877 { "show", MORE, 1, show },
4878 { "status", MORE, 2, show },
4879 { "dump", EQUAL, 1, dump },
4880 { "dot", EQUAL, 1, dot },
4881 { "snapshot", LESS, 2, snapshot },
4882 { "delete", MORE, 2, delete_snapshot },
4883 { "daemon-reload", EQUAL, 1, daemon_reload },
4884 { "daemon-reexec", EQUAL, 1, daemon_reload },
4885 { "show-environment", EQUAL, 1, show_enviroment },
4886 { "set-environment", MORE, 2, set_environment },
4887 { "unset-environment", MORE, 2, set_environment },
4888 { "halt", EQUAL, 1, start_special },
4889 { "poweroff", EQUAL, 1, start_special },
4890 { "reboot", EQUAL, 1, start_special },
4891 { "kexec", EQUAL, 1, start_special },
4892 { "default", EQUAL, 1, start_special },
4893 { "rescue", EQUAL, 1, start_special },
4894 { "emergency", EQUAL, 1, start_special },
4895 { "exit", EQUAL, 1, start_special },
4896 { "reset-failed", MORE, 1, reset_failed },
4897 { "enable", MORE, 2, enable_unit },
4898 { "disable", MORE, 2, enable_unit },
4899 { "is-enabled", MORE, 2, unit_is_enabled },
4900 { "reenable", MORE, 2, enable_unit },
4901 { "preset", MORE, 2, enable_unit },
4902 { "mask", MORE, 2, enable_unit },
4903 { "unmask", MORE, 2, enable_unit },
4904 { "link", MORE, 2, enable_unit }
4914 left = argc - optind;
4917 /* Special rule: no arguments means "list-units" */
4920 if (streq(argv[optind], "help")) {
4925 for (i = 0; i < ELEMENTSOF(verbs); i++)
4926 if (streq(argv[optind], verbs[i].verb))
4929 if (i >= ELEMENTSOF(verbs)) {
4930 log_error("Unknown operation %s", argv[optind]);
4935 switch (verbs[i].argc_cmp) {
4938 if (left != verbs[i].argc) {
4939 log_error("Invalid number of arguments.");
4946 if (left < verbs[i].argc) {
4947 log_error("Too few arguments.");
4954 if (left > verbs[i].argc) {
4955 log_error("Too many arguments.");
4962 assert_not_reached("Unknown comparison operator.");
4965 /* Require a bus connection for all operations but
4967 if (!streq(verbs[i].verb, "enable") &&
4968 !streq(verbs[i].verb, "disable") &&
4969 !streq(verbs[i].verb, "is-enable") &&
4970 !streq(verbs[i].verb, "reenable") &&
4971 !streq(verbs[i].verb, "preset") &&
4972 !streq(verbs[i].verb, "mask") &&
4973 !streq(verbs[i].verb, "unmask") &&
4974 !streq(verbs[i].verb, "link")) {
4976 if (running_in_chroot() > 0) {
4977 log_info("Running in chroot, ignoring request.");
4981 if (!bus && !avoid_bus()) {
4982 log_error("Failed to get D-Bus connection: %s", error->message);
4987 return verbs[i].dispatch(bus, argv + optind);
4990 static int send_shutdownd(usec_t t, char mode, bool dry_run, bool warn, const char *message) {
4992 struct msghdr msghdr;
4994 union sockaddr_union sockaddr;
4995 struct shutdownd_command c;
5000 c.dry_run = dry_run;
5004 strncpy(c.wall_message, message, sizeof(c.wall_message));
5006 if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0)
5010 sockaddr.sa.sa_family = AF_UNIX;
5011 sockaddr.un.sun_path[0] = 0;
5012 strncpy(sockaddr.un.sun_path, "/run/systemd/shutdownd", sizeof(sockaddr.un.sun_path));
5015 iovec.iov_base = (char*) &c;
5016 iovec.iov_len = sizeof(c);
5019 msghdr.msg_name = &sockaddr;
5020 msghdr.msg_namelen = offsetof(struct sockaddr_un, sun_path) + sizeof("/run/systemd/shutdownd") - 1;
5022 msghdr.msg_iov = &iovec;
5023 msghdr.msg_iovlen = 1;
5025 if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) < 0) {
5026 close_nointr_nofail(fd);
5030 close_nointr_nofail(fd);
5034 static int reload_with_fallback(DBusConnection *bus) {
5037 /* First, try systemd via D-Bus. */
5038 if (daemon_reload(bus, NULL) >= 0)
5042 /* Nothing else worked, so let's try signals */
5043 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
5045 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
5046 log_error("kill() failed: %m");
5053 static int start_with_fallback(DBusConnection *bus) {
5056 /* First, try systemd via D-Bus. */
5057 if (start_unit(bus, NULL) >= 0)
5061 /* Hmm, talking to systemd via D-Bus didn't work. Then
5062 * let's try to talk to Upstart via D-Bus. */
5063 if (talk_upstart() > 0)
5066 /* Nothing else worked, so let's try
5068 if (talk_initctl() > 0)
5071 log_error("Failed to talk to init daemon.");
5075 warn_wall(arg_action);
5079 static int halt_main(DBusConnection *bus) {
5082 if (geteuid() != 0) {
5083 log_error("Must be root.");
5089 char date[FORMAT_TIMESTAMP_MAX];
5091 m = strv_join(arg_wall, " ");
5092 r = send_shutdownd(arg_when,
5093 arg_action == ACTION_HALT ? 'H' :
5094 arg_action == ACTION_POWEROFF ? 'P' :
5102 log_warning("Failed to talk to shutdownd, proceeding with immediate shutdown: %s", strerror(-r));
5104 log_info("Shutdown scheduled for %s, use 'shutdown -c' to cancel.",
5105 format_timestamp(date, sizeof(date), arg_when));
5110 if (!arg_dry && !arg_immediate)
5111 return start_with_fallback(bus);
5114 if (sd_booted() > 0)
5115 log_debug("Not writing utmp record, assuming that systemd-update-utmp is used.");
5116 else if ((r = utmp_put_shutdown(0)) < 0)
5117 log_warning("Failed to write utmp record: %s", strerror(-r));
5126 /* Make sure C-A-D is handled by the kernel from this
5128 reboot(RB_ENABLE_CAD);
5130 switch (arg_action) {
5133 log_info("Halting.");
5134 reboot(RB_HALT_SYSTEM);
5137 case ACTION_POWEROFF:
5138 log_info("Powering off.");
5139 reboot(RB_POWER_OFF);
5143 log_info("Rebooting.");
5144 reboot(RB_AUTOBOOT);
5148 assert_not_reached("Unknown halt action.");
5151 /* We should never reach this. */
5155 static int runlevel_main(void) {
5156 int r, runlevel, previous;
5158 r = utmp_get_runlevel(&runlevel, &previous);
5165 previous <= 0 ? 'N' : previous,
5166 runlevel <= 0 ? 'N' : runlevel);
5171 int main(int argc, char*argv[]) {
5172 int r, retval = EXIT_FAILURE;
5173 DBusConnection *bus = NULL;
5176 dbus_error_init(&error);
5178 log_parse_environment();
5181 if ((r = parse_argv(argc, argv)) < 0)
5184 retval = EXIT_SUCCESS;
5188 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
5189 * let's shortcut this */
5190 if (arg_action == ACTION_RUNLEVEL) {
5191 r = runlevel_main();
5192 retval = r < 0 ? EXIT_FAILURE : r;
5196 if (running_in_chroot() > 0 && arg_action != ACTION_SYSTEMCTL) {
5197 log_info("Running in chroot, ignoring request.");
5203 if (arg_transport == TRANSPORT_NORMAL)
5204 bus_connect(arg_scope == UNIT_FILE_SYSTEM ? DBUS_BUS_SYSTEM : DBUS_BUS_SESSION, &bus, &private_bus, &error);
5205 else if (arg_transport == TRANSPORT_POLKIT) {
5206 bus_connect_system_polkit(&bus, &error);
5207 private_bus = false;
5208 } else if (arg_transport == TRANSPORT_SSH) {
5209 bus_connect_system_ssh(NULL, arg_host, &bus, &error);
5210 private_bus = false;
5212 assert_not_reached("Uh, invalid transport...");
5215 switch (arg_action) {
5217 case ACTION_SYSTEMCTL:
5218 r = systemctl_main(bus, argc, argv, &error);
5222 case ACTION_POWEROFF:
5228 case ACTION_RUNLEVEL2:
5229 case ACTION_RUNLEVEL3:
5230 case ACTION_RUNLEVEL4:
5231 case ACTION_RUNLEVEL5:
5233 case ACTION_EMERGENCY:
5234 case ACTION_DEFAULT:
5235 r = start_with_fallback(bus);
5240 r = reload_with_fallback(bus);
5243 case ACTION_CANCEL_SHUTDOWN:
5244 r = send_shutdownd(0, 0, false, false, NULL);
5247 case ACTION_INVALID:
5248 case ACTION_RUNLEVEL:
5250 assert_not_reached("Unknown action");
5253 retval = r < 0 ? EXIT_FAILURE : r;
5257 dbus_connection_flush(bus);
5258 dbus_connection_close(bus);
5259 dbus_connection_unref(bus);
5262 dbus_error_free(&error);
5266 strv_free(arg_property);