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/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 #define CONNECTIONS_MAX 52
49 static const char bus_properties_interface[] = BUS_PROPERTIES_INTERFACE;
50 static const char bus_introspectable_interface[] = BUS_INTROSPECTABLE_INTERFACE;
52 const char *const bus_interface_table[] = {
53 "org.freedesktop.DBus.Properties", bus_properties_interface,
54 "org.freedesktop.DBus.Introspectable", bus_introspectable_interface,
55 "org.freedesktop.systemd1.Manager", bus_manager_interface,
56 "org.freedesktop.systemd1.Job", bus_job_interface,
57 "org.freedesktop.systemd1.Unit", bus_unit_interface,
58 "org.freedesktop.systemd1.Service", bus_service_interface,
59 "org.freedesktop.systemd1.Socket", bus_socket_interface,
60 "org.freedesktop.systemd1.Target", bus_target_interface,
61 "org.freedesktop.systemd1.Device", bus_device_interface,
62 "org.freedesktop.systemd1.Mount", bus_mount_interface,
63 "org.freedesktop.systemd1.Automount", bus_automount_interface,
64 "org.freedesktop.systemd1.Snapshot", bus_snapshot_interface,
65 "org.freedesktop.systemd1.Swap", bus_swap_interface,
66 "org.freedesktop.systemd1.Timer", bus_timer_interface,
67 "org.freedesktop.systemd1.Path", bus_path_interface,
71 static const char *error_to_dbus(int error);
72 static void bus_done_api(Manager *m);
73 static void bus_done_system(Manager *m);
74 static void bus_done_private(Manager *m);
75 static void shutdown_connection(Manager *m, DBusConnection *c);
77 static void bus_dispatch_status(DBusConnection *bus, DBusDispatchStatus status, void *data) {
83 /* We maintain two sets, one for those connections where we
84 * requested a dispatch, and another where we didn't. And then,
85 * we move the connections between the two sets. */
87 if (status == DBUS_DISPATCH_COMPLETE)
88 set_move_one(m->bus_connections, m->bus_connections_for_dispatch, bus);
90 set_move_one(m->bus_connections_for_dispatch, m->bus_connections, bus);
93 static uint32_t bus_flags_to_events(DBusWatch *bus_watch) {
99 /* no watch flags for disabled watches */
100 if (!dbus_watch_get_enabled(bus_watch))
103 flags = dbus_watch_get_flags(bus_watch);
105 if (flags & DBUS_WATCH_READABLE)
107 if (flags & DBUS_WATCH_WRITABLE)
110 return events | EPOLLHUP | EPOLLERR;
113 static unsigned events_to_bus_flags(uint32_t events) {
116 if (events & EPOLLIN)
117 flags |= DBUS_WATCH_READABLE;
118 if (events & EPOLLOUT)
119 flags |= DBUS_WATCH_WRITABLE;
120 if (events & EPOLLHUP)
121 flags |= DBUS_WATCH_HANGUP;
122 if (events & EPOLLERR)
123 flags |= DBUS_WATCH_ERROR;
128 void bus_watch_event(Manager *m, Watch *w, int events) {
132 /* This is called by the event loop whenever there is
133 * something happening on D-Bus' file handles. */
135 if (!dbus_watch_get_enabled(w->data.bus_watch))
138 dbus_watch_handle(w->data.bus_watch, events_to_bus_flags(events));
141 static dbus_bool_t bus_add_watch(DBusWatch *bus_watch, void *data) {
144 struct epoll_event ev;
149 if (!(w = new0(Watch, 1)))
152 w->fd = dbus_watch_get_unix_fd(bus_watch);
153 w->type = WATCH_DBUS_WATCH;
154 w->data.bus_watch = bus_watch;
157 ev.events = bus_flags_to_events(bus_watch);
160 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, w->fd, &ev) < 0) {
162 if (errno != EEXIST) {
167 /* Hmm, bloody D-Bus creates multiple watches on the
168 * same fd. epoll() does not like that. As a dirty
169 * hack we simply dup() the fd and hence get a second
170 * one we can safely add to the epoll(). */
172 if ((w->fd = dup(w->fd)) < 0) {
177 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, w->fd, &ev) < 0) {
179 close_nointr_nofail(w->fd);
183 w->fd_is_dupped = true;
186 dbus_watch_set_data(bus_watch, w, NULL);
191 static void bus_remove_watch(DBusWatch *bus_watch, void *data) {
198 if (!(w = dbus_watch_get_data(bus_watch)))
201 assert(w->type == WATCH_DBUS_WATCH);
202 assert_se(epoll_ctl(m->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
205 close_nointr_nofail(w->fd);
210 static void bus_toggle_watch(DBusWatch *bus_watch, void *data) {
213 struct epoll_event ev;
218 assert_se(w = dbus_watch_get_data(bus_watch));
219 assert(w->type == WATCH_DBUS_WATCH);
222 ev.events = bus_flags_to_events(bus_watch);
225 assert_se(epoll_ctl(m->epoll_fd, EPOLL_CTL_MOD, w->fd, &ev) == 0);
228 static int bus_timeout_arm(Manager *m, Watch *w) {
229 struct itimerspec its;
236 if (dbus_timeout_get_enabled(w->data.bus_timeout)) {
237 timespec_store(&its.it_value, dbus_timeout_get_interval(w->data.bus_timeout) * USEC_PER_MSEC);
238 its.it_interval = its.it_interval;
241 if (timerfd_settime(w->fd, 0, &its, NULL) < 0)
247 void bus_timeout_event(Manager *m, Watch *w, int events) {
251 /* This is called by the event loop whenever there is
252 * something happening on D-Bus' file handles. */
254 if (!(dbus_timeout_get_enabled(w->data.bus_timeout)))
257 dbus_timeout_handle(w->data.bus_timeout);
260 static dbus_bool_t bus_add_timeout(DBusTimeout *timeout, void *data) {
263 struct epoll_event ev;
268 if (!(w = new0(Watch, 1)))
271 if (!(w->fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC)) < 0)
274 w->type = WATCH_DBUS_TIMEOUT;
275 w->data.bus_timeout = timeout;
277 if (bus_timeout_arm(m, w) < 0)
284 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, w->fd, &ev) < 0)
287 dbus_timeout_set_data(timeout, w, NULL);
293 close_nointr_nofail(w->fd);
299 static void bus_remove_timeout(DBusTimeout *timeout, void *data) {
306 if (!(w = dbus_timeout_get_data(timeout)))
309 assert(w->type == WATCH_DBUS_TIMEOUT);
310 assert_se(epoll_ctl(m->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
311 close_nointr_nofail(w->fd);
315 static void bus_toggle_timeout(DBusTimeout *timeout, void *data) {
323 assert_se(w = dbus_timeout_get_data(timeout));
324 assert(w->type == WATCH_DBUS_TIMEOUT);
326 if ((r = bus_timeout_arm(m, w)) < 0)
327 log_error("Failed to rearm timer: %s", strerror(-r));
330 static DBusHandlerResult api_bus_message_filter(DBusConnection *connection, DBusMessage *message, void *data) {
333 DBusMessage *reply = NULL;
339 dbus_error_init(&error);
341 if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL ||
342 dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_SIGNAL)
343 log_debug("Got D-Bus request: %s.%s() on %s",
344 dbus_message_get_interface(message),
345 dbus_message_get_member(message),
346 dbus_message_get_path(message));
348 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
349 log_error("Warning! API D-Bus connection terminated.");
352 } else if (dbus_message_is_signal(message, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) {
353 const char *name, *old_owner, *new_owner;
355 if (!dbus_message_get_args(message, &error,
356 DBUS_TYPE_STRING, &name,
357 DBUS_TYPE_STRING, &old_owner,
358 DBUS_TYPE_STRING, &new_owner,
360 log_error("Failed to parse NameOwnerChanged message: %s", error.message);
362 if (set_remove(BUS_CONNECTION_SUBSCRIBED(m, connection), (char*) name))
363 log_debug("Subscription client vanished: %s (left: %u)", name, set_size(BUS_CONNECTION_SUBSCRIBED(m, connection)));
365 if (old_owner[0] == 0)
368 if (new_owner[0] == 0)
371 manager_dispatch_bus_name_owner_changed(m, name, old_owner, new_owner);
373 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Activator", "ActivationRequest")) {
376 if (!dbus_message_get_args(message, &error,
377 DBUS_TYPE_STRING, &name,
379 log_error("Failed to parse ActivationRequest message: %s", error.message);
384 log_debug("Got D-Bus activation request for %s", name);
386 r = manager_load_unit(m, name, NULL, &error, &u);
388 if (r >= 0 && u->meta.only_by_dependency)
392 r = manager_add_job(m, JOB_START, u, JOB_REPLACE, true, &error, NULL);
395 const char *id, *text;
397 log_warning("D-Bus activation failed for %s: %s", name, strerror(-r));
399 if (!(reply = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Activator", "ActivationFailure")))
402 id = error.name ? error.name : error_to_dbus(r);
403 text = bus_error(&error, r);
405 if (!dbus_message_set_destination(reply, DBUS_SERVICE_DBUS) ||
406 !dbus_message_append_args(reply,
407 DBUS_TYPE_STRING, &name,
408 DBUS_TYPE_STRING, &id,
409 DBUS_TYPE_STRING, &text,
414 /* On success we don't do anything, the service will be spwaned now */
418 dbus_error_free(&error);
421 if (!dbus_connection_send(connection, reply, NULL))
424 dbus_message_unref(reply);
427 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
431 dbus_message_unref(reply);
433 dbus_error_free(&error);
435 return DBUS_HANDLER_RESULT_NEED_MEMORY;
438 static DBusHandlerResult system_bus_message_filter(DBusConnection *connection, DBusMessage *message, void *data) {
446 dbus_error_init(&error);
448 if (m->api_bus != m->system_bus &&
449 (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL ||
450 dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_SIGNAL))
451 log_debug("Got D-Bus request on system bus: %s.%s() on %s",
452 dbus_message_get_interface(message),
453 dbus_message_get_member(message),
454 dbus_message_get_path(message));
456 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
457 log_error("Warning! System D-Bus connection terminated.");
460 } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Agent", "Released")) {
463 if (!dbus_message_get_args(message, &error,
464 DBUS_TYPE_STRING, &cgroup,
466 log_error("Failed to parse Released message: %s", error.message);
468 cgroup_notify_empty(m, cgroup);
471 dbus_error_free(&error);
472 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
475 static DBusHandlerResult private_bus_message_filter(DBusConnection *connection, DBusMessage *message, void *data) {
482 if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL ||
483 dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_SIGNAL)
484 log_debug("Got D-Bus request: %s.%s() on %s",
485 dbus_message_get_interface(message),
486 dbus_message_get_member(message),
487 dbus_message_get_path(message));
489 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected"))
490 shutdown_connection(m, connection);
492 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
495 unsigned bus_dispatch(Manager *m) {
500 if (m->queued_message) {
501 /* If we cannot get rid of this message we won't
502 * dispatch any D-Bus messages, so that we won't end
503 * up wanting to queue another message. */
505 if (m->queued_message_connection)
506 if (!dbus_connection_send(m->queued_message_connection, m->queued_message, NULL))
509 dbus_message_unref(m->queued_message);
510 m->queued_message = NULL;
511 m->queued_message_connection = NULL;
514 if ((c = set_first(m->bus_connections_for_dispatch))) {
515 if (dbus_connection_dispatch(c) == DBUS_DISPATCH_COMPLETE)
516 set_move_one(m->bus_connections, m->bus_connections_for_dispatch, c);
524 static void request_name_pending_cb(DBusPendingCall *pending, void *userdata) {
528 dbus_error_init(&error);
530 assert_se(reply = dbus_pending_call_steal_reply(pending));
532 switch (dbus_message_get_type(reply)) {
534 case DBUS_MESSAGE_TYPE_ERROR:
536 assert_se(dbus_set_error_from_message(&error, reply));
537 log_warning("RequestName() failed: %s", error.message);
540 case DBUS_MESSAGE_TYPE_METHOD_RETURN: {
543 if (!dbus_message_get_args(reply,
545 DBUS_TYPE_UINT32, &r,
546 DBUS_TYPE_INVALID)) {
547 log_error("Failed to parse RequestName() reply: %s", error.message);
552 log_debug("Successfully acquired name.");
554 log_error("Name already owned.");
560 assert_not_reached("Invalid reply message");
563 dbus_message_unref(reply);
564 dbus_error_free(&error);
567 static int request_name(Manager *m) {
568 const char *name = "org.freedesktop.systemd1";
570 DBusMessage *message = NULL;
571 DBusPendingCall *pending = NULL;
573 if (!(message = dbus_message_new_method_call(
580 if (!dbus_message_append_args(
582 DBUS_TYPE_STRING, &name,
583 DBUS_TYPE_UINT32, &flags,
587 if (!dbus_connection_send_with_reply(m->api_bus, message, &pending, -1))
590 if (!dbus_pending_call_set_notify(pending, request_name_pending_cb, m, NULL))
593 dbus_message_unref(message);
594 dbus_pending_call_unref(pending);
596 /* We simple ask for the name and don't wait for it. Sooner or
597 * later we'll have it. */
603 dbus_pending_call_cancel(pending);
604 dbus_pending_call_unref(pending);
608 dbus_message_unref(message);
613 static void query_name_list_pending_cb(DBusPendingCall *pending, void *userdata) {
616 Manager *m = userdata;
620 dbus_error_init(&error);
622 assert_se(reply = dbus_pending_call_steal_reply(pending));
624 switch (dbus_message_get_type(reply)) {
626 case DBUS_MESSAGE_TYPE_ERROR:
628 assert_se(dbus_set_error_from_message(&error, reply));
629 log_warning("ListNames() failed: %s", error.message);
632 case DBUS_MESSAGE_TYPE_METHOD_RETURN: {
636 if ((r = bus_parse_strv(reply, &l)) < 0)
637 log_warning("Failed to parse ListNames() reply: %s", strerror(-r));
642 /* This is a bit hacky, we say the
643 * owner of the name is the name
644 * itself, because we don't want the
645 * extra traffic to figure out the
647 manager_dispatch_bus_name_owner_changed(m, *t, NULL, *t);
656 assert_not_reached("Invalid reply message");
659 dbus_message_unref(reply);
660 dbus_error_free(&error);
663 static int query_name_list(Manager *m) {
664 DBusMessage *message = NULL;
665 DBusPendingCall *pending = NULL;
667 /* Asks for the currently installed bus names */
669 if (!(message = dbus_message_new_method_call(
676 if (!dbus_connection_send_with_reply(m->api_bus, message, &pending, -1))
679 if (!dbus_pending_call_set_notify(pending, query_name_list_pending_cb, m, NULL))
682 dbus_message_unref(message);
683 dbus_pending_call_unref(pending);
685 /* We simple ask for the list and don't wait for it. Sooner or
686 * later we'll get it. */
692 dbus_pending_call_cancel(pending);
693 dbus_pending_call_unref(pending);
697 dbus_message_unref(message);
702 static int bus_setup_loop(Manager *m, DBusConnection *bus) {
706 dbus_connection_set_exit_on_disconnect(bus, FALSE);
708 if (!dbus_connection_set_watch_functions(bus, bus_add_watch, bus_remove_watch, bus_toggle_watch, m, NULL) ||
709 !dbus_connection_set_timeout_functions(bus, bus_add_timeout, bus_remove_timeout, bus_toggle_timeout, m, NULL)) {
710 log_error("Not enough memory");
714 if (set_put(m->bus_connections_for_dispatch, bus) < 0) {
715 log_error("Not enough memory");
719 dbus_connection_set_dispatch_status_function(bus, bus_dispatch_status, m, NULL);
723 static dbus_bool_t allow_only_root(DBusConnection *connection, unsigned long uid, void *data) {
727 static void bus_new_connection(
729 DBusConnection *new_connection,
736 if (set_size(m->bus_connections) >= CONNECTIONS_MAX) {
737 log_error("Too many concurrent connections.");
741 dbus_connection_set_unix_user_function(new_connection, allow_only_root, NULL, NULL);
743 if (bus_setup_loop(m, new_connection) < 0)
746 if (!dbus_connection_register_object_path(new_connection, "/org/freedesktop/systemd1", &bus_manager_vtable, m) ||
747 !dbus_connection_register_fallback(new_connection, "/org/freedesktop/systemd1/unit", &bus_unit_vtable, m) ||
748 !dbus_connection_register_fallback(new_connection, "/org/freedesktop/systemd1/job", &bus_job_vtable, m) ||
749 !dbus_connection_add_filter(new_connection, private_bus_message_filter, m, NULL)) {
750 log_error("Not enough memory.");
754 log_debug("Accepted connection on private bus.");
756 dbus_connection_ref(new_connection);
759 static int bus_init_system(Manager *m) {
766 dbus_error_init(&error);
771 if (m->running_as == MANAGER_SYSTEM && m->api_bus)
772 m->system_bus = m->api_bus;
774 if (!(m->system_bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error))) {
775 log_debug("Failed to get system D-Bus connection, retrying later: %s", error.message);
780 if ((r = bus_setup_loop(m, m->system_bus)) < 0)
784 if (!dbus_connection_add_filter(m->system_bus, system_bus_message_filter, m, NULL)) {
785 log_error("Not enough memory");
790 dbus_bus_add_match(m->system_bus,
792 "interface='org.freedesktop.systemd1.Agent',"
794 "path='/org/freedesktop/systemd1/agent'",
797 if (dbus_error_is_set(&error)) {
798 log_error("Failed to register match: %s", error.message);
803 log_info("Successfully connected to system D-Bus bus %s as %s",
804 strnull((id = dbus_connection_get_server_id(m->system_bus))),
805 strnull(dbus_bus_get_unique_name(m->system_bus)));
812 dbus_error_free(&error);
817 static int bus_init_api(Manager *m) {
824 dbus_error_init(&error);
829 if (m->running_as == MANAGER_SYSTEM && m->system_bus)
830 m->api_bus = m->system_bus;
832 if (!(m->api_bus = dbus_bus_get_private(m->running_as == MANAGER_SESSION ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &error))) {
833 log_debug("Failed to get API D-Bus connection, retrying later: %s", error.message);
838 if ((r = bus_setup_loop(m, m->api_bus)) < 0)
842 if (!dbus_connection_register_object_path(m->api_bus, "/org/freedesktop/systemd1", &bus_manager_vtable, m) ||
843 !dbus_connection_register_fallback(m->api_bus, "/org/freedesktop/systemd1/unit", &bus_unit_vtable, m) ||
844 !dbus_connection_register_fallback(m->api_bus, "/org/freedesktop/systemd1/job", &bus_job_vtable, m) ||
845 !dbus_connection_add_filter(m->api_bus, api_bus_message_filter, m, NULL)) {
846 log_error("Not enough memory");
851 /* Get NameOwnerChange messages */
852 dbus_bus_add_match(m->api_bus,
854 "sender='"DBUS_SERVICE_DBUS"',"
855 "interface='"DBUS_INTERFACE_DBUS"',"
856 "member='NameOwnerChanged',"
857 "path='"DBUS_PATH_DBUS"'",
860 if (dbus_error_is_set(&error)) {
861 log_error("Failed to register match: %s", error.message);
866 /* Get activation requests */
867 dbus_bus_add_match(m->api_bus,
869 "sender='"DBUS_SERVICE_DBUS"',"
870 "interface='org.freedesktop.systemd1.Activator',"
871 "member='ActivationRequest',"
872 "path='"DBUS_PATH_DBUS"'",
875 if (dbus_error_is_set(&error)) {
876 log_error("Failed to register match: %s", error.message);
881 if ((r = request_name(m)) < 0)
884 if ((r = query_name_list(m)) < 0)
887 log_info("Successfully connected to API D-Bus bus %s as %s",
888 strnull((id = dbus_connection_get_server_id(m->api_bus))),
889 strnull(dbus_bus_get_unique_name(m->api_bus)));
896 dbus_error_free(&error);
901 static int bus_init_private(Manager *m) {
904 const char *const external_only[] = {
911 dbus_error_init(&error);
916 /* We want the private bus only when running as init */
920 if (!(m->private_bus = dbus_server_listen("unix:abstract=/org/freedesktop/systemd1/private", &error))) {
921 log_error("Failed to create private D-Bus server: %s", error.message);
926 if (!dbus_server_set_auth_mechanisms(m->private_bus, (const char**) external_only) ||
927 !dbus_server_set_watch_functions(m->private_bus, bus_add_watch, bus_remove_watch, bus_toggle_watch, m, NULL) ||
928 !dbus_server_set_timeout_functions(m->private_bus, bus_add_timeout, bus_remove_timeout, bus_toggle_timeout, m, NULL)) {
929 log_error("Not enough memory");
934 dbus_server_set_new_connection_function(m->private_bus, bus_new_connection, m, NULL);
936 log_debug("Successfully created private D-Bus server.");
942 dbus_error_free(&error);
947 int bus_init(Manager *m) {
950 if (set_ensure_allocated(&m->bus_connections, trivial_hash_func, trivial_compare_func) < 0 ||
951 set_ensure_allocated(&m->bus_connections_for_dispatch, trivial_hash_func, trivial_compare_func) < 0) {
952 log_error("Not enough memory");
956 if (m->name_data_slot < 0)
957 if (!dbus_pending_call_allocate_data_slot(&m->name_data_slot)) {
958 log_error("Not enough memory");
962 if (m->subscribed_data_slot < 0)
963 if (!dbus_pending_call_allocate_data_slot(&m->subscribed_data_slot)) {
964 log_error("Not enough memory");
968 if ((r = bus_init_system(m)) < 0 ||
969 (r = bus_init_api(m)) < 0 ||
970 (r = bus_init_private(m)) < 0)
976 static void shutdown_connection(Manager *m, DBusConnection *c) {
981 HASHMAP_FOREACH(j, m->jobs, i)
984 j->bus_client = NULL;
989 set_remove(m->bus_connections, c);
990 set_remove(m->bus_connections_for_dispatch, c);
992 if ((s = BUS_CONNECTION_SUBSCRIBED(m, c))) {
995 while ((t = set_steal_first(s)))
1001 if (m->queued_message_connection == c) {
1002 m->queued_message_connection = NULL;
1004 if (m->queued_message) {
1005 dbus_message_unref(m->queued_message);
1006 m->queued_message = NULL;
1010 dbus_connection_set_dispatch_status_function(c, NULL, NULL, NULL);
1011 dbus_connection_flush(c);
1012 dbus_connection_close(c);
1013 dbus_connection_unref(c);
1016 static void bus_done_api(Manager *m) {
1020 if (m->system_bus == m->api_bus)
1021 m->system_bus = NULL;
1023 shutdown_connection(m, m->api_bus);
1028 if (m->queued_message) {
1029 dbus_message_unref(m->queued_message);
1030 m->queued_message = NULL;
1034 static void bus_done_system(Manager *m) {
1037 if (m->system_bus == m->api_bus)
1040 if (m->system_bus) {
1041 shutdown_connection(m, m->system_bus);
1042 m->system_bus = NULL;
1046 static void bus_done_private(Manager *m) {
1048 if (m->private_bus) {
1049 dbus_server_disconnect(m->private_bus);
1050 dbus_server_unref(m->private_bus);
1051 m->private_bus = NULL;
1055 void bus_done(Manager *m) {
1060 bus_done_private(m);
1062 while ((c = set_steal_first(m->bus_connections)))
1063 shutdown_connection(m, c);
1065 while ((c = set_steal_first(m->bus_connections_for_dispatch)))
1066 shutdown_connection(m, c);
1068 set_free(m->bus_connections);
1069 set_free(m->bus_connections_for_dispatch);
1071 if (m->name_data_slot >= 0)
1072 dbus_pending_call_free_data_slot(&m->name_data_slot);
1074 if (m->subscribed_data_slot >= 0)
1075 dbus_pending_call_free_data_slot(&m->subscribed_data_slot);
1078 static void query_pid_pending_cb(DBusPendingCall *pending, void *userdata) {
1079 Manager *m = userdata;
1084 dbus_error_init(&error);
1086 assert_se(name = BUS_PENDING_CALL_NAME(m, pending));
1087 assert_se(reply = dbus_pending_call_steal_reply(pending));
1089 switch (dbus_message_get_type(reply)) {
1091 case DBUS_MESSAGE_TYPE_ERROR:
1093 assert_se(dbus_set_error_from_message(&error, reply));
1094 log_warning("GetConnectionUnixProcessID() failed: %s", error.message);
1097 case DBUS_MESSAGE_TYPE_METHOD_RETURN: {
1100 if (!dbus_message_get_args(reply,
1102 DBUS_TYPE_UINT32, &r,
1103 DBUS_TYPE_INVALID)) {
1104 log_error("Failed to parse GetConnectionUnixProcessID() reply: %s", error.message);
1108 manager_dispatch_bus_query_pid_done(m, name, (pid_t) r);
1113 assert_not_reached("Invalid reply message");
1116 dbus_message_unref(reply);
1117 dbus_error_free(&error);
1120 int bus_query_pid(Manager *m, const char *name) {
1121 DBusMessage *message = NULL;
1122 DBusPendingCall *pending = NULL;
1128 if (!(message = dbus_message_new_method_call(
1131 DBUS_INTERFACE_DBUS,
1132 "GetConnectionUnixProcessID")))
1135 if (!(dbus_message_append_args(
1137 DBUS_TYPE_STRING, &name,
1138 DBUS_TYPE_INVALID)))
1141 if (!dbus_connection_send_with_reply(m->api_bus, message, &pending, -1))
1144 if (!(n = strdup(name)))
1147 if (!dbus_pending_call_set_data(pending, m->name_data_slot, n, free))
1152 if (!dbus_pending_call_set_notify(pending, query_pid_pending_cb, m, NULL))
1155 dbus_message_unref(message);
1156 dbus_pending_call_unref(pending);
1164 dbus_pending_call_cancel(pending);
1165 dbus_pending_call_unref(pending);
1169 dbus_message_unref(message);
1174 DBusHandlerResult bus_default_message_handler(Manager *m, DBusConnection *c, DBusMessage *message, const char*introspection, const BusProperty *properties) {
1176 DBusMessage *reply = NULL;
1182 dbus_error_init(&error);
1184 if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect") && introspection) {
1186 if (!(reply = dbus_message_new_method_return(message)))
1189 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID))
1192 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "Get") && properties) {
1193 const char *interface, *property;
1194 const BusProperty *p;
1196 if (!dbus_message_get_args(
1199 DBUS_TYPE_STRING, &interface,
1200 DBUS_TYPE_STRING, &property,
1202 return bus_send_error_reply(m, c, message, &error, -EINVAL);
1204 for (p = properties; p->property; p++)
1205 if (streq(p->interface, interface) && streq(p->property, property))
1209 DBusMessageIter iter, sub;
1211 if (!(reply = dbus_message_new_method_return(message)))
1214 dbus_message_iter_init_append(reply, &iter);
1216 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, p->signature, &sub))
1219 if ((r = p->append(m, &sub, property, (void*) p->data)) < 0) {
1224 dbus_message_unref(reply);
1225 return bus_send_error_reply(m, c, message, NULL, r);
1228 if (!dbus_message_iter_close_container(&iter, &sub))
1231 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "GetAll") && properties) {
1232 const char *interface;
1233 const BusProperty *p;
1234 DBusMessageIter iter, sub, sub2, sub3;
1236 if (!dbus_message_get_args(
1239 DBUS_TYPE_STRING, &interface,
1241 return bus_send_error_reply(m, c, message, &error, -EINVAL);
1243 if (!(reply = dbus_message_new_method_return(message)))
1246 dbus_message_iter_init_append(reply, &iter);
1248 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub))
1251 for (p = properties; p->property; p++) {
1252 if (interface[0] && !streq(p->interface, interface))
1255 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_DICT_ENTRY, NULL, &sub2) ||
1256 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &p->property) ||
1257 !dbus_message_iter_open_container(&sub2, DBUS_TYPE_VARIANT, p->signature, &sub3))
1260 if ((r = p->append(m, &sub3, p->property, (void*) p->data)) < 0) {
1265 dbus_message_unref(reply);
1266 return bus_send_error_reply(m, c, message, NULL, r);
1269 if (!dbus_message_iter_close_container(&sub2, &sub3) ||
1270 !dbus_message_iter_close_container(&sub, &sub2))
1274 if (!dbus_message_iter_close_container(&iter, &sub))
1279 if (!dbus_connection_send(c, reply, NULL))
1282 dbus_message_unref(reply);
1283 return DBUS_HANDLER_RESULT_HANDLED;
1286 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1290 dbus_message_unref(reply);
1292 dbus_error_free(&error);
1294 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1297 static const char *error_to_dbus(int error) {
1302 return DBUS_ERROR_INVALID_ARGS;
1305 return DBUS_ERROR_NO_MEMORY;
1309 return DBUS_ERROR_ACCESS_DENIED;
1312 return DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN;
1315 return DBUS_ERROR_FILE_NOT_FOUND;
1318 return DBUS_ERROR_FILE_EXISTS;
1321 return DBUS_ERROR_TIMEOUT;
1324 return DBUS_ERROR_IO_ERROR;
1329 return DBUS_ERROR_DISCONNECTED;
1332 return DBUS_ERROR_FAILED;
1335 DBusHandlerResult bus_send_error_reply(Manager *m, DBusConnection *c, DBusMessage *message, DBusError *berror, int error) {
1336 DBusMessage *reply = NULL;
1337 const char *name, *text;
1339 if (berror && dbus_error_is_set(berror)) {
1340 name = berror->name;
1341 text = berror->message;
1343 name = error_to_dbus(error);
1344 text = strerror(-error);
1347 if (!(reply = dbus_message_new_error(message, name, text)))
1350 if (!dbus_connection_send(c, reply, NULL))
1353 dbus_message_unref(reply);
1356 dbus_error_free(berror);
1358 return DBUS_HANDLER_RESULT_HANDLED;
1362 dbus_message_unref(reply);
1365 dbus_error_free(berror);
1367 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1370 int bus_broadcast(Manager *m, DBusMessage *message) {
1378 SET_FOREACH(c, m->bus_connections_for_dispatch, i)
1379 if (c != m->system_bus || m->running_as == MANAGER_SYSTEM)
1380 oom = !dbus_connection_send(c, message, NULL);
1382 SET_FOREACH(c, m->bus_connections, i)
1383 if (c != m->system_bus || m->running_as == MANAGER_SYSTEM)
1384 oom = !dbus_connection_send(c, message, NULL);
1386 return oom ? -ENOMEM : 0;
1389 int bus_property_append_string(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1390 const char *t = data;
1399 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
1405 int bus_property_append_strv(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1406 DBusMessageIter sub;
1413 if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub))
1417 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, t))
1420 if (!dbus_message_iter_close_container(i, &sub))
1426 int bus_property_append_bool(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1437 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &db))
1443 int bus_property_append_uint64(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1449 /* Let's ensure that pid_t is actually 64bit, and hence this
1450 * function can be used for usec_t */
1451 assert_cc(sizeof(uint64_t) == sizeof(usec_t));
1453 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, data))
1459 int bus_property_append_uint32(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1465 /* Let's ensure that pid_t and mode_t is actually 32bit, and
1466 * hence this function can be used for pid_t/mode_t */
1467 assert_cc(sizeof(uint32_t) == sizeof(pid_t));
1468 assert_cc(sizeof(uint32_t) == sizeof(mode_t));
1469 assert_cc(sizeof(uint32_t) == sizeof(unsigned));
1471 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, data))
1477 int bus_property_append_int32(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1483 assert_cc(sizeof(int32_t) == sizeof(int));
1485 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT32, data))
1491 int bus_property_append_size(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1499 u = (uint64_t) *(size_t*) data;
1501 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
1507 int bus_property_append_ul(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1515 u = (uint64_t) *(unsigned long*) data;
1517 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
1523 int bus_parse_strv(DBusMessage *m, char ***_l) {
1524 DBusMessageIter iter, sub;
1525 unsigned n = 0, i = 0;
1531 if (!dbus_message_iter_init(m, &iter) ||
1532 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
1533 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRING)
1536 dbus_message_iter_recurse(&iter, &sub);
1538 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1540 dbus_message_iter_next(&sub);
1543 if (!(l = new(char*, n+1)))
1546 assert_se(dbus_message_iter_init(m, &iter));
1547 dbus_message_iter_recurse(&iter, &sub);
1549 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1552 assert_se(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
1553 dbus_message_iter_get_basic(&sub, &s);
1555 if (!(l[i++] = strdup(s))) {
1560 dbus_message_iter_next(&sub);
1572 bool bus_has_subscriber(Manager *m) {
1578 SET_FOREACH(c, m->bus_connections_for_dispatch, i)
1579 if (bus_connection_has_subscriber(m, c))
1582 SET_FOREACH(c, m->bus_connections, i)
1583 if (bus_connection_has_subscriber(m, c))
1589 bool bus_connection_has_subscriber(Manager *m, DBusConnection *c) {
1593 return !set_isempty(BUS_CONNECTION_SUBSCRIBED(m, c));