1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/reboot.h>
28 #include <sys/ioctl.h>
32 #include <sys/socket.h>
35 #include <sys/prctl.h>
36 #include <dbus/dbus.h>
38 #include <systemd/sd-daemon.h>
44 #include "utmp-wtmp.h"
48 #include "dbus-common.h"
49 #include "cgroup-show.h"
50 #include "cgroup-util.h"
52 #include "path-lookup.h"
53 #include "conf-parser.h"
54 #include "shutdownd.h"
55 #include "exit-status.h"
56 #include "bus-errors.h"
58 #include "unit-name.h"
60 #include "spawn-agent.h"
62 #include "logs-show.h"
64 static const char *arg_type = NULL;
65 static char **arg_property = NULL;
66 static bool arg_all = false;
67 static const char *arg_job_mode = "replace";
68 static UnitFileScope arg_scope = UNIT_FILE_SYSTEM;
69 static bool arg_immediate = false;
70 static bool arg_no_block = false;
71 static bool arg_no_legend = false;
72 static bool arg_no_pager = false;
73 static bool arg_no_wtmp = false;
74 static bool arg_no_sync = false;
75 static bool arg_no_wall = false;
76 static bool arg_no_reload = false;
77 static bool arg_dry = false;
78 static bool arg_quiet = false;
79 static bool arg_full = false;
80 static bool arg_force = false;
81 static bool arg_ask_password = false;
82 static bool arg_failed = false;
83 static bool arg_runtime = false;
84 static char **arg_wall = NULL;
85 static const char *arg_kill_who = NULL;
86 static const char *arg_kill_mode = NULL;
87 static int arg_signal = SIGTERM;
88 static const char *arg_root = NULL;
89 static usec_t arg_when = 0;
108 ACTION_CANCEL_SHUTDOWN,
110 } arg_action = ACTION_SYSTEMCTL;
116 static enum transport {
120 } arg_transport = TRANSPORT_NORMAL;
121 static const char *arg_host = NULL;
122 static bool arg_follow = false;
123 static unsigned arg_lines = 10;
124 static OutputMode arg_output = OUTPUT_SHORT;
126 static bool private_bus = false;
128 static int daemon_reload(DBusConnection *bus, char **args);
130 static bool on_tty(void) {
133 /* Note that this is invoked relatively early, before we start
134 * the pager. That means the value we return reflects whether
135 * we originally were started on a tty, not if we currently
136 * are. But this is intended, since we want colour and so on
137 * when run in our own pager. */
139 if (_unlikely_(t < 0))
140 t = isatty(STDOUT_FILENO) > 0;
145 static void pager_open_if_enabled(void) {
147 /* Cache result before we open the pager */
156 static void agent_open_if_enabled(void) {
158 /* Open the password agent as a child process if necessary */
160 if (!arg_ask_password)
163 if (arg_scope != UNIT_FILE_SYSTEM)
169 static const char *ansi_highlight_red(bool b) {
174 return b ? ANSI_HIGHLIGHT_RED_ON : ANSI_HIGHLIGHT_OFF;
177 static const char *ansi_highlight_green(bool b) {
182 return b ? ANSI_HIGHLIGHT_GREEN_ON : ANSI_HIGHLIGHT_OFF;
185 static bool error_is_no_service(const DBusError *error) {
188 if (!dbus_error_is_set(error))
191 if (dbus_error_has_name(error, DBUS_ERROR_NAME_HAS_NO_OWNER))
194 if (dbus_error_has_name(error, DBUS_ERROR_SERVICE_UNKNOWN))
197 return startswith(error->name, "org.freedesktop.DBus.Error.Spawn.");
200 static int translate_bus_error_to_exit_status(int r, const DBusError *error) {
203 if (!dbus_error_is_set(error))
206 if (dbus_error_has_name(error, DBUS_ERROR_ACCESS_DENIED) ||
207 dbus_error_has_name(error, BUS_ERROR_ONLY_BY_DEPENDENCY) ||
208 dbus_error_has_name(error, BUS_ERROR_NO_ISOLATION) ||
209 dbus_error_has_name(error, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE))
210 return EXIT_NOPERMISSION;
212 if (dbus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT))
213 return EXIT_NOTINSTALLED;
215 if (dbus_error_has_name(error, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE) ||
216 dbus_error_has_name(error, BUS_ERROR_NOT_SUPPORTED))
217 return EXIT_NOTIMPLEMENTED;
219 if (dbus_error_has_name(error, BUS_ERROR_LOAD_FAILED))
220 return EXIT_NOTCONFIGURED;
228 static void warn_wall(enum action action) {
229 static const char *table[_ACTION_MAX] = {
230 [ACTION_HALT] = "The system is going down for system halt NOW!",
231 [ACTION_REBOOT] = "The system is going down for reboot NOW!",
232 [ACTION_POWEROFF] = "The system is going down for power-off NOW!",
233 [ACTION_KEXEC] = "The system is going down for kexec reboot NOW!",
234 [ACTION_RESCUE] = "The system is going down to rescue mode NOW!",
235 [ACTION_EMERGENCY] = "The system is going down to emergency mode NOW!"
244 if (!(p = strv_join(arg_wall, " "))) {
245 log_error("Failed to join strings.");
261 utmp_wall(table[action], NULL);
264 static bool avoid_bus(void) {
266 if (running_in_chroot() > 0)
269 if (sd_booted() <= 0)
272 if (!isempty(arg_root))
275 if (arg_scope == UNIT_FILE_GLOBAL)
283 const char *description;
284 const char *load_state;
285 const char *active_state;
286 const char *sub_state;
287 const char *following;
288 const char *unit_path;
290 const char *job_type;
291 const char *job_path;
294 static int compare_unit_info(const void *a, const void *b) {
296 const struct unit_info *u = a, *v = b;
298 d1 = strrchr(u->id, '.');
299 d2 = strrchr(v->id, '.');
304 if ((r = strcasecmp(d1, d2)) != 0)
308 return strcasecmp(u->id, v->id);
311 static bool output_show_unit(const struct unit_info *u) {
315 return streq(u->active_state, "failed");
317 return (!arg_type || ((dot = strrchr(u->id, '.')) &&
318 streq(dot+1, arg_type))) &&
319 (arg_all || !(streq(u->active_state, "inactive") || u->following[0]) || u->job_id > 0);
322 static void output_units_list(const struct unit_info *unit_infos, unsigned c) {
323 unsigned id_len, max_id_len, active_len, sub_len, job_len, desc_len, n_shown = 0;
324 const struct unit_info *u;
326 max_id_len = sizeof("UNIT")-1;
327 active_len = sizeof("ACTIVE")-1;
328 sub_len = sizeof("SUB")-1;
329 job_len = sizeof("JOB")-1;
332 for (u = unit_infos; u < unit_infos + c; u++) {
333 if (!output_show_unit(u))
336 max_id_len = MAX(max_id_len, strlen(u->id));
337 active_len = MAX(active_len, strlen(u->active_state));
338 sub_len = MAX(sub_len, strlen(u->sub_state));
340 job_len = MAX(job_len, strlen(u->job_type));
345 id_len = MIN(max_id_len, 25);
346 basic_len = 5 + id_len + 6 + active_len + sub_len + job_len;
347 if (basic_len < (unsigned) columns()) {
348 unsigned extra_len, incr;
349 extra_len = columns() - basic_len;
350 /* Either UNIT already got 25, or is fully satisfied.
351 * Grant up to 25 to DESC now. */
352 incr = MIN(extra_len, 25);
355 /* split the remaining space between UNIT and DESC,
356 * but do not give UNIT more than it needs. */
358 incr = MIN(extra_len / 2, max_id_len - id_len);
360 desc_len += extra_len - incr;
366 if (!arg_no_legend) {
367 printf("%-*s %-6s %-*s %-*s %-*s ", id_len, "UNIT", "LOAD",
368 active_len, "ACTIVE", sub_len, "SUB", job_len, "JOB");
369 if (!arg_full && arg_no_pager)
370 printf("%.*s\n", desc_len, "DESCRIPTION");
372 printf("%s\n", "DESCRIPTION");
375 for (u = unit_infos; u < unit_infos + c; u++) {
377 const char *on_loaded, *off_loaded;
378 const char *on_active, *off_active;
380 if (!output_show_unit(u))
385 if (streq(u->load_state, "error")) {
386 on_loaded = ansi_highlight_red(true);
387 off_loaded = ansi_highlight_red(false);
389 on_loaded = off_loaded = "";
391 if (streq(u->active_state, "failed")) {
392 on_active = ansi_highlight_red(true);
393 off_active = ansi_highlight_red(false);
395 on_active = off_active = "";
397 e = arg_full ? NULL : ellipsize(u->id, id_len, 33);
399 printf("%-*s %s%-6s%s %s%-*s %-*s%s %-*s ",
400 id_len, e ? e : u->id,
401 on_loaded, u->load_state, off_loaded,
402 on_active, active_len, u->active_state,
403 sub_len, u->sub_state, off_active,
404 job_len, u->job_id ? u->job_type : "");
405 if (!arg_full && arg_no_pager)
406 printf("%.*s\n", desc_len, u->description);
408 printf("%s\n", u->description);
413 if (!arg_no_legend) {
414 printf("\nLOAD = Reflects whether the unit definition was properly loaded.\n"
415 "ACTIVE = The high-level unit activation state, i.e. generalization of SUB.\n"
416 "SUB = The low-level unit activation state, values depend on unit type.\n"
417 "JOB = Pending job for the unit.\n");
420 printf("\n%u units listed.\n", n_shown);
422 printf("\n%u units listed. Pass --all to see inactive units, too.\n", n_shown);
426 static int list_units(DBusConnection *bus, char **args) {
427 DBusMessage *m = NULL, *reply = NULL;
430 DBusMessageIter iter, sub, sub2;
431 unsigned c = 0, n_units = 0;
432 struct unit_info *unit_infos = NULL;
434 dbus_error_init(&error);
438 pager_open_if_enabled();
440 if (!(m = dbus_message_new_method_call(
441 "org.freedesktop.systemd1",
442 "/org/freedesktop/systemd1",
443 "org.freedesktop.systemd1.Manager",
445 log_error("Could not allocate message.");
449 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
450 log_error("Failed to issue method call: %s", bus_error_message(&error));
455 if (!dbus_message_iter_init(reply, &iter) ||
456 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
457 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
458 log_error("Failed to parse reply.");
463 dbus_message_iter_recurse(&iter, &sub);
465 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
468 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
469 log_error("Failed to parse reply.");
477 n_units = MAX(2*c, 16);
478 w = realloc(unit_infos, sizeof(struct unit_info) * n_units);
481 log_error("Failed to allocate unit array.");
491 dbus_message_iter_recurse(&sub, &sub2);
493 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->id, true) < 0 ||
494 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->description, true) < 0 ||
495 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->load_state, true) < 0 ||
496 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->active_state, true) < 0 ||
497 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->sub_state, true) < 0 ||
498 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->following, true) < 0 ||
499 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &u->unit_path, true) < 0 ||
500 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &u->job_id, true) < 0 ||
501 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->job_type, true) < 0 ||
502 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &u->job_path, false) < 0) {
503 log_error("Failed to parse reply.");
508 dbus_message_iter_next(&sub);
513 qsort(unit_infos, c, sizeof(struct unit_info), compare_unit_info);
514 output_units_list(unit_infos, c);
521 dbus_message_unref(m);
524 dbus_message_unref(reply);
528 dbus_error_free(&error);
533 static int compare_unit_file_list(const void *a, const void *b) {
535 const UnitFileList *u = a, *v = b;
537 d1 = strrchr(u->path, '.');
538 d2 = strrchr(v->path, '.');
543 r = strcasecmp(d1, d2);
548 return strcasecmp(file_name_from_path(u->path), file_name_from_path(v->path));
551 static bool output_show_unit_file(const UnitFileList *u) {
554 return !arg_type || ((dot = strrchr(u->path, '.')) && streq(dot+1, arg_type));
557 static void output_unit_file_list(const UnitFileList *units, unsigned c) {
558 unsigned max_id_len, id_cols, state_cols, n_shown = 0;
559 const UnitFileList *u;
561 max_id_len = sizeof("UNIT FILE")-1;
562 state_cols = sizeof("STATE")-1;
563 for (u = units; u < units + c; u++) {
564 if (!output_show_unit_file(u))
567 max_id_len = MAX(max_id_len, strlen(file_name_from_path(u->path)));
568 state_cols = MAX(state_cols, strlen(unit_file_state_to_string(u->state)));
573 id_cols = MIN(max_id_len, 25);
574 basic_cols = 1 + id_cols + state_cols;
575 if (basic_cols < (unsigned) columns())
576 id_cols += MIN(columns() - basic_cols, max_id_len - id_cols);
578 id_cols = max_id_len;
581 printf("%-*s %-*s\n", id_cols, "UNIT FILE", state_cols, "STATE");
583 for (u = units; u < units + c; u++) {
585 const char *on, *off;
588 if (!output_show_unit_file(u))
593 if (u->state == UNIT_FILE_MASKED ||
594 u->state == UNIT_FILE_MASKED_RUNTIME ||
595 u->state == UNIT_FILE_DISABLED) {
596 on = ansi_highlight_red(true);
597 off = ansi_highlight_red(false);
598 } else if (u->state == UNIT_FILE_ENABLED) {
599 on = ansi_highlight_green(true);
600 off = ansi_highlight_green(false);
604 id = file_name_from_path(u->path);
606 e = arg_full ? NULL : ellipsize(id, id_cols, 33);
608 printf("%-*s %s%-*s%s\n",
610 on, state_cols, unit_file_state_to_string(u->state), off);
616 printf("\n%u unit files listed.\n", n_shown);
619 static int list_unit_files(DBusConnection *bus, char **args) {
620 DBusMessage *m = NULL, *reply = NULL;
623 DBusMessageIter iter, sub, sub2;
624 unsigned c = 0, n_units = 0;
625 UnitFileList *units = NULL;
627 dbus_error_init(&error);
629 pager_open_if_enabled();
636 h = hashmap_new(string_hash_func, string_compare_func);
638 log_error("Out of memory");
642 r = unit_file_get_list(arg_scope, arg_root, h);
644 unit_file_list_free(h);
645 log_error("Failed to get unit file list: %s", strerror(-r));
649 n_units = hashmap_size(h);
650 units = new(UnitFileList, n_units);
652 unit_file_list_free(h);
653 log_error("Out of memory");
657 HASHMAP_FOREACH(u, h, i) {
658 memcpy(units + c++, u, sizeof(UnitFileList));
666 m = dbus_message_new_method_call(
667 "org.freedesktop.systemd1",
668 "/org/freedesktop/systemd1",
669 "org.freedesktop.systemd1.Manager",
672 log_error("Could not allocate message.");
676 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
678 log_error("Failed to issue method call: %s", bus_error_message(&error));
683 if (!dbus_message_iter_init(reply, &iter) ||
684 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
685 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
686 log_error("Failed to parse reply.");
691 dbus_message_iter_recurse(&iter, &sub);
693 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
697 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
698 log_error("Failed to parse reply.");
706 n_units = MAX(2*c, 16);
707 w = realloc(units, sizeof(struct UnitFileList) * n_units);
710 log_error("Failed to allocate unit array.");
720 dbus_message_iter_recurse(&sub, &sub2);
722 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &u->path, true) < 0 ||
723 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, false) < 0) {
724 log_error("Failed to parse reply.");
729 u->state = unit_file_state_from_string(state);
731 dbus_message_iter_next(&sub);
737 qsort(units, c, sizeof(UnitFileList), compare_unit_file_list);
738 output_unit_file_list(units, c);
745 dbus_message_unref(m);
748 dbus_message_unref(reply);
752 dbus_error_free(&error);
757 static int dot_one_property(const char *name, const char *prop, DBusMessageIter *iter) {
758 static const char * const colors[] = {
759 "Requires", "[color=\"black\"]",
760 "RequiresOverridable", "[color=\"black\"]",
761 "Requisite", "[color=\"darkblue\"]",
762 "RequisiteOverridable", "[color=\"darkblue\"]",
763 "Wants", "[color=\"darkgrey\"]",
764 "Conflicts", "[color=\"red\"]",
765 "ConflictedBy", "[color=\"red\"]",
766 "After", "[color=\"green\"]"
769 const char *c = NULL;
776 for (i = 0; i < ELEMENTSOF(colors); i += 2)
777 if (streq(colors[i], prop)) {
785 if (arg_dot != DOT_ALL)
786 if ((arg_dot == DOT_ORDER) != streq(prop, "After"))
789 switch (dbus_message_iter_get_arg_type(iter)) {
791 case DBUS_TYPE_ARRAY:
793 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
796 dbus_message_iter_recurse(iter, &sub);
798 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
801 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
802 dbus_message_iter_get_basic(&sub, &s);
803 printf("\t\"%s\"->\"%s\" %s;\n", name, s, c);
805 dbus_message_iter_next(&sub);
815 static int dot_one(DBusConnection *bus, const char *name, const char *path) {
816 DBusMessage *m = NULL, *reply = NULL;
817 const char *interface = "org.freedesktop.systemd1.Unit";
820 DBusMessageIter iter, sub, sub2, sub3;
825 dbus_error_init(&error);
827 if (!(m = dbus_message_new_method_call(
828 "org.freedesktop.systemd1",
830 "org.freedesktop.DBus.Properties",
832 log_error("Could not allocate message.");
837 if (!dbus_message_append_args(m,
838 DBUS_TYPE_STRING, &interface,
839 DBUS_TYPE_INVALID)) {
840 log_error("Could not append arguments to message.");
845 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
846 log_error("Failed to issue method call: %s", bus_error_message(&error));
851 if (!dbus_message_iter_init(reply, &iter) ||
852 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
853 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
854 log_error("Failed to parse reply.");
859 dbus_message_iter_recurse(&iter, &sub);
861 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
864 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
865 log_error("Failed to parse reply.");
870 dbus_message_iter_recurse(&sub, &sub2);
872 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &prop, true) < 0) {
873 log_error("Failed to parse reply.");
878 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
879 log_error("Failed to parse reply.");
884 dbus_message_iter_recurse(&sub2, &sub3);
886 if (dot_one_property(name, prop, &sub3)) {
887 log_error("Failed to parse reply.");
892 dbus_message_iter_next(&sub);
899 dbus_message_unref(m);
902 dbus_message_unref(reply);
904 dbus_error_free(&error);
909 static int dot(DBusConnection *bus, char **args) {
910 DBusMessage *m = NULL, *reply = NULL;
913 DBusMessageIter iter, sub, sub2;
915 dbus_error_init(&error);
919 if (!(m = dbus_message_new_method_call(
920 "org.freedesktop.systemd1",
921 "/org/freedesktop/systemd1",
922 "org.freedesktop.systemd1.Manager",
924 log_error("Could not allocate message.");
928 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
929 log_error("Failed to issue method call: %s", bus_error_message(&error));
934 if (!dbus_message_iter_init(reply, &iter) ||
935 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
936 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
937 log_error("Failed to parse reply.");
942 printf("digraph systemd {\n");
944 dbus_message_iter_recurse(&iter, &sub);
945 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
946 const char *id, *description, *load_state, *active_state, *sub_state, *following, *unit_path;
948 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
949 log_error("Failed to parse reply.");
954 dbus_message_iter_recurse(&sub, &sub2);
956 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
957 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
958 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
959 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
960 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
961 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &following, true) < 0 ||
962 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, true) < 0) {
963 log_error("Failed to parse reply.");
968 if ((r = dot_one(bus, id, unit_path)) < 0)
971 /* printf("\t\"%s\";\n", id); */
972 dbus_message_iter_next(&sub);
977 log_info(" Color legend: black = Requires\n"
978 " dark blue = Requisite\n"
979 " dark grey = Wants\n"
984 log_notice("-- You probably want to process this output with graphviz' dot tool.\n"
985 "-- Try a shell pipeline like 'systemctl dot | dot -Tsvg > systemd.svg'!\n");
991 dbus_message_unref(m);
994 dbus_message_unref(reply);
996 dbus_error_free(&error);
1001 static int list_jobs(DBusConnection *bus, char **args) {
1002 DBusMessage *m = NULL, *reply = NULL;
1005 DBusMessageIter iter, sub, sub2;
1008 dbus_error_init(&error);
1012 pager_open_if_enabled();
1014 if (!(m = dbus_message_new_method_call(
1015 "org.freedesktop.systemd1",
1016 "/org/freedesktop/systemd1",
1017 "org.freedesktop.systemd1.Manager",
1019 log_error("Could not allocate message.");
1023 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1024 log_error("Failed to issue method call: %s", bus_error_message(&error));
1029 if (!dbus_message_iter_init(reply, &iter) ||
1030 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
1031 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
1032 log_error("Failed to parse reply.");
1037 dbus_message_iter_recurse(&iter, &sub);
1040 printf("%4s %-25s %-15s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
1042 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1043 const char *name, *type, *state, *job_path, *unit_path;
1047 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
1048 log_error("Failed to parse reply.");
1053 dbus_message_iter_recurse(&sub, &sub2);
1055 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
1056 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
1057 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
1058 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
1059 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
1060 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
1061 log_error("Failed to parse reply.");
1066 e = arg_full ? NULL : ellipsize(name, 25, 33);
1067 printf("%4u %-25s %-15s %-7s\n", id, e ? e : name, type, state);
1072 dbus_message_iter_next(&sub);
1076 printf("\n%u jobs listed.\n", k);
1082 dbus_message_unref(m);
1085 dbus_message_unref(reply);
1087 dbus_error_free(&error);
1092 static int load_unit(DBusConnection *bus, char **args) {
1093 DBusMessage *m = NULL;
1098 dbus_error_init(&error);
1103 STRV_FOREACH(name, args+1) {
1106 if (!(m = dbus_message_new_method_call(
1107 "org.freedesktop.systemd1",
1108 "/org/freedesktop/systemd1",
1109 "org.freedesktop.systemd1.Manager",
1111 log_error("Could not allocate message.");
1116 if (!dbus_message_append_args(m,
1117 DBUS_TYPE_STRING, name,
1118 DBUS_TYPE_INVALID)) {
1119 log_error("Could not append arguments to message.");
1124 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1125 log_error("Failed to issue method call: %s", bus_error_message(&error));
1130 dbus_message_unref(m);
1131 dbus_message_unref(reply);
1140 dbus_message_unref(m);
1142 dbus_error_free(&error);
1147 static int cancel_job(DBusConnection *bus, char **args) {
1148 DBusMessage *m = NULL, *reply = NULL;
1153 dbus_error_init(&error);
1158 if (strv_length(args) <= 1)
1159 return daemon_reload(bus, args);
1161 STRV_FOREACH(name, args+1) {
1165 if (!(m = dbus_message_new_method_call(
1166 "org.freedesktop.systemd1",
1167 "/org/freedesktop/systemd1",
1168 "org.freedesktop.systemd1.Manager",
1170 log_error("Could not allocate message.");
1175 if ((r = safe_atou(*name, &id)) < 0) {
1176 log_error("Failed to parse job id: %s", strerror(-r));
1180 assert_cc(sizeof(uint32_t) == sizeof(id));
1181 if (!dbus_message_append_args(m,
1182 DBUS_TYPE_UINT32, &id,
1183 DBUS_TYPE_INVALID)) {
1184 log_error("Could not append arguments to message.");
1189 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1190 log_error("Failed to issue method call: %s", bus_error_message(&error));
1195 if (!dbus_message_get_args(reply, &error,
1196 DBUS_TYPE_OBJECT_PATH, &path,
1197 DBUS_TYPE_INVALID)) {
1198 log_error("Failed to parse reply: %s", bus_error_message(&error));
1203 dbus_message_unref(m);
1204 if (!(m = dbus_message_new_method_call(
1205 "org.freedesktop.systemd1",
1207 "org.freedesktop.systemd1.Job",
1209 log_error("Could not allocate message.");
1214 dbus_message_unref(reply);
1215 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1216 log_error("Failed to issue method call: %s", bus_error_message(&error));
1221 dbus_message_unref(m);
1222 dbus_message_unref(reply);
1230 dbus_message_unref(m);
1233 dbus_message_unref(reply);
1235 dbus_error_free(&error);
1240 static bool need_daemon_reload(DBusConnection *bus, const char *unit) {
1241 DBusMessage *m = NULL, *reply = NULL;
1242 dbus_bool_t b = FALSE;
1243 DBusMessageIter iter, sub;
1245 *interface = "org.freedesktop.systemd1.Unit",
1246 *property = "NeedDaemonReload",
1249 /* We ignore all errors here, since this is used to show a warning only */
1251 if (!(m = dbus_message_new_method_call(
1252 "org.freedesktop.systemd1",
1253 "/org/freedesktop/systemd1",
1254 "org.freedesktop.systemd1.Manager",
1258 if (!dbus_message_append_args(m,
1259 DBUS_TYPE_STRING, &unit,
1263 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
1266 if (!dbus_message_get_args(reply, NULL,
1267 DBUS_TYPE_OBJECT_PATH, &path,
1271 dbus_message_unref(m);
1272 if (!(m = dbus_message_new_method_call(
1273 "org.freedesktop.systemd1",
1275 "org.freedesktop.DBus.Properties",
1279 if (!dbus_message_append_args(m,
1280 DBUS_TYPE_STRING, &interface,
1281 DBUS_TYPE_STRING, &property,
1282 DBUS_TYPE_INVALID)) {
1286 dbus_message_unref(reply);
1287 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
1290 if (!dbus_message_iter_init(reply, &iter) ||
1291 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
1294 dbus_message_iter_recurse(&iter, &sub);
1296 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_BOOLEAN)
1299 dbus_message_iter_get_basic(&sub, &b);
1303 dbus_message_unref(m);
1306 dbus_message_unref(reply);
1311 typedef struct WaitData {
1316 static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
1324 dbus_error_init(&error);
1326 log_debug("Got D-Bus request: %s.%s() on %s",
1327 dbus_message_get_interface(message),
1328 dbus_message_get_member(message),
1329 dbus_message_get_path(message));
1331 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
1332 log_error("Warning! D-Bus connection terminated.");
1333 dbus_connection_close(connection);
1335 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
1337 const char *path, *result;
1338 dbus_bool_t success = true;
1340 if (dbus_message_get_args(message, &error,
1341 DBUS_TYPE_UINT32, &id,
1342 DBUS_TYPE_OBJECT_PATH, &path,
1343 DBUS_TYPE_STRING, &result,
1344 DBUS_TYPE_INVALID)) {
1347 if ((p = set_remove(d->set, (char*) path)))
1351 d->result = strdup(result);
1356 dbus_error_free(&error);
1358 if (dbus_message_get_args(message, &error,
1359 DBUS_TYPE_UINT32, &id,
1360 DBUS_TYPE_OBJECT_PATH, &path,
1361 DBUS_TYPE_BOOLEAN, &success,
1362 DBUS_TYPE_INVALID)) {
1365 /* Compatibility with older systemd versions <
1366 * 19 during upgrades. This should be dropped
1369 if ((p = set_remove(d->set, (char*) path)))
1373 d->result = strdup("failed");
1379 log_error("Failed to parse message: %s", bus_error_message(&error));
1383 dbus_error_free(&error);
1384 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1387 static int enable_wait_for_jobs(DBusConnection *bus) {
1395 dbus_error_init(&error);
1396 dbus_bus_add_match(bus,
1398 "sender='org.freedesktop.systemd1',"
1399 "interface='org.freedesktop.systemd1.Manager',"
1400 "member='JobRemoved',"
1401 "path='/org/freedesktop/systemd1'",
1404 if (dbus_error_is_set(&error)) {
1405 log_error("Failed to add match: %s", bus_error_message(&error));
1406 dbus_error_free(&error);
1410 /* This is slightly dirty, since we don't undo the match registrations. */
1414 static int wait_for_jobs(DBusConnection *bus, Set *s) {
1424 if (!dbus_connection_add_filter(bus, wait_filter, &d, NULL)) {
1425 log_error("Failed to add filter.");
1430 while (!set_isempty(s) &&
1431 dbus_connection_read_write_dispatch(bus, -1))
1434 if (!arg_quiet && d.result) {
1435 if (streq(d.result, "timeout"))
1436 log_error("Job timed out.");
1437 else if (streq(d.result, "canceled"))
1438 log_error("Job canceled.");
1439 else if (streq(d.result, "dependency"))
1440 log_error("A dependency job failed. See system journal for details.");
1441 else if (!streq(d.result, "done") && !streq(d.result, "skipped"))
1442 log_error("Job failed. See system journal and 'systemctl status' for details.");
1445 if (streq_ptr(d.result, "timeout"))
1447 else if (streq_ptr(d.result, "canceled"))
1449 else if (!streq_ptr(d.result, "done") && !streq_ptr(d.result, "skipped"))
1457 /* This is slightly dirty, since we don't undo the filter registration. */
1462 static int start_unit_one(
1463 DBusConnection *bus,
1470 DBusMessage *m = NULL, *reply = NULL;
1479 assert(arg_no_block || s);
1481 if (!(m = dbus_message_new_method_call(
1482 "org.freedesktop.systemd1",
1483 "/org/freedesktop/systemd1",
1484 "org.freedesktop.systemd1.Manager",
1486 log_error("Could not allocate message.");
1491 if (!dbus_message_append_args(m,
1492 DBUS_TYPE_STRING, &name,
1493 DBUS_TYPE_STRING, &mode,
1494 DBUS_TYPE_INVALID)) {
1495 log_error("Could not append arguments to message.");
1500 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, error))) {
1502 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(error)) {
1503 /* There's always a fallback possible for
1504 * legacy actions. */
1509 log_error("Failed to issue method call: %s", bus_error_message(error));
1514 if (!dbus_message_get_args(reply, error,
1515 DBUS_TYPE_OBJECT_PATH, &path,
1516 DBUS_TYPE_INVALID)) {
1517 log_error("Failed to parse reply: %s", bus_error_message(error));
1522 if (need_daemon_reload(bus, name))
1523 log_warning("Warning: Unit file of created job changed on disk, 'systemctl %s daemon-reload' recommended.",
1524 arg_scope == UNIT_FILE_SYSTEM ? "--system" : "--user");
1526 if (!arg_no_block) {
1529 if (!(p = strdup(path))) {
1530 log_error("Failed to duplicate path.");
1535 if ((r = set_put(s, p)) < 0) {
1537 log_error("Failed to add path to set.");
1546 dbus_message_unref(m);
1549 dbus_message_unref(reply);
1554 static enum action verb_to_action(const char *verb) {
1555 if (streq(verb, "halt"))
1557 else if (streq(verb, "poweroff"))
1558 return ACTION_POWEROFF;
1559 else if (streq(verb, "reboot"))
1560 return ACTION_REBOOT;
1561 else if (streq(verb, "kexec"))
1562 return ACTION_KEXEC;
1563 else if (streq(verb, "rescue"))
1564 return ACTION_RESCUE;
1565 else if (streq(verb, "emergency"))
1566 return ACTION_EMERGENCY;
1567 else if (streq(verb, "default"))
1568 return ACTION_DEFAULT;
1569 else if (streq(verb, "exit"))
1572 return ACTION_INVALID;
1575 static int start_unit(DBusConnection *bus, char **args) {
1577 static const char * const table[_ACTION_MAX] = {
1578 [ACTION_HALT] = SPECIAL_HALT_TARGET,
1579 [ACTION_POWEROFF] = SPECIAL_POWEROFF_TARGET,
1580 [ACTION_REBOOT] = SPECIAL_REBOOT_TARGET,
1581 [ACTION_KEXEC] = SPECIAL_KEXEC_TARGET,
1582 [ACTION_RUNLEVEL2] = SPECIAL_RUNLEVEL2_TARGET,
1583 [ACTION_RUNLEVEL3] = SPECIAL_RUNLEVEL3_TARGET,
1584 [ACTION_RUNLEVEL4] = SPECIAL_RUNLEVEL4_TARGET,
1585 [ACTION_RUNLEVEL5] = SPECIAL_RUNLEVEL5_TARGET,
1586 [ACTION_RESCUE] = SPECIAL_RESCUE_TARGET,
1587 [ACTION_EMERGENCY] = SPECIAL_EMERGENCY_TARGET,
1588 [ACTION_DEFAULT] = SPECIAL_DEFAULT_TARGET,
1589 [ACTION_EXIT] = SPECIAL_EXIT_TARGET
1593 const char *method, *mode, *one_name;
1598 dbus_error_init(&error);
1602 agent_open_if_enabled();
1604 if (arg_action == ACTION_SYSTEMCTL) {
1606 streq(args[0], "stop") ||
1607 streq(args[0], "condstop") ? "StopUnit" :
1608 streq(args[0], "reload") ? "ReloadUnit" :
1609 streq(args[0], "restart") ? "RestartUnit" :
1611 streq(args[0], "try-restart") ||
1612 streq(args[0], "condrestart") ? "TryRestartUnit" :
1614 streq(args[0], "reload-or-restart") ? "ReloadOrRestartUnit" :
1616 streq(args[0], "reload-or-try-restart") ||
1617 streq(args[0], "condreload") ||
1619 streq(args[0], "force-reload") ? "ReloadOrTryRestartUnit" :
1623 (streq(args[0], "isolate") ||
1624 streq(args[0], "rescue") ||
1625 streq(args[0], "emergency")) ? "isolate" : arg_job_mode;
1627 one_name = table[verb_to_action(args[0])];
1630 assert(arg_action < ELEMENTSOF(table));
1631 assert(table[arg_action]);
1633 method = "StartUnit";
1635 mode = (arg_action == ACTION_EMERGENCY ||
1636 arg_action == ACTION_RESCUE ||
1637 arg_action == ACTION_RUNLEVEL2 ||
1638 arg_action == ACTION_RUNLEVEL3 ||
1639 arg_action == ACTION_RUNLEVEL4 ||
1640 arg_action == ACTION_RUNLEVEL5) ? "isolate" : "replace";
1642 one_name = table[arg_action];
1645 if (!arg_no_block) {
1646 if ((ret = enable_wait_for_jobs(bus)) < 0) {
1647 log_error("Could not watch jobs: %s", strerror(-ret));
1651 if (!(s = set_new(string_hash_func, string_compare_func))) {
1652 log_error("Failed to allocate set.");
1659 if ((ret = start_unit_one(bus, method, one_name, mode, &error, s)) <= 0)
1662 STRV_FOREACH(name, args+1)
1663 if ((r = start_unit_one(bus, method, *name, mode, &error, s)) != 0) {
1664 ret = translate_bus_error_to_exit_status(r, &error);
1665 dbus_error_free(&error);
1670 if ((r = wait_for_jobs(bus, s)) < 0) {
1679 dbus_error_free(&error);
1684 static int start_special(DBusConnection *bus, char **args) {
1691 (streq(args[0], "halt") ||
1692 streq(args[0], "poweroff") ||
1693 streq(args[0], "reboot") ||
1694 streq(args[0], "kexec") ||
1695 streq(args[0], "exit")))
1696 return daemon_reload(bus, args);
1698 r = start_unit(bus, args);
1701 warn_wall(verb_to_action(args[0]));
1706 static int check_unit(DBusConnection *bus, char **args) {
1707 DBusMessage *m = NULL, *reply = NULL;
1709 *interface = "org.freedesktop.systemd1.Unit",
1710 *property = "ActiveState";
1711 int r = 3; /* According to LSB: "program is not running" */
1718 dbus_error_init(&error);
1720 STRV_FOREACH(name, args+1) {
1721 const char *path = NULL;
1723 DBusMessageIter iter, sub;
1725 if (!(m = dbus_message_new_method_call(
1726 "org.freedesktop.systemd1",
1727 "/org/freedesktop/systemd1",
1728 "org.freedesktop.systemd1.Manager",
1730 log_error("Could not allocate message.");
1735 if (!dbus_message_append_args(m,
1736 DBUS_TYPE_STRING, name,
1737 DBUS_TYPE_INVALID)) {
1738 log_error("Could not append arguments to message.");
1743 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1745 /* Hmm, cannot figure out anything about this unit... */
1749 dbus_error_free(&error);
1750 dbus_message_unref(m);
1755 if (!dbus_message_get_args(reply, &error,
1756 DBUS_TYPE_OBJECT_PATH, &path,
1757 DBUS_TYPE_INVALID)) {
1758 log_error("Failed to parse reply: %s", bus_error_message(&error));
1763 dbus_message_unref(m);
1764 if (!(m = dbus_message_new_method_call(
1765 "org.freedesktop.systemd1",
1767 "org.freedesktop.DBus.Properties",
1769 log_error("Could not allocate message.");
1774 if (!dbus_message_append_args(m,
1775 DBUS_TYPE_STRING, &interface,
1776 DBUS_TYPE_STRING, &property,
1777 DBUS_TYPE_INVALID)) {
1778 log_error("Could not append arguments to message.");
1783 dbus_message_unref(reply);
1784 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1785 log_error("Failed to issue method call: %s", bus_error_message(&error));
1790 if (!dbus_message_iter_init(reply, &iter) ||
1791 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1792 log_error("Failed to parse reply.");
1797 dbus_message_iter_recurse(&iter, &sub);
1799 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1800 log_error("Failed to parse reply.");
1805 dbus_message_iter_get_basic(&sub, &state);
1810 if (streq(state, "active") || streq(state, "reloading"))
1813 dbus_message_unref(m);
1814 dbus_message_unref(reply);
1820 dbus_message_unref(m);
1823 dbus_message_unref(reply);
1825 dbus_error_free(&error);
1830 static int kill_unit(DBusConnection *bus, char **args) {
1831 DBusMessage *m = NULL;
1839 dbus_error_init(&error);
1842 arg_kill_who = "all";
1845 arg_kill_mode = streq(arg_kill_who, "all") ? "control-group" : "process";
1847 STRV_FOREACH(name, args+1) {
1850 if (!(m = dbus_message_new_method_call(
1851 "org.freedesktop.systemd1",
1852 "/org/freedesktop/systemd1",
1853 "org.freedesktop.systemd1.Manager",
1855 log_error("Could not allocate message.");
1860 if (!dbus_message_append_args(m,
1861 DBUS_TYPE_STRING, name,
1862 DBUS_TYPE_STRING, &arg_kill_who,
1863 DBUS_TYPE_STRING, &arg_kill_mode,
1864 DBUS_TYPE_INT32, &arg_signal,
1865 DBUS_TYPE_INVALID)) {
1866 log_error("Could not append arguments to message.");
1871 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1872 log_error("Failed to issue method call: %s", bus_error_message(&error));
1873 dbus_error_free(&error);
1877 dbus_message_unref(m);
1880 dbus_message_unref(reply);
1886 dbus_message_unref(m);
1888 dbus_error_free(&error);
1893 typedef struct ExecStatusInfo {
1901 usec_t start_timestamp;
1902 usec_t exit_timestamp;
1907 LIST_FIELDS(struct ExecStatusInfo, exec);
1910 static void exec_status_info_free(ExecStatusInfo *i) {
1919 static int exec_status_info_deserialize(DBusMessageIter *sub, ExecStatusInfo *i) {
1920 uint64_t start_timestamp, exit_timestamp, start_timestamp_monotonic, exit_timestamp_monotonic;
1921 DBusMessageIter sub2, sub3;
1925 int32_t code, status;
1931 if (dbus_message_iter_get_arg_type(sub) != DBUS_TYPE_STRUCT)
1934 dbus_message_iter_recurse(sub, &sub2);
1936 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
1939 if (!(i->path = strdup(path)))
1942 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
1943 dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
1947 dbus_message_iter_recurse(&sub2, &sub3);
1948 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1949 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1950 dbus_message_iter_next(&sub3);
1955 if (!(i->argv = new0(char*, n+1)))
1959 dbus_message_iter_recurse(&sub2, &sub3);
1960 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1963 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1964 dbus_message_iter_get_basic(&sub3, &s);
1965 dbus_message_iter_next(&sub3);
1967 if (!(i->argv[n++] = strdup(s)))
1971 if (!dbus_message_iter_next(&sub2) ||
1972 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, true) < 0 ||
1973 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp, true) < 0 ||
1974 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp_monotonic, true) < 0 ||
1975 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp, true) < 0 ||
1976 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp_monotonic, true) < 0 ||
1977 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) < 0 ||
1978 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) < 0 ||
1979 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) < 0)
1983 i->start_timestamp = (usec_t) start_timestamp;
1984 i->exit_timestamp = (usec_t) exit_timestamp;
1985 i->pid = (pid_t) pid;
1992 typedef struct UnitStatusInfo {
1994 const char *load_state;
1995 const char *active_state;
1996 const char *sub_state;
1997 const char *unit_file_state;
1999 const char *description;
2000 const char *following;
2003 const char *default_control_group;
2005 const char *load_error;
2008 usec_t inactive_exit_timestamp;
2009 usec_t inactive_exit_timestamp_monotonic;
2010 usec_t active_enter_timestamp;
2011 usec_t active_exit_timestamp;
2012 usec_t inactive_enter_timestamp;
2014 bool need_daemon_reload;
2019 const char *status_text;
2021 #ifdef HAVE_SYSV_COMPAT
2025 usec_t start_timestamp;
2026 usec_t exit_timestamp;
2028 int exit_code, exit_status;
2030 usec_t condition_timestamp;
2031 bool condition_result;
2034 unsigned n_accepted;
2035 unsigned n_connections;
2039 const char *sysfs_path;
2041 /* Mount, Automount */
2047 LIST_HEAD(ExecStatusInfo, exec);
2050 static void print_status_info(UnitStatusInfo *i) {
2052 const char *on, *off, *ss;
2054 char since1[FORMAT_TIMESTAMP_PRETTY_MAX], *s1;
2055 char since2[FORMAT_TIMESTAMP_MAX], *s2;
2059 /* This shows pretty information about a unit. See
2060 * print_property() for a low-level property printer */
2062 printf("%s", strna(i->id));
2064 if (i->description && !streq_ptr(i->id, i->description))
2065 printf(" - %s", i->description);
2070 printf("\t Follow: unit currently follows state of %s\n", i->following);
2072 if (streq_ptr(i->load_state, "error")) {
2073 on = ansi_highlight_red(true);
2074 off = ansi_highlight_red(false);
2079 printf("\t Loaded: %s%s%s (Reason: %s)\n", on, strna(i->load_state), off, i->load_error);
2080 else if (i->path && i->unit_file_state)
2081 printf("\t Loaded: %s%s%s (%s; %s)\n", on, strna(i->load_state), off, i->path, i->unit_file_state);
2083 printf("\t Loaded: %s%s%s (%s)\n", on, strna(i->load_state), off, i->path);
2085 printf("\t Loaded: %s%s%s\n", on, strna(i->load_state), off);
2087 ss = streq_ptr(i->active_state, i->sub_state) ? NULL : i->sub_state;
2089 if (streq_ptr(i->active_state, "failed")) {
2090 on = ansi_highlight_red(true);
2091 off = ansi_highlight_red(false);
2092 } else if (streq_ptr(i->active_state, "active") || streq_ptr(i->active_state, "reloading")) {
2093 on = ansi_highlight_green(true);
2094 off = ansi_highlight_green(false);
2099 printf("\t Active: %s%s (%s)%s",
2101 strna(i->active_state),
2105 printf("\t Active: %s%s%s",
2107 strna(i->active_state),
2110 if (!isempty(i->result) && !streq(i->result, "success"))
2111 printf(" (Result: %s)", i->result);
2113 timestamp = (streq_ptr(i->active_state, "active") ||
2114 streq_ptr(i->active_state, "reloading")) ? i->active_enter_timestamp :
2115 (streq_ptr(i->active_state, "inactive") ||
2116 streq_ptr(i->active_state, "failed")) ? i->inactive_enter_timestamp :
2117 streq_ptr(i->active_state, "activating") ? i->inactive_exit_timestamp :
2118 i->active_exit_timestamp;
2120 s1 = format_timestamp_pretty(since1, sizeof(since1), timestamp);
2121 s2 = format_timestamp(since2, sizeof(since2), timestamp);
2124 printf(" since %s; %s\n", s2, s1);
2126 printf(" since %s\n", s2);
2130 if (!i->condition_result && i->condition_timestamp > 0) {
2131 s1 = format_timestamp_pretty(since1, sizeof(since1), i->condition_timestamp);
2132 s2 = format_timestamp(since2, sizeof(since2), i->condition_timestamp);
2135 printf("\t start condition failed at %s; %s\n", s2, s1);
2137 printf("\t start condition failed at %s\n", s2);
2141 printf("\t Device: %s\n", i->sysfs_path);
2143 printf("\t Where: %s\n", i->where);
2145 printf("\t What: %s\n", i->what);
2148 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
2150 LIST_FOREACH(exec, p, i->exec) {
2154 /* Only show exited processes here */
2158 t = strv_join(p->argv, " ");
2159 printf("\t Process: %u %s=%s ", p->pid, p->name, strna(t));
2162 #ifdef HAVE_SYSV_COMPAT
2164 good = is_clean_exit_lsb(p->code, p->status);
2167 good = is_clean_exit(p->code, p->status);
2170 on = ansi_highlight_red(true);
2171 off = ansi_highlight_red(false);
2175 printf("%s(code=%s, ", on, sigchld_code_to_string(p->code));
2177 if (p->code == CLD_EXITED) {
2180 printf("status=%i", p->status);
2182 #ifdef HAVE_SYSV_COMPAT
2183 if ((c = exit_status_to_string(p->status, i->is_sysv ? EXIT_STATUS_LSB : EXIT_STATUS_SYSTEMD)))
2185 if ((c = exit_status_to_string(p->status, EXIT_STATUS_SYSTEMD)))
2190 printf("signal=%s", signal_to_string(p->status));
2192 printf(")%s\n", off);
2194 if (i->main_pid == p->pid &&
2195 i->start_timestamp == p->start_timestamp &&
2196 i->exit_timestamp == p->start_timestamp)
2197 /* Let's not show this twice */
2200 if (p->pid == i->control_pid)
2204 if (i->main_pid > 0 || i->control_pid > 0) {
2207 if (i->main_pid > 0) {
2208 printf("Main PID: %u", (unsigned) i->main_pid);
2212 get_process_comm(i->main_pid, &t);
2217 } else if (i->exit_code > 0) {
2218 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
2220 if (i->exit_code == CLD_EXITED) {
2223 printf("status=%i", i->exit_status);
2225 #ifdef HAVE_SYSV_COMPAT
2226 if ((c = exit_status_to_string(i->exit_status, i->is_sysv ? EXIT_STATUS_LSB : EXIT_STATUS_SYSTEMD)))
2228 if ((c = exit_status_to_string(i->exit_status, EXIT_STATUS_SYSTEMD)))
2233 printf("signal=%s", signal_to_string(i->exit_status));
2238 if (i->main_pid > 0 && i->control_pid > 0)
2241 if (i->control_pid > 0) {
2244 printf(" Control: %u", (unsigned) i->control_pid);
2246 get_process_comm(i->control_pid, &t);
2257 printf("\t Status: \"%s\"\n", i->status_text);
2259 if (i->default_control_group) {
2262 printf("\t CGroup: %s\n", i->default_control_group);
2264 if (arg_transport != TRANSPORT_SSH) {
2265 if ((c = columns()) > 18)
2270 show_cgroup_by_path(i->default_control_group, "\t\t ", c, false);
2274 if (i->id && arg_transport != TRANSPORT_SSH) {
2276 show_journal_by_unit(i->id, arg_output, NULL, 0, i->inactive_exit_timestamp_monotonic, arg_lines, arg_all, arg_follow);
2279 if (i->need_daemon_reload)
2280 printf("\n%sWarning:%s Unit file changed on disk, 'systemctl %s daemon-reload' recommended.\n",
2281 ansi_highlight_red(true),
2282 ansi_highlight_red(false),
2283 arg_scope == UNIT_FILE_SYSTEM ? "--system" : "--user");
2286 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
2292 switch (dbus_message_iter_get_arg_type(iter)) {
2294 case DBUS_TYPE_STRING: {
2297 dbus_message_iter_get_basic(iter, &s);
2300 if (streq(name, "Id"))
2302 else if (streq(name, "LoadState"))
2304 else if (streq(name, "ActiveState"))
2305 i->active_state = s;
2306 else if (streq(name, "SubState"))
2308 else if (streq(name, "Description"))
2310 else if (streq(name, "FragmentPath"))
2312 #ifdef HAVE_SYSV_COMPAT
2313 else if (streq(name, "SysVPath")) {
2318 else if (streq(name, "DefaultControlGroup"))
2319 i->default_control_group = s;
2320 else if (streq(name, "StatusText"))
2322 else if (streq(name, "SysFSPath"))
2324 else if (streq(name, "Where"))
2326 else if (streq(name, "What"))
2328 else if (streq(name, "Following"))
2330 else if (streq(name, "UnitFileState"))
2331 i->unit_file_state = s;
2332 else if (streq(name, "Result"))
2339 case DBUS_TYPE_BOOLEAN: {
2342 dbus_message_iter_get_basic(iter, &b);
2344 if (streq(name, "Accept"))
2346 else if (streq(name, "NeedDaemonReload"))
2347 i->need_daemon_reload = b;
2348 else if (streq(name, "ConditionResult"))
2349 i->condition_result = b;
2354 case DBUS_TYPE_UINT32: {
2357 dbus_message_iter_get_basic(iter, &u);
2359 if (streq(name, "MainPID")) {
2361 i->main_pid = (pid_t) u;
2364 } else if (streq(name, "ControlPID"))
2365 i->control_pid = (pid_t) u;
2366 else if (streq(name, "ExecMainPID")) {
2368 i->main_pid = (pid_t) u;
2369 } else if (streq(name, "NAccepted"))
2371 else if (streq(name, "NConnections"))
2372 i->n_connections = u;
2377 case DBUS_TYPE_INT32: {
2380 dbus_message_iter_get_basic(iter, &j);
2382 if (streq(name, "ExecMainCode"))
2383 i->exit_code = (int) j;
2384 else if (streq(name, "ExecMainStatus"))
2385 i->exit_status = (int) j;
2390 case DBUS_TYPE_UINT64: {
2393 dbus_message_iter_get_basic(iter, &u);
2395 if (streq(name, "ExecMainStartTimestamp"))
2396 i->start_timestamp = (usec_t) u;
2397 else if (streq(name, "ExecMainExitTimestamp"))
2398 i->exit_timestamp = (usec_t) u;
2399 else if (streq(name, "ActiveEnterTimestamp"))
2400 i->active_enter_timestamp = (usec_t) u;
2401 else if (streq(name, "InactiveEnterTimestamp"))
2402 i->inactive_enter_timestamp = (usec_t) u;
2403 else if (streq(name, "InactiveExitTimestamp"))
2404 i->inactive_exit_timestamp = (usec_t) u;
2405 else if (streq(name, "InactiveExitTimestampMonotonic"))
2406 i->inactive_exit_timestamp_monotonic = (usec_t) u;
2407 else if (streq(name, "ActiveExitTimestamp"))
2408 i->active_exit_timestamp = (usec_t) u;
2409 else if (streq(name, "ConditionTimestamp"))
2410 i->condition_timestamp = (usec_t) u;
2415 case DBUS_TYPE_ARRAY: {
2417 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT &&
2418 startswith(name, "Exec")) {
2419 DBusMessageIter sub;
2421 dbus_message_iter_recurse(iter, &sub);
2422 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2423 ExecStatusInfo *info;
2426 if (!(info = new0(ExecStatusInfo, 1)))
2429 if (!(info->name = strdup(name))) {
2434 if ((r = exec_status_info_deserialize(&sub, info)) < 0) {
2439 LIST_PREPEND(ExecStatusInfo, exec, i->exec, info);
2441 dbus_message_iter_next(&sub);
2448 case DBUS_TYPE_STRUCT: {
2450 if (streq(name, "LoadError")) {
2451 DBusMessageIter sub;
2452 const char *n, *message;
2455 dbus_message_iter_recurse(iter, &sub);
2457 r = bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &n, true);
2461 r = bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &message, false);
2465 if (!isempty(message))
2466 i->load_error = message;
2476 static int print_property(const char *name, DBusMessageIter *iter) {
2480 /* This is a low-level property printer, see
2481 * print_status_info() for the nicer output */
2483 if (arg_property && !strv_find(arg_property, name))
2486 switch (dbus_message_iter_get_arg_type(iter)) {
2488 case DBUS_TYPE_STRUCT: {
2489 DBusMessageIter sub;
2490 dbus_message_iter_recurse(iter, &sub);
2492 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
2495 dbus_message_iter_get_basic(&sub, &u);
2498 printf("%s=%u\n", name, (unsigned) u);
2500 printf("%s=\n", name);
2503 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
2506 dbus_message_iter_get_basic(&sub, &s);
2508 if (arg_all || s[0])
2509 printf("%s=%s\n", name, s);
2512 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "LoadError")) {
2513 const char *a = NULL, *b = NULL;
2515 if (bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &a, true) >= 0)
2516 bus_iter_get_basic_and_next(&sub, DBUS_TYPE_STRING, &b, false);
2518 if (arg_all || !isempty(a) || !isempty(b))
2519 printf("%s=%s \"%s\"\n", name, strempty(a), strempty(b));
2527 case DBUS_TYPE_ARRAY:
2529 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "EnvironmentFiles")) {
2530 DBusMessageIter sub, sub2;
2532 dbus_message_iter_recurse(iter, &sub);
2533 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2537 dbus_message_iter_recurse(&sub, &sub2);
2539 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) >= 0 &&
2540 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, false) >= 0)
2541 printf("EnvironmentFile=%s (ignore_errors=%s)\n", path, yes_no(ignore));
2543 dbus_message_iter_next(&sub);
2548 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
2549 DBusMessageIter sub, sub2;
2551 dbus_message_iter_recurse(iter, &sub);
2552 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2553 const char *type, *path;
2555 dbus_message_iter_recurse(&sub, &sub2);
2557 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
2558 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
2559 printf("%s=%s\n", type, path);
2561 dbus_message_iter_next(&sub);
2566 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
2567 DBusMessageIter sub, sub2;
2569 dbus_message_iter_recurse(iter, &sub);
2570 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2572 uint64_t value, next_elapse;
2574 dbus_message_iter_recurse(&sub, &sub2);
2576 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
2577 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
2578 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
2579 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
2581 printf("%s={ value=%s ; next_elapse=%s }\n",
2583 format_timespan(timespan1, sizeof(timespan1), value),
2584 format_timespan(timespan2, sizeof(timespan2), next_elapse));
2587 dbus_message_iter_next(&sub);
2592 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "ControlGroupAttributes")) {
2593 DBusMessageIter sub, sub2;
2595 dbus_message_iter_recurse(iter, &sub);
2596 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2597 const char *controller, *attr, *value;
2599 dbus_message_iter_recurse(&sub, &sub2);
2601 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &controller, true) >= 0 &&
2602 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &attr, true) >= 0 &&
2603 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &value, false) >= 0) {
2605 printf("ControlGroupAttribute={ controller=%s ; attribute=%s ; value=\"%s\" }\n",
2611 dbus_message_iter_next(&sub);
2616 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
2617 DBusMessageIter sub;
2619 dbus_message_iter_recurse(iter, &sub);
2620 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
2621 ExecStatusInfo info;
2624 if (exec_status_info_deserialize(&sub, &info) >= 0) {
2625 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
2628 t = strv_join(info.argv, " ");
2630 printf("%s={ path=%s ; argv[]=%s ; ignore_errors=%s ; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n",
2634 yes_no(info.ignore),
2635 strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)),
2636 strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)),
2637 (unsigned) info. pid,
2638 sigchld_code_to_string(info.code),
2640 info.code == CLD_EXITED ? "" : "/",
2641 strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status)));
2647 strv_free(info.argv);
2649 dbus_message_iter_next(&sub);
2658 if (generic_print_property(name, iter, arg_all) > 0)
2662 printf("%s=[unprintable]\n", name);
2667 static int show_one(const char *verb, DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
2668 DBusMessage *m = NULL, *reply = NULL;
2669 const char *interface = "";
2672 DBusMessageIter iter, sub, sub2, sub3;
2673 UnitStatusInfo info;
2681 dbus_error_init(&error);
2683 if (!(m = dbus_message_new_method_call(
2684 "org.freedesktop.systemd1",
2686 "org.freedesktop.DBus.Properties",
2688 log_error("Could not allocate message.");
2693 if (!dbus_message_append_args(m,
2694 DBUS_TYPE_STRING, &interface,
2695 DBUS_TYPE_INVALID)) {
2696 log_error("Could not append arguments to message.");
2701 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2702 log_error("Failed to issue method call: %s", bus_error_message(&error));
2707 if (!dbus_message_iter_init(reply, &iter) ||
2708 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
2709 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
2710 log_error("Failed to parse reply.");
2715 dbus_message_iter_recurse(&iter, &sub);
2722 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2725 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
2726 log_error("Failed to parse reply.");
2731 dbus_message_iter_recurse(&sub, &sub2);
2733 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
2734 log_error("Failed to parse reply.");
2739 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
2740 log_error("Failed to parse reply.");
2745 dbus_message_iter_recurse(&sub2, &sub3);
2747 if (show_properties)
2748 r = print_property(name, &sub3);
2750 r = status_property(name, &sub3, &info);
2753 log_error("Failed to parse reply.");
2758 dbus_message_iter_next(&sub);
2763 if (!show_properties)
2764 print_status_info(&info);
2766 if (!streq_ptr(info.active_state, "active") &&
2767 !streq_ptr(info.active_state, "reloading") &&
2768 streq(verb, "status"))
2769 /* According to LSB: "program not running" */
2772 while ((p = info.exec)) {
2773 LIST_REMOVE(ExecStatusInfo, exec, info.exec, p);
2774 exec_status_info_free(p);
2779 dbus_message_unref(m);
2782 dbus_message_unref(reply);
2784 dbus_error_free(&error);
2789 static int show(DBusConnection *bus, char **args) {
2790 DBusMessage *m = NULL, *reply = NULL;
2793 bool show_properties, new_line = false;
2799 dbus_error_init(&error);
2801 show_properties = !streq(args[0], "status");
2803 if (show_properties)
2804 pager_open_if_enabled();
2806 if (show_properties && strv_length(args) <= 1) {
2807 /* If not argument is specified inspect the manager
2810 ret = show_one(args[0], bus, "/org/freedesktop/systemd1", show_properties, &new_line);
2814 STRV_FOREACH(name, args+1) {
2815 const char *path = NULL;
2818 if (safe_atou32(*name, &id) < 0) {
2820 /* Interpret as unit name */
2822 if (!(m = dbus_message_new_method_call(
2823 "org.freedesktop.systemd1",
2824 "/org/freedesktop/systemd1",
2825 "org.freedesktop.systemd1.Manager",
2827 log_error("Could not allocate message.");
2832 if (!dbus_message_append_args(m,
2833 DBUS_TYPE_STRING, name,
2834 DBUS_TYPE_INVALID)) {
2835 log_error("Could not append arguments to message.");
2840 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2842 if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
2843 log_error("Failed to issue method call: %s", bus_error_message(&error));
2848 dbus_error_free(&error);
2850 dbus_message_unref(m);
2851 if (!(m = dbus_message_new_method_call(
2852 "org.freedesktop.systemd1",
2853 "/org/freedesktop/systemd1",
2854 "org.freedesktop.systemd1.Manager",
2856 log_error("Could not allocate message.");
2861 if (!dbus_message_append_args(m,
2862 DBUS_TYPE_STRING, name,
2863 DBUS_TYPE_INVALID)) {
2864 log_error("Could not append arguments to message.");
2869 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2870 log_error("Failed to issue method call: %s", bus_error_message(&error));
2872 if (dbus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT))
2873 ret = 4; /* According to LSB: "program or service status is unknown" */
2880 } else if (show_properties) {
2882 /* Interpret as job id */
2884 if (!(m = dbus_message_new_method_call(
2885 "org.freedesktop.systemd1",
2886 "/org/freedesktop/systemd1",
2887 "org.freedesktop.systemd1.Manager",
2889 log_error("Could not allocate message.");
2894 if (!dbus_message_append_args(m,
2895 DBUS_TYPE_UINT32, &id,
2896 DBUS_TYPE_INVALID)) {
2897 log_error("Could not append arguments to message.");
2902 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2903 log_error("Failed to issue method call: %s", bus_error_message(&error));
2909 /* Interpret as PID */
2911 if (!(m = dbus_message_new_method_call(
2912 "org.freedesktop.systemd1",
2913 "/org/freedesktop/systemd1",
2914 "org.freedesktop.systemd1.Manager",
2916 log_error("Could not allocate message.");
2921 if (!dbus_message_append_args(m,
2922 DBUS_TYPE_UINT32, &id,
2923 DBUS_TYPE_INVALID)) {
2924 log_error("Could not append arguments to message.");
2929 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2930 log_error("Failed to issue method call: %s", bus_error_message(&error));
2936 if (!dbus_message_get_args(reply, &error,
2937 DBUS_TYPE_OBJECT_PATH, &path,
2938 DBUS_TYPE_INVALID)) {
2939 log_error("Failed to parse reply: %s", bus_error_message(&error));
2944 if ((r = show_one(args[0], bus, path, show_properties, &new_line)) != 0)
2947 dbus_message_unref(m);
2948 dbus_message_unref(reply);
2954 dbus_message_unref(m);
2957 dbus_message_unref(reply);
2959 dbus_error_free(&error);
2964 static int dump(DBusConnection *bus, char **args) {
2965 DBusMessage *m = NULL, *reply = NULL;
2970 dbus_error_init(&error);
2972 pager_open_if_enabled();
2974 if (!(m = dbus_message_new_method_call(
2975 "org.freedesktop.systemd1",
2976 "/org/freedesktop/systemd1",
2977 "org.freedesktop.systemd1.Manager",
2979 log_error("Could not allocate message.");
2983 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2984 log_error("Failed to issue method call: %s", bus_error_message(&error));
2989 if (!dbus_message_get_args(reply, &error,
2990 DBUS_TYPE_STRING, &text,
2991 DBUS_TYPE_INVALID)) {
2992 log_error("Failed to parse reply: %s", bus_error_message(&error));
2997 fputs(text, stdout);
3003 dbus_message_unref(m);
3006 dbus_message_unref(reply);
3008 dbus_error_free(&error);
3013 static int snapshot(DBusConnection *bus, char **args) {
3014 DBusMessage *m = NULL, *reply = NULL;
3017 const char *name = "", *path, *id;
3018 dbus_bool_t cleanup = FALSE;
3019 DBusMessageIter iter, sub;
3021 *interface = "org.freedesktop.systemd1.Unit",
3024 dbus_error_init(&error);
3026 if (!(m = dbus_message_new_method_call(
3027 "org.freedesktop.systemd1",
3028 "/org/freedesktop/systemd1",
3029 "org.freedesktop.systemd1.Manager",
3030 "CreateSnapshot"))) {
3031 log_error("Could not allocate message.");
3035 if (strv_length(args) > 1)
3038 if (!dbus_message_append_args(m,
3039 DBUS_TYPE_STRING, &name,
3040 DBUS_TYPE_BOOLEAN, &cleanup,
3041 DBUS_TYPE_INVALID)) {
3042 log_error("Could not append arguments to message.");
3047 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3048 log_error("Failed to issue method call: %s", bus_error_message(&error));
3053 if (!dbus_message_get_args(reply, &error,
3054 DBUS_TYPE_OBJECT_PATH, &path,
3055 DBUS_TYPE_INVALID)) {
3056 log_error("Failed to parse reply: %s", bus_error_message(&error));
3061 dbus_message_unref(m);
3062 if (!(m = dbus_message_new_method_call(
3063 "org.freedesktop.systemd1",
3065 "org.freedesktop.DBus.Properties",
3067 log_error("Could not allocate message.");
3071 if (!dbus_message_append_args(m,
3072 DBUS_TYPE_STRING, &interface,
3073 DBUS_TYPE_STRING, &property,
3074 DBUS_TYPE_INVALID)) {
3075 log_error("Could not append arguments to message.");
3080 dbus_message_unref(reply);
3081 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3082 log_error("Failed to issue method call: %s", bus_error_message(&error));
3087 if (!dbus_message_iter_init(reply, &iter) ||
3088 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
3089 log_error("Failed to parse reply.");
3094 dbus_message_iter_recurse(&iter, &sub);
3096 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
3097 log_error("Failed to parse reply.");
3102 dbus_message_iter_get_basic(&sub, &id);
3110 dbus_message_unref(m);
3113 dbus_message_unref(reply);
3115 dbus_error_free(&error);
3120 static int delete_snapshot(DBusConnection *bus, char **args) {
3121 DBusMessage *m = NULL, *reply = NULL;
3129 dbus_error_init(&error);
3131 STRV_FOREACH(name, args+1) {
3132 const char *path = NULL;
3134 if (!(m = dbus_message_new_method_call(
3135 "org.freedesktop.systemd1",
3136 "/org/freedesktop/systemd1",
3137 "org.freedesktop.systemd1.Manager",
3139 log_error("Could not allocate message.");
3144 if (!dbus_message_append_args(m,
3145 DBUS_TYPE_STRING, name,
3146 DBUS_TYPE_INVALID)) {
3147 log_error("Could not append arguments to message.");
3152 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3153 log_error("Failed to issue method call: %s", bus_error_message(&error));
3158 if (!dbus_message_get_args(reply, &error,
3159 DBUS_TYPE_OBJECT_PATH, &path,
3160 DBUS_TYPE_INVALID)) {
3161 log_error("Failed to parse reply: %s", bus_error_message(&error));
3166 dbus_message_unref(m);
3167 if (!(m = dbus_message_new_method_call(
3168 "org.freedesktop.systemd1",
3170 "org.freedesktop.systemd1.Snapshot",
3172 log_error("Could not allocate message.");
3177 dbus_message_unref(reply);
3178 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3179 log_error("Failed to issue method call: %s", bus_error_message(&error));
3184 dbus_message_unref(m);
3185 dbus_message_unref(reply);
3193 dbus_message_unref(m);
3196 dbus_message_unref(reply);
3198 dbus_error_free(&error);
3203 static int daemon_reload(DBusConnection *bus, char **args) {
3204 DBusMessage *m = NULL, *reply = NULL;
3209 dbus_error_init(&error);
3211 if (arg_action == ACTION_RELOAD)
3213 else if (arg_action == ACTION_REEXEC)
3214 method = "Reexecute";
3216 assert(arg_action == ACTION_SYSTEMCTL);
3219 streq(args[0], "clear-jobs") ||
3220 streq(args[0], "cancel") ? "ClearJobs" :
3221 streq(args[0], "daemon-reexec") ? "Reexecute" :
3222 streq(args[0], "reset-failed") ? "ResetFailed" :
3223 streq(args[0], "halt") ? "Halt" :
3224 streq(args[0], "poweroff") ? "PowerOff" :
3225 streq(args[0], "reboot") ? "Reboot" :
3226 streq(args[0], "kexec") ? "KExec" :
3227 streq(args[0], "exit") ? "Exit" :
3228 /* "daemon-reload" */ "Reload";
3231 if (!(m = dbus_message_new_method_call(
3232 "org.freedesktop.systemd1",
3233 "/org/freedesktop/systemd1",
3234 "org.freedesktop.systemd1.Manager",
3236 log_error("Could not allocate message.");
3240 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3242 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
3243 /* There's always a fallback possible for
3244 * legacy actions. */
3249 if (streq(method, "Reexecute") && dbus_error_has_name(&error, DBUS_ERROR_NO_REPLY)) {
3250 /* On reexecution, we expect a disconnect, not
3256 log_error("Failed to issue method call: %s", bus_error_message(&error));
3265 dbus_message_unref(m);
3268 dbus_message_unref(reply);
3270 dbus_error_free(&error);
3275 static int reset_failed(DBusConnection *bus, char **args) {
3276 DBusMessage *m = NULL;
3282 dbus_error_init(&error);
3284 if (strv_length(args) <= 1)
3285 return daemon_reload(bus, args);
3287 STRV_FOREACH(name, args+1) {
3290 if (!(m = dbus_message_new_method_call(
3291 "org.freedesktop.systemd1",
3292 "/org/freedesktop/systemd1",
3293 "org.freedesktop.systemd1.Manager",
3294 "ResetFailedUnit"))) {
3295 log_error("Could not allocate message.");
3300 if (!dbus_message_append_args(m,
3301 DBUS_TYPE_STRING, name,
3302 DBUS_TYPE_INVALID)) {
3303 log_error("Could not append arguments to message.");
3308 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3309 log_error("Failed to issue method call: %s", bus_error_message(&error));
3314 dbus_message_unref(m);
3315 dbus_message_unref(reply);
3323 dbus_message_unref(m);
3325 dbus_error_free(&error);
3330 static int show_enviroment(DBusConnection *bus, char **args) {
3331 DBusMessage *m = NULL, *reply = NULL;
3333 DBusMessageIter iter, sub, sub2;
3336 *interface = "org.freedesktop.systemd1.Manager",
3337 *property = "Environment";
3339 dbus_error_init(&error);
3341 pager_open_if_enabled();
3343 if (!(m = dbus_message_new_method_call(
3344 "org.freedesktop.systemd1",
3345 "/org/freedesktop/systemd1",
3346 "org.freedesktop.DBus.Properties",
3348 log_error("Could not allocate message.");
3352 if (!dbus_message_append_args(m,
3353 DBUS_TYPE_STRING, &interface,
3354 DBUS_TYPE_STRING, &property,
3355 DBUS_TYPE_INVALID)) {
3356 log_error("Could not append arguments to message.");
3361 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3362 log_error("Failed to issue method call: %s", bus_error_message(&error));
3367 if (!dbus_message_iter_init(reply, &iter) ||
3368 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
3369 log_error("Failed to parse reply.");
3374 dbus_message_iter_recurse(&iter, &sub);
3376 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
3377 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
3378 log_error("Failed to parse reply.");
3383 dbus_message_iter_recurse(&sub, &sub2);
3385 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
3388 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
3389 log_error("Failed to parse reply.");
3394 dbus_message_iter_get_basic(&sub2, &text);
3395 printf("%s\n", text);
3397 dbus_message_iter_next(&sub2);
3404 dbus_message_unref(m);
3407 dbus_message_unref(reply);
3409 dbus_error_free(&error);
3414 static int set_environment(DBusConnection *bus, char **args) {
3415 DBusMessage *m = NULL, *reply = NULL;
3419 DBusMessageIter iter, sub;
3422 dbus_error_init(&error);
3424 method = streq(args[0], "set-environment")
3426 : "UnsetEnvironment";
3428 if (!(m = dbus_message_new_method_call(
3429 "org.freedesktop.systemd1",
3430 "/org/freedesktop/systemd1",
3431 "org.freedesktop.systemd1.Manager",
3434 log_error("Could not allocate message.");
3438 dbus_message_iter_init_append(m, &iter);
3440 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
3441 log_error("Could not append arguments to message.");
3446 STRV_FOREACH(name, args+1)
3447 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, name)) {
3448 log_error("Could not append arguments to message.");
3453 if (!dbus_message_iter_close_container(&iter, &sub)) {
3454 log_error("Could not append arguments to message.");
3459 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
3460 log_error("Failed to issue method call: %s", bus_error_message(&error));
3469 dbus_message_unref(m);
3472 dbus_message_unref(reply);
3474 dbus_error_free(&error);
3479 static int enable_sysv_units(char **args) {
3482 #if defined (HAVE_SYSV_COMPAT) && (defined(TARGET_FEDORA) || defined(TARGET_MANDRIVA) || defined(TARGET_SUSE) || defined(TARGET_MEEGO) || defined(TARGET_ALTLINUX) || defined(TARGET_MAGEIA))
3483 const char *verb = args[0];
3484 unsigned f = 1, t = 1;
3487 if (arg_scope != UNIT_FILE_SYSTEM)
3490 if (!streq(verb, "enable") &&
3491 !streq(verb, "disable") &&
3492 !streq(verb, "is-enabled"))
3495 /* Processes all SysV units, and reshuffles the array so that
3496 * afterwards only the native units remain */
3499 r = lookup_paths_init(&paths, MANAGER_SYSTEM, false);
3505 for (f = 1; args[f]; f++) {
3508 bool found_native = false, found_sysv;
3510 const char *argv[6] = { "/sbin/chkconfig", NULL, NULL, NULL, NULL };
3511 char **k, *l, *q = NULL;
3518 if (!endswith(name, ".service"))
3521 if (path_is_absolute(name))
3524 STRV_FOREACH(k, paths.unit_path) {
3527 if (!isempty(arg_root))
3528 asprintf(&p, "%s/%s/%s", arg_root, *k, name);
3530 asprintf(&p, "%s/%s", *k, name);
3533 log_error("No memory");
3538 found_native = access(p, F_OK) >= 0;
3549 if (!isempty(arg_root))
3550 asprintf(&p, "%s/" SYSTEM_SYSVINIT_PATH "/%s", arg_root, name);
3552 asprintf(&p, SYSTEM_SYSVINIT_PATH "/%s", name);
3554 log_error("No memory");
3559 p[strlen(p) - sizeof(".service") + 1] = 0;
3560 found_sysv = access(p, F_OK) >= 0;
3567 /* Mark this entry, so that we don't try enabling it as native unit */
3568 args[f] = (char*) "";
3570 log_info("%s is not a native service, redirecting to /sbin/chkconfig.", name);
3572 if (!isempty(arg_root))
3573 argv[c++] = q = strappend("--root=", arg_root);
3575 argv[c++] = file_name_from_path(p);
3577 streq(verb, "enable") ? "on" :
3578 streq(verb, "disable") ? "off" : "--level=5";
3581 l = strv_join((char**)argv, " ");
3583 log_error("No memory.");
3590 log_info("Executing %s", l);
3595 log_error("Failed to fork: %m");
3600 } else if (pid == 0) {
3603 execv(argv[0], (char**) argv);
3604 _exit(EXIT_FAILURE);
3610 j = wait_for_terminate(pid, &status);
3612 log_error("Failed to wait for child: %s", strerror(-r));
3617 if (status.si_code == CLD_EXITED) {
3618 if (streq(verb, "is-enabled")) {
3619 if (status.si_status == 0) {
3628 } else if (status.si_status != 0) {
3639 lookup_paths_free(&paths);
3641 /* Drop all SysV units */
3642 for (f = 1, t = 1; args[f]; f++) {
3644 if (isempty(args[f]))
3647 args[t++] = args[f];
3656 static int enable_unit(DBusConnection *bus, char **args) {
3657 const char *verb = args[0];
3658 UnitFileChange *changes = NULL;
3659 unsigned n_changes = 0, i;
3660 int carries_install_info = -1;
3661 DBusMessage *m = NULL, *reply = NULL;
3665 dbus_error_init(&error);
3667 r = enable_sysv_units(args);
3671 if (!bus || avoid_bus()) {
3672 if (streq(verb, "enable")) {
3673 r = unit_file_enable(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3674 carries_install_info = r;
3675 } else if (streq(verb, "disable"))
3676 r = unit_file_disable(arg_scope, arg_runtime, arg_root, args+1, &changes, &n_changes);
3677 else if (streq(verb, "reenable")) {
3678 r = unit_file_reenable(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3679 carries_install_info = r;
3680 } else if (streq(verb, "link"))
3681 r = unit_file_link(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3682 else if (streq(verb, "preset")) {
3683 r = unit_file_preset(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3684 carries_install_info = r;
3685 } else if (streq(verb, "mask"))
3686 r = unit_file_mask(arg_scope, arg_runtime, arg_root, args+1, arg_force, &changes, &n_changes);
3687 else if (streq(verb, "unmask"))
3688 r = unit_file_unmask(arg_scope, arg_runtime, arg_root, args+1, &changes, &n_changes);
3690 assert_not_reached("Unknown verb");
3693 log_error("Operation failed: %s", strerror(-r));
3698 for (i = 0; i < n_changes; i++) {
3699 if (changes[i].type == UNIT_FILE_SYMLINK)
3700 log_info("ln -s '%s' '%s'", changes[i].source, changes[i].path);
3702 log_info("rm '%s'", changes[i].path);
3708 bool send_force = true, expect_carries_install_info = false;
3710 DBusMessageIter iter, sub, sub2;
3712 if (streq(verb, "enable")) {
3713 method = "EnableUnitFiles";
3714 expect_carries_install_info = true;
3715 } else if (streq(verb, "disable")) {
3716 method = "DisableUnitFiles";
3718 } else if (streq(verb, "reenable")) {
3719 method = "ReenableUnitFiles";
3720 expect_carries_install_info = true;
3721 } else if (streq(verb, "link"))
3722 method = "LinkUnitFiles";
3723 else if (streq(verb, "preset")) {
3724 method = "PresetUnitFiles";
3725 expect_carries_install_info = true;
3726 } else if (streq(verb, "mask"))
3727 method = "MaskUnitFiles";
3728 else if (streq(verb, "unmask")) {
3729 method = "UnmaskUnitFiles";
3732 assert_not_reached("Unknown verb");
3734 m = dbus_message_new_method_call(
3735 "org.freedesktop.systemd1",
3736 "/org/freedesktop/systemd1",
3737 "org.freedesktop.systemd1.Manager",
3740 log_error("Out of memory");
3745 dbus_message_iter_init_append(m, &iter);
3747 r = bus_append_strv_iter(&iter, args+1);
3749 log_error("Failed to append unit files.");
3754 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &a)) {
3755 log_error("Failed to append runtime boolean.");
3763 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b)) {
3764 log_error("Failed to append force boolean.");
3770 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
3772 log_error("Failed to issue method call: %s", bus_error_message(&error));
3777 if (!dbus_message_iter_init(reply, &iter)) {
3778 log_error("Failed to initialize iterator.");
3782 if (expect_carries_install_info) {
3783 r = bus_iter_get_basic_and_next(&iter, DBUS_TYPE_BOOLEAN, &b, true);
3785 log_error("Failed to parse reply.");
3789 carries_install_info = b;
3792 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
3793 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
3794 log_error("Failed to parse reply.");
3799 dbus_message_iter_recurse(&iter, &sub);
3800 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
3801 const char *type, *path, *source;
3803 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
3804 log_error("Failed to parse reply.");
3809 dbus_message_iter_recurse(&sub, &sub2);
3811 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
3812 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0 ||
3813 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &source, false) < 0) {
3814 log_error("Failed to parse reply.");
3820 if (streq(type, "symlink"))
3821 log_info("ln -s '%s' '%s'", source, path);
3823 log_info("rm '%s'", path);
3826 dbus_message_iter_next(&sub);
3829 /* Try to reload if enabeld */
3831 r = daemon_reload(bus, args);
3834 if (carries_install_info == 0)
3835 log_warning("Warning: unit files do not carry install information. No operation executed.");
3839 dbus_message_unref(m);
3842 dbus_message_unref(reply);
3844 unit_file_changes_free(changes, n_changes);
3846 dbus_error_free(&error);
3850 static int unit_is_enabled(DBusConnection *bus, char **args) {
3853 DBusMessage *m = NULL, *reply = NULL;
3857 dbus_error_init(&error);
3859 r = enable_sysv_units(args);
3865 if (!bus || avoid_bus()) {
3867 STRV_FOREACH(name, args+1) {
3868 UnitFileState state;
3870 state = unit_file_get_state(arg_scope, arg_root, *name);
3876 if (state == UNIT_FILE_ENABLED ||
3877 state == UNIT_FILE_ENABLED_RUNTIME ||
3878 state == UNIT_FILE_STATIC)
3882 puts(unit_file_state_to_string(state));
3886 STRV_FOREACH(name, args+1) {
3889 m = dbus_message_new_method_call(
3890 "org.freedesktop.systemd1",
3891 "/org/freedesktop/systemd1",
3892 "org.freedesktop.systemd1.Manager",
3893 "GetUnitFileState");
3895 log_error("Out of memory");
3900 if (!dbus_message_append_args(m,
3901 DBUS_TYPE_STRING, name,
3902 DBUS_TYPE_INVALID)) {
3903 log_error("Could not append arguments to message.");
3908 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
3910 log_error("Failed to issue method call: %s", bus_error_message(&error));
3915 if (!dbus_message_get_args(reply, &error,
3916 DBUS_TYPE_STRING, &s,
3917 DBUS_TYPE_INVALID)) {
3918 log_error("Failed to parse reply: %s", bus_error_message(&error));
3923 dbus_message_unref(m);
3924 dbus_message_unref(reply);
3927 if (streq(s, "enabled") ||
3928 streq(s, "enabled-runtime") ||
3937 r = enabled ? 0 : 1;
3941 dbus_message_unref(m);
3944 dbus_message_unref(reply);
3946 dbus_error_free(&error);
3950 static int systemctl_help(void) {
3952 pager_open_if_enabled();
3954 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
3955 "Query or send control commands to the systemd manager.\n\n"
3956 " -h --help Show this help\n"
3957 " --version Show package version\n"
3958 " -t --type=TYPE List only units of a particular type\n"
3959 " -p --property=NAME Show only properties by this name\n"
3960 " -a --all Show all units/properties, including dead/empty ones\n"
3961 " --failed Show only failed units\n"
3962 " --full Don't ellipsize unit names on output\n"
3963 " --fail When queueing a new job, fail if conflicting jobs are\n"
3965 " --ignore-dependencies\n"
3966 " When queueing a new job, ignore all its dependencies\n"
3967 " --kill-who=WHO Who to send signal to\n"
3968 " -s --signal=SIGNAL Which signal to send\n"
3969 " -H --host=[USER@]HOST\n"
3970 " Show information for remote host\n"
3971 " -P --privileged Acquire privileges before execution\n"
3972 " -q --quiet Suppress output\n"
3973 " --no-block Do not wait until operation finished\n"
3974 " --no-wall Don't send wall message before halt/power-off/reboot\n"
3975 " --no-reload When enabling/disabling unit files, don't reload daemon\n"
3977 " --no-legend Do not print a legend (column headers and hints)\n"
3978 " --no-pager Do not pipe output into a pager\n"
3979 " --no-ask-password\n"
3980 " Do not ask for system passwords\n"
3981 " --order When generating graph for dot, show only order\n"
3982 " --require When generating graph for dot, show only requirement\n"
3983 " --system Connect to system manager\n"
3984 " --user Connect to user service manager\n"
3985 " --global Enable/disable unit files globally\n"
3986 " -f --force When enabling unit files, override existing symlinks\n"
3987 " When shutting down, execute action immediately\n"
3988 " --root=PATH Enable unit files in the specified root directory\n"
3989 " --runtime Enable unit files only temporarily until next reboot\n"
3990 " -n --lines=INTEGER Journal entries to show\n"
3991 " --follow Follow journal\n"
3992 " -o --output=STRING Change journal output mode (short, short-monotonic,\n"
3993 " verbose, export, json, cat)\n\n"
3995 " list-units List loaded units\n"
3996 " start [NAME...] Start (activate) one or more units\n"
3997 " stop [NAME...] Stop (deactivate) one or more units\n"
3998 " reload [NAME...] Reload one or more units\n"
3999 " restart [NAME...] Start or restart one or more units\n"
4000 " try-restart [NAME...] Restart one or more units if active\n"
4001 " reload-or-restart [NAME...] Reload one or more units is possible,\n"
4002 " otherwise start or restart\n"
4003 " reload-or-try-restart [NAME...] Reload one or more units is possible,\n"
4004 " otherwise restart if active\n"
4005 " isolate [NAME] Start one unit and stop all others\n"
4006 " kill [NAME...] Send signal to processes of a unit\n"
4007 " is-active [NAME...] Check whether units are active\n"
4008 " status [NAME...|PID...] Show runtime status of one or more units\n"
4009 " show [NAME...|JOB...] Show properties of one or more\n"
4010 " units/jobs or the manager\n"
4011 " reset-failed [NAME...] Reset failed state for all, one, or more\n"
4013 " load [NAME...] Load one or more units\n\n"
4014 "Unit File Commands:\n"
4015 " list-unit-files List installed unit files\n"
4016 " enable [NAME...] Enable one or more unit files\n"
4017 " disable [NAME...] Disable one or more unit files\n"
4018 " reenable [NAME...] Reenable one or more unit files\n"
4019 " preset [NAME...] Enable/disable one or more unit files\n"
4020 " based on preset configuration\n"
4021 " mask [NAME...] Mask one or more units\n"
4022 " unmask [NAME...] Unmask one or more units\n"
4023 " link [PATH...] Link one or more units files into\n"
4024 " the search path\n"
4025 " is-enabled [NAME...] Check whether unit files are enabled\n\n"
4027 " list-jobs List jobs\n"
4028 " cancel [JOB...] Cancel all, one, or more jobs\n\n"
4029 "Status Commands:\n"
4030 " dump Dump server status\n"
4031 " dot Dump dependency graph for dot(1)\n\n"
4032 "Snapshot Commands:\n"
4033 " snapshot [NAME] Create a snapshot\n"
4034 " delete [NAME...] Remove one or more snapshots\n\n"
4035 "Environment Commands:\n"
4036 " show-environment Dump environment\n"
4037 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
4038 " unset-environment [NAME...] Unset one or more environment variables\n\n"
4039 "Manager Lifecycle Commands:\n"
4040 " daemon-reload Reload systemd manager configuration\n"
4041 " daemon-reexec Reexecute systemd manager\n\n"
4042 "System Commands:\n"
4043 " default Enter system default mode\n"
4044 " rescue Enter system rescue mode\n"
4045 " emergency Enter system emergency mode\n"
4046 " halt Shut down and halt the system\n"
4047 " poweroff Shut down and power-off the system\n"
4048 " reboot Shut down and reboot the system\n"
4049 " kexec Shut down and reboot the system with kexec\n"
4050 " exit Ask for user instance termination\n",
4051 program_invocation_short_name);
4056 static int halt_help(void) {
4058 printf("%s [OPTIONS...]\n\n"
4059 "%s the system.\n\n"
4060 " --help Show this help\n"
4061 " --halt Halt the machine\n"
4062 " -p --poweroff Switch off the machine\n"
4063 " --reboot Reboot the machine\n"
4064 " -f --force Force immediate halt/power-off/reboot\n"
4065 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
4066 " -d --no-wtmp Don't write wtmp record\n"
4067 " -n --no-sync Don't sync before halt/power-off/reboot\n"
4068 " --no-wall Don't send wall message before halt/power-off/reboot\n",
4069 program_invocation_short_name,
4070 arg_action == ACTION_REBOOT ? "Reboot" :
4071 arg_action == ACTION_POWEROFF ? "Power off" :
4077 static int shutdown_help(void) {
4079 printf("%s [OPTIONS...] [TIME] [WALL...]\n\n"
4080 "Shut down the system.\n\n"
4081 " --help Show this help\n"
4082 " -H --halt Halt the machine\n"
4083 " -P --poweroff Power-off the machine\n"
4084 " -r --reboot Reboot the machine\n"
4085 " -h Equivalent to --poweroff, overriden by --halt\n"
4086 " -k Don't halt/power-off/reboot, just send warnings\n"
4087 " --no-wall Don't send wall message before halt/power-off/reboot\n"
4088 " -c Cancel a pending shutdown\n",
4089 program_invocation_short_name);
4094 static int telinit_help(void) {
4096 printf("%s [OPTIONS...] {COMMAND}\n\n"
4097 "Send control commands to the init daemon.\n\n"
4098 " --help Show this help\n"
4099 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
4101 " 0 Power-off the machine\n"
4102 " 6 Reboot the machine\n"
4103 " 2, 3, 4, 5 Start runlevelX.target unit\n"
4104 " 1, s, S Enter rescue mode\n"
4105 " q, Q Reload init daemon configuration\n"
4106 " u, U Reexecute init daemon\n",
4107 program_invocation_short_name);
4112 static int runlevel_help(void) {
4114 printf("%s [OPTIONS...]\n\n"
4115 "Prints the previous and current runlevel of the init system.\n\n"
4116 " --help Show this help\n",
4117 program_invocation_short_name);
4122 static int systemctl_parse_argv(int argc, char *argv[]) {
4126 ARG_IGNORE_DEPENDENCIES,
4142 ARG_NO_ASK_PASSWORD,
4148 static const struct option options[] = {
4149 { "help", no_argument, NULL, 'h' },
4150 { "version", no_argument, NULL, ARG_VERSION },
4151 { "type", required_argument, NULL, 't' },
4152 { "property", required_argument, NULL, 'p' },
4153 { "all", no_argument, NULL, 'a' },
4154 { "failed", no_argument, NULL, ARG_FAILED },
4155 { "full", no_argument, NULL, ARG_FULL },
4156 { "fail", no_argument, NULL, ARG_FAIL },
4157 { "ignore-dependencies", no_argument, NULL, ARG_IGNORE_DEPENDENCIES },
4158 { "user", no_argument, NULL, ARG_USER },
4159 { "system", no_argument, NULL, ARG_SYSTEM },
4160 { "global", no_argument, NULL, ARG_GLOBAL },
4161 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
4162 { "no-legend", no_argument, NULL, ARG_NO_LEGEND },
4163 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
4164 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4165 { "quiet", no_argument, NULL, 'q' },
4166 { "order", no_argument, NULL, ARG_ORDER },
4167 { "require", no_argument, NULL, ARG_REQUIRE },
4168 { "root", required_argument, NULL, ARG_ROOT },
4169 { "force", no_argument, NULL, 'f' },
4170 { "no-reload", no_argument, NULL, ARG_NO_RELOAD },
4171 { "kill-mode", required_argument, NULL, ARG_KILL_MODE }, /* undocumented on purpose */
4172 { "kill-who", required_argument, NULL, ARG_KILL_WHO },
4173 { "signal", required_argument, NULL, 's' },
4174 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
4175 { "host", required_argument, NULL, 'H' },
4176 { "privileged",no_argument, NULL, 'P' },
4177 { "runtime", no_argument, NULL, ARG_RUNTIME },
4178 { "lines", required_argument, NULL, 'n' },
4179 { "follow", no_argument, NULL, ARG_FOLLOW },
4180 { "output", required_argument, NULL, 'o' },
4181 { NULL, 0, NULL, 0 }
4189 /* Only when running as systemctl we ask for passwords */
4190 arg_ask_password = true;
4192 while ((c = getopt_long(argc, argv, "ht:p:aqfs:H:Pn:o:", options, NULL)) >= 0) {
4201 puts(PACKAGE_STRING);
4203 puts(SYSTEMD_FEATURES);
4213 if (!(l = strv_append(arg_property, optarg)))
4216 strv_free(arg_property);
4219 /* If the user asked for a particular
4220 * property, show it to him, even if it is
4231 arg_job_mode = "fail";
4234 case ARG_IGNORE_DEPENDENCIES:
4235 arg_job_mode = "ignore-dependencies";
4239 arg_scope = UNIT_FILE_USER;
4243 arg_scope = UNIT_FILE_SYSTEM;
4247 arg_scope = UNIT_FILE_GLOBAL;
4251 arg_no_block = true;
4255 arg_no_legend = true;
4259 arg_no_pager = true;
4267 arg_dot = DOT_ORDER;
4271 arg_dot = DOT_REQUIRE;
4295 arg_no_reload = true;
4299 arg_kill_who = optarg;
4303 arg_kill_mode = optarg;
4307 if ((arg_signal = signal_from_string_try_harder(optarg)) < 0) {
4308 log_error("Failed to parse signal string %s.", optarg);
4313 case ARG_NO_ASK_PASSWORD:
4314 arg_ask_password = false;
4318 arg_transport = TRANSPORT_POLKIT;
4322 arg_transport = TRANSPORT_SSH;
4331 if (safe_atou(optarg, &arg_lines) < 0) {
4332 log_error("Failed to parse lines '%s'", optarg);
4342 arg_output = output_mode_from_string(optarg);
4343 if (arg_output < 0) {
4344 log_error("Unknown output '%s'.", optarg);
4353 log_error("Unknown option code %c", c);
4358 if (arg_transport != TRANSPORT_NORMAL && arg_scope != UNIT_FILE_SYSTEM) {
4359 log_error("Cannot access user instance remotely.");
4366 static int halt_parse_argv(int argc, char *argv[]) {
4375 static const struct option options[] = {
4376 { "help", no_argument, NULL, ARG_HELP },
4377 { "halt", no_argument, NULL, ARG_HALT },
4378 { "poweroff", no_argument, NULL, 'p' },
4379 { "reboot", no_argument, NULL, ARG_REBOOT },
4380 { "force", no_argument, NULL, 'f' },
4381 { "wtmp-only", no_argument, NULL, 'w' },
4382 { "no-wtmp", no_argument, NULL, 'd' },
4383 { "no-sync", no_argument, NULL, 'n' },
4384 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4385 { NULL, 0, NULL, 0 }
4393 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
4394 if (runlevel == '0' || runlevel == '6')
4395 arg_immediate = true;
4397 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
4405 arg_action = ACTION_HALT;
4409 if (arg_action != ACTION_REBOOT)
4410 arg_action = ACTION_POWEROFF;
4414 arg_action = ACTION_REBOOT;
4418 arg_immediate = true;
4439 /* Compatibility nops */
4446 log_error("Unknown option code %c", c);
4451 if (optind < argc) {
4452 log_error("Too many arguments.");
4459 static int parse_time_spec(const char *t, usec_t *_u) {
4463 if (streq(t, "now"))
4465 else if (!strchr(t, ':')) {
4468 if (safe_atou64(t, &u) < 0)
4471 *_u = now(CLOCK_REALTIME) + USEC_PER_MINUTE * u;
4480 hour = strtol(t, &e, 10);
4481 if (errno != 0 || *e != ':' || hour < 0 || hour > 23)
4484 minute = strtol(e+1, &e, 10);
4485 if (errno != 0 || *e != 0 || minute < 0 || minute > 59)
4488 n = now(CLOCK_REALTIME);
4489 s = (time_t) (n / USEC_PER_SEC);
4492 assert_se(localtime_r(&s, &tm));
4494 tm.tm_hour = (int) hour;
4495 tm.tm_min = (int) minute;
4498 assert_se(s = mktime(&tm));
4500 *_u = (usec_t) s * USEC_PER_SEC;
4503 *_u += USEC_PER_DAY;
4509 static int shutdown_parse_argv(int argc, char *argv[]) {
4516 static const struct option options[] = {
4517 { "help", no_argument, NULL, ARG_HELP },
4518 { "halt", no_argument, NULL, 'H' },
4519 { "poweroff", no_argument, NULL, 'P' },
4520 { "reboot", no_argument, NULL, 'r' },
4521 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4522 { NULL, 0, NULL, 0 }
4530 while ((c = getopt_long(argc, argv, "HPrhkt:afFc", options, NULL)) >= 0) {
4538 arg_action = ACTION_HALT;
4542 arg_action = ACTION_POWEROFF;
4547 arg_action = ACTION_KEXEC;
4549 arg_action = ACTION_REBOOT;
4553 if (arg_action != ACTION_HALT)
4554 arg_action = ACTION_POWEROFF;
4567 /* Compatibility nops */
4571 arg_action = ACTION_CANCEL_SHUTDOWN;
4578 log_error("Unknown option code %c", c);
4583 if (argc > optind) {
4584 if ((r = parse_time_spec(argv[optind], &arg_when)) < 0) {
4585 log_error("Failed to parse time specification: %s", argv[optind]);
4589 arg_when = now(CLOCK_REALTIME) + USEC_PER_MINUTE;
4591 /* We skip the time argument */
4592 if (argc > optind + 1)
4593 arg_wall = argv + optind + 1;
4600 static int telinit_parse_argv(int argc, char *argv[]) {
4607 static const struct option options[] = {
4608 { "help", no_argument, NULL, ARG_HELP },
4609 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4610 { NULL, 0, NULL, 0 }
4613 static const struct {
4617 { '0', ACTION_POWEROFF },
4618 { '6', ACTION_REBOOT },
4619 { '1', ACTION_RESCUE },
4620 { '2', ACTION_RUNLEVEL2 },
4621 { '3', ACTION_RUNLEVEL3 },
4622 { '4', ACTION_RUNLEVEL4 },
4623 { '5', ACTION_RUNLEVEL5 },
4624 { 's', ACTION_RESCUE },
4625 { 'S', ACTION_RESCUE },
4626 { 'q', ACTION_RELOAD },
4627 { 'Q', ACTION_RELOAD },
4628 { 'u', ACTION_REEXEC },
4629 { 'U', ACTION_REEXEC }
4638 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4653 log_error("Unknown option code %c", c);
4658 if (optind >= argc) {
4663 if (optind + 1 < argc) {
4664 log_error("Too many arguments.");
4668 if (strlen(argv[optind]) != 1) {
4669 log_error("Expected single character argument.");
4673 for (i = 0; i < ELEMENTSOF(table); i++)
4674 if (table[i].from == argv[optind][0])
4677 if (i >= ELEMENTSOF(table)) {
4678 log_error("Unknown command %s.", argv[optind]);
4682 arg_action = table[i].to;
4689 static int runlevel_parse_argv(int argc, char *argv[]) {
4695 static const struct option options[] = {
4696 { "help", no_argument, NULL, ARG_HELP },
4697 { NULL, 0, NULL, 0 }
4705 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4716 log_error("Unknown option code %c", c);
4721 if (optind < argc) {
4722 log_error("Too many arguments.");
4729 static int parse_argv(int argc, char *argv[]) {
4733 if (program_invocation_short_name) {
4735 if (strstr(program_invocation_short_name, "halt")) {
4736 arg_action = ACTION_HALT;
4737 return halt_parse_argv(argc, argv);
4738 } else if (strstr(program_invocation_short_name, "poweroff")) {
4739 arg_action = ACTION_POWEROFF;
4740 return halt_parse_argv(argc, argv);
4741 } else if (strstr(program_invocation_short_name, "reboot")) {
4743 arg_action = ACTION_KEXEC;
4745 arg_action = ACTION_REBOOT;
4746 return halt_parse_argv(argc, argv);
4747 } else if (strstr(program_invocation_short_name, "shutdown")) {
4748 arg_action = ACTION_POWEROFF;
4749 return shutdown_parse_argv(argc, argv);
4750 } else if (strstr(program_invocation_short_name, "init")) {
4752 if (sd_booted() > 0) {
4753 arg_action = ACTION_INVALID;
4754 return telinit_parse_argv(argc, argv);
4756 /* Hmm, so some other init system is
4757 * running, we need to forward this
4758 * request to it. For now we simply
4759 * guess that it is Upstart. */
4761 execv("/lib/upstart/telinit", argv);
4763 log_error("Couldn't find an alternative telinit implementation to spawn.");
4767 } else if (strstr(program_invocation_short_name, "runlevel")) {
4768 arg_action = ACTION_RUNLEVEL;
4769 return runlevel_parse_argv(argc, argv);
4773 arg_action = ACTION_SYSTEMCTL;
4774 return systemctl_parse_argv(argc, argv);
4777 static int action_to_runlevel(void) {
4779 static const char table[_ACTION_MAX] = {
4780 [ACTION_HALT] = '0',
4781 [ACTION_POWEROFF] = '0',
4782 [ACTION_REBOOT] = '6',
4783 [ACTION_RUNLEVEL2] = '2',
4784 [ACTION_RUNLEVEL3] = '3',
4785 [ACTION_RUNLEVEL4] = '4',
4786 [ACTION_RUNLEVEL5] = '5',
4787 [ACTION_RESCUE] = '1'
4790 assert(arg_action < _ACTION_MAX);
4792 return table[arg_action];
4795 static int talk_upstart(void) {
4796 DBusMessage *m = NULL, *reply = NULL;
4798 int previous, rl, r;
4800 env1_buf[] = "RUNLEVEL=X",
4801 env2_buf[] = "PREVLEVEL=X";
4802 char *env1 = env1_buf, *env2 = env2_buf;
4803 const char *emit = "runlevel";
4804 dbus_bool_t b_false = FALSE;
4805 DBusMessageIter iter, sub;
4806 DBusConnection *bus;
4808 dbus_error_init(&error);
4810 if (!(rl = action_to_runlevel()))
4813 if (utmp_get_runlevel(&previous, NULL) < 0)
4816 if (!(bus = dbus_connection_open_private("unix:abstract=/com/ubuntu/upstart", &error))) {
4817 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
4822 log_error("Failed to connect to Upstart bus: %s", bus_error_message(&error));
4827 if ((r = bus_check_peercred(bus)) < 0) {
4828 log_error("Failed to verify owner of bus.");
4832 if (!(m = dbus_message_new_method_call(
4833 "com.ubuntu.Upstart",
4834 "/com/ubuntu/Upstart",
4835 "com.ubuntu.Upstart0_6",
4838 log_error("Could not allocate message.");
4843 dbus_message_iter_init_append(m, &iter);
4845 env1_buf[sizeof(env1_buf)-2] = rl;
4846 env2_buf[sizeof(env2_buf)-2] = previous;
4848 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
4849 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
4850 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
4851 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
4852 !dbus_message_iter_close_container(&iter, &sub) ||
4853 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
4854 log_error("Could not append arguments to message.");
4859 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
4861 if (error_is_no_service(&error)) {
4866 log_error("Failed to issue method call: %s", bus_error_message(&error));
4875 dbus_message_unref(m);
4878 dbus_message_unref(reply);
4881 dbus_connection_flush(bus);
4882 dbus_connection_close(bus);
4883 dbus_connection_unref(bus);
4886 dbus_error_free(&error);
4891 static int talk_initctl(void) {
4892 struct init_request request;
4896 if (!(rl = action_to_runlevel()))
4900 request.magic = INIT_MAGIC;
4901 request.sleeptime = 0;
4902 request.cmd = INIT_CMD_RUNLVL;
4903 request.runlevel = rl;
4905 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
4907 if (errno == ENOENT)
4910 log_error("Failed to open "INIT_FIFO": %m");
4915 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
4916 close_nointr_nofail(fd);
4919 log_error("Failed to write to "INIT_FIFO": %m");
4920 return errno ? -errno : -EIO;
4926 static int systemctl_main(DBusConnection *bus, int argc, char *argv[], DBusError *error) {
4928 static const struct {
4936 int (* const dispatch)(DBusConnection *bus, char **args);
4938 { "list-units", LESS, 1, list_units },
4939 { "list-unit-files", EQUAL, 1, list_unit_files },
4940 { "list-jobs", EQUAL, 1, list_jobs },
4941 { "clear-jobs", EQUAL, 1, daemon_reload },
4942 { "load", MORE, 2, load_unit },
4943 { "cancel", MORE, 2, cancel_job },
4944 { "start", MORE, 2, start_unit },
4945 { "stop", MORE, 2, start_unit },
4946 { "condstop", MORE, 2, start_unit }, /* For compatibility with ALTLinux */
4947 { "reload", MORE, 2, start_unit },
4948 { "restart", MORE, 2, start_unit },
4949 { "try-restart", MORE, 2, start_unit },
4950 { "reload-or-restart", MORE, 2, start_unit },
4951 { "reload-or-try-restart", MORE, 2, start_unit },
4952 { "force-reload", MORE, 2, start_unit }, /* For compatibility with SysV */
4953 { "condreload", MORE, 2, start_unit }, /* For compatibility with ALTLinux */
4954 { "condrestart", MORE, 2, start_unit }, /* For compatibility with RH */
4955 { "isolate", EQUAL, 2, start_unit },
4956 { "kill", MORE, 2, kill_unit },
4957 { "is-active", MORE, 2, check_unit },
4958 { "check", MORE, 2, check_unit },
4959 { "show", MORE, 1, show },
4960 { "status", MORE, 2, show },
4961 { "dump", EQUAL, 1, dump },
4962 { "dot", EQUAL, 1, dot },
4963 { "snapshot", LESS, 2, snapshot },
4964 { "delete", MORE, 2, delete_snapshot },
4965 { "daemon-reload", EQUAL, 1, daemon_reload },
4966 { "daemon-reexec", EQUAL, 1, daemon_reload },
4967 { "show-environment", EQUAL, 1, show_enviroment },
4968 { "set-environment", MORE, 2, set_environment },
4969 { "unset-environment", MORE, 2, set_environment },
4970 { "halt", EQUAL, 1, start_special },
4971 { "poweroff", EQUAL, 1, start_special },
4972 { "reboot", EQUAL, 1, start_special },
4973 { "kexec", EQUAL, 1, start_special },
4974 { "default", EQUAL, 1, start_special },
4975 { "rescue", EQUAL, 1, start_special },
4976 { "emergency", EQUAL, 1, start_special },
4977 { "exit", EQUAL, 1, start_special },
4978 { "reset-failed", MORE, 1, reset_failed },
4979 { "enable", MORE, 2, enable_unit },
4980 { "disable", MORE, 2, enable_unit },
4981 { "is-enabled", MORE, 2, unit_is_enabled },
4982 { "reenable", MORE, 2, enable_unit },
4983 { "preset", MORE, 2, enable_unit },
4984 { "mask", MORE, 2, enable_unit },
4985 { "unmask", MORE, 2, enable_unit },
4986 { "link", MORE, 2, enable_unit }
4996 left = argc - optind;
4999 /* Special rule: no arguments means "list-units" */
5002 if (streq(argv[optind], "help")) {
5007 for (i = 0; i < ELEMENTSOF(verbs); i++)
5008 if (streq(argv[optind], verbs[i].verb))
5011 if (i >= ELEMENTSOF(verbs)) {
5012 log_error("Unknown operation %s", argv[optind]);
5017 switch (verbs[i].argc_cmp) {
5020 if (left != verbs[i].argc) {
5021 log_error("Invalid number of arguments.");
5028 if (left < verbs[i].argc) {
5029 log_error("Too few arguments.");
5036 if (left > verbs[i].argc) {
5037 log_error("Too many arguments.");
5044 assert_not_reached("Unknown comparison operator.");
5047 /* Require a bus connection for all operations but
5049 if (!streq(verbs[i].verb, "enable") &&
5050 !streq(verbs[i].verb, "disable") &&
5051 !streq(verbs[i].verb, "is-enabled") &&
5052 !streq(verbs[i].verb, "list-unit-files") &&
5053 !streq(verbs[i].verb, "reenable") &&
5054 !streq(verbs[i].verb, "preset") &&
5055 !streq(verbs[i].verb, "mask") &&
5056 !streq(verbs[i].verb, "unmask") &&
5057 !streq(verbs[i].verb, "link")) {
5059 if (running_in_chroot() > 0) {
5060 log_info("Running in chroot, ignoring request.");
5065 log_error("Failed to get D-Bus connection: %s",
5066 dbus_error_is_set(error) ? error->message : "No connection to service manager.");
5072 if (!bus && !avoid_bus()) {
5073 log_error("Failed to get D-Bus connection: %s",
5074 dbus_error_is_set(error) ? error->message : "No connection to service manager.");
5079 return verbs[i].dispatch(bus, argv + optind);
5082 static int send_shutdownd(usec_t t, char mode, bool dry_run, bool warn, const char *message) {
5084 struct msghdr msghdr;
5086 union sockaddr_union sockaddr;
5087 struct shutdownd_command c;
5092 c.dry_run = dry_run;
5096 strncpy(c.wall_message, message, sizeof(c.wall_message));
5098 if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0)
5102 sockaddr.sa.sa_family = AF_UNIX;
5103 sockaddr.un.sun_path[0] = 0;
5104 strncpy(sockaddr.un.sun_path, "/run/systemd/shutdownd", sizeof(sockaddr.un.sun_path));
5107 iovec.iov_base = (char*) &c;
5108 iovec.iov_len = sizeof(c);
5111 msghdr.msg_name = &sockaddr;
5112 msghdr.msg_namelen = offsetof(struct sockaddr_un, sun_path) + sizeof("/run/systemd/shutdownd") - 1;
5114 msghdr.msg_iov = &iovec;
5115 msghdr.msg_iovlen = 1;
5117 if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) < 0) {
5118 close_nointr_nofail(fd);
5122 close_nointr_nofail(fd);
5126 static int reload_with_fallback(DBusConnection *bus) {
5129 /* First, try systemd via D-Bus. */
5130 if (daemon_reload(bus, NULL) >= 0)
5134 /* Nothing else worked, so let's try signals */
5135 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
5137 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
5138 log_error("kill() failed: %m");
5145 static int start_with_fallback(DBusConnection *bus) {
5148 /* First, try systemd via D-Bus. */
5149 if (start_unit(bus, NULL) >= 0)
5153 /* Hmm, talking to systemd via D-Bus didn't work. Then
5154 * let's try to talk to Upstart via D-Bus. */
5155 if (talk_upstart() > 0)
5158 /* Nothing else worked, so let's try
5160 if (talk_initctl() > 0)
5163 log_error("Failed to talk to init daemon.");
5167 warn_wall(arg_action);
5171 static int halt_main(DBusConnection *bus) {
5174 if (geteuid() != 0) {
5175 log_error("Must be root.");
5181 char date[FORMAT_TIMESTAMP_MAX];
5183 m = strv_join(arg_wall, " ");
5184 r = send_shutdownd(arg_when,
5185 arg_action == ACTION_HALT ? 'H' :
5186 arg_action == ACTION_POWEROFF ? 'P' :
5194 log_warning("Failed to talk to shutdownd, proceeding with immediate shutdown: %s", strerror(-r));
5196 log_info("Shutdown scheduled for %s, use 'shutdown -c' to cancel.",
5197 format_timestamp(date, sizeof(date), arg_when));
5202 if (!arg_dry && !arg_immediate)
5203 return start_with_fallback(bus);
5206 if (sd_booted() > 0)
5207 log_debug("Not writing utmp record, assuming that systemd-update-utmp is used.");
5208 else if ((r = utmp_put_shutdown()) < 0)
5209 log_warning("Failed to write utmp record: %s", strerror(-r));
5218 /* Make sure C-A-D is handled by the kernel from this
5220 reboot(RB_ENABLE_CAD);
5222 switch (arg_action) {
5225 log_info("Halting.");
5226 reboot(RB_HALT_SYSTEM);
5229 case ACTION_POWEROFF:
5230 log_info("Powering off.");
5231 reboot(RB_POWER_OFF);
5235 log_info("Rebooting.");
5236 reboot(RB_AUTOBOOT);
5240 assert_not_reached("Unknown halt action.");
5243 /* We should never reach this. */
5247 static int runlevel_main(void) {
5248 int r, runlevel, previous;
5250 r = utmp_get_runlevel(&runlevel, &previous);
5257 previous <= 0 ? 'N' : previous,
5258 runlevel <= 0 ? 'N' : runlevel);
5263 int main(int argc, char*argv[]) {
5264 int r, retval = EXIT_FAILURE;
5265 DBusConnection *bus = NULL;
5268 dbus_error_init(&error);
5270 log_parse_environment();
5273 if ((r = parse_argv(argc, argv)) < 0)
5276 retval = EXIT_SUCCESS;
5280 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
5281 * let's shortcut this */
5282 if (arg_action == ACTION_RUNLEVEL) {
5283 r = runlevel_main();
5284 retval = r < 0 ? EXIT_FAILURE : r;
5288 if (running_in_chroot() > 0 && arg_action != ACTION_SYSTEMCTL) {
5289 log_info("Running in chroot, ignoring request.");
5295 if (arg_transport == TRANSPORT_NORMAL)
5296 bus_connect(arg_scope == UNIT_FILE_SYSTEM ? DBUS_BUS_SYSTEM : DBUS_BUS_SESSION, &bus, &private_bus, &error);
5297 else if (arg_transport == TRANSPORT_POLKIT) {
5298 bus_connect_system_polkit(&bus, &error);
5299 private_bus = false;
5300 } else if (arg_transport == TRANSPORT_SSH) {
5301 bus_connect_system_ssh(NULL, arg_host, &bus, &error);
5302 private_bus = false;
5304 assert_not_reached("Uh, invalid transport...");
5307 switch (arg_action) {
5309 case ACTION_SYSTEMCTL:
5310 r = systemctl_main(bus, argc, argv, &error);
5314 case ACTION_POWEROFF:
5320 case ACTION_RUNLEVEL2:
5321 case ACTION_RUNLEVEL3:
5322 case ACTION_RUNLEVEL4:
5323 case ACTION_RUNLEVEL5:
5325 case ACTION_EMERGENCY:
5326 case ACTION_DEFAULT:
5327 r = start_with_fallback(bus);
5332 r = reload_with_fallback(bus);
5335 case ACTION_CANCEL_SHUTDOWN:
5336 r = send_shutdownd(0, 0, false, false, NULL);
5339 case ACTION_INVALID:
5340 case ACTION_RUNLEVEL:
5342 assert_not_reached("Unknown action");
5345 retval = r < 0 ? EXIT_FAILURE : r;
5349 dbus_connection_flush(bus);
5350 dbus_connection_close(bus);
5351 dbus_connection_unref(bus);
5354 dbus_error_free(&error);
5358 strv_free(arg_property);