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>
42 #include "utmp-wtmp.h"
46 #include "dbus-common.h"
47 #include "cgroup-show.h"
48 #include "cgroup-util.h"
50 #include "path-lookup.h"
51 #include "conf-parser.h"
52 #include "sd-daemon.h"
53 #include "shutdownd.h"
54 #include "exit-status.h"
55 #include "bus-errors.h"
57 #include "unit-name.h"
59 #include "spawn-agent.h"
61 #include "logs-show.h"
63 static const char *arg_type = NULL;
64 static char **arg_property = NULL;
65 static bool arg_all = false;
66 static const char *arg_job_mode = "replace";
67 static UnitFileScope arg_scope = UNIT_FILE_SYSTEM;
68 static bool arg_immediate = false;
69 static bool arg_no_block = false;
70 static bool arg_no_legend = false;
71 static bool arg_no_pager = false;
72 static bool arg_no_wtmp = false;
73 static bool arg_no_sync = false;
74 static bool arg_no_wall = false;
75 static bool arg_no_reload = false;
76 static bool arg_dry = false;
77 static bool arg_quiet = false;
78 static bool arg_full = false;
79 static bool arg_force = false;
80 static bool arg_ask_password = false;
81 static bool arg_failed = false;
82 static bool arg_runtime = false;
83 static char **arg_wall = NULL;
84 static const char *arg_kill_who = NULL;
85 static const char *arg_kill_mode = NULL;
86 static int arg_signal = SIGTERM;
87 static const char *arg_root = NULL;
88 static usec_t arg_when = 0;
107 ACTION_CANCEL_SHUTDOWN,
109 } arg_action = ACTION_SYSTEMCTL;
115 static enum transport {
119 } arg_transport = TRANSPORT_NORMAL;
120 static const char *arg_host = NULL;
122 static bool private_bus = false;
124 static int daemon_reload(DBusConnection *bus, char **args);
126 static bool on_tty(void) {
129 /* Note that this is invoked relatively early, before we start
130 * the pager. That means the value we return reflects whether
131 * we originally were started on a tty, not if we currently
132 * are. But this is intended, since we want colour and so on
133 * when run in our own pager. */
135 if (_unlikely_(t < 0))
136 t = isatty(STDOUT_FILENO) > 0;
141 static void pager_open_if_enabled(void) {
143 /* Cache result before we open the pager */
152 static void agent_open_if_enabled(void) {
154 /* Open the password agent as a child process if necessary */
156 if (!arg_ask_password)
159 if (arg_scope != UNIT_FILE_SYSTEM)
165 static const char *ansi_highlight(bool b) {
170 return b ? ANSI_HIGHLIGHT_ON : ANSI_HIGHLIGHT_OFF;
173 static const char *ansi_highlight_green(bool b) {
178 return b ? ANSI_HIGHLIGHT_GREEN_ON : ANSI_HIGHLIGHT_OFF;
181 static bool error_is_no_service(const DBusError *error) {
184 if (!dbus_error_is_set(error))
187 if (dbus_error_has_name(error, DBUS_ERROR_NAME_HAS_NO_OWNER))
190 if (dbus_error_has_name(error, DBUS_ERROR_SERVICE_UNKNOWN))
193 return startswith(error->name, "org.freedesktop.DBus.Error.Spawn.");
196 static int translate_bus_error_to_exit_status(int r, const DBusError *error) {
199 if (!dbus_error_is_set(error))
202 if (dbus_error_has_name(error, DBUS_ERROR_ACCESS_DENIED) ||
203 dbus_error_has_name(error, BUS_ERROR_ONLY_BY_DEPENDENCY) ||
204 dbus_error_has_name(error, BUS_ERROR_NO_ISOLATION) ||
205 dbus_error_has_name(error, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE))
206 return EXIT_NOPERMISSION;
208 if (dbus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT))
209 return EXIT_NOTINSTALLED;
211 if (dbus_error_has_name(error, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE) ||
212 dbus_error_has_name(error, BUS_ERROR_NOT_SUPPORTED))
213 return EXIT_NOTIMPLEMENTED;
215 if (dbus_error_has_name(error, BUS_ERROR_LOAD_FAILED))
216 return EXIT_NOTCONFIGURED;
224 static void warn_wall(enum action action) {
225 static const char *table[_ACTION_MAX] = {
226 [ACTION_HALT] = "The system is going down for system halt NOW!",
227 [ACTION_REBOOT] = "The system is going down for reboot NOW!",
228 [ACTION_POWEROFF] = "The system is going down for power-off NOW!",
229 [ACTION_KEXEC] = "The system is going down for kexec reboot NOW!",
230 [ACTION_RESCUE] = "The system is going down to rescue mode NOW!",
231 [ACTION_EMERGENCY] = "The system is going down to emergency mode NOW!"
240 if (!(p = strv_join(arg_wall, " "))) {
241 log_error("Failed to join strings.");
257 utmp_wall(table[action], NULL);
260 static bool avoid_bus(void) {
262 if (running_in_chroot() > 0)
265 if (sd_booted() <= 0)
268 if (!isempty(arg_root))
271 if (arg_scope == UNIT_FILE_GLOBAL)
279 const char *description;
280 const char *load_state;
281 const char *active_state;
282 const char *sub_state;
283 const char *following;
284 const char *unit_path;
286 const char *job_type;
287 const char *job_path;
290 static int compare_unit_info(const void *a, const void *b) {
292 const struct unit_info *u = a, *v = b;
294 d1 = strrchr(u->id, '.');
295 d2 = strrchr(v->id, '.');
300 if ((r = strcasecmp(d1, d2)) != 0)
304 return strcasecmp(u->id, v->id);
307 static bool output_show_unit(const struct unit_info *u) {
311 return streq(u->active_state, "failed");
313 return (!arg_type || ((dot = strrchr(u->id, '.')) &&
314 streq(dot+1, arg_type))) &&
315 (arg_all || !(streq(u->active_state, "inactive") || u->following[0]) || u->job_id > 0);
318 static void output_units_list(const struct unit_info *unit_infos, unsigned c) {
319 unsigned id_len, max_id_len, active_len, sub_len, job_len, desc_len, n_shown = 0;
320 const struct unit_info *u;
322 max_id_len = sizeof("UNIT")-1;
323 active_len = sizeof("ACTIVE")-1;
324 sub_len = sizeof("SUB")-1;
325 job_len = sizeof("JOB")-1;
328 for (u = unit_infos; u < unit_infos + c; u++) {
329 if (!output_show_unit(u))
332 max_id_len = MAX(max_id_len, strlen(u->id));
333 active_len = MAX(active_len, strlen(u->active_state));
334 sub_len = MAX(sub_len, strlen(u->sub_state));
336 job_len = MAX(job_len, strlen(u->job_type));
341 id_len = MIN(max_id_len, 25);
342 basic_len = 5 + id_len + 6 + active_len + sub_len + job_len;
343 if (basic_len < (unsigned) columns()) {
344 unsigned extra_len, incr;
345 extra_len = columns() - basic_len;
346 /* Either UNIT already got 25, or is fully satisfied.
347 * Grant up to 25 to DESC now. */
348 incr = MIN(extra_len, 25);
351 /* split the remaining space between UNIT and DESC,
352 * but do not give UNIT more than it needs. */
354 incr = MIN(extra_len / 2, max_id_len - id_len);
356 desc_len += extra_len - incr;
362 if (!arg_no_legend) {
363 printf("%-*s %-6s %-*s %-*s %-*s ", id_len, "UNIT", "LOAD",
364 active_len, "ACTIVE", sub_len, "SUB", job_len, "JOB");
365 if (!arg_full && arg_no_pager)
366 printf("%.*s\n", desc_len, "DESCRIPTION");
368 printf("%s\n", "DESCRIPTION");
371 for (u = unit_infos; u < unit_infos + c; u++) {
373 const char *on_loaded, *off_loaded;
374 const char *on_active, *off_active;
376 if (!output_show_unit(u))
381 if (streq(u->load_state, "error")) {
382 on_loaded = ansi_highlight(true);
383 off_loaded = ansi_highlight(false);
385 on_loaded = off_loaded = "";
387 if (streq(u->active_state, "failed")) {
388 on_active = ansi_highlight(true);
389 off_active = ansi_highlight(false);
391 on_active = off_active = "";
393 e = arg_full ? NULL : ellipsize(u->id, id_len, 33);
395 printf("%-*s %s%-6s%s %s%-*s %-*s%s %-*s ",
396 id_len, e ? e : u->id,
397 on_loaded, u->load_state, off_loaded,
398 on_active, active_len, u->active_state,
399 sub_len, u->sub_state, off_active,
400 job_len, u->job_id ? u->job_type : "");
401 if (!arg_full && arg_no_pager)
402 printf("%.*s\n", desc_len, u->description);
404 printf("%s\n", u->description);
409 if (!arg_no_legend) {
410 printf("\nLOAD = Reflects whether the unit definition was properly loaded.\n"
411 "ACTIVE = The high-level unit activation state, i.e. generalization of SUB.\n"
412 "SUB = The low-level unit activation state, values depend on unit type.\n"
413 "JOB = Pending job for the unit.\n");
416 printf("\n%u units listed.\n", n_shown);
418 printf("\n%u units listed. Pass --all to see inactive units, too.\n", n_shown);
422 static int list_units(DBusConnection *bus, char **args) {
423 DBusMessage *m = NULL, *reply = NULL;
426 DBusMessageIter iter, sub, sub2;
427 unsigned c = 0, n_units = 0;
428 struct unit_info *unit_infos = NULL;
430 dbus_error_init(&error);
434 pager_open_if_enabled();
436 if (!(m = dbus_message_new_method_call(
437 "org.freedesktop.systemd1",
438 "/org/freedesktop/systemd1",
439 "org.freedesktop.systemd1.Manager",
441 log_error("Could not allocate message.");
445 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
446 log_error("Failed to issue method call: %s", bus_error_message(&error));
451 if (!dbus_message_iter_init(reply, &iter) ||
452 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
453 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
454 log_error("Failed to parse reply.");
459 dbus_message_iter_recurse(&iter, &sub);
461 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
464 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
465 log_error("Failed to parse reply.");
473 n_units = MAX(2*c, 16);
474 w = realloc(unit_infos, sizeof(struct unit_info) * n_units);
477 log_error("Failed to allocate unit array.");
487 dbus_message_iter_recurse(&sub, &sub2);
489 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->id, true) < 0 ||
490 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->description, true) < 0 ||
491 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->load_state, true) < 0 ||
492 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->active_state, true) < 0 ||
493 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->sub_state, true) < 0 ||
494 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->following, true) < 0 ||
495 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &u->unit_path, true) < 0 ||
496 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &u->job_id, true) < 0 ||
497 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->job_type, true) < 0 ||
498 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &u->job_path, false) < 0) {
499 log_error("Failed to parse reply.");
504 dbus_message_iter_next(&sub);
509 qsort(unit_infos, c, sizeof(struct unit_info), compare_unit_info);
510 output_units_list(unit_infos, c);
517 dbus_message_unref(m);
520 dbus_message_unref(reply);
524 dbus_error_free(&error);
529 static int compare_unit_file_list(const void *a, const void *b) {
531 const UnitFileList *u = a, *v = b;
533 d1 = strrchr(u->path, '.');
534 d2 = strrchr(v->path, '.');
539 r = strcasecmp(d1, d2);
544 return strcasecmp(file_name_from_path(u->path), file_name_from_path(v->path));
547 static bool output_show_unit_file(const UnitFileList *u) {
550 return !arg_type || ((dot = strrchr(u->path, '.')) && streq(dot+1, arg_type));
553 static void output_unit_file_list(const UnitFileList *units, unsigned c) {
554 unsigned max_id_len, id_cols, state_cols, n_shown = 0;
555 const UnitFileList *u;
557 max_id_len = sizeof("UNIT FILE")-1;
558 state_cols = sizeof("STATE")-1;
559 for (u = units; u < units + c; u++) {
560 if (!output_show_unit_file(u))
563 max_id_len = MAX(max_id_len, strlen(file_name_from_path(u->path)));
564 state_cols = MAX(state_cols, strlen(unit_file_state_to_string(u->state)));
569 id_cols = MIN(max_id_len, 25);
570 basic_cols = 1 + id_cols + state_cols;
571 if (basic_cols < (unsigned) columns())
572 id_cols += MIN(columns() - basic_cols, max_id_len - id_cols);
574 id_cols = max_id_len;
577 printf("%-*s %-*s\n", id_cols, "UNIT FILE", state_cols, "STATE");
579 for (u = units; u < units + c; u++) {
581 const char *on, *off;
584 if (!output_show_unit_file(u))
589 if (u->state == UNIT_FILE_MASKED ||
590 u->state == UNIT_FILE_MASKED_RUNTIME ||
591 u->state == UNIT_FILE_DISABLED) {
592 on = ansi_highlight(true);
593 off = ansi_highlight(false);
594 } else if (u->state == UNIT_FILE_ENABLED) {
595 on = ansi_highlight_green(true);
596 off = ansi_highlight_green(false);
600 id = file_name_from_path(u->path);
602 e = arg_full ? NULL : ellipsize(id, id_cols, 33);
604 printf("%-*s %s%-*s%s\n",
606 on, state_cols, unit_file_state_to_string(u->state), off);
612 printf("\n%u unit files listed.\n", n_shown);
615 static int list_unit_files(DBusConnection *bus, char **args) {
616 DBusMessage *m = NULL, *reply = NULL;
619 DBusMessageIter iter, sub, sub2;
620 unsigned c = 0, n_units = 0;
621 UnitFileList *units = NULL;
623 dbus_error_init(&error);
625 pager_open_if_enabled();
632 h = hashmap_new(string_hash_func, string_compare_func);
634 log_error("Out of memory");
638 r = unit_file_get_list(arg_scope, arg_root, h);
640 unit_file_list_free(h);
641 log_error("Failed to get unit file list: %s", strerror(-r));
645 n_units = hashmap_size(h);
646 units = new(UnitFileList, n_units);
648 unit_file_list_free(h);
649 log_error("Out of memory");
653 HASHMAP_FOREACH(u, h, i) {
654 memcpy(units + c++, u, sizeof(UnitFileList));
662 m = dbus_message_new_method_call(
663 "org.freedesktop.systemd1",
664 "/org/freedesktop/systemd1",
665 "org.freedesktop.systemd1.Manager",
668 log_error("Could not allocate message.");
672 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
674 log_error("Failed to issue method call: %s", bus_error_message(&error));
679 if (!dbus_message_iter_init(reply, &iter) ||
680 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
681 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
682 log_error("Failed to parse reply.");
687 dbus_message_iter_recurse(&iter, &sub);
689 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
693 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
694 log_error("Failed to parse reply.");
702 n_units = MAX(2*c, 16);
703 w = realloc(units, sizeof(struct UnitFileList) * n_units);
706 log_error("Failed to allocate unit array.");
716 dbus_message_iter_recurse(&sub, &sub2);
718 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->path, true) < 0 ||
719 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, false) < 0) {
720 log_error("Failed to parse reply.");
725 u->state = unit_file_state_from_string(state);
727 dbus_message_iter_next(&sub);
733 qsort(units, c, sizeof(UnitFileList), compare_unit_file_list);
734 output_unit_file_list(units, c);
741 dbus_message_unref(m);
744 dbus_message_unref(reply);
748 dbus_error_free(&error);
753 static int dot_one_property(const char *name, const char *prop, DBusMessageIter *iter) {
754 static const char * const colors[] = {
755 "Requires", "[color=\"black\"]",
756 "RequiresOverridable", "[color=\"black\"]",
757 "Requisite", "[color=\"darkblue\"]",
758 "RequisiteOverridable", "[color=\"darkblue\"]",
759 "Wants", "[color=\"darkgrey\"]",
760 "Conflicts", "[color=\"red\"]",
761 "ConflictedBy", "[color=\"red\"]",
762 "After", "[color=\"green\"]"
765 const char *c = NULL;
772 for (i = 0; i < ELEMENTSOF(colors); i += 2)
773 if (streq(colors[i], prop)) {
781 if (arg_dot != DOT_ALL)
782 if ((arg_dot == DOT_ORDER) != streq(prop, "After"))
785 switch (dbus_message_iter_get_arg_type(iter)) {
787 case DBUS_TYPE_ARRAY:
789 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
792 dbus_message_iter_recurse(iter, &sub);
794 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
797 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
798 dbus_message_iter_get_basic(&sub, &s);
799 printf("\t\"%s\"->\"%s\" %s;\n", name, s, c);
801 dbus_message_iter_next(&sub);
811 static int dot_one(DBusConnection *bus, const char *name, const char *path) {
812 DBusMessage *m = NULL, *reply = NULL;
813 const char *interface = "org.freedesktop.systemd1.Unit";
816 DBusMessageIter iter, sub, sub2, sub3;
821 dbus_error_init(&error);
823 if (!(m = dbus_message_new_method_call(
824 "org.freedesktop.systemd1",
826 "org.freedesktop.DBus.Properties",
828 log_error("Could not allocate message.");
833 if (!dbus_message_append_args(m,
834 DBUS_TYPE_STRING, &interface,
835 DBUS_TYPE_INVALID)) {
836 log_error("Could not append arguments to message.");
841 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
842 log_error("Failed to issue method call: %s", bus_error_message(&error));
847 if (!dbus_message_iter_init(reply, &iter) ||
848 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
849 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
850 log_error("Failed to parse reply.");
855 dbus_message_iter_recurse(&iter, &sub);
857 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
860 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
861 log_error("Failed to parse reply.");
866 dbus_message_iter_recurse(&sub, &sub2);
868 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &prop, true) < 0) {
869 log_error("Failed to parse reply.");
874 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
875 log_error("Failed to parse reply.");
880 dbus_message_iter_recurse(&sub2, &sub3);
882 if (dot_one_property(name, prop, &sub3)) {
883 log_error("Failed to parse reply.");
888 dbus_message_iter_next(&sub);
895 dbus_message_unref(m);
898 dbus_message_unref(reply);
900 dbus_error_free(&error);
905 static int dot(DBusConnection *bus, char **args) {
906 DBusMessage *m = NULL, *reply = NULL;
909 DBusMessageIter iter, sub, sub2;
911 dbus_error_init(&error);
915 if (!(m = dbus_message_new_method_call(
916 "org.freedesktop.systemd1",
917 "/org/freedesktop/systemd1",
918 "org.freedesktop.systemd1.Manager",
920 log_error("Could not allocate message.");
924 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
925 log_error("Failed to issue method call: %s", bus_error_message(&error));
930 if (!dbus_message_iter_init(reply, &iter) ||
931 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
932 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
933 log_error("Failed to parse reply.");
938 printf("digraph systemd {\n");
940 dbus_message_iter_recurse(&iter, &sub);
941 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
942 const char *id, *description, *load_state, *active_state, *sub_state, *following, *unit_path;
944 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
945 log_error("Failed to parse reply.");
950 dbus_message_iter_recurse(&sub, &sub2);
952 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
953 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
954 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
955 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
956 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
957 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &following, true) < 0 ||
958 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, true) < 0) {
959 log_error("Failed to parse reply.");
964 if ((r = dot_one(bus, id, unit_path)) < 0)
967 /* printf("\t\"%s\";\n", id); */
968 dbus_message_iter_next(&sub);
973 log_info(" Color legend: black = Requires\n"
974 " dark blue = Requisite\n"
975 " dark grey = Wants\n"
980 log_notice("-- You probably want to process this output with graphviz' dot tool.\n"
981 "-- Try a shell pipeline like 'systemctl dot | dot -Tsvg > systemd.svg'!\n");
987 dbus_message_unref(m);
990 dbus_message_unref(reply);
992 dbus_error_free(&error);
997 static int list_jobs(DBusConnection *bus, char **args) {
998 DBusMessage *m = NULL, *reply = NULL;
1001 DBusMessageIter iter, sub, sub2;
1004 dbus_error_init(&error);
1008 pager_open_if_enabled();
1010 if (!(m = dbus_message_new_method_call(
1011 "org.freedesktop.systemd1",
1012 "/org/freedesktop/systemd1",
1013 "org.freedesktop.systemd1.Manager",
1015 log_error("Could not allocate message.");
1019 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1020 log_error("Failed to issue method call: %s", bus_error_message(&error));
1025 if (!dbus_message_iter_init(reply, &iter) ||
1026 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
1027 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
1028 log_error("Failed to parse reply.");
1033 dbus_message_iter_recurse(&iter, &sub);
1036 printf("%4s %-25s %-15s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
1038 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1039 const char *name, *type, *state, *job_path, *unit_path;
1043 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
1044 log_error("Failed to parse reply.");
1049 dbus_message_iter_recurse(&sub, &sub2);
1051 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
1052 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
1053 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
1054 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
1055 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
1056 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
1057 log_error("Failed to parse reply.");
1062 e = arg_full ? NULL : ellipsize(name, 25, 33);
1063 printf("%4u %-25s %-15s %-7s\n", id, e ? e : name, type, state);
1068 dbus_message_iter_next(&sub);
1072 printf("\n%u jobs listed.\n", k);
1078 dbus_message_unref(m);
1081 dbus_message_unref(reply);
1083 dbus_error_free(&error);
1088 static int load_unit(DBusConnection *bus, char **args) {
1089 DBusMessage *m = NULL;
1094 dbus_error_init(&error);
1099 STRV_FOREACH(name, args+1) {
1102 if (!(m = dbus_message_new_method_call(
1103 "org.freedesktop.systemd1",
1104 "/org/freedesktop/systemd1",
1105 "org.freedesktop.systemd1.Manager",
1107 log_error("Could not allocate message.");
1112 if (!dbus_message_append_args(m,
1113 DBUS_TYPE_STRING, name,
1114 DBUS_TYPE_INVALID)) {
1115 log_error("Could not append arguments to message.");
1120 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1121 log_error("Failed to issue method call: %s", bus_error_message(&error));
1126 dbus_message_unref(m);
1127 dbus_message_unref(reply);
1136 dbus_message_unref(m);
1138 dbus_error_free(&error);
1143 static int cancel_job(DBusConnection *bus, char **args) {
1144 DBusMessage *m = NULL, *reply = NULL;
1149 dbus_error_init(&error);
1154 if (strv_length(args) <= 1)
1155 return daemon_reload(bus, args);
1157 STRV_FOREACH(name, args+1) {
1161 if (!(m = dbus_message_new_method_call(
1162 "org.freedesktop.systemd1",
1163 "/org/freedesktop/systemd1",
1164 "org.freedesktop.systemd1.Manager",
1166 log_error("Could not allocate message.");
1171 if ((r = safe_atou(*name, &id)) < 0) {
1172 log_error("Failed to parse job id: %s", strerror(-r));
1176 assert_cc(sizeof(uint32_t) == sizeof(id));
1177 if (!dbus_message_append_args(m,
1178 DBUS_TYPE_UINT32, &id,
1179 DBUS_TYPE_INVALID)) {
1180 log_error("Could not append arguments to message.");
1185 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1186 log_error("Failed to issue method call: %s", bus_error_message(&error));
1191 if (!dbus_message_get_args(reply, &error,
1192 DBUS_TYPE_OBJECT_PATH, &path,
1193 DBUS_TYPE_INVALID)) {
1194 log_error("Failed to parse reply: %s", bus_error_message(&error));
1199 dbus_message_unref(m);
1200 if (!(m = dbus_message_new_method_call(
1201 "org.freedesktop.systemd1",
1203 "org.freedesktop.systemd1.Job",
1205 log_error("Could not allocate message.");
1210 dbus_message_unref(reply);
1211 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1212 log_error("Failed to issue method call: %s", bus_error_message(&error));
1217 dbus_message_unref(m);
1218 dbus_message_unref(reply);
1226 dbus_message_unref(m);
1229 dbus_message_unref(reply);
1231 dbus_error_free(&error);
1236 static bool need_daemon_reload(DBusConnection *bus, const char *unit) {
1237 DBusMessage *m = NULL, *reply = NULL;
1238 dbus_bool_t b = FALSE;
1239 DBusMessageIter iter, sub;
1241 *interface = "org.freedesktop.systemd1.Unit",
1242 *property = "NeedDaemonReload",
1245 /* We ignore all errors here, since this is used to show a warning only */
1247 if (!(m = dbus_message_new_method_call(
1248 "org.freedesktop.systemd1",
1249 "/org/freedesktop/systemd1",
1250 "org.freedesktop.systemd1.Manager",
1254 if (!dbus_message_append_args(m,
1255 DBUS_TYPE_STRING, &unit,
1259 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
1262 if (!dbus_message_get_args(reply, NULL,
1263 DBUS_TYPE_OBJECT_PATH, &path,
1267 dbus_message_unref(m);
1268 if (!(m = dbus_message_new_method_call(
1269 "org.freedesktop.systemd1",
1271 "org.freedesktop.DBus.Properties",
1275 if (!dbus_message_append_args(m,
1276 DBUS_TYPE_STRING, &interface,
1277 DBUS_TYPE_STRING, &property,
1278 DBUS_TYPE_INVALID)) {
1282 dbus_message_unref(reply);
1283 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
1286 if (!dbus_message_iter_init(reply, &iter) ||
1287 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
1290 dbus_message_iter_recurse(&iter, &sub);
1292 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_BOOLEAN)
1295 dbus_message_iter_get_basic(&sub, &b);
1299 dbus_message_unref(m);
1302 dbus_message_unref(reply);
1307 typedef struct WaitData {
1312 static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
1320 dbus_error_init(&error);
1322 log_debug("Got D-Bus request: %s.%s() on %s",
1323 dbus_message_get_interface(message),
1324 dbus_message_get_member(message),
1325 dbus_message_get_path(message));
1327 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
1328 log_error("Warning! D-Bus connection terminated.");
1329 dbus_connection_close(connection);
1331 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
1333 const char *path, *result;
1334 dbus_bool_t success = true;
1336 if (dbus_message_get_args(message, &error,
1337 DBUS_TYPE_UINT32, &id,
1338 DBUS_TYPE_OBJECT_PATH, &path,
1339 DBUS_TYPE_STRING, &result,
1340 DBUS_TYPE_INVALID)) {
1343 if ((p = set_remove(d->set, (char*) path)))
1347 d->result = strdup(result);
1352 dbus_error_free(&error);
1354 if (dbus_message_get_args(message, &error,
1355 DBUS_TYPE_UINT32, &id,
1356 DBUS_TYPE_OBJECT_PATH, &path,
1357 DBUS_TYPE_BOOLEAN, &success,
1358 DBUS_TYPE_INVALID)) {
1361 /* Compatibility with older systemd versions <
1362 * 19 during upgrades. This should be dropped
1365 if ((p = set_remove(d->set, (char*) path)))
1369 d->result = strdup("failed");
1375 log_error("Failed to parse message: %s", bus_error_message(&error));
1379 dbus_error_free(&error);
1380 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1383 static int enable_wait_for_jobs(DBusConnection *bus) {
1391 dbus_error_init(&error);
1392 dbus_bus_add_match(bus,
1394 "sender='org.freedesktop.systemd1',"
1395 "interface='org.freedesktop.systemd1.Manager',"
1396 "member='JobRemoved',"
1397 "path='/org/freedesktop/systemd1'",
1400 if (dbus_error_is_set(&error)) {
1401 log_error("Failed to add match: %s", bus_error_message(&error));
1402 dbus_error_free(&error);
1406 /* This is slightly dirty, since we don't undo the match registrations. */
1410 static int wait_for_jobs(DBusConnection *bus, Set *s) {
1420 if (!dbus_connection_add_filter(bus, wait_filter, &d, NULL)) {
1421 log_error("Failed to add filter.");
1426 while (!set_isempty(s) &&
1427 dbus_connection_read_write_dispatch(bus, -1))
1430 if (!arg_quiet && d.result) {
1431 if (streq(d.result, "timeout"))
1432 log_error("Job timed out.");
1433 else if (streq(d.result, "canceled"))
1434 log_error("Job canceled.");
1435 else if (streq(d.result, "dependency"))
1436 log_error("A dependency job failed. See system logs for details.");
1437 else if (!streq(d.result, "done") && !streq(d.result, "skipped"))
1438 log_error("Job failed. See system logs and 'systemctl status' for details.");
1441 if (streq_ptr(d.result, "timeout"))
1443 else if (streq_ptr(d.result, "canceled"))
1445 else if (!streq_ptr(d.result, "done") && !streq_ptr(d.result, "skipped"))
1453 /* This is slightly dirty, since we don't undo the filter registration. */
1458 static int start_unit_one(
1459 DBusConnection *bus,
1466 DBusMessage *m = NULL, *reply = NULL;
1475 assert(arg_no_block || s);
1477 if (!(m = dbus_message_new_method_call(
1478 "org.freedesktop.systemd1",
1479 "/org/freedesktop/systemd1",
1480 "org.freedesktop.systemd1.Manager",
1482 log_error("Could not allocate message.");
1487 if (!dbus_message_append_args(m,
1488 DBUS_TYPE_STRING, &name,
1489 DBUS_TYPE_STRING, &mode,
1490 DBUS_TYPE_INVALID)) {
1491 log_error("Could not append arguments to message.");
1496 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, error))) {
1498 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(error)) {
1499 /* There's always a fallback possible for
1500 * legacy actions. */
1505 log_error("Failed to issue method call: %s", bus_error_message(error));
1510 if (!dbus_message_get_args(reply, error,
1511 DBUS_TYPE_OBJECT_PATH, &path,
1512 DBUS_TYPE_INVALID)) {
1513 log_error("Failed to parse reply: %s", bus_error_message(error));
1518 if (need_daemon_reload(bus, name))
1519 log_warning("Warning: Unit file of created job changed on disk, 'systemctl %s daemon-reload' recommended.",
1520 arg_scope == UNIT_FILE_SYSTEM ? "--system" : "--user");
1522 if (!arg_no_block) {
1525 if (!(p = strdup(path))) {
1526 log_error("Failed to duplicate path.");
1531 if ((r = set_put(s, p)) < 0) {
1533 log_error("Failed to add path to set.");
1542 dbus_message_unref(m);
1545 dbus_message_unref(reply);
1550 static enum action verb_to_action(const char *verb) {
1551 if (streq(verb, "halt"))
1553 else if (streq(verb, "poweroff"))
1554 return ACTION_POWEROFF;
1555 else if (streq(verb, "reboot"))
1556 return ACTION_REBOOT;
1557 else if (streq(verb, "kexec"))
1558 return ACTION_KEXEC;
1559 else if (streq(verb, "rescue"))
1560 return ACTION_RESCUE;
1561 else if (streq(verb, "emergency"))
1562 return ACTION_EMERGENCY;
1563 else if (streq(verb, "default"))
1564 return ACTION_DEFAULT;
1565 else if (streq(verb, "exit"))
1568 return ACTION_INVALID;
1571 static int start_unit(DBusConnection *bus, char **args) {
1573 static const char * const table[_ACTION_MAX] = {
1574 [ACTION_HALT] = SPECIAL_HALT_TARGET,
1575 [ACTION_POWEROFF] = SPECIAL_POWEROFF_TARGET,
1576 [ACTION_REBOOT] = SPECIAL_REBOOT_TARGET,
1577 [ACTION_KEXEC] = SPECIAL_KEXEC_TARGET,
1578 [ACTION_RUNLEVEL2] = SPECIAL_RUNLEVEL2_TARGET,
1579 [ACTION_RUNLEVEL3] = SPECIAL_RUNLEVEL3_TARGET,
1580 [ACTION_RUNLEVEL4] = SPECIAL_RUNLEVEL4_TARGET,
1581 [ACTION_RUNLEVEL5] = SPECIAL_RUNLEVEL5_TARGET,
1582 [ACTION_RESCUE] = SPECIAL_RESCUE_TARGET,
1583 [ACTION_EMERGENCY] = SPECIAL_EMERGENCY_TARGET,
1584 [ACTION_DEFAULT] = SPECIAL_DEFAULT_TARGET,
1585 [ACTION_EXIT] = SPECIAL_EXIT_TARGET
1589 const char *method, *mode, *one_name;
1594 dbus_error_init(&error);
1598 agent_open_if_enabled();
1600 if (arg_action == ACTION_SYSTEMCTL) {
1602 streq(args[0], "stop") ||
1603 streq(args[0], "condstop") ? "StopUnit" :
1604 streq(args[0], "reload") ? "ReloadUnit" :
1605 streq(args[0], "restart") ? "RestartUnit" :
1607 streq(args[0], "try-restart") ||
1608 streq(args[0], "condrestart") ? "TryRestartUnit" :
1610 streq(args[0], "reload-or-restart") ? "ReloadOrRestartUnit" :
1612 streq(args[0], "reload-or-try-restart") ||
1613 streq(args[0], "condreload") ||
1615 streq(args[0], "force-reload") ? "ReloadOrTryRestartUnit" :
1619 (streq(args[0], "isolate") ||
1620 streq(args[0], "rescue") ||
1621 streq(args[0], "emergency")) ? "isolate" : arg_job_mode;
1623 one_name = table[verb_to_action(args[0])];
1626 assert(arg_action < ELEMENTSOF(table));
1627 assert(table[arg_action]);
1629 method = "StartUnit";
1631 mode = (arg_action == ACTION_EMERGENCY ||
1632 arg_action == ACTION_RESCUE ||
1633 arg_action == ACTION_RUNLEVEL2 ||
1634 arg_action == ACTION_RUNLEVEL3 ||
1635 arg_action == ACTION_RUNLEVEL4 ||
1636 arg_action == ACTION_RUNLEVEL5) ? "isolate" : "replace";
1638 one_name = table[arg_action];
1641 if (!arg_no_block) {
1642 if ((ret = enable_wait_for_jobs(bus)) < 0) {
1643 log_error("Could not watch jobs: %s", strerror(-ret));
1647 if (!(s = set_new(string_hash_func, string_compare_func))) {
1648 log_error("Failed to allocate set.");
1655 if ((ret = start_unit_one(bus, method, one_name, mode, &error, s)) <= 0)
1658 STRV_FOREACH(name, args+1)
1659 if ((r = start_unit_one(bus, method, *name, mode, &error, s)) != 0) {
1660 ret = translate_bus_error_to_exit_status(r, &error);
1661 dbus_error_free(&error);
1666 if ((r = wait_for_jobs(bus, s)) < 0) {
1675 dbus_error_free(&error);
1680 static int start_special(DBusConnection *bus, char **args) {
1687 (streq(args[0], "halt") ||
1688 streq(args[0], "poweroff") ||
1689 streq(args[0], "reboot") ||
1690 streq(args[0], "kexec") ||
1691 streq(args[0], "exit")))
1692 return daemon_reload(bus, args);
1694 r = start_unit(bus, args);
1697 warn_wall(verb_to_action(args[0]));
1702 static int check_unit(DBusConnection *bus, char **args) {
1703 DBusMessage *m = NULL, *reply = NULL;
1705 *interface = "org.freedesktop.systemd1.Unit",
1706 *property = "ActiveState";
1707 int r = 3; /* According to LSB: "program is not running" */
1714 dbus_error_init(&error);
1716 STRV_FOREACH(name, args+1) {
1717 const char *path = NULL;
1719 DBusMessageIter iter, sub;
1721 if (!(m = dbus_message_new_method_call(
1722 "org.freedesktop.systemd1",
1723 "/org/freedesktop/systemd1",
1724 "org.freedesktop.systemd1.Manager",
1726 log_error("Could not allocate message.");
1731 if (!dbus_message_append_args(m,
1732 DBUS_TYPE_STRING, name,
1733 DBUS_TYPE_INVALID)) {
1734 log_error("Could not append arguments to message.");
1739 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1741 /* Hmm, cannot figure out anything about this unit... */
1745 dbus_error_free(&error);
1746 dbus_message_unref(m);
1751 if (!dbus_message_get_args(reply, &error,
1752 DBUS_TYPE_OBJECT_PATH, &path,
1753 DBUS_TYPE_INVALID)) {
1754 log_error("Failed to parse reply: %s", bus_error_message(&error));
1759 dbus_message_unref(m);
1760 if (!(m = dbus_message_new_method_call(
1761 "org.freedesktop.systemd1",
1763 "org.freedesktop.DBus.Properties",
1765 log_error("Could not allocate message.");
1770 if (!dbus_message_append_args(m,
1771 DBUS_TYPE_STRING, &interface,
1772 DBUS_TYPE_STRING, &property,
1773 DBUS_TYPE_INVALID)) {
1774 log_error("Could not append arguments to message.");
1779 dbus_message_unref(reply);
1780 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1781 log_error("Failed to issue method call: %s", bus_error_message(&error));
1786 if (!dbus_message_iter_init(reply, &iter) ||
1787 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1788 log_error("Failed to parse reply.");
1793 dbus_message_iter_recurse(&iter, &sub);
1795 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1796 log_error("Failed to parse reply.");
1801 dbus_message_iter_get_basic(&sub, &state);
1806 if (streq(state, "active") || streq(state, "reloading"))
1809 dbus_message_unref(m);
1810 dbus_message_unref(reply);
1816 dbus_message_unref(m);
1819 dbus_message_unref(reply);
1821 dbus_error_free(&error);
1826 static int kill_unit(DBusConnection *bus, char **args) {
1827 DBusMessage *m = NULL;
1835 dbus_error_init(&error);
1838 arg_kill_who = "all";
1841 arg_kill_mode = streq(arg_kill_who, "all") ? "control-group" : "process";
1843 STRV_FOREACH(name, args+1) {
1846 if (!(m = dbus_message_new_method_call(
1847 "org.freedesktop.systemd1",
1848 "/org/freedesktop/systemd1",
1849 "org.freedesktop.systemd1.Manager",
1851 log_error("Could not allocate message.");
1856 if (!dbus_message_append_args(m,
1857 DBUS_TYPE_STRING, name,
1858 DBUS_TYPE_STRING, &arg_kill_who,
1859 DBUS_TYPE_STRING, &arg_kill_mode,
1860 DBUS_TYPE_INT32, &arg_signal,
1861 DBUS_TYPE_INVALID)) {
1862 log_error("Could not append arguments to message.");
1867 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1868 log_error("Failed to issue method call: %s", bus_error_message(&error));
1869 dbus_error_free(&error);
1873 dbus_message_unref(m);
1876 dbus_message_unref(reply);
1882 dbus_message_unref(m);
1884 dbus_error_free(&error);
1889 typedef struct ExecStatusInfo {
1897 usec_t start_timestamp;
1898 usec_t exit_timestamp;
1903 LIST_FIELDS(struct ExecStatusInfo, exec);
1906 static void exec_status_info_free(ExecStatusInfo *i) {
1915 static int exec_status_info_deserialize(DBusMessageIter *sub, ExecStatusInfo *i) {
1916 uint64_t start_timestamp, exit_timestamp, start_timestamp_monotonic, exit_timestamp_monotonic;
1917 DBusMessageIter sub2, sub3;
1921 int32_t code, status;
1927 if (dbus_message_iter_get_arg_type(sub) != DBUS_TYPE_STRUCT)
1930 dbus_message_iter_recurse(sub, &sub2);
1932 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
1935 if (!(i->path = strdup(path)))
1938 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
1939 dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
1943 dbus_message_iter_recurse(&sub2, &sub3);
1944 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1945 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1946 dbus_message_iter_next(&sub3);
1951 if (!(i->argv = new0(char*, n+1)))
1955 dbus_message_iter_recurse(&sub2, &sub3);
1956 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1959 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1960 dbus_message_iter_get_basic(&sub3, &s);
1961 dbus_message_iter_next(&sub3);
1963 if (!(i->argv[n++] = strdup(s)))
1967 if (!dbus_message_iter_next(&sub2) ||
1968 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, true) < 0 ||
1969 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp, true) < 0 ||
1970 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp_monotonic, true) < 0 ||
1971 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp, true) < 0 ||
1972 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp_monotonic, true) < 0 ||
1973 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) < 0 ||
1974 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) < 0 ||
1975 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) < 0)
1979 i->start_timestamp = (usec_t) start_timestamp;
1980 i->exit_timestamp = (usec_t) exit_timestamp;
1981 i->pid = (pid_t) pid;
1988 typedef struct UnitStatusInfo {
1990 const char *load_state;
1991 const char *active_state;
1992 const char *sub_state;
1993 const char *unit_file_state;
1995 const char *description;
1996 const char *following;
1999 const char *default_control_group;
2001 const char *load_error;
2003 usec_t inactive_exit_timestamp;
2004 usec_t active_enter_timestamp;
2005 usec_t active_exit_timestamp;
2006 usec_t inactive_enter_timestamp;
2008 bool need_daemon_reload;
2013 const char *status_text;
2015 #ifdef HAVE_SYSV_COMPAT
2019 usec_t start_timestamp;
2020 usec_t exit_timestamp;
2022 int exit_code, exit_status;
2024 usec_t condition_timestamp;
2025 bool condition_result;
2028 unsigned n_accepted;
2029 unsigned n_connections;
2033 const char *sysfs_path;
2035 /* Mount, Automount */
2041 LIST_HEAD(ExecStatusInfo, exec);
2044 static void print_status_info(UnitStatusInfo *i) {
2046 const char *on, *off, *ss;
2048 char since1[FORMAT_TIMESTAMP_PRETTY_MAX], *s1;
2049 char since2[FORMAT_TIMESTAMP_MAX], *s2;
2053 /* This shows pretty information about a unit. See
2054 * print_property() for a low-level property printer */
2056 printf("%s", strna(i->id));
2058 if (i->description && !streq_ptr(i->id, i->description))
2059 printf(" - %s", i->description);
2064 printf("\t Follow: unit currently follows state of %s\n", i->following);
2066 if (streq_ptr(i->load_state, "error")) {
2067 on = ansi_highlight(true);
2068 off = ansi_highlight(false);
2073 printf("\t Loaded: %s%s%s (Reason: %s)\n", on, strna(i->load_state), off, i->load_error);
2074 else if (i->path && i->unit_file_state)
2075 printf("\t Loaded: %s%s%s (%s; %s)\n", on, strna(i->load_state), off, i->path, i->unit_file_state);
2077 printf("\t Loaded: %s%s%s (%s)\n", on, strna(i->load_state), off, i->path);
2079 printf("\t Loaded: %s%s%s\n", on, strna(i->load_state), off);
2081 ss = streq_ptr(i->active_state, i->sub_state) ? NULL : i->sub_state;
2083 if (streq_ptr(i->active_state, "failed")) {
2084 on = ansi_highlight(true);
2085 off = ansi_highlight(false);
2086 } else if (streq_ptr(i->active_state, "active") || streq_ptr(i->active_state, "reloading")) {
2087 on = ansi_highlight_green(true);
2088 off = ansi_highlight_green(false);
2093 printf("\t Active: %s%s (%s)%s",
2095 strna(i->active_state),
2099 printf("\t Active: %s%s%s",
2101 strna(i->active_state),
2104 timestamp = (streq_ptr(i->active_state, "active") ||
2105 streq_ptr(i->active_state, "reloading")) ? i->active_enter_timestamp :
2106 (streq_ptr(i->active_state, "inactive") ||
2107 streq_ptr(i->active_state, "failed")) ? i->inactive_enter_timestamp :
2108 streq_ptr(i->active_state, "activating") ? i->inactive_exit_timestamp :
2109 i->active_exit_timestamp;
2111 s1 = format_timestamp_pretty(since1, sizeof(since1), timestamp);
2112 s2 = format_timestamp(since2, sizeof(since2), timestamp);
2115 printf(" since %s; %s\n", s2, s1);
2117 printf(" since %s\n", s2);
2121 if (!i->condition_result && i->condition_timestamp > 0) {
2122 s1 = format_timestamp_pretty(since1, sizeof(since1), i->condition_timestamp);
2123 s2 = format_timestamp(since2, sizeof(since2), i->condition_timestamp);
2126 printf("\t start condition failed at %s; %s\n", s2, s1);
2128 printf("\t start condition failed at %s\n", s2);
2132 printf("\t Device: %s\n", i->sysfs_path);
2134 printf("\t Where: %s\n", i->where);
2136 printf("\t What: %s\n", i->what);
2139 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
2141 LIST_FOREACH(exec, p, i->exec) {
2145 /* Only show exited processes here */
2149 t = strv_join(p->argv, " ");
2150 printf("\t Process: %u %s=%s ", p->pid, p->name, strna(t));
2153 #ifdef HAVE_SYSV_COMPAT
2155 good = is_clean_exit_lsb(p->code, p->status);
2158 good = is_clean_exit(p->code, p->status);
2161 on = ansi_highlight(true);
2162 off = ansi_highlight(false);
2166 printf("%s(code=%s, ", on, sigchld_code_to_string(p->code));
2168 if (p->code == CLD_EXITED) {
2171 printf("status=%i", p->status);
2173 #ifdef HAVE_SYSV_COMPAT
2174 if ((c = exit_status_to_string(p->status, i->is_sysv ? EXIT_STATUS_LSB : EXIT_STATUS_SYSTEMD)))
2176 if ((c = exit_status_to_string(p->status, EXIT_STATUS_SYSTEMD)))
2181 printf("signal=%s", signal_to_string(p->status));
2183 printf(")%s\n", off);
2185 if (i->main_pid == p->pid &&
2186 i->start_timestamp == p->start_timestamp &&
2187 i->exit_timestamp == p->start_timestamp)
2188 /* Let's not show this twice */
2191 if (p->pid == i->control_pid)
2195 if (i->main_pid > 0 || i->control_pid > 0) {
2198 if (i->main_pid > 0) {
2199 printf("Main PID: %u", (unsigned) i->main_pid);
2203 get_process_comm(i->main_pid, &t);
2208 } else if (i->exit_code > 0) {
2209 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
2211 if (i->exit_code == CLD_EXITED) {
2214 printf("status=%i", i->exit_status);
2216 #ifdef HAVE_SYSV_COMPAT
2217 if ((c = exit_status_to_string(i->exit_status, i->is_sysv ? EXIT_STATUS_LSB : EXIT_STATUS_SYSTEMD)))
2219 if ((c = exit_status_to_string(i->exit_status, EXIT_STATUS_SYSTEMD)))
2224 printf("signal=%s", signal_to_string(i->exit_status));
2229 if (i->main_pid > 0 && i->control_pid > 0)
2232 if (i->control_pid > 0) {
2235 printf(" Control: %u", (unsigned) i->control_pid);
2237 get_process_comm(i->control_pid, &t);
2248 printf("\t Status: \"%s\"\n", i->status_text);
2250 if (i->default_control_group) {
2253 printf("\t CGroup: %s\n", i->default_control_group);
2255 if (arg_transport != TRANSPORT_SSH) {
2256 if ((c = columns()) > 18)
2261 show_cgroup_by_path(i->default_control_group, "\t\t ", c);
2265 if (i->id && arg_transport != TRANSPORT_SSH) {
2267 show_journal_by_service(i->id, OUTPUT_SHORT, NULL, 0, 0, 0, arg_all);
2270 if (i->need_daemon_reload)
2271 printf("\n%sWarning:%s Unit file changed on disk, 'systemctl %s daemon-reload' recommended.\n",
2272 ansi_highlight(true),
2273 ansi_highlight(false),
2274 arg_scope == UNIT_FILE_SYSTEM ? "--system" : "--user");
2277 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
2283 switch (dbus_message_iter_get_arg_type(iter)) {
2285 case DBUS_TYPE_STRING: {
2288 dbus_message_iter_get_basic(iter, &s);
2291 if (streq(name, "Id"))
2293 else if (streq(name, "LoadState"))
2295 else if (streq(name, "ActiveState"))
2296 i->active_state = s;
2297 else if (streq(name, "SubState"))
2299 else if (streq(name, "Description"))
2301 else if (streq(name, "FragmentPath"))
2303 #ifdef HAVE_SYSV_COMPAT
2304 else if (streq(name, "SysVPath")) {
2309 else if (streq(name, "DefaultControlGroup"))
2310 i->default_control_group = s;
2311 else if (streq(name, "StatusText"))
2313 else if (streq(name, "SysFSPath"))
2315 else if (streq(name, "Where"))
2317 else if (streq(name, "What"))
2319 else if (streq(name, "Following"))
2321 else if (streq(name, "UnitFileState"))
2322 i->unit_file_state = s;
2328 case DBUS_TYPE_BOOLEAN: {
2331 dbus_message_iter_get_basic(iter, &b);
2333 if (streq(name, "Accept"))
2335 else if (streq(name, "NeedDaemonReload"))
2336 i->need_daemon_reload = b;
2337 else if (streq(name, "ConditionResult"))
2338 i->condition_result = b;
2343 case DBUS_TYPE_UINT32: {
2346 dbus_message_iter_get_basic(iter, &u);
2348 if (streq(name, "MainPID")) {
2350 i->main_pid = (pid_t) u;
2353 } else if (streq(name, "ControlPID"))
2354 i->control_pid = (pid_t) u;
2355 else if (streq(name, "ExecMainPID")) {
2357 i->main_pid = (pid_t) u;
2358 } else if (streq(name, "NAccepted"))
2360 else if (streq(name, "NConnections"))
2361 i->n_connections = u;
2366 case DBUS_TYPE_INT32: {
2369 dbus_message_iter_get_basic(iter, &j);
2371 if (streq(name, "ExecMainCode"))
2372 i->exit_code = (int) j;
2373 else if (streq(name, "ExecMainStatus"))
2374 i->exit_status = (int) j;
2379 case DBUS_TYPE_UINT64: {
2382 dbus_message_iter_get_basic(iter, &u);
2384 if (streq(name, "ExecMainStartTimestamp"))
2385 i->start_timestamp = (usec_t) u;
2386 else if (streq(name, "ExecMainExitTimestamp"))
2387 i->exit_timestamp = (usec_t) u;
2388 else if (streq(name, "ActiveEnterTimestamp"))
2389 i->active_enter_timestamp = (usec_t) u;
2390 else if (streq(name, "InactiveEnterTimestamp"))
2391 i->inactive_enter_timestamp = (usec_t) u;
2392 else if (streq(name, "InactiveExitTimestamp"))
2393 i->inactive_exit_timestamp = (usec_t) u;
2394 else if (streq(name, "ActiveExitTimestamp"))
2395 i->active_exit_timestamp = (usec_t) u;
2396 else if (streq(name, "ConditionTimestamp"))
2397 i->condition_timestamp = (usec_t) u;
2402 case DBUS_TYPE_ARRAY: {
2404 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT &&
2405 startswith(name, "Exec")) {
2406 DBusMessageIter sub;
2408 dbus_message_iter_recurse(iter, &sub);
2409 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2410 ExecStatusInfo *info;
2413 if (!(info = new0(ExecStatusInfo, 1)))
2416 if (!(info->name = strdup(name))) {
2421 if ((r = exec_status_info_deserialize(&sub, info)) < 0) {
2426 LIST_PREPEND(ExecStatusInfo, exec, i->exec, info);
2428 dbus_message_iter_next(&sub);
2435 case DBUS_TYPE_STRUCT: {
2437 if (streq(name, "LoadError")) {
2438 DBusMessageIter sub;
2439 const char *n, *message;
2442 dbus_message_iter_recurse(iter, &sub);
2444 r = bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &n, true);
2448 r = bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &message, false);
2452 if (!isempty(message))
2453 i->load_error = message;
2463 static int print_property(const char *name, DBusMessageIter *iter) {
2467 /* This is a low-level property printer, see
2468 * print_status_info() for the nicer output */
2470 if (arg_property && !strv_find(arg_property, name))
2473 switch (dbus_message_iter_get_arg_type(iter)) {
2475 case DBUS_TYPE_STRUCT: {
2476 DBusMessageIter sub;
2477 dbus_message_iter_recurse(iter, &sub);
2479 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
2482 dbus_message_iter_get_basic(&sub, &u);
2485 printf("%s=%u\n", name, (unsigned) u);
2487 printf("%s=\n", name);
2490 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
2493 dbus_message_iter_get_basic(&sub, &s);
2495 if (arg_all || s[0])
2496 printf("%s=%s\n", name, s);
2499 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "LoadError")) {
2500 const char *a = NULL, *b = NULL;
2502 if (bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &a, true) >= 0)
2503 bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &b, false);
2505 if (arg_all || !isempty(a) || !isempty(b))
2506 printf("%s=%s \"%s\"\n", name, strempty(a), strempty(b));
2514 case DBUS_TYPE_ARRAY:
2516 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "EnvironmentFiles")) {
2517 DBusMessageIter sub, sub2;
2519 dbus_message_iter_recurse(iter, &sub);
2520 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2524 dbus_message_iter_recurse(&sub, &sub2);
2526 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) >= 0 &&
2527 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, false) >= 0)
2528 printf("EnvironmentFile=%s (ignore_errors=%s)\n", path, yes_no(ignore));
2530 dbus_message_iter_next(&sub);
2535 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
2536 DBusMessageIter sub, sub2;
2538 dbus_message_iter_recurse(iter, &sub);
2539 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2540 const char *type, *path;
2542 dbus_message_iter_recurse(&sub, &sub2);
2544 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
2545 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
2546 printf("%s=%s\n", type, path);
2548 dbus_message_iter_next(&sub);
2553 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
2554 DBusMessageIter sub, sub2;
2556 dbus_message_iter_recurse(iter, &sub);
2557 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2559 uint64_t value, next_elapse;
2561 dbus_message_iter_recurse(&sub, &sub2);
2563 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
2564 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
2565 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
2566 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
2568 printf("%s={ value=%s ; next_elapse=%s }\n",
2570 format_timespan(timespan1, sizeof(timespan1), value),
2571 format_timespan(timespan2, sizeof(timespan2), next_elapse));
2574 dbus_message_iter_next(&sub);
2579 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "ControlGroupAttributes")) {
2580 DBusMessageIter sub, sub2;
2582 dbus_message_iter_recurse(iter, &sub);
2583 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2584 const char *controller, *attr, *value;
2586 dbus_message_iter_recurse(&sub, &sub2);
2588 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &controller, true) >= 0 &&
2589 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &attr, true) >= 0 &&
2590 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &value, false) >= 0) {
2592 printf("ControlGroupAttribute={ controller=%s ; attribute=%s ; value=\"%s\" }\n",
2598 dbus_message_iter_next(&sub);
2603 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
2604 DBusMessageIter sub;
2606 dbus_message_iter_recurse(iter, &sub);
2607 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2608 ExecStatusInfo info;
2611 if (exec_status_info_deserialize(&sub, &info) >= 0) {
2612 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
2615 t = strv_join(info.argv, " ");
2617 printf("%s={ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n",
2621 yes_no(info.ignore),
2622 strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)),
2623 strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)),
2624 (unsigned) info. pid,
2625 sigchld_code_to_string(info.code),
2627 info.code == CLD_EXITED ? "" : "/",
2628 strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status)));
2634 strv_free(info.argv);
2636 dbus_message_iter_next(&sub);
2645 if (generic_print_property(name, iter, arg_all) > 0)
2649 printf("%s=[unprintable]\n", name);
2654 static int show_one(const char *verb, DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
2655 DBusMessage *m = NULL, *reply = NULL;
2656 const char *interface = "";
2659 DBusMessageIter iter, sub, sub2, sub3;
2660 UnitStatusInfo info;
2668 dbus_error_init(&error);
2670 if (!(m = dbus_message_new_method_call(
2671 "org.freedesktop.systemd1",
2673 "org.freedesktop.DBus.Properties",
2675 log_error("Could not allocate message.");
2680 if (!dbus_message_append_args(m,
2681 DBUS_TYPE_STRING, &interface,
2682 DBUS_TYPE_INVALID)) {
2683 log_error("Could not append arguments to message.");
2688 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2689 log_error("Failed to issue method call: %s", bus_error_message(&error));
2694 if (!dbus_message_iter_init(reply, &iter) ||
2695 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
2696 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
2697 log_error("Failed to parse reply.");
2702 dbus_message_iter_recurse(&iter, &sub);
2709 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2712 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
2713 log_error("Failed to parse reply.");
2718 dbus_message_iter_recurse(&sub, &sub2);
2720 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
2721 log_error("Failed to parse reply.");
2726 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
2727 log_error("Failed to parse reply.");
2732 dbus_message_iter_recurse(&sub2, &sub3);
2734 if (show_properties)
2735 r = print_property(name, &sub3);
2737 r = status_property(name, &sub3, &info);
2740 log_error("Failed to parse reply.");
2745 dbus_message_iter_next(&sub);
2750 if (!show_properties)
2751 print_status_info(&info);
2753 if (!streq_ptr(info.active_state, "active") &&
2754 !streq_ptr(info.active_state, "reloading") &&
2755 streq(verb, "status"))
2756 /* According to LSB: "program not running" */
2759 while ((p = info.exec)) {
2760 LIST_REMOVE(ExecStatusInfo, exec, info.exec, p);
2761 exec_status_info_free(p);
2766 dbus_message_unref(m);
2769 dbus_message_unref(reply);
2771 dbus_error_free(&error);
2776 static int show(DBusConnection *bus, char **args) {
2777 DBusMessage *m = NULL, *reply = NULL;
2780 bool show_properties, new_line = false;
2786 dbus_error_init(&error);
2788 show_properties = !streq(args[0], "status");
2790 if (show_properties)
2791 pager_open_if_enabled();
2793 if (show_properties && strv_length(args) <= 1) {
2794 /* If not argument is specified inspect the manager
2797 ret = show_one(args[0], bus, "/org/freedesktop/systemd1", show_properties, &new_line);
2801 STRV_FOREACH(name, args+1) {
2802 const char *path = NULL;
2805 if (safe_atou32(*name, &id) < 0) {
2807 /* Interpret as unit name */
2809 if (!(m = dbus_message_new_method_call(
2810 "org.freedesktop.systemd1",
2811 "/org/freedesktop/systemd1",
2812 "org.freedesktop.systemd1.Manager",
2814 log_error("Could not allocate message.");
2819 if (!dbus_message_append_args(m,
2820 DBUS_TYPE_STRING, name,
2821 DBUS_TYPE_INVALID)) {
2822 log_error("Could not append arguments to message.");
2827 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2829 if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
2830 log_error("Failed to issue method call: %s", bus_error_message(&error));
2835 dbus_error_free(&error);
2837 dbus_message_unref(m);
2838 if (!(m = dbus_message_new_method_call(
2839 "org.freedesktop.systemd1",
2840 "/org/freedesktop/systemd1",
2841 "org.freedesktop.systemd1.Manager",
2843 log_error("Could not allocate message.");
2848 if (!dbus_message_append_args(m,
2849 DBUS_TYPE_STRING, name,
2850 DBUS_TYPE_INVALID)) {
2851 log_error("Could not append arguments to message.");
2856 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2857 log_error("Failed to issue method call: %s", bus_error_message(&error));
2859 if (dbus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT))
2860 ret = 4; /* According to LSB: "program or service status is unknown" */
2867 } else if (show_properties) {
2869 /* Interpret as job id */
2871 if (!(m = dbus_message_new_method_call(
2872 "org.freedesktop.systemd1",
2873 "/org/freedesktop/systemd1",
2874 "org.freedesktop.systemd1.Manager",
2876 log_error("Could not allocate message.");
2881 if (!dbus_message_append_args(m,
2882 DBUS_TYPE_UINT32, &id,
2883 DBUS_TYPE_INVALID)) {
2884 log_error("Could not append arguments to message.");
2889 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2890 log_error("Failed to issue method call: %s", bus_error_message(&error));
2896 /* Interpret as PID */
2898 if (!(m = dbus_message_new_method_call(
2899 "org.freedesktop.systemd1",
2900 "/org/freedesktop/systemd1",
2901 "org.freedesktop.systemd1.Manager",
2903 log_error("Could not allocate message.");
2908 if (!dbus_message_append_args(m,
2909 DBUS_TYPE_UINT32, &id,
2910 DBUS_TYPE_INVALID)) {
2911 log_error("Could not append arguments to message.");
2916 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2917 log_error("Failed to issue method call: %s", bus_error_message(&error));
2923 if (!dbus_message_get_args(reply, &error,
2924 DBUS_TYPE_OBJECT_PATH, &path,
2925 DBUS_TYPE_INVALID)) {
2926 log_error("Failed to parse reply: %s", bus_error_message(&error));
2931 if ((r = show_one(args[0], bus, path, show_properties, &new_line)) != 0)
2934 dbus_message_unref(m);
2935 dbus_message_unref(reply);
2941 dbus_message_unref(m);
2944 dbus_message_unref(reply);
2946 dbus_error_free(&error);
2951 static int dump(DBusConnection *bus, char **args) {
2952 DBusMessage *m = NULL, *reply = NULL;
2957 dbus_error_init(&error);
2959 pager_open_if_enabled();
2961 if (!(m = dbus_message_new_method_call(
2962 "org.freedesktop.systemd1",
2963 "/org/freedesktop/systemd1",
2964 "org.freedesktop.systemd1.Manager",
2966 log_error("Could not allocate message.");
2970 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2971 log_error("Failed to issue method call: %s", bus_error_message(&error));
2976 if (!dbus_message_get_args(reply, &error,
2977 DBUS_TYPE_STRING, &text,
2978 DBUS_TYPE_INVALID)) {
2979 log_error("Failed to parse reply: %s", bus_error_message(&error));
2984 fputs(text, stdout);
2990 dbus_message_unref(m);
2993 dbus_message_unref(reply);
2995 dbus_error_free(&error);
3000 static int snapshot(DBusConnection *bus, char **args) {
3001 DBusMessage *m = NULL, *reply = NULL;
3004 const char *name = "", *path, *id;
3005 dbus_bool_t cleanup = FALSE;
3006 DBusMessageIter iter, sub;
3008 *interface = "org.freedesktop.systemd1.Unit",
3011 dbus_error_init(&error);
3013 if (!(m = dbus_message_new_method_call(
3014 "org.freedesktop.systemd1",
3015 "/org/freedesktop/systemd1",
3016 "org.freedesktop.systemd1.Manager",
3017 "CreateSnapshot"))) {
3018 log_error("Could not allocate message.");
3022 if (strv_length(args) > 1)
3025 if (!dbus_message_append_args(m,
3026 DBUS_TYPE_STRING, &name,
3027 DBUS_TYPE_BOOLEAN, &cleanup,
3028 DBUS_TYPE_INVALID)) {
3029 log_error("Could not append arguments to message.");
3034 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3035 log_error("Failed to issue method call: %s", bus_error_message(&error));
3040 if (!dbus_message_get_args(reply, &error,
3041 DBUS_TYPE_OBJECT_PATH, &path,
3042 DBUS_TYPE_INVALID)) {
3043 log_error("Failed to parse reply: %s", bus_error_message(&error));
3048 dbus_message_unref(m);
3049 if (!(m = dbus_message_new_method_call(
3050 "org.freedesktop.systemd1",
3052 "org.freedesktop.DBus.Properties",
3054 log_error("Could not allocate message.");
3058 if (!dbus_message_append_args(m,
3059 DBUS_TYPE_STRING, &interface,
3060 DBUS_TYPE_STRING, &property,
3061 DBUS_TYPE_INVALID)) {
3062 log_error("Could not append arguments to message.");
3067 dbus_message_unref(reply);
3068 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3069 log_error("Failed to issue method call: %s", bus_error_message(&error));
3074 if (!dbus_message_iter_init(reply, &iter) ||
3075 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
3076 log_error("Failed to parse reply.");
3081 dbus_message_iter_recurse(&iter, &sub);
3083 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
3084 log_error("Failed to parse reply.");
3089 dbus_message_iter_get_basic(&sub, &id);
3097 dbus_message_unref(m);
3100 dbus_message_unref(reply);
3102 dbus_error_free(&error);
3107 static int delete_snapshot(DBusConnection *bus, char **args) {
3108 DBusMessage *m = NULL, *reply = NULL;
3116 dbus_error_init(&error);
3118 STRV_FOREACH(name, args+1) {
3119 const char *path = NULL;
3121 if (!(m = dbus_message_new_method_call(
3122 "org.freedesktop.systemd1",
3123 "/org/freedesktop/systemd1",
3124 "org.freedesktop.systemd1.Manager",
3126 log_error("Could not allocate message.");
3131 if (!dbus_message_append_args(m,
3132 DBUS_TYPE_STRING, name,
3133 DBUS_TYPE_INVALID)) {
3134 log_error("Could not append arguments to message.");
3139 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3140 log_error("Failed to issue method call: %s", bus_error_message(&error));
3145 if (!dbus_message_get_args(reply, &error,
3146 DBUS_TYPE_OBJECT_PATH, &path,
3147 DBUS_TYPE_INVALID)) {
3148 log_error("Failed to parse reply: %s", bus_error_message(&error));
3153 dbus_message_unref(m);
3154 if (!(m = dbus_message_new_method_call(
3155 "org.freedesktop.systemd1",
3157 "org.freedesktop.systemd1.Snapshot",
3159 log_error("Could not allocate message.");
3164 dbus_message_unref(reply);
3165 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3166 log_error("Failed to issue method call: %s", bus_error_message(&error));
3171 dbus_message_unref(m);
3172 dbus_message_unref(reply);
3180 dbus_message_unref(m);
3183 dbus_message_unref(reply);
3185 dbus_error_free(&error);
3190 static int daemon_reload(DBusConnection *bus, char **args) {
3191 DBusMessage *m = NULL, *reply = NULL;
3196 dbus_error_init(&error);
3198 if (arg_action == ACTION_RELOAD)
3200 else if (arg_action == ACTION_REEXEC)
3201 method = "Reexecute";
3203 assert(arg_action == ACTION_SYSTEMCTL);
3206 streq(args[0], "clear-jobs") ||
3207 streq(args[0], "cancel") ? "ClearJobs" :
3208 streq(args[0], "daemon-reexec") ? "Reexecute" :
3209 streq(args[0], "reset-failed") ? "ResetFailed" :
3210 streq(args[0], "halt") ? "Halt" :
3211 streq(args[0], "poweroff") ? "PowerOff" :
3212 streq(args[0], "reboot") ? "Reboot" :
3213 streq(args[0], "kexec") ? "KExec" :
3214 streq(args[0], "exit") ? "Exit" :
3215 /* "daemon-reload" */ "Reload";
3218 if (!(m = dbus_message_new_method_call(
3219 "org.freedesktop.systemd1",
3220 "/org/freedesktop/systemd1",
3221 "org.freedesktop.systemd1.Manager",
3223 log_error("Could not allocate message.");
3227 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3229 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
3230 /* There's always a fallback possible for
3231 * legacy actions. */
3236 if (streq(method, "Reexecute") && dbus_error_has_name(&error, DBUS_ERROR_NO_REPLY)) {
3237 /* On reexecution, we expect a disconnect, not
3243 log_error("Failed to issue method call: %s", bus_error_message(&error));
3252 dbus_message_unref(m);
3255 dbus_message_unref(reply);
3257 dbus_error_free(&error);
3262 static int reset_failed(DBusConnection *bus, char **args) {
3263 DBusMessage *m = NULL;
3269 dbus_error_init(&error);
3271 if (strv_length(args) <= 1)
3272 return daemon_reload(bus, args);
3274 STRV_FOREACH(name, args+1) {
3277 if (!(m = dbus_message_new_method_call(
3278 "org.freedesktop.systemd1",
3279 "/org/freedesktop/systemd1",
3280 "org.freedesktop.systemd1.Manager",
3281 "ResetFailedUnit"))) {
3282 log_error("Could not allocate message.");
3287 if (!dbus_message_append_args(m,
3288 DBUS_TYPE_STRING, name,
3289 DBUS_TYPE_INVALID)) {
3290 log_error("Could not append arguments to message.");
3295 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3296 log_error("Failed to issue method call: %s", bus_error_message(&error));
3301 dbus_message_unref(m);
3302 dbus_message_unref(reply);
3310 dbus_message_unref(m);
3312 dbus_error_free(&error);
3317 static int show_enviroment(DBusConnection *bus, char **args) {
3318 DBusMessage *m = NULL, *reply = NULL;
3320 DBusMessageIter iter, sub, sub2;
3323 *interface = "org.freedesktop.systemd1.Manager",
3324 *property = "Environment";
3326 dbus_error_init(&error);
3328 pager_open_if_enabled();
3330 if (!(m = dbus_message_new_method_call(
3331 "org.freedesktop.systemd1",
3332 "/org/freedesktop/systemd1",
3333 "org.freedesktop.DBus.Properties",
3335 log_error("Could not allocate message.");
3339 if (!dbus_message_append_args(m,
3340 DBUS_TYPE_STRING, &interface,
3341 DBUS_TYPE_STRING, &property,
3342 DBUS_TYPE_INVALID)) {
3343 log_error("Could not append arguments to message.");
3348 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3349 log_error("Failed to issue method call: %s", bus_error_message(&error));
3354 if (!dbus_message_iter_init(reply, &iter) ||
3355 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
3356 log_error("Failed to parse reply.");
3361 dbus_message_iter_recurse(&iter, &sub);
3363 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
3364 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
3365 log_error("Failed to parse reply.");
3370 dbus_message_iter_recurse(&sub, &sub2);
3372 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
3375 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
3376 log_error("Failed to parse reply.");
3381 dbus_message_iter_get_basic(&sub2, &text);
3382 printf("%s\n", text);
3384 dbus_message_iter_next(&sub2);
3391 dbus_message_unref(m);
3394 dbus_message_unref(reply);
3396 dbus_error_free(&error);
3401 static int set_environment(DBusConnection *bus, char **args) {
3402 DBusMessage *m = NULL, *reply = NULL;
3406 DBusMessageIter iter, sub;
3409 dbus_error_init(&error);
3411 method = streq(args[0], "set-environment")
3413 : "UnsetEnvironment";
3415 if (!(m = dbus_message_new_method_call(
3416 "org.freedesktop.systemd1",
3417 "/org/freedesktop/systemd1",
3418 "org.freedesktop.systemd1.Manager",
3421 log_error("Could not allocate message.");
3425 dbus_message_iter_init_append(m, &iter);
3427 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
3428 log_error("Could not append arguments to message.");
3433 STRV_FOREACH(name, args+1)
3434 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, name)) {
3435 log_error("Could not append arguments to message.");
3440 if (!dbus_message_iter_close_container(&iter, &sub)) {
3441 log_error("Could not append arguments to message.");
3446 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3447 log_error("Failed to issue method call: %s", bus_error_message(&error));
3456 dbus_message_unref(m);
3459 dbus_message_unref(reply);
3461 dbus_error_free(&error);
3466 static int enable_sysv_units(char **args) {
3469 #if defined (HAVE_SYSV_COMPAT) && (defined(TARGET_FEDORA) || defined(TARGET_MANDRIVA) || defined(TARGET_SUSE) || defined(TARGET_MEEGO) || defined(TARGET_ALTLINUX) || defined(TARGET_MAGEIA))
3470 const char *verb = args[0];
3471 unsigned f = 1, t = 1;
3474 if (arg_scope != UNIT_FILE_SYSTEM)
3477 if (!streq(verb, "enable") &&
3478 !streq(verb, "disable") &&
3479 !streq(verb, "is-enabled"))
3482 /* Processes all SysV units, and reshuffles the array so that
3483 * afterwards only the native units remain */
3486 r = lookup_paths_init(&paths, MANAGER_SYSTEM, false);
3492 for (f = 1; args[f]; f++) {
3495 bool found_native = false, found_sysv;
3497 const char *argv[6] = { "/sbin/chkconfig", NULL, NULL, NULL, NULL };
3498 char **k, *l, *q = NULL;
3505 if (!endswith(name, ".service"))
3508 if (path_is_absolute(name))
3511 STRV_FOREACH(k, paths.unit_path) {
3514 if (!isempty(arg_root))
3515 asprintf(&p, "%s/%s/%s", arg_root, *k, name);
3517 asprintf(&p, "%s/%s", *k, name);
3520 log_error("No memory");
3525 found_native = access(p, F_OK) >= 0;
3536 if (!isempty(arg_root))
3537 asprintf(&p, "%s/" SYSTEM_SYSVINIT_PATH "/%s", arg_root, name);
3539 asprintf(&p, SYSTEM_SYSVINIT_PATH "/%s", name);
3541 log_error("No memory");
3546 p[strlen(p) - sizeof(".service") + 1] = 0;
3547 found_sysv = access(p, F_OK) >= 0;
3554 /* Mark this entry, so that we don't try enabling it as native unit */
3555 args[f] = (char*) "";
3557 log_info("%s is not a native service, redirecting to /sbin/chkconfig.", name);
3559 if (!isempty(arg_root))
3560 argv[c++] = q = strappend("--root=", arg_root);
3562 argv[c++] = file_name_from_path(p);
3564 streq(verb, "enable") ? "on" :
3565 streq(verb, "disable") ? "off" : "--level=5";
3568 l = strv_join((char**)argv, " ");
3570 log_error("No memory.");
3577 log_info("Executing %s", l);
3582 log_error("Failed to fork: %m");
3587 } else if (pid == 0) {
3590 execv(argv[0], (char**) argv);
3591 _exit(EXIT_FAILURE);
3597 j = wait_for_terminate(pid, &status);
3599 log_error("Failed to wait for child: %s", strerror(-r));
3604 if (status.si_code == CLD_EXITED) {
3605 if (streq(verb, "is-enabled")) {
3606 if (status.si_status == 0) {
3615 } else if (status.si_status != 0) {
3626 lookup_paths_free(&paths);
3628 /* Drop all SysV units */
3629 for (f = 1, t = 1; args[f]; f++) {
3631 if (isempty(args[f]))
3634 args[t++] = args[f];
3643 static int enable_unit(DBusConnection *bus, char **args) {
3644 const char *verb = args[0];
3645 UnitFileChange *changes = NULL;
3646 unsigned n_changes = 0, i;
3647 int carries_install_info = -1;
3648 DBusMessage *m = NULL, *reply = NULL;
3652 dbus_error_init(&error);
3654 r = enable_sysv_units(args);
3658 if (!bus || avoid_bus()) {
3659 if (streq(verb, "enable")) {
3660 r = unit_file_enable(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3661 carries_install_info = r;
3662 } else if (streq(verb, "disable"))
3663 r = unit_file_disable(arg_scope, arg_runtime, arg_root, args+1, &changes, &n_changes);
3664 else if (streq(verb, "reenable")) {
3665 r = unit_file_reenable(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3666 carries_install_info = r;
3667 } else if (streq(verb, "link"))
3668 r = unit_file_link(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3669 else if (streq(verb, "preset")) {
3670 r = unit_file_preset(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3671 carries_install_info = r;
3672 } else if (streq(verb, "mask"))
3673 r = unit_file_mask(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3674 else if (streq(verb, "unmask"))
3675 r = unit_file_unmask(arg_scope, arg_runtime, arg_root, args+1, &changes, &n_changes);
3677 assert_not_reached("Unknown verb");
3680 log_error("Operation failed: %s", strerror(-r));
3684 for (i = 0; i < n_changes; i++) {
3685 if (changes[i].type == UNIT_FILE_SYMLINK)
3686 log_info("ln -s '%s' '%s'", changes[i].source, changes[i].path);
3688 log_info("rm '%s'", changes[i].path);
3693 bool send_force = true, expect_carries_install_info = false;
3695 DBusMessageIter iter, sub, sub2;
3697 if (streq(verb, "enable")) {
3698 method = "EnableUnitFiles";
3699 expect_carries_install_info = true;
3700 } else if (streq(verb, "disable")) {
3701 method = "DisableUnitFiles";
3703 } else if (streq(verb, "reenable")) {
3704 method = "ReenableUnitFiles";
3705 expect_carries_install_info = true;
3706 } else if (streq(verb, "link"))
3707 method = "LinkUnitFiles";
3708 else if (streq(verb, "preset")) {
3709 method = "PresetUnitFiles";
3710 expect_carries_install_info = true;
3711 } else if (streq(verb, "mask"))
3712 method = "MaskUnitFiles";
3713 else if (streq(verb, "unmask")) {
3714 method = "UnmaskUnitFiles";
3717 assert_not_reached("Unknown verb");
3719 m = dbus_message_new_method_call(
3720 "org.freedesktop.systemd1",
3721 "/org/freedesktop/systemd1",
3722 "org.freedesktop.systemd1.Manager",
3725 log_error("Out of memory");
3730 dbus_message_iter_init_append(m, &iter);
3732 r = bus_append_strv_iter(&iter, args+1);
3734 log_error("Failed to append unit files.");
3739 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &a)) {
3740 log_error("Failed to append runtime boolean.");
3748 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b)) {
3749 log_error("Failed to append force boolean.");
3755 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
3757 log_error("Failed to issue method call: %s", bus_error_message(&error));
3762 if (!dbus_message_iter_init(reply, &iter)) {
3763 log_error("Failed to initialize iterator.");
3767 if (expect_carries_install_info) {
3768 r = bus_iter_get_basic_and_next(&iter, DBUS_TYPE_BOOLEAN, &b, true);
3770 log_error("Failed to parse reply.");
3774 carries_install_info = b;
3777 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
3778 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
3779 log_error("Failed to parse reply.");
3784 dbus_message_iter_recurse(&iter, &sub);
3785 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
3786 const char *type, *path, *source;
3788 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
3789 log_error("Failed to parse reply.");
3794 dbus_message_iter_recurse(&sub, &sub2);
3796 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
3797 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0 ||
3798 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &source, false) < 0) {
3799 log_error("Failed to parse reply.");
3804 if (streq(type, "symlink"))
3805 log_info("ln -s '%s' '%s'", source, path);
3807 log_info("rm '%s'", path);
3809 dbus_message_iter_next(&sub);
3812 /* Try to reload if enabeld */
3814 r = daemon_reload(bus, args);
3817 if (carries_install_info == 0)
3818 log_warning("Warning: unit files do not carry install information. No operation executed.");
3822 dbus_message_unref(m);
3825 dbus_message_unref(reply);
3827 unit_file_changes_free(changes, n_changes);
3829 dbus_error_free(&error);
3833 static int unit_is_enabled(DBusConnection *bus, char **args) {
3836 DBusMessage *m = NULL, *reply = NULL;
3840 dbus_error_init(&error);
3842 r = enable_sysv_units(args);
3848 if (!bus || avoid_bus()) {
3850 STRV_FOREACH(name, args+1) {
3851 UnitFileState state;
3853 state = unit_file_get_state(arg_scope, arg_root, *name);
3859 if (state == UNIT_FILE_ENABLED ||
3860 state == UNIT_FILE_ENABLED_RUNTIME ||
3861 state == UNIT_FILE_STATIC)
3865 puts(unit_file_state_to_string(state));
3869 STRV_FOREACH(name, args+1) {
3872 m = dbus_message_new_method_call(
3873 "org.freedesktop.systemd1",
3874 "/org/freedesktop/systemd1",
3875 "org.freedesktop.systemd1.Manager",
3876 "GetUnitFileState");
3878 log_error("Out of memory");
3883 if (!dbus_message_append_args(m,
3884 DBUS_TYPE_STRING, name,
3885 DBUS_TYPE_INVALID)) {
3886 log_error("Could not append arguments to message.");
3891 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
3893 log_error("Failed to issue method call: %s", bus_error_message(&error));
3898 if (!dbus_message_get_args(reply, &error,
3899 DBUS_TYPE_STRING, &s,
3900 DBUS_TYPE_INVALID)) {
3901 log_error("Failed to parse reply: %s", bus_error_message(&error));
3906 dbus_message_unref(m);
3907 dbus_message_unref(reply);
3910 if (streq(s, "enabled") ||
3911 streq(s, "enabled-runtime") ||
3920 r = enabled ? 0 : 1;
3924 dbus_message_unref(m);
3927 dbus_message_unref(reply);
3929 dbus_error_free(&error);
3933 static int systemctl_help(void) {
3935 pager_open_if_enabled();
3937 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
3938 "Query or send control commands to the systemd manager.\n\n"
3939 " -h --help Show this help\n"
3940 " --version Show package version\n"
3941 " -t --type=TYPE List only units of a particular type\n"
3942 " -p --property=NAME Show only properties by this name\n"
3943 " -a --all Show all units/properties, including dead/empty ones\n"
3944 " --failed Show only failed units\n"
3945 " --full Don't ellipsize unit names on output\n"
3946 " --fail When queueing a new job, fail if conflicting jobs are\n"
3948 " --ignore-dependencies\n"
3949 " When queueing a new job, ignore all its dependencies\n"
3950 " --kill-who=WHO Who to send signal to\n"
3951 " -s --signal=SIGNAL Which signal to send\n"
3952 " -H --host=[USER@]HOST\n"
3953 " Show information for remote host\n"
3954 " -P --privileged Acquire privileges before execution\n"
3955 " -q --quiet Suppress output\n"
3956 " --no-block Do not wait until operation finished\n"
3957 " --no-wall Don't send wall message before halt/power-off/reboot\n"
3958 " --no-reload When enabling/disabling unit files, don't reload daemon\n"
3960 " --no-legend Do not print a legend (column headers and hints)\n"
3961 " --no-pager Do not pipe output into a pager\n"
3962 " --no-ask-password\n"
3963 " Do not ask for system passwords\n"
3964 " --order When generating graph for dot, show only order\n"
3965 " --require When generating graph for dot, show only requirement\n"
3966 " --system Connect to system manager\n"
3967 " --user Connect to user service manager\n"
3968 " --global Enable/disable unit files globally\n"
3969 " -f --force When enabling unit files, override existing symlinks\n"
3970 " When shutting down, execute action immediately\n"
3971 " --root=PATH Enable unit files in the specified root directory\n"
3972 " --runtime Enable unit files only temporarily until next reboot\n\n"
3974 " list-units List loaded units\n"
3975 " start [NAME...] Start (activate) one or more units\n"
3976 " stop [NAME...] Stop (deactivate) one or more units\n"
3977 " reload [NAME...] Reload one or more units\n"
3978 " restart [NAME...] Start or restart one or more units\n"
3979 " try-restart [NAME...] Restart one or more units if active\n"
3980 " reload-or-restart [NAME...] Reload one or more units is possible,\n"
3981 " otherwise start or restart\n"
3982 " reload-or-try-restart [NAME...] Reload one or more units is possible,\n"
3983 " otherwise restart if active\n"
3984 " isolate [NAME] Start one unit and stop all others\n"
3985 " kill [NAME...] Send signal to processes of a unit\n"
3986 " is-active [NAME...] Check whether units are active\n"
3987 " status [NAME...|PID...] Show runtime status of one or more units\n"
3988 " show [NAME...|JOB...] Show properties of one or more\n"
3989 " units/jobs or the manager\n"
3990 " reset-failed [NAME...] Reset failed state for all, one, or more\n"
3992 " load [NAME...] Load one or more units\n\n"
3993 "Unit File Commands:\n"
3994 " list-unit-files List installed unit files\n"
3995 " enable [NAME...] Enable one or more unit files\n"
3996 " disable [NAME...] Disable one or more unit files\n"
3997 " reenable [NAME...] Reenable one or more unit files\n"
3998 " preset [NAME...] Enable/disable one or more unit files\n"
3999 " based on preset configuration\n"
4000 " mask [NAME...] Mask one or more units\n"
4001 " unmask [NAME...] Unmask one or more units\n"
4002 " link [PATH...] Link one or more units files into\n"
4003 " the search path\n"
4004 " is-enabled [NAME...] Check whether unit files are enabled\n\n"
4006 " list-jobs List jobs\n"
4007 " cancel [JOB...] Cancel all, one, or more jobs\n\n"
4008 "Status Commands:\n"
4009 " dump Dump server status\n"
4010 " dot Dump dependency graph for dot(1)\n\n"
4011 "Snapshot Commands:\n"
4012 " snapshot [NAME] Create a snapshot\n"
4013 " delete [NAME...] Remove one or more snapshots\n\n"
4014 "Environment Commands:\n"
4015 " show-environment Dump environment\n"
4016 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
4017 " unset-environment [NAME...] Unset one or more environment variables\n\n"
4018 "Manager Lifecycle Commands:\n"
4019 " daemon-reload Reload systemd manager configuration\n"
4020 " daemon-reexec Reexecute systemd manager\n\n"
4021 "System Commands:\n"
4022 " default Enter system default mode\n"
4023 " rescue Enter system rescue mode\n"
4024 " emergency Enter system emergency mode\n"
4025 " halt Shut down and halt the system\n"
4026 " poweroff Shut down and power-off the system\n"
4027 " reboot Shut down and reboot the system\n"
4028 " kexec Shut down and reboot the system with kexec\n"
4029 " exit Ask for user instance termination\n",
4030 program_invocation_short_name);
4035 static int halt_help(void) {
4037 printf("%s [OPTIONS...]\n\n"
4038 "%s the system.\n\n"
4039 " --help Show this help\n"
4040 " --halt Halt the machine\n"
4041 " -p --poweroff Switch off the machine\n"
4042 " --reboot Reboot the machine\n"
4043 " -f --force Force immediate halt/power-off/reboot\n"
4044 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
4045 " -d --no-wtmp Don't write wtmp record\n"
4046 " -n --no-sync Don't sync before halt/power-off/reboot\n"
4047 " --no-wall Don't send wall message before halt/power-off/reboot\n",
4048 program_invocation_short_name,
4049 arg_action == ACTION_REBOOT ? "Reboot" :
4050 arg_action == ACTION_POWEROFF ? "Power off" :
4056 static int shutdown_help(void) {
4058 printf("%s [OPTIONS...] [TIME] [WALL...]\n\n"
4059 "Shut down the system.\n\n"
4060 " --help Show this help\n"
4061 " -H --halt Halt the machine\n"
4062 " -P --poweroff Power-off the machine\n"
4063 " -r --reboot Reboot the machine\n"
4064 " -h Equivalent to --poweroff, overriden by --halt\n"
4065 " -k Don't halt/power-off/reboot, just send warnings\n"
4066 " --no-wall Don't send wall message before halt/power-off/reboot\n"
4067 " -c Cancel a pending shutdown\n",
4068 program_invocation_short_name);
4073 static int telinit_help(void) {
4075 printf("%s [OPTIONS...] {COMMAND}\n\n"
4076 "Send control commands to the init daemon.\n\n"
4077 " --help Show this help\n"
4078 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
4080 " 0 Power-off the machine\n"
4081 " 6 Reboot the machine\n"
4082 " 2, 3, 4, 5 Start runlevelX.target unit\n"
4083 " 1, s, S Enter rescue mode\n"
4084 " q, Q Reload init daemon configuration\n"
4085 " u, U Reexecute init daemon\n",
4086 program_invocation_short_name);
4091 static int runlevel_help(void) {
4093 printf("%s [OPTIONS...]\n\n"
4094 "Prints the previous and current runlevel of the init system.\n\n"
4095 " --help Show this help\n",
4096 program_invocation_short_name);
4101 static int systemctl_parse_argv(int argc, char *argv[]) {
4105 ARG_IGNORE_DEPENDENCIES,
4121 ARG_NO_ASK_PASSWORD,
4126 static const struct option options[] = {
4127 { "help", no_argument, NULL, 'h' },
4128 { "version", no_argument, NULL, ARG_VERSION },
4129 { "type", required_argument, NULL, 't' },
4130 { "property", required_argument, NULL, 'p' },
4131 { "all", no_argument, NULL, 'a' },
4132 { "failed", no_argument, NULL, ARG_FAILED },
4133 { "full", no_argument, NULL, ARG_FULL },
4134 { "fail", no_argument, NULL, ARG_FAIL },
4135 { "ignore-dependencies", no_argument, NULL, ARG_IGNORE_DEPENDENCIES },
4136 { "user", no_argument, NULL, ARG_USER },
4137 { "system", no_argument, NULL, ARG_SYSTEM },
4138 { "global", no_argument, NULL, ARG_GLOBAL },
4139 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
4140 { "no-legend", no_argument, NULL, ARG_NO_LEGEND },
4141 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
4142 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4143 { "quiet", no_argument, NULL, 'q' },
4144 { "order", no_argument, NULL, ARG_ORDER },
4145 { "require", no_argument, NULL, ARG_REQUIRE },
4146 { "root", required_argument, NULL, ARG_ROOT },
4147 { "force", no_argument, NULL, 'f' },
4148 { "no-reload", no_argument, NULL, ARG_NO_RELOAD },
4149 { "kill-mode", required_argument, NULL, ARG_KILL_MODE }, /* undocumented on purpose */
4150 { "kill-who", required_argument, NULL, ARG_KILL_WHO },
4151 { "signal", required_argument, NULL, 's' },
4152 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
4153 { "host", required_argument, NULL, 'H' },
4154 { "privileged",no_argument, NULL, 'P' },
4155 { "runtime", no_argument, NULL, ARG_RUNTIME },
4156 { NULL, 0, NULL, 0 }
4164 /* Only when running as systemctl we ask for passwords */
4165 arg_ask_password = true;
4167 while ((c = getopt_long(argc, argv, "ht:p:aqfs:H:P", options, NULL)) >= 0) {
4176 puts(PACKAGE_STRING);
4178 puts(SYSTEMD_FEATURES);
4188 if (!(l = strv_append(arg_property, optarg)))
4191 strv_free(arg_property);
4194 /* If the user asked for a particular
4195 * property, show it to him, even if it is
4206 arg_job_mode = "fail";
4209 case ARG_IGNORE_DEPENDENCIES:
4210 arg_job_mode = "ignore-dependencies";
4214 arg_scope = UNIT_FILE_USER;
4218 arg_scope = UNIT_FILE_SYSTEM;
4222 arg_scope = UNIT_FILE_GLOBAL;
4226 arg_no_block = true;
4230 arg_no_legend = true;
4234 arg_no_pager = true;
4242 arg_dot = DOT_ORDER;
4246 arg_dot = DOT_REQUIRE;
4270 arg_no_reload = true;
4274 arg_kill_who = optarg;
4278 arg_kill_mode = optarg;
4282 if ((arg_signal = signal_from_string_try_harder(optarg)) < 0) {
4283 log_error("Failed to parse signal string %s.", optarg);
4288 case ARG_NO_ASK_PASSWORD:
4289 arg_ask_password = false;
4293 arg_transport = TRANSPORT_POLKIT;
4297 arg_transport = TRANSPORT_SSH;
4309 log_error("Unknown option code %c", c);
4314 if (arg_transport != TRANSPORT_NORMAL && arg_scope != UNIT_FILE_SYSTEM) {
4315 log_error("Cannot access user instance remotely.");
4322 static int halt_parse_argv(int argc, char *argv[]) {
4331 static const struct option options[] = {
4332 { "help", no_argument, NULL, ARG_HELP },
4333 { "halt", no_argument, NULL, ARG_HALT },
4334 { "poweroff", no_argument, NULL, 'p' },
4335 { "reboot", no_argument, NULL, ARG_REBOOT },
4336 { "force", no_argument, NULL, 'f' },
4337 { "wtmp-only", no_argument, NULL, 'w' },
4338 { "no-wtmp", no_argument, NULL, 'd' },
4339 { "no-sync", no_argument, NULL, 'n' },
4340 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4341 { NULL, 0, NULL, 0 }
4349 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
4350 if (runlevel == '0' || runlevel == '6')
4351 arg_immediate = true;
4353 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
4361 arg_action = ACTION_HALT;
4365 if (arg_action != ACTION_REBOOT)
4366 arg_action = ACTION_POWEROFF;
4370 arg_action = ACTION_REBOOT;
4374 arg_immediate = true;
4395 /* Compatibility nops */
4402 log_error("Unknown option code %c", c);
4407 if (optind < argc) {
4408 log_error("Too many arguments.");
4415 static int parse_time_spec(const char *t, usec_t *_u) {
4419 if (streq(t, "now"))
4421 else if (!strchr(t, ':')) {
4424 if (safe_atou64(t, &u) < 0)
4427 *_u = now(CLOCK_REALTIME) + USEC_PER_MINUTE * u;
4436 hour = strtol(t, &e, 10);
4437 if (errno != 0 || *e != ':' || hour < 0 || hour > 23)
4440 minute = strtol(e+1, &e, 10);
4441 if (errno != 0 || *e != 0 || minute < 0 || minute > 59)
4444 n = now(CLOCK_REALTIME);
4445 s = (time_t) (n / USEC_PER_SEC);
4448 assert_se(localtime_r(&s, &tm));
4450 tm.tm_hour = (int) hour;
4451 tm.tm_min = (int) minute;
4454 assert_se(s = mktime(&tm));
4456 *_u = (usec_t) s * USEC_PER_SEC;
4459 *_u += USEC_PER_DAY;
4465 static int shutdown_parse_argv(int argc, char *argv[]) {
4472 static const struct option options[] = {
4473 { "help", no_argument, NULL, ARG_HELP },
4474 { "halt", no_argument, NULL, 'H' },
4475 { "poweroff", no_argument, NULL, 'P' },
4476 { "reboot", no_argument, NULL, 'r' },
4477 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4478 { NULL, 0, NULL, 0 }
4486 while ((c = getopt_long(argc, argv, "HPrhkt:afFc", options, NULL)) >= 0) {
4494 arg_action = ACTION_HALT;
4498 arg_action = ACTION_POWEROFF;
4503 arg_action = ACTION_KEXEC;
4505 arg_action = ACTION_REBOOT;
4509 if (arg_action != ACTION_HALT)
4510 arg_action = ACTION_POWEROFF;
4523 /* Compatibility nops */
4527 arg_action = ACTION_CANCEL_SHUTDOWN;
4534 log_error("Unknown option code %c", c);
4539 if (argc > optind) {
4540 if ((r = parse_time_spec(argv[optind], &arg_when)) < 0) {
4541 log_error("Failed to parse time specification: %s", argv[optind]);
4545 arg_when = now(CLOCK_REALTIME) + USEC_PER_MINUTE;
4547 /* We skip the time argument */
4548 if (argc > optind + 1)
4549 arg_wall = argv + optind + 1;
4556 static int telinit_parse_argv(int argc, char *argv[]) {
4563 static const struct option options[] = {
4564 { "help", no_argument, NULL, ARG_HELP },
4565 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4566 { NULL, 0, NULL, 0 }
4569 static const struct {
4573 { '0', ACTION_POWEROFF },
4574 { '6', ACTION_REBOOT },
4575 { '1', ACTION_RESCUE },
4576 { '2', ACTION_RUNLEVEL2 },
4577 { '3', ACTION_RUNLEVEL3 },
4578 { '4', ACTION_RUNLEVEL4 },
4579 { '5', ACTION_RUNLEVEL5 },
4580 { 's', ACTION_RESCUE },
4581 { 'S', ACTION_RESCUE },
4582 { 'q', ACTION_RELOAD },
4583 { 'Q', ACTION_RELOAD },
4584 { 'u', ACTION_REEXEC },
4585 { 'U', ACTION_REEXEC }
4594 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4609 log_error("Unknown option code %c", c);
4614 if (optind >= argc) {
4619 if (optind + 1 < argc) {
4620 log_error("Too many arguments.");
4624 if (strlen(argv[optind]) != 1) {
4625 log_error("Expected single character argument.");
4629 for (i = 0; i < ELEMENTSOF(table); i++)
4630 if (table[i].from == argv[optind][0])
4633 if (i >= ELEMENTSOF(table)) {
4634 log_error("Unknown command %s.", argv[optind]);
4638 arg_action = table[i].to;
4645 static int runlevel_parse_argv(int argc, char *argv[]) {
4651 static const struct option options[] = {
4652 { "help", no_argument, NULL, ARG_HELP },
4653 { NULL, 0, NULL, 0 }
4661 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4672 log_error("Unknown option code %c", c);
4677 if (optind < argc) {
4678 log_error("Too many arguments.");
4685 static int parse_argv(int argc, char *argv[]) {
4689 if (program_invocation_short_name) {
4691 if (strstr(program_invocation_short_name, "halt")) {
4692 arg_action = ACTION_HALT;
4693 return halt_parse_argv(argc, argv);
4694 } else if (strstr(program_invocation_short_name, "poweroff")) {
4695 arg_action = ACTION_POWEROFF;
4696 return halt_parse_argv(argc, argv);
4697 } else if (strstr(program_invocation_short_name, "reboot")) {
4699 arg_action = ACTION_KEXEC;
4701 arg_action = ACTION_REBOOT;
4702 return halt_parse_argv(argc, argv);
4703 } else if (strstr(program_invocation_short_name, "shutdown")) {
4704 arg_action = ACTION_POWEROFF;
4705 return shutdown_parse_argv(argc, argv);
4706 } else if (strstr(program_invocation_short_name, "init")) {
4708 if (sd_booted() > 0) {
4709 arg_action = ACTION_INVALID;
4710 return telinit_parse_argv(argc, argv);
4712 /* Hmm, so some other init system is
4713 * running, we need to forward this
4714 * request to it. For now we simply
4715 * guess that it is Upstart. */
4717 execv("/lib/upstart/telinit", argv);
4719 log_error("Couldn't find an alternative telinit implementation to spawn.");
4723 } else if (strstr(program_invocation_short_name, "runlevel")) {
4724 arg_action = ACTION_RUNLEVEL;
4725 return runlevel_parse_argv(argc, argv);
4729 arg_action = ACTION_SYSTEMCTL;
4730 return systemctl_parse_argv(argc, argv);
4733 static int action_to_runlevel(void) {
4735 static const char table[_ACTION_MAX] = {
4736 [ACTION_HALT] = '0',
4737 [ACTION_POWEROFF] = '0',
4738 [ACTION_REBOOT] = '6',
4739 [ACTION_RUNLEVEL2] = '2',
4740 [ACTION_RUNLEVEL3] = '3',
4741 [ACTION_RUNLEVEL4] = '4',
4742 [ACTION_RUNLEVEL5] = '5',
4743 [ACTION_RESCUE] = '1'
4746 assert(arg_action < _ACTION_MAX);
4748 return table[arg_action];
4751 static int talk_upstart(void) {
4752 DBusMessage *m = NULL, *reply = NULL;
4754 int previous, rl, r;
4756 env1_buf[] = "RUNLEVEL=X",
4757 env2_buf[] = "PREVLEVEL=X";
4758 char *env1 = env1_buf, *env2 = env2_buf;
4759 const char *emit = "runlevel";
4760 dbus_bool_t b_false = FALSE;
4761 DBusMessageIter iter, sub;
4762 DBusConnection *bus;
4764 dbus_error_init(&error);
4766 if (!(rl = action_to_runlevel()))
4769 if (utmp_get_runlevel(&previous, NULL) < 0)
4772 if (!(bus = dbus_connection_open_private("unix:abstract=/com/ubuntu/upstart", &error))) {
4773 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
4778 log_error("Failed to connect to Upstart bus: %s", bus_error_message(&error));
4783 if ((r = bus_check_peercred(bus)) < 0) {
4784 log_error("Failed to verify owner of bus.");
4788 if (!(m = dbus_message_new_method_call(
4789 "com.ubuntu.Upstart",
4790 "/com/ubuntu/Upstart",
4791 "com.ubuntu.Upstart0_6",
4794 log_error("Could not allocate message.");
4799 dbus_message_iter_init_append(m, &iter);
4801 env1_buf[sizeof(env1_buf)-2] = rl;
4802 env2_buf[sizeof(env2_buf)-2] = previous;
4804 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
4805 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
4806 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
4807 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
4808 !dbus_message_iter_close_container(&iter, &sub) ||
4809 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
4810 log_error("Could not append arguments to message.");
4815 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
4817 if (error_is_no_service(&error)) {
4822 log_error("Failed to issue method call: %s", bus_error_message(&error));
4831 dbus_message_unref(m);
4834 dbus_message_unref(reply);
4837 dbus_connection_flush(bus);
4838 dbus_connection_close(bus);
4839 dbus_connection_unref(bus);
4842 dbus_error_free(&error);
4847 static int talk_initctl(void) {
4848 struct init_request request;
4852 if (!(rl = action_to_runlevel()))
4856 request.magic = INIT_MAGIC;
4857 request.sleeptime = 0;
4858 request.cmd = INIT_CMD_RUNLVL;
4859 request.runlevel = rl;
4861 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
4863 if (errno == ENOENT)
4866 log_error("Failed to open "INIT_FIFO": %m");
4871 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
4872 close_nointr_nofail(fd);
4875 log_error("Failed to write to "INIT_FIFO": %m");
4876 return errno ? -errno : -EIO;
4882 static int systemctl_main(DBusConnection *bus, int argc, char *argv[], DBusError *error) {
4884 static const struct {
4892 int (* const dispatch)(DBusConnection *bus, char **args);
4894 { "list-units", LESS, 1, list_units },
4895 { "list-unit-files", EQUAL, 1, list_unit_files },
4896 { "list-jobs", EQUAL, 1, list_jobs },
4897 { "clear-jobs", EQUAL, 1, daemon_reload },
4898 { "load", MORE, 2, load_unit },
4899 { "cancel", MORE, 2, cancel_job },
4900 { "start", MORE, 2, start_unit },
4901 { "stop", MORE, 2, start_unit },
4902 { "condstop", MORE, 2, start_unit }, /* For compatibility with ALTLinux */
4903 { "reload", MORE, 2, start_unit },
4904 { "restart", MORE, 2, start_unit },
4905 { "try-restart", MORE, 2, start_unit },
4906 { "reload-or-restart", MORE, 2, start_unit },
4907 { "reload-or-try-restart", MORE, 2, start_unit },
4908 { "force-reload", MORE, 2, start_unit }, /* For compatibility with SysV */
4909 { "condreload", MORE, 2, start_unit }, /* For compatibility with ALTLinux */
4910 { "condrestart", MORE, 2, start_unit }, /* For compatibility with RH */
4911 { "isolate", EQUAL, 2, start_unit },
4912 { "kill", MORE, 2, kill_unit },
4913 { "is-active", MORE, 2, check_unit },
4914 { "check", MORE, 2, check_unit },
4915 { "show", MORE, 1, show },
4916 { "status", MORE, 2, show },
4917 { "dump", EQUAL, 1, dump },
4918 { "dot", EQUAL, 1, dot },
4919 { "snapshot", LESS, 2, snapshot },
4920 { "delete", MORE, 2, delete_snapshot },
4921 { "daemon-reload", EQUAL, 1, daemon_reload },
4922 { "daemon-reexec", EQUAL, 1, daemon_reload },
4923 { "show-environment", EQUAL, 1, show_enviroment },
4924 { "set-environment", MORE, 2, set_environment },
4925 { "unset-environment", MORE, 2, set_environment },
4926 { "halt", EQUAL, 1, start_special },
4927 { "poweroff", EQUAL, 1, start_special },
4928 { "reboot", EQUAL, 1, start_special },
4929 { "kexec", EQUAL, 1, start_special },
4930 { "default", EQUAL, 1, start_special },
4931 { "rescue", EQUAL, 1, start_special },
4932 { "emergency", EQUAL, 1, start_special },
4933 { "exit", EQUAL, 1, start_special },
4934 { "reset-failed", MORE, 1, reset_failed },
4935 { "enable", MORE, 2, enable_unit },
4936 { "disable", MORE, 2, enable_unit },
4937 { "is-enabled", MORE, 2, unit_is_enabled },
4938 { "reenable", MORE, 2, enable_unit },
4939 { "preset", MORE, 2, enable_unit },
4940 { "mask", MORE, 2, enable_unit },
4941 { "unmask", MORE, 2, enable_unit },
4942 { "link", MORE, 2, enable_unit }
4952 left = argc - optind;
4955 /* Special rule: no arguments means "list-units" */
4958 if (streq(argv[optind], "help")) {
4963 for (i = 0; i < ELEMENTSOF(verbs); i++)
4964 if (streq(argv[optind], verbs[i].verb))
4967 if (i >= ELEMENTSOF(verbs)) {
4968 log_error("Unknown operation %s", argv[optind]);
4973 switch (verbs[i].argc_cmp) {
4976 if (left != verbs[i].argc) {
4977 log_error("Invalid number of arguments.");
4984 if (left < verbs[i].argc) {
4985 log_error("Too few arguments.");
4992 if (left > verbs[i].argc) {
4993 log_error("Too many arguments.");
5000 assert_not_reached("Unknown comparison operator.");
5003 /* Require a bus connection for all operations but
5005 if (!streq(verbs[i].verb, "enable") &&
5006 !streq(verbs[i].verb, "disable") &&
5007 !streq(verbs[i].verb, "is-enabled") &&
5008 !streq(verbs[i].verb, "list-unit-files") &&
5009 !streq(verbs[i].verb, "reenable") &&
5010 !streq(verbs[i].verb, "preset") &&
5011 !streq(verbs[i].verb, "mask") &&
5012 !streq(verbs[i].verb, "unmask") &&
5013 !streq(verbs[i].verb, "link")) {
5015 if (running_in_chroot() > 0) {
5016 log_info("Running in chroot, ignoring request.");
5021 log_error("Failed to get D-Bus connection: %s",
5022 dbus_error_is_set(error) ? error->message : "No connection to service manager.");
5028 if (!bus && !avoid_bus()) {
5029 log_error("Failed to get D-Bus connection: %s",
5030 dbus_error_is_set(error) ? error->message : "No connection to service manager.");
5035 return verbs[i].dispatch(bus, argv + optind);
5038 static int send_shutdownd(usec_t t, char mode, bool dry_run, bool warn, const char *message) {
5040 struct msghdr msghdr;
5042 union sockaddr_union sockaddr;
5043 struct shutdownd_command c;
5048 c.dry_run = dry_run;
5052 strncpy(c.wall_message, message, sizeof(c.wall_message));
5054 if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0)
5058 sockaddr.sa.sa_family = AF_UNIX;
5059 sockaddr.un.sun_path[0] = 0;
5060 strncpy(sockaddr.un.sun_path, "/run/systemd/shutdownd", sizeof(sockaddr.un.sun_path));
5063 iovec.iov_base = (char*) &c;
5064 iovec.iov_len = sizeof(c);
5067 msghdr.msg_name = &sockaddr;
5068 msghdr.msg_namelen = offsetof(struct sockaddr_un, sun_path) + sizeof("/run/systemd/shutdownd") - 1;
5070 msghdr.msg_iov = &iovec;
5071 msghdr.msg_iovlen = 1;
5073 if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) < 0) {
5074 close_nointr_nofail(fd);
5078 close_nointr_nofail(fd);
5082 static int reload_with_fallback(DBusConnection *bus) {
5085 /* First, try systemd via D-Bus. */
5086 if (daemon_reload(bus, NULL) >= 0)
5090 /* Nothing else worked, so let's try signals */
5091 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
5093 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
5094 log_error("kill() failed: %m");
5101 static int start_with_fallback(DBusConnection *bus) {
5104 /* First, try systemd via D-Bus. */
5105 if (start_unit(bus, NULL) >= 0)
5109 /* Hmm, talking to systemd via D-Bus didn't work. Then
5110 * let's try to talk to Upstart via D-Bus. */
5111 if (talk_upstart() > 0)
5114 /* Nothing else worked, so let's try
5116 if (talk_initctl() > 0)
5119 log_error("Failed to talk to init daemon.");
5123 warn_wall(arg_action);
5127 static int halt_main(DBusConnection *bus) {
5130 if (geteuid() != 0) {
5131 log_error("Must be root.");
5137 char date[FORMAT_TIMESTAMP_MAX];
5139 m = strv_join(arg_wall, " ");
5140 r = send_shutdownd(arg_when,
5141 arg_action == ACTION_HALT ? 'H' :
5142 arg_action == ACTION_POWEROFF ? 'P' :
5150 log_warning("Failed to talk to shutdownd, proceeding with immediate shutdown: %s", strerror(-r));
5152 log_info("Shutdown scheduled for %s, use 'shutdown -c' to cancel.",
5153 format_timestamp(date, sizeof(date), arg_when));
5158 if (!arg_dry && !arg_immediate)
5159 return start_with_fallback(bus);
5162 if (sd_booted() > 0)
5163 log_debug("Not writing utmp record, assuming that systemd-update-utmp is used.");
5164 else if ((r = utmp_put_shutdown()) < 0)
5165 log_warning("Failed to write utmp record: %s", strerror(-r));
5174 /* Make sure C-A-D is handled by the kernel from this
5176 reboot(RB_ENABLE_CAD);
5178 switch (arg_action) {
5181 log_info("Halting.");
5182 reboot(RB_HALT_SYSTEM);
5185 case ACTION_POWEROFF:
5186 log_info("Powering off.");
5187 reboot(RB_POWER_OFF);
5191 log_info("Rebooting.");
5192 reboot(RB_AUTOBOOT);
5196 assert_not_reached("Unknown halt action.");
5199 /* We should never reach this. */
5203 static int runlevel_main(void) {
5204 int r, runlevel, previous;
5206 r = utmp_get_runlevel(&runlevel, &previous);
5213 previous <= 0 ? 'N' : previous,
5214 runlevel <= 0 ? 'N' : runlevel);
5219 int main(int argc, char*argv[]) {
5220 int r, retval = EXIT_FAILURE;
5221 DBusConnection *bus = NULL;
5224 dbus_error_init(&error);
5226 log_parse_environment();
5229 if ((r = parse_argv(argc, argv)) < 0)
5232 retval = EXIT_SUCCESS;
5236 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
5237 * let's shortcut this */
5238 if (arg_action == ACTION_RUNLEVEL) {
5239 r = runlevel_main();
5240 retval = r < 0 ? EXIT_FAILURE : r;
5244 if (running_in_chroot() > 0 && arg_action != ACTION_SYSTEMCTL) {
5245 log_info("Running in chroot, ignoring request.");
5251 if (arg_transport == TRANSPORT_NORMAL)
5252 bus_connect(arg_scope == UNIT_FILE_SYSTEM ? DBUS_BUS_SYSTEM : DBUS_BUS_SESSION, &bus, &private_bus, &error);
5253 else if (arg_transport == TRANSPORT_POLKIT) {
5254 bus_connect_system_polkit(&bus, &error);
5255 private_bus = false;
5256 } else if (arg_transport == TRANSPORT_SSH) {
5257 bus_connect_system_ssh(NULL, arg_host, &bus, &error);
5258 private_bus = false;
5260 assert_not_reached("Uh, invalid transport...");
5263 switch (arg_action) {
5265 case ACTION_SYSTEMCTL:
5266 r = systemctl_main(bus, argc, argv, &error);
5270 case ACTION_POWEROFF:
5276 case ACTION_RUNLEVEL2:
5277 case ACTION_RUNLEVEL3:
5278 case ACTION_RUNLEVEL4:
5279 case ACTION_RUNLEVEL5:
5281 case ACTION_EMERGENCY:
5282 case ACTION_DEFAULT:
5283 r = start_with_fallback(bus);
5288 r = reload_with_fallback(bus);
5291 case ACTION_CANCEL_SHUTDOWN:
5292 r = send_shutdownd(0, 0, false, false, NULL);
5295 case ACTION_INVALID:
5296 case ACTION_RUNLEVEL:
5298 assert_not_reached("Unknown action");
5301 retval = r < 0 ? EXIT_FAILURE : r;
5305 dbus_connection_flush(bus);
5306 dbus_connection_close(bus);
5307 dbus_connection_unref(bus);
5310 dbus_error_free(&error);
5314 strv_free(arg_property);