1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/reboot.h>
28 #include <sys/ioctl.h>
32 #include <sys/socket.h>
35 #include <sys/prctl.h>
36 #include <dbus/dbus.h>
42 #include "utmp-wtmp.h"
46 #include "dbus-common.h"
47 #include "cgroup-show.h"
48 #include "cgroup-util.h"
50 #include "path-lookup.h"
51 #include "conf-parser.h"
52 #include "sd-daemon.h"
53 #include "shutdownd.h"
54 #include "exit-status.h"
55 #include "bus-errors.h"
57 #include "unit-name.h"
59 #include "spawn-agent.h"
62 static const char *arg_type = NULL;
63 static char **arg_property = NULL;
64 static bool arg_all = false;
65 static const char *arg_job_mode = "replace";
66 static UnitFileScope arg_scope = UNIT_FILE_SYSTEM;
67 static bool arg_immediate = false;
68 static bool arg_no_block = false;
69 static bool arg_no_pager = false;
70 static bool arg_no_wtmp = false;
71 static bool arg_no_sync = false;
72 static bool arg_no_wall = false;
73 static bool arg_no_reload = false;
74 static bool arg_dry = false;
75 static bool arg_quiet = false;
76 static bool arg_full = false;
77 static bool arg_force = false;
78 static bool arg_ask_password = false;
79 static bool arg_failed = false;
80 static bool arg_runtime = false;
81 static char **arg_wall = NULL;
82 static const char *arg_kill_who = NULL;
83 static const char *arg_kill_mode = NULL;
84 static int arg_signal = SIGTERM;
85 static const char *arg_root = NULL;
86 static usec_t arg_when = 0;
105 ACTION_CANCEL_SHUTDOWN,
107 } arg_action = ACTION_SYSTEMCTL;
113 static enum transport {
117 } arg_transport = TRANSPORT_NORMAL;
118 static const char *arg_host = NULL;
120 static bool private_bus = false;
122 static int daemon_reload(DBusConnection *bus, char **args);
124 static bool on_tty(void) {
127 /* Note that this is invoked relatively early, before we start
128 * the pager. That means the value we return reflects whether
129 * we originally were started on a tty, not if we currently
130 * are. But this is intended, since we want colour and so on
131 * when run in our own pager. */
133 if (_unlikely_(t < 0))
134 t = isatty(STDOUT_FILENO) > 0;
139 static void pager_open_if_enabled(void) {
148 static void agent_open_if_enabled(void) {
150 /* Open the password agent as a child process if necessary */
152 if (!arg_ask_password)
155 if (arg_scope != UNIT_FILE_SYSTEM)
161 static const char *ansi_highlight(bool b) {
166 return b ? ANSI_HIGHLIGHT_ON : ANSI_HIGHLIGHT_OFF;
169 static const char *ansi_highlight_green(bool b) {
174 return b ? ANSI_HIGHLIGHT_GREEN_ON : ANSI_HIGHLIGHT_OFF;
177 static bool error_is_no_service(const DBusError *error) {
180 if (!dbus_error_is_set(error))
183 if (dbus_error_has_name(error, DBUS_ERROR_NAME_HAS_NO_OWNER))
186 if (dbus_error_has_name(error, DBUS_ERROR_SERVICE_UNKNOWN))
189 return startswith(error->name, "org.freedesktop.DBus.Error.Spawn.");
192 static int translate_bus_error_to_exit_status(int r, const DBusError *error) {
195 if (!dbus_error_is_set(error))
198 if (dbus_error_has_name(error, DBUS_ERROR_ACCESS_DENIED) ||
199 dbus_error_has_name(error, BUS_ERROR_ONLY_BY_DEPENDENCY) ||
200 dbus_error_has_name(error, BUS_ERROR_NO_ISOLATION) ||
201 dbus_error_has_name(error, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE))
202 return EXIT_NOPERMISSION;
204 if (dbus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT))
205 return EXIT_NOTINSTALLED;
207 if (dbus_error_has_name(error, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE) ||
208 dbus_error_has_name(error, BUS_ERROR_NOT_SUPPORTED))
209 return EXIT_NOTIMPLEMENTED;
211 if (dbus_error_has_name(error, BUS_ERROR_LOAD_FAILED))
212 return EXIT_NOTCONFIGURED;
220 static void warn_wall(enum action action) {
221 static const char *table[_ACTION_MAX] = {
222 [ACTION_HALT] = "The system is going down for system halt NOW!",
223 [ACTION_REBOOT] = "The system is going down for reboot NOW!",
224 [ACTION_POWEROFF] = "The system is going down for power-off NOW!",
225 [ACTION_KEXEC] = "The system is going down for kexec reboot NOW!",
226 [ACTION_RESCUE] = "The system is going down to rescue mode NOW!",
227 [ACTION_EMERGENCY] = "The system is going down to emergency mode NOW!"
236 if (!(p = strv_join(arg_wall, " "))) {
237 log_error("Failed to join strings.");
253 utmp_wall(table[action], NULL);
256 static bool avoid_bus(void) {
258 if (running_in_chroot() > 0)
261 if (sd_booted() <= 0)
264 if (!isempty(arg_root))
267 if (arg_scope == UNIT_FILE_GLOBAL)
275 const char *description;
276 const char *load_state;
277 const char *active_state;
278 const char *sub_state;
279 const char *following;
280 const char *unit_path;
282 const char *job_type;
283 const char *job_path;
286 static int compare_unit_info(const void *a, const void *b) {
288 const struct unit_info *u = a, *v = b;
290 d1 = strrchr(u->id, '.');
291 d2 = strrchr(v->id, '.');
296 if ((r = strcasecmp(d1, d2)) != 0)
300 return strcasecmp(u->id, v->id);
303 static bool output_show_unit(const struct unit_info *u) {
307 return streq(u->active_state, "failed");
309 return (!arg_type || ((dot = strrchr(u->id, '.')) &&
310 streq(dot+1, arg_type))) &&
311 (arg_all || !(streq(u->active_state, "inactive") || u->following[0]) || u->job_id > 0);
314 static void output_units_list(const struct unit_info *unit_infos, unsigned c) {
315 unsigned active_len, sub_len, job_len, n_shown = 0;
316 const struct unit_info *u;
318 active_len = sizeof("ACTIVE")-1;
319 sub_len = sizeof("SUB")-1;
320 job_len = sizeof("JOB")-1;
322 for (u = unit_infos; u < unit_infos + c; u++) {
323 if (!output_show_unit(u))
326 active_len = MAX(active_len, strlen(u->active_state));
327 sub_len = MAX(sub_len, strlen(u->sub_state));
329 job_len = MAX(job_len, strlen(u->job_type));
333 printf("%-25s %-6s %-*s %-*s %-*s", "UNIT", "LOAD",
334 active_len, "ACTIVE", sub_len, "SUB", job_len, "JOB");
335 if (columns() >= 80+12 || arg_full || !arg_no_pager)
336 printf(" %s\n", "DESCRIPTION");
341 for (u = unit_infos; u < unit_infos + c; u++) {
344 const char *on_loaded, *off_loaded;
345 const char *on_active, *off_active;
347 if (!output_show_unit(u))
352 if (!streq(u->load_state, "loaded") &&
353 !streq(u->load_state, "banned")) {
354 on_loaded = ansi_highlight(true);
355 off_loaded = ansi_highlight(false);
357 on_loaded = off_loaded = "";
359 if (streq(u->active_state, "failed")) {
360 on_active = ansi_highlight(true);
361 off_active = ansi_highlight(false);
363 on_active = off_active = "";
365 e = arg_full ? NULL : ellipsize(u->id, 25, 33);
367 printf("%-25s %s%-6s%s %s%-*s %-*s%s%n",
369 on_loaded, u->load_state, off_loaded,
370 on_active, active_len, u->active_state,
371 sub_len, u->sub_state, off_active,
376 a -= strlen(on_loaded) + strlen(off_loaded);
377 a -= strlen(on_active) + strlen(off_active);
380 printf(" %-*s", job_len, u->job_type);
384 if (a + b + 1 < columns()) {
386 printf(" %-*s", job_len, "");
388 if (arg_full || !arg_no_pager)
389 printf(" %s", u->description);
391 printf(" %.*s", columns() - a - b - 1, u->description);
398 printf("\nLOAD = Reflects whether the unit definition was properly loaded.\n"
399 "ACTIVE = The high-level unit activation state, i.e. generalization of SUB.\n"
400 "SUB = The low-level unit activation state, values depend on unit type.\n"
401 "JOB = Pending job for the unit.\n");
404 printf("\n%u units listed.\n", n_shown);
406 printf("\n%u units listed. Pass --all to see inactive units, too.\n", n_shown);
410 static int list_units(DBusConnection *bus, char **args) {
411 DBusMessage *m = NULL, *reply = NULL;
414 DBusMessageIter iter, sub, sub2;
415 unsigned c = 0, n_units = 0;
416 struct unit_info *unit_infos = NULL;
418 dbus_error_init(&error);
422 pager_open_if_enabled();
424 if (!(m = dbus_message_new_method_call(
425 "org.freedesktop.systemd1",
426 "/org/freedesktop/systemd1",
427 "org.freedesktop.systemd1.Manager",
429 log_error("Could not allocate message.");
433 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
434 log_error("Failed to issue method call: %s", bus_error_message(&error));
439 if (!dbus_message_iter_init(reply, &iter) ||
440 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
441 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
442 log_error("Failed to parse reply.");
447 dbus_message_iter_recurse(&iter, &sub);
449 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
452 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
453 log_error("Failed to parse reply.");
461 n_units = MAX(2*c, 16);
462 w = realloc(unit_infos, sizeof(struct unit_info) * n_units);
465 log_error("Failed to allocate unit array.");
475 dbus_message_iter_recurse(&sub, &sub2);
477 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->id, true) < 0 ||
478 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->description, true) < 0 ||
479 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->load_state, true) < 0 ||
480 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->active_state, true) < 0 ||
481 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->sub_state, true) < 0 ||
482 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->following, true) < 0 ||
483 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &u->unit_path, true) < 0 ||
484 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &u->job_id, true) < 0 ||
485 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->job_type, true) < 0 ||
486 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &u->job_path, false) < 0) {
487 log_error("Failed to parse reply.");
492 dbus_message_iter_next(&sub);
497 qsort(unit_infos, c, sizeof(struct unit_info), compare_unit_info);
498 output_units_list(unit_infos, c);
505 dbus_message_unref(m);
508 dbus_message_unref(reply);
512 dbus_error_free(&error);
517 static int compare_unit_file_list(const void *a, const void *b) {
519 const UnitFileList *u = a, *v = b;
521 d1 = strrchr(u->path, '.');
522 d2 = strrchr(v->path, '.');
527 r = strcasecmp(d1, d2);
532 return strcasecmp(file_name_from_path(u->path), file_name_from_path(v->path));
535 static bool output_show_unit_file(const UnitFileList *u) {
538 return !arg_type || ((dot = strrchr(u->path, '.')) && streq(dot+1, arg_type));
541 static void output_unit_file_list(const UnitFileList *units, unsigned c) {
542 unsigned n_shown = 0;
543 const UnitFileList *u;
546 printf("%-25s %-6s\n", "UNIT FILE", "STATE");
548 for (u = units; u < units + c; u++) {
550 const char *on, *off;
553 if (!output_show_unit_file(u))
558 if (u->state == UNIT_FILE_MASKED ||
559 u->state == UNIT_FILE_MASKED_RUNTIME ||
560 u->state == UNIT_FILE_DISABLED) {
561 on = ansi_highlight(true);
562 off = ansi_highlight(false);
563 } else if (u->state == UNIT_FILE_ENABLED) {
564 on = ansi_highlight_green(true);
565 off = ansi_highlight_green(false);
569 id = file_name_from_path(u->path);
571 e = arg_full ? NULL : ellipsize(id, 25, 33);
573 printf("%-25s %s%-6s%s\n",
575 on, unit_file_state_to_string(u->state), off);
581 printf("\n%u unit files listed.\n", n_shown);
584 static int list_unit_files(DBusConnection *bus, char **args) {
585 DBusMessage *m = NULL, *reply = NULL;
588 DBusMessageIter iter, sub, sub2;
589 unsigned c = 0, n_units = 0;
590 UnitFileList *units = NULL;
592 dbus_error_init(&error);
596 pager_open_if_enabled();
603 h = hashmap_new(string_hash_func, string_compare_func);
605 log_error("Out of memory");
609 r = unit_file_get_list(arg_scope, arg_root, h);
611 unit_file_list_free(h);
612 log_error("Failed to get unit file list: %s", strerror(-r));
616 n_units = hashmap_size(h);
617 units = new(UnitFileList, n_units);
619 unit_file_list_free(h);
620 log_error("Out of memory");
624 HASHMAP_FOREACH(u, h, i) {
625 memcpy(units + c++, u, sizeof(UnitFileList));
631 m = dbus_message_new_method_call(
632 "org.freedesktop.systemd1",
633 "/org/freedesktop/systemd1",
634 "org.freedesktop.systemd1.Manager",
637 log_error("Could not allocate message.");
641 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
643 log_error("Failed to issue method call: %s", bus_error_message(&error));
648 if (!dbus_message_iter_init(reply, &iter) ||
649 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
650 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
651 log_error("Failed to parse reply.");
656 dbus_message_iter_recurse(&iter, &sub);
658 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
662 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
663 log_error("Failed to parse reply.");
671 n_units = MAX(2*c, 16);
672 w = realloc(units, sizeof(struct UnitFileList) * n_units);
675 log_error("Failed to allocate unit array.");
685 dbus_message_iter_recurse(&sub, &sub2);
687 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->path, true) < 0 ||
688 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, false) < 0) {
689 log_error("Failed to parse reply.");
694 u->state = unit_file_state_from_string(state);
696 dbus_message_iter_next(&sub);
702 qsort(units, c, sizeof(UnitFileList), compare_unit_file_list);
703 output_unit_file_list(units, c);
710 dbus_message_unref(m);
713 dbus_message_unref(reply);
717 dbus_error_free(&error);
722 static int dot_one_property(const char *name, const char *prop, DBusMessageIter *iter) {
723 static const char * const colors[] = {
724 "Requires", "[color=\"black\"]",
725 "RequiresOverridable", "[color=\"black\"]",
726 "Requisite", "[color=\"darkblue\"]",
727 "RequisiteOverridable", "[color=\"darkblue\"]",
728 "Wants", "[color=\"darkgrey\"]",
729 "Conflicts", "[color=\"red\"]",
730 "ConflictedBy", "[color=\"red\"]",
731 "After", "[color=\"green\"]"
734 const char *c = NULL;
741 for (i = 0; i < ELEMENTSOF(colors); i += 2)
742 if (streq(colors[i], prop)) {
750 if (arg_dot != DOT_ALL)
751 if ((arg_dot == DOT_ORDER) != streq(prop, "After"))
754 switch (dbus_message_iter_get_arg_type(iter)) {
756 case DBUS_TYPE_ARRAY:
758 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
761 dbus_message_iter_recurse(iter, &sub);
763 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
766 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
767 dbus_message_iter_get_basic(&sub, &s);
768 printf("\t\"%s\"->\"%s\" %s;\n", name, s, c);
770 dbus_message_iter_next(&sub);
780 static int dot_one(DBusConnection *bus, const char *name, const char *path) {
781 DBusMessage *m = NULL, *reply = NULL;
782 const char *interface = "org.freedesktop.systemd1.Unit";
785 DBusMessageIter iter, sub, sub2, sub3;
790 dbus_error_init(&error);
792 if (!(m = dbus_message_new_method_call(
793 "org.freedesktop.systemd1",
795 "org.freedesktop.DBus.Properties",
797 log_error("Could not allocate message.");
802 if (!dbus_message_append_args(m,
803 DBUS_TYPE_STRING, &interface,
804 DBUS_TYPE_INVALID)) {
805 log_error("Could not append arguments to message.");
810 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
811 log_error("Failed to issue method call: %s", bus_error_message(&error));
816 if (!dbus_message_iter_init(reply, &iter) ||
817 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
818 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
819 log_error("Failed to parse reply.");
824 dbus_message_iter_recurse(&iter, &sub);
826 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
829 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
830 log_error("Failed to parse reply.");
835 dbus_message_iter_recurse(&sub, &sub2);
837 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &prop, true) < 0) {
838 log_error("Failed to parse reply.");
843 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
844 log_error("Failed to parse reply.");
849 dbus_message_iter_recurse(&sub2, &sub3);
851 if (dot_one_property(name, prop, &sub3)) {
852 log_error("Failed to parse reply.");
857 dbus_message_iter_next(&sub);
864 dbus_message_unref(m);
867 dbus_message_unref(reply);
869 dbus_error_free(&error);
874 static int dot(DBusConnection *bus, char **args) {
875 DBusMessage *m = NULL, *reply = NULL;
878 DBusMessageIter iter, sub, sub2;
880 dbus_error_init(&error);
884 if (!(m = dbus_message_new_method_call(
885 "org.freedesktop.systemd1",
886 "/org/freedesktop/systemd1",
887 "org.freedesktop.systemd1.Manager",
889 log_error("Could not allocate message.");
893 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
894 log_error("Failed to issue method call: %s", bus_error_message(&error));
899 if (!dbus_message_iter_init(reply, &iter) ||
900 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
901 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
902 log_error("Failed to parse reply.");
907 printf("digraph systemd {\n");
909 dbus_message_iter_recurse(&iter, &sub);
910 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
911 const char *id, *description, *load_state, *active_state, *sub_state, *following, *unit_path;
913 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
914 log_error("Failed to parse reply.");
919 dbus_message_iter_recurse(&sub, &sub2);
921 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
922 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
923 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
924 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
925 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
926 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &following, true) < 0 ||
927 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, true) < 0) {
928 log_error("Failed to parse reply.");
933 if ((r = dot_one(bus, id, unit_path)) < 0)
936 /* printf("\t\"%s\";\n", id); */
937 dbus_message_iter_next(&sub);
942 log_info(" Color legend: black = Requires\n"
943 " dark blue = Requisite\n"
944 " dark grey = Wants\n"
949 log_notice("-- You probably want to process this output with graphviz' dot tool.\n"
950 "-- Try a shell pipeline like 'systemctl dot | dot -Tsvg > systemd.svg'!\n");
956 dbus_message_unref(m);
959 dbus_message_unref(reply);
961 dbus_error_free(&error);
966 static int list_jobs(DBusConnection *bus, char **args) {
967 DBusMessage *m = NULL, *reply = NULL;
970 DBusMessageIter iter, sub, sub2;
973 dbus_error_init(&error);
977 pager_open_if_enabled();
979 if (!(m = dbus_message_new_method_call(
980 "org.freedesktop.systemd1",
981 "/org/freedesktop/systemd1",
982 "org.freedesktop.systemd1.Manager",
984 log_error("Could not allocate message.");
988 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
989 log_error("Failed to issue method call: %s", bus_error_message(&error));
994 if (!dbus_message_iter_init(reply, &iter) ||
995 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
996 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
997 log_error("Failed to parse reply.");
1002 dbus_message_iter_recurse(&iter, &sub);
1005 printf("%4s %-25s %-15s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
1007 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1008 const char *name, *type, *state, *job_path, *unit_path;
1012 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
1013 log_error("Failed to parse reply.");
1018 dbus_message_iter_recurse(&sub, &sub2);
1020 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
1021 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
1022 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
1023 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
1024 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
1025 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
1026 log_error("Failed to parse reply.");
1031 e = arg_full ? NULL : ellipsize(name, 25, 33);
1032 printf("%4u %-25s %-15s %-7s\n", id, e ? e : name, type, state);
1037 dbus_message_iter_next(&sub);
1041 printf("\n%u jobs listed.\n", k);
1047 dbus_message_unref(m);
1050 dbus_message_unref(reply);
1052 dbus_error_free(&error);
1057 static int load_unit(DBusConnection *bus, char **args) {
1058 DBusMessage *m = NULL, *reply = NULL;
1063 dbus_error_init(&error);
1068 STRV_FOREACH(name, args+1) {
1070 if (!(m = dbus_message_new_method_call(
1071 "org.freedesktop.systemd1",
1072 "/org/freedesktop/systemd1",
1073 "org.freedesktop.systemd1.Manager",
1075 log_error("Could not allocate message.");
1080 if (!dbus_message_append_args(m,
1081 DBUS_TYPE_STRING, name,
1082 DBUS_TYPE_INVALID)) {
1083 log_error("Could not append arguments to message.");
1088 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1089 log_error("Failed to issue method call: %s", bus_error_message(&error));
1094 dbus_message_unref(m);
1095 dbus_message_unref(reply);
1104 dbus_message_unref(m);
1107 dbus_message_unref(reply);
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, *reply = 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) {
1816 if (!(m = dbus_message_new_method_call(
1817 "org.freedesktop.systemd1",
1818 "/org/freedesktop/systemd1",
1819 "org.freedesktop.systemd1.Manager",
1821 log_error("Could not allocate message.");
1826 if (!dbus_message_append_args(m,
1827 DBUS_TYPE_STRING, name,
1828 DBUS_TYPE_STRING, &arg_kill_who,
1829 DBUS_TYPE_STRING, &arg_kill_mode,
1830 DBUS_TYPE_INT32, &arg_signal,
1831 DBUS_TYPE_INVALID)) {
1832 log_error("Could not append arguments to message.");
1837 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1838 log_error("Failed to issue method call: %s", bus_error_message(&error));
1839 dbus_error_free(&error);
1843 dbus_message_unref(m);
1846 dbus_message_unref(reply);
1852 dbus_message_unref(m);
1855 dbus_message_unref(reply);
1857 dbus_error_free(&error);
1862 typedef struct ExecStatusInfo {
1870 usec_t start_timestamp;
1871 usec_t exit_timestamp;
1876 LIST_FIELDS(struct ExecStatusInfo, exec);
1879 static void exec_status_info_free(ExecStatusInfo *i) {
1888 static int exec_status_info_deserialize(DBusMessageIter *sub, ExecStatusInfo *i) {
1889 uint64_t start_timestamp, exit_timestamp, start_timestamp_monotonic, exit_timestamp_monotonic;
1890 DBusMessageIter sub2, sub3;
1894 int32_t code, status;
1900 if (dbus_message_iter_get_arg_type(sub) != DBUS_TYPE_STRUCT)
1903 dbus_message_iter_recurse(sub, &sub2);
1905 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
1908 if (!(i->path = strdup(path)))
1911 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
1912 dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
1916 dbus_message_iter_recurse(&sub2, &sub3);
1917 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1918 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1919 dbus_message_iter_next(&sub3);
1924 if (!(i->argv = new0(char*, n+1)))
1928 dbus_message_iter_recurse(&sub2, &sub3);
1929 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1932 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1933 dbus_message_iter_get_basic(&sub3, &s);
1934 dbus_message_iter_next(&sub3);
1936 if (!(i->argv[n++] = strdup(s)))
1940 if (!dbus_message_iter_next(&sub2) ||
1941 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, true) < 0 ||
1942 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp, true) < 0 ||
1943 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp_monotonic, true) < 0 ||
1944 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp, true) < 0 ||
1945 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp_monotonic, true) < 0 ||
1946 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) < 0 ||
1947 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) < 0 ||
1948 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) < 0)
1952 i->start_timestamp = (usec_t) start_timestamp;
1953 i->exit_timestamp = (usec_t) exit_timestamp;
1954 i->pid = (pid_t) pid;
1961 typedef struct UnitStatusInfo {
1963 const char *load_state;
1964 const char *active_state;
1965 const char *sub_state;
1966 const char *unit_file_state;
1968 const char *description;
1969 const char *following;
1972 const char *default_control_group;
1974 const char *load_error;
1976 usec_t inactive_exit_timestamp;
1977 usec_t active_enter_timestamp;
1978 usec_t active_exit_timestamp;
1979 usec_t inactive_enter_timestamp;
1981 bool need_daemon_reload;
1986 const char *status_text;
1988 #ifdef HAVE_SYSV_COMPAT
1992 usec_t start_timestamp;
1993 usec_t exit_timestamp;
1995 int exit_code, exit_status;
1997 usec_t condition_timestamp;
1998 bool condition_result;
2001 unsigned n_accepted;
2002 unsigned n_connections;
2006 const char *sysfs_path;
2008 /* Mount, Automount */
2014 LIST_HEAD(ExecStatusInfo, exec);
2017 static void print_status_info(UnitStatusInfo *i) {
2019 const char *on, *off, *ss;
2021 char since1[FORMAT_TIMESTAMP_PRETTY_MAX], *s1;
2022 char since2[FORMAT_TIMESTAMP_MAX], *s2;
2026 /* This shows pretty information about a unit. See
2027 * print_property() for a low-level property printer */
2029 printf("%s", strna(i->id));
2031 if (i->description && !streq_ptr(i->id, i->description))
2032 printf(" - %s", i->description);
2037 printf("\t Follow: unit currently follows state of %s\n", i->following);
2039 if (streq_ptr(i->load_state, "failed") ||
2040 streq_ptr(i->load_state, "banned")) {
2041 on = ansi_highlight(true);
2042 off = ansi_highlight(false);
2047 printf("\t Loaded: %s%s%s (Reason: %s)\n", on, strna(i->load_state), off, i->load_error);
2048 else if (i->path && i->unit_file_state)
2049 printf("\t Loaded: %s%s%s (%s; %s)\n", on, strna(i->load_state), off, i->path, i->unit_file_state);
2051 printf("\t Loaded: %s%s%s (%s)\n", on, strna(i->load_state), off, i->path);
2053 printf("\t Loaded: %s%s%s\n", on, strna(i->load_state), off);
2055 ss = streq_ptr(i->active_state, i->sub_state) ? NULL : i->sub_state;
2057 if (streq_ptr(i->active_state, "failed")) {
2058 on = ansi_highlight(true);
2059 off = ansi_highlight(false);
2060 } else if (streq_ptr(i->active_state, "active") || streq_ptr(i->active_state, "reloading")) {
2061 on = ansi_highlight_green(true);
2062 off = ansi_highlight_green(false);
2067 printf("\t Active: %s%s (%s)%s",
2069 strna(i->active_state),
2073 printf("\t Active: %s%s%s",
2075 strna(i->active_state),
2078 timestamp = (streq_ptr(i->active_state, "active") ||
2079 streq_ptr(i->active_state, "reloading")) ? i->active_enter_timestamp :
2080 (streq_ptr(i->active_state, "inactive") ||
2081 streq_ptr(i->active_state, "failed")) ? i->inactive_enter_timestamp :
2082 streq_ptr(i->active_state, "activating") ? i->inactive_exit_timestamp :
2083 i->active_exit_timestamp;
2085 s1 = format_timestamp_pretty(since1, sizeof(since1), timestamp);
2086 s2 = format_timestamp(since2, sizeof(since2), timestamp);
2089 printf(" since %s; %s\n", s2, s1);
2091 printf(" since %s\n", s2);
2095 if (!i->condition_result && i->condition_timestamp > 0) {
2096 s1 = format_timestamp_pretty(since1, sizeof(since1), i->condition_timestamp);
2097 s2 = format_timestamp(since2, sizeof(since2), i->condition_timestamp);
2100 printf("\t start condition failed at %s; %s\n", s2, s1);
2102 printf("\t start condition failed at %s\n", s2);
2106 printf("\t Device: %s\n", i->sysfs_path);
2108 printf("\t Where: %s\n", i->where);
2110 printf("\t What: %s\n", i->what);
2113 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
2115 LIST_FOREACH(exec, p, i->exec) {
2119 /* Only show exited processes here */
2123 t = strv_join(p->argv, " ");
2124 printf("\t Process: %u %s=%s ", p->pid, p->name, strna(t));
2127 #ifdef HAVE_SYSV_COMPAT
2129 good = is_clean_exit_lsb(p->code, p->status);
2132 good = is_clean_exit(p->code, p->status);
2135 on = ansi_highlight(true);
2136 off = ansi_highlight(false);
2140 printf("%s(code=%s, ", on, sigchld_code_to_string(p->code));
2142 if (p->code == CLD_EXITED) {
2145 printf("status=%i", p->status);
2147 #ifdef HAVE_SYSV_COMPAT
2148 if ((c = exit_status_to_string(p->status, i->is_sysv ? EXIT_STATUS_LSB : EXIT_STATUS_SYSTEMD)))
2150 if ((c = exit_status_to_string(p->status, EXIT_STATUS_SYSTEMD)))
2155 printf("signal=%s", signal_to_string(p->status));
2157 printf(")%s\n", off);
2161 if (i->main_pid == p->pid &&
2162 i->start_timestamp == p->start_timestamp &&
2163 i->exit_timestamp == p->start_timestamp)
2164 /* Let's not show this twice */
2167 if (p->pid == i->control_pid)
2171 if (i->main_pid > 0 || i->control_pid > 0) {
2174 if (i->main_pid > 0) {
2175 printf("Main PID: %u", (unsigned) i->main_pid);
2179 get_process_name(i->main_pid, &t);
2184 } else if (i->exit_code > 0) {
2185 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
2187 if (i->exit_code == CLD_EXITED) {
2190 printf("status=%i", i->exit_status);
2192 #ifdef HAVE_SYSV_COMPAT
2193 if ((c = exit_status_to_string(i->exit_status, i->is_sysv ? EXIT_STATUS_LSB : EXIT_STATUS_SYSTEMD)))
2195 if ((c = exit_status_to_string(i->exit_status, EXIT_STATUS_SYSTEMD)))
2200 printf("signal=%s", signal_to_string(i->exit_status));
2205 if (i->main_pid > 0 && i->control_pid > 0)
2208 if (i->control_pid > 0) {
2211 printf(" Control: %u", (unsigned) i->control_pid);
2213 get_process_name(i->control_pid, &t);
2224 printf("\t Status: \"%s\"\n", i->status_text);
2226 if (i->default_control_group) {
2229 printf("\t CGroup: %s\n", i->default_control_group);
2231 if (arg_transport != TRANSPORT_SSH) {
2232 if ((c = columns()) > 18)
2237 show_cgroup_by_path(i->default_control_group, "\t\t ", c);
2241 if (i->need_daemon_reload)
2242 printf("\n%sWarning:%s Unit file changed on disk, 'systemctl %s daemon-reload' recommended.\n",
2243 ansi_highlight(true),
2244 ansi_highlight(false),
2245 arg_scope == UNIT_FILE_SYSTEM ? "--system" : "--user");
2248 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
2254 switch (dbus_message_iter_get_arg_type(iter)) {
2256 case DBUS_TYPE_STRING: {
2259 dbus_message_iter_get_basic(iter, &s);
2262 if (streq(name, "Id"))
2264 else if (streq(name, "LoadState"))
2266 else if (streq(name, "ActiveState"))
2267 i->active_state = s;
2268 else if (streq(name, "SubState"))
2270 else if (streq(name, "Description"))
2272 else if (streq(name, "FragmentPath"))
2274 #ifdef HAVE_SYSV_COMPAT
2275 else if (streq(name, "SysVPath")) {
2280 else if (streq(name, "DefaultControlGroup"))
2281 i->default_control_group = s;
2282 else if (streq(name, "StatusText"))
2284 else if (streq(name, "SysFSPath"))
2286 else if (streq(name, "Where"))
2288 else if (streq(name, "What"))
2290 else if (streq(name, "Following"))
2292 else if (streq(name, "UnitFileState"))
2293 i->unit_file_state = s;
2299 case DBUS_TYPE_BOOLEAN: {
2302 dbus_message_iter_get_basic(iter, &b);
2304 if (streq(name, "Accept"))
2306 else if (streq(name, "NeedDaemonReload"))
2307 i->need_daemon_reload = b;
2308 else if (streq(name, "ConditionResult"))
2309 i->condition_result = b;
2314 case DBUS_TYPE_UINT32: {
2317 dbus_message_iter_get_basic(iter, &u);
2319 if (streq(name, "MainPID")) {
2321 i->main_pid = (pid_t) u;
2324 } else if (streq(name, "ControlPID"))
2325 i->control_pid = (pid_t) u;
2326 else if (streq(name, "ExecMainPID")) {
2328 i->main_pid = (pid_t) u;
2329 } else if (streq(name, "NAccepted"))
2331 else if (streq(name, "NConnections"))
2332 i->n_connections = u;
2337 case DBUS_TYPE_INT32: {
2340 dbus_message_iter_get_basic(iter, &j);
2342 if (streq(name, "ExecMainCode"))
2343 i->exit_code = (int) j;
2344 else if (streq(name, "ExecMainStatus"))
2345 i->exit_status = (int) j;
2350 case DBUS_TYPE_UINT64: {
2353 dbus_message_iter_get_basic(iter, &u);
2355 if (streq(name, "ExecMainStartTimestamp"))
2356 i->start_timestamp = (usec_t) u;
2357 else if (streq(name, "ExecMainExitTimestamp"))
2358 i->exit_timestamp = (usec_t) u;
2359 else if (streq(name, "ActiveEnterTimestamp"))
2360 i->active_enter_timestamp = (usec_t) u;
2361 else if (streq(name, "InactiveEnterTimestamp"))
2362 i->inactive_enter_timestamp = (usec_t) u;
2363 else if (streq(name, "InactiveExitTimestamp"))
2364 i->inactive_exit_timestamp = (usec_t) u;
2365 else if (streq(name, "ActiveExitTimestamp"))
2366 i->active_exit_timestamp = (usec_t) u;
2367 else if (streq(name, "ConditionTimestamp"))
2368 i->condition_timestamp = (usec_t) u;
2373 case DBUS_TYPE_ARRAY: {
2375 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT &&
2376 startswith(name, "Exec")) {
2377 DBusMessageIter sub;
2379 dbus_message_iter_recurse(iter, &sub);
2380 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2381 ExecStatusInfo *info;
2384 if (!(info = new0(ExecStatusInfo, 1)))
2387 if (!(info->name = strdup(name))) {
2392 if ((r = exec_status_info_deserialize(&sub, info)) < 0) {
2397 LIST_PREPEND(ExecStatusInfo, exec, i->exec, info);
2399 dbus_message_iter_next(&sub);
2406 case DBUS_TYPE_STRUCT: {
2408 if (streq(name, "LoadError")) {
2409 DBusMessageIter sub;
2410 const char *n, *message;
2413 dbus_message_iter_recurse(iter, &sub);
2415 r = bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &n, true);
2419 r = bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &message, false);
2423 if (!isempty(message))
2424 i->load_error = message;
2434 static int print_property(const char *name, DBusMessageIter *iter) {
2438 /* This is a low-level property printer, see
2439 * print_status_info() for the nicer output */
2441 if (arg_property && !strv_find(arg_property, name))
2444 switch (dbus_message_iter_get_arg_type(iter)) {
2446 case DBUS_TYPE_STRUCT: {
2447 DBusMessageIter sub;
2448 dbus_message_iter_recurse(iter, &sub);
2450 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
2453 dbus_message_iter_get_basic(&sub, &u);
2456 printf("%s=%u\n", name, (unsigned) u);
2458 printf("%s=\n", name);
2461 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
2464 dbus_message_iter_get_basic(&sub, &s);
2466 if (arg_all || s[0])
2467 printf("%s=%s\n", name, s);
2470 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "LoadError")) {
2471 const char *a = NULL, *b = NULL;
2473 if (bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &a, true) >= 0)
2474 bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &b, false);
2476 if (arg_all || !isempty(a) || !isempty(b))
2477 printf("%s=%s \"%s\"\n", name, strempty(a), strempty(b));
2485 case DBUS_TYPE_ARRAY:
2487 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "EnvironmentFiles")) {
2488 DBusMessageIter sub, sub2;
2490 dbus_message_iter_recurse(iter, &sub);
2491 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2495 dbus_message_iter_recurse(&sub, &sub2);
2497 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) >= 0 &&
2498 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, false) >= 0)
2499 printf("EnvironmentFile=%s (ignore_errors=%s)\n", path, yes_no(ignore));
2501 dbus_message_iter_next(&sub);
2506 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
2507 DBusMessageIter sub, sub2;
2509 dbus_message_iter_recurse(iter, &sub);
2510 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2511 const char *type, *path;
2513 dbus_message_iter_recurse(&sub, &sub2);
2515 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
2516 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
2517 printf("%s=%s\n", type, path);
2519 dbus_message_iter_next(&sub);
2524 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
2525 DBusMessageIter sub, sub2;
2527 dbus_message_iter_recurse(iter, &sub);
2528 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2530 uint64_t value, next_elapse;
2532 dbus_message_iter_recurse(&sub, &sub2);
2534 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
2535 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
2536 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
2537 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
2539 printf("%s={ value=%s ; next_elapse=%s }\n",
2541 format_timespan(timespan1, sizeof(timespan1), value),
2542 format_timespan(timespan2, sizeof(timespan2), next_elapse));
2545 dbus_message_iter_next(&sub);
2550 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "ControlGroupAttributes")) {
2551 DBusMessageIter sub, sub2;
2553 dbus_message_iter_recurse(iter, &sub);
2554 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2555 const char *controller, *attr, *value;
2557 dbus_message_iter_recurse(&sub, &sub2);
2559 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &controller, true) >= 0 &&
2560 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &attr, true) >= 0 &&
2561 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &value, false) >= 0) {
2563 printf("ControlGroupAttribute={ controller=%s ; attribute=%s ; value=\"%s\" }\n",
2569 dbus_message_iter_next(&sub);
2574 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
2575 DBusMessageIter sub;
2577 dbus_message_iter_recurse(iter, &sub);
2578 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2579 ExecStatusInfo info;
2582 if (exec_status_info_deserialize(&sub, &info) >= 0) {
2583 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
2586 t = strv_join(info.argv, " ");
2588 printf("%s={ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n",
2592 yes_no(info.ignore),
2593 strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)),
2594 strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)),
2595 (unsigned) info. pid,
2596 sigchld_code_to_string(info.code),
2598 info.code == CLD_EXITED ? "" : "/",
2599 strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status)));
2605 strv_free(info.argv);
2607 dbus_message_iter_next(&sub);
2616 if (generic_print_property(name, iter, arg_all) > 0)
2620 printf("%s=[unprintable]\n", name);
2625 static int show_one(const char *verb, DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
2626 DBusMessage *m = NULL, *reply = NULL;
2627 const char *interface = "";
2630 DBusMessageIter iter, sub, sub2, sub3;
2631 UnitStatusInfo info;
2639 dbus_error_init(&error);
2641 if (!(m = dbus_message_new_method_call(
2642 "org.freedesktop.systemd1",
2644 "org.freedesktop.DBus.Properties",
2646 log_error("Could not allocate message.");
2651 if (!dbus_message_append_args(m,
2652 DBUS_TYPE_STRING, &interface,
2653 DBUS_TYPE_INVALID)) {
2654 log_error("Could not append arguments to message.");
2659 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2660 log_error("Failed to issue method call: %s", bus_error_message(&error));
2665 if (!dbus_message_iter_init(reply, &iter) ||
2666 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
2667 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
2668 log_error("Failed to parse reply.");
2673 dbus_message_iter_recurse(&iter, &sub);
2680 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2683 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
2684 log_error("Failed to parse reply.");
2689 dbus_message_iter_recurse(&sub, &sub2);
2691 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
2692 log_error("Failed to parse reply.");
2697 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
2698 log_error("Failed to parse reply.");
2703 dbus_message_iter_recurse(&sub2, &sub3);
2705 if (show_properties)
2706 r = print_property(name, &sub3);
2708 r = status_property(name, &sub3, &info);
2711 log_error("Failed to parse reply.");
2716 dbus_message_iter_next(&sub);
2721 if (!show_properties)
2722 print_status_info(&info);
2724 if (!streq_ptr(info.active_state, "active") &&
2725 !streq_ptr(info.active_state, "reloading") &&
2726 streq(verb, "status"))
2727 /* According to LSB: "program not running" */
2730 while ((p = info.exec)) {
2731 LIST_REMOVE(ExecStatusInfo, exec, info.exec, p);
2732 exec_status_info_free(p);
2737 dbus_message_unref(m);
2740 dbus_message_unref(reply);
2742 dbus_error_free(&error);
2747 static int show(DBusConnection *bus, char **args) {
2748 DBusMessage *m = NULL, *reply = NULL;
2751 bool show_properties, new_line = false;
2757 dbus_error_init(&error);
2759 show_properties = !streq(args[0], "status");
2761 if (show_properties)
2762 pager_open_if_enabled();
2764 if (show_properties && strv_length(args) <= 1) {
2765 /* If not argument is specified inspect the manager
2768 ret = show_one(args[0], bus, "/org/freedesktop/systemd1", show_properties, &new_line);
2772 STRV_FOREACH(name, args+1) {
2773 const char *path = NULL;
2776 if (safe_atou32(*name, &id) < 0) {
2778 /* Interpret as unit name */
2780 if (!(m = dbus_message_new_method_call(
2781 "org.freedesktop.systemd1",
2782 "/org/freedesktop/systemd1",
2783 "org.freedesktop.systemd1.Manager",
2785 log_error("Could not allocate message.");
2790 if (!dbus_message_append_args(m,
2791 DBUS_TYPE_STRING, name,
2792 DBUS_TYPE_INVALID)) {
2793 log_error("Could not append arguments to message.");
2798 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2800 if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
2801 log_error("Failed to issue method call: %s", bus_error_message(&error));
2806 dbus_error_free(&error);
2808 dbus_message_unref(m);
2809 if (!(m = dbus_message_new_method_call(
2810 "org.freedesktop.systemd1",
2811 "/org/freedesktop/systemd1",
2812 "org.freedesktop.systemd1.Manager",
2814 log_error("Could not allocate message.");
2819 if (!dbus_message_append_args(m,
2820 DBUS_TYPE_STRING, name,
2821 DBUS_TYPE_INVALID)) {
2822 log_error("Could not append arguments to message.");
2827 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2828 log_error("Failed to issue method call: %s", bus_error_message(&error));
2830 if (dbus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT))
2831 ret = 4; /* According to LSB: "program or service status is unknown" */
2838 } else if (show_properties) {
2840 /* Interpret as job id */
2842 if (!(m = dbus_message_new_method_call(
2843 "org.freedesktop.systemd1",
2844 "/org/freedesktop/systemd1",
2845 "org.freedesktop.systemd1.Manager",
2847 log_error("Could not allocate message.");
2852 if (!dbus_message_append_args(m,
2853 DBUS_TYPE_UINT32, &id,
2854 DBUS_TYPE_INVALID)) {
2855 log_error("Could not append arguments to message.");
2860 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2861 log_error("Failed to issue method call: %s", bus_error_message(&error));
2867 /* Interpret as PID */
2869 if (!(m = dbus_message_new_method_call(
2870 "org.freedesktop.systemd1",
2871 "/org/freedesktop/systemd1",
2872 "org.freedesktop.systemd1.Manager",
2874 log_error("Could not allocate message.");
2879 if (!dbus_message_append_args(m,
2880 DBUS_TYPE_UINT32, &id,
2881 DBUS_TYPE_INVALID)) {
2882 log_error("Could not append arguments to message.");
2887 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2888 log_error("Failed to issue method call: %s", bus_error_message(&error));
2894 if (!dbus_message_get_args(reply, &error,
2895 DBUS_TYPE_OBJECT_PATH, &path,
2896 DBUS_TYPE_INVALID)) {
2897 log_error("Failed to parse reply: %s", bus_error_message(&error));
2902 if ((r = show_one(args[0], bus, path, show_properties, &new_line)) != 0)
2905 dbus_message_unref(m);
2906 dbus_message_unref(reply);
2912 dbus_message_unref(m);
2915 dbus_message_unref(reply);
2917 dbus_error_free(&error);
2922 static int dump(DBusConnection *bus, char **args) {
2923 DBusMessage *m = NULL, *reply = NULL;
2928 dbus_error_init(&error);
2930 pager_open_if_enabled();
2932 if (!(m = dbus_message_new_method_call(
2933 "org.freedesktop.systemd1",
2934 "/org/freedesktop/systemd1",
2935 "org.freedesktop.systemd1.Manager",
2937 log_error("Could not allocate message.");
2941 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2942 log_error("Failed to issue method call: %s", bus_error_message(&error));
2947 if (!dbus_message_get_args(reply, &error,
2948 DBUS_TYPE_STRING, &text,
2949 DBUS_TYPE_INVALID)) {
2950 log_error("Failed to parse reply: %s", bus_error_message(&error));
2955 fputs(text, stdout);
2961 dbus_message_unref(m);
2964 dbus_message_unref(reply);
2966 dbus_error_free(&error);
2971 static int snapshot(DBusConnection *bus, char **args) {
2972 DBusMessage *m = NULL, *reply = NULL;
2975 const char *name = "", *path, *id;
2976 dbus_bool_t cleanup = FALSE;
2977 DBusMessageIter iter, sub;
2979 *interface = "org.freedesktop.systemd1.Unit",
2982 dbus_error_init(&error);
2984 if (!(m = dbus_message_new_method_call(
2985 "org.freedesktop.systemd1",
2986 "/org/freedesktop/systemd1",
2987 "org.freedesktop.systemd1.Manager",
2988 "CreateSnapshot"))) {
2989 log_error("Could not allocate message.");
2993 if (strv_length(args) > 1)
2996 if (!dbus_message_append_args(m,
2997 DBUS_TYPE_STRING, &name,
2998 DBUS_TYPE_BOOLEAN, &cleanup,
2999 DBUS_TYPE_INVALID)) {
3000 log_error("Could not append arguments to message.");
3005 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3006 log_error("Failed to issue method call: %s", bus_error_message(&error));
3011 if (!dbus_message_get_args(reply, &error,
3012 DBUS_TYPE_OBJECT_PATH, &path,
3013 DBUS_TYPE_INVALID)) {
3014 log_error("Failed to parse reply: %s", bus_error_message(&error));
3019 dbus_message_unref(m);
3020 if (!(m = dbus_message_new_method_call(
3021 "org.freedesktop.systemd1",
3023 "org.freedesktop.DBus.Properties",
3025 log_error("Could not allocate message.");
3029 if (!dbus_message_append_args(m,
3030 DBUS_TYPE_STRING, &interface,
3031 DBUS_TYPE_STRING, &property,
3032 DBUS_TYPE_INVALID)) {
3033 log_error("Could not append arguments to message.");
3038 dbus_message_unref(reply);
3039 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3040 log_error("Failed to issue method call: %s", bus_error_message(&error));
3045 if (!dbus_message_iter_init(reply, &iter) ||
3046 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
3047 log_error("Failed to parse reply.");
3052 dbus_message_iter_recurse(&iter, &sub);
3054 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
3055 log_error("Failed to parse reply.");
3060 dbus_message_iter_get_basic(&sub, &id);
3068 dbus_message_unref(m);
3071 dbus_message_unref(reply);
3073 dbus_error_free(&error);
3078 static int delete_snapshot(DBusConnection *bus, char **args) {
3079 DBusMessage *m = NULL, *reply = NULL;
3087 dbus_error_init(&error);
3089 STRV_FOREACH(name, args+1) {
3090 const char *path = NULL;
3092 if (!(m = dbus_message_new_method_call(
3093 "org.freedesktop.systemd1",
3094 "/org/freedesktop/systemd1",
3095 "org.freedesktop.systemd1.Manager",
3097 log_error("Could not allocate message.");
3102 if (!dbus_message_append_args(m,
3103 DBUS_TYPE_STRING, name,
3104 DBUS_TYPE_INVALID)) {
3105 log_error("Could not append arguments to message.");
3110 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3111 log_error("Failed to issue method call: %s", bus_error_message(&error));
3116 if (!dbus_message_get_args(reply, &error,
3117 DBUS_TYPE_OBJECT_PATH, &path,
3118 DBUS_TYPE_INVALID)) {
3119 log_error("Failed to parse reply: %s", bus_error_message(&error));
3124 dbus_message_unref(m);
3125 if (!(m = dbus_message_new_method_call(
3126 "org.freedesktop.systemd1",
3128 "org.freedesktop.systemd1.Snapshot",
3130 log_error("Could not allocate message.");
3135 dbus_message_unref(reply);
3136 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3137 log_error("Failed to issue method call: %s", bus_error_message(&error));
3142 dbus_message_unref(m);
3143 dbus_message_unref(reply);
3151 dbus_message_unref(m);
3154 dbus_message_unref(reply);
3156 dbus_error_free(&error);
3161 static int daemon_reload(DBusConnection *bus, char **args) {
3162 DBusMessage *m = NULL, *reply = NULL;
3167 dbus_error_init(&error);
3169 if (arg_action == ACTION_RELOAD)
3171 else if (arg_action == ACTION_REEXEC)
3172 method = "Reexecute";
3174 assert(arg_action == ACTION_SYSTEMCTL);
3177 streq(args[0], "clear-jobs") ||
3178 streq(args[0], "cancel") ? "ClearJobs" :
3179 streq(args[0], "daemon-reexec") ? "Reexecute" :
3180 streq(args[0], "reset-failed") ? "ResetFailed" :
3181 streq(args[0], "halt") ? "Halt" :
3182 streq(args[0], "poweroff") ? "PowerOff" :
3183 streq(args[0], "reboot") ? "Reboot" :
3184 streq(args[0], "kexec") ? "KExec" :
3185 streq(args[0], "exit") ? "Exit" :
3186 /* "daemon-reload" */ "Reload";
3189 if (!(m = dbus_message_new_method_call(
3190 "org.freedesktop.systemd1",
3191 "/org/freedesktop/systemd1",
3192 "org.freedesktop.systemd1.Manager",
3194 log_error("Could not allocate message.");
3198 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3200 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
3201 /* There's always a fallback possible for
3202 * legacy actions. */
3207 if (streq(method, "Reexecute") && dbus_error_has_name(&error, DBUS_ERROR_NO_REPLY)) {
3208 /* On reexecution, we expect a disconnect, not
3214 log_error("Failed to issue method call: %s", bus_error_message(&error));
3223 dbus_message_unref(m);
3226 dbus_message_unref(reply);
3228 dbus_error_free(&error);
3233 static int reset_failed(DBusConnection *bus, char **args) {
3234 DBusMessage *m = NULL, *reply = NULL;
3240 dbus_error_init(&error);
3242 if (strv_length(args) <= 1)
3243 return daemon_reload(bus, args);
3245 STRV_FOREACH(name, args+1) {
3247 if (!(m = dbus_message_new_method_call(
3248 "org.freedesktop.systemd1",
3249 "/org/freedesktop/systemd1",
3250 "org.freedesktop.systemd1.Manager",
3251 "ResetFailedUnit"))) {
3252 log_error("Could not allocate message.");
3257 if (!dbus_message_append_args(m,
3258 DBUS_TYPE_STRING, name,
3259 DBUS_TYPE_INVALID)) {
3260 log_error("Could not append arguments to message.");
3265 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3266 log_error("Failed to issue method call: %s", bus_error_message(&error));
3271 dbus_message_unref(m);
3272 dbus_message_unref(reply);
3280 dbus_message_unref(m);
3283 dbus_message_unref(reply);
3285 dbus_error_free(&error);
3290 static int show_enviroment(DBusConnection *bus, char **args) {
3291 DBusMessage *m = NULL, *reply = NULL;
3293 DBusMessageIter iter, sub, sub2;
3296 *interface = "org.freedesktop.systemd1.Manager",
3297 *property = "Environment";
3299 dbus_error_init(&error);
3301 pager_open_if_enabled();
3303 if (!(m = dbus_message_new_method_call(
3304 "org.freedesktop.systemd1",
3305 "/org/freedesktop/systemd1",
3306 "org.freedesktop.DBus.Properties",
3308 log_error("Could not allocate message.");
3312 if (!dbus_message_append_args(m,
3313 DBUS_TYPE_STRING, &interface,
3314 DBUS_TYPE_STRING, &property,
3315 DBUS_TYPE_INVALID)) {
3316 log_error("Could not append arguments to message.");
3321 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3322 log_error("Failed to issue method call: %s", bus_error_message(&error));
3327 if (!dbus_message_iter_init(reply, &iter) ||
3328 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
3329 log_error("Failed to parse reply.");
3334 dbus_message_iter_recurse(&iter, &sub);
3336 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
3337 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
3338 log_error("Failed to parse reply.");
3343 dbus_message_iter_recurse(&sub, &sub2);
3345 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
3348 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
3349 log_error("Failed to parse reply.");
3354 dbus_message_iter_get_basic(&sub2, &text);
3355 printf("%s\n", text);
3357 dbus_message_iter_next(&sub2);
3364 dbus_message_unref(m);
3367 dbus_message_unref(reply);
3369 dbus_error_free(&error);
3374 static int set_environment(DBusConnection *bus, char **args) {
3375 DBusMessage *m = NULL, *reply = NULL;
3379 DBusMessageIter iter, sub;
3382 dbus_error_init(&error);
3384 method = streq(args[0], "set-environment")
3386 : "UnsetEnvironment";
3388 if (!(m = dbus_message_new_method_call(
3389 "org.freedesktop.systemd1",
3390 "/org/freedesktop/systemd1",
3391 "org.freedesktop.systemd1.Manager",
3394 log_error("Could not allocate message.");
3398 dbus_message_iter_init_append(m, &iter);
3400 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
3401 log_error("Could not append arguments to message.");
3406 STRV_FOREACH(name, args+1)
3407 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, name)) {
3408 log_error("Could not append arguments to message.");
3413 if (!dbus_message_iter_close_container(&iter, &sub)) {
3414 log_error("Could not append arguments to message.");
3419 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3420 log_error("Failed to issue method call: %s", bus_error_message(&error));
3429 dbus_message_unref(m);
3432 dbus_message_unref(reply);
3434 dbus_error_free(&error);
3439 static int enable_sysv_units(char **args) {
3442 #if defined (HAVE_SYSV_COMPAT) && (defined(TARGET_FEDORA) || defined(TARGET_MANDRIVA) || defined(TARGET_SUSE) || defined(TARGET_MEEGO) || defined(TARGET_ALTLINUX))
3443 const char *verb = args[0];
3444 unsigned f = 1, t = 1;
3447 if (arg_scope != UNIT_FILE_SYSTEM)
3450 if (!streq(verb, "enable") &&
3451 !streq(verb, "disable") &&
3452 !streq(verb, "is-enabled"))
3455 /* Processes all SysV units, and reshuffles the array so that
3456 * afterwards only the native units remain */
3459 r = lookup_paths_init(&paths, MANAGER_SYSTEM, false);
3465 for (f = 1; args[f]; f++) {
3468 bool found_native = false, found_sysv;
3470 const char *argv[6] = { "/sbin/chkconfig", NULL, NULL, NULL, NULL };
3471 char **k, *l, *q = NULL;
3478 if (!endswith(name, ".service"))
3481 if (path_is_absolute(name))
3484 STRV_FOREACH(k, paths.unit_path) {
3487 if (!isempty(arg_root))
3488 asprintf(&p, "%s/%s/%s", arg_root, *k, name);
3490 asprintf(&p, "%s/%s", *k, name);
3493 log_error("No memory");
3498 found_native = access(p, F_OK) >= 0;
3509 if (!isempty(arg_root))
3510 asprintf(&p, "%s/" SYSTEM_SYSVINIT_PATH "/%s", arg_root, name);
3512 asprintf(&p, SYSTEM_SYSVINIT_PATH "/%s", name);
3514 log_error("No memory");
3519 p[strlen(p) - sizeof(".service") + 1] = 0;
3520 found_sysv = access(p, F_OK) >= 0;
3527 /* Mark this entry, so that we don't try enabling it as native unit */
3528 args[f] = (char*) "";
3530 log_info("%s is not a native service, redirecting to /sbin/chkconfig.", name);
3532 if (!isempty(arg_root))
3533 argv[c++] = q = strappend("--root=", arg_root);
3535 argv[c++] = file_name_from_path(p);
3537 streq(verb, "enable") ? "on" :
3538 streq(verb, "disable") ? "off" : "--level=5";
3541 l = strv_join((char**)argv, " ");
3543 log_error("No memory.");
3550 log_info("Executing %s", l);
3555 log_error("Failed to fork: %m");
3560 } else if (pid == 0) {
3563 execv(argv[0], (char**) argv);
3564 _exit(EXIT_FAILURE);
3570 j = wait_for_terminate(pid, &status);
3572 log_error("Failed to wait for child: %s", strerror(-r));
3577 if (status.si_code == CLD_EXITED) {
3578 if (streq(verb, "is-enabled")) {
3579 if (status.si_status == 0) {
3588 } else if (status.si_status != 0) {
3599 lookup_paths_free(&paths);
3601 /* Drop all SysV units */
3602 for (f = 1, t = 1; args[f]; f++) {
3604 if (isempty(args[f]))
3607 args[t++] = args[f];
3616 static int enable_unit(DBusConnection *bus, char **args) {
3617 const char *verb = args[0];
3618 UnitFileChange *changes = NULL;
3619 unsigned n_changes = 0, i;
3620 int carries_install_info = -1;
3621 DBusMessage *m = NULL, *reply = NULL;
3625 dbus_error_init(&error);
3627 r = enable_sysv_units(args);
3631 if (!bus || avoid_bus()) {
3632 if (streq(verb, "enable")) {
3633 r = unit_file_enable(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3634 carries_install_info = r;
3635 } else if (streq(verb, "disable"))
3636 r = unit_file_disable(arg_scope, arg_runtime, arg_root, args+1, &changes, &n_changes);
3637 else if (streq(verb, "reenable")) {
3638 r = unit_file_reenable(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3639 carries_install_info = r;
3640 } else if (streq(verb, "link"))
3641 r = unit_file_link(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3642 else if (streq(verb, "preset")) {
3643 r = unit_file_preset(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3644 carries_install_info = r;
3645 } else if (streq(verb, "mask"))
3646 r = unit_file_mask(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3647 else if (streq(verb, "unmask"))
3648 r = unit_file_unmask(arg_scope, arg_runtime, arg_root, args+1, &changes, &n_changes);
3650 assert_not_reached("Unknown verb");
3653 log_error("Operation failed: %s", strerror(-r));
3657 for (i = 0; i < n_changes; i++) {
3658 if (changes[i].type == UNIT_FILE_SYMLINK)
3659 log_info("ln -s '%s' '%s'", changes[i].source, changes[i].path);
3661 log_info("rm '%s'", changes[i].path);
3666 bool send_force = true, expect_carries_install_info = false;
3668 DBusMessageIter iter, sub, sub2;
3670 if (streq(verb, "enable")) {
3671 method = "EnableUnitFiles";
3672 expect_carries_install_info = true;
3673 } else if (streq(verb, "disable")) {
3674 method = "DisableUnitFiles";
3676 } else if (streq(verb, "reenable")) {
3677 method = "ReenableUnitFiles";
3678 expect_carries_install_info = true;
3679 } else if (streq(verb, "link"))
3680 method = "LinkUnitFiles";
3681 else if (streq(verb, "preset")) {
3682 method = "PresetUnitFiles";
3683 expect_carries_install_info = true;
3684 } else if (streq(verb, "mask"))
3685 method = "MaskUnitFiles";
3686 else if (streq(verb, "unmask")) {
3687 method = "UnmaskUnitFiles";
3690 assert_not_reached("Unknown verb");
3692 m = dbus_message_new_method_call(
3693 "org.freedesktop.systemd1",
3694 "/org/freedesktop/systemd1",
3695 "org.freedesktop.systemd1.Manager",
3698 log_error("Out of memory");
3703 dbus_message_iter_init_append(m, &iter);
3705 r = bus_append_strv_iter(&iter, args+1);
3707 log_error("Failed to append unit files.");
3712 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &a)) {
3713 log_error("Failed to append runtime boolean.");
3721 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b)) {
3722 log_error("Failed to append force boolean.");
3728 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
3730 log_error("Failed to issue method call: %s", bus_error_message(&error));
3735 if (!dbus_message_iter_init(reply, &iter)) {
3736 log_error("Failed to initialize iterator.");
3740 if (expect_carries_install_info) {
3741 r = bus_iter_get_basic_and_next(&iter, DBUS_TYPE_BOOLEAN, &b, true);
3743 log_error("Failed to parse reply.");
3747 carries_install_info = b;
3750 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
3751 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
3752 log_error("Failed to parse reply.");
3757 dbus_message_iter_recurse(&iter, &sub);
3758 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
3759 const char *type, *path, *source;
3761 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
3762 log_error("Failed to parse reply.");
3767 dbus_message_iter_recurse(&sub, &sub2);
3769 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
3770 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0 ||
3771 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &source, false) < 0) {
3772 log_error("Failed to parse reply.");
3777 if (streq(type, "symlink"))
3778 log_info("ln -s '%s' '%s'", source, path);
3780 log_info("rm '%s'", path);
3782 dbus_message_iter_next(&sub);
3785 /* Try to reload if enabeld */
3787 r = daemon_reload(bus, args);
3790 if (carries_install_info == 0)
3791 log_warning("Warning: unit files do not carry install information. No operation executed.");
3795 dbus_message_unref(m);
3798 dbus_message_unref(reply);
3800 unit_file_changes_free(changes, n_changes);
3802 dbus_error_free(&error);
3806 static int unit_is_enabled(DBusConnection *bus, char **args) {
3809 DBusMessage *m = NULL, *reply = NULL;
3813 dbus_error_init(&error);
3815 r = enable_sysv_units(args);
3821 if (!bus || avoid_bus()) {
3823 STRV_FOREACH(name, args+1) {
3824 UnitFileState state;
3826 state = unit_file_get_state(arg_scope, arg_root, *name);
3832 if (state == UNIT_FILE_ENABLED ||
3833 state == UNIT_FILE_ENABLED_RUNTIME ||
3834 state == UNIT_FILE_STATIC)
3838 puts(unit_file_state_to_string(state));
3842 STRV_FOREACH(name, args+1) {
3845 m = dbus_message_new_method_call(
3846 "org.freedesktop.systemd1",
3847 "/org/freedesktop/systemd1",
3848 "org.freedesktop.systemd1.Manager",
3849 "GetUnitFileState");
3851 log_error("Out of memory");
3856 if (!dbus_message_append_args(m,
3857 DBUS_TYPE_STRING, name,
3858 DBUS_TYPE_INVALID)) {
3859 log_error("Could not append arguments to message.");
3864 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
3866 log_error("Failed to issue method call: %s", bus_error_message(&error));
3871 if (!dbus_message_get_args(reply, &error,
3872 DBUS_TYPE_STRING, &s,
3873 DBUS_TYPE_INVALID)) {
3874 log_error("Failed to parse reply: %s", bus_error_message(&error));
3879 dbus_message_unref(m);
3880 dbus_message_unref(reply);
3883 if (streq(s, "enabled") ||
3884 streq(s, "enabled-runtime") ||
3893 r = enabled ? 0 : 1;
3897 dbus_message_unref(m);
3900 dbus_message_unref(reply);
3902 dbus_error_free(&error);
3906 static int systemctl_help(void) {
3908 pager_open_if_enabled();
3910 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
3911 "Query or send control commands to the systemd manager.\n\n"
3912 " -h --help Show this help\n"
3913 " --version Show package version\n"
3914 " -t --type=TYPE List only units of a particular type\n"
3915 " -p --property=NAME Show only properties by this name\n"
3916 " -a --all Show all units/properties, including dead/empty ones\n"
3917 " --failed Show only failed units\n"
3918 " --full Don't ellipsize unit names on output\n"
3919 " --fail When queueing a new job, fail if conflicting jobs are\n"
3921 " --ignore-dependencies\n"
3922 " When queueing a new job, ignore all its dependencies\n"
3923 " --kill-who=WHO Who to send signal to\n"
3924 " -s --signal=SIGNAL Which signal to send\n"
3925 " -H --host=[USER@]HOST\n"
3926 " Show information for remote host\n"
3927 " -P --privileged Acquire privileges before execution\n"
3928 " -q --quiet Suppress output\n"
3929 " --no-block Do not wait until operation finished\n"
3930 " --no-wall Don't send wall message before halt/power-off/reboot\n"
3931 " --no-reload When enabling/disabling unit files, don't reload daemon\n"
3933 " --no-pager Do not pipe output into a pager\n"
3934 " --no-ask-password\n"
3935 " Do not ask for system passwords\n"
3936 " --order When generating graph for dot, show only order\n"
3937 " --require When generating graph for dot, show only requirement\n"
3938 " --system Connect to system manager\n"
3939 " --user Connect to user service manager\n"
3940 " --global Enable/disable unit files globally\n"
3941 " -f --force When enabling unit files, override existing symlinks\n"
3942 " When shutting down, execute action immediately\n"
3943 " --root=PATH Enable unit files in the specified root directory\n"
3944 " --runtime Enable unit files only temporarily until next reboot\n\n"
3946 " list-units List loaded units\n"
3947 " start [NAME...] Start (activate) one or more units\n"
3948 " stop [NAME...] Stop (deactivate) one or more units\n"
3949 " reload [NAME...] Reload one or more units\n"
3950 " restart [NAME...] Start or restart one or more units\n"
3951 " try-restart [NAME...] Restart one or more units if active\n"
3952 " reload-or-restart [NAME...] Reload one or more units is possible,\n"
3953 " otherwise start or restart\n"
3954 " reload-or-try-restart [NAME...] Reload one or more units is possible,\n"
3955 " otherwise restart if active\n"
3956 " isolate [NAME] Start one unit and stop all others\n"
3957 " kill [NAME...] Send signal to processes of a unit\n"
3958 " is-active [NAME...] Check whether units are active\n"
3959 " status [NAME...|PID...] Show runtime status of one or more units\n"
3960 " show [NAME...|JOB...] Show properties of one or more\n"
3961 " units/jobs or the manager\n"
3962 " reset-failed [NAME...] Reset failed state for all, one, or more\n"
3964 " load [NAME...] Load one or more units\n\n"
3965 "Unit File Commands:\n"
3966 " list-unit-files List installed unit files\n"
3967 " enable [NAME...] Enable one or more unit files\n"
3968 " disable [NAME...] Disable one or more unit files\n"
3969 " reenable [NAME...] Reenable one or more unit files\n"
3970 " preset [NAME...] Enable/disable one or more unit files\n"
3971 " based on preset configuration\n"
3972 " mask [NAME...] Mask one or more units\n"
3973 " unmask [NAME...] Unmask one or more units\n"
3974 " link [PATH...] Link one or more units files into\n"
3975 " the search path\n"
3976 " is-enabled [NAME...] Check whether unit files are enabled\n\n"
3978 " list-jobs List jobs\n"
3979 " cancel [JOB...] Cancel all, one, or more jobs\n\n"
3980 "Status Commands:\n"
3981 " dump Dump server status\n"
3982 " dot Dump dependency graph for dot(1)\n\n"
3983 "Snapshot Commands:\n"
3984 " snapshot [NAME] Create a snapshot\n"
3985 " delete [NAME...] Remove one or more snapshots\n\n"
3986 "Environment Commands:\n"
3987 " show-environment Dump environment\n"
3988 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
3989 " unset-environment [NAME...] Unset one or more environment variables\n\n"
3990 "Manager Lifecycle Commands:\n"
3991 " daemon-reload Reload systemd manager configuration\n"
3992 " daemon-reexec Reexecute systemd manager\n\n"
3993 "System Commands:\n"
3994 " default Enter system default mode\n"
3995 " rescue Enter system rescue mode\n"
3996 " emergency Enter system emergency mode\n"
3997 " halt Shut down and halt the system\n"
3998 " poweroff Shut down and power-off the system\n"
3999 " reboot Shut down and reboot the system\n"
4000 " kexec Shut down and reboot the system with kexec\n"
4001 " exit Ask for user instance termination\n",
4002 program_invocation_short_name);
4007 static int halt_help(void) {
4009 printf("%s [OPTIONS...]\n\n"
4010 "%s the system.\n\n"
4011 " --help Show this help\n"
4012 " --halt Halt the machine\n"
4013 " -p --poweroff Switch off the machine\n"
4014 " --reboot Reboot the machine\n"
4015 " -f --force Force immediate halt/power-off/reboot\n"
4016 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
4017 " -d --no-wtmp Don't write wtmp record\n"
4018 " -n --no-sync Don't sync before halt/power-off/reboot\n"
4019 " --no-wall Don't send wall message before halt/power-off/reboot\n",
4020 program_invocation_short_name,
4021 arg_action == ACTION_REBOOT ? "Reboot" :
4022 arg_action == ACTION_POWEROFF ? "Power off" :
4028 static int shutdown_help(void) {
4030 printf("%s [OPTIONS...] [TIME] [WALL...]\n\n"
4031 "Shut down the system.\n\n"
4032 " --help Show this help\n"
4033 " -H --halt Halt the machine\n"
4034 " -P --poweroff Power-off the machine\n"
4035 " -r --reboot Reboot the machine\n"
4036 " -h Equivalent to --poweroff, overriden by --halt\n"
4037 " -k Don't halt/power-off/reboot, just send warnings\n"
4038 " --no-wall Don't send wall message before halt/power-off/reboot\n"
4039 " -c Cancel a pending shutdown\n",
4040 program_invocation_short_name);
4045 static int telinit_help(void) {
4047 printf("%s [OPTIONS...] {COMMAND}\n\n"
4048 "Send control commands to the init daemon.\n\n"
4049 " --help Show this help\n"
4050 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
4052 " 0 Power-off the machine\n"
4053 " 6 Reboot the machine\n"
4054 " 2, 3, 4, 5 Start runlevelX.target unit\n"
4055 " 1, s, S Enter rescue mode\n"
4056 " q, Q Reload init daemon configuration\n"
4057 " u, U Reexecute init daemon\n",
4058 program_invocation_short_name);
4063 static int runlevel_help(void) {
4065 printf("%s [OPTIONS...]\n\n"
4066 "Prints the previous and current runlevel of the init system.\n\n"
4067 " --help Show this help\n",
4068 program_invocation_short_name);
4073 static int systemctl_parse_argv(int argc, char *argv[]) {
4077 ARG_IGNORE_DEPENDENCIES,
4092 ARG_NO_ASK_PASSWORD,
4097 static const struct option options[] = {
4098 { "help", no_argument, NULL, 'h' },
4099 { "version", no_argument, NULL, ARG_VERSION },
4100 { "type", required_argument, NULL, 't' },
4101 { "property", required_argument, NULL, 'p' },
4102 { "all", no_argument, NULL, 'a' },
4103 { "failed", no_argument, NULL, ARG_FAILED },
4104 { "full", no_argument, NULL, ARG_FULL },
4105 { "fail", no_argument, NULL, ARG_FAIL },
4106 { "ignore-dependencies", no_argument, NULL, ARG_IGNORE_DEPENDENCIES },
4107 { "user", no_argument, NULL, ARG_USER },
4108 { "system", no_argument, NULL, ARG_SYSTEM },
4109 { "global", no_argument, NULL, ARG_GLOBAL },
4110 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
4111 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
4112 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4113 { "quiet", no_argument, NULL, 'q' },
4114 { "order", no_argument, NULL, ARG_ORDER },
4115 { "require", no_argument, NULL, ARG_REQUIRE },
4116 { "root", required_argument, NULL, ARG_ROOT },
4117 { "force", no_argument, NULL, 'f' },
4118 { "no-reload", no_argument, NULL, ARG_NO_RELOAD },
4119 { "kill-mode", required_argument, NULL, ARG_KILL_MODE }, /* undocumented on purpose */
4120 { "kill-who", required_argument, NULL, ARG_KILL_WHO },
4121 { "signal", required_argument, NULL, 's' },
4122 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
4123 { "host", required_argument, NULL, 'H' },
4124 { "privileged",no_argument, NULL, 'P' },
4125 { "runtime", no_argument, NULL, ARG_RUNTIME },
4126 { NULL, 0, NULL, 0 }
4134 /* Only when running as systemctl we ask for passwords */
4135 arg_ask_password = true;
4137 while ((c = getopt_long(argc, argv, "ht:p:aqfs:H:P", options, NULL)) >= 0) {
4146 puts(PACKAGE_STRING);
4148 puts(SYSTEMD_FEATURES);
4158 if (!(l = strv_append(arg_property, optarg)))
4161 strv_free(arg_property);
4164 /* If the user asked for a particular
4165 * property, show it to him, even if it is
4176 arg_job_mode = "fail";
4179 case ARG_IGNORE_DEPENDENCIES:
4180 arg_job_mode = "ignore-dependencies";
4184 arg_scope = UNIT_FILE_USER;
4188 arg_scope = UNIT_FILE_SYSTEM;
4192 arg_scope = UNIT_FILE_GLOBAL;
4196 arg_no_block = true;
4200 arg_no_pager = true;
4208 arg_dot = DOT_ORDER;
4212 arg_dot = DOT_REQUIRE;
4236 arg_no_reload = true;
4240 arg_kill_who = optarg;
4244 arg_kill_mode = optarg;
4248 if ((arg_signal = signal_from_string_try_harder(optarg)) < 0) {
4249 log_error("Failed to parse signal string %s.", optarg);
4254 case ARG_NO_ASK_PASSWORD:
4255 arg_ask_password = false;
4259 arg_transport = TRANSPORT_POLKIT;
4263 arg_transport = TRANSPORT_SSH;
4275 log_error("Unknown option code %c", c);
4280 if (arg_transport != TRANSPORT_NORMAL && arg_scope != UNIT_FILE_SYSTEM) {
4281 log_error("Cannot access user instance remotely.");
4288 static int halt_parse_argv(int argc, char *argv[]) {
4297 static const struct option options[] = {
4298 { "help", no_argument, NULL, ARG_HELP },
4299 { "halt", no_argument, NULL, ARG_HALT },
4300 { "poweroff", no_argument, NULL, 'p' },
4301 { "reboot", no_argument, NULL, ARG_REBOOT },
4302 { "force", no_argument, NULL, 'f' },
4303 { "wtmp-only", no_argument, NULL, 'w' },
4304 { "no-wtmp", no_argument, NULL, 'd' },
4305 { "no-sync", no_argument, NULL, 'n' },
4306 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4307 { NULL, 0, NULL, 0 }
4315 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
4316 if (runlevel == '0' || runlevel == '6')
4317 arg_immediate = true;
4319 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
4327 arg_action = ACTION_HALT;
4331 if (arg_action != ACTION_REBOOT)
4332 arg_action = ACTION_POWEROFF;
4336 arg_action = ACTION_REBOOT;
4340 arg_immediate = true;
4361 /* Compatibility nops */
4368 log_error("Unknown option code %c", c);
4373 if (optind < argc) {
4374 log_error("Too many arguments.");
4381 static int parse_time_spec(const char *t, usec_t *_u) {
4385 if (streq(t, "now"))
4387 else if (!strchr(t, ':')) {
4390 if (safe_atou64(t, &u) < 0)
4393 *_u = now(CLOCK_REALTIME) + USEC_PER_MINUTE * u;
4402 hour = strtol(t, &e, 10);
4403 if (errno != 0 || *e != ':' || hour < 0 || hour > 23)
4406 minute = strtol(e+1, &e, 10);
4407 if (errno != 0 || *e != 0 || minute < 0 || minute > 59)
4410 n = now(CLOCK_REALTIME);
4411 s = (time_t) (n / USEC_PER_SEC);
4414 assert_se(localtime_r(&s, &tm));
4416 tm.tm_hour = (int) hour;
4417 tm.tm_min = (int) minute;
4420 assert_se(s = mktime(&tm));
4422 *_u = (usec_t) s * USEC_PER_SEC;
4425 *_u += USEC_PER_DAY;
4431 static int shutdown_parse_argv(int argc, char *argv[]) {
4438 static const struct option options[] = {
4439 { "help", no_argument, NULL, ARG_HELP },
4440 { "halt", no_argument, NULL, 'H' },
4441 { "poweroff", no_argument, NULL, 'P' },
4442 { "reboot", no_argument, NULL, 'r' },
4443 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4444 { NULL, 0, NULL, 0 }
4452 while ((c = getopt_long(argc, argv, "HPrhkt:afFc", options, NULL)) >= 0) {
4460 arg_action = ACTION_HALT;
4464 arg_action = ACTION_POWEROFF;
4469 arg_action = ACTION_KEXEC;
4471 arg_action = ACTION_REBOOT;
4475 if (arg_action != ACTION_HALT)
4476 arg_action = ACTION_POWEROFF;
4489 /* Compatibility nops */
4493 arg_action = ACTION_CANCEL_SHUTDOWN;
4500 log_error("Unknown option code %c", c);
4505 if (argc > optind) {
4506 if ((r = parse_time_spec(argv[optind], &arg_when)) < 0) {
4507 log_error("Failed to parse time specification: %s", argv[optind]);
4511 arg_when = now(CLOCK_REALTIME) + USEC_PER_MINUTE;
4513 /* We skip the time argument */
4514 if (argc > optind + 1)
4515 arg_wall = argv + optind + 1;
4522 static int telinit_parse_argv(int argc, char *argv[]) {
4529 static const struct option options[] = {
4530 { "help", no_argument, NULL, ARG_HELP },
4531 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4532 { NULL, 0, NULL, 0 }
4535 static const struct {
4539 { '0', ACTION_POWEROFF },
4540 { '6', ACTION_REBOOT },
4541 { '1', ACTION_RESCUE },
4542 { '2', ACTION_RUNLEVEL2 },
4543 { '3', ACTION_RUNLEVEL3 },
4544 { '4', ACTION_RUNLEVEL4 },
4545 { '5', ACTION_RUNLEVEL5 },
4546 { 's', ACTION_RESCUE },
4547 { 'S', ACTION_RESCUE },
4548 { 'q', ACTION_RELOAD },
4549 { 'Q', ACTION_RELOAD },
4550 { 'u', ACTION_REEXEC },
4551 { 'U', ACTION_REEXEC }
4560 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4575 log_error("Unknown option code %c", c);
4580 if (optind >= argc) {
4585 if (optind + 1 < argc) {
4586 log_error("Too many arguments.");
4590 if (strlen(argv[optind]) != 1) {
4591 log_error("Expected single character argument.");
4595 for (i = 0; i < ELEMENTSOF(table); i++)
4596 if (table[i].from == argv[optind][0])
4599 if (i >= ELEMENTSOF(table)) {
4600 log_error("Unknown command %s.", argv[optind]);
4604 arg_action = table[i].to;
4611 static int runlevel_parse_argv(int argc, char *argv[]) {
4617 static const struct option options[] = {
4618 { "help", no_argument, NULL, ARG_HELP },
4619 { NULL, 0, NULL, 0 }
4627 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4638 log_error("Unknown option code %c", c);
4643 if (optind < argc) {
4644 log_error("Too many arguments.");
4651 static int parse_argv(int argc, char *argv[]) {
4655 if (program_invocation_short_name) {
4657 if (strstr(program_invocation_short_name, "halt")) {
4658 arg_action = ACTION_HALT;
4659 return halt_parse_argv(argc, argv);
4660 } else if (strstr(program_invocation_short_name, "poweroff")) {
4661 arg_action = ACTION_POWEROFF;
4662 return halt_parse_argv(argc, argv);
4663 } else if (strstr(program_invocation_short_name, "reboot")) {
4665 arg_action = ACTION_KEXEC;
4667 arg_action = ACTION_REBOOT;
4668 return halt_parse_argv(argc, argv);
4669 } else if (strstr(program_invocation_short_name, "shutdown")) {
4670 arg_action = ACTION_POWEROFF;
4671 return shutdown_parse_argv(argc, argv);
4672 } else if (strstr(program_invocation_short_name, "init")) {
4674 if (sd_booted() > 0) {
4675 arg_action = ACTION_INVALID;
4676 return telinit_parse_argv(argc, argv);
4678 /* Hmm, so some other init system is
4679 * running, we need to forward this
4680 * request to it. For now we simply
4681 * guess that it is Upstart. */
4683 execv("/lib/upstart/telinit", argv);
4685 log_error("Couldn't find an alternative telinit implementation to spawn.");
4689 } else if (strstr(program_invocation_short_name, "runlevel")) {
4690 arg_action = ACTION_RUNLEVEL;
4691 return runlevel_parse_argv(argc, argv);
4695 arg_action = ACTION_SYSTEMCTL;
4696 return systemctl_parse_argv(argc, argv);
4699 static int action_to_runlevel(void) {
4701 static const char table[_ACTION_MAX] = {
4702 [ACTION_HALT] = '0',
4703 [ACTION_POWEROFF] = '0',
4704 [ACTION_REBOOT] = '6',
4705 [ACTION_RUNLEVEL2] = '2',
4706 [ACTION_RUNLEVEL3] = '3',
4707 [ACTION_RUNLEVEL4] = '4',
4708 [ACTION_RUNLEVEL5] = '5',
4709 [ACTION_RESCUE] = '1'
4712 assert(arg_action < _ACTION_MAX);
4714 return table[arg_action];
4717 static int talk_upstart(void) {
4718 DBusMessage *m = NULL, *reply = NULL;
4720 int previous, rl, r;
4722 env1_buf[] = "RUNLEVEL=X",
4723 env2_buf[] = "PREVLEVEL=X";
4724 char *env1 = env1_buf, *env2 = env2_buf;
4725 const char *emit = "runlevel";
4726 dbus_bool_t b_false = FALSE;
4727 DBusMessageIter iter, sub;
4728 DBusConnection *bus;
4730 dbus_error_init(&error);
4732 if (!(rl = action_to_runlevel()))
4735 if (utmp_get_runlevel(&previous, NULL) < 0)
4738 if (!(bus = dbus_connection_open_private("unix:abstract=/com/ubuntu/upstart", &error))) {
4739 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
4744 log_error("Failed to connect to Upstart bus: %s", bus_error_message(&error));
4749 if ((r = bus_check_peercred(bus)) < 0) {
4750 log_error("Failed to verify owner of bus.");
4754 if (!(m = dbus_message_new_method_call(
4755 "com.ubuntu.Upstart",
4756 "/com/ubuntu/Upstart",
4757 "com.ubuntu.Upstart0_6",
4760 log_error("Could not allocate message.");
4765 dbus_message_iter_init_append(m, &iter);
4767 env1_buf[sizeof(env1_buf)-2] = rl;
4768 env2_buf[sizeof(env2_buf)-2] = previous;
4770 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
4771 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
4772 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
4773 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
4774 !dbus_message_iter_close_container(&iter, &sub) ||
4775 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
4776 log_error("Could not append arguments to message.");
4781 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
4783 if (error_is_no_service(&error)) {
4788 log_error("Failed to issue method call: %s", bus_error_message(&error));
4797 dbus_message_unref(m);
4800 dbus_message_unref(reply);
4803 dbus_connection_flush(bus);
4804 dbus_connection_close(bus);
4805 dbus_connection_unref(bus);
4808 dbus_error_free(&error);
4813 static int talk_initctl(void) {
4814 struct init_request request;
4818 if (!(rl = action_to_runlevel()))
4822 request.magic = INIT_MAGIC;
4823 request.sleeptime = 0;
4824 request.cmd = INIT_CMD_RUNLVL;
4825 request.runlevel = rl;
4827 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
4829 if (errno == ENOENT)
4832 log_error("Failed to open "INIT_FIFO": %m");
4837 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
4838 close_nointr_nofail(fd);
4841 log_error("Failed to write to "INIT_FIFO": %m");
4842 return errno ? -errno : -EIO;
4848 static int systemctl_main(DBusConnection *bus, int argc, char *argv[], DBusError *error) {
4850 static const struct {
4858 int (* const dispatch)(DBusConnection *bus, char **args);
4860 { "list-units", LESS, 1, list_units },
4861 { "list-unit-files", EQUAL, 1, list_unit_files },
4862 { "list-jobs", EQUAL, 1, list_jobs },
4863 { "clear-jobs", EQUAL, 1, daemon_reload },
4864 { "load", MORE, 2, load_unit },
4865 { "cancel", MORE, 2, cancel_job },
4866 { "start", MORE, 2, start_unit },
4867 { "stop", MORE, 2, start_unit },
4868 { "condstop", MORE, 2, start_unit }, /* For compatibility with ALTLinux */
4869 { "reload", MORE, 2, start_unit },
4870 { "restart", MORE, 2, start_unit },
4871 { "try-restart", MORE, 2, start_unit },
4872 { "reload-or-restart", MORE, 2, start_unit },
4873 { "reload-or-try-restart", MORE, 2, start_unit },
4874 { "force-reload", MORE, 2, start_unit }, /* For compatibility with SysV */
4875 { "condreload", MORE, 2, start_unit }, /* For compatibility with ALTLinux */
4876 { "condrestart", MORE, 2, start_unit }, /* For compatibility with RH */
4877 { "isolate", EQUAL, 2, start_unit },
4878 { "kill", MORE, 2, kill_unit },
4879 { "is-active", MORE, 2, check_unit },
4880 { "check", MORE, 2, check_unit },
4881 { "show", MORE, 1, show },
4882 { "status", MORE, 2, show },
4883 { "dump", EQUAL, 1, dump },
4884 { "dot", EQUAL, 1, dot },
4885 { "snapshot", LESS, 2, snapshot },
4886 { "delete", MORE, 2, delete_snapshot },
4887 { "daemon-reload", EQUAL, 1, daemon_reload },
4888 { "daemon-reexec", EQUAL, 1, daemon_reload },
4889 { "show-environment", EQUAL, 1, show_enviroment },
4890 { "set-environment", MORE, 2, set_environment },
4891 { "unset-environment", MORE, 2, set_environment },
4892 { "halt", EQUAL, 1, start_special },
4893 { "poweroff", EQUAL, 1, start_special },
4894 { "reboot", EQUAL, 1, start_special },
4895 { "kexec", EQUAL, 1, start_special },
4896 { "default", EQUAL, 1, start_special },
4897 { "rescue", EQUAL, 1, start_special },
4898 { "emergency", EQUAL, 1, start_special },
4899 { "exit", EQUAL, 1, start_special },
4900 { "reset-failed", MORE, 1, reset_failed },
4901 { "enable", MORE, 2, enable_unit },
4902 { "disable", MORE, 2, enable_unit },
4903 { "is-enabled", MORE, 2, unit_is_enabled },
4904 { "reenable", MORE, 2, enable_unit },
4905 { "preset", MORE, 2, enable_unit },
4906 { "mask", MORE, 2, enable_unit },
4907 { "unmask", MORE, 2, enable_unit },
4908 { "link", MORE, 2, enable_unit }
4918 left = argc - optind;
4921 /* Special rule: no arguments means "list-units" */
4924 if (streq(argv[optind], "help")) {
4929 for (i = 0; i < ELEMENTSOF(verbs); i++)
4930 if (streq(argv[optind], verbs[i].verb))
4933 if (i >= ELEMENTSOF(verbs)) {
4934 log_error("Unknown operation %s", argv[optind]);
4939 switch (verbs[i].argc_cmp) {
4942 if (left != verbs[i].argc) {
4943 log_error("Invalid number of arguments.");
4950 if (left < verbs[i].argc) {
4951 log_error("Too few arguments.");
4958 if (left > verbs[i].argc) {
4959 log_error("Too many arguments.");
4966 assert_not_reached("Unknown comparison operator.");
4969 /* Require a bus connection for all operations but
4971 if (!streq(verbs[i].verb, "enable") &&
4972 !streq(verbs[i].verb, "disable") &&
4973 !streq(verbs[i].verb, "is-enable") &&
4974 !streq(verbs[i].verb, "reenable") &&
4975 !streq(verbs[i].verb, "preset") &&
4976 !streq(verbs[i].verb, "mask") &&
4977 !streq(verbs[i].verb, "unmask") &&
4978 !streq(verbs[i].verb, "link")) {
4980 if (running_in_chroot() > 0) {
4981 log_info("Running in chroot, ignoring request.");
4985 if (!bus && !avoid_bus()) {
4986 log_error("Failed to get D-Bus connection: %s", error->message);
4991 return verbs[i].dispatch(bus, argv + optind);
4994 static int send_shutdownd(usec_t t, char mode, bool dry_run, bool warn, const char *message) {
4996 struct msghdr msghdr;
4998 union sockaddr_union sockaddr;
4999 struct shutdownd_command c;
5004 c.dry_run = dry_run;
5008 strncpy(c.wall_message, message, sizeof(c.wall_message));
5010 if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0)
5014 sockaddr.sa.sa_family = AF_UNIX;
5015 sockaddr.un.sun_path[0] = 0;
5016 strncpy(sockaddr.un.sun_path, "/run/systemd/shutdownd", sizeof(sockaddr.un.sun_path));
5019 iovec.iov_base = (char*) &c;
5020 iovec.iov_len = sizeof(c);
5023 msghdr.msg_name = &sockaddr;
5024 msghdr.msg_namelen = offsetof(struct sockaddr_un, sun_path) + sizeof("/run/systemd/shutdownd") - 1;
5026 msghdr.msg_iov = &iovec;
5027 msghdr.msg_iovlen = 1;
5029 if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) < 0) {
5030 close_nointr_nofail(fd);
5034 close_nointr_nofail(fd);
5038 static int reload_with_fallback(DBusConnection *bus) {
5041 /* First, try systemd via D-Bus. */
5042 if (daemon_reload(bus, NULL) >= 0)
5046 /* Nothing else worked, so let's try signals */
5047 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
5049 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
5050 log_error("kill() failed: %m");
5057 static int start_with_fallback(DBusConnection *bus) {
5060 /* First, try systemd via D-Bus. */
5061 if (start_unit(bus, NULL) >= 0)
5065 /* Hmm, talking to systemd via D-Bus didn't work. Then
5066 * let's try to talk to Upstart via D-Bus. */
5067 if (talk_upstart() > 0)
5070 /* Nothing else worked, so let's try
5072 if (talk_initctl() > 0)
5075 log_error("Failed to talk to init daemon.");
5079 warn_wall(arg_action);
5083 static int halt_main(DBusConnection *bus) {
5086 if (geteuid() != 0) {
5087 log_error("Must be root.");
5093 char date[FORMAT_TIMESTAMP_MAX];
5095 m = strv_join(arg_wall, " ");
5096 r = send_shutdownd(arg_when,
5097 arg_action == ACTION_HALT ? 'H' :
5098 arg_action == ACTION_POWEROFF ? 'P' :
5106 log_warning("Failed to talk to shutdownd, proceeding with immediate shutdown: %s", strerror(-r));
5108 log_info("Shutdown scheduled for %s, use 'shutdown -c' to cancel.",
5109 format_timestamp(date, sizeof(date), arg_when));
5114 if (!arg_dry && !arg_immediate)
5115 return start_with_fallback(bus);
5118 if (sd_booted() > 0)
5119 log_debug("Not writing utmp record, assuming that systemd-update-utmp is used.");
5120 else if ((r = utmp_put_shutdown(0)) < 0)
5121 log_warning("Failed to write utmp record: %s", strerror(-r));
5130 /* Make sure C-A-D is handled by the kernel from this
5132 reboot(RB_ENABLE_CAD);
5134 switch (arg_action) {
5137 log_info("Halting.");
5138 reboot(RB_HALT_SYSTEM);
5141 case ACTION_POWEROFF:
5142 log_info("Powering off.");
5143 reboot(RB_POWER_OFF);
5147 log_info("Rebooting.");
5148 reboot(RB_AUTOBOOT);
5152 assert_not_reached("Unknown halt action.");
5155 /* We should never reach this. */
5159 static int runlevel_main(void) {
5160 int r, runlevel, previous;
5162 r = utmp_get_runlevel(&runlevel, &previous);
5169 previous <= 0 ? 'N' : previous,
5170 runlevel <= 0 ? 'N' : runlevel);
5175 int main(int argc, char*argv[]) {
5176 int r, retval = EXIT_FAILURE;
5177 DBusConnection *bus = NULL;
5180 dbus_error_init(&error);
5182 log_parse_environment();
5185 if ((r = parse_argv(argc, argv)) < 0)
5188 retval = EXIT_SUCCESS;
5192 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
5193 * let's shortcut this */
5194 if (arg_action == ACTION_RUNLEVEL) {
5195 r = runlevel_main();
5196 retval = r < 0 ? EXIT_FAILURE : r;
5200 if (running_in_chroot() > 0 && arg_action != ACTION_SYSTEMCTL) {
5201 log_info("Running in chroot, ignoring request.");
5207 if (arg_transport == TRANSPORT_NORMAL)
5208 bus_connect(arg_scope == UNIT_FILE_SYSTEM ? DBUS_BUS_SYSTEM : DBUS_BUS_SESSION, &bus, &private_bus, &error);
5209 else if (arg_transport == TRANSPORT_POLKIT) {
5210 bus_connect_system_polkit(&bus, &error);
5211 private_bus = false;
5212 } else if (arg_transport == TRANSPORT_SSH) {
5213 bus_connect_system_ssh(NULL, arg_host, &bus, &error);
5214 private_bus = false;
5216 assert_not_reached("Uh, invalid transport...");
5219 switch (arg_action) {
5221 case ACTION_SYSTEMCTL:
5222 r = systemctl_main(bus, argc, argv, &error);
5226 case ACTION_POWEROFF:
5232 case ACTION_RUNLEVEL2:
5233 case ACTION_RUNLEVEL3:
5234 case ACTION_RUNLEVEL4:
5235 case ACTION_RUNLEVEL5:
5237 case ACTION_EMERGENCY:
5238 case ACTION_DEFAULT:
5239 r = start_with_fallback(bus);
5244 r = reload_with_fallback(bus);
5247 case ACTION_CANCEL_SHUTDOWN:
5248 r = send_shutdownd(0, 0, false, false, NULL);
5251 case ACTION_INVALID:
5252 case ACTION_RUNLEVEL:
5254 assert_not_reached("Unknown action");
5257 retval = r < 0 ? EXIT_FAILURE : r;
5261 dbus_connection_flush(bus);
5262 dbus_connection_close(bus);
5263 dbus_connection_unref(bus);
5266 dbus_error_free(&error);
5270 strv_free(arg_property);