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 if ((r = wait_for_jobs(bus, s)) < 0)
1186 static int start_special(DBusConnection *bus, char **args, unsigned n) {
1192 r = start_unit(bus, args, n);
1195 warn_wall(verb_to_action(args[0]));
1200 static int check_unit(DBusConnection *bus, char **args, unsigned n) {
1201 DBusMessage *m = NULL, *reply = NULL;
1203 *interface = "org.freedesktop.systemd1.Unit",
1204 *property = "ActiveState";
1205 int r = -EADDRNOTAVAIL;
1212 dbus_error_init(&error);
1214 for (i = 1; i < n; i++) {
1215 const char *path = NULL;
1217 DBusMessageIter iter, sub;
1219 if (!(m = dbus_message_new_method_call(
1220 "org.freedesktop.systemd1",
1221 "/org/freedesktop/systemd1",
1222 "org.freedesktop.systemd1.Manager",
1224 log_error("Could not allocate message.");
1229 if (!dbus_message_append_args(m,
1230 DBUS_TYPE_STRING, &args[i],
1231 DBUS_TYPE_INVALID)) {
1232 log_error("Could not append arguments to message.");
1237 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1239 /* Hmm, cannot figure out anything about this unit... */
1243 dbus_error_free(&error);
1247 if (!dbus_message_get_args(reply, &error,
1248 DBUS_TYPE_OBJECT_PATH, &path,
1249 DBUS_TYPE_INVALID)) {
1250 log_error("Failed to parse reply: %s", error.message);
1255 dbus_message_unref(m);
1256 if (!(m = dbus_message_new_method_call(
1257 "org.freedesktop.systemd1",
1259 "org.freedesktop.DBus.Properties",
1261 log_error("Could not allocate message.");
1266 if (!dbus_message_append_args(m,
1267 DBUS_TYPE_STRING, &interface,
1268 DBUS_TYPE_STRING, &property,
1269 DBUS_TYPE_INVALID)) {
1270 log_error("Could not append arguments to message.");
1275 dbus_message_unref(reply);
1276 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
1277 log_error("Failed to issue method call: %s", error.message);
1282 if (!dbus_message_iter_init(reply, &iter) ||
1283 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
1284 log_error("Failed to parse reply.");
1289 dbus_message_iter_recurse(&iter, &sub);
1291 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
1292 log_error("Failed to parse reply.");
1297 dbus_message_iter_get_basic(&sub, &state);
1302 if (streq(state, "active") || startswith(state, "reloading"))
1305 dbus_message_unref(m);
1306 dbus_message_unref(reply);
1312 dbus_message_unref(m);
1315 dbus_message_unref(reply);
1317 dbus_error_free(&error);
1322 typedef struct ExecStatusInfo {
1328 usec_t start_timestamp;
1329 usec_t exit_timestamp;
1334 LIST_FIELDS(struct ExecStatusInfo, exec);
1337 static void exec_status_info_free(ExecStatusInfo *i) {
1345 static int exec_status_info_deserialize(DBusMessageIter *sub, ExecStatusInfo *i) {
1346 uint64_t start_timestamp, exit_timestamp;
1347 DBusMessageIter sub2, sub3;
1351 int32_t code, status;
1357 if (dbus_message_iter_get_arg_type(sub) != DBUS_TYPE_STRUCT)
1360 dbus_message_iter_recurse(sub, &sub2);
1362 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, true) < 0)
1365 if (!(i->path = strdup(path)))
1368 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_ARRAY ||
1369 dbus_message_iter_get_element_type(&sub2) != DBUS_TYPE_STRING)
1373 dbus_message_iter_recurse(&sub2, &sub3);
1374 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1375 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1376 dbus_message_iter_next(&sub3);
1381 if (!(i->argv = new0(char*, n+1)))
1385 dbus_message_iter_recurse(&sub2, &sub3);
1386 while (dbus_message_iter_get_arg_type(&sub3) != DBUS_TYPE_INVALID) {
1389 assert(dbus_message_iter_get_arg_type(&sub3) == DBUS_TYPE_STRING);
1390 dbus_message_iter_get_basic(&sub3, &s);
1391 dbus_message_iter_next(&sub3);
1393 if (!(i->argv[n++] = strdup(s)))
1397 if (!dbus_message_iter_next(&sub2) ||
1398 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_BOOLEAN, &ignore, true) < 0 ||
1399 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &start_timestamp, true) < 0 ||
1400 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &exit_timestamp, true) < 0 ||
1401 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, true) < 0 ||
1402 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &code, true) < 0 ||
1403 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_INT32, &status, false) < 0)
1407 i->start_timestamp = (usec_t) start_timestamp;
1408 i->exit_timestamp = (usec_t) exit_timestamp;
1409 i->pid = (pid_t) pid;
1416 typedef struct UnitStatusInfo {
1418 const char *load_state;
1419 const char *active_state;
1420 const char *sub_state;
1422 const char *description;
1425 const char *default_control_group;
1427 bool need_daemon_reload;
1432 const char *status_text;
1435 usec_t start_timestamp;
1436 usec_t exit_timestamp;
1438 int exit_code, exit_status;
1441 unsigned n_accepted;
1442 unsigned n_connections;
1446 const char *sysfs_path;
1448 /* Mount, Automount */
1454 LIST_HEAD(ExecStatusInfo, exec);
1457 static void print_status_info(UnitStatusInfo *i) {
1459 const char *on, *off, *ss;
1463 /* This shows pretty information about a unit. See
1464 * print_property() for a low-level property printer */
1466 printf("%s", strna(i->id));
1468 if (i->description && !streq_ptr(i->id, i->description))
1469 printf(" - %s", i->description);
1473 if (streq_ptr(i->load_state, "failed")) {
1474 on = ansi_highlight(true);
1475 off = ansi_highlight(false);
1480 printf("\t Loaded: %s%s%s (%s)\n", on, strna(i->load_state), off, i->path);
1482 printf("\t Loaded: %s%s%s\n", on, strna(i->load_state), off);
1484 ss = streq_ptr(i->active_state, i->sub_state) ? NULL : i->sub_state;
1486 if (streq_ptr(i->active_state, "maintenance")) {
1487 on = ansi_highlight(true);
1488 off = ansi_highlight(false);
1489 } else if (streq_ptr(i->active_state, "active") || streq_ptr(i->active_state, "reloading")) {
1490 on = ansi_highlight_green(true);
1491 off = ansi_highlight_green(false);
1496 printf("\t Active: %s%s (%s)%s\n",
1498 strna(i->active_state),
1502 printf("\t Active: %s%s%s\n",
1504 strna(i->active_state),
1508 printf("\t Device: %s\n", i->sysfs_path);
1510 printf("\t Where: %s\n", i->where);
1512 printf("\t What: %s\n", i->what);
1515 printf("\tAccepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
1517 LIST_FOREACH(exec, p, i->exec) {
1520 /* Only show exited processes here */
1524 t = strv_join(p->argv, " ");
1525 printf("\t Exited: %u (%s, code=%s, ", p->pid, strna(t), sigchld_code_to_string(p->code));
1528 if (p->code == CLD_EXITED)
1529 printf("status=%i", p->status);
1531 printf("signal=%s", signal_to_string(p->status));
1534 if (i->main_pid == p->pid &&
1535 i->start_timestamp == p->start_timestamp &&
1536 i->exit_timestamp == p->start_timestamp)
1537 /* Let's not show this twice */
1540 if (p->pid == i->control_pid)
1544 if (i->main_pid > 0 || i->control_pid > 0) {
1547 if (i->main_pid > 0) {
1548 printf(" Main: %u", (unsigned) i->main_pid);
1552 get_process_name(i->main_pid, &t);
1557 } else if (i->exit_code > 0) {
1558 printf(" (code=%s, ", sigchld_code_to_string(i->exit_code));
1560 if (i->exit_code == CLD_EXITED)
1561 printf("status=%i", i->exit_status);
1563 printf("signal=%s", signal_to_string(i->exit_status));
1568 if (i->main_pid > 0 && i->control_pid > 0)
1571 if (i->control_pid > 0) {
1574 printf(" Control: %u", (unsigned) i->control_pid);
1576 get_process_name(i->control_pid, &t);
1587 printf("\t Status: \"%s\"\n", i->status_text);
1589 if (i->default_control_group) {
1592 printf("\t CGroup: %s\n", i->default_control_group);
1594 if ((c = columns()) > 18)
1599 show_cgroup_by_path(i->default_control_group, "\t\t ", c);
1602 if (i->need_daemon_reload)
1603 printf("\n%sWarning:%s Unit file changed on disk, 'systemctl %s daemon-reload' recommended.\n",
1604 ansi_highlight(true),
1605 ansi_highlight(false),
1606 arg_session ? "--session" : "--system");
1609 static int status_property(const char *name, DBusMessageIter *iter, UnitStatusInfo *i) {
1611 switch (dbus_message_iter_get_arg_type(iter)) {
1613 case DBUS_TYPE_STRING: {
1616 dbus_message_iter_get_basic(iter, &s);
1619 if (streq(name, "Id"))
1621 else if (streq(name, "LoadState"))
1623 else if (streq(name, "ActiveState"))
1624 i->active_state = s;
1625 else if (streq(name, "SubState"))
1627 else if (streq(name, "Description"))
1629 else if (streq(name, "FragmentPath"))
1631 else if (streq(name, "SysVPath"))
1633 else if (streq(name, "DefaultControlGroup"))
1634 i->default_control_group = s;
1635 else if (streq(name, "StatusText"))
1637 else if (streq(name, "SysFSPath"))
1639 else if (streq(name, "Where"))
1641 else if (streq(name, "What"))
1648 case DBUS_TYPE_BOOLEAN: {
1651 dbus_message_iter_get_basic(iter, &b);
1653 if (streq(name, "Accept"))
1655 else if (streq(name, "NeedDaemonReload"))
1656 i->need_daemon_reload = b;
1661 case DBUS_TYPE_UINT32: {
1664 dbus_message_iter_get_basic(iter, &u);
1666 if (streq(name, "MainPID")) {
1668 i->main_pid = (pid_t) u;
1671 } else if (streq(name, "ControlPID"))
1672 i->control_pid = (pid_t) u;
1673 else if (streq(name, "ExecMainPID")) {
1675 i->main_pid = (pid_t) u;
1676 } else if (streq(name, "NAccepted"))
1678 else if (streq(name, "NConnections"))
1679 i->n_connections = u;
1684 case DBUS_TYPE_INT32: {
1687 dbus_message_iter_get_basic(iter, &j);
1689 if (streq(name, "ExecMainCode"))
1690 i->exit_code = (int) j;
1691 else if (streq(name, "ExecMainStatus"))
1692 i->exit_status = (int) j;
1697 case DBUS_TYPE_UINT64: {
1700 dbus_message_iter_get_basic(iter, &u);
1702 if (streq(name, "ExecMainStartTimestamp"))
1703 i->start_timestamp = (usec_t) u;
1704 else if (streq(name, "ExecMainExitTimestamp"))
1705 i->exit_timestamp = (usec_t) u;
1710 case DBUS_TYPE_ARRAY: {
1712 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT &&
1713 startswith(name, "Exec")) {
1714 DBusMessageIter sub;
1716 dbus_message_iter_recurse(iter, &sub);
1717 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1718 ExecStatusInfo *info;
1721 if (!(info = new0(ExecStatusInfo, 1)))
1724 if ((r = exec_status_info_deserialize(&sub, info)) < 0) {
1729 LIST_PREPEND(ExecStatusInfo, exec, i->exec, info);
1731 dbus_message_iter_next(&sub);
1742 static int print_property(const char *name, DBusMessageIter *iter) {
1746 /* This is a low-level property printer, see
1747 * print_status_info() for the nicer output */
1749 if (arg_property && !strv_find(arg_property, name))
1752 switch (dbus_message_iter_get_arg_type(iter)) {
1754 case DBUS_TYPE_STRING: {
1756 dbus_message_iter_get_basic(iter, &s);
1758 if (arg_all || s[0])
1759 printf("%s=%s\n", name, s);
1764 case DBUS_TYPE_BOOLEAN: {
1766 dbus_message_iter_get_basic(iter, &b);
1767 printf("%s=%s\n", name, yes_no(b));
1772 case DBUS_TYPE_UINT64: {
1774 dbus_message_iter_get_basic(iter, &u);
1776 /* Yes, heuristics! But we can change this check
1777 * should it turn out to not be sufficient */
1779 if (strstr(name, "Timestamp")) {
1780 char timestamp[FORMAT_TIMESTAMP_MAX], *t;
1782 if ((t = format_timestamp(timestamp, sizeof(timestamp), u)) || arg_all)
1783 printf("%s=%s\n", name, strempty(t));
1784 } else if (strstr(name, "USec")) {
1785 char timespan[FORMAT_TIMESPAN_MAX];
1787 printf("%s=%s\n", name, format_timespan(timespan, sizeof(timespan), u));
1789 printf("%s=%llu\n", name, (unsigned long long) u);
1794 case DBUS_TYPE_UINT32: {
1796 dbus_message_iter_get_basic(iter, &u);
1798 if (strstr(name, "UMask") || strstr(name, "Mode"))
1799 printf("%s=%04o\n", name, u);
1801 printf("%s=%u\n", name, (unsigned) u);
1806 case DBUS_TYPE_INT32: {
1808 dbus_message_iter_get_basic(iter, &i);
1810 printf("%s=%i\n", name, (int) i);
1814 case DBUS_TYPE_STRUCT: {
1815 DBusMessageIter sub;
1816 dbus_message_iter_recurse(iter, &sub);
1818 if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_UINT32 && streq(name, "Job")) {
1821 dbus_message_iter_get_basic(&sub, &u);
1824 printf("%s=%u\n", name, (unsigned) u);
1826 printf("%s=\n", name);
1829 } else if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING && streq(name, "Unit")) {
1832 dbus_message_iter_get_basic(&sub, &s);
1834 if (arg_all || s[0])
1835 printf("%s=%s\n", name, s);
1843 case DBUS_TYPE_ARRAY:
1845 if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRING) {
1846 DBusMessageIter sub;
1849 dbus_message_iter_recurse(iter, &sub);
1851 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1852 printf("%s=", name);
1854 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1857 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
1858 dbus_message_iter_get_basic(&sub, &s);
1859 printf("%s%s", space ? " " : "", s);
1862 dbus_message_iter_next(&sub);
1870 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_BYTE) {
1871 DBusMessageIter sub;
1873 dbus_message_iter_recurse(iter, &sub);
1875 dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1876 printf("%s=", name);
1878 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1881 assert(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_BYTE);
1882 dbus_message_iter_get_basic(&sub, &u);
1885 dbus_message_iter_next(&sub);
1893 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Paths")) {
1894 DBusMessageIter sub, sub2;
1896 dbus_message_iter_recurse(iter, &sub);
1897 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1898 const char *type, *path;
1900 dbus_message_iter_recurse(&sub, &sub2);
1902 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &type, true) >= 0 &&
1903 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &path, false) >= 0)
1904 printf("%s=%s\n", type, path);
1906 dbus_message_iter_next(&sub);
1911 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && streq(name, "Timers")) {
1912 DBusMessageIter sub, sub2;
1914 dbus_message_iter_recurse(iter, &sub);
1915 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1917 uint64_t value, next_elapse;
1919 dbus_message_iter_recurse(&sub, &sub2);
1921 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &base, true) >= 0 &&
1922 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &value, true) >= 0 &&
1923 bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT64, &next_elapse, false) >= 0) {
1924 char timespan1[FORMAT_TIMESPAN_MAX], timespan2[FORMAT_TIMESPAN_MAX];
1926 printf("%s={ value=%s ; next_elapse=%s }\n",
1928 format_timespan(timespan1, sizeof(timespan1), value),
1929 format_timespan(timespan2, sizeof(timespan2), next_elapse));
1932 dbus_message_iter_next(&sub);
1937 } else if (dbus_message_iter_get_element_type(iter) == DBUS_TYPE_STRUCT && startswith(name, "Exec")) {
1938 DBusMessageIter sub;
1940 dbus_message_iter_recurse(iter, &sub);
1941 while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRUCT) {
1942 ExecStatusInfo info;
1945 if (exec_status_info_deserialize(&sub, &info) >= 0) {
1946 char timestamp1[FORMAT_TIMESTAMP_MAX], timestamp2[FORMAT_TIMESTAMP_MAX];
1949 t = strv_join(info.argv, " ");
1951 printf("%s={ path=%s ; argv[]=%s ; ignore=%s ; start_time=[%s] ; stop_time=[%s] ; pid=%u ; code=%s ; status=%i%s%s }\n",
1955 yes_no(info.ignore),
1956 strna(format_timestamp(timestamp1, sizeof(timestamp1), info.start_timestamp)),
1957 strna(format_timestamp(timestamp2, sizeof(timestamp2), info.exit_timestamp)),
1958 (unsigned) info. pid,
1959 sigchld_code_to_string(info.code),
1961 info.code == CLD_EXITED ? "" : "/",
1962 strempty(info.code == CLD_EXITED ? NULL : signal_to_string(info.status)));
1968 strv_free(info.argv);
1970 dbus_message_iter_next(&sub);
1980 printf("%s=[unprintable]\n", name);
1985 static int show_one(DBusConnection *bus, const char *path, bool show_properties, bool *new_line) {
1986 DBusMessage *m = NULL, *reply = NULL;
1987 const char *interface = "";
1990 DBusMessageIter iter, sub, sub2, sub3;
1991 UnitStatusInfo info;
1999 dbus_error_init(&error);
2001 if (!(m = dbus_message_new_method_call(
2002 "org.freedesktop.systemd1",
2004 "org.freedesktop.DBus.Properties",
2006 log_error("Could not allocate message.");
2011 if (!dbus_message_append_args(m,
2012 DBUS_TYPE_STRING, &interface,
2013 DBUS_TYPE_INVALID)) {
2014 log_error("Could not append arguments to message.");
2019 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2020 log_error("Failed to issue method call: %s", error.message);
2025 if (!dbus_message_iter_init(reply, &iter) ||
2026 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
2027 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_DICT_ENTRY) {
2028 log_error("Failed to parse reply.");
2033 dbus_message_iter_recurse(&iter, &sub);
2040 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
2043 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_DICT_ENTRY) {
2044 log_error("Failed to parse reply.");
2049 dbus_message_iter_recurse(&sub, &sub2);
2051 if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &name, true) < 0) {
2052 log_error("Failed to parse reply.");
2057 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_VARIANT) {
2058 log_error("Failed to parse reply.");
2063 dbus_message_iter_recurse(&sub2, &sub3);
2065 if (show_properties)
2066 r = print_property(name, &sub3);
2068 r = status_property(name, &sub3, &info);
2071 log_error("Failed to parse reply.");
2076 dbus_message_iter_next(&sub);
2079 if (!show_properties)
2080 print_status_info(&info);
2082 while ((p = info.exec)) {
2083 LIST_REMOVE(ExecStatusInfo, exec, info.exec, p);
2084 exec_status_info_free(p);
2091 dbus_message_unref(m);
2094 dbus_message_unref(reply);
2096 dbus_error_free(&error);
2101 static int show(DBusConnection *bus, char **args, unsigned n) {
2102 DBusMessage *m = NULL, *reply = NULL;
2106 bool show_properties, new_line = false;
2111 dbus_error_init(&error);
2113 show_properties = !streq(args[0], "status");
2115 if (show_properties && n <= 1) {
2116 /* If not argument is specified inspect the manager
2119 r = show_one(bus, "/org/freedesktop/systemd1", show_properties, &new_line);
2123 for (i = 1; i < n; i++) {
2124 const char *path = NULL;
2127 if (safe_atou32(args[i], &id) < 0) {
2129 /* Interpret as unit name */
2131 if (!(m = dbus_message_new_method_call(
2132 "org.freedesktop.systemd1",
2133 "/org/freedesktop/systemd1",
2134 "org.freedesktop.systemd1.Manager",
2136 log_error("Could not allocate message.");
2141 if (!dbus_message_append_args(m,
2142 DBUS_TYPE_STRING, &args[i],
2143 DBUS_TYPE_INVALID)) {
2144 log_error("Could not append arguments to message.");
2149 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2151 if (!dbus_error_has_name(&error, DBUS_ERROR_ACCESS_DENIED)) {
2152 log_error("Failed to issue method call: %s", error.message);
2157 dbus_error_free(&error);
2159 dbus_message_unref(m);
2160 if (!(m = dbus_message_new_method_call(
2161 "org.freedesktop.systemd1",
2162 "/org/freedesktop/systemd1",
2163 "org.freedesktop.systemd1.Manager",
2165 log_error("Could not allocate message.");
2170 if (!dbus_message_append_args(m,
2171 DBUS_TYPE_STRING, &args[i],
2172 DBUS_TYPE_INVALID)) {
2173 log_error("Could not append arguments to message.");
2178 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2179 log_error("Failed to issue method call: %s", error.message);
2185 } else if (show_properties) {
2187 /* Interpret as job id */
2189 if (!(m = dbus_message_new_method_call(
2190 "org.freedesktop.systemd1",
2191 "/org/freedesktop/systemd1",
2192 "org.freedesktop.systemd1.Manager",
2194 log_error("Could not allocate message.");
2199 if (!dbus_message_append_args(m,
2200 DBUS_TYPE_UINT32, &id,
2201 DBUS_TYPE_INVALID)) {
2202 log_error("Could not append arguments to message.");
2207 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2208 log_error("Failed to issue method call: %s", error.message);
2214 /* Interpret as PID */
2216 if (!(m = dbus_message_new_method_call(
2217 "org.freedesktop.systemd1",
2218 "/org/freedesktop/systemd1",
2219 "org.freedesktop.systemd1.Manager",
2221 log_error("Could not allocate message.");
2226 if (!dbus_message_append_args(m,
2227 DBUS_TYPE_UINT32, &id,
2228 DBUS_TYPE_INVALID)) {
2229 log_error("Could not append arguments to message.");
2234 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2235 log_error("Failed to issue method call: %s", error.message);
2241 if (!dbus_message_get_args(reply, &error,
2242 DBUS_TYPE_OBJECT_PATH, &path,
2243 DBUS_TYPE_INVALID)) {
2244 log_error("Failed to parse reply: %s", error.message);
2249 if ((r = show_one(bus, path, show_properties, &new_line)) < 0)
2252 dbus_message_unref(m);
2253 dbus_message_unref(reply);
2261 dbus_message_unref(m);
2264 dbus_message_unref(reply);
2266 dbus_error_free(&error);
2271 static DBusHandlerResult monitor_filter(DBusConnection *connection, DBusMessage *message, void *data) {
2273 DBusMessage *m = NULL, *reply = NULL;
2278 dbus_error_init(&error);
2280 log_debug("Got D-Bus request: %s.%s() on %s",
2281 dbus_message_get_interface(message),
2282 dbus_message_get_member(message),
2283 dbus_message_get_path(message));
2285 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
2286 log_error("Warning! D-Bus connection terminated.");
2287 dbus_connection_close(connection);
2289 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitNew") ||
2290 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "UnitRemoved")) {
2291 const char *id, *path;
2293 if (!dbus_message_get_args(message, &error,
2294 DBUS_TYPE_STRING, &id,
2295 DBUS_TYPE_OBJECT_PATH, &path,
2297 log_error("Failed to parse message: %s", error.message);
2298 else if (streq(dbus_message_get_member(message), "UnitNew"))
2299 printf("Unit %s added.\n", id);
2301 printf("Unit %s removed.\n", id);
2303 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobNew") ||
2304 dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "JobRemoved")) {
2308 if (!dbus_message_get_args(message, &error,
2309 DBUS_TYPE_UINT32, &id,
2310 DBUS_TYPE_OBJECT_PATH, &path,
2312 log_error("Failed to parse message: %s", error.message);
2313 else if (streq(dbus_message_get_member(message), "JobNew"))
2314 printf("Job %u added.\n", id);
2316 printf("Job %u removed.\n", id);
2319 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Unit", "Changed") ||
2320 dbus_message_is_signal(message, "org.freedesktop.systemd1.Job", "Changed")) {
2322 const char *path, *interface, *property = "Id";
2323 DBusMessageIter iter, sub;
2325 path = dbus_message_get_path(message);
2326 interface = dbus_message_get_interface(message);
2328 if (!(m = dbus_message_new_method_call(
2329 "org.freedesktop.systemd1",
2331 "org.freedesktop.DBus.Properties",
2333 log_error("Could not allocate message.");
2337 if (!dbus_message_append_args(m,
2338 DBUS_TYPE_STRING, &interface,
2339 DBUS_TYPE_STRING, &property,
2340 DBUS_TYPE_INVALID)) {
2341 log_error("Could not append arguments to message.");
2345 if (!(reply = dbus_connection_send_with_reply_and_block(connection, m, -1, &error))) {
2346 log_error("Failed to issue method call: %s", error.message);
2350 if (!dbus_message_iter_init(reply, &iter) ||
2351 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2352 log_error("Failed to parse reply.");
2356 dbus_message_iter_recurse(&iter, &sub);
2358 if (streq(interface, "org.freedesktop.systemd1.Unit")) {
2361 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2362 log_error("Failed to parse reply.");
2366 dbus_message_iter_get_basic(&sub, &id);
2367 printf("Unit %s changed.\n", id);
2371 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32) {
2372 log_error("Failed to parse reply.");
2376 dbus_message_iter_get_basic(&sub, &id);
2377 printf("Job %u changed.\n", id);
2383 dbus_message_unref(m);
2386 dbus_message_unref(reply);
2388 dbus_error_free(&error);
2389 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2393 dbus_message_unref(m);
2396 dbus_message_unref(reply);
2398 dbus_error_free(&error);
2399 return DBUS_HANDLER_RESULT_NEED_MEMORY;
2402 static int monitor(DBusConnection *bus, char **args, unsigned n) {
2403 DBusMessage *m = NULL, *reply = NULL;
2407 dbus_error_init(&error);
2410 dbus_bus_add_match(bus,
2412 "sender='org.freedesktop.systemd1',"
2413 "interface='org.freedesktop.systemd1.Manager',"
2414 "path='/org/freedesktop/systemd1'",
2417 if (dbus_error_is_set(&error)) {
2418 log_error("Failed to add match: %s", error.message);
2423 dbus_bus_add_match(bus,
2425 "sender='org.freedesktop.systemd1',"
2426 "interface='org.freedesktop.systemd1.Unit',"
2430 if (dbus_error_is_set(&error)) {
2431 log_error("Failed to add match: %s", error.message);
2436 dbus_bus_add_match(bus,
2438 "sender='org.freedesktop.systemd1',"
2439 "interface='org.freedesktop.systemd1.Job',"
2443 if (dbus_error_is_set(&error)) {
2444 log_error("Failed to add match: %s", error.message);
2450 if (!dbus_connection_add_filter(bus, monitor_filter, NULL, NULL)) {
2451 log_error("Failed to add filter.");
2456 if (!(m = dbus_message_new_method_call(
2457 "org.freedesktop.systemd1",
2458 "/org/freedesktop/systemd1",
2459 "org.freedesktop.systemd1.Manager",
2461 log_error("Could not allocate message.");
2466 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2467 log_error("Failed to issue method call: %s", error.message);
2472 while (dbus_connection_read_write_dispatch(bus, -1))
2479 /* This is slightly dirty, since we don't undo the filter or the matches. */
2482 dbus_message_unref(m);
2485 dbus_message_unref(reply);
2487 dbus_error_free(&error);
2492 static int dump(DBusConnection *bus, char **args, unsigned n) {
2493 DBusMessage *m = NULL, *reply = NULL;
2498 dbus_error_init(&error);
2500 if (!(m = dbus_message_new_method_call(
2501 "org.freedesktop.systemd1",
2502 "/org/freedesktop/systemd1",
2503 "org.freedesktop.systemd1.Manager",
2505 log_error("Could not allocate message.");
2509 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2510 log_error("Failed to issue method call: %s", error.message);
2515 if (!dbus_message_get_args(reply, &error,
2516 DBUS_TYPE_STRING, &text,
2517 DBUS_TYPE_INVALID)) {
2518 log_error("Failed to parse reply: %s", error.message);
2523 fputs(text, stdout);
2529 dbus_message_unref(m);
2532 dbus_message_unref(reply);
2534 dbus_error_free(&error);
2539 static int snapshot(DBusConnection *bus, char **args, unsigned n) {
2540 DBusMessage *m = NULL, *reply = NULL;
2543 const char *name = "", *path, *id;
2544 dbus_bool_t cleanup = FALSE;
2545 DBusMessageIter iter, sub;
2547 *interface = "org.freedesktop.systemd1.Unit",
2550 dbus_error_init(&error);
2552 if (!(m = dbus_message_new_method_call(
2553 "org.freedesktop.systemd1",
2554 "/org/freedesktop/systemd1",
2555 "org.freedesktop.systemd1.Manager",
2556 "CreateSnapshot"))) {
2557 log_error("Could not allocate message.");
2564 if (!dbus_message_append_args(m,
2565 DBUS_TYPE_STRING, &name,
2566 DBUS_TYPE_BOOLEAN, &cleanup,
2567 DBUS_TYPE_INVALID)) {
2568 log_error("Could not append arguments to message.");
2573 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2574 log_error("Failed to issue method call: %s", error.message);
2579 if (!dbus_message_get_args(reply, &error,
2580 DBUS_TYPE_OBJECT_PATH, &path,
2581 DBUS_TYPE_INVALID)) {
2582 log_error("Failed to parse reply: %s", error.message);
2587 dbus_message_unref(m);
2588 if (!(m = dbus_message_new_method_call(
2589 "org.freedesktop.systemd1",
2591 "org.freedesktop.DBus.Properties",
2593 log_error("Could not allocate message.");
2597 if (!dbus_message_append_args(m,
2598 DBUS_TYPE_STRING, &interface,
2599 DBUS_TYPE_STRING, &property,
2600 DBUS_TYPE_INVALID)) {
2601 log_error("Could not append arguments to message.");
2606 dbus_message_unref(reply);
2607 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2608 log_error("Failed to issue method call: %s", error.message);
2613 if (!dbus_message_iter_init(reply, &iter) ||
2614 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2615 log_error("Failed to parse reply.");
2620 dbus_message_iter_recurse(&iter, &sub);
2622 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
2623 log_error("Failed to parse reply.");
2628 dbus_message_iter_get_basic(&sub, &id);
2636 dbus_message_unref(m);
2639 dbus_message_unref(reply);
2641 dbus_error_free(&error);
2646 static int delete_snapshot(DBusConnection *bus, char **args, unsigned n) {
2647 DBusMessage *m = NULL, *reply = NULL;
2655 dbus_error_init(&error);
2657 for (i = 1; i < n; i++) {
2658 const char *path = NULL;
2660 if (!(m = dbus_message_new_method_call(
2661 "org.freedesktop.systemd1",
2662 "/org/freedesktop/systemd1",
2663 "org.freedesktop.systemd1.Manager",
2665 log_error("Could not allocate message.");
2670 if (!dbus_message_append_args(m,
2671 DBUS_TYPE_STRING, &args[i],
2672 DBUS_TYPE_INVALID)) {
2673 log_error("Could not append arguments to message.");
2678 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2679 log_error("Failed to issue method call: %s", error.message);
2684 if (!dbus_message_get_args(reply, &error,
2685 DBUS_TYPE_OBJECT_PATH, &path,
2686 DBUS_TYPE_INVALID)) {
2687 log_error("Failed to parse reply: %s", error.message);
2692 dbus_message_unref(m);
2693 if (!(m = dbus_message_new_method_call(
2694 "org.freedesktop.systemd1",
2696 "org.freedesktop.systemd1.Snapshot",
2698 log_error("Could not allocate message.");
2703 dbus_message_unref(reply);
2704 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2705 log_error("Failed to issue method call: %s", error.message);
2710 dbus_message_unref(m);
2711 dbus_message_unref(reply);
2719 dbus_message_unref(m);
2722 dbus_message_unref(reply);
2724 dbus_error_free(&error);
2729 static int daemon_reload(DBusConnection *bus, char **args, unsigned n) {
2730 DBusMessage *m = NULL, *reply = NULL;
2735 dbus_error_init(&error);
2737 if (arg_action == ACTION_RELOAD)
2739 else if (arg_action == ACTION_REEXEC)
2740 method = "Reexecute";
2742 assert(arg_action == ACTION_SYSTEMCTL);
2745 streq(args[0], "clear-jobs") ||
2746 streq(args[0], "cancel") ? "ClearJobs" :
2747 streq(args[0], "daemon-reexec") ? "Reexecute" :
2748 streq(args[0], "reset-maintenance") ? "ResetMaintenance" :
2749 streq(args[0], "daemon-exit") ? "Exit" :
2753 if (!(m = dbus_message_new_method_call(
2754 "org.freedesktop.systemd1",
2755 "/org/freedesktop/systemd1",
2756 "org.freedesktop.systemd1.Manager",
2758 log_error("Could not allocate message.");
2762 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2764 if (arg_action != ACTION_SYSTEMCTL && error_is_no_service(&error)) {
2765 /* There's always a fallback possible for
2766 * legacy actions. */
2771 log_error("Failed to issue method call: %s", error.message);
2780 dbus_message_unref(m);
2783 dbus_message_unref(reply);
2785 dbus_error_free(&error);
2790 static int reset_maintenance(DBusConnection *bus, char **args, unsigned n) {
2791 DBusMessage *m = NULL, *reply = NULL;
2797 dbus_error_init(&error);
2800 return daemon_reload(bus, args, n);
2802 for (i = 1; i < n; i++) {
2804 if (!(m = dbus_message_new_method_call(
2805 "org.freedesktop.systemd1",
2806 "/org/freedesktop/systemd1",
2807 "org.freedesktop.systemd1.Manager",
2808 "ResetMaintenanceUnit"))) {
2809 log_error("Could not allocate message.");
2814 if (!dbus_message_append_args(m,
2815 DBUS_TYPE_STRING, args + i,
2816 DBUS_TYPE_INVALID)) {
2817 log_error("Could not append arguments to message.");
2822 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2823 log_error("Failed to issue method call: %s", error.message);
2828 dbus_message_unref(m);
2829 dbus_message_unref(reply);
2837 dbus_message_unref(m);
2840 dbus_message_unref(reply);
2842 dbus_error_free(&error);
2847 static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
2848 DBusMessage *m = NULL, *reply = NULL;
2850 DBusMessageIter iter, sub, sub2;
2853 *interface = "org.freedesktop.systemd1.Manager",
2854 *property = "Environment";
2856 dbus_error_init(&error);
2858 if (!(m = dbus_message_new_method_call(
2859 "org.freedesktop.systemd1",
2860 "/org/freedesktop/systemd1",
2861 "org.freedesktop.DBus.Properties",
2863 log_error("Could not allocate message.");
2867 if (!dbus_message_append_args(m,
2868 DBUS_TYPE_STRING, &interface,
2869 DBUS_TYPE_STRING, &property,
2870 DBUS_TYPE_INVALID)) {
2871 log_error("Could not append arguments to message.");
2876 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2877 log_error("Failed to issue method call: %s", error.message);
2882 if (!dbus_message_iter_init(reply, &iter) ||
2883 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
2884 log_error("Failed to parse reply.");
2889 dbus_message_iter_recurse(&iter, &sub);
2891 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
2892 dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
2893 log_error("Failed to parse reply.");
2898 dbus_message_iter_recurse(&sub, &sub2);
2900 while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
2903 if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
2904 log_error("Failed to parse reply.");
2909 dbus_message_iter_get_basic(&sub2, &text);
2910 printf("%s\n", text);
2912 dbus_message_iter_next(&sub2);
2919 dbus_message_unref(m);
2922 dbus_message_unref(reply);
2924 dbus_error_free(&error);
2929 static int set_environment(DBusConnection *bus, char **args, unsigned n) {
2930 DBusMessage *m = NULL, *reply = NULL;
2934 DBusMessageIter iter, sub;
2937 dbus_error_init(&error);
2939 method = streq(args[0], "set-environment")
2941 : "UnsetEnvironment";
2943 if (!(m = dbus_message_new_method_call(
2944 "org.freedesktop.systemd1",
2945 "/org/freedesktop/systemd1",
2946 "org.freedesktop.systemd1.Manager",
2949 log_error("Could not allocate message.");
2953 dbus_message_iter_init_append(m, &iter);
2955 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
2956 log_error("Could not append arguments to message.");
2961 for (i = 1; i < n; i++)
2962 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) {
2963 log_error("Could not append arguments to message.");
2968 if (!dbus_message_iter_close_container(&iter, &sub)) {
2969 log_error("Could not append arguments to message.");
2974 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
2975 log_error("Failed to issue method call: %s", error.message);
2984 dbus_message_unref(m);
2987 dbus_message_unref(reply);
2989 dbus_error_free(&error);
3002 static Hashmap *will_install = NULL, *have_installed = NULL;
3003 static Set *remove_symlinks_to = NULL;
3005 static void install_info_free(InstallInfo *i) {
3010 strv_free(i->aliases);
3011 strv_free(i->wanted_by);
3015 static void install_info_hashmap_free(Hashmap *m) {
3018 while ((i = hashmap_steal_first(m)))
3019 install_info_free(i);
3024 static bool unit_name_valid(const char *name) {
3026 /* This is a minimal version of unit_name_valid() from
3032 if (ignore_file(name))
3038 static int install_info_add(const char *name) {
3042 assert(will_install);
3044 if (!unit_name_valid(name))
3047 if (hashmap_get(have_installed, name) ||
3048 hashmap_get(will_install, name))
3051 if (!(i = new0(InstallInfo, 1))) {
3056 if (!(i->name = strdup(name))) {
3061 if ((r = hashmap_put(will_install, i->name, i)) < 0)
3068 install_info_free(i);
3073 static int config_parse_also(
3074 const char *filename,
3076 const char *section,
3090 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
3094 if (!(n = strndup(w, l)))
3097 r = install_info_add(n);
3107 static int mark_symlink_for_removal(const char *p) {
3112 assert(path_is_absolute(p));
3114 if (!remove_symlinks_to)
3117 if (!(n = strdup(p)))
3120 path_kill_slashes(n);
3122 if ((r = set_put(remove_symlinks_to, n)) < 0) {
3124 return r == -EEXIST ? 0 : r;
3130 static int remove_marked_symlinks_fd(int fd, const char *config_path, const char *root, bool *deleted) {
3139 if (!(d = fdopendir(fd))) {
3140 close_nointr_nofail(fd);
3146 while ((de = readdir(d))) {
3147 bool is_dir = false, is_link = false;
3149 if (ignore_file(de->d_name))
3152 if (de->d_type == DT_LNK)
3154 else if (de->d_type == DT_DIR)
3156 else if (de->d_type == DT_UNKNOWN) {
3159 if (fstatat(fd, de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) {
3160 log_error("Failed to stat %s/%s: %m", root, de->d_name);
3167 is_link = S_ISLNK(st.st_mode);
3168 is_dir = S_ISDIR(st.st_mode);
3176 if ((nfd = openat(fd, de->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW)) < 0) {
3177 log_error("Failed to open %s/%s: %m", root, de->d_name);
3184 if (asprintf(&p, "%s/%s", root, de->d_name) < 0) {
3185 log_error("Failed to allocate directory string.");
3186 close_nointr_nofail(nfd);
3191 /* This will close nfd, regardless whether it succeeds or not */
3192 q = remove_marked_symlinks_fd(nfd, config_path, p, deleted);
3198 } else if (is_link) {
3202 if (asprintf(&p, "%s/%s", root, de->d_name) < 0) {
3203 log_error("Failed to allocate symlink string.");
3208 if ((q = readlink_and_make_absolute(p, &dest)) < 0) {
3209 log_error("Cannot read symlink %s: %s", p, strerror(-q));
3217 if ((c = canonicalize_file_name(dest))) {
3218 /* This might fail if the destination
3219 * is already removed */
3225 path_kill_slashes(dest);
3226 if (set_get(remove_symlinks_to, dest)) {
3229 log_info("rm '%s'", p);
3231 if (unlink(p) < 0) {
3232 log_error("Cannot unlink symlink %s: %m", p);
3237 rmdir_parents(p, config_path);
3238 path_kill_slashes(p);
3240 if (!set_get(remove_symlinks_to, p)) {
3242 if ((r = mark_symlink_for_removal(p)) < 0) {
3261 static int remove_marked_symlinks(const char *config_path) {
3265 assert(config_path);
3267 if (set_size(remove_symlinks_to) <= 0)
3270 if ((fd = open(config_path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW)) < 0)
3277 if ((cfd = dup(fd)) < 0) {
3282 /* This takes possession of cfd and closes it */
3283 if ((q = remove_marked_symlinks_fd(cfd, config_path, config_path, &deleted)) < 0) {
3289 close_nointr_nofail(fd);
3294 static int create_symlink(const char *verb, const char *old_path, const char *new_path) {
3301 if (streq(verb, "enable")) {
3304 mkdir_parents(new_path, 0755);
3306 if (symlink(old_path, new_path) >= 0) {
3309 log_info("ln -s '%s' '%s'", old_path, new_path);
3314 if (errno != EEXIST) {
3315 log_error("Cannot link %s to %s: %m", old_path, new_path);
3319 if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
3321 if (errno == EINVAL) {
3322 log_error("Cannot link %s to %s, file exists already and is not a symlink.", old_path, new_path);
3326 log_error("readlink() failed: %s", strerror(-r));
3330 if (streq(dest, old_path)) {
3336 log_error("Cannot link %s to %s, symlink exists already and points to %s.", old_path, new_path, dest);
3345 log_info("ln -s '%s' '%s'", old_path, new_path);
3347 if (symlink(old_path, new_path) >= 0)
3350 log_error("Cannot link %s to %s: %m", old_path, new_path);
3353 } else if (streq(verb, "disable")) {
3356 if ((r = mark_symlink_for_removal(old_path)) < 0)
3359 if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
3360 if (errno == ENOENT)
3363 if (errno == EINVAL) {
3364 log_warning("File %s not a symlink, ignoring.", old_path);
3368 log_error("readlink() failed: %s", strerror(-r));
3372 if (!streq(dest, old_path)) {
3373 log_warning("File %s not a symlink to %s but points to %s, ignoring.", new_path, old_path, dest);
3380 if ((r = mark_symlink_for_removal(new_path)) < 0)
3384 log_info("rm '%s'", new_path);
3386 if (unlink(new_path) >= 0)
3389 log_error("Cannot unlink %s: %m", new_path);
3392 } else if (streq(verb, "is-enabled")) {
3395 if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
3397 if (errno == ENOENT || errno == EINVAL)
3400 log_error("readlink() failed: %s", strerror(-r));
3404 if (streq(dest, old_path)) {
3412 assert_not_reached("Unknown action.");
3415 static int install_info_symlink_alias(const char *verb, InstallInfo *i, const char *config_path) {
3417 char *alias_path = NULL;
3422 assert(config_path);
3424 STRV_FOREACH(s, i->aliases) {
3426 if (!unit_name_valid(*s)) {
3427 log_error("Invalid name %s.", *s);
3433 if (!(alias_path = path_make_absolute(*s, config_path))) {
3434 log_error("Out of memory");
3439 if ((r = create_symlink(verb, i->path, alias_path)) != 0)
3442 if (streq(verb, "disable"))
3443 rmdir_parents(alias_path, config_path);
3454 static int install_info_symlink_wants(const char *verb, InstallInfo *i, const char *config_path) {
3456 char *alias_path = NULL;
3461 assert(config_path);
3463 STRV_FOREACH(s, i->wanted_by) {
3464 if (!unit_name_valid(*s)) {
3465 log_error("Invalid name %s.", *s);
3473 if (asprintf(&alias_path, "%s/%s.wants/%s", config_path, *s, i->name) < 0) {
3474 log_error("Out of memory");
3479 if ((r = create_symlink(verb, i->path, alias_path)) != 0)
3482 if (streq(verb, "disable"))
3483 rmdir_parents(alias_path, config_path);
3494 static int install_info_apply(const char *verb, LookupPaths *paths, InstallInfo *i, const char *config_path) {
3496 const ConfigItem items[] = {
3497 { "Alias", config_parse_strv, &i->aliases, "Install" },
3498 { "WantedBy", config_parse_strv, &i->wanted_by, "Install" },
3499 { "Also", config_parse_also, NULL, "Install" },
3501 { NULL, NULL, NULL, NULL }
3505 char *filename = NULL;
3512 STRV_FOREACH(p, paths->unit_path) {
3515 if (!(filename = path_make_absolute(i->name, *p))) {
3516 log_error("Out of memory");
3520 /* Ensure that we don't follow symlinks */
3521 if ((fd = open(filename, O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NOCTTY)) >= 0)
3522 if ((f = fdopen(fd, "re")))
3525 if (errno == ELOOP) {
3526 log_error("Refusing to operate on symlinks, please pass unit names or absolute paths to unit files.");
3531 if (errno != ENOENT) {
3532 log_error("Failed to open %s: %m", filename);
3542 log_error("Couldn't find %s.", i->name);
3548 if ((r = config_parse(filename, f, NULL, items, true, i)) < 0) {
3555 if ((r = install_info_symlink_alias(verb, i, config_path)) != 0)
3558 if ((r = install_info_symlink_wants(verb, i, config_path)) != 0)
3561 if ((r = mark_symlink_for_removal(filename)) < 0)
3564 if ((r = remove_marked_symlinks(config_path)) < 0)
3570 static char *get_config_path(void) {
3572 if (arg_session && arg_global)
3573 return strdup(SESSION_CONFIG_UNIT_PATH);
3578 if (session_config_home(&p) < 0)
3584 return strdup(SYSTEM_CONFIG_UNIT_PATH);
3587 static int enable_unit(DBusConnection *bus, char **args, unsigned n) {
3591 char *config_path = NULL;
3594 const char *verb = args[0];
3596 dbus_error_init(&error);
3599 if ((r = lookup_paths_init(&paths, arg_session ? MANAGER_SESSION : MANAGER_SYSTEM)) < 0) {
3600 log_error("Failed to determine lookup paths: %s", strerror(-r));
3604 if (!(config_path = get_config_path())) {
3605 log_error("Failed to determine config path");
3610 will_install = hashmap_new(string_hash_func, string_compare_func);
3611 have_installed = hashmap_new(string_hash_func, string_compare_func);
3613 if (!will_install || !have_installed) {
3614 log_error("Failed to allocate unit sets.");
3619 if (!arg_defaults && streq(verb, "disable"))
3620 if (!(remove_symlinks_to = set_new(string_hash_func, string_compare_func))) {
3621 log_error("Failed to allocate symlink sets.");
3626 for (j = 1; j < n; j++)
3627 if ((r = install_info_add(args[j])) < 0)
3630 while ((i = hashmap_first(will_install))) {
3633 assert_se(hashmap_move_one(have_installed, will_install, i->name) == 0);
3635 if ((q = install_info_apply(verb, &paths, i, config_path)) != 0) {
3643 /* In test mode and found something */
3649 if (streq(verb, "is-enabled"))
3650 r = r > 0 ? 0 : -ENOENT;
3652 /* Don't try to reload anything if the user asked us to not do this */
3654 /* Don't try to reload anything when updating a unit globally */
3656 /* Don't try to reload anything if we are called for system changes but the system wasn't booted with systemd */
3657 (arg_session || sd_booted() > 0) &&
3658 /* Don't try to reload anything if we are running in a chroot environment */
3659 (arg_session || running_in_chroot() <= 0) ) {
3662 if ((q = daemon_reload(bus, args, n)) < 0)
3667 install_info_hashmap_free(will_install);
3668 install_info_hashmap_free(have_installed);
3670 set_free_free(remove_symlinks_to);
3672 lookup_paths_free(&paths);
3679 static int systemctl_help(void) {
3681 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
3682 "Send control commands to or query the systemd manager.\n\n"
3683 " -h --help Show this help\n"
3684 " -t --type=TYPE List only units of a particular type\n"
3685 " -p --property=NAME Show only properties by this name\n"
3686 " -a --all Show all units/properties, including dead/empty ones\n"
3687 " --full Don't ellipsize unit names on output\n"
3688 " --fail When queueing a new job, fail if conflicting jobs are\n"
3690 " -q --quiet Suppress output\n"
3691 " --no-block Do not wait until operation finished\n"
3692 " --system Connect to system bus\n"
3693 " --session Connect to session bus\n"
3694 " --order When generating graph for dot, show only order\n"
3695 " --require When generating graph for dot, show only requirement\n"
3696 " --no-wall Don't send wall message before halt/power-off/reboot\n"
3697 " --global Enable/disable unit files globally\n"
3698 " --no-reload When enabling/disabling unit files, don't reload daemon\n"
3700 " --force When enabling unit files, override existing symlinks\n"
3701 " --defaults When disabling unit files, remove default symlinks only\n\n"
3703 " list-units List units\n"
3704 " start [NAME...] Start (activate) one or more units\n"
3705 " stop [NAME...] Stop (deactivate) one or more units\n"
3706 " reload [NAME...] Reload one or more units\n"
3707 " restart [NAME...] Start or restart one or more units\n"
3708 " try-restart [NAME...] Restart one or more units if active\n"
3709 " reload-or-restart [NAME...] Reload one or more units is possible,\n"
3710 " otherwise start or restart\n"
3711 " reload-or-try-restart [NAME...] Reload one or more units is possible,\n"
3712 " otherwise restart if active\n"
3713 " isolate [NAME] Start one unit and stop all others\n"
3714 " is-active [NAME...] Check whether units are active\n"
3715 " status [NAME...|PID...] Show runtime status of one or more units\n"
3716 " show [NAME...|JOB...] Show properties of one or more\n"
3717 " units/jobs or the manager\n"
3718 " reset-maintenance [NAME...] Reset maintenance state for all, one,\n"
3720 " enable [NAME...] Enable one or more unit files\n"
3721 " disable [NAME...] Disable one or more unit files\n"
3722 " is-enabled [NAME...] Check whether unit files are enabled\n"
3723 " load [NAME...] Load one or more units\n"
3724 " list-jobs List jobs\n"
3725 " cancel [JOB...] Cancel all, one, or more jobs\n"
3726 " monitor Monitor unit/job changes\n"
3727 " dump Dump server status\n"
3728 " dot Dump dependency graph for dot(1)\n"
3729 " snapshot [NAME] Create a snapshot\n"
3730 " delete [NAME...] Remove one or more snapshots\n"
3731 " daemon-reload Reload systemd manager configuration\n"
3732 " daemon-reexec Reexecute systemd manager\n"
3733 " daemon-exit Ask the systemd manager to quit\n"
3734 " show-environment Dump environment\n"
3735 " set-environment [NAME=VALUE...] Set one or more environment variables\n"
3736 " unset-environment [NAME...] Unset one or more environment variables\n"
3737 " halt Shut down and halt the system\n"
3738 " poweroff Shut down and power-off the system\n"
3739 " reboot Shut down and reboot the system\n"
3740 " rescue Enter system rescue mode\n"
3741 " emergency Enter system emergency mode\n"
3742 " default Enter system default mode\n",
3743 program_invocation_short_name);
3748 static int halt_help(void) {
3750 printf("%s [OPTIONS...]\n\n"
3751 "%s the system.\n\n"
3752 " --help Show this help\n"
3753 " --halt Halt the machine\n"
3754 " -p --poweroff Switch off the machine\n"
3755 " --reboot Reboot the machine\n"
3756 " -f --force Force immediate halt/power-off/reboot\n"
3757 " -w --wtmp-only Don't halt/power-off/reboot, just write wtmp record\n"
3758 " -d --no-wtmp Don't write wtmp record\n"
3759 " -n --no-sync Don't sync before halt/power-off/reboot\n"
3760 " --no-wall Don't send wall message before halt/power-off/reboot\n",
3761 program_invocation_short_name,
3762 arg_action == ACTION_REBOOT ? "Reboot" :
3763 arg_action == ACTION_POWEROFF ? "Power off" :
3769 static int shutdown_help(void) {
3771 printf("%s [OPTIONS...] [now] [WALL...]\n\n"
3772 "Shut down the system.\n\n"
3773 " --help Show this help\n"
3774 " -H --halt Halt the machine\n"
3775 " -P --poweroff Power-off the machine\n"
3776 " -r --reboot Reboot the machine\n"
3777 " -h Equivalent to --poweroff, overriden by --halt\n"
3778 " -k Don't halt/power-off/reboot, just send warnings\n"
3779 " --no-wall Don't send wall message before halt/power-off/reboot\n",
3780 program_invocation_short_name);
3785 static int telinit_help(void) {
3787 printf("%s [OPTIONS...] {COMMAND}\n\n"
3788 "Send control commands to the init daemon.\n\n"
3789 " --help Show this help\n"
3790 " --no-wall Don't send wall message before halt/power-off/reboot\n\n"
3792 " 0 Power-off the machine\n"
3793 " 6 Reboot the machine\n"
3794 " 2, 3, 4, 5 Start runlevelX.target unit\n"
3795 " 1, s, S Enter rescue mode\n"
3796 " q, Q Reload init daemon configuration\n"
3797 " u, U Reexecute init daemon\n",
3798 program_invocation_short_name);
3803 static int runlevel_help(void) {
3805 printf("%s [OPTIONS...]\n\n"
3806 "Prints the previous and current runlevel of the init system.\n\n"
3807 " --help Show this help\n",
3808 program_invocation_short_name);
3813 static int systemctl_parse_argv(int argc, char *argv[]) {
3830 static const struct option options[] = {
3831 { "help", no_argument, NULL, 'h' },
3832 { "type", required_argument, NULL, 't' },
3833 { "property", required_argument, NULL, 'p' },
3834 { "all", no_argument, NULL, 'a' },
3835 { "full", no_argument, NULL, ARG_FULL },
3836 { "fail", no_argument, NULL, ARG_FAIL },
3837 { "session", no_argument, NULL, ARG_SESSION },
3838 { "system", no_argument, NULL, ARG_SYSTEM },
3839 { "global", no_argument, NULL, ARG_GLOBAL },
3840 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
3841 { "no-wall", no_argument, NULL, ARG_NO_WALL },
3842 { "quiet", no_argument, NULL, 'q' },
3843 { "order", no_argument, NULL, ARG_ORDER },
3844 { "require", no_argument, NULL, ARG_REQUIRE },
3845 { "force", no_argument, NULL, ARG_FORCE },
3846 { "no-reload", no_argument, NULL, ARG_NO_RELOAD },
3847 { "defaults", no_argument, NULL, ARG_DEFAULTS },
3848 { NULL, 0, NULL, 0 }
3856 while ((c = getopt_long(argc, argv, "ht:p:aq", options, NULL)) >= 0) {
3871 if (!(l = strv_append(arg_property, optarg)))
3874 strv_free(arg_property);
3877 /* If the user asked for a particular
3878 * property, show it to him, even if it is
3897 arg_session = false;
3901 arg_no_block = true;
3909 arg_dot = DOT_ORDER;
3913 arg_dot = DOT_REQUIRE;
3929 arg_no_reload = true;
3938 arg_defaults = true;
3945 log_error("Unknown option code %c", c);
3953 static int halt_parse_argv(int argc, char *argv[]) {
3962 static const struct option options[] = {
3963 { "help", no_argument, NULL, ARG_HELP },
3964 { "halt", no_argument, NULL, ARG_HALT },
3965 { "poweroff", no_argument, NULL, 'p' },
3966 { "reboot", no_argument, NULL, ARG_REBOOT },
3967 { "force", no_argument, NULL, 'f' },
3968 { "wtmp-only", no_argument, NULL, 'w' },
3969 { "no-wtmp", no_argument, NULL, 'd' },
3970 { "no-sync", no_argument, NULL, 'n' },
3971 { "no-wall", no_argument, NULL, ARG_NO_WALL },
3972 { NULL, 0, NULL, 0 }
3980 if (utmp_get_runlevel(&runlevel, NULL) >= 0)
3981 if (runlevel == '0' || runlevel == '6')
3982 arg_immediate = true;
3984 while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0) {
3992 arg_action = ACTION_HALT;
3996 if (arg_action != ACTION_REBOOT)
3997 arg_action = ACTION_POWEROFF;
4001 arg_action = ACTION_REBOOT;
4005 arg_immediate = true;
4026 /* Compatibility nops */
4033 log_error("Unknown option code %c", c);
4038 if (optind < argc) {
4039 log_error("Too many arguments.");
4046 static int shutdown_parse_argv(int argc, char *argv[]) {
4053 static const struct option options[] = {
4054 { "help", no_argument, NULL, ARG_HELP },
4055 { "halt", no_argument, NULL, 'H' },
4056 { "poweroff", no_argument, NULL, 'P' },
4057 { "reboot", no_argument, NULL, 'r' },
4058 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4059 { NULL, 0, NULL, 0 }
4067 while ((c = getopt_long(argc, argv, "HPrhkt:a", options, NULL)) >= 0) {
4075 arg_action = ACTION_HALT;
4079 arg_action = ACTION_POWEROFF;
4083 arg_action = ACTION_REBOOT;
4087 if (arg_action != ACTION_HALT)
4088 arg_action = ACTION_POWEROFF;
4101 /* Compatibility nops */
4108 log_error("Unknown option code %c", c);
4113 if (argc > optind && !streq(argv[optind], "now"))
4114 log_warning("First argument '%s' isn't 'now'. Ignoring.", argv[optind]);
4116 /* We ignore the time argument */
4117 if (argc > optind + 1)
4118 arg_wall = argv + optind + 1;
4125 static int telinit_parse_argv(int argc, char *argv[]) {
4132 static const struct option options[] = {
4133 { "help", no_argument, NULL, ARG_HELP },
4134 { "no-wall", no_argument, NULL, ARG_NO_WALL },
4135 { NULL, 0, NULL, 0 }
4138 static const struct {
4142 { '0', ACTION_POWEROFF },
4143 { '6', ACTION_REBOOT },
4144 { '1', ACTION_RESCUE },
4145 { '2', ACTION_RUNLEVEL2 },
4146 { '3', ACTION_RUNLEVEL3 },
4147 { '4', ACTION_RUNLEVEL4 },
4148 { '5', ACTION_RUNLEVEL5 },
4149 { 's', ACTION_RESCUE },
4150 { 'S', ACTION_RESCUE },
4151 { 'q', ACTION_RELOAD },
4152 { 'Q', ACTION_RELOAD },
4153 { 'u', ACTION_REEXEC },
4154 { 'U', ACTION_REEXEC }
4163 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4178 log_error("Unknown option code %c", c);
4183 if (optind >= argc) {
4188 if (optind + 1 < argc) {
4189 log_error("Too many arguments.");
4193 if (strlen(argv[optind]) != 1) {
4194 log_error("Expected single character argument.");
4198 for (i = 0; i < ELEMENTSOF(table); i++)
4199 if (table[i].from == argv[optind][0])
4202 if (i >= ELEMENTSOF(table)) {
4203 log_error("Unknown command %s.", argv[optind]);
4207 arg_action = table[i].to;
4214 static int runlevel_parse_argv(int argc, char *argv[]) {
4220 static const struct option options[] = {
4221 { "help", no_argument, NULL, ARG_HELP },
4222 { NULL, 0, NULL, 0 }
4230 while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0) {
4241 log_error("Unknown option code %c", c);
4246 if (optind < argc) {
4247 log_error("Too many arguments.");
4254 static int parse_argv(int argc, char *argv[]) {
4258 if (program_invocation_short_name) {
4260 if (strstr(program_invocation_short_name, "halt")) {
4261 arg_action = ACTION_HALT;
4262 return halt_parse_argv(argc, argv);
4263 } else if (strstr(program_invocation_short_name, "poweroff")) {
4264 arg_action = ACTION_POWEROFF;
4265 return halt_parse_argv(argc, argv);
4266 } else if (strstr(program_invocation_short_name, "reboot")) {
4267 arg_action = ACTION_REBOOT;
4268 return halt_parse_argv(argc, argv);
4269 } else if (strstr(program_invocation_short_name, "shutdown")) {
4270 arg_action = ACTION_POWEROFF;
4271 return shutdown_parse_argv(argc, argv);
4272 } else if (strstr(program_invocation_short_name, "init")) {
4274 if (sd_booted() > 0) {
4275 arg_action = ACTION_INVALID;
4276 return telinit_parse_argv(argc, argv);
4278 /* Hmm, so some other init system is
4279 * running, we need to forward this
4280 * request to it. For now we simply
4281 * guess that it is Upstart. */
4283 execv("/lib/upstart/telinit", argv);
4285 log_error("Couldn't find an alternative telinit implementation to spawn.");
4289 } else if (strstr(program_invocation_short_name, "runlevel")) {
4290 arg_action = ACTION_RUNLEVEL;
4291 return runlevel_parse_argv(argc, argv);
4295 arg_action = ACTION_SYSTEMCTL;
4296 return systemctl_parse_argv(argc, argv);
4299 static int action_to_runlevel(void) {
4301 static const char table[_ACTION_MAX] = {
4302 [ACTION_HALT] = '0',
4303 [ACTION_POWEROFF] = '0',
4304 [ACTION_REBOOT] = '6',
4305 [ACTION_RUNLEVEL2] = '2',
4306 [ACTION_RUNLEVEL3] = '3',
4307 [ACTION_RUNLEVEL4] = '4',
4308 [ACTION_RUNLEVEL5] = '5',
4309 [ACTION_RESCUE] = '1'
4312 assert(arg_action < _ACTION_MAX);
4314 return table[arg_action];
4317 static int talk_upstart(void) {
4318 DBusMessage *m = NULL, *reply = NULL;
4320 int previous, rl, r;
4322 env1_buf[] = "RUNLEVEL=X",
4323 env2_buf[] = "PREVLEVEL=X";
4324 char *env1 = env1_buf, *env2 = env2_buf;
4325 const char *emit = "runlevel";
4326 dbus_bool_t b_false = FALSE;
4327 DBusMessageIter iter, sub;
4328 DBusConnection *bus;
4330 dbus_error_init(&error);
4332 if (!(rl = action_to_runlevel()))
4335 if (utmp_get_runlevel(&previous, NULL) < 0)
4338 if (!(bus = dbus_connection_open_private("unix:abstract=/com/ubuntu/upstart", &error))) {
4339 if (dbus_error_has_name(&error, DBUS_ERROR_NO_SERVER)) {
4344 log_error("Failed to connect to Upstart bus: %s", error.message);
4349 if ((r = bus_check_peercred(bus)) < 0) {
4350 log_error("Failed to verify owner of bus.");
4354 if (!(m = dbus_message_new_method_call(
4355 "com.ubuntu.Upstart",
4356 "/com/ubuntu/Upstart",
4357 "com.ubuntu.Upstart0_6",
4360 log_error("Could not allocate message.");
4365 dbus_message_iter_init_append(m, &iter);
4367 env1_buf[sizeof(env1_buf)-2] = rl;
4368 env2_buf[sizeof(env2_buf)-2] = previous;
4370 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
4371 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
4372 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
4373 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
4374 !dbus_message_iter_close_container(&iter, &sub) ||
4375 !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
4376 log_error("Could not append arguments to message.");
4381 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
4383 if (error_is_no_service(&error)) {
4388 log_error("Failed to issue method call: %s", error.message);
4397 dbus_message_unref(m);
4400 dbus_message_unref(reply);
4403 dbus_connection_close(bus);
4404 dbus_connection_unref(bus);
4407 dbus_error_free(&error);
4412 static int talk_initctl(void) {
4413 struct init_request request;
4417 if (!(rl = action_to_runlevel()))
4421 request.magic = INIT_MAGIC;
4422 request.sleeptime = 0;
4423 request.cmd = INIT_CMD_RUNLVL;
4424 request.runlevel = rl;
4426 if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
4428 if (errno == ENOENT)
4431 log_error("Failed to open "INIT_FIFO": %m");
4436 r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
4437 close_nointr_nofail(fd);
4440 log_error("Failed to write to "INIT_FIFO": %m");
4441 return errno ? -errno : -EIO;
4447 static int systemctl_main(DBusConnection *bus, int argc, char *argv[], DBusError *error) {
4449 static const struct {
4457 int (* const dispatch)(DBusConnection *bus, char **args, unsigned n);
4459 { "list-units", LESS, 1, list_units },
4460 { "list-jobs", EQUAL, 1, list_jobs },
4461 { "clear-jobs", EQUAL, 1, daemon_reload },
4462 { "load", MORE, 2, load_unit },
4463 { "cancel", MORE, 2, cancel_job },
4464 { "start", MORE, 2, start_unit },
4465 { "stop", MORE, 2, start_unit },
4466 { "reload", MORE, 2, start_unit },
4467 { "restart", MORE, 2, start_unit },
4468 { "try-restart", MORE, 2, start_unit },
4469 { "reload-or-restart", MORE, 2, start_unit },
4470 { "reload-or-try-restart", MORE, 2, start_unit },
4471 { "force-reload", MORE, 2, start_unit }, /* For compatibility with SysV */
4472 { "condrestart", MORE, 2, start_unit }, /* For compatibility with RH */
4473 { "isolate", EQUAL, 2, start_unit },
4474 { "is-active", MORE, 2, check_unit },
4475 { "check", MORE, 2, check_unit },
4476 { "show", MORE, 1, show },
4477 { "status", MORE, 2, show },
4478 { "monitor", EQUAL, 1, monitor },
4479 { "dump", EQUAL, 1, dump },
4480 { "dot", EQUAL, 1, dot },
4481 { "snapshot", LESS, 2, snapshot },
4482 { "delete", MORE, 2, delete_snapshot },
4483 { "daemon-reload", EQUAL, 1, daemon_reload },
4484 { "daemon-reexec", EQUAL, 1, daemon_reload },
4485 { "daemon-exit", EQUAL, 1, daemon_reload },
4486 { "show-environment", EQUAL, 1, show_enviroment },
4487 { "set-environment", MORE, 2, set_environment },
4488 { "unset-environment", MORE, 2, set_environment },
4489 { "halt", EQUAL, 1, start_special },
4490 { "poweroff", EQUAL, 1, start_special },
4491 { "reboot", EQUAL, 1, start_special },
4492 { "default", EQUAL, 1, start_special },
4493 { "rescue", EQUAL, 1, start_special },
4494 { "emergency", EQUAL, 1, start_special },
4495 { "reset-maintenance", MORE, 1, reset_maintenance },
4496 { "enable", MORE, 2, enable_unit },
4497 { "disable", MORE, 2, enable_unit },
4498 { "is-enabled", MORE, 2, enable_unit }
4508 left = argc - optind;
4511 /* Special rule: no arguments means "list-units" */
4514 if (streq(argv[optind], "help")) {
4519 for (i = 0; i < ELEMENTSOF(verbs); i++)
4520 if (streq(argv[optind], verbs[i].verb))
4523 if (i >= ELEMENTSOF(verbs)) {
4524 log_error("Unknown operation %s", argv[optind]);
4529 switch (verbs[i].argc_cmp) {
4532 if (left != verbs[i].argc) {
4533 log_error("Invalid number of arguments.");
4540 if (left < verbs[i].argc) {
4541 log_error("Too few arguments.");
4548 if (left > verbs[i].argc) {
4549 log_error("Too many arguments.");
4556 assert_not_reached("Unknown comparison operator.");
4559 /* Require a bus connection for all operations but
4561 if (!streq(verbs[i].verb, "enable") &&
4562 !streq(verbs[i].verb, "disable") &&
4564 log_error("Failed to get D-Bus connection: %s", error->message);
4568 return verbs[i].dispatch(bus, argv + optind, left);
4571 static int reload_with_fallback(DBusConnection *bus) {
4574 /* First, try systemd via D-Bus. */
4575 if (daemon_reload(bus, NULL, 0) > 0)
4579 /* Nothing else worked, so let's try signals */
4580 assert(arg_action == ACTION_RELOAD || arg_action == ACTION_REEXEC);
4582 if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0) {
4583 log_error("kill() failed: %m");
4590 static int start_with_fallback(DBusConnection *bus) {
4593 /* First, try systemd via D-Bus. */
4594 if (start_unit(bus, NULL, 0) > 0)
4598 /* Hmm, talking to systemd via D-Bus didn't work. Then
4599 * let's try to talk to Upstart via D-Bus. */
4600 if (talk_upstart() > 0)
4603 /* Nothing else worked, so let's try
4605 if (talk_initctl() > 0)
4608 log_error("Failed to talk to init daemon.");
4612 warn_wall(arg_action);
4616 static int halt_main(DBusConnection *bus) {
4619 if (geteuid() != 0) {
4620 log_error("Must to be root.");
4624 if (!arg_dry && !arg_immediate)
4625 return start_with_fallback(bus);
4628 if ((r = utmp_put_shutdown(0)) < 0)
4629 log_warning("Failed to write utmp record: %s", strerror(-r));
4637 /* Make sure C-A-D is handled by the kernel from this
4639 reboot(RB_ENABLE_CAD);
4641 switch (arg_action) {
4644 log_info("Halting.");
4645 reboot(RB_HALT_SYSTEM);
4648 case ACTION_POWEROFF:
4649 log_info("Powering off.");
4650 reboot(RB_POWER_OFF);
4654 log_info("Rebooting.");
4655 reboot(RB_AUTOBOOT);
4659 assert_not_reached("Unknown halt action.");
4662 /* We should never reach this. */
4666 static int runlevel_main(void) {
4667 int r, runlevel, previous;
4669 if ((r = utmp_get_runlevel(&runlevel, &previous)) < 0) {
4670 printf("unknown\n");
4675 previous <= 0 ? 'N' : previous,
4676 runlevel <= 0 ? 'N' : runlevel);
4681 int main(int argc, char*argv[]) {
4683 DBusConnection *bus = NULL;
4686 dbus_error_init(&error);
4688 log_parse_environment();
4690 if ((r = parse_argv(argc, argv)) < 0)
4697 /* /sbin/runlevel doesn't need to communicate via D-Bus, so
4698 * let's shortcut this */
4699 if (arg_action == ACTION_RUNLEVEL) {
4700 retval = runlevel_main() < 0;
4704 bus_connect(arg_session ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &bus, &private_bus, &error);
4706 switch (arg_action) {
4708 case ACTION_SYSTEMCTL: {
4709 retval = systemctl_main(bus, argc, argv, &error) < 0;
4714 case ACTION_POWEROFF:
4716 retval = halt_main(bus) < 0;
4719 case ACTION_RUNLEVEL2:
4720 case ACTION_RUNLEVEL3:
4721 case ACTION_RUNLEVEL4:
4722 case ACTION_RUNLEVEL5:
4724 case ACTION_EMERGENCY:
4725 case ACTION_DEFAULT:
4726 retval = start_with_fallback(bus) < 0;
4731 retval = reload_with_fallback(bus) < 0;
4734 case ACTION_INVALID:
4735 case ACTION_RUNLEVEL:
4737 assert_not_reached("Unknown action");
4743 dbus_connection_close(bus);
4744 dbus_connection_unref(bus);
4747 dbus_error_free(&error);
4751 strv_free(arg_property);