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_legend = false;
70 static bool arg_no_pager = false;
71 static bool arg_no_wtmp = false;
72 static bool arg_no_sync = false;
73 static bool arg_no_wall = false;
74 static bool arg_no_reload = false;
75 static bool arg_dry = false;
76 static bool arg_quiet = false;
77 static bool arg_full = false;
78 static bool arg_force = false;
79 static bool arg_ask_password = false;
80 static bool arg_failed = false;
81 static bool arg_runtime = false;
82 static char **arg_wall = NULL;
83 static const char *arg_kill_who = NULL;
84 static const char *arg_kill_mode = NULL;
85 static int arg_signal = SIGTERM;
86 static const char *arg_root = NULL;
87 static usec_t arg_when = 0;
106 ACTION_CANCEL_SHUTDOWN,
108 } arg_action = ACTION_SYSTEMCTL;
114 static enum transport {
118 } arg_transport = TRANSPORT_NORMAL;
119 static const char *arg_host = NULL;
121 static bool private_bus = false;
123 static int daemon_reload(DBusConnection *bus, char **args);
125 static bool on_tty(void) {
128 /* Note that this is invoked relatively early, before we start
129 * the pager. That means the value we return reflects whether
130 * we originally were started on a tty, not if we currently
131 * are. But this is intended, since we want colour and so on
132 * when run in our own pager. */
134 if (_unlikely_(t < 0))
135 t = isatty(STDOUT_FILENO) > 0;
140 static void pager_open_if_enabled(void) {
142 /* Cache result before we open the pager */
151 static void agent_open_if_enabled(void) {
153 /* Open the password agent as a child process if necessary */
155 if (!arg_ask_password)
158 if (arg_scope != UNIT_FILE_SYSTEM)
164 static const char *ansi_highlight(bool b) {
169 return b ? ANSI_HIGHLIGHT_ON : ANSI_HIGHLIGHT_OFF;
172 static const char *ansi_highlight_green(bool b) {
177 return b ? ANSI_HIGHLIGHT_GREEN_ON : ANSI_HIGHLIGHT_OFF;
180 static bool error_is_no_service(const DBusError *error) {
183 if (!dbus_error_is_set(error))
186 if (dbus_error_has_name(error, DBUS_ERROR_NAME_HAS_NO_OWNER))
189 if (dbus_error_has_name(error, DBUS_ERROR_SERVICE_UNKNOWN))
192 return startswith(error->name, "org.freedesktop.DBus.Error.Spawn.");
195 static int translate_bus_error_to_exit_status(int r, const DBusError *error) {
198 if (!dbus_error_is_set(error))
201 if (dbus_error_has_name(error, DBUS_ERROR_ACCESS_DENIED) ||
202 dbus_error_has_name(error, BUS_ERROR_ONLY_BY_DEPENDENCY) ||
203 dbus_error_has_name(error, BUS_ERROR_NO_ISOLATION) ||
204 dbus_error_has_name(error, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE))
205 return EXIT_NOPERMISSION;
207 if (dbus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT))
208 return EXIT_NOTINSTALLED;
210 if (dbus_error_has_name(error, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE) ||
211 dbus_error_has_name(error, BUS_ERROR_NOT_SUPPORTED))
212 return EXIT_NOTIMPLEMENTED;
214 if (dbus_error_has_name(error, BUS_ERROR_LOAD_FAILED))
215 return EXIT_NOTCONFIGURED;
223 static void warn_wall(enum action action) {
224 static const char *table[_ACTION_MAX] = {
225 [ACTION_HALT] = "The system is going down for system halt NOW!",
226 [ACTION_REBOOT] = "The system is going down for reboot NOW!",
227 [ACTION_POWEROFF] = "The system is going down for power-off NOW!",
228 [ACTION_KEXEC] = "The system is going down for kexec reboot NOW!",
229 [ACTION_RESCUE] = "The system is going down to rescue mode NOW!",
230 [ACTION_EMERGENCY] = "The system is going down to emergency mode NOW!"
239 if (!(p = strv_join(arg_wall, " "))) {
240 log_error("Failed to join strings.");
256 utmp_wall(table[action], NULL);
259 static bool avoid_bus(void) {
261 if (running_in_chroot() > 0)
264 if (sd_booted() <= 0)
267 if (!isempty(arg_root))
270 if (arg_scope == UNIT_FILE_GLOBAL)
278 const char *description;
279 const char *load_state;
280 const char *active_state;
281 const char *sub_state;
282 const char *following;
283 const char *unit_path;
285 const char *job_type;
286 const char *job_path;
289 static int compare_unit_info(const void *a, const void *b) {
291 const struct unit_info *u = a, *v = b;
293 d1 = strrchr(u->id, '.');
294 d2 = strrchr(v->id, '.');
299 if ((r = strcasecmp(d1, d2)) != 0)
303 return strcasecmp(u->id, v->id);
306 static bool output_show_unit(const struct unit_info *u) {
310 return streq(u->active_state, "failed");
312 return (!arg_type || ((dot = strrchr(u->id, '.')) &&
313 streq(dot+1, arg_type))) &&
314 (arg_all || !(streq(u->active_state, "inactive") || u->following[0]) || u->job_id > 0);
317 static void output_units_list(const struct unit_info *unit_infos, unsigned c) {
318 unsigned active_len, sub_len, job_len, n_shown = 0;
319 const struct unit_info *u;
321 active_len = sizeof("ACTIVE")-1;
322 sub_len = sizeof("SUB")-1;
323 job_len = sizeof("JOB")-1;
325 for (u = unit_infos; u < unit_infos + c; u++) {
326 if (!output_show_unit(u))
329 active_len = MAX(active_len, strlen(u->active_state));
330 sub_len = MAX(sub_len, strlen(u->sub_state));
332 job_len = MAX(job_len, strlen(u->job_type));
335 if (!arg_no_legend) {
336 printf("%-25s %-6s %-*s %-*s %-*s", "UNIT", "LOAD",
337 active_len, "ACTIVE", sub_len, "SUB", job_len, "JOB");
338 if (columns() >= 80+12 || arg_full || !arg_no_pager)
339 printf(" %s\n", "DESCRIPTION");
344 for (u = unit_infos; u < unit_infos + c; u++) {
347 const char *on_loaded, *off_loaded;
348 const char *on_active, *off_active;
350 if (!output_show_unit(u))
355 if (!streq(u->load_state, "loaded") &&
356 !streq(u->load_state, "banned")) {
357 on_loaded = ansi_highlight(true);
358 off_loaded = ansi_highlight(false);
360 on_loaded = off_loaded = "";
362 if (streq(u->active_state, "failed")) {
363 on_active = ansi_highlight(true);
364 off_active = ansi_highlight(false);
366 on_active = off_active = "";
368 e = arg_full ? NULL : ellipsize(u->id, 25, 33);
370 printf("%-25s %s%-6s%s %s%-*s %-*s%s%n",
372 on_loaded, u->load_state, off_loaded,
373 on_active, active_len, u->active_state,
374 sub_len, u->sub_state, off_active,
379 a -= strlen(on_loaded) + strlen(off_loaded);
380 a -= strlen(on_active) + strlen(off_active);
383 printf(" %-*s", job_len, u->job_type);
387 if (a + b + 1 < columns()) {
389 printf(" %-*s", job_len, "");
391 if (arg_full || !arg_no_pager)
392 printf(" %s", u->description);
394 printf(" %.*s", columns() - a - b - 1, u->description);
400 if (!arg_no_legend) {
401 printf("\nLOAD = Reflects whether the unit definition was properly loaded.\n"
402 "ACTIVE = The high-level unit activation state, i.e. generalization of SUB.\n"
403 "SUB = The low-level unit activation state, values depend on unit type.\n"
404 "JOB = Pending job for the unit.\n");
407 printf("\n%u units listed.\n", n_shown);
409 printf("\n%u units listed. Pass --all to see inactive units, too.\n", n_shown);
413 static int list_units(DBusConnection *bus, char **args) {
414 DBusMessage *m = NULL, *reply = NULL;
417 DBusMessageIter iter, sub, sub2;
418 unsigned c = 0, n_units = 0;
419 struct unit_info *unit_infos = NULL;
421 dbus_error_init(&error);
425 pager_open_if_enabled();
427 if (!(m = dbus_message_new_method_call(
428 "org.freedesktop.systemd1",
429 "/org/freedesktop/systemd1",
430 "org.freedesktop.systemd1.Manager",
432 log_error("Could not allocate message.");
436 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
437 log_error("Failed to issue method call: %s", bus_error_message(&error));
442 if (!dbus_message_iter_init(reply, &iter) ||
443 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
444 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
445 log_error("Failed to parse reply.");
450 dbus_message_iter_recurse(&iter, &sub);
452 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
455 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
456 log_error("Failed to parse reply.");
464 n_units = MAX(2*c, 16);
465 w = realloc(unit_infos, sizeof(struct unit_info) * n_units);
468 log_error("Failed to allocate unit array.");
478 dbus_message_iter_recurse(&sub, &sub2);
480 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->id, true) < 0 ||
481 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->description, true) < 0 ||
482 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->load_state, true) < 0 ||
483 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->active_state, true) < 0 ||
484 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->sub_state, true) < 0 ||
485 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->following, true) < 0 ||
486 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &u->unit_path, true) < 0 ||
487 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &u->job_id, true) < 0 ||
488 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->job_type, true) < 0 ||
489 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &u->job_path, false) < 0) {
490 log_error("Failed to parse reply.");
495 dbus_message_iter_next(&sub);
500 qsort(unit_infos, c, sizeof(struct unit_info), compare_unit_info);
501 output_units_list(unit_infos, c);
508 dbus_message_unref(m);
511 dbus_message_unref(reply);
515 dbus_error_free(&error);
520 static int compare_unit_file_list(const void *a, const void *b) {
522 const UnitFileList *u = a, *v = b;
524 d1 = strrchr(u->path, '.');
525 d2 = strrchr(v->path, '.');
530 r = strcasecmp(d1, d2);
535 return strcasecmp(file_name_from_path(u->path), file_name_from_path(v->path));
538 static bool output_show_unit_file(const UnitFileList *u) {
541 return !arg_type || ((dot = strrchr(u->path, '.')) && streq(dot+1, arg_type));
544 static void output_unit_file_list(const UnitFileList *units, unsigned c) {
545 unsigned n_shown = 0;
546 const UnitFileList *u;
549 printf("%-25s %-6s\n", "UNIT FILE", "STATE");
551 for (u = units; u < units + c; u++) {
553 const char *on, *off;
556 if (!output_show_unit_file(u))
561 if (u->state == UNIT_FILE_MASKED ||
562 u->state == UNIT_FILE_MASKED_RUNTIME ||
563 u->state == UNIT_FILE_DISABLED) {
564 on = ansi_highlight(true);
565 off = ansi_highlight(false);
566 } else if (u->state == UNIT_FILE_ENABLED) {
567 on = ansi_highlight_green(true);
568 off = ansi_highlight_green(false);
572 id = file_name_from_path(u->path);
574 e = arg_full ? NULL : ellipsize(id, 25, 33);
576 printf("%-25s %s%-6s%s\n",
578 on, unit_file_state_to_string(u->state), off);
584 printf("\n%u unit files listed.\n", n_shown);
587 static int list_unit_files(DBusConnection *bus, char **args) {
588 DBusMessage *m = NULL, *reply = NULL;
591 DBusMessageIter iter, sub, sub2;
592 unsigned c = 0, n_units = 0;
593 UnitFileList *units = NULL;
595 dbus_error_init(&error);
599 pager_open_if_enabled();
606 h = hashmap_new(string_hash_func, string_compare_func);
608 log_error("Out of memory");
612 r = unit_file_get_list(arg_scope, arg_root, h);
614 unit_file_list_free(h);
615 log_error("Failed to get unit file list: %s", strerror(-r));
619 n_units = hashmap_size(h);
620 units = new(UnitFileList, n_units);
622 unit_file_list_free(h);
623 log_error("Out of memory");
627 HASHMAP_FOREACH(u, h, i) {
628 memcpy(units + c++, u, sizeof(UnitFileList));
634 m = dbus_message_new_method_call(
635 "org.freedesktop.systemd1",
636 "/org/freedesktop/systemd1",
637 "org.freedesktop.systemd1.Manager",
640 log_error("Could not allocate message.");
644 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
646 log_error("Failed to issue method call: %s", bus_error_message(&error));
651 if (!dbus_message_iter_init(reply, &iter) ||
652 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
653 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
654 log_error("Failed to parse reply.");
659 dbus_message_iter_recurse(&iter, &sub);
661 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
665 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
666 log_error("Failed to parse reply.");
674 n_units = MAX(2*c, 16);
675 w = realloc(units, sizeof(struct UnitFileList) * n_units);
678 log_error("Failed to allocate unit array.");
688 dbus_message_iter_recurse(&sub, &sub2);
690 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->path, true) < 0 ||
691 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, false) < 0) {
692 log_error("Failed to parse reply.");
697 u->state = unit_file_state_from_string(state);
699 dbus_message_iter_next(&sub);
705 qsort(units, c, sizeof(UnitFileList), compare_unit_file_list);
706 output_unit_file_list(units, c);
713 dbus_message_unref(m);
716 dbus_message_unref(reply);
720 dbus_error_free(&error);
725 static int dot_one_property(const char *name, const char *prop, DBusMessageIter *iter) {
726 static const char * const colors[] = {
727 "Requires", "[color=\"black\"]",
728 "RequiresOverridable", "[color=\"black\"]",
729 "Requisite", "[color=\"darkblue\"]",
730 "RequisiteOverridable", "[color=\"darkblue\"]",
731 "Wants", "[color=\"darkgrey\"]",
732 "Conflicts", "[color=\"red\"]",
733 "ConflictedBy", "[color=\"red\"]",
734 "After", "[color=\"green\"]"
737 const char *c = NULL;
744 for (i = 0; i < ELEMENTSOF(colors); i += 2)
745 if (streq(colors[i], prop)) {
753 if (arg_dot != DOT_ALL)
754 if ((arg_dot == DOT_ORDER) != streq(prop, "After"))
757 switch (dbus_message_iter_get_arg_type(iter)) {
759 case DBUS_TYPE_ARRAY:
761 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
764 dbus_message_iter_recurse(iter, &sub);
766 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
769 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
770 dbus_message_iter_get_basic(&sub, &s);
771 printf("\t\"%s\"->\"%s\" %s;\n", name, s, c);
773 dbus_message_iter_next(&sub);
783 static int dot_one(DBusConnection *bus, const char *name, const char *path) {
784 DBusMessage *m = NULL, *reply = NULL;
785 const char *interface = "org.freedesktop.systemd1.Unit";
788 DBusMessageIter iter, sub, sub2, sub3;
793 dbus_error_init(&error);
795 if (!(m = dbus_message_new_method_call(
796 "org.freedesktop.systemd1",
798 "org.freedesktop.DBus.Properties",
800 log_error("Could not allocate message.");
805 if (!dbus_message_append_args(m,
806 DBUS_TYPE_STRING, &interface,
807 DBUS_TYPE_INVALID)) {
808 log_error("Could not append arguments to message.");
813 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
814 log_error("Failed to issue method call: %s", bus_error_message(&error));
819 if (!dbus_message_iter_init(reply, &iter) ||
820 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
821 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
822 log_error("Failed to parse reply.");
827 dbus_message_iter_recurse(&iter, &sub);
829 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
832 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
833 log_error("Failed to parse reply.");
838 dbus_message_iter_recurse(&sub, &sub2);
840 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &prop, true) < 0) {
841 log_error("Failed to parse reply.");
846 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
847 log_error("Failed to parse reply.");
852 dbus_message_iter_recurse(&sub2, &sub3);
854 if (dot_one_property(name, prop, &sub3)) {
855 log_error("Failed to parse reply.");
860 dbus_message_iter_next(&sub);
867 dbus_message_unref(m);
870 dbus_message_unref(reply);
872 dbus_error_free(&error);
877 static int dot(DBusConnection *bus, char **args) {
878 DBusMessage *m = NULL, *reply = NULL;
881 DBusMessageIter iter, sub, sub2;
883 dbus_error_init(&error);
887 if (!(m = dbus_message_new_method_call(
888 "org.freedesktop.systemd1",
889 "/org/freedesktop/systemd1",
890 "org.freedesktop.systemd1.Manager",
892 log_error("Could not allocate message.");
896 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
897 log_error("Failed to issue method call: %s", bus_error_message(&error));
902 if (!dbus_message_iter_init(reply, &iter) ||
903 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
904 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
905 log_error("Failed to parse reply.");
910 printf("digraph systemd {\n");
912 dbus_message_iter_recurse(&iter, &sub);
913 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
914 const char *id, *description, *load_state, *active_state, *sub_state, *following, *unit_path;
916 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
917 log_error("Failed to parse reply.");
922 dbus_message_iter_recurse(&sub, &sub2);
924 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
925 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
926 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
927 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
928 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
929 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &following, true) < 0 ||
930 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, true) < 0) {
931 log_error("Failed to parse reply.");
936 if ((r = dot_one(bus, id, unit_path)) < 0)
939 /* printf("\t\"%s\";\n", id); */
940 dbus_message_iter_next(&sub);
945 log_info(" Color legend: black = Requires\n"
946 " dark blue = Requisite\n"
947 " dark grey = Wants\n"
952 log_notice("-- You probably want to process this output with graphviz' dot tool.\n"
953 "-- Try a shell pipeline like 'systemctl dot | dot -Tsvg > systemd.svg'!\n");
959 dbus_message_unref(m);
962 dbus_message_unref(reply);
964 dbus_error_free(&error);
969 static int list_jobs(DBusConnection *bus, char **args) {
970 DBusMessage *m = NULL, *reply = NULL;
973 DBusMessageIter iter, sub, sub2;
976 dbus_error_init(&error);
980 pager_open_if_enabled();
982 if (!(m = dbus_message_new_method_call(
983 "org.freedesktop.systemd1",
984 "/org/freedesktop/systemd1",
985 "org.freedesktop.systemd1.Manager",
987 log_error("Could not allocate message.");
991 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
992 log_error("Failed to issue method call: %s", bus_error_message(&error));
997 if (!dbus_message_iter_init(reply, &iter) ||
998 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
999 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
1000 log_error("Failed to parse reply.");
1005 dbus_message_iter_recurse(&iter, &sub);
1008 printf("%4s %-25s %-15s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
1010 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1011 const char *name, *type, *state, *job_path, *unit_path;
1015 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
1016 log_error("Failed to parse reply.");
1021 dbus_message_iter_recurse(&sub, &sub2);
1023 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
1024 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
1025 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
1026 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
1027 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
1028 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
1029 log_error("Failed to parse reply.");
1034 e = arg_full ? NULL : ellipsize(name, 25, 33);
1035 printf("%4u %-25s %-15s %-7s\n", id, e ? e : name, type, state);
1040 dbus_message_iter_next(&sub);
1044 printf("\n%u jobs listed.\n", k);
1050 dbus_message_unref(m);
1053 dbus_message_unref(reply);
1055 dbus_error_free(&error);
1060 static int load_unit(DBusConnection *bus, char **args) {
1061 DBusMessage *m = NULL;
1066 dbus_error_init(&error);
1071 STRV_FOREACH(name, args+1) {
1074 if (!(m = dbus_message_new_method_call(
1075 "org.freedesktop.systemd1",
1076 "/org/freedesktop/systemd1",
1077 "org.freedesktop.systemd1.Manager",
1079 log_error("Could not allocate message.");
1084 if (!dbus_message_append_args(m,
1085 DBUS_TYPE_STRING, name,
1086 DBUS_TYPE_INVALID)) {
1087 log_error("Could not append arguments to message.");
1092 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1093 log_error("Failed to issue method call: %s", bus_error_message(&error));
1098 dbus_message_unref(m);
1099 dbus_message_unref(reply);
1108 dbus_message_unref(m);
1110 dbus_error_free(&error);
1115 static int cancel_job(DBusConnection *bus, char **args) {
1116 DBusMessage *m = NULL, *reply = NULL;
1121 dbus_error_init(&error);
1126 if (strv_length(args) <= 1)
1127 return daemon_reload(bus, args);
1129 STRV_FOREACH(name, args+1) {
1133 if (!(m = dbus_message_new_method_call(
1134 "org.freedesktop.systemd1",
1135 "/org/freedesktop/systemd1",
1136 "org.freedesktop.systemd1.Manager",
1138 log_error("Could not allocate message.");
1143 if ((r = safe_atou(*name, &id)) < 0) {
1144 log_error("Failed to parse job id: %s", strerror(-r));
1148 assert_cc(sizeof(uint32_t) == sizeof(id));
1149 if (!dbus_message_append_args(m,
1150 DBUS_TYPE_UINT32, &id,
1151 DBUS_TYPE_INVALID)) {
1152 log_error("Could not append arguments to message.");
1157 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1158 log_error("Failed to issue method call: %s", bus_error_message(&error));
1163 if (!dbus_message_get_args(reply, &error,
1164 DBUS_TYPE_OBJECT_PATH, &path,
1165 DBUS_TYPE_INVALID)) {
1166 log_error("Failed to parse reply: %s", bus_error_message(&error));
1171 dbus_message_unref(m);
1172 if (!(m = dbus_message_new_method_call(
1173 "org.freedesktop.systemd1",
1175 "org.freedesktop.systemd1.Job",
1177 log_error("Could not allocate message.");
1182 dbus_message_unref(reply);
1183 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1184 log_error("Failed to issue method call: %s", bus_error_message(&error));
1189 dbus_message_unref(m);
1190 dbus_message_unref(reply);
1198 dbus_message_unref(m);
1201 dbus_message_unref(reply);
1203 dbus_error_free(&error);
1208 static bool need_daemon_reload(DBusConnection *bus, const char *unit) {
1209 DBusMessage *m = NULL, *reply = NULL;
1210 dbus_bool_t b = FALSE;
1211 DBusMessageIter iter, sub;
1213 *interface = "org.freedesktop.systemd1.Unit",
1214 *property = "NeedDaemonReload",
1217 /* We ignore all errors here, since this is used to show a warning only */
1219 if (!(m = dbus_message_new_method_call(
1220 "org.freedesktop.systemd1",
1221 "/org/freedesktop/systemd1",
1222 "org.freedesktop.systemd1.Manager",
1226 if (!dbus_message_append_args(m,
1227 DBUS_TYPE_STRING, &unit,
1231 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
1234 if (!dbus_message_get_args(reply, NULL,
1235 DBUS_TYPE_OBJECT_PATH, &path,
1239 dbus_message_unref(m);
1240 if (!(m = dbus_message_new_method_call(
1241 "org.freedesktop.systemd1",
1243 "org.freedesktop.DBus.Properties",
1247 if (!dbus_message_append_args(m,
1248 DBUS_TYPE_STRING, &interface,
1249 DBUS_TYPE_STRING, &property,
1250 DBUS_TYPE_INVALID)) {
1254 dbus_message_unref(reply);
1255 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
1258 if (!dbus_message_iter_init(reply, &iter) ||
1259 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
1262 dbus_message_iter_recurse(&iter, &sub);
1264 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_BOOLEAN)
1267 dbus_message_iter_get_basic(&sub, &b);
1271 dbus_message_unref(m);
1274 dbus_message_unref(reply);
1279 typedef struct WaitData {
1284 static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
1292 dbus_error_init(&error);
1294 log_debug("Got D-Bus request: %s.%s() on %s",
1295 dbus_message_get_interface(message),
1296 dbus_message_get_member(message),
1297 dbus_message_get_path(message));
1299 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
1300 log_error("Warning! D-Bus connection terminated.");
1301 dbus_connection_close(connection);
1303 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
1305 const char *path, *result;
1306 dbus_bool_t success = true;
1308 if (dbus_message_get_args(message, &error,
1309 DBUS_TYPE_UINT32, &id,
1310 DBUS_TYPE_OBJECT_PATH, &path,
1311 DBUS_TYPE_STRING, &result,
1312 DBUS_TYPE_INVALID)) {
1315 if ((p = set_remove(d->set, (char*) path)))
1319 d->result = strdup(result);
1324 dbus_error_free(&error);
1326 if (dbus_message_get_args(message, &error,
1327 DBUS_TYPE_UINT32, &id,
1328 DBUS_TYPE_OBJECT_PATH, &path,
1329 DBUS_TYPE_BOOLEAN, &success,
1330 DBUS_TYPE_INVALID)) {
1333 /* Compatibility with older systemd versions <
1334 * 19 during upgrades. This should be dropped
1337 if ((p = set_remove(d->set, (char*) path)))
1341 d->result = strdup("failed");
1347 log_error("Failed to parse message: %s", bus_error_message(&error));
1351 dbus_error_free(&error);
1352 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1355 static int enable_wait_for_jobs(DBusConnection *bus) {
1363 dbus_error_init(&error);
1364 dbus_bus_add_match(bus,
1366 "sender='org.freedesktop.systemd1',"
1367 "interface='org.freedesktop.systemd1.Manager',"
1368 "member='JobRemoved',"
1369 "path='/org/freedesktop/systemd1'",
1372 if (dbus_error_is_set(&error)) {
1373 log_error("Failed to add match: %s", bus_error_message(&error));
1374 dbus_error_free(&error);
1378 /* This is slightly dirty, since we don't undo the match registrations. */
1382 static int wait_for_jobs(DBusConnection *bus, Set *s) {
1392 if (!dbus_connection_add_filter(bus, wait_filter, &d, NULL)) {
1393 log_error("Failed to add filter.");
1398 while (!set_isempty(s) &&
1399 dbus_connection_read_write_dispatch(bus, -1))
1402 if (!arg_quiet && d.result) {
1403 if (streq(d.result, "timeout"))
1404 log_error("Job timed out.");
1405 else if (streq(d.result, "canceled"))
1406 log_error("Job canceled.");
1407 else if (streq(d.result, "dependency"))
1408 log_error("A dependency job failed. See system logs for details.");
1409 else if (!streq(d.result, "done") && !streq(d.result, "skipped"))
1410 log_error("Job failed. See system logs and 'systemctl status' for details.");
1413 if (streq_ptr(d.result, "timeout"))
1415 else if (streq_ptr(d.result, "canceled"))
1417 else if (!streq_ptr(d.result, "done") && !streq_ptr(d.result, "skipped"))
1425 /* This is slightly dirty, since we don't undo the filter registration. */
1430 static int start_unit_one(
1431 DBusConnection *bus,
1438 DBusMessage *m = NULL, *reply = NULL;
1447 assert(arg_no_block || s);
1449 if (!(m = dbus_message_new_method_call(
1450 "org.freedesktop.systemd1",
1451 "/org/freedesktop/systemd1",
1452 "org.freedesktop.systemd1.Manager",
1454 log_error("Could not allocate message.");
1459 if (!dbus_message_append_args(m,
1460 DBUS_TYPE_STRING, &name,
1461 DBUS_TYPE_STRING, &mode,
1462 DBUS_TYPE_INVALID)) {
1463 log_error("Could not append arguments to message.");
1468 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, error))) {
1470 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(error)) {
1471 /* There's always a fallback possible for
1472 * legacy actions. */
1477 log_error("Failed to issue method call: %s", bus_error_message(error));
1482 if (!dbus_message_get_args(reply, error,
1483 DBUS_TYPE_OBJECT_PATH, &path,
1484 DBUS_TYPE_INVALID)) {
1485 log_error("Failed to parse reply: %s", bus_error_message(error));
1490 if (need_daemon_reload(bus, name))
1491 log_warning("Warning: Unit file of created job changed on disk, 'systemctl %s daemon-reload' recommended.",
1492 arg_scope == UNIT_FILE_SYSTEM ? "--system" : "--user");
1494 if (!arg_no_block) {
1497 if (!(p = strdup(path))) {
1498 log_error("Failed to duplicate path.");
1503 if ((r = set_put(s, p)) < 0) {
1505 log_error("Failed to add path to set.");
1514 dbus_message_unref(m);
1517 dbus_message_unref(reply);
1522 static enum action verb_to_action(const char *verb) {
1523 if (streq(verb, "halt"))
1525 else if (streq(verb, "poweroff"))
1526 return ACTION_POWEROFF;
1527 else if (streq(verb, "reboot"))
1528 return ACTION_REBOOT;
1529 else if (streq(verb, "kexec"))
1530 return ACTION_KEXEC;
1531 else if (streq(verb, "rescue"))
1532 return ACTION_RESCUE;
1533 else if (streq(verb, "emergency"))
1534 return ACTION_EMERGENCY;
1535 else if (streq(verb, "default"))
1536 return ACTION_DEFAULT;
1537 else if (streq(verb, "exit"))
1540 return ACTION_INVALID;
1543 static int start_unit(DBusConnection *bus, char **args) {
1545 static const char * const table[_ACTION_MAX] = {
1546 [ACTION_HALT] = SPECIAL_HALT_TARGET,
1547 [ACTION_POWEROFF] = SPECIAL_POWEROFF_TARGET,
1548 [ACTION_REBOOT] = SPECIAL_REBOOT_TARGET,
1549 [ACTION_KEXEC] = SPECIAL_KEXEC_TARGET,
1550 [ACTION_RUNLEVEL2] = SPECIAL_RUNLEVEL2_TARGET,
1551 [ACTION_RUNLEVEL3] = SPECIAL_RUNLEVEL3_TARGET,
1552 [ACTION_RUNLEVEL4] = SPECIAL_RUNLEVEL4_TARGET,
1553 [ACTION_RUNLEVEL5] = SPECIAL_RUNLEVEL5_TARGET,
1554 [ACTION_RESCUE] = SPECIAL_RESCUE_TARGET,
1555 [ACTION_EMERGENCY] = SPECIAL_EMERGENCY_TARGET,
1556 [ACTION_DEFAULT] = SPECIAL_DEFAULT_TARGET,
1557 [ACTION_EXIT] = SPECIAL_EXIT_TARGET
1561 const char *method, *mode, *one_name;
1566 dbus_error_init(&error);
1570 agent_open_if_enabled();
1572 if (arg_action == ACTION_SYSTEMCTL) {
1574 streq(args[0], "stop") ||
1575 streq(args[0], "condstop") ? "StopUnit" :
1576 streq(args[0], "reload") ? "ReloadUnit" :
1577 streq(args[0], "restart") ? "RestartUnit" :
1579 streq(args[0], "try-restart") ||
1580 streq(args[0], "condrestart") ? "TryRestartUnit" :
1582 streq(args[0], "reload-or-restart") ? "ReloadOrRestartUnit" :
1584 streq(args[0], "reload-or-try-restart") ||
1585 streq(args[0], "condreload") ||
1587 streq(args[0], "force-reload") ? "ReloadOrTryRestartUnit" :
1591 (streq(args[0], "isolate") ||
1592 streq(args[0], "rescue") ||
1593 streq(args[0], "emergency")) ? "isolate" : arg_job_mode;
1595 one_name = table[verb_to_action(args[0])];
1598 assert(arg_action < ELEMENTSOF(table));
1599 assert(table[arg_action]);
1601 method = "StartUnit";
1603 mode = (arg_action == ACTION_EMERGENCY ||
1604 arg_action == ACTION_RESCUE ||
1605 arg_action == ACTION_RUNLEVEL2 ||
1606 arg_action == ACTION_RUNLEVEL3 ||
1607 arg_action == ACTION_RUNLEVEL4 ||
1608 arg_action == ACTION_RUNLEVEL5) ? "isolate" : "replace";
1610 one_name = table[arg_action];
1613 if (!arg_no_block) {
1614 if ((ret = enable_wait_for_jobs(bus)) < 0) {
1615 log_error("Could not watch jobs: %s", strerror(-ret));
1619 if (!(s = set_new(string_hash_func, string_compare_func))) {
1620 log_error("Failed to allocate set.");
1627 if ((ret = start_unit_one(bus, method, one_name, mode, &error, s)) <= 0)
1630 STRV_FOREACH(name, args+1)
1631 if ((r = start_unit_one(bus, method, *name, mode, &error, s)) != 0) {
1632 ret = translate_bus_error_to_exit_status(r, &error);
1633 dbus_error_free(&error);
1638 if ((r = wait_for_jobs(bus, s)) < 0) {
1647 dbus_error_free(&error);
1652 static int start_special(DBusConnection *bus, char **args) {
1659 (streq(args[0], "halt") ||
1660 streq(args[0], "poweroff") ||
1661 streq(args[0], "reboot") ||
1662 streq(args[0], "kexec") ||
1663 streq(args[0], "exit")))
1664 return daemon_reload(bus, args);
1666 r = start_unit(bus, args);
1669 warn_wall(verb_to_action(args[0]));
1674 static int check_unit(DBusConnection *bus, char **args) {
1675 DBusMessage *m = NULL, *reply = NULL;
1677 *interface = "org.freedesktop.systemd1.Unit",
1678 *property = "ActiveState";
1679 int r = 3; /* According to LSB: "program is not running" */
1686 dbus_error_init(&error);
1688 STRV_FOREACH(name, args+1) {
1689 const char *path = NULL;
1691 DBusMessageIter iter, sub;
1693 if (!(m = dbus_message_new_method_call(
1694 "org.freedesktop.systemd1",
1695 "/org/freedesktop/systemd1",
1696 "org.freedesktop.systemd1.Manager",
1698 log_error("Could not allocate message.");
1703 if (!dbus_message_append_args(m,
1704 DBUS_TYPE_STRING, name,
1705 DBUS_TYPE_INVALID)) {
1706 log_error("Could not append arguments to message.");
1711 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1713 /* Hmm, cannot figure out anything about this unit... */
1717 dbus_error_free(&error);
1718 dbus_message_unref(m);
1723 if (!dbus_message_get_args(reply, &error,
1724 DBUS_TYPE_OBJECT_PATH, &path,
1725 DBUS_TYPE_INVALID)) {
1726 log_error("Failed to parse reply: %s", bus_error_message(&error));
1731 dbus_message_unref(m);
1732 if (!(m = dbus_message_new_method_call(
1733 "org.freedesktop.systemd1",
1735 "org.freedesktop.DBus.Properties",
1737 log_error("Could not allocate message.");
1742 if (!dbus_message_append_args(m,
1743 DBUS_TYPE_STRING, &interface,
1744 DBUS_TYPE_STRING, &property,
1745 DBUS_TYPE_INVALID)) {
1746 log_error("Could not append arguments to message.");
1751 dbus_message_unref(reply);
1752 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1753 log_error("Failed to issue method call: %s", bus_error_message(&error));
1758 if (!dbus_message_iter_init(reply, &iter) ||
1759 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1760 log_error("Failed to parse reply.");
1765 dbus_message_iter_recurse(&iter, &sub);
1767 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1768 log_error("Failed to parse reply.");
1773 dbus_message_iter_get_basic(&sub, &state);
1778 if (streq(state, "active") || streq(state, "reloading"))
1781 dbus_message_unref(m);
1782 dbus_message_unref(reply);
1788 dbus_message_unref(m);
1791 dbus_message_unref(reply);
1793 dbus_error_free(&error);
1798 static int kill_unit(DBusConnection *bus, char **args) {
1799 DBusMessage *m = NULL;
1807 dbus_error_init(&error);
1810 arg_kill_who = "all";
1813 arg_kill_mode = streq(arg_kill_who, "all") ? "control-group" : "process";
1815 STRV_FOREACH(name, args+1) {
1818 if (!(m = dbus_message_new_method_call(
1819 "org.freedesktop.systemd1",
1820 "/org/freedesktop/systemd1",
1821 "org.freedesktop.systemd1.Manager",
1823 log_error("Could not allocate message.");
1828 if (!dbus_message_append_args(m,
1829 DBUS_TYPE_STRING, name,
1830 DBUS_TYPE_STRING, &arg_kill_who,
1831 DBUS_TYPE_STRING, &arg_kill_mode,
1832 DBUS_TYPE_INT32, &arg_signal,
1833 DBUS_TYPE_INVALID)) {
1834 log_error("Could not append arguments to message.");
1839 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1840 log_error("Failed to issue method call: %s", bus_error_message(&error));
1841 dbus_error_free(&error);
1845 dbus_message_unref(m);
1848 dbus_message_unref(reply);
1854 dbus_message_unref(m);
1856 dbus_error_free(&error);
1861 typedef struct ExecStatusInfo {
1869 usec_t start_timestamp;
1870 usec_t exit_timestamp;
1875 LIST_FIELDS(struct ExecStatusInfo, exec);
1878 static void exec_status_info_free(ExecStatusInfo *i) {
1887 static int exec_status_info_deserialize(DBusMessageIter *sub, ExecStatusInfo *i) {
1888 uint64_t start_timestamp, exit_timestamp, start_timestamp_monotonic, exit_timestamp_monotonic;
1889 DBusMessageIter sub2, sub3;
1893 int32_t code, status;
1899 if (dbus_message_iter_get_arg_type(sub) != DBUS_TYPE_STRUCT)
1902 dbus_message_iter_recurse(sub, &sub2);
1904 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
1907 if (!(i->path = strdup(path)))
1910 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
1911 dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
1915 dbus_message_iter_recurse(&sub2, &sub3);
1916 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1917 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1918 dbus_message_iter_next(&sub3);
1923 if (!(i->argv = new0(char*, n+1)))
1927 dbus_message_iter_recurse(&sub2, &sub3);
1928 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1931 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1932 dbus_message_iter_get_basic(&sub3, &s);
1933 dbus_message_iter_next(&sub3);
1935 if (!(i->argv[n++] = strdup(s)))
1939 if (!dbus_message_iter_next(&sub2) ||
1940 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, true) < 0 ||
1941 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp, true) < 0 ||
1942 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp_monotonic, true) < 0 ||
1943 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp, true) < 0 ||
1944 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp_monotonic, true) < 0 ||
1945 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) < 0 ||
1946 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) < 0 ||
1947 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) < 0)
1951 i->start_timestamp = (usec_t) start_timestamp;
1952 i->exit_timestamp = (usec_t) exit_timestamp;
1953 i->pid = (pid_t) pid;
1960 typedef struct UnitStatusInfo {
1962 const char *load_state;
1963 const char *active_state;
1964 const char *sub_state;
1965 const char *unit_file_state;
1967 const char *description;
1968 const char *following;
1971 const char *default_control_group;
1973 const char *load_error;
1975 usec_t inactive_exit_timestamp;
1976 usec_t active_enter_timestamp;
1977 usec_t active_exit_timestamp;
1978 usec_t inactive_enter_timestamp;
1980 bool need_daemon_reload;
1985 const char *status_text;
1987 #ifdef HAVE_SYSV_COMPAT
1991 usec_t start_timestamp;
1992 usec_t exit_timestamp;
1994 int exit_code, exit_status;
1996 usec_t condition_timestamp;
1997 bool condition_result;
2000 unsigned n_accepted;
2001 unsigned n_connections;
2005 const char *sysfs_path;
2007 /* Mount, Automount */
2013 LIST_HEAD(ExecStatusInfo, exec);
2016 static void print_status_info(UnitStatusInfo *i) {
2018 const char *on, *off, *ss;
2020 char since1[FORMAT_TIMESTAMP_PRETTY_MAX], *s1;
2021 char since2[FORMAT_TIMESTAMP_MAX], *s2;
2025 /* This shows pretty information about a unit. See
2026 * print_property() for a low-level property printer */
2028 printf("%s", strna(i->id));
2030 if (i->description && !streq_ptr(i->id, i->description))
2031 printf(" - %s", i->description);
2036 printf("\t Follow: unit currently follows state of %s\n", i->following);
2038 if (streq_ptr(i->load_state, "failed") ||
2039 streq_ptr(i->load_state, "banned")) {
2040 on = ansi_highlight(true);
2041 off = ansi_highlight(false);
2046 printf("\t Loaded: %s%s%s (Reason: %s)\n", on, strna(i->load_state), off, i->load_error);
2047 else if (i->path && i->unit_file_state)
2048 printf("\t Loaded: %s%s%s (%s; %s)\n", on, strna(i->load_state), off, i->path, i->unit_file_state);
2050 printf("\t Loaded: %s%s%s (%s)\n", on, strna(i->load_state), off, i->path);
2052 printf("\t Loaded: %s%s%s\n", on, strna(i->load_state), off);
2054 ss = streq_ptr(i->active_state, i->sub_state) ? NULL : i->sub_state;
2056 if (streq_ptr(i->active_state, "failed")) {
2057 on = ansi_highlight(true);
2058 off = ansi_highlight(false);
2059 } else if (streq_ptr(i->active_state, "active") || streq_ptr(i->active_state, "reloading")) {
2060 on = ansi_highlight_green(true);
2061 off = ansi_highlight_green(false);
2066 printf("\t Active: %s%s (%s)%s",
2068 strna(i->active_state),
2072 printf("\t Active: %s%s%s",
2074 strna(i->active_state),
2077 timestamp = (streq_ptr(i->active_state, "active") ||
2078 streq_ptr(i->active_state, "reloading")) ? i->active_enter_timestamp :
2079 (streq_ptr(i->active_state, "inactive") ||
2080 streq_ptr(i->active_state, "failed")) ? i->inactive_enter_timestamp :
2081 streq_ptr(i->active_state, "activating") ? i->inactive_exit_timestamp :
2082 i->active_exit_timestamp;
2084 s1 = format_timestamp_pretty(since1, sizeof(since1), timestamp);
2085 s2 = format_timestamp(since2, sizeof(since2), timestamp);
2088 printf(" since %s; %s\n", s2, s1);
2090 printf(" since %s\n", s2);
2094 if (!i->condition_result && i->condition_timestamp > 0) {
2095 s1 = format_timestamp_pretty(since1, sizeof(since1), i->condition_timestamp);
2096 s2 = format_timestamp(since2, sizeof(since2), i->condition_timestamp);
2099 printf("\t start condition failed at %s; %s\n", s2, s1);
2101 printf("\t start condition failed at %s\n", s2);
2105 printf("\t Device: %s\n", i->sysfs_path);
2107 printf("\t Where: %s\n", i->where);
2109 printf("\t What: %s\n", i->what);
2112 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
2114 LIST_FOREACH(exec, p, i->exec) {
2118 /* Only show exited processes here */
2122 t = strv_join(p->argv, " ");
2123 printf("\t Process: %u %s=%s ", p->pid, p->name, strna(t));
2126 #ifdef HAVE_SYSV_COMPAT
2128 good = is_clean_exit_lsb(p->code, p->status);
2131 good = is_clean_exit(p->code, p->status);
2134 on = ansi_highlight(true);
2135 off = ansi_highlight(false);
2139 printf("%s(code=%s, ", on, sigchld_code_to_string(p->code));
2141 if (p->code == CLD_EXITED) {
2144 printf("status=%i", p->status);
2146 #ifdef HAVE_SYSV_COMPAT
2147 if ((c = exit_status_to_string(p->status, i->is_sysv ? EXIT_STATUS_LSB : EXIT_STATUS_SYSTEMD)))
2149 if ((c = exit_status_to_string(p->status, EXIT_STATUS_SYSTEMD)))
2154 printf("signal=%s", signal_to_string(p->status));
2156 printf(")%s\n", off);
2158 if (i->main_pid == p->pid &&
2159 i->start_timestamp == p->start_timestamp &&
2160 i->exit_timestamp == p->start_timestamp)
2161 /* Let's not show this twice */
2164 if (p->pid == i->control_pid)
2168 if (i->main_pid > 0 || i->control_pid > 0) {
2171 if (i->main_pid > 0) {
2172 printf("Main PID: %u", (unsigned) i->main_pid);
2176 get_process_name(i->main_pid, &t);
2181 } else if (i->exit_code > 0) {
2182 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
2184 if (i->exit_code == CLD_EXITED) {
2187 printf("status=%i", i->exit_status);
2189 #ifdef HAVE_SYSV_COMPAT
2190 if ((c = exit_status_to_string(i->exit_status, i->is_sysv ? EXIT_STATUS_LSB : EXIT_STATUS_SYSTEMD)))
2192 if ((c = exit_status_to_string(i->exit_status, EXIT_STATUS_SYSTEMD)))
2197 printf("signal=%s", signal_to_string(i->exit_status));
2202 if (i->main_pid > 0 && i->control_pid > 0)
2205 if (i->control_pid > 0) {
2208 printf(" Control: %u", (unsigned) i->control_pid);
2210 get_process_name(i->control_pid, &t);
2221 printf("\t Status: \"%s\"\n", i->status_text);
2223 if (i->default_control_group) {
2226 printf("\t CGroup: %s\n", i->default_control_group);
2228 if (arg_transport != TRANSPORT_SSH) {
2229 if ((c = columns()) > 18)
2234 show_cgroup_by_path(i->default_control_group, "\t\t ", c);
2238 if (i->need_daemon_reload)
2239 printf("\n%sWarning:%s Unit file changed on disk, 'systemctl %s daemon-reload' recommended.\n",
2240 ansi_highlight(true),
2241 ansi_highlight(false),
2242 arg_scope == UNIT_FILE_SYSTEM ? "--system" : "--user");
2245 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
2251 switch (dbus_message_iter_get_arg_type(iter)) {
2253 case DBUS_TYPE_STRING: {
2256 dbus_message_iter_get_basic(iter, &s);
2259 if (streq(name, "Id"))
2261 else if (streq(name, "LoadState"))
2263 else if (streq(name, "ActiveState"))
2264 i->active_state = s;
2265 else if (streq(name, "SubState"))
2267 else if (streq(name, "Description"))
2269 else if (streq(name, "FragmentPath"))
2271 #ifdef HAVE_SYSV_COMPAT
2272 else if (streq(name, "SysVPath")) {
2277 else if (streq(name, "DefaultControlGroup"))
2278 i->default_control_group = s;
2279 else if (streq(name, "StatusText"))
2281 else if (streq(name, "SysFSPath"))
2283 else if (streq(name, "Where"))
2285 else if (streq(name, "What"))
2287 else if (streq(name, "Following"))
2289 else if (streq(name, "UnitFileState"))
2290 i->unit_file_state = s;
2296 case DBUS_TYPE_BOOLEAN: {
2299 dbus_message_iter_get_basic(iter, &b);
2301 if (streq(name, "Accept"))
2303 else if (streq(name, "NeedDaemonReload"))
2304 i->need_daemon_reload = b;
2305 else if (streq(name, "ConditionResult"))
2306 i->condition_result = b;
2311 case DBUS_TYPE_UINT32: {
2314 dbus_message_iter_get_basic(iter, &u);
2316 if (streq(name, "MainPID")) {
2318 i->main_pid = (pid_t) u;
2321 } else if (streq(name, "ControlPID"))
2322 i->control_pid = (pid_t) u;
2323 else if (streq(name, "ExecMainPID")) {
2325 i->main_pid = (pid_t) u;
2326 } else if (streq(name, "NAccepted"))
2328 else if (streq(name, "NConnections"))
2329 i->n_connections = u;
2334 case DBUS_TYPE_INT32: {
2337 dbus_message_iter_get_basic(iter, &j);
2339 if (streq(name, "ExecMainCode"))
2340 i->exit_code = (int) j;
2341 else if (streq(name, "ExecMainStatus"))
2342 i->exit_status = (int) j;
2347 case DBUS_TYPE_UINT64: {
2350 dbus_message_iter_get_basic(iter, &u);
2352 if (streq(name, "ExecMainStartTimestamp"))
2353 i->start_timestamp = (usec_t) u;
2354 else if (streq(name, "ExecMainExitTimestamp"))
2355 i->exit_timestamp = (usec_t) u;
2356 else if (streq(name, "ActiveEnterTimestamp"))
2357 i->active_enter_timestamp = (usec_t) u;
2358 else if (streq(name, "InactiveEnterTimestamp"))
2359 i->inactive_enter_timestamp = (usec_t) u;
2360 else if (streq(name, "InactiveExitTimestamp"))
2361 i->inactive_exit_timestamp = (usec_t) u;
2362 else if (streq(name, "ActiveExitTimestamp"))
2363 i->active_exit_timestamp = (usec_t) u;
2364 else if (streq(name, "ConditionTimestamp"))
2365 i->condition_timestamp = (usec_t) u;
2370 case DBUS_TYPE_ARRAY: {
2372 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT &&
2373 startswith(name, "Exec")) {
2374 DBusMessageIter sub;
2376 dbus_message_iter_recurse(iter, &sub);
2377 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2378 ExecStatusInfo *info;
2381 if (!(info = new0(ExecStatusInfo, 1)))
2384 if (!(info->name = strdup(name))) {
2389 if ((r = exec_status_info_deserialize(&sub, info)) < 0) {
2394 LIST_PREPEND(ExecStatusInfo, exec, i->exec, info);
2396 dbus_message_iter_next(&sub);
2403 case DBUS_TYPE_STRUCT: {
2405 if (streq(name, "LoadError")) {
2406 DBusMessageIter sub;
2407 const char *n, *message;
2410 dbus_message_iter_recurse(iter, &sub);
2412 r = bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &n, true);
2416 r = bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &message, false);
2420 if (!isempty(message))
2421 i->load_error = message;
2431 static int print_property(const char *name, DBusMessageIter *iter) {
2435 /* This is a low-level property printer, see
2436 * print_status_info() for the nicer output */
2438 if (arg_property && !strv_find(arg_property, name))
2441 switch (dbus_message_iter_get_arg_type(iter)) {
2443 case DBUS_TYPE_STRUCT: {
2444 DBusMessageIter sub;
2445 dbus_message_iter_recurse(iter, &sub);
2447 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
2450 dbus_message_iter_get_basic(&sub, &u);
2453 printf("%s=%u\n", name, (unsigned) u);
2455 printf("%s=\n", name);
2458 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
2461 dbus_message_iter_get_basic(&sub, &s);
2463 if (arg_all || s[0])
2464 printf("%s=%s\n", name, s);
2467 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "LoadError")) {
2468 const char *a = NULL, *b = NULL;
2470 if (bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &a, true) >= 0)
2471 bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &b, false);
2473 if (arg_all || !isempty(a) || !isempty(b))
2474 printf("%s=%s \"%s\"\n", name, strempty(a), strempty(b));
2482 case DBUS_TYPE_ARRAY:
2484 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "EnvironmentFiles")) {
2485 DBusMessageIter sub, sub2;
2487 dbus_message_iter_recurse(iter, &sub);
2488 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2492 dbus_message_iter_recurse(&sub, &sub2);
2494 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) >= 0 &&
2495 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, false) >= 0)
2496 printf("EnvironmentFile=%s (ignore_errors=%s)\n", path, yes_no(ignore));
2498 dbus_message_iter_next(&sub);
2503 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
2504 DBusMessageIter sub, sub2;
2506 dbus_message_iter_recurse(iter, &sub);
2507 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2508 const char *type, *path;
2510 dbus_message_iter_recurse(&sub, &sub2);
2512 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
2513 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
2514 printf("%s=%s\n", type, path);
2516 dbus_message_iter_next(&sub);
2521 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
2522 DBusMessageIter sub, sub2;
2524 dbus_message_iter_recurse(iter, &sub);
2525 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2527 uint64_t value, next_elapse;
2529 dbus_message_iter_recurse(&sub, &sub2);
2531 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
2532 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
2533 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
2534 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
2536 printf("%s={ value=%s ; next_elapse=%s }\n",
2538 format_timespan(timespan1, sizeof(timespan1), value),
2539 format_timespan(timespan2, sizeof(timespan2), next_elapse));
2542 dbus_message_iter_next(&sub);
2547 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "ControlGroupAttributes")) {
2548 DBusMessageIter sub, sub2;
2550 dbus_message_iter_recurse(iter, &sub);
2551 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2552 const char *controller, *attr, *value;
2554 dbus_message_iter_recurse(&sub, &sub2);
2556 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &controller, true) >= 0 &&
2557 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &attr, true) >= 0 &&
2558 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &value, false) >= 0) {
2560 printf("ControlGroupAttribute={ controller=%s ; attribute=%s ; value=\"%s\" }\n",
2566 dbus_message_iter_next(&sub);
2571 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
2572 DBusMessageIter sub;
2574 dbus_message_iter_recurse(iter, &sub);
2575 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2576 ExecStatusInfo info;
2579 if (exec_status_info_deserialize(&sub, &info) >= 0) {
2580 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
2583 t = strv_join(info.argv, " ");
2585 printf("%s={ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n",
2589 yes_no(info.ignore),
2590 strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)),
2591 strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)),
2592 (unsigned) info. pid,
2593 sigchld_code_to_string(info.code),
2595 info.code == CLD_EXITED ? "" : "/",
2596 strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status)));
2602 strv_free(info.argv);
2604 dbus_message_iter_next(&sub);
2613 if (generic_print_property(name, iter, arg_all) > 0)
2617 printf("%s=[unprintable]\n", name);
2622 static int show_one(const char *verb, DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
2623 DBusMessage *m = NULL, *reply = NULL;
2624 const char *interface = "";
2627 DBusMessageIter iter, sub, sub2, sub3;
2628 UnitStatusInfo info;
2636 dbus_error_init(&error);
2638 if (!(m = dbus_message_new_method_call(
2639 "org.freedesktop.systemd1",
2641 "org.freedesktop.DBus.Properties",
2643 log_error("Could not allocate message.");
2648 if (!dbus_message_append_args(m,
2649 DBUS_TYPE_STRING, &interface,
2650 DBUS_TYPE_INVALID)) {
2651 log_error("Could not append arguments to message.");
2656 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2657 log_error("Failed to issue method call: %s", bus_error_message(&error));
2662 if (!dbus_message_iter_init(reply, &iter) ||
2663 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
2664 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
2665 log_error("Failed to parse reply.");
2670 dbus_message_iter_recurse(&iter, &sub);
2677 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2680 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
2681 log_error("Failed to parse reply.");
2686 dbus_message_iter_recurse(&sub, &sub2);
2688 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
2689 log_error("Failed to parse reply.");
2694 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
2695 log_error("Failed to parse reply.");
2700 dbus_message_iter_recurse(&sub2, &sub3);
2702 if (show_properties)
2703 r = print_property(name, &sub3);
2705 r = status_property(name, &sub3, &info);
2708 log_error("Failed to parse reply.");
2713 dbus_message_iter_next(&sub);
2718 if (!show_properties)
2719 print_status_info(&info);
2721 if (!streq_ptr(info.active_state, "active") &&
2722 !streq_ptr(info.active_state, "reloading") &&
2723 streq(verb, "status"))
2724 /* According to LSB: "program not running" */
2727 while ((p = info.exec)) {
2728 LIST_REMOVE(ExecStatusInfo, exec, info.exec, p);
2729 exec_status_info_free(p);
2734 dbus_message_unref(m);
2737 dbus_message_unref(reply);
2739 dbus_error_free(&error);
2744 static int show(DBusConnection *bus, char **args) {
2745 DBusMessage *m = NULL, *reply = NULL;
2748 bool show_properties, new_line = false;
2754 dbus_error_init(&error);
2756 show_properties = !streq(args[0], "status");
2758 if (show_properties)
2759 pager_open_if_enabled();
2761 if (show_properties && strv_length(args) <= 1) {
2762 /* If not argument is specified inspect the manager
2765 ret = show_one(args[0], bus, "/org/freedesktop/systemd1", show_properties, &new_line);
2769 STRV_FOREACH(name, args+1) {
2770 const char *path = NULL;
2773 if (safe_atou32(*name, &id) < 0) {
2775 /* Interpret as unit name */
2777 if (!(m = dbus_message_new_method_call(
2778 "org.freedesktop.systemd1",
2779 "/org/freedesktop/systemd1",
2780 "org.freedesktop.systemd1.Manager",
2782 log_error("Could not allocate message.");
2787 if (!dbus_message_append_args(m,
2788 DBUS_TYPE_STRING, name,
2789 DBUS_TYPE_INVALID)) {
2790 log_error("Could not append arguments to message.");
2795 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2797 if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
2798 log_error("Failed to issue method call: %s", bus_error_message(&error));
2803 dbus_error_free(&error);
2805 dbus_message_unref(m);
2806 if (!(m = dbus_message_new_method_call(
2807 "org.freedesktop.systemd1",
2808 "/org/freedesktop/systemd1",
2809 "org.freedesktop.systemd1.Manager",
2811 log_error("Could not allocate message.");
2816 if (!dbus_message_append_args(m,
2817 DBUS_TYPE_STRING, name,
2818 DBUS_TYPE_INVALID)) {
2819 log_error("Could not append arguments to message.");
2824 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2825 log_error("Failed to issue method call: %s", bus_error_message(&error));
2827 if (dbus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT))
2828 ret = 4; /* According to LSB: "program or service status is unknown" */
2835 } else if (show_properties) {
2837 /* Interpret as job id */
2839 if (!(m = dbus_message_new_method_call(
2840 "org.freedesktop.systemd1",
2841 "/org/freedesktop/systemd1",
2842 "org.freedesktop.systemd1.Manager",
2844 log_error("Could not allocate message.");
2849 if (!dbus_message_append_args(m,
2850 DBUS_TYPE_UINT32, &id,
2851 DBUS_TYPE_INVALID)) {
2852 log_error("Could not append arguments to message.");
2857 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2858 log_error("Failed to issue method call: %s", bus_error_message(&error));
2864 /* Interpret as PID */
2866 if (!(m = dbus_message_new_method_call(
2867 "org.freedesktop.systemd1",
2868 "/org/freedesktop/systemd1",
2869 "org.freedesktop.systemd1.Manager",
2871 log_error("Could not allocate message.");
2876 if (!dbus_message_append_args(m,
2877 DBUS_TYPE_UINT32, &id,
2878 DBUS_TYPE_INVALID)) {
2879 log_error("Could not append arguments to message.");
2884 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2885 log_error("Failed to issue method call: %s", bus_error_message(&error));
2891 if (!dbus_message_get_args(reply, &error,
2892 DBUS_TYPE_OBJECT_PATH, &path,
2893 DBUS_TYPE_INVALID)) {
2894 log_error("Failed to parse reply: %s", bus_error_message(&error));
2899 if ((r = show_one(args[0], bus, path, show_properties, &new_line)) != 0)
2902 dbus_message_unref(m);
2903 dbus_message_unref(reply);
2909 dbus_message_unref(m);
2912 dbus_message_unref(reply);
2914 dbus_error_free(&error);
2919 static int dump(DBusConnection *bus, char **args) {
2920 DBusMessage *m = NULL, *reply = NULL;
2925 dbus_error_init(&error);
2927 pager_open_if_enabled();
2929 if (!(m = dbus_message_new_method_call(
2930 "org.freedesktop.systemd1",
2931 "/org/freedesktop/systemd1",
2932 "org.freedesktop.systemd1.Manager",
2934 log_error("Could not allocate message.");
2938 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2939 log_error("Failed to issue method call: %s", bus_error_message(&error));
2944 if (!dbus_message_get_args(reply, &error,
2945 DBUS_TYPE_STRING, &text,
2946 DBUS_TYPE_INVALID)) {
2947 log_error("Failed to parse reply: %s", bus_error_message(&error));
2952 fputs(text, stdout);
2958 dbus_message_unref(m);
2961 dbus_message_unref(reply);
2963 dbus_error_free(&error);
2968 static int snapshot(DBusConnection *bus, char **args) {
2969 DBusMessage *m = NULL, *reply = NULL;
2972 const char *name = "", *path, *id;
2973 dbus_bool_t cleanup = FALSE;
2974 DBusMessageIter iter, sub;
2976 *interface = "org.freedesktop.systemd1.Unit",
2979 dbus_error_init(&error);
2981 if (!(m = dbus_message_new_method_call(
2982 "org.freedesktop.systemd1",
2983 "/org/freedesktop/systemd1",
2984 "org.freedesktop.systemd1.Manager",
2985 "CreateSnapshot"))) {
2986 log_error("Could not allocate message.");
2990 if (strv_length(args) > 1)
2993 if (!dbus_message_append_args(m,
2994 DBUS_TYPE_STRING, &name,
2995 DBUS_TYPE_BOOLEAN, &cleanup,
2996 DBUS_TYPE_INVALID)) {
2997 log_error("Could not append arguments to message.");
3002 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3003 log_error("Failed to issue method call: %s", bus_error_message(&error));
3008 if (!dbus_message_get_args(reply, &error,
3009 DBUS_TYPE_OBJECT_PATH, &path,
3010 DBUS_TYPE_INVALID)) {
3011 log_error("Failed to parse reply: %s", bus_error_message(&error));
3016 dbus_message_unref(m);
3017 if (!(m = dbus_message_new_method_call(
3018 "org.freedesktop.systemd1",
3020 "org.freedesktop.DBus.Properties",
3022 log_error("Could not allocate message.");
3026 if (!dbus_message_append_args(m,
3027 DBUS_TYPE_STRING, &interface,
3028 DBUS_TYPE_STRING, &property,
3029 DBUS_TYPE_INVALID)) {
3030 log_error("Could not append arguments to message.");
3035 dbus_message_unref(reply);
3036 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3037 log_error("Failed to issue method call: %s", bus_error_message(&error));
3042 if (!dbus_message_iter_init(reply, &iter) ||
3043 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
3044 log_error("Failed to parse reply.");
3049 dbus_message_iter_recurse(&iter, &sub);
3051 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
3052 log_error("Failed to parse reply.");
3057 dbus_message_iter_get_basic(&sub, &id);
3065 dbus_message_unref(m);
3068 dbus_message_unref(reply);
3070 dbus_error_free(&error);
3075 static int delete_snapshot(DBusConnection *bus, char **args) {
3076 DBusMessage *m = NULL, *reply = NULL;
3084 dbus_error_init(&error);
3086 STRV_FOREACH(name, args+1) {
3087 const char *path = NULL;
3089 if (!(m = dbus_message_new_method_call(
3090 "org.freedesktop.systemd1",
3091 "/org/freedesktop/systemd1",
3092 "org.freedesktop.systemd1.Manager",
3094 log_error("Could not allocate message.");
3099 if (!dbus_message_append_args(m,
3100 DBUS_TYPE_STRING, name,
3101 DBUS_TYPE_INVALID)) {
3102 log_error("Could not append arguments to message.");
3107 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3108 log_error("Failed to issue method call: %s", bus_error_message(&error));
3113 if (!dbus_message_get_args(reply, &error,
3114 DBUS_TYPE_OBJECT_PATH, &path,
3115 DBUS_TYPE_INVALID)) {
3116 log_error("Failed to parse reply: %s", bus_error_message(&error));
3121 dbus_message_unref(m);
3122 if (!(m = dbus_message_new_method_call(
3123 "org.freedesktop.systemd1",
3125 "org.freedesktop.systemd1.Snapshot",
3127 log_error("Could not allocate message.");
3132 dbus_message_unref(reply);
3133 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3134 log_error("Failed to issue method call: %s", bus_error_message(&error));
3139 dbus_message_unref(m);
3140 dbus_message_unref(reply);
3148 dbus_message_unref(m);
3151 dbus_message_unref(reply);
3153 dbus_error_free(&error);
3158 static int daemon_reload(DBusConnection *bus, char **args) {
3159 DBusMessage *m = NULL, *reply = NULL;
3164 dbus_error_init(&error);
3166 if (arg_action == ACTION_RELOAD)
3168 else if (arg_action == ACTION_REEXEC)
3169 method = "Reexecute";
3171 assert(arg_action == ACTION_SYSTEMCTL);
3174 streq(args[0], "clear-jobs") ||
3175 streq(args[0], "cancel") ? "ClearJobs" :
3176 streq(args[0], "daemon-reexec") ? "Reexecute" :
3177 streq(args[0], "reset-failed") ? "ResetFailed" :
3178 streq(args[0], "halt") ? "Halt" :
3179 streq(args[0], "poweroff") ? "PowerOff" :
3180 streq(args[0], "reboot") ? "Reboot" :
3181 streq(args[0], "kexec") ? "KExec" :
3182 streq(args[0], "exit") ? "Exit" :
3183 /* "daemon-reload" */ "Reload";
3186 if (!(m = dbus_message_new_method_call(
3187 "org.freedesktop.systemd1",
3188 "/org/freedesktop/systemd1",
3189 "org.freedesktop.systemd1.Manager",
3191 log_error("Could not allocate message.");
3195 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3197 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
3198 /* There's always a fallback possible for
3199 * legacy actions. */
3204 if (streq(method, "Reexecute") && dbus_error_has_name(&error, DBUS_ERROR_NO_REPLY)) {
3205 /* On reexecution, we expect a disconnect, not
3211 log_error("Failed to issue method call: %s", bus_error_message(&error));
3220 dbus_message_unref(m);
3223 dbus_message_unref(reply);
3225 dbus_error_free(&error);
3230 static int reset_failed(DBusConnection *bus, char **args) {
3231 DBusMessage *m = NULL;
3237 dbus_error_init(&error);
3239 if (strv_length(args) <= 1)
3240 return daemon_reload(bus, args);
3242 STRV_FOREACH(name, args+1) {
3245 if (!(m = dbus_message_new_method_call(
3246 "org.freedesktop.systemd1",
3247 "/org/freedesktop/systemd1",
3248 "org.freedesktop.systemd1.Manager",
3249 "ResetFailedUnit"))) {
3250 log_error("Could not allocate message.");
3255 if (!dbus_message_append_args(m,
3256 DBUS_TYPE_STRING, name,
3257 DBUS_TYPE_INVALID)) {
3258 log_error("Could not append arguments to message.");
3263 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3264 log_error("Failed to issue method call: %s", bus_error_message(&error));
3269 dbus_message_unref(m);
3270 dbus_message_unref(reply);
3278 dbus_message_unref(m);
3280 dbus_error_free(&error);
3285 static int show_enviroment(DBusConnection *bus, char **args) {
3286 DBusMessage *m = NULL, *reply = NULL;
3288 DBusMessageIter iter, sub, sub2;
3291 *interface = "org.freedesktop.systemd1.Manager",
3292 *property = "Environment";
3294 dbus_error_init(&error);
3296 pager_open_if_enabled();
3298 if (!(m = dbus_message_new_method_call(
3299 "org.freedesktop.systemd1",
3300 "/org/freedesktop/systemd1",
3301 "org.freedesktop.DBus.Properties",
3303 log_error("Could not allocate message.");
3307 if (!dbus_message_append_args(m,
3308 DBUS_TYPE_STRING, &interface,
3309 DBUS_TYPE_STRING, &property,
3310 DBUS_TYPE_INVALID)) {
3311 log_error("Could not append arguments to message.");
3316 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3317 log_error("Failed to issue method call: %s", bus_error_message(&error));
3322 if (!dbus_message_iter_init(reply, &iter) ||
3323 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
3324 log_error("Failed to parse reply.");
3329 dbus_message_iter_recurse(&iter, &sub);
3331 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
3332 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
3333 log_error("Failed to parse reply.");
3338 dbus_message_iter_recurse(&sub, &sub2);
3340 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
3343 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
3344 log_error("Failed to parse reply.");
3349 dbus_message_iter_get_basic(&sub2, &text);
3350 printf("%s\n", text);
3352 dbus_message_iter_next(&sub2);
3359 dbus_message_unref(m);
3362 dbus_message_unref(reply);
3364 dbus_error_free(&error);
3369 static int set_environment(DBusConnection *bus, char **args) {
3370 DBusMessage *m = NULL, *reply = NULL;
3374 DBusMessageIter iter, sub;
3377 dbus_error_init(&error);
3379 method = streq(args[0], "set-environment")
3381 : "UnsetEnvironment";
3383 if (!(m = dbus_message_new_method_call(
3384 "org.freedesktop.systemd1",
3385 "/org/freedesktop/systemd1",
3386 "org.freedesktop.systemd1.Manager",
3389 log_error("Could not allocate message.");
3393 dbus_message_iter_init_append(m, &iter);
3395 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
3396 log_error("Could not append arguments to message.");
3401 STRV_FOREACH(name, args+1)
3402 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, name)) {
3403 log_error("Could not append arguments to message.");
3408 if (!dbus_message_iter_close_container(&iter, &sub)) {
3409 log_error("Could not append arguments to message.");
3414 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3415 log_error("Failed to issue method call: %s", bus_error_message(&error));
3424 dbus_message_unref(m);
3427 dbus_message_unref(reply);
3429 dbus_error_free(&error);
3434 static int enable_sysv_units(char **args) {
3437 #if defined (HAVE_SYSV_COMPAT) && (defined(TARGET_FEDORA) || defined(TARGET_MANDRIVA) || defined(TARGET_SUSE) || defined(TARGET_MEEGO) || defined(TARGET_ALTLINUX))
3438 const char *verb = args[0];
3439 unsigned f = 1, t = 1;
3442 if (arg_scope != UNIT_FILE_SYSTEM)
3445 if (!streq(verb, "enable") &&
3446 !streq(verb, "disable") &&
3447 !streq(verb, "is-enabled"))
3450 /* Processes all SysV units, and reshuffles the array so that
3451 * afterwards only the native units remain */
3454 r = lookup_paths_init(&paths, MANAGER_SYSTEM, false);
3460 for (f = 1; args[f]; f++) {
3463 bool found_native = false, found_sysv;
3465 const char *argv[6] = { "/sbin/chkconfig", NULL, NULL, NULL, NULL };
3466 char **k, *l, *q = NULL;
3473 if (!endswith(name, ".service"))
3476 if (path_is_absolute(name))
3479 STRV_FOREACH(k, paths.unit_path) {
3482 if (!isempty(arg_root))
3483 asprintf(&p, "%s/%s/%s", arg_root, *k, name);
3485 asprintf(&p, "%s/%s", *k, name);
3488 log_error("No memory");
3493 found_native = access(p, F_OK) >= 0;
3504 if (!isempty(arg_root))
3505 asprintf(&p, "%s/" SYSTEM_SYSVINIT_PATH "/%s", arg_root, name);
3507 asprintf(&p, SYSTEM_SYSVINIT_PATH "/%s", name);
3509 log_error("No memory");
3514 p[strlen(p) - sizeof(".service") + 1] = 0;
3515 found_sysv = access(p, F_OK) >= 0;
3522 /* Mark this entry, so that we don't try enabling it as native unit */
3523 args[f] = (char*) "";
3525 log_info("%s is not a native service, redirecting to /sbin/chkconfig.", name);
3527 if (!isempty(arg_root))
3528 argv[c++] = q = strappend("--root=", arg_root);
3530 argv[c++] = file_name_from_path(p);
3532 streq(verb, "enable") ? "on" :
3533 streq(verb, "disable") ? "off" : "--level=5";
3536 l = strv_join((char**)argv, " ");
3538 log_error("No memory.");
3545 log_info("Executing %s", l);
3550 log_error("Failed to fork: %m");
3555 } else if (pid == 0) {
3558 execv(argv[0], (char**) argv);
3559 _exit(EXIT_FAILURE);
3565 j = wait_for_terminate(pid, &status);
3567 log_error("Failed to wait for child: %s", strerror(-r));
3572 if (status.si_code == CLD_EXITED) {
3573 if (streq(verb, "is-enabled")) {
3574 if (status.si_status == 0) {
3583 } else if (status.si_status != 0) {
3594 lookup_paths_free(&paths);
3596 /* Drop all SysV units */
3597 for (f = 1, t = 1; args[f]; f++) {
3599 if (isempty(args[f]))
3602 args[t++] = args[f];
3611 static int enable_unit(DBusConnection *bus, char **args) {
3612 const char *verb = args[0];
3613 UnitFileChange *changes = NULL;
3614 unsigned n_changes = 0, i;
3615 int carries_install_info = -1;
3616 DBusMessage *m = NULL, *reply = NULL;
3620 dbus_error_init(&error);
3622 r = enable_sysv_units(args);
3626 if (!bus || avoid_bus()) {
3627 if (streq(verb, "enable")) {
3628 r = unit_file_enable(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3629 carries_install_info = r;
3630 } else if (streq(verb, "disable"))
3631 r = unit_file_disable(arg_scope, arg_runtime, arg_root, args+1, &changes, &n_changes);
3632 else if (streq(verb, "reenable")) {
3633 r = unit_file_reenable(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3634 carries_install_info = r;
3635 } else if (streq(verb, "link"))
3636 r = unit_file_link(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3637 else if (streq(verb, "preset")) {
3638 r = unit_file_preset(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3639 carries_install_info = r;
3640 } else if (streq(verb, "mask"))
3641 r = unit_file_mask(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3642 else if (streq(verb, "unmask"))
3643 r = unit_file_unmask(arg_scope, arg_runtime, arg_root, args+1, &changes, &n_changes);
3645 assert_not_reached("Unknown verb");
3648 log_error("Operation failed: %s", strerror(-r));
3652 for (i = 0; i < n_changes; i++) {
3653 if (changes[i].type == UNIT_FILE_SYMLINK)
3654 log_info("ln -s '%s' '%s'", changes[i].source, changes[i].path);
3656 log_info("rm '%s'", changes[i].path);
3661 bool send_force = true, expect_carries_install_info = false;
3663 DBusMessageIter iter, sub, sub2;
3665 if (streq(verb, "enable")) {
3666 method = "EnableUnitFiles";
3667 expect_carries_install_info = true;
3668 } else if (streq(verb, "disable")) {
3669 method = "DisableUnitFiles";
3671 } else if (streq(verb, "reenable")) {
3672 method = "ReenableUnitFiles";
3673 expect_carries_install_info = true;
3674 } else if (streq(verb, "link"))
3675 method = "LinkUnitFiles";
3676 else if (streq(verb, "preset")) {
3677 method = "PresetUnitFiles";
3678 expect_carries_install_info = true;
3679 } else if (streq(verb, "mask"))
3680 method = "MaskUnitFiles";
3681 else if (streq(verb, "unmask")) {
3682 method = "UnmaskUnitFiles";
3685 assert_not_reached("Unknown verb");
3687 m = dbus_message_new_method_call(
3688 "org.freedesktop.systemd1",
3689 "/org/freedesktop/systemd1",
3690 "org.freedesktop.systemd1.Manager",
3693 log_error("Out of memory");
3698 dbus_message_iter_init_append(m, &iter);
3700 r = bus_append_strv_iter(&iter, args+1);
3702 log_error("Failed to append unit files.");
3707 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &a)) {
3708 log_error("Failed to append runtime boolean.");
3716 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b)) {
3717 log_error("Failed to append force boolean.");
3723 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
3725 log_error("Failed to issue method call: %s", bus_error_message(&error));
3730 if (!dbus_message_iter_init(reply, &iter)) {
3731 log_error("Failed to initialize iterator.");
3735 if (expect_carries_install_info) {
3736 r = bus_iter_get_basic_and_next(&iter, DBUS_TYPE_BOOLEAN, &b, true);
3738 log_error("Failed to parse reply.");
3742 carries_install_info = b;
3745 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
3746 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
3747 log_error("Failed to parse reply.");
3752 dbus_message_iter_recurse(&iter, &sub);
3753 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
3754 const char *type, *path, *source;
3756 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
3757 log_error("Failed to parse reply.");
3762 dbus_message_iter_recurse(&sub, &sub2);
3764 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
3765 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0 ||
3766 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &source, false) < 0) {
3767 log_error("Failed to parse reply.");
3772 if (streq(type, "symlink"))
3773 log_info("ln -s '%s' '%s'", source, path);
3775 log_info("rm '%s'", path);
3777 dbus_message_iter_next(&sub);
3780 /* Try to reload if enabeld */
3782 r = daemon_reload(bus, args);
3785 if (carries_install_info == 0)
3786 log_warning("Warning: unit files do not carry install information. No operation executed.");
3790 dbus_message_unref(m);
3793 dbus_message_unref(reply);
3795 unit_file_changes_free(changes, n_changes);
3797 dbus_error_free(&error);
3801 static int unit_is_enabled(DBusConnection *bus, char **args) {
3804 DBusMessage *m = NULL, *reply = NULL;
3808 dbus_error_init(&error);
3810 r = enable_sysv_units(args);
3816 if (!bus || avoid_bus()) {
3818 STRV_FOREACH(name, args+1) {
3819 UnitFileState state;
3821 state = unit_file_get_state(arg_scope, arg_root, *name);
3827 if (state == UNIT_FILE_ENABLED ||
3828 state == UNIT_FILE_ENABLED_RUNTIME ||
3829 state == UNIT_FILE_STATIC)
3833 puts(unit_file_state_to_string(state));
3837 STRV_FOREACH(name, args+1) {
3840 m = dbus_message_new_method_call(
3841 "org.freedesktop.systemd1",
3842 "/org/freedesktop/systemd1",
3843 "org.freedesktop.systemd1.Manager",
3844 "GetUnitFileState");
3846 log_error("Out of memory");
3851 if (!dbus_message_append_args(m,
3852 DBUS_TYPE_STRING, name,
3853 DBUS_TYPE_INVALID)) {
3854 log_error("Could not append arguments to message.");
3859 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
3861 log_error("Failed to issue method call: %s", bus_error_message(&error));
3866 if (!dbus_message_get_args(reply, &error,
3867 DBUS_TYPE_STRING, &s,
3868 DBUS_TYPE_INVALID)) {
3869 log_error("Failed to parse reply: %s", bus_error_message(&error));
3874 dbus_message_unref(m);
3875 dbus_message_unref(reply);
3878 if (streq(s, "enabled") ||
3879 streq(s, "enabled-runtime") ||
3888 r = enabled ? 0 : 1;
3892 dbus_message_unref(m);
3895 dbus_message_unref(reply);
3897 dbus_error_free(&error);
3901 static int systemctl_help(void) {
3903 pager_open_if_enabled();
3905 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
3906 "Query or send control commands to the systemd manager.\n\n"
3907 " -h --help Show this help\n"
3908 " --version Show package version\n"
3909 " -t --type=TYPE List only units of a particular type\n"
3910 " -p --property=NAME Show only properties by this name\n"
3911 " -a --all Show all units/properties, including dead/empty ones\n"
3912 " --failed Show only failed units\n"
3913 " --full Don't ellipsize unit names on output\n"
3914 " --fail When queueing a new job, fail if conflicting jobs are\n"
3916 " --ignore-dependencies\n"
3917 " When queueing a new job, ignore all its dependencies\n"
3918 " --kill-who=WHO Who to send signal to\n"
3919 " -s --signal=SIGNAL Which signal to send\n"
3920 " -H --host=[USER@]HOST\n"
3921 " Show information for remote host\n"
3922 " -P --privileged Acquire privileges before execution\n"
3923 " -q --quiet Suppress output\n"
3924 " --no-block Do not wait until operation finished\n"
3925 " --no-wall Don't send wall message before halt/power-off/reboot\n"
3926 " --no-reload When enabling/disabling unit files, don't reload daemon\n"
3928 " --no-legend Do not print a legend (column headers and hints)\n"
3929 " --no-pager Do not pipe output into a pager\n"
3930 " --no-ask-password\n"
3931 " Do not ask for system passwords\n"
3932 " --order When generating graph for dot, show only order\n"
3933 " --require When generating graph for dot, show only requirement\n"
3934 " --system Connect to system manager\n"
3935 " --user Connect to user service manager\n"
3936 " --global Enable/disable unit files globally\n"
3937 " -f --force When enabling unit files, override existing symlinks\n"
3938 " When shutting down, execute action immediately\n"
3939 " --root=PATH Enable unit files in the specified root directory\n"
3940 " --runtime Enable unit files only temporarily until next reboot\n\n"
3942 " list-units List loaded units\n"
3943 " start [NAME...] Start (activate) one or more units\n"
3944 " stop [NAME...] Stop (deactivate) one or more units\n"
3945 " reload [NAME...] Reload one or more units\n"
3946 " restart [NAME...] Start or restart one or more units\n"
3947 " try-restart [NAME...] Restart one or more units if active\n"
3948 " reload-or-restart [NAME...] Reload one or more units is possible,\n"
3949 " otherwise start or restart\n"
3950 " reload-or-try-restart [NAME...] Reload one or more units is possible,\n"
3951 " otherwise restart if active\n"
3952 " isolate [NAME] Start one unit and stop all others\n"
3953 " kill [NAME...] Send signal to processes of a unit\n"
3954 " is-active [NAME...] Check whether units are active\n"
3955 " status [NAME...|PID...] Show runtime status of one or more units\n"
3956 " show [NAME...|JOB...] Show properties of one or more\n"
3957 " units/jobs or the manager\n"
3958 " reset-failed [NAME...] Reset failed state for all, one, or more\n"
3960 " load [NAME...] Load one or more units\n\n"
3961 "Unit File Commands:\n"
3962 " list-unit-files List installed unit files\n"
3963 " enable [NAME...] Enable one or more unit files\n"
3964 " disable [NAME...] Disable one or more unit files\n"
3965 " reenable [NAME...] Reenable one or more unit files\n"
3966 " preset [NAME...] Enable/disable one or more unit files\n"
3967 " based on preset configuration\n"
3968 " mask [NAME...] Mask one or more units\n"
3969 " unmask [NAME...] Unmask one or more units\n"
3970 " link [PATH...] Link one or more units files into\n"
3971 " the search path\n"
3972 " is-enabled [NAME...] Check whether unit files are enabled\n\n"
3974 " list-jobs List jobs\n"
3975 " cancel [JOB...] Cancel all, one, or more jobs\n\n"
3976 "Status Commands:\n"
3977 " dump Dump server status\n"
3978 " dot Dump dependency graph for dot(1)\n\n"
3979 "Snapshot Commands:\n"
3980 " snapshot [NAME] Create a snapshot\n"
3981 " delete [NAME...] Remove one or more snapshots\n\n"
3982 "Environment Commands:\n"
3983 " show-environment Dump environment\n"
3984 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
3985 " unset-environment [NAME...] Unset one or more environment variables\n\n"
3986 "Manager Lifecycle Commands:\n"
3987 " daemon-reload Reload systemd manager configuration\n"
3988 " daemon-reexec Reexecute systemd manager\n\n"
3989 "System Commands:\n"
3990 " default Enter system default mode\n"
3991 " rescue Enter system rescue mode\n"
3992 " emergency Enter system emergency mode\n"
3993 " halt Shut down and halt the system\n"
3994 " poweroff Shut down and power-off the system\n"
3995 " reboot Shut down and reboot the system\n"
3996 " kexec Shut down and reboot the system with kexec\n"
3997 " exit Ask for user instance termination\n",
3998 program_invocation_short_name);
4003 static int halt_help(void) {
4005 printf("%s [OPTIONS...]\n\n"
4006 "%s the system.\n\n"
4007 " --help Show this help\n"
4008 " --halt Halt the machine\n"
4009 " -p --poweroff Switch off the machine\n"
4010 " --reboot Reboot the machine\n"
4011 " -f --force Force immediate halt/power-off/reboot\n"
4012 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
4013 " -d --no-wtmp Don't write wtmp record\n"
4014 " -n --no-sync Don't sync before halt/power-off/reboot\n"
4015 " --no-wall Don't send wall message before halt/power-off/reboot\n",
4016 program_invocation_short_name,
4017 arg_action == ACTION_REBOOT ? "Reboot" :
4018 arg_action == ACTION_POWEROFF ? "Power off" :
4024 static int shutdown_help(void) {
4026 printf("%s [OPTIONS...] [TIME] [WALL...]\n\n"
4027 "Shut down the system.\n\n"
4028 " --help Show this help\n"
4029 " -H --halt Halt the machine\n"
4030 " -P --poweroff Power-off the machine\n"
4031 " -r --reboot Reboot the machine\n"
4032 " -h Equivalent to --poweroff, overriden by --halt\n"
4033 " -k Don't halt/power-off/reboot, just send warnings\n"
4034 " --no-wall Don't send wall message before halt/power-off/reboot\n"
4035 " -c Cancel a pending shutdown\n",
4036 program_invocation_short_name);
4041 static int telinit_help(void) {
4043 printf("%s [OPTIONS...] {COMMAND}\n\n"
4044 "Send control commands to the init daemon.\n\n"
4045 " --help Show this help\n"
4046 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
4048 " 0 Power-off the machine\n"
4049 " 6 Reboot the machine\n"
4050 " 2, 3, 4, 5 Start runlevelX.target unit\n"
4051 " 1, s, S Enter rescue mode\n"
4052 " q, Q Reload init daemon configuration\n"
4053 " u, U Reexecute init daemon\n",
4054 program_invocation_short_name);
4059 static int runlevel_help(void) {
4061 printf("%s [OPTIONS...]\n\n"
4062 "Prints the previous and current runlevel of the init system.\n\n"
4063 " --help Show this help\n",
4064 program_invocation_short_name);
4069 static int systemctl_parse_argv(int argc, char *argv[]) {
4073 ARG_IGNORE_DEPENDENCIES,
4089 ARG_NO_ASK_PASSWORD,
4094 static const struct option options[] = {
4095 { "help", no_argument, NULL, 'h' },
4096 { "version", no_argument, NULL, ARG_VERSION },
4097 { "type", required_argument, NULL, 't' },
4098 { "property", required_argument, NULL, 'p' },
4099 { "all", no_argument, NULL, 'a' },
4100 { "failed", no_argument, NULL, ARG_FAILED },
4101 { "full", no_argument, NULL, ARG_FULL },
4102 { "fail", no_argument, NULL, ARG_FAIL },
4103 { "ignore-dependencies", no_argument, NULL, ARG_IGNORE_DEPENDENCIES },
4104 { "user", no_argument, NULL, ARG_USER },
4105 { "system", no_argument, NULL, ARG_SYSTEM },
4106 { "global", no_argument, NULL, ARG_GLOBAL },
4107 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
4108 { "no-legend", no_argument, NULL, ARG_NO_LEGEND },
4109 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
4110 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4111 { "quiet", no_argument, NULL, 'q' },
4112 { "order", no_argument, NULL, ARG_ORDER },
4113 { "require", no_argument, NULL, ARG_REQUIRE },
4114 { "root", required_argument, NULL, ARG_ROOT },
4115 { "force", no_argument, NULL, 'f' },
4116 { "no-reload", no_argument, NULL, ARG_NO_RELOAD },
4117 { "kill-mode", required_argument, NULL, ARG_KILL_MODE }, /* undocumented on purpose */
4118 { "kill-who", required_argument, NULL, ARG_KILL_WHO },
4119 { "signal", required_argument, NULL, 's' },
4120 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
4121 { "host", required_argument, NULL, 'H' },
4122 { "privileged",no_argument, NULL, 'P' },
4123 { "runtime", no_argument, NULL, ARG_RUNTIME },
4124 { NULL, 0, NULL, 0 }
4132 /* Only when running as systemctl we ask for passwords */
4133 arg_ask_password = true;
4135 while ((c = getopt_long(argc, argv, "ht:p:aqfs:H:P", options, NULL)) >= 0) {
4144 puts(PACKAGE_STRING);
4146 puts(SYSTEMD_FEATURES);
4156 if (!(l = strv_append(arg_property, optarg)))
4159 strv_free(arg_property);
4162 /* If the user asked for a particular
4163 * property, show it to him, even if it is
4174 arg_job_mode = "fail";
4177 case ARG_IGNORE_DEPENDENCIES:
4178 arg_job_mode = "ignore-dependencies";
4182 arg_scope = UNIT_FILE_USER;
4186 arg_scope = UNIT_FILE_SYSTEM;
4190 arg_scope = UNIT_FILE_GLOBAL;
4194 arg_no_block = true;
4198 arg_no_legend = true;
4202 arg_no_pager = true;
4210 arg_dot = DOT_ORDER;
4214 arg_dot = DOT_REQUIRE;
4238 arg_no_reload = true;
4242 arg_kill_who = optarg;
4246 arg_kill_mode = optarg;
4250 if ((arg_signal = signal_from_string_try_harder(optarg)) < 0) {
4251 log_error("Failed to parse signal string %s.", optarg);
4256 case ARG_NO_ASK_PASSWORD:
4257 arg_ask_password = false;
4261 arg_transport = TRANSPORT_POLKIT;
4265 arg_transport = TRANSPORT_SSH;
4277 log_error("Unknown option code %c", c);
4282 if (arg_transport != TRANSPORT_NORMAL && arg_scope != UNIT_FILE_SYSTEM) {
4283 log_error("Cannot access user instance remotely.");
4290 static int halt_parse_argv(int argc, char *argv[]) {
4299 static const struct option options[] = {
4300 { "help", no_argument, NULL, ARG_HELP },
4301 { "halt", no_argument, NULL, ARG_HALT },
4302 { "poweroff", no_argument, NULL, 'p' },
4303 { "reboot", no_argument, NULL, ARG_REBOOT },
4304 { "force", no_argument, NULL, 'f' },
4305 { "wtmp-only", no_argument, NULL, 'w' },
4306 { "no-wtmp", no_argument, NULL, 'd' },
4307 { "no-sync", no_argument, NULL, 'n' },
4308 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4309 { NULL, 0, NULL, 0 }
4317 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
4318 if (runlevel == '0' || runlevel == '6')
4319 arg_immediate = true;
4321 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
4329 arg_action = ACTION_HALT;
4333 if (arg_action != ACTION_REBOOT)
4334 arg_action = ACTION_POWEROFF;
4338 arg_action = ACTION_REBOOT;
4342 arg_immediate = true;
4363 /* Compatibility nops */
4370 log_error("Unknown option code %c", c);
4375 if (optind < argc) {
4376 log_error("Too many arguments.");
4383 static int parse_time_spec(const char *t, usec_t *_u) {
4387 if (streq(t, "now"))
4389 else if (!strchr(t, ':')) {
4392 if (safe_atou64(t, &u) < 0)
4395 *_u = now(CLOCK_REALTIME) + USEC_PER_MINUTE * u;
4404 hour = strtol(t, &e, 10);
4405 if (errno != 0 || *e != ':' || hour < 0 || hour > 23)
4408 minute = strtol(e+1, &e, 10);
4409 if (errno != 0 || *e != 0 || minute < 0 || minute > 59)
4412 n = now(CLOCK_REALTIME);
4413 s = (time_t) (n / USEC_PER_SEC);
4416 assert_se(localtime_r(&s, &tm));
4418 tm.tm_hour = (int) hour;
4419 tm.tm_min = (int) minute;
4422 assert_se(s = mktime(&tm));
4424 *_u = (usec_t) s * USEC_PER_SEC;
4427 *_u += USEC_PER_DAY;
4433 static int shutdown_parse_argv(int argc, char *argv[]) {
4440 static const struct option options[] = {
4441 { "help", no_argument, NULL, ARG_HELP },
4442 { "halt", no_argument, NULL, 'H' },
4443 { "poweroff", no_argument, NULL, 'P' },
4444 { "reboot", no_argument, NULL, 'r' },
4445 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4446 { NULL, 0, NULL, 0 }
4454 while ((c = getopt_long(argc, argv, "HPrhkt:afFc", options, NULL)) >= 0) {
4462 arg_action = ACTION_HALT;
4466 arg_action = ACTION_POWEROFF;
4471 arg_action = ACTION_KEXEC;
4473 arg_action = ACTION_REBOOT;
4477 if (arg_action != ACTION_HALT)
4478 arg_action = ACTION_POWEROFF;
4491 /* Compatibility nops */
4495 arg_action = ACTION_CANCEL_SHUTDOWN;
4502 log_error("Unknown option code %c", c);
4507 if (argc > optind) {
4508 if ((r = parse_time_spec(argv[optind], &arg_when)) < 0) {
4509 log_error("Failed to parse time specification: %s", argv[optind]);
4513 arg_when = now(CLOCK_REALTIME) + USEC_PER_MINUTE;
4515 /* We skip the time argument */
4516 if (argc > optind + 1)
4517 arg_wall = argv + optind + 1;
4524 static int telinit_parse_argv(int argc, char *argv[]) {
4531 static const struct option options[] = {
4532 { "help", no_argument, NULL, ARG_HELP },
4533 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4534 { NULL, 0, NULL, 0 }
4537 static const struct {
4541 { '0', ACTION_POWEROFF },
4542 { '6', ACTION_REBOOT },
4543 { '1', ACTION_RESCUE },
4544 { '2', ACTION_RUNLEVEL2 },
4545 { '3', ACTION_RUNLEVEL3 },
4546 { '4', ACTION_RUNLEVEL4 },
4547 { '5', ACTION_RUNLEVEL5 },
4548 { 's', ACTION_RESCUE },
4549 { 'S', ACTION_RESCUE },
4550 { 'q', ACTION_RELOAD },
4551 { 'Q', ACTION_RELOAD },
4552 { 'u', ACTION_REEXEC },
4553 { 'U', ACTION_REEXEC }
4562 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4577 log_error("Unknown option code %c", c);
4582 if (optind >= argc) {
4587 if (optind + 1 < argc) {
4588 log_error("Too many arguments.");
4592 if (strlen(argv[optind]) != 1) {
4593 log_error("Expected single character argument.");
4597 for (i = 0; i < ELEMENTSOF(table); i++)
4598 if (table[i].from == argv[optind][0])
4601 if (i >= ELEMENTSOF(table)) {
4602 log_error("Unknown command %s.", argv[optind]);
4606 arg_action = table[i].to;
4613 static int runlevel_parse_argv(int argc, char *argv[]) {
4619 static const struct option options[] = {
4620 { "help", no_argument, NULL, ARG_HELP },
4621 { NULL, 0, NULL, 0 }
4629 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4640 log_error("Unknown option code %c", c);
4645 if (optind < argc) {
4646 log_error("Too many arguments.");
4653 static int parse_argv(int argc, char *argv[]) {
4657 if (program_invocation_short_name) {
4659 if (strstr(program_invocation_short_name, "halt")) {
4660 arg_action = ACTION_HALT;
4661 return halt_parse_argv(argc, argv);
4662 } else if (strstr(program_invocation_short_name, "poweroff")) {
4663 arg_action = ACTION_POWEROFF;
4664 return halt_parse_argv(argc, argv);
4665 } else if (strstr(program_invocation_short_name, "reboot")) {
4667 arg_action = ACTION_KEXEC;
4669 arg_action = ACTION_REBOOT;
4670 return halt_parse_argv(argc, argv);
4671 } else if (strstr(program_invocation_short_name, "shutdown")) {
4672 arg_action = ACTION_POWEROFF;
4673 return shutdown_parse_argv(argc, argv);
4674 } else if (strstr(program_invocation_short_name, "init")) {
4676 if (sd_booted() > 0) {
4677 arg_action = ACTION_INVALID;
4678 return telinit_parse_argv(argc, argv);
4680 /* Hmm, so some other init system is
4681 * running, we need to forward this
4682 * request to it. For now we simply
4683 * guess that it is Upstart. */
4685 execv("/lib/upstart/telinit", argv);
4687 log_error("Couldn't find an alternative telinit implementation to spawn.");
4691 } else if (strstr(program_invocation_short_name, "runlevel")) {
4692 arg_action = ACTION_RUNLEVEL;
4693 return runlevel_parse_argv(argc, argv);
4697 arg_action = ACTION_SYSTEMCTL;
4698 return systemctl_parse_argv(argc, argv);
4701 static int action_to_runlevel(void) {
4703 static const char table[_ACTION_MAX] = {
4704 [ACTION_HALT] = '0',
4705 [ACTION_POWEROFF] = '0',
4706 [ACTION_REBOOT] = '6',
4707 [ACTION_RUNLEVEL2] = '2',
4708 [ACTION_RUNLEVEL3] = '3',
4709 [ACTION_RUNLEVEL4] = '4',
4710 [ACTION_RUNLEVEL5] = '5',
4711 [ACTION_RESCUE] = '1'
4714 assert(arg_action < _ACTION_MAX);
4716 return table[arg_action];
4719 static int talk_upstart(void) {
4720 DBusMessage *m = NULL, *reply = NULL;
4722 int previous, rl, r;
4724 env1_buf[] = "RUNLEVEL=X",
4725 env2_buf[] = "PREVLEVEL=X";
4726 char *env1 = env1_buf, *env2 = env2_buf;
4727 const char *emit = "runlevel";
4728 dbus_bool_t b_false = FALSE;
4729 DBusMessageIter iter, sub;
4730 DBusConnection *bus;
4732 dbus_error_init(&error);
4734 if (!(rl = action_to_runlevel()))
4737 if (utmp_get_runlevel(&previous, NULL) < 0)
4740 if (!(bus = dbus_connection_open_private("unix:abstract=/com/ubuntu/upstart", &error))) {
4741 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
4746 log_error("Failed to connect to Upstart bus: %s", bus_error_message(&error));
4751 if ((r = bus_check_peercred(bus)) < 0) {
4752 log_error("Failed to verify owner of bus.");
4756 if (!(m = dbus_message_new_method_call(
4757 "com.ubuntu.Upstart",
4758 "/com/ubuntu/Upstart",
4759 "com.ubuntu.Upstart0_6",
4762 log_error("Could not allocate message.");
4767 dbus_message_iter_init_append(m, &iter);
4769 env1_buf[sizeof(env1_buf)-2] = rl;
4770 env2_buf[sizeof(env2_buf)-2] = previous;
4772 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
4773 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
4774 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
4775 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
4776 !dbus_message_iter_close_container(&iter, &sub) ||
4777 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
4778 log_error("Could not append arguments to message.");
4783 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
4785 if (error_is_no_service(&error)) {
4790 log_error("Failed to issue method call: %s", bus_error_message(&error));
4799 dbus_message_unref(m);
4802 dbus_message_unref(reply);
4805 dbus_connection_flush(bus);
4806 dbus_connection_close(bus);
4807 dbus_connection_unref(bus);
4810 dbus_error_free(&error);
4815 static int talk_initctl(void) {
4816 struct init_request request;
4820 if (!(rl = action_to_runlevel()))
4824 request.magic = INIT_MAGIC;
4825 request.sleeptime = 0;
4826 request.cmd = INIT_CMD_RUNLVL;
4827 request.runlevel = rl;
4829 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
4831 if (errno == ENOENT)
4834 log_error("Failed to open "INIT_FIFO": %m");
4839 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
4840 close_nointr_nofail(fd);
4843 log_error("Failed to write to "INIT_FIFO": %m");
4844 return errno ? -errno : -EIO;
4850 static int systemctl_main(DBusConnection *bus, int argc, char *argv[], DBusError *error) {
4852 static const struct {
4860 int (* const dispatch)(DBusConnection *bus, char **args);
4862 { "list-units", LESS, 1, list_units },
4863 { "list-unit-files", EQUAL, 1, list_unit_files },
4864 { "list-jobs", EQUAL, 1, list_jobs },
4865 { "clear-jobs", EQUAL, 1, daemon_reload },
4866 { "load", MORE, 2, load_unit },
4867 { "cancel", MORE, 2, cancel_job },
4868 { "start", MORE, 2, start_unit },
4869 { "stop", MORE, 2, start_unit },
4870 { "condstop", MORE, 2, start_unit }, /* For compatibility with ALTLinux */
4871 { "reload", MORE, 2, start_unit },
4872 { "restart", MORE, 2, start_unit },
4873 { "try-restart", MORE, 2, start_unit },
4874 { "reload-or-restart", MORE, 2, start_unit },
4875 { "reload-or-try-restart", MORE, 2, start_unit },
4876 { "force-reload", MORE, 2, start_unit }, /* For compatibility with SysV */
4877 { "condreload", MORE, 2, start_unit }, /* For compatibility with ALTLinux */
4878 { "condrestart", MORE, 2, start_unit }, /* For compatibility with RH */
4879 { "isolate", EQUAL, 2, start_unit },
4880 { "kill", MORE, 2, kill_unit },
4881 { "is-active", MORE, 2, check_unit },
4882 { "check", MORE, 2, check_unit },
4883 { "show", MORE, 1, show },
4884 { "status", MORE, 2, show },
4885 { "dump", EQUAL, 1, dump },
4886 { "dot", EQUAL, 1, dot },
4887 { "snapshot", LESS, 2, snapshot },
4888 { "delete", MORE, 2, delete_snapshot },
4889 { "daemon-reload", EQUAL, 1, daemon_reload },
4890 { "daemon-reexec", EQUAL, 1, daemon_reload },
4891 { "show-environment", EQUAL, 1, show_enviroment },
4892 { "set-environment", MORE, 2, set_environment },
4893 { "unset-environment", MORE, 2, set_environment },
4894 { "halt", EQUAL, 1, start_special },
4895 { "poweroff", EQUAL, 1, start_special },
4896 { "reboot", EQUAL, 1, start_special },
4897 { "kexec", EQUAL, 1, start_special },
4898 { "default", EQUAL, 1, start_special },
4899 { "rescue", EQUAL, 1, start_special },
4900 { "emergency", EQUAL, 1, start_special },
4901 { "exit", EQUAL, 1, start_special },
4902 { "reset-failed", MORE, 1, reset_failed },
4903 { "enable", MORE, 2, enable_unit },
4904 { "disable", MORE, 2, enable_unit },
4905 { "is-enabled", MORE, 2, unit_is_enabled },
4906 { "reenable", MORE, 2, enable_unit },
4907 { "preset", MORE, 2, enable_unit },
4908 { "mask", MORE, 2, enable_unit },
4909 { "unmask", MORE, 2, enable_unit },
4910 { "link", MORE, 2, enable_unit }
4920 left = argc - optind;
4923 /* Special rule: no arguments means "list-units" */
4926 if (streq(argv[optind], "help")) {
4931 for (i = 0; i < ELEMENTSOF(verbs); i++)
4932 if (streq(argv[optind], verbs[i].verb))
4935 if (i >= ELEMENTSOF(verbs)) {
4936 log_error("Unknown operation %s", argv[optind]);
4941 switch (verbs[i].argc_cmp) {
4944 if (left != verbs[i].argc) {
4945 log_error("Invalid number of arguments.");
4952 if (left < verbs[i].argc) {
4953 log_error("Too few arguments.");
4960 if (left > verbs[i].argc) {
4961 log_error("Too many arguments.");
4968 assert_not_reached("Unknown comparison operator.");
4971 /* Require a bus connection for all operations but
4973 if (!streq(verbs[i].verb, "enable") &&
4974 !streq(verbs[i].verb, "disable") &&
4975 !streq(verbs[i].verb, "is-enable") &&
4976 !streq(verbs[i].verb, "reenable") &&
4977 !streq(verbs[i].verb, "preset") &&
4978 !streq(verbs[i].verb, "mask") &&
4979 !streq(verbs[i].verb, "unmask") &&
4980 !streq(verbs[i].verb, "link")) {
4982 if (running_in_chroot() > 0) {
4983 log_info("Running in chroot, ignoring request.");
4987 if (!bus && !avoid_bus()) {
4988 log_error("Failed to get D-Bus connection: %s", error->message);
4993 return verbs[i].dispatch(bus, argv + optind);
4996 static int send_shutdownd(usec_t t, char mode, bool dry_run, bool warn, const char *message) {
4998 struct msghdr msghdr;
5000 union sockaddr_union sockaddr;
5001 struct shutdownd_command c;
5006 c.dry_run = dry_run;
5010 strncpy(c.wall_message, message, sizeof(c.wall_message));
5012 if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0)
5016 sockaddr.sa.sa_family = AF_UNIX;
5017 sockaddr.un.sun_path[0] = 0;
5018 strncpy(sockaddr.un.sun_path, "/run/systemd/shutdownd", sizeof(sockaddr.un.sun_path));
5021 iovec.iov_base = (char*) &c;
5022 iovec.iov_len = sizeof(c);
5025 msghdr.msg_name = &sockaddr;
5026 msghdr.msg_namelen = offsetof(struct sockaddr_un, sun_path) + sizeof("/run/systemd/shutdownd") - 1;
5028 msghdr.msg_iov = &iovec;
5029 msghdr.msg_iovlen = 1;
5031 if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) < 0) {
5032 close_nointr_nofail(fd);
5036 close_nointr_nofail(fd);
5040 static int reload_with_fallback(DBusConnection *bus) {
5043 /* First, try systemd via D-Bus. */
5044 if (daemon_reload(bus, NULL) >= 0)
5048 /* Nothing else worked, so let's try signals */
5049 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
5051 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
5052 log_error("kill() failed: %m");
5059 static int start_with_fallback(DBusConnection *bus) {
5062 /* First, try systemd via D-Bus. */
5063 if (start_unit(bus, NULL) >= 0)
5067 /* Hmm, talking to systemd via D-Bus didn't work. Then
5068 * let's try to talk to Upstart via D-Bus. */
5069 if (talk_upstart() > 0)
5072 /* Nothing else worked, so let's try
5074 if (talk_initctl() > 0)
5077 log_error("Failed to talk to init daemon.");
5081 warn_wall(arg_action);
5085 static int halt_main(DBusConnection *bus) {
5088 if (geteuid() != 0) {
5089 log_error("Must be root.");
5095 char date[FORMAT_TIMESTAMP_MAX];
5097 m = strv_join(arg_wall, " ");
5098 r = send_shutdownd(arg_when,
5099 arg_action == ACTION_HALT ? 'H' :
5100 arg_action == ACTION_POWEROFF ? 'P' :
5108 log_warning("Failed to talk to shutdownd, proceeding with immediate shutdown: %s", strerror(-r));
5110 log_info("Shutdown scheduled for %s, use 'shutdown -c' to cancel.",
5111 format_timestamp(date, sizeof(date), arg_when));
5116 if (!arg_dry && !arg_immediate)
5117 return start_with_fallback(bus);
5120 if (sd_booted() > 0)
5121 log_debug("Not writing utmp record, assuming that systemd-update-utmp is used.");
5122 else if ((r = utmp_put_shutdown(0)) < 0)
5123 log_warning("Failed to write utmp record: %s", strerror(-r));
5132 /* Make sure C-A-D is handled by the kernel from this
5134 reboot(RB_ENABLE_CAD);
5136 switch (arg_action) {
5139 log_info("Halting.");
5140 reboot(RB_HALT_SYSTEM);
5143 case ACTION_POWEROFF:
5144 log_info("Powering off.");
5145 reboot(RB_POWER_OFF);
5149 log_info("Rebooting.");
5150 reboot(RB_AUTOBOOT);
5154 assert_not_reached("Unknown halt action.");
5157 /* We should never reach this. */
5161 static int runlevel_main(void) {
5162 int r, runlevel, previous;
5164 r = utmp_get_runlevel(&runlevel, &previous);
5171 previous <= 0 ? 'N' : previous,
5172 runlevel <= 0 ? 'N' : runlevel);
5177 int main(int argc, char*argv[]) {
5178 int r, retval = EXIT_FAILURE;
5179 DBusConnection *bus = NULL;
5182 dbus_error_init(&error);
5184 log_parse_environment();
5187 if ((r = parse_argv(argc, argv)) < 0)
5190 retval = EXIT_SUCCESS;
5194 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
5195 * let's shortcut this */
5196 if (arg_action == ACTION_RUNLEVEL) {
5197 r = runlevel_main();
5198 retval = r < 0 ? EXIT_FAILURE : r;
5202 if (running_in_chroot() > 0 && arg_action != ACTION_SYSTEMCTL) {
5203 log_info("Running in chroot, ignoring request.");
5209 if (arg_transport == TRANSPORT_NORMAL)
5210 bus_connect(arg_scope == UNIT_FILE_SYSTEM ? DBUS_BUS_SYSTEM : DBUS_BUS_SESSION, &bus, &private_bus, &error);
5211 else if (arg_transport == TRANSPORT_POLKIT) {
5212 bus_connect_system_polkit(&bus, &error);
5213 private_bus = false;
5214 } else if (arg_transport == TRANSPORT_SSH) {
5215 bus_connect_system_ssh(NULL, arg_host, &bus, &error);
5216 private_bus = false;
5218 assert_not_reached("Uh, invalid transport...");
5221 switch (arg_action) {
5223 case ACTION_SYSTEMCTL:
5224 r = systemctl_main(bus, argc, argv, &error);
5228 case ACTION_POWEROFF:
5234 case ACTION_RUNLEVEL2:
5235 case ACTION_RUNLEVEL3:
5236 case ACTION_RUNLEVEL4:
5237 case ACTION_RUNLEVEL5:
5239 case ACTION_EMERGENCY:
5240 case ACTION_DEFAULT:
5241 r = start_with_fallback(bus);
5246 r = reload_with_fallback(bus);
5249 case ACTION_CANCEL_SHUTDOWN:
5250 r = send_shutdownd(0, 0, false, false, NULL);
5253 case ACTION_INVALID:
5254 case ACTION_RUNLEVEL:
5256 assert_not_reached("Unknown action");
5259 retval = r < 0 ? EXIT_FAILURE : r;
5263 dbus_connection_flush(bus);
5264 dbus_connection_close(bus);
5265 dbus_connection_unref(bus);
5268 dbus_error_free(&error);
5272 strv_free(arg_property);