1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/reboot.h>
28 #include <sys/ioctl.h>
32 #include <sys/socket.h>
35 #include <sys/prctl.h>
37 #include <dbus/dbus.h>
43 #include "utmp-wtmp.h"
47 #include "dbus-common.h"
48 #include "cgroup-show.h"
49 #include "cgroup-util.h"
51 #include "path-lookup.h"
52 #include "conf-parser.h"
53 #include "sd-daemon.h"
54 #include "shutdownd.h"
55 #include "exit-status.h"
56 #include "bus-errors.h"
58 #include "unit-name.h"
60 static const char *arg_type = NULL;
61 static char **arg_property = NULL;
62 static bool arg_all = false;
63 static bool arg_fail = false;
64 static bool arg_user = false;
65 static bool arg_global = false;
66 static bool arg_immediate = false;
67 static bool arg_no_block = false;
68 static bool arg_no_pager = false;
69 static bool arg_no_wtmp = false;
70 static bool arg_no_sync = false;
71 static bool arg_no_wall = false;
72 static bool arg_no_reload = false;
73 static bool arg_dry = false;
74 static bool arg_quiet = false;
75 static bool arg_full = false;
76 static bool arg_force = false;
77 static bool arg_defaults = false;
78 static bool arg_ask_password = false;
79 static char **arg_wall = NULL;
80 static const char *arg_kill_who = NULL;
81 static const char *arg_kill_mode = NULL;
82 static int arg_signal = SIGTERM;
83 static usec_t arg_when = 0;
102 ACTION_CANCEL_SHUTDOWN,
104 } arg_action = ACTION_SYSTEMCTL;
111 static bool private_bus = false;
113 static pid_t pager_pid = 0;
115 static int daemon_reload(DBusConnection *bus, char **args, unsigned n);
117 static bool on_tty(void) {
120 if (_unlikely_(t < 0))
121 t = isatty(STDOUT_FILENO) > 0;
126 static void spawn_ask_password_agent(void) {
129 /* We check STDIN here, not STDOUT, since this is about input,
131 if (!isatty(STDIN_FILENO))
134 if (!arg_ask_password)
139 /* Spawns a temporary TTY agent, making sure it goes away when
142 if ((child = fork()) < 0)
148 const char * const args[] = {
149 SYSTEMD_TTY_ASK_PASSWORD_AGENT_BINARY_PATH,
154 if (prctl(PR_SET_PDEATHSIG, SIGTERM) < 0)
157 /* Check whether our parent died before we were able
158 * to set the death signal */
159 if (getppid() != parent)
162 execv(args[0], (char **) args);
167 static const char *ansi_highlight(bool b) {
172 return b ? ANSI_HIGHLIGHT_ON : ANSI_HIGHLIGHT_OFF;
175 static const char *ansi_highlight_green(bool b) {
180 return b ? ANSI_HIGHLIGHT_GREEN_ON : ANSI_HIGHLIGHT_OFF;
183 static bool error_is_no_service(const DBusError *error) {
186 if (!dbus_error_is_set(error))
189 if (dbus_error_has_name(error, DBUS_ERROR_NAME_HAS_NO_OWNER))
192 if (dbus_error_has_name(error, DBUS_ERROR_SERVICE_UNKNOWN))
195 return startswith(error->name, "org.freedesktop.DBus.Error.Spawn.");
198 static int translate_bus_error_to_exit_status(int r, const DBusError *error) {
201 if (!dbus_error_is_set(error))
204 if (dbus_error_has_name(error, DBUS_ERROR_ACCESS_DENIED) ||
205 dbus_error_has_name(error, BUS_ERROR_ONLY_BY_DEPENDENCY) ||
206 dbus_error_has_name(error, BUS_ERROR_NO_ISOLATION) ||
207 dbus_error_has_name(error, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE))
208 return EXIT_NOPERMISSION;
210 if (dbus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT))
211 return EXIT_NOTINSTALLED;
213 if (dbus_error_has_name(error, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE) ||
214 dbus_error_has_name(error, BUS_ERROR_NOT_SUPPORTED))
215 return EXIT_NOTIMPLEMENTED;
217 if (dbus_error_has_name(error, BUS_ERROR_LOAD_FAILED))
218 return EXIT_NOTCONFIGURED;
226 static int bus_iter_get_basic_and_next(DBusMessageIter *iter, int type, void *data, bool next) {
231 if (dbus_message_iter_get_arg_type(iter) != type)
234 dbus_message_iter_get_basic(iter, data);
236 if (!dbus_message_iter_next(iter) != !next)
242 static void warn_wall(enum action action) {
243 static const char *table[_ACTION_MAX] = {
244 [ACTION_HALT] = "The system is going down for system halt NOW!",
245 [ACTION_REBOOT] = "The system is going down for reboot NOW!",
246 [ACTION_POWEROFF] = "The system is going down for power-off NOW!",
247 [ACTION_KEXEC] = "The system is going down for kexec reboot NOW!",
248 [ACTION_RESCUE] = "The system is going down to rescue mode NOW!",
249 [ACTION_EMERGENCY] = "The system is going down to emergency mode NOW!"
258 if (!(p = strv_join(arg_wall, " "))) {
259 log_error("Failed to join strings.");
275 utmp_wall(table[action], NULL);
280 const char *description;
281 const char *load_state;
282 const char *active_state;
283 const char *sub_state;
284 const char *following;
285 const char *unit_path;
287 const char *job_type;
288 const char *job_path;
291 static int compare_unit_info(const void *a, const void *b) {
293 const struct unit_info *u = a, *v = b;
295 d1 = strrchr(u->id, '.');
296 d2 = strrchr(v->id, '.');
301 if ((r = strcasecmp(d1, d2)) != 0)
305 return strcasecmp(u->id, v->id);
308 static bool output_show_job(const struct unit_info *u) {
311 return (!arg_type || ((dot = strrchr(u->id, '.')) &&
312 streq(dot+1, arg_type))) &&
313 (arg_all || !(streq(u->active_state, "inactive") || u->following[0]) || u->job_id > 0);
316 static void output_units_list(const struct unit_info *unit_infos, unsigned c) {
317 unsigned active_len, sub_len, job_len, n_shown = 0;
318 const struct unit_info *u;
320 active_len = sizeof("ACTIVE")-1;
321 sub_len = sizeof("SUB")-1;
322 job_len = sizeof("JOB")-1;
324 for (u = unit_infos; u < unit_infos + c; u++) {
325 if (!output_show_job(u))
328 active_len = MAX(active_len, strlen(u->active_state));
329 sub_len = MAX(sub_len, strlen(u->sub_state));
331 job_len = MAX(job_len, strlen(u->job_type));
335 printf("%-25s %-6s %-*s %-*s %-*s", "UNIT", "LOAD",
336 active_len, "ACTIVE", sub_len, "SUB", job_len, "JOB");
337 if (columns() >= 80+12 || arg_full)
338 printf(" %s\n", "DESCRIPTION");
343 for (u = unit_infos; u < unit_infos + c; u++) {
346 const char *on_loaded, *off_loaded;
347 const char *on_active, *off_active;
349 if (!output_show_job(u))
354 if (!streq(u->load_state, "loaded") &&
355 !streq(u->load_state, "banned")) {
356 on_loaded = ansi_highlight(true);
357 off_loaded = ansi_highlight(false);
359 on_loaded = off_loaded = "";
361 if (streq(u->active_state, "failed")) {
362 on_active = ansi_highlight(true);
363 off_active = ansi_highlight(false);
365 on_active = off_active = "";
367 e = arg_full ? NULL : ellipsize(u->id, 25, 33);
369 printf("%-25s %s%-6s%s %s%-*s %-*s%s%n",
371 on_loaded, u->load_state, off_loaded,
372 on_active, active_len, u->active_state,
373 sub_len, u->sub_state, off_active,
378 a -= strlen(on_loaded) + strlen(off_loaded);
379 a -= strlen(on_active) + strlen(off_active);
382 printf(" %-*s", job_len, u->job_type);
386 if (a + b + 1 < columns()) {
388 printf(" %-*s", job_len, "");
391 printf(" %s", u->description);
393 printf(" %.*s", columns() - a - b - 1, u->description);
400 printf("\nLOAD = Reflects whether the unit definition was properly loaded.\n"
401 "ACTIVE = The high-level unit activation state, i.e. generalization of SUB.\n"
402 "SUB = The low-level unit activation state, values depend on unit type.\n"
403 "JOB = Pending job for the unit.\n");
406 printf("\n%u units listed.\n", n_shown);
408 printf("\n%u units listed. Pass --all to see inactive units, too.\n", n_shown);
412 static int list_units(DBusConnection *bus, char **args, unsigned n) {
413 DBusMessage *m = NULL, *reply = NULL;
416 DBusMessageIter iter, sub, sub2;
417 unsigned c = 0, n_units = 0;
418 struct unit_info *unit_infos = NULL;
420 dbus_error_init(&error);
424 if (!(m = dbus_message_new_method_call(
425 "org.freedesktop.systemd1",
426 "/org/freedesktop/systemd1",
427 "org.freedesktop.systemd1.Manager",
429 log_error("Could not allocate message.");
433 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
434 log_error("Failed to issue method call: %s", bus_error_message(&error));
439 if (!dbus_message_iter_init(reply, &iter) ||
440 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
441 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
442 log_error("Failed to parse reply.");
447 dbus_message_iter_recurse(&iter, &sub);
449 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
452 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
453 log_error("Failed to parse reply.");
461 n_units = MAX(2*c, 16);
462 w = realloc(unit_infos, sizeof(struct unit_info) * n_units);
465 log_error("Failed to allocate unit array.");
475 dbus_message_iter_recurse(&sub, &sub2);
477 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->id, true) < 0 ||
478 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->description, true) < 0 ||
479 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->load_state, true) < 0 ||
480 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->active_state, true) < 0 ||
481 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->sub_state, true) < 0 ||
482 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->following, true) < 0 ||
483 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &u->unit_path, true) < 0 ||
484 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &u->job_id, true) < 0 ||
485 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->job_type, true) < 0 ||
486 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &u->job_path, false) < 0) {
487 log_error("Failed to parse reply.");
492 dbus_message_iter_next(&sub);
496 qsort(unit_infos, c, sizeof(struct unit_info), compare_unit_info);
497 output_units_list(unit_infos, c);
503 dbus_message_unref(m);
506 dbus_message_unref(reply);
510 dbus_error_free(&error);
515 static int dot_one_property(const char *name, const char *prop, DBusMessageIter *iter) {
516 static const char * const colors[] = {
517 "Requires", "[color=\"black\"]",
518 "RequiresOverridable", "[color=\"black\"]",
519 "Requisite", "[color=\"darkblue\"]",
520 "RequisiteOverridable", "[color=\"darkblue\"]",
521 "Wants", "[color=\"darkgrey\"]",
522 "Conflicts", "[color=\"red\"]",
523 "ConflictedBy", "[color=\"red\"]",
524 "After", "[color=\"green\"]"
527 const char *c = NULL;
534 for (i = 0; i < ELEMENTSOF(colors); i += 2)
535 if (streq(colors[i], prop)) {
543 if (arg_dot != DOT_ALL)
544 if ((arg_dot == DOT_ORDER) != streq(prop, "After"))
547 switch (dbus_message_iter_get_arg_type(iter)) {
549 case DBUS_TYPE_ARRAY:
551 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
554 dbus_message_iter_recurse(iter, &sub);
556 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
559 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
560 dbus_message_iter_get_basic(&sub, &s);
561 printf("\t\"%s\"->\"%s\" %s;\n", name, s, c);
563 dbus_message_iter_next(&sub);
573 static int dot_one(DBusConnection *bus, const char *name, const char *path) {
574 DBusMessage *m = NULL, *reply = NULL;
575 const char *interface = "org.freedesktop.systemd1.Unit";
578 DBusMessageIter iter, sub, sub2, sub3;
583 dbus_error_init(&error);
585 if (!(m = dbus_message_new_method_call(
586 "org.freedesktop.systemd1",
588 "org.freedesktop.DBus.Properties",
590 log_error("Could not allocate message.");
595 if (!dbus_message_append_args(m,
596 DBUS_TYPE_STRING, &interface,
597 DBUS_TYPE_INVALID)) {
598 log_error("Could not append arguments to message.");
603 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
604 log_error("Failed to issue method call: %s", bus_error_message(&error));
609 if (!dbus_message_iter_init(reply, &iter) ||
610 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
611 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
612 log_error("Failed to parse reply.");
617 dbus_message_iter_recurse(&iter, &sub);
619 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
622 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
623 log_error("Failed to parse reply.");
628 dbus_message_iter_recurse(&sub, &sub2);
630 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &prop, true) < 0) {
631 log_error("Failed to parse reply.");
636 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
637 log_error("Failed to parse reply.");
642 dbus_message_iter_recurse(&sub2, &sub3);
644 if (dot_one_property(name, prop, &sub3)) {
645 log_error("Failed to parse reply.");
650 dbus_message_iter_next(&sub);
657 dbus_message_unref(m);
660 dbus_message_unref(reply);
662 dbus_error_free(&error);
667 static int dot(DBusConnection *bus, char **args, unsigned n) {
668 DBusMessage *m = NULL, *reply = NULL;
671 DBusMessageIter iter, sub, sub2;
673 dbus_error_init(&error);
677 if (!(m = dbus_message_new_method_call(
678 "org.freedesktop.systemd1",
679 "/org/freedesktop/systemd1",
680 "org.freedesktop.systemd1.Manager",
682 log_error("Could not allocate message.");
686 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
687 log_error("Failed to issue method call: %s", bus_error_message(&error));
692 if (!dbus_message_iter_init(reply, &iter) ||
693 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
694 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
695 log_error("Failed to parse reply.");
700 printf("digraph systemd {\n");
702 dbus_message_iter_recurse(&iter, &sub);
703 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
704 const char *id, *description, *load_state, *active_state, *sub_state, *following, *unit_path;
706 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
707 log_error("Failed to parse reply.");
712 dbus_message_iter_recurse(&sub, &sub2);
714 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
715 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
716 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
717 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
718 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
719 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &following, true) < 0 ||
720 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, true) < 0) {
721 log_error("Failed to parse reply.");
726 if ((r = dot_one(bus, id, unit_path)) < 0)
729 /* printf("\t\"%s\";\n", id); */
730 dbus_message_iter_next(&sub);
735 log_info(" Color legend: black = Requires\n"
736 " dark blue = Requisite\n"
737 " dark grey = Wants\n"
741 if (isatty(fileno(stdout)))
742 log_notice("-- You probably want to process this output with graphviz' dot tool.\n"
743 "-- Try a shell pipeline like 'systemctl dot | dot -Tsvg > systemd.svg'!\n");
749 dbus_message_unref(m);
752 dbus_message_unref(reply);
754 dbus_error_free(&error);
759 static int list_jobs(DBusConnection *bus, char **args, unsigned n) {
760 DBusMessage *m = NULL, *reply = NULL;
763 DBusMessageIter iter, sub, sub2;
766 dbus_error_init(&error);
770 if (!(m = dbus_message_new_method_call(
771 "org.freedesktop.systemd1",
772 "/org/freedesktop/systemd1",
773 "org.freedesktop.systemd1.Manager",
775 log_error("Could not allocate message.");
779 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
780 log_error("Failed to issue method call: %s", bus_error_message(&error));
785 if (!dbus_message_iter_init(reply, &iter) ||
786 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
787 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
788 log_error("Failed to parse reply.");
793 dbus_message_iter_recurse(&iter, &sub);
795 if (isatty(STDOUT_FILENO))
796 printf("%4s %-25s %-15s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
798 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
799 const char *name, *type, *state, *job_path, *unit_path;
803 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
804 log_error("Failed to parse reply.");
809 dbus_message_iter_recurse(&sub, &sub2);
811 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
812 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
813 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
814 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
815 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
816 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
817 log_error("Failed to parse reply.");
822 e = arg_full ? NULL : ellipsize(name, 25, 33);
823 printf("%4u %-25s %-15s %-7s\n", id, e ? e : name, type, state);
828 dbus_message_iter_next(&sub);
831 if (isatty(STDOUT_FILENO))
832 printf("\n%u jobs listed.\n", k);
838 dbus_message_unref(m);
841 dbus_message_unref(reply);
843 dbus_error_free(&error);
848 static int load_unit(DBusConnection *bus, char **args, unsigned n) {
849 DBusMessage *m = NULL, *reply = NULL;
854 dbus_error_init(&error);
859 for (i = 1; i < n; i++) {
861 if (!(m = dbus_message_new_method_call(
862 "org.freedesktop.systemd1",
863 "/org/freedesktop/systemd1",
864 "org.freedesktop.systemd1.Manager",
866 log_error("Could not allocate message.");
871 if (!dbus_message_append_args(m,
872 DBUS_TYPE_STRING, &args[i],
873 DBUS_TYPE_INVALID)) {
874 log_error("Could not append arguments to message.");
879 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
880 log_error("Failed to issue method call: %s", bus_error_message(&error));
885 dbus_message_unref(m);
886 dbus_message_unref(reply);
895 dbus_message_unref(m);
898 dbus_message_unref(reply);
900 dbus_error_free(&error);
905 static int cancel_job(DBusConnection *bus, char **args, unsigned n) {
906 DBusMessage *m = NULL, *reply = NULL;
911 dbus_error_init(&error);
917 return daemon_reload(bus, args, n);
919 for (i = 1; i < n; i++) {
923 if (!(m = dbus_message_new_method_call(
924 "org.freedesktop.systemd1",
925 "/org/freedesktop/systemd1",
926 "org.freedesktop.systemd1.Manager",
928 log_error("Could not allocate message.");
933 if ((r = safe_atou(args[i], &id)) < 0) {
934 log_error("Failed to parse job id: %s", strerror(-r));
938 assert_cc(sizeof(uint32_t) == sizeof(id));
939 if (!dbus_message_append_args(m,
940 DBUS_TYPE_UINT32, &id,
941 DBUS_TYPE_INVALID)) {
942 log_error("Could not append arguments to message.");
947 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
948 log_error("Failed to issue method call: %s", bus_error_message(&error));
953 if (!dbus_message_get_args(reply, &error,
954 DBUS_TYPE_OBJECT_PATH, &path,
955 DBUS_TYPE_INVALID)) {
956 log_error("Failed to parse reply: %s", bus_error_message(&error));
961 dbus_message_unref(m);
962 if (!(m = dbus_message_new_method_call(
963 "org.freedesktop.systemd1",
965 "org.freedesktop.systemd1.Job",
967 log_error("Could not allocate message.");
972 dbus_message_unref(reply);
973 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
974 log_error("Failed to issue method call: %s", bus_error_message(&error));
979 dbus_message_unref(m);
980 dbus_message_unref(reply);
988 dbus_message_unref(m);
991 dbus_message_unref(reply);
993 dbus_error_free(&error);
998 static bool need_daemon_reload(DBusConnection *bus, const char *unit) {
999 DBusMessage *m = NULL, *reply = NULL;
1000 dbus_bool_t b = FALSE;
1001 DBusMessageIter iter, sub;
1003 *interface = "org.freedesktop.systemd1.Unit",
1004 *property = "NeedDaemonReload",
1007 /* We ignore all errors here, since this is used to show a warning only */
1009 if (!(m = dbus_message_new_method_call(
1010 "org.freedesktop.systemd1",
1011 "/org/freedesktop/systemd1",
1012 "org.freedesktop.systemd1.Manager",
1016 if (!dbus_message_append_args(m,
1017 DBUS_TYPE_STRING, &unit,
1021 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
1024 if (!dbus_message_get_args(reply, NULL,
1025 DBUS_TYPE_OBJECT_PATH, &path,
1029 dbus_message_unref(m);
1030 if (!(m = dbus_message_new_method_call(
1031 "org.freedesktop.systemd1",
1033 "org.freedesktop.DBus.Properties",
1037 if (!dbus_message_append_args(m,
1038 DBUS_TYPE_STRING, &interface,
1039 DBUS_TYPE_STRING, &property,
1040 DBUS_TYPE_INVALID)) {
1044 dbus_message_unref(reply);
1045 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
1048 if (!dbus_message_iter_init(reply, &iter) ||
1049 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
1052 dbus_message_iter_recurse(&iter, &sub);
1054 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_BOOLEAN)
1057 dbus_message_iter_get_basic(&sub, &b);
1061 dbus_message_unref(m);
1064 dbus_message_unref(reply);
1069 typedef struct WaitData {
1074 static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
1082 dbus_error_init(&error);
1084 log_debug("Got D-Bus request: %s.%s() on %s",
1085 dbus_message_get_interface(message),
1086 dbus_message_get_member(message),
1087 dbus_message_get_path(message));
1089 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
1090 log_error("Warning! D-Bus connection terminated.");
1091 dbus_connection_close(connection);
1093 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
1096 dbus_bool_t success = true;
1098 if (!dbus_message_get_args(message, &error,
1099 DBUS_TYPE_UINT32, &id,
1100 DBUS_TYPE_OBJECT_PATH, &path,
1101 DBUS_TYPE_BOOLEAN, &success,
1103 log_error("Failed to parse message: %s", bus_error_message(&error));
1107 if ((p = set_remove(d->set, (char*) path)))
1115 dbus_error_free(&error);
1116 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1119 static int enable_wait_for_jobs(DBusConnection *bus) {
1127 dbus_error_init(&error);
1128 dbus_bus_add_match(bus,
1130 "sender='org.freedesktop.systemd1',"
1131 "interface='org.freedesktop.systemd1.Manager',"
1132 "member='JobRemoved',"
1133 "path='/org/freedesktop/systemd1'",
1136 if (dbus_error_is_set(&error)) {
1137 log_error("Failed to add match: %s", bus_error_message(&error));
1138 dbus_error_free(&error);
1142 /* This is slightly dirty, since we don't undo the match registrations. */
1146 static int wait_for_jobs(DBusConnection *bus, Set *s) {
1157 if (!dbus_connection_add_filter(bus, wait_filter, &d, NULL)) {
1158 log_error("Failed to add filter.");
1163 while (!set_isempty(s) &&
1164 dbus_connection_read_write_dispatch(bus, -1))
1167 if (!arg_quiet && d.failed)
1168 log_error("Job failed. See system logs and 'systemctl status' for details.");
1170 r = d.failed ? -EIO : 0;
1173 /* This is slightly dirty, since we don't undo the filter registration. */
1178 static int start_unit_one(
1179 DBusConnection *bus,
1186 DBusMessage *m = NULL, *reply = NULL;
1195 assert(arg_no_block || s);
1197 if (!(m = dbus_message_new_method_call(
1198 "org.freedesktop.systemd1",
1199 "/org/freedesktop/systemd1",
1200 "org.freedesktop.systemd1.Manager",
1202 log_error("Could not allocate message.");
1207 if (!dbus_message_append_args(m,
1208 DBUS_TYPE_STRING, &name,
1209 DBUS_TYPE_STRING, &mode,
1210 DBUS_TYPE_INVALID)) {
1211 log_error("Could not append arguments to message.");
1216 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, error))) {
1218 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(error)) {
1219 /* There's always a fallback possible for
1220 * legacy actions. */
1225 log_error("Failed to issue method call: %s", bus_error_message(error));
1230 if (!dbus_message_get_args(reply, error,
1231 DBUS_TYPE_OBJECT_PATH, &path,
1232 DBUS_TYPE_INVALID)) {
1233 log_error("Failed to parse reply: %s", bus_error_message(error));
1238 if (need_daemon_reload(bus, name))
1239 log_warning("Unit file of created job changed on disk, 'systemctl %s daemon-reload' recommended.",
1240 arg_user ? "--user" : "--system");
1242 if (!arg_no_block) {
1245 if (!(p = strdup(path))) {
1246 log_error("Failed to duplicate path.");
1251 if ((r = set_put(s, p)) < 0) {
1253 log_error("Failed to add path to set.");
1262 dbus_message_unref(m);
1265 dbus_message_unref(reply);
1270 static enum action verb_to_action(const char *verb) {
1271 if (streq(verb, "halt"))
1273 else if (streq(verb, "poweroff"))
1274 return ACTION_POWEROFF;
1275 else if (streq(verb, "reboot"))
1276 return ACTION_REBOOT;
1277 else if (streq(verb, "kexec"))
1278 return ACTION_KEXEC;
1279 else if (streq(verb, "rescue"))
1280 return ACTION_RESCUE;
1281 else if (streq(verb, "emergency"))
1282 return ACTION_EMERGENCY;
1283 else if (streq(verb, "default"))
1284 return ACTION_DEFAULT;
1285 else if (streq(verb, "exit"))
1288 return ACTION_INVALID;
1291 static int start_unit(DBusConnection *bus, char **args, unsigned n) {
1293 static const char * const table[_ACTION_MAX] = {
1294 [ACTION_HALT] = SPECIAL_HALT_TARGET,
1295 [ACTION_POWEROFF] = SPECIAL_POWEROFF_TARGET,
1296 [ACTION_REBOOT] = SPECIAL_REBOOT_TARGET,
1297 [ACTION_KEXEC] = SPECIAL_KEXEC_TARGET,
1298 [ACTION_RUNLEVEL2] = SPECIAL_RUNLEVEL2_TARGET,
1299 [ACTION_RUNLEVEL3] = SPECIAL_RUNLEVEL3_TARGET,
1300 [ACTION_RUNLEVEL4] = SPECIAL_RUNLEVEL4_TARGET,
1301 [ACTION_RUNLEVEL5] = SPECIAL_RUNLEVEL5_TARGET,
1302 [ACTION_RESCUE] = SPECIAL_RESCUE_TARGET,
1303 [ACTION_EMERGENCY] = SPECIAL_EMERGENCY_TARGET,
1304 [ACTION_DEFAULT] = SPECIAL_DEFAULT_TARGET,
1305 [ACTION_EXIT] = SPECIAL_EXIT_TARGET
1310 const char *method, *mode, *one_name;
1314 dbus_error_init(&error);
1318 spawn_ask_password_agent();
1320 if (arg_action == ACTION_SYSTEMCTL) {
1322 streq(args[0], "stop") ? "StopUnit" :
1323 streq(args[0], "reload") ? "ReloadUnit" :
1324 streq(args[0], "restart") ? "RestartUnit" :
1325 streq(args[0], "try-restart") ||
1326 streq(args[0], "condrestart") ? "TryRestartUnit" :
1327 streq(args[0], "reload-or-restart") ? "ReloadOrRestartUnit" :
1328 streq(args[0], "reload-or-try-restart") ||
1329 streq(args[0], "force-reload") ? "ReloadOrTryRestartUnit" :
1333 (streq(args[0], "isolate") ||
1334 streq(args[0], "rescue") ||
1335 streq(args[0], "emergency")) ? "isolate" :
1339 one_name = table[verb_to_action(args[0])];
1342 assert(arg_action < ELEMENTSOF(table));
1343 assert(table[arg_action]);
1345 method = "StartUnit";
1347 mode = (arg_action == ACTION_EMERGENCY ||
1348 arg_action == ACTION_RESCUE ||
1349 arg_action == ACTION_RUNLEVEL2 ||
1350 arg_action == ACTION_RUNLEVEL3 ||
1351 arg_action == ACTION_RUNLEVEL4 ||
1352 arg_action == ACTION_RUNLEVEL5) ? "isolate" : "replace";
1354 one_name = table[arg_action];
1357 if (!arg_no_block) {
1358 if ((ret = enable_wait_for_jobs(bus)) < 0) {
1359 log_error("Could not watch jobs: %s", strerror(-ret));
1363 if (!(s = set_new(string_hash_func, string_compare_func))) {
1364 log_error("Failed to allocate set.");
1371 if ((ret = start_unit_one(bus, method, one_name, mode, &error, s)) <= 0)
1374 for (i = 1; i < n; i++)
1375 if ((r = start_unit_one(bus, method, args[i], mode, &error, s)) != 0) {
1376 ret = translate_bus_error_to_exit_status(r, &error);
1377 dbus_error_free(&error);
1382 if ((r = wait_for_jobs(bus, s)) < 0) {
1391 dbus_error_free(&error);
1396 static int start_special(DBusConnection *bus, char **args, unsigned n) {
1403 (streq(args[0], "halt") ||
1404 streq(args[0], "poweroff") ||
1405 streq(args[0], "reboot") ||
1406 streq(args[0], "kexec") ||
1407 streq(args[0], "exit")))
1408 return daemon_reload(bus, args, n);
1410 r = start_unit(bus, args, n);
1413 warn_wall(verb_to_action(args[0]));
1418 static int check_unit(DBusConnection *bus, char **args, unsigned n) {
1419 DBusMessage *m = NULL, *reply = NULL;
1421 *interface = "org.freedesktop.systemd1.Unit",
1422 *property = "ActiveState";
1423 int r = 3; /* According to LSB: "program is not running" */
1430 dbus_error_init(&error);
1432 for (i = 1; i < n; i++) {
1433 const char *path = NULL;
1435 DBusMessageIter iter, sub;
1437 if (!(m = dbus_message_new_method_call(
1438 "org.freedesktop.systemd1",
1439 "/org/freedesktop/systemd1",
1440 "org.freedesktop.systemd1.Manager",
1442 log_error("Could not allocate message.");
1447 if (!dbus_message_append_args(m,
1448 DBUS_TYPE_STRING, &args[i],
1449 DBUS_TYPE_INVALID)) {
1450 log_error("Could not append arguments to message.");
1455 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1457 /* Hmm, cannot figure out anything about this unit... */
1461 dbus_error_free(&error);
1462 dbus_message_unref(m);
1466 if (!dbus_message_get_args(reply, &error,
1467 DBUS_TYPE_OBJECT_PATH, &path,
1468 DBUS_TYPE_INVALID)) {
1469 log_error("Failed to parse reply: %s", bus_error_message(&error));
1474 dbus_message_unref(m);
1475 if (!(m = dbus_message_new_method_call(
1476 "org.freedesktop.systemd1",
1478 "org.freedesktop.DBus.Properties",
1480 log_error("Could not allocate message.");
1485 if (!dbus_message_append_args(m,
1486 DBUS_TYPE_STRING, &interface,
1487 DBUS_TYPE_STRING, &property,
1488 DBUS_TYPE_INVALID)) {
1489 log_error("Could not append arguments to message.");
1494 dbus_message_unref(reply);
1495 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1496 log_error("Failed to issue method call: %s", bus_error_message(&error));
1501 if (!dbus_message_iter_init(reply, &iter) ||
1502 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1503 log_error("Failed to parse reply.");
1508 dbus_message_iter_recurse(&iter, &sub);
1510 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1511 log_error("Failed to parse reply.");
1516 dbus_message_iter_get_basic(&sub, &state);
1521 if (streq(state, "active") || streq(state, "reloading"))
1524 dbus_message_unref(m);
1525 dbus_message_unref(reply);
1531 dbus_message_unref(m);
1534 dbus_message_unref(reply);
1536 dbus_error_free(&error);
1541 static int kill_unit(DBusConnection *bus, char **args, unsigned n) {
1542 DBusMessage *m = NULL, *reply = NULL;
1550 dbus_error_init(&error);
1553 arg_kill_who = "all";
1556 arg_kill_mode = streq(arg_kill_who, "all") ? "control-group" : "process";
1558 for (i = 1; i < n; i++) {
1560 if (!(m = dbus_message_new_method_call(
1561 "org.freedesktop.systemd1",
1562 "/org/freedesktop/systemd1",
1563 "org.freedesktop.systemd1.Manager",
1565 log_error("Could not allocate message.");
1570 if (!dbus_message_append_args(m,
1571 DBUS_TYPE_STRING, &args[i],
1572 DBUS_TYPE_STRING, &arg_kill_who,
1573 DBUS_TYPE_STRING, &arg_kill_mode,
1574 DBUS_TYPE_INT32, &arg_signal,
1575 DBUS_TYPE_INVALID)) {
1576 log_error("Could not append arguments to message.");
1581 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1582 log_error("Failed to issue method call: %s", bus_error_message(&error));
1583 dbus_error_free(&error);
1587 dbus_message_unref(m);
1590 dbus_message_unref(reply);
1596 dbus_message_unref(m);
1599 dbus_message_unref(reply);
1601 dbus_error_free(&error);
1606 typedef struct ExecStatusInfo {
1612 usec_t start_timestamp;
1613 usec_t exit_timestamp;
1618 LIST_FIELDS(struct ExecStatusInfo, exec);
1621 static void exec_status_info_free(ExecStatusInfo *i) {
1629 static int exec_status_info_deserialize(DBusMessageIter *sub, ExecStatusInfo *i) {
1630 uint64_t start_timestamp, exit_timestamp;
1631 DBusMessageIter sub2, sub3;
1635 int32_t code, status;
1641 if (dbus_message_iter_get_arg_type(sub) != DBUS_TYPE_STRUCT)
1644 dbus_message_iter_recurse(sub, &sub2);
1646 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
1649 if (!(i->path = strdup(path)))
1652 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
1653 dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
1657 dbus_message_iter_recurse(&sub2, &sub3);
1658 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1659 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1660 dbus_message_iter_next(&sub3);
1665 if (!(i->argv = new0(char*, n+1)))
1669 dbus_message_iter_recurse(&sub2, &sub3);
1670 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1673 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1674 dbus_message_iter_get_basic(&sub3, &s);
1675 dbus_message_iter_next(&sub3);
1677 if (!(i->argv[n++] = strdup(s)))
1681 if (!dbus_message_iter_next(&sub2) ||
1682 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, true) < 0 ||
1683 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp, true) < 0 ||
1684 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp, true) < 0 ||
1685 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) < 0 ||
1686 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) < 0 ||
1687 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) < 0)
1691 i->start_timestamp = (usec_t) start_timestamp;
1692 i->exit_timestamp = (usec_t) exit_timestamp;
1693 i->pid = (pid_t) pid;
1700 typedef struct UnitStatusInfo {
1702 const char *load_state;
1703 const char *active_state;
1704 const char *sub_state;
1706 const char *description;
1707 const char *following;
1710 const char *default_control_group;
1712 usec_t inactive_exit_timestamp;
1713 usec_t active_enter_timestamp;
1714 usec_t active_exit_timestamp;
1715 usec_t inactive_enter_timestamp;
1717 bool need_daemon_reload;
1722 const char *status_text;
1724 #ifdef HAVE_SYSV_COMPAT
1728 usec_t start_timestamp;
1729 usec_t exit_timestamp;
1731 int exit_code, exit_status;
1734 unsigned n_accepted;
1735 unsigned n_connections;
1739 const char *sysfs_path;
1741 /* Mount, Automount */
1747 LIST_HEAD(ExecStatusInfo, exec);
1750 static void print_status_info(UnitStatusInfo *i) {
1752 const char *on, *off, *ss;
1754 char since1[FORMAT_TIMESTAMP_PRETTY_MAX], *s1;
1755 char since2[FORMAT_TIMESTAMP_MAX], *s2;
1759 /* This shows pretty information about a unit. See
1760 * print_property() for a low-level property printer */
1762 printf("%s", strna(i->id));
1764 if (i->description && !streq_ptr(i->id, i->description))
1765 printf(" - %s", i->description);
1770 printf("\t Follow: unit currently follows state of %s\n", i->following);
1772 if (streq_ptr(i->load_state, "failed") ||
1773 streq_ptr(i->load_state, "banned")) {
1774 on = ansi_highlight(true);
1775 off = ansi_highlight(false);
1780 printf("\t Loaded: %s%s%s (%s)\n", on, strna(i->load_state), off, i->path);
1782 printf("\t Loaded: %s%s%s\n", on, strna(i->load_state), off);
1784 ss = streq_ptr(i->active_state, i->sub_state) ? NULL : i->sub_state;
1786 if (streq_ptr(i->active_state, "failed")) {
1787 on = ansi_highlight(true);
1788 off = ansi_highlight(false);
1789 } else if (streq_ptr(i->active_state, "active") || streq_ptr(i->active_state, "reloading")) {
1790 on = ansi_highlight_green(true);
1791 off = ansi_highlight_green(false);
1796 printf("\t Active: %s%s (%s)%s",
1798 strna(i->active_state),
1802 printf("\t Active: %s%s%s",
1804 strna(i->active_state),
1807 timestamp = (streq_ptr(i->active_state, "active") ||
1808 streq_ptr(i->active_state, "reloading")) ? i->active_enter_timestamp :
1809 (streq_ptr(i->active_state, "inactive") ||
1810 streq_ptr(i->active_state, "failed")) ? i->inactive_enter_timestamp :
1811 streq_ptr(i->active_state, "activating") ? i->inactive_exit_timestamp :
1812 i->active_exit_timestamp;
1814 s1 = format_timestamp_pretty(since1, sizeof(since1), timestamp);
1815 s2 = format_timestamp(since2, sizeof(since2), timestamp);
1818 printf(" since %s; %s\n", s2, s1);
1820 printf(" since %s\n", s2);
1825 printf("\t Device: %s\n", i->sysfs_path);
1827 printf("\t Where: %s\n", i->where);
1829 printf("\t What: %s\n", i->what);
1832 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
1834 LIST_FOREACH(exec, p, i->exec) {
1837 /* Only show exited processes here */
1841 t = strv_join(p->argv, " ");
1842 printf("\t Process: %u (%s, code=%s, ", p->pid, strna(t), sigchld_code_to_string(p->code));
1845 if (p->code == CLD_EXITED) {
1848 printf("status=%i", p->status);
1850 #ifdef HAVE_SYSV_COMPAT
1851 if ((c = exit_status_to_string(p->status, i->is_sysv ? EXIT_STATUS_LSB : EXIT_STATUS_SYSTEMD)))
1853 if ((c = exit_status_to_string(p->status, EXIT_STATUS_SYSTEMD)))
1858 printf("signal=%s", signal_to_string(p->status));
1861 if (i->main_pid == p->pid &&
1862 i->start_timestamp == p->start_timestamp &&
1863 i->exit_timestamp == p->start_timestamp)
1864 /* Let's not show this twice */
1867 if (p->pid == i->control_pid)
1871 if (i->main_pid > 0 || i->control_pid > 0) {
1874 if (i->main_pid > 0) {
1875 printf("Main PID: %u", (unsigned) i->main_pid);
1879 get_process_name(i->main_pid, &t);
1884 } else if (i->exit_code > 0) {
1885 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
1887 if (i->exit_code == CLD_EXITED) {
1890 printf("status=%i", i->exit_status);
1892 #ifdef HAVE_SYSV_COMPAT
1893 if ((c = exit_status_to_string(i->exit_status, i->is_sysv ? EXIT_STATUS_LSB : EXIT_STATUS_SYSTEMD)))
1895 if ((c = exit_status_to_string(i->exit_status, EXIT_STATUS_SYSTEMD)))
1900 printf("signal=%s", signal_to_string(i->exit_status));
1905 if (i->main_pid > 0 && i->control_pid > 0)
1908 if (i->control_pid > 0) {
1911 printf(" Control: %u", (unsigned) i->control_pid);
1913 get_process_name(i->control_pid, &t);
1924 printf("\t Status: \"%s\"\n", i->status_text);
1926 if (i->default_control_group) {
1929 printf("\t CGroup: %s\n", i->default_control_group);
1931 if ((c = columns()) > 18)
1936 show_cgroup_by_path(i->default_control_group, "\t\t ", c);
1939 if (i->need_daemon_reload)
1940 printf("\n%sWarning:%s Unit file changed on disk, 'systemctl %s daemon-reload' recommended.\n",
1941 ansi_highlight(true),
1942 ansi_highlight(false),
1943 arg_user ? "--user" : "--system");
1946 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
1948 switch (dbus_message_iter_get_arg_type(iter)) {
1950 case DBUS_TYPE_STRING: {
1953 dbus_message_iter_get_basic(iter, &s);
1956 if (streq(name, "Id"))
1958 else if (streq(name, "LoadState"))
1960 else if (streq(name, "ActiveState"))
1961 i->active_state = s;
1962 else if (streq(name, "SubState"))
1964 else if (streq(name, "Description"))
1966 else if (streq(name, "FragmentPath"))
1968 #ifdef HAVE_SYSV_COMPAT
1969 else if (streq(name, "SysVPath")) {
1974 else if (streq(name, "DefaultControlGroup"))
1975 i->default_control_group = s;
1976 else if (streq(name, "StatusText"))
1978 else if (streq(name, "SysFSPath"))
1980 else if (streq(name, "Where"))
1982 else if (streq(name, "What"))
1984 else if (streq(name, "Following"))
1991 case DBUS_TYPE_BOOLEAN: {
1994 dbus_message_iter_get_basic(iter, &b);
1996 if (streq(name, "Accept"))
1998 else if (streq(name, "NeedDaemonReload"))
1999 i->need_daemon_reload = b;
2004 case DBUS_TYPE_UINT32: {
2007 dbus_message_iter_get_basic(iter, &u);
2009 if (streq(name, "MainPID")) {
2011 i->main_pid = (pid_t) u;
2014 } else if (streq(name, "ControlPID"))
2015 i->control_pid = (pid_t) u;
2016 else if (streq(name, "ExecMainPID")) {
2018 i->main_pid = (pid_t) u;
2019 } else if (streq(name, "NAccepted"))
2021 else if (streq(name, "NConnections"))
2022 i->n_connections = u;
2027 case DBUS_TYPE_INT32: {
2030 dbus_message_iter_get_basic(iter, &j);
2032 if (streq(name, "ExecMainCode"))
2033 i->exit_code = (int) j;
2034 else if (streq(name, "ExecMainStatus"))
2035 i->exit_status = (int) j;
2040 case DBUS_TYPE_UINT64: {
2043 dbus_message_iter_get_basic(iter, &u);
2045 if (streq(name, "ExecMainStartTimestamp"))
2046 i->start_timestamp = (usec_t) u;
2047 else if (streq(name, "ExecMainExitTimestamp"))
2048 i->exit_timestamp = (usec_t) u;
2049 else if (streq(name, "ActiveEnterTimestamp"))
2050 i->active_enter_timestamp = (usec_t) u;
2051 else if (streq(name, "InactiveEnterTimestamp"))
2052 i->inactive_enter_timestamp = (usec_t) u;
2053 else if (streq(name, "InactiveExitTimestamp"))
2054 i->inactive_exit_timestamp = (usec_t) u;
2055 else if (streq(name, "ActiveExitTimestamp"))
2056 i->active_exit_timestamp = (usec_t) u;
2061 case DBUS_TYPE_ARRAY: {
2063 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT &&
2064 startswith(name, "Exec")) {
2065 DBusMessageIter sub;
2067 dbus_message_iter_recurse(iter, &sub);
2068 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2069 ExecStatusInfo *info;
2072 if (!(info = new0(ExecStatusInfo, 1)))
2075 if ((r = exec_status_info_deserialize(&sub, info)) < 0) {
2080 LIST_PREPEND(ExecStatusInfo, exec, i->exec, info);
2082 dbus_message_iter_next(&sub);
2093 static int print_property(const char *name, DBusMessageIter *iter) {
2097 /* This is a low-level property printer, see
2098 * print_status_info() for the nicer output */
2100 if (arg_property && !strv_find(arg_property, name))
2103 switch (dbus_message_iter_get_arg_type(iter)) {
2105 case DBUS_TYPE_STRING: {
2107 dbus_message_iter_get_basic(iter, &s);
2109 if (arg_all || s[0])
2110 printf("%s=%s\n", name, s);
2115 case DBUS_TYPE_BOOLEAN: {
2117 dbus_message_iter_get_basic(iter, &b);
2118 printf("%s=%s\n", name, yes_no(b));
2123 case DBUS_TYPE_UINT64: {
2125 dbus_message_iter_get_basic(iter, &u);
2127 /* Yes, heuristics! But we can change this check
2128 * should it turn out to not be sufficient */
2130 if (strstr(name, "Timestamp")) {
2131 char timestamp[FORMAT_TIMESTAMP_MAX], *t;
2133 if ((t = format_timestamp(timestamp, sizeof(timestamp), u)) || arg_all)
2134 printf("%s=%s\n", name, strempty(t));
2135 } else if (strstr(name, "USec")) {
2136 char timespan[FORMAT_TIMESPAN_MAX];
2138 printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u));
2140 printf("%s=%llu\n", name, (unsigned long long) u);
2145 case DBUS_TYPE_UINT32: {
2147 dbus_message_iter_get_basic(iter, &u);
2149 if (strstr(name, "UMask") || strstr(name, "Mode"))
2150 printf("%s=%04o\n", name, u);
2152 printf("%s=%u\n", name, (unsigned) u);
2157 case DBUS_TYPE_INT32: {
2159 dbus_message_iter_get_basic(iter, &i);
2161 printf("%s=%i\n", name, (int) i);
2165 case DBUS_TYPE_DOUBLE: {
2167 dbus_message_iter_get_basic(iter, &d);
2169 printf("%s=%g\n", name, d);
2173 case DBUS_TYPE_STRUCT: {
2174 DBusMessageIter sub;
2175 dbus_message_iter_recurse(iter, &sub);
2177 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
2180 dbus_message_iter_get_basic(&sub, &u);
2183 printf("%s=%u\n", name, (unsigned) u);
2185 printf("%s=\n", name);
2188 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
2191 dbus_message_iter_get_basic(&sub, &s);
2193 if (arg_all || s[0])
2194 printf("%s=%s\n", name, s);
2202 case DBUS_TYPE_ARRAY:
2204 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
2205 DBusMessageIter sub;
2208 dbus_message_iter_recurse(iter, &sub);
2210 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2211 printf("%s=", name);
2213 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2216 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
2217 dbus_message_iter_get_basic(&sub, &s);
2218 printf("%s%s", space ? " " : "", s);
2221 dbus_message_iter_next(&sub);
2229 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_BYTE) {
2230 DBusMessageIter sub;
2232 dbus_message_iter_recurse(iter, &sub);
2234 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2235 printf("%s=", name);
2237 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2240 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_BYTE);
2241 dbus_message_iter_get_basic(&sub, &u);
2244 dbus_message_iter_next(&sub);
2252 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
2253 DBusMessageIter sub, sub2;
2255 dbus_message_iter_recurse(iter, &sub);
2256 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2257 const char *type, *path;
2259 dbus_message_iter_recurse(&sub, &sub2);
2261 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
2262 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
2263 printf("%s=%s\n", type, path);
2265 dbus_message_iter_next(&sub);
2270 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
2271 DBusMessageIter sub, sub2;
2273 dbus_message_iter_recurse(iter, &sub);
2274 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2276 uint64_t value, next_elapse;
2278 dbus_message_iter_recurse(&sub, &sub2);
2280 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
2281 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
2282 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
2283 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
2285 printf("%s={ value=%s ; next_elapse=%s }\n",
2287 format_timespan(timespan1, sizeof(timespan1), value),
2288 format_timespan(timespan2, sizeof(timespan2), next_elapse));
2291 dbus_message_iter_next(&sub);
2296 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
2297 DBusMessageIter sub;
2299 dbus_message_iter_recurse(iter, &sub);
2300 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2301 ExecStatusInfo info;
2304 if (exec_status_info_deserialize(&sub, &info) >= 0) {
2305 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
2308 t = strv_join(info.argv, " ");
2310 printf("%s={ path=%s ; argv[]=%s ; ignore=%s ; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n",
2314 yes_no(info.ignore),
2315 strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)),
2316 strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)),
2317 (unsigned) info. pid,
2318 sigchld_code_to_string(info.code),
2320 info.code == CLD_EXITED ? "" : "/",
2321 strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status)));
2327 strv_free(info.argv);
2329 dbus_message_iter_next(&sub);
2339 printf("%s=[unprintable]\n", name);
2344 static int show_one(const char *verb, DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
2345 DBusMessage *m = NULL, *reply = NULL;
2346 const char *interface = "";
2349 DBusMessageIter iter, sub, sub2, sub3;
2350 UnitStatusInfo info;
2358 dbus_error_init(&error);
2360 if (!(m = dbus_message_new_method_call(
2361 "org.freedesktop.systemd1",
2363 "org.freedesktop.DBus.Properties",
2365 log_error("Could not allocate message.");
2370 if (!dbus_message_append_args(m,
2371 DBUS_TYPE_STRING, &interface,
2372 DBUS_TYPE_INVALID)) {
2373 log_error("Could not append arguments to message.");
2378 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2379 log_error("Failed to issue method call: %s", bus_error_message(&error));
2384 if (!dbus_message_iter_init(reply, &iter) ||
2385 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
2386 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
2387 log_error("Failed to parse reply.");
2392 dbus_message_iter_recurse(&iter, &sub);
2399 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2402 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
2403 log_error("Failed to parse reply.");
2408 dbus_message_iter_recurse(&sub, &sub2);
2410 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
2411 log_error("Failed to parse reply.");
2416 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
2417 log_error("Failed to parse reply.");
2422 dbus_message_iter_recurse(&sub2, &sub3);
2424 if (show_properties)
2425 r = print_property(name, &sub3);
2427 r = status_property(name, &sub3, &info);
2430 log_error("Failed to parse reply.");
2435 dbus_message_iter_next(&sub);
2440 if (!show_properties)
2441 print_status_info(&info);
2443 if (!streq_ptr(info.active_state, "active") &&
2444 !streq_ptr(info.active_state, "reloading") &&
2445 streq(verb, "status"))
2446 /* According to LSB: "program not running" */
2449 while ((p = info.exec)) {
2450 LIST_REMOVE(ExecStatusInfo, exec, info.exec, p);
2451 exec_status_info_free(p);
2456 dbus_message_unref(m);
2459 dbus_message_unref(reply);
2461 dbus_error_free(&error);
2466 static int show(DBusConnection *bus, char **args, unsigned n) {
2467 DBusMessage *m = NULL, *reply = NULL;
2471 bool show_properties, new_line = false;
2476 dbus_error_init(&error);
2478 show_properties = !streq(args[0], "status");
2480 if (show_properties && n <= 1) {
2481 /* If not argument is specified inspect the manager
2484 ret = show_one(args[0], bus, "/org/freedesktop/systemd1", show_properties, &new_line);
2488 for (i = 1; i < n; i++) {
2489 const char *path = NULL;
2492 if (safe_atou32(args[i], &id) < 0) {
2494 /* Interpret as unit name */
2496 if (!(m = dbus_message_new_method_call(
2497 "org.freedesktop.systemd1",
2498 "/org/freedesktop/systemd1",
2499 "org.freedesktop.systemd1.Manager",
2501 log_error("Could not allocate message.");
2506 if (!dbus_message_append_args(m,
2507 DBUS_TYPE_STRING, &args[i],
2508 DBUS_TYPE_INVALID)) {
2509 log_error("Could not append arguments to message.");
2514 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2516 if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
2517 log_error("Failed to issue method call: %s", bus_error_message(&error));
2522 dbus_error_free(&error);
2524 dbus_message_unref(m);
2525 if (!(m = dbus_message_new_method_call(
2526 "org.freedesktop.systemd1",
2527 "/org/freedesktop/systemd1",
2528 "org.freedesktop.systemd1.Manager",
2530 log_error("Could not allocate message.");
2535 if (!dbus_message_append_args(m,
2536 DBUS_TYPE_STRING, &args[i],
2537 DBUS_TYPE_INVALID)) {
2538 log_error("Could not append arguments to message.");
2543 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2544 log_error("Failed to issue method call: %s", bus_error_message(&error));
2546 if (dbus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT))
2547 ret = 4; /* According to LSB: "program or service status is unknown" */
2554 } else if (show_properties) {
2556 /* Interpret as job id */
2558 if (!(m = dbus_message_new_method_call(
2559 "org.freedesktop.systemd1",
2560 "/org/freedesktop/systemd1",
2561 "org.freedesktop.systemd1.Manager",
2563 log_error("Could not allocate message.");
2568 if (!dbus_message_append_args(m,
2569 DBUS_TYPE_UINT32, &id,
2570 DBUS_TYPE_INVALID)) {
2571 log_error("Could not append arguments to message.");
2576 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2577 log_error("Failed to issue method call: %s", bus_error_message(&error));
2583 /* Interpret as PID */
2585 if (!(m = dbus_message_new_method_call(
2586 "org.freedesktop.systemd1",
2587 "/org/freedesktop/systemd1",
2588 "org.freedesktop.systemd1.Manager",
2590 log_error("Could not allocate message.");
2595 if (!dbus_message_append_args(m,
2596 DBUS_TYPE_UINT32, &id,
2597 DBUS_TYPE_INVALID)) {
2598 log_error("Could not append arguments to message.");
2603 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2604 log_error("Failed to issue method call: %s", bus_error_message(&error));
2610 if (!dbus_message_get_args(reply, &error,
2611 DBUS_TYPE_OBJECT_PATH, &path,
2612 DBUS_TYPE_INVALID)) {
2613 log_error("Failed to parse reply: %s", bus_error_message(&error));
2618 if ((r = show_one(args[0], bus, path, show_properties, &new_line)) != 0)
2621 dbus_message_unref(m);
2622 dbus_message_unref(reply);
2628 dbus_message_unref(m);
2631 dbus_message_unref(reply);
2633 dbus_error_free(&error);
2638 static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
2640 DBusMessage *m = NULL, *reply = NULL;
2645 dbus_error_init(&error);
2647 log_debug("Got D-Bus request: %s.%s() on %s",
2648 dbus_message_get_interface(message),
2649 dbus_message_get_member(message),
2650 dbus_message_get_path(message));
2652 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
2653 log_error("Warning! D-Bus connection terminated.");
2654 dbus_connection_close(connection);
2656 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") ||
2657 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
2658 const char *id, *path;
2660 if (!dbus_message_get_args(message, &error,
2661 DBUS_TYPE_STRING, &id,
2662 DBUS_TYPE_OBJECT_PATH, &path,
2664 log_error("Failed to parse message: %s", bus_error_message(&error));
2665 else if (streq(dbus_message_get_member(message), "UnitNew"))
2666 printf("Unit %s added.\n", id);
2668 printf("Unit %s removed.\n", id);
2670 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew") ||
2671 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
2675 if (!dbus_message_get_args(message, &error,
2676 DBUS_TYPE_UINT32, &id,
2677 DBUS_TYPE_OBJECT_PATH, &path,
2679 log_error("Failed to parse message: %s", bus_error_message(&error));
2680 else if (streq(dbus_message_get_member(message), "JobNew"))
2681 printf("Job %u added.\n", id);
2683 printf("Job %u removed.\n", id);
2686 } else if (dbus_message_is_signal(message, "org.freedesktop.DBus.Properties", "PropertiesChanged")) {
2688 const char *path, *interface, *property = "Id";
2689 DBusMessageIter iter, sub;
2691 path = dbus_message_get_path(message);
2693 if (!dbus_message_get_args(message, &error,
2694 DBUS_TYPE_STRING, &interface,
2695 DBUS_TYPE_INVALID)) {
2696 log_error("Failed to parse message: %s", bus_error_message(&error));
2700 if (!streq(interface, "org.freedesktop.systemd1.Job") &&
2701 !streq(interface, "org.freedesktop.systemd1.Unit"))
2704 if (!(m = dbus_message_new_method_call(
2705 "org.freedesktop.systemd1",
2707 "org.freedesktop.DBus.Properties",
2709 log_error("Could not allocate message.");
2713 if (!dbus_message_append_args(m,
2714 DBUS_TYPE_STRING, &interface,
2715 DBUS_TYPE_STRING, &property,
2716 DBUS_TYPE_INVALID)) {
2717 log_error("Could not append arguments to message.");
2721 if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
2722 log_error("Failed to issue method call: %s", bus_error_message(&error));
2726 if (!dbus_message_iter_init(reply, &iter) ||
2727 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2728 log_error("Failed to parse reply.");
2732 dbus_message_iter_recurse(&iter, &sub);
2734 if (streq(interface, "org.freedesktop.systemd1.Unit")) {
2737 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2738 log_error("Failed to parse reply.");
2742 dbus_message_iter_get_basic(&sub, &id);
2743 printf("Unit %s changed.\n", id);
2747 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32) {
2748 log_error("Failed to parse reply.");
2752 dbus_message_iter_get_basic(&sub, &id);
2753 printf("Job %u changed.\n", id);
2759 dbus_message_unref(m);
2762 dbus_message_unref(reply);
2764 dbus_error_free(&error);
2765 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2769 dbus_message_unref(m);
2772 dbus_message_unref(reply);
2774 dbus_error_free(&error);
2775 return DBUS_HANDLER_RESULT_NEED_MEMORY;
2778 static int monitor(DBusConnection *bus, char **args, unsigned n) {
2779 DBusMessage *m = NULL, *reply = NULL;
2783 dbus_error_init(&error);
2786 dbus_bus_add_match(bus,
2788 "sender='org.freedesktop.systemd1',"
2789 "interface='org.freedesktop.systemd1.Manager',"
2790 "path='/org/freedesktop/systemd1'",
2793 if (dbus_error_is_set(&error)) {
2794 log_error("Failed to add match: %s", bus_error_message(&error));
2799 dbus_bus_add_match(bus,
2801 "sender='org.freedesktop.systemd1',"
2802 "interface='org.freedesktop.DBus.Properties',"
2803 "member='PropertiesChanged'",
2806 if (dbus_error_is_set(&error)) {
2807 log_error("Failed to add match: %s", bus_error_message(&error));
2813 if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
2814 log_error("Failed to add filter.");
2819 if (!(m = dbus_message_new_method_call(
2820 "org.freedesktop.systemd1",
2821 "/org/freedesktop/systemd1",
2822 "org.freedesktop.systemd1.Manager",
2824 log_error("Could not allocate message.");
2829 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2830 log_error("Failed to issue method call: %s", bus_error_message(&error));
2835 while (dbus_connection_read_write_dispatch(bus, -1))
2842 /* This is slightly dirty, since we don't undo the filter or the matches. */
2845 dbus_message_unref(m);
2848 dbus_message_unref(reply);
2850 dbus_error_free(&error);
2855 static int dump(DBusConnection *bus, char **args, unsigned n) {
2856 DBusMessage *m = NULL, *reply = NULL;
2861 dbus_error_init(&error);
2863 if (!(m = dbus_message_new_method_call(
2864 "org.freedesktop.systemd1",
2865 "/org/freedesktop/systemd1",
2866 "org.freedesktop.systemd1.Manager",
2868 log_error("Could not allocate message.");
2872 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2873 log_error("Failed to issue method call: %s", bus_error_message(&error));
2878 if (!dbus_message_get_args(reply, &error,
2879 DBUS_TYPE_STRING, &text,
2880 DBUS_TYPE_INVALID)) {
2881 log_error("Failed to parse reply: %s", bus_error_message(&error));
2886 fputs(text, stdout);
2892 dbus_message_unref(m);
2895 dbus_message_unref(reply);
2897 dbus_error_free(&error);
2902 static int snapshot(DBusConnection *bus, char **args, unsigned n) {
2903 DBusMessage *m = NULL, *reply = NULL;
2906 const char *name = "", *path, *id;
2907 dbus_bool_t cleanup = FALSE;
2908 DBusMessageIter iter, sub;
2910 *interface = "org.freedesktop.systemd1.Unit",
2913 dbus_error_init(&error);
2915 if (!(m = dbus_message_new_method_call(
2916 "org.freedesktop.systemd1",
2917 "/org/freedesktop/systemd1",
2918 "org.freedesktop.systemd1.Manager",
2919 "CreateSnapshot"))) {
2920 log_error("Could not allocate message.");
2927 if (!dbus_message_append_args(m,
2928 DBUS_TYPE_STRING, &name,
2929 DBUS_TYPE_BOOLEAN, &cleanup,
2930 DBUS_TYPE_INVALID)) {
2931 log_error("Could not append arguments to message.");
2936 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2937 log_error("Failed to issue method call: %s", bus_error_message(&error));
2942 if (!dbus_message_get_args(reply, &error,
2943 DBUS_TYPE_OBJECT_PATH, &path,
2944 DBUS_TYPE_INVALID)) {
2945 log_error("Failed to parse reply: %s", bus_error_message(&error));
2950 dbus_message_unref(m);
2951 if (!(m = dbus_message_new_method_call(
2952 "org.freedesktop.systemd1",
2954 "org.freedesktop.DBus.Properties",
2956 log_error("Could not allocate message.");
2960 if (!dbus_message_append_args(m,
2961 DBUS_TYPE_STRING, &interface,
2962 DBUS_TYPE_STRING, &property,
2963 DBUS_TYPE_INVALID)) {
2964 log_error("Could not append arguments to message.");
2969 dbus_message_unref(reply);
2970 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2971 log_error("Failed to issue method call: %s", bus_error_message(&error));
2976 if (!dbus_message_iter_init(reply, &iter) ||
2977 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2978 log_error("Failed to parse reply.");
2983 dbus_message_iter_recurse(&iter, &sub);
2985 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2986 log_error("Failed to parse reply.");
2991 dbus_message_iter_get_basic(&sub, &id);
2999 dbus_message_unref(m);
3002 dbus_message_unref(reply);
3004 dbus_error_free(&error);
3009 static int delete_snapshot(DBusConnection *bus, char **args, unsigned n) {
3010 DBusMessage *m = NULL, *reply = NULL;
3018 dbus_error_init(&error);
3020 for (i = 1; i < n; i++) {
3021 const char *path = NULL;
3023 if (!(m = dbus_message_new_method_call(
3024 "org.freedesktop.systemd1",
3025 "/org/freedesktop/systemd1",
3026 "org.freedesktop.systemd1.Manager",
3028 log_error("Could not allocate message.");
3033 if (!dbus_message_append_args(m,
3034 DBUS_TYPE_STRING, &args[i],
3035 DBUS_TYPE_INVALID)) {
3036 log_error("Could not append arguments to message.");
3041 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3042 log_error("Failed to issue method call: %s", bus_error_message(&error));
3047 if (!dbus_message_get_args(reply, &error,
3048 DBUS_TYPE_OBJECT_PATH, &path,
3049 DBUS_TYPE_INVALID)) {
3050 log_error("Failed to parse reply: %s", bus_error_message(&error));
3055 dbus_message_unref(m);
3056 if (!(m = dbus_message_new_method_call(
3057 "org.freedesktop.systemd1",
3059 "org.freedesktop.systemd1.Snapshot",
3061 log_error("Could not allocate message.");
3066 dbus_message_unref(reply);
3067 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3068 log_error("Failed to issue method call: %s", bus_error_message(&error));
3073 dbus_message_unref(m);
3074 dbus_message_unref(reply);
3082 dbus_message_unref(m);
3085 dbus_message_unref(reply);
3087 dbus_error_free(&error);
3092 static int daemon_reload(DBusConnection *bus, char **args, unsigned n) {
3093 DBusMessage *m = NULL, *reply = NULL;
3098 dbus_error_init(&error);
3100 if (arg_action == ACTION_RELOAD)
3102 else if (arg_action == ACTION_REEXEC)
3103 method = "Reexecute";
3105 assert(arg_action == ACTION_SYSTEMCTL);
3108 streq(args[0], "clear-jobs") ||
3109 streq(args[0], "cancel") ? "ClearJobs" :
3110 streq(args[0], "daemon-reexec") ? "Reexecute" :
3111 streq(args[0], "reset-failed") ? "ResetFailed" :
3112 streq(args[0], "halt") ? "Halt" :
3113 streq(args[0], "poweroff") ? "PowerOff" :
3114 streq(args[0], "reboot") ? "Reboot" :
3115 streq(args[0], "kexec") ? "KExec" :
3116 streq(args[0], "exit") ? "Exit" :
3117 /* "daemon-reload" */ "Reload";
3120 if (!(m = dbus_message_new_method_call(
3121 "org.freedesktop.systemd1",
3122 "/org/freedesktop/systemd1",
3123 "org.freedesktop.systemd1.Manager",
3125 log_error("Could not allocate message.");
3129 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3131 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
3132 /* There's always a fallback possible for
3133 * legacy actions. */
3138 log_error("Failed to issue method call: %s", bus_error_message(&error));
3147 dbus_message_unref(m);
3150 dbus_message_unref(reply);
3152 dbus_error_free(&error);
3157 static int reset_failed(DBusConnection *bus, char **args, unsigned n) {
3158 DBusMessage *m = NULL, *reply = NULL;
3164 dbus_error_init(&error);
3167 return daemon_reload(bus, args, n);
3169 for (i = 1; i < n; i++) {
3171 if (!(m = dbus_message_new_method_call(
3172 "org.freedesktop.systemd1",
3173 "/org/freedesktop/systemd1",
3174 "org.freedesktop.systemd1.Manager",
3175 "ResetFailedUnit"))) {
3176 log_error("Could not allocate message.");
3181 if (!dbus_message_append_args(m,
3182 DBUS_TYPE_STRING, args + i,
3183 DBUS_TYPE_INVALID)) {
3184 log_error("Could not append arguments to message.");
3189 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3190 log_error("Failed to issue method call: %s", bus_error_message(&error));
3195 dbus_message_unref(m);
3196 dbus_message_unref(reply);
3204 dbus_message_unref(m);
3207 dbus_message_unref(reply);
3209 dbus_error_free(&error);
3214 static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
3215 DBusMessage *m = NULL, *reply = NULL;
3217 DBusMessageIter iter, sub, sub2;
3220 *interface = "org.freedesktop.systemd1.Manager",
3221 *property = "Environment";
3223 dbus_error_init(&error);
3225 if (!(m = dbus_message_new_method_call(
3226 "org.freedesktop.systemd1",
3227 "/org/freedesktop/systemd1",
3228 "org.freedesktop.DBus.Properties",
3230 log_error("Could not allocate message.");
3234 if (!dbus_message_append_args(m,
3235 DBUS_TYPE_STRING, &interface,
3236 DBUS_TYPE_STRING, &property,
3237 DBUS_TYPE_INVALID)) {
3238 log_error("Could not append arguments to message.");
3243 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3244 log_error("Failed to issue method call: %s", bus_error_message(&error));
3249 if (!dbus_message_iter_init(reply, &iter) ||
3250 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
3251 log_error("Failed to parse reply.");
3256 dbus_message_iter_recurse(&iter, &sub);
3258 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
3259 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
3260 log_error("Failed to parse reply.");
3265 dbus_message_iter_recurse(&sub, &sub2);
3267 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
3270 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
3271 log_error("Failed to parse reply.");
3276 dbus_message_iter_get_basic(&sub2, &text);
3277 printf("%s\n", text);
3279 dbus_message_iter_next(&sub2);
3286 dbus_message_unref(m);
3289 dbus_message_unref(reply);
3291 dbus_error_free(&error);
3296 static int set_environment(DBusConnection *bus, char **args, unsigned n) {
3297 DBusMessage *m = NULL, *reply = NULL;
3301 DBusMessageIter iter, sub;
3304 dbus_error_init(&error);
3306 method = streq(args[0], "set-environment")
3308 : "UnsetEnvironment";
3310 if (!(m = dbus_message_new_method_call(
3311 "org.freedesktop.systemd1",
3312 "/org/freedesktop/systemd1",
3313 "org.freedesktop.systemd1.Manager",
3316 log_error("Could not allocate message.");
3320 dbus_message_iter_init_append(m, &iter);
3322 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
3323 log_error("Could not append arguments to message.");
3328 for (i = 1; i < n; i++)
3329 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) {
3330 log_error("Could not append arguments to message.");
3335 if (!dbus_message_iter_close_container(&iter, &sub)) {
3336 log_error("Could not append arguments to message.");
3341 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3342 log_error("Failed to issue method call: %s", bus_error_message(&error));
3351 dbus_message_unref(m);
3354 dbus_message_unref(reply);
3356 dbus_error_free(&error);
3369 static Hashmap *will_install = NULL, *have_installed = NULL;
3370 static Set *remove_symlinks_to = NULL;
3371 static unsigned n_symlinks = 0;
3373 static void install_info_free(InstallInfo *i) {
3378 strv_free(i->aliases);
3379 strv_free(i->wanted_by);
3383 static void install_info_hashmap_free(Hashmap *m) {
3386 while ((i = hashmap_steal_first(m)))
3387 install_info_free(i);
3392 static int install_info_add(const char *name) {
3396 assert(will_install);
3398 if (!unit_name_is_valid_no_type(name, true)) {
3399 log_warning("Unit name %s is not a valid unit name.", name);
3403 if (hashmap_get(have_installed, name) ||
3404 hashmap_get(will_install, name))
3407 if (!(i = new0(InstallInfo, 1))) {
3412 if (!(i->name = strdup(name))) {
3417 if ((r = hashmap_put(will_install, i->name, i)) < 0)
3424 install_info_free(i);
3429 static int config_parse_also(
3430 const char *filename,
3432 const char *section,
3446 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
3450 if (!(n = strndup(w, l)))
3453 if ((r = install_info_add(n)) < 0) {
3454 log_warning("Cannot install unit %s: %s", n, strerror(-r));
3465 static int mark_symlink_for_removal(const char *p) {
3470 assert(path_is_absolute(p));
3472 if (!remove_symlinks_to)
3475 if (!(n = strdup(p)))
3478 path_kill_slashes(n);
3480 if ((r = set_put(remove_symlinks_to, n)) < 0) {
3482 return r == -EEXIST ? 0 : r;
3488 static int remove_marked_symlinks_fd(int fd, const char *config_path, const char *root, bool *deleted) {
3497 if (!(d = fdopendir(fd))) {
3498 close_nointr_nofail(fd);
3504 while ((de = readdir(d))) {
3505 bool is_dir = false, is_link = false;
3507 if (ignore_file(de->d_name))
3510 if (de->d_type == DT_LNK)
3512 else if (de->d_type == DT_DIR)
3514 else if (de->d_type == DT_UNKNOWN) {
3517 if (fstatat(fd, de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) {
3518 log_error("Failed to stat %s/%s: %m", root, de->d_name);
3525 is_link = S_ISLNK(st.st_mode);
3526 is_dir = S_ISDIR(st.st_mode);
3534 if ((nfd = openat(fd, de->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW)) < 0) {
3535 log_error("Failed to open %s/%s: %m", root, de->d_name);
3542 if (asprintf(&p, "%s/%s", root, de->d_name) < 0) {
3543 log_error("Failed to allocate directory string.");
3544 close_nointr_nofail(nfd);
3549 /* This will close nfd, regardless whether it succeeds or not */
3550 q = remove_marked_symlinks_fd(nfd, config_path, p, deleted);
3556 } else if (is_link) {
3560 if (asprintf(&p, "%s/%s", root, de->d_name) < 0) {
3561 log_error("Failed to allocate symlink string.");
3566 if ((q = readlink_and_make_absolute(p, &dest)) < 0) {
3567 log_error("Cannot read symlink %s: %s", p, strerror(-q));
3575 if ((c = canonicalize_file_name(dest))) {
3576 /* This might fail if the destination
3577 * is already removed */
3583 path_kill_slashes(dest);
3584 if (set_get(remove_symlinks_to, dest)) {
3587 log_info("rm '%s'", p);
3589 if (unlink(p) < 0) {
3590 log_error("Cannot unlink symlink %s: %m", p);
3595 rmdir_parents(p, config_path);
3596 path_kill_slashes(p);
3598 if (!set_get(remove_symlinks_to, p)) {
3600 if ((r = mark_symlink_for_removal(p)) < 0) {
3619 static int remove_marked_symlinks(const char *config_path) {
3623 assert(config_path);
3625 if (set_size(remove_symlinks_to) <= 0)
3628 if ((fd = open(config_path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW)) < 0)
3635 if ((cfd = dup(fd)) < 0) {
3640 /* This takes possession of cfd and closes it */
3641 if ((q = remove_marked_symlinks_fd(cfd, config_path, config_path, &deleted)) < 0) {
3647 close_nointr_nofail(fd);
3652 static int create_symlink(const char *verb, const char *old_path, const char *new_path) {
3659 if (streq(verb, "enable")) {
3662 mkdir_parents(new_path, 0755);
3664 if (symlink(old_path, new_path) >= 0) {
3667 log_info("ln -s '%s' '%s'", old_path, new_path);
3672 if (errno != EEXIST) {
3673 log_error("Cannot link %s to %s: %m", old_path, new_path);
3677 if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
3679 if (errno == EINVAL) {
3680 log_error("Cannot link %s to %s, file exists already and is not a symlink.", old_path, new_path);
3684 log_error("readlink() failed: %s", strerror(-r));
3688 if (streq(dest, old_path)) {
3694 log_error("Cannot link %s to %s, symlink exists already and points to %s.", old_path, new_path, dest);
3703 log_info("ln -s '%s' '%s'", old_path, new_path);
3705 if (symlink(old_path, new_path) >= 0)
3708 log_error("Cannot link %s to %s: %m", old_path, new_path);
3711 } else if (streq(verb, "disable")) {
3714 if ((r = mark_symlink_for_removal(old_path)) < 0)
3717 if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
3718 if (errno == ENOENT)
3721 if (errno == EINVAL) {
3722 log_warning("File %s not a symlink, ignoring.", old_path);
3726 log_error("readlink() failed: %s", strerror(-r));
3730 if (!streq(dest, old_path)) {
3731 log_warning("File %s not a symlink to %s but points to %s, ignoring.", new_path, old_path, dest);
3738 if ((r = mark_symlink_for_removal(new_path)) < 0)
3742 log_info("rm '%s'", new_path);
3744 if (unlink(new_path) >= 0)
3747 log_error("Cannot unlink %s: %m", new_path);
3750 } else if (streq(verb, "is-enabled")) {
3753 if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
3755 if (errno == ENOENT || errno == EINVAL)
3758 log_error("readlink() failed: %s", strerror(-r));
3762 if (streq(dest, old_path)) {
3770 assert_not_reached("Unknown action.");
3773 static int install_info_symlink_alias(const char *verb, InstallInfo *i, const char *config_path) {
3775 char *alias_path = NULL;
3780 assert(config_path);
3782 STRV_FOREACH(s, i->aliases) {
3785 if (!(alias_path = path_make_absolute(*s, config_path))) {
3786 log_error("Out of memory");
3791 if ((r = create_symlink(verb, i->path, alias_path)) != 0)
3794 if (streq(verb, "disable"))
3795 rmdir_parents(alias_path, config_path);
3805 static int install_info_symlink_wants(const char *verb, InstallInfo *i, const char *config_path) {
3807 char *alias_path = NULL;
3812 assert(config_path);
3814 STRV_FOREACH(s, i->wanted_by) {
3815 if (!unit_name_is_valid_no_type(*s, true)) {
3816 log_error("Invalid name %s.", *s);
3824 if (asprintf(&alias_path, "%s/%s.wants/%s", config_path, *s, i->name) < 0) {
3825 log_error("Out of memory");
3830 if ((r = create_symlink(verb, i->path, alias_path)) != 0)
3833 if (streq(verb, "disable"))
3834 rmdir_parents(alias_path, config_path);
3845 static int install_info_apply(const char *verb, LookupPaths *paths, InstallInfo *i, const char *config_path) {
3847 const ConfigItem items[] = {
3848 { "Alias", config_parse_strv, &i->aliases, "Install" },
3849 { "WantedBy", config_parse_strv, &i->wanted_by, "Install" },
3850 { "Also", config_parse_also, NULL, "Install" },
3852 { NULL, NULL, NULL, NULL }
3856 char *filename = NULL;
3863 STRV_FOREACH(p, paths->unit_path) {
3866 if (!(filename = path_make_absolute(i->name, *p))) {
3867 log_error("Out of memory");
3871 /* Ensure that we don't follow symlinks */
3872 if ((fd = open(filename, O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NOCTTY)) >= 0)
3873 if ((f = fdopen(fd, "re")))
3876 if (errno == ELOOP) {
3877 log_error("Refusing to operate on symlinks, please pass unit names or absolute paths to unit files.");
3882 if (errno != ENOENT) {
3883 log_error("Failed to open %s: %m", filename);
3893 #if defined(TARGET_FEDORA) && defined (HAVE_SYSV_COMPAT)
3895 if (endswith(i->name, ".service")) {
3899 if (asprintf(&sysv, SYSTEM_SYSVINIT_PATH "/%s", i->name) < 0) {
3900 log_error("Out of memory");
3904 sysv[strlen(sysv) - sizeof(".service") + 1] = 0;
3905 exists = access(sysv, F_OK) >= 0;
3911 const char *argv[] = {
3918 log_info("%s is not a native service, redirecting to /sbin/chkconfig.", i->name);
3920 argv[1] = file_name_from_path(sysv);
3922 streq(verb, "enable") ? "on" :
3923 streq(verb, "disable") ? "off" : NULL;
3925 log_info("Executing %s %s %s", argv[0], argv[1], strempty(argv[2]));
3927 if ((pid = fork()) < 0) {
3928 log_error("Failed to fork: %m");
3931 } else if (pid == 0) {
3932 execv(argv[0], (char**) argv);
3933 _exit(EXIT_FAILURE);
3938 if ((r = wait_for_terminate(pid, &status)) < 0)
3941 if (status.si_code == CLD_EXITED) {
3942 if (status.si_status == 0 && (streq(verb, "enable") || streq(verb, "disable")))
3945 return status.si_status == 0 ? 0 : -EINVAL;
3955 log_error("Couldn't find %s.", i->name);
3961 if ((r = config_parse(filename, f, NULL, items, true, i)) < 0) {
3966 n_symlinks += strv_length(i->aliases);
3967 n_symlinks += strv_length(i->wanted_by);
3971 if ((r = install_info_symlink_alias(verb, i, config_path)) != 0)
3974 if ((r = install_info_symlink_wants(verb, i, config_path)) != 0)
3977 if ((r = mark_symlink_for_removal(filename)) < 0)
3980 if ((r = remove_marked_symlinks(config_path)) < 0)
3986 static char *get_config_path(void) {
3988 if (arg_user && arg_global)
3989 return strdup(USER_CONFIG_UNIT_PATH);
3994 if (user_config_home(&p) < 0)
4000 return strdup(SYSTEM_CONFIG_UNIT_PATH);
4003 static int enable_unit(DBusConnection *bus, char **args, unsigned n) {
4007 char *config_path = NULL;
4010 const char *verb = args[0];
4012 dbus_error_init(&error);
4015 if ((r = lookup_paths_init(&paths, arg_user ? MANAGER_USER : MANAGER_SYSTEM)) < 0) {
4016 log_error("Failed to determine lookup paths: %s", strerror(-r));
4020 if (!(config_path = get_config_path())) {
4021 log_error("Failed to determine config path");
4026 will_install = hashmap_new(string_hash_func, string_compare_func);
4027 have_installed = hashmap_new(string_hash_func, string_compare_func);
4029 if (!will_install || !have_installed) {
4030 log_error("Failed to allocate unit sets.");
4035 if (!arg_defaults && streq(verb, "disable"))
4036 if (!(remove_symlinks_to = set_new(string_hash_func, string_compare_func))) {
4037 log_error("Failed to allocate symlink sets.");
4042 for (j = 1; j < n; j++)
4043 if ((r = install_info_add(args[j])) < 0) {
4044 log_warning("Cannot install unit %s: %s", args[j], strerror(-r));
4048 while ((i = hashmap_first(will_install))) {
4051 assert_se(hashmap_move_one(have_installed, will_install, i->name) == 0);
4053 if ((q = install_info_apply(verb, &paths, i, config_path)) != 0) {
4061 /* In test mode and found something */
4067 if (streq(verb, "is-enabled"))
4068 r = r > 0 ? 0 : -ENOENT;
4070 if (n_symlinks <= 0)
4071 log_warning("Unit files contain no applicable installation information. Ignoring.");
4074 /* Don't try to reload anything if the user asked us to not do this */
4076 /* Don't try to reload anything when updating a unit globally */
4078 /* Don't try to reload anything if we are called for system changes but the system wasn't booted with systemd */
4079 (arg_user || sd_booted() > 0) &&
4080 /* Don't try to reload anything if we are running in a chroot environment */
4081 (arg_user || running_in_chroot() <= 0) ) {
4084 if ((q = daemon_reload(bus, args, n)) < 0)
4090 install_info_hashmap_free(will_install);
4091 install_info_hashmap_free(have_installed);
4093 set_free_free(remove_symlinks_to);
4095 lookup_paths_free(&paths);
4102 static int systemctl_help(void) {
4104 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
4105 "Send control commands to or query the systemd manager.\n\n"
4106 " -h --help Show this help\n"
4107 " --version Show package version\n"
4108 " -t --type=TYPE List only units of a particular type\n"
4109 " -p --property=NAME Show only properties by this name\n"
4110 " -a --all Show all units/properties, including dead/empty ones\n"
4111 " --full Don't ellipsize unit names on output\n"
4112 " --fail When queueing a new job, fail if conflicting jobs are\n"
4114 " -q --quiet Suppress output\n"
4115 " --no-block Do not wait until operation finished\n"
4116 " --no-pager Do not pipe output into a pager.\n"
4117 " --system Connect to system manager\n"
4118 " --user Connect to user service manager\n"
4119 " --order When generating graph for dot, show only order\n"
4120 " --require When generating graph for dot, show only requirement\n"
4121 " --no-wall Don't send wall message before halt/power-off/reboot\n"
4122 " --global Enable/disable unit files globally\n"
4123 " --no-reload When enabling/disabling unit files, don't reload daemon\n"
4125 " --no-ask-password\n"
4126 " Do not ask for system passwords\n"
4127 " --kill-mode=MODE How to send signal\n"
4128 " --kill-who=WHO Who to send signal to\n"
4129 " -s --signal=SIGNAL Which signal to send\n"
4130 " -f --force When enabling unit files, override existing symlinks\n"
4131 " When shutting down, execute action immediately\n"
4132 " --defaults When disabling unit files, remove default symlinks only\n\n"
4134 " list-units List units\n"
4135 " start [NAME...] Start (activate) one or more units\n"
4136 " stop [NAME...] Stop (deactivate) one or more units\n"
4137 " reload [NAME...] Reload one or more units\n"
4138 " restart [NAME...] Start or restart one or more units\n"
4139 " try-restart [NAME...] Restart one or more units if active\n"
4140 " reload-or-restart [NAME...] Reload one or more units is possible,\n"
4141 " otherwise start or restart\n"
4142 " reload-or-try-restart [NAME...] Reload one or more units is possible,\n"
4143 " otherwise restart if active\n"
4144 " isolate [NAME] Start one unit and stop all others\n"
4145 " kill [NAME...] Send signal to processes of a unit\n"
4146 " is-active [NAME...] Check whether units are active\n"
4147 " status [NAME...|PID...] Show runtime status of one or more units\n"
4148 " show [NAME...|JOB...] Show properties of one or more\n"
4149 " units/jobs or the manager\n"
4150 " reset-failed [NAME...] Reset failed state for all, one, or more\n"
4152 " enable [NAME...] Enable one or more unit files\n"
4153 " disable [NAME...] Disable one or more unit files\n"
4154 " is-enabled [NAME...] Check whether unit files are enabled\n"
4155 " load [NAME...] Load one or more units\n"
4156 " list-jobs List jobs\n"
4157 " cancel [JOB...] Cancel all, one, or more jobs\n"
4158 " monitor Monitor unit/job changes\n"
4159 " dump Dump server status\n"
4160 " dot Dump dependency graph for dot(1)\n"
4161 " snapshot [NAME] Create a snapshot\n"
4162 " delete [NAME...] Remove one or more snapshots\n"
4163 " daemon-reload Reload systemd manager configuration\n"
4164 " daemon-reexec Reexecute systemd manager\n"
4165 " show-environment Dump environment\n"
4166 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
4167 " unset-environment [NAME...] Unset one or more environment variables\n"
4168 " default Enter system default mode\n"
4169 " rescue Enter system rescue mode\n"
4170 " emergency Enter system emergency mode\n"
4171 " halt Shut down and halt the system\n"
4172 " poweroff Shut down and power-off the system\n"
4173 " reboot Shut down and reboot the system\n"
4174 " kexec Shut down and reboot the system with kexec\n"
4175 " exit Ask for user instance termination\n",
4176 program_invocation_short_name);
4181 static int halt_help(void) {
4183 printf("%s [OPTIONS...]\n\n"
4184 "%s the system.\n\n"
4185 " --help Show this help\n"
4186 " --halt Halt the machine\n"
4187 " -p --poweroff Switch off the machine\n"
4188 " --reboot Reboot the machine\n"
4189 " -f --force Force immediate halt/power-off/reboot\n"
4190 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
4191 " -d --no-wtmp Don't write wtmp record\n"
4192 " -n --no-sync Don't sync before halt/power-off/reboot\n"
4193 " --no-wall Don't send wall message before halt/power-off/reboot\n",
4194 program_invocation_short_name,
4195 arg_action == ACTION_REBOOT ? "Reboot" :
4196 arg_action == ACTION_POWEROFF ? "Power off" :
4202 static int shutdown_help(void) {
4204 printf("%s [OPTIONS...] [TIME] [WALL...]\n\n"
4205 "Shut down the system.\n\n"
4206 " --help Show this help\n"
4207 " -H --halt Halt the machine\n"
4208 " -P --poweroff Power-off the machine\n"
4209 " -r --reboot Reboot the machine\n"
4210 " -h Equivalent to --poweroff, overriden by --halt\n"
4211 " -k Don't halt/power-off/reboot, just send warnings\n"
4212 " --no-wall Don't send wall message before halt/power-off/reboot\n"
4213 " -c Cancel a pending shutdown\n",
4214 program_invocation_short_name);
4219 static int telinit_help(void) {
4221 printf("%s [OPTIONS...] {COMMAND}\n\n"
4222 "Send control commands to the init daemon.\n\n"
4223 " --help Show this help\n"
4224 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
4226 " 0 Power-off the machine\n"
4227 " 6 Reboot the machine\n"
4228 " 2, 3, 4, 5 Start runlevelX.target unit\n"
4229 " 1, s, S Enter rescue mode\n"
4230 " q, Q Reload init daemon configuration\n"
4231 " u, U Reexecute init daemon\n",
4232 program_invocation_short_name);
4237 static int runlevel_help(void) {
4239 printf("%s [OPTIONS...]\n\n"
4240 "Prints the previous and current runlevel of the init system.\n\n"
4241 " --help Show this help\n",
4242 program_invocation_short_name);
4247 static int systemctl_parse_argv(int argc, char *argv[]) {
4268 static const struct option options[] = {
4269 { "help", no_argument, NULL, 'h' },
4270 { "version", no_argument, NULL, ARG_VERSION },
4271 { "type", required_argument, NULL, 't' },
4272 { "property", required_argument, NULL, 'p' },
4273 { "all", no_argument, NULL, 'a' },
4274 { "full", no_argument, NULL, ARG_FULL },
4275 { "fail", no_argument, NULL, ARG_FAIL },
4276 { "user", no_argument, NULL, ARG_USER },
4277 { "system", no_argument, NULL, ARG_SYSTEM },
4278 { "global", no_argument, NULL, ARG_GLOBAL },
4279 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
4280 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
4281 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4282 { "quiet", no_argument, NULL, 'q' },
4283 { "order", no_argument, NULL, ARG_ORDER },
4284 { "require", no_argument, NULL, ARG_REQUIRE },
4285 { "force", no_argument, NULL, 'f' },
4286 { "no-reload", no_argument, NULL, ARG_NO_RELOAD },
4287 { "defaults", no_argument, NULL, ARG_DEFAULTS },
4288 { "kill-mode", required_argument, NULL, ARG_KILL_MODE },
4289 { "kill-who", required_argument, NULL, ARG_KILL_WHO },
4290 { "signal", required_argument, NULL, 's' },
4291 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
4292 { NULL, 0, NULL, 0 }
4300 /* Only when running as systemctl we ask for passwords */
4301 arg_ask_password = true;
4303 while ((c = getopt_long(argc, argv, "ht:p:aqfs:", options, NULL)) >= 0) {
4312 puts(PACKAGE_STRING);
4314 puts(SYSTEMD_FEATURES);
4324 if (!(l = strv_append(arg_property, optarg)))
4327 strv_free(arg_property);
4330 /* If the user asked for a particular
4331 * property, show it to him, even if it is
4354 arg_no_block = true;
4358 arg_no_pager = true;
4366 arg_dot = DOT_ORDER;
4370 arg_dot = DOT_REQUIRE;
4386 arg_no_reload = true;
4395 arg_defaults = true;
4399 arg_kill_who = optarg;
4403 arg_kill_mode = optarg;
4407 if ((arg_signal = signal_from_string_try_harder(optarg)) < 0) {
4408 log_error("Failed to parse signal string %s.", optarg);
4413 case ARG_NO_ASK_PASSWORD:
4414 arg_ask_password = false;
4421 log_error("Unknown option code %c", c);
4429 static int halt_parse_argv(int argc, char *argv[]) {
4438 static const struct option options[] = {
4439 { "help", no_argument, NULL, ARG_HELP },
4440 { "halt", no_argument, NULL, ARG_HALT },
4441 { "poweroff", no_argument, NULL, 'p' },
4442 { "reboot", no_argument, NULL, ARG_REBOOT },
4443 { "force", no_argument, NULL, 'f' },
4444 { "wtmp-only", no_argument, NULL, 'w' },
4445 { "no-wtmp", no_argument, NULL, 'd' },
4446 { "no-sync", no_argument, NULL, 'n' },
4447 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4448 { NULL, 0, NULL, 0 }
4456 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
4457 if (runlevel == '0' || runlevel == '6')
4458 arg_immediate = true;
4460 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
4468 arg_action = ACTION_HALT;
4472 if (arg_action != ACTION_REBOOT)
4473 arg_action = ACTION_POWEROFF;
4477 arg_action = ACTION_REBOOT;
4481 arg_immediate = true;
4502 /* Compatibility nops */
4509 log_error("Unknown option code %c", c);
4514 if (optind < argc) {
4515 log_error("Too many arguments.");
4522 static int parse_time_spec(const char *t, usec_t *_u) {
4526 if (streq(t, "now"))
4528 else if (t[0] == '+') {
4531 if (safe_atou64(t + 1, &u) < 0)
4534 *_u = now(CLOCK_REALTIME) + USEC_PER_MINUTE * u;
4543 hour = strtol(t, &e, 10);
4544 if (errno != 0 || *e != ':' || hour < 0 || hour > 23)
4547 minute = strtol(e+1, &e, 10);
4548 if (errno != 0 || *e != 0 || minute < 0 || minute > 59)
4551 n = now(CLOCK_REALTIME);
4552 s = (time_t) (n / USEC_PER_SEC);
4555 assert_se(localtime_r(&s, &tm));
4557 tm.tm_hour = (int) hour;
4558 tm.tm_min = (int) minute;
4561 assert_se(s = mktime(&tm));
4563 *_u = (usec_t) s * USEC_PER_SEC;
4566 *_u += USEC_PER_DAY;
4572 static int shutdown_parse_argv(int argc, char *argv[]) {
4579 static const struct option options[] = {
4580 { "help", no_argument, NULL, ARG_HELP },
4581 { "halt", no_argument, NULL, 'H' },
4582 { "poweroff", no_argument, NULL, 'P' },
4583 { "reboot", no_argument, NULL, 'r' },
4584 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4585 { NULL, 0, NULL, 0 }
4593 while ((c = getopt_long(argc, argv, "HPrhkt:afFc", options, NULL)) >= 0) {
4601 arg_action = ACTION_HALT;
4605 arg_action = ACTION_POWEROFF;
4609 arg_action = ACTION_REBOOT;
4613 if (arg_action != ACTION_HALT)
4614 arg_action = ACTION_POWEROFF;
4627 /* Compatibility nops */
4631 arg_action = ACTION_CANCEL_SHUTDOWN;
4638 log_error("Unknown option code %c", c);
4643 if (argc > optind) {
4644 if ((r = parse_time_spec(argv[optind], &arg_when)) < 0) {
4645 log_error("Failed to parse time specification: %s", argv[optind]);
4649 arg_when = now(CLOCK_REALTIME) + USEC_PER_MINUTE;
4651 /* We skip the time argument */
4652 if (argc > optind + 1)
4653 arg_wall = argv + optind + 1;
4660 static int telinit_parse_argv(int argc, char *argv[]) {
4667 static const struct option options[] = {
4668 { "help", no_argument, NULL, ARG_HELP },
4669 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4670 { NULL, 0, NULL, 0 }
4673 static const struct {
4677 { '0', ACTION_POWEROFF },
4678 { '6', ACTION_REBOOT },
4679 { '1', ACTION_RESCUE },
4680 { '2', ACTION_RUNLEVEL2 },
4681 { '3', ACTION_RUNLEVEL3 },
4682 { '4', ACTION_RUNLEVEL4 },
4683 { '5', ACTION_RUNLEVEL5 },
4684 { 's', ACTION_RESCUE },
4685 { 'S', ACTION_RESCUE },
4686 { 'q', ACTION_RELOAD },
4687 { 'Q', ACTION_RELOAD },
4688 { 'u', ACTION_REEXEC },
4689 { 'U', ACTION_REEXEC }
4698 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4713 log_error("Unknown option code %c", c);
4718 if (optind >= argc) {
4723 if (optind + 1 < argc) {
4724 log_error("Too many arguments.");
4728 if (strlen(argv[optind]) != 1) {
4729 log_error("Expected single character argument.");
4733 for (i = 0; i < ELEMENTSOF(table); i++)
4734 if (table[i].from == argv[optind][0])
4737 if (i >= ELEMENTSOF(table)) {
4738 log_error("Unknown command %s.", argv[optind]);
4742 arg_action = table[i].to;
4749 static int runlevel_parse_argv(int argc, char *argv[]) {
4755 static const struct option options[] = {
4756 { "help", no_argument, NULL, ARG_HELP },
4757 { NULL, 0, NULL, 0 }
4765 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4776 log_error("Unknown option code %c", c);
4781 if (optind < argc) {
4782 log_error("Too many arguments.");
4789 static int parse_argv(int argc, char *argv[]) {
4793 if (program_invocation_short_name) {
4795 if (strstr(program_invocation_short_name, "halt")) {
4796 arg_action = ACTION_HALT;
4797 return halt_parse_argv(argc, argv);
4798 } else if (strstr(program_invocation_short_name, "poweroff")) {
4799 arg_action = ACTION_POWEROFF;
4800 return halt_parse_argv(argc, argv);
4801 } else if (strstr(program_invocation_short_name, "reboot")) {
4802 arg_action = ACTION_REBOOT;
4803 return halt_parse_argv(argc, argv);
4804 } else if (strstr(program_invocation_short_name, "shutdown")) {
4805 arg_action = ACTION_POWEROFF;
4806 return shutdown_parse_argv(argc, argv);
4807 } else if (strstr(program_invocation_short_name, "init")) {
4809 if (sd_booted() > 0) {
4810 arg_action = ACTION_INVALID;
4811 return telinit_parse_argv(argc, argv);
4813 /* Hmm, so some other init system is
4814 * running, we need to forward this
4815 * request to it. For now we simply
4816 * guess that it is Upstart. */
4818 execv("/lib/upstart/telinit", argv);
4820 log_error("Couldn't find an alternative telinit implementation to spawn.");
4824 } else if (strstr(program_invocation_short_name, "runlevel")) {
4825 arg_action = ACTION_RUNLEVEL;
4826 return runlevel_parse_argv(argc, argv);
4830 arg_action = ACTION_SYSTEMCTL;
4831 return systemctl_parse_argv(argc, argv);
4834 static int action_to_runlevel(void) {
4836 static const char table[_ACTION_MAX] = {
4837 [ACTION_HALT] = '0',
4838 [ACTION_POWEROFF] = '0',
4839 [ACTION_REBOOT] = '6',
4840 [ACTION_RUNLEVEL2] = '2',
4841 [ACTION_RUNLEVEL3] = '3',
4842 [ACTION_RUNLEVEL4] = '4',
4843 [ACTION_RUNLEVEL5] = '5',
4844 [ACTION_RESCUE] = '1'
4847 assert(arg_action < _ACTION_MAX);
4849 return table[arg_action];
4852 static int talk_upstart(void) {
4853 DBusMessage *m = NULL, *reply = NULL;
4855 int previous, rl, r;
4857 env1_buf[] = "RUNLEVEL=X",
4858 env2_buf[] = "PREVLEVEL=X";
4859 char *env1 = env1_buf, *env2 = env2_buf;
4860 const char *emit = "runlevel";
4861 dbus_bool_t b_false = FALSE;
4862 DBusMessageIter iter, sub;
4863 DBusConnection *bus;
4865 dbus_error_init(&error);
4867 if (!(rl = action_to_runlevel()))
4870 if (utmp_get_runlevel(&previous, NULL) < 0)
4873 if (!(bus = dbus_connection_open_private("unix:abstract=/com/ubuntu/upstart", &error))) {
4874 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
4879 log_error("Failed to connect to Upstart bus: %s", bus_error_message(&error));
4884 if ((r = bus_check_peercred(bus)) < 0) {
4885 log_error("Failed to verify owner of bus.");
4889 if (!(m = dbus_message_new_method_call(
4890 "com.ubuntu.Upstart",
4891 "/com/ubuntu/Upstart",
4892 "com.ubuntu.Upstart0_6",
4895 log_error("Could not allocate message.");
4900 dbus_message_iter_init_append(m, &iter);
4902 env1_buf[sizeof(env1_buf)-2] = rl;
4903 env2_buf[sizeof(env2_buf)-2] = previous;
4905 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
4906 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
4907 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
4908 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
4909 !dbus_message_iter_close_container(&iter, &sub) ||
4910 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
4911 log_error("Could not append arguments to message.");
4916 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
4918 if (error_is_no_service(&error)) {
4923 log_error("Failed to issue method call: %s", bus_error_message(&error));
4932 dbus_message_unref(m);
4935 dbus_message_unref(reply);
4938 dbus_connection_flush(bus);
4939 dbus_connection_close(bus);
4940 dbus_connection_unref(bus);
4943 dbus_error_free(&error);
4948 static int talk_initctl(void) {
4949 struct init_request request;
4953 if (!(rl = action_to_runlevel()))
4957 request.magic = INIT_MAGIC;
4958 request.sleeptime = 0;
4959 request.cmd = INIT_CMD_RUNLVL;
4960 request.runlevel = rl;
4962 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
4964 if (errno == ENOENT)
4967 log_error("Failed to open "INIT_FIFO": %m");
4972 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
4973 close_nointr_nofail(fd);
4976 log_error("Failed to write to "INIT_FIFO": %m");
4977 return errno ? -errno : -EIO;
4983 static int systemctl_main(DBusConnection *bus, int argc, char *argv[], DBusError *error) {
4985 static const struct {
4993 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
4995 { "list-units", LESS, 1, list_units },
4996 { "list-jobs", EQUAL, 1, list_jobs },
4997 { "clear-jobs", EQUAL, 1, daemon_reload },
4998 { "load", MORE, 2, load_unit },
4999 { "cancel", MORE, 2, cancel_job },
5000 { "start", MORE, 2, start_unit },
5001 { "stop", MORE, 2, start_unit },
5002 { "reload", MORE, 2, start_unit },
5003 { "restart", MORE, 2, start_unit },
5004 { "try-restart", MORE, 2, start_unit },
5005 { "reload-or-restart", MORE, 2, start_unit },
5006 { "reload-or-try-restart", MORE, 2, start_unit },
5007 { "force-reload", MORE, 2, start_unit }, /* For compatibility with SysV */
5008 { "condrestart", MORE, 2, start_unit }, /* For compatibility with RH */
5009 { "isolate", EQUAL, 2, start_unit },
5010 { "kill", MORE, 2, kill_unit },
5011 { "is-active", MORE, 2, check_unit },
5012 { "check", MORE, 2, check_unit },
5013 { "show", MORE, 1, show },
5014 { "status", MORE, 2, show },
5015 { "monitor", EQUAL, 1, monitor },
5016 { "dump", EQUAL, 1, dump },
5017 { "dot", EQUAL, 1, dot },
5018 { "snapshot", LESS, 2, snapshot },
5019 { "delete", MORE, 2, delete_snapshot },
5020 { "daemon-reload", EQUAL, 1, daemon_reload },
5021 { "daemon-reexec", EQUAL, 1, daemon_reload },
5022 { "show-environment", EQUAL, 1, show_enviroment },
5023 { "set-environment", MORE, 2, set_environment },
5024 { "unset-environment", MORE, 2, set_environment },
5025 { "halt", EQUAL, 1, start_special },
5026 { "poweroff", EQUAL, 1, start_special },
5027 { "reboot", EQUAL, 1, start_special },
5028 { "kexec", EQUAL, 1, start_special },
5029 { "default", EQUAL, 1, start_special },
5030 { "rescue", EQUAL, 1, start_special },
5031 { "emergency", EQUAL, 1, start_special },
5032 { "exit", EQUAL, 1, start_special },
5033 { "reset-failed", MORE, 1, reset_failed },
5034 { "enable", MORE, 2, enable_unit },
5035 { "disable", MORE, 2, enable_unit },
5036 { "is-enabled", MORE, 2, enable_unit }
5046 left = argc - optind;
5049 /* Special rule: no arguments means "list-units" */
5052 if (streq(argv[optind], "help")) {
5057 for (i = 0; i < ELEMENTSOF(verbs); i++)
5058 if (streq(argv[optind], verbs[i].verb))
5061 if (i >= ELEMENTSOF(verbs)) {
5062 log_error("Unknown operation %s", argv[optind]);
5067 switch (verbs[i].argc_cmp) {
5070 if (left != verbs[i].argc) {
5071 log_error("Invalid number of arguments.");
5078 if (left < verbs[i].argc) {
5079 log_error("Too few arguments.");
5086 if (left > verbs[i].argc) {
5087 log_error("Too many arguments.");
5094 assert_not_reached("Unknown comparison operator.");
5097 /* Require a bus connection for all operations but
5099 if (!streq(verbs[i].verb, "enable") &&
5100 !streq(verbs[i].verb, "disable") &&
5102 log_error("Failed to get D-Bus connection: %s", error->message);
5106 return verbs[i].dispatch(bus, argv + optind, left);
5109 static int send_shutdownd(usec_t t, char mode, bool warn, const char *message) {
5111 struct msghdr msghdr;
5113 union sockaddr_union sockaddr;
5114 struct shutdownd_command c;
5122 strncpy(c.wall_message, message, sizeof(c.wall_message));
5124 if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0)
5128 sockaddr.sa.sa_family = AF_UNIX;
5129 sockaddr.un.sun_path[0] = 0;
5130 strncpy(sockaddr.un.sun_path+1, "/org/freedesktop/systemd1/shutdownd", sizeof(sockaddr.un.sun_path)-1);
5133 iovec.iov_base = (char*) &c;
5134 iovec.iov_len = sizeof(c);
5137 msghdr.msg_name = &sockaddr;
5138 msghdr.msg_namelen = offsetof(struct sockaddr_un, sun_path) + 1 + sizeof("/org/freedesktop/systemd1/shutdownd") - 1;
5140 msghdr.msg_iov = &iovec;
5141 msghdr.msg_iovlen = 1;
5143 if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) < 0) {
5144 close_nointr_nofail(fd);
5148 close_nointr_nofail(fd);
5152 static int reload_with_fallback(DBusConnection *bus) {
5155 /* First, try systemd via D-Bus. */
5156 if (daemon_reload(bus, NULL, 0) > 0)
5160 /* Nothing else worked, so let's try signals */
5161 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
5163 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
5164 log_error("kill() failed: %m");
5171 static int start_with_fallback(DBusConnection *bus) {
5174 /* First, try systemd via D-Bus. */
5175 if (start_unit(bus, NULL, 0) >= 0)
5179 /* Hmm, talking to systemd via D-Bus didn't work. Then
5180 * let's try to talk to Upstart via D-Bus. */
5181 if (talk_upstart() > 0)
5184 /* Nothing else worked, so let's try
5186 if (talk_initctl() > 0)
5189 log_error("Failed to talk to init daemon.");
5193 warn_wall(arg_action);
5197 static int halt_main(DBusConnection *bus) {
5200 if (geteuid() != 0) {
5201 log_error("Must be root.");
5207 char date[FORMAT_TIMESTAMP_MAX];
5209 m = strv_join(arg_wall, " ");
5210 r = send_shutdownd(arg_when,
5211 arg_action == ACTION_HALT ? 'H' :
5212 arg_action == ACTION_POWEROFF ? 'P' :
5219 log_warning("Failed to talk to shutdownd, proceeding with immediate shutdown: %s", strerror(-r));
5221 log_info("Shutdown scheduled for %s, use 'shutdown -c' to cancel.",
5222 format_timestamp(date, sizeof(date), arg_when));
5227 if (!arg_dry && !arg_immediate)
5228 return start_with_fallback(bus);
5231 if (sd_booted() > 0)
5232 log_debug("Not writing utmp record, assuming that systemd-update-utmp is used.");
5233 else if ((r = utmp_put_shutdown(0)) < 0)
5234 log_warning("Failed to write utmp record: %s", strerror(-r));
5243 /* Make sure C-A-D is handled by the kernel from this
5245 reboot(RB_ENABLE_CAD);
5247 switch (arg_action) {
5250 log_info("Halting.");
5251 reboot(RB_HALT_SYSTEM);
5254 case ACTION_POWEROFF:
5255 log_info("Powering off.");
5256 reboot(RB_POWER_OFF);
5260 log_info("Rebooting.");
5261 reboot(RB_AUTOBOOT);
5265 assert_not_reached("Unknown halt action.");
5268 /* We should never reach this. */
5272 static int runlevel_main(void) {
5273 int r, runlevel, previous;
5275 if ((r = utmp_get_runlevel(&runlevel, &previous)) < 0) {
5276 printf("unknown\n");
5281 previous <= 0 ? 'N' : previous,
5282 runlevel <= 0 ? 'N' : runlevel);
5287 static void pager_open(void) {
5294 if (!on_tty() || arg_no_pager)
5297 if ((pager = getenv("PAGER")))
5298 if (!*pager || streq(pager, "cat"))
5302 log_error("Failed to create pager pipe: %m");
5307 if (pager_pid < 0) {
5308 log_error("Failed to fork pager: %m");
5313 /* In the child start the pager */
5314 if (pager_pid == 0) {
5316 dup2(fd[0], STDIN_FILENO);
5319 if (!getenv("LESS"))
5320 setenv("LESS", "FRSX", 0);
5322 prctl(PR_SET_PDEATHSIG, SIGTERM);
5325 execlp(pager, pager, NULL);
5326 execl("/bin/sh", "sh", "-c", pager, NULL);
5328 execlp("sensible-pager", "sensible-pager", NULL);
5329 execlp("less", "less", NULL);
5330 execlp("more", "more", NULL);
5333 log_error("Unable to execute pager: %m");
5334 _exit(EXIT_FAILURE);
5337 /* Return in the parent */
5338 if (dup2(fd[1], STDOUT_FILENO) < 0)
5339 log_error("Failed to duplicate pager pipe: %m");
5344 static void pager_close(void) {
5350 /* Inform pager that we are done */
5352 wait_for_terminate(pager_pid, &dummy);
5356 int main(int argc, char*argv[]) {
5357 int r, retval = EXIT_FAILURE;
5358 DBusConnection *bus = NULL;
5361 dbus_error_init(&error);
5363 log_parse_environment();
5366 if ((r = parse_argv(argc, argv)) < 0)
5369 retval = EXIT_SUCCESS;
5375 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
5376 * let's shortcut this */
5377 if (arg_action == ACTION_RUNLEVEL) {
5378 r = runlevel_main();
5379 retval = r < 0 ? EXIT_FAILURE : r;
5383 bus_connect(arg_user ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &bus, &private_bus, &error);
5385 switch (arg_action) {
5387 case ACTION_SYSTEMCTL:
5388 r = systemctl_main(bus, argc, argv, &error);
5392 case ACTION_POWEROFF:
5397 case ACTION_RUNLEVEL2:
5398 case ACTION_RUNLEVEL3:
5399 case ACTION_RUNLEVEL4:
5400 case ACTION_RUNLEVEL5:
5402 case ACTION_EMERGENCY:
5403 case ACTION_DEFAULT:
5404 r = start_with_fallback(bus);
5409 r = reload_with_fallback(bus);
5412 case ACTION_CANCEL_SHUTDOWN:
5413 r = send_shutdownd(0, 0, false, NULL);
5416 case ACTION_INVALID:
5417 case ACTION_RUNLEVEL:
5419 assert_not_reached("Unknown action");
5422 retval = r < 0 ? EXIT_FAILURE : r;
5427 dbus_connection_flush(bus);
5428 dbus_connection_close(bus);
5429 dbus_connection_unref(bus);
5432 dbus_error_free(&error);
5436 strv_free(arg_property);