1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/reboot.h>
28 #include <sys/ioctl.h>
32 #include <sys/socket.h>
35 #include <sys/prctl.h>
36 #include <dbus/dbus.h>
38 #include <systemd/sd-daemon.h>
39 #include <systemd/sd-shutdown.h>
45 #include "utmp-wtmp.h"
49 #include "dbus-common.h"
50 #include "cgroup-show.h"
51 #include "cgroup-util.h"
53 #include "path-lookup.h"
54 #include "conf-parser.h"
55 #include "exit-status.h"
56 #include "bus-errors.h"
58 #include "unit-name.h"
60 #include "spawn-agent.h"
62 #include "logs-show.h"
64 static const char *arg_type = NULL;
65 static char **arg_property = NULL;
66 static bool arg_all = false;
67 static const char *arg_job_mode = "replace";
68 static UnitFileScope arg_scope = UNIT_FILE_SYSTEM;
69 static bool arg_immediate = false;
70 static bool arg_no_block = false;
71 static bool arg_no_legend = false;
72 static bool arg_no_pager = false;
73 static bool arg_no_wtmp = false;
74 static bool arg_no_sync = false;
75 static bool arg_no_wall = false;
76 static bool arg_no_reload = false;
77 static bool arg_dry = false;
78 static bool arg_quiet = false;
79 static bool arg_full = false;
80 static int arg_force = 0;
81 static bool arg_ask_password = false;
82 static bool arg_failed = false;
83 static bool arg_runtime = false;
84 static char **arg_wall = NULL;
85 static const char *arg_kill_who = NULL;
86 static const char *arg_kill_mode = NULL;
87 static int arg_signal = SIGTERM;
88 static const char *arg_root = NULL;
89 static usec_t arg_when = 0;
108 ACTION_CANCEL_SHUTDOWN,
110 } arg_action = ACTION_SYSTEMCTL;
116 static enum transport {
120 } arg_transport = TRANSPORT_NORMAL;
121 static const char *arg_host = NULL;
122 static bool arg_follow = false;
123 static unsigned arg_lines = 10;
124 static OutputMode arg_output = OUTPUT_SHORT;
126 static bool private_bus = false;
128 static int daemon_reload(DBusConnection *bus, char **args);
129 static void halt_now(enum action a);
131 static bool on_tty(void) {
134 /* Note that this is invoked relatively early, before we start
135 * the pager. That means the value we return reflects whether
136 * we originally were started on a tty, not if we currently
137 * are. But this is intended, since we want colour and so on
138 * when run in our own pager. */
140 if (_unlikely_(t < 0))
141 t = isatty(STDOUT_FILENO) > 0;
146 static void pager_open_if_enabled(void) {
148 /* Cache result before we open the pager */
157 static void agent_open_if_enabled(void) {
159 /* Open the password agent as a child process if necessary */
161 if (!arg_ask_password)
164 if (arg_scope != UNIT_FILE_SYSTEM)
170 static const char *ansi_highlight_red(bool b) {
175 return b ? ANSI_HIGHLIGHT_RED_ON : ANSI_HIGHLIGHT_OFF;
178 static const char *ansi_highlight_green(bool b) {
183 return b ? ANSI_HIGHLIGHT_GREEN_ON : ANSI_HIGHLIGHT_OFF;
186 static bool error_is_no_service(const DBusError *error) {
189 if (!dbus_error_is_set(error))
192 if (dbus_error_has_name(error, DBUS_ERROR_NAME_HAS_NO_OWNER))
195 if (dbus_error_has_name(error, DBUS_ERROR_SERVICE_UNKNOWN))
198 return startswith(error->name, "org.freedesktop.DBus.Error.Spawn.");
201 static int translate_bus_error_to_exit_status(int r, const DBusError *error) {
204 if (!dbus_error_is_set(error))
207 if (dbus_error_has_name(error, DBUS_ERROR_ACCESS_DENIED) ||
208 dbus_error_has_name(error, BUS_ERROR_ONLY_BY_DEPENDENCY) ||
209 dbus_error_has_name(error, BUS_ERROR_NO_ISOLATION) ||
210 dbus_error_has_name(error, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE))
211 return EXIT_NOPERMISSION;
213 if (dbus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT))
214 return EXIT_NOTINSTALLED;
216 if (dbus_error_has_name(error, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE) ||
217 dbus_error_has_name(error, BUS_ERROR_NOT_SUPPORTED))
218 return EXIT_NOTIMPLEMENTED;
220 if (dbus_error_has_name(error, BUS_ERROR_LOAD_FAILED))
221 return EXIT_NOTCONFIGURED;
229 static void warn_wall(enum action a) {
230 static const char *table[_ACTION_MAX] = {
231 [ACTION_HALT] = "The system is going down for system halt NOW!",
232 [ACTION_REBOOT] = "The system is going down for reboot NOW!",
233 [ACTION_POWEROFF] = "The system is going down for power-off NOW!",
234 [ACTION_KEXEC] = "The system is going down for kexec reboot NOW!",
235 [ACTION_RESCUE] = "The system is going down to rescue mode NOW!",
236 [ACTION_EMERGENCY] = "The system is going down to emergency mode NOW!"
245 p = strv_join(arg_wall, " ");
247 log_error("Failed to join strings.");
263 utmp_wall(table[a], NULL);
266 static bool avoid_bus(void) {
268 if (running_in_chroot() > 0)
271 if (sd_booted() <= 0)
274 if (!isempty(arg_root))
277 if (arg_scope == UNIT_FILE_GLOBAL)
285 const char *description;
286 const char *load_state;
287 const char *active_state;
288 const char *sub_state;
289 const char *following;
290 const char *unit_path;
292 const char *job_type;
293 const char *job_path;
296 static int compare_unit_info(const void *a, const void *b) {
298 const struct unit_info *u = a, *v = b;
300 d1 = strrchr(u->id, '.');
301 d2 = strrchr(v->id, '.');
306 if ((r = strcasecmp(d1, d2)) != 0)
310 return strcasecmp(u->id, v->id);
313 static bool output_show_unit(const struct unit_info *u) {
317 return streq(u->active_state, "failed");
319 return (!arg_type || ((dot = strrchr(u->id, '.')) &&
320 streq(dot+1, arg_type))) &&
321 (arg_all || !(streq(u->active_state, "inactive") || u->following[0]) || u->job_id > 0);
324 static void output_units_list(const struct unit_info *unit_infos, unsigned c) {
325 unsigned id_len, max_id_len, active_len, sub_len, job_len, desc_len, n_shown = 0;
326 const struct unit_info *u;
328 max_id_len = sizeof("UNIT")-1;
329 active_len = sizeof("ACTIVE")-1;
330 sub_len = sizeof("SUB")-1;
331 job_len = sizeof("JOB")-1;
334 for (u = unit_infos; u < unit_infos + c; u++) {
335 if (!output_show_unit(u))
338 max_id_len = MAX(max_id_len, strlen(u->id));
339 active_len = MAX(active_len, strlen(u->active_state));
340 sub_len = MAX(sub_len, strlen(u->sub_state));
342 job_len = MAX(job_len, strlen(u->job_type));
347 id_len = MIN(max_id_len, 25);
348 basic_len = 5 + id_len + 6 + active_len + sub_len + job_len;
349 if (basic_len < (unsigned) columns()) {
350 unsigned extra_len, incr;
351 extra_len = columns() - basic_len;
352 /* Either UNIT already got 25, or is fully satisfied.
353 * Grant up to 25 to DESC now. */
354 incr = MIN(extra_len, 25);
357 /* split the remaining space between UNIT and DESC,
358 * but do not give UNIT more than it needs. */
360 incr = MIN(extra_len / 2, max_id_len - id_len);
362 desc_len += extra_len - incr;
368 if (!arg_no_legend) {
369 printf("%-*s %-6s %-*s %-*s %-*s ", id_len, "UNIT", "LOAD",
370 active_len, "ACTIVE", sub_len, "SUB", job_len, "JOB");
371 if (!arg_full && arg_no_pager)
372 printf("%.*s\n", desc_len, "DESCRIPTION");
374 printf("%s\n", "DESCRIPTION");
377 for (u = unit_infos; u < unit_infos + c; u++) {
379 const char *on_loaded, *off_loaded;
380 const char *on_active, *off_active;
382 if (!output_show_unit(u))
387 if (streq(u->load_state, "error")) {
388 on_loaded = ansi_highlight_red(true);
389 off_loaded = ansi_highlight_red(false);
391 on_loaded = off_loaded = "";
393 if (streq(u->active_state, "failed")) {
394 on_active = ansi_highlight_red(true);
395 off_active = ansi_highlight_red(false);
397 on_active = off_active = "";
399 e = arg_full ? NULL : ellipsize(u->id, id_len, 33);
401 printf("%-*s %s%-6s%s %s%-*s %-*s%s %-*s ",
402 id_len, e ? e : u->id,
403 on_loaded, u->load_state, off_loaded,
404 on_active, active_len, u->active_state,
405 sub_len, u->sub_state, off_active,
406 job_len, u->job_id ? u->job_type : "");
407 if (!arg_full && arg_no_pager)
408 printf("%.*s\n", desc_len, u->description);
410 printf("%s\n", u->description);
415 if (!arg_no_legend) {
416 printf("\nLOAD = Reflects whether the unit definition was properly loaded.\n"
417 "ACTIVE = The high-level unit activation state, i.e. generalization of SUB.\n"
418 "SUB = The low-level unit activation state, values depend on unit type.\n"
419 "JOB = Pending job for the unit.\n");
422 printf("\n%u units listed.\n", n_shown);
424 printf("\n%u units listed. Pass --all to see inactive units, too.\n", n_shown);
428 static int list_units(DBusConnection *bus, char **args) {
429 DBusMessage *m = NULL, *reply = NULL;
432 DBusMessageIter iter, sub, sub2;
433 unsigned c = 0, n_units = 0;
434 struct unit_info *unit_infos = NULL;
436 dbus_error_init(&error);
440 pager_open_if_enabled();
442 if (!(m = dbus_message_new_method_call(
443 "org.freedesktop.systemd1",
444 "/org/freedesktop/systemd1",
445 "org.freedesktop.systemd1.Manager",
447 log_error("Could not allocate message.");
451 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
452 log_error("Failed to issue method call: %s", bus_error_message(&error));
457 if (!dbus_message_iter_init(reply, &iter) ||
458 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
459 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
460 log_error("Failed to parse reply.");
465 dbus_message_iter_recurse(&iter, &sub);
467 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
470 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
471 log_error("Failed to parse reply.");
479 n_units = MAX(2*c, 16);
480 w = realloc(unit_infos, sizeof(struct unit_info) * n_units);
483 log_error("Failed to allocate unit array.");
493 dbus_message_iter_recurse(&sub, &sub2);
495 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->id, true) < 0 ||
496 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->description, true) < 0 ||
497 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->load_state, true) < 0 ||
498 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->active_state, true) < 0 ||
499 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->sub_state, true) < 0 ||
500 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->following, true) < 0 ||
501 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &u->unit_path, true) < 0 ||
502 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &u->job_id, true) < 0 ||
503 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->job_type, true) < 0 ||
504 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &u->job_path, false) < 0) {
505 log_error("Failed to parse reply.");
510 dbus_message_iter_next(&sub);
515 qsort(unit_infos, c, sizeof(struct unit_info), compare_unit_info);
516 output_units_list(unit_infos, c);
523 dbus_message_unref(m);
526 dbus_message_unref(reply);
530 dbus_error_free(&error);
535 static int compare_unit_file_list(const void *a, const void *b) {
537 const UnitFileList *u = a, *v = b;
539 d1 = strrchr(u->path, '.');
540 d2 = strrchr(v->path, '.');
545 r = strcasecmp(d1, d2);
550 return strcasecmp(file_name_from_path(u->path), file_name_from_path(v->path));
553 static bool output_show_unit_file(const UnitFileList *u) {
556 return !arg_type || ((dot = strrchr(u->path, '.')) && streq(dot+1, arg_type));
559 static void output_unit_file_list(const UnitFileList *units, unsigned c) {
560 unsigned max_id_len, id_cols, state_cols, n_shown = 0;
561 const UnitFileList *u;
563 max_id_len = sizeof("UNIT FILE")-1;
564 state_cols = sizeof("STATE")-1;
565 for (u = units; u < units + c; u++) {
566 if (!output_show_unit_file(u))
569 max_id_len = MAX(max_id_len, strlen(file_name_from_path(u->path)));
570 state_cols = MAX(state_cols, strlen(unit_file_state_to_string(u->state)));
575 id_cols = MIN(max_id_len, 25);
576 basic_cols = 1 + id_cols + state_cols;
577 if (basic_cols < (unsigned) columns())
578 id_cols += MIN(columns() - basic_cols, max_id_len - id_cols);
580 id_cols = max_id_len;
583 printf("%-*s %-*s\n", id_cols, "UNIT FILE", state_cols, "STATE");
585 for (u = units; u < units + c; u++) {
587 const char *on, *off;
590 if (!output_show_unit_file(u))
595 if (u->state == UNIT_FILE_MASKED ||
596 u->state == UNIT_FILE_MASKED_RUNTIME ||
597 u->state == UNIT_FILE_DISABLED) {
598 on = ansi_highlight_red(true);
599 off = ansi_highlight_red(false);
600 } else if (u->state == UNIT_FILE_ENABLED) {
601 on = ansi_highlight_green(true);
602 off = ansi_highlight_green(false);
606 id = file_name_from_path(u->path);
608 e = arg_full ? NULL : ellipsize(id, id_cols, 33);
610 printf("%-*s %s%-*s%s\n",
612 on, state_cols, unit_file_state_to_string(u->state), off);
618 printf("\n%u unit files listed.\n", n_shown);
621 static int list_unit_files(DBusConnection *bus, char **args) {
622 DBusMessage *m = NULL, *reply = NULL;
625 DBusMessageIter iter, sub, sub2;
626 unsigned c = 0, n_units = 0;
627 UnitFileList *units = NULL;
629 dbus_error_init(&error);
631 pager_open_if_enabled();
638 h = hashmap_new(string_hash_func, string_compare_func);
640 log_error("Out of memory");
644 r = unit_file_get_list(arg_scope, arg_root, h);
646 unit_file_list_free(h);
647 log_error("Failed to get unit file list: %s", strerror(-r));
651 n_units = hashmap_size(h);
652 units = new(UnitFileList, n_units);
654 unit_file_list_free(h);
655 log_error("Out of memory");
659 HASHMAP_FOREACH(u, h, i) {
660 memcpy(units + c++, u, sizeof(UnitFileList));
668 m = dbus_message_new_method_call(
669 "org.freedesktop.systemd1",
670 "/org/freedesktop/systemd1",
671 "org.freedesktop.systemd1.Manager",
674 log_error("Could not allocate message.");
678 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
680 log_error("Failed to issue method call: %s", bus_error_message(&error));
685 if (!dbus_message_iter_init(reply, &iter) ||
686 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
687 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
688 log_error("Failed to parse reply.");
693 dbus_message_iter_recurse(&iter, &sub);
695 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
699 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
700 log_error("Failed to parse reply.");
708 n_units = MAX(2*c, 16);
709 w = realloc(units, sizeof(struct UnitFileList) * n_units);
712 log_error("Failed to allocate unit array.");
722 dbus_message_iter_recurse(&sub, &sub2);
724 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->path, true) < 0 ||
725 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, false) < 0) {
726 log_error("Failed to parse reply.");
731 u->state = unit_file_state_from_string(state);
733 dbus_message_iter_next(&sub);
739 qsort(units, c, sizeof(UnitFileList), compare_unit_file_list);
740 output_unit_file_list(units, c);
747 dbus_message_unref(m);
750 dbus_message_unref(reply);
754 dbus_error_free(&error);
759 static int dot_one_property(const char *name, const char *prop, DBusMessageIter *iter) {
760 static const char * const colors[] = {
761 "Requires", "[color=\"black\"]",
762 "RequiresOverridable", "[color=\"black\"]",
763 "Requisite", "[color=\"darkblue\"]",
764 "RequisiteOverridable", "[color=\"darkblue\"]",
765 "Wants", "[color=\"darkgrey\"]",
766 "Conflicts", "[color=\"red\"]",
767 "ConflictedBy", "[color=\"red\"]",
768 "After", "[color=\"green\"]"
771 const char *c = NULL;
778 for (i = 0; i < ELEMENTSOF(colors); i += 2)
779 if (streq(colors[i], prop)) {
787 if (arg_dot != DOT_ALL)
788 if ((arg_dot == DOT_ORDER) != streq(prop, "After"))
791 switch (dbus_message_iter_get_arg_type(iter)) {
793 case DBUS_TYPE_ARRAY:
795 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
798 dbus_message_iter_recurse(iter, &sub);
800 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
803 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
804 dbus_message_iter_get_basic(&sub, &s);
805 printf("\t\"%s\"->\"%s\" %s;\n", name, s, c);
807 dbus_message_iter_next(&sub);
817 static int dot_one(DBusConnection *bus, const char *name, const char *path) {
818 DBusMessage *m = NULL, *reply = NULL;
819 const char *interface = "org.freedesktop.systemd1.Unit";
822 DBusMessageIter iter, sub, sub2, sub3;
827 dbus_error_init(&error);
829 if (!(m = dbus_message_new_method_call(
830 "org.freedesktop.systemd1",
832 "org.freedesktop.DBus.Properties",
834 log_error("Could not allocate message.");
839 if (!dbus_message_append_args(m,
840 DBUS_TYPE_STRING, &interface,
841 DBUS_TYPE_INVALID)) {
842 log_error("Could not append arguments to message.");
847 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
848 log_error("Failed to issue method call: %s", bus_error_message(&error));
853 if (!dbus_message_iter_init(reply, &iter) ||
854 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
855 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
856 log_error("Failed to parse reply.");
861 dbus_message_iter_recurse(&iter, &sub);
863 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
866 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
867 log_error("Failed to parse reply.");
872 dbus_message_iter_recurse(&sub, &sub2);
874 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &prop, true) < 0) {
875 log_error("Failed to parse reply.");
880 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
881 log_error("Failed to parse reply.");
886 dbus_message_iter_recurse(&sub2, &sub3);
888 if (dot_one_property(name, prop, &sub3)) {
889 log_error("Failed to parse reply.");
894 dbus_message_iter_next(&sub);
901 dbus_message_unref(m);
904 dbus_message_unref(reply);
906 dbus_error_free(&error);
911 static int dot(DBusConnection *bus, char **args) {
912 DBusMessage *m = NULL, *reply = NULL;
915 DBusMessageIter iter, sub, sub2;
917 dbus_error_init(&error);
921 if (!(m = dbus_message_new_method_call(
922 "org.freedesktop.systemd1",
923 "/org/freedesktop/systemd1",
924 "org.freedesktop.systemd1.Manager",
926 log_error("Could not allocate message.");
930 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
931 log_error("Failed to issue method call: %s", bus_error_message(&error));
936 if (!dbus_message_iter_init(reply, &iter) ||
937 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
938 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
939 log_error("Failed to parse reply.");
944 printf("digraph systemd {\n");
946 dbus_message_iter_recurse(&iter, &sub);
947 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
948 const char *id, *description, *load_state, *active_state, *sub_state, *following, *unit_path;
950 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
951 log_error("Failed to parse reply.");
956 dbus_message_iter_recurse(&sub, &sub2);
958 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
959 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
960 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
961 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
962 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
963 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &following, true) < 0 ||
964 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, true) < 0) {
965 log_error("Failed to parse reply.");
970 if ((r = dot_one(bus, id, unit_path)) < 0)
973 /* printf("\t\"%s\";\n", id); */
974 dbus_message_iter_next(&sub);
979 log_info(" Color legend: black = Requires\n"
980 " dark blue = Requisite\n"
981 " dark grey = Wants\n"
986 log_notice("-- You probably want to process this output with graphviz' dot tool.\n"
987 "-- Try a shell pipeline like 'systemctl dot | dot -Tsvg > systemd.svg'!\n");
993 dbus_message_unref(m);
996 dbus_message_unref(reply);
998 dbus_error_free(&error);
1003 static int list_jobs(DBusConnection *bus, char **args) {
1004 DBusMessage *m = NULL, *reply = NULL;
1007 DBusMessageIter iter, sub, sub2;
1010 dbus_error_init(&error);
1014 pager_open_if_enabled();
1016 if (!(m = dbus_message_new_method_call(
1017 "org.freedesktop.systemd1",
1018 "/org/freedesktop/systemd1",
1019 "org.freedesktop.systemd1.Manager",
1021 log_error("Could not allocate message.");
1025 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1026 log_error("Failed to issue method call: %s", bus_error_message(&error));
1031 if (!dbus_message_iter_init(reply, &iter) ||
1032 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
1033 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
1034 log_error("Failed to parse reply.");
1039 dbus_message_iter_recurse(&iter, &sub);
1042 printf("%4s %-25s %-15s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
1044 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1045 const char *name, *type, *state, *job_path, *unit_path;
1049 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
1050 log_error("Failed to parse reply.");
1055 dbus_message_iter_recurse(&sub, &sub2);
1057 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
1058 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
1059 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
1060 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
1061 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
1062 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
1063 log_error("Failed to parse reply.");
1068 e = arg_full ? NULL : ellipsize(name, 25, 33);
1069 printf("%4u %-25s %-15s %-7s\n", id, e ? e : name, type, state);
1074 dbus_message_iter_next(&sub);
1078 printf("\n%u jobs listed.\n", k);
1084 dbus_message_unref(m);
1087 dbus_message_unref(reply);
1089 dbus_error_free(&error);
1094 static int load_unit(DBusConnection *bus, char **args) {
1095 DBusMessage *m = NULL;
1100 dbus_error_init(&error);
1105 STRV_FOREACH(name, args+1) {
1108 if (!(m = dbus_message_new_method_call(
1109 "org.freedesktop.systemd1",
1110 "/org/freedesktop/systemd1",
1111 "org.freedesktop.systemd1.Manager",
1113 log_error("Could not allocate message.");
1118 if (!dbus_message_append_args(m,
1119 DBUS_TYPE_STRING, name,
1120 DBUS_TYPE_INVALID)) {
1121 log_error("Could not append arguments to message.");
1126 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1127 log_error("Failed to issue method call: %s", bus_error_message(&error));
1132 dbus_message_unref(m);
1133 dbus_message_unref(reply);
1142 dbus_message_unref(m);
1144 dbus_error_free(&error);
1149 static int cancel_job(DBusConnection *bus, char **args) {
1150 DBusMessage *m = NULL, *reply = NULL;
1155 dbus_error_init(&error);
1160 if (strv_length(args) <= 1)
1161 return daemon_reload(bus, args);
1163 STRV_FOREACH(name, args+1) {
1167 if (!(m = dbus_message_new_method_call(
1168 "org.freedesktop.systemd1",
1169 "/org/freedesktop/systemd1",
1170 "org.freedesktop.systemd1.Manager",
1172 log_error("Could not allocate message.");
1177 if ((r = safe_atou(*name, &id)) < 0) {
1178 log_error("Failed to parse job id: %s", strerror(-r));
1182 assert_cc(sizeof(uint32_t) == sizeof(id));
1183 if (!dbus_message_append_args(m,
1184 DBUS_TYPE_UINT32, &id,
1185 DBUS_TYPE_INVALID)) {
1186 log_error("Could not append arguments to message.");
1191 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1192 log_error("Failed to issue method call: %s", bus_error_message(&error));
1197 if (!dbus_message_get_args(reply, &error,
1198 DBUS_TYPE_OBJECT_PATH, &path,
1199 DBUS_TYPE_INVALID)) {
1200 log_error("Failed to parse reply: %s", bus_error_message(&error));
1205 dbus_message_unref(m);
1206 if (!(m = dbus_message_new_method_call(
1207 "org.freedesktop.systemd1",
1209 "org.freedesktop.systemd1.Job",
1211 log_error("Could not allocate message.");
1216 dbus_message_unref(reply);
1217 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1218 log_error("Failed to issue method call: %s", bus_error_message(&error));
1223 dbus_message_unref(m);
1224 dbus_message_unref(reply);
1232 dbus_message_unref(m);
1235 dbus_message_unref(reply);
1237 dbus_error_free(&error);
1242 static bool need_daemon_reload(DBusConnection *bus, const char *unit) {
1243 DBusMessage *m = NULL, *reply = NULL;
1244 dbus_bool_t b = FALSE;
1245 DBusMessageIter iter, sub;
1247 *interface = "org.freedesktop.systemd1.Unit",
1248 *property = "NeedDaemonReload",
1251 /* We ignore all errors here, since this is used to show a warning only */
1253 if (!(m = dbus_message_new_method_call(
1254 "org.freedesktop.systemd1",
1255 "/org/freedesktop/systemd1",
1256 "org.freedesktop.systemd1.Manager",
1260 if (!dbus_message_append_args(m,
1261 DBUS_TYPE_STRING, &unit,
1265 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
1268 if (!dbus_message_get_args(reply, NULL,
1269 DBUS_TYPE_OBJECT_PATH, &path,
1273 dbus_message_unref(m);
1274 if (!(m = dbus_message_new_method_call(
1275 "org.freedesktop.systemd1",
1277 "org.freedesktop.DBus.Properties",
1281 if (!dbus_message_append_args(m,
1282 DBUS_TYPE_STRING, &interface,
1283 DBUS_TYPE_STRING, &property,
1284 DBUS_TYPE_INVALID)) {
1288 dbus_message_unref(reply);
1289 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
1292 if (!dbus_message_iter_init(reply, &iter) ||
1293 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
1296 dbus_message_iter_recurse(&iter, &sub);
1298 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_BOOLEAN)
1301 dbus_message_iter_get_basic(&sub, &b);
1305 dbus_message_unref(m);
1308 dbus_message_unref(reply);
1313 typedef struct WaitData {
1318 static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
1326 dbus_error_init(&error);
1328 log_debug("Got D-Bus request: %s.%s() on %s",
1329 dbus_message_get_interface(message),
1330 dbus_message_get_member(message),
1331 dbus_message_get_path(message));
1333 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
1334 log_error("Warning! D-Bus connection terminated.");
1335 dbus_connection_close(connection);
1337 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
1339 const char *path, *result;
1340 dbus_bool_t success = true;
1342 if (dbus_message_get_args(message, &error,
1343 DBUS_TYPE_UINT32, &id,
1344 DBUS_TYPE_OBJECT_PATH, &path,
1345 DBUS_TYPE_STRING, &result,
1346 DBUS_TYPE_INVALID)) {
1349 if ((p = set_remove(d->set, (char*) path)))
1353 d->result = strdup(result);
1358 dbus_error_free(&error);
1360 if (dbus_message_get_args(message, &error,
1361 DBUS_TYPE_UINT32, &id,
1362 DBUS_TYPE_OBJECT_PATH, &path,
1363 DBUS_TYPE_BOOLEAN, &success,
1364 DBUS_TYPE_INVALID)) {
1367 /* Compatibility with older systemd versions <
1368 * 19 during upgrades. This should be dropped
1371 if ((p = set_remove(d->set, (char*) path)))
1375 d->result = strdup("failed");
1381 log_error("Failed to parse message: %s", bus_error_message(&error));
1385 dbus_error_free(&error);
1386 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1389 static int enable_wait_for_jobs(DBusConnection *bus) {
1397 dbus_error_init(&error);
1398 dbus_bus_add_match(bus,
1400 "sender='org.freedesktop.systemd1',"
1401 "interface='org.freedesktop.systemd1.Manager',"
1402 "member='JobRemoved',"
1403 "path='/org/freedesktop/systemd1'",
1406 if (dbus_error_is_set(&error)) {
1407 log_error("Failed to add match: %s", bus_error_message(&error));
1408 dbus_error_free(&error);
1412 /* This is slightly dirty, since we don't undo the match registrations. */
1416 static int wait_for_jobs(DBusConnection *bus, Set *s) {
1426 if (!dbus_connection_add_filter(bus, wait_filter, &d, NULL)) {
1427 log_error("Failed to add filter.");
1432 while (!set_isempty(s) &&
1433 dbus_connection_read_write_dispatch(bus, -1))
1436 if (!arg_quiet && d.result) {
1437 if (streq(d.result, "timeout"))
1438 log_error("Job timed out.");
1439 else if (streq(d.result, "canceled"))
1440 log_error("Job canceled.");
1441 else if (streq(d.result, "dependency"))
1442 log_error("A dependency job failed. See system journal for details.");
1443 else if (!streq(d.result, "done") && !streq(d.result, "skipped"))
1444 log_error("Job failed. See system journal and 'systemctl status' for details.");
1447 if (streq_ptr(d.result, "timeout"))
1449 else if (streq_ptr(d.result, "canceled"))
1451 else if (!streq_ptr(d.result, "done") && !streq_ptr(d.result, "skipped"))
1459 /* This is slightly dirty, since we don't undo the filter registration. */
1464 static int start_unit_one(
1465 DBusConnection *bus,
1472 DBusMessage *m = NULL, *reply = NULL;
1481 assert(arg_no_block || s);
1483 if (!(m = dbus_message_new_method_call(
1484 "org.freedesktop.systemd1",
1485 "/org/freedesktop/systemd1",
1486 "org.freedesktop.systemd1.Manager",
1488 log_error("Could not allocate message.");
1493 if (!dbus_message_append_args(m,
1494 DBUS_TYPE_STRING, &name,
1495 DBUS_TYPE_STRING, &mode,
1496 DBUS_TYPE_INVALID)) {
1497 log_error("Could not append arguments to message.");
1502 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, error))) {
1504 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(error)) {
1505 /* There's always a fallback possible for
1506 * legacy actions. */
1511 log_error("Failed to issue method call: %s", bus_error_message(error));
1516 if (!dbus_message_get_args(reply, error,
1517 DBUS_TYPE_OBJECT_PATH, &path,
1518 DBUS_TYPE_INVALID)) {
1519 log_error("Failed to parse reply: %s", bus_error_message(error));
1524 if (need_daemon_reload(bus, name))
1525 log_warning("Warning: Unit file of created job changed on disk, 'systemctl %s daemon-reload' recommended.",
1526 arg_scope == UNIT_FILE_SYSTEM ? "--system" : "--user");
1528 if (!arg_no_block) {
1531 if (!(p = strdup(path))) {
1532 log_error("Failed to duplicate path.");
1537 if ((r = set_put(s, p)) < 0) {
1539 log_error("Failed to add path to set.");
1548 dbus_message_unref(m);
1551 dbus_message_unref(reply);
1556 static enum action verb_to_action(const char *verb) {
1557 if (streq(verb, "halt"))
1559 else if (streq(verb, "poweroff"))
1560 return ACTION_POWEROFF;
1561 else if (streq(verb, "reboot"))
1562 return ACTION_REBOOT;
1563 else if (streq(verb, "kexec"))
1564 return ACTION_KEXEC;
1565 else if (streq(verb, "rescue"))
1566 return ACTION_RESCUE;
1567 else if (streq(verb, "emergency"))
1568 return ACTION_EMERGENCY;
1569 else if (streq(verb, "default"))
1570 return ACTION_DEFAULT;
1571 else if (streq(verb, "exit"))
1574 return ACTION_INVALID;
1577 static int start_unit(DBusConnection *bus, char **args) {
1579 static const char * const table[_ACTION_MAX] = {
1580 [ACTION_HALT] = SPECIAL_HALT_TARGET,
1581 [ACTION_POWEROFF] = SPECIAL_POWEROFF_TARGET,
1582 [ACTION_REBOOT] = SPECIAL_REBOOT_TARGET,
1583 [ACTION_KEXEC] = SPECIAL_KEXEC_TARGET,
1584 [ACTION_RUNLEVEL2] = SPECIAL_RUNLEVEL2_TARGET,
1585 [ACTION_RUNLEVEL3] = SPECIAL_RUNLEVEL3_TARGET,
1586 [ACTION_RUNLEVEL4] = SPECIAL_RUNLEVEL4_TARGET,
1587 [ACTION_RUNLEVEL5] = SPECIAL_RUNLEVEL5_TARGET,
1588 [ACTION_RESCUE] = SPECIAL_RESCUE_TARGET,
1589 [ACTION_EMERGENCY] = SPECIAL_EMERGENCY_TARGET,
1590 [ACTION_DEFAULT] = SPECIAL_DEFAULT_TARGET,
1591 [ACTION_EXIT] = SPECIAL_EXIT_TARGET
1595 const char *method, *mode, *one_name;
1600 dbus_error_init(&error);
1604 agent_open_if_enabled();
1606 if (arg_action == ACTION_SYSTEMCTL) {
1608 streq(args[0], "stop") ||
1609 streq(args[0], "condstop") ? "StopUnit" :
1610 streq(args[0], "reload") ? "ReloadUnit" :
1611 streq(args[0], "restart") ? "RestartUnit" :
1613 streq(args[0], "try-restart") ||
1614 streq(args[0], "condrestart") ? "TryRestartUnit" :
1616 streq(args[0], "reload-or-restart") ? "ReloadOrRestartUnit" :
1618 streq(args[0], "reload-or-try-restart") ||
1619 streq(args[0], "condreload") ||
1621 streq(args[0], "force-reload") ? "ReloadOrTryRestartUnit" :
1625 (streq(args[0], "isolate") ||
1626 streq(args[0], "rescue") ||
1627 streq(args[0], "emergency")) ? "isolate" : arg_job_mode;
1629 one_name = table[verb_to_action(args[0])];
1632 assert(arg_action < ELEMENTSOF(table));
1633 assert(table[arg_action]);
1635 method = "StartUnit";
1637 mode = (arg_action == ACTION_EMERGENCY ||
1638 arg_action == ACTION_RESCUE ||
1639 arg_action == ACTION_RUNLEVEL2 ||
1640 arg_action == ACTION_RUNLEVEL3 ||
1641 arg_action == ACTION_RUNLEVEL4 ||
1642 arg_action == ACTION_RUNLEVEL5) ? "isolate" : "replace";
1644 one_name = table[arg_action];
1647 if (!arg_no_block) {
1648 if ((ret = enable_wait_for_jobs(bus)) < 0) {
1649 log_error("Could not watch jobs: %s", strerror(-ret));
1653 if (!(s = set_new(string_hash_func, string_compare_func))) {
1654 log_error("Failed to allocate set.");
1661 if ((ret = start_unit_one(bus, method, one_name, mode, &error, s)) <= 0)
1664 STRV_FOREACH(name, args+1)
1665 if ((r = start_unit_one(bus, method, *name, mode, &error, s)) != 0) {
1666 ret = translate_bus_error_to_exit_status(r, &error);
1667 dbus_error_free(&error);
1672 if ((r = wait_for_jobs(bus, s)) < 0) {
1681 dbus_error_free(&error);
1686 /* Ask systemd-logind, which might grant access to unprivileged users
1687 * through PolicyKit */
1688 static int reboot_with_logind(DBusConnection *bus, enum action a) {
1691 DBusMessage *m = NULL, *reply = NULL;
1693 dbus_bool_t interactive = true;
1696 dbus_error_init(&error);
1704 case ACTION_POWEROFF:
1705 method = "PowerOff";
1712 m = dbus_message_new_method_call(
1713 "org.freedesktop.login1",
1714 "/org/freedesktop/login1",
1715 "org.freedesktop.login1.Manager",
1718 log_error("Could not allocate message.");
1723 if (!dbus_message_append_args(m,
1724 DBUS_TYPE_BOOLEAN, &interactive,
1725 DBUS_TYPE_INVALID)) {
1726 log_error("Could not append arguments to message.");
1731 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
1733 if (error_is_no_service(&error)) {
1734 log_debug("Failed to issue method call: %s", bus_error_message(&error));
1739 if (dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
1740 log_debug("Failed to issue method call: %s", bus_error_message(&error));
1745 log_info("Failed to issue method call: %s", bus_error_message(&error));
1754 dbus_message_unref(m);
1757 dbus_message_unref(reply);
1759 dbus_error_free(&error);
1767 static int start_special(DBusConnection *bus, char **args) {
1774 a = verb_to_action(args[0]);
1776 if (arg_force >= 2 &&
1777 (a == ACTION_HALT ||
1778 a == ACTION_POWEROFF ||
1779 a == ACTION_REBOOT))
1782 if (arg_force >= 1 &&
1783 (a == ACTION_HALT ||
1784 a == ACTION_POWEROFF ||
1785 a == ACTION_REBOOT ||
1786 a == ACTION_KEXEC ||
1788 return daemon_reload(bus, args);
1790 /* first try logind, to allow authentication with polkit */
1791 if (geteuid() != 0 &&
1792 (a == ACTION_POWEROFF ||
1793 a == ACTION_REBOOT)) {
1794 r = reboot_with_logind(bus, a);
1799 r = start_unit(bus, args);
1806 static int check_unit(DBusConnection *bus, char **args) {
1807 DBusMessage *m = NULL, *reply = NULL;
1809 *interface = "org.freedesktop.systemd1.Unit",
1810 *property = "ActiveState";
1811 int r = 3; /* According to LSB: "program is not running" */
1818 dbus_error_init(&error);
1820 STRV_FOREACH(name, args+1) {
1821 const char *path = NULL;
1823 DBusMessageIter iter, sub;
1825 if (!(m = dbus_message_new_method_call(
1826 "org.freedesktop.systemd1",
1827 "/org/freedesktop/systemd1",
1828 "org.freedesktop.systemd1.Manager",
1830 log_error("Could not allocate message.");
1835 if (!dbus_message_append_args(m,
1836 DBUS_TYPE_STRING, name,
1837 DBUS_TYPE_INVALID)) {
1838 log_error("Could not append arguments to message.");
1843 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1845 /* Hmm, cannot figure out anything about this unit... */
1849 dbus_error_free(&error);
1850 dbus_message_unref(m);
1855 if (!dbus_message_get_args(reply, &error,
1856 DBUS_TYPE_OBJECT_PATH, &path,
1857 DBUS_TYPE_INVALID)) {
1858 log_error("Failed to parse reply: %s", bus_error_message(&error));
1863 dbus_message_unref(m);
1864 if (!(m = dbus_message_new_method_call(
1865 "org.freedesktop.systemd1",
1867 "org.freedesktop.DBus.Properties",
1869 log_error("Could not allocate message.");
1874 if (!dbus_message_append_args(m,
1875 DBUS_TYPE_STRING, &interface,
1876 DBUS_TYPE_STRING, &property,
1877 DBUS_TYPE_INVALID)) {
1878 log_error("Could not append arguments to message.");
1883 dbus_message_unref(reply);
1884 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1885 log_error("Failed to issue method call: %s", bus_error_message(&error));
1890 if (!dbus_message_iter_init(reply, &iter) ||
1891 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1892 log_error("Failed to parse reply.");
1897 dbus_message_iter_recurse(&iter, &sub);
1899 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1900 log_error("Failed to parse reply.");
1905 dbus_message_iter_get_basic(&sub, &state);
1910 if (streq(state, "active") || streq(state, "reloading"))
1913 dbus_message_unref(m);
1914 dbus_message_unref(reply);
1920 dbus_message_unref(m);
1923 dbus_message_unref(reply);
1925 dbus_error_free(&error);
1930 static int kill_unit(DBusConnection *bus, char **args) {
1931 DBusMessage *m = NULL;
1939 dbus_error_init(&error);
1942 arg_kill_who = "all";
1945 arg_kill_mode = streq(arg_kill_who, "all") ? "control-group" : "process";
1947 STRV_FOREACH(name, args+1) {
1950 if (!(m = dbus_message_new_method_call(
1951 "org.freedesktop.systemd1",
1952 "/org/freedesktop/systemd1",
1953 "org.freedesktop.systemd1.Manager",
1955 log_error("Could not allocate message.");
1960 if (!dbus_message_append_args(m,
1961 DBUS_TYPE_STRING, name,
1962 DBUS_TYPE_STRING, &arg_kill_who,
1963 DBUS_TYPE_STRING, &arg_kill_mode,
1964 DBUS_TYPE_INT32, &arg_signal,
1965 DBUS_TYPE_INVALID)) {
1966 log_error("Could not append arguments to message.");
1971 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1972 log_error("Failed to issue method call: %s", bus_error_message(&error));
1973 dbus_error_free(&error);
1977 dbus_message_unref(m);
1980 dbus_message_unref(reply);
1986 dbus_message_unref(m);
1988 dbus_error_free(&error);
1993 typedef struct ExecStatusInfo {
2001 usec_t start_timestamp;
2002 usec_t exit_timestamp;
2007 LIST_FIELDS(struct ExecStatusInfo, exec);
2010 static void exec_status_info_free(ExecStatusInfo *i) {
2019 static int exec_status_info_deserialize(DBusMessageIter *sub, ExecStatusInfo *i) {
2020 uint64_t start_timestamp, exit_timestamp, start_timestamp_monotonic, exit_timestamp_monotonic;
2021 DBusMessageIter sub2, sub3;
2025 int32_t code, status;
2031 if (dbus_message_iter_get_arg_type(sub) != DBUS_TYPE_STRUCT)
2034 dbus_message_iter_recurse(sub, &sub2);
2036 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
2039 if (!(i->path = strdup(path)))
2042 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
2043 dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
2047 dbus_message_iter_recurse(&sub2, &sub3);
2048 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
2049 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
2050 dbus_message_iter_next(&sub3);
2055 if (!(i->argv = new0(char*, n+1)))
2059 dbus_message_iter_recurse(&sub2, &sub3);
2060 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
2063 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
2064 dbus_message_iter_get_basic(&sub3, &s);
2065 dbus_message_iter_next(&sub3);
2067 if (!(i->argv[n++] = strdup(s)))
2071 if (!dbus_message_iter_next(&sub2) ||
2072 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, true) < 0 ||
2073 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp, true) < 0 ||
2074 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp_monotonic, true) < 0 ||
2075 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp, true) < 0 ||
2076 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp_monotonic, true) < 0 ||
2077 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) < 0 ||
2078 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) < 0 ||
2079 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) < 0)
2083 i->start_timestamp = (usec_t) start_timestamp;
2084 i->exit_timestamp = (usec_t) exit_timestamp;
2085 i->pid = (pid_t) pid;
2092 typedef struct UnitStatusInfo {
2094 const char *load_state;
2095 const char *active_state;
2096 const char *sub_state;
2097 const char *unit_file_state;
2099 const char *description;
2100 const char *following;
2103 const char *default_control_group;
2105 const char *load_error;
2108 usec_t inactive_exit_timestamp;
2109 usec_t inactive_exit_timestamp_monotonic;
2110 usec_t active_enter_timestamp;
2111 usec_t active_exit_timestamp;
2112 usec_t inactive_enter_timestamp;
2114 bool need_daemon_reload;
2119 const char *status_text;
2121 #ifdef HAVE_SYSV_COMPAT
2125 usec_t start_timestamp;
2126 usec_t exit_timestamp;
2128 int exit_code, exit_status;
2130 usec_t condition_timestamp;
2131 bool condition_result;
2134 unsigned n_accepted;
2135 unsigned n_connections;
2139 const char *sysfs_path;
2141 /* Mount, Automount */
2147 LIST_HEAD(ExecStatusInfo, exec);
2150 static void print_status_info(UnitStatusInfo *i) {
2152 const char *on, *off, *ss;
2154 char since1[FORMAT_TIMESTAMP_PRETTY_MAX], *s1;
2155 char since2[FORMAT_TIMESTAMP_MAX], *s2;
2159 /* This shows pretty information about a unit. See
2160 * print_property() for a low-level property printer */
2162 printf("%s", strna(i->id));
2164 if (i->description && !streq_ptr(i->id, i->description))
2165 printf(" - %s", i->description);
2170 printf("\t Follow: unit currently follows state of %s\n", i->following);
2172 if (streq_ptr(i->load_state, "error")) {
2173 on = ansi_highlight_red(true);
2174 off = ansi_highlight_red(false);
2179 printf("\t Loaded: %s%s%s (Reason: %s)\n", on, strna(i->load_state), off, i->load_error);
2180 else if (i->path && i->unit_file_state)
2181 printf("\t Loaded: %s%s%s (%s; %s)\n", on, strna(i->load_state), off, i->path, i->unit_file_state);
2183 printf("\t Loaded: %s%s%s (%s)\n", on, strna(i->load_state), off, i->path);
2185 printf("\t Loaded: %s%s%s\n", on, strna(i->load_state), off);
2187 ss = streq_ptr(i->active_state, i->sub_state) ? NULL : i->sub_state;
2189 if (streq_ptr(i->active_state, "failed")) {
2190 on = ansi_highlight_red(true);
2191 off = ansi_highlight_red(false);
2192 } else if (streq_ptr(i->active_state, "active") || streq_ptr(i->active_state, "reloading")) {
2193 on = ansi_highlight_green(true);
2194 off = ansi_highlight_green(false);
2199 printf("\t Active: %s%s (%s)%s",
2201 strna(i->active_state),
2205 printf("\t Active: %s%s%s",
2207 strna(i->active_state),
2210 if (!isempty(i->result) && !streq(i->result, "success"))
2211 printf(" (Result: %s)", i->result);
2213 timestamp = (streq_ptr(i->active_state, "active") ||
2214 streq_ptr(i->active_state, "reloading")) ? i->active_enter_timestamp :
2215 (streq_ptr(i->active_state, "inactive") ||
2216 streq_ptr(i->active_state, "failed")) ? i->inactive_enter_timestamp :
2217 streq_ptr(i->active_state, "activating") ? i->inactive_exit_timestamp :
2218 i->active_exit_timestamp;
2220 s1 = format_timestamp_pretty(since1, sizeof(since1), timestamp);
2221 s2 = format_timestamp(since2, sizeof(since2), timestamp);
2224 printf(" since %s; %s\n", s2, s1);
2226 printf(" since %s\n", s2);
2230 if (!i->condition_result && i->condition_timestamp > 0) {
2231 s1 = format_timestamp_pretty(since1, sizeof(since1), i->condition_timestamp);
2232 s2 = format_timestamp(since2, sizeof(since2), i->condition_timestamp);
2235 printf("\t start condition failed at %s; %s\n", s2, s1);
2237 printf("\t start condition failed at %s\n", s2);
2241 printf("\t Device: %s\n", i->sysfs_path);
2243 printf("\t Where: %s\n", i->where);
2245 printf("\t What: %s\n", i->what);
2248 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
2250 LIST_FOREACH(exec, p, i->exec) {
2254 /* Only show exited processes here */
2258 t = strv_join(p->argv, " ");
2259 printf("\t Process: %u %s=%s ", p->pid, p->name, strna(t));
2262 #ifdef HAVE_SYSV_COMPAT
2264 good = is_clean_exit_lsb(p->code, p->status);
2267 good = is_clean_exit(p->code, p->status);
2270 on = ansi_highlight_red(true);
2271 off = ansi_highlight_red(false);
2275 printf("%s(code=%s, ", on, sigchld_code_to_string(p->code));
2277 if (p->code == CLD_EXITED) {
2280 printf("status=%i", p->status);
2282 #ifdef HAVE_SYSV_COMPAT
2283 if ((c = exit_status_to_string(p->status, i->is_sysv ? EXIT_STATUS_LSB : EXIT_STATUS_SYSTEMD)))
2285 if ((c = exit_status_to_string(p->status, EXIT_STATUS_SYSTEMD)))
2290 printf("signal=%s", signal_to_string(p->status));
2292 printf(")%s\n", off);
2294 if (i->main_pid == p->pid &&
2295 i->start_timestamp == p->start_timestamp &&
2296 i->exit_timestamp == p->start_timestamp)
2297 /* Let's not show this twice */
2300 if (p->pid == i->control_pid)
2304 if (i->main_pid > 0 || i->control_pid > 0) {
2307 if (i->main_pid > 0) {
2308 printf("Main PID: %u", (unsigned) i->main_pid);
2312 get_process_comm(i->main_pid, &t);
2317 } else if (i->exit_code > 0) {
2318 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
2320 if (i->exit_code == CLD_EXITED) {
2323 printf("status=%i", i->exit_status);
2325 #ifdef HAVE_SYSV_COMPAT
2326 if ((c = exit_status_to_string(i->exit_status, i->is_sysv ? EXIT_STATUS_LSB : EXIT_STATUS_SYSTEMD)))
2328 if ((c = exit_status_to_string(i->exit_status, EXIT_STATUS_SYSTEMD)))
2333 printf("signal=%s", signal_to_string(i->exit_status));
2338 if (i->main_pid > 0 && i->control_pid > 0)
2341 if (i->control_pid > 0) {
2344 printf(" Control: %u", (unsigned) i->control_pid);
2346 get_process_comm(i->control_pid, &t);
2357 printf("\t Status: \"%s\"\n", i->status_text);
2359 if (i->default_control_group) {
2362 printf("\t CGroup: %s\n", i->default_control_group);
2364 if (arg_transport != TRANSPORT_SSH) {
2365 if ((c = columns()) > 18)
2370 show_cgroup_by_path(i->default_control_group, "\t\t ", c, false);
2374 if (i->id && arg_transport != TRANSPORT_SSH) {
2376 show_journal_by_unit(i->id, arg_output, 0, i->inactive_exit_timestamp_monotonic, arg_lines, arg_all, arg_follow);
2379 if (i->need_daemon_reload)
2380 printf("\n%sWarning:%s Unit file changed on disk, 'systemctl %s daemon-reload' recommended.\n",
2381 ansi_highlight_red(true),
2382 ansi_highlight_red(false),
2383 arg_scope == UNIT_FILE_SYSTEM ? "--system" : "--user");
2386 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
2392 switch (dbus_message_iter_get_arg_type(iter)) {
2394 case DBUS_TYPE_STRING: {
2397 dbus_message_iter_get_basic(iter, &s);
2400 if (streq(name, "Id"))
2402 else if (streq(name, "LoadState"))
2404 else if (streq(name, "ActiveState"))
2405 i->active_state = s;
2406 else if (streq(name, "SubState"))
2408 else if (streq(name, "Description"))
2410 else if (streq(name, "FragmentPath"))
2412 #ifdef HAVE_SYSV_COMPAT
2413 else if (streq(name, "SysVPath")) {
2418 else if (streq(name, "DefaultControlGroup"))
2419 i->default_control_group = s;
2420 else if (streq(name, "StatusText"))
2422 else if (streq(name, "SysFSPath"))
2424 else if (streq(name, "Where"))
2426 else if (streq(name, "What"))
2428 else if (streq(name, "Following"))
2430 else if (streq(name, "UnitFileState"))
2431 i->unit_file_state = s;
2432 else if (streq(name, "Result"))
2439 case DBUS_TYPE_BOOLEAN: {
2442 dbus_message_iter_get_basic(iter, &b);
2444 if (streq(name, "Accept"))
2446 else if (streq(name, "NeedDaemonReload"))
2447 i->need_daemon_reload = b;
2448 else if (streq(name, "ConditionResult"))
2449 i->condition_result = b;
2454 case DBUS_TYPE_UINT32: {
2457 dbus_message_iter_get_basic(iter, &u);
2459 if (streq(name, "MainPID")) {
2461 i->main_pid = (pid_t) u;
2464 } else if (streq(name, "ControlPID"))
2465 i->control_pid = (pid_t) u;
2466 else if (streq(name, "ExecMainPID")) {
2468 i->main_pid = (pid_t) u;
2469 } else if (streq(name, "NAccepted"))
2471 else if (streq(name, "NConnections"))
2472 i->n_connections = u;
2477 case DBUS_TYPE_INT32: {
2480 dbus_message_iter_get_basic(iter, &j);
2482 if (streq(name, "ExecMainCode"))
2483 i->exit_code = (int) j;
2484 else if (streq(name, "ExecMainStatus"))
2485 i->exit_status = (int) j;
2490 case DBUS_TYPE_UINT64: {
2493 dbus_message_iter_get_basic(iter, &u);
2495 if (streq(name, "ExecMainStartTimestamp"))
2496 i->start_timestamp = (usec_t) u;
2497 else if (streq(name, "ExecMainExitTimestamp"))
2498 i->exit_timestamp = (usec_t) u;
2499 else if (streq(name, "ActiveEnterTimestamp"))
2500 i->active_enter_timestamp = (usec_t) u;
2501 else if (streq(name, "InactiveEnterTimestamp"))
2502 i->inactive_enter_timestamp = (usec_t) u;
2503 else if (streq(name, "InactiveExitTimestamp"))
2504 i->inactive_exit_timestamp = (usec_t) u;
2505 else if (streq(name, "InactiveExitTimestampMonotonic"))
2506 i->inactive_exit_timestamp_monotonic = (usec_t) u;
2507 else if (streq(name, "ActiveExitTimestamp"))
2508 i->active_exit_timestamp = (usec_t) u;
2509 else if (streq(name, "ConditionTimestamp"))
2510 i->condition_timestamp = (usec_t) u;
2515 case DBUS_TYPE_ARRAY: {
2517 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT &&
2518 startswith(name, "Exec")) {
2519 DBusMessageIter sub;
2521 dbus_message_iter_recurse(iter, &sub);
2522 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2523 ExecStatusInfo *info;
2526 if (!(info = new0(ExecStatusInfo, 1)))
2529 if (!(info->name = strdup(name))) {
2534 if ((r = exec_status_info_deserialize(&sub, info)) < 0) {
2539 LIST_PREPEND(ExecStatusInfo, exec, i->exec, info);
2541 dbus_message_iter_next(&sub);
2548 case DBUS_TYPE_STRUCT: {
2550 if (streq(name, "LoadError")) {
2551 DBusMessageIter sub;
2552 const char *n, *message;
2555 dbus_message_iter_recurse(iter, &sub);
2557 r = bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &n, true);
2561 r = bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &message, false);
2565 if (!isempty(message))
2566 i->load_error = message;
2576 static int print_property(const char *name, DBusMessageIter *iter) {
2580 /* This is a low-level property printer, see
2581 * print_status_info() for the nicer output */
2583 if (arg_property && !strv_find(arg_property, name))
2586 switch (dbus_message_iter_get_arg_type(iter)) {
2588 case DBUS_TYPE_STRUCT: {
2589 DBusMessageIter sub;
2590 dbus_message_iter_recurse(iter, &sub);
2592 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
2595 dbus_message_iter_get_basic(&sub, &u);
2598 printf("%s=%u\n", name, (unsigned) u);
2600 printf("%s=\n", name);
2603 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
2606 dbus_message_iter_get_basic(&sub, &s);
2608 if (arg_all || s[0])
2609 printf("%s=%s\n", name, s);
2612 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "LoadError")) {
2613 const char *a = NULL, *b = NULL;
2615 if (bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &a, true) >= 0)
2616 bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &b, false);
2618 if (arg_all || !isempty(a) || !isempty(b))
2619 printf("%s=%s \"%s\"\n", name, strempty(a), strempty(b));
2627 case DBUS_TYPE_ARRAY:
2629 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "EnvironmentFiles")) {
2630 DBusMessageIter sub, sub2;
2632 dbus_message_iter_recurse(iter, &sub);
2633 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2637 dbus_message_iter_recurse(&sub, &sub2);
2639 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) >= 0 &&
2640 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, false) >= 0)
2641 printf("EnvironmentFile=%s (ignore_errors=%s)\n", path, yes_no(ignore));
2643 dbus_message_iter_next(&sub);
2648 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
2649 DBusMessageIter sub, sub2;
2651 dbus_message_iter_recurse(iter, &sub);
2652 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2653 const char *type, *path;
2655 dbus_message_iter_recurse(&sub, &sub2);
2657 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
2658 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
2659 printf("%s=%s\n", type, path);
2661 dbus_message_iter_next(&sub);
2666 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
2667 DBusMessageIter sub, sub2;
2669 dbus_message_iter_recurse(iter, &sub);
2670 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2672 uint64_t value, next_elapse;
2674 dbus_message_iter_recurse(&sub, &sub2);
2676 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
2677 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
2678 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
2679 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
2681 printf("%s={ value=%s ; next_elapse=%s }\n",
2683 format_timespan(timespan1, sizeof(timespan1), value),
2684 format_timespan(timespan2, sizeof(timespan2), next_elapse));
2687 dbus_message_iter_next(&sub);
2692 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "ControlGroupAttributes")) {
2693 DBusMessageIter sub, sub2;
2695 dbus_message_iter_recurse(iter, &sub);
2696 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2697 const char *controller, *attr, *value;
2699 dbus_message_iter_recurse(&sub, &sub2);
2701 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &controller, true) >= 0 &&
2702 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &attr, true) >= 0 &&
2703 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &value, false) >= 0) {
2705 printf("ControlGroupAttribute={ controller=%s ; attribute=%s ; value=\"%s\" }\n",
2711 dbus_message_iter_next(&sub);
2716 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
2717 DBusMessageIter sub;
2719 dbus_message_iter_recurse(iter, &sub);
2720 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2721 ExecStatusInfo info;
2724 if (exec_status_info_deserialize(&sub, &info) >= 0) {
2725 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
2728 t = strv_join(info.argv, " ");
2730 printf("%s={ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n",
2734 yes_no(info.ignore),
2735 strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)),
2736 strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)),
2737 (unsigned) info. pid,
2738 sigchld_code_to_string(info.code),
2740 info.code == CLD_EXITED ? "" : "/",
2741 strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status)));
2747 strv_free(info.argv);
2749 dbus_message_iter_next(&sub);
2758 if (generic_print_property(name, iter, arg_all) > 0)
2762 printf("%s=[unprintable]\n", name);
2767 static int show_one(const char *verb, DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
2768 DBusMessage *m = NULL, *reply = NULL;
2769 const char *interface = "";
2772 DBusMessageIter iter, sub, sub2, sub3;
2773 UnitStatusInfo info;
2781 dbus_error_init(&error);
2783 if (!(m = dbus_message_new_method_call(
2784 "org.freedesktop.systemd1",
2786 "org.freedesktop.DBus.Properties",
2788 log_error("Could not allocate message.");
2793 if (!dbus_message_append_args(m,
2794 DBUS_TYPE_STRING, &interface,
2795 DBUS_TYPE_INVALID)) {
2796 log_error("Could not append arguments to message.");
2801 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2802 log_error("Failed to issue method call: %s", bus_error_message(&error));
2807 if (!dbus_message_iter_init(reply, &iter) ||
2808 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
2809 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
2810 log_error("Failed to parse reply.");
2815 dbus_message_iter_recurse(&iter, &sub);
2822 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2825 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
2826 log_error("Failed to parse reply.");
2831 dbus_message_iter_recurse(&sub, &sub2);
2833 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
2834 log_error("Failed to parse reply.");
2839 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
2840 log_error("Failed to parse reply.");
2845 dbus_message_iter_recurse(&sub2, &sub3);
2847 if (show_properties)
2848 r = print_property(name, &sub3);
2850 r = status_property(name, &sub3, &info);
2853 log_error("Failed to parse reply.");
2858 dbus_message_iter_next(&sub);
2863 if (!show_properties)
2864 print_status_info(&info);
2866 if (!streq_ptr(info.active_state, "active") &&
2867 !streq_ptr(info.active_state, "reloading") &&
2868 streq(verb, "status"))
2869 /* According to LSB: "program not running" */
2872 while ((p = info.exec)) {
2873 LIST_REMOVE(ExecStatusInfo, exec, info.exec, p);
2874 exec_status_info_free(p);
2879 dbus_message_unref(m);
2882 dbus_message_unref(reply);
2884 dbus_error_free(&error);
2889 static int show(DBusConnection *bus, char **args) {
2890 DBusMessage *m = NULL, *reply = NULL;
2893 bool show_properties, new_line = false;
2899 dbus_error_init(&error);
2901 show_properties = !streq(args[0], "status");
2903 if (show_properties)
2904 pager_open_if_enabled();
2906 if (show_properties && strv_length(args) <= 1) {
2907 /* If not argument is specified inspect the manager
2910 ret = show_one(args[0], bus, "/org/freedesktop/systemd1", show_properties, &new_line);
2914 STRV_FOREACH(name, args+1) {
2915 const char *path = NULL;
2918 if (safe_atou32(*name, &id) < 0) {
2920 /* Interpret as unit name */
2922 if (!(m = dbus_message_new_method_call(
2923 "org.freedesktop.systemd1",
2924 "/org/freedesktop/systemd1",
2925 "org.freedesktop.systemd1.Manager",
2927 log_error("Could not allocate message.");
2932 if (!dbus_message_append_args(m,
2933 DBUS_TYPE_STRING, name,
2934 DBUS_TYPE_INVALID)) {
2935 log_error("Could not append arguments to message.");
2940 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2942 if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
2943 log_error("Failed to issue method call: %s", bus_error_message(&error));
2948 dbus_error_free(&error);
2950 dbus_message_unref(m);
2951 if (!(m = dbus_message_new_method_call(
2952 "org.freedesktop.systemd1",
2953 "/org/freedesktop/systemd1",
2954 "org.freedesktop.systemd1.Manager",
2956 log_error("Could not allocate message.");
2961 if (!dbus_message_append_args(m,
2962 DBUS_TYPE_STRING, name,
2963 DBUS_TYPE_INVALID)) {
2964 log_error("Could not append arguments to message.");
2969 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2970 log_error("Failed to issue method call: %s", bus_error_message(&error));
2972 if (dbus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT))
2973 ret = 4; /* According to LSB: "program or service status is unknown" */
2980 } else if (show_properties) {
2982 /* Interpret as job id */
2984 if (!(m = dbus_message_new_method_call(
2985 "org.freedesktop.systemd1",
2986 "/org/freedesktop/systemd1",
2987 "org.freedesktop.systemd1.Manager",
2989 log_error("Could not allocate message.");
2994 if (!dbus_message_append_args(m,
2995 DBUS_TYPE_UINT32, &id,
2996 DBUS_TYPE_INVALID)) {
2997 log_error("Could not append arguments to message.");
3002 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3003 log_error("Failed to issue method call: %s", bus_error_message(&error));
3009 /* Interpret as PID */
3011 if (!(m = dbus_message_new_method_call(
3012 "org.freedesktop.systemd1",
3013 "/org/freedesktop/systemd1",
3014 "org.freedesktop.systemd1.Manager",
3016 log_error("Could not allocate message.");
3021 if (!dbus_message_append_args(m,
3022 DBUS_TYPE_UINT32, &id,
3023 DBUS_TYPE_INVALID)) {
3024 log_error("Could not append arguments to message.");
3029 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3030 log_error("Failed to issue method call: %s", bus_error_message(&error));
3036 if (!dbus_message_get_args(reply, &error,
3037 DBUS_TYPE_OBJECT_PATH, &path,
3038 DBUS_TYPE_INVALID)) {
3039 log_error("Failed to parse reply: %s", bus_error_message(&error));
3044 if ((r = show_one(args[0], bus, path, show_properties, &new_line)) != 0)
3047 dbus_message_unref(m);
3048 dbus_message_unref(reply);
3054 dbus_message_unref(m);
3057 dbus_message_unref(reply);
3059 dbus_error_free(&error);
3064 static int dump(DBusConnection *bus, char **args) {
3065 DBusMessage *m = NULL, *reply = NULL;
3070 dbus_error_init(&error);
3072 pager_open_if_enabled();
3074 if (!(m = dbus_message_new_method_call(
3075 "org.freedesktop.systemd1",
3076 "/org/freedesktop/systemd1",
3077 "org.freedesktop.systemd1.Manager",
3079 log_error("Could not allocate message.");
3083 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3084 log_error("Failed to issue method call: %s", bus_error_message(&error));
3089 if (!dbus_message_get_args(reply, &error,
3090 DBUS_TYPE_STRING, &text,
3091 DBUS_TYPE_INVALID)) {
3092 log_error("Failed to parse reply: %s", bus_error_message(&error));
3097 fputs(text, stdout);
3103 dbus_message_unref(m);
3106 dbus_message_unref(reply);
3108 dbus_error_free(&error);
3113 static int snapshot(DBusConnection *bus, char **args) {
3114 DBusMessage *m = NULL, *reply = NULL;
3117 const char *name = "", *path, *id;
3118 dbus_bool_t cleanup = FALSE;
3119 DBusMessageIter iter, sub;
3121 *interface = "org.freedesktop.systemd1.Unit",
3124 dbus_error_init(&error);
3126 if (!(m = dbus_message_new_method_call(
3127 "org.freedesktop.systemd1",
3128 "/org/freedesktop/systemd1",
3129 "org.freedesktop.systemd1.Manager",
3130 "CreateSnapshot"))) {
3131 log_error("Could not allocate message.");
3135 if (strv_length(args) > 1)
3138 if (!dbus_message_append_args(m,
3139 DBUS_TYPE_STRING, &name,
3140 DBUS_TYPE_BOOLEAN, &cleanup,
3141 DBUS_TYPE_INVALID)) {
3142 log_error("Could not append arguments to message.");
3147 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3148 log_error("Failed to issue method call: %s", bus_error_message(&error));
3153 if (!dbus_message_get_args(reply, &error,
3154 DBUS_TYPE_OBJECT_PATH, &path,
3155 DBUS_TYPE_INVALID)) {
3156 log_error("Failed to parse reply: %s", bus_error_message(&error));
3161 dbus_message_unref(m);
3162 if (!(m = dbus_message_new_method_call(
3163 "org.freedesktop.systemd1",
3165 "org.freedesktop.DBus.Properties",
3167 log_error("Could not allocate message.");
3171 if (!dbus_message_append_args(m,
3172 DBUS_TYPE_STRING, &interface,
3173 DBUS_TYPE_STRING, &property,
3174 DBUS_TYPE_INVALID)) {
3175 log_error("Could not append arguments to message.");
3180 dbus_message_unref(reply);
3181 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3182 log_error("Failed to issue method call: %s", bus_error_message(&error));
3187 if (!dbus_message_iter_init(reply, &iter) ||
3188 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
3189 log_error("Failed to parse reply.");
3194 dbus_message_iter_recurse(&iter, &sub);
3196 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
3197 log_error("Failed to parse reply.");
3202 dbus_message_iter_get_basic(&sub, &id);
3210 dbus_message_unref(m);
3213 dbus_message_unref(reply);
3215 dbus_error_free(&error);
3220 static int delete_snapshot(DBusConnection *bus, char **args) {
3221 DBusMessage *m = NULL, *reply = NULL;
3229 dbus_error_init(&error);
3231 STRV_FOREACH(name, args+1) {
3232 const char *path = NULL;
3234 if (!(m = dbus_message_new_method_call(
3235 "org.freedesktop.systemd1",
3236 "/org/freedesktop/systemd1",
3237 "org.freedesktop.systemd1.Manager",
3239 log_error("Could not allocate message.");
3244 if (!dbus_message_append_args(m,
3245 DBUS_TYPE_STRING, name,
3246 DBUS_TYPE_INVALID)) {
3247 log_error("Could not append arguments to message.");
3252 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3253 log_error("Failed to issue method call: %s", bus_error_message(&error));
3258 if (!dbus_message_get_args(reply, &error,
3259 DBUS_TYPE_OBJECT_PATH, &path,
3260 DBUS_TYPE_INVALID)) {
3261 log_error("Failed to parse reply: %s", bus_error_message(&error));
3266 dbus_message_unref(m);
3267 if (!(m = dbus_message_new_method_call(
3268 "org.freedesktop.systemd1",
3270 "org.freedesktop.systemd1.Snapshot",
3272 log_error("Could not allocate message.");
3277 dbus_message_unref(reply);
3278 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3279 log_error("Failed to issue method call: %s", bus_error_message(&error));
3284 dbus_message_unref(m);
3285 dbus_message_unref(reply);
3293 dbus_message_unref(m);
3296 dbus_message_unref(reply);
3298 dbus_error_free(&error);
3303 static int daemon_reload(DBusConnection *bus, char **args) {
3304 DBusMessage *m = NULL, *reply = NULL;
3309 dbus_error_init(&error);
3311 if (arg_action == ACTION_RELOAD)
3313 else if (arg_action == ACTION_REEXEC)
3314 method = "Reexecute";
3316 assert(arg_action == ACTION_SYSTEMCTL);
3319 streq(args[0], "clear-jobs") ||
3320 streq(args[0], "cancel") ? "ClearJobs" :
3321 streq(args[0], "daemon-reexec") ? "Reexecute" :
3322 streq(args[0], "reset-failed") ? "ResetFailed" :
3323 streq(args[0], "halt") ? "Halt" :
3324 streq(args[0], "poweroff") ? "PowerOff" :
3325 streq(args[0], "reboot") ? "Reboot" :
3326 streq(args[0], "kexec") ? "KExec" :
3327 streq(args[0], "exit") ? "Exit" :
3328 /* "daemon-reload" */ "Reload";
3331 if (!(m = dbus_message_new_method_call(
3332 "org.freedesktop.systemd1",
3333 "/org/freedesktop/systemd1",
3334 "org.freedesktop.systemd1.Manager",
3336 log_error("Could not allocate message.");
3340 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3342 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
3343 /* There's always a fallback possible for
3344 * legacy actions. */
3349 if (streq(method, "Reexecute") && dbus_error_has_name(&error, DBUS_ERROR_NO_REPLY)) {
3350 /* On reexecution, we expect a disconnect, not
3356 log_error("Failed to issue method call: %s", bus_error_message(&error));
3365 dbus_message_unref(m);
3368 dbus_message_unref(reply);
3370 dbus_error_free(&error);
3375 static int reset_failed(DBusConnection *bus, char **args) {
3376 DBusMessage *m = NULL;
3382 dbus_error_init(&error);
3384 if (strv_length(args) <= 1)
3385 return daemon_reload(bus, args);
3387 STRV_FOREACH(name, args+1) {
3390 if (!(m = dbus_message_new_method_call(
3391 "org.freedesktop.systemd1",
3392 "/org/freedesktop/systemd1",
3393 "org.freedesktop.systemd1.Manager",
3394 "ResetFailedUnit"))) {
3395 log_error("Could not allocate message.");
3400 if (!dbus_message_append_args(m,
3401 DBUS_TYPE_STRING, name,
3402 DBUS_TYPE_INVALID)) {
3403 log_error("Could not append arguments to message.");
3408 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3409 log_error("Failed to issue method call: %s", bus_error_message(&error));
3414 dbus_message_unref(m);
3415 dbus_message_unref(reply);
3423 dbus_message_unref(m);
3425 dbus_error_free(&error);
3430 static int show_enviroment(DBusConnection *bus, char **args) {
3431 DBusMessage *m = NULL, *reply = NULL;
3433 DBusMessageIter iter, sub, sub2;
3436 *interface = "org.freedesktop.systemd1.Manager",
3437 *property = "Environment";
3439 dbus_error_init(&error);
3441 pager_open_if_enabled();
3443 if (!(m = dbus_message_new_method_call(
3444 "org.freedesktop.systemd1",
3445 "/org/freedesktop/systemd1",
3446 "org.freedesktop.DBus.Properties",
3448 log_error("Could not allocate message.");
3452 if (!dbus_message_append_args(m,
3453 DBUS_TYPE_STRING, &interface,
3454 DBUS_TYPE_STRING, &property,
3455 DBUS_TYPE_INVALID)) {
3456 log_error("Could not append arguments to message.");
3461 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3462 log_error("Failed to issue method call: %s", bus_error_message(&error));
3467 if (!dbus_message_iter_init(reply, &iter) ||
3468 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
3469 log_error("Failed to parse reply.");
3474 dbus_message_iter_recurse(&iter, &sub);
3476 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
3477 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
3478 log_error("Failed to parse reply.");
3483 dbus_message_iter_recurse(&sub, &sub2);
3485 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
3488 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
3489 log_error("Failed to parse reply.");
3494 dbus_message_iter_get_basic(&sub2, &text);
3495 printf("%s\n", text);
3497 dbus_message_iter_next(&sub2);
3504 dbus_message_unref(m);
3507 dbus_message_unref(reply);
3509 dbus_error_free(&error);
3514 static int set_environment(DBusConnection *bus, char **args) {
3515 DBusMessage *m = NULL, *reply = NULL;
3519 DBusMessageIter iter, sub;
3522 dbus_error_init(&error);
3524 method = streq(args[0], "set-environment")
3526 : "UnsetEnvironment";
3528 if (!(m = dbus_message_new_method_call(
3529 "org.freedesktop.systemd1",
3530 "/org/freedesktop/systemd1",
3531 "org.freedesktop.systemd1.Manager",
3534 log_error("Could not allocate message.");
3538 dbus_message_iter_init_append(m, &iter);
3540 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
3541 log_error("Could not append arguments to message.");
3546 STRV_FOREACH(name, args+1)
3547 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, name)) {
3548 log_error("Could not append arguments to message.");
3553 if (!dbus_message_iter_close_container(&iter, &sub)) {
3554 log_error("Could not append arguments to message.");
3559 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3560 log_error("Failed to issue method call: %s", bus_error_message(&error));
3569 dbus_message_unref(m);
3572 dbus_message_unref(reply);
3574 dbus_error_free(&error);
3579 static int enable_sysv_units(char **args) {
3582 #if defined (HAVE_SYSV_COMPAT) && (defined(TARGET_FEDORA) || defined(TARGET_MANDRIVA) || defined(TARGET_SUSE) || defined(TARGET_MEEGO) || defined(TARGET_ALTLINUX) || defined(TARGET_MAGEIA))
3583 const char *verb = args[0];
3584 unsigned f = 1, t = 1;
3587 if (arg_scope != UNIT_FILE_SYSTEM)
3590 if (!streq(verb, "enable") &&
3591 !streq(verb, "disable") &&
3592 !streq(verb, "is-enabled"))
3595 /* Processes all SysV units, and reshuffles the array so that
3596 * afterwards only the native units remain */
3599 r = lookup_paths_init(&paths, MANAGER_SYSTEM, false);
3605 for (f = 1; args[f]; f++) {
3608 bool found_native = false, found_sysv;
3610 const char *argv[6] = { "/sbin/chkconfig", NULL, NULL, NULL, NULL };
3611 char **k, *l, *q = NULL;
3618 if (!endswith(name, ".service"))
3621 if (path_is_absolute(name))
3624 STRV_FOREACH(k, paths.unit_path) {
3627 if (!isempty(arg_root))
3628 asprintf(&p, "%s/%s/%s", arg_root, *k, name);
3630 asprintf(&p, "%s/%s", *k, name);
3633 log_error("No memory");
3638 found_native = access(p, F_OK) >= 0;
3649 if (!isempty(arg_root))
3650 asprintf(&p, "%s/" SYSTEM_SYSVINIT_PATH "/%s", arg_root, name);
3652 asprintf(&p, SYSTEM_SYSVINIT_PATH "/%s", name);
3654 log_error("No memory");
3659 p[strlen(p) - sizeof(".service") + 1] = 0;
3660 found_sysv = access(p, F_OK) >= 0;
3667 /* Mark this entry, so that we don't try enabling it as native unit */
3668 args[f] = (char*) "";
3670 log_info("%s is not a native service, redirecting to /sbin/chkconfig.", name);
3672 if (!isempty(arg_root))
3673 argv[c++] = q = strappend("--root=", arg_root);
3675 argv[c++] = file_name_from_path(p);
3677 streq(verb, "enable") ? "on" :
3678 streq(verb, "disable") ? "off" : "--level=5";
3681 l = strv_join((char**)argv, " ");
3683 log_error("No memory.");
3690 log_info("Executing %s", l);
3695 log_error("Failed to fork: %m");
3700 } else if (pid == 0) {
3703 execv(argv[0], (char**) argv);
3704 _exit(EXIT_FAILURE);
3710 j = wait_for_terminate(pid, &status);
3712 log_error("Failed to wait for child: %s", strerror(-r));
3717 if (status.si_code == CLD_EXITED) {
3718 if (streq(verb, "is-enabled")) {
3719 if (status.si_status == 0) {
3728 } else if (status.si_status != 0) {
3739 lookup_paths_free(&paths);
3741 /* Drop all SysV units */
3742 for (f = 1, t = 1; args[f]; f++) {
3744 if (isempty(args[f]))
3747 args[t++] = args[f];
3756 static int enable_unit(DBusConnection *bus, char **args) {
3757 const char *verb = args[0];
3758 UnitFileChange *changes = NULL;
3759 unsigned n_changes = 0, i;
3760 int carries_install_info = -1;
3761 DBusMessage *m = NULL, *reply = NULL;
3765 r = enable_sysv_units(args);
3772 dbus_error_init(&error);
3774 if (!bus || avoid_bus()) {
3775 if (streq(verb, "enable")) {
3776 r = unit_file_enable(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3777 carries_install_info = r;
3778 } else if (streq(verb, "disable"))
3779 r = unit_file_disable(arg_scope, arg_runtime, arg_root, args+1, &changes, &n_changes);
3780 else if (streq(verb, "reenable")) {
3781 r = unit_file_reenable(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3782 carries_install_info = r;
3783 } else if (streq(verb, "link"))
3784 r = unit_file_link(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3785 else if (streq(verb, "preset")) {
3786 r = unit_file_preset(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3787 carries_install_info = r;
3788 } else if (streq(verb, "mask"))
3789 r = unit_file_mask(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3790 else if (streq(verb, "unmask"))
3791 r = unit_file_unmask(arg_scope, arg_runtime, arg_root, args+1, &changes, &n_changes);
3793 assert_not_reached("Unknown verb");
3796 log_error("Operation failed: %s", strerror(-r));
3801 for (i = 0; i < n_changes; i++) {
3802 if (changes[i].type == UNIT_FILE_SYMLINK)
3803 log_info("ln -s '%s' '%s'", changes[i].source, changes[i].path);
3805 log_info("rm '%s'", changes[i].path);
3811 bool send_force = true, expect_carries_install_info = false;
3813 DBusMessageIter iter, sub, sub2;
3815 if (streq(verb, "enable")) {
3816 method = "EnableUnitFiles";
3817 expect_carries_install_info = true;
3818 } else if (streq(verb, "disable")) {
3819 method = "DisableUnitFiles";
3821 } else if (streq(verb, "reenable")) {
3822 method = "ReenableUnitFiles";
3823 expect_carries_install_info = true;
3824 } else if (streq(verb, "link"))
3825 method = "LinkUnitFiles";
3826 else if (streq(verb, "preset")) {
3827 method = "PresetUnitFiles";
3828 expect_carries_install_info = true;
3829 } else if (streq(verb, "mask"))
3830 method = "MaskUnitFiles";
3831 else if (streq(verb, "unmask")) {
3832 method = "UnmaskUnitFiles";
3835 assert_not_reached("Unknown verb");
3837 m = dbus_message_new_method_call(
3838 "org.freedesktop.systemd1",
3839 "/org/freedesktop/systemd1",
3840 "org.freedesktop.systemd1.Manager",
3843 log_error("Out of memory");
3848 dbus_message_iter_init_append(m, &iter);
3850 r = bus_append_strv_iter(&iter, args+1);
3852 log_error("Failed to append unit files.");
3857 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &a)) {
3858 log_error("Failed to append runtime boolean.");
3866 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b)) {
3867 log_error("Failed to append force boolean.");
3873 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
3875 log_error("Failed to issue method call: %s", bus_error_message(&error));
3880 if (!dbus_message_iter_init(reply, &iter)) {
3881 log_error("Failed to initialize iterator.");
3885 if (expect_carries_install_info) {
3886 r = bus_iter_get_basic_and_next(&iter, DBUS_TYPE_BOOLEAN, &b, true);
3888 log_error("Failed to parse reply.");
3892 carries_install_info = b;
3895 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
3896 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
3897 log_error("Failed to parse reply.");
3902 dbus_message_iter_recurse(&iter, &sub);
3903 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
3904 const char *type, *path, *source;
3906 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
3907 log_error("Failed to parse reply.");
3912 dbus_message_iter_recurse(&sub, &sub2);
3914 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
3915 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0 ||
3916 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &source, false) < 0) {
3917 log_error("Failed to parse reply.");
3923 if (streq(type, "symlink"))
3924 log_info("ln -s '%s' '%s'", source, path);
3926 log_info("rm '%s'", path);
3929 dbus_message_iter_next(&sub);
3932 /* Try to reload if enabeld */
3934 r = daemon_reload(bus, args);
3937 if (carries_install_info == 0)
3938 log_warning("Warning: unit files do not carry install information. No operation executed.");
3942 dbus_message_unref(m);
3945 dbus_message_unref(reply);
3947 unit_file_changes_free(changes, n_changes);
3949 dbus_error_free(&error);
3953 static int unit_is_enabled(DBusConnection *bus, char **args) {
3956 DBusMessage *m = NULL, *reply = NULL;
3960 dbus_error_init(&error);
3962 r = enable_sysv_units(args);
3968 if (!bus || avoid_bus()) {
3970 STRV_FOREACH(name, args+1) {
3971 UnitFileState state;
3973 state = unit_file_get_state(arg_scope, arg_root, *name);
3979 if (state == UNIT_FILE_ENABLED ||
3980 state == UNIT_FILE_ENABLED_RUNTIME ||
3981 state == UNIT_FILE_STATIC)
3985 puts(unit_file_state_to_string(state));
3989 STRV_FOREACH(name, args+1) {
3992 m = dbus_message_new_method_call(
3993 "org.freedesktop.systemd1",
3994 "/org/freedesktop/systemd1",
3995 "org.freedesktop.systemd1.Manager",
3996 "GetUnitFileState");
3998 log_error("Out of memory");
4003 if (!dbus_message_append_args(m,
4004 DBUS_TYPE_STRING, name,
4005 DBUS_TYPE_INVALID)) {
4006 log_error("Could not append arguments to message.");
4011 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
4013 log_error("Failed to issue method call: %s", bus_error_message(&error));
4018 if (!dbus_message_get_args(reply, &error,
4019 DBUS_TYPE_STRING, &s,
4020 DBUS_TYPE_INVALID)) {
4021 log_error("Failed to parse reply: %s", bus_error_message(&error));
4026 dbus_message_unref(m);
4027 dbus_message_unref(reply);
4030 if (streq(s, "enabled") ||
4031 streq(s, "enabled-runtime") ||
4040 r = enabled ? 0 : 1;
4044 dbus_message_unref(m);
4047 dbus_message_unref(reply);
4049 dbus_error_free(&error);
4053 static int systemctl_help(void) {
4055 pager_open_if_enabled();
4057 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
4058 "Query or send control commands to the systemd manager.\n\n"
4059 " -h --help Show this help\n"
4060 " --version Show package version\n"
4061 " -t --type=TYPE List only units of a particular type\n"
4062 " -p --property=NAME Show only properties by this name\n"
4063 " -a --all Show all units/properties, including dead/empty ones\n"
4064 " --failed Show only failed units\n"
4065 " --full Don't ellipsize unit names on output\n"
4066 " --fail When queueing a new job, fail if conflicting jobs are\n"
4068 " --ignore-dependencies\n"
4069 " When queueing a new job, ignore all its dependencies\n"
4070 " --kill-who=WHO Who to send signal to\n"
4071 " -s --signal=SIGNAL Which signal to send\n"
4072 " -H --host=[USER@]HOST\n"
4073 " Show information for remote host\n"
4074 " -P --privileged Acquire privileges before execution\n"
4075 " -q --quiet Suppress output\n"
4076 " --no-block Do not wait until operation finished\n"
4077 " --no-wall Don't send wall message before halt/power-off/reboot\n"
4078 " --no-reload When enabling/disabling unit files, don't reload daemon\n"
4080 " --no-legend Do not print a legend (column headers and hints)\n"
4081 " --no-pager Do not pipe output into a pager\n"
4082 " --no-ask-password\n"
4083 " Do not ask for system passwords\n"
4084 " --order When generating graph for dot, show only order\n"
4085 " --require When generating graph for dot, show only requirement\n"
4086 " --system Connect to system manager\n"
4087 " --user Connect to user service manager\n"
4088 " --global Enable/disable unit files globally\n"
4089 " -f --force When enabling unit files, override existing symlinks\n"
4090 " When shutting down, execute action immediately\n"
4091 " --root=PATH Enable unit files in the specified root directory\n"
4092 " --runtime Enable unit files only temporarily until next reboot\n"
4093 " -n --lines=INTEGER Journal entries to show\n"
4094 " --follow Follow journal\n"
4095 " -o --output=STRING Change journal output mode (short, short-monotonic,\n"
4096 " verbose, export, json, cat)\n\n"
4098 " list-units List loaded units\n"
4099 " start [NAME...] Start (activate) one or more units\n"
4100 " stop [NAME...] Stop (deactivate) one or more units\n"
4101 " reload [NAME...] Reload one or more units\n"
4102 " restart [NAME...] Start or restart one or more units\n"
4103 " try-restart [NAME...] Restart one or more units if active\n"
4104 " reload-or-restart [NAME...] Reload one or more units is possible,\n"
4105 " otherwise start or restart\n"
4106 " reload-or-try-restart [NAME...] Reload one or more units is possible,\n"
4107 " otherwise restart if active\n"
4108 " isolate [NAME] Start one unit and stop all others\n"
4109 " kill [NAME...] Send signal to processes of a unit\n"
4110 " is-active [NAME...] Check whether units are active\n"
4111 " status [NAME...|PID...] Show runtime status of one or more units\n"
4112 " show [NAME...|JOB...] Show properties of one or more\n"
4113 " units/jobs or the manager\n"
4114 " reset-failed [NAME...] Reset failed state for all, one, or more\n"
4116 " load [NAME...] Load one or more units\n\n"
4117 "Unit File Commands:\n"
4118 " list-unit-files List installed unit files\n"
4119 " enable [NAME...] Enable one or more unit files\n"
4120 " disable [NAME...] Disable one or more unit files\n"
4121 " reenable [NAME...] Reenable one or more unit files\n"
4122 " preset [NAME...] Enable/disable one or more unit files\n"
4123 " based on preset configuration\n"
4124 " mask [NAME...] Mask one or more units\n"
4125 " unmask [NAME...] Unmask one or more units\n"
4126 " link [PATH...] Link one or more units files into\n"
4127 " the search path\n"
4128 " is-enabled [NAME...] Check whether unit files are enabled\n\n"
4130 " list-jobs List jobs\n"
4131 " cancel [JOB...] Cancel all, one, or more jobs\n\n"
4132 "Status Commands:\n"
4133 " dump Dump server status\n"
4134 " dot Dump dependency graph for dot(1)\n\n"
4135 "Snapshot Commands:\n"
4136 " snapshot [NAME] Create a snapshot\n"
4137 " delete [NAME...] Remove one or more snapshots\n\n"
4138 "Environment Commands:\n"
4139 " show-environment Dump environment\n"
4140 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
4141 " unset-environment [NAME...] Unset one or more environment variables\n\n"
4142 "Manager Lifecycle Commands:\n"
4143 " daemon-reload Reload systemd manager configuration\n"
4144 " daemon-reexec Reexecute systemd manager\n\n"
4145 "System Commands:\n"
4146 " default Enter system default mode\n"
4147 " rescue Enter system rescue mode\n"
4148 " emergency Enter system emergency mode\n"
4149 " halt Shut down and halt the system\n"
4150 " poweroff Shut down and power-off the system\n"
4151 " reboot Shut down and reboot the system\n"
4152 " kexec Shut down and reboot the system with kexec\n"
4153 " exit Ask for user instance termination\n",
4154 program_invocation_short_name);
4159 static int halt_help(void) {
4161 printf("%s [OPTIONS...]\n\n"
4162 "%s the system.\n\n"
4163 " --help Show this help\n"
4164 " --halt Halt the machine\n"
4165 " -p --poweroff Switch off the machine\n"
4166 " --reboot Reboot the machine\n"
4167 " -f --force Force immediate halt/power-off/reboot\n"
4168 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
4169 " -d --no-wtmp Don't write wtmp record\n"
4170 " -n --no-sync Don't sync before halt/power-off/reboot\n"
4171 " --no-wall Don't send wall message before halt/power-off/reboot\n",
4172 program_invocation_short_name,
4173 arg_action == ACTION_REBOOT ? "Reboot" :
4174 arg_action == ACTION_POWEROFF ? "Power off" :
4180 static int shutdown_help(void) {
4182 printf("%s [OPTIONS...] [TIME] [WALL...]\n\n"
4183 "Shut down the system.\n\n"
4184 " --help Show this help\n"
4185 " -H --halt Halt the machine\n"
4186 " -P --poweroff Power-off the machine\n"
4187 " -r --reboot Reboot the machine\n"
4188 " -h Equivalent to --poweroff, overriden by --halt\n"
4189 " -k Don't halt/power-off/reboot, just send warnings\n"
4190 " --no-wall Don't send wall message before halt/power-off/reboot\n"
4191 " -c Cancel a pending shutdown\n",
4192 program_invocation_short_name);
4197 static int telinit_help(void) {
4199 printf("%s [OPTIONS...] {COMMAND}\n\n"
4200 "Send control commands to the init daemon.\n\n"
4201 " --help Show this help\n"
4202 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
4204 " 0 Power-off the machine\n"
4205 " 6 Reboot the machine\n"
4206 " 2, 3, 4, 5 Start runlevelX.target unit\n"
4207 " 1, s, S Enter rescue mode\n"
4208 " q, Q Reload init daemon configuration\n"
4209 " u, U Reexecute init daemon\n",
4210 program_invocation_short_name);
4215 static int runlevel_help(void) {
4217 printf("%s [OPTIONS...]\n\n"
4218 "Prints the previous and current runlevel of the init system.\n\n"
4219 " --help Show this help\n",
4220 program_invocation_short_name);
4225 static int systemctl_parse_argv(int argc, char *argv[]) {
4229 ARG_IGNORE_DEPENDENCIES,
4245 ARG_NO_ASK_PASSWORD,
4252 static const struct option options[] = {
4253 { "help", no_argument, NULL, 'h' },
4254 { "version", no_argument, NULL, ARG_VERSION },
4255 { "type", required_argument, NULL, 't' },
4256 { "property", required_argument, NULL, 'p' },
4257 { "all", no_argument, NULL, 'a' },
4258 { "failed", no_argument, NULL, ARG_FAILED },
4259 { "full", no_argument, NULL, ARG_FULL },
4260 { "fail", no_argument, NULL, ARG_FAIL },
4261 { "ignore-dependencies", no_argument, NULL, ARG_IGNORE_DEPENDENCIES },
4262 { "user", no_argument, NULL, ARG_USER },
4263 { "system", no_argument, NULL, ARG_SYSTEM },
4264 { "global", no_argument, NULL, ARG_GLOBAL },
4265 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
4266 { "no-legend", no_argument, NULL, ARG_NO_LEGEND },
4267 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
4268 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4269 { "quiet", no_argument, NULL, 'q' },
4270 { "order", no_argument, NULL, ARG_ORDER },
4271 { "require", no_argument, NULL, ARG_REQUIRE },
4272 { "root", required_argument, NULL, ARG_ROOT },
4273 { "force", no_argument, NULL, ARG_FORCE },
4274 { "no-reload", no_argument, NULL, ARG_NO_RELOAD },
4275 { "kill-mode", required_argument, NULL, ARG_KILL_MODE }, /* undocumented on purpose */
4276 { "kill-who", required_argument, NULL, ARG_KILL_WHO },
4277 { "signal", required_argument, NULL, 's' },
4278 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
4279 { "host", required_argument, NULL, 'H' },
4280 { "privileged",no_argument, NULL, 'P' },
4281 { "runtime", no_argument, NULL, ARG_RUNTIME },
4282 { "lines", required_argument, NULL, 'n' },
4283 { "follow", no_argument, NULL, ARG_FOLLOW },
4284 { "output", required_argument, NULL, 'o' },
4285 { NULL, 0, NULL, 0 }
4293 /* Only when running as systemctl we ask for passwords */
4294 arg_ask_password = true;
4296 while ((c = getopt_long(argc, argv, "ht:p:aqfs:H:Pn:o:", options, NULL)) >= 0) {
4305 puts(PACKAGE_STRING);
4307 puts(SYSTEMD_FEATURES);
4317 if (!(l = strv_append(arg_property, optarg)))
4320 strv_free(arg_property);
4323 /* If the user asked for a particular
4324 * property, show it to him, even if it is
4335 arg_job_mode = "fail";
4338 case ARG_IGNORE_DEPENDENCIES:
4339 arg_job_mode = "ignore-dependencies";
4343 arg_scope = UNIT_FILE_USER;
4347 arg_scope = UNIT_FILE_SYSTEM;
4351 arg_scope = UNIT_FILE_GLOBAL;
4355 arg_no_block = true;
4359 arg_no_legend = true;
4363 arg_no_pager = true;
4371 arg_dot = DOT_ORDER;
4375 arg_dot = DOT_REQUIRE;
4403 /* -f is short for both --follow and --force! */
4409 arg_no_reload = true;
4413 arg_kill_who = optarg;
4417 arg_kill_mode = optarg;
4421 if ((arg_signal = signal_from_string_try_harder(optarg)) < 0) {
4422 log_error("Failed to parse signal string %s.", optarg);
4427 case ARG_NO_ASK_PASSWORD:
4428 arg_ask_password = false;
4432 arg_transport = TRANSPORT_POLKIT;
4436 arg_transport = TRANSPORT_SSH;
4445 if (safe_atou(optarg, &arg_lines) < 0) {
4446 log_error("Failed to parse lines '%s'", optarg);
4452 arg_output = output_mode_from_string(optarg);
4453 if (arg_output < 0) {
4454 log_error("Unknown output '%s'.", optarg);
4463 log_error("Unknown option code %c", c);
4468 if (arg_transport != TRANSPORT_NORMAL && arg_scope != UNIT_FILE_SYSTEM) {
4469 log_error("Cannot access user instance remotely.");
4476 static int halt_parse_argv(int argc, char *argv[]) {
4485 static const struct option options[] = {
4486 { "help", no_argument, NULL, ARG_HELP },
4487 { "halt", no_argument, NULL, ARG_HALT },
4488 { "poweroff", no_argument, NULL, 'p' },
4489 { "reboot", no_argument, NULL, ARG_REBOOT },
4490 { "force", no_argument, NULL, 'f' },
4491 { "wtmp-only", no_argument, NULL, 'w' },
4492 { "no-wtmp", no_argument, NULL, 'd' },
4493 { "no-sync", no_argument, NULL, 'n' },
4494 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4495 { NULL, 0, NULL, 0 }
4503 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
4504 if (runlevel == '0' || runlevel == '6')
4505 arg_immediate = true;
4507 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
4515 arg_action = ACTION_HALT;
4519 if (arg_action != ACTION_REBOOT)
4520 arg_action = ACTION_POWEROFF;
4524 arg_action = ACTION_REBOOT;
4528 arg_immediate = true;
4549 /* Compatibility nops */
4556 log_error("Unknown option code %c", c);
4561 if (optind < argc) {
4562 log_error("Too many arguments.");
4569 static int parse_time_spec(const char *t, usec_t *_u) {
4573 if (streq(t, "now"))
4575 else if (!strchr(t, ':')) {
4578 if (safe_atou64(t, &u) < 0)
4581 *_u = now(CLOCK_REALTIME) + USEC_PER_MINUTE * u;
4590 hour = strtol(t, &e, 10);
4591 if (errno != 0 || *e != ':' || hour < 0 || hour > 23)
4594 minute = strtol(e+1, &e, 10);
4595 if (errno != 0 || *e != 0 || minute < 0 || minute > 59)
4598 n = now(CLOCK_REALTIME);
4599 s = (time_t) (n / USEC_PER_SEC);
4602 assert_se(localtime_r(&s, &tm));
4604 tm.tm_hour = (int) hour;
4605 tm.tm_min = (int) minute;
4608 assert_se(s = mktime(&tm));
4610 *_u = (usec_t) s * USEC_PER_SEC;
4613 *_u += USEC_PER_DAY;
4619 static int shutdown_parse_argv(int argc, char *argv[]) {
4626 static const struct option options[] = {
4627 { "help", no_argument, NULL, ARG_HELP },
4628 { "halt", no_argument, NULL, 'H' },
4629 { "poweroff", no_argument, NULL, 'P' },
4630 { "reboot", no_argument, NULL, 'r' },
4631 { "kexec", no_argument, NULL, 'K' }, /* not documented extension */
4632 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4633 { NULL, 0, NULL, 0 }
4641 while ((c = getopt_long(argc, argv, "HPrhkt:afFc", options, NULL)) >= 0) {
4649 arg_action = ACTION_HALT;
4653 arg_action = ACTION_POWEROFF;
4658 arg_action = ACTION_KEXEC;
4660 arg_action = ACTION_REBOOT;
4664 arg_action = ACTION_KEXEC;
4668 if (arg_action != ACTION_HALT)
4669 arg_action = ACTION_POWEROFF;
4682 /* Compatibility nops */
4686 arg_action = ACTION_CANCEL_SHUTDOWN;
4693 log_error("Unknown option code %c", c);
4698 if (argc > optind) {
4699 r = parse_time_spec(argv[optind], &arg_when);
4701 log_error("Failed to parse time specification: %s", argv[optind]);
4705 arg_when = now(CLOCK_REALTIME) + USEC_PER_MINUTE;
4707 /* We skip the time argument */
4708 if (argc > optind + 1)
4709 arg_wall = argv + optind + 1;
4716 static int telinit_parse_argv(int argc, char *argv[]) {
4723 static const struct option options[] = {
4724 { "help", no_argument, NULL, ARG_HELP },
4725 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4726 { NULL, 0, NULL, 0 }
4729 static const struct {
4733 { '0', ACTION_POWEROFF },
4734 { '6', ACTION_REBOOT },
4735 { '1', ACTION_RESCUE },
4736 { '2', ACTION_RUNLEVEL2 },
4737 { '3', ACTION_RUNLEVEL3 },
4738 { '4', ACTION_RUNLEVEL4 },
4739 { '5', ACTION_RUNLEVEL5 },
4740 { 's', ACTION_RESCUE },
4741 { 'S', ACTION_RESCUE },
4742 { 'q', ACTION_RELOAD },
4743 { 'Q', ACTION_RELOAD },
4744 { 'u', ACTION_REEXEC },
4745 { 'U', ACTION_REEXEC }
4754 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4769 log_error("Unknown option code %c", c);
4774 if (optind >= argc) {
4779 if (optind + 1 < argc) {
4780 log_error("Too many arguments.");
4784 if (strlen(argv[optind]) != 1) {
4785 log_error("Expected single character argument.");
4789 for (i = 0; i < ELEMENTSOF(table); i++)
4790 if (table[i].from == argv[optind][0])
4793 if (i >= ELEMENTSOF(table)) {
4794 log_error("Unknown command %s.", argv[optind]);
4798 arg_action = table[i].to;
4805 static int runlevel_parse_argv(int argc, char *argv[]) {
4811 static const struct option options[] = {
4812 { "help", no_argument, NULL, ARG_HELP },
4813 { NULL, 0, NULL, 0 }
4821 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4832 log_error("Unknown option code %c", c);
4837 if (optind < argc) {
4838 log_error("Too many arguments.");
4845 static int parse_argv(int argc, char *argv[]) {
4849 if (program_invocation_short_name) {
4851 if (strstr(program_invocation_short_name, "halt")) {
4852 arg_action = ACTION_HALT;
4853 return halt_parse_argv(argc, argv);
4854 } else if (strstr(program_invocation_short_name, "poweroff")) {
4855 arg_action = ACTION_POWEROFF;
4856 return halt_parse_argv(argc, argv);
4857 } else if (strstr(program_invocation_short_name, "reboot")) {
4859 arg_action = ACTION_KEXEC;
4861 arg_action = ACTION_REBOOT;
4862 return halt_parse_argv(argc, argv);
4863 } else if (strstr(program_invocation_short_name, "shutdown")) {
4864 arg_action = ACTION_POWEROFF;
4865 return shutdown_parse_argv(argc, argv);
4866 } else if (strstr(program_invocation_short_name, "init")) {
4868 if (sd_booted() > 0) {
4869 arg_action = ACTION_INVALID;
4870 return telinit_parse_argv(argc, argv);
4872 /* Hmm, so some other init system is
4873 * running, we need to forward this
4874 * request to it. For now we simply
4875 * guess that it is Upstart. */
4877 execv("/lib/upstart/telinit", argv);
4879 log_error("Couldn't find an alternative telinit implementation to spawn.");
4883 } else if (strstr(program_invocation_short_name, "runlevel")) {
4884 arg_action = ACTION_RUNLEVEL;
4885 return runlevel_parse_argv(argc, argv);
4889 arg_action = ACTION_SYSTEMCTL;
4890 return systemctl_parse_argv(argc, argv);
4893 static int action_to_runlevel(void) {
4895 static const char table[_ACTION_MAX] = {
4896 [ACTION_HALT] = '0',
4897 [ACTION_POWEROFF] = '0',
4898 [ACTION_REBOOT] = '6',
4899 [ACTION_RUNLEVEL2] = '2',
4900 [ACTION_RUNLEVEL3] = '3',
4901 [ACTION_RUNLEVEL4] = '4',
4902 [ACTION_RUNLEVEL5] = '5',
4903 [ACTION_RESCUE] = '1'
4906 assert(arg_action < _ACTION_MAX);
4908 return table[arg_action];
4911 static int talk_upstart(void) {
4912 DBusMessage *m = NULL, *reply = NULL;
4914 int previous, rl, r;
4916 env1_buf[] = "RUNLEVEL=X",
4917 env2_buf[] = "PREVLEVEL=X";
4918 char *env1 = env1_buf, *env2 = env2_buf;
4919 const char *emit = "runlevel";
4920 dbus_bool_t b_false = FALSE;
4921 DBusMessageIter iter, sub;
4922 DBusConnection *bus;
4924 dbus_error_init(&error);
4926 if (!(rl = action_to_runlevel()))
4929 if (utmp_get_runlevel(&previous, NULL) < 0)
4932 if (!(bus = dbus_connection_open_private("unix:abstract=/com/ubuntu/upstart", &error))) {
4933 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
4938 log_error("Failed to connect to Upstart bus: %s", bus_error_message(&error));
4943 if ((r = bus_check_peercred(bus)) < 0) {
4944 log_error("Failed to verify owner of bus.");
4948 if (!(m = dbus_message_new_method_call(
4949 "com.ubuntu.Upstart",
4950 "/com/ubuntu/Upstart",
4951 "com.ubuntu.Upstart0_6",
4954 log_error("Could not allocate message.");
4959 dbus_message_iter_init_append(m, &iter);
4961 env1_buf[sizeof(env1_buf)-2] = rl;
4962 env2_buf[sizeof(env2_buf)-2] = previous;
4964 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
4965 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
4966 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
4967 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
4968 !dbus_message_iter_close_container(&iter, &sub) ||
4969 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
4970 log_error("Could not append arguments to message.");
4975 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
4977 if (error_is_no_service(&error)) {
4982 log_error("Failed to issue method call: %s", bus_error_message(&error));
4991 dbus_message_unref(m);
4994 dbus_message_unref(reply);
4997 dbus_connection_flush(bus);
4998 dbus_connection_close(bus);
4999 dbus_connection_unref(bus);
5002 dbus_error_free(&error);
5007 static int talk_initctl(void) {
5008 struct init_request request;
5012 if (!(rl = action_to_runlevel()))
5016 request.magic = INIT_MAGIC;
5017 request.sleeptime = 0;
5018 request.cmd = INIT_CMD_RUNLVL;
5019 request.runlevel = rl;
5021 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
5023 if (errno == ENOENT)
5026 log_error("Failed to open "INIT_FIFO": %m");
5031 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
5032 close_nointr_nofail(fd);
5035 log_error("Failed to write to "INIT_FIFO": %m");
5036 return errno ? -errno : -EIO;
5042 static int systemctl_main(DBusConnection *bus, int argc, char *argv[], DBusError *error) {
5044 static const struct {
5052 int (* const dispatch)(DBusConnection *bus, char **args);
5054 { "list-units", LESS, 1, list_units },
5055 { "list-unit-files", EQUAL, 1, list_unit_files },
5056 { "list-jobs", EQUAL, 1, list_jobs },
5057 { "clear-jobs", EQUAL, 1, daemon_reload },
5058 { "load", MORE, 2, load_unit },
5059 { "cancel", MORE, 2, cancel_job },
5060 { "start", MORE, 2, start_unit },
5061 { "stop", MORE, 2, start_unit },
5062 { "condstop", MORE, 2, start_unit }, /* For compatibility with ALTLinux */
5063 { "reload", MORE, 2, start_unit },
5064 { "restart", MORE, 2, start_unit },
5065 { "try-restart", MORE, 2, start_unit },
5066 { "reload-or-restart", MORE, 2, start_unit },
5067 { "reload-or-try-restart", MORE, 2, start_unit },
5068 { "force-reload", MORE, 2, start_unit }, /* For compatibility with SysV */
5069 { "condreload", MORE, 2, start_unit }, /* For compatibility with ALTLinux */
5070 { "condrestart", MORE, 2, start_unit }, /* For compatibility with RH */
5071 { "isolate", EQUAL, 2, start_unit },
5072 { "kill", MORE, 2, kill_unit },
5073 { "is-active", MORE, 2, check_unit },
5074 { "check", MORE, 2, check_unit },
5075 { "show", MORE, 1, show },
5076 { "status", MORE, 2, show },
5077 { "dump", EQUAL, 1, dump },
5078 { "dot", EQUAL, 1, dot },
5079 { "snapshot", LESS, 2, snapshot },
5080 { "delete", MORE, 2, delete_snapshot },
5081 { "daemon-reload", EQUAL, 1, daemon_reload },
5082 { "daemon-reexec", EQUAL, 1, daemon_reload },
5083 { "show-environment", EQUAL, 1, show_enviroment },
5084 { "set-environment", MORE, 2, set_environment },
5085 { "unset-environment", MORE, 2, set_environment },
5086 { "halt", EQUAL, 1, start_special },
5087 { "poweroff", EQUAL, 1, start_special },
5088 { "reboot", EQUAL, 1, start_special },
5089 { "kexec", EQUAL, 1, start_special },
5090 { "default", EQUAL, 1, start_special },
5091 { "rescue", EQUAL, 1, start_special },
5092 { "emergency", EQUAL, 1, start_special },
5093 { "exit", EQUAL, 1, start_special },
5094 { "reset-failed", MORE, 1, reset_failed },
5095 { "enable", MORE, 2, enable_unit },
5096 { "disable", MORE, 2, enable_unit },
5097 { "is-enabled", MORE, 2, unit_is_enabled },
5098 { "reenable", MORE, 2, enable_unit },
5099 { "preset", MORE, 2, enable_unit },
5100 { "mask", MORE, 2, enable_unit },
5101 { "unmask", MORE, 2, enable_unit },
5102 { "link", MORE, 2, enable_unit }
5112 left = argc - optind;
5115 /* Special rule: no arguments means "list-units" */
5118 if (streq(argv[optind], "help")) {
5123 for (i = 0; i < ELEMENTSOF(verbs); i++)
5124 if (streq(argv[optind], verbs[i].verb))
5127 if (i >= ELEMENTSOF(verbs)) {
5128 log_error("Unknown operation %s", argv[optind]);
5133 switch (verbs[i].argc_cmp) {
5136 if (left != verbs[i].argc) {
5137 log_error("Invalid number of arguments.");
5144 if (left < verbs[i].argc) {
5145 log_error("Too few arguments.");
5152 if (left > verbs[i].argc) {
5153 log_error("Too many arguments.");
5160 assert_not_reached("Unknown comparison operator.");
5163 /* Require a bus connection for all operations but
5165 if (!streq(verbs[i].verb, "enable") &&
5166 !streq(verbs[i].verb, "disable") &&
5167 !streq(verbs[i].verb, "is-enabled") &&
5168 !streq(verbs[i].verb, "list-unit-files") &&
5169 !streq(verbs[i].verb, "reenable") &&
5170 !streq(verbs[i].verb, "preset") &&
5171 !streq(verbs[i].verb, "mask") &&
5172 !streq(verbs[i].verb, "unmask") &&
5173 !streq(verbs[i].verb, "link")) {
5175 if (running_in_chroot() > 0) {
5176 log_info("Running in chroot, ignoring request.");
5181 log_error("Failed to get D-Bus connection: %s",
5182 dbus_error_is_set(error) ? error->message : "No connection to service manager.");
5188 if (!bus && !avoid_bus()) {
5189 log_error("Failed to get D-Bus connection: %s",
5190 dbus_error_is_set(error) ? error->message : "No connection to service manager.");
5195 return verbs[i].dispatch(bus, argv + optind);
5198 static int send_shutdownd(usec_t t, char mode, bool dry_run, bool warn, const char *message) {
5200 struct msghdr msghdr;
5201 struct iovec iovec[2];
5202 union sockaddr_union sockaddr;
5203 struct sd_shutdown_command c;
5205 fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
5212 c.dry_run = dry_run;
5216 sockaddr.sa.sa_family = AF_UNIX;
5217 strncpy(sockaddr.un.sun_path, "/run/systemd/shutdownd", sizeof(sockaddr.un.sun_path));
5220 msghdr.msg_name = &sockaddr;
5221 msghdr.msg_namelen = offsetof(struct sockaddr_un, sun_path) + sizeof("/run/systemd/shutdownd") - 1;
5224 iovec[0].iov_base = (char*) &c;
5225 iovec[0].iov_len = offsetof(struct sd_shutdown_command, wall_message);
5227 if (isempty(message))
5228 msghdr.msg_iovlen = 1;
5230 iovec[1].iov_base = (char*) message;
5231 iovec[1].iov_len = strlen(message);
5232 msghdr.msg_iovlen = 2;
5234 msghdr.msg_iov = iovec;
5236 if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) < 0) {
5237 close_nointr_nofail(fd);
5241 close_nointr_nofail(fd);
5245 static int reload_with_fallback(DBusConnection *bus) {
5248 /* First, try systemd via D-Bus. */
5249 if (daemon_reload(bus, NULL) >= 0)
5253 /* Nothing else worked, so let's try signals */
5254 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
5256 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
5257 log_error("kill() failed: %m");
5264 static int start_with_fallback(DBusConnection *bus) {
5267 /* First, try systemd via D-Bus. */
5268 if (start_unit(bus, NULL) >= 0)
5272 /* Hmm, talking to systemd via D-Bus didn't work. Then
5273 * let's try to talk to Upstart via D-Bus. */
5274 if (talk_upstart() > 0)
5277 /* Nothing else worked, so let's try
5279 if (talk_initctl() > 0)
5282 log_error("Failed to talk to init daemon.");
5286 warn_wall(arg_action);
5290 static void halt_now(enum action a) {
5292 /* Make sure C-A-D is handled by the kernel from this
5294 reboot(RB_ENABLE_CAD);
5299 log_info("Halting.");
5300 reboot(RB_HALT_SYSTEM);
5303 case ACTION_POWEROFF:
5304 log_info("Powering off.");
5305 reboot(RB_POWER_OFF);
5309 log_info("Rebooting.");
5310 reboot(RB_AUTOBOOT);
5314 assert_not_reached("Unknown halt action.");
5317 assert_not_reached("Uh? This shouldn't happen.");
5320 static int halt_main(DBusConnection *bus) {
5323 if (geteuid() != 0) {
5324 /* Try logind if we are a normal user and no special
5325 * mode applies. Maybe PolicyKit allows us to shutdown
5328 if (arg_when <= 0 &&
5331 (arg_action == ACTION_POWEROFF ||
5332 arg_action == ACTION_REBOOT)) {
5333 r = reboot_with_logind(bus, arg_action);
5338 log_error("Must be root.");
5345 m = strv_join(arg_wall, " ");
5346 r = send_shutdownd(arg_when,
5347 arg_action == ACTION_HALT ? 'H' :
5348 arg_action == ACTION_POWEROFF ? 'P' :
5349 arg_action == ACTION_KEXEC ? 'K' :
5357 log_warning("Failed to talk to shutdownd, proceeding with immediate shutdown: %s", strerror(-r));
5359 char date[FORMAT_TIMESTAMP_MAX];
5361 log_info("Shutdown scheduled for %s, use 'shutdown -c' to cancel.",
5362 format_timestamp(date, sizeof(date), arg_when));
5367 if (!arg_dry && !arg_immediate)
5368 return start_with_fallback(bus);
5371 if (sd_booted() > 0)
5372 log_debug("Not writing utmp record, assuming that systemd-update-utmp is used.");
5374 r = utmp_put_shutdown();
5376 log_warning("Failed to write utmp record: %s", strerror(-r));
5386 halt_now(arg_action);
5387 /* We should never reach this. */
5391 static int runlevel_main(void) {
5392 int r, runlevel, previous;
5394 r = utmp_get_runlevel(&runlevel, &previous);
5401 previous <= 0 ? 'N' : previous,
5402 runlevel <= 0 ? 'N' : runlevel);
5407 int main(int argc, char*argv[]) {
5408 int r, retval = EXIT_FAILURE;
5409 DBusConnection *bus = NULL;
5412 dbus_error_init(&error);
5414 log_parse_environment();
5417 r = parse_argv(argc, argv);
5421 retval = EXIT_SUCCESS;
5425 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
5426 * let's shortcut this */
5427 if (arg_action == ACTION_RUNLEVEL) {
5428 r = runlevel_main();
5429 retval = r < 0 ? EXIT_FAILURE : r;
5433 if (running_in_chroot() > 0 && arg_action != ACTION_SYSTEMCTL) {
5434 log_info("Running in chroot, ignoring request.");
5440 if (arg_transport == TRANSPORT_NORMAL)
5441 bus_connect(arg_scope == UNIT_FILE_SYSTEM ? DBUS_BUS_SYSTEM : DBUS_BUS_SESSION, &bus, &private_bus, &error);
5442 else if (arg_transport == TRANSPORT_POLKIT) {
5443 bus_connect_system_polkit(&bus, &error);
5444 private_bus = false;
5445 } else if (arg_transport == TRANSPORT_SSH) {
5446 bus_connect_system_ssh(NULL, arg_host, &bus, &error);
5447 private_bus = false;
5449 assert_not_reached("Uh, invalid transport...");
5452 switch (arg_action) {
5454 case ACTION_SYSTEMCTL:
5455 r = systemctl_main(bus, argc, argv, &error);
5459 case ACTION_POWEROFF:
5465 case ACTION_RUNLEVEL2:
5466 case ACTION_RUNLEVEL3:
5467 case ACTION_RUNLEVEL4:
5468 case ACTION_RUNLEVEL5:
5470 case ACTION_EMERGENCY:
5471 case ACTION_DEFAULT:
5472 r = start_with_fallback(bus);
5477 r = reload_with_fallback(bus);
5480 case ACTION_CANCEL_SHUTDOWN:
5481 r = send_shutdownd(0, 0, false, false, NULL);
5484 case ACTION_INVALID:
5485 case ACTION_RUNLEVEL:
5487 assert_not_reached("Unknown action");
5490 retval = r < 0 ? EXIT_FAILURE : r;
5494 dbus_connection_flush(bus);
5495 dbus_connection_close(bus);
5496 dbus_connection_unref(bus);
5499 dbus_error_free(&error);
5503 strv_free(arg_property);