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_wtmp = false;
69 static bool arg_no_sync = false;
70 static bool arg_no_wall = false;
71 static bool arg_no_reload = false;
72 static bool arg_dry = false;
73 static bool arg_quiet = false;
74 static bool arg_full = false;
75 static bool arg_force = false;
76 static bool arg_defaults = false;
77 static bool arg_ask_password = false;
78 static char **arg_wall = NULL;
79 static const char *arg_kill_who = NULL;
80 static const char *arg_kill_mode = NULL;
81 static int arg_signal = SIGTERM;
82 static usec_t arg_when = 0;
101 ACTION_CANCEL_SHUTDOWN,
103 } arg_action = ACTION_SYSTEMCTL;
110 static bool private_bus = false;
112 static int daemon_reload(DBusConnection *bus, char **args, unsigned n);
114 static bool on_tty(void) {
117 if (_unlikely_(t < 0))
118 t = isatty(STDOUT_FILENO) > 0;
123 static void spawn_ask_password_agent(void) {
126 /* We check STDIN here, not STDOUT, since this is about input,
128 if (!isatty(STDIN_FILENO))
131 if (!arg_ask_password)
136 /* Spawns a temporary TTY agent, making sure it goes away when
139 if ((child = fork()) < 0)
145 const char * const args[] = {
146 SYSTEMD_TTY_ASK_PASSWORD_AGENT_BINARY_PATH,
151 if (prctl(PR_SET_PDEATHSIG, SIGTERM) < 0)
154 /* Check whether our parent died before we were able
155 * to set the death signal */
156 if (getppid() != parent)
159 execv(args[0], (char **) args);
164 static const char *ansi_highlight(bool b) {
169 return b ? ANSI_HIGHLIGHT_ON : ANSI_HIGHLIGHT_OFF;
172 static const char *ansi_highlight_green(bool b) {
177 return b ? ANSI_HIGHLIGHT_GREEN_ON : ANSI_HIGHLIGHT_OFF;
180 static bool error_is_no_service(const DBusError *error) {
183 if (!dbus_error_is_set(error))
186 if (dbus_error_has_name(error, DBUS_ERROR_NAME_HAS_NO_OWNER))
189 if (dbus_error_has_name(error, DBUS_ERROR_SERVICE_UNKNOWN))
192 return startswith(error->name, "org.freedesktop.DBus.Error.Spawn.");
195 static int translate_bus_error_to_exit_status(int r, const DBusError *error) {
198 if (!dbus_error_is_set(error))
201 if (dbus_error_has_name(error, DBUS_ERROR_ACCESS_DENIED) ||
202 dbus_error_has_name(error, BUS_ERROR_ONLY_BY_DEPENDENCY) ||
203 dbus_error_has_name(error, BUS_ERROR_NO_ISOLATION) ||
204 dbus_error_has_name(error, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE))
205 return EXIT_NOPERMISSION;
207 if (dbus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT))
208 return EXIT_NOTINSTALLED;
210 if (dbus_error_has_name(error, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE) ||
211 dbus_error_has_name(error, BUS_ERROR_NOT_SUPPORTED))
212 return EXIT_NOTIMPLEMENTED;
214 if (dbus_error_has_name(error, BUS_ERROR_LOAD_FAILED))
215 return EXIT_NOTCONFIGURED;
223 static int bus_iter_get_basic_and_next(DBusMessageIter *iter, int type, void *data, bool next) {
228 if (dbus_message_iter_get_arg_type(iter) != type)
231 dbus_message_iter_get_basic(iter, data);
233 if (!dbus_message_iter_next(iter) != !next)
239 static void warn_wall(enum action action) {
240 static const char *table[_ACTION_MAX] = {
241 [ACTION_HALT] = "The system is going down for system halt NOW!",
242 [ACTION_REBOOT] = "The system is going down for reboot NOW!",
243 [ACTION_POWEROFF] = "The system is going down for power-off NOW!",
244 [ACTION_KEXEC] = "The system is going down for kexec reboot NOW!",
245 [ACTION_RESCUE] = "The system is going down to rescue mode NOW!",
246 [ACTION_EMERGENCY] = "The system is going down to emergency mode NOW!"
255 if (!(p = strv_join(arg_wall, " "))) {
256 log_error("Failed to join strings.");
272 utmp_wall(table[action], NULL);
277 const char *description;
278 const char *load_state;
279 const char *active_state;
280 const char *sub_state;
281 const char *following;
282 const char *unit_path;
284 const char *job_type;
285 const char *job_path;
288 static int compare_unit_info(const void *a, const void *b) {
290 const struct unit_info *u = a, *v = b;
292 d1 = strrchr(u->id, '.');
293 d2 = strrchr(v->id, '.');
298 if ((r = strcasecmp(d1, d2)) != 0)
302 return strcasecmp(u->id, v->id);
305 static bool output_show_job(const struct unit_info *u) {
308 return (!arg_type || ((dot = strrchr(u->id, '.')) &&
309 streq(dot+1, arg_type))) &&
310 (arg_all || !(streq(u->active_state, "inactive") || u->following[0]) || u->job_id > 0);
313 static void output_units_list(const struct unit_info *unit_infos, unsigned c) {
314 unsigned active_len, sub_len, job_len, n_shown = 0;
315 const struct unit_info *u;
317 active_len = sizeof("ACTIVE")-1;
318 sub_len = sizeof("SUB")-1;
319 job_len = sizeof("JOB")-1;
321 for (u = unit_infos; u < unit_infos + c; u++) {
322 if (!output_show_job(u))
325 active_len = MAX(active_len, strlen(u->active_state));
326 sub_len = MAX(sub_len, strlen(u->sub_state));
328 job_len = MAX(job_len, strlen(u->job_type));
332 printf("%-25s %-6s %-*s %-*s %-*s", "UNIT", "LOAD",
333 active_len, "ACTIVE", sub_len, "SUB", job_len, "JOB");
334 if (columns() >= 80+12 || arg_full)
335 printf(" %s\n", "DESCRIPTION");
340 for (u = unit_infos; u < unit_infos + c; u++) {
343 const char *on_loaded, *off_loaded;
344 const char *on_active, *off_active;
346 if (!output_show_job(u))
351 if (!streq(u->load_state, "loaded") &&
352 !streq(u->load_state, "banned")) {
353 on_loaded = ansi_highlight(true);
354 off_loaded = ansi_highlight(false);
356 on_loaded = off_loaded = "";
358 if (streq(u->active_state, "failed")) {
359 on_active = ansi_highlight(true);
360 off_active = ansi_highlight(false);
362 on_active = off_active = "";
364 e = arg_full ? NULL : ellipsize(u->id, 25, 33);
366 printf("%-25s %s%-6s%s %s%-*s %-*s%s%n",
368 on_loaded, u->load_state, off_loaded,
369 on_active, active_len, u->active_state,
370 sub_len, u->sub_state, off_active,
375 a -= strlen(on_loaded) + strlen(off_loaded);
376 a -= strlen(on_active) + strlen(off_active);
379 printf(" %-*s", job_len, u->job_type);
383 if (a + b + 1 < columns()) {
385 printf(" %-*s", job_len, "");
388 printf(" %s", u->description);
390 printf(" %.*s", columns() - a - b - 1, u->description);
397 printf("\nLOAD = Reflects whether the unit definition was properly loaded.\n"
398 "ACTIVE = The high-level unit activation state, i.e. generalization of SUB.\n"
399 "SUB = The low-level unit activation state, values depend on unit type.\n"
400 "JOB = Pending job for the unit.\n");
403 printf("\n%u units listed.\n", n_shown);
405 printf("\n%u units listed. Pass --all to see inactive units, too.\n", n_shown);
409 static int list_units(DBusConnection *bus, char **args, unsigned n) {
410 DBusMessage *m = NULL, *reply = NULL;
413 DBusMessageIter iter, sub, sub2;
414 unsigned c = 0, n_units = 0;
415 struct unit_info *unit_infos = NULL;
417 dbus_error_init(&error);
421 if (!(m = dbus_message_new_method_call(
422 "org.freedesktop.systemd1",
423 "/org/freedesktop/systemd1",
424 "org.freedesktop.systemd1.Manager",
426 log_error("Could not allocate message.");
430 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
431 log_error("Failed to issue method call: %s", bus_error_message(&error));
436 if (!dbus_message_iter_init(reply, &iter) ||
437 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
438 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
439 log_error("Failed to parse reply.");
444 dbus_message_iter_recurse(&iter, &sub);
446 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
449 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
450 log_error("Failed to parse reply.");
458 n_units = MAX(2*c, 16);
459 w = realloc(unit_infos, sizeof(struct unit_info) * n_units);
462 log_error("Failed to allocate unit array.");
472 dbus_message_iter_recurse(&sub, &sub2);
474 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->id, true) < 0 ||
475 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->description, true) < 0 ||
476 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->load_state, true) < 0 ||
477 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->active_state, true) < 0 ||
478 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->sub_state, true) < 0 ||
479 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->following, true) < 0 ||
480 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &u->unit_path, true) < 0 ||
481 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &u->job_id, true) < 0 ||
482 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->job_type, true) < 0 ||
483 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &u->job_path, false) < 0) {
484 log_error("Failed to parse reply.");
489 dbus_message_iter_next(&sub);
493 qsort(unit_infos, c, sizeof(struct unit_info), compare_unit_info);
494 output_units_list(unit_infos, c);
500 dbus_message_unref(m);
503 dbus_message_unref(reply);
507 dbus_error_free(&error);
512 static int dot_one_property(const char *name, const char *prop, DBusMessageIter *iter) {
513 static const char * const colors[] = {
514 "Requires", "[color=\"black\"]",
515 "RequiresOverridable", "[color=\"black\"]",
516 "Requisite", "[color=\"darkblue\"]",
517 "RequisiteOverridable", "[color=\"darkblue\"]",
518 "Wants", "[color=\"darkgrey\"]",
519 "Conflicts", "[color=\"red\"]",
520 "ConflictedBy", "[color=\"red\"]",
521 "After", "[color=\"green\"]"
524 const char *c = NULL;
531 for (i = 0; i < ELEMENTSOF(colors); i += 2)
532 if (streq(colors[i], prop)) {
540 if (arg_dot != DOT_ALL)
541 if ((arg_dot == DOT_ORDER) != streq(prop, "After"))
544 switch (dbus_message_iter_get_arg_type(iter)) {
546 case DBUS_TYPE_ARRAY:
548 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
551 dbus_message_iter_recurse(iter, &sub);
553 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
556 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
557 dbus_message_iter_get_basic(&sub, &s);
558 printf("\t\"%s\"->\"%s\" %s;\n", name, s, c);
560 dbus_message_iter_next(&sub);
570 static int dot_one(DBusConnection *bus, const char *name, const char *path) {
571 DBusMessage *m = NULL, *reply = NULL;
572 const char *interface = "org.freedesktop.systemd1.Unit";
575 DBusMessageIter iter, sub, sub2, sub3;
580 dbus_error_init(&error);
582 if (!(m = dbus_message_new_method_call(
583 "org.freedesktop.systemd1",
585 "org.freedesktop.DBus.Properties",
587 log_error("Could not allocate message.");
592 if (!dbus_message_append_args(m,
593 DBUS_TYPE_STRING, &interface,
594 DBUS_TYPE_INVALID)) {
595 log_error("Could not append arguments to message.");
600 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
601 log_error("Failed to issue method call: %s", bus_error_message(&error));
606 if (!dbus_message_iter_init(reply, &iter) ||
607 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
608 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
609 log_error("Failed to parse reply.");
614 dbus_message_iter_recurse(&iter, &sub);
616 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
619 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
620 log_error("Failed to parse reply.");
625 dbus_message_iter_recurse(&sub, &sub2);
627 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &prop, true) < 0) {
628 log_error("Failed to parse reply.");
633 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
634 log_error("Failed to parse reply.");
639 dbus_message_iter_recurse(&sub2, &sub3);
641 if (dot_one_property(name, prop, &sub3)) {
642 log_error("Failed to parse reply.");
647 dbus_message_iter_next(&sub);
654 dbus_message_unref(m);
657 dbus_message_unref(reply);
659 dbus_error_free(&error);
664 static int dot(DBusConnection *bus, char **args, unsigned n) {
665 DBusMessage *m = NULL, *reply = NULL;
668 DBusMessageIter iter, sub, sub2;
670 dbus_error_init(&error);
674 if (!(m = dbus_message_new_method_call(
675 "org.freedesktop.systemd1",
676 "/org/freedesktop/systemd1",
677 "org.freedesktop.systemd1.Manager",
679 log_error("Could not allocate message.");
683 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
684 log_error("Failed to issue method call: %s", bus_error_message(&error));
689 if (!dbus_message_iter_init(reply, &iter) ||
690 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
691 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
692 log_error("Failed to parse reply.");
697 printf("digraph systemd {\n");
699 dbus_message_iter_recurse(&iter, &sub);
700 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
701 const char *id, *description, *load_state, *active_state, *sub_state, *following, *unit_path;
703 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
704 log_error("Failed to parse reply.");
709 dbus_message_iter_recurse(&sub, &sub2);
711 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
712 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
713 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
714 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
715 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
716 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &following, true) < 0 ||
717 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, true) < 0) {
718 log_error("Failed to parse reply.");
723 if ((r = dot_one(bus, id, unit_path)) < 0)
726 /* printf("\t\"%s\";\n", id); */
727 dbus_message_iter_next(&sub);
732 log_info(" Color legend: black = Requires\n"
733 " dark blue = Requisite\n"
734 " dark grey = Wants\n"
738 if (isatty(fileno(stdout)))
739 log_notice("-- You probably want to process this output with graphviz' dot tool.\n"
740 "-- Try a shell pipeline like 'systemctl dot | dot -Tsvg > systemd.svg'!\n");
746 dbus_message_unref(m);
749 dbus_message_unref(reply);
751 dbus_error_free(&error);
756 static int list_jobs(DBusConnection *bus, char **args, unsigned n) {
757 DBusMessage *m = NULL, *reply = NULL;
760 DBusMessageIter iter, sub, sub2;
763 dbus_error_init(&error);
767 if (!(m = dbus_message_new_method_call(
768 "org.freedesktop.systemd1",
769 "/org/freedesktop/systemd1",
770 "org.freedesktop.systemd1.Manager",
772 log_error("Could not allocate message.");
776 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
777 log_error("Failed to issue method call: %s", bus_error_message(&error));
782 if (!dbus_message_iter_init(reply, &iter) ||
783 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
784 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
785 log_error("Failed to parse reply.");
790 dbus_message_iter_recurse(&iter, &sub);
792 if (isatty(STDOUT_FILENO))
793 printf("%4s %-25s %-15s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
795 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
796 const char *name, *type, *state, *job_path, *unit_path;
800 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
801 log_error("Failed to parse reply.");
806 dbus_message_iter_recurse(&sub, &sub2);
808 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
809 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
810 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
811 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
812 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
813 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
814 log_error("Failed to parse reply.");
819 e = arg_full ? NULL : ellipsize(name, 25, 33);
820 printf("%4u %-25s %-15s %-7s\n", id, e ? e : name, type, state);
825 dbus_message_iter_next(&sub);
828 if (isatty(STDOUT_FILENO))
829 printf("\n%u jobs listed.\n", k);
835 dbus_message_unref(m);
838 dbus_message_unref(reply);
840 dbus_error_free(&error);
845 static int load_unit(DBusConnection *bus, char **args, unsigned n) {
846 DBusMessage *m = NULL, *reply = NULL;
851 dbus_error_init(&error);
856 for (i = 1; i < n; i++) {
858 if (!(m = dbus_message_new_method_call(
859 "org.freedesktop.systemd1",
860 "/org/freedesktop/systemd1",
861 "org.freedesktop.systemd1.Manager",
863 log_error("Could not allocate message.");
868 if (!dbus_message_append_args(m,
869 DBUS_TYPE_STRING, &args[i],
870 DBUS_TYPE_INVALID)) {
871 log_error("Could not append arguments to message.");
876 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
877 log_error("Failed to issue method call: %s", bus_error_message(&error));
882 dbus_message_unref(m);
883 dbus_message_unref(reply);
892 dbus_message_unref(m);
895 dbus_message_unref(reply);
897 dbus_error_free(&error);
902 static int cancel_job(DBusConnection *bus, char **args, unsigned n) {
903 DBusMessage *m = NULL, *reply = NULL;
908 dbus_error_init(&error);
914 return daemon_reload(bus, args, n);
916 for (i = 1; i < n; i++) {
920 if (!(m = dbus_message_new_method_call(
921 "org.freedesktop.systemd1",
922 "/org/freedesktop/systemd1",
923 "org.freedesktop.systemd1.Manager",
925 log_error("Could not allocate message.");
930 if ((r = safe_atou(args[i], &id)) < 0) {
931 log_error("Failed to parse job id: %s", strerror(-r));
935 assert_cc(sizeof(uint32_t) == sizeof(id));
936 if (!dbus_message_append_args(m,
937 DBUS_TYPE_UINT32, &id,
938 DBUS_TYPE_INVALID)) {
939 log_error("Could not append arguments to message.");
944 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
945 log_error("Failed to issue method call: %s", bus_error_message(&error));
950 if (!dbus_message_get_args(reply, &error,
951 DBUS_TYPE_OBJECT_PATH, &path,
952 DBUS_TYPE_INVALID)) {
953 log_error("Failed to parse reply: %s", bus_error_message(&error));
958 dbus_message_unref(m);
959 if (!(m = dbus_message_new_method_call(
960 "org.freedesktop.systemd1",
962 "org.freedesktop.systemd1.Job",
964 log_error("Could not allocate message.");
969 dbus_message_unref(reply);
970 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
971 log_error("Failed to issue method call: %s", bus_error_message(&error));
976 dbus_message_unref(m);
977 dbus_message_unref(reply);
985 dbus_message_unref(m);
988 dbus_message_unref(reply);
990 dbus_error_free(&error);
995 static bool need_daemon_reload(DBusConnection *bus, const char *unit) {
996 DBusMessage *m = NULL, *reply = NULL;
997 dbus_bool_t b = FALSE;
998 DBusMessageIter iter, sub;
1000 *interface = "org.freedesktop.systemd1.Unit",
1001 *property = "NeedDaemonReload",
1004 /* We ignore all errors here, since this is used to show a warning only */
1006 if (!(m = dbus_message_new_method_call(
1007 "org.freedesktop.systemd1",
1008 "/org/freedesktop/systemd1",
1009 "org.freedesktop.systemd1.Manager",
1013 if (!dbus_message_append_args(m,
1014 DBUS_TYPE_STRING, &unit,
1018 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
1021 if (!dbus_message_get_args(reply, NULL,
1022 DBUS_TYPE_OBJECT_PATH, &path,
1026 dbus_message_unref(m);
1027 if (!(m = dbus_message_new_method_call(
1028 "org.freedesktop.systemd1",
1030 "org.freedesktop.DBus.Properties",
1034 if (!dbus_message_append_args(m,
1035 DBUS_TYPE_STRING, &interface,
1036 DBUS_TYPE_STRING, &property,
1037 DBUS_TYPE_INVALID)) {
1041 dbus_message_unref(reply);
1042 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
1045 if (!dbus_message_iter_init(reply, &iter) ||
1046 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
1049 dbus_message_iter_recurse(&iter, &sub);
1051 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_BOOLEAN)
1054 dbus_message_iter_get_basic(&sub, &b);
1058 dbus_message_unref(m);
1061 dbus_message_unref(reply);
1066 typedef struct WaitData {
1071 static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
1079 dbus_error_init(&error);
1081 log_debug("Got D-Bus request: %s.%s() on %s",
1082 dbus_message_get_interface(message),
1083 dbus_message_get_member(message),
1084 dbus_message_get_path(message));
1086 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
1087 log_error("Warning! D-Bus connection terminated.");
1088 dbus_connection_close(connection);
1090 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
1093 dbus_bool_t success = true;
1095 if (!dbus_message_get_args(message, &error,
1096 DBUS_TYPE_UINT32, &id,
1097 DBUS_TYPE_OBJECT_PATH, &path,
1098 DBUS_TYPE_BOOLEAN, &success,
1100 log_error("Failed to parse message: %s", bus_error_message(&error));
1104 if ((p = set_remove(d->set, (char*) path)))
1112 dbus_error_free(&error);
1113 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1116 static int enable_wait_for_jobs(DBusConnection *bus) {
1124 dbus_error_init(&error);
1125 dbus_bus_add_match(bus,
1127 "sender='org.freedesktop.systemd1',"
1128 "interface='org.freedesktop.systemd1.Manager',"
1129 "member='JobRemoved',"
1130 "path='/org/freedesktop/systemd1'",
1133 if (dbus_error_is_set(&error)) {
1134 log_error("Failed to add match: %s", bus_error_message(&error));
1135 dbus_error_free(&error);
1139 /* This is slightly dirty, since we don't undo the match registrations. */
1143 static int wait_for_jobs(DBusConnection *bus, Set *s) {
1154 if (!dbus_connection_add_filter(bus, wait_filter, &d, NULL)) {
1155 log_error("Failed to add filter.");
1160 while (!set_isempty(s) &&
1161 dbus_connection_read_write_dispatch(bus, -1))
1164 if (!arg_quiet && d.failed)
1165 log_error("Job failed. See system logs and 'systemctl status' for details.");
1167 r = d.failed ? -EIO : 0;
1170 /* This is slightly dirty, since we don't undo the filter registration. */
1175 static int start_unit_one(
1176 DBusConnection *bus,
1183 DBusMessage *m = NULL, *reply = NULL;
1192 assert(arg_no_block || s);
1194 if (!(m = dbus_message_new_method_call(
1195 "org.freedesktop.systemd1",
1196 "/org/freedesktop/systemd1",
1197 "org.freedesktop.systemd1.Manager",
1199 log_error("Could not allocate message.");
1204 if (!dbus_message_append_args(m,
1205 DBUS_TYPE_STRING, &name,
1206 DBUS_TYPE_STRING, &mode,
1207 DBUS_TYPE_INVALID)) {
1208 log_error("Could not append arguments to message.");
1213 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, error))) {
1215 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(error)) {
1216 /* There's always a fallback possible for
1217 * legacy actions. */
1222 log_error("Failed to issue method call: %s", bus_error_message(error));
1227 if (!dbus_message_get_args(reply, error,
1228 DBUS_TYPE_OBJECT_PATH, &path,
1229 DBUS_TYPE_INVALID)) {
1230 log_error("Failed to parse reply: %s", bus_error_message(error));
1235 if (need_daemon_reload(bus, name))
1236 log_warning("Unit file of created job changed on disk, 'systemctl %s daemon-reload' recommended.",
1237 arg_user ? "--user" : "--system");
1239 if (!arg_no_block) {
1242 if (!(p = strdup(path))) {
1243 log_error("Failed to duplicate path.");
1248 if ((r = set_put(s, p)) < 0) {
1250 log_error("Failed to add path to set.");
1259 dbus_message_unref(m);
1262 dbus_message_unref(reply);
1267 static enum action verb_to_action(const char *verb) {
1268 if (streq(verb, "halt"))
1270 else if (streq(verb, "poweroff"))
1271 return ACTION_POWEROFF;
1272 else if (streq(verb, "reboot"))
1273 return ACTION_REBOOT;
1274 else if (streq(verb, "kexec"))
1275 return ACTION_KEXEC;
1276 else if (streq(verb, "rescue"))
1277 return ACTION_RESCUE;
1278 else if (streq(verb, "emergency"))
1279 return ACTION_EMERGENCY;
1280 else if (streq(verb, "default"))
1281 return ACTION_DEFAULT;
1282 else if (streq(verb, "exit"))
1285 return ACTION_INVALID;
1288 static int start_unit(DBusConnection *bus, char **args, unsigned n) {
1290 static const char * const table[_ACTION_MAX] = {
1291 [ACTION_HALT] = SPECIAL_HALT_TARGET,
1292 [ACTION_POWEROFF] = SPECIAL_POWEROFF_TARGET,
1293 [ACTION_REBOOT] = SPECIAL_REBOOT_TARGET,
1294 [ACTION_KEXEC] = SPECIAL_KEXEC_TARGET,
1295 [ACTION_RUNLEVEL2] = SPECIAL_RUNLEVEL2_TARGET,
1296 [ACTION_RUNLEVEL3] = SPECIAL_RUNLEVEL3_TARGET,
1297 [ACTION_RUNLEVEL4] = SPECIAL_RUNLEVEL4_TARGET,
1298 [ACTION_RUNLEVEL5] = SPECIAL_RUNLEVEL5_TARGET,
1299 [ACTION_RESCUE] = SPECIAL_RESCUE_TARGET,
1300 [ACTION_EMERGENCY] = SPECIAL_EMERGENCY_TARGET,
1301 [ACTION_DEFAULT] = SPECIAL_DEFAULT_TARGET,
1302 [ACTION_EXIT] = SPECIAL_EXIT_TARGET
1307 const char *method, *mode, *one_name;
1311 dbus_error_init(&error);
1315 spawn_ask_password_agent();
1317 if (arg_action == ACTION_SYSTEMCTL) {
1319 streq(args[0], "stop") ? "StopUnit" :
1320 streq(args[0], "reload") ? "ReloadUnit" :
1321 streq(args[0], "restart") ? "RestartUnit" :
1322 streq(args[0], "try-restart") ||
1323 streq(args[0], "condrestart") ? "TryRestartUnit" :
1324 streq(args[0], "reload-or-restart") ? "ReloadOrRestartUnit" :
1325 streq(args[0], "reload-or-try-restart") ||
1326 streq(args[0], "force-reload") ? "ReloadOrTryRestartUnit" :
1330 (streq(args[0], "isolate") ||
1331 streq(args[0], "rescue") ||
1332 streq(args[0], "emergency")) ? "isolate" :
1336 one_name = table[verb_to_action(args[0])];
1339 assert(arg_action < ELEMENTSOF(table));
1340 assert(table[arg_action]);
1342 method = "StartUnit";
1344 mode = (arg_action == ACTION_EMERGENCY ||
1345 arg_action == ACTION_RESCUE ||
1346 arg_action == ACTION_RUNLEVEL2 ||
1347 arg_action == ACTION_RUNLEVEL3 ||
1348 arg_action == ACTION_RUNLEVEL4 ||
1349 arg_action == ACTION_RUNLEVEL5) ? "isolate" : "replace";
1351 one_name = table[arg_action];
1354 if (!arg_no_block) {
1355 if ((ret = enable_wait_for_jobs(bus)) < 0) {
1356 log_error("Could not watch jobs: %s", strerror(-ret));
1360 if (!(s = set_new(string_hash_func, string_compare_func))) {
1361 log_error("Failed to allocate set.");
1368 if ((ret = start_unit_one(bus, method, one_name, mode, &error, s)) <= 0)
1371 for (i = 1; i < n; i++)
1372 if ((r = start_unit_one(bus, method, args[i], mode, &error, s)) != 0) {
1373 ret = translate_bus_error_to_exit_status(r, &error);
1374 dbus_error_free(&error);
1379 if ((r = wait_for_jobs(bus, s)) < 0) {
1388 dbus_error_free(&error);
1393 static int start_special(DBusConnection *bus, char **args, unsigned n) {
1400 (streq(args[0], "halt") ||
1401 streq(args[0], "poweroff") ||
1402 streq(args[0], "reboot") ||
1403 streq(args[0], "kexec") ||
1404 streq(args[0], "exit")))
1405 return daemon_reload(bus, args, n);
1407 r = start_unit(bus, args, n);
1410 warn_wall(verb_to_action(args[0]));
1415 static int check_unit(DBusConnection *bus, char **args, unsigned n) {
1416 DBusMessage *m = NULL, *reply = NULL;
1418 *interface = "org.freedesktop.systemd1.Unit",
1419 *property = "ActiveState";
1420 int r = 3; /* According to LSB: "program is not running" */
1427 dbus_error_init(&error);
1429 for (i = 1; i < n; i++) {
1430 const char *path = NULL;
1432 DBusMessageIter iter, sub;
1434 if (!(m = dbus_message_new_method_call(
1435 "org.freedesktop.systemd1",
1436 "/org/freedesktop/systemd1",
1437 "org.freedesktop.systemd1.Manager",
1439 log_error("Could not allocate message.");
1444 if (!dbus_message_append_args(m,
1445 DBUS_TYPE_STRING, &args[i],
1446 DBUS_TYPE_INVALID)) {
1447 log_error("Could not append arguments to message.");
1452 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1454 /* Hmm, cannot figure out anything about this unit... */
1458 dbus_error_free(&error);
1459 dbus_message_unref(m);
1463 if (!dbus_message_get_args(reply, &error,
1464 DBUS_TYPE_OBJECT_PATH, &path,
1465 DBUS_TYPE_INVALID)) {
1466 log_error("Failed to parse reply: %s", bus_error_message(&error));
1471 dbus_message_unref(m);
1472 if (!(m = dbus_message_new_method_call(
1473 "org.freedesktop.systemd1",
1475 "org.freedesktop.DBus.Properties",
1477 log_error("Could not allocate message.");
1482 if (!dbus_message_append_args(m,
1483 DBUS_TYPE_STRING, &interface,
1484 DBUS_TYPE_STRING, &property,
1485 DBUS_TYPE_INVALID)) {
1486 log_error("Could not append arguments to message.");
1491 dbus_message_unref(reply);
1492 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1493 log_error("Failed to issue method call: %s", bus_error_message(&error));
1498 if (!dbus_message_iter_init(reply, &iter) ||
1499 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1500 log_error("Failed to parse reply.");
1505 dbus_message_iter_recurse(&iter, &sub);
1507 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1508 log_error("Failed to parse reply.");
1513 dbus_message_iter_get_basic(&sub, &state);
1518 if (streq(state, "active") || streq(state, "reloading"))
1521 dbus_message_unref(m);
1522 dbus_message_unref(reply);
1528 dbus_message_unref(m);
1531 dbus_message_unref(reply);
1533 dbus_error_free(&error);
1538 static int kill_unit(DBusConnection *bus, char **args, unsigned n) {
1539 DBusMessage *m = NULL, *reply = NULL;
1547 dbus_error_init(&error);
1550 arg_kill_who = "all";
1553 arg_kill_mode = streq(arg_kill_who, "all") ? "control-group" : "process";
1555 for (i = 1; i < n; i++) {
1557 if (!(m = dbus_message_new_method_call(
1558 "org.freedesktop.systemd1",
1559 "/org/freedesktop/systemd1",
1560 "org.freedesktop.systemd1.Manager",
1562 log_error("Could not allocate message.");
1567 if (!dbus_message_append_args(m,
1568 DBUS_TYPE_STRING, &args[i],
1569 DBUS_TYPE_STRING, &arg_kill_who,
1570 DBUS_TYPE_STRING, &arg_kill_mode,
1571 DBUS_TYPE_INT32, &arg_signal,
1572 DBUS_TYPE_INVALID)) {
1573 log_error("Could not append arguments to message.");
1578 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1579 log_error("Failed to issue method call: %s", bus_error_message(&error));
1580 dbus_error_free(&error);
1584 dbus_message_unref(m);
1587 dbus_message_unref(reply);
1593 dbus_message_unref(m);
1596 dbus_message_unref(reply);
1598 dbus_error_free(&error);
1603 typedef struct ExecStatusInfo {
1609 usec_t start_timestamp;
1610 usec_t exit_timestamp;
1615 LIST_FIELDS(struct ExecStatusInfo, exec);
1618 static void exec_status_info_free(ExecStatusInfo *i) {
1626 static int exec_status_info_deserialize(DBusMessageIter *sub, ExecStatusInfo *i) {
1627 uint64_t start_timestamp, exit_timestamp;
1628 DBusMessageIter sub2, sub3;
1632 int32_t code, status;
1638 if (dbus_message_iter_get_arg_type(sub) != DBUS_TYPE_STRUCT)
1641 dbus_message_iter_recurse(sub, &sub2);
1643 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
1646 if (!(i->path = strdup(path)))
1649 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
1650 dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
1654 dbus_message_iter_recurse(&sub2, &sub3);
1655 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1656 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1657 dbus_message_iter_next(&sub3);
1662 if (!(i->argv = new0(char*, n+1)))
1666 dbus_message_iter_recurse(&sub2, &sub3);
1667 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1670 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1671 dbus_message_iter_get_basic(&sub3, &s);
1672 dbus_message_iter_next(&sub3);
1674 if (!(i->argv[n++] = strdup(s)))
1678 if (!dbus_message_iter_next(&sub2) ||
1679 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, true) < 0 ||
1680 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp, true) < 0 ||
1681 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp, true) < 0 ||
1682 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) < 0 ||
1683 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) < 0 ||
1684 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) < 0)
1688 i->start_timestamp = (usec_t) start_timestamp;
1689 i->exit_timestamp = (usec_t) exit_timestamp;
1690 i->pid = (pid_t) pid;
1697 typedef struct UnitStatusInfo {
1699 const char *load_state;
1700 const char *active_state;
1701 const char *sub_state;
1703 const char *description;
1704 const char *following;
1707 const char *default_control_group;
1709 usec_t inactive_exit_timestamp;
1710 usec_t active_enter_timestamp;
1711 usec_t active_exit_timestamp;
1712 usec_t inactive_enter_timestamp;
1714 bool need_daemon_reload;
1719 const char *status_text;
1721 #ifdef HAVE_SYSV_COMPAT
1725 usec_t start_timestamp;
1726 usec_t exit_timestamp;
1728 int exit_code, exit_status;
1731 unsigned n_accepted;
1732 unsigned n_connections;
1736 const char *sysfs_path;
1738 /* Mount, Automount */
1744 LIST_HEAD(ExecStatusInfo, exec);
1747 static void print_status_info(UnitStatusInfo *i) {
1749 const char *on, *off, *ss;
1751 char since1[FORMAT_TIMESTAMP_PRETTY_MAX], *s1;
1752 char since2[FORMAT_TIMESTAMP_MAX], *s2;
1756 /* This shows pretty information about a unit. See
1757 * print_property() for a low-level property printer */
1759 printf("%s", strna(i->id));
1761 if (i->description && !streq_ptr(i->id, i->description))
1762 printf(" - %s", i->description);
1767 printf("\t Follow: unit currently follows state of %s\n", i->following);
1769 if (streq_ptr(i->load_state, "failed") ||
1770 streq_ptr(i->load_state, "banned")) {
1771 on = ansi_highlight(true);
1772 off = ansi_highlight(false);
1777 printf("\t Loaded: %s%s%s (%s)\n", on, strna(i->load_state), off, i->path);
1779 printf("\t Loaded: %s%s%s\n", on, strna(i->load_state), off);
1781 ss = streq_ptr(i->active_state, i->sub_state) ? NULL : i->sub_state;
1783 if (streq_ptr(i->active_state, "failed")) {
1784 on = ansi_highlight(true);
1785 off = ansi_highlight(false);
1786 } else if (streq_ptr(i->active_state, "active") || streq_ptr(i->active_state, "reloading")) {
1787 on = ansi_highlight_green(true);
1788 off = ansi_highlight_green(false);
1793 printf("\t Active: %s%s (%s)%s",
1795 strna(i->active_state),
1799 printf("\t Active: %s%s%s",
1801 strna(i->active_state),
1804 timestamp = (streq_ptr(i->active_state, "active") ||
1805 streq_ptr(i->active_state, "reloading")) ? i->active_enter_timestamp :
1806 (streq_ptr(i->active_state, "inactive") ||
1807 streq_ptr(i->active_state, "failed")) ? i->inactive_enter_timestamp :
1808 streq_ptr(i->active_state, "activating") ? i->inactive_exit_timestamp :
1809 i->active_exit_timestamp;
1811 s1 = format_timestamp_pretty(since1, sizeof(since1), timestamp);
1812 s2 = format_timestamp(since2, sizeof(since2), timestamp);
1815 printf(" since %s; %s\n", s2, s1);
1817 printf(" since %s\n", s2);
1822 printf("\t Device: %s\n", i->sysfs_path);
1824 printf("\t Where: %s\n", i->where);
1826 printf("\t What: %s\n", i->what);
1829 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
1831 LIST_FOREACH(exec, p, i->exec) {
1834 /* Only show exited processes here */
1838 t = strv_join(p->argv, " ");
1839 printf("\t Process: %u (%s, code=%s, ", p->pid, strna(t), sigchld_code_to_string(p->code));
1842 if (p->code == CLD_EXITED) {
1845 printf("status=%i", p->status);
1847 #ifdef HAVE_SYSV_COMPAT
1848 if ((c = exit_status_to_string(p->status, i->is_sysv ? EXIT_STATUS_LSB : EXIT_STATUS_SYSTEMD)))
1850 if ((c = exit_status_to_string(p->status, EXIT_STATUS_SYSTEMD)))
1855 printf("signal=%s", signal_to_string(p->status));
1858 if (i->main_pid == p->pid &&
1859 i->start_timestamp == p->start_timestamp &&
1860 i->exit_timestamp == p->start_timestamp)
1861 /* Let's not show this twice */
1864 if (p->pid == i->control_pid)
1868 if (i->main_pid > 0 || i->control_pid > 0) {
1871 if (i->main_pid > 0) {
1872 printf("Main PID: %u", (unsigned) i->main_pid);
1876 get_process_name(i->main_pid, &t);
1881 } else if (i->exit_code > 0) {
1882 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
1884 if (i->exit_code == CLD_EXITED) {
1887 printf("status=%i", i->exit_status);
1889 #ifdef HAVE_SYSV_COMPAT
1890 if ((c = exit_status_to_string(i->exit_status, i->is_sysv ? EXIT_STATUS_LSB : EXIT_STATUS_SYSTEMD)))
1892 if ((c = exit_status_to_string(i->exit_status, EXIT_STATUS_SYSTEMD)))
1897 printf("signal=%s", signal_to_string(i->exit_status));
1902 if (i->main_pid > 0 && i->control_pid > 0)
1905 if (i->control_pid > 0) {
1908 printf(" Control: %u", (unsigned) i->control_pid);
1910 get_process_name(i->control_pid, &t);
1921 printf("\t Status: \"%s\"\n", i->status_text);
1923 if (i->default_control_group) {
1926 printf("\t CGroup: %s\n", i->default_control_group);
1928 if ((c = columns()) > 18)
1933 show_cgroup_by_path(i->default_control_group, "\t\t ", c);
1936 if (i->need_daemon_reload)
1937 printf("\n%sWarning:%s Unit file changed on disk, 'systemctl %s daemon-reload' recommended.\n",
1938 ansi_highlight(true),
1939 ansi_highlight(false),
1940 arg_user ? "--user" : "--system");
1943 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
1945 switch (dbus_message_iter_get_arg_type(iter)) {
1947 case DBUS_TYPE_STRING: {
1950 dbus_message_iter_get_basic(iter, &s);
1953 if (streq(name, "Id"))
1955 else if (streq(name, "LoadState"))
1957 else if (streq(name, "ActiveState"))
1958 i->active_state = s;
1959 else if (streq(name, "SubState"))
1961 else if (streq(name, "Description"))
1963 else if (streq(name, "FragmentPath"))
1965 #ifdef HAVE_SYSV_COMPAT
1966 else if (streq(name, "SysVPath")) {
1971 else if (streq(name, "DefaultControlGroup"))
1972 i->default_control_group = s;
1973 else if (streq(name, "StatusText"))
1975 else if (streq(name, "SysFSPath"))
1977 else if (streq(name, "Where"))
1979 else if (streq(name, "What"))
1981 else if (streq(name, "Following"))
1988 case DBUS_TYPE_BOOLEAN: {
1991 dbus_message_iter_get_basic(iter, &b);
1993 if (streq(name, "Accept"))
1995 else if (streq(name, "NeedDaemonReload"))
1996 i->need_daemon_reload = b;
2001 case DBUS_TYPE_UINT32: {
2004 dbus_message_iter_get_basic(iter, &u);
2006 if (streq(name, "MainPID")) {
2008 i->main_pid = (pid_t) u;
2011 } else if (streq(name, "ControlPID"))
2012 i->control_pid = (pid_t) u;
2013 else if (streq(name, "ExecMainPID")) {
2015 i->main_pid = (pid_t) u;
2016 } else if (streq(name, "NAccepted"))
2018 else if (streq(name, "NConnections"))
2019 i->n_connections = u;
2024 case DBUS_TYPE_INT32: {
2027 dbus_message_iter_get_basic(iter, &j);
2029 if (streq(name, "ExecMainCode"))
2030 i->exit_code = (int) j;
2031 else if (streq(name, "ExecMainStatus"))
2032 i->exit_status = (int) j;
2037 case DBUS_TYPE_UINT64: {
2040 dbus_message_iter_get_basic(iter, &u);
2042 if (streq(name, "ExecMainStartTimestamp"))
2043 i->start_timestamp = (usec_t) u;
2044 else if (streq(name, "ExecMainExitTimestamp"))
2045 i->exit_timestamp = (usec_t) u;
2046 else if (streq(name, "ActiveEnterTimestamp"))
2047 i->active_enter_timestamp = (usec_t) u;
2048 else if (streq(name, "InactiveEnterTimestamp"))
2049 i->inactive_enter_timestamp = (usec_t) u;
2050 else if (streq(name, "InactiveExitTimestamp"))
2051 i->inactive_exit_timestamp = (usec_t) u;
2052 else if (streq(name, "ActiveExitTimestamp"))
2053 i->active_exit_timestamp = (usec_t) u;
2058 case DBUS_TYPE_ARRAY: {
2060 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT &&
2061 startswith(name, "Exec")) {
2062 DBusMessageIter sub;
2064 dbus_message_iter_recurse(iter, &sub);
2065 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2066 ExecStatusInfo *info;
2069 if (!(info = new0(ExecStatusInfo, 1)))
2072 if ((r = exec_status_info_deserialize(&sub, info)) < 0) {
2077 LIST_PREPEND(ExecStatusInfo, exec, i->exec, info);
2079 dbus_message_iter_next(&sub);
2090 static int print_property(const char *name, DBusMessageIter *iter) {
2094 /* This is a low-level property printer, see
2095 * print_status_info() for the nicer output */
2097 if (arg_property && !strv_find(arg_property, name))
2100 switch (dbus_message_iter_get_arg_type(iter)) {
2102 case DBUS_TYPE_STRING: {
2104 dbus_message_iter_get_basic(iter, &s);
2106 if (arg_all || s[0])
2107 printf("%s=%s\n", name, s);
2112 case DBUS_TYPE_BOOLEAN: {
2114 dbus_message_iter_get_basic(iter, &b);
2115 printf("%s=%s\n", name, yes_no(b));
2120 case DBUS_TYPE_UINT64: {
2122 dbus_message_iter_get_basic(iter, &u);
2124 /* Yes, heuristics! But we can change this check
2125 * should it turn out to not be sufficient */
2127 if (strstr(name, "Timestamp")) {
2128 char timestamp[FORMAT_TIMESTAMP_MAX], *t;
2130 if ((t = format_timestamp(timestamp, sizeof(timestamp), u)) || arg_all)
2131 printf("%s=%s\n", name, strempty(t));
2132 } else if (strstr(name, "USec")) {
2133 char timespan[FORMAT_TIMESPAN_MAX];
2135 printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u));
2137 printf("%s=%llu\n", name, (unsigned long long) u);
2142 case DBUS_TYPE_UINT32: {
2144 dbus_message_iter_get_basic(iter, &u);
2146 if (strstr(name, "UMask") || strstr(name, "Mode"))
2147 printf("%s=%04o\n", name, u);
2149 printf("%s=%u\n", name, (unsigned) u);
2154 case DBUS_TYPE_INT32: {
2156 dbus_message_iter_get_basic(iter, &i);
2158 printf("%s=%i\n", name, (int) i);
2162 case DBUS_TYPE_DOUBLE: {
2164 dbus_message_iter_get_basic(iter, &d);
2166 printf("%s=%g\n", name, d);
2170 case DBUS_TYPE_STRUCT: {
2171 DBusMessageIter sub;
2172 dbus_message_iter_recurse(iter, &sub);
2174 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
2177 dbus_message_iter_get_basic(&sub, &u);
2180 printf("%s=%u\n", name, (unsigned) u);
2182 printf("%s=\n", name);
2185 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
2188 dbus_message_iter_get_basic(&sub, &s);
2190 if (arg_all || s[0])
2191 printf("%s=%s\n", name, s);
2199 case DBUS_TYPE_ARRAY:
2201 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
2202 DBusMessageIter sub;
2205 dbus_message_iter_recurse(iter, &sub);
2207 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2208 printf("%s=", name);
2210 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2213 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
2214 dbus_message_iter_get_basic(&sub, &s);
2215 printf("%s%s", space ? " " : "", s);
2218 dbus_message_iter_next(&sub);
2226 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_BYTE) {
2227 DBusMessageIter sub;
2229 dbus_message_iter_recurse(iter, &sub);
2231 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2232 printf("%s=", name);
2234 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2237 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_BYTE);
2238 dbus_message_iter_get_basic(&sub, &u);
2241 dbus_message_iter_next(&sub);
2249 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
2250 DBusMessageIter sub, sub2;
2252 dbus_message_iter_recurse(iter, &sub);
2253 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2254 const char *type, *path;
2256 dbus_message_iter_recurse(&sub, &sub2);
2258 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
2259 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
2260 printf("%s=%s\n", type, path);
2262 dbus_message_iter_next(&sub);
2267 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
2268 DBusMessageIter sub, sub2;
2270 dbus_message_iter_recurse(iter, &sub);
2271 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2273 uint64_t value, next_elapse;
2275 dbus_message_iter_recurse(&sub, &sub2);
2277 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
2278 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
2279 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
2280 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
2282 printf("%s={ value=%s ; next_elapse=%s }\n",
2284 format_timespan(timespan1, sizeof(timespan1), value),
2285 format_timespan(timespan2, sizeof(timespan2), next_elapse));
2288 dbus_message_iter_next(&sub);
2293 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
2294 DBusMessageIter sub;
2296 dbus_message_iter_recurse(iter, &sub);
2297 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2298 ExecStatusInfo info;
2301 if (exec_status_info_deserialize(&sub, &info) >= 0) {
2302 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
2305 t = strv_join(info.argv, " ");
2307 printf("%s={ path=%s ; argv[]=%s ; ignore=%s ; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n",
2311 yes_no(info.ignore),
2312 strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)),
2313 strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)),
2314 (unsigned) info. pid,
2315 sigchld_code_to_string(info.code),
2317 info.code == CLD_EXITED ? "" : "/",
2318 strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status)));
2324 strv_free(info.argv);
2326 dbus_message_iter_next(&sub);
2336 printf("%s=[unprintable]\n", name);
2341 static int show_one(const char *verb, DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
2342 DBusMessage *m = NULL, *reply = NULL;
2343 const char *interface = "";
2346 DBusMessageIter iter, sub, sub2, sub3;
2347 UnitStatusInfo info;
2355 dbus_error_init(&error);
2357 if (!(m = dbus_message_new_method_call(
2358 "org.freedesktop.systemd1",
2360 "org.freedesktop.DBus.Properties",
2362 log_error("Could not allocate message.");
2367 if (!dbus_message_append_args(m,
2368 DBUS_TYPE_STRING, &interface,
2369 DBUS_TYPE_INVALID)) {
2370 log_error("Could not append arguments to message.");
2375 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2376 log_error("Failed to issue method call: %s", bus_error_message(&error));
2381 if (!dbus_message_iter_init(reply, &iter) ||
2382 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
2383 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
2384 log_error("Failed to parse reply.");
2389 dbus_message_iter_recurse(&iter, &sub);
2396 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2399 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
2400 log_error("Failed to parse reply.");
2405 dbus_message_iter_recurse(&sub, &sub2);
2407 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
2408 log_error("Failed to parse reply.");
2413 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
2414 log_error("Failed to parse reply.");
2419 dbus_message_iter_recurse(&sub2, &sub3);
2421 if (show_properties)
2422 r = print_property(name, &sub3);
2424 r = status_property(name, &sub3, &info);
2427 log_error("Failed to parse reply.");
2432 dbus_message_iter_next(&sub);
2437 if (!show_properties)
2438 print_status_info(&info);
2440 if (!streq_ptr(info.active_state, "active") &&
2441 !streq_ptr(info.active_state, "reloading") &&
2442 streq(verb, "status"))
2443 /* According to LSB: "program not running" */
2446 while ((p = info.exec)) {
2447 LIST_REMOVE(ExecStatusInfo, exec, info.exec, p);
2448 exec_status_info_free(p);
2453 dbus_message_unref(m);
2456 dbus_message_unref(reply);
2458 dbus_error_free(&error);
2463 static int show(DBusConnection *bus, char **args, unsigned n) {
2464 DBusMessage *m = NULL, *reply = NULL;
2468 bool show_properties, new_line = false;
2473 dbus_error_init(&error);
2475 show_properties = !streq(args[0], "status");
2477 if (show_properties && n <= 1) {
2478 /* If not argument is specified inspect the manager
2481 ret = show_one(args[0], bus, "/org/freedesktop/systemd1", show_properties, &new_line);
2485 for (i = 1; i < n; i++) {
2486 const char *path = NULL;
2489 if (safe_atou32(args[i], &id) < 0) {
2491 /* Interpret as unit name */
2493 if (!(m = dbus_message_new_method_call(
2494 "org.freedesktop.systemd1",
2495 "/org/freedesktop/systemd1",
2496 "org.freedesktop.systemd1.Manager",
2498 log_error("Could not allocate message.");
2503 if (!dbus_message_append_args(m,
2504 DBUS_TYPE_STRING, &args[i],
2505 DBUS_TYPE_INVALID)) {
2506 log_error("Could not append arguments to message.");
2511 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2513 if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
2514 log_error("Failed to issue method call: %s", bus_error_message(&error));
2519 dbus_error_free(&error);
2521 dbus_message_unref(m);
2522 if (!(m = dbus_message_new_method_call(
2523 "org.freedesktop.systemd1",
2524 "/org/freedesktop/systemd1",
2525 "org.freedesktop.systemd1.Manager",
2527 log_error("Could not allocate message.");
2532 if (!dbus_message_append_args(m,
2533 DBUS_TYPE_STRING, &args[i],
2534 DBUS_TYPE_INVALID)) {
2535 log_error("Could not append arguments to message.");
2540 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2541 log_error("Failed to issue method call: %s", bus_error_message(&error));
2543 if (dbus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT))
2544 ret = 4; /* According to LSB: "program or service status is unknown" */
2551 } else if (show_properties) {
2553 /* Interpret as job id */
2555 if (!(m = dbus_message_new_method_call(
2556 "org.freedesktop.systemd1",
2557 "/org/freedesktop/systemd1",
2558 "org.freedesktop.systemd1.Manager",
2560 log_error("Could not allocate message.");
2565 if (!dbus_message_append_args(m,
2566 DBUS_TYPE_UINT32, &id,
2567 DBUS_TYPE_INVALID)) {
2568 log_error("Could not append arguments to message.");
2573 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2574 log_error("Failed to issue method call: %s", bus_error_message(&error));
2580 /* Interpret as PID */
2582 if (!(m = dbus_message_new_method_call(
2583 "org.freedesktop.systemd1",
2584 "/org/freedesktop/systemd1",
2585 "org.freedesktop.systemd1.Manager",
2587 log_error("Could not allocate message.");
2592 if (!dbus_message_append_args(m,
2593 DBUS_TYPE_UINT32, &id,
2594 DBUS_TYPE_INVALID)) {
2595 log_error("Could not append arguments to message.");
2600 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2601 log_error("Failed to issue method call: %s", bus_error_message(&error));
2607 if (!dbus_message_get_args(reply, &error,
2608 DBUS_TYPE_OBJECT_PATH, &path,
2609 DBUS_TYPE_INVALID)) {
2610 log_error("Failed to parse reply: %s", bus_error_message(&error));
2615 if ((r = show_one(args[0], bus, path, show_properties, &new_line)) != 0)
2618 dbus_message_unref(m);
2619 dbus_message_unref(reply);
2625 dbus_message_unref(m);
2628 dbus_message_unref(reply);
2630 dbus_error_free(&error);
2635 static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
2637 DBusMessage *m = NULL, *reply = NULL;
2642 dbus_error_init(&error);
2644 log_debug("Got D-Bus request: %s.%s() on %s",
2645 dbus_message_get_interface(message),
2646 dbus_message_get_member(message),
2647 dbus_message_get_path(message));
2649 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
2650 log_error("Warning! D-Bus connection terminated.");
2651 dbus_connection_close(connection);
2653 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") ||
2654 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
2655 const char *id, *path;
2657 if (!dbus_message_get_args(message, &error,
2658 DBUS_TYPE_STRING, &id,
2659 DBUS_TYPE_OBJECT_PATH, &path,
2661 log_error("Failed to parse message: %s", bus_error_message(&error));
2662 else if (streq(dbus_message_get_member(message), "UnitNew"))
2663 printf("Unit %s added.\n", id);
2665 printf("Unit %s removed.\n", id);
2667 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew") ||
2668 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
2672 if (!dbus_message_get_args(message, &error,
2673 DBUS_TYPE_UINT32, &id,
2674 DBUS_TYPE_OBJECT_PATH, &path,
2676 log_error("Failed to parse message: %s", bus_error_message(&error));
2677 else if (streq(dbus_message_get_member(message), "JobNew"))
2678 printf("Job %u added.\n", id);
2680 printf("Job %u removed.\n", id);
2683 } else if (dbus_message_is_signal(message, "org.freedesktop.DBus.Properties", "PropertiesChanged")) {
2685 const char *path, *interface, *property = "Id";
2686 DBusMessageIter iter, sub;
2688 path = dbus_message_get_path(message);
2690 if (!dbus_message_get_args(message, &error,
2691 DBUS_TYPE_STRING, &interface,
2692 DBUS_TYPE_INVALID)) {
2693 log_error("Failed to parse message: %s", bus_error_message(&error));
2697 if (!streq(interface, "org.freedesktop.systemd1.Job") &&
2698 !streq(interface, "org.freedesktop.systemd1.Unit"))
2701 if (!(m = dbus_message_new_method_call(
2702 "org.freedesktop.systemd1",
2704 "org.freedesktop.DBus.Properties",
2706 log_error("Could not allocate message.");
2710 if (!dbus_message_append_args(m,
2711 DBUS_TYPE_STRING, &interface,
2712 DBUS_TYPE_STRING, &property,
2713 DBUS_TYPE_INVALID)) {
2714 log_error("Could not append arguments to message.");
2718 if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
2719 log_error("Failed to issue method call: %s", bus_error_message(&error));
2723 if (!dbus_message_iter_init(reply, &iter) ||
2724 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2725 log_error("Failed to parse reply.");
2729 dbus_message_iter_recurse(&iter, &sub);
2731 if (streq(interface, "org.freedesktop.systemd1.Unit")) {
2734 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2735 log_error("Failed to parse reply.");
2739 dbus_message_iter_get_basic(&sub, &id);
2740 printf("Unit %s changed.\n", id);
2744 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32) {
2745 log_error("Failed to parse reply.");
2749 dbus_message_iter_get_basic(&sub, &id);
2750 printf("Job %u changed.\n", id);
2756 dbus_message_unref(m);
2759 dbus_message_unref(reply);
2761 dbus_error_free(&error);
2762 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2766 dbus_message_unref(m);
2769 dbus_message_unref(reply);
2771 dbus_error_free(&error);
2772 return DBUS_HANDLER_RESULT_NEED_MEMORY;
2775 static int monitor(DBusConnection *bus, char **args, unsigned n) {
2776 DBusMessage *m = NULL, *reply = NULL;
2780 dbus_error_init(&error);
2783 dbus_bus_add_match(bus,
2785 "sender='org.freedesktop.systemd1',"
2786 "interface='org.freedesktop.systemd1.Manager',"
2787 "path='/org/freedesktop/systemd1'",
2790 if (dbus_error_is_set(&error)) {
2791 log_error("Failed to add match: %s", bus_error_message(&error));
2796 dbus_bus_add_match(bus,
2798 "sender='org.freedesktop.systemd1',"
2799 "interface='org.freedesktop.DBus.Properties',"
2800 "member='PropertiesChanged'",
2803 if (dbus_error_is_set(&error)) {
2804 log_error("Failed to add match: %s", bus_error_message(&error));
2810 if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
2811 log_error("Failed to add filter.");
2816 if (!(m = dbus_message_new_method_call(
2817 "org.freedesktop.systemd1",
2818 "/org/freedesktop/systemd1",
2819 "org.freedesktop.systemd1.Manager",
2821 log_error("Could not allocate message.");
2826 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2827 log_error("Failed to issue method call: %s", bus_error_message(&error));
2832 while (dbus_connection_read_write_dispatch(bus, -1))
2839 /* This is slightly dirty, since we don't undo the filter or the matches. */
2842 dbus_message_unref(m);
2845 dbus_message_unref(reply);
2847 dbus_error_free(&error);
2852 static int dump(DBusConnection *bus, char **args, unsigned n) {
2853 DBusMessage *m = NULL, *reply = NULL;
2858 dbus_error_init(&error);
2860 if (!(m = dbus_message_new_method_call(
2861 "org.freedesktop.systemd1",
2862 "/org/freedesktop/systemd1",
2863 "org.freedesktop.systemd1.Manager",
2865 log_error("Could not allocate message.");
2869 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2870 log_error("Failed to issue method call: %s", bus_error_message(&error));
2875 if (!dbus_message_get_args(reply, &error,
2876 DBUS_TYPE_STRING, &text,
2877 DBUS_TYPE_INVALID)) {
2878 log_error("Failed to parse reply: %s", bus_error_message(&error));
2883 fputs(text, stdout);
2889 dbus_message_unref(m);
2892 dbus_message_unref(reply);
2894 dbus_error_free(&error);
2899 static int snapshot(DBusConnection *bus, char **args, unsigned n) {
2900 DBusMessage *m = NULL, *reply = NULL;
2903 const char *name = "", *path, *id;
2904 dbus_bool_t cleanup = FALSE;
2905 DBusMessageIter iter, sub;
2907 *interface = "org.freedesktop.systemd1.Unit",
2910 dbus_error_init(&error);
2912 if (!(m = dbus_message_new_method_call(
2913 "org.freedesktop.systemd1",
2914 "/org/freedesktop/systemd1",
2915 "org.freedesktop.systemd1.Manager",
2916 "CreateSnapshot"))) {
2917 log_error("Could not allocate message.");
2924 if (!dbus_message_append_args(m,
2925 DBUS_TYPE_STRING, &name,
2926 DBUS_TYPE_BOOLEAN, &cleanup,
2927 DBUS_TYPE_INVALID)) {
2928 log_error("Could not append arguments to message.");
2933 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2934 log_error("Failed to issue method call: %s", bus_error_message(&error));
2939 if (!dbus_message_get_args(reply, &error,
2940 DBUS_TYPE_OBJECT_PATH, &path,
2941 DBUS_TYPE_INVALID)) {
2942 log_error("Failed to parse reply: %s", bus_error_message(&error));
2947 dbus_message_unref(m);
2948 if (!(m = dbus_message_new_method_call(
2949 "org.freedesktop.systemd1",
2951 "org.freedesktop.DBus.Properties",
2953 log_error("Could not allocate message.");
2957 if (!dbus_message_append_args(m,
2958 DBUS_TYPE_STRING, &interface,
2959 DBUS_TYPE_STRING, &property,
2960 DBUS_TYPE_INVALID)) {
2961 log_error("Could not append arguments to message.");
2966 dbus_message_unref(reply);
2967 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2968 log_error("Failed to issue method call: %s", bus_error_message(&error));
2973 if (!dbus_message_iter_init(reply, &iter) ||
2974 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2975 log_error("Failed to parse reply.");
2980 dbus_message_iter_recurse(&iter, &sub);
2982 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2983 log_error("Failed to parse reply.");
2988 dbus_message_iter_get_basic(&sub, &id);
2996 dbus_message_unref(m);
2999 dbus_message_unref(reply);
3001 dbus_error_free(&error);
3006 static int delete_snapshot(DBusConnection *bus, char **args, unsigned n) {
3007 DBusMessage *m = NULL, *reply = NULL;
3015 dbus_error_init(&error);
3017 for (i = 1; i < n; i++) {
3018 const char *path = NULL;
3020 if (!(m = dbus_message_new_method_call(
3021 "org.freedesktop.systemd1",
3022 "/org/freedesktop/systemd1",
3023 "org.freedesktop.systemd1.Manager",
3025 log_error("Could not allocate message.");
3030 if (!dbus_message_append_args(m,
3031 DBUS_TYPE_STRING, &args[i],
3032 DBUS_TYPE_INVALID)) {
3033 log_error("Could not append arguments to message.");
3038 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3039 log_error("Failed to issue method call: %s", bus_error_message(&error));
3044 if (!dbus_message_get_args(reply, &error,
3045 DBUS_TYPE_OBJECT_PATH, &path,
3046 DBUS_TYPE_INVALID)) {
3047 log_error("Failed to parse reply: %s", bus_error_message(&error));
3052 dbus_message_unref(m);
3053 if (!(m = dbus_message_new_method_call(
3054 "org.freedesktop.systemd1",
3056 "org.freedesktop.systemd1.Snapshot",
3058 log_error("Could not allocate message.");
3063 dbus_message_unref(reply);
3064 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3065 log_error("Failed to issue method call: %s", bus_error_message(&error));
3070 dbus_message_unref(m);
3071 dbus_message_unref(reply);
3079 dbus_message_unref(m);
3082 dbus_message_unref(reply);
3084 dbus_error_free(&error);
3089 static int daemon_reload(DBusConnection *bus, char **args, unsigned n) {
3090 DBusMessage *m = NULL, *reply = NULL;
3095 dbus_error_init(&error);
3097 if (arg_action == ACTION_RELOAD)
3099 else if (arg_action == ACTION_REEXEC)
3100 method = "Reexecute";
3102 assert(arg_action == ACTION_SYSTEMCTL);
3105 streq(args[0], "clear-jobs") ||
3106 streq(args[0], "cancel") ? "ClearJobs" :
3107 streq(args[0], "daemon-reexec") ? "Reexecute" :
3108 streq(args[0], "reset-failed") ? "ResetFailed" :
3109 streq(args[0], "halt") ? "Halt" :
3110 streq(args[0], "poweroff") ? "PowerOff" :
3111 streq(args[0], "reboot") ? "Reboot" :
3112 streq(args[0], "kexec") ? "KExec" :
3113 streq(args[0], "exit") ? "Exit" :
3114 /* "daemon-reload" */ "Reload";
3117 if (!(m = dbus_message_new_method_call(
3118 "org.freedesktop.systemd1",
3119 "/org/freedesktop/systemd1",
3120 "org.freedesktop.systemd1.Manager",
3122 log_error("Could not allocate message.");
3126 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3128 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
3129 /* There's always a fallback possible for
3130 * legacy actions. */
3135 log_error("Failed to issue method call: %s", bus_error_message(&error));
3144 dbus_message_unref(m);
3147 dbus_message_unref(reply);
3149 dbus_error_free(&error);
3154 static int reset_failed(DBusConnection *bus, char **args, unsigned n) {
3155 DBusMessage *m = NULL, *reply = NULL;
3161 dbus_error_init(&error);
3164 return daemon_reload(bus, args, n);
3166 for (i = 1; i < n; i++) {
3168 if (!(m = dbus_message_new_method_call(
3169 "org.freedesktop.systemd1",
3170 "/org/freedesktop/systemd1",
3171 "org.freedesktop.systemd1.Manager",
3172 "ResetFailedUnit"))) {
3173 log_error("Could not allocate message.");
3178 if (!dbus_message_append_args(m,
3179 DBUS_TYPE_STRING, args + i,
3180 DBUS_TYPE_INVALID)) {
3181 log_error("Could not append arguments to message.");
3186 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3187 log_error("Failed to issue method call: %s", bus_error_message(&error));
3192 dbus_message_unref(m);
3193 dbus_message_unref(reply);
3201 dbus_message_unref(m);
3204 dbus_message_unref(reply);
3206 dbus_error_free(&error);
3211 static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
3212 DBusMessage *m = NULL, *reply = NULL;
3214 DBusMessageIter iter, sub, sub2;
3217 *interface = "org.freedesktop.systemd1.Manager",
3218 *property = "Environment";
3220 dbus_error_init(&error);
3222 if (!(m = dbus_message_new_method_call(
3223 "org.freedesktop.systemd1",
3224 "/org/freedesktop/systemd1",
3225 "org.freedesktop.DBus.Properties",
3227 log_error("Could not allocate message.");
3231 if (!dbus_message_append_args(m,
3232 DBUS_TYPE_STRING, &interface,
3233 DBUS_TYPE_STRING, &property,
3234 DBUS_TYPE_INVALID)) {
3235 log_error("Could not append arguments to message.");
3240 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3241 log_error("Failed to issue method call: %s", bus_error_message(&error));
3246 if (!dbus_message_iter_init(reply, &iter) ||
3247 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
3248 log_error("Failed to parse reply.");
3253 dbus_message_iter_recurse(&iter, &sub);
3255 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
3256 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
3257 log_error("Failed to parse reply.");
3262 dbus_message_iter_recurse(&sub, &sub2);
3264 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
3267 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
3268 log_error("Failed to parse reply.");
3273 dbus_message_iter_get_basic(&sub2, &text);
3274 printf("%s\n", text);
3276 dbus_message_iter_next(&sub2);
3283 dbus_message_unref(m);
3286 dbus_message_unref(reply);
3288 dbus_error_free(&error);
3293 static int set_environment(DBusConnection *bus, char **args, unsigned n) {
3294 DBusMessage *m = NULL, *reply = NULL;
3298 DBusMessageIter iter, sub;
3301 dbus_error_init(&error);
3303 method = streq(args[0], "set-environment")
3305 : "UnsetEnvironment";
3307 if (!(m = dbus_message_new_method_call(
3308 "org.freedesktop.systemd1",
3309 "/org/freedesktop/systemd1",
3310 "org.freedesktop.systemd1.Manager",
3313 log_error("Could not allocate message.");
3317 dbus_message_iter_init_append(m, &iter);
3319 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
3320 log_error("Could not append arguments to message.");
3325 for (i = 1; i < n; i++)
3326 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) {
3327 log_error("Could not append arguments to message.");
3332 if (!dbus_message_iter_close_container(&iter, &sub)) {
3333 log_error("Could not append arguments to message.");
3338 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3339 log_error("Failed to issue method call: %s", bus_error_message(&error));
3348 dbus_message_unref(m);
3351 dbus_message_unref(reply);
3353 dbus_error_free(&error);
3366 static Hashmap *will_install = NULL, *have_installed = NULL;
3367 static Set *remove_symlinks_to = NULL;
3368 static unsigned n_symlinks = 0;
3370 static void install_info_free(InstallInfo *i) {
3375 strv_free(i->aliases);
3376 strv_free(i->wanted_by);
3380 static void install_info_hashmap_free(Hashmap *m) {
3383 while ((i = hashmap_steal_first(m)))
3384 install_info_free(i);
3389 static int install_info_add(const char *name) {
3393 assert(will_install);
3395 if (!unit_name_is_valid_no_type(name, true)) {
3396 log_warning("Unit name %s is not a valid unit name.", name);
3400 if (hashmap_get(have_installed, name) ||
3401 hashmap_get(will_install, name))
3404 if (!(i = new0(InstallInfo, 1))) {
3409 if (!(i->name = strdup(name))) {
3414 if ((r = hashmap_put(will_install, i->name, i)) < 0)
3421 install_info_free(i);
3426 static int config_parse_also(
3427 const char *filename,
3429 const char *section,
3443 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
3447 if (!(n = strndup(w, l)))
3450 if ((r = install_info_add(n)) < 0) {
3451 log_warning("Cannot install unit %s: %s", n, strerror(-r));
3462 static int mark_symlink_for_removal(const char *p) {
3467 assert(path_is_absolute(p));
3469 if (!remove_symlinks_to)
3472 if (!(n = strdup(p)))
3475 path_kill_slashes(n);
3477 if ((r = set_put(remove_symlinks_to, n)) < 0) {
3479 return r == -EEXIST ? 0 : r;
3485 static int remove_marked_symlinks_fd(int fd, const char *config_path, const char *root, bool *deleted) {
3494 if (!(d = fdopendir(fd))) {
3495 close_nointr_nofail(fd);
3501 while ((de = readdir(d))) {
3502 bool is_dir = false, is_link = false;
3504 if (ignore_file(de->d_name))
3507 if (de->d_type == DT_LNK)
3509 else if (de->d_type == DT_DIR)
3511 else if (de->d_type == DT_UNKNOWN) {
3514 if (fstatat(fd, de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) {
3515 log_error("Failed to stat %s/%s: %m", root, de->d_name);
3522 is_link = S_ISLNK(st.st_mode);
3523 is_dir = S_ISDIR(st.st_mode);
3531 if ((nfd = openat(fd, de->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW)) < 0) {
3532 log_error("Failed to open %s/%s: %m", root, de->d_name);
3539 if (asprintf(&p, "%s/%s", root, de->d_name) < 0) {
3540 log_error("Failed to allocate directory string.");
3541 close_nointr_nofail(nfd);
3546 /* This will close nfd, regardless whether it succeeds or not */
3547 q = remove_marked_symlinks_fd(nfd, config_path, p, deleted);
3553 } else if (is_link) {
3557 if (asprintf(&p, "%s/%s", root, de->d_name) < 0) {
3558 log_error("Failed to allocate symlink string.");
3563 if ((q = readlink_and_make_absolute(p, &dest)) < 0) {
3564 log_error("Cannot read symlink %s: %s", p, strerror(-q));
3572 if ((c = canonicalize_file_name(dest))) {
3573 /* This might fail if the destination
3574 * is already removed */
3580 path_kill_slashes(dest);
3581 if (set_get(remove_symlinks_to, dest)) {
3584 log_info("rm '%s'", p);
3586 if (unlink(p) < 0) {
3587 log_error("Cannot unlink symlink %s: %m", p);
3592 rmdir_parents(p, config_path);
3593 path_kill_slashes(p);
3595 if (!set_get(remove_symlinks_to, p)) {
3597 if ((r = mark_symlink_for_removal(p)) < 0) {
3616 static int remove_marked_symlinks(const char *config_path) {
3620 assert(config_path);
3622 if (set_size(remove_symlinks_to) <= 0)
3625 if ((fd = open(config_path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW)) < 0)
3632 if ((cfd = dup(fd)) < 0) {
3637 /* This takes possession of cfd and closes it */
3638 if ((q = remove_marked_symlinks_fd(cfd, config_path, config_path, &deleted)) < 0) {
3644 close_nointr_nofail(fd);
3649 static int create_symlink(const char *verb, const char *old_path, const char *new_path) {
3656 if (streq(verb, "enable")) {
3659 mkdir_parents(new_path, 0755);
3661 if (symlink(old_path, new_path) >= 0) {
3664 log_info("ln -s '%s' '%s'", old_path, new_path);
3669 if (errno != EEXIST) {
3670 log_error("Cannot link %s to %s: %m", old_path, new_path);
3674 if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
3676 if (errno == EINVAL) {
3677 log_error("Cannot link %s to %s, file exists already and is not a symlink.", old_path, new_path);
3681 log_error("readlink() failed: %s", strerror(-r));
3685 if (streq(dest, old_path)) {
3691 log_error("Cannot link %s to %s, symlink exists already and points to %s.", old_path, new_path, dest);
3700 log_info("ln -s '%s' '%s'", old_path, new_path);
3702 if (symlink(old_path, new_path) >= 0)
3705 log_error("Cannot link %s to %s: %m", old_path, new_path);
3708 } else if (streq(verb, "disable")) {
3711 if ((r = mark_symlink_for_removal(old_path)) < 0)
3714 if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
3715 if (errno == ENOENT)
3718 if (errno == EINVAL) {
3719 log_warning("File %s not a symlink, ignoring.", old_path);
3723 log_error("readlink() failed: %s", strerror(-r));
3727 if (!streq(dest, old_path)) {
3728 log_warning("File %s not a symlink to %s but points to %s, ignoring.", new_path, old_path, dest);
3735 if ((r = mark_symlink_for_removal(new_path)) < 0)
3739 log_info("rm '%s'", new_path);
3741 if (unlink(new_path) >= 0)
3744 log_error("Cannot unlink %s: %m", new_path);
3747 } else if (streq(verb, "is-enabled")) {
3750 if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
3752 if (errno == ENOENT || errno == EINVAL)
3755 log_error("readlink() failed: %s", strerror(-r));
3759 if (streq(dest, old_path)) {
3767 assert_not_reached("Unknown action.");
3770 static int install_info_symlink_alias(const char *verb, InstallInfo *i, const char *config_path) {
3772 char *alias_path = NULL;
3777 assert(config_path);
3779 STRV_FOREACH(s, i->aliases) {
3782 if (!(alias_path = path_make_absolute(*s, config_path))) {
3783 log_error("Out of memory");
3788 if ((r = create_symlink(verb, i->path, alias_path)) != 0)
3791 if (streq(verb, "disable"))
3792 rmdir_parents(alias_path, config_path);
3802 static int install_info_symlink_wants(const char *verb, InstallInfo *i, const char *config_path) {
3804 char *alias_path = NULL;
3809 assert(config_path);
3811 STRV_FOREACH(s, i->wanted_by) {
3812 if (!unit_name_is_valid_no_type(*s, true)) {
3813 log_error("Invalid name %s.", *s);
3821 if (asprintf(&alias_path, "%s/%s.wants/%s", config_path, *s, i->name) < 0) {
3822 log_error("Out of memory");
3827 if ((r = create_symlink(verb, i->path, alias_path)) != 0)
3830 if (streq(verb, "disable"))
3831 rmdir_parents(alias_path, config_path);
3842 static int install_info_apply(const char *verb, LookupPaths *paths, InstallInfo *i, const char *config_path) {
3844 const ConfigItem items[] = {
3845 { "Alias", config_parse_strv, &i->aliases, "Install" },
3846 { "WantedBy", config_parse_strv, &i->wanted_by, "Install" },
3847 { "Also", config_parse_also, NULL, "Install" },
3849 { NULL, NULL, NULL, NULL }
3853 char *filename = NULL;
3860 STRV_FOREACH(p, paths->unit_path) {
3863 if (!(filename = path_make_absolute(i->name, *p))) {
3864 log_error("Out of memory");
3868 /* Ensure that we don't follow symlinks */
3869 if ((fd = open(filename, O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NOCTTY)) >= 0)
3870 if ((f = fdopen(fd, "re")))
3873 if (errno == ELOOP) {
3874 log_error("Refusing to operate on symlinks, please pass unit names or absolute paths to unit files.");
3879 if (errno != ENOENT) {
3880 log_error("Failed to open %s: %m", filename);
3890 #if defined(TARGET_FEDORA) && defined (HAVE_SYSV_COMPAT)
3892 if (endswith(i->name, ".service")) {
3896 if (asprintf(&sysv, SYSTEM_SYSVINIT_PATH "/%s", i->name) < 0) {
3897 log_error("Out of memory");
3901 sysv[strlen(sysv) - sizeof(".service") + 1] = 0;
3902 exists = access(sysv, F_OK) >= 0;
3908 const char *argv[] = {
3915 log_info("%s is not a native service, redirecting to /sbin/chkconfig.", i->name);
3917 argv[1] = file_name_from_path(sysv);
3919 streq(verb, "enable") ? "on" :
3920 streq(verb, "disable") ? "off" : NULL;
3922 log_info("Executing %s %s %s", argv[0], argv[1], strempty(argv[2]));
3924 if ((pid = fork()) < 0) {
3925 log_error("Failed to fork: %m");
3928 } else if (pid == 0) {
3929 execv(argv[0], (char**) argv);
3930 _exit(EXIT_FAILURE);
3935 if ((r = wait_for_terminate(pid, &status)) < 0)
3938 if (status.si_code == CLD_EXITED) {
3939 if (status.si_status == 0 && (streq(verb, "enable") || streq(verb, "disable")))
3942 return status.si_status == 0 ? 0 : -EINVAL;
3952 log_error("Couldn't find %s.", i->name);
3958 if ((r = config_parse(filename, f, NULL, items, true, i)) < 0) {
3963 n_symlinks += strv_length(i->aliases);
3964 n_symlinks += strv_length(i->wanted_by);
3968 if ((r = install_info_symlink_alias(verb, i, config_path)) != 0)
3971 if ((r = install_info_symlink_wants(verb, i, config_path)) != 0)
3974 if ((r = mark_symlink_for_removal(filename)) < 0)
3977 if ((r = remove_marked_symlinks(config_path)) < 0)
3983 static char *get_config_path(void) {
3985 if (arg_user && arg_global)
3986 return strdup(USER_CONFIG_UNIT_PATH);
3991 if (user_config_home(&p) < 0)
3997 return strdup(SYSTEM_CONFIG_UNIT_PATH);
4000 static int enable_unit(DBusConnection *bus, char **args, unsigned n) {
4004 char *config_path = NULL;
4007 const char *verb = args[0];
4009 dbus_error_init(&error);
4012 if ((r = lookup_paths_init(&paths, arg_user ? MANAGER_USER : MANAGER_SYSTEM)) < 0) {
4013 log_error("Failed to determine lookup paths: %s", strerror(-r));
4017 if (!(config_path = get_config_path())) {
4018 log_error("Failed to determine config path");
4023 will_install = hashmap_new(string_hash_func, string_compare_func);
4024 have_installed = hashmap_new(string_hash_func, string_compare_func);
4026 if (!will_install || !have_installed) {
4027 log_error("Failed to allocate unit sets.");
4032 if (!arg_defaults && streq(verb, "disable"))
4033 if (!(remove_symlinks_to = set_new(string_hash_func, string_compare_func))) {
4034 log_error("Failed to allocate symlink sets.");
4039 for (j = 1; j < n; j++)
4040 if ((r = install_info_add(args[j])) < 0) {
4041 log_warning("Cannot install unit %s: %s", args[j], strerror(-r));
4045 while ((i = hashmap_first(will_install))) {
4048 assert_se(hashmap_move_one(have_installed, will_install, i->name) == 0);
4050 if ((q = install_info_apply(verb, &paths, i, config_path)) != 0) {
4058 /* In test mode and found something */
4064 if (streq(verb, "is-enabled"))
4065 r = r > 0 ? 0 : -ENOENT;
4067 if (n_symlinks <= 0)
4068 log_warning("Unit files contain no applicable installation information. Ignoring.");
4071 /* Don't try to reload anything if the user asked us to not do this */
4073 /* Don't try to reload anything when updating a unit globally */
4075 /* Don't try to reload anything if we are called for system changes but the system wasn't booted with systemd */
4076 (arg_user || sd_booted() > 0) &&
4077 /* Don't try to reload anything if we are running in a chroot environment */
4078 (arg_user || running_in_chroot() <= 0) ) {
4081 if ((q = daemon_reload(bus, args, n)) < 0)
4087 install_info_hashmap_free(will_install);
4088 install_info_hashmap_free(have_installed);
4090 set_free_free(remove_symlinks_to);
4092 lookup_paths_free(&paths);
4099 static int systemctl_help(void) {
4101 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
4102 "Send control commands to or query the systemd manager.\n\n"
4103 " -h --help Show this help\n"
4104 " --version Show package version\n"
4105 " -t --type=TYPE List only units of a particular type\n"
4106 " -p --property=NAME Show only properties by this name\n"
4107 " -a --all Show all units/properties, including dead/empty ones\n"
4108 " --full Don't ellipsize unit names on output\n"
4109 " --fail When queueing a new job, fail if conflicting jobs are\n"
4111 " -q --quiet Suppress output\n"
4112 " --no-block Do not wait until operation finished\n"
4113 " --system Connect to system manager\n"
4114 " --user Connect to user service manager\n"
4115 " --order When generating graph for dot, show only order\n"
4116 " --require When generating graph for dot, show only requirement\n"
4117 " --no-wall Don't send wall message before halt/power-off/reboot\n"
4118 " --global Enable/disable unit files globally\n"
4119 " --no-reload When enabling/disabling unit files, don't reload daemon\n"
4121 " --no-ask-password\n"
4122 " Do not ask for system passwords\n"
4123 " --kill-mode=MODE How to send signal\n"
4124 " --kill-who=WHO Who to send signal to\n"
4125 " -s --signal=SIGNAL Which signal to send\n"
4126 " -f --force When enabling unit files, override existing symlinks\n"
4127 " When shutting down, execute action immediately\n"
4128 " --defaults When disabling unit files, remove default symlinks only\n\n"
4130 " list-units List units\n"
4131 " start [NAME...] Start (activate) one or more units\n"
4132 " stop [NAME...] Stop (deactivate) one or more units\n"
4133 " reload [NAME...] Reload one or more units\n"
4134 " restart [NAME...] Start or restart one or more units\n"
4135 " try-restart [NAME...] Restart one or more units if active\n"
4136 " reload-or-restart [NAME...] Reload one or more units is possible,\n"
4137 " otherwise start or restart\n"
4138 " reload-or-try-restart [NAME...] Reload one or more units is possible,\n"
4139 " otherwise restart if active\n"
4140 " isolate [NAME] Start one unit and stop all others\n"
4141 " kill [NAME...] Send signal to processes of a unit\n"
4142 " is-active [NAME...] Check whether units are active\n"
4143 " status [NAME...|PID...] Show runtime status of one or more units\n"
4144 " show [NAME...|JOB...] Show properties of one or more\n"
4145 " units/jobs or the manager\n"
4146 " reset-failed [NAME...] Reset failed state for all, one, or more\n"
4148 " enable [NAME...] Enable one or more unit files\n"
4149 " disable [NAME...] Disable one or more unit files\n"
4150 " is-enabled [NAME...] Check whether unit files are enabled\n"
4151 " load [NAME...] Load one or more units\n"
4152 " list-jobs List jobs\n"
4153 " cancel [JOB...] Cancel all, one, or more jobs\n"
4154 " monitor Monitor unit/job changes\n"
4155 " dump Dump server status\n"
4156 " dot Dump dependency graph for dot(1)\n"
4157 " snapshot [NAME] Create a snapshot\n"
4158 " delete [NAME...] Remove one or more snapshots\n"
4159 " daemon-reload Reload systemd manager configuration\n"
4160 " daemon-reexec Reexecute systemd manager\n"
4161 " show-environment Dump environment\n"
4162 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
4163 " unset-environment [NAME...] Unset one or more environment variables\n"
4164 " default Enter system default mode\n"
4165 " rescue Enter system rescue mode\n"
4166 " emergency Enter system emergency mode\n"
4167 " halt Shut down and halt the system\n"
4168 " poweroff Shut down and power-off the system\n"
4169 " reboot Shut down and reboot the system\n"
4170 " kexec Shut down and reboot the system with kexec\n"
4171 " exit Ask for user instance termination\n",
4172 program_invocation_short_name);
4177 static int halt_help(void) {
4179 printf("%s [OPTIONS...]\n\n"
4180 "%s the system.\n\n"
4181 " --help Show this help\n"
4182 " --halt Halt the machine\n"
4183 " -p --poweroff Switch off the machine\n"
4184 " --reboot Reboot the machine\n"
4185 " -f --force Force immediate halt/power-off/reboot\n"
4186 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
4187 " -d --no-wtmp Don't write wtmp record\n"
4188 " -n --no-sync Don't sync before halt/power-off/reboot\n"
4189 " --no-wall Don't send wall message before halt/power-off/reboot\n",
4190 program_invocation_short_name,
4191 arg_action == ACTION_REBOOT ? "Reboot" :
4192 arg_action == ACTION_POWEROFF ? "Power off" :
4198 static int shutdown_help(void) {
4200 printf("%s [OPTIONS...] [TIME] [WALL...]\n\n"
4201 "Shut down the system.\n\n"
4202 " --help Show this help\n"
4203 " -H --halt Halt the machine\n"
4204 " -P --poweroff Power-off the machine\n"
4205 " -r --reboot Reboot the machine\n"
4206 " -h Equivalent to --poweroff, overriden by --halt\n"
4207 " -k Don't halt/power-off/reboot, just send warnings\n"
4208 " --no-wall Don't send wall message before halt/power-off/reboot\n"
4209 " -c Cancel a pending shutdown\n",
4210 program_invocation_short_name);
4215 static int telinit_help(void) {
4217 printf("%s [OPTIONS...] {COMMAND}\n\n"
4218 "Send control commands to the init daemon.\n\n"
4219 " --help Show this help\n"
4220 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
4222 " 0 Power-off the machine\n"
4223 " 6 Reboot the machine\n"
4224 " 2, 3, 4, 5 Start runlevelX.target unit\n"
4225 " 1, s, S Enter rescue mode\n"
4226 " q, Q Reload init daemon configuration\n"
4227 " u, U Reexecute init daemon\n",
4228 program_invocation_short_name);
4233 static int runlevel_help(void) {
4235 printf("%s [OPTIONS...]\n\n"
4236 "Prints the previous and current runlevel of the init system.\n\n"
4237 " --help Show this help\n",
4238 program_invocation_short_name);
4243 static int systemctl_parse_argv(int argc, char *argv[]) {
4263 static const struct option options[] = {
4264 { "help", no_argument, NULL, 'h' },
4265 { "version", no_argument, NULL, ARG_VERSION },
4266 { "type", required_argument, NULL, 't' },
4267 { "property", required_argument, NULL, 'p' },
4268 { "all", no_argument, NULL, 'a' },
4269 { "full", no_argument, NULL, ARG_FULL },
4270 { "fail", no_argument, NULL, ARG_FAIL },
4271 { "user", no_argument, NULL, ARG_USER },
4272 { "system", no_argument, NULL, ARG_SYSTEM },
4273 { "global", no_argument, NULL, ARG_GLOBAL },
4274 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
4275 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4276 { "quiet", no_argument, NULL, 'q' },
4277 { "order", no_argument, NULL, ARG_ORDER },
4278 { "require", no_argument, NULL, ARG_REQUIRE },
4279 { "force", no_argument, NULL, 'f' },
4280 { "no-reload", no_argument, NULL, ARG_NO_RELOAD },
4281 { "defaults", no_argument, NULL, ARG_DEFAULTS },
4282 { "kill-mode", required_argument, NULL, ARG_KILL_MODE },
4283 { "kill-who", required_argument, NULL, ARG_KILL_WHO },
4284 { "signal", required_argument, NULL, 's' },
4285 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
4286 { NULL, 0, NULL, 0 }
4294 /* Only when running as systemctl we ask for passwords */
4295 arg_ask_password = true;
4297 while ((c = getopt_long(argc, argv, "ht:p:aqfs:", options, NULL)) >= 0) {
4306 puts(PACKAGE_STRING);
4308 puts(SYSTEMD_FEATURES);
4318 if (!(l = strv_append(arg_property, optarg)))
4321 strv_free(arg_property);
4324 /* If the user asked for a particular
4325 * property, show it to him, even if it is
4348 arg_no_block = true;
4356 arg_dot = DOT_ORDER;
4360 arg_dot = DOT_REQUIRE;
4376 arg_no_reload = true;
4385 arg_defaults = true;
4389 arg_kill_who = optarg;
4393 arg_kill_mode = optarg;
4397 if ((arg_signal = signal_from_string_try_harder(optarg)) < 0) {
4398 log_error("Failed to parse signal string %s.", optarg);
4403 case ARG_NO_ASK_PASSWORD:
4404 arg_ask_password = false;
4411 log_error("Unknown option code %c", c);
4419 static int halt_parse_argv(int argc, char *argv[]) {
4428 static const struct option options[] = {
4429 { "help", no_argument, NULL, ARG_HELP },
4430 { "halt", no_argument, NULL, ARG_HALT },
4431 { "poweroff", no_argument, NULL, 'p' },
4432 { "reboot", no_argument, NULL, ARG_REBOOT },
4433 { "force", no_argument, NULL, 'f' },
4434 { "wtmp-only", no_argument, NULL, 'w' },
4435 { "no-wtmp", no_argument, NULL, 'd' },
4436 { "no-sync", no_argument, NULL, 'n' },
4437 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4438 { NULL, 0, NULL, 0 }
4446 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
4447 if (runlevel == '0' || runlevel == '6')
4448 arg_immediate = true;
4450 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
4458 arg_action = ACTION_HALT;
4462 if (arg_action != ACTION_REBOOT)
4463 arg_action = ACTION_POWEROFF;
4467 arg_action = ACTION_REBOOT;
4471 arg_immediate = true;
4492 /* Compatibility nops */
4499 log_error("Unknown option code %c", c);
4504 if (optind < argc) {
4505 log_error("Too many arguments.");
4512 static int parse_time_spec(const char *t, usec_t *_u) {
4516 if (streq(t, "now"))
4518 else if (t[0] == '+') {
4521 if (safe_atou64(t + 1, &u) < 0)
4524 *_u = now(CLOCK_REALTIME) + USEC_PER_MINUTE * u;
4533 hour = strtol(t, &e, 10);
4534 if (errno != 0 || *e != ':' || hour < 0 || hour > 23)
4537 minute = strtol(e+1, &e, 10);
4538 if (errno != 0 || *e != 0 || minute < 0 || minute > 59)
4541 n = now(CLOCK_REALTIME);
4542 s = (time_t) (n / USEC_PER_SEC);
4545 assert_se(localtime_r(&s, &tm));
4547 tm.tm_hour = (int) hour;
4548 tm.tm_min = (int) minute;
4551 assert_se(s = mktime(&tm));
4553 *_u = (usec_t) s * USEC_PER_SEC;
4556 *_u += USEC_PER_DAY;
4562 static int shutdown_parse_argv(int argc, char *argv[]) {
4569 static const struct option options[] = {
4570 { "help", no_argument, NULL, ARG_HELP },
4571 { "halt", no_argument, NULL, 'H' },
4572 { "poweroff", no_argument, NULL, 'P' },
4573 { "reboot", no_argument, NULL, 'r' },
4574 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4575 { NULL, 0, NULL, 0 }
4583 while ((c = getopt_long(argc, argv, "HPrhkt:afFc", options, NULL)) >= 0) {
4591 arg_action = ACTION_HALT;
4595 arg_action = ACTION_POWEROFF;
4599 arg_action = ACTION_REBOOT;
4603 if (arg_action != ACTION_HALT)
4604 arg_action = ACTION_POWEROFF;
4617 /* Compatibility nops */
4621 arg_action = ACTION_CANCEL_SHUTDOWN;
4628 log_error("Unknown option code %c", c);
4633 if (argc > optind) {
4634 if ((r = parse_time_spec(argv[optind], &arg_when)) < 0) {
4635 log_error("Failed to parse time specification: %s", argv[optind]);
4639 arg_when = now(CLOCK_REALTIME) + USEC_PER_MINUTE;
4641 /* We skip the time argument */
4642 if (argc > optind + 1)
4643 arg_wall = argv + optind + 1;
4650 static int telinit_parse_argv(int argc, char *argv[]) {
4657 static const struct option options[] = {
4658 { "help", no_argument, NULL, ARG_HELP },
4659 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4660 { NULL, 0, NULL, 0 }
4663 static const struct {
4667 { '0', ACTION_POWEROFF },
4668 { '6', ACTION_REBOOT },
4669 { '1', ACTION_RESCUE },
4670 { '2', ACTION_RUNLEVEL2 },
4671 { '3', ACTION_RUNLEVEL3 },
4672 { '4', ACTION_RUNLEVEL4 },
4673 { '5', ACTION_RUNLEVEL5 },
4674 { 's', ACTION_RESCUE },
4675 { 'S', ACTION_RESCUE },
4676 { 'q', ACTION_RELOAD },
4677 { 'Q', ACTION_RELOAD },
4678 { 'u', ACTION_REEXEC },
4679 { 'U', ACTION_REEXEC }
4688 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4703 log_error("Unknown option code %c", c);
4708 if (optind >= argc) {
4713 if (optind + 1 < argc) {
4714 log_error("Too many arguments.");
4718 if (strlen(argv[optind]) != 1) {
4719 log_error("Expected single character argument.");
4723 for (i = 0; i < ELEMENTSOF(table); i++)
4724 if (table[i].from == argv[optind][0])
4727 if (i >= ELEMENTSOF(table)) {
4728 log_error("Unknown command %s.", argv[optind]);
4732 arg_action = table[i].to;
4739 static int runlevel_parse_argv(int argc, char *argv[]) {
4745 static const struct option options[] = {
4746 { "help", no_argument, NULL, ARG_HELP },
4747 { NULL, 0, NULL, 0 }
4755 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4766 log_error("Unknown option code %c", c);
4771 if (optind < argc) {
4772 log_error("Too many arguments.");
4779 static int parse_argv(int argc, char *argv[]) {
4783 if (program_invocation_short_name) {
4785 if (strstr(program_invocation_short_name, "halt")) {
4786 arg_action = ACTION_HALT;
4787 return halt_parse_argv(argc, argv);
4788 } else if (strstr(program_invocation_short_name, "poweroff")) {
4789 arg_action = ACTION_POWEROFF;
4790 return halt_parse_argv(argc, argv);
4791 } else if (strstr(program_invocation_short_name, "reboot")) {
4792 arg_action = ACTION_REBOOT;
4793 return halt_parse_argv(argc, argv);
4794 } else if (strstr(program_invocation_short_name, "shutdown")) {
4795 arg_action = ACTION_POWEROFF;
4796 return shutdown_parse_argv(argc, argv);
4797 } else if (strstr(program_invocation_short_name, "init")) {
4799 if (sd_booted() > 0) {
4800 arg_action = ACTION_INVALID;
4801 return telinit_parse_argv(argc, argv);
4803 /* Hmm, so some other init system is
4804 * running, we need to forward this
4805 * request to it. For now we simply
4806 * guess that it is Upstart. */
4808 execv("/lib/upstart/telinit", argv);
4810 log_error("Couldn't find an alternative telinit implementation to spawn.");
4814 } else if (strstr(program_invocation_short_name, "runlevel")) {
4815 arg_action = ACTION_RUNLEVEL;
4816 return runlevel_parse_argv(argc, argv);
4820 arg_action = ACTION_SYSTEMCTL;
4821 return systemctl_parse_argv(argc, argv);
4824 static int action_to_runlevel(void) {
4826 static const char table[_ACTION_MAX] = {
4827 [ACTION_HALT] = '0',
4828 [ACTION_POWEROFF] = '0',
4829 [ACTION_REBOOT] = '6',
4830 [ACTION_RUNLEVEL2] = '2',
4831 [ACTION_RUNLEVEL3] = '3',
4832 [ACTION_RUNLEVEL4] = '4',
4833 [ACTION_RUNLEVEL5] = '5',
4834 [ACTION_RESCUE] = '1'
4837 assert(arg_action < _ACTION_MAX);
4839 return table[arg_action];
4842 static int talk_upstart(void) {
4843 DBusMessage *m = NULL, *reply = NULL;
4845 int previous, rl, r;
4847 env1_buf[] = "RUNLEVEL=X",
4848 env2_buf[] = "PREVLEVEL=X";
4849 char *env1 = env1_buf, *env2 = env2_buf;
4850 const char *emit = "runlevel";
4851 dbus_bool_t b_false = FALSE;
4852 DBusMessageIter iter, sub;
4853 DBusConnection *bus;
4855 dbus_error_init(&error);
4857 if (!(rl = action_to_runlevel()))
4860 if (utmp_get_runlevel(&previous, NULL) < 0)
4863 if (!(bus = dbus_connection_open_private("unix:abstract=/com/ubuntu/upstart", &error))) {
4864 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
4869 log_error("Failed to connect to Upstart bus: %s", bus_error_message(&error));
4874 if ((r = bus_check_peercred(bus)) < 0) {
4875 log_error("Failed to verify owner of bus.");
4879 if (!(m = dbus_message_new_method_call(
4880 "com.ubuntu.Upstart",
4881 "/com/ubuntu/Upstart",
4882 "com.ubuntu.Upstart0_6",
4885 log_error("Could not allocate message.");
4890 dbus_message_iter_init_append(m, &iter);
4892 env1_buf[sizeof(env1_buf)-2] = rl;
4893 env2_buf[sizeof(env2_buf)-2] = previous;
4895 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
4896 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
4897 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
4898 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
4899 !dbus_message_iter_close_container(&iter, &sub) ||
4900 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
4901 log_error("Could not append arguments to message.");
4906 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
4908 if (error_is_no_service(&error)) {
4913 log_error("Failed to issue method call: %s", bus_error_message(&error));
4922 dbus_message_unref(m);
4925 dbus_message_unref(reply);
4928 dbus_connection_flush(bus);
4929 dbus_connection_close(bus);
4930 dbus_connection_unref(bus);
4933 dbus_error_free(&error);
4938 static int talk_initctl(void) {
4939 struct init_request request;
4943 if (!(rl = action_to_runlevel()))
4947 request.magic = INIT_MAGIC;
4948 request.sleeptime = 0;
4949 request.cmd = INIT_CMD_RUNLVL;
4950 request.runlevel = rl;
4952 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
4954 if (errno == ENOENT)
4957 log_error("Failed to open "INIT_FIFO": %m");
4962 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
4963 close_nointr_nofail(fd);
4966 log_error("Failed to write to "INIT_FIFO": %m");
4967 return errno ? -errno : -EIO;
4973 static int systemctl_main(DBusConnection *bus, int argc, char *argv[], DBusError *error) {
4975 static const struct {
4983 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
4985 { "list-units", LESS, 1, list_units },
4986 { "list-jobs", EQUAL, 1, list_jobs },
4987 { "clear-jobs", EQUAL, 1, daemon_reload },
4988 { "load", MORE, 2, load_unit },
4989 { "cancel", MORE, 2, cancel_job },
4990 { "start", MORE, 2, start_unit },
4991 { "stop", MORE, 2, start_unit },
4992 { "reload", MORE, 2, start_unit },
4993 { "restart", MORE, 2, start_unit },
4994 { "try-restart", MORE, 2, start_unit },
4995 { "reload-or-restart", MORE, 2, start_unit },
4996 { "reload-or-try-restart", MORE, 2, start_unit },
4997 { "force-reload", MORE, 2, start_unit }, /* For compatibility with SysV */
4998 { "condrestart", MORE, 2, start_unit }, /* For compatibility with RH */
4999 { "isolate", EQUAL, 2, start_unit },
5000 { "kill", MORE, 2, kill_unit },
5001 { "is-active", MORE, 2, check_unit },
5002 { "check", MORE, 2, check_unit },
5003 { "show", MORE, 1, show },
5004 { "status", MORE, 2, show },
5005 { "monitor", EQUAL, 1, monitor },
5006 { "dump", EQUAL, 1, dump },
5007 { "dot", EQUAL, 1, dot },
5008 { "snapshot", LESS, 2, snapshot },
5009 { "delete", MORE, 2, delete_snapshot },
5010 { "daemon-reload", EQUAL, 1, daemon_reload },
5011 { "daemon-reexec", EQUAL, 1, daemon_reload },
5012 { "show-environment", EQUAL, 1, show_enviroment },
5013 { "set-environment", MORE, 2, set_environment },
5014 { "unset-environment", MORE, 2, set_environment },
5015 { "halt", EQUAL, 1, start_special },
5016 { "poweroff", EQUAL, 1, start_special },
5017 { "reboot", EQUAL, 1, start_special },
5018 { "kexec", EQUAL, 1, start_special },
5019 { "default", EQUAL, 1, start_special },
5020 { "rescue", EQUAL, 1, start_special },
5021 { "emergency", EQUAL, 1, start_special },
5022 { "exit", EQUAL, 1, start_special },
5023 { "reset-failed", MORE, 1, reset_failed },
5024 { "enable", MORE, 2, enable_unit },
5025 { "disable", MORE, 2, enable_unit },
5026 { "is-enabled", MORE, 2, enable_unit }
5036 left = argc - optind;
5039 /* Special rule: no arguments means "list-units" */
5042 if (streq(argv[optind], "help")) {
5047 for (i = 0; i < ELEMENTSOF(verbs); i++)
5048 if (streq(argv[optind], verbs[i].verb))
5051 if (i >= ELEMENTSOF(verbs)) {
5052 log_error("Unknown operation %s", argv[optind]);
5057 switch (verbs[i].argc_cmp) {
5060 if (left != verbs[i].argc) {
5061 log_error("Invalid number of arguments.");
5068 if (left < verbs[i].argc) {
5069 log_error("Too few arguments.");
5076 if (left > verbs[i].argc) {
5077 log_error("Too many arguments.");
5084 assert_not_reached("Unknown comparison operator.");
5087 /* Require a bus connection for all operations but
5089 if (!streq(verbs[i].verb, "enable") &&
5090 !streq(verbs[i].verb, "disable") &&
5092 log_error("Failed to get D-Bus connection: %s", error->message);
5096 return verbs[i].dispatch(bus, argv + optind, left);
5099 static int send_shutdownd(usec_t t, char mode, bool warn, const char *message) {
5101 struct msghdr msghdr;
5103 union sockaddr_union sockaddr;
5104 struct shutdownd_command c;
5112 strncpy(c.wall_message, message, sizeof(c.wall_message));
5114 if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0)
5118 sockaddr.sa.sa_family = AF_UNIX;
5119 sockaddr.un.sun_path[0] = 0;
5120 strncpy(sockaddr.un.sun_path+1, "/org/freedesktop/systemd1/shutdownd", sizeof(sockaddr.un.sun_path)-1);
5123 iovec.iov_base = (char*) &c;
5124 iovec.iov_len = sizeof(c);
5127 msghdr.msg_name = &sockaddr;
5128 msghdr.msg_namelen = offsetof(struct sockaddr_un, sun_path) + 1 + sizeof("/org/freedesktop/systemd1/shutdownd") - 1;
5130 msghdr.msg_iov = &iovec;
5131 msghdr.msg_iovlen = 1;
5133 if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) < 0) {
5134 close_nointr_nofail(fd);
5138 close_nointr_nofail(fd);
5142 static int reload_with_fallback(DBusConnection *bus) {
5145 /* First, try systemd via D-Bus. */
5146 if (daemon_reload(bus, NULL, 0) > 0)
5150 /* Nothing else worked, so let's try signals */
5151 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
5153 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
5154 log_error("kill() failed: %m");
5161 static int start_with_fallback(DBusConnection *bus) {
5164 /* First, try systemd via D-Bus. */
5165 if (start_unit(bus, NULL, 0) >= 0)
5169 /* Hmm, talking to systemd via D-Bus didn't work. Then
5170 * let's try to talk to Upstart via D-Bus. */
5171 if (talk_upstart() > 0)
5174 /* Nothing else worked, so let's try
5176 if (talk_initctl() > 0)
5179 log_error("Failed to talk to init daemon.");
5183 warn_wall(arg_action);
5187 static int halt_main(DBusConnection *bus) {
5190 if (geteuid() != 0) {
5191 log_error("Must be root.");
5197 char date[FORMAT_TIMESTAMP_MAX];
5199 m = strv_join(arg_wall, " ");
5200 r = send_shutdownd(arg_when,
5201 arg_action == ACTION_HALT ? 'H' :
5202 arg_action == ACTION_POWEROFF ? 'P' :
5209 log_warning("Failed to talk to shutdownd, proceeding with immediate shutdown: %s", strerror(-r));
5211 log_info("Shutdown scheduled for %s, use 'shutdown -c' to cancel.",
5212 format_timestamp(date, sizeof(date), arg_when));
5217 if (!arg_dry && !arg_immediate)
5218 return start_with_fallback(bus);
5221 if (sd_booted() > 0)
5222 log_debug("Not writing utmp record, assuming that systemd-update-utmp is used.");
5223 else if ((r = utmp_put_shutdown(0)) < 0)
5224 log_warning("Failed to write utmp record: %s", strerror(-r));
5233 /* Make sure C-A-D is handled by the kernel from this
5235 reboot(RB_ENABLE_CAD);
5237 switch (arg_action) {
5240 log_info("Halting.");
5241 reboot(RB_HALT_SYSTEM);
5244 case ACTION_POWEROFF:
5245 log_info("Powering off.");
5246 reboot(RB_POWER_OFF);
5250 log_info("Rebooting.");
5251 reboot(RB_AUTOBOOT);
5255 assert_not_reached("Unknown halt action.");
5258 /* We should never reach this. */
5262 static int runlevel_main(void) {
5263 int r, runlevel, previous;
5265 if ((r = utmp_get_runlevel(&runlevel, &previous)) < 0) {
5266 printf("unknown\n");
5271 previous <= 0 ? 'N' : previous,
5272 runlevel <= 0 ? 'N' : runlevel);
5277 int main(int argc, char*argv[]) {
5278 int r, retval = EXIT_FAILURE;
5279 DBusConnection *bus = NULL;
5282 dbus_error_init(&error);
5284 log_parse_environment();
5287 if ((r = parse_argv(argc, argv)) < 0)
5290 retval = EXIT_SUCCESS;
5294 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
5295 * let's shortcut this */
5296 if (arg_action == ACTION_RUNLEVEL) {
5297 r = runlevel_main();
5298 retval = r < 0 ? EXIT_FAILURE : r;
5302 bus_connect(arg_user ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &bus, &private_bus, &error);
5304 switch (arg_action) {
5306 case ACTION_SYSTEMCTL:
5307 r = systemctl_main(bus, argc, argv, &error);
5311 case ACTION_POWEROFF:
5316 case ACTION_RUNLEVEL2:
5317 case ACTION_RUNLEVEL3:
5318 case ACTION_RUNLEVEL4:
5319 case ACTION_RUNLEVEL5:
5321 case ACTION_EMERGENCY:
5322 case ACTION_DEFAULT:
5323 r = start_with_fallback(bus);
5328 r = reload_with_fallback(bus);
5331 case ACTION_CANCEL_SHUTDOWN:
5332 r = send_shutdownd(0, 0, false, NULL);
5335 case ACTION_INVALID:
5336 case ACTION_RUNLEVEL:
5338 assert_not_reached("Unknown action");
5341 retval = r < 0 ? EXIT_FAILURE : r;
5346 dbus_connection_flush(bus);
5347 dbus_connection_close(bus);
5348 dbus_connection_unref(bus);
5351 dbus_error_free(&error);
5355 strv_free(arg_property);