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>
37 #include <dbus/dbus.h>
43 #include "utmp-wtmp.h"
47 #include "dbus-common.h"
48 #include "cgroup-show.h"
49 #include "cgroup-util.h"
51 #include "path-lookup.h"
52 #include "conf-parser.h"
53 #include "sd-daemon.h"
54 #include "shutdownd.h"
55 #include "exit-status.h"
56 #include "bus-errors.h"
58 #include "unit-name.h"
60 static const char *arg_type = NULL;
61 static char **arg_property = NULL;
62 static bool arg_all = false;
63 static bool arg_fail = false;
64 static bool arg_user = false;
65 static bool arg_global = false;
66 static bool arg_immediate = false;
67 static bool arg_no_block = false;
68 static bool arg_no_pager = false;
69 static bool arg_no_wtmp = false;
70 static bool arg_no_sync = false;
71 static bool arg_no_wall = false;
72 static bool arg_no_reload = false;
73 static bool arg_dry = false;
74 static bool arg_quiet = false;
75 static bool arg_full = false;
76 static bool arg_force = false;
77 static bool arg_defaults = false;
78 static bool arg_ask_password = false;
79 static char **arg_wall = NULL;
80 static const char *arg_kill_who = NULL;
81 static const char *arg_kill_mode = NULL;
82 static int arg_signal = SIGTERM;
83 static usec_t arg_when = 0;
102 ACTION_CANCEL_SHUTDOWN,
104 } arg_action = ACTION_SYSTEMCTL;
111 static bool private_bus = false;
113 static pid_t pager_pid = 0;
115 static int daemon_reload(DBusConnection *bus, char **args, unsigned n);
116 static void pager_open(void);
118 static bool on_tty(void) {
121 if (_unlikely_(t < 0))
122 t = isatty(STDOUT_FILENO) > 0;
127 static void spawn_ask_password_agent(void) {
130 /* We check STDIN here, not STDOUT, since this is about input,
132 if (!isatty(STDIN_FILENO))
135 if (!arg_ask_password)
140 /* Spawns a temporary TTY agent, making sure it goes away when
143 if ((child = fork()) < 0)
149 const char * const args[] = {
150 SYSTEMD_TTY_ASK_PASSWORD_AGENT_BINARY_PATH,
155 if (prctl(PR_SET_PDEATHSIG, SIGTERM) < 0)
158 /* Check whether our parent died before we were able
159 * to set the death signal */
160 if (getppid() != parent)
163 execv(args[0], (char **) args);
168 static const char *ansi_highlight(bool b) {
173 return b ? ANSI_HIGHLIGHT_ON : ANSI_HIGHLIGHT_OFF;
176 static const char *ansi_highlight_green(bool b) {
181 return b ? ANSI_HIGHLIGHT_GREEN_ON : ANSI_HIGHLIGHT_OFF;
184 static bool error_is_no_service(const DBusError *error) {
187 if (!dbus_error_is_set(error))
190 if (dbus_error_has_name(error, DBUS_ERROR_NAME_HAS_NO_OWNER))
193 if (dbus_error_has_name(error, DBUS_ERROR_SERVICE_UNKNOWN))
196 return startswith(error->name, "org.freedesktop.DBus.Error.Spawn.");
199 static int translate_bus_error_to_exit_status(int r, const DBusError *error) {
202 if (!dbus_error_is_set(error))
205 if (dbus_error_has_name(error, DBUS_ERROR_ACCESS_DENIED) ||
206 dbus_error_has_name(error, BUS_ERROR_ONLY_BY_DEPENDENCY) ||
207 dbus_error_has_name(error, BUS_ERROR_NO_ISOLATION) ||
208 dbus_error_has_name(error, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE))
209 return EXIT_NOPERMISSION;
211 if (dbus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT))
212 return EXIT_NOTINSTALLED;
214 if (dbus_error_has_name(error, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE) ||
215 dbus_error_has_name(error, BUS_ERROR_NOT_SUPPORTED))
216 return EXIT_NOTIMPLEMENTED;
218 if (dbus_error_has_name(error, BUS_ERROR_LOAD_FAILED))
219 return EXIT_NOTCONFIGURED;
227 static int bus_iter_get_basic_and_next(DBusMessageIter *iter, int type, void *data, bool next) {
232 if (dbus_message_iter_get_arg_type(iter) != type)
235 dbus_message_iter_get_basic(iter, data);
237 if (!dbus_message_iter_next(iter) != !next)
243 static void warn_wall(enum action action) {
244 static const char *table[_ACTION_MAX] = {
245 [ACTION_HALT] = "The system is going down for system halt NOW!",
246 [ACTION_REBOOT] = "The system is going down for reboot NOW!",
247 [ACTION_POWEROFF] = "The system is going down for power-off NOW!",
248 [ACTION_KEXEC] = "The system is going down for kexec reboot NOW!",
249 [ACTION_RESCUE] = "The system is going down to rescue mode NOW!",
250 [ACTION_EMERGENCY] = "The system is going down to emergency mode NOW!"
259 if (!(p = strv_join(arg_wall, " "))) {
260 log_error("Failed to join strings.");
276 utmp_wall(table[action], NULL);
281 const char *description;
282 const char *load_state;
283 const char *active_state;
284 const char *sub_state;
285 const char *following;
286 const char *unit_path;
288 const char *job_type;
289 const char *job_path;
292 static int compare_unit_info(const void *a, const void *b) {
294 const struct unit_info *u = a, *v = b;
296 d1 = strrchr(u->id, '.');
297 d2 = strrchr(v->id, '.');
302 if ((r = strcasecmp(d1, d2)) != 0)
306 return strcasecmp(u->id, v->id);
309 static bool output_show_job(const struct unit_info *u) {
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_job(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));
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)
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_job(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, "");
392 printf(" %s", u->description);
394 printf(" %.*s", columns() - a - b - 1, u->description);
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, unsigned n) {
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);
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);
499 qsort(unit_infos, c, sizeof(struct unit_info), compare_unit_info);
500 output_units_list(unit_infos, c);
506 dbus_message_unref(m);
509 dbus_message_unref(reply);
513 dbus_error_free(&error);
518 static int dot_one_property(const char *name, const char *prop, DBusMessageIter *iter) {
519 static const char * const colors[] = {
520 "Requires", "[color=\"black\"]",
521 "RequiresOverridable", "[color=\"black\"]",
522 "Requisite", "[color=\"darkblue\"]",
523 "RequisiteOverridable", "[color=\"darkblue\"]",
524 "Wants", "[color=\"darkgrey\"]",
525 "Conflicts", "[color=\"red\"]",
526 "ConflictedBy", "[color=\"red\"]",
527 "After", "[color=\"green\"]"
530 const char *c = NULL;
537 for (i = 0; i < ELEMENTSOF(colors); i += 2)
538 if (streq(colors[i], prop)) {
546 if (arg_dot != DOT_ALL)
547 if ((arg_dot == DOT_ORDER) != streq(prop, "After"))
550 switch (dbus_message_iter_get_arg_type(iter)) {
552 case DBUS_TYPE_ARRAY:
554 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
557 dbus_message_iter_recurse(iter, &sub);
559 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
562 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
563 dbus_message_iter_get_basic(&sub, &s);
564 printf("\t\"%s\"->\"%s\" %s;\n", name, s, c);
566 dbus_message_iter_next(&sub);
576 static int dot_one(DBusConnection *bus, const char *name, const char *path) {
577 DBusMessage *m = NULL, *reply = NULL;
578 const char *interface = "org.freedesktop.systemd1.Unit";
581 DBusMessageIter iter, sub, sub2, sub3;
586 dbus_error_init(&error);
588 if (!(m = dbus_message_new_method_call(
589 "org.freedesktop.systemd1",
591 "org.freedesktop.DBus.Properties",
593 log_error("Could not allocate message.");
598 if (!dbus_message_append_args(m,
599 DBUS_TYPE_STRING, &interface,
600 DBUS_TYPE_INVALID)) {
601 log_error("Could not append arguments to message.");
606 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
607 log_error("Failed to issue method call: %s", bus_error_message(&error));
612 if (!dbus_message_iter_init(reply, &iter) ||
613 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
614 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
615 log_error("Failed to parse reply.");
620 dbus_message_iter_recurse(&iter, &sub);
622 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
625 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
626 log_error("Failed to parse reply.");
631 dbus_message_iter_recurse(&sub, &sub2);
633 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &prop, true) < 0) {
634 log_error("Failed to parse reply.");
639 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
640 log_error("Failed to parse reply.");
645 dbus_message_iter_recurse(&sub2, &sub3);
647 if (dot_one_property(name, prop, &sub3)) {
648 log_error("Failed to parse reply.");
653 dbus_message_iter_next(&sub);
660 dbus_message_unref(m);
663 dbus_message_unref(reply);
665 dbus_error_free(&error);
670 static int dot(DBusConnection *bus, char **args, unsigned n) {
671 DBusMessage *m = NULL, *reply = NULL;
674 DBusMessageIter iter, sub, sub2;
676 dbus_error_init(&error);
680 if (!(m = dbus_message_new_method_call(
681 "org.freedesktop.systemd1",
682 "/org/freedesktop/systemd1",
683 "org.freedesktop.systemd1.Manager",
685 log_error("Could not allocate message.");
689 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
690 log_error("Failed to issue method call: %s", bus_error_message(&error));
695 if (!dbus_message_iter_init(reply, &iter) ||
696 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
697 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
698 log_error("Failed to parse reply.");
703 printf("digraph systemd {\n");
705 dbus_message_iter_recurse(&iter, &sub);
706 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
707 const char *id, *description, *load_state, *active_state, *sub_state, *following, *unit_path;
709 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
710 log_error("Failed to parse reply.");
715 dbus_message_iter_recurse(&sub, &sub2);
717 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
718 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
719 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
720 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
721 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
722 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &following, true) < 0 ||
723 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, true) < 0) {
724 log_error("Failed to parse reply.");
729 if ((r = dot_one(bus, id, unit_path)) < 0)
732 /* printf("\t\"%s\";\n", id); */
733 dbus_message_iter_next(&sub);
738 log_info(" Color legend: black = Requires\n"
739 " dark blue = Requisite\n"
740 " dark grey = Wants\n"
744 if (isatty(fileno(stdout)))
745 log_notice("-- You probably want to process this output with graphviz' dot tool.\n"
746 "-- Try a shell pipeline like 'systemctl dot | dot -Tsvg > systemd.svg'!\n");
752 dbus_message_unref(m);
755 dbus_message_unref(reply);
757 dbus_error_free(&error);
762 static int list_jobs(DBusConnection *bus, char **args, unsigned n) {
763 DBusMessage *m = NULL, *reply = NULL;
766 DBusMessageIter iter, sub, sub2;
769 dbus_error_init(&error);
775 if (!(m = dbus_message_new_method_call(
776 "org.freedesktop.systemd1",
777 "/org/freedesktop/systemd1",
778 "org.freedesktop.systemd1.Manager",
780 log_error("Could not allocate message.");
784 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
785 log_error("Failed to issue method call: %s", bus_error_message(&error));
790 if (!dbus_message_iter_init(reply, &iter) ||
791 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
792 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
793 log_error("Failed to parse reply.");
798 dbus_message_iter_recurse(&iter, &sub);
800 if (isatty(STDOUT_FILENO))
801 printf("%4s %-25s %-15s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
803 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
804 const char *name, *type, *state, *job_path, *unit_path;
808 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
809 log_error("Failed to parse reply.");
814 dbus_message_iter_recurse(&sub, &sub2);
816 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
817 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
818 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
819 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
820 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
821 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
822 log_error("Failed to parse reply.");
827 e = arg_full ? NULL : ellipsize(name, 25, 33);
828 printf("%4u %-25s %-15s %-7s\n", id, e ? e : name, type, state);
833 dbus_message_iter_next(&sub);
836 if (isatty(STDOUT_FILENO))
837 printf("\n%u jobs listed.\n", k);
843 dbus_message_unref(m);
846 dbus_message_unref(reply);
848 dbus_error_free(&error);
853 static int load_unit(DBusConnection *bus, char **args, unsigned n) {
854 DBusMessage *m = NULL, *reply = NULL;
859 dbus_error_init(&error);
864 for (i = 1; i < n; i++) {
866 if (!(m = dbus_message_new_method_call(
867 "org.freedesktop.systemd1",
868 "/org/freedesktop/systemd1",
869 "org.freedesktop.systemd1.Manager",
871 log_error("Could not allocate message.");
876 if (!dbus_message_append_args(m,
877 DBUS_TYPE_STRING, &args[i],
878 DBUS_TYPE_INVALID)) {
879 log_error("Could not append arguments to message.");
884 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
885 log_error("Failed to issue method call: %s", bus_error_message(&error));
890 dbus_message_unref(m);
891 dbus_message_unref(reply);
900 dbus_message_unref(m);
903 dbus_message_unref(reply);
905 dbus_error_free(&error);
910 static int cancel_job(DBusConnection *bus, char **args, unsigned n) {
911 DBusMessage *m = NULL, *reply = NULL;
916 dbus_error_init(&error);
922 return daemon_reload(bus, args, n);
924 for (i = 1; i < n; i++) {
928 if (!(m = dbus_message_new_method_call(
929 "org.freedesktop.systemd1",
930 "/org/freedesktop/systemd1",
931 "org.freedesktop.systemd1.Manager",
933 log_error("Could not allocate message.");
938 if ((r = safe_atou(args[i], &id)) < 0) {
939 log_error("Failed to parse job id: %s", strerror(-r));
943 assert_cc(sizeof(uint32_t) == sizeof(id));
944 if (!dbus_message_append_args(m,
945 DBUS_TYPE_UINT32, &id,
946 DBUS_TYPE_INVALID)) {
947 log_error("Could not append arguments to message.");
952 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
953 log_error("Failed to issue method call: %s", bus_error_message(&error));
958 if (!dbus_message_get_args(reply, &error,
959 DBUS_TYPE_OBJECT_PATH, &path,
960 DBUS_TYPE_INVALID)) {
961 log_error("Failed to parse reply: %s", bus_error_message(&error));
966 dbus_message_unref(m);
967 if (!(m = dbus_message_new_method_call(
968 "org.freedesktop.systemd1",
970 "org.freedesktop.systemd1.Job",
972 log_error("Could not allocate message.");
977 dbus_message_unref(reply);
978 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
979 log_error("Failed to issue method call: %s", bus_error_message(&error));
984 dbus_message_unref(m);
985 dbus_message_unref(reply);
993 dbus_message_unref(m);
996 dbus_message_unref(reply);
998 dbus_error_free(&error);
1003 static bool need_daemon_reload(DBusConnection *bus, const char *unit) {
1004 DBusMessage *m = NULL, *reply = NULL;
1005 dbus_bool_t b = FALSE;
1006 DBusMessageIter iter, sub;
1008 *interface = "org.freedesktop.systemd1.Unit",
1009 *property = "NeedDaemonReload",
1012 /* We ignore all errors here, since this is used to show a warning only */
1014 if (!(m = dbus_message_new_method_call(
1015 "org.freedesktop.systemd1",
1016 "/org/freedesktop/systemd1",
1017 "org.freedesktop.systemd1.Manager",
1021 if (!dbus_message_append_args(m,
1022 DBUS_TYPE_STRING, &unit,
1026 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
1029 if (!dbus_message_get_args(reply, NULL,
1030 DBUS_TYPE_OBJECT_PATH, &path,
1034 dbus_message_unref(m);
1035 if (!(m = dbus_message_new_method_call(
1036 "org.freedesktop.systemd1",
1038 "org.freedesktop.DBus.Properties",
1042 if (!dbus_message_append_args(m,
1043 DBUS_TYPE_STRING, &interface,
1044 DBUS_TYPE_STRING, &property,
1045 DBUS_TYPE_INVALID)) {
1049 dbus_message_unref(reply);
1050 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
1053 if (!dbus_message_iter_init(reply, &iter) ||
1054 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
1057 dbus_message_iter_recurse(&iter, &sub);
1059 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_BOOLEAN)
1062 dbus_message_iter_get_basic(&sub, &b);
1066 dbus_message_unref(m);
1069 dbus_message_unref(reply);
1074 typedef struct WaitData {
1079 static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
1087 dbus_error_init(&error);
1089 log_debug("Got D-Bus request: %s.%s() on %s",
1090 dbus_message_get_interface(message),
1091 dbus_message_get_member(message),
1092 dbus_message_get_path(message));
1094 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
1095 log_error("Warning! D-Bus connection terminated.");
1096 dbus_connection_close(connection);
1098 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
1101 dbus_bool_t success = true;
1103 if (!dbus_message_get_args(message, &error,
1104 DBUS_TYPE_UINT32, &id,
1105 DBUS_TYPE_OBJECT_PATH, &path,
1106 DBUS_TYPE_BOOLEAN, &success,
1108 log_error("Failed to parse message: %s", bus_error_message(&error));
1112 if ((p = set_remove(d->set, (char*) path)))
1120 dbus_error_free(&error);
1121 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1124 static int enable_wait_for_jobs(DBusConnection *bus) {
1132 dbus_error_init(&error);
1133 dbus_bus_add_match(bus,
1135 "sender='org.freedesktop.systemd1',"
1136 "interface='org.freedesktop.systemd1.Manager',"
1137 "member='JobRemoved',"
1138 "path='/org/freedesktop/systemd1'",
1141 if (dbus_error_is_set(&error)) {
1142 log_error("Failed to add match: %s", bus_error_message(&error));
1143 dbus_error_free(&error);
1147 /* This is slightly dirty, since we don't undo the match registrations. */
1151 static int wait_for_jobs(DBusConnection *bus, Set *s) {
1162 if (!dbus_connection_add_filter(bus, wait_filter, &d, NULL)) {
1163 log_error("Failed to add filter.");
1168 while (!set_isempty(s) &&
1169 dbus_connection_read_write_dispatch(bus, -1))
1172 if (!arg_quiet && d.failed)
1173 log_error("Job failed. See system logs and 'systemctl status' for details.");
1175 r = d.failed ? -EIO : 0;
1178 /* This is slightly dirty, since we don't undo the filter registration. */
1183 static int start_unit_one(
1184 DBusConnection *bus,
1191 DBusMessage *m = NULL, *reply = NULL;
1200 assert(arg_no_block || s);
1202 if (!(m = dbus_message_new_method_call(
1203 "org.freedesktop.systemd1",
1204 "/org/freedesktop/systemd1",
1205 "org.freedesktop.systemd1.Manager",
1207 log_error("Could not allocate message.");
1212 if (!dbus_message_append_args(m,
1213 DBUS_TYPE_STRING, &name,
1214 DBUS_TYPE_STRING, &mode,
1215 DBUS_TYPE_INVALID)) {
1216 log_error("Could not append arguments to message.");
1221 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, error))) {
1223 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(error)) {
1224 /* There's always a fallback possible for
1225 * legacy actions. */
1230 log_error("Failed to issue method call: %s", bus_error_message(error));
1235 if (!dbus_message_get_args(reply, error,
1236 DBUS_TYPE_OBJECT_PATH, &path,
1237 DBUS_TYPE_INVALID)) {
1238 log_error("Failed to parse reply: %s", bus_error_message(error));
1243 if (need_daemon_reload(bus, name))
1244 log_warning("Unit file of created job changed on disk, 'systemctl %s daemon-reload' recommended.",
1245 arg_user ? "--user" : "--system");
1247 if (!arg_no_block) {
1250 if (!(p = strdup(path))) {
1251 log_error("Failed to duplicate path.");
1256 if ((r = set_put(s, p)) < 0) {
1258 log_error("Failed to add path to set.");
1267 dbus_message_unref(m);
1270 dbus_message_unref(reply);
1275 static enum action verb_to_action(const char *verb) {
1276 if (streq(verb, "halt"))
1278 else if (streq(verb, "poweroff"))
1279 return ACTION_POWEROFF;
1280 else if (streq(verb, "reboot"))
1281 return ACTION_REBOOT;
1282 else if (streq(verb, "kexec"))
1283 return ACTION_KEXEC;
1284 else if (streq(verb, "rescue"))
1285 return ACTION_RESCUE;
1286 else if (streq(verb, "emergency"))
1287 return ACTION_EMERGENCY;
1288 else if (streq(verb, "default"))
1289 return ACTION_DEFAULT;
1290 else if (streq(verb, "exit"))
1293 return ACTION_INVALID;
1296 static int start_unit(DBusConnection *bus, char **args, unsigned n) {
1298 static const char * const table[_ACTION_MAX] = {
1299 [ACTION_HALT] = SPECIAL_HALT_TARGET,
1300 [ACTION_POWEROFF] = SPECIAL_POWEROFF_TARGET,
1301 [ACTION_REBOOT] = SPECIAL_REBOOT_TARGET,
1302 [ACTION_KEXEC] = SPECIAL_KEXEC_TARGET,
1303 [ACTION_RUNLEVEL2] = SPECIAL_RUNLEVEL2_TARGET,
1304 [ACTION_RUNLEVEL3] = SPECIAL_RUNLEVEL3_TARGET,
1305 [ACTION_RUNLEVEL4] = SPECIAL_RUNLEVEL4_TARGET,
1306 [ACTION_RUNLEVEL5] = SPECIAL_RUNLEVEL5_TARGET,
1307 [ACTION_RESCUE] = SPECIAL_RESCUE_TARGET,
1308 [ACTION_EMERGENCY] = SPECIAL_EMERGENCY_TARGET,
1309 [ACTION_DEFAULT] = SPECIAL_DEFAULT_TARGET,
1310 [ACTION_EXIT] = SPECIAL_EXIT_TARGET
1315 const char *method, *mode, *one_name;
1319 dbus_error_init(&error);
1323 spawn_ask_password_agent();
1325 if (arg_action == ACTION_SYSTEMCTL) {
1327 streq(args[0], "stop") ? "StopUnit" :
1328 streq(args[0], "reload") ? "ReloadUnit" :
1329 streq(args[0], "restart") ? "RestartUnit" :
1330 streq(args[0], "try-restart") ||
1331 streq(args[0], "condrestart") ? "TryRestartUnit" :
1332 streq(args[0], "reload-or-restart") ? "ReloadOrRestartUnit" :
1333 streq(args[0], "reload-or-try-restart") ||
1334 streq(args[0], "force-reload") ? "ReloadOrTryRestartUnit" :
1338 (streq(args[0], "isolate") ||
1339 streq(args[0], "rescue") ||
1340 streq(args[0], "emergency")) ? "isolate" :
1344 one_name = table[verb_to_action(args[0])];
1347 assert(arg_action < ELEMENTSOF(table));
1348 assert(table[arg_action]);
1350 method = "StartUnit";
1352 mode = (arg_action == ACTION_EMERGENCY ||
1353 arg_action == ACTION_RESCUE ||
1354 arg_action == ACTION_RUNLEVEL2 ||
1355 arg_action == ACTION_RUNLEVEL3 ||
1356 arg_action == ACTION_RUNLEVEL4 ||
1357 arg_action == ACTION_RUNLEVEL5) ? "isolate" : "replace";
1359 one_name = table[arg_action];
1362 if (!arg_no_block) {
1363 if ((ret = enable_wait_for_jobs(bus)) < 0) {
1364 log_error("Could not watch jobs: %s", strerror(-ret));
1368 if (!(s = set_new(string_hash_func, string_compare_func))) {
1369 log_error("Failed to allocate set.");
1376 if ((ret = start_unit_one(bus, method, one_name, mode, &error, s)) <= 0)
1379 for (i = 1; i < n; i++)
1380 if ((r = start_unit_one(bus, method, args[i], mode, &error, s)) != 0) {
1381 ret = translate_bus_error_to_exit_status(r, &error);
1382 dbus_error_free(&error);
1387 if ((r = wait_for_jobs(bus, s)) < 0) {
1396 dbus_error_free(&error);
1401 static int start_special(DBusConnection *bus, char **args, unsigned n) {
1408 (streq(args[0], "halt") ||
1409 streq(args[0], "poweroff") ||
1410 streq(args[0], "reboot") ||
1411 streq(args[0], "kexec") ||
1412 streq(args[0], "exit")))
1413 return daemon_reload(bus, args, n);
1415 r = start_unit(bus, args, n);
1418 warn_wall(verb_to_action(args[0]));
1423 static int check_unit(DBusConnection *bus, char **args, unsigned n) {
1424 DBusMessage *m = NULL, *reply = NULL;
1426 *interface = "org.freedesktop.systemd1.Unit",
1427 *property = "ActiveState";
1428 int r = 3; /* According to LSB: "program is not running" */
1435 dbus_error_init(&error);
1437 for (i = 1; i < n; i++) {
1438 const char *path = NULL;
1440 DBusMessageIter iter, sub;
1442 if (!(m = dbus_message_new_method_call(
1443 "org.freedesktop.systemd1",
1444 "/org/freedesktop/systemd1",
1445 "org.freedesktop.systemd1.Manager",
1447 log_error("Could not allocate message.");
1452 if (!dbus_message_append_args(m,
1453 DBUS_TYPE_STRING, &args[i],
1454 DBUS_TYPE_INVALID)) {
1455 log_error("Could not append arguments to message.");
1460 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1462 /* Hmm, cannot figure out anything about this unit... */
1466 dbus_error_free(&error);
1467 dbus_message_unref(m);
1471 if (!dbus_message_get_args(reply, &error,
1472 DBUS_TYPE_OBJECT_PATH, &path,
1473 DBUS_TYPE_INVALID)) {
1474 log_error("Failed to parse reply: %s", bus_error_message(&error));
1479 dbus_message_unref(m);
1480 if (!(m = dbus_message_new_method_call(
1481 "org.freedesktop.systemd1",
1483 "org.freedesktop.DBus.Properties",
1485 log_error("Could not allocate message.");
1490 if (!dbus_message_append_args(m,
1491 DBUS_TYPE_STRING, &interface,
1492 DBUS_TYPE_STRING, &property,
1493 DBUS_TYPE_INVALID)) {
1494 log_error("Could not append arguments to message.");
1499 dbus_message_unref(reply);
1500 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1501 log_error("Failed to issue method call: %s", bus_error_message(&error));
1506 if (!dbus_message_iter_init(reply, &iter) ||
1507 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1508 log_error("Failed to parse reply.");
1513 dbus_message_iter_recurse(&iter, &sub);
1515 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1516 log_error("Failed to parse reply.");
1521 dbus_message_iter_get_basic(&sub, &state);
1526 if (streq(state, "active") || streq(state, "reloading"))
1529 dbus_message_unref(m);
1530 dbus_message_unref(reply);
1536 dbus_message_unref(m);
1539 dbus_message_unref(reply);
1541 dbus_error_free(&error);
1546 static int kill_unit(DBusConnection *bus, char **args, unsigned n) {
1547 DBusMessage *m = NULL, *reply = NULL;
1555 dbus_error_init(&error);
1558 arg_kill_who = "all";
1561 arg_kill_mode = streq(arg_kill_who, "all") ? "control-group" : "process";
1563 for (i = 1; i < n; i++) {
1565 if (!(m = dbus_message_new_method_call(
1566 "org.freedesktop.systemd1",
1567 "/org/freedesktop/systemd1",
1568 "org.freedesktop.systemd1.Manager",
1570 log_error("Could not allocate message.");
1575 if (!dbus_message_append_args(m,
1576 DBUS_TYPE_STRING, &args[i],
1577 DBUS_TYPE_STRING, &arg_kill_who,
1578 DBUS_TYPE_STRING, &arg_kill_mode,
1579 DBUS_TYPE_INT32, &arg_signal,
1580 DBUS_TYPE_INVALID)) {
1581 log_error("Could not append arguments to message.");
1586 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1587 log_error("Failed to issue method call: %s", bus_error_message(&error));
1588 dbus_error_free(&error);
1592 dbus_message_unref(m);
1595 dbus_message_unref(reply);
1601 dbus_message_unref(m);
1604 dbus_message_unref(reply);
1606 dbus_error_free(&error);
1611 typedef struct ExecStatusInfo {
1617 usec_t start_timestamp;
1618 usec_t exit_timestamp;
1623 LIST_FIELDS(struct ExecStatusInfo, exec);
1626 static void exec_status_info_free(ExecStatusInfo *i) {
1634 static int exec_status_info_deserialize(DBusMessageIter *sub, ExecStatusInfo *i) {
1635 uint64_t start_timestamp, exit_timestamp;
1636 DBusMessageIter sub2, sub3;
1640 int32_t code, status;
1646 if (dbus_message_iter_get_arg_type(sub) != DBUS_TYPE_STRUCT)
1649 dbus_message_iter_recurse(sub, &sub2);
1651 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
1654 if (!(i->path = strdup(path)))
1657 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
1658 dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
1662 dbus_message_iter_recurse(&sub2, &sub3);
1663 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1664 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1665 dbus_message_iter_next(&sub3);
1670 if (!(i->argv = new0(char*, n+1)))
1674 dbus_message_iter_recurse(&sub2, &sub3);
1675 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1678 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1679 dbus_message_iter_get_basic(&sub3, &s);
1680 dbus_message_iter_next(&sub3);
1682 if (!(i->argv[n++] = strdup(s)))
1686 if (!dbus_message_iter_next(&sub2) ||
1687 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, true) < 0 ||
1688 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp, true) < 0 ||
1689 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp, true) < 0 ||
1690 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) < 0 ||
1691 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) < 0 ||
1692 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) < 0)
1696 i->start_timestamp = (usec_t) start_timestamp;
1697 i->exit_timestamp = (usec_t) exit_timestamp;
1698 i->pid = (pid_t) pid;
1705 typedef struct UnitStatusInfo {
1707 const char *load_state;
1708 const char *active_state;
1709 const char *sub_state;
1711 const char *description;
1712 const char *following;
1715 const char *default_control_group;
1717 usec_t inactive_exit_timestamp;
1718 usec_t active_enter_timestamp;
1719 usec_t active_exit_timestamp;
1720 usec_t inactive_enter_timestamp;
1722 bool need_daemon_reload;
1727 const char *status_text;
1729 #ifdef HAVE_SYSV_COMPAT
1733 usec_t start_timestamp;
1734 usec_t exit_timestamp;
1736 int exit_code, exit_status;
1739 unsigned n_accepted;
1740 unsigned n_connections;
1744 const char *sysfs_path;
1746 /* Mount, Automount */
1752 LIST_HEAD(ExecStatusInfo, exec);
1755 static void print_status_info(UnitStatusInfo *i) {
1757 const char *on, *off, *ss;
1759 char since1[FORMAT_TIMESTAMP_PRETTY_MAX], *s1;
1760 char since2[FORMAT_TIMESTAMP_MAX], *s2;
1764 /* This shows pretty information about a unit. See
1765 * print_property() for a low-level property printer */
1767 printf("%s", strna(i->id));
1769 if (i->description && !streq_ptr(i->id, i->description))
1770 printf(" - %s", i->description);
1775 printf("\t Follow: unit currently follows state of %s\n", i->following);
1777 if (streq_ptr(i->load_state, "failed") ||
1778 streq_ptr(i->load_state, "banned")) {
1779 on = ansi_highlight(true);
1780 off = ansi_highlight(false);
1785 printf("\t Loaded: %s%s%s (%s)\n", on, strna(i->load_state), off, i->path);
1787 printf("\t Loaded: %s%s%s\n", on, strna(i->load_state), off);
1789 ss = streq_ptr(i->active_state, i->sub_state) ? NULL : i->sub_state;
1791 if (streq_ptr(i->active_state, "failed")) {
1792 on = ansi_highlight(true);
1793 off = ansi_highlight(false);
1794 } else if (streq_ptr(i->active_state, "active") || streq_ptr(i->active_state, "reloading")) {
1795 on = ansi_highlight_green(true);
1796 off = ansi_highlight_green(false);
1801 printf("\t Active: %s%s (%s)%s",
1803 strna(i->active_state),
1807 printf("\t Active: %s%s%s",
1809 strna(i->active_state),
1812 timestamp = (streq_ptr(i->active_state, "active") ||
1813 streq_ptr(i->active_state, "reloading")) ? i->active_enter_timestamp :
1814 (streq_ptr(i->active_state, "inactive") ||
1815 streq_ptr(i->active_state, "failed")) ? i->inactive_enter_timestamp :
1816 streq_ptr(i->active_state, "activating") ? i->inactive_exit_timestamp :
1817 i->active_exit_timestamp;
1819 s1 = format_timestamp_pretty(since1, sizeof(since1), timestamp);
1820 s2 = format_timestamp(since2, sizeof(since2), timestamp);
1823 printf(" since %s; %s\n", s2, s1);
1825 printf(" since %s\n", s2);
1830 printf("\t Device: %s\n", i->sysfs_path);
1832 printf("\t Where: %s\n", i->where);
1834 printf("\t What: %s\n", i->what);
1837 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
1839 LIST_FOREACH(exec, p, i->exec) {
1842 /* Only show exited processes here */
1846 t = strv_join(p->argv, " ");
1847 printf("\t Process: %u (%s, code=%s, ", p->pid, strna(t), sigchld_code_to_string(p->code));
1850 if (p->code == CLD_EXITED) {
1853 printf("status=%i", p->status);
1855 #ifdef HAVE_SYSV_COMPAT
1856 if ((c = exit_status_to_string(p->status, i->is_sysv ? EXIT_STATUS_LSB : EXIT_STATUS_SYSTEMD)))
1858 if ((c = exit_status_to_string(p->status, EXIT_STATUS_SYSTEMD)))
1863 printf("signal=%s", signal_to_string(p->status));
1866 if (i->main_pid == p->pid &&
1867 i->start_timestamp == p->start_timestamp &&
1868 i->exit_timestamp == p->start_timestamp)
1869 /* Let's not show this twice */
1872 if (p->pid == i->control_pid)
1876 if (i->main_pid > 0 || i->control_pid > 0) {
1879 if (i->main_pid > 0) {
1880 printf("Main PID: %u", (unsigned) i->main_pid);
1884 get_process_name(i->main_pid, &t);
1889 } else if (i->exit_code > 0) {
1890 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
1892 if (i->exit_code == CLD_EXITED) {
1895 printf("status=%i", i->exit_status);
1897 #ifdef HAVE_SYSV_COMPAT
1898 if ((c = exit_status_to_string(i->exit_status, i->is_sysv ? EXIT_STATUS_LSB : EXIT_STATUS_SYSTEMD)))
1900 if ((c = exit_status_to_string(i->exit_status, EXIT_STATUS_SYSTEMD)))
1905 printf("signal=%s", signal_to_string(i->exit_status));
1910 if (i->main_pid > 0 && i->control_pid > 0)
1913 if (i->control_pid > 0) {
1916 printf(" Control: %u", (unsigned) i->control_pid);
1918 get_process_name(i->control_pid, &t);
1929 printf("\t Status: \"%s\"\n", i->status_text);
1931 if (i->default_control_group) {
1934 printf("\t CGroup: %s\n", i->default_control_group);
1936 if ((c = columns()) > 18)
1941 show_cgroup_by_path(i->default_control_group, "\t\t ", c);
1944 if (i->need_daemon_reload)
1945 printf("\n%sWarning:%s Unit file changed on disk, 'systemctl %s daemon-reload' recommended.\n",
1946 ansi_highlight(true),
1947 ansi_highlight(false),
1948 arg_user ? "--user" : "--system");
1951 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
1953 switch (dbus_message_iter_get_arg_type(iter)) {
1955 case DBUS_TYPE_STRING: {
1958 dbus_message_iter_get_basic(iter, &s);
1961 if (streq(name, "Id"))
1963 else if (streq(name, "LoadState"))
1965 else if (streq(name, "ActiveState"))
1966 i->active_state = s;
1967 else if (streq(name, "SubState"))
1969 else if (streq(name, "Description"))
1971 else if (streq(name, "FragmentPath"))
1973 #ifdef HAVE_SYSV_COMPAT
1974 else if (streq(name, "SysVPath")) {
1979 else if (streq(name, "DefaultControlGroup"))
1980 i->default_control_group = s;
1981 else if (streq(name, "StatusText"))
1983 else if (streq(name, "SysFSPath"))
1985 else if (streq(name, "Where"))
1987 else if (streq(name, "What"))
1989 else if (streq(name, "Following"))
1996 case DBUS_TYPE_BOOLEAN: {
1999 dbus_message_iter_get_basic(iter, &b);
2001 if (streq(name, "Accept"))
2003 else if (streq(name, "NeedDaemonReload"))
2004 i->need_daemon_reload = b;
2009 case DBUS_TYPE_UINT32: {
2012 dbus_message_iter_get_basic(iter, &u);
2014 if (streq(name, "MainPID")) {
2016 i->main_pid = (pid_t) u;
2019 } else if (streq(name, "ControlPID"))
2020 i->control_pid = (pid_t) u;
2021 else if (streq(name, "ExecMainPID")) {
2023 i->main_pid = (pid_t) u;
2024 } else if (streq(name, "NAccepted"))
2026 else if (streq(name, "NConnections"))
2027 i->n_connections = u;
2032 case DBUS_TYPE_INT32: {
2035 dbus_message_iter_get_basic(iter, &j);
2037 if (streq(name, "ExecMainCode"))
2038 i->exit_code = (int) j;
2039 else if (streq(name, "ExecMainStatus"))
2040 i->exit_status = (int) j;
2045 case DBUS_TYPE_UINT64: {
2048 dbus_message_iter_get_basic(iter, &u);
2050 if (streq(name, "ExecMainStartTimestamp"))
2051 i->start_timestamp = (usec_t) u;
2052 else if (streq(name, "ExecMainExitTimestamp"))
2053 i->exit_timestamp = (usec_t) u;
2054 else if (streq(name, "ActiveEnterTimestamp"))
2055 i->active_enter_timestamp = (usec_t) u;
2056 else if (streq(name, "InactiveEnterTimestamp"))
2057 i->inactive_enter_timestamp = (usec_t) u;
2058 else if (streq(name, "InactiveExitTimestamp"))
2059 i->inactive_exit_timestamp = (usec_t) u;
2060 else if (streq(name, "ActiveExitTimestamp"))
2061 i->active_exit_timestamp = (usec_t) u;
2066 case DBUS_TYPE_ARRAY: {
2068 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT &&
2069 startswith(name, "Exec")) {
2070 DBusMessageIter sub;
2072 dbus_message_iter_recurse(iter, &sub);
2073 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2074 ExecStatusInfo *info;
2077 if (!(info = new0(ExecStatusInfo, 1)))
2080 if ((r = exec_status_info_deserialize(&sub, info)) < 0) {
2085 LIST_PREPEND(ExecStatusInfo, exec, i->exec, info);
2087 dbus_message_iter_next(&sub);
2098 static int print_property(const char *name, DBusMessageIter *iter) {
2102 /* This is a low-level property printer, see
2103 * print_status_info() for the nicer output */
2105 if (arg_property && !strv_find(arg_property, name))
2108 switch (dbus_message_iter_get_arg_type(iter)) {
2110 case DBUS_TYPE_STRING: {
2112 dbus_message_iter_get_basic(iter, &s);
2114 if (arg_all || s[0])
2115 printf("%s=%s\n", name, s);
2120 case DBUS_TYPE_BOOLEAN: {
2122 dbus_message_iter_get_basic(iter, &b);
2123 printf("%s=%s\n", name, yes_no(b));
2128 case DBUS_TYPE_UINT64: {
2130 dbus_message_iter_get_basic(iter, &u);
2132 /* Yes, heuristics! But we can change this check
2133 * should it turn out to not be sufficient */
2135 if (strstr(name, "Timestamp")) {
2136 char timestamp[FORMAT_TIMESTAMP_MAX], *t;
2138 if ((t = format_timestamp(timestamp, sizeof(timestamp), u)) || arg_all)
2139 printf("%s=%s\n", name, strempty(t));
2140 } else if (strstr(name, "USec")) {
2141 char timespan[FORMAT_TIMESPAN_MAX];
2143 printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u));
2145 printf("%s=%llu\n", name, (unsigned long long) u);
2150 case DBUS_TYPE_UINT32: {
2152 dbus_message_iter_get_basic(iter, &u);
2154 if (strstr(name, "UMask") || strstr(name, "Mode"))
2155 printf("%s=%04o\n", name, u);
2157 printf("%s=%u\n", name, (unsigned) u);
2162 case DBUS_TYPE_INT32: {
2164 dbus_message_iter_get_basic(iter, &i);
2166 printf("%s=%i\n", name, (int) i);
2170 case DBUS_TYPE_DOUBLE: {
2172 dbus_message_iter_get_basic(iter, &d);
2174 printf("%s=%g\n", name, d);
2178 case DBUS_TYPE_STRUCT: {
2179 DBusMessageIter sub;
2180 dbus_message_iter_recurse(iter, &sub);
2182 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
2185 dbus_message_iter_get_basic(&sub, &u);
2188 printf("%s=%u\n", name, (unsigned) u);
2190 printf("%s=\n", name);
2193 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
2196 dbus_message_iter_get_basic(&sub, &s);
2198 if (arg_all || s[0])
2199 printf("%s=%s\n", name, s);
2207 case DBUS_TYPE_ARRAY:
2209 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
2210 DBusMessageIter sub;
2213 dbus_message_iter_recurse(iter, &sub);
2215 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2216 printf("%s=", name);
2218 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2221 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
2222 dbus_message_iter_get_basic(&sub, &s);
2223 printf("%s%s", space ? " " : "", s);
2226 dbus_message_iter_next(&sub);
2234 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_BYTE) {
2235 DBusMessageIter sub;
2237 dbus_message_iter_recurse(iter, &sub);
2239 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2240 printf("%s=", name);
2242 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2245 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_BYTE);
2246 dbus_message_iter_get_basic(&sub, &u);
2249 dbus_message_iter_next(&sub);
2257 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
2258 DBusMessageIter sub, sub2;
2260 dbus_message_iter_recurse(iter, &sub);
2261 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2262 const char *type, *path;
2264 dbus_message_iter_recurse(&sub, &sub2);
2266 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
2267 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
2268 printf("%s=%s\n", type, path);
2270 dbus_message_iter_next(&sub);
2275 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
2276 DBusMessageIter sub, sub2;
2278 dbus_message_iter_recurse(iter, &sub);
2279 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2281 uint64_t value, next_elapse;
2283 dbus_message_iter_recurse(&sub, &sub2);
2285 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
2286 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
2287 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
2288 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
2290 printf("%s={ value=%s ; next_elapse=%s }\n",
2292 format_timespan(timespan1, sizeof(timespan1), value),
2293 format_timespan(timespan2, sizeof(timespan2), next_elapse));
2296 dbus_message_iter_next(&sub);
2301 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
2302 DBusMessageIter sub;
2304 dbus_message_iter_recurse(iter, &sub);
2305 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2306 ExecStatusInfo info;
2309 if (exec_status_info_deserialize(&sub, &info) >= 0) {
2310 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
2313 t = strv_join(info.argv, " ");
2315 printf("%s={ path=%s ; argv[]=%s ; ignore=%s ; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n",
2319 yes_no(info.ignore),
2320 strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)),
2321 strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)),
2322 (unsigned) info. pid,
2323 sigchld_code_to_string(info.code),
2325 info.code == CLD_EXITED ? "" : "/",
2326 strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status)));
2332 strv_free(info.argv);
2334 dbus_message_iter_next(&sub);
2344 printf("%s=[unprintable]\n", name);
2349 static int show_one(const char *verb, DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
2350 DBusMessage *m = NULL, *reply = NULL;
2351 const char *interface = "";
2354 DBusMessageIter iter, sub, sub2, sub3;
2355 UnitStatusInfo info;
2363 dbus_error_init(&error);
2365 if (!(m = dbus_message_new_method_call(
2366 "org.freedesktop.systemd1",
2368 "org.freedesktop.DBus.Properties",
2370 log_error("Could not allocate message.");
2375 if (!dbus_message_append_args(m,
2376 DBUS_TYPE_STRING, &interface,
2377 DBUS_TYPE_INVALID)) {
2378 log_error("Could not append arguments to message.");
2383 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2384 log_error("Failed to issue method call: %s", bus_error_message(&error));
2389 if (!dbus_message_iter_init(reply, &iter) ||
2390 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
2391 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
2392 log_error("Failed to parse reply.");
2397 dbus_message_iter_recurse(&iter, &sub);
2404 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2407 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
2408 log_error("Failed to parse reply.");
2413 dbus_message_iter_recurse(&sub, &sub2);
2415 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
2416 log_error("Failed to parse reply.");
2421 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
2422 log_error("Failed to parse reply.");
2427 dbus_message_iter_recurse(&sub2, &sub3);
2429 if (show_properties)
2430 r = print_property(name, &sub3);
2432 r = status_property(name, &sub3, &info);
2435 log_error("Failed to parse reply.");
2440 dbus_message_iter_next(&sub);
2445 if (!show_properties)
2446 print_status_info(&info);
2448 if (!streq_ptr(info.active_state, "active") &&
2449 !streq_ptr(info.active_state, "reloading") &&
2450 streq(verb, "status"))
2451 /* According to LSB: "program not running" */
2454 while ((p = info.exec)) {
2455 LIST_REMOVE(ExecStatusInfo, exec, info.exec, p);
2456 exec_status_info_free(p);
2461 dbus_message_unref(m);
2464 dbus_message_unref(reply);
2466 dbus_error_free(&error);
2471 static int show(DBusConnection *bus, char **args, unsigned n) {
2472 DBusMessage *m = NULL, *reply = NULL;
2476 bool show_properties, new_line = false;
2481 dbus_error_init(&error);
2483 show_properties = !streq(args[0], "status");
2485 if (show_properties)
2488 if (show_properties && n <= 1) {
2489 /* If not argument is specified inspect the manager
2492 ret = show_one(args[0], bus, "/org/freedesktop/systemd1", show_properties, &new_line);
2496 for (i = 1; i < n; i++) {
2497 const char *path = NULL;
2500 if (safe_atou32(args[i], &id) < 0) {
2502 /* Interpret as unit name */
2504 if (!(m = dbus_message_new_method_call(
2505 "org.freedesktop.systemd1",
2506 "/org/freedesktop/systemd1",
2507 "org.freedesktop.systemd1.Manager",
2509 log_error("Could not allocate message.");
2514 if (!dbus_message_append_args(m,
2515 DBUS_TYPE_STRING, &args[i],
2516 DBUS_TYPE_INVALID)) {
2517 log_error("Could not append arguments to message.");
2522 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2524 if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
2525 log_error("Failed to issue method call: %s", bus_error_message(&error));
2530 dbus_error_free(&error);
2532 dbus_message_unref(m);
2533 if (!(m = dbus_message_new_method_call(
2534 "org.freedesktop.systemd1",
2535 "/org/freedesktop/systemd1",
2536 "org.freedesktop.systemd1.Manager",
2538 log_error("Could not allocate message.");
2543 if (!dbus_message_append_args(m,
2544 DBUS_TYPE_STRING, &args[i],
2545 DBUS_TYPE_INVALID)) {
2546 log_error("Could not append arguments to message.");
2551 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2552 log_error("Failed to issue method call: %s", bus_error_message(&error));
2554 if (dbus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT))
2555 ret = 4; /* According to LSB: "program or service status is unknown" */
2562 } else if (show_properties) {
2564 /* Interpret as job id */
2566 if (!(m = dbus_message_new_method_call(
2567 "org.freedesktop.systemd1",
2568 "/org/freedesktop/systemd1",
2569 "org.freedesktop.systemd1.Manager",
2571 log_error("Could not allocate message.");
2576 if (!dbus_message_append_args(m,
2577 DBUS_TYPE_UINT32, &id,
2578 DBUS_TYPE_INVALID)) {
2579 log_error("Could not append arguments to message.");
2584 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2585 log_error("Failed to issue method call: %s", bus_error_message(&error));
2591 /* Interpret as PID */
2593 if (!(m = dbus_message_new_method_call(
2594 "org.freedesktop.systemd1",
2595 "/org/freedesktop/systemd1",
2596 "org.freedesktop.systemd1.Manager",
2598 log_error("Could not allocate message.");
2603 if (!dbus_message_append_args(m,
2604 DBUS_TYPE_UINT32, &id,
2605 DBUS_TYPE_INVALID)) {
2606 log_error("Could not append arguments to message.");
2611 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2612 log_error("Failed to issue method call: %s", bus_error_message(&error));
2618 if (!dbus_message_get_args(reply, &error,
2619 DBUS_TYPE_OBJECT_PATH, &path,
2620 DBUS_TYPE_INVALID)) {
2621 log_error("Failed to parse reply: %s", bus_error_message(&error));
2626 if ((r = show_one(args[0], bus, path, show_properties, &new_line)) != 0)
2629 dbus_message_unref(m);
2630 dbus_message_unref(reply);
2636 dbus_message_unref(m);
2639 dbus_message_unref(reply);
2641 dbus_error_free(&error);
2646 static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
2648 DBusMessage *m = NULL, *reply = NULL;
2653 dbus_error_init(&error);
2655 log_debug("Got D-Bus request: %s.%s() on %s",
2656 dbus_message_get_interface(message),
2657 dbus_message_get_member(message),
2658 dbus_message_get_path(message));
2660 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
2661 log_error("Warning! D-Bus connection terminated.");
2662 dbus_connection_close(connection);
2664 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") ||
2665 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
2666 const char *id, *path;
2668 if (!dbus_message_get_args(message, &error,
2669 DBUS_TYPE_STRING, &id,
2670 DBUS_TYPE_OBJECT_PATH, &path,
2672 log_error("Failed to parse message: %s", bus_error_message(&error));
2673 else if (streq(dbus_message_get_member(message), "UnitNew"))
2674 printf("Unit %s added.\n", id);
2676 printf("Unit %s removed.\n", id);
2678 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew") ||
2679 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
2683 if (!dbus_message_get_args(message, &error,
2684 DBUS_TYPE_UINT32, &id,
2685 DBUS_TYPE_OBJECT_PATH, &path,
2687 log_error("Failed to parse message: %s", bus_error_message(&error));
2688 else if (streq(dbus_message_get_member(message), "JobNew"))
2689 printf("Job %u added.\n", id);
2691 printf("Job %u removed.\n", id);
2694 } else if (dbus_message_is_signal(message, "org.freedesktop.DBus.Properties", "PropertiesChanged")) {
2696 const char *path, *interface, *property = "Id";
2697 DBusMessageIter iter, sub;
2699 path = dbus_message_get_path(message);
2701 if (!dbus_message_get_args(message, &error,
2702 DBUS_TYPE_STRING, &interface,
2703 DBUS_TYPE_INVALID)) {
2704 log_error("Failed to parse message: %s", bus_error_message(&error));
2708 if (!streq(interface, "org.freedesktop.systemd1.Job") &&
2709 !streq(interface, "org.freedesktop.systemd1.Unit"))
2712 if (!(m = dbus_message_new_method_call(
2713 "org.freedesktop.systemd1",
2715 "org.freedesktop.DBus.Properties",
2717 log_error("Could not allocate message.");
2721 if (!dbus_message_append_args(m,
2722 DBUS_TYPE_STRING, &interface,
2723 DBUS_TYPE_STRING, &property,
2724 DBUS_TYPE_INVALID)) {
2725 log_error("Could not append arguments to message.");
2729 if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
2730 log_error("Failed to issue method call: %s", bus_error_message(&error));
2734 if (!dbus_message_iter_init(reply, &iter) ||
2735 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2736 log_error("Failed to parse reply.");
2740 dbus_message_iter_recurse(&iter, &sub);
2742 if (streq(interface, "org.freedesktop.systemd1.Unit")) {
2745 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2746 log_error("Failed to parse reply.");
2750 dbus_message_iter_get_basic(&sub, &id);
2751 printf("Unit %s changed.\n", id);
2755 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32) {
2756 log_error("Failed to parse reply.");
2760 dbus_message_iter_get_basic(&sub, &id);
2761 printf("Job %u changed.\n", id);
2767 dbus_message_unref(m);
2770 dbus_message_unref(reply);
2772 dbus_error_free(&error);
2773 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2777 dbus_message_unref(m);
2780 dbus_message_unref(reply);
2782 dbus_error_free(&error);
2783 return DBUS_HANDLER_RESULT_NEED_MEMORY;
2786 static int monitor(DBusConnection *bus, char **args, unsigned n) {
2787 DBusMessage *m = NULL, *reply = NULL;
2791 dbus_error_init(&error);
2794 dbus_bus_add_match(bus,
2796 "sender='org.freedesktop.systemd1',"
2797 "interface='org.freedesktop.systemd1.Manager',"
2798 "path='/org/freedesktop/systemd1'",
2801 if (dbus_error_is_set(&error)) {
2802 log_error("Failed to add match: %s", bus_error_message(&error));
2807 dbus_bus_add_match(bus,
2809 "sender='org.freedesktop.systemd1',"
2810 "interface='org.freedesktop.DBus.Properties',"
2811 "member='PropertiesChanged'",
2814 if (dbus_error_is_set(&error)) {
2815 log_error("Failed to add match: %s", bus_error_message(&error));
2821 if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
2822 log_error("Failed to add filter.");
2827 if (!(m = dbus_message_new_method_call(
2828 "org.freedesktop.systemd1",
2829 "/org/freedesktop/systemd1",
2830 "org.freedesktop.systemd1.Manager",
2832 log_error("Could not allocate message.");
2837 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2838 log_error("Failed to issue method call: %s", bus_error_message(&error));
2843 while (dbus_connection_read_write_dispatch(bus, -1))
2850 /* This is slightly dirty, since we don't undo the filter or the matches. */
2853 dbus_message_unref(m);
2856 dbus_message_unref(reply);
2858 dbus_error_free(&error);
2863 static int dump(DBusConnection *bus, char **args, unsigned n) {
2864 DBusMessage *m = NULL, *reply = NULL;
2869 dbus_error_init(&error);
2873 if (!(m = dbus_message_new_method_call(
2874 "org.freedesktop.systemd1",
2875 "/org/freedesktop/systemd1",
2876 "org.freedesktop.systemd1.Manager",
2878 log_error("Could not allocate message.");
2882 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2883 log_error("Failed to issue method call: %s", bus_error_message(&error));
2888 if (!dbus_message_get_args(reply, &error,
2889 DBUS_TYPE_STRING, &text,
2890 DBUS_TYPE_INVALID)) {
2891 log_error("Failed to parse reply: %s", bus_error_message(&error));
2896 fputs(text, stdout);
2902 dbus_message_unref(m);
2905 dbus_message_unref(reply);
2907 dbus_error_free(&error);
2912 static int snapshot(DBusConnection *bus, char **args, unsigned n) {
2913 DBusMessage *m = NULL, *reply = NULL;
2916 const char *name = "", *path, *id;
2917 dbus_bool_t cleanup = FALSE;
2918 DBusMessageIter iter, sub;
2920 *interface = "org.freedesktop.systemd1.Unit",
2923 dbus_error_init(&error);
2925 if (!(m = dbus_message_new_method_call(
2926 "org.freedesktop.systemd1",
2927 "/org/freedesktop/systemd1",
2928 "org.freedesktop.systemd1.Manager",
2929 "CreateSnapshot"))) {
2930 log_error("Could not allocate message.");
2937 if (!dbus_message_append_args(m,
2938 DBUS_TYPE_STRING, &name,
2939 DBUS_TYPE_BOOLEAN, &cleanup,
2940 DBUS_TYPE_INVALID)) {
2941 log_error("Could not append arguments to message.");
2946 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2947 log_error("Failed to issue method call: %s", bus_error_message(&error));
2952 if (!dbus_message_get_args(reply, &error,
2953 DBUS_TYPE_OBJECT_PATH, &path,
2954 DBUS_TYPE_INVALID)) {
2955 log_error("Failed to parse reply: %s", bus_error_message(&error));
2960 dbus_message_unref(m);
2961 if (!(m = dbus_message_new_method_call(
2962 "org.freedesktop.systemd1",
2964 "org.freedesktop.DBus.Properties",
2966 log_error("Could not allocate message.");
2970 if (!dbus_message_append_args(m,
2971 DBUS_TYPE_STRING, &interface,
2972 DBUS_TYPE_STRING, &property,
2973 DBUS_TYPE_INVALID)) {
2974 log_error("Could not append arguments to message.");
2979 dbus_message_unref(reply);
2980 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2981 log_error("Failed to issue method call: %s", bus_error_message(&error));
2986 if (!dbus_message_iter_init(reply, &iter) ||
2987 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2988 log_error("Failed to parse reply.");
2993 dbus_message_iter_recurse(&iter, &sub);
2995 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2996 log_error("Failed to parse reply.");
3001 dbus_message_iter_get_basic(&sub, &id);
3009 dbus_message_unref(m);
3012 dbus_message_unref(reply);
3014 dbus_error_free(&error);
3019 static int delete_snapshot(DBusConnection *bus, char **args, unsigned n) {
3020 DBusMessage *m = NULL, *reply = NULL;
3028 dbus_error_init(&error);
3030 for (i = 1; i < n; i++) {
3031 const char *path = NULL;
3033 if (!(m = dbus_message_new_method_call(
3034 "org.freedesktop.systemd1",
3035 "/org/freedesktop/systemd1",
3036 "org.freedesktop.systemd1.Manager",
3038 log_error("Could not allocate message.");
3043 if (!dbus_message_append_args(m,
3044 DBUS_TYPE_STRING, &args[i],
3045 DBUS_TYPE_INVALID)) {
3046 log_error("Could not append arguments to message.");
3051 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3052 log_error("Failed to issue method call: %s", bus_error_message(&error));
3057 if (!dbus_message_get_args(reply, &error,
3058 DBUS_TYPE_OBJECT_PATH, &path,
3059 DBUS_TYPE_INVALID)) {
3060 log_error("Failed to parse reply: %s", bus_error_message(&error));
3065 dbus_message_unref(m);
3066 if (!(m = dbus_message_new_method_call(
3067 "org.freedesktop.systemd1",
3069 "org.freedesktop.systemd1.Snapshot",
3071 log_error("Could not allocate message.");
3076 dbus_message_unref(reply);
3077 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3078 log_error("Failed to issue method call: %s", bus_error_message(&error));
3083 dbus_message_unref(m);
3084 dbus_message_unref(reply);
3092 dbus_message_unref(m);
3095 dbus_message_unref(reply);
3097 dbus_error_free(&error);
3102 static int daemon_reload(DBusConnection *bus, char **args, unsigned n) {
3103 DBusMessage *m = NULL, *reply = NULL;
3108 dbus_error_init(&error);
3110 if (arg_action == ACTION_RELOAD)
3112 else if (arg_action == ACTION_REEXEC)
3113 method = "Reexecute";
3115 assert(arg_action == ACTION_SYSTEMCTL);
3118 streq(args[0], "clear-jobs") ||
3119 streq(args[0], "cancel") ? "ClearJobs" :
3120 streq(args[0], "daemon-reexec") ? "Reexecute" :
3121 streq(args[0], "reset-failed") ? "ResetFailed" :
3122 streq(args[0], "halt") ? "Halt" :
3123 streq(args[0], "poweroff") ? "PowerOff" :
3124 streq(args[0], "reboot") ? "Reboot" :
3125 streq(args[0], "kexec") ? "KExec" :
3126 streq(args[0], "exit") ? "Exit" :
3127 /* "daemon-reload" */ "Reload";
3130 if (!(m = dbus_message_new_method_call(
3131 "org.freedesktop.systemd1",
3132 "/org/freedesktop/systemd1",
3133 "org.freedesktop.systemd1.Manager",
3135 log_error("Could not allocate message.");
3139 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3141 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
3142 /* There's always a fallback possible for
3143 * legacy actions. */
3148 log_error("Failed to issue method call: %s", bus_error_message(&error));
3157 dbus_message_unref(m);
3160 dbus_message_unref(reply);
3162 dbus_error_free(&error);
3167 static int reset_failed(DBusConnection *bus, char **args, unsigned n) {
3168 DBusMessage *m = NULL, *reply = NULL;
3174 dbus_error_init(&error);
3177 return daemon_reload(bus, args, n);
3179 for (i = 1; i < n; i++) {
3181 if (!(m = dbus_message_new_method_call(
3182 "org.freedesktop.systemd1",
3183 "/org/freedesktop/systemd1",
3184 "org.freedesktop.systemd1.Manager",
3185 "ResetFailedUnit"))) {
3186 log_error("Could not allocate message.");
3191 if (!dbus_message_append_args(m,
3192 DBUS_TYPE_STRING, args + i,
3193 DBUS_TYPE_INVALID)) {
3194 log_error("Could not append arguments to message.");
3199 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3200 log_error("Failed to issue method call: %s", bus_error_message(&error));
3205 dbus_message_unref(m);
3206 dbus_message_unref(reply);
3214 dbus_message_unref(m);
3217 dbus_message_unref(reply);
3219 dbus_error_free(&error);
3224 static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
3225 DBusMessage *m = NULL, *reply = NULL;
3227 DBusMessageIter iter, sub, sub2;
3230 *interface = "org.freedesktop.systemd1.Manager",
3231 *property = "Environment";
3233 dbus_error_init(&error);
3237 if (!(m = dbus_message_new_method_call(
3238 "org.freedesktop.systemd1",
3239 "/org/freedesktop/systemd1",
3240 "org.freedesktop.DBus.Properties",
3242 log_error("Could not allocate message.");
3246 if (!dbus_message_append_args(m,
3247 DBUS_TYPE_STRING, &interface,
3248 DBUS_TYPE_STRING, &property,
3249 DBUS_TYPE_INVALID)) {
3250 log_error("Could not append arguments to message.");
3255 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3256 log_error("Failed to issue method call: %s", bus_error_message(&error));
3261 if (!dbus_message_iter_init(reply, &iter) ||
3262 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
3263 log_error("Failed to parse reply.");
3268 dbus_message_iter_recurse(&iter, &sub);
3270 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
3271 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
3272 log_error("Failed to parse reply.");
3277 dbus_message_iter_recurse(&sub, &sub2);
3279 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
3282 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
3283 log_error("Failed to parse reply.");
3288 dbus_message_iter_get_basic(&sub2, &text);
3289 printf("%s\n", text);
3291 dbus_message_iter_next(&sub2);
3298 dbus_message_unref(m);
3301 dbus_message_unref(reply);
3303 dbus_error_free(&error);
3308 static int set_environment(DBusConnection *bus, char **args, unsigned n) {
3309 DBusMessage *m = NULL, *reply = NULL;
3313 DBusMessageIter iter, sub;
3316 dbus_error_init(&error);
3318 method = streq(args[0], "set-environment")
3320 : "UnsetEnvironment";
3322 if (!(m = dbus_message_new_method_call(
3323 "org.freedesktop.systemd1",
3324 "/org/freedesktop/systemd1",
3325 "org.freedesktop.systemd1.Manager",
3328 log_error("Could not allocate message.");
3332 dbus_message_iter_init_append(m, &iter);
3334 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
3335 log_error("Could not append arguments to message.");
3340 for (i = 1; i < n; i++)
3341 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) {
3342 log_error("Could not append arguments to message.");
3347 if (!dbus_message_iter_close_container(&iter, &sub)) {
3348 log_error("Could not append arguments to message.");
3353 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3354 log_error("Failed to issue method call: %s", bus_error_message(&error));
3363 dbus_message_unref(m);
3366 dbus_message_unref(reply);
3368 dbus_error_free(&error);
3381 static Hashmap *will_install = NULL, *have_installed = NULL;
3382 static Set *remove_symlinks_to = NULL;
3383 static unsigned n_symlinks = 0;
3385 static void install_info_free(InstallInfo *i) {
3390 strv_free(i->aliases);
3391 strv_free(i->wanted_by);
3395 static void install_info_hashmap_free(Hashmap *m) {
3398 while ((i = hashmap_steal_first(m)))
3399 install_info_free(i);
3404 static int install_info_add(const char *name) {
3408 assert(will_install);
3410 if (!unit_name_is_valid_no_type(name, true)) {
3411 log_warning("Unit name %s is not a valid unit name.", name);
3415 if (hashmap_get(have_installed, name) ||
3416 hashmap_get(will_install, name))
3419 if (!(i = new0(InstallInfo, 1))) {
3424 if (!(i->name = strdup(name))) {
3429 if ((r = hashmap_put(will_install, i->name, i)) < 0)
3436 install_info_free(i);
3441 static int config_parse_also(
3442 const char *filename,
3444 const char *section,
3458 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
3462 if (!(n = strndup(w, l)))
3465 if ((r = install_info_add(n)) < 0) {
3466 log_warning("Cannot install unit %s: %s", n, strerror(-r));
3477 static int mark_symlink_for_removal(const char *p) {
3482 assert(path_is_absolute(p));
3484 if (!remove_symlinks_to)
3487 if (!(n = strdup(p)))
3490 path_kill_slashes(n);
3492 if ((r = set_put(remove_symlinks_to, n)) < 0) {
3494 return r == -EEXIST ? 0 : r;
3500 static int remove_marked_symlinks_fd(int fd, const char *config_path, const char *root, bool *deleted) {
3509 if (!(d = fdopendir(fd))) {
3510 close_nointr_nofail(fd);
3516 while ((de = readdir(d))) {
3517 bool is_dir = false, is_link = false;
3519 if (ignore_file(de->d_name))
3522 if (de->d_type == DT_LNK)
3524 else if (de->d_type == DT_DIR)
3526 else if (de->d_type == DT_UNKNOWN) {
3529 if (fstatat(fd, de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) {
3530 log_error("Failed to stat %s/%s: %m", root, de->d_name);
3537 is_link = S_ISLNK(st.st_mode);
3538 is_dir = S_ISDIR(st.st_mode);
3546 if ((nfd = openat(fd, de->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW)) < 0) {
3547 log_error("Failed to open %s/%s: %m", root, de->d_name);
3554 if (asprintf(&p, "%s/%s", root, de->d_name) < 0) {
3555 log_error("Failed to allocate directory string.");
3556 close_nointr_nofail(nfd);
3561 /* This will close nfd, regardless whether it succeeds or not */
3562 q = remove_marked_symlinks_fd(nfd, config_path, p, deleted);
3568 } else if (is_link) {
3572 if (asprintf(&p, "%s/%s", root, de->d_name) < 0) {
3573 log_error("Failed to allocate symlink string.");
3578 if ((q = readlink_and_make_absolute(p, &dest)) < 0) {
3579 log_error("Cannot read symlink %s: %s", p, strerror(-q));
3587 if ((c = canonicalize_file_name(dest))) {
3588 /* This might fail if the destination
3589 * is already removed */
3595 path_kill_slashes(dest);
3596 if (set_get(remove_symlinks_to, dest)) {
3599 log_info("rm '%s'", p);
3601 if (unlink(p) < 0) {
3602 log_error("Cannot unlink symlink %s: %m", p);
3607 rmdir_parents(p, config_path);
3608 path_kill_slashes(p);
3610 if (!set_get(remove_symlinks_to, p)) {
3612 if ((r = mark_symlink_for_removal(p)) < 0) {
3631 static int remove_marked_symlinks(const char *config_path) {
3635 assert(config_path);
3637 if (set_size(remove_symlinks_to) <= 0)
3640 if ((fd = open(config_path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW)) < 0)
3647 if ((cfd = dup(fd)) < 0) {
3652 /* This takes possession of cfd and closes it */
3653 if ((q = remove_marked_symlinks_fd(cfd, config_path, config_path, &deleted)) < 0) {
3659 close_nointr_nofail(fd);
3664 static int create_symlink(const char *verb, const char *old_path, const char *new_path) {
3671 if (streq(verb, "enable")) {
3674 mkdir_parents(new_path, 0755);
3676 if (symlink(old_path, new_path) >= 0) {
3679 log_info("ln -s '%s' '%s'", old_path, new_path);
3684 if (errno != EEXIST) {
3685 log_error("Cannot link %s to %s: %m", old_path, new_path);
3689 if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
3691 if (errno == EINVAL) {
3692 log_error("Cannot link %s to %s, file exists already and is not a symlink.", old_path, new_path);
3696 log_error("readlink() failed: %s", strerror(-r));
3700 if (streq(dest, old_path)) {
3706 log_error("Cannot link %s to %s, symlink exists already and points to %s.", old_path, new_path, dest);
3715 log_info("ln -s '%s' '%s'", old_path, new_path);
3717 if (symlink(old_path, new_path) >= 0)
3720 log_error("Cannot link %s to %s: %m", old_path, new_path);
3723 } else if (streq(verb, "disable")) {
3726 if ((r = mark_symlink_for_removal(old_path)) < 0)
3729 if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
3730 if (errno == ENOENT)
3733 if (errno == EINVAL) {
3734 log_warning("File %s not a symlink, ignoring.", old_path);
3738 log_error("readlink() failed: %s", strerror(-r));
3742 if (!streq(dest, old_path)) {
3743 log_warning("File %s not a symlink to %s but points to %s, ignoring.", new_path, old_path, dest);
3750 if ((r = mark_symlink_for_removal(new_path)) < 0)
3754 log_info("rm '%s'", new_path);
3756 if (unlink(new_path) >= 0)
3759 log_error("Cannot unlink %s: %m", new_path);
3762 } else if (streq(verb, "is-enabled")) {
3765 if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
3767 if (errno == ENOENT || errno == EINVAL)
3770 log_error("readlink() failed: %s", strerror(-r));
3774 if (streq(dest, old_path)) {
3782 assert_not_reached("Unknown action.");
3785 static int install_info_symlink_alias(const char *verb, InstallInfo *i, const char *config_path) {
3787 char *alias_path = NULL;
3792 assert(config_path);
3794 STRV_FOREACH(s, i->aliases) {
3797 if (!(alias_path = path_make_absolute(*s, config_path))) {
3798 log_error("Out of memory");
3803 if ((r = create_symlink(verb, i->path, alias_path)) != 0)
3806 if (streq(verb, "disable"))
3807 rmdir_parents(alias_path, config_path);
3817 static int install_info_symlink_wants(const char *verb, InstallInfo *i, const char *config_path) {
3819 char *alias_path = NULL;
3824 assert(config_path);
3826 STRV_FOREACH(s, i->wanted_by) {
3827 if (!unit_name_is_valid_no_type(*s, true)) {
3828 log_error("Invalid name %s.", *s);
3836 if (asprintf(&alias_path, "%s/%s.wants/%s", config_path, *s, i->name) < 0) {
3837 log_error("Out of memory");
3842 if ((r = create_symlink(verb, i->path, alias_path)) != 0)
3845 if (streq(verb, "disable"))
3846 rmdir_parents(alias_path, config_path);
3857 static int install_info_apply(const char *verb, LookupPaths *paths, InstallInfo *i, const char *config_path) {
3859 const ConfigItem items[] = {
3860 { "Alias", config_parse_strv, &i->aliases, "Install" },
3861 { "WantedBy", config_parse_strv, &i->wanted_by, "Install" },
3862 { "Also", config_parse_also, NULL, "Install" },
3864 { NULL, NULL, NULL, NULL }
3868 char *filename = NULL;
3875 STRV_FOREACH(p, paths->unit_path) {
3878 if (!(filename = path_make_absolute(i->name, *p))) {
3879 log_error("Out of memory");
3883 /* Ensure that we don't follow symlinks */
3884 if ((fd = open(filename, O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NOCTTY)) >= 0)
3885 if ((f = fdopen(fd, "re")))
3888 if (errno == ELOOP) {
3889 log_error("Refusing to operate on symlinks, please pass unit names or absolute paths to unit files.");
3894 if (errno != ENOENT) {
3895 log_error("Failed to open %s: %m", filename);
3905 #if defined(TARGET_FEDORA) && defined (HAVE_SYSV_COMPAT)
3907 if (endswith(i->name, ".service")) {
3911 if (asprintf(&sysv, SYSTEM_SYSVINIT_PATH "/%s", i->name) < 0) {
3912 log_error("Out of memory");
3916 sysv[strlen(sysv) - sizeof(".service") + 1] = 0;
3917 exists = access(sysv, F_OK) >= 0;
3923 const char *argv[] = {
3930 log_info("%s is not a native service, redirecting to /sbin/chkconfig.", i->name);
3932 argv[1] = file_name_from_path(sysv);
3934 streq(verb, "enable") ? "on" :
3935 streq(verb, "disable") ? "off" : NULL;
3937 log_info("Executing %s %s %s", argv[0], argv[1], strempty(argv[2]));
3939 if ((pid = fork()) < 0) {
3940 log_error("Failed to fork: %m");
3943 } else if (pid == 0) {
3944 execv(argv[0], (char**) argv);
3945 _exit(EXIT_FAILURE);
3950 if ((r = wait_for_terminate(pid, &status)) < 0)
3953 if (status.si_code == CLD_EXITED) {
3954 if (status.si_status == 0 && (streq(verb, "enable") || streq(verb, "disable")))
3957 return status.si_status == 0 ? 0 : -EINVAL;
3967 log_error("Couldn't find %s.", i->name);
3973 if ((r = config_parse(filename, f, NULL, items, true, i)) < 0) {
3978 n_symlinks += strv_length(i->aliases);
3979 n_symlinks += strv_length(i->wanted_by);
3983 if ((r = install_info_symlink_alias(verb, i, config_path)) != 0)
3986 if ((r = install_info_symlink_wants(verb, i, config_path)) != 0)
3989 if ((r = mark_symlink_for_removal(filename)) < 0)
3992 if ((r = remove_marked_symlinks(config_path)) < 0)
3998 static char *get_config_path(void) {
4000 if (arg_user && arg_global)
4001 return strdup(USER_CONFIG_UNIT_PATH);
4006 if (user_config_home(&p) < 0)
4012 return strdup(SYSTEM_CONFIG_UNIT_PATH);
4015 static int enable_unit(DBusConnection *bus, char **args, unsigned n) {
4019 char *config_path = NULL;
4022 const char *verb = args[0];
4024 dbus_error_init(&error);
4027 if ((r = lookup_paths_init(&paths, arg_user ? MANAGER_USER : MANAGER_SYSTEM)) < 0) {
4028 log_error("Failed to determine lookup paths: %s", strerror(-r));
4032 if (!(config_path = get_config_path())) {
4033 log_error("Failed to determine config path");
4038 will_install = hashmap_new(string_hash_func, string_compare_func);
4039 have_installed = hashmap_new(string_hash_func, string_compare_func);
4041 if (!will_install || !have_installed) {
4042 log_error("Failed to allocate unit sets.");
4047 if (!arg_defaults && streq(verb, "disable"))
4048 if (!(remove_symlinks_to = set_new(string_hash_func, string_compare_func))) {
4049 log_error("Failed to allocate symlink sets.");
4054 for (j = 1; j < n; j++)
4055 if ((r = install_info_add(args[j])) < 0) {
4056 log_warning("Cannot install unit %s: %s", args[j], strerror(-r));
4060 while ((i = hashmap_first(will_install))) {
4063 assert_se(hashmap_move_one(have_installed, will_install, i->name) == 0);
4065 if ((q = install_info_apply(verb, &paths, i, config_path)) != 0) {
4073 /* In test mode and found something */
4079 if (streq(verb, "is-enabled"))
4080 r = r > 0 ? 0 : -ENOENT;
4082 if (n_symlinks <= 0)
4083 log_warning("Unit files contain no applicable installation information. Ignoring.");
4086 /* Don't try to reload anything if the user asked us to not do this */
4088 /* Don't try to reload anything when updating a unit globally */
4090 /* Don't try to reload anything if we are called for system changes but the system wasn't booted with systemd */
4091 (arg_user || sd_booted() > 0) &&
4092 /* Don't try to reload anything if we are running in a chroot environment */
4093 (arg_user || running_in_chroot() <= 0) ) {
4096 if ((q = daemon_reload(bus, args, n)) < 0)
4102 install_info_hashmap_free(will_install);
4103 install_info_hashmap_free(have_installed);
4105 set_free_free(remove_symlinks_to);
4107 lookup_paths_free(&paths);
4114 static int systemctl_help(void) {
4116 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
4117 "Send control commands to or query the systemd manager.\n\n"
4118 " -h --help Show this help\n"
4119 " --version Show package version\n"
4120 " -t --type=TYPE List only units of a particular type\n"
4121 " -p --property=NAME Show only properties by this name\n"
4122 " -a --all Show all units/properties, including dead/empty ones\n"
4123 " --full Don't ellipsize unit names on output\n"
4124 " --fail When queueing a new job, fail if conflicting jobs are\n"
4126 " -q --quiet Suppress output\n"
4127 " --no-block Do not wait until operation finished\n"
4128 " --no-pager Do not pipe output into a pager.\n"
4129 " --system Connect to system manager\n"
4130 " --user Connect to user service manager\n"
4131 " --order When generating graph for dot, show only order\n"
4132 " --require When generating graph for dot, show only requirement\n"
4133 " --no-wall Don't send wall message before halt/power-off/reboot\n"
4134 " --global Enable/disable unit files globally\n"
4135 " --no-reload When enabling/disabling unit files, don't reload daemon\n"
4137 " --no-ask-password\n"
4138 " Do not ask for system passwords\n"
4139 " --kill-mode=MODE How to send signal\n"
4140 " --kill-who=WHO Who to send signal to\n"
4141 " -s --signal=SIGNAL Which signal to send\n"
4142 " -f --force When enabling unit files, override existing symlinks\n"
4143 " When shutting down, execute action immediately\n"
4144 " --defaults When disabling unit files, remove default symlinks only\n\n"
4146 " list-units List units\n"
4147 " start [NAME...] Start (activate) one or more units\n"
4148 " stop [NAME...] Stop (deactivate) one or more units\n"
4149 " reload [NAME...] Reload one or more units\n"
4150 " restart [NAME...] Start or restart one or more units\n"
4151 " try-restart [NAME...] Restart one or more units if active\n"
4152 " reload-or-restart [NAME...] Reload one or more units is possible,\n"
4153 " otherwise start or restart\n"
4154 " reload-or-try-restart [NAME...] Reload one or more units is possible,\n"
4155 " otherwise restart if active\n"
4156 " isolate [NAME] Start one unit and stop all others\n"
4157 " kill [NAME...] Send signal to processes of a unit\n"
4158 " is-active [NAME...] Check whether units are active\n"
4159 " status [NAME...|PID...] Show runtime status of one or more units\n"
4160 " show [NAME...|JOB...] Show properties of one or more\n"
4161 " units/jobs or the manager\n"
4162 " reset-failed [NAME...] Reset failed state for all, one, or more\n"
4164 " enable [NAME...] Enable one or more unit files\n"
4165 " disable [NAME...] Disable one or more unit files\n"
4166 " is-enabled [NAME...] Check whether unit files are enabled\n"
4167 " load [NAME...] Load one or more units\n"
4168 " list-jobs List jobs\n"
4169 " cancel [JOB...] Cancel all, one, or more jobs\n"
4170 " monitor Monitor unit/job changes\n"
4171 " dump Dump server status\n"
4172 " dot Dump dependency graph for dot(1)\n"
4173 " snapshot [NAME] Create a snapshot\n"
4174 " delete [NAME...] Remove one or more snapshots\n"
4175 " daemon-reload Reload systemd manager configuration\n"
4176 " daemon-reexec Reexecute systemd manager\n"
4177 " show-environment Dump environment\n"
4178 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
4179 " unset-environment [NAME...] Unset one or more environment variables\n"
4180 " default Enter system default mode\n"
4181 " rescue Enter system rescue mode\n"
4182 " emergency Enter system emergency mode\n"
4183 " halt Shut down and halt the system\n"
4184 " poweroff Shut down and power-off the system\n"
4185 " reboot Shut down and reboot the system\n"
4186 " kexec Shut down and reboot the system with kexec\n"
4187 " exit Ask for user instance termination\n",
4188 program_invocation_short_name);
4193 static int halt_help(void) {
4195 printf("%s [OPTIONS...]\n\n"
4196 "%s the system.\n\n"
4197 " --help Show this help\n"
4198 " --halt Halt the machine\n"
4199 " -p --poweroff Switch off the machine\n"
4200 " --reboot Reboot the machine\n"
4201 " -f --force Force immediate halt/power-off/reboot\n"
4202 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
4203 " -d --no-wtmp Don't write wtmp record\n"
4204 " -n --no-sync Don't sync before halt/power-off/reboot\n"
4205 " --no-wall Don't send wall message before halt/power-off/reboot\n",
4206 program_invocation_short_name,
4207 arg_action == ACTION_REBOOT ? "Reboot" :
4208 arg_action == ACTION_POWEROFF ? "Power off" :
4214 static int shutdown_help(void) {
4216 printf("%s [OPTIONS...] [TIME] [WALL...]\n\n"
4217 "Shut down the system.\n\n"
4218 " --help Show this help\n"
4219 " -H --halt Halt the machine\n"
4220 " -P --poweroff Power-off the machine\n"
4221 " -r --reboot Reboot the machine\n"
4222 " -h Equivalent to --poweroff, overriden by --halt\n"
4223 " -k Don't halt/power-off/reboot, just send warnings\n"
4224 " --no-wall Don't send wall message before halt/power-off/reboot\n"
4225 " -c Cancel a pending shutdown\n",
4226 program_invocation_short_name);
4231 static int telinit_help(void) {
4233 printf("%s [OPTIONS...] {COMMAND}\n\n"
4234 "Send control commands to the init daemon.\n\n"
4235 " --help Show this help\n"
4236 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
4238 " 0 Power-off the machine\n"
4239 " 6 Reboot the machine\n"
4240 " 2, 3, 4, 5 Start runlevelX.target unit\n"
4241 " 1, s, S Enter rescue mode\n"
4242 " q, Q Reload init daemon configuration\n"
4243 " u, U Reexecute init daemon\n",
4244 program_invocation_short_name);
4249 static int runlevel_help(void) {
4251 printf("%s [OPTIONS...]\n\n"
4252 "Prints the previous and current runlevel of the init system.\n\n"
4253 " --help Show this help\n",
4254 program_invocation_short_name);
4259 static int systemctl_parse_argv(int argc, char *argv[]) {
4280 static const struct option options[] = {
4281 { "help", no_argument, NULL, 'h' },
4282 { "version", no_argument, NULL, ARG_VERSION },
4283 { "type", required_argument, NULL, 't' },
4284 { "property", required_argument, NULL, 'p' },
4285 { "all", no_argument, NULL, 'a' },
4286 { "full", no_argument, NULL, ARG_FULL },
4287 { "fail", no_argument, NULL, ARG_FAIL },
4288 { "user", no_argument, NULL, ARG_USER },
4289 { "system", no_argument, NULL, ARG_SYSTEM },
4290 { "global", no_argument, NULL, ARG_GLOBAL },
4291 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
4292 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
4293 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4294 { "quiet", no_argument, NULL, 'q' },
4295 { "order", no_argument, NULL, ARG_ORDER },
4296 { "require", no_argument, NULL, ARG_REQUIRE },
4297 { "force", no_argument, NULL, 'f' },
4298 { "no-reload", no_argument, NULL, ARG_NO_RELOAD },
4299 { "defaults", no_argument, NULL, ARG_DEFAULTS },
4300 { "kill-mode", required_argument, NULL, ARG_KILL_MODE },
4301 { "kill-who", required_argument, NULL, ARG_KILL_WHO },
4302 { "signal", required_argument, NULL, 's' },
4303 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
4304 { NULL, 0, NULL, 0 }
4312 /* Only when running as systemctl we ask for passwords */
4313 arg_ask_password = true;
4315 while ((c = getopt_long(argc, argv, "ht:p:aqfs:", options, NULL)) >= 0) {
4324 puts(PACKAGE_STRING);
4326 puts(SYSTEMD_FEATURES);
4336 if (!(l = strv_append(arg_property, optarg)))
4339 strv_free(arg_property);
4342 /* If the user asked for a particular
4343 * property, show it to him, even if it is
4366 arg_no_block = true;
4370 arg_no_pager = true;
4378 arg_dot = DOT_ORDER;
4382 arg_dot = DOT_REQUIRE;
4398 arg_no_reload = true;
4407 arg_defaults = true;
4411 arg_kill_who = optarg;
4415 arg_kill_mode = optarg;
4419 if ((arg_signal = signal_from_string_try_harder(optarg)) < 0) {
4420 log_error("Failed to parse signal string %s.", optarg);
4425 case ARG_NO_ASK_PASSWORD:
4426 arg_ask_password = false;
4433 log_error("Unknown option code %c", c);
4441 static int halt_parse_argv(int argc, char *argv[]) {
4450 static const struct option options[] = {
4451 { "help", no_argument, NULL, ARG_HELP },
4452 { "halt", no_argument, NULL, ARG_HALT },
4453 { "poweroff", no_argument, NULL, 'p' },
4454 { "reboot", no_argument, NULL, ARG_REBOOT },
4455 { "force", no_argument, NULL, 'f' },
4456 { "wtmp-only", no_argument, NULL, 'w' },
4457 { "no-wtmp", no_argument, NULL, 'd' },
4458 { "no-sync", no_argument, NULL, 'n' },
4459 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4460 { NULL, 0, NULL, 0 }
4468 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
4469 if (runlevel == '0' || runlevel == '6')
4470 arg_immediate = true;
4472 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
4480 arg_action = ACTION_HALT;
4484 if (arg_action != ACTION_REBOOT)
4485 arg_action = ACTION_POWEROFF;
4489 arg_action = ACTION_REBOOT;
4493 arg_immediate = true;
4514 /* Compatibility nops */
4521 log_error("Unknown option code %c", c);
4526 if (optind < argc) {
4527 log_error("Too many arguments.");
4534 static int parse_time_spec(const char *t, usec_t *_u) {
4538 if (streq(t, "now"))
4540 else if (t[0] == '+') {
4543 if (safe_atou64(t + 1, &u) < 0)
4546 *_u = now(CLOCK_REALTIME) + USEC_PER_MINUTE * u;
4555 hour = strtol(t, &e, 10);
4556 if (errno != 0 || *e != ':' || hour < 0 || hour > 23)
4559 minute = strtol(e+1, &e, 10);
4560 if (errno != 0 || *e != 0 || minute < 0 || minute > 59)
4563 n = now(CLOCK_REALTIME);
4564 s = (time_t) (n / USEC_PER_SEC);
4567 assert_se(localtime_r(&s, &tm));
4569 tm.tm_hour = (int) hour;
4570 tm.tm_min = (int) minute;
4573 assert_se(s = mktime(&tm));
4575 *_u = (usec_t) s * USEC_PER_SEC;
4578 *_u += USEC_PER_DAY;
4584 static int shutdown_parse_argv(int argc, char *argv[]) {
4591 static const struct option options[] = {
4592 { "help", no_argument, NULL, ARG_HELP },
4593 { "halt", no_argument, NULL, 'H' },
4594 { "poweroff", no_argument, NULL, 'P' },
4595 { "reboot", no_argument, NULL, 'r' },
4596 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4597 { NULL, 0, NULL, 0 }
4605 while ((c = getopt_long(argc, argv, "HPrhkt:afFc", options, NULL)) >= 0) {
4613 arg_action = ACTION_HALT;
4617 arg_action = ACTION_POWEROFF;
4621 arg_action = ACTION_REBOOT;
4625 if (arg_action != ACTION_HALT)
4626 arg_action = ACTION_POWEROFF;
4639 /* Compatibility nops */
4643 arg_action = ACTION_CANCEL_SHUTDOWN;
4650 log_error("Unknown option code %c", c);
4655 if (argc > optind) {
4656 if ((r = parse_time_spec(argv[optind], &arg_when)) < 0) {
4657 log_error("Failed to parse time specification: %s", argv[optind]);
4661 arg_when = now(CLOCK_REALTIME) + USEC_PER_MINUTE;
4663 /* We skip the time argument */
4664 if (argc > optind + 1)
4665 arg_wall = argv + optind + 1;
4672 static int telinit_parse_argv(int argc, char *argv[]) {
4679 static const struct option options[] = {
4680 { "help", no_argument, NULL, ARG_HELP },
4681 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4682 { NULL, 0, NULL, 0 }
4685 static const struct {
4689 { '0', ACTION_POWEROFF },
4690 { '6', ACTION_REBOOT },
4691 { '1', ACTION_RESCUE },
4692 { '2', ACTION_RUNLEVEL2 },
4693 { '3', ACTION_RUNLEVEL3 },
4694 { '4', ACTION_RUNLEVEL4 },
4695 { '5', ACTION_RUNLEVEL5 },
4696 { 's', ACTION_RESCUE },
4697 { 'S', ACTION_RESCUE },
4698 { 'q', ACTION_RELOAD },
4699 { 'Q', ACTION_RELOAD },
4700 { 'u', ACTION_REEXEC },
4701 { 'U', ACTION_REEXEC }
4710 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4725 log_error("Unknown option code %c", c);
4730 if (optind >= argc) {
4735 if (optind + 1 < argc) {
4736 log_error("Too many arguments.");
4740 if (strlen(argv[optind]) != 1) {
4741 log_error("Expected single character argument.");
4745 for (i = 0; i < ELEMENTSOF(table); i++)
4746 if (table[i].from == argv[optind][0])
4749 if (i >= ELEMENTSOF(table)) {
4750 log_error("Unknown command %s.", argv[optind]);
4754 arg_action = table[i].to;
4761 static int runlevel_parse_argv(int argc, char *argv[]) {
4767 static const struct option options[] = {
4768 { "help", no_argument, NULL, ARG_HELP },
4769 { NULL, 0, NULL, 0 }
4777 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4788 log_error("Unknown option code %c", c);
4793 if (optind < argc) {
4794 log_error("Too many arguments.");
4801 static int parse_argv(int argc, char *argv[]) {
4805 if (program_invocation_short_name) {
4807 if (strstr(program_invocation_short_name, "halt")) {
4808 arg_action = ACTION_HALT;
4809 return halt_parse_argv(argc, argv);
4810 } else if (strstr(program_invocation_short_name, "poweroff")) {
4811 arg_action = ACTION_POWEROFF;
4812 return halt_parse_argv(argc, argv);
4813 } else if (strstr(program_invocation_short_name, "reboot")) {
4814 arg_action = ACTION_REBOOT;
4815 return halt_parse_argv(argc, argv);
4816 } else if (strstr(program_invocation_short_name, "shutdown")) {
4817 arg_action = ACTION_POWEROFF;
4818 return shutdown_parse_argv(argc, argv);
4819 } else if (strstr(program_invocation_short_name, "init")) {
4821 if (sd_booted() > 0) {
4822 arg_action = ACTION_INVALID;
4823 return telinit_parse_argv(argc, argv);
4825 /* Hmm, so some other init system is
4826 * running, we need to forward this
4827 * request to it. For now we simply
4828 * guess that it is Upstart. */
4830 execv("/lib/upstart/telinit", argv);
4832 log_error("Couldn't find an alternative telinit implementation to spawn.");
4836 } else if (strstr(program_invocation_short_name, "runlevel")) {
4837 arg_action = ACTION_RUNLEVEL;
4838 return runlevel_parse_argv(argc, argv);
4842 arg_action = ACTION_SYSTEMCTL;
4843 return systemctl_parse_argv(argc, argv);
4846 static int action_to_runlevel(void) {
4848 static const char table[_ACTION_MAX] = {
4849 [ACTION_HALT] = '0',
4850 [ACTION_POWEROFF] = '0',
4851 [ACTION_REBOOT] = '6',
4852 [ACTION_RUNLEVEL2] = '2',
4853 [ACTION_RUNLEVEL3] = '3',
4854 [ACTION_RUNLEVEL4] = '4',
4855 [ACTION_RUNLEVEL5] = '5',
4856 [ACTION_RESCUE] = '1'
4859 assert(arg_action < _ACTION_MAX);
4861 return table[arg_action];
4864 static int talk_upstart(void) {
4865 DBusMessage *m = NULL, *reply = NULL;
4867 int previous, rl, r;
4869 env1_buf[] = "RUNLEVEL=X",
4870 env2_buf[] = "PREVLEVEL=X";
4871 char *env1 = env1_buf, *env2 = env2_buf;
4872 const char *emit = "runlevel";
4873 dbus_bool_t b_false = FALSE;
4874 DBusMessageIter iter, sub;
4875 DBusConnection *bus;
4877 dbus_error_init(&error);
4879 if (!(rl = action_to_runlevel()))
4882 if (utmp_get_runlevel(&previous, NULL) < 0)
4885 if (!(bus = dbus_connection_open_private("unix:abstract=/com/ubuntu/upstart", &error))) {
4886 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
4891 log_error("Failed to connect to Upstart bus: %s", bus_error_message(&error));
4896 if ((r = bus_check_peercred(bus)) < 0) {
4897 log_error("Failed to verify owner of bus.");
4901 if (!(m = dbus_message_new_method_call(
4902 "com.ubuntu.Upstart",
4903 "/com/ubuntu/Upstart",
4904 "com.ubuntu.Upstart0_6",
4907 log_error("Could not allocate message.");
4912 dbus_message_iter_init_append(m, &iter);
4914 env1_buf[sizeof(env1_buf)-2] = rl;
4915 env2_buf[sizeof(env2_buf)-2] = previous;
4917 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
4918 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
4919 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
4920 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
4921 !dbus_message_iter_close_container(&iter, &sub) ||
4922 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
4923 log_error("Could not append arguments to message.");
4928 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
4930 if (error_is_no_service(&error)) {
4935 log_error("Failed to issue method call: %s", bus_error_message(&error));
4944 dbus_message_unref(m);
4947 dbus_message_unref(reply);
4950 dbus_connection_flush(bus);
4951 dbus_connection_close(bus);
4952 dbus_connection_unref(bus);
4955 dbus_error_free(&error);
4960 static int talk_initctl(void) {
4961 struct init_request request;
4965 if (!(rl = action_to_runlevel()))
4969 request.magic = INIT_MAGIC;
4970 request.sleeptime = 0;
4971 request.cmd = INIT_CMD_RUNLVL;
4972 request.runlevel = rl;
4974 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
4976 if (errno == ENOENT)
4979 log_error("Failed to open "INIT_FIFO": %m");
4984 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
4985 close_nointr_nofail(fd);
4988 log_error("Failed to write to "INIT_FIFO": %m");
4989 return errno ? -errno : -EIO;
4995 static int systemctl_main(DBusConnection *bus, int argc, char *argv[], DBusError *error) {
4997 static const struct {
5005 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
5007 { "list-units", LESS, 1, list_units },
5008 { "list-jobs", EQUAL, 1, list_jobs },
5009 { "clear-jobs", EQUAL, 1, daemon_reload },
5010 { "load", MORE, 2, load_unit },
5011 { "cancel", MORE, 2, cancel_job },
5012 { "start", MORE, 2, start_unit },
5013 { "stop", MORE, 2, start_unit },
5014 { "reload", MORE, 2, start_unit },
5015 { "restart", MORE, 2, start_unit },
5016 { "try-restart", MORE, 2, start_unit },
5017 { "reload-or-restart", MORE, 2, start_unit },
5018 { "reload-or-try-restart", MORE, 2, start_unit },
5019 { "force-reload", MORE, 2, start_unit }, /* For compatibility with SysV */
5020 { "condrestart", MORE, 2, start_unit }, /* For compatibility with RH */
5021 { "isolate", EQUAL, 2, start_unit },
5022 { "kill", MORE, 2, kill_unit },
5023 { "is-active", MORE, 2, check_unit },
5024 { "check", MORE, 2, check_unit },
5025 { "show", MORE, 1, show },
5026 { "status", MORE, 2, show },
5027 { "monitor", EQUAL, 1, monitor },
5028 { "dump", EQUAL, 1, dump },
5029 { "dot", EQUAL, 1, dot },
5030 { "snapshot", LESS, 2, snapshot },
5031 { "delete", MORE, 2, delete_snapshot },
5032 { "daemon-reload", EQUAL, 1, daemon_reload },
5033 { "daemon-reexec", EQUAL, 1, daemon_reload },
5034 { "show-environment", EQUAL, 1, show_enviroment },
5035 { "set-environment", MORE, 2, set_environment },
5036 { "unset-environment", MORE, 2, set_environment },
5037 { "halt", EQUAL, 1, start_special },
5038 { "poweroff", EQUAL, 1, start_special },
5039 { "reboot", EQUAL, 1, start_special },
5040 { "kexec", EQUAL, 1, start_special },
5041 { "default", EQUAL, 1, start_special },
5042 { "rescue", EQUAL, 1, start_special },
5043 { "emergency", EQUAL, 1, start_special },
5044 { "exit", EQUAL, 1, start_special },
5045 { "reset-failed", MORE, 1, reset_failed },
5046 { "enable", MORE, 2, enable_unit },
5047 { "disable", MORE, 2, enable_unit },
5048 { "is-enabled", MORE, 2, enable_unit }
5058 left = argc - optind;
5061 /* Special rule: no arguments means "list-units" */
5064 if (streq(argv[optind], "help")) {
5069 for (i = 0; i < ELEMENTSOF(verbs); i++)
5070 if (streq(argv[optind], verbs[i].verb))
5073 if (i >= ELEMENTSOF(verbs)) {
5074 log_error("Unknown operation %s", argv[optind]);
5079 switch (verbs[i].argc_cmp) {
5082 if (left != verbs[i].argc) {
5083 log_error("Invalid number of arguments.");
5090 if (left < verbs[i].argc) {
5091 log_error("Too few arguments.");
5098 if (left > verbs[i].argc) {
5099 log_error("Too many arguments.");
5106 assert_not_reached("Unknown comparison operator.");
5109 /* Require a bus connection for all operations but
5111 if (!streq(verbs[i].verb, "enable") &&
5112 !streq(verbs[i].verb, "disable") &&
5114 log_error("Failed to get D-Bus connection: %s", error->message);
5118 return verbs[i].dispatch(bus, argv + optind, left);
5121 static int send_shutdownd(usec_t t, char mode, bool warn, const char *message) {
5123 struct msghdr msghdr;
5125 union sockaddr_union sockaddr;
5126 struct shutdownd_command c;
5134 strncpy(c.wall_message, message, sizeof(c.wall_message));
5136 if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0)
5140 sockaddr.sa.sa_family = AF_UNIX;
5141 sockaddr.un.sun_path[0] = 0;
5142 strncpy(sockaddr.un.sun_path+1, "/org/freedesktop/systemd1/shutdownd", sizeof(sockaddr.un.sun_path)-1);
5145 iovec.iov_base = (char*) &c;
5146 iovec.iov_len = sizeof(c);
5149 msghdr.msg_name = &sockaddr;
5150 msghdr.msg_namelen = offsetof(struct sockaddr_un, sun_path) + 1 + sizeof("/org/freedesktop/systemd1/shutdownd") - 1;
5152 msghdr.msg_iov = &iovec;
5153 msghdr.msg_iovlen = 1;
5155 if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) < 0) {
5156 close_nointr_nofail(fd);
5160 close_nointr_nofail(fd);
5164 static int reload_with_fallback(DBusConnection *bus) {
5167 /* First, try systemd via D-Bus. */
5168 if (daemon_reload(bus, NULL, 0) > 0)
5172 /* Nothing else worked, so let's try signals */
5173 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
5175 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
5176 log_error("kill() failed: %m");
5183 static int start_with_fallback(DBusConnection *bus) {
5186 /* First, try systemd via D-Bus. */
5187 if (start_unit(bus, NULL, 0) >= 0)
5191 /* Hmm, talking to systemd via D-Bus didn't work. Then
5192 * let's try to talk to Upstart via D-Bus. */
5193 if (talk_upstart() > 0)
5196 /* Nothing else worked, so let's try
5198 if (talk_initctl() > 0)
5201 log_error("Failed to talk to init daemon.");
5205 warn_wall(arg_action);
5209 static int halt_main(DBusConnection *bus) {
5212 if (geteuid() != 0) {
5213 log_error("Must be root.");
5219 char date[FORMAT_TIMESTAMP_MAX];
5221 m = strv_join(arg_wall, " ");
5222 r = send_shutdownd(arg_when,
5223 arg_action == ACTION_HALT ? 'H' :
5224 arg_action == ACTION_POWEROFF ? 'P' :
5231 log_warning("Failed to talk to shutdownd, proceeding with immediate shutdown: %s", strerror(-r));
5233 log_info("Shutdown scheduled for %s, use 'shutdown -c' to cancel.",
5234 format_timestamp(date, sizeof(date), arg_when));
5239 if (!arg_dry && !arg_immediate)
5240 return start_with_fallback(bus);
5243 if (sd_booted() > 0)
5244 log_debug("Not writing utmp record, assuming that systemd-update-utmp is used.");
5245 else if ((r = utmp_put_shutdown(0)) < 0)
5246 log_warning("Failed to write utmp record: %s", strerror(-r));
5255 /* Make sure C-A-D is handled by the kernel from this
5257 reboot(RB_ENABLE_CAD);
5259 switch (arg_action) {
5262 log_info("Halting.");
5263 reboot(RB_HALT_SYSTEM);
5266 case ACTION_POWEROFF:
5267 log_info("Powering off.");
5268 reboot(RB_POWER_OFF);
5272 log_info("Rebooting.");
5273 reboot(RB_AUTOBOOT);
5277 assert_not_reached("Unknown halt action.");
5280 /* We should never reach this. */
5284 static int runlevel_main(void) {
5285 int r, runlevel, previous;
5287 if ((r = utmp_get_runlevel(&runlevel, &previous)) < 0) {
5288 printf("unknown\n");
5293 previous <= 0 ? 'N' : previous,
5294 runlevel <= 0 ? 'N' : runlevel);
5299 static void pager_open(void) {
5306 if (!on_tty() || arg_no_pager)
5309 if ((pager = getenv("PAGER")))
5310 if (!*pager || streq(pager, "cat"))
5314 log_error("Failed to create pager pipe: %m");
5319 if (pager_pid < 0) {
5320 log_error("Failed to fork pager: %m");
5325 /* In the child start the pager */
5326 if (pager_pid == 0) {
5328 dup2(fd[0], STDIN_FILENO);
5331 setenv("LESS", "FRSX", 0);
5333 prctl(PR_SET_PDEATHSIG, SIGTERM);
5336 execlp(pager, pager, NULL);
5337 execl("/bin/sh", "sh", "-c", pager, NULL);
5339 execlp("sensible-pager", "sensible-pager", NULL);
5340 execlp("less", "less", NULL);
5341 execlp("more", "more", NULL);
5344 log_error("Unable to execute pager: %m");
5345 _exit(EXIT_FAILURE);
5348 /* Return in the parent */
5349 if (dup2(fd[1], STDOUT_FILENO) < 0)
5350 log_error("Failed to duplicate pager pipe: %m");
5355 static void pager_close(void) {
5361 /* Inform pager that we are done */
5363 wait_for_terminate(pager_pid, &dummy);
5367 int main(int argc, char*argv[]) {
5368 int r, retval = EXIT_FAILURE;
5369 DBusConnection *bus = NULL;
5372 dbus_error_init(&error);
5374 log_parse_environment();
5377 if ((r = parse_argv(argc, argv)) < 0)
5380 retval = EXIT_SUCCESS;
5384 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
5385 * let's shortcut this */
5386 if (arg_action == ACTION_RUNLEVEL) {
5387 r = runlevel_main();
5388 retval = r < 0 ? EXIT_FAILURE : r;
5392 bus_connect(arg_user ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &bus, &private_bus, &error);
5394 switch (arg_action) {
5396 case ACTION_SYSTEMCTL:
5397 r = systemctl_main(bus, argc, argv, &error);
5401 case ACTION_POWEROFF:
5406 case ACTION_RUNLEVEL2:
5407 case ACTION_RUNLEVEL3:
5408 case ACTION_RUNLEVEL4:
5409 case ACTION_RUNLEVEL5:
5411 case ACTION_EMERGENCY:
5412 case ACTION_DEFAULT:
5413 r = start_with_fallback(bus);
5418 r = reload_with_fallback(bus);
5421 case ACTION_CANCEL_SHUTDOWN:
5422 r = send_shutdownd(0, 0, false, NULL);
5425 case ACTION_INVALID:
5426 case ACTION_RUNLEVEL:
5428 assert_not_reached("Unknown action");
5431 retval = r < 0 ? EXIT_FAILURE : r;
5436 dbus_connection_flush(bus);
5437 dbus_connection_close(bus);
5438 dbus_connection_unref(bus);
5441 dbus_error_free(&error);
5445 strv_free(arg_property);