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;
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 (streq_ptr(i->load_state, "failed")) {
1471 on = ansi_highlight(true);
1472 off = ansi_highlight(false);
1477 printf("\t Loaded: %s%s%s (%s)\n", on, strna(i->load_state), off, i->path);
1479 printf("\t Loaded: %s%s%s\n", on, strna(i->load_state), off);
1481 ss = streq_ptr(i->active_state, i->sub_state) ? NULL : i->sub_state;
1483 if (streq_ptr(i->active_state, "maintenance")) {
1484 on = ansi_highlight(true);
1485 off = ansi_highlight(false);
1486 } else if (streq_ptr(i->active_state, "active") || streq_ptr(i->active_state, "reloading")) {
1487 on = ansi_highlight_green(true);
1488 off = ansi_highlight_green(false);
1493 printf("\t Active: %s%s (%s)%s\n",
1495 strna(i->active_state),
1499 printf("\t Active: %s%s%s\n",
1501 strna(i->active_state),
1505 printf("\t Device: %s\n", i->sysfs_path);
1507 printf("\t Where: %s\n", i->where);
1509 printf("\t What: %s\n", i->what);
1512 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
1514 LIST_FOREACH(exec, p, i->exec) {
1517 /* Only show exited processes here */
1521 t = strv_join(p->argv, " ");
1522 printf("\t Exited: %u (%s, code=%s, ", p->pid, strna(t), sigchld_code_to_string(p->code));
1525 if (p->code == CLD_EXITED)
1526 printf("status=%i", p->status);
1528 printf("signal=%s", signal_to_string(p->status));
1531 if (i->main_pid == p->pid &&
1532 i->start_timestamp == p->start_timestamp &&
1533 i->exit_timestamp == p->start_timestamp)
1534 /* Let's not show this twice */
1537 if (p->pid == i->control_pid)
1541 if (i->main_pid > 0 || i->control_pid > 0) {
1544 if (i->main_pid > 0) {
1545 printf(" Main: %u", (unsigned) i->main_pid);
1549 get_process_name(i->main_pid, &t);
1554 } else if (i->exit_code > 0) {
1555 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
1557 if (i->exit_code == CLD_EXITED)
1558 printf("status=%i", i->exit_status);
1560 printf("signal=%s", signal_to_string(i->exit_status));
1565 if (i->main_pid > 0 && i->control_pid > 0)
1568 if (i->control_pid > 0) {
1571 printf(" Control: %u", (unsigned) i->control_pid);
1573 get_process_name(i->control_pid, &t);
1584 printf("\t Status: \"%s\"\n", i->status_text);
1586 if (i->default_control_group) {
1589 printf("\t CGroup: %s\n", i->default_control_group);
1591 if ((c = columns()) > 18)
1596 show_cgroup_by_path(i->default_control_group, "\t\t ", c);
1599 if (i->need_daemon_reload)
1600 printf("\n%sWarning:%s Unit file changed on disk, 'systemctl %s daemon-reload' recommended.\n",
1601 ansi_highlight(true),
1602 ansi_highlight(false),
1603 arg_session ? "--session" : "--system");
1606 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
1608 switch (dbus_message_iter_get_arg_type(iter)) {
1610 case DBUS_TYPE_STRING: {
1613 dbus_message_iter_get_basic(iter, &s);
1616 if (streq(name, "Id"))
1618 else if (streq(name, "LoadState"))
1620 else if (streq(name, "ActiveState"))
1621 i->active_state = s;
1622 else if (streq(name, "SubState"))
1624 else if (streq(name, "Description"))
1626 else if (streq(name, "FragmentPath"))
1628 else if (streq(name, "SysVPath"))
1630 else if (streq(name, "DefaultControlGroup"))
1631 i->default_control_group = s;
1632 else if (streq(name, "StatusText"))
1634 else if (streq(name, "SysFSPath"))
1636 else if (streq(name, "Where"))
1638 else if (streq(name, "What"))
1645 case DBUS_TYPE_BOOLEAN: {
1648 dbus_message_iter_get_basic(iter, &b);
1650 if (streq(name, "Accept"))
1652 else if (streq(name, "NeedDaemonReload"))
1653 i->need_daemon_reload = b;
1658 case DBUS_TYPE_UINT32: {
1661 dbus_message_iter_get_basic(iter, &u);
1663 if (streq(name, "MainPID")) {
1665 i->main_pid = (pid_t) u;
1668 } else if (streq(name, "ControlPID"))
1669 i->control_pid = (pid_t) u;
1670 else if (streq(name, "ExecMainPID")) {
1672 i->main_pid = (pid_t) u;
1673 } else if (streq(name, "NAccepted"))
1675 else if (streq(name, "NConnections"))
1676 i->n_connections = u;
1681 case DBUS_TYPE_INT32: {
1684 dbus_message_iter_get_basic(iter, &j);
1686 if (streq(name, "ExecMainCode"))
1687 i->exit_code = (int) j;
1688 else if (streq(name, "ExecMainStatus"))
1689 i->exit_status = (int) j;
1694 case DBUS_TYPE_UINT64: {
1697 dbus_message_iter_get_basic(iter, &u);
1699 if (streq(name, "ExecMainStartTimestamp"))
1700 i->start_timestamp = (usec_t) u;
1701 else if (streq(name, "ExecMainExitTimestamp"))
1702 i->exit_timestamp = (usec_t) u;
1707 case DBUS_TYPE_ARRAY: {
1709 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT &&
1710 startswith(name, "Exec")) {
1711 DBusMessageIter sub;
1713 dbus_message_iter_recurse(iter, &sub);
1714 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1715 ExecStatusInfo *info;
1718 if (!(info = new0(ExecStatusInfo, 1)))
1721 if ((r = exec_status_info_deserialize(&sub, info)) < 0) {
1726 LIST_PREPEND(ExecStatusInfo, exec, i->exec, info);
1728 dbus_message_iter_next(&sub);
1739 static int print_property(const char *name, DBusMessageIter *iter) {
1743 /* This is a low-level property printer, see
1744 * print_status_info() for the nicer output */
1746 if (arg_property && !strv_find(arg_property, name))
1749 switch (dbus_message_iter_get_arg_type(iter)) {
1751 case DBUS_TYPE_STRING: {
1753 dbus_message_iter_get_basic(iter, &s);
1755 if (arg_all || s[0])
1756 printf("%s=%s\n", name, s);
1761 case DBUS_TYPE_BOOLEAN: {
1763 dbus_message_iter_get_basic(iter, &b);
1764 printf("%s=%s\n", name, yes_no(b));
1769 case DBUS_TYPE_UINT64: {
1771 dbus_message_iter_get_basic(iter, &u);
1773 /* Yes, heuristics! But we can change this check
1774 * should it turn out to not be sufficient */
1776 if (strstr(name, "Timestamp")) {
1777 char timestamp[FORMAT_TIMESTAMP_MAX], *t;
1779 if ((t = format_timestamp(timestamp, sizeof(timestamp), u)) || arg_all)
1780 printf("%s=%s\n", name, strempty(t));
1781 } else if (strstr(name, "USec")) {
1782 char timespan[FORMAT_TIMESPAN_MAX];
1784 printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u));
1786 printf("%s=%llu\n", name, (unsigned long long) u);
1791 case DBUS_TYPE_UINT32: {
1793 dbus_message_iter_get_basic(iter, &u);
1795 if (strstr(name, "UMask") || strstr(name, "Mode"))
1796 printf("%s=%04o\n", name, u);
1798 printf("%s=%u\n", name, (unsigned) u);
1803 case DBUS_TYPE_INT32: {
1805 dbus_message_iter_get_basic(iter, &i);
1807 printf("%s=%i\n", name, (int) i);
1811 case DBUS_TYPE_STRUCT: {
1812 DBusMessageIter sub;
1813 dbus_message_iter_recurse(iter, &sub);
1815 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
1818 dbus_message_iter_get_basic(&sub, &u);
1821 printf("%s=%u\n", name, (unsigned) u);
1823 printf("%s=\n", name);
1826 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
1829 dbus_message_iter_get_basic(&sub, &s);
1831 if (arg_all || s[0])
1832 printf("%s=%s\n", name, s);
1840 case DBUS_TYPE_ARRAY:
1842 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
1843 DBusMessageIter sub;
1846 dbus_message_iter_recurse(iter, &sub);
1848 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1849 printf("%s=", name);
1851 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1854 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
1855 dbus_message_iter_get_basic(&sub, &s);
1856 printf("%s%s", space ? " " : "", s);
1859 dbus_message_iter_next(&sub);
1867 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_BYTE) {
1868 DBusMessageIter sub;
1870 dbus_message_iter_recurse(iter, &sub);
1872 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1873 printf("%s=", name);
1875 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1878 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_BYTE);
1879 dbus_message_iter_get_basic(&sub, &u);
1882 dbus_message_iter_next(&sub);
1890 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
1891 DBusMessageIter sub, sub2;
1893 dbus_message_iter_recurse(iter, &sub);
1894 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1895 const char *type, *path;
1897 dbus_message_iter_recurse(&sub, &sub2);
1899 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
1900 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
1901 printf("%s=%s\n", type, path);
1903 dbus_message_iter_next(&sub);
1908 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
1909 DBusMessageIter sub, sub2;
1911 dbus_message_iter_recurse(iter, &sub);
1912 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1914 uint64_t value, next_elapse;
1916 dbus_message_iter_recurse(&sub, &sub2);
1918 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
1919 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
1920 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
1921 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
1923 printf("%s={ value=%s ; next_elapse=%s }\n",
1925 format_timespan(timespan1, sizeof(timespan1), value),
1926 format_timespan(timespan2, sizeof(timespan2), next_elapse));
1929 dbus_message_iter_next(&sub);
1934 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
1935 DBusMessageIter sub;
1937 dbus_message_iter_recurse(iter, &sub);
1938 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1939 ExecStatusInfo info;
1942 if (exec_status_info_deserialize(&sub, &info) >= 0) {
1943 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
1946 t = strv_join(info.argv, " ");
1948 printf("%s={ path=%s ; argv[]=%s ; ignore=%s ; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n",
1952 yes_no(info.ignore),
1953 strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)),
1954 strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)),
1955 (unsigned) info. pid,
1956 sigchld_code_to_string(info.code),
1958 info.code == CLD_EXITED ? "" : "/",
1959 strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status)));
1965 strv_free(info.argv);
1967 dbus_message_iter_next(&sub);
1977 printf("%s=[unprintable]\n", name);
1982 static int show_one(DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
1983 DBusMessage *m = NULL, *reply = NULL;
1984 const char *interface = "";
1987 DBusMessageIter iter, sub, sub2, sub3;
1988 UnitStatusInfo info;
1996 dbus_error_init(&error);
1998 if (!(m = dbus_message_new_method_call(
1999 "org.freedesktop.systemd1",
2001 "org.freedesktop.DBus.Properties",
2003 log_error("Could not allocate message.");
2008 if (!dbus_message_append_args(m,
2009 DBUS_TYPE_STRING, &interface,
2010 DBUS_TYPE_INVALID)) {
2011 log_error("Could not append arguments to message.");
2016 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2017 log_error("Failed to issue method call: %s", error.message);
2022 if (!dbus_message_iter_init(reply, &iter) ||
2023 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
2024 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
2025 log_error("Failed to parse reply.");
2030 dbus_message_iter_recurse(&iter, &sub);
2037 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2040 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
2041 log_error("Failed to parse reply.");
2046 dbus_message_iter_recurse(&sub, &sub2);
2048 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
2049 log_error("Failed to parse reply.");
2054 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
2055 log_error("Failed to parse reply.");
2060 dbus_message_iter_recurse(&sub2, &sub3);
2062 if (show_properties)
2063 r = print_property(name, &sub3);
2065 r = status_property(name, &sub3, &info);
2068 log_error("Failed to parse reply.");
2073 dbus_message_iter_next(&sub);
2076 if (!show_properties)
2077 print_status_info(&info);
2079 while ((p = info.exec)) {
2080 LIST_REMOVE(ExecStatusInfo, exec, info.exec, p);
2081 exec_status_info_free(p);
2088 dbus_message_unref(m);
2091 dbus_message_unref(reply);
2093 dbus_error_free(&error);
2098 static int show(DBusConnection *bus, char **args, unsigned n) {
2099 DBusMessage *m = NULL, *reply = NULL;
2103 bool show_properties, new_line = false;
2108 dbus_error_init(&error);
2110 show_properties = !streq(args[0], "status");
2112 if (show_properties && n <= 1) {
2113 /* If not argument is specified inspect the manager
2116 r = show_one(bus, "/org/freedesktop/systemd1", show_properties, &new_line);
2120 for (i = 1; i < n; i++) {
2121 const char *path = NULL;
2124 if (safe_atou32(args[i], &id) < 0) {
2126 /* Interpret as unit name */
2128 if (!(m = dbus_message_new_method_call(
2129 "org.freedesktop.systemd1",
2130 "/org/freedesktop/systemd1",
2131 "org.freedesktop.systemd1.Manager",
2133 log_error("Could not allocate message.");
2138 if (!dbus_message_append_args(m,
2139 DBUS_TYPE_STRING, &args[i],
2140 DBUS_TYPE_INVALID)) {
2141 log_error("Could not append arguments to message.");
2146 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2148 if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
2149 log_error("Failed to issue method call: %s", error.message);
2154 dbus_error_free(&error);
2156 dbus_message_unref(m);
2157 if (!(m = dbus_message_new_method_call(
2158 "org.freedesktop.systemd1",
2159 "/org/freedesktop/systemd1",
2160 "org.freedesktop.systemd1.Manager",
2162 log_error("Could not allocate message.");
2167 if (!dbus_message_append_args(m,
2168 DBUS_TYPE_STRING, &args[i],
2169 DBUS_TYPE_INVALID)) {
2170 log_error("Could not append arguments to message.");
2175 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2176 log_error("Failed to issue method call: %s", error.message);
2182 } else if (show_properties) {
2184 /* Interpret as job id */
2186 if (!(m = dbus_message_new_method_call(
2187 "org.freedesktop.systemd1",
2188 "/org/freedesktop/systemd1",
2189 "org.freedesktop.systemd1.Manager",
2191 log_error("Could not allocate message.");
2196 if (!dbus_message_append_args(m,
2197 DBUS_TYPE_UINT32, &id,
2198 DBUS_TYPE_INVALID)) {
2199 log_error("Could not append arguments to message.");
2204 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2205 log_error("Failed to issue method call: %s", error.message);
2211 /* Interpret as PID */
2213 if (!(m = dbus_message_new_method_call(
2214 "org.freedesktop.systemd1",
2215 "/org/freedesktop/systemd1",
2216 "org.freedesktop.systemd1.Manager",
2218 log_error("Could not allocate message.");
2223 if (!dbus_message_append_args(m,
2224 DBUS_TYPE_UINT32, &id,
2225 DBUS_TYPE_INVALID)) {
2226 log_error("Could not append arguments to message.");
2231 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2232 log_error("Failed to issue method call: %s", error.message);
2238 if (!dbus_message_get_args(reply, &error,
2239 DBUS_TYPE_OBJECT_PATH, &path,
2240 DBUS_TYPE_INVALID)) {
2241 log_error("Failed to parse reply: %s", error.message);
2246 if ((r = show_one(bus, path, show_properties, &new_line)) < 0)
2249 dbus_message_unref(m);
2250 dbus_message_unref(reply);
2258 dbus_message_unref(m);
2261 dbus_message_unref(reply);
2263 dbus_error_free(&error);
2268 static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
2270 DBusMessage *m = NULL, *reply = NULL;
2275 dbus_error_init(&error);
2277 log_debug("Got D-Bus request: %s.%s() on %s",
2278 dbus_message_get_interface(message),
2279 dbus_message_get_member(message),
2280 dbus_message_get_path(message));
2282 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
2283 log_error("Warning! D-Bus connection terminated.");
2284 dbus_connection_close(connection);
2286 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") ||
2287 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
2288 const char *id, *path;
2290 if (!dbus_message_get_args(message, &error,
2291 DBUS_TYPE_STRING, &id,
2292 DBUS_TYPE_OBJECT_PATH, &path,
2294 log_error("Failed to parse message: %s", error.message);
2295 else if (streq(dbus_message_get_member(message), "UnitNew"))
2296 printf("Unit %s added.\n", id);
2298 printf("Unit %s removed.\n", id);
2300 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew") ||
2301 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
2305 if (!dbus_message_get_args(message, &error,
2306 DBUS_TYPE_UINT32, &id,
2307 DBUS_TYPE_OBJECT_PATH, &path,
2309 log_error("Failed to parse message: %s", error.message);
2310 else if (streq(dbus_message_get_member(message), "JobNew"))
2311 printf("Job %u added.\n", id);
2313 printf("Job %u removed.\n", id);
2316 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Unit", "Changed") ||
2317 dbus_message_is_signal(message, "org.freedesktop.systemd1.Job", "Changed")) {
2319 const char *path, *interface, *property = "Id";
2320 DBusMessageIter iter, sub;
2322 path = dbus_message_get_path(message);
2323 interface = dbus_message_get_interface(message);
2325 if (!(m = dbus_message_new_method_call(
2326 "org.freedesktop.systemd1",
2328 "org.freedesktop.DBus.Properties",
2330 log_error("Could not allocate message.");
2334 if (!dbus_message_append_args(m,
2335 DBUS_TYPE_STRING, &interface,
2336 DBUS_TYPE_STRING, &property,
2337 DBUS_TYPE_INVALID)) {
2338 log_error("Could not append arguments to message.");
2342 if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
2343 log_error("Failed to issue method call: %s", error.message);
2347 if (!dbus_message_iter_init(reply, &iter) ||
2348 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2349 log_error("Failed to parse reply.");
2353 dbus_message_iter_recurse(&iter, &sub);
2355 if (streq(interface, "org.freedesktop.systemd1.Unit")) {
2358 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2359 log_error("Failed to parse reply.");
2363 dbus_message_iter_get_basic(&sub, &id);
2364 printf("Unit %s changed.\n", id);
2368 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32) {
2369 log_error("Failed to parse reply.");
2373 dbus_message_iter_get_basic(&sub, &id);
2374 printf("Job %u changed.\n", id);
2380 dbus_message_unref(m);
2383 dbus_message_unref(reply);
2385 dbus_error_free(&error);
2386 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2390 dbus_message_unref(m);
2393 dbus_message_unref(reply);
2395 dbus_error_free(&error);
2396 return DBUS_HANDLER_RESULT_NEED_MEMORY;
2399 static int monitor(DBusConnection *bus, char **args, unsigned n) {
2400 DBusMessage *m = NULL, *reply = NULL;
2404 dbus_error_init(&error);
2407 dbus_bus_add_match(bus,
2409 "sender='org.freedesktop.systemd1',"
2410 "interface='org.freedesktop.systemd1.Manager',"
2411 "path='/org/freedesktop/systemd1'",
2414 if (dbus_error_is_set(&error)) {
2415 log_error("Failed to add match: %s", error.message);
2420 dbus_bus_add_match(bus,
2422 "sender='org.freedesktop.systemd1',"
2423 "interface='org.freedesktop.systemd1.Unit',"
2427 if (dbus_error_is_set(&error)) {
2428 log_error("Failed to add match: %s", error.message);
2433 dbus_bus_add_match(bus,
2435 "sender='org.freedesktop.systemd1',"
2436 "interface='org.freedesktop.systemd1.Job',"
2440 if (dbus_error_is_set(&error)) {
2441 log_error("Failed to add match: %s", error.message);
2447 if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
2448 log_error("Failed to add filter.");
2453 if (!(m = dbus_message_new_method_call(
2454 "org.freedesktop.systemd1",
2455 "/org/freedesktop/systemd1",
2456 "org.freedesktop.systemd1.Manager",
2458 log_error("Could not allocate message.");
2463 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2464 log_error("Failed to issue method call: %s", error.message);
2469 while (dbus_connection_read_write_dispatch(bus, -1))
2476 /* This is slightly dirty, since we don't undo the filter or the matches. */
2479 dbus_message_unref(m);
2482 dbus_message_unref(reply);
2484 dbus_error_free(&error);
2489 static int dump(DBusConnection *bus, char **args, unsigned n) {
2490 DBusMessage *m = NULL, *reply = NULL;
2495 dbus_error_init(&error);
2497 if (!(m = dbus_message_new_method_call(
2498 "org.freedesktop.systemd1",
2499 "/org/freedesktop/systemd1",
2500 "org.freedesktop.systemd1.Manager",
2502 log_error("Could not allocate message.");
2506 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2507 log_error("Failed to issue method call: %s", error.message);
2512 if (!dbus_message_get_args(reply, &error,
2513 DBUS_TYPE_STRING, &text,
2514 DBUS_TYPE_INVALID)) {
2515 log_error("Failed to parse reply: %s", error.message);
2520 fputs(text, stdout);
2526 dbus_message_unref(m);
2529 dbus_message_unref(reply);
2531 dbus_error_free(&error);
2536 static int snapshot(DBusConnection *bus, char **args, unsigned n) {
2537 DBusMessage *m = NULL, *reply = NULL;
2540 const char *name = "", *path, *id;
2541 dbus_bool_t cleanup = FALSE;
2542 DBusMessageIter iter, sub;
2544 *interface = "org.freedesktop.systemd1.Unit",
2547 dbus_error_init(&error);
2549 if (!(m = dbus_message_new_method_call(
2550 "org.freedesktop.systemd1",
2551 "/org/freedesktop/systemd1",
2552 "org.freedesktop.systemd1.Manager",
2553 "CreateSnapshot"))) {
2554 log_error("Could not allocate message.");
2561 if (!dbus_message_append_args(m,
2562 DBUS_TYPE_STRING, &name,
2563 DBUS_TYPE_BOOLEAN, &cleanup,
2564 DBUS_TYPE_INVALID)) {
2565 log_error("Could not append arguments to message.");
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_get_args(reply, &error,
2577 DBUS_TYPE_OBJECT_PATH, &path,
2578 DBUS_TYPE_INVALID)) {
2579 log_error("Failed to parse reply: %s", error.message);
2584 dbus_message_unref(m);
2585 if (!(m = dbus_message_new_method_call(
2586 "org.freedesktop.systemd1",
2588 "org.freedesktop.DBus.Properties",
2590 log_error("Could not allocate message.");
2594 if (!dbus_message_append_args(m,
2595 DBUS_TYPE_STRING, &interface,
2596 DBUS_TYPE_STRING, &property,
2597 DBUS_TYPE_INVALID)) {
2598 log_error("Could not append arguments to message.");
2603 dbus_message_unref(reply);
2604 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2605 log_error("Failed to issue method call: %s", error.message);
2610 if (!dbus_message_iter_init(reply, &iter) ||
2611 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2612 log_error("Failed to parse reply.");
2617 dbus_message_iter_recurse(&iter, &sub);
2619 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2620 log_error("Failed to parse reply.");
2625 dbus_message_iter_get_basic(&sub, &id);
2633 dbus_message_unref(m);
2636 dbus_message_unref(reply);
2638 dbus_error_free(&error);
2643 static int delete_snapshot(DBusConnection *bus, char **args, unsigned n) {
2644 DBusMessage *m = NULL, *reply = NULL;
2652 dbus_error_init(&error);
2654 for (i = 1; i < n; i++) {
2655 const char *path = NULL;
2657 if (!(m = dbus_message_new_method_call(
2658 "org.freedesktop.systemd1",
2659 "/org/freedesktop/systemd1",
2660 "org.freedesktop.systemd1.Manager",
2662 log_error("Could not allocate message.");
2667 if (!dbus_message_append_args(m,
2668 DBUS_TYPE_STRING, &args[i],
2669 DBUS_TYPE_INVALID)) {
2670 log_error("Could not append arguments to message.");
2675 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2676 log_error("Failed to issue method call: %s", error.message);
2681 if (!dbus_message_get_args(reply, &error,
2682 DBUS_TYPE_OBJECT_PATH, &path,
2683 DBUS_TYPE_INVALID)) {
2684 log_error("Failed to parse reply: %s", error.message);
2689 dbus_message_unref(m);
2690 if (!(m = dbus_message_new_method_call(
2691 "org.freedesktop.systemd1",
2693 "org.freedesktop.systemd1.Snapshot",
2695 log_error("Could not allocate message.");
2700 dbus_message_unref(reply);
2701 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2702 log_error("Failed to issue method call: %s", error.message);
2707 dbus_message_unref(m);
2708 dbus_message_unref(reply);
2716 dbus_message_unref(m);
2719 dbus_message_unref(reply);
2721 dbus_error_free(&error);
2726 static int daemon_reload(DBusConnection *bus, char **args, unsigned n) {
2727 DBusMessage *m = NULL, *reply = NULL;
2732 dbus_error_init(&error);
2734 if (arg_action == ACTION_RELOAD)
2736 else if (arg_action == ACTION_REEXEC)
2737 method = "Reexecute";
2739 assert(arg_action == ACTION_SYSTEMCTL);
2742 streq(args[0], "clear-jobs") ||
2743 streq(args[0], "cancel") ? "ClearJobs" :
2744 streq(args[0], "daemon-reexec") ? "Reexecute" :
2745 streq(args[0], "reset-maintenance") ? "ResetMaintenance" :
2746 streq(args[0], "daemon-exit") ? "Exit" :
2750 if (!(m = dbus_message_new_method_call(
2751 "org.freedesktop.systemd1",
2752 "/org/freedesktop/systemd1",
2753 "org.freedesktop.systemd1.Manager",
2755 log_error("Could not allocate message.");
2759 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2761 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
2762 /* There's always a fallback possible for
2763 * legacy actions. */
2768 log_error("Failed to issue method call: %s", error.message);
2777 dbus_message_unref(m);
2780 dbus_message_unref(reply);
2782 dbus_error_free(&error);
2787 static int reset_maintenance(DBusConnection *bus, char **args, unsigned n) {
2788 DBusMessage *m = NULL, *reply = NULL;
2794 dbus_error_init(&error);
2797 return daemon_reload(bus, args, n);
2799 for (i = 1; i < n; i++) {
2801 if (!(m = dbus_message_new_method_call(
2802 "org.freedesktop.systemd1",
2803 "/org/freedesktop/systemd1",
2804 "org.freedesktop.systemd1.Manager",
2805 "ResetMaintenanceUnit"))) {
2806 log_error("Could not allocate message.");
2811 if (!dbus_message_append_args(m,
2812 DBUS_TYPE_STRING, args + i,
2813 DBUS_TYPE_INVALID)) {
2814 log_error("Could not append arguments to message.");
2819 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2820 log_error("Failed to issue method call: %s", error.message);
2825 dbus_message_unref(m);
2826 dbus_message_unref(reply);
2834 dbus_message_unref(m);
2837 dbus_message_unref(reply);
2839 dbus_error_free(&error);
2844 static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
2845 DBusMessage *m = NULL, *reply = NULL;
2847 DBusMessageIter iter, sub, sub2;
2850 *interface = "org.freedesktop.systemd1.Manager",
2851 *property = "Environment";
2853 dbus_error_init(&error);
2855 if (!(m = dbus_message_new_method_call(
2856 "org.freedesktop.systemd1",
2857 "/org/freedesktop/systemd1",
2858 "org.freedesktop.DBus.Properties",
2860 log_error("Could not allocate message.");
2864 if (!dbus_message_append_args(m,
2865 DBUS_TYPE_STRING, &interface,
2866 DBUS_TYPE_STRING, &property,
2867 DBUS_TYPE_INVALID)) {
2868 log_error("Could not append arguments to message.");
2873 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2874 log_error("Failed to issue method call: %s", error.message);
2879 if (!dbus_message_iter_init(reply, &iter) ||
2880 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2881 log_error("Failed to parse reply.");
2886 dbus_message_iter_recurse(&iter, &sub);
2888 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
2889 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
2890 log_error("Failed to parse reply.");
2895 dbus_message_iter_recurse(&sub, &sub2);
2897 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
2900 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
2901 log_error("Failed to parse reply.");
2906 dbus_message_iter_get_basic(&sub2, &text);
2907 printf("%s\n", text);
2909 dbus_message_iter_next(&sub2);
2916 dbus_message_unref(m);
2919 dbus_message_unref(reply);
2921 dbus_error_free(&error);
2926 static int set_environment(DBusConnection *bus, char **args, unsigned n) {
2927 DBusMessage *m = NULL, *reply = NULL;
2931 DBusMessageIter iter, sub;
2934 dbus_error_init(&error);
2936 method = streq(args[0], "set-environment")
2938 : "UnsetEnvironment";
2940 if (!(m = dbus_message_new_method_call(
2941 "org.freedesktop.systemd1",
2942 "/org/freedesktop/systemd1",
2943 "org.freedesktop.systemd1.Manager",
2946 log_error("Could not allocate message.");
2950 dbus_message_iter_init_append(m, &iter);
2952 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
2953 log_error("Could not append arguments to message.");
2958 for (i = 1; i < n; i++)
2959 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) {
2960 log_error("Could not append arguments to message.");
2965 if (!dbus_message_iter_close_container(&iter, &sub)) {
2966 log_error("Could not append arguments to message.");
2971 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2972 log_error("Failed to issue method call: %s", error.message);
2981 dbus_message_unref(m);
2984 dbus_message_unref(reply);
2986 dbus_error_free(&error);
2999 static Hashmap *will_install = NULL, *have_installed = NULL;
3000 static Set *remove_symlinks_to = NULL;
3002 static void install_info_free(InstallInfo *i) {
3007 strv_free(i->aliases);
3008 strv_free(i->wanted_by);
3012 static void install_info_hashmap_free(Hashmap *m) {
3015 while ((i = hashmap_steal_first(m)))
3016 install_info_free(i);
3021 static bool unit_name_valid(const char *name) {
3023 /* This is a minimal version of unit_name_valid() from
3029 if (ignore_file(name))
3035 static int install_info_add(const char *name) {
3039 assert(will_install);
3041 if (!unit_name_valid(name))
3044 if (hashmap_get(have_installed, name) ||
3045 hashmap_get(will_install, name))
3048 if (!(i = new0(InstallInfo, 1))) {
3053 if (!(i->name = strdup(name))) {
3058 if ((r = hashmap_put(will_install, i->name, i)) < 0)
3065 install_info_free(i);
3070 static int config_parse_also(
3071 const char *filename,
3073 const char *section,
3087 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
3091 if (!(n = strndup(w, l)))
3094 r = install_info_add(n);
3104 static int mark_symlink_for_removal(const char *p) {
3109 assert(path_is_absolute(p));
3111 if (!remove_symlinks_to)
3114 if (!(n = strdup(p)))
3117 path_kill_slashes(n);
3119 if ((r = set_put(remove_symlinks_to, n)) < 0) {
3121 return r == -EEXIST ? 0 : r;
3127 static int remove_marked_symlinks_fd(int fd, const char *config_path, const char *root, bool *deleted) {
3136 if (!(d = fdopendir(fd))) {
3137 close_nointr_nofail(fd);
3143 while ((de = readdir(d))) {
3144 bool is_dir = false, is_link = false;
3146 if (ignore_file(de->d_name))
3149 if (de->d_type == DT_LNK)
3151 else if (de->d_type == DT_DIR)
3153 else if (de->d_type == DT_UNKNOWN) {
3156 if (fstatat(fd, de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) {
3157 log_error("Failed to stat %s/%s: %m", root, de->d_name);
3164 is_link = S_ISLNK(st.st_mode);
3165 is_dir = S_ISDIR(st.st_mode);
3173 if ((nfd = openat(fd, de->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW)) < 0) {
3174 log_error("Failed to open %s/%s: %m", root, de->d_name);
3181 if (asprintf(&p, "%s/%s", root, de->d_name) < 0) {
3182 log_error("Failed to allocate directory string.");
3183 close_nointr_nofail(nfd);
3188 /* This will close nfd, regardless whether it succeeds or not */
3189 q = remove_marked_symlinks_fd(nfd, config_path, p, deleted);
3195 } else if (is_link) {
3199 if (asprintf(&p, "%s/%s", root, de->d_name) < 0) {
3200 log_error("Failed to allocate symlink string.");
3205 if ((q = readlink_and_make_absolute(p, &dest)) < 0) {
3206 log_error("Cannot read symlink %s: %s", p, strerror(-q));
3214 if ((c = canonicalize_file_name(dest))) {
3215 /* This might fail if the destination
3216 * is already removed */
3222 path_kill_slashes(dest);
3223 if (set_get(remove_symlinks_to, dest)) {
3226 log_info("rm '%s'", p);
3228 if (unlink(p) < 0) {
3229 log_error("Cannot unlink symlink %s: %m", p);
3234 rmdir_parents(p, config_path);
3235 path_kill_slashes(p);
3237 if (!set_get(remove_symlinks_to, p)) {
3239 if ((r = mark_symlink_for_removal(p)) < 0) {
3258 static int remove_marked_symlinks(const char *config_path) {
3262 assert(config_path);
3264 if (set_size(remove_symlinks_to) <= 0)
3267 if ((fd = open(config_path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW)) < 0)
3274 if ((cfd = dup(fd)) < 0) {
3279 /* This takes possession of cfd and closes it */
3280 if ((q = remove_marked_symlinks_fd(cfd, config_path, config_path, &deleted)) < 0) {
3286 close_nointr_nofail(fd);
3291 static int create_symlink(const char *verb, const char *old_path, const char *new_path) {
3298 if (streq(verb, "enable")) {
3301 mkdir_parents(new_path, 0755);
3303 if (symlink(old_path, new_path) >= 0) {
3306 log_info("ln -s '%s' '%s'", old_path, new_path);
3311 if (errno != EEXIST) {
3312 log_error("Cannot link %s to %s: %m", old_path, new_path);
3316 if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
3318 if (errno == EINVAL) {
3319 log_error("Cannot link %s to %s, file exists already and is not a symlink.", old_path, new_path);
3323 log_error("readlink() failed: %s", strerror(-r));
3327 if (streq(dest, old_path)) {
3333 log_error("Cannot link %s to %s, symlink exists already and points to %s.", old_path, new_path, dest);
3342 log_info("ln -s '%s' '%s'", old_path, new_path);
3344 if (symlink(old_path, new_path) >= 0)
3347 log_error("Cannot link %s to %s: %m", old_path, new_path);
3350 } else if (streq(verb, "disable")) {
3353 if ((r = mark_symlink_for_removal(old_path)) < 0)
3356 if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
3357 if (errno == ENOENT)
3360 if (errno == EINVAL) {
3361 log_warning("File %s not a symlink, ignoring.", old_path);
3365 log_error("readlink() failed: %s", strerror(-r));
3369 if (!streq(dest, old_path)) {
3370 log_warning("File %s not a symlink to %s but points to %s, ignoring.", new_path, old_path, dest);
3377 if ((r = mark_symlink_for_removal(new_path)) < 0)
3381 log_info("rm '%s'", new_path);
3383 if (unlink(new_path) >= 0)
3386 log_error("Cannot unlink %s: %m", new_path);
3389 } else if (streq(verb, "is-enabled")) {
3392 if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
3394 if (errno == ENOENT || errno == EINVAL)
3397 log_error("readlink() failed: %s", strerror(-r));
3401 if (streq(dest, old_path)) {
3409 assert_not_reached("Unknown action.");
3412 static int install_info_symlink_alias(const char *verb, InstallInfo *i, const char *config_path) {
3414 char *alias_path = NULL;
3419 assert(config_path);
3421 STRV_FOREACH(s, i->aliases) {
3423 if (!unit_name_valid(*s)) {
3424 log_error("Invalid name %s.", *s);
3430 if (!(alias_path = path_make_absolute(*s, config_path))) {
3431 log_error("Out of memory");
3436 if ((r = create_symlink(verb, i->path, alias_path)) != 0)
3439 if (streq(verb, "disable"))
3440 rmdir_parents(alias_path, config_path);
3451 static int install_info_symlink_wants(const char *verb, InstallInfo *i, const char *config_path) {
3453 char *alias_path = NULL;
3458 assert(config_path);
3460 STRV_FOREACH(s, i->wanted_by) {
3461 if (!unit_name_valid(*s)) {
3462 log_error("Invalid name %s.", *s);
3470 if (asprintf(&alias_path, "%s/%s.wants/%s", config_path, *s, i->name) < 0) {
3471 log_error("Out of memory");
3476 if ((r = create_symlink(verb, i->path, alias_path)) != 0)
3479 if (streq(verb, "disable"))
3480 rmdir_parents(alias_path, config_path);
3491 static int install_info_apply(const char *verb, LookupPaths *paths, InstallInfo *i, const char *config_path) {
3493 const ConfigItem items[] = {
3494 { "Alias", config_parse_strv, &i->aliases, "Install" },
3495 { "WantedBy", config_parse_strv, &i->wanted_by, "Install" },
3496 { "Also", config_parse_also, NULL, "Install" },
3498 { NULL, NULL, NULL, NULL }
3502 char *filename = NULL;
3509 STRV_FOREACH(p, paths->unit_path) {
3512 if (!(filename = path_make_absolute(i->name, *p))) {
3513 log_error("Out of memory");
3517 /* Ensure that we don't follow symlinks */
3518 if ((fd = open(filename, O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NOCTTY)) >= 0)
3519 if ((f = fdopen(fd, "re")))
3522 if (errno == ELOOP) {
3523 log_error("Refusing to operate on symlinks, please pass unit names or absolute paths to unit files.");
3528 if (errno != ENOENT) {
3529 log_error("Failed to open %s: %m", filename);
3539 log_error("Couldn't find %s.", i->name);
3545 if ((r = config_parse(filename, f, NULL, items, true, i)) < 0) {
3552 if ((r = install_info_symlink_alias(verb, i, config_path)) != 0)
3555 if ((r = install_info_symlink_wants(verb, i, config_path)) != 0)
3558 if ((r = mark_symlink_for_removal(filename)) < 0)
3561 if ((r = remove_marked_symlinks(config_path)) < 0)
3567 static char *get_config_path(void) {
3569 if (arg_session && arg_global)
3570 return strdup(SESSION_CONFIG_UNIT_PATH);
3575 if (session_config_home(&p) < 0)
3581 return strdup(SYSTEM_CONFIG_UNIT_PATH);
3584 static int enable_unit(DBusConnection *bus, char **args, unsigned n) {
3588 char *config_path = NULL;
3591 const char *verb = args[0];
3593 dbus_error_init(&error);
3596 if ((r = lookup_paths_init(&paths, arg_session ? MANAGER_SESSION : MANAGER_SYSTEM)) < 0) {
3597 log_error("Failed to determine lookup paths: %s", strerror(-r));
3601 if (!(config_path = get_config_path())) {
3602 log_error("Failed to determine config path");
3607 will_install = hashmap_new(string_hash_func, string_compare_func);
3608 have_installed = hashmap_new(string_hash_func, string_compare_func);
3610 if (!will_install || !have_installed) {
3611 log_error("Failed to allocate unit sets.");
3616 if (!arg_defaults && streq(verb, "disable"))
3617 if (!(remove_symlinks_to = set_new(string_hash_func, string_compare_func))) {
3618 log_error("Failed to allocate symlink sets.");
3623 for (j = 1; j < n; j++)
3624 if ((r = install_info_add(args[j])) < 0)
3627 while ((i = hashmap_first(will_install))) {
3630 assert_se(hashmap_move_one(have_installed, will_install, i->name) == 0);
3632 if ((q = install_info_apply(verb, &paths, i, config_path)) != 0) {
3640 /* In test mode and found something */
3646 if (streq(verb, "is-enabled"))
3647 r = r > 0 ? 0 : -ENOENT;
3649 /* Don't try to reload anything if the user asked us to not do this */
3651 /* Don't try to reload anything when updating a unit globally */
3653 /* Don't try to reload anything if we are called for system changes but the system wasn't booted with systemd */
3654 (arg_session || sd_booted() > 0) &&
3655 /* Don't try to reload anything if we are running in a chroot environment */
3656 (arg_session || running_in_chroot() <= 0) ) {
3659 if ((q = daemon_reload(bus, args, n)) < 0)
3664 install_info_hashmap_free(will_install);
3665 install_info_hashmap_free(have_installed);
3667 set_free_free(remove_symlinks_to);
3669 lookup_paths_free(&paths);
3676 static int systemctl_help(void) {
3678 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
3679 "Send control commands to or query the systemd manager.\n\n"
3680 " -h --help Show this help\n"
3681 " -t --type=TYPE List only units of a particular type\n"
3682 " -p --property=NAME Show only properties by this name\n"
3683 " -a --all Show all units/properties, including dead/empty ones\n"
3684 " --full Don't ellipsize unit names on output\n"
3685 " --fail When queueing a new job, fail if conflicting jobs are\n"
3687 " -q --quiet Suppress output\n"
3688 " --no-block Do not wait until operation finished\n"
3689 " --system Connect to system bus\n"
3690 " --session Connect to session bus\n"
3691 " --order When generating graph for dot, show only order\n"
3692 " --require When generating graph for dot, show only requirement\n"
3693 " --no-wall Don't send wall message before halt/power-off/reboot\n"
3694 " --global Enable/disable unit files globally\n"
3695 " --no-reload When enabling/disabling unit files, don't reload daemon\n"
3697 " --force When enabling unit files, override existing symlinks\n"
3698 " --defaults When disabling unit files, remove default symlinks only\n\n"
3700 " list-units List units\n"
3701 " start [NAME...] Start (activate) one or more units\n"
3702 " stop [NAME...] Stop (deactivate) one or more units\n"
3703 " reload [NAME...] Reload one or more units\n"
3704 " restart [NAME...] Start or restart one or more units\n"
3705 " try-restart [NAME...] Restart one or more units if active\n"
3706 " reload-or-restart [NAME...] Reload one or more units is possible,\n"
3707 " otherwise start or restart\n"
3708 " reload-or-try-restart [NAME...] Reload one or more units is possible,\n"
3709 " otherwise restart if active\n"
3710 " isolate [NAME] Start one unit and stop all others\n"
3711 " is-active [NAME...] Check whether units are active\n"
3712 " status [NAME...] Show runtime status of one or more units\n"
3713 " show [NAME...|JOB...] Show properties of one or more\n"
3714 " units/jobs or the manager\n"
3715 " reset-maintenance [NAME...] Reset maintenance state for all, one,\n"
3717 " enable [NAME...] Enable one or more unit files\n"
3718 " disable [NAME...] Disable one or more unit files\n"
3719 " is-enabled [NAME...] Check whether unit files are enabled\n"
3720 " load [NAME...] Load one or more units\n"
3721 " list-jobs List jobs\n"
3722 " cancel [JOB...] Cancel all, one, or more jobs\n"
3723 " monitor Monitor unit/job changes\n"
3724 " dump Dump server status\n"
3725 " dot Dump dependency graph for dot(1)\n"
3726 " snapshot [NAME] Create a snapshot\n"
3727 " delete [NAME...] Remove one or more snapshots\n"
3728 " daemon-reload Reload systemd manager configuration\n"
3729 " daemon-reexec Reexecute systemd manager\n"
3730 " daemon-exit Ask the systemd manager to quit\n"
3731 " show-environment Dump environment\n"
3732 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
3733 " unset-environment [NAME...] Unset one or more environment variables\n"
3734 " halt Shut down and halt the system\n"
3735 " poweroff Shut down and power-off the system\n"
3736 " reboot Shut down and reboot the system\n"
3737 " rescue Enter system rescue mode\n"
3738 " emergency Enter system emergency mode\n"
3739 " default Enter system default mode\n",
3740 program_invocation_short_name);
3745 static int halt_help(void) {
3747 printf("%s [OPTIONS...]\n\n"
3748 "%s the system.\n\n"
3749 " --help Show this help\n"
3750 " --halt Halt the machine\n"
3751 " -p --poweroff Switch off the machine\n"
3752 " --reboot Reboot the machine\n"
3753 " -f --force Force immediate halt/power-off/reboot\n"
3754 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
3755 " -d --no-wtmp Don't write wtmp record\n"
3756 " -n --no-sync Don't sync before halt/power-off/reboot\n"
3757 " --no-wall Don't send wall message before halt/power-off/reboot\n",
3758 program_invocation_short_name,
3759 arg_action == ACTION_REBOOT ? "Reboot" :
3760 arg_action == ACTION_POWEROFF ? "Power off" :
3766 static int shutdown_help(void) {
3768 printf("%s [OPTIONS...] [now] [WALL...]\n\n"
3769 "Shut down the system.\n\n"
3770 " --help Show this help\n"
3771 " -H --halt Halt the machine\n"
3772 " -P --poweroff Power-off the machine\n"
3773 " -r --reboot Reboot the machine\n"
3774 " -h Equivalent to --poweroff, overriden by --halt\n"
3775 " -k Don't halt/power-off/reboot, just send warnings\n"
3776 " --no-wall Don't send wall message before halt/power-off/reboot\n",
3777 program_invocation_short_name);
3782 static int telinit_help(void) {
3784 printf("%s [OPTIONS...] {COMMAND}\n\n"
3785 "Send control commands to the init daemon.\n\n"
3786 " --help Show this help\n"
3787 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
3789 " 0 Power-off the machine\n"
3790 " 6 Reboot the machine\n"
3791 " 2, 3, 4, 5 Start runlevelX.target unit\n"
3792 " 1, s, S Enter rescue mode\n"
3793 " q, Q Reload init daemon configuration\n"
3794 " u, U Reexecute init daemon\n",
3795 program_invocation_short_name);
3800 static int runlevel_help(void) {
3802 printf("%s [OPTIONS...]\n\n"
3803 "Prints the previous and current runlevel of the init system.\n\n"
3804 " --help Show this help\n",
3805 program_invocation_short_name);
3810 static int systemctl_parse_argv(int argc, char *argv[]) {
3827 static const struct option options[] = {
3828 { "help", no_argument, NULL, 'h' },
3829 { "type", required_argument, NULL, 't' },
3830 { "property", required_argument, NULL, 'p' },
3831 { "all", no_argument, NULL, 'a' },
3832 { "full", no_argument, NULL, ARG_FULL },
3833 { "fail", no_argument, NULL, ARG_FAIL },
3834 { "session", no_argument, NULL, ARG_SESSION },
3835 { "system", no_argument, NULL, ARG_SYSTEM },
3836 { "global", no_argument, NULL, ARG_GLOBAL },
3837 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
3838 { "no-wall", no_argument, NULL, ARG_NO_WALL },
3839 { "quiet", no_argument, NULL, 'q' },
3840 { "order", no_argument, NULL, ARG_ORDER },
3841 { "require", no_argument, NULL, ARG_REQUIRE },
3842 { "force", no_argument, NULL, ARG_FORCE },
3843 { "no-reload", no_argument, NULL, ARG_NO_RELOAD },
3844 { "defaults", no_argument, NULL, ARG_DEFAULTS },
3845 { NULL, 0, NULL, 0 }
3853 while ((c = getopt_long(argc, argv, "ht:p:aq", options, NULL)) >= 0) {
3868 if (!(l = strv_append(arg_property, optarg)))
3871 strv_free(arg_property);
3874 /* If the user asked for a particular
3875 * property, show it to him, even if it is
3894 arg_session = false;
3898 arg_no_block = true;
3906 arg_dot = DOT_ORDER;
3910 arg_dot = DOT_REQUIRE;
3926 arg_no_reload = true;
3935 arg_defaults = true;
3942 log_error("Unknown option code %c", c);
3950 static int halt_parse_argv(int argc, char *argv[]) {
3959 static const struct option options[] = {
3960 { "help", no_argument, NULL, ARG_HELP },
3961 { "halt", no_argument, NULL, ARG_HALT },
3962 { "poweroff", no_argument, NULL, 'p' },
3963 { "reboot", no_argument, NULL, ARG_REBOOT },
3964 { "force", no_argument, NULL, 'f' },
3965 { "wtmp-only", no_argument, NULL, 'w' },
3966 { "no-wtmp", no_argument, NULL, 'd' },
3967 { "no-sync", no_argument, NULL, 'n' },
3968 { "no-wall", no_argument, NULL, ARG_NO_WALL },
3969 { NULL, 0, NULL, 0 }
3977 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
3978 if (runlevel == '0' || runlevel == '6')
3979 arg_immediate = true;
3981 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
3989 arg_action = ACTION_HALT;
3993 if (arg_action != ACTION_REBOOT)
3994 arg_action = ACTION_POWEROFF;
3998 arg_action = ACTION_REBOOT;
4002 arg_immediate = true;
4023 /* Compatibility nops */
4030 log_error("Unknown option code %c", c);
4035 if (optind < argc) {
4036 log_error("Too many arguments.");
4043 static int shutdown_parse_argv(int argc, char *argv[]) {
4050 static const struct option options[] = {
4051 { "help", no_argument, NULL, ARG_HELP },
4052 { "halt", no_argument, NULL, 'H' },
4053 { "poweroff", no_argument, NULL, 'P' },
4054 { "reboot", no_argument, NULL, 'r' },
4055 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4056 { NULL, 0, NULL, 0 }
4064 while ((c = getopt_long(argc, argv, "HPrhkt:a", options, NULL)) >= 0) {
4072 arg_action = ACTION_HALT;
4076 arg_action = ACTION_POWEROFF;
4080 arg_action = ACTION_REBOOT;
4084 if (arg_action != ACTION_HALT)
4085 arg_action = ACTION_POWEROFF;
4098 /* Compatibility nops */
4105 log_error("Unknown option code %c", c);
4110 if (argc > optind && !streq(argv[optind], "now"))
4111 log_warning("First argument '%s' isn't 'now'. Ignoring.", argv[optind]);
4113 /* We ignore the time argument */
4114 if (argc > optind + 1)
4115 arg_wall = argv + optind + 1;
4122 static int telinit_parse_argv(int argc, char *argv[]) {
4129 static const struct option options[] = {
4130 { "help", no_argument, NULL, ARG_HELP },
4131 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4132 { NULL, 0, NULL, 0 }
4135 static const struct {
4139 { '0', ACTION_POWEROFF },
4140 { '6', ACTION_REBOOT },
4141 { '1', ACTION_RESCUE },
4142 { '2', ACTION_RUNLEVEL2 },
4143 { '3', ACTION_RUNLEVEL3 },
4144 { '4', ACTION_RUNLEVEL4 },
4145 { '5', ACTION_RUNLEVEL5 },
4146 { 's', ACTION_RESCUE },
4147 { 'S', ACTION_RESCUE },
4148 { 'q', ACTION_RELOAD },
4149 { 'Q', ACTION_RELOAD },
4150 { 'u', ACTION_REEXEC },
4151 { 'U', ACTION_REEXEC }
4160 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4175 log_error("Unknown option code %c", c);
4180 if (optind >= argc) {
4185 if (optind + 1 < argc) {
4186 log_error("Too many arguments.");
4190 if (strlen(argv[optind]) != 1) {
4191 log_error("Expected single character argument.");
4195 for (i = 0; i < ELEMENTSOF(table); i++)
4196 if (table[i].from == argv[optind][0])
4199 if (i >= ELEMENTSOF(table)) {
4200 log_error("Unknown command %s.", argv[optind]);
4204 arg_action = table[i].to;
4211 static int runlevel_parse_argv(int argc, char *argv[]) {
4217 static const struct option options[] = {
4218 { "help", no_argument, NULL, ARG_HELP },
4219 { NULL, 0, NULL, 0 }
4227 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4238 log_error("Unknown option code %c", c);
4243 if (optind < argc) {
4244 log_error("Too many arguments.");
4251 static int parse_argv(int argc, char *argv[]) {
4255 if (program_invocation_short_name) {
4257 if (strstr(program_invocation_short_name, "halt")) {
4258 arg_action = ACTION_HALT;
4259 return halt_parse_argv(argc, argv);
4260 } else if (strstr(program_invocation_short_name, "poweroff")) {
4261 arg_action = ACTION_POWEROFF;
4262 return halt_parse_argv(argc, argv);
4263 } else if (strstr(program_invocation_short_name, "reboot")) {
4264 arg_action = ACTION_REBOOT;
4265 return halt_parse_argv(argc, argv);
4266 } else if (strstr(program_invocation_short_name, "shutdown")) {
4267 arg_action = ACTION_POWEROFF;
4268 return shutdown_parse_argv(argc, argv);
4269 } else if (strstr(program_invocation_short_name, "init")) {
4271 if (sd_booted() > 0) {
4272 arg_action = ACTION_INVALID;
4273 return telinit_parse_argv(argc, argv);
4275 /* Hmm, so some other init system is
4276 * running, we need to forward this
4277 * request to it. For now we simply
4278 * guess that it is Upstart. */
4280 execv("/lib/upstart/telinit", argv);
4282 log_error("Couldn't find an alternative telinit implementation to spawn.");
4286 } else if (strstr(program_invocation_short_name, "runlevel")) {
4287 arg_action = ACTION_RUNLEVEL;
4288 return runlevel_parse_argv(argc, argv);
4292 arg_action = ACTION_SYSTEMCTL;
4293 return systemctl_parse_argv(argc, argv);
4296 static int action_to_runlevel(void) {
4298 static const char table[_ACTION_MAX] = {
4299 [ACTION_HALT] = '0',
4300 [ACTION_POWEROFF] = '0',
4301 [ACTION_REBOOT] = '6',
4302 [ACTION_RUNLEVEL2] = '2',
4303 [ACTION_RUNLEVEL3] = '3',
4304 [ACTION_RUNLEVEL4] = '4',
4305 [ACTION_RUNLEVEL5] = '5',
4306 [ACTION_RESCUE] = '1'
4309 assert(arg_action < _ACTION_MAX);
4311 return table[arg_action];
4314 static int talk_upstart(void) {
4315 DBusMessage *m = NULL, *reply = NULL;
4317 int previous, rl, r;
4319 env1_buf[] = "RUNLEVEL=X",
4320 env2_buf[] = "PREVLEVEL=X";
4321 char *env1 = env1_buf, *env2 = env2_buf;
4322 const char *emit = "runlevel";
4323 dbus_bool_t b_false = FALSE;
4324 DBusMessageIter iter, sub;
4325 DBusConnection *bus;
4327 dbus_error_init(&error);
4329 if (!(rl = action_to_runlevel()))
4332 if (utmp_get_runlevel(&previous, NULL) < 0)
4335 if (!(bus = dbus_connection_open_private("unix:abstract=/com/ubuntu/upstart", &error))) {
4336 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
4341 log_error("Failed to connect to Upstart bus: %s", error.message);
4346 if ((r = bus_check_peercred(bus)) < 0) {
4347 log_error("Failed to verify owner of bus.");
4351 if (!(m = dbus_message_new_method_call(
4352 "com.ubuntu.Upstart",
4353 "/com/ubuntu/Upstart",
4354 "com.ubuntu.Upstart0_6",
4357 log_error("Could not allocate message.");
4362 dbus_message_iter_init_append(m, &iter);
4364 env1_buf[sizeof(env1_buf)-2] = rl;
4365 env2_buf[sizeof(env2_buf)-2] = previous;
4367 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
4368 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
4369 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
4370 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
4371 !dbus_message_iter_close_container(&iter, &sub) ||
4372 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
4373 log_error("Could not append arguments to message.");
4378 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
4380 if (error_is_no_service(&error)) {
4385 log_error("Failed to issue method call: %s", error.message);
4394 dbus_message_unref(m);
4397 dbus_message_unref(reply);
4400 dbus_connection_close(bus);
4401 dbus_connection_unref(bus);
4404 dbus_error_free(&error);
4409 static int talk_initctl(void) {
4410 struct init_request request;
4414 if (!(rl = action_to_runlevel()))
4418 request.magic = INIT_MAGIC;
4419 request.sleeptime = 0;
4420 request.cmd = INIT_CMD_RUNLVL;
4421 request.runlevel = rl;
4423 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
4425 if (errno == ENOENT)
4428 log_error("Failed to open "INIT_FIFO": %m");
4433 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
4434 close_nointr_nofail(fd);
4437 log_error("Failed to write to "INIT_FIFO": %m");
4438 return errno ? -errno : -EIO;
4444 static int systemctl_main(DBusConnection *bus, int argc, char *argv[], DBusError *error) {
4446 static const struct {
4454 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
4456 { "list-units", LESS, 1, list_units },
4457 { "list-jobs", EQUAL, 1, list_jobs },
4458 { "clear-jobs", EQUAL, 1, daemon_reload },
4459 { "load", MORE, 2, load_unit },
4460 { "cancel", MORE, 2, cancel_job },
4461 { "start", MORE, 2, start_unit },
4462 { "stop", MORE, 2, start_unit },
4463 { "reload", MORE, 2, start_unit },
4464 { "restart", MORE, 2, start_unit },
4465 { "try-restart", MORE, 2, start_unit },
4466 { "reload-or-restart", MORE, 2, start_unit },
4467 { "reload-or-try-restart", MORE, 2, start_unit },
4468 { "force-reload", MORE, 2, start_unit }, /* For compatibility with SysV */
4469 { "condrestart", MORE, 2, start_unit }, /* For compatibility with RH */
4470 { "isolate", EQUAL, 2, start_unit },
4471 { "is-active", MORE, 2, check_unit },
4472 { "check", MORE, 2, check_unit },
4473 { "show", MORE, 1, show },
4474 { "status", MORE, 2, show },
4475 { "monitor", EQUAL, 1, monitor },
4476 { "dump", EQUAL, 1, dump },
4477 { "dot", EQUAL, 1, dot },
4478 { "snapshot", LESS, 2, snapshot },
4479 { "delete", MORE, 2, delete_snapshot },
4480 { "daemon-reload", EQUAL, 1, daemon_reload },
4481 { "daemon-reexec", EQUAL, 1, daemon_reload },
4482 { "daemon-exit", EQUAL, 1, daemon_reload },
4483 { "show-environment", EQUAL, 1, show_enviroment },
4484 { "set-environment", MORE, 2, set_environment },
4485 { "unset-environment", MORE, 2, set_environment },
4486 { "halt", EQUAL, 1, start_special },
4487 { "poweroff", EQUAL, 1, start_special },
4488 { "reboot", EQUAL, 1, start_special },
4489 { "default", EQUAL, 1, start_special },
4490 { "rescue", EQUAL, 1, start_special },
4491 { "emergency", EQUAL, 1, start_special },
4492 { "reset-maintenance", MORE, 1, reset_maintenance },
4493 { "enable", MORE, 2, enable_unit },
4494 { "disable", MORE, 2, enable_unit },
4495 { "is-enabled", MORE, 2, enable_unit }
4505 left = argc - optind;
4508 /* Special rule: no arguments means "list-units" */
4511 if (streq(argv[optind], "help")) {
4516 for (i = 0; i < ELEMENTSOF(verbs); i++)
4517 if (streq(argv[optind], verbs[i].verb))
4520 if (i >= ELEMENTSOF(verbs)) {
4521 log_error("Unknown operation %s", argv[optind]);
4526 switch (verbs[i].argc_cmp) {
4529 if (left != verbs[i].argc) {
4530 log_error("Invalid number of arguments.");
4537 if (left < verbs[i].argc) {
4538 log_error("Too few arguments.");
4545 if (left > verbs[i].argc) {
4546 log_error("Too many arguments.");
4553 assert_not_reached("Unknown comparison operator.");
4556 /* Require a bus connection for all operations but
4558 if (!streq(verbs[i].verb, "enable") &&
4559 !streq(verbs[i].verb, "disable") &&
4561 log_error("Failed to get D-Bus connection: %s", error->message);
4565 return verbs[i].dispatch(bus, argv + optind, left);
4568 static int reload_with_fallback(DBusConnection *bus) {
4571 /* First, try systemd via D-Bus. */
4572 if (daemon_reload(bus, NULL, 0) > 0)
4576 /* Nothing else worked, so let's try signals */
4577 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
4579 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
4580 log_error("kill() failed: %m");
4587 static int start_with_fallback(DBusConnection *bus) {
4590 /* First, try systemd via D-Bus. */
4591 if (start_unit(bus, NULL, 0) > 0)
4595 /* Hmm, talking to systemd via D-Bus didn't work. Then
4596 * let's try to talk to Upstart via D-Bus. */
4597 if (talk_upstart() > 0)
4600 /* Nothing else worked, so let's try
4602 if (talk_initctl() != 0)
4605 log_error("Failed to talk to init daemon.");
4609 warn_wall(arg_action);
4613 static int halt_main(DBusConnection *bus) {
4616 if (geteuid() != 0) {
4617 log_error("Must to be root.");
4621 if (!arg_dry && !arg_immediate)
4622 return start_with_fallback(bus);
4625 if ((r = utmp_put_shutdown(0)) < 0)
4626 log_warning("Failed to write utmp record: %s", strerror(-r));
4634 /* Make sure C-A-D is handled by the kernel from this
4636 reboot(RB_ENABLE_CAD);
4638 switch (arg_action) {
4641 log_info("Halting.");
4642 reboot(RB_HALT_SYSTEM);
4645 case ACTION_POWEROFF:
4646 log_info("Powering off.");
4647 reboot(RB_POWER_OFF);
4651 log_info("Rebooting.");
4652 reboot(RB_AUTOBOOT);
4656 assert_not_reached("Unknown halt action.");
4659 /* We should never reach this. */
4663 static int runlevel_main(void) {
4664 int r, runlevel, previous;
4666 if ((r = utmp_get_runlevel(&runlevel, &previous)) < 0) {
4667 printf("unknown\n");
4672 previous <= 0 ? 'N' : previous,
4673 runlevel <= 0 ? 'N' : runlevel);
4678 int main(int argc, char*argv[]) {
4680 DBusConnection *bus = NULL;
4683 dbus_error_init(&error);
4685 log_parse_environment();
4687 if ((r = parse_argv(argc, argv)) < 0)
4694 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
4695 * let's shortcut this */
4696 if (arg_action == ACTION_RUNLEVEL) {
4697 retval = runlevel_main() < 0;
4701 bus_connect(arg_session ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &bus, &private_bus, &error);
4703 switch (arg_action) {
4705 case ACTION_SYSTEMCTL: {
4706 retval = systemctl_main(bus, argc, argv, &error) < 0;
4711 case ACTION_POWEROFF:
4713 retval = halt_main(bus) < 0;
4716 case ACTION_RUNLEVEL2:
4717 case ACTION_RUNLEVEL3:
4718 case ACTION_RUNLEVEL4:
4719 case ACTION_RUNLEVEL5:
4721 case ACTION_EMERGENCY:
4722 case ACTION_DEFAULT:
4723 retval = start_with_fallback(bus) < 0;
4728 retval = reload_with_fallback(bus) < 0;
4731 case ACTION_INVALID:
4732 case ACTION_RUNLEVEL:
4734 assert_not_reached("Unknown action");
4740 dbus_connection_close(bus);
4741 dbus_connection_unref(bus);
4744 dbus_error_free(&error);
4748 strv_free(arg_property);