1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
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 <dbus/dbus.h>
41 #include "utmp-wtmp.h"
45 #include "dbus-common.h"
46 #include "cgroup-show.h"
47 #include "cgroup-util.h"
49 #include "path-lookup.h"
50 #include "conf-parser.h"
51 #include "sd-daemon.h"
53 static const char *arg_type = NULL;
54 static char **arg_property = NULL;
55 static bool arg_all = false;
56 static bool arg_fail = false;
57 static bool arg_session = false;
58 static bool arg_global = false;
59 static bool arg_immediate = false;
60 static bool arg_no_block = false;
61 static bool arg_no_wtmp = false;
62 static bool arg_no_sync = false;
63 static bool arg_no_wall = false;
64 static bool arg_no_reload = false;
65 static bool arg_dry = false;
66 static bool arg_quiet = false;
67 static bool arg_full = false;
68 static bool arg_force = false;
69 static bool arg_defaults = false;
70 static char **arg_wall = NULL;
88 } arg_action = ACTION_SYSTEMCTL;
95 static bool private_bus = false;
97 static int daemon_reload(DBusConnection *bus, char **args, unsigned n);
99 static bool on_tty(void) {
102 if (_unlikely_(t < 0))
103 t = isatty(STDOUT_FILENO) > 0;
108 static const char *ansi_highlight(bool b) {
113 return b ? ANSI_HIGHLIGHT_ON : ANSI_HIGHLIGHT_OFF;
116 static const char *ansi_highlight_green(bool b) {
121 return b ? ANSI_HIGHLIGHT_GREEN_ON : ANSI_HIGHLIGHT_OFF;
124 static bool error_is_no_service(DBusError *error) {
128 if (!dbus_error_is_set(error))
131 if (dbus_error_has_name(error, DBUS_ERROR_NAME_HAS_NO_OWNER))
134 if (dbus_error_has_name(error, DBUS_ERROR_SERVICE_UNKNOWN))
137 return startswith(error->name, "org.freedesktop.DBus.Error.Spawn.");
140 static int bus_iter_get_basic_and_next(DBusMessageIter *iter, int type, void *data, bool next) {
145 if (dbus_message_iter_get_arg_type(iter) != type)
148 dbus_message_iter_get_basic(iter, data);
150 if (!dbus_message_iter_next(iter) != !next)
156 static void warn_wall(enum action action) {
157 static const char *table[_ACTION_MAX] = {
158 [ACTION_HALT] = "The system is going down for system halt NOW!",
159 [ACTION_REBOOT] = "The system is going down for reboot NOW!",
160 [ACTION_POWEROFF] = "The system is going down for power-off NOW!",
161 [ACTION_RESCUE] = "The system is going down to rescue mode NOW!",
162 [ACTION_EMERGENCY] = "The system is going down to emergency mode NOW!"
171 if (!(p = strv_join(arg_wall, " "))) {
172 log_error("Failed to join strings.");
188 utmp_wall(table[action]);
191 static int list_units(DBusConnection *bus, char **args, unsigned n) {
192 DBusMessage *m = NULL, *reply = NULL;
195 DBusMessageIter iter, sub, sub2;
198 dbus_error_init(&error);
202 if (!(m = dbus_message_new_method_call(
203 "org.freedesktop.systemd1",
204 "/org/freedesktop/systemd1",
205 "org.freedesktop.systemd1.Manager",
207 log_error("Could not allocate message.");
211 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
212 log_error("Failed to issue method call: %s", error.message);
217 if (!dbus_message_iter_init(reply, &iter) ||
218 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
219 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
220 log_error("Failed to parse reply.");
225 dbus_message_iter_recurse(&iter, &sub);
227 if (isatty(STDOUT_FILENO))
228 printf("%-45s %-6s %-12s %-12s %-15s %s\n", "UNIT", "LOAD", "ACTIVE", "SUB", "JOB", "DESCRIPTION");
230 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
231 const char *id, *description, *load_state, *active_state, *sub_state, *following, *unit_path, *job_type, *job_path, *dot;
234 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
235 log_error("Failed to parse reply.");
240 dbus_message_iter_recurse(&sub, &sub2);
242 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
243 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
244 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
245 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
246 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
247 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &following, true) < 0 ||
248 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, true) < 0 ||
249 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &job_id, true) < 0 ||
250 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &job_type, true) < 0 ||
251 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, false) < 0) {
252 log_error("Failed to parse reply.");
257 if ((!arg_type || ((dot = strrchr(id, '.')) &&
258 streq(dot+1, arg_type))) &&
259 (arg_all || !(streq(active_state, "inactive") || following[0]) || job_id > 0)) {
262 const char *on, *off;
264 if (streq(active_state, "maintenance")) {
265 on = ansi_highlight(true);
266 off = ansi_highlight(false);
270 e = arg_full ? NULL : ellipsize(id, 45, 33);
271 printf("%-45s %-6s %s%-12s %-12s%s%n", e ? e : id, load_state, on, active_state, sub_state, off, &a);
274 a -= strlen(on) + strlen(off);
277 printf(" => %-12s%n", job_type, &b);
281 if (a + b + 2 < columns()) {
285 printf(" %.*s", columns() - a - b - 2, description);
292 dbus_message_iter_next(&sub);
295 if (isatty(STDOUT_FILENO)) {
297 printf("\nLOAD = Load State, reflects whether the unit configuration was properly loaded.\n"
298 "ACTIVE = Active State, the high-level unit activation state, i.e. generalization of the substate.\n"
299 "SUB = Substate, the low-level unit activation state, possible values depend on unit type.\n"
300 "JOB = Job, shows pending jobs for the unit.\n");
303 printf("\n%u units listed.\n", k);
305 printf("\n%u units listed. Pass --all to see inactive units, too.\n", k);
312 dbus_message_unref(m);
315 dbus_message_unref(reply);
317 dbus_error_free(&error);
322 static int dot_one_property(const char *name, const char *prop, DBusMessageIter *iter) {
323 static const char * const colors[] = {
324 "Requires", "[color=\"black\"]",
325 "RequiresOverridable", "[color=\"black\"]",
326 "Requisite", "[color=\"darkblue\"]",
327 "RequisiteOverridable", "[color=\"darkblue\"]",
328 "Wants", "[color=\"darkgrey\"]",
329 "Conflicts", "[color=\"red\"]",
330 "ConflictedBy", "[color=\"red\"]",
331 "After", "[color=\"green\"]"
334 const char *c = NULL;
341 for (i = 0; i < ELEMENTSOF(colors); i += 2)
342 if (streq(colors[i], prop)) {
350 if (arg_dot != DOT_ALL)
351 if ((arg_dot == DOT_ORDER) != streq(prop, "After"))
354 switch (dbus_message_iter_get_arg_type(iter)) {
356 case DBUS_TYPE_ARRAY:
358 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
361 dbus_message_iter_recurse(iter, &sub);
363 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
366 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
367 dbus_message_iter_get_basic(&sub, &s);
368 printf("\t\"%s\"->\"%s\" %s;\n", name, s, c);
370 dbus_message_iter_next(&sub);
380 static int dot_one(DBusConnection *bus, const char *name, const char *path) {
381 DBusMessage *m = NULL, *reply = NULL;
382 const char *interface = "org.freedesktop.systemd1.Unit";
385 DBusMessageIter iter, sub, sub2, sub3;
390 dbus_error_init(&error);
392 if (!(m = dbus_message_new_method_call(
393 "org.freedesktop.systemd1",
395 "org.freedesktop.DBus.Properties",
397 log_error("Could not allocate message.");
402 if (!dbus_message_append_args(m,
403 DBUS_TYPE_STRING, &interface,
404 DBUS_TYPE_INVALID)) {
405 log_error("Could not append arguments to message.");
410 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
411 log_error("Failed to issue method call: %s", error.message);
416 if (!dbus_message_iter_init(reply, &iter) ||
417 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
418 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
419 log_error("Failed to parse reply.");
424 dbus_message_iter_recurse(&iter, &sub);
426 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
429 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
430 log_error("Failed to parse reply.");
435 dbus_message_iter_recurse(&sub, &sub2);
437 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &prop, true) < 0) {
438 log_error("Failed to parse reply.");
443 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
444 log_error("Failed to parse reply.");
449 dbus_message_iter_recurse(&sub2, &sub3);
451 if (dot_one_property(name, prop, &sub3)) {
452 log_error("Failed to parse reply.");
457 dbus_message_iter_next(&sub);
464 dbus_message_unref(m);
467 dbus_message_unref(reply);
469 dbus_error_free(&error);
474 static int dot(DBusConnection *bus, char **args, unsigned n) {
475 DBusMessage *m = NULL, *reply = NULL;
478 DBusMessageIter iter, sub, sub2;
480 dbus_error_init(&error);
484 if (!(m = dbus_message_new_method_call(
485 "org.freedesktop.systemd1",
486 "/org/freedesktop/systemd1",
487 "org.freedesktop.systemd1.Manager",
489 log_error("Could not allocate message.");
493 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
494 log_error("Failed to issue method call: %s", error.message);
499 if (!dbus_message_iter_init(reply, &iter) ||
500 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
501 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
502 log_error("Failed to parse reply.");
507 printf("digraph systemd {\n");
509 dbus_message_iter_recurse(&iter, &sub);
510 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
511 const char *id, *description, *load_state, *active_state, *sub_state, *following, *unit_path;
513 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
514 log_error("Failed to parse reply.");
519 dbus_message_iter_recurse(&sub, &sub2);
521 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &id, true) < 0 ||
522 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &description, true) < 0 ||
523 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &load_state, true) < 0 ||
524 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &active_state, true) < 0 ||
525 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &sub_state, true) < 0 ||
526 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &following, true) < 0 ||
527 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, true) < 0) {
528 log_error("Failed to parse reply.");
533 if ((r = dot_one(bus, id, unit_path)) < 0)
536 /* printf("\t\"%s\";\n", id); */
537 dbus_message_iter_next(&sub);
542 log_info(" Color legend: black = Requires\n"
543 " dark blue = Requisite\n"
544 " dark grey = Wants\n"
548 if (isatty(fileno(stdout)))
549 log_notice("-- You probably want to process this output with graphviz' dot tool.\n"
550 "-- Try a shell pipeline like 'systemctl dot | dot -Tsvg > systemd.svg'!\n");
556 dbus_message_unref(m);
559 dbus_message_unref(reply);
561 dbus_error_free(&error);
566 static int list_jobs(DBusConnection *bus, char **args, unsigned n) {
567 DBusMessage *m = NULL, *reply = NULL;
570 DBusMessageIter iter, sub, sub2;
573 dbus_error_init(&error);
577 if (!(m = dbus_message_new_method_call(
578 "org.freedesktop.systemd1",
579 "/org/freedesktop/systemd1",
580 "org.freedesktop.systemd1.Manager",
582 log_error("Could not allocate message.");
586 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
587 log_error("Failed to issue method call: %s", error.message);
592 if (!dbus_message_iter_init(reply, &iter) ||
593 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
594 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRUCT) {
595 log_error("Failed to parse reply.");
600 dbus_message_iter_recurse(&iter, &sub);
602 printf("%4s %-45s %-17s %-7s\n", "JOB", "UNIT", "TYPE", "STATE");
604 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
605 const char *name, *type, *state, *job_path, *unit_path;
609 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) {
610 log_error("Failed to parse reply.");
615 dbus_message_iter_recurse(&sub, &sub2);
617 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &id, true) < 0 ||
618 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0 ||
619 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) < 0 ||
620 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &state, true) < 0 ||
621 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &job_path, true) < 0 ||
622 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_OBJECT_PATH, &unit_path, false) < 0) {
623 log_error("Failed to parse reply.");
628 e = arg_full ? NULL : ellipsize(name, 45, 33);
629 printf("%4u %-45s %-17s %-7s\n", id, e ? e : name, type, state);
634 dbus_message_iter_next(&sub);
637 printf("\n%u jobs listed.\n", k);
642 dbus_message_unref(m);
645 dbus_message_unref(reply);
647 dbus_error_free(&error);
652 static int load_unit(DBusConnection *bus, char **args, unsigned n) {
653 DBusMessage *m = NULL, *reply = NULL;
658 dbus_error_init(&error);
663 for (i = 1; i < n; i++) {
665 if (!(m = dbus_message_new_method_call(
666 "org.freedesktop.systemd1",
667 "/org/freedesktop/systemd1",
668 "org.freedesktop.systemd1.Manager",
670 log_error("Could not allocate message.");
675 if (!dbus_message_append_args(m,
676 DBUS_TYPE_STRING, &args[i],
677 DBUS_TYPE_INVALID)) {
678 log_error("Could not append arguments to message.");
683 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
684 log_error("Failed to issue method call: %s", error.message);
689 dbus_message_unref(m);
690 dbus_message_unref(reply);
699 dbus_message_unref(m);
702 dbus_message_unref(reply);
704 dbus_error_free(&error);
709 static int cancel_job(DBusConnection *bus, char **args, unsigned n) {
710 DBusMessage *m = NULL, *reply = NULL;
715 dbus_error_init(&error);
721 return daemon_reload(bus, args, n);
723 for (i = 1; i < n; i++) {
727 if (!(m = dbus_message_new_method_call(
728 "org.freedesktop.systemd1",
729 "/org/freedesktop/systemd1",
730 "org.freedesktop.systemd1.Manager",
732 log_error("Could not allocate message.");
737 if ((r = safe_atou(args[i], &id)) < 0) {
738 log_error("Failed to parse job id: %s", strerror(-r));
742 assert_cc(sizeof(uint32_t) == sizeof(id));
743 if (!dbus_message_append_args(m,
744 DBUS_TYPE_UINT32, &id,
745 DBUS_TYPE_INVALID)) {
746 log_error("Could not append arguments to message.");
751 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
752 log_error("Failed to issue method call: %s", error.message);
757 if (!dbus_message_get_args(reply, &error,
758 DBUS_TYPE_OBJECT_PATH, &path,
759 DBUS_TYPE_INVALID)) {
760 log_error("Failed to parse reply: %s", error.message);
765 dbus_message_unref(m);
766 if (!(m = dbus_message_new_method_call(
767 "org.freedesktop.systemd1",
769 "org.freedesktop.systemd1.Job",
771 log_error("Could not allocate message.");
776 dbus_message_unref(reply);
777 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
778 log_error("Failed to issue method call: %s", error.message);
783 dbus_message_unref(m);
784 dbus_message_unref(reply);
792 dbus_message_unref(m);
795 dbus_message_unref(reply);
797 dbus_error_free(&error);
802 static bool need_daemon_reload(DBusConnection *bus, const char *unit) {
803 DBusMessage *m = NULL, *reply = NULL;
804 dbus_bool_t b = FALSE;
805 DBusMessageIter iter, sub;
807 *interface = "org.freedesktop.systemd1.Unit",
808 *property = "NeedDaemonReload",
811 /* We ignore all errors here, since this is used to show a warning only */
813 if (!(m = dbus_message_new_method_call(
814 "org.freedesktop.systemd1",
815 "/org/freedesktop/systemd1",
816 "org.freedesktop.systemd1.Manager",
820 if (!dbus_message_append_args(m,
821 DBUS_TYPE_STRING, &unit,
825 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
828 if (!dbus_message_get_args(reply, NULL,
829 DBUS_TYPE_OBJECT_PATH, &path,
833 dbus_message_unref(m);
834 if (!(m = dbus_message_new_method_call(
835 "org.freedesktop.systemd1",
837 "org.freedesktop.DBus.Properties",
841 if (!dbus_message_append_args(m,
842 DBUS_TYPE_STRING, &interface,
843 DBUS_TYPE_STRING, &property,
844 DBUS_TYPE_INVALID)) {
848 dbus_message_unref(reply);
849 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, NULL)))
852 if (!dbus_message_iter_init(reply, &iter) ||
853 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
856 dbus_message_iter_recurse(&iter, &sub);
858 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_BOOLEAN)
861 dbus_message_iter_get_basic(&sub, &b);
865 dbus_message_unref(m);
868 dbus_message_unref(reply);
873 typedef struct WaitData {
878 static DBusHandlerResult wait_filter(DBusConnection *connection, DBusMessage *message, void *data) {
886 dbus_error_init(&error);
888 log_debug("Got D-Bus request: %s.%s() on %s",
889 dbus_message_get_interface(message),
890 dbus_message_get_member(message),
891 dbus_message_get_path(message));
893 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
894 log_error("Warning! D-Bus connection terminated.");
895 dbus_connection_close(connection);
897 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
900 dbus_bool_t success = true;
902 if (!dbus_message_get_args(message, &error,
903 DBUS_TYPE_UINT32, &id,
904 DBUS_TYPE_OBJECT_PATH, &path,
905 DBUS_TYPE_BOOLEAN, &success,
907 log_error("Failed to parse message: %s", error.message);
911 if ((p = set_remove(d->set, (char*) path)))
919 dbus_error_free(&error);
920 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
923 static int enable_wait_for_jobs(DBusConnection *bus) {
931 dbus_error_init(&error);
932 dbus_bus_add_match(bus,
934 "sender='org.freedesktop.systemd1',"
935 "interface='org.freedesktop.systemd1.Manager',"
936 "member='JobRemoved',"
937 "path='/org/freedesktop/systemd1'",
940 if (dbus_error_is_set(&error)) {
941 log_error("Failed to add match: %s", error.message);
942 dbus_error_free(&error);
946 /* This is slightly dirty, since we don't undo the match registrations. */
950 static int wait_for_jobs(DBusConnection *bus, Set *s) {
961 if (!dbus_connection_add_filter(bus, wait_filter, &d, NULL)) {
962 log_error("Failed to add filter.");
967 while (!set_isempty(s) &&
968 dbus_connection_read_write_dispatch(bus, -1))
971 if (!arg_quiet && d.failed)
972 log_error("Job failed, see logs for details.");
974 r = d.failed ? -EIO : 0;
977 /* This is slightly dirty, since we don't undo the filter registration. */
982 static int start_unit_one(
989 DBusMessage *m = NULL, *reply = NULL;
998 assert(arg_no_block || s);
1000 dbus_error_init(&error);
1002 if (!(m = dbus_message_new_method_call(
1003 "org.freedesktop.systemd1",
1004 "/org/freedesktop/systemd1",
1005 "org.freedesktop.systemd1.Manager",
1007 log_error("Could not allocate message.");
1012 if (!dbus_message_append_args(m,
1013 DBUS_TYPE_STRING, &name,
1014 DBUS_TYPE_STRING, &mode,
1015 DBUS_TYPE_INVALID)) {
1016 log_error("Could not append arguments to message.");
1021 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1023 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
1024 /* There's always a fallback possible for
1025 * legacy actions. */
1030 log_error("Failed to issue method call: %s", error.message);
1035 if (!dbus_message_get_args(reply, &error,
1036 DBUS_TYPE_OBJECT_PATH, &path,
1037 DBUS_TYPE_INVALID)) {
1038 log_error("Failed to parse reply: %s", error.message);
1043 if (need_daemon_reload(bus, name))
1044 log_warning("Unit file of created job changed on disk, 'systemctl %s daemon-reload' recommended.",
1045 arg_session ? "--session" : "--system");
1047 if (!arg_no_block) {
1050 if (!(p = strdup(path))) {
1051 log_error("Failed to duplicate path.");
1056 if ((r = set_put(s, p)) < 0) {
1058 log_error("Failed to add path to set.");
1067 dbus_message_unref(m);
1070 dbus_message_unref(reply);
1072 dbus_error_free(&error);
1077 static enum action verb_to_action(const char *verb) {
1078 if (streq(verb, "halt"))
1080 else if (streq(verb, "poweroff"))
1081 return ACTION_POWEROFF;
1082 else if (streq(verb, "reboot"))
1083 return ACTION_REBOOT;
1084 else if (streq(verb, "rescue"))
1085 return ACTION_RESCUE;
1086 else if (streq(verb, "emergency"))
1087 return ACTION_EMERGENCY;
1088 else if (streq(verb, "default"))
1089 return ACTION_DEFAULT;
1091 return ACTION_INVALID;
1094 static int start_unit(DBusConnection *bus, char **args, unsigned n) {
1096 static const char * const table[_ACTION_MAX] = {
1097 [ACTION_HALT] = SPECIAL_HALT_TARGET,
1098 [ACTION_POWEROFF] = SPECIAL_POWEROFF_TARGET,
1099 [ACTION_REBOOT] = SPECIAL_REBOOT_TARGET,
1100 [ACTION_RUNLEVEL2] = SPECIAL_RUNLEVEL2_TARGET,
1101 [ACTION_RUNLEVEL3] = SPECIAL_RUNLEVEL3_TARGET,
1102 [ACTION_RUNLEVEL4] = SPECIAL_RUNLEVEL4_TARGET,
1103 [ACTION_RUNLEVEL5] = SPECIAL_RUNLEVEL5_TARGET,
1104 [ACTION_RESCUE] = SPECIAL_RESCUE_TARGET,
1105 [ACTION_EMERGENCY] = SPECIAL_EMERGENCY_TARGET,
1106 [ACTION_DEFAULT] = SPECIAL_DEFAULT_TARGET
1111 const char *method, *mode, *one_name;
1116 if (arg_action == ACTION_SYSTEMCTL) {
1118 streq(args[0], "stop") ? "StopUnit" :
1119 streq(args[0], "reload") ? "ReloadUnit" :
1120 streq(args[0], "restart") ? "RestartUnit" :
1121 streq(args[0], "try-restart") ? "TryRestartUnit" :
1122 streq(args[0], "reload-or-restart") ? "ReloadOrRestartUnit" :
1123 streq(args[0], "reload-or-try-restart") ||
1124 streq(args[0], "force-reload") ||
1125 streq(args[0], "condrestart") ? "ReloadOrTryRestartUnit" :
1129 (streq(args[0], "isolate") ||
1130 streq(args[0], "rescue") ||
1131 streq(args[0], "emergency")) ? "isolate" :
1135 one_name = table[verb_to_action(args[0])];
1138 assert(arg_action < ELEMENTSOF(table));
1139 assert(table[arg_action]);
1141 method = "StartUnit";
1143 mode = (arg_action == ACTION_EMERGENCY ||
1144 arg_action == ACTION_RESCUE) ? "isolate" : "replace";
1146 one_name = table[arg_action];
1149 if (!arg_no_block) {
1150 if ((r = enable_wait_for_jobs(bus)) < 0) {
1151 log_error("Could not watch jobs: %s", strerror(-r));
1155 if (!(s = set_new(string_hash_func, string_compare_func))) {
1156 log_error("Failed to allocate set.");
1165 if ((r = start_unit_one(bus, method, one_name, mode, s)) <= 0)
1168 for (i = 1; i < n; i++)
1169 if ((r = start_unit_one(bus, method, args[i], mode, s)) < 0)
1174 r = wait_for_jobs(bus, s);
1183 static int start_special(DBusConnection *bus, char **args, unsigned n) {
1189 r = start_unit(bus, args, n);
1192 warn_wall(verb_to_action(args[0]));
1197 static int check_unit(DBusConnection *bus, char **args, unsigned n) {
1198 DBusMessage *m = NULL, *reply = NULL;
1200 *interface = "org.freedesktop.systemd1.Unit",
1201 *property = "ActiveState";
1202 int r = -EADDRNOTAVAIL;
1209 dbus_error_init(&error);
1211 for (i = 1; i < n; i++) {
1212 const char *path = NULL;
1214 DBusMessageIter iter, sub;
1216 if (!(m = dbus_message_new_method_call(
1217 "org.freedesktop.systemd1",
1218 "/org/freedesktop/systemd1",
1219 "org.freedesktop.systemd1.Manager",
1221 log_error("Could not allocate message.");
1226 if (!dbus_message_append_args(m,
1227 DBUS_TYPE_STRING, &args[i],
1228 DBUS_TYPE_INVALID)) {
1229 log_error("Could not append arguments to message.");
1234 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1236 /* Hmm, cannot figure out anything about this unit... */
1240 dbus_error_free(&error);
1244 if (!dbus_message_get_args(reply, &error,
1245 DBUS_TYPE_OBJECT_PATH, &path,
1246 DBUS_TYPE_INVALID)) {
1247 log_error("Failed to parse reply: %s", error.message);
1252 dbus_message_unref(m);
1253 if (!(m = dbus_message_new_method_call(
1254 "org.freedesktop.systemd1",
1256 "org.freedesktop.DBus.Properties",
1258 log_error("Could not allocate message.");
1263 if (!dbus_message_append_args(m,
1264 DBUS_TYPE_STRING, &interface,
1265 DBUS_TYPE_STRING, &property,
1266 DBUS_TYPE_INVALID)) {
1267 log_error("Could not append arguments to message.");
1272 dbus_message_unref(reply);
1273 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1274 log_error("Failed to issue method call: %s", error.message);
1279 if (!dbus_message_iter_init(reply, &iter) ||
1280 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1281 log_error("Failed to parse reply.");
1286 dbus_message_iter_recurse(&iter, &sub);
1288 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1289 log_error("Failed to parse reply.");
1294 dbus_message_iter_get_basic(&sub, &state);
1299 if (streq(state, "active") || startswith(state, "reloading"))
1302 dbus_message_unref(m);
1303 dbus_message_unref(reply);
1309 dbus_message_unref(m);
1312 dbus_message_unref(reply);
1314 dbus_error_free(&error);
1319 typedef struct ExecStatusInfo {
1325 usec_t start_timestamp;
1326 usec_t exit_timestamp;
1331 LIST_FIELDS(struct ExecStatusInfo, exec);
1334 static void exec_status_info_free(ExecStatusInfo *i) {
1342 static int exec_status_info_deserialize(DBusMessageIter *sub, ExecStatusInfo *i) {
1343 uint64_t start_timestamp, exit_timestamp;
1344 DBusMessageIter sub2, sub3;
1348 int32_t code, status;
1354 if (dbus_message_iter_get_arg_type(sub) != DBUS_TYPE_STRUCT)
1357 dbus_message_iter_recurse(sub, &sub2);
1359 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
1362 if (!(i->path = strdup(path)))
1365 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
1366 dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
1370 dbus_message_iter_recurse(&sub2, &sub3);
1371 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1372 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1373 dbus_message_iter_next(&sub3);
1378 if (!(i->argv = new0(char*, n+1)))
1382 dbus_message_iter_recurse(&sub2, &sub3);
1383 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1386 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1387 dbus_message_iter_get_basic(&sub3, &s);
1388 dbus_message_iter_next(&sub3);
1390 if (!(i->argv[n++] = strdup(s)))
1394 if (!dbus_message_iter_next(&sub2) ||
1395 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, true) < 0 ||
1396 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp, true) < 0 ||
1397 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp, true) < 0 ||
1398 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) < 0 ||
1399 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) < 0 ||
1400 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) < 0)
1404 i->start_timestamp = (usec_t) start_timestamp;
1405 i->exit_timestamp = (usec_t) exit_timestamp;
1406 i->pid = (pid_t) pid;
1413 typedef struct UnitStatusInfo {
1415 const char *load_state;
1416 const char *active_state;
1417 const char *sub_state;
1419 const char *description;
1421 const char *fragment_path;
1422 const char *default_control_group;
1424 bool need_daemon_reload;
1429 const char *status_text;
1432 usec_t start_timestamp;
1433 usec_t exit_timestamp;
1435 int exit_code, exit_status;
1438 unsigned n_accepted;
1439 unsigned n_connections;
1443 const char *sysfs_path;
1445 /* Mount, Automount */
1451 LIST_HEAD(ExecStatusInfo, exec);
1454 static void print_status_info(UnitStatusInfo *i) {
1456 const char *on, *off, *ss;
1460 /* This shows pretty information about a unit. See
1461 * print_property() for a low-level property printer */
1463 printf("%s", strna(i->id));
1465 if (i->description && !streq_ptr(i->id, i->description))
1466 printf(" - %s", i->description);
1470 if (i->fragment_path)
1471 printf("\t Loaded: %s (%s)\n", strna(i->load_state), i->fragment_path);
1472 else if (streq_ptr(i->load_state, "failed"))
1473 printf("\t Loaded: %s%s%s\n",
1474 ansi_highlight(true),
1475 strna(i->load_state),
1476 ansi_highlight(false));
1478 printf("\t Loaded: %s\n", strna(i->load_state));
1480 ss = streq_ptr(i->active_state, i->sub_state) ? NULL : i->sub_state;
1482 if (streq_ptr(i->active_state, "maintenance")) {
1483 on = ansi_highlight(true);
1484 off = ansi_highlight(false);
1485 } else if (streq_ptr(i->active_state, "active") || streq_ptr(i->active_state, "reloading")) {
1486 on = ansi_highlight_green(true);
1487 off = ansi_highlight_green(false);
1492 printf("\t Active: %s%s (%s)%s\n",
1494 strna(i->active_state),
1498 printf("\t Active: %s%s%s\n",
1500 strna(i->active_state),
1504 printf("\t Device: %s\n", i->sysfs_path);
1506 printf("\t Where: %s\n", i->where);
1508 printf("\t What: %s\n", i->what);
1511 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
1513 LIST_FOREACH(exec, p, i->exec) {
1516 /* Only show exited processes here */
1520 t = strv_join(p->argv, " ");
1521 printf("\t Exited: %u (%s, code=%s, ", p->pid, strna(t), sigchld_code_to_string(p->code));
1524 if (p->code == CLD_EXITED)
1525 printf("status=%i", p->status);
1527 printf("signal=%s", signal_to_string(p->status));
1530 if (i->main_pid == p->pid &&
1531 i->start_timestamp == p->start_timestamp &&
1532 i->exit_timestamp == p->start_timestamp)
1533 /* Let's not show this twice */
1536 if (p->pid == i->control_pid)
1540 if (i->main_pid > 0 || i->control_pid > 0) {
1543 if (i->main_pid > 0) {
1544 printf(" Main: %u", (unsigned) i->main_pid);
1548 get_process_name(i->main_pid, &t);
1553 } else if (i->exit_code > 0) {
1554 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
1556 if (i->exit_code == CLD_EXITED)
1557 printf("status=%i", i->exit_status);
1559 printf("signal=%s", signal_to_string(i->exit_status));
1564 if (i->main_pid > 0 && i->control_pid > 0)
1567 if (i->control_pid > 0) {
1570 printf(" Control: %u", (unsigned) i->control_pid);
1572 get_process_name(i->control_pid, &t);
1583 printf("\t Status: \"%s\"\n", i->status_text);
1585 if (i->default_control_group) {
1588 printf("\t CGroup: %s\n", i->default_control_group);
1590 if ((c = columns()) > 18)
1595 show_cgroup_by_path(i->default_control_group, "\t\t ", c);
1598 if (i->need_daemon_reload)
1599 printf("\n%sWarning:%s Unit file changed on disk, 'systemctl %s daemon-reload' recommended.\n",
1600 ansi_highlight(true),
1601 ansi_highlight(false),
1602 arg_session ? "--session" : "--system");
1605 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
1607 switch (dbus_message_iter_get_arg_type(iter)) {
1609 case DBUS_TYPE_STRING: {
1612 dbus_message_iter_get_basic(iter, &s);
1615 if (streq(name, "Id"))
1617 else if (streq(name, "LoadState"))
1619 else if (streq(name, "ActiveState"))
1620 i->active_state = s;
1621 else if (streq(name, "SubState"))
1623 else if (streq(name, "Description"))
1625 else if (streq(name, "FragmentPath"))
1626 i->fragment_path = s;
1627 else if (streq(name, "DefaultControlGroup"))
1628 i->default_control_group = s;
1629 else if (streq(name, "StatusText"))
1631 else if (streq(name, "SysFSPath"))
1633 else if (streq(name, "Where"))
1635 else if (streq(name, "What"))
1642 case DBUS_TYPE_BOOLEAN: {
1645 dbus_message_iter_get_basic(iter, &b);
1647 if (streq(name, "Accept"))
1649 else if (streq(name, "NeedDaemonReload"))
1650 i->need_daemon_reload = b;
1655 case DBUS_TYPE_UINT32: {
1658 dbus_message_iter_get_basic(iter, &u);
1660 if (streq(name, "MainPID")) {
1662 i->main_pid = (pid_t) u;
1665 } else if (streq(name, "ControlPID"))
1666 i->control_pid = (pid_t) u;
1667 else if (streq(name, "ExecMainPID")) {
1669 i->main_pid = (pid_t) u;
1670 } else if (streq(name, "NAccepted"))
1672 else if (streq(name, "NConnections"))
1673 i->n_connections = u;
1678 case DBUS_TYPE_INT32: {
1681 dbus_message_iter_get_basic(iter, &j);
1683 if (streq(name, "ExecMainCode"))
1684 i->exit_code = (int) j;
1685 else if (streq(name, "ExecMainStatus"))
1686 i->exit_status = (int) j;
1691 case DBUS_TYPE_UINT64: {
1694 dbus_message_iter_get_basic(iter, &u);
1696 if (streq(name, "ExecMainStartTimestamp"))
1697 i->start_timestamp = (usec_t) u;
1698 else if (streq(name, "ExecMainExitTimestamp"))
1699 i->exit_timestamp = (usec_t) u;
1704 case DBUS_TYPE_ARRAY: {
1706 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT &&
1707 startswith(name, "Exec")) {
1708 DBusMessageIter sub;
1710 dbus_message_iter_recurse(iter, &sub);
1711 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1712 ExecStatusInfo *info;
1715 if (!(info = new0(ExecStatusInfo, 1)))
1718 if ((r = exec_status_info_deserialize(&sub, info)) < 0) {
1723 LIST_PREPEND(ExecStatusInfo, exec, i->exec, info);
1725 dbus_message_iter_next(&sub);
1736 static int print_property(const char *name, DBusMessageIter *iter) {
1740 /* This is a low-level property printer, see
1741 * print_status_info() for the nicer output */
1743 if (arg_property && !strv_find(arg_property, name))
1746 switch (dbus_message_iter_get_arg_type(iter)) {
1748 case DBUS_TYPE_STRING: {
1750 dbus_message_iter_get_basic(iter, &s);
1752 if (arg_all || s[0])
1753 printf("%s=%s\n", name, s);
1758 case DBUS_TYPE_BOOLEAN: {
1760 dbus_message_iter_get_basic(iter, &b);
1761 printf("%s=%s\n", name, yes_no(b));
1766 case DBUS_TYPE_UINT64: {
1768 dbus_message_iter_get_basic(iter, &u);
1770 /* Yes, heuristics! But we can change this check
1771 * should it turn out to not be sufficient */
1773 if (strstr(name, "Timestamp")) {
1774 char timestamp[FORMAT_TIMESTAMP_MAX], *t;
1776 if ((t = format_timestamp(timestamp, sizeof(timestamp), u)) || arg_all)
1777 printf("%s=%s\n", name, strempty(t));
1778 } else if (strstr(name, "USec")) {
1779 char timespan[FORMAT_TIMESPAN_MAX];
1781 printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u));
1783 printf("%s=%llu\n", name, (unsigned long long) u);
1788 case DBUS_TYPE_UINT32: {
1790 dbus_message_iter_get_basic(iter, &u);
1792 if (strstr(name, "UMask") || strstr(name, "Mode"))
1793 printf("%s=%04o\n", name, u);
1795 printf("%s=%u\n", name, (unsigned) u);
1800 case DBUS_TYPE_INT32: {
1802 dbus_message_iter_get_basic(iter, &i);
1804 printf("%s=%i\n", name, (int) i);
1808 case DBUS_TYPE_STRUCT: {
1809 DBusMessageIter sub;
1810 dbus_message_iter_recurse(iter, &sub);
1812 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
1815 dbus_message_iter_get_basic(&sub, &u);
1818 printf("%s=%u\n", name, (unsigned) u);
1820 printf("%s=\n", name);
1823 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
1826 dbus_message_iter_get_basic(&sub, &s);
1828 if (arg_all || s[0])
1829 printf("%s=%s\n", name, s);
1837 case DBUS_TYPE_ARRAY:
1839 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
1840 DBusMessageIter sub;
1843 dbus_message_iter_recurse(iter, &sub);
1845 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1846 printf("%s=", name);
1848 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1851 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
1852 dbus_message_iter_get_basic(&sub, &s);
1853 printf("%s%s", space ? " " : "", s);
1856 dbus_message_iter_next(&sub);
1864 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_BYTE) {
1865 DBusMessageIter sub;
1867 dbus_message_iter_recurse(iter, &sub);
1869 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1870 printf("%s=", name);
1872 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1875 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_BYTE);
1876 dbus_message_iter_get_basic(&sub, &u);
1879 dbus_message_iter_next(&sub);
1887 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
1888 DBusMessageIter sub, sub2;
1890 dbus_message_iter_recurse(iter, &sub);
1891 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1892 const char *type, *path;
1894 dbus_message_iter_recurse(&sub, &sub2);
1896 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
1897 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
1898 printf("%s=%s\n", type, path);
1900 dbus_message_iter_next(&sub);
1905 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
1906 DBusMessageIter sub, sub2;
1908 dbus_message_iter_recurse(iter, &sub);
1909 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1911 uint64_t value, next_elapse;
1913 dbus_message_iter_recurse(&sub, &sub2);
1915 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
1916 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
1917 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
1918 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
1920 printf("%s={ value=%s ; next_elapse=%s }\n",
1922 format_timespan(timespan1, sizeof(timespan1), value),
1923 format_timespan(timespan2, sizeof(timespan2), next_elapse));
1926 dbus_message_iter_next(&sub);
1931 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
1932 DBusMessageIter sub;
1934 dbus_message_iter_recurse(iter, &sub);
1935 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1936 ExecStatusInfo info;
1939 if (exec_status_info_deserialize(&sub, &info) >= 0) {
1940 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
1943 t = strv_join(info.argv, " ");
1945 printf("%s={ path=%s ; argv[]=%s ; ignore=%s ; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n",
1949 yes_no(info.ignore),
1950 strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)),
1951 strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)),
1952 (unsigned) info. pid,
1953 sigchld_code_to_string(info.code),
1955 info.code == CLD_EXITED ? "" : "/",
1956 strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status)));
1962 strv_free(info.argv);
1964 dbus_message_iter_next(&sub);
1974 printf("%s=[unprintable]\n", name);
1979 static int show_one(DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
1980 DBusMessage *m = NULL, *reply = NULL;
1981 const char *interface = "";
1984 DBusMessageIter iter, sub, sub2, sub3;
1985 UnitStatusInfo info;
1993 dbus_error_init(&error);
1995 if (!(m = dbus_message_new_method_call(
1996 "org.freedesktop.systemd1",
1998 "org.freedesktop.DBus.Properties",
2000 log_error("Could not allocate message.");
2005 if (!dbus_message_append_args(m,
2006 DBUS_TYPE_STRING, &interface,
2007 DBUS_TYPE_INVALID)) {
2008 log_error("Could not append arguments to message.");
2013 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2014 log_error("Failed to issue method call: %s", error.message);
2019 if (!dbus_message_iter_init(reply, &iter) ||
2020 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
2021 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
2022 log_error("Failed to parse reply.");
2027 dbus_message_iter_recurse(&iter, &sub);
2034 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2037 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
2038 log_error("Failed to parse reply.");
2043 dbus_message_iter_recurse(&sub, &sub2);
2045 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
2046 log_error("Failed to parse reply.");
2051 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
2052 log_error("Failed to parse reply.");
2057 dbus_message_iter_recurse(&sub2, &sub3);
2059 if (show_properties)
2060 r = print_property(name, &sub3);
2062 r = status_property(name, &sub3, &info);
2065 log_error("Failed to parse reply.");
2070 dbus_message_iter_next(&sub);
2073 if (!show_properties)
2074 print_status_info(&info);
2076 while ((p = info.exec)) {
2077 LIST_REMOVE(ExecStatusInfo, exec, info.exec, p);
2078 exec_status_info_free(p);
2085 dbus_message_unref(m);
2088 dbus_message_unref(reply);
2090 dbus_error_free(&error);
2095 static int show(DBusConnection *bus, char **args, unsigned n) {
2096 DBusMessage *m = NULL, *reply = NULL;
2100 bool show_properties, new_line = false;
2105 dbus_error_init(&error);
2107 show_properties = !streq(args[0], "status");
2109 if (show_properties && n <= 1) {
2110 /* If not argument is specified inspect the manager
2113 r = show_one(bus, "/org/freedesktop/systemd1", show_properties, &new_line);
2117 for (i = 1; i < n; i++) {
2118 const char *path = NULL;
2121 if (!show_properties || safe_atou32(args[i], &id) < 0) {
2123 if (!(m = dbus_message_new_method_call(
2124 "org.freedesktop.systemd1",
2125 "/org/freedesktop/systemd1",
2126 "org.freedesktop.systemd1.Manager",
2128 log_error("Could not allocate message.");
2133 if (!dbus_message_append_args(m,
2134 DBUS_TYPE_STRING, &args[i],
2135 DBUS_TYPE_INVALID)) {
2136 log_error("Could not append arguments to message.");
2141 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2143 if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
2144 log_error("Failed to issue method call: %s", error.message);
2149 dbus_error_free(&error);
2151 dbus_message_unref(m);
2152 if (!(m = dbus_message_new_method_call(
2153 "org.freedesktop.systemd1",
2154 "/org/freedesktop/systemd1",
2155 "org.freedesktop.systemd1.Manager",
2157 log_error("Could not allocate message.");
2162 if (!dbus_message_append_args(m,
2163 DBUS_TYPE_STRING, &args[i],
2164 DBUS_TYPE_INVALID)) {
2165 log_error("Could not append arguments to message.");
2170 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2171 log_error("Failed to issue method call: %s", error.message);
2179 if (!(m = dbus_message_new_method_call(
2180 "org.freedesktop.systemd1",
2181 "/org/freedesktop/systemd1",
2182 "org.freedesktop.systemd1.Manager",
2184 log_error("Could not allocate message.");
2189 if (!dbus_message_append_args(m,
2190 DBUS_TYPE_UINT32, &id,
2191 DBUS_TYPE_INVALID)) {
2192 log_error("Could not append arguments to message.");
2197 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2198 log_error("Failed to issue method call: %s", error.message);
2204 if (!dbus_message_get_args(reply, &error,
2205 DBUS_TYPE_OBJECT_PATH, &path,
2206 DBUS_TYPE_INVALID)) {
2207 log_error("Failed to parse reply: %s", error.message);
2212 if ((r = show_one(bus, path, show_properties, &new_line)) < 0)
2215 dbus_message_unref(m);
2216 dbus_message_unref(reply);
2224 dbus_message_unref(m);
2227 dbus_message_unref(reply);
2229 dbus_error_free(&error);
2234 static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
2236 DBusMessage *m = NULL, *reply = NULL;
2241 dbus_error_init(&error);
2243 log_debug("Got D-Bus request: %s.%s() on %s",
2244 dbus_message_get_interface(message),
2245 dbus_message_get_member(message),
2246 dbus_message_get_path(message));
2248 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
2249 log_error("Warning! D-Bus connection terminated.");
2250 dbus_connection_close(connection);
2252 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") ||
2253 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
2254 const char *id, *path;
2256 if (!dbus_message_get_args(message, &error,
2257 DBUS_TYPE_STRING, &id,
2258 DBUS_TYPE_OBJECT_PATH, &path,
2260 log_error("Failed to parse message: %s", error.message);
2261 else if (streq(dbus_message_get_member(message), "UnitNew"))
2262 printf("Unit %s added.\n", id);
2264 printf("Unit %s removed.\n", id);
2266 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew") ||
2267 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
2271 if (!dbus_message_get_args(message, &error,
2272 DBUS_TYPE_UINT32, &id,
2273 DBUS_TYPE_OBJECT_PATH, &path,
2275 log_error("Failed to parse message: %s", error.message);
2276 else if (streq(dbus_message_get_member(message), "JobNew"))
2277 printf("Job %u added.\n", id);
2279 printf("Job %u removed.\n", id);
2282 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Unit", "Changed") ||
2283 dbus_message_is_signal(message, "org.freedesktop.systemd1.Job", "Changed")) {
2285 const char *path, *interface, *property = "Id";
2286 DBusMessageIter iter, sub;
2288 path = dbus_message_get_path(message);
2289 interface = dbus_message_get_interface(message);
2291 if (!(m = dbus_message_new_method_call(
2292 "org.freedesktop.systemd1",
2294 "org.freedesktop.DBus.Properties",
2296 log_error("Could not allocate message.");
2300 if (!dbus_message_append_args(m,
2301 DBUS_TYPE_STRING, &interface,
2302 DBUS_TYPE_STRING, &property,
2303 DBUS_TYPE_INVALID)) {
2304 log_error("Could not append arguments to message.");
2308 if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
2309 log_error("Failed to issue method call: %s", error.message);
2313 if (!dbus_message_iter_init(reply, &iter) ||
2314 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2315 log_error("Failed to parse reply.");
2319 dbus_message_iter_recurse(&iter, &sub);
2321 if (streq(interface, "org.freedesktop.systemd1.Unit")) {
2324 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2325 log_error("Failed to parse reply.");
2329 dbus_message_iter_get_basic(&sub, &id);
2330 printf("Unit %s changed.\n", id);
2334 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32) {
2335 log_error("Failed to parse reply.");
2339 dbus_message_iter_get_basic(&sub, &id);
2340 printf("Job %u changed.\n", id);
2346 dbus_message_unref(m);
2349 dbus_message_unref(reply);
2351 dbus_error_free(&error);
2352 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2356 dbus_message_unref(m);
2359 dbus_message_unref(reply);
2361 dbus_error_free(&error);
2362 return DBUS_HANDLER_RESULT_NEED_MEMORY;
2365 static int monitor(DBusConnection *bus, char **args, unsigned n) {
2366 DBusMessage *m = NULL, *reply = NULL;
2370 dbus_error_init(&error);
2373 dbus_bus_add_match(bus,
2375 "sender='org.freedesktop.systemd1',"
2376 "interface='org.freedesktop.systemd1.Manager',"
2377 "path='/org/freedesktop/systemd1'",
2380 if (dbus_error_is_set(&error)) {
2381 log_error("Failed to add match: %s", error.message);
2386 dbus_bus_add_match(bus,
2388 "sender='org.freedesktop.systemd1',"
2389 "interface='org.freedesktop.systemd1.Unit',"
2393 if (dbus_error_is_set(&error)) {
2394 log_error("Failed to add match: %s", error.message);
2399 dbus_bus_add_match(bus,
2401 "sender='org.freedesktop.systemd1',"
2402 "interface='org.freedesktop.systemd1.Job',"
2406 if (dbus_error_is_set(&error)) {
2407 log_error("Failed to add match: %s", error.message);
2413 if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
2414 log_error("Failed to add filter.");
2419 if (!(m = dbus_message_new_method_call(
2420 "org.freedesktop.systemd1",
2421 "/org/freedesktop/systemd1",
2422 "org.freedesktop.systemd1.Manager",
2424 log_error("Could not allocate message.");
2429 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2430 log_error("Failed to issue method call: %s", error.message);
2435 while (dbus_connection_read_write_dispatch(bus, -1))
2442 /* This is slightly dirty, since we don't undo the filter or the matches. */
2445 dbus_message_unref(m);
2448 dbus_message_unref(reply);
2450 dbus_error_free(&error);
2455 static int dump(DBusConnection *bus, char **args, unsigned n) {
2456 DBusMessage *m = NULL, *reply = NULL;
2461 dbus_error_init(&error);
2463 if (!(m = dbus_message_new_method_call(
2464 "org.freedesktop.systemd1",
2465 "/org/freedesktop/systemd1",
2466 "org.freedesktop.systemd1.Manager",
2468 log_error("Could not allocate message.");
2472 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2473 log_error("Failed to issue method call: %s", error.message);
2478 if (!dbus_message_get_args(reply, &error,
2479 DBUS_TYPE_STRING, &text,
2480 DBUS_TYPE_INVALID)) {
2481 log_error("Failed to parse reply: %s", error.message);
2486 fputs(text, stdout);
2492 dbus_message_unref(m);
2495 dbus_message_unref(reply);
2497 dbus_error_free(&error);
2502 static int snapshot(DBusConnection *bus, char **args, unsigned n) {
2503 DBusMessage *m = NULL, *reply = NULL;
2506 const char *name = "", *path, *id;
2507 dbus_bool_t cleanup = FALSE;
2508 DBusMessageIter iter, sub;
2510 *interface = "org.freedesktop.systemd1.Unit",
2513 dbus_error_init(&error);
2515 if (!(m = dbus_message_new_method_call(
2516 "org.freedesktop.systemd1",
2517 "/org/freedesktop/systemd1",
2518 "org.freedesktop.systemd1.Manager",
2519 "CreateSnapshot"))) {
2520 log_error("Could not allocate message.");
2527 if (!dbus_message_append_args(m,
2528 DBUS_TYPE_STRING, &name,
2529 DBUS_TYPE_BOOLEAN, &cleanup,
2530 DBUS_TYPE_INVALID)) {
2531 log_error("Could not append arguments to message.");
2536 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2537 log_error("Failed to issue method call: %s", error.message);
2542 if (!dbus_message_get_args(reply, &error,
2543 DBUS_TYPE_OBJECT_PATH, &path,
2544 DBUS_TYPE_INVALID)) {
2545 log_error("Failed to parse reply: %s", error.message);
2550 dbus_message_unref(m);
2551 if (!(m = dbus_message_new_method_call(
2552 "org.freedesktop.systemd1",
2554 "org.freedesktop.DBus.Properties",
2556 log_error("Could not allocate message.");
2560 if (!dbus_message_append_args(m,
2561 DBUS_TYPE_STRING, &interface,
2562 DBUS_TYPE_STRING, &property,
2563 DBUS_TYPE_INVALID)) {
2564 log_error("Could not append arguments to message.");
2569 dbus_message_unref(reply);
2570 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2571 log_error("Failed to issue method call: %s", error.message);
2576 if (!dbus_message_iter_init(reply, &iter) ||
2577 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2578 log_error("Failed to parse reply.");
2583 dbus_message_iter_recurse(&iter, &sub);
2585 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2586 log_error("Failed to parse reply.");
2591 dbus_message_iter_get_basic(&sub, &id);
2599 dbus_message_unref(m);
2602 dbus_message_unref(reply);
2604 dbus_error_free(&error);
2609 static int delete_snapshot(DBusConnection *bus, char **args, unsigned n) {
2610 DBusMessage *m = NULL, *reply = NULL;
2618 dbus_error_init(&error);
2620 for (i = 1; i < n; i++) {
2621 const char *path = NULL;
2623 if (!(m = dbus_message_new_method_call(
2624 "org.freedesktop.systemd1",
2625 "/org/freedesktop/systemd1",
2626 "org.freedesktop.systemd1.Manager",
2628 log_error("Could not allocate message.");
2633 if (!dbus_message_append_args(m,
2634 DBUS_TYPE_STRING, &args[i],
2635 DBUS_TYPE_INVALID)) {
2636 log_error("Could not append arguments to message.");
2641 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2642 log_error("Failed to issue method call: %s", error.message);
2647 if (!dbus_message_get_args(reply, &error,
2648 DBUS_TYPE_OBJECT_PATH, &path,
2649 DBUS_TYPE_INVALID)) {
2650 log_error("Failed to parse reply: %s", error.message);
2655 dbus_message_unref(m);
2656 if (!(m = dbus_message_new_method_call(
2657 "org.freedesktop.systemd1",
2659 "org.freedesktop.systemd1.Snapshot",
2661 log_error("Could not allocate message.");
2666 dbus_message_unref(reply);
2667 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2668 log_error("Failed to issue method call: %s", error.message);
2673 dbus_message_unref(m);
2674 dbus_message_unref(reply);
2682 dbus_message_unref(m);
2685 dbus_message_unref(reply);
2687 dbus_error_free(&error);
2692 static int daemon_reload(DBusConnection *bus, char **args, unsigned n) {
2693 DBusMessage *m = NULL, *reply = NULL;
2698 dbus_error_init(&error);
2700 if (arg_action == ACTION_RELOAD)
2702 else if (arg_action == ACTION_REEXEC)
2703 method = "Reexecute";
2705 assert(arg_action == ACTION_SYSTEMCTL);
2708 streq(args[0], "clear-jobs") ||
2709 streq(args[0], "cancel") ? "ClearJobs" :
2710 streq(args[0], "daemon-reexec") ? "Reexecute" :
2711 streq(args[0], "reset-maintenance") ? "ResetMaintenance" :
2712 streq(args[0], "daemon-exit") ? "Exit" :
2716 if (!(m = dbus_message_new_method_call(
2717 "org.freedesktop.systemd1",
2718 "/org/freedesktop/systemd1",
2719 "org.freedesktop.systemd1.Manager",
2721 log_error("Could not allocate message.");
2725 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2727 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
2728 /* There's always a fallback possible for
2729 * legacy actions. */
2734 log_error("Failed to issue method call: %s", error.message);
2743 dbus_message_unref(m);
2746 dbus_message_unref(reply);
2748 dbus_error_free(&error);
2753 static int reset_maintenance(DBusConnection *bus, char **args, unsigned n) {
2754 DBusMessage *m = NULL, *reply = NULL;
2760 dbus_error_init(&error);
2763 return daemon_reload(bus, args, n);
2765 for (i = 1; i < n; i++) {
2767 if (!(m = dbus_message_new_method_call(
2768 "org.freedesktop.systemd1",
2769 "/org/freedesktop/systemd1",
2770 "org.freedesktop.systemd1.Manager",
2771 "ResetMaintenanceUnit"))) {
2772 log_error("Could not allocate message.");
2777 if (!dbus_message_append_args(m,
2778 DBUS_TYPE_STRING, args + i,
2779 DBUS_TYPE_INVALID)) {
2780 log_error("Could not append arguments to message.");
2785 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2786 log_error("Failed to issue method call: %s", error.message);
2791 dbus_message_unref(m);
2792 dbus_message_unref(reply);
2800 dbus_message_unref(m);
2803 dbus_message_unref(reply);
2805 dbus_error_free(&error);
2810 static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
2811 DBusMessage *m = NULL, *reply = NULL;
2813 DBusMessageIter iter, sub, sub2;
2816 *interface = "org.freedesktop.systemd1.Manager",
2817 *property = "Environment";
2819 dbus_error_init(&error);
2821 if (!(m = dbus_message_new_method_call(
2822 "org.freedesktop.systemd1",
2823 "/org/freedesktop/systemd1",
2824 "org.freedesktop.DBus.Properties",
2826 log_error("Could not allocate message.");
2830 if (!dbus_message_append_args(m,
2831 DBUS_TYPE_STRING, &interface,
2832 DBUS_TYPE_STRING, &property,
2833 DBUS_TYPE_INVALID)) {
2834 log_error("Could not append arguments to message.");
2839 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2840 log_error("Failed to issue method call: %s", error.message);
2845 if (!dbus_message_iter_init(reply, &iter) ||
2846 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2847 log_error("Failed to parse reply.");
2852 dbus_message_iter_recurse(&iter, &sub);
2854 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
2855 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
2856 log_error("Failed to parse reply.");
2861 dbus_message_iter_recurse(&sub, &sub2);
2863 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
2866 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
2867 log_error("Failed to parse reply.");
2872 dbus_message_iter_get_basic(&sub2, &text);
2873 printf("%s\n", text);
2875 dbus_message_iter_next(&sub2);
2882 dbus_message_unref(m);
2885 dbus_message_unref(reply);
2887 dbus_error_free(&error);
2892 static int set_environment(DBusConnection *bus, char **args, unsigned n) {
2893 DBusMessage *m = NULL, *reply = NULL;
2897 DBusMessageIter iter, sub;
2900 dbus_error_init(&error);
2902 method = streq(args[0], "set-environment")
2904 : "UnsetEnvironment";
2906 if (!(m = dbus_message_new_method_call(
2907 "org.freedesktop.systemd1",
2908 "/org/freedesktop/systemd1",
2909 "org.freedesktop.systemd1.Manager",
2912 log_error("Could not allocate message.");
2916 dbus_message_iter_init_append(m, &iter);
2918 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
2919 log_error("Could not append arguments to message.");
2924 for (i = 1; i < n; i++)
2925 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) {
2926 log_error("Could not append arguments to message.");
2931 if (!dbus_message_iter_close_container(&iter, &sub)) {
2932 log_error("Could not append arguments to message.");
2937 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2938 log_error("Failed to issue method call: %s", error.message);
2947 dbus_message_unref(m);
2950 dbus_message_unref(reply);
2952 dbus_error_free(&error);
2965 static Hashmap *will_install = NULL, *have_installed = NULL;
2966 static Set *remove_symlinks_to = NULL;
2968 static void install_info_free(InstallInfo *i) {
2973 strv_free(i->aliases);
2974 strv_free(i->wanted_by);
2978 static void install_info_hashmap_free(Hashmap *m) {
2981 while ((i = hashmap_steal_first(m)))
2982 install_info_free(i);
2987 static bool unit_name_valid(const char *name) {
2989 /* This is a minimal version of unit_name_valid() from
2995 if (ignore_file(name))
3001 static int install_info_add(const char *name) {
3005 assert(will_install);
3007 if (!unit_name_valid(name))
3010 if (hashmap_get(have_installed, name) ||
3011 hashmap_get(will_install, name))
3014 if (!(i = new0(InstallInfo, 1))) {
3019 if (!(i->name = strdup(name))) {
3024 if ((r = hashmap_put(will_install, i->name, i)) < 0)
3031 install_info_free(i);
3036 static int config_parse_also(
3037 const char *filename,
3039 const char *section,
3053 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
3057 if (!(n = strndup(w, l)))
3060 r = install_info_add(n);
3070 static int mark_symlink_for_removal(const char *p) {
3075 assert(path_is_absolute(p));
3077 if (!remove_symlinks_to)
3080 if (!(n = strdup(p)))
3083 path_kill_slashes(n);
3085 if ((r = set_put(remove_symlinks_to, n)) < 0) {
3087 return r == -EEXIST ? 0 : r;
3093 static int remove_marked_symlinks_fd(int fd, const char *config_path, const char *root, bool *deleted) {
3102 if (!(d = fdopendir(fd))) {
3103 close_nointr_nofail(fd);
3109 while ((de = readdir(d))) {
3110 bool is_dir = false, is_link = false;
3112 if (ignore_file(de->d_name))
3115 if (de->d_type == DT_LNK)
3117 else if (de->d_type == DT_DIR)
3119 else if (de->d_type == DT_UNKNOWN) {
3122 if (fstatat(fd, de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) {
3123 log_error("Failed to stat %s/%s: %m", root, de->d_name);
3130 is_link = S_ISLNK(st.st_mode);
3131 is_dir = S_ISDIR(st.st_mode);
3139 if ((nfd = openat(fd, de->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW)) < 0) {
3140 log_error("Failed to open %s/%s: %m", root, de->d_name);
3147 if (asprintf(&p, "%s/%s", root, de->d_name) < 0) {
3148 log_error("Failed to allocate directory string.");
3149 close_nointr_nofail(nfd);
3154 /* This will close nfd, regardless whether it succeeds or not */
3155 q = remove_marked_symlinks_fd(nfd, config_path, p, deleted);
3161 } else if (is_link) {
3165 if (asprintf(&p, "%s/%s", root, de->d_name) < 0) {
3166 log_error("Failed to allocate symlink string.");
3171 if ((q = readlink_and_make_absolute(p, &dest)) < 0) {
3172 log_error("Cannot read symlink %s: %s", p, strerror(-q));
3180 if ((c = canonicalize_file_name(dest))) {
3181 /* This might fail if the destination
3182 * is already removed */
3188 path_kill_slashes(dest);
3189 if (set_get(remove_symlinks_to, dest)) {
3192 log_info("rm '%s'", p);
3194 if (unlink(p) < 0) {
3195 log_error("Cannot unlink symlink %s: %m", p);
3200 rmdir_parents(p, config_path);
3201 path_kill_slashes(p);
3203 if (!set_get(remove_symlinks_to, p)) {
3205 if ((r = mark_symlink_for_removal(p)) < 0) {
3224 static int remove_marked_symlinks(const char *config_path) {
3228 assert(config_path);
3230 if (set_size(remove_symlinks_to) <= 0)
3233 if ((fd = open(config_path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW)) < 0)
3240 if ((cfd = dup(fd)) < 0) {
3245 /* This takes possession of cfd and closes it */
3246 if ((q = remove_marked_symlinks_fd(cfd, config_path, config_path, &deleted)) < 0) {
3252 close_nointr_nofail(fd);
3257 static int create_symlink(const char *verb, const char *old_path, const char *new_path) {
3264 if (streq(verb, "enable")) {
3267 mkdir_parents(new_path, 0755);
3269 if (symlink(old_path, new_path) >= 0) {
3272 log_info("ln -s '%s' '%s'", old_path, new_path);
3277 if (errno != EEXIST) {
3278 log_error("Cannot link %s to %s: %m", old_path, new_path);
3282 if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
3284 if (errno == EINVAL) {
3285 log_error("Cannot link %s to %s, file exists already and is not a symlink.", old_path, new_path);
3289 log_error("readlink() failed: %s", strerror(-r));
3293 if (streq(dest, old_path)) {
3299 log_error("Cannot link %s to %s, symlink exists already and points to %s.", old_path, new_path, dest);
3308 log_info("ln -s '%s' '%s'", old_path, new_path);
3310 if (symlink(old_path, new_path) >= 0)
3313 log_error("Cannot link %s to %s: %m", old_path, new_path);
3316 } else if (streq(verb, "disable")) {
3319 if ((r = mark_symlink_for_removal(old_path)) < 0)
3322 if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
3323 if (errno == ENOENT)
3326 if (errno == EINVAL) {
3327 log_warning("File %s not a symlink, ignoring.", old_path);
3331 log_error("readlink() failed: %s", strerror(-r));
3335 if (!streq(dest, old_path)) {
3336 log_warning("File %s not a symlink to %s but points to %s, ignoring.", new_path, old_path, dest);
3343 if ((r = mark_symlink_for_removal(new_path)) < 0)
3347 log_info("rm '%s'", new_path);
3349 if (unlink(new_path) >= 0)
3352 log_error("Cannot unlink %s: %m", new_path);
3355 } else if (streq(verb, "is-enabled")) {
3358 if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
3360 if (errno == ENOENT || errno == EINVAL)
3363 log_error("readlink() failed: %s", strerror(-r));
3367 if (streq(dest, old_path)) {
3375 assert_not_reached("Unknown action.");
3378 static int install_info_symlink_alias(const char *verb, InstallInfo *i, const char *config_path) {
3380 char *alias_path = NULL;
3385 assert(config_path);
3387 STRV_FOREACH(s, i->aliases) {
3389 if (!unit_name_valid(*s)) {
3390 log_error("Invalid name %s.", *s);
3396 if (!(alias_path = path_make_absolute(*s, config_path))) {
3397 log_error("Out of memory");
3402 if ((r = create_symlink(verb, i->path, alias_path)) != 0)
3405 if (streq(verb, "disable"))
3406 rmdir_parents(alias_path, config_path);
3417 static int install_info_symlink_wants(const char *verb, InstallInfo *i, const char *config_path) {
3419 char *alias_path = NULL;
3424 assert(config_path);
3426 STRV_FOREACH(s, i->wanted_by) {
3427 if (!unit_name_valid(*s)) {
3428 log_error("Invalid name %s.", *s);
3436 if (asprintf(&alias_path, "%s/%s.wants/%s", config_path, *s, i->name) < 0) {
3437 log_error("Out of memory");
3442 if ((r = create_symlink(verb, i->path, alias_path)) != 0)
3445 if (streq(verb, "disable"))
3446 rmdir_parents(alias_path, config_path);
3457 static int install_info_apply(const char *verb, LookupPaths *paths, InstallInfo *i, const char *config_path) {
3459 const ConfigItem items[] = {
3460 { "Alias", config_parse_strv, &i->aliases, "Install" },
3461 { "WantedBy", config_parse_strv, &i->wanted_by, "Install" },
3462 { "Also", config_parse_also, NULL, "Install" },
3464 { NULL, NULL, NULL, NULL }
3468 char *filename = NULL;
3475 STRV_FOREACH(p, paths->unit_path) {
3478 if (!(filename = path_make_absolute(i->name, *p))) {
3479 log_error("Out of memory");
3483 /* Ensure that we don't follow symlinks */
3484 if ((fd = open(filename, O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NOCTTY)) >= 0)
3485 if ((f = fdopen(fd, "re")))
3488 if (errno == ELOOP) {
3489 log_error("Refusing to operate on symlinks, please pass unit names or absolute paths to unit files.");
3494 if (errno != ENOENT) {
3495 log_error("Failed to open %s: %m", filename);
3505 log_error("Couldn't find %s.", i->name);
3511 if ((r = config_parse(filename, f, NULL, items, true, i)) < 0) {
3518 if ((r = install_info_symlink_alias(verb, i, config_path)) != 0)
3521 if ((r = install_info_symlink_wants(verb, i, config_path)) != 0)
3524 if ((r = mark_symlink_for_removal(filename)) < 0)
3527 if ((r = remove_marked_symlinks(config_path)) < 0)
3533 static char *get_config_path(void) {
3535 if (arg_session && arg_global)
3536 return strdup(SESSION_CONFIG_UNIT_PATH);
3541 if (session_config_home(&p) < 0)
3547 return strdup(SYSTEM_CONFIG_UNIT_PATH);
3550 static int enable_unit(DBusConnection *bus, char **args, unsigned n) {
3554 char *config_path = NULL;
3557 const char *verb = args[0];
3559 dbus_error_init(&error);
3562 if ((r = lookup_paths_init(&paths, arg_session ? MANAGER_SESSION : MANAGER_SYSTEM)) < 0) {
3563 log_error("Failed to determine lookup paths: %s", strerror(-r));
3567 if (!(config_path = get_config_path())) {
3568 log_error("Failed to determine config path");
3573 will_install = hashmap_new(string_hash_func, string_compare_func);
3574 have_installed = hashmap_new(string_hash_func, string_compare_func);
3576 if (!will_install || !have_installed) {
3577 log_error("Failed to allocate unit sets.");
3582 if (!arg_defaults && streq(verb, "disable"))
3583 if (!(remove_symlinks_to = set_new(string_hash_func, string_compare_func))) {
3584 log_error("Failed to allocate symlink sets.");
3589 for (j = 1; j < n; j++)
3590 if ((r = install_info_add(args[j])) < 0)
3593 while ((i = hashmap_first(will_install))) {
3596 assert_se(hashmap_move_one(have_installed, will_install, i->name) == 0);
3598 if ((q = install_info_apply(verb, &paths, i, config_path)) != 0) {
3606 /* In test mode and found something */
3612 if (streq(verb, "is-enabled"))
3613 r = r > 0 ? 0 : -ENOENT;
3615 /* Don't try to reload anything if the user asked us to not do this */
3617 /* Don't try to reload anything when updating a unit globally */
3619 /* Don't try to reload anything if we are called for system changes but the system wasn't booted with systemd */
3620 (arg_session || sd_booted() > 0) &&
3621 /* Don't try to reload anything if we are running in a chroot environment */
3622 (arg_session || running_in_chroot() <= 0) ) {
3625 if ((q = daemon_reload(bus, args, n)) < 0)
3630 install_info_hashmap_free(will_install);
3631 install_info_hashmap_free(have_installed);
3633 set_free_free(remove_symlinks_to);
3635 lookup_paths_free(&paths);
3642 static int systemctl_help(void) {
3644 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
3645 "Send control commands to or query the systemd manager.\n\n"
3646 " -h --help Show this help\n"
3647 " -t --type=TYPE List only units of a particular type\n"
3648 " -p --property=NAME Show only properties by this name\n"
3649 " -a --all Show all units/properties, including dead/empty ones\n"
3650 " --full Don't ellipsize unit names on output\n"
3651 " --fail When queueing a new job, fail if conflicting jobs are\n"
3653 " -q --quiet Suppress output\n"
3654 " --no-block Do not wait until operation finished\n"
3655 " --system Connect to system bus\n"
3656 " --session Connect to session bus\n"
3657 " --order When generating graph for dot, show only order\n"
3658 " --require When generating graph for dot, show only requirement\n"
3659 " --no-wall Don't send wall message before halt/power-off/reboot\n"
3660 " --global Enable/disable unit files globally\n"
3661 " --no-reload When enabling/disabling unit files, don't reload daemon\n"
3663 " --force When enabling unit files, override existing symlinks\n"
3664 " --defaults When disabling unit files, remove default symlinks only\n\n"
3666 " list-units List units\n"
3667 " start [NAME...] Start (activate) one or more units\n"
3668 " stop [NAME...] Stop (deactivate) one or more units\n"
3669 " reload [NAME...] Reload one or more units\n"
3670 " restart [NAME...] Start or restart one or more units\n"
3671 " try-restart [NAME...] Restart one or more units if active\n"
3672 " reload-or-restart [NAME...] Reload one or more units is possible,\n"
3673 " otherwise start or restart\n"
3674 " reload-or-try-restart [NAME...] Reload one or more units is possible,\n"
3675 " otherwise restart if active\n"
3676 " isolate [NAME] Start one unit and stop all others\n"
3677 " is-active [NAME...] Check whether units are active\n"
3678 " status [NAME...] Show runtime status of one or more units\n"
3679 " show [NAME...|JOB...] Show properties of one or more\n"
3680 " units/jobs or the manager\n"
3681 " reset-maintenance [NAME...] Reset maintenance state for all, one,\n"
3683 " enable [NAME...] Enable one or more unit files\n"
3684 " disable [NAME...] Disable one or more unit files\n"
3685 " is-enabled [NAME...] Check whether unit files are enabled\n"
3686 " load [NAME...] Load one or more units\n"
3687 " list-jobs List jobs\n"
3688 " cancel [JOB...] Cancel all, one, or more jobs\n"
3689 " monitor Monitor unit/job changes\n"
3690 " dump Dump server status\n"
3691 " dot Dump dependency graph for dot(1)\n"
3692 " snapshot [NAME] Create a snapshot\n"
3693 " delete [NAME...] Remove one or more snapshots\n"
3694 " daemon-reload Reload systemd manager configuration\n"
3695 " daemon-reexec Reexecute systemd manager\n"
3696 " daemon-exit Ask the systemd manager to quit\n"
3697 " show-environment Dump environment\n"
3698 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
3699 " unset-environment [NAME...] Unset one or more environment variables\n"
3700 " halt Shut down and halt the system\n"
3701 " poweroff Shut down and power-off the system\n"
3702 " reboot Shut down and reboot the system\n"
3703 " rescue Enter system rescue mode\n"
3704 " emergency Enter system emergency mode\n"
3705 " default Enter system default mode\n",
3706 program_invocation_short_name);
3711 static int halt_help(void) {
3713 printf("%s [OPTIONS...]\n\n"
3714 "%s the system.\n\n"
3715 " --help Show this help\n"
3716 " --halt Halt the machine\n"
3717 " -p --poweroff Switch off the machine\n"
3718 " --reboot Reboot the machine\n"
3719 " -f --force Force immediate halt/power-off/reboot\n"
3720 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
3721 " -d --no-wtmp Don't write wtmp record\n"
3722 " -n --no-sync Don't sync before halt/power-off/reboot\n"
3723 " --no-wall Don't send wall message before halt/power-off/reboot\n",
3724 program_invocation_short_name,
3725 arg_action == ACTION_REBOOT ? "Reboot" :
3726 arg_action == ACTION_POWEROFF ? "Power off" :
3732 static int shutdown_help(void) {
3734 printf("%s [OPTIONS...] [now] [WALL...]\n\n"
3735 "Shut down the system.\n\n"
3736 " --help Show this help\n"
3737 " -H --halt Halt the machine\n"
3738 " -P --poweroff Power-off the machine\n"
3739 " -r --reboot Reboot the machine\n"
3740 " -h Equivalent to --poweroff, overriden by --halt\n"
3741 " -k Don't halt/power-off/reboot, just send warnings\n"
3742 " --no-wall Don't send wall message before halt/power-off/reboot\n",
3743 program_invocation_short_name);
3748 static int telinit_help(void) {
3750 printf("%s [OPTIONS...] {COMMAND}\n\n"
3751 "Send control commands to the init daemon.\n\n"
3752 " --help Show this help\n"
3753 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
3755 " 0 Power-off the machine\n"
3756 " 6 Reboot the machine\n"
3757 " 2, 3, 4, 5 Start runlevelX.target unit\n"
3758 " 1, s, S Enter rescue mode\n"
3759 " q, Q Reload init daemon configuration\n"
3760 " u, U Reexecute init daemon\n",
3761 program_invocation_short_name);
3766 static int runlevel_help(void) {
3768 printf("%s [OPTIONS...]\n\n"
3769 "Prints the previous and current runlevel of the init system.\n\n"
3770 " --help Show this help\n",
3771 program_invocation_short_name);
3776 static int systemctl_parse_argv(int argc, char *argv[]) {
3793 static const struct option options[] = {
3794 { "help", no_argument, NULL, 'h' },
3795 { "type", required_argument, NULL, 't' },
3796 { "property", required_argument, NULL, 'p' },
3797 { "all", no_argument, NULL, 'a' },
3798 { "full", no_argument, NULL, ARG_FULL },
3799 { "fail", no_argument, NULL, ARG_FAIL },
3800 { "session", no_argument, NULL, ARG_SESSION },
3801 { "system", no_argument, NULL, ARG_SYSTEM },
3802 { "global", no_argument, NULL, ARG_GLOBAL },
3803 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
3804 { "no-wall", no_argument, NULL, ARG_NO_WALL },
3805 { "quiet", no_argument, NULL, 'q' },
3806 { "order", no_argument, NULL, ARG_ORDER },
3807 { "require", no_argument, NULL, ARG_REQUIRE },
3808 { "force", no_argument, NULL, ARG_FORCE },
3809 { "no-reload", no_argument, NULL, ARG_NO_RELOAD },
3810 { "defaults", no_argument, NULL, ARG_DEFAULTS },
3811 { NULL, 0, NULL, 0 }
3819 while ((c = getopt_long(argc, argv, "ht:p:aq", options, NULL)) >= 0) {
3834 if (!(l = strv_append(arg_property, optarg)))
3837 strv_free(arg_property);
3840 /* If the user asked for a particular
3841 * property, show it to him, even if it is
3860 arg_session = false;
3864 arg_no_block = true;
3872 arg_dot = DOT_ORDER;
3876 arg_dot = DOT_REQUIRE;
3892 arg_no_reload = true;
3901 arg_defaults = true;
3908 log_error("Unknown option code %c", c);
3916 static int halt_parse_argv(int argc, char *argv[]) {
3925 static const struct option options[] = {
3926 { "help", no_argument, NULL, ARG_HELP },
3927 { "halt", no_argument, NULL, ARG_HALT },
3928 { "poweroff", no_argument, NULL, 'p' },
3929 { "reboot", no_argument, NULL, ARG_REBOOT },
3930 { "force", no_argument, NULL, 'f' },
3931 { "wtmp-only", no_argument, NULL, 'w' },
3932 { "no-wtmp", no_argument, NULL, 'd' },
3933 { "no-sync", no_argument, NULL, 'n' },
3934 { "no-wall", no_argument, NULL, ARG_NO_WALL },
3935 { NULL, 0, NULL, 0 }
3943 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
3944 if (runlevel == '0' || runlevel == '6')
3945 arg_immediate = true;
3947 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
3955 arg_action = ACTION_HALT;
3959 if (arg_action != ACTION_REBOOT)
3960 arg_action = ACTION_POWEROFF;
3964 arg_action = ACTION_REBOOT;
3968 arg_immediate = true;
3989 /* Compatibility nops */
3996 log_error("Unknown option code %c", c);
4001 if (optind < argc) {
4002 log_error("Too many arguments.");
4009 static int shutdown_parse_argv(int argc, char *argv[]) {
4016 static const struct option options[] = {
4017 { "help", no_argument, NULL, ARG_HELP },
4018 { "halt", no_argument, NULL, 'H' },
4019 { "poweroff", no_argument, NULL, 'P' },
4020 { "reboot", no_argument, NULL, 'r' },
4021 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4022 { NULL, 0, NULL, 0 }
4030 while ((c = getopt_long(argc, argv, "HPrhkt:a", options, NULL)) >= 0) {
4038 arg_action = ACTION_HALT;
4042 arg_action = ACTION_POWEROFF;
4046 arg_action = ACTION_REBOOT;
4050 if (arg_action != ACTION_HALT)
4051 arg_action = ACTION_POWEROFF;
4064 /* Compatibility nops */
4071 log_error("Unknown option code %c", c);
4076 if (argc > optind && !streq(argv[optind], "now"))
4077 log_warning("First argument '%s' isn't 'now'. Ignoring.", argv[optind]);
4079 /* We ignore the time argument */
4080 if (argc > optind + 1)
4081 arg_wall = argv + optind + 1;
4088 static int telinit_parse_argv(int argc, char *argv[]) {
4095 static const struct option options[] = {
4096 { "help", no_argument, NULL, ARG_HELP },
4097 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4098 { NULL, 0, NULL, 0 }
4101 static const struct {
4105 { '0', ACTION_POWEROFF },
4106 { '6', ACTION_REBOOT },
4107 { '1', ACTION_RESCUE },
4108 { '2', ACTION_RUNLEVEL2 },
4109 { '3', ACTION_RUNLEVEL3 },
4110 { '4', ACTION_RUNLEVEL4 },
4111 { '5', ACTION_RUNLEVEL5 },
4112 { 's', ACTION_RESCUE },
4113 { 'S', ACTION_RESCUE },
4114 { 'q', ACTION_RELOAD },
4115 { 'Q', ACTION_RELOAD },
4116 { 'u', ACTION_REEXEC },
4117 { 'U', ACTION_REEXEC }
4126 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4141 log_error("Unknown option code %c", c);
4146 if (optind >= argc) {
4151 if (optind + 1 < argc) {
4152 log_error("Too many arguments.");
4156 if (strlen(argv[optind]) != 1) {
4157 log_error("Expected single character argument.");
4161 for (i = 0; i < ELEMENTSOF(table); i++)
4162 if (table[i].from == argv[optind][0])
4165 if (i >= ELEMENTSOF(table)) {
4166 log_error("Unknown command %s.", argv[optind]);
4170 arg_action = table[i].to;
4177 static int runlevel_parse_argv(int argc, char *argv[]) {
4183 static const struct option options[] = {
4184 { "help", no_argument, NULL, ARG_HELP },
4185 { NULL, 0, NULL, 0 }
4193 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4204 log_error("Unknown option code %c", c);
4209 if (optind < argc) {
4210 log_error("Too many arguments.");
4217 static int parse_argv(int argc, char *argv[]) {
4221 if (program_invocation_short_name) {
4223 if (strstr(program_invocation_short_name, "halt")) {
4224 arg_action = ACTION_HALT;
4225 return halt_parse_argv(argc, argv);
4226 } else if (strstr(program_invocation_short_name, "poweroff")) {
4227 arg_action = ACTION_POWEROFF;
4228 return halt_parse_argv(argc, argv);
4229 } else if (strstr(program_invocation_short_name, "reboot")) {
4230 arg_action = ACTION_REBOOT;
4231 return halt_parse_argv(argc, argv);
4232 } else if (strstr(program_invocation_short_name, "shutdown")) {
4233 arg_action = ACTION_POWEROFF;
4234 return shutdown_parse_argv(argc, argv);
4235 } else if (strstr(program_invocation_short_name, "init")) {
4237 if (sd_booted() > 0) {
4238 arg_action = ACTION_INVALID;
4239 return telinit_parse_argv(argc, argv);
4241 /* Hmm, so some other init system is
4242 * running, we need to forward this
4243 * request to it. For now we simply
4244 * guess that it is Upstart. */
4246 execv("/lib/upstart/telinit", argv);
4248 log_error("Couldn't find an alternative telinit implementation to spawn.");
4252 } else if (strstr(program_invocation_short_name, "runlevel")) {
4253 arg_action = ACTION_RUNLEVEL;
4254 return runlevel_parse_argv(argc, argv);
4258 arg_action = ACTION_SYSTEMCTL;
4259 return systemctl_parse_argv(argc, argv);
4262 static int action_to_runlevel(void) {
4264 static const char table[_ACTION_MAX] = {
4265 [ACTION_HALT] = '0',
4266 [ACTION_POWEROFF] = '0',
4267 [ACTION_REBOOT] = '6',
4268 [ACTION_RUNLEVEL2] = '2',
4269 [ACTION_RUNLEVEL3] = '3',
4270 [ACTION_RUNLEVEL4] = '4',
4271 [ACTION_RUNLEVEL5] = '5',
4272 [ACTION_RESCUE] = '1'
4275 assert(arg_action < _ACTION_MAX);
4277 return table[arg_action];
4280 static int talk_upstart(void) {
4281 DBusMessage *m = NULL, *reply = NULL;
4283 int previous, rl, r;
4285 env1_buf[] = "RUNLEVEL=X",
4286 env2_buf[] = "PREVLEVEL=X";
4287 char *env1 = env1_buf, *env2 = env2_buf;
4288 const char *emit = "runlevel";
4289 dbus_bool_t b_false = FALSE;
4290 DBusMessageIter iter, sub;
4291 DBusConnection *bus;
4293 dbus_error_init(&error);
4295 if (!(rl = action_to_runlevel()))
4298 if (utmp_get_runlevel(&previous, NULL) < 0)
4301 if (!(bus = dbus_connection_open_private("unix:abstract=/com/ubuntu/upstart", &error))) {
4302 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
4307 log_error("Failed to connect to Upstart bus: %s", error.message);
4312 if ((r = bus_check_peercred(bus)) < 0) {
4313 log_error("Failed to verify owner of bus.");
4317 if (!(m = dbus_message_new_method_call(
4318 "com.ubuntu.Upstart",
4319 "/com/ubuntu/Upstart",
4320 "com.ubuntu.Upstart0_6",
4323 log_error("Could not allocate message.");
4328 dbus_message_iter_init_append(m, &iter);
4330 env1_buf[sizeof(env1_buf)-2] = rl;
4331 env2_buf[sizeof(env2_buf)-2] = previous;
4333 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
4334 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
4335 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
4336 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
4337 !dbus_message_iter_close_container(&iter, &sub) ||
4338 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
4339 log_error("Could not append arguments to message.");
4344 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
4346 if (error_is_no_service(&error)) {
4351 log_error("Failed to issue method call: %s", error.message);
4360 dbus_message_unref(m);
4363 dbus_message_unref(reply);
4366 dbus_connection_close(bus);
4367 dbus_connection_unref(bus);
4370 dbus_error_free(&error);
4375 static int talk_initctl(void) {
4376 struct init_request request;
4380 if (!(rl = action_to_runlevel()))
4384 request.magic = INIT_MAGIC;
4385 request.sleeptime = 0;
4386 request.cmd = INIT_CMD_RUNLVL;
4387 request.runlevel = rl;
4389 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
4391 if (errno == ENOENT)
4394 log_error("Failed to open "INIT_FIFO": %m");
4399 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
4400 close_nointr_nofail(fd);
4403 log_error("Failed to write to "INIT_FIFO": %m");
4404 return errno ? -errno : -EIO;
4410 static int systemctl_main(DBusConnection *bus, int argc, char *argv[], DBusError *error) {
4412 static const struct {
4420 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
4422 { "list-units", LESS, 1, list_units },
4423 { "list-jobs", EQUAL, 1, list_jobs },
4424 { "clear-jobs", EQUAL, 1, daemon_reload },
4425 { "load", MORE, 2, load_unit },
4426 { "cancel", MORE, 2, cancel_job },
4427 { "start", MORE, 2, start_unit },
4428 { "stop", MORE, 2, start_unit },
4429 { "reload", MORE, 2, start_unit },
4430 { "restart", MORE, 2, start_unit },
4431 { "try-restart", MORE, 2, start_unit },
4432 { "reload-or-restart", MORE, 2, start_unit },
4433 { "reload-or-try-restart", MORE, 2, start_unit },
4434 { "force-reload", MORE, 2, start_unit }, /* For compatibility with SysV */
4435 { "condrestart", MORE, 2, start_unit }, /* For compatibility with RH */
4436 { "isolate", EQUAL, 2, start_unit },
4437 { "is-active", MORE, 2, check_unit },
4438 { "check", MORE, 2, check_unit },
4439 { "show", MORE, 1, show },
4440 { "status", MORE, 2, show },
4441 { "monitor", EQUAL, 1, monitor },
4442 { "dump", EQUAL, 1, dump },
4443 { "dot", EQUAL, 1, dot },
4444 { "snapshot", LESS, 2, snapshot },
4445 { "delete", MORE, 2, delete_snapshot },
4446 { "daemon-reload", EQUAL, 1, daemon_reload },
4447 { "daemon-reexec", EQUAL, 1, daemon_reload },
4448 { "daemon-exit", EQUAL, 1, daemon_reload },
4449 { "show-environment", EQUAL, 1, show_enviroment },
4450 { "set-environment", MORE, 2, set_environment },
4451 { "unset-environment", MORE, 2, set_environment },
4452 { "halt", EQUAL, 1, start_special },
4453 { "poweroff", EQUAL, 1, start_special },
4454 { "reboot", EQUAL, 1, start_special },
4455 { "default", EQUAL, 1, start_special },
4456 { "rescue", EQUAL, 1, start_special },
4457 { "emergency", EQUAL, 1, start_special },
4458 { "reset-maintenance", MORE, 1, reset_maintenance },
4459 { "enable", MORE, 2, enable_unit },
4460 { "disable", MORE, 2, enable_unit },
4461 { "is-enabled", MORE, 2, enable_unit }
4471 left = argc - optind;
4474 /* Special rule: no arguments means "list-units" */
4477 if (streq(argv[optind], "help")) {
4482 for (i = 0; i < ELEMENTSOF(verbs); i++)
4483 if (streq(argv[optind], verbs[i].verb))
4486 if (i >= ELEMENTSOF(verbs)) {
4487 log_error("Unknown operation %s", argv[optind]);
4492 switch (verbs[i].argc_cmp) {
4495 if (left != verbs[i].argc) {
4496 log_error("Invalid number of arguments.");
4503 if (left < verbs[i].argc) {
4504 log_error("Too few arguments.");
4511 if (left > verbs[i].argc) {
4512 log_error("Too many arguments.");
4519 assert_not_reached("Unknown comparison operator.");
4522 /* Require a bus connection for all operations but
4524 if (!streq(verbs[i].verb, "enable") &&
4525 !streq(verbs[i].verb, "disable") &&
4527 log_error("Failed to get D-Bus connection: %s", error->message);
4531 return verbs[i].dispatch(bus, argv + optind, left);
4534 static int reload_with_fallback(DBusConnection *bus) {
4537 /* First, try systemd via D-Bus. */
4538 if (daemon_reload(bus, NULL, 0) > 0)
4542 /* Nothing else worked, so let's try signals */
4543 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
4545 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
4546 log_error("kill() failed: %m");
4553 static int start_with_fallback(DBusConnection *bus) {
4556 /* First, try systemd via D-Bus. */
4557 if (start_unit(bus, NULL, 0) > 0)
4561 /* Hmm, talking to systemd via D-Bus didn't work. Then
4562 * let's try to talk to Upstart via D-Bus. */
4563 if (talk_upstart() > 0)
4566 /* Nothing else worked, so let's try
4568 if (talk_initctl() != 0)
4571 log_error("Failed to talk to init daemon.");
4575 warn_wall(arg_action);
4579 static int halt_main(DBusConnection *bus) {
4582 if (geteuid() != 0) {
4583 log_error("Must to be root.");
4587 if (!arg_dry && !arg_immediate)
4588 return start_with_fallback(bus);
4591 if ((r = utmp_put_shutdown(0)) < 0)
4592 log_warning("Failed to write utmp record: %s", strerror(-r));
4600 /* Make sure C-A-D is handled by the kernel from this
4602 reboot(RB_ENABLE_CAD);
4604 switch (arg_action) {
4607 log_info("Halting.");
4608 reboot(RB_HALT_SYSTEM);
4611 case ACTION_POWEROFF:
4612 log_info("Powering off.");
4613 reboot(RB_POWER_OFF);
4617 log_info("Rebooting.");
4618 reboot(RB_AUTOBOOT);
4622 assert_not_reached("Unknown halt action.");
4625 /* We should never reach this. */
4629 static int runlevel_main(void) {
4630 int r, runlevel, previous;
4632 if ((r = utmp_get_runlevel(&runlevel, &previous)) < 0) {
4633 printf("unknown\n");
4638 previous <= 0 ? 'N' : previous,
4639 runlevel <= 0 ? 'N' : runlevel);
4644 int main(int argc, char*argv[]) {
4646 DBusConnection *bus = NULL;
4649 dbus_error_init(&error);
4651 log_parse_environment();
4653 if ((r = parse_argv(argc, argv)) < 0)
4660 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
4661 * let's shortcut this */
4662 if (arg_action == ACTION_RUNLEVEL) {
4663 retval = runlevel_main() < 0;
4667 bus_connect(arg_session ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &bus, &private_bus, &error);
4669 switch (arg_action) {
4671 case ACTION_SYSTEMCTL: {
4672 retval = systemctl_main(bus, argc, argv, &error) < 0;
4677 case ACTION_POWEROFF:
4679 retval = halt_main(bus) < 0;
4682 case ACTION_RUNLEVEL2:
4683 case ACTION_RUNLEVEL3:
4684 case ACTION_RUNLEVEL4:
4685 case ACTION_RUNLEVEL5:
4687 case ACTION_EMERGENCY:
4688 case ACTION_DEFAULT:
4689 retval = start_with_fallback(bus) < 0;
4694 retval = reload_with_fallback(bus) < 0;
4697 case ACTION_INVALID:
4698 case ACTION_RUNLEVEL:
4700 assert_not_reached("Unknown action");
4706 dbus_connection_close(bus);
4707 dbus_connection_unref(bus);
4710 dbus_error_free(&error);
4714 strv_free(arg_property);