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 int daemon_reload(DBusConnection *bus, char **args, unsigned n);
115 static bool on_tty(void) {
118 if (_unlikely_(t < 0))
119 t = isatty(STDOUT_FILENO) > 0;
124 static void spawn_ask_password_agent(void) {
127 /* We check STDIN here, not STDOUT, since this is about input,
129 if (!isatty(STDIN_FILENO))
132 if (!arg_ask_password)
137 /* Spawns a temporary TTY agent, making sure it goes away when
140 if ((child = fork()) < 0)
146 const char * const args[] = {
147 SYSTEMD_TTY_ASK_PASSWORD_AGENT_BINARY_PATH,
152 if (prctl(PR_SET_PDEATHSIG, SIGTERM) < 0)
155 /* Check whether our parent died before we were able
156 * to set the death signal */
157 if (getppid() != parent)
160 execv(args[0], (char **) args);
165 static const char *ansi_highlight(bool b) {
170 return b ? ANSI_HIGHLIGHT_ON : ANSI_HIGHLIGHT_OFF;
173 static const char *ansi_highlight_green(bool b) {
178 return b ? ANSI_HIGHLIGHT_GREEN_ON : ANSI_HIGHLIGHT_OFF;
181 static bool error_is_no_service(const DBusError *error) {
184 if (!dbus_error_is_set(error))
187 if (dbus_error_has_name(error, DBUS_ERROR_NAME_HAS_NO_OWNER))
190 if (dbus_error_has_name(error, DBUS_ERROR_SERVICE_UNKNOWN))
193 return startswith(error->name, "org.freedesktop.DBus.Error.Spawn.");
196 static int translate_bus_error_to_exit_status(int r, const DBusError *error) {
199 if (!dbus_error_is_set(error))
202 if (dbus_error_has_name(error, DBUS_ERROR_ACCESS_DENIED) ||
203 dbus_error_has_name(error, BUS_ERROR_ONLY_BY_DEPENDENCY) ||
204 dbus_error_has_name(error, BUS_ERROR_NO_ISOLATION) ||
205 dbus_error_has_name(error, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE))
206 return EXIT_NOPERMISSION;
208 if (dbus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT))
209 return EXIT_NOTINSTALLED;
211 if (dbus_error_has_name(error, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE) ||
212 dbus_error_has_name(error, BUS_ERROR_NOT_SUPPORTED))
213 return EXIT_NOTIMPLEMENTED;
215 if (dbus_error_has_name(error, BUS_ERROR_LOAD_FAILED))
216 return EXIT_NOTCONFIGURED;
224 static int bus_iter_get_basic_and_next(DBusMessageIter *iter, int type, void *data, bool next) {
229 if (dbus_message_iter_get_arg_type(iter) != type)
232 dbus_message_iter_get_basic(iter, data);
234 if (!dbus_message_iter_next(iter) != !next)
240 static void warn_wall(enum action action) {
241 static const char *table[_ACTION_MAX] = {
242 [ACTION_HALT] = "The system is going down for system halt NOW!",
243 [ACTION_REBOOT] = "The system is going down for reboot NOW!",
244 [ACTION_POWEROFF] = "The system is going down for power-off NOW!",
245 [ACTION_KEXEC] = "The system is going down for kexec reboot NOW!",
246 [ACTION_RESCUE] = "The system is going down to rescue mode NOW!",
247 [ACTION_EMERGENCY] = "The system is going down to emergency mode NOW!"
256 if (!(p = strv_join(arg_wall, " "))) {
257 log_error("Failed to join strings.");
273 utmp_wall(table[action], NULL);
278 const char *description;
279 const char *load_state;
280 const char *active_state;
281 const char *sub_state;
282 const char *following;
283 const char *unit_path;
285 const char *job_type;
286 const char *job_path;
289 static int compare_unit_info(const void *a, const void *b) {
291 const struct unit_info *u = a, *v = b;
293 d1 = strrchr(u->id, '.');
294 d2 = strrchr(v->id, '.');
299 if ((r = strcasecmp(d1, d2)) != 0)
303 return strcasecmp(u->id, v->id);
306 static bool output_show_job(const struct unit_info *u) {
309 return (!arg_type || ((dot = strrchr(u->id, '.')) &&
310 streq(dot+1, arg_type))) &&
311 (arg_all || !(streq(u->active_state, "inactive") || u->following[0]) || u->job_id > 0);
314 static void output_units_list(const struct unit_info *unit_infos, unsigned c) {
315 unsigned active_len, sub_len, job_len, n_shown = 0;
316 const struct unit_info *u;
318 active_len = sizeof("ACTIVE")-1;
319 sub_len = sizeof("SUB")-1;
320 job_len = sizeof("JOB")-1;
322 for (u = unit_infos; u < unit_infos + c; u++) {
323 if (!output_show_job(u))
326 active_len = MAX(active_len, strlen(u->active_state));
327 sub_len = MAX(sub_len, strlen(u->sub_state));
329 job_len = MAX(job_len, strlen(u->job_type));
333 printf("%-25s %-6s %-*s %-*s %-*s", "UNIT", "LOAD",
334 active_len, "ACTIVE", sub_len, "SUB", job_len, "JOB");
335 if (columns() >= 80+12 || arg_full)
336 printf(" %s\n", "DESCRIPTION");
341 for (u = unit_infos; u < unit_infos + c; u++) {
344 const char *on_loaded, *off_loaded;
345 const char *on_active, *off_active;
347 if (!output_show_job(u))
352 if (!streq(u->load_state, "loaded") &&
353 !streq(u->load_state, "banned")) {
354 on_loaded = ansi_highlight(true);
355 off_loaded = ansi_highlight(false);
357 on_loaded = off_loaded = "";
359 if (streq(u->active_state, "failed")) {
360 on_active = ansi_highlight(true);
361 off_active = ansi_highlight(false);
363 on_active = off_active = "";
365 e = arg_full ? NULL : ellipsize(u->id, 25, 33);
367 printf("%-25s %s%-6s%s %s%-*s %-*s%s%n",
369 on_loaded, u->load_state, off_loaded,
370 on_active, active_len, u->active_state,
371 sub_len, u->sub_state, off_active,
376 a -= strlen(on_loaded) + strlen(off_loaded);
377 a -= strlen(on_active) + strlen(off_active);
380 printf(" %-*s", job_len, u->job_type);
384 if (a + b + 1 < columns()) {
386 printf(" %-*s", job_len, "");
389 printf(" %s", u->description);
391 printf(" %.*s", columns() - a - b - 1, u->description);
398 printf("\nLOAD = Reflects whether the unit definition was properly loaded.\n"
399 "ACTIVE = The high-level unit activation state, i.e. generalization of SUB.\n"
400 "SUB = The low-level unit activation state, values depend on unit type.\n"
401 "JOB = Pending job for the unit.\n");
404 printf("\n%u units listed.\n", n_shown);
406 printf("\n%u units listed. Pass --all to see inactive units, too.\n", n_shown);
410 static int list_units(DBusConnection *bus, char **args, unsigned n) {
411 DBusMessage *m = NULL, *reply = NULL;
414 DBusMessageIter iter, sub, sub2;
415 unsigned c = 0, n_units = 0;
416 struct unit_info *unit_infos = NULL;
418 dbus_error_init(&error);
422 if (!(m = dbus_message_new_method_call(
423 "org.freedesktop.systemd1",
424 "/org/freedesktop/systemd1",
425 "org.freedesktop.systemd1.Manager",
427 log_error("Could not allocate message.");
431 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
432 log_error("Failed to issue method call: %s", bus_error_message(&error));
437 if (!dbus_message_iter_init(reply, &iter) ||
438 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
439 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
440 log_error("Failed to parse reply.");
445 dbus_message_iter_recurse(&iter, &sub);
447 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
450 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
451 log_error("Failed to parse reply.");
459 n_units = MAX(2*c, 16);
460 w = realloc(unit_infos, sizeof(struct unit_info) * n_units);
463 log_error("Failed to allocate unit array.");
473 dbus_message_iter_recurse(&sub, &sub2);
475 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->id, true) < 0 ||
476 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->description, true) < 0 ||
477 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->load_state, true) < 0 ||
478 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->active_state, true) < 0 ||
479 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->sub_state, true) < 0 ||
480 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->following, true) < 0 ||
481 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &u->unit_path, true) < 0 ||
482 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &u->job_id, true) < 0 ||
483 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->job_type, true) < 0 ||
484 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &u->job_path, false) < 0) {
485 log_error("Failed to parse reply.");
490 dbus_message_iter_next(&sub);
494 qsort(unit_infos, c, sizeof(struct unit_info), compare_unit_info);
495 output_units_list(unit_infos, c);
501 dbus_message_unref(m);
504 dbus_message_unref(reply);
508 dbus_error_free(&error);
513 static int dot_one_property(const char *name, const char *prop, DBusMessageIter *iter) {
514 static const char * const colors[] = {
515 "Requires", "[color=\"black\"]",
516 "RequiresOverridable", "[color=\"black\"]",
517 "Requisite", "[color=\"darkblue\"]",
518 "RequisiteOverridable", "[color=\"darkblue\"]",
519 "Wants", "[color=\"darkgrey\"]",
520 "Conflicts", "[color=\"red\"]",
521 "ConflictedBy", "[color=\"red\"]",
522 "After", "[color=\"green\"]"
525 const char *c = NULL;
532 for (i = 0; i < ELEMENTSOF(colors); i += 2)
533 if (streq(colors[i], prop)) {
541 if (arg_dot != DOT_ALL)
542 if ((arg_dot == DOT_ORDER) != streq(prop, "After"))
545 switch (dbus_message_iter_get_arg_type(iter)) {
547 case DBUS_TYPE_ARRAY:
549 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
552 dbus_message_iter_recurse(iter, &sub);
554 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
557 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
558 dbus_message_iter_get_basic(&sub, &s);
559 printf("\t\"%s\"->\"%s\" %s;\n", name, s, c);
561 dbus_message_iter_next(&sub);
571 static int dot_one(DBusConnection *bus, const char *name, const char *path) {
572 DBusMessage *m = NULL, *reply = NULL;
573 const char *interface = "org.freedesktop.systemd1.Unit";
576 DBusMessageIter iter, sub, sub2, sub3;
581 dbus_error_init(&error);
583 if (!(m = dbus_message_new_method_call(
584 "org.freedesktop.systemd1",
586 "org.freedesktop.DBus.Properties",
588 log_error("Could not allocate message.");
593 if (!dbus_message_append_args(m,
594 DBUS_TYPE_STRING, &interface,
595 DBUS_TYPE_INVALID)) {
596 log_error("Could not append arguments to message.");
601 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
602 log_error("Failed to issue method call: %s", bus_error_message(&error));
607 if (!dbus_message_iter_init(reply, &iter) ||
608 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
609 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
610 log_error("Failed to parse reply.");
615 dbus_message_iter_recurse(&iter, &sub);
617 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
620 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
621 log_error("Failed to parse reply.");
626 dbus_message_iter_recurse(&sub, &sub2);
628 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &prop, true) < 0) {
629 log_error("Failed to parse reply.");
634 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
635 log_error("Failed to parse reply.");
640 dbus_message_iter_recurse(&sub2, &sub3);
642 if (dot_one_property(name, prop, &sub3)) {
643 log_error("Failed to parse reply.");
648 dbus_message_iter_next(&sub);
655 dbus_message_unref(m);
658 dbus_message_unref(reply);
660 dbus_error_free(&error);
665 static int dot(DBusConnection *bus, char **args, unsigned n) {
666 DBusMessage *m = NULL, *reply = NULL;
669 DBusMessageIter iter, sub, sub2;
671 dbus_error_init(&error);
675 if (!(m = dbus_message_new_method_call(
676 "org.freedesktop.systemd1",
677 "/org/freedesktop/systemd1",
678 "org.freedesktop.systemd1.Manager",
680 log_error("Could not allocate message.");
684 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
685 log_error("Failed to issue method call: %s", bus_error_message(&error));
690 if (!dbus_message_iter_init(reply, &iter) ||
691 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
692 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
693 log_error("Failed to parse reply.");
698 printf("digraph systemd {\n");
700 dbus_message_iter_recurse(&iter, &sub);
701 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
702 const char *id, *description, *load_state, *active_state, *sub_state, *following, *unit_path;
704 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
705 log_error("Failed to parse reply.");
710 dbus_message_iter_recurse(&sub, &sub2);
712 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
713 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
714 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
715 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
716 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
717 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &following, true) < 0 ||
718 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, true) < 0) {
719 log_error("Failed to parse reply.");
724 if ((r = dot_one(bus, id, unit_path)) < 0)
727 /* printf("\t\"%s\";\n", id); */
728 dbus_message_iter_next(&sub);
733 log_info(" Color legend: black = Requires\n"
734 " dark blue = Requisite\n"
735 " dark grey = Wants\n"
739 if (isatty(fileno(stdout)))
740 log_notice("-- You probably want to process this output with graphviz' dot tool.\n"
741 "-- Try a shell pipeline like 'systemctl dot | dot -Tsvg > systemd.svg'!\n");
747 dbus_message_unref(m);
750 dbus_message_unref(reply);
752 dbus_error_free(&error);
757 static int list_jobs(DBusConnection *bus, char **args, unsigned n) {
758 DBusMessage *m = NULL, *reply = NULL;
761 DBusMessageIter iter, sub, sub2;
764 dbus_error_init(&error);
768 if (!(m = dbus_message_new_method_call(
769 "org.freedesktop.systemd1",
770 "/org/freedesktop/systemd1",
771 "org.freedesktop.systemd1.Manager",
773 log_error("Could not allocate message.");
777 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
778 log_error("Failed to issue method call: %s", bus_error_message(&error));
783 if (!dbus_message_iter_init(reply, &iter) ||
784 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
785 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
786 log_error("Failed to parse reply.");
791 dbus_message_iter_recurse(&iter, &sub);
793 if (isatty(STDOUT_FILENO))
794 printf("%4s %-25s %-15s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
796 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
797 const char *name, *type, *state, *job_path, *unit_path;
801 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
802 log_error("Failed to parse reply.");
807 dbus_message_iter_recurse(&sub, &sub2);
809 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
810 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
811 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
812 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
813 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
814 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
815 log_error("Failed to parse reply.");
820 e = arg_full ? NULL : ellipsize(name, 25, 33);
821 printf("%4u %-25s %-15s %-7s\n", id, e ? e : name, type, state);
826 dbus_message_iter_next(&sub);
829 if (isatty(STDOUT_FILENO))
830 printf("\n%u jobs listed.\n", k);
836 dbus_message_unref(m);
839 dbus_message_unref(reply);
841 dbus_error_free(&error);
846 static int load_unit(DBusConnection *bus, char **args, unsigned n) {
847 DBusMessage *m = NULL, *reply = NULL;
852 dbus_error_init(&error);
857 for (i = 1; i < n; i++) {
859 if (!(m = dbus_message_new_method_call(
860 "org.freedesktop.systemd1",
861 "/org/freedesktop/systemd1",
862 "org.freedesktop.systemd1.Manager",
864 log_error("Could not allocate message.");
869 if (!dbus_message_append_args(m,
870 DBUS_TYPE_STRING, &args[i],
871 DBUS_TYPE_INVALID)) {
872 log_error("Could not append arguments to message.");
877 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
878 log_error("Failed to issue method call: %s", bus_error_message(&error));
883 dbus_message_unref(m);
884 dbus_message_unref(reply);
893 dbus_message_unref(m);
896 dbus_message_unref(reply);
898 dbus_error_free(&error);
903 static int cancel_job(DBusConnection *bus, char **args, unsigned n) {
904 DBusMessage *m = NULL, *reply = NULL;
909 dbus_error_init(&error);
915 return daemon_reload(bus, args, n);
917 for (i = 1; i < n; i++) {
921 if (!(m = dbus_message_new_method_call(
922 "org.freedesktop.systemd1",
923 "/org/freedesktop/systemd1",
924 "org.freedesktop.systemd1.Manager",
926 log_error("Could not allocate message.");
931 if ((r = safe_atou(args[i], &id)) < 0) {
932 log_error("Failed to parse job id: %s", strerror(-r));
936 assert_cc(sizeof(uint32_t) == sizeof(id));
937 if (!dbus_message_append_args(m,
938 DBUS_TYPE_UINT32, &id,
939 DBUS_TYPE_INVALID)) {
940 log_error("Could not append arguments to message.");
945 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
946 log_error("Failed to issue method call: %s", bus_error_message(&error));
951 if (!dbus_message_get_args(reply, &error,
952 DBUS_TYPE_OBJECT_PATH, &path,
953 DBUS_TYPE_INVALID)) {
954 log_error("Failed to parse reply: %s", bus_error_message(&error));
959 dbus_message_unref(m);
960 if (!(m = dbus_message_new_method_call(
961 "org.freedesktop.systemd1",
963 "org.freedesktop.systemd1.Job",
965 log_error("Could not allocate message.");
970 dbus_message_unref(reply);
971 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
972 log_error("Failed to issue method call: %s", bus_error_message(&error));
977 dbus_message_unref(m);
978 dbus_message_unref(reply);
986 dbus_message_unref(m);
989 dbus_message_unref(reply);
991 dbus_error_free(&error);
996 static bool need_daemon_reload(DBusConnection *bus, const char *unit) {
997 DBusMessage *m = NULL, *reply = NULL;
998 dbus_bool_t b = FALSE;
999 DBusMessageIter iter, sub;
1001 *interface = "org.freedesktop.systemd1.Unit",
1002 *property = "NeedDaemonReload",
1005 /* We ignore all errors here, since this is used to show a warning only */
1007 if (!(m = dbus_message_new_method_call(
1008 "org.freedesktop.systemd1",
1009 "/org/freedesktop/systemd1",
1010 "org.freedesktop.systemd1.Manager",
1014 if (!dbus_message_append_args(m,
1015 DBUS_TYPE_STRING, &unit,
1019 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
1022 if (!dbus_message_get_args(reply, NULL,
1023 DBUS_TYPE_OBJECT_PATH, &path,
1027 dbus_message_unref(m);
1028 if (!(m = dbus_message_new_method_call(
1029 "org.freedesktop.systemd1",
1031 "org.freedesktop.DBus.Properties",
1035 if (!dbus_message_append_args(m,
1036 DBUS_TYPE_STRING, &interface,
1037 DBUS_TYPE_STRING, &property,
1038 DBUS_TYPE_INVALID)) {
1042 dbus_message_unref(reply);
1043 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
1046 if (!dbus_message_iter_init(reply, &iter) ||
1047 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
1050 dbus_message_iter_recurse(&iter, &sub);
1052 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_BOOLEAN)
1055 dbus_message_iter_get_basic(&sub, &b);
1059 dbus_message_unref(m);
1062 dbus_message_unref(reply);
1067 typedef struct WaitData {
1072 static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
1080 dbus_error_init(&error);
1082 log_debug("Got D-Bus request: %s.%s() on %s",
1083 dbus_message_get_interface(message),
1084 dbus_message_get_member(message),
1085 dbus_message_get_path(message));
1087 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
1088 log_error("Warning! D-Bus connection terminated.");
1089 dbus_connection_close(connection);
1091 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
1094 dbus_bool_t success = true;
1096 if (!dbus_message_get_args(message, &error,
1097 DBUS_TYPE_UINT32, &id,
1098 DBUS_TYPE_OBJECT_PATH, &path,
1099 DBUS_TYPE_BOOLEAN, &success,
1101 log_error("Failed to parse message: %s", bus_error_message(&error));
1105 if ((p = set_remove(d->set, (char*) path)))
1113 dbus_error_free(&error);
1114 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1117 static int enable_wait_for_jobs(DBusConnection *bus) {
1125 dbus_error_init(&error);
1126 dbus_bus_add_match(bus,
1128 "sender='org.freedesktop.systemd1',"
1129 "interface='org.freedesktop.systemd1.Manager',"
1130 "member='JobRemoved',"
1131 "path='/org/freedesktop/systemd1'",
1134 if (dbus_error_is_set(&error)) {
1135 log_error("Failed to add match: %s", bus_error_message(&error));
1136 dbus_error_free(&error);
1140 /* This is slightly dirty, since we don't undo the match registrations. */
1144 static int wait_for_jobs(DBusConnection *bus, Set *s) {
1155 if (!dbus_connection_add_filter(bus, wait_filter, &d, NULL)) {
1156 log_error("Failed to add filter.");
1161 while (!set_isempty(s) &&
1162 dbus_connection_read_write_dispatch(bus, -1))
1165 if (!arg_quiet && d.failed)
1166 log_error("Job failed. See system logs and 'systemctl status' for details.");
1168 r = d.failed ? -EIO : 0;
1171 /* This is slightly dirty, since we don't undo the filter registration. */
1176 static int start_unit_one(
1177 DBusConnection *bus,
1184 DBusMessage *m = NULL, *reply = NULL;
1193 assert(arg_no_block || s);
1195 if (!(m = dbus_message_new_method_call(
1196 "org.freedesktop.systemd1",
1197 "/org/freedesktop/systemd1",
1198 "org.freedesktop.systemd1.Manager",
1200 log_error("Could not allocate message.");
1205 if (!dbus_message_append_args(m,
1206 DBUS_TYPE_STRING, &name,
1207 DBUS_TYPE_STRING, &mode,
1208 DBUS_TYPE_INVALID)) {
1209 log_error("Could not append arguments to message.");
1214 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, error))) {
1216 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(error)) {
1217 /* There's always a fallback possible for
1218 * legacy actions. */
1223 log_error("Failed to issue method call: %s", bus_error_message(error));
1228 if (!dbus_message_get_args(reply, error,
1229 DBUS_TYPE_OBJECT_PATH, &path,
1230 DBUS_TYPE_INVALID)) {
1231 log_error("Failed to parse reply: %s", bus_error_message(error));
1236 if (need_daemon_reload(bus, name))
1237 log_warning("Unit file of created job changed on disk, 'systemctl %s daemon-reload' recommended.",
1238 arg_user ? "--user" : "--system");
1240 if (!arg_no_block) {
1243 if (!(p = strdup(path))) {
1244 log_error("Failed to duplicate path.");
1249 if ((r = set_put(s, p)) < 0) {
1251 log_error("Failed to add path to set.");
1260 dbus_message_unref(m);
1263 dbus_message_unref(reply);
1268 static enum action verb_to_action(const char *verb) {
1269 if (streq(verb, "halt"))
1271 else if (streq(verb, "poweroff"))
1272 return ACTION_POWEROFF;
1273 else if (streq(verb, "reboot"))
1274 return ACTION_REBOOT;
1275 else if (streq(verb, "kexec"))
1276 return ACTION_KEXEC;
1277 else if (streq(verb, "rescue"))
1278 return ACTION_RESCUE;
1279 else if (streq(verb, "emergency"))
1280 return ACTION_EMERGENCY;
1281 else if (streq(verb, "default"))
1282 return ACTION_DEFAULT;
1283 else if (streq(verb, "exit"))
1286 return ACTION_INVALID;
1289 static int start_unit(DBusConnection *bus, char **args, unsigned n) {
1291 static const char * const table[_ACTION_MAX] = {
1292 [ACTION_HALT] = SPECIAL_HALT_TARGET,
1293 [ACTION_POWEROFF] = SPECIAL_POWEROFF_TARGET,
1294 [ACTION_REBOOT] = SPECIAL_REBOOT_TARGET,
1295 [ACTION_KEXEC] = SPECIAL_KEXEC_TARGET,
1296 [ACTION_RUNLEVEL2] = SPECIAL_RUNLEVEL2_TARGET,
1297 [ACTION_RUNLEVEL3] = SPECIAL_RUNLEVEL3_TARGET,
1298 [ACTION_RUNLEVEL4] = SPECIAL_RUNLEVEL4_TARGET,
1299 [ACTION_RUNLEVEL5] = SPECIAL_RUNLEVEL5_TARGET,
1300 [ACTION_RESCUE] = SPECIAL_RESCUE_TARGET,
1301 [ACTION_EMERGENCY] = SPECIAL_EMERGENCY_TARGET,
1302 [ACTION_DEFAULT] = SPECIAL_DEFAULT_TARGET,
1303 [ACTION_EXIT] = SPECIAL_EXIT_TARGET
1308 const char *method, *mode, *one_name;
1312 dbus_error_init(&error);
1316 spawn_ask_password_agent();
1318 if (arg_action == ACTION_SYSTEMCTL) {
1320 streq(args[0], "stop") ? "StopUnit" :
1321 streq(args[0], "reload") ? "ReloadUnit" :
1322 streq(args[0], "restart") ? "RestartUnit" :
1323 streq(args[0], "try-restart") ||
1324 streq(args[0], "condrestart") ? "TryRestartUnit" :
1325 streq(args[0], "reload-or-restart") ? "ReloadOrRestartUnit" :
1326 streq(args[0], "reload-or-try-restart") ||
1327 streq(args[0], "force-reload") ? "ReloadOrTryRestartUnit" :
1331 (streq(args[0], "isolate") ||
1332 streq(args[0], "rescue") ||
1333 streq(args[0], "emergency")) ? "isolate" :
1337 one_name = table[verb_to_action(args[0])];
1340 assert(arg_action < ELEMENTSOF(table));
1341 assert(table[arg_action]);
1343 method = "StartUnit";
1345 mode = (arg_action == ACTION_EMERGENCY ||
1346 arg_action == ACTION_RESCUE ||
1347 arg_action == ACTION_RUNLEVEL2 ||
1348 arg_action == ACTION_RUNLEVEL3 ||
1349 arg_action == ACTION_RUNLEVEL4 ||
1350 arg_action == ACTION_RUNLEVEL5) ? "isolate" : "replace";
1352 one_name = table[arg_action];
1355 if (!arg_no_block) {
1356 if ((ret = enable_wait_for_jobs(bus)) < 0) {
1357 log_error("Could not watch jobs: %s", strerror(-ret));
1361 if (!(s = set_new(string_hash_func, string_compare_func))) {
1362 log_error("Failed to allocate set.");
1369 if ((ret = start_unit_one(bus, method, one_name, mode, &error, s)) <= 0)
1372 for (i = 1; i < n; i++)
1373 if ((r = start_unit_one(bus, method, args[i], mode, &error, s)) != 0) {
1374 ret = translate_bus_error_to_exit_status(r, &error);
1375 dbus_error_free(&error);
1380 if ((r = wait_for_jobs(bus, s)) < 0) {
1389 dbus_error_free(&error);
1394 static int start_special(DBusConnection *bus, char **args, unsigned n) {
1401 (streq(args[0], "halt") ||
1402 streq(args[0], "poweroff") ||
1403 streq(args[0], "reboot") ||
1404 streq(args[0], "kexec") ||
1405 streq(args[0], "exit")))
1406 return daemon_reload(bus, args, n);
1408 r = start_unit(bus, args, n);
1411 warn_wall(verb_to_action(args[0]));
1416 static int check_unit(DBusConnection *bus, char **args, unsigned n) {
1417 DBusMessage *m = NULL, *reply = NULL;
1419 *interface = "org.freedesktop.systemd1.Unit",
1420 *property = "ActiveState";
1421 int r = 3; /* According to LSB: "program is not running" */
1428 dbus_error_init(&error);
1430 for (i = 1; i < n; i++) {
1431 const char *path = NULL;
1433 DBusMessageIter iter, sub;
1435 if (!(m = dbus_message_new_method_call(
1436 "org.freedesktop.systemd1",
1437 "/org/freedesktop/systemd1",
1438 "org.freedesktop.systemd1.Manager",
1440 log_error("Could not allocate message.");
1445 if (!dbus_message_append_args(m,
1446 DBUS_TYPE_STRING, &args[i],
1447 DBUS_TYPE_INVALID)) {
1448 log_error("Could not append arguments to message.");
1453 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1455 /* Hmm, cannot figure out anything about this unit... */
1459 dbus_error_free(&error);
1460 dbus_message_unref(m);
1464 if (!dbus_message_get_args(reply, &error,
1465 DBUS_TYPE_OBJECT_PATH, &path,
1466 DBUS_TYPE_INVALID)) {
1467 log_error("Failed to parse reply: %s", bus_error_message(&error));
1472 dbus_message_unref(m);
1473 if (!(m = dbus_message_new_method_call(
1474 "org.freedesktop.systemd1",
1476 "org.freedesktop.DBus.Properties",
1478 log_error("Could not allocate message.");
1483 if (!dbus_message_append_args(m,
1484 DBUS_TYPE_STRING, &interface,
1485 DBUS_TYPE_STRING, &property,
1486 DBUS_TYPE_INVALID)) {
1487 log_error("Could not append arguments to message.");
1492 dbus_message_unref(reply);
1493 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1494 log_error("Failed to issue method call: %s", bus_error_message(&error));
1499 if (!dbus_message_iter_init(reply, &iter) ||
1500 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1501 log_error("Failed to parse reply.");
1506 dbus_message_iter_recurse(&iter, &sub);
1508 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1509 log_error("Failed to parse reply.");
1514 dbus_message_iter_get_basic(&sub, &state);
1519 if (streq(state, "active") || streq(state, "reloading"))
1522 dbus_message_unref(m);
1523 dbus_message_unref(reply);
1529 dbus_message_unref(m);
1532 dbus_message_unref(reply);
1534 dbus_error_free(&error);
1539 static int kill_unit(DBusConnection *bus, char **args, unsigned n) {
1540 DBusMessage *m = NULL, *reply = NULL;
1548 dbus_error_init(&error);
1551 arg_kill_who = "all";
1554 arg_kill_mode = streq(arg_kill_who, "all") ? "control-group" : "process";
1556 for (i = 1; i < n; i++) {
1558 if (!(m = dbus_message_new_method_call(
1559 "org.freedesktop.systemd1",
1560 "/org/freedesktop/systemd1",
1561 "org.freedesktop.systemd1.Manager",
1563 log_error("Could not allocate message.");
1568 if (!dbus_message_append_args(m,
1569 DBUS_TYPE_STRING, &args[i],
1570 DBUS_TYPE_STRING, &arg_kill_who,
1571 DBUS_TYPE_STRING, &arg_kill_mode,
1572 DBUS_TYPE_INT32, &arg_signal,
1573 DBUS_TYPE_INVALID)) {
1574 log_error("Could not append arguments to message.");
1579 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1580 log_error("Failed to issue method call: %s", bus_error_message(&error));
1581 dbus_error_free(&error);
1585 dbus_message_unref(m);
1588 dbus_message_unref(reply);
1594 dbus_message_unref(m);
1597 dbus_message_unref(reply);
1599 dbus_error_free(&error);
1604 typedef struct ExecStatusInfo {
1610 usec_t start_timestamp;
1611 usec_t exit_timestamp;
1616 LIST_FIELDS(struct ExecStatusInfo, exec);
1619 static void exec_status_info_free(ExecStatusInfo *i) {
1627 static int exec_status_info_deserialize(DBusMessageIter *sub, ExecStatusInfo *i) {
1628 uint64_t start_timestamp, exit_timestamp;
1629 DBusMessageIter sub2, sub3;
1633 int32_t code, status;
1639 if (dbus_message_iter_get_arg_type(sub) != DBUS_TYPE_STRUCT)
1642 dbus_message_iter_recurse(sub, &sub2);
1644 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
1647 if (!(i->path = strdup(path)))
1650 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
1651 dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
1655 dbus_message_iter_recurse(&sub2, &sub3);
1656 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1657 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1658 dbus_message_iter_next(&sub3);
1663 if (!(i->argv = new0(char*, n+1)))
1667 dbus_message_iter_recurse(&sub2, &sub3);
1668 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1671 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1672 dbus_message_iter_get_basic(&sub3, &s);
1673 dbus_message_iter_next(&sub3);
1675 if (!(i->argv[n++] = strdup(s)))
1679 if (!dbus_message_iter_next(&sub2) ||
1680 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, true) < 0 ||
1681 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp, true) < 0 ||
1682 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp, true) < 0 ||
1683 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) < 0 ||
1684 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) < 0 ||
1685 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) < 0)
1689 i->start_timestamp = (usec_t) start_timestamp;
1690 i->exit_timestamp = (usec_t) exit_timestamp;
1691 i->pid = (pid_t) pid;
1698 typedef struct UnitStatusInfo {
1700 const char *load_state;
1701 const char *active_state;
1702 const char *sub_state;
1704 const char *description;
1705 const char *following;
1708 const char *default_control_group;
1710 usec_t inactive_exit_timestamp;
1711 usec_t active_enter_timestamp;
1712 usec_t active_exit_timestamp;
1713 usec_t inactive_enter_timestamp;
1715 bool need_daemon_reload;
1720 const char *status_text;
1722 #ifdef HAVE_SYSV_COMPAT
1726 usec_t start_timestamp;
1727 usec_t exit_timestamp;
1729 int exit_code, exit_status;
1732 unsigned n_accepted;
1733 unsigned n_connections;
1737 const char *sysfs_path;
1739 /* Mount, Automount */
1745 LIST_HEAD(ExecStatusInfo, exec);
1748 static void print_status_info(UnitStatusInfo *i) {
1750 const char *on, *off, *ss;
1752 char since1[FORMAT_TIMESTAMP_PRETTY_MAX], *s1;
1753 char since2[FORMAT_TIMESTAMP_MAX], *s2;
1757 /* This shows pretty information about a unit. See
1758 * print_property() for a low-level property printer */
1760 printf("%s", strna(i->id));
1762 if (i->description && !streq_ptr(i->id, i->description))
1763 printf(" - %s", i->description);
1768 printf("\t Follow: unit currently follows state of %s\n", i->following);
1770 if (streq_ptr(i->load_state, "failed") ||
1771 streq_ptr(i->load_state, "banned")) {
1772 on = ansi_highlight(true);
1773 off = ansi_highlight(false);
1778 printf("\t Loaded: %s%s%s (%s)\n", on, strna(i->load_state), off, i->path);
1780 printf("\t Loaded: %s%s%s\n", on, strna(i->load_state), off);
1782 ss = streq_ptr(i->active_state, i->sub_state) ? NULL : i->sub_state;
1784 if (streq_ptr(i->active_state, "failed")) {
1785 on = ansi_highlight(true);
1786 off = ansi_highlight(false);
1787 } else if (streq_ptr(i->active_state, "active") || streq_ptr(i->active_state, "reloading")) {
1788 on = ansi_highlight_green(true);
1789 off = ansi_highlight_green(false);
1794 printf("\t Active: %s%s (%s)%s",
1796 strna(i->active_state),
1800 printf("\t Active: %s%s%s",
1802 strna(i->active_state),
1805 timestamp = (streq_ptr(i->active_state, "active") ||
1806 streq_ptr(i->active_state, "reloading")) ? i->active_enter_timestamp :
1807 (streq_ptr(i->active_state, "inactive") ||
1808 streq_ptr(i->active_state, "failed")) ? i->inactive_enter_timestamp :
1809 streq_ptr(i->active_state, "activating") ? i->inactive_exit_timestamp :
1810 i->active_exit_timestamp;
1812 s1 = format_timestamp_pretty(since1, sizeof(since1), timestamp);
1813 s2 = format_timestamp(since2, sizeof(since2), timestamp);
1816 printf(" since %s; %s\n", s2, s1);
1818 printf(" since %s\n", s2);
1823 printf("\t Device: %s\n", i->sysfs_path);
1825 printf("\t Where: %s\n", i->where);
1827 printf("\t What: %s\n", i->what);
1830 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
1832 LIST_FOREACH(exec, p, i->exec) {
1835 /* Only show exited processes here */
1839 t = strv_join(p->argv, " ");
1840 printf("\t Process: %u (%s, code=%s, ", p->pid, strna(t), sigchld_code_to_string(p->code));
1843 if (p->code == CLD_EXITED) {
1846 printf("status=%i", p->status);
1848 #ifdef HAVE_SYSV_COMPAT
1849 if ((c = exit_status_to_string(p->status, i->is_sysv ? EXIT_STATUS_LSB : EXIT_STATUS_SYSTEMD)))
1851 if ((c = exit_status_to_string(p->status, EXIT_STATUS_SYSTEMD)))
1856 printf("signal=%s", signal_to_string(p->status));
1859 if (i->main_pid == p->pid &&
1860 i->start_timestamp == p->start_timestamp &&
1861 i->exit_timestamp == p->start_timestamp)
1862 /* Let's not show this twice */
1865 if (p->pid == i->control_pid)
1869 if (i->main_pid > 0 || i->control_pid > 0) {
1872 if (i->main_pid > 0) {
1873 printf("Main PID: %u", (unsigned) i->main_pid);
1877 get_process_name(i->main_pid, &t);
1882 } else if (i->exit_code > 0) {
1883 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
1885 if (i->exit_code == CLD_EXITED) {
1888 printf("status=%i", i->exit_status);
1890 #ifdef HAVE_SYSV_COMPAT
1891 if ((c = exit_status_to_string(i->exit_status, i->is_sysv ? EXIT_STATUS_LSB : EXIT_STATUS_SYSTEMD)))
1893 if ((c = exit_status_to_string(i->exit_status, EXIT_STATUS_SYSTEMD)))
1898 printf("signal=%s", signal_to_string(i->exit_status));
1903 if (i->main_pid > 0 && i->control_pid > 0)
1906 if (i->control_pid > 0) {
1909 printf(" Control: %u", (unsigned) i->control_pid);
1911 get_process_name(i->control_pid, &t);
1922 printf("\t Status: \"%s\"\n", i->status_text);
1924 if (i->default_control_group) {
1927 printf("\t CGroup: %s\n", i->default_control_group);
1929 if ((c = columns()) > 18)
1934 show_cgroup_by_path(i->default_control_group, "\t\t ", c);
1937 if (i->need_daemon_reload)
1938 printf("\n%sWarning:%s Unit file changed on disk, 'systemctl %s daemon-reload' recommended.\n",
1939 ansi_highlight(true),
1940 ansi_highlight(false),
1941 arg_user ? "--user" : "--system");
1944 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
1946 switch (dbus_message_iter_get_arg_type(iter)) {
1948 case DBUS_TYPE_STRING: {
1951 dbus_message_iter_get_basic(iter, &s);
1954 if (streq(name, "Id"))
1956 else if (streq(name, "LoadState"))
1958 else if (streq(name, "ActiveState"))
1959 i->active_state = s;
1960 else if (streq(name, "SubState"))
1962 else if (streq(name, "Description"))
1964 else if (streq(name, "FragmentPath"))
1966 #ifdef HAVE_SYSV_COMPAT
1967 else if (streq(name, "SysVPath")) {
1972 else if (streq(name, "DefaultControlGroup"))
1973 i->default_control_group = s;
1974 else if (streq(name, "StatusText"))
1976 else if (streq(name, "SysFSPath"))
1978 else if (streq(name, "Where"))
1980 else if (streq(name, "What"))
1982 else if (streq(name, "Following"))
1989 case DBUS_TYPE_BOOLEAN: {
1992 dbus_message_iter_get_basic(iter, &b);
1994 if (streq(name, "Accept"))
1996 else if (streq(name, "NeedDaemonReload"))
1997 i->need_daemon_reload = b;
2002 case DBUS_TYPE_UINT32: {
2005 dbus_message_iter_get_basic(iter, &u);
2007 if (streq(name, "MainPID")) {
2009 i->main_pid = (pid_t) u;
2012 } else if (streq(name, "ControlPID"))
2013 i->control_pid = (pid_t) u;
2014 else if (streq(name, "ExecMainPID")) {
2016 i->main_pid = (pid_t) u;
2017 } else if (streq(name, "NAccepted"))
2019 else if (streq(name, "NConnections"))
2020 i->n_connections = u;
2025 case DBUS_TYPE_INT32: {
2028 dbus_message_iter_get_basic(iter, &j);
2030 if (streq(name, "ExecMainCode"))
2031 i->exit_code = (int) j;
2032 else if (streq(name, "ExecMainStatus"))
2033 i->exit_status = (int) j;
2038 case DBUS_TYPE_UINT64: {
2041 dbus_message_iter_get_basic(iter, &u);
2043 if (streq(name, "ExecMainStartTimestamp"))
2044 i->start_timestamp = (usec_t) u;
2045 else if (streq(name, "ExecMainExitTimestamp"))
2046 i->exit_timestamp = (usec_t) u;
2047 else if (streq(name, "ActiveEnterTimestamp"))
2048 i->active_enter_timestamp = (usec_t) u;
2049 else if (streq(name, "InactiveEnterTimestamp"))
2050 i->inactive_enter_timestamp = (usec_t) u;
2051 else if (streq(name, "InactiveExitTimestamp"))
2052 i->inactive_exit_timestamp = (usec_t) u;
2053 else if (streq(name, "ActiveExitTimestamp"))
2054 i->active_exit_timestamp = (usec_t) u;
2059 case DBUS_TYPE_ARRAY: {
2061 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT &&
2062 startswith(name, "Exec")) {
2063 DBusMessageIter sub;
2065 dbus_message_iter_recurse(iter, &sub);
2066 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2067 ExecStatusInfo *info;
2070 if (!(info = new0(ExecStatusInfo, 1)))
2073 if ((r = exec_status_info_deserialize(&sub, info)) < 0) {
2078 LIST_PREPEND(ExecStatusInfo, exec, i->exec, info);
2080 dbus_message_iter_next(&sub);
2091 static int print_property(const char *name, DBusMessageIter *iter) {
2095 /* This is a low-level property printer, see
2096 * print_status_info() for the nicer output */
2098 if (arg_property && !strv_find(arg_property, name))
2101 switch (dbus_message_iter_get_arg_type(iter)) {
2103 case DBUS_TYPE_STRING: {
2105 dbus_message_iter_get_basic(iter, &s);
2107 if (arg_all || s[0])
2108 printf("%s=%s\n", name, s);
2113 case DBUS_TYPE_BOOLEAN: {
2115 dbus_message_iter_get_basic(iter, &b);
2116 printf("%s=%s\n", name, yes_no(b));
2121 case DBUS_TYPE_UINT64: {
2123 dbus_message_iter_get_basic(iter, &u);
2125 /* Yes, heuristics! But we can change this check
2126 * should it turn out to not be sufficient */
2128 if (strstr(name, "Timestamp")) {
2129 char timestamp[FORMAT_TIMESTAMP_MAX], *t;
2131 if ((t = format_timestamp(timestamp, sizeof(timestamp), u)) || arg_all)
2132 printf("%s=%s\n", name, strempty(t));
2133 } else if (strstr(name, "USec")) {
2134 char timespan[FORMAT_TIMESPAN_MAX];
2136 printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u));
2138 printf("%s=%llu\n", name, (unsigned long long) u);
2143 case DBUS_TYPE_UINT32: {
2145 dbus_message_iter_get_basic(iter, &u);
2147 if (strstr(name, "UMask") || strstr(name, "Mode"))
2148 printf("%s=%04o\n", name, u);
2150 printf("%s=%u\n", name, (unsigned) u);
2155 case DBUS_TYPE_INT32: {
2157 dbus_message_iter_get_basic(iter, &i);
2159 printf("%s=%i\n", name, (int) i);
2163 case DBUS_TYPE_DOUBLE: {
2165 dbus_message_iter_get_basic(iter, &d);
2167 printf("%s=%g\n", name, d);
2171 case DBUS_TYPE_STRUCT: {
2172 DBusMessageIter sub;
2173 dbus_message_iter_recurse(iter, &sub);
2175 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
2178 dbus_message_iter_get_basic(&sub, &u);
2181 printf("%s=%u\n", name, (unsigned) u);
2183 printf("%s=\n", name);
2186 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
2189 dbus_message_iter_get_basic(&sub, &s);
2191 if (arg_all || s[0])
2192 printf("%s=%s\n", name, s);
2200 case DBUS_TYPE_ARRAY:
2202 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
2203 DBusMessageIter sub;
2206 dbus_message_iter_recurse(iter, &sub);
2208 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2209 printf("%s=", name);
2211 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2214 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
2215 dbus_message_iter_get_basic(&sub, &s);
2216 printf("%s%s", space ? " " : "", s);
2219 dbus_message_iter_next(&sub);
2227 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_BYTE) {
2228 DBusMessageIter sub;
2230 dbus_message_iter_recurse(iter, &sub);
2232 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2233 printf("%s=", name);
2235 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2238 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_BYTE);
2239 dbus_message_iter_get_basic(&sub, &u);
2242 dbus_message_iter_next(&sub);
2250 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
2251 DBusMessageIter sub, sub2;
2253 dbus_message_iter_recurse(iter, &sub);
2254 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2255 const char *type, *path;
2257 dbus_message_iter_recurse(&sub, &sub2);
2259 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
2260 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
2261 printf("%s=%s\n", type, path);
2263 dbus_message_iter_next(&sub);
2268 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
2269 DBusMessageIter sub, sub2;
2271 dbus_message_iter_recurse(iter, &sub);
2272 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2274 uint64_t value, next_elapse;
2276 dbus_message_iter_recurse(&sub, &sub2);
2278 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
2279 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
2280 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
2281 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
2283 printf("%s={ value=%s ; next_elapse=%s }\n",
2285 format_timespan(timespan1, sizeof(timespan1), value),
2286 format_timespan(timespan2, sizeof(timespan2), next_elapse));
2289 dbus_message_iter_next(&sub);
2294 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
2295 DBusMessageIter sub;
2297 dbus_message_iter_recurse(iter, &sub);
2298 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2299 ExecStatusInfo info;
2302 if (exec_status_info_deserialize(&sub, &info) >= 0) {
2303 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
2306 t = strv_join(info.argv, " ");
2308 printf("%s={ path=%s ; argv[]=%s ; ignore=%s ; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n",
2312 yes_no(info.ignore),
2313 strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)),
2314 strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)),
2315 (unsigned) info. pid,
2316 sigchld_code_to_string(info.code),
2318 info.code == CLD_EXITED ? "" : "/",
2319 strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status)));
2325 strv_free(info.argv);
2327 dbus_message_iter_next(&sub);
2337 printf("%s=[unprintable]\n", name);
2342 static int show_one(const char *verb, DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
2343 DBusMessage *m = NULL, *reply = NULL;
2344 const char *interface = "";
2347 DBusMessageIter iter, sub, sub2, sub3;
2348 UnitStatusInfo info;
2356 dbus_error_init(&error);
2358 if (!(m = dbus_message_new_method_call(
2359 "org.freedesktop.systemd1",
2361 "org.freedesktop.DBus.Properties",
2363 log_error("Could not allocate message.");
2368 if (!dbus_message_append_args(m,
2369 DBUS_TYPE_STRING, &interface,
2370 DBUS_TYPE_INVALID)) {
2371 log_error("Could not append arguments to message.");
2376 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2377 log_error("Failed to issue method call: %s", bus_error_message(&error));
2382 if (!dbus_message_iter_init(reply, &iter) ||
2383 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
2384 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
2385 log_error("Failed to parse reply.");
2390 dbus_message_iter_recurse(&iter, &sub);
2397 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2400 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
2401 log_error("Failed to parse reply.");
2406 dbus_message_iter_recurse(&sub, &sub2);
2408 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
2409 log_error("Failed to parse reply.");
2414 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
2415 log_error("Failed to parse reply.");
2420 dbus_message_iter_recurse(&sub2, &sub3);
2422 if (show_properties)
2423 r = print_property(name, &sub3);
2425 r = status_property(name, &sub3, &info);
2428 log_error("Failed to parse reply.");
2433 dbus_message_iter_next(&sub);
2438 if (!show_properties)
2439 print_status_info(&info);
2441 if (!streq_ptr(info.active_state, "active") &&
2442 !streq_ptr(info.active_state, "reloading") &&
2443 streq(verb, "status"))
2444 /* According to LSB: "program not running" */
2447 while ((p = info.exec)) {
2448 LIST_REMOVE(ExecStatusInfo, exec, info.exec, p);
2449 exec_status_info_free(p);
2454 dbus_message_unref(m);
2457 dbus_message_unref(reply);
2459 dbus_error_free(&error);
2464 static int show(DBusConnection *bus, char **args, unsigned n) {
2465 DBusMessage *m = NULL, *reply = NULL;
2469 bool show_properties, new_line = false;
2474 dbus_error_init(&error);
2476 show_properties = !streq(args[0], "status");
2478 if (show_properties && n <= 1) {
2479 /* If not argument is specified inspect the manager
2482 ret = show_one(args[0], bus, "/org/freedesktop/systemd1", show_properties, &new_line);
2486 for (i = 1; i < n; i++) {
2487 const char *path = NULL;
2490 if (safe_atou32(args[i], &id) < 0) {
2492 /* Interpret as unit name */
2494 if (!(m = dbus_message_new_method_call(
2495 "org.freedesktop.systemd1",
2496 "/org/freedesktop/systemd1",
2497 "org.freedesktop.systemd1.Manager",
2499 log_error("Could not allocate message.");
2504 if (!dbus_message_append_args(m,
2505 DBUS_TYPE_STRING, &args[i],
2506 DBUS_TYPE_INVALID)) {
2507 log_error("Could not append arguments to message.");
2512 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2514 if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
2515 log_error("Failed to issue method call: %s", bus_error_message(&error));
2520 dbus_error_free(&error);
2522 dbus_message_unref(m);
2523 if (!(m = dbus_message_new_method_call(
2524 "org.freedesktop.systemd1",
2525 "/org/freedesktop/systemd1",
2526 "org.freedesktop.systemd1.Manager",
2528 log_error("Could not allocate message.");
2533 if (!dbus_message_append_args(m,
2534 DBUS_TYPE_STRING, &args[i],
2535 DBUS_TYPE_INVALID)) {
2536 log_error("Could not append arguments to message.");
2541 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2542 log_error("Failed to issue method call: %s", bus_error_message(&error));
2544 if (dbus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT))
2545 ret = 4; /* According to LSB: "program or service status is unknown" */
2552 } else if (show_properties) {
2554 /* Interpret as job id */
2556 if (!(m = dbus_message_new_method_call(
2557 "org.freedesktop.systemd1",
2558 "/org/freedesktop/systemd1",
2559 "org.freedesktop.systemd1.Manager",
2561 log_error("Could not allocate message.");
2566 if (!dbus_message_append_args(m,
2567 DBUS_TYPE_UINT32, &id,
2568 DBUS_TYPE_INVALID)) {
2569 log_error("Could not append arguments to message.");
2574 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2575 log_error("Failed to issue method call: %s", bus_error_message(&error));
2581 /* Interpret as PID */
2583 if (!(m = dbus_message_new_method_call(
2584 "org.freedesktop.systemd1",
2585 "/org/freedesktop/systemd1",
2586 "org.freedesktop.systemd1.Manager",
2588 log_error("Could not allocate message.");
2593 if (!dbus_message_append_args(m,
2594 DBUS_TYPE_UINT32, &id,
2595 DBUS_TYPE_INVALID)) {
2596 log_error("Could not append arguments to message.");
2601 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2602 log_error("Failed to issue method call: %s", bus_error_message(&error));
2608 if (!dbus_message_get_args(reply, &error,
2609 DBUS_TYPE_OBJECT_PATH, &path,
2610 DBUS_TYPE_INVALID)) {
2611 log_error("Failed to parse reply: %s", bus_error_message(&error));
2616 if ((r = show_one(args[0], bus, path, show_properties, &new_line)) != 0)
2619 dbus_message_unref(m);
2620 dbus_message_unref(reply);
2626 dbus_message_unref(m);
2629 dbus_message_unref(reply);
2631 dbus_error_free(&error);
2636 static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
2638 DBusMessage *m = NULL, *reply = NULL;
2643 dbus_error_init(&error);
2645 log_debug("Got D-Bus request: %s.%s() on %s",
2646 dbus_message_get_interface(message),
2647 dbus_message_get_member(message),
2648 dbus_message_get_path(message));
2650 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
2651 log_error("Warning! D-Bus connection terminated.");
2652 dbus_connection_close(connection);
2654 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") ||
2655 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
2656 const char *id, *path;
2658 if (!dbus_message_get_args(message, &error,
2659 DBUS_TYPE_STRING, &id,
2660 DBUS_TYPE_OBJECT_PATH, &path,
2662 log_error("Failed to parse message: %s", bus_error_message(&error));
2663 else if (streq(dbus_message_get_member(message), "UnitNew"))
2664 printf("Unit %s added.\n", id);
2666 printf("Unit %s removed.\n", id);
2668 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew") ||
2669 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
2673 if (!dbus_message_get_args(message, &error,
2674 DBUS_TYPE_UINT32, &id,
2675 DBUS_TYPE_OBJECT_PATH, &path,
2677 log_error("Failed to parse message: %s", bus_error_message(&error));
2678 else if (streq(dbus_message_get_member(message), "JobNew"))
2679 printf("Job %u added.\n", id);
2681 printf("Job %u removed.\n", id);
2684 } else if (dbus_message_is_signal(message, "org.freedesktop.DBus.Properties", "PropertiesChanged")) {
2686 const char *path, *interface, *property = "Id";
2687 DBusMessageIter iter, sub;
2689 path = dbus_message_get_path(message);
2691 if (!dbus_message_get_args(message, &error,
2692 DBUS_TYPE_STRING, &interface,
2693 DBUS_TYPE_INVALID)) {
2694 log_error("Failed to parse message: %s", bus_error_message(&error));
2698 if (!streq(interface, "org.freedesktop.systemd1.Job") &&
2699 !streq(interface, "org.freedesktop.systemd1.Unit"))
2702 if (!(m = dbus_message_new_method_call(
2703 "org.freedesktop.systemd1",
2705 "org.freedesktop.DBus.Properties",
2707 log_error("Could not allocate message.");
2711 if (!dbus_message_append_args(m,
2712 DBUS_TYPE_STRING, &interface,
2713 DBUS_TYPE_STRING, &property,
2714 DBUS_TYPE_INVALID)) {
2715 log_error("Could not append arguments to message.");
2719 if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
2720 log_error("Failed to issue method call: %s", bus_error_message(&error));
2724 if (!dbus_message_iter_init(reply, &iter) ||
2725 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2726 log_error("Failed to parse reply.");
2730 dbus_message_iter_recurse(&iter, &sub);
2732 if (streq(interface, "org.freedesktop.systemd1.Unit")) {
2735 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2736 log_error("Failed to parse reply.");
2740 dbus_message_iter_get_basic(&sub, &id);
2741 printf("Unit %s changed.\n", id);
2745 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32) {
2746 log_error("Failed to parse reply.");
2750 dbus_message_iter_get_basic(&sub, &id);
2751 printf("Job %u changed.\n", id);
2757 dbus_message_unref(m);
2760 dbus_message_unref(reply);
2762 dbus_error_free(&error);
2763 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2767 dbus_message_unref(m);
2770 dbus_message_unref(reply);
2772 dbus_error_free(&error);
2773 return DBUS_HANDLER_RESULT_NEED_MEMORY;
2776 static int monitor(DBusConnection *bus, char **args, unsigned n) {
2777 DBusMessage *m = NULL, *reply = NULL;
2781 dbus_error_init(&error);
2784 dbus_bus_add_match(bus,
2786 "sender='org.freedesktop.systemd1',"
2787 "interface='org.freedesktop.systemd1.Manager',"
2788 "path='/org/freedesktop/systemd1'",
2791 if (dbus_error_is_set(&error)) {
2792 log_error("Failed to add match: %s", bus_error_message(&error));
2797 dbus_bus_add_match(bus,
2799 "sender='org.freedesktop.systemd1',"
2800 "interface='org.freedesktop.DBus.Properties',"
2801 "member='PropertiesChanged'",
2804 if (dbus_error_is_set(&error)) {
2805 log_error("Failed to add match: %s", bus_error_message(&error));
2811 if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
2812 log_error("Failed to add filter.");
2817 if (!(m = dbus_message_new_method_call(
2818 "org.freedesktop.systemd1",
2819 "/org/freedesktop/systemd1",
2820 "org.freedesktop.systemd1.Manager",
2822 log_error("Could not allocate message.");
2827 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2828 log_error("Failed to issue method call: %s", bus_error_message(&error));
2833 while (dbus_connection_read_write_dispatch(bus, -1))
2840 /* This is slightly dirty, since we don't undo the filter or the matches. */
2843 dbus_message_unref(m);
2846 dbus_message_unref(reply);
2848 dbus_error_free(&error);
2853 static int dump(DBusConnection *bus, char **args, unsigned n) {
2854 DBusMessage *m = NULL, *reply = NULL;
2859 dbus_error_init(&error);
2861 if (!(m = dbus_message_new_method_call(
2862 "org.freedesktop.systemd1",
2863 "/org/freedesktop/systemd1",
2864 "org.freedesktop.systemd1.Manager",
2866 log_error("Could not allocate message.");
2870 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2871 log_error("Failed to issue method call: %s", bus_error_message(&error));
2876 if (!dbus_message_get_args(reply, &error,
2877 DBUS_TYPE_STRING, &text,
2878 DBUS_TYPE_INVALID)) {
2879 log_error("Failed to parse reply: %s", bus_error_message(&error));
2884 fputs(text, stdout);
2890 dbus_message_unref(m);
2893 dbus_message_unref(reply);
2895 dbus_error_free(&error);
2900 static int snapshot(DBusConnection *bus, char **args, unsigned n) {
2901 DBusMessage *m = NULL, *reply = NULL;
2904 const char *name = "", *path, *id;
2905 dbus_bool_t cleanup = FALSE;
2906 DBusMessageIter iter, sub;
2908 *interface = "org.freedesktop.systemd1.Unit",
2911 dbus_error_init(&error);
2913 if (!(m = dbus_message_new_method_call(
2914 "org.freedesktop.systemd1",
2915 "/org/freedesktop/systemd1",
2916 "org.freedesktop.systemd1.Manager",
2917 "CreateSnapshot"))) {
2918 log_error("Could not allocate message.");
2925 if (!dbus_message_append_args(m,
2926 DBUS_TYPE_STRING, &name,
2927 DBUS_TYPE_BOOLEAN, &cleanup,
2928 DBUS_TYPE_INVALID)) {
2929 log_error("Could not append arguments to message.");
2934 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2935 log_error("Failed to issue method call: %s", bus_error_message(&error));
2940 if (!dbus_message_get_args(reply, &error,
2941 DBUS_TYPE_OBJECT_PATH, &path,
2942 DBUS_TYPE_INVALID)) {
2943 log_error("Failed to parse reply: %s", bus_error_message(&error));
2948 dbus_message_unref(m);
2949 if (!(m = dbus_message_new_method_call(
2950 "org.freedesktop.systemd1",
2952 "org.freedesktop.DBus.Properties",
2954 log_error("Could not allocate message.");
2958 if (!dbus_message_append_args(m,
2959 DBUS_TYPE_STRING, &interface,
2960 DBUS_TYPE_STRING, &property,
2961 DBUS_TYPE_INVALID)) {
2962 log_error("Could not append arguments to message.");
2967 dbus_message_unref(reply);
2968 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2969 log_error("Failed to issue method call: %s", bus_error_message(&error));
2974 if (!dbus_message_iter_init(reply, &iter) ||
2975 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2976 log_error("Failed to parse reply.");
2981 dbus_message_iter_recurse(&iter, &sub);
2983 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2984 log_error("Failed to parse reply.");
2989 dbus_message_iter_get_basic(&sub, &id);
2997 dbus_message_unref(m);
3000 dbus_message_unref(reply);
3002 dbus_error_free(&error);
3007 static int delete_snapshot(DBusConnection *bus, char **args, unsigned n) {
3008 DBusMessage *m = NULL, *reply = NULL;
3016 dbus_error_init(&error);
3018 for (i = 1; i < n; i++) {
3019 const char *path = NULL;
3021 if (!(m = dbus_message_new_method_call(
3022 "org.freedesktop.systemd1",
3023 "/org/freedesktop/systemd1",
3024 "org.freedesktop.systemd1.Manager",
3026 log_error("Could not allocate message.");
3031 if (!dbus_message_append_args(m,
3032 DBUS_TYPE_STRING, &args[i],
3033 DBUS_TYPE_INVALID)) {
3034 log_error("Could not append arguments to message.");
3039 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3040 log_error("Failed to issue method call: %s", bus_error_message(&error));
3045 if (!dbus_message_get_args(reply, &error,
3046 DBUS_TYPE_OBJECT_PATH, &path,
3047 DBUS_TYPE_INVALID)) {
3048 log_error("Failed to parse reply: %s", bus_error_message(&error));
3053 dbus_message_unref(m);
3054 if (!(m = dbus_message_new_method_call(
3055 "org.freedesktop.systemd1",
3057 "org.freedesktop.systemd1.Snapshot",
3059 log_error("Could not allocate message.");
3064 dbus_message_unref(reply);
3065 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3066 log_error("Failed to issue method call: %s", bus_error_message(&error));
3071 dbus_message_unref(m);
3072 dbus_message_unref(reply);
3080 dbus_message_unref(m);
3083 dbus_message_unref(reply);
3085 dbus_error_free(&error);
3090 static int daemon_reload(DBusConnection *bus, char **args, unsigned n) {
3091 DBusMessage *m = NULL, *reply = NULL;
3096 dbus_error_init(&error);
3098 if (arg_action == ACTION_RELOAD)
3100 else if (arg_action == ACTION_REEXEC)
3101 method = "Reexecute";
3103 assert(arg_action == ACTION_SYSTEMCTL);
3106 streq(args[0], "clear-jobs") ||
3107 streq(args[0], "cancel") ? "ClearJobs" :
3108 streq(args[0], "daemon-reexec") ? "Reexecute" :
3109 streq(args[0], "reset-failed") ? "ResetFailed" :
3110 streq(args[0], "halt") ? "Halt" :
3111 streq(args[0], "poweroff") ? "PowerOff" :
3112 streq(args[0], "reboot") ? "Reboot" :
3113 streq(args[0], "kexec") ? "KExec" :
3114 streq(args[0], "exit") ? "Exit" :
3115 /* "daemon-reload" */ "Reload";
3118 if (!(m = dbus_message_new_method_call(
3119 "org.freedesktop.systemd1",
3120 "/org/freedesktop/systemd1",
3121 "org.freedesktop.systemd1.Manager",
3123 log_error("Could not allocate message.");
3127 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3129 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
3130 /* There's always a fallback possible for
3131 * legacy actions. */
3136 log_error("Failed to issue method call: %s", bus_error_message(&error));
3145 dbus_message_unref(m);
3148 dbus_message_unref(reply);
3150 dbus_error_free(&error);
3155 static int reset_failed(DBusConnection *bus, char **args, unsigned n) {
3156 DBusMessage *m = NULL, *reply = NULL;
3162 dbus_error_init(&error);
3165 return daemon_reload(bus, args, n);
3167 for (i = 1; i < n; i++) {
3169 if (!(m = dbus_message_new_method_call(
3170 "org.freedesktop.systemd1",
3171 "/org/freedesktop/systemd1",
3172 "org.freedesktop.systemd1.Manager",
3173 "ResetFailedUnit"))) {
3174 log_error("Could not allocate message.");
3179 if (!dbus_message_append_args(m,
3180 DBUS_TYPE_STRING, args + i,
3181 DBUS_TYPE_INVALID)) {
3182 log_error("Could not append arguments to message.");
3187 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3188 log_error("Failed to issue method call: %s", bus_error_message(&error));
3193 dbus_message_unref(m);
3194 dbus_message_unref(reply);
3202 dbus_message_unref(m);
3205 dbus_message_unref(reply);
3207 dbus_error_free(&error);
3212 static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
3213 DBusMessage *m = NULL, *reply = NULL;
3215 DBusMessageIter iter, sub, sub2;
3218 *interface = "org.freedesktop.systemd1.Manager",
3219 *property = "Environment";
3221 dbus_error_init(&error);
3223 if (!(m = dbus_message_new_method_call(
3224 "org.freedesktop.systemd1",
3225 "/org/freedesktop/systemd1",
3226 "org.freedesktop.DBus.Properties",
3228 log_error("Could not allocate message.");
3232 if (!dbus_message_append_args(m,
3233 DBUS_TYPE_STRING, &interface,
3234 DBUS_TYPE_STRING, &property,
3235 DBUS_TYPE_INVALID)) {
3236 log_error("Could not append arguments to message.");
3241 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3242 log_error("Failed to issue method call: %s", bus_error_message(&error));
3247 if (!dbus_message_iter_init(reply, &iter) ||
3248 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
3249 log_error("Failed to parse reply.");
3254 dbus_message_iter_recurse(&iter, &sub);
3256 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
3257 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
3258 log_error("Failed to parse reply.");
3263 dbus_message_iter_recurse(&sub, &sub2);
3265 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
3268 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
3269 log_error("Failed to parse reply.");
3274 dbus_message_iter_get_basic(&sub2, &text);
3275 printf("%s\n", text);
3277 dbus_message_iter_next(&sub2);
3284 dbus_message_unref(m);
3287 dbus_message_unref(reply);
3289 dbus_error_free(&error);
3294 static int set_environment(DBusConnection *bus, char **args, unsigned n) {
3295 DBusMessage *m = NULL, *reply = NULL;
3299 DBusMessageIter iter, sub;
3302 dbus_error_init(&error);
3304 method = streq(args[0], "set-environment")
3306 : "UnsetEnvironment";
3308 if (!(m = dbus_message_new_method_call(
3309 "org.freedesktop.systemd1",
3310 "/org/freedesktop/systemd1",
3311 "org.freedesktop.systemd1.Manager",
3314 log_error("Could not allocate message.");
3318 dbus_message_iter_init_append(m, &iter);
3320 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
3321 log_error("Could not append arguments to message.");
3326 for (i = 1; i < n; i++)
3327 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) {
3328 log_error("Could not append arguments to message.");
3333 if (!dbus_message_iter_close_container(&iter, &sub)) {
3334 log_error("Could not append arguments to message.");
3339 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3340 log_error("Failed to issue method call: %s", bus_error_message(&error));
3349 dbus_message_unref(m);
3352 dbus_message_unref(reply);
3354 dbus_error_free(&error);
3367 static Hashmap *will_install = NULL, *have_installed = NULL;
3368 static Set *remove_symlinks_to = NULL;
3369 static unsigned n_symlinks = 0;
3371 static void install_info_free(InstallInfo *i) {
3376 strv_free(i->aliases);
3377 strv_free(i->wanted_by);
3381 static void install_info_hashmap_free(Hashmap *m) {
3384 while ((i = hashmap_steal_first(m)))
3385 install_info_free(i);
3390 static int install_info_add(const char *name) {
3394 assert(will_install);
3396 if (!unit_name_is_valid_no_type(name, true)) {
3397 log_warning("Unit name %s is not a valid unit name.", name);
3401 if (hashmap_get(have_installed, name) ||
3402 hashmap_get(will_install, name))
3405 if (!(i = new0(InstallInfo, 1))) {
3410 if (!(i->name = strdup(name))) {
3415 if ((r = hashmap_put(will_install, i->name, i)) < 0)
3422 install_info_free(i);
3427 static int config_parse_also(
3428 const char *filename,
3430 const char *section,
3444 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
3448 if (!(n = strndup(w, l)))
3451 if ((r = install_info_add(n)) < 0) {
3452 log_warning("Cannot install unit %s: %s", n, strerror(-r));
3463 static int mark_symlink_for_removal(const char *p) {
3468 assert(path_is_absolute(p));
3470 if (!remove_symlinks_to)
3473 if (!(n = strdup(p)))
3476 path_kill_slashes(n);
3478 if ((r = set_put(remove_symlinks_to, n)) < 0) {
3480 return r == -EEXIST ? 0 : r;
3486 static int remove_marked_symlinks_fd(int fd, const char *config_path, const char *root, bool *deleted) {
3495 if (!(d = fdopendir(fd))) {
3496 close_nointr_nofail(fd);
3502 while ((de = readdir(d))) {
3503 bool is_dir = false, is_link = false;
3505 if (ignore_file(de->d_name))
3508 if (de->d_type == DT_LNK)
3510 else if (de->d_type == DT_DIR)
3512 else if (de->d_type == DT_UNKNOWN) {
3515 if (fstatat(fd, de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) {
3516 log_error("Failed to stat %s/%s: %m", root, de->d_name);
3523 is_link = S_ISLNK(st.st_mode);
3524 is_dir = S_ISDIR(st.st_mode);
3532 if ((nfd = openat(fd, de->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW)) < 0) {
3533 log_error("Failed to open %s/%s: %m", root, de->d_name);
3540 if (asprintf(&p, "%s/%s", root, de->d_name) < 0) {
3541 log_error("Failed to allocate directory string.");
3542 close_nointr_nofail(nfd);
3547 /* This will close nfd, regardless whether it succeeds or not */
3548 q = remove_marked_symlinks_fd(nfd, config_path, p, deleted);
3554 } else if (is_link) {
3558 if (asprintf(&p, "%s/%s", root, de->d_name) < 0) {
3559 log_error("Failed to allocate symlink string.");
3564 if ((q = readlink_and_make_absolute(p, &dest)) < 0) {
3565 log_error("Cannot read symlink %s: %s", p, strerror(-q));
3573 if ((c = canonicalize_file_name(dest))) {
3574 /* This might fail if the destination
3575 * is already removed */
3581 path_kill_slashes(dest);
3582 if (set_get(remove_symlinks_to, dest)) {
3585 log_info("rm '%s'", p);
3587 if (unlink(p) < 0) {
3588 log_error("Cannot unlink symlink %s: %m", p);
3593 rmdir_parents(p, config_path);
3594 path_kill_slashes(p);
3596 if (!set_get(remove_symlinks_to, p)) {
3598 if ((r = mark_symlink_for_removal(p)) < 0) {
3617 static int remove_marked_symlinks(const char *config_path) {
3621 assert(config_path);
3623 if (set_size(remove_symlinks_to) <= 0)
3626 if ((fd = open(config_path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW)) < 0)
3633 if ((cfd = dup(fd)) < 0) {
3638 /* This takes possession of cfd and closes it */
3639 if ((q = remove_marked_symlinks_fd(cfd, config_path, config_path, &deleted)) < 0) {
3645 close_nointr_nofail(fd);
3650 static int create_symlink(const char *verb, const char *old_path, const char *new_path) {
3657 if (streq(verb, "enable")) {
3660 mkdir_parents(new_path, 0755);
3662 if (symlink(old_path, new_path) >= 0) {
3665 log_info("ln -s '%s' '%s'", old_path, new_path);
3670 if (errno != EEXIST) {
3671 log_error("Cannot link %s to %s: %m", old_path, new_path);
3675 if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
3677 if (errno == EINVAL) {
3678 log_error("Cannot link %s to %s, file exists already and is not a symlink.", old_path, new_path);
3682 log_error("readlink() failed: %s", strerror(-r));
3686 if (streq(dest, old_path)) {
3692 log_error("Cannot link %s to %s, symlink exists already and points to %s.", old_path, new_path, dest);
3701 log_info("ln -s '%s' '%s'", old_path, new_path);
3703 if (symlink(old_path, new_path) >= 0)
3706 log_error("Cannot link %s to %s: %m", old_path, new_path);
3709 } else if (streq(verb, "disable")) {
3712 if ((r = mark_symlink_for_removal(old_path)) < 0)
3715 if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
3716 if (errno == ENOENT)
3719 if (errno == EINVAL) {
3720 log_warning("File %s not a symlink, ignoring.", old_path);
3724 log_error("readlink() failed: %s", strerror(-r));
3728 if (!streq(dest, old_path)) {
3729 log_warning("File %s not a symlink to %s but points to %s, ignoring.", new_path, old_path, dest);
3736 if ((r = mark_symlink_for_removal(new_path)) < 0)
3740 log_info("rm '%s'", new_path);
3742 if (unlink(new_path) >= 0)
3745 log_error("Cannot unlink %s: %m", new_path);
3748 } else if (streq(verb, "is-enabled")) {
3751 if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
3753 if (errno == ENOENT || errno == EINVAL)
3756 log_error("readlink() failed: %s", strerror(-r));
3760 if (streq(dest, old_path)) {
3768 assert_not_reached("Unknown action.");
3771 static int install_info_symlink_alias(const char *verb, InstallInfo *i, const char *config_path) {
3773 char *alias_path = NULL;
3778 assert(config_path);
3780 STRV_FOREACH(s, i->aliases) {
3783 if (!(alias_path = path_make_absolute(*s, config_path))) {
3784 log_error("Out of memory");
3789 if ((r = create_symlink(verb, i->path, alias_path)) != 0)
3792 if (streq(verb, "disable"))
3793 rmdir_parents(alias_path, config_path);
3803 static int install_info_symlink_wants(const char *verb, InstallInfo *i, const char *config_path) {
3805 char *alias_path = NULL;
3810 assert(config_path);
3812 STRV_FOREACH(s, i->wanted_by) {
3813 if (!unit_name_is_valid_no_type(*s, true)) {
3814 log_error("Invalid name %s.", *s);
3822 if (asprintf(&alias_path, "%s/%s.wants/%s", config_path, *s, i->name) < 0) {
3823 log_error("Out of memory");
3828 if ((r = create_symlink(verb, i->path, alias_path)) != 0)
3831 if (streq(verb, "disable"))
3832 rmdir_parents(alias_path, config_path);
3843 static int install_info_apply(const char *verb, LookupPaths *paths, InstallInfo *i, const char *config_path) {
3845 const ConfigItem items[] = {
3846 { "Alias", config_parse_strv, &i->aliases, "Install" },
3847 { "WantedBy", config_parse_strv, &i->wanted_by, "Install" },
3848 { "Also", config_parse_also, NULL, "Install" },
3850 { NULL, NULL, NULL, NULL }
3854 char *filename = NULL;
3861 STRV_FOREACH(p, paths->unit_path) {
3864 if (!(filename = path_make_absolute(i->name, *p))) {
3865 log_error("Out of memory");
3869 /* Ensure that we don't follow symlinks */
3870 if ((fd = open(filename, O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NOCTTY)) >= 0)
3871 if ((f = fdopen(fd, "re")))
3874 if (errno == ELOOP) {
3875 log_error("Refusing to operate on symlinks, please pass unit names or absolute paths to unit files.");
3880 if (errno != ENOENT) {
3881 log_error("Failed to open %s: %m", filename);
3891 #if defined(TARGET_FEDORA) && defined (HAVE_SYSV_COMPAT)
3893 if (endswith(i->name, ".service")) {
3897 if (asprintf(&sysv, SYSTEM_SYSVINIT_PATH "/%s", i->name) < 0) {
3898 log_error("Out of memory");
3902 sysv[strlen(sysv) - sizeof(".service") + 1] = 0;
3903 exists = access(sysv, F_OK) >= 0;
3909 const char *argv[] = {
3916 log_info("%s is not a native service, redirecting to /sbin/chkconfig.", i->name);
3918 argv[1] = file_name_from_path(sysv);
3920 streq(verb, "enable") ? "on" :
3921 streq(verb, "disable") ? "off" : NULL;
3923 log_info("Executing %s %s %s", argv[0], argv[1], strempty(argv[2]));
3925 if ((pid = fork()) < 0) {
3926 log_error("Failed to fork: %m");
3929 } else if (pid == 0) {
3930 execv(argv[0], (char**) argv);
3931 _exit(EXIT_FAILURE);
3936 if ((r = wait_for_terminate(pid, &status)) < 0)
3939 if (status.si_code == CLD_EXITED) {
3940 if (status.si_status == 0 && (streq(verb, "enable") || streq(verb, "disable")))
3943 return status.si_status == 0 ? 0 : -EINVAL;
3953 log_error("Couldn't find %s.", i->name);
3959 if ((r = config_parse(filename, f, NULL, items, true, i)) < 0) {
3964 n_symlinks += strv_length(i->aliases);
3965 n_symlinks += strv_length(i->wanted_by);
3969 if ((r = install_info_symlink_alias(verb, i, config_path)) != 0)
3972 if ((r = install_info_symlink_wants(verb, i, config_path)) != 0)
3975 if ((r = mark_symlink_for_removal(filename)) < 0)
3978 if ((r = remove_marked_symlinks(config_path)) < 0)
3984 static char *get_config_path(void) {
3986 if (arg_user && arg_global)
3987 return strdup(USER_CONFIG_UNIT_PATH);
3992 if (user_config_home(&p) < 0)
3998 return strdup(SYSTEM_CONFIG_UNIT_PATH);
4001 static int enable_unit(DBusConnection *bus, char **args, unsigned n) {
4005 char *config_path = NULL;
4008 const char *verb = args[0];
4010 dbus_error_init(&error);
4013 if ((r = lookup_paths_init(&paths, arg_user ? MANAGER_USER : MANAGER_SYSTEM)) < 0) {
4014 log_error("Failed to determine lookup paths: %s", strerror(-r));
4018 if (!(config_path = get_config_path())) {
4019 log_error("Failed to determine config path");
4024 will_install = hashmap_new(string_hash_func, string_compare_func);
4025 have_installed = hashmap_new(string_hash_func, string_compare_func);
4027 if (!will_install || !have_installed) {
4028 log_error("Failed to allocate unit sets.");
4033 if (!arg_defaults && streq(verb, "disable"))
4034 if (!(remove_symlinks_to = set_new(string_hash_func, string_compare_func))) {
4035 log_error("Failed to allocate symlink sets.");
4040 for (j = 1; j < n; j++)
4041 if ((r = install_info_add(args[j])) < 0) {
4042 log_warning("Cannot install unit %s: %s", args[j], strerror(-r));
4046 while ((i = hashmap_first(will_install))) {
4049 assert_se(hashmap_move_one(have_installed, will_install, i->name) == 0);
4051 if ((q = install_info_apply(verb, &paths, i, config_path)) != 0) {
4059 /* In test mode and found something */
4065 if (streq(verb, "is-enabled"))
4066 r = r > 0 ? 0 : -ENOENT;
4068 if (n_symlinks <= 0)
4069 log_warning("Unit files contain no applicable installation information. Ignoring.");
4072 /* Don't try to reload anything if the user asked us to not do this */
4074 /* Don't try to reload anything when updating a unit globally */
4076 /* Don't try to reload anything if we are called for system changes but the system wasn't booted with systemd */
4077 (arg_user || sd_booted() > 0) &&
4078 /* Don't try to reload anything if we are running in a chroot environment */
4079 (arg_user || running_in_chroot() <= 0) ) {
4082 if ((q = daemon_reload(bus, args, n)) < 0)
4088 install_info_hashmap_free(will_install);
4089 install_info_hashmap_free(have_installed);
4091 set_free_free(remove_symlinks_to);
4093 lookup_paths_free(&paths);
4100 static int systemctl_help(void) {
4102 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
4103 "Send control commands to or query the systemd manager.\n\n"
4104 " -h --help Show this help\n"
4105 " --version Show package version\n"
4106 " -t --type=TYPE List only units of a particular type\n"
4107 " -p --property=NAME Show only properties by this name\n"
4108 " -a --all Show all units/properties, including dead/empty ones\n"
4109 " --full Don't ellipsize unit names on output\n"
4110 " --fail When queueing a new job, fail if conflicting jobs are\n"
4112 " -q --quiet Suppress output\n"
4113 " --no-block Do not wait until operation finished\n"
4114 " --no-pager Do not pipe output into a pager.\n"
4115 " --system Connect to system manager\n"
4116 " --user Connect to user service manager\n"
4117 " --order When generating graph for dot, show only order\n"
4118 " --require When generating graph for dot, show only requirement\n"
4119 " --no-wall Don't send wall message before halt/power-off/reboot\n"
4120 " --global Enable/disable unit files globally\n"
4121 " --no-reload When enabling/disabling unit files, don't reload daemon\n"
4123 " --no-ask-password\n"
4124 " Do not ask for system passwords\n"
4125 " --kill-mode=MODE How to send signal\n"
4126 " --kill-who=WHO Who to send signal to\n"
4127 " -s --signal=SIGNAL Which signal to send\n"
4128 " -f --force When enabling unit files, override existing symlinks\n"
4129 " When shutting down, execute action immediately\n"
4130 " --defaults When disabling unit files, remove default symlinks only\n\n"
4132 " list-units List units\n"
4133 " start [NAME...] Start (activate) one or more units\n"
4134 " stop [NAME...] Stop (deactivate) one or more units\n"
4135 " reload [NAME...] Reload one or more units\n"
4136 " restart [NAME...] Start or restart one or more units\n"
4137 " try-restart [NAME...] Restart one or more units if active\n"
4138 " reload-or-restart [NAME...] Reload one or more units is possible,\n"
4139 " otherwise start or restart\n"
4140 " reload-or-try-restart [NAME...] Reload one or more units is possible,\n"
4141 " otherwise restart if active\n"
4142 " isolate [NAME] Start one unit and stop all others\n"
4143 " kill [NAME...] Send signal to processes of a unit\n"
4144 " is-active [NAME...] Check whether units are active\n"
4145 " status [NAME...|PID...] Show runtime status of one or more units\n"
4146 " show [NAME...|JOB...] Show properties of one or more\n"
4147 " units/jobs or the manager\n"
4148 " reset-failed [NAME...] Reset failed state for all, one, or more\n"
4150 " enable [NAME...] Enable one or more unit files\n"
4151 " disable [NAME...] Disable one or more unit files\n"
4152 " is-enabled [NAME...] Check whether unit files are enabled\n"
4153 " load [NAME...] Load one or more units\n"
4154 " list-jobs List jobs\n"
4155 " cancel [JOB...] Cancel all, one, or more jobs\n"
4156 " monitor Monitor unit/job changes\n"
4157 " dump Dump server status\n"
4158 " dot Dump dependency graph for dot(1)\n"
4159 " snapshot [NAME] Create a snapshot\n"
4160 " delete [NAME...] Remove one or more snapshots\n"
4161 " daemon-reload Reload systemd manager configuration\n"
4162 " daemon-reexec Reexecute systemd manager\n"
4163 " show-environment Dump environment\n"
4164 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
4165 " unset-environment [NAME...] Unset one or more environment variables\n"
4166 " default Enter system default mode\n"
4167 " rescue Enter system rescue mode\n"
4168 " emergency Enter system emergency mode\n"
4169 " halt Shut down and halt the system\n"
4170 " poweroff Shut down and power-off the system\n"
4171 " reboot Shut down and reboot the system\n"
4172 " kexec Shut down and reboot the system with kexec\n"
4173 " exit Ask for user instance termination\n",
4174 program_invocation_short_name);
4179 static int halt_help(void) {
4181 printf("%s [OPTIONS...]\n\n"
4182 "%s the system.\n\n"
4183 " --help Show this help\n"
4184 " --halt Halt the machine\n"
4185 " -p --poweroff Switch off the machine\n"
4186 " --reboot Reboot the machine\n"
4187 " -f --force Force immediate halt/power-off/reboot\n"
4188 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
4189 " -d --no-wtmp Don't write wtmp record\n"
4190 " -n --no-sync Don't sync before halt/power-off/reboot\n"
4191 " --no-wall Don't send wall message before halt/power-off/reboot\n",
4192 program_invocation_short_name,
4193 arg_action == ACTION_REBOOT ? "Reboot" :
4194 arg_action == ACTION_POWEROFF ? "Power off" :
4200 static int shutdown_help(void) {
4202 printf("%s [OPTIONS...] [TIME] [WALL...]\n\n"
4203 "Shut down the system.\n\n"
4204 " --help Show this help\n"
4205 " -H --halt Halt the machine\n"
4206 " -P --poweroff Power-off the machine\n"
4207 " -r --reboot Reboot the machine\n"
4208 " -h Equivalent to --poweroff, overriden by --halt\n"
4209 " -k Don't halt/power-off/reboot, just send warnings\n"
4210 " --no-wall Don't send wall message before halt/power-off/reboot\n"
4211 " -c Cancel a pending shutdown\n",
4212 program_invocation_short_name);
4217 static int telinit_help(void) {
4219 printf("%s [OPTIONS...] {COMMAND}\n\n"
4220 "Send control commands to the init daemon.\n\n"
4221 " --help Show this help\n"
4222 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
4224 " 0 Power-off the machine\n"
4225 " 6 Reboot the machine\n"
4226 " 2, 3, 4, 5 Start runlevelX.target unit\n"
4227 " 1, s, S Enter rescue mode\n"
4228 " q, Q Reload init daemon configuration\n"
4229 " u, U Reexecute init daemon\n",
4230 program_invocation_short_name);
4235 static int runlevel_help(void) {
4237 printf("%s [OPTIONS...]\n\n"
4238 "Prints the previous and current runlevel of the init system.\n\n"
4239 " --help Show this help\n",
4240 program_invocation_short_name);
4245 static int systemctl_parse_argv(int argc, char *argv[]) {
4266 static const struct option options[] = {
4267 { "help", no_argument, NULL, 'h' },
4268 { "version", no_argument, NULL, ARG_VERSION },
4269 { "type", required_argument, NULL, 't' },
4270 { "property", required_argument, NULL, 'p' },
4271 { "all", no_argument, NULL, 'a' },
4272 { "full", no_argument, NULL, ARG_FULL },
4273 { "fail", no_argument, NULL, ARG_FAIL },
4274 { "user", no_argument, NULL, ARG_USER },
4275 { "system", no_argument, NULL, ARG_SYSTEM },
4276 { "global", no_argument, NULL, ARG_GLOBAL },
4277 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
4278 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
4279 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4280 { "quiet", no_argument, NULL, 'q' },
4281 { "order", no_argument, NULL, ARG_ORDER },
4282 { "require", no_argument, NULL, ARG_REQUIRE },
4283 { "force", no_argument, NULL, 'f' },
4284 { "no-reload", no_argument, NULL, ARG_NO_RELOAD },
4285 { "defaults", no_argument, NULL, ARG_DEFAULTS },
4286 { "kill-mode", required_argument, NULL, ARG_KILL_MODE },
4287 { "kill-who", required_argument, NULL, ARG_KILL_WHO },
4288 { "signal", required_argument, NULL, 's' },
4289 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
4290 { NULL, 0, NULL, 0 }
4298 /* Only when running as systemctl we ask for passwords */
4299 arg_ask_password = true;
4301 while ((c = getopt_long(argc, argv, "ht:p:aqfs:", options, NULL)) >= 0) {
4310 puts(PACKAGE_STRING);
4312 puts(SYSTEMD_FEATURES);
4322 if (!(l = strv_append(arg_property, optarg)))
4325 strv_free(arg_property);
4328 /* If the user asked for a particular
4329 * property, show it to him, even if it is
4352 arg_no_block = true;
4356 arg_no_pager = true;
4364 arg_dot = DOT_ORDER;
4368 arg_dot = DOT_REQUIRE;
4384 arg_no_reload = true;
4393 arg_defaults = true;
4397 arg_kill_who = optarg;
4401 arg_kill_mode = optarg;
4405 if ((arg_signal = signal_from_string_try_harder(optarg)) < 0) {
4406 log_error("Failed to parse signal string %s.", optarg);
4411 case ARG_NO_ASK_PASSWORD:
4412 arg_ask_password = false;
4419 log_error("Unknown option code %c", c);
4427 static int halt_parse_argv(int argc, char *argv[]) {
4436 static const struct option options[] = {
4437 { "help", no_argument, NULL, ARG_HELP },
4438 { "halt", no_argument, NULL, ARG_HALT },
4439 { "poweroff", no_argument, NULL, 'p' },
4440 { "reboot", no_argument, NULL, ARG_REBOOT },
4441 { "force", no_argument, NULL, 'f' },
4442 { "wtmp-only", no_argument, NULL, 'w' },
4443 { "no-wtmp", no_argument, NULL, 'd' },
4444 { "no-sync", no_argument, NULL, 'n' },
4445 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4446 { NULL, 0, NULL, 0 }
4454 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
4455 if (runlevel == '0' || runlevel == '6')
4456 arg_immediate = true;
4458 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
4466 arg_action = ACTION_HALT;
4470 if (arg_action != ACTION_REBOOT)
4471 arg_action = ACTION_POWEROFF;
4475 arg_action = ACTION_REBOOT;
4479 arg_immediate = true;
4500 /* Compatibility nops */
4507 log_error("Unknown option code %c", c);
4512 if (optind < argc) {
4513 log_error("Too many arguments.");
4520 static int parse_time_spec(const char *t, usec_t *_u) {
4524 if (streq(t, "now"))
4526 else if (t[0] == '+') {
4529 if (safe_atou64(t + 1, &u) < 0)
4532 *_u = now(CLOCK_REALTIME) + USEC_PER_MINUTE * u;
4541 hour = strtol(t, &e, 10);
4542 if (errno != 0 || *e != ':' || hour < 0 || hour > 23)
4545 minute = strtol(e+1, &e, 10);
4546 if (errno != 0 || *e != 0 || minute < 0 || minute > 59)
4549 n = now(CLOCK_REALTIME);
4550 s = (time_t) (n / USEC_PER_SEC);
4553 assert_se(localtime_r(&s, &tm));
4555 tm.tm_hour = (int) hour;
4556 tm.tm_min = (int) minute;
4559 assert_se(s = mktime(&tm));
4561 *_u = (usec_t) s * USEC_PER_SEC;
4564 *_u += USEC_PER_DAY;
4570 static int shutdown_parse_argv(int argc, char *argv[]) {
4577 static const struct option options[] = {
4578 { "help", no_argument, NULL, ARG_HELP },
4579 { "halt", no_argument, NULL, 'H' },
4580 { "poweroff", no_argument, NULL, 'P' },
4581 { "reboot", no_argument, NULL, 'r' },
4582 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4583 { NULL, 0, NULL, 0 }
4591 while ((c = getopt_long(argc, argv, "HPrhkt:afFc", options, NULL)) >= 0) {
4599 arg_action = ACTION_HALT;
4603 arg_action = ACTION_POWEROFF;
4607 arg_action = ACTION_REBOOT;
4611 if (arg_action != ACTION_HALT)
4612 arg_action = ACTION_POWEROFF;
4625 /* Compatibility nops */
4629 arg_action = ACTION_CANCEL_SHUTDOWN;
4636 log_error("Unknown option code %c", c);
4641 if (argc > optind) {
4642 if ((r = parse_time_spec(argv[optind], &arg_when)) < 0) {
4643 log_error("Failed to parse time specification: %s", argv[optind]);
4647 arg_when = now(CLOCK_REALTIME) + USEC_PER_MINUTE;
4649 /* We skip the time argument */
4650 if (argc > optind + 1)
4651 arg_wall = argv + optind + 1;
4658 static int telinit_parse_argv(int argc, char *argv[]) {
4665 static const struct option options[] = {
4666 { "help", no_argument, NULL, ARG_HELP },
4667 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4668 { NULL, 0, NULL, 0 }
4671 static const struct {
4675 { '0', ACTION_POWEROFF },
4676 { '6', ACTION_REBOOT },
4677 { '1', ACTION_RESCUE },
4678 { '2', ACTION_RUNLEVEL2 },
4679 { '3', ACTION_RUNLEVEL3 },
4680 { '4', ACTION_RUNLEVEL4 },
4681 { '5', ACTION_RUNLEVEL5 },
4682 { 's', ACTION_RESCUE },
4683 { 'S', ACTION_RESCUE },
4684 { 'q', ACTION_RELOAD },
4685 { 'Q', ACTION_RELOAD },
4686 { 'u', ACTION_REEXEC },
4687 { 'U', ACTION_REEXEC }
4696 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4711 log_error("Unknown option code %c", c);
4716 if (optind >= argc) {
4721 if (optind + 1 < argc) {
4722 log_error("Too many arguments.");
4726 if (strlen(argv[optind]) != 1) {
4727 log_error("Expected single character argument.");
4731 for (i = 0; i < ELEMENTSOF(table); i++)
4732 if (table[i].from == argv[optind][0])
4735 if (i >= ELEMENTSOF(table)) {
4736 log_error("Unknown command %s.", argv[optind]);
4740 arg_action = table[i].to;
4747 static int runlevel_parse_argv(int argc, char *argv[]) {
4753 static const struct option options[] = {
4754 { "help", no_argument, NULL, ARG_HELP },
4755 { NULL, 0, NULL, 0 }
4763 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4774 log_error("Unknown option code %c", c);
4779 if (optind < argc) {
4780 log_error("Too many arguments.");
4787 static int parse_argv(int argc, char *argv[]) {
4791 if (program_invocation_short_name) {
4793 if (strstr(program_invocation_short_name, "halt")) {
4794 arg_action = ACTION_HALT;
4795 return halt_parse_argv(argc, argv);
4796 } else if (strstr(program_invocation_short_name, "poweroff")) {
4797 arg_action = ACTION_POWEROFF;
4798 return halt_parse_argv(argc, argv);
4799 } else if (strstr(program_invocation_short_name, "reboot")) {
4800 arg_action = ACTION_REBOOT;
4801 return halt_parse_argv(argc, argv);
4802 } else if (strstr(program_invocation_short_name, "shutdown")) {
4803 arg_action = ACTION_POWEROFF;
4804 return shutdown_parse_argv(argc, argv);
4805 } else if (strstr(program_invocation_short_name, "init")) {
4807 if (sd_booted() > 0) {
4808 arg_action = ACTION_INVALID;
4809 return telinit_parse_argv(argc, argv);
4811 /* Hmm, so some other init system is
4812 * running, we need to forward this
4813 * request to it. For now we simply
4814 * guess that it is Upstart. */
4816 execv("/lib/upstart/telinit", argv);
4818 log_error("Couldn't find an alternative telinit implementation to spawn.");
4822 } else if (strstr(program_invocation_short_name, "runlevel")) {
4823 arg_action = ACTION_RUNLEVEL;
4824 return runlevel_parse_argv(argc, argv);
4828 arg_action = ACTION_SYSTEMCTL;
4829 return systemctl_parse_argv(argc, argv);
4832 static int action_to_runlevel(void) {
4834 static const char table[_ACTION_MAX] = {
4835 [ACTION_HALT] = '0',
4836 [ACTION_POWEROFF] = '0',
4837 [ACTION_REBOOT] = '6',
4838 [ACTION_RUNLEVEL2] = '2',
4839 [ACTION_RUNLEVEL3] = '3',
4840 [ACTION_RUNLEVEL4] = '4',
4841 [ACTION_RUNLEVEL5] = '5',
4842 [ACTION_RESCUE] = '1'
4845 assert(arg_action < _ACTION_MAX);
4847 return table[arg_action];
4850 static int talk_upstart(void) {
4851 DBusMessage *m = NULL, *reply = NULL;
4853 int previous, rl, r;
4855 env1_buf[] = "RUNLEVEL=X",
4856 env2_buf[] = "PREVLEVEL=X";
4857 char *env1 = env1_buf, *env2 = env2_buf;
4858 const char *emit = "runlevel";
4859 dbus_bool_t b_false = FALSE;
4860 DBusMessageIter iter, sub;
4861 DBusConnection *bus;
4863 dbus_error_init(&error);
4865 if (!(rl = action_to_runlevel()))
4868 if (utmp_get_runlevel(&previous, NULL) < 0)
4871 if (!(bus = dbus_connection_open_private("unix:abstract=/com/ubuntu/upstart", &error))) {
4872 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
4877 log_error("Failed to connect to Upstart bus: %s", bus_error_message(&error));
4882 if ((r = bus_check_peercred(bus)) < 0) {
4883 log_error("Failed to verify owner of bus.");
4887 if (!(m = dbus_message_new_method_call(
4888 "com.ubuntu.Upstart",
4889 "/com/ubuntu/Upstart",
4890 "com.ubuntu.Upstart0_6",
4893 log_error("Could not allocate message.");
4898 dbus_message_iter_init_append(m, &iter);
4900 env1_buf[sizeof(env1_buf)-2] = rl;
4901 env2_buf[sizeof(env2_buf)-2] = previous;
4903 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
4904 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
4905 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
4906 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
4907 !dbus_message_iter_close_container(&iter, &sub) ||
4908 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
4909 log_error("Could not append arguments to message.");
4914 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
4916 if (error_is_no_service(&error)) {
4921 log_error("Failed to issue method call: %s", bus_error_message(&error));
4930 dbus_message_unref(m);
4933 dbus_message_unref(reply);
4936 dbus_connection_flush(bus);
4937 dbus_connection_close(bus);
4938 dbus_connection_unref(bus);
4941 dbus_error_free(&error);
4946 static int talk_initctl(void) {
4947 struct init_request request;
4951 if (!(rl = action_to_runlevel()))
4955 request.magic = INIT_MAGIC;
4956 request.sleeptime = 0;
4957 request.cmd = INIT_CMD_RUNLVL;
4958 request.runlevel = rl;
4960 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
4962 if (errno == ENOENT)
4965 log_error("Failed to open "INIT_FIFO": %m");
4970 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
4971 close_nointr_nofail(fd);
4974 log_error("Failed to write to "INIT_FIFO": %m");
4975 return errno ? -errno : -EIO;
4981 static int systemctl_main(DBusConnection *bus, int argc, char *argv[], DBusError *error) {
4983 static const struct {
4991 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
4993 { "list-units", LESS, 1, list_units },
4994 { "list-jobs", EQUAL, 1, list_jobs },
4995 { "clear-jobs", EQUAL, 1, daemon_reload },
4996 { "load", MORE, 2, load_unit },
4997 { "cancel", MORE, 2, cancel_job },
4998 { "start", MORE, 2, start_unit },
4999 { "stop", MORE, 2, start_unit },
5000 { "reload", MORE, 2, start_unit },
5001 { "restart", MORE, 2, start_unit },
5002 { "try-restart", MORE, 2, start_unit },
5003 { "reload-or-restart", MORE, 2, start_unit },
5004 { "reload-or-try-restart", MORE, 2, start_unit },
5005 { "force-reload", MORE, 2, start_unit }, /* For compatibility with SysV */
5006 { "condrestart", MORE, 2, start_unit }, /* For compatibility with RH */
5007 { "isolate", EQUAL, 2, start_unit },
5008 { "kill", MORE, 2, kill_unit },
5009 { "is-active", MORE, 2, check_unit },
5010 { "check", MORE, 2, check_unit },
5011 { "show", MORE, 1, show },
5012 { "status", MORE, 2, show },
5013 { "monitor", EQUAL, 1, monitor },
5014 { "dump", EQUAL, 1, dump },
5015 { "dot", EQUAL, 1, dot },
5016 { "snapshot", LESS, 2, snapshot },
5017 { "delete", MORE, 2, delete_snapshot },
5018 { "daemon-reload", EQUAL, 1, daemon_reload },
5019 { "daemon-reexec", EQUAL, 1, daemon_reload },
5020 { "show-environment", EQUAL, 1, show_enviroment },
5021 { "set-environment", MORE, 2, set_environment },
5022 { "unset-environment", MORE, 2, set_environment },
5023 { "halt", EQUAL, 1, start_special },
5024 { "poweroff", EQUAL, 1, start_special },
5025 { "reboot", EQUAL, 1, start_special },
5026 { "kexec", EQUAL, 1, start_special },
5027 { "default", EQUAL, 1, start_special },
5028 { "rescue", EQUAL, 1, start_special },
5029 { "emergency", EQUAL, 1, start_special },
5030 { "exit", EQUAL, 1, start_special },
5031 { "reset-failed", MORE, 1, reset_failed },
5032 { "enable", MORE, 2, enable_unit },
5033 { "disable", MORE, 2, enable_unit },
5034 { "is-enabled", MORE, 2, enable_unit }
5044 left = argc - optind;
5047 /* Special rule: no arguments means "list-units" */
5050 if (streq(argv[optind], "help")) {
5055 for (i = 0; i < ELEMENTSOF(verbs); i++)
5056 if (streq(argv[optind], verbs[i].verb))
5059 if (i >= ELEMENTSOF(verbs)) {
5060 log_error("Unknown operation %s", argv[optind]);
5065 switch (verbs[i].argc_cmp) {
5068 if (left != verbs[i].argc) {
5069 log_error("Invalid number of arguments.");
5076 if (left < verbs[i].argc) {
5077 log_error("Too few arguments.");
5084 if (left > verbs[i].argc) {
5085 log_error("Too many arguments.");
5092 assert_not_reached("Unknown comparison operator.");
5095 /* Require a bus connection for all operations but
5097 if (!streq(verbs[i].verb, "enable") &&
5098 !streq(verbs[i].verb, "disable") &&
5100 log_error("Failed to get D-Bus connection: %s", error->message);
5104 return verbs[i].dispatch(bus, argv + optind, left);
5107 static int send_shutdownd(usec_t t, char mode, bool warn, const char *message) {
5109 struct msghdr msghdr;
5111 union sockaddr_union sockaddr;
5112 struct shutdownd_command c;
5120 strncpy(c.wall_message, message, sizeof(c.wall_message));
5122 if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0)
5126 sockaddr.sa.sa_family = AF_UNIX;
5127 sockaddr.un.sun_path[0] = 0;
5128 strncpy(sockaddr.un.sun_path+1, "/org/freedesktop/systemd1/shutdownd", sizeof(sockaddr.un.sun_path)-1);
5131 iovec.iov_base = (char*) &c;
5132 iovec.iov_len = sizeof(c);
5135 msghdr.msg_name = &sockaddr;
5136 msghdr.msg_namelen = offsetof(struct sockaddr_un, sun_path) + 1 + sizeof("/org/freedesktop/systemd1/shutdownd") - 1;
5138 msghdr.msg_iov = &iovec;
5139 msghdr.msg_iovlen = 1;
5141 if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) < 0) {
5142 close_nointr_nofail(fd);
5146 close_nointr_nofail(fd);
5150 static int reload_with_fallback(DBusConnection *bus) {
5153 /* First, try systemd via D-Bus. */
5154 if (daemon_reload(bus, NULL, 0) > 0)
5158 /* Nothing else worked, so let's try signals */
5159 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
5161 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
5162 log_error("kill() failed: %m");
5169 static int start_with_fallback(DBusConnection *bus) {
5172 /* First, try systemd via D-Bus. */
5173 if (start_unit(bus, NULL, 0) >= 0)
5177 /* Hmm, talking to systemd via D-Bus didn't work. Then
5178 * let's try to talk to Upstart via D-Bus. */
5179 if (talk_upstart() > 0)
5182 /* Nothing else worked, so let's try
5184 if (talk_initctl() > 0)
5187 log_error("Failed to talk to init daemon.");
5191 warn_wall(arg_action);
5195 static int halt_main(DBusConnection *bus) {
5198 if (geteuid() != 0) {
5199 log_error("Must be root.");
5205 char date[FORMAT_TIMESTAMP_MAX];
5207 m = strv_join(arg_wall, " ");
5208 r = send_shutdownd(arg_when,
5209 arg_action == ACTION_HALT ? 'H' :
5210 arg_action == ACTION_POWEROFF ? 'P' :
5217 log_warning("Failed to talk to shutdownd, proceeding with immediate shutdown: %s", strerror(-r));
5219 log_info("Shutdown scheduled for %s, use 'shutdown -c' to cancel.",
5220 format_timestamp(date, sizeof(date), arg_when));
5225 if (!arg_dry && !arg_immediate)
5226 return start_with_fallback(bus);
5229 if (sd_booted() > 0)
5230 log_debug("Not writing utmp record, assuming that systemd-update-utmp is used.");
5231 else if ((r = utmp_put_shutdown(0)) < 0)
5232 log_warning("Failed to write utmp record: %s", strerror(-r));
5241 /* Make sure C-A-D is handled by the kernel from this
5243 reboot(RB_ENABLE_CAD);
5245 switch (arg_action) {
5248 log_info("Halting.");
5249 reboot(RB_HALT_SYSTEM);
5252 case ACTION_POWEROFF:
5253 log_info("Powering off.");
5254 reboot(RB_POWER_OFF);
5258 log_info("Rebooting.");
5259 reboot(RB_AUTOBOOT);
5263 assert_not_reached("Unknown halt action.");
5266 /* We should never reach this. */
5270 static int runlevel_main(void) {
5271 int r, runlevel, previous;
5273 if ((r = utmp_get_runlevel(&runlevel, &previous)) < 0) {
5274 printf("unknown\n");
5279 previous <= 0 ? 'N' : previous,
5280 runlevel <= 0 ? 'N' : runlevel);
5285 static void pager_open(void) {
5288 const char *pager = getenv("PAGER");
5290 if (!on_tty() || arg_no_pager)
5294 else if (!*pager || !strcmp(pager, "cat"))
5306 /* Return in the child */
5314 /* The original process turns into the PAGER */
5319 setenv("LESS", "FRSX", 0);
5320 execlp(pager, pager, NULL);
5321 execl("/bin/sh", "sh", "-c", pager, NULL);
5323 log_error("unable to execute pager '%s'", pager);
5324 _exit(EXIT_FAILURE);
5327 int main(int argc, char*argv[]) {
5328 int r, retval = EXIT_FAILURE;
5329 DBusConnection *bus = NULL;
5332 dbus_error_init(&error);
5334 log_parse_environment();
5337 if ((r = parse_argv(argc, argv)) < 0)
5340 retval = EXIT_SUCCESS;
5346 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
5347 * let's shortcut this */
5348 if (arg_action == ACTION_RUNLEVEL) {
5349 r = runlevel_main();
5350 retval = r < 0 ? EXIT_FAILURE : r;
5354 bus_connect(arg_user ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &bus, &private_bus, &error);
5356 switch (arg_action) {
5358 case ACTION_SYSTEMCTL:
5359 r = systemctl_main(bus, argc, argv, &error);
5363 case ACTION_POWEROFF:
5368 case ACTION_RUNLEVEL2:
5369 case ACTION_RUNLEVEL3:
5370 case ACTION_RUNLEVEL4:
5371 case ACTION_RUNLEVEL5:
5373 case ACTION_EMERGENCY:
5374 case ACTION_DEFAULT:
5375 r = start_with_fallback(bus);
5380 r = reload_with_fallback(bus);
5383 case ACTION_CANCEL_SHUTDOWN:
5384 r = send_shutdownd(0, 0, false, NULL);
5387 case ACTION_INVALID:
5388 case ACTION_RUNLEVEL:
5390 assert_not_reached("Unknown action");
5393 retval = r < 0 ? EXIT_FAILURE : r;
5398 dbus_connection_flush(bus);
5399 dbus_connection_close(bus);
5400 dbus_connection_unref(bus);
5403 dbus_error_free(&error);
5407 strv_free(arg_property);