1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/epoll.h>
23 #include <sys/timerfd.h>
26 #include <dbus/dbus.h>
32 #include "dbus-unit.h"
34 #include "dbus-manager.h"
35 #include "dbus-service.h"
36 #include "dbus-socket.h"
37 #include "dbus-target.h"
38 #include "dbus-device.h"
39 #include "dbus-mount.h"
40 #include "dbus-automount.h"
41 #include "dbus-snapshot.h"
42 #include "dbus-swap.h"
43 #include "dbus-timer.h"
44 #include "dbus-path.h"
45 #include "bus-errors.h"
47 #include "dbus-common.h"
49 #define CONNECTIONS_MAX 52
51 static const char bus_properties_interface[] = BUS_PROPERTIES_INTERFACE;
52 static const char bus_introspectable_interface[] = BUS_INTROSPECTABLE_INTERFACE;
54 const char *const bus_interface_table[] = {
55 "org.freedesktop.DBus.Properties", bus_properties_interface,
56 "org.freedesktop.DBus.Introspectable", bus_introspectable_interface,
57 "org.freedesktop.systemd1.Manager", bus_manager_interface,
58 "org.freedesktop.systemd1.Job", bus_job_interface,
59 "org.freedesktop.systemd1.Unit", bus_unit_interface,
60 "org.freedesktop.systemd1.Service", bus_service_interface,
61 "org.freedesktop.systemd1.Socket", bus_socket_interface,
62 "org.freedesktop.systemd1.Target", bus_target_interface,
63 "org.freedesktop.systemd1.Device", bus_device_interface,
64 "org.freedesktop.systemd1.Mount", bus_mount_interface,
65 "org.freedesktop.systemd1.Automount", bus_automount_interface,
66 "org.freedesktop.systemd1.Snapshot", bus_snapshot_interface,
67 "org.freedesktop.systemd1.Swap", bus_swap_interface,
68 "org.freedesktop.systemd1.Timer", bus_timer_interface,
69 "org.freedesktop.systemd1.Path", bus_path_interface,
73 static void bus_done_api(Manager *m);
74 static void bus_done_system(Manager *m);
75 static void bus_done_private(Manager *m);
76 static void shutdown_connection(Manager *m, DBusConnection *c);
78 static void bus_dispatch_status(DBusConnection *bus, DBusDispatchStatus status, void *data) {
84 /* We maintain two sets, one for those connections where we
85 * requested a dispatch, and another where we didn't. And then,
86 * we move the connections between the two sets. */
88 if (status == DBUS_DISPATCH_COMPLETE)
89 set_move_one(m->bus_connections, m->bus_connections_for_dispatch, bus);
91 set_move_one(m->bus_connections_for_dispatch, m->bus_connections, bus);
94 void bus_watch_event(Manager *m, Watch *w, int events) {
98 /* This is called by the event loop whenever there is
99 * something happening on D-Bus' file handles. */
101 if (!dbus_watch_get_enabled(w->data.bus_watch))
104 dbus_watch_handle(w->data.bus_watch, bus_events_to_flags(events));
107 static dbus_bool_t bus_add_watch(DBusWatch *bus_watch, void *data) {
110 struct epoll_event ev;
115 if (!(w = new0(Watch, 1)))
118 w->fd = dbus_watch_get_unix_fd(bus_watch);
119 w->type = WATCH_DBUS_WATCH;
120 w->data.bus_watch = bus_watch;
123 ev.events = bus_flags_to_events(bus_watch);
126 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, w->fd, &ev) < 0) {
128 if (errno != EEXIST) {
133 /* Hmm, bloody D-Bus creates multiple watches on the
134 * same fd. epoll() does not like that. As a dirty
135 * hack we simply dup() the fd and hence get a second
136 * one we can safely add to the epoll(). */
138 if ((w->fd = dup(w->fd)) < 0) {
143 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, w->fd, &ev) < 0) {
144 close_nointr_nofail(w->fd);
149 w->fd_is_dupped = true;
152 dbus_watch_set_data(bus_watch, w, NULL);
157 static void bus_remove_watch(DBusWatch *bus_watch, void *data) {
164 w = dbus_watch_get_data(bus_watch);
168 assert(w->type == WATCH_DBUS_WATCH);
169 assert_se(epoll_ctl(m->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
172 close_nointr_nofail(w->fd);
177 static void bus_toggle_watch(DBusWatch *bus_watch, void *data) {
180 struct epoll_event ev;
185 w = dbus_watch_get_data(bus_watch);
189 assert(w->type == WATCH_DBUS_WATCH);
192 ev.events = bus_flags_to_events(bus_watch);
195 assert_se(epoll_ctl(m->epoll_fd, EPOLL_CTL_MOD, w->fd, &ev) == 0);
198 static int bus_timeout_arm(Manager *m, Watch *w) {
199 struct itimerspec its;
206 if (dbus_timeout_get_enabled(w->data.bus_timeout)) {
207 timespec_store(&its.it_value, dbus_timeout_get_interval(w->data.bus_timeout) * USEC_PER_MSEC);
208 its.it_interval = its.it_value;
211 if (timerfd_settime(w->fd, 0, &its, NULL) < 0)
217 void bus_timeout_event(Manager *m, Watch *w, int events) {
221 /* This is called by the event loop whenever there is
222 * something happening on D-Bus' file handles. */
224 if (!(dbus_timeout_get_enabled(w->data.bus_timeout)))
227 dbus_timeout_handle(w->data.bus_timeout);
230 static dbus_bool_t bus_add_timeout(DBusTimeout *timeout, void *data) {
233 struct epoll_event ev;
238 if (!(w = new0(Watch, 1)))
241 if ((w->fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC)) < 0)
244 w->type = WATCH_DBUS_TIMEOUT;
245 w->data.bus_timeout = timeout;
247 if (bus_timeout_arm(m, w) < 0)
254 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, w->fd, &ev) < 0)
257 dbus_timeout_set_data(timeout, w, NULL);
263 close_nointr_nofail(w->fd);
269 static void bus_remove_timeout(DBusTimeout *timeout, void *data) {
276 w = dbus_timeout_get_data(timeout);
280 assert(w->type == WATCH_DBUS_TIMEOUT);
282 assert_se(epoll_ctl(m->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
283 close_nointr_nofail(w->fd);
287 static void bus_toggle_timeout(DBusTimeout *timeout, void *data) {
295 w = dbus_timeout_get_data(timeout);
299 assert(w->type == WATCH_DBUS_TIMEOUT);
301 if ((r = bus_timeout_arm(m, w)) < 0)
302 log_error("Failed to rearm timer: %s", strerror(-r));
305 static DBusHandlerResult api_bus_message_filter(DBusConnection *connection, DBusMessage *message, void *data) {
308 DBusMessage *reply = NULL;
314 dbus_error_init(&error);
316 if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL ||
317 dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_SIGNAL)
318 log_debug("Got D-Bus request: %s.%s() on %s",
319 dbus_message_get_interface(message),
320 dbus_message_get_member(message),
321 dbus_message_get_path(message));
323 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
324 log_debug("API D-Bus connection terminated.");
327 } else if (dbus_message_is_signal(message, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) {
328 const char *name, *old_owner, *new_owner;
330 if (!dbus_message_get_args(message, &error,
331 DBUS_TYPE_STRING, &name,
332 DBUS_TYPE_STRING, &old_owner,
333 DBUS_TYPE_STRING, &new_owner,
335 log_error("Failed to parse NameOwnerChanged message: %s", error.message);
337 if (set_remove(BUS_CONNECTION_SUBSCRIBED(m, connection), (char*) name))
338 log_debug("Subscription client vanished: %s (left: %u)", name, set_size(BUS_CONNECTION_SUBSCRIBED(m, connection)));
340 if (old_owner[0] == 0)
343 if (new_owner[0] == 0)
346 manager_dispatch_bus_name_owner_changed(m, name, old_owner, new_owner);
348 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Activator", "ActivationRequest")) {
351 if (!dbus_message_get_args(message, &error,
352 DBUS_TYPE_STRING, &name,
354 log_error("Failed to parse ActivationRequest message: %s", error.message);
359 log_debug("Got D-Bus activation request for %s", name);
361 if (manager_unit_pending_inactive(m, SPECIAL_DBUS_SERVICE) ||
362 manager_unit_pending_inactive(m, SPECIAL_DBUS_SOCKET)) {
364 dbus_set_error(&error, BUS_ERROR_SHUTTING_DOWN, "Refusing activation, D-Bus is shutting down.");
366 r = manager_load_unit(m, name, NULL, &error, &u);
368 if (r >= 0 && u->meta.refuse_manual_start)
372 r = manager_add_job(m, JOB_START, u, JOB_REPLACE, true, &error, NULL);
376 const char *id, *text;
378 log_debug("D-Bus activation failed for %s: %s", name, strerror(-r));
380 if (!(reply = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Activator", "ActivationFailure")))
383 id = error.name ? error.name : bus_errno_to_dbus(r);
384 text = bus_error(&error, r);
386 if (!dbus_message_set_destination(reply, DBUS_SERVICE_DBUS) ||
387 !dbus_message_append_args(reply,
388 DBUS_TYPE_STRING, &name,
389 DBUS_TYPE_STRING, &id,
390 DBUS_TYPE_STRING, &text,
395 /* On success we don't do anything, the service will be spawned now */
399 dbus_error_free(&error);
402 if (!dbus_connection_send(connection, reply, NULL))
405 dbus_message_unref(reply);
408 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
412 dbus_message_unref(reply);
414 dbus_error_free(&error);
416 return DBUS_HANDLER_RESULT_NEED_MEMORY;
419 static DBusHandlerResult system_bus_message_filter(DBusConnection *connection, DBusMessage *message, void *data) {
427 dbus_error_init(&error);
429 if (m->api_bus != m->system_bus &&
430 (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL ||
431 dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_SIGNAL))
432 log_debug("Got D-Bus request on system bus: %s.%s() on %s",
433 dbus_message_get_interface(message),
434 dbus_message_get_member(message),
435 dbus_message_get_path(message));
437 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
438 log_debug("System D-Bus connection terminated.");
441 } else if (m->running_as != MANAGER_SYSTEM &&
442 dbus_message_is_signal(message, "org.freedesktop.systemd1.Agent", "Released")) {
446 if (!dbus_message_get_args(message, &error,
447 DBUS_TYPE_STRING, &cgroup,
449 log_error("Failed to parse Released message: %s", error.message);
451 cgroup_notify_empty(m, cgroup);
454 dbus_error_free(&error);
455 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
458 static DBusHandlerResult private_bus_message_filter(DBusConnection *connection, DBusMessage *message, void *data) {
466 dbus_error_init(&error);
468 if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL ||
469 dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_SIGNAL)
470 log_debug("Got D-Bus request: %s.%s() on %s",
471 dbus_message_get_interface(message),
472 dbus_message_get_member(message),
473 dbus_message_get_path(message));
475 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected"))
476 shutdown_connection(m, connection);
477 else if (m->running_as == MANAGER_SYSTEM &&
478 dbus_message_is_signal(message, "org.freedesktop.systemd1.Agent", "Released")) {
482 if (!dbus_message_get_args(message, &error,
483 DBUS_TYPE_STRING, &cgroup,
485 log_error("Failed to parse Released message: %s", error.message);
487 cgroup_notify_empty(m, cgroup);
489 /* Forward the message to the system bus, so that user
490 * instances are notified as well */
493 dbus_connection_send(m->system_bus, message, NULL);
496 dbus_error_free(&error);
498 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
501 unsigned bus_dispatch(Manager *m) {
506 if (m->queued_message) {
507 /* If we cannot get rid of this message we won't
508 * dispatch any D-Bus messages, so that we won't end
509 * up wanting to queue another message. */
511 if (m->queued_message_connection)
512 if (!dbus_connection_send(m->queued_message_connection, m->queued_message, NULL))
515 dbus_message_unref(m->queued_message);
516 m->queued_message = NULL;
517 m->queued_message_connection = NULL;
520 if ((c = set_first(m->bus_connections_for_dispatch))) {
521 if (dbus_connection_dispatch(c) == DBUS_DISPATCH_COMPLETE)
522 set_move_one(m->bus_connections, m->bus_connections_for_dispatch, c);
530 static void request_name_pending_cb(DBusPendingCall *pending, void *userdata) {
534 dbus_error_init(&error);
536 assert_se(reply = dbus_pending_call_steal_reply(pending));
538 switch (dbus_message_get_type(reply)) {
540 case DBUS_MESSAGE_TYPE_ERROR:
542 assert_se(dbus_set_error_from_message(&error, reply));
543 log_warning("RequestName() failed: %s", error.message);
546 case DBUS_MESSAGE_TYPE_METHOD_RETURN: {
549 if (!dbus_message_get_args(reply,
551 DBUS_TYPE_UINT32, &r,
552 DBUS_TYPE_INVALID)) {
553 log_error("Failed to parse RequestName() reply: %s", error.message);
558 log_debug("Successfully acquired name.");
560 log_error("Name already owned.");
566 assert_not_reached("Invalid reply message");
569 dbus_message_unref(reply);
570 dbus_error_free(&error);
573 static int request_name(Manager *m) {
574 const char *name = "org.freedesktop.systemd1";
575 /* Allow replacing of our name, to ease implementation of
576 * reexecution, where we keep the old connection open until
577 * after the new connection is set up and the name installed
578 * to allow clients to synchronously wait for reexecution to
580 uint32_t flags = DBUS_NAME_FLAG_ALLOW_REPLACEMENT|DBUS_NAME_FLAG_REPLACE_EXISTING;
581 DBusMessage *message = NULL;
582 DBusPendingCall *pending = NULL;
584 if (!(message = dbus_message_new_method_call(
591 if (!dbus_message_append_args(
593 DBUS_TYPE_STRING, &name,
594 DBUS_TYPE_UINT32, &flags,
598 if (!dbus_connection_send_with_reply(m->api_bus, message, &pending, -1))
601 if (!dbus_pending_call_set_notify(pending, request_name_pending_cb, m, NULL))
604 dbus_message_unref(message);
605 dbus_pending_call_unref(pending);
607 /* We simple ask for the name and don't wait for it. Sooner or
608 * later we'll have it. */
614 dbus_pending_call_cancel(pending);
615 dbus_pending_call_unref(pending);
619 dbus_message_unref(message);
624 static void query_name_list_pending_cb(DBusPendingCall *pending, void *userdata) {
627 Manager *m = userdata;
631 dbus_error_init(&error);
633 assert_se(reply = dbus_pending_call_steal_reply(pending));
635 switch (dbus_message_get_type(reply)) {
637 case DBUS_MESSAGE_TYPE_ERROR:
639 assert_se(dbus_set_error_from_message(&error, reply));
640 log_warning("ListNames() failed: %s", error.message);
643 case DBUS_MESSAGE_TYPE_METHOD_RETURN: {
647 if ((r = bus_parse_strv(reply, &l)) < 0)
648 log_warning("Failed to parse ListNames() reply: %s", strerror(-r));
653 /* This is a bit hacky, we say the
654 * owner of the name is the name
655 * itself, because we don't want the
656 * extra traffic to figure out the
658 manager_dispatch_bus_name_owner_changed(m, *t, NULL, *t);
667 assert_not_reached("Invalid reply message");
670 dbus_message_unref(reply);
671 dbus_error_free(&error);
674 static int query_name_list(Manager *m) {
675 DBusMessage *message = NULL;
676 DBusPendingCall *pending = NULL;
678 /* Asks for the currently installed bus names */
680 if (!(message = dbus_message_new_method_call(
687 if (!dbus_connection_send_with_reply(m->api_bus, message, &pending, -1))
690 if (!dbus_pending_call_set_notify(pending, query_name_list_pending_cb, m, NULL))
693 dbus_message_unref(message);
694 dbus_pending_call_unref(pending);
696 /* We simple ask for the list and don't wait for it. Sooner or
697 * later we'll get it. */
703 dbus_pending_call_cancel(pending);
704 dbus_pending_call_unref(pending);
708 dbus_message_unref(message);
713 static int bus_setup_loop(Manager *m, DBusConnection *bus) {
717 dbus_connection_set_exit_on_disconnect(bus, FALSE);
719 if (!dbus_connection_set_watch_functions(bus, bus_add_watch, bus_remove_watch, bus_toggle_watch, m, NULL) ||
720 !dbus_connection_set_timeout_functions(bus, bus_add_timeout, bus_remove_timeout, bus_toggle_timeout, m, NULL)) {
721 log_error("Not enough memory");
725 if (set_put(m->bus_connections_for_dispatch, bus) < 0) {
726 log_error("Not enough memory");
730 dbus_connection_set_dispatch_status_function(bus, bus_dispatch_status, m, NULL);
734 static dbus_bool_t allow_only_root(DBusConnection *connection, unsigned long uid, void *data) {
738 static void bus_new_connection(
740 DBusConnection *new_connection,
747 if (set_size(m->bus_connections) >= CONNECTIONS_MAX) {
748 log_error("Too many concurrent connections.");
752 dbus_connection_set_unix_user_function(new_connection, allow_only_root, NULL, NULL);
754 if (bus_setup_loop(m, new_connection) < 0)
757 if (!dbus_connection_register_object_path(new_connection, "/org/freedesktop/systemd1", &bus_manager_vtable, m) ||
758 !dbus_connection_register_fallback(new_connection, "/org/freedesktop/systemd1/unit", &bus_unit_vtable, m) ||
759 !dbus_connection_register_fallback(new_connection, "/org/freedesktop/systemd1/job", &bus_job_vtable, m) ||
760 !dbus_connection_add_filter(new_connection, private_bus_message_filter, m, NULL)) {
761 log_error("Not enough memory.");
765 log_debug("Accepted connection on private bus.");
767 dbus_connection_ref(new_connection);
770 static int bus_init_system(Manager *m) {
776 dbus_error_init(&error);
781 if (m->running_as == MANAGER_SYSTEM && m->api_bus)
782 m->system_bus = m->api_bus;
784 if (!(m->system_bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error))) {
785 log_debug("Failed to get system D-Bus connection, retrying later: %s", error.message);
790 if ((r = bus_setup_loop(m, m->system_bus)) < 0)
794 if (!dbus_connection_add_filter(m->system_bus, system_bus_message_filter, m, NULL)) {
795 log_error("Not enough memory");
800 if (m->running_as != MANAGER_SYSTEM) {
801 dbus_bus_add_match(m->system_bus,
803 "interface='org.freedesktop.systemd1.Agent',"
805 "path='/org/freedesktop/systemd1/agent'",
808 if (dbus_error_is_set(&error)) {
809 log_error("Failed to register match: %s", error.message);
815 if (m->api_bus != m->system_bus) {
817 log_debug("Successfully connected to system D-Bus bus %s as %s",
818 strnull((id = dbus_connection_get_server_id(m->system_bus))),
819 strnull(dbus_bus_get_unique_name(m->system_bus)));
827 dbus_error_free(&error);
832 static int bus_init_api(Manager *m) {
838 dbus_error_init(&error);
843 if (m->running_as == MANAGER_SYSTEM && m->system_bus)
844 m->api_bus = m->system_bus;
846 if (!(m->api_bus = dbus_bus_get_private(m->running_as == MANAGER_USER ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &error))) {
847 log_debug("Failed to get API D-Bus connection, retrying later: %s", error.message);
852 if ((r = bus_setup_loop(m, m->api_bus)) < 0)
856 if (!dbus_connection_register_object_path(m->api_bus, "/org/freedesktop/systemd1", &bus_manager_vtable, m) ||
857 !dbus_connection_register_fallback(m->api_bus, "/org/freedesktop/systemd1/unit", &bus_unit_vtable, m) ||
858 !dbus_connection_register_fallback(m->api_bus, "/org/freedesktop/systemd1/job", &bus_job_vtable, m) ||
859 !dbus_connection_add_filter(m->api_bus, api_bus_message_filter, m, NULL)) {
860 log_error("Not enough memory");
865 /* Get NameOwnerChange messages */
866 dbus_bus_add_match(m->api_bus,
868 "sender='"DBUS_SERVICE_DBUS"',"
869 "interface='"DBUS_INTERFACE_DBUS"',"
870 "member='NameOwnerChanged',"
871 "path='"DBUS_PATH_DBUS"'",
874 if (dbus_error_is_set(&error)) {
875 log_error("Failed to register match: %s", error.message);
880 /* Get activation requests */
881 dbus_bus_add_match(m->api_bus,
883 "sender='"DBUS_SERVICE_DBUS"',"
884 "interface='org.freedesktop.systemd1.Activator',"
885 "member='ActivationRequest',"
886 "path='"DBUS_PATH_DBUS"'",
889 if (dbus_error_is_set(&error)) {
890 log_error("Failed to register match: %s", error.message);
895 if ((r = request_name(m)) < 0)
898 if ((r = query_name_list(m)) < 0)
901 if (m->api_bus != m->system_bus) {
903 log_debug("Successfully connected to API D-Bus bus %s as %s",
904 strnull((id = dbus_connection_get_server_id(m->api_bus))),
905 strnull(dbus_bus_get_unique_name(m->api_bus)));
913 dbus_error_free(&error);
918 static int bus_init_private(Manager *m) {
921 const char *const external_only[] = {
928 dbus_error_init(&error);
933 /* We want the private bus only when running as init */
937 unlink("/run/systemd/private");
938 if (!(m->private_bus = dbus_server_listen("unix:path=/run/systemd/private", &error))) {
939 log_error("Failed to create private D-Bus server: %s", error.message);
944 if (!dbus_server_set_auth_mechanisms(m->private_bus, (const char**) external_only) ||
945 !dbus_server_set_watch_functions(m->private_bus, bus_add_watch, bus_remove_watch, bus_toggle_watch, m, NULL) ||
946 !dbus_server_set_timeout_functions(m->private_bus, bus_add_timeout, bus_remove_timeout, bus_toggle_timeout, m, NULL)) {
947 log_error("Not enough memory");
952 dbus_server_set_new_connection_function(m->private_bus, bus_new_connection, m, NULL);
954 log_debug("Successfully created private D-Bus server.");
960 dbus_error_free(&error);
965 int bus_init(Manager *m, bool try_bus_connect) {
968 if (set_ensure_allocated(&m->bus_connections, trivial_hash_func, trivial_compare_func) < 0 ||
969 set_ensure_allocated(&m->bus_connections_for_dispatch, trivial_hash_func, trivial_compare_func) < 0) {
970 log_error("Not enough memory");
974 if (m->name_data_slot < 0)
975 if (!dbus_pending_call_allocate_data_slot(&m->name_data_slot)) {
976 log_error("Not enough memory");
980 if (m->subscribed_data_slot < 0)
981 if (!dbus_connection_allocate_data_slot(&m->subscribed_data_slot)) {
982 log_error("Not enough memory");
986 if (try_bus_connect) {
987 if ((r = bus_init_system(m)) < 0 ||
988 (r = bus_init_api(m)) < 0)
992 if ((r = bus_init_private(m)) < 0)
998 static void shutdown_connection(Manager *m, DBusConnection *c) {
1003 HASHMAP_FOREACH(j, m->jobs, i)
1005 free(j->bus_client);
1006 j->bus_client = NULL;
1011 set_remove(m->bus_connections, c);
1012 set_remove(m->bus_connections_for_dispatch, c);
1014 if ((s = BUS_CONNECTION_SUBSCRIBED(m, c))) {
1017 while ((t = set_steal_first(s)))
1023 if (m->queued_message_connection == c) {
1024 m->queued_message_connection = NULL;
1026 if (m->queued_message) {
1027 dbus_message_unref(m->queued_message);
1028 m->queued_message = NULL;
1032 dbus_connection_set_dispatch_status_function(c, NULL, NULL, NULL);
1033 dbus_connection_flush(c);
1034 dbus_connection_close(c);
1035 dbus_connection_unref(c);
1038 static void bus_done_api(Manager *m) {
1042 if (m->system_bus == m->api_bus)
1043 m->system_bus = NULL;
1045 shutdown_connection(m, m->api_bus);
1050 if (m->queued_message) {
1051 dbus_message_unref(m->queued_message);
1052 m->queued_message = NULL;
1056 static void bus_done_system(Manager *m) {
1059 if (m->system_bus == m->api_bus)
1062 if (m->system_bus) {
1063 shutdown_connection(m, m->system_bus);
1064 m->system_bus = NULL;
1068 static void bus_done_private(Manager *m) {
1070 if (m->private_bus) {
1071 dbus_server_disconnect(m->private_bus);
1072 dbus_server_unref(m->private_bus);
1073 m->private_bus = NULL;
1077 void bus_done(Manager *m) {
1082 bus_done_private(m);
1084 while ((c = set_steal_first(m->bus_connections)))
1085 shutdown_connection(m, c);
1087 while ((c = set_steal_first(m->bus_connections_for_dispatch)))
1088 shutdown_connection(m, c);
1090 set_free(m->bus_connections);
1091 set_free(m->bus_connections_for_dispatch);
1093 if (m->name_data_slot >= 0)
1094 dbus_pending_call_free_data_slot(&m->name_data_slot);
1096 if (m->subscribed_data_slot >= 0)
1097 dbus_connection_free_data_slot(&m->subscribed_data_slot);
1100 static void query_pid_pending_cb(DBusPendingCall *pending, void *userdata) {
1101 Manager *m = userdata;
1106 dbus_error_init(&error);
1108 assert_se(name = BUS_PENDING_CALL_NAME(m, pending));
1109 assert_se(reply = dbus_pending_call_steal_reply(pending));
1111 switch (dbus_message_get_type(reply)) {
1113 case DBUS_MESSAGE_TYPE_ERROR:
1115 assert_se(dbus_set_error_from_message(&error, reply));
1116 log_warning("GetConnectionUnixProcessID() failed: %s", error.message);
1119 case DBUS_MESSAGE_TYPE_METHOD_RETURN: {
1122 if (!dbus_message_get_args(reply,
1124 DBUS_TYPE_UINT32, &r,
1125 DBUS_TYPE_INVALID)) {
1126 log_error("Failed to parse GetConnectionUnixProcessID() reply: %s", error.message);
1130 manager_dispatch_bus_query_pid_done(m, name, (pid_t) r);
1135 assert_not_reached("Invalid reply message");
1138 dbus_message_unref(reply);
1139 dbus_error_free(&error);
1142 int bus_query_pid(Manager *m, const char *name) {
1143 DBusMessage *message = NULL;
1144 DBusPendingCall *pending = NULL;
1150 if (!(message = dbus_message_new_method_call(
1153 DBUS_INTERFACE_DBUS,
1154 "GetConnectionUnixProcessID")))
1157 if (!(dbus_message_append_args(
1159 DBUS_TYPE_STRING, &name,
1160 DBUS_TYPE_INVALID)))
1163 if (!dbus_connection_send_with_reply(m->api_bus, message, &pending, -1))
1166 if (!(n = strdup(name)))
1169 if (!dbus_pending_call_set_data(pending, m->name_data_slot, n, free))
1174 if (!dbus_pending_call_set_notify(pending, query_pid_pending_cb, m, NULL))
1177 dbus_message_unref(message);
1178 dbus_pending_call_unref(pending);
1186 dbus_pending_call_cancel(pending);
1187 dbus_pending_call_unref(pending);
1191 dbus_message_unref(message);
1196 int bus_broadcast(Manager *m, DBusMessage *message) {
1204 SET_FOREACH(c, m->bus_connections_for_dispatch, i)
1205 if (c != m->system_bus || m->running_as == MANAGER_SYSTEM)
1206 oom = !dbus_connection_send(c, message, NULL);
1208 SET_FOREACH(c, m->bus_connections, i)
1209 if (c != m->system_bus || m->running_as == MANAGER_SYSTEM)
1210 oom = !dbus_connection_send(c, message, NULL);
1212 return oom ? -ENOMEM : 0;
1215 int bus_parse_strv(DBusMessage *m, char ***_l) {
1216 DBusMessageIter iter, sub;
1217 unsigned n = 0, i = 0;
1223 if (!dbus_message_iter_init(m, &iter) ||
1224 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
1225 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRING)
1228 dbus_message_iter_recurse(&iter, &sub);
1230 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1232 dbus_message_iter_next(&sub);
1235 if (!(l = new(char*, n+1)))
1238 assert_se(dbus_message_iter_init(m, &iter));
1239 dbus_message_iter_recurse(&iter, &sub);
1241 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1244 assert_se(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
1245 dbus_message_iter_get_basic(&sub, &s);
1247 if (!(l[i++] = strdup(s))) {
1252 dbus_message_iter_next(&sub);
1264 bool bus_has_subscriber(Manager *m) {
1270 SET_FOREACH(c, m->bus_connections_for_dispatch, i)
1271 if (bus_connection_has_subscriber(m, c))
1274 SET_FOREACH(c, m->bus_connections, i)
1275 if (bus_connection_has_subscriber(m, c))
1281 bool bus_connection_has_subscriber(Manager *m, DBusConnection *c) {
1285 return !set_isempty(BUS_CONNECTION_SUBSCRIBED(m, c));
1288 int bus_fdset_add_all(Manager *m, FDSet *fds) {
1295 /* When we are about to reexecute we add all D-Bus fds to the
1296 * set to pass over to the newly executed systemd. They won't
1297 * be used there however, except that they are closed at the
1298 * very end of deserialization, those making it possible for
1299 * clients to synchronously wait for systemd to reexec by
1300 * simply waiting for disconnection */
1302 SET_FOREACH(c, m->bus_connections_for_dispatch, i) {
1305 if (dbus_connection_get_unix_fd(c, &fd)) {
1306 fd = fdset_put_dup(fds, fd);
1313 SET_FOREACH(c, m->bus_connections, i) {
1316 if (dbus_connection_get_unix_fd(c, &fd)) {
1317 fd = fdset_put_dup(fds, fd);