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"
46 #define CONNECTIONS_MAX 52
48 static const char bus_properties_interface
[] = BUS_PROPERTIES_INTERFACE
;
49 static const char bus_introspectable_interface
[] = BUS_INTROSPECTABLE_INTERFACE
;
51 const char *const bus_interface_table
[] = {
52 "org.freedesktop.DBus.Properties", bus_properties_interface
,
53 "org.freedesktop.DBus.Introspectable", bus_introspectable_interface
,
54 "org.freedesktop.systemd1.Manager", bus_manager_interface
,
55 "org.freedesktop.systemd1.Job", bus_job_interface
,
56 "org.freedesktop.systemd1.Unit", bus_unit_interface
,
57 "org.freedesktop.systemd1.Service", bus_service_interface
,
58 "org.freedesktop.systemd1.Socket", bus_socket_interface
,
59 "org.freedesktop.systemd1.Target", bus_target_interface
,
60 "org.freedesktop.systemd1.Device", bus_device_interface
,
61 "org.freedesktop.systemd1.Mount", bus_mount_interface
,
62 "org.freedesktop.systemd1.Automount", bus_automount_interface
,
63 "org.freedesktop.systemd1.Snapshot", bus_snapshot_interface
,
64 "org.freedesktop.systemd1.Swap", bus_swap_interface
,
65 "org.freedesktop.systemd1.Timer", bus_timer_interface
,
66 "org.freedesktop.systemd1.Path", bus_path_interface
,
70 static const char *error_to_dbus(int error
);
71 static void bus_done_api(Manager
*m
);
72 static void bus_done_system(Manager
*m
);
73 static void bus_done_private(Manager
*m
);
74 static void shutdown_connection(Manager
*m
, DBusConnection
*c
);
76 static void bus_dispatch_status(DBusConnection
*bus
, DBusDispatchStatus status
, void *data
) {
82 /* We maintain two sets, one for those connections where we
83 * requested a dispatch, and another where we didn't. And then,
84 * we move the connections between the two sets. */
86 if (status
== DBUS_DISPATCH_COMPLETE
)
87 set_move_one(m
->bus_connections
, m
->bus_connections_for_dispatch
, bus
);
89 set_move_one(m
->bus_connections_for_dispatch
, m
->bus_connections
, bus
);
92 static uint32_t bus_flags_to_events(DBusWatch
*bus_watch
) {
98 /* no watch flags for disabled watches */
99 if (!dbus_watch_get_enabled(bus_watch
))
102 flags
= dbus_watch_get_flags(bus_watch
);
104 if (flags
& DBUS_WATCH_READABLE
)
106 if (flags
& DBUS_WATCH_WRITABLE
)
109 return events
| EPOLLHUP
| EPOLLERR
;
112 static unsigned events_to_bus_flags(uint32_t events
) {
115 if (events
& EPOLLIN
)
116 flags
|= DBUS_WATCH_READABLE
;
117 if (events
& EPOLLOUT
)
118 flags
|= DBUS_WATCH_WRITABLE
;
119 if (events
& EPOLLHUP
)
120 flags
|= DBUS_WATCH_HANGUP
;
121 if (events
& EPOLLERR
)
122 flags
|= DBUS_WATCH_ERROR
;
127 void bus_watch_event(Manager
*m
, Watch
*w
, int events
) {
131 /* This is called by the event loop whenever there is
132 * something happening on D-Bus' file handles. */
134 if (!dbus_watch_get_enabled(w
->data
.bus_watch
))
137 dbus_watch_handle(w
->data
.bus_watch
, events_to_bus_flags(events
));
140 static dbus_bool_t
bus_add_watch(DBusWatch
*bus_watch
, void *data
) {
143 struct epoll_event ev
;
148 if (!(w
= new0(Watch
, 1)))
151 w
->fd
= dbus_watch_get_unix_fd(bus_watch
);
152 w
->type
= WATCH_DBUS_WATCH
;
153 w
->data
.bus_watch
= bus_watch
;
156 ev
.events
= bus_flags_to_events(bus_watch
);
159 if (epoll_ctl(m
->epoll_fd
, EPOLL_CTL_ADD
, w
->fd
, &ev
) < 0) {
161 if (errno
!= EEXIST
) {
166 /* Hmm, bloody D-Bus creates multiple watches on the
167 * same fd. epoll() does not like that. As a dirty
168 * hack we simply dup() the fd and hence get a second
169 * one we can safely add to the epoll(). */
171 if ((w
->fd
= dup(w
->fd
)) < 0) {
176 if (epoll_ctl(m
->epoll_fd
, EPOLL_CTL_ADD
, w
->fd
, &ev
) < 0) {
178 close_nointr_nofail(w
->fd
);
182 w
->fd_is_dupped
= true;
185 dbus_watch_set_data(bus_watch
, w
, NULL
);
190 static void bus_remove_watch(DBusWatch
*bus_watch
, void *data
) {
197 if (!(w
= dbus_watch_get_data(bus_watch
)))
200 assert(w
->type
== WATCH_DBUS_WATCH
);
201 assert_se(epoll_ctl(m
->epoll_fd
, EPOLL_CTL_DEL
, w
->fd
, NULL
) >= 0);
204 close_nointr_nofail(w
->fd
);
209 static void bus_toggle_watch(DBusWatch
*bus_watch
, void *data
) {
212 struct epoll_event ev
;
217 assert_se(w
= dbus_watch_get_data(bus_watch
));
218 assert(w
->type
== WATCH_DBUS_WATCH
);
221 ev
.events
= bus_flags_to_events(bus_watch
);
224 assert_se(epoll_ctl(m
->epoll_fd
, EPOLL_CTL_MOD
, w
->fd
, &ev
) == 0);
227 static int bus_timeout_arm(Manager
*m
, Watch
*w
) {
228 struct itimerspec its
;
235 if (dbus_timeout_get_enabled(w
->data
.bus_timeout
)) {
236 timespec_store(&its
.it_value
, dbus_timeout_get_interval(w
->data
.bus_timeout
) * USEC_PER_MSEC
);
237 its
.it_interval
= its
.it_interval
;
240 if (timerfd_settime(w
->fd
, 0, &its
, NULL
) < 0)
246 void bus_timeout_event(Manager
*m
, Watch
*w
, int events
) {
250 /* This is called by the event loop whenever there is
251 * something happening on D-Bus' file handles. */
253 if (!(dbus_timeout_get_enabled(w
->data
.bus_timeout
)))
256 dbus_timeout_handle(w
->data
.bus_timeout
);
259 static dbus_bool_t
bus_add_timeout(DBusTimeout
*timeout
, void *data
) {
262 struct epoll_event ev
;
267 if (!(w
= new0(Watch
, 1)))
270 if (!(w
->fd
= timerfd_create(CLOCK_MONOTONIC
, TFD_NONBLOCK
|TFD_CLOEXEC
)) < 0)
273 w
->type
= WATCH_DBUS_TIMEOUT
;
274 w
->data
.bus_timeout
= timeout
;
276 if (bus_timeout_arm(m
, w
) < 0)
283 if (epoll_ctl(m
->epoll_fd
, EPOLL_CTL_ADD
, w
->fd
, &ev
) < 0)
286 dbus_timeout_set_data(timeout
, w
, NULL
);
292 close_nointr_nofail(w
->fd
);
298 static void bus_remove_timeout(DBusTimeout
*timeout
, void *data
) {
305 if (!(w
= dbus_timeout_get_data(timeout
)))
308 assert(w
->type
== WATCH_DBUS_TIMEOUT
);
309 assert_se(epoll_ctl(m
->epoll_fd
, EPOLL_CTL_DEL
, w
->fd
, NULL
) >= 0);
310 close_nointr_nofail(w
->fd
);
314 static void bus_toggle_timeout(DBusTimeout
*timeout
, void *data
) {
322 assert_se(w
= dbus_timeout_get_data(timeout
));
323 assert(w
->type
== WATCH_DBUS_TIMEOUT
);
325 if ((r
= bus_timeout_arm(m
, w
)) < 0)
326 log_error("Failed to rearm timer: %s", strerror(-r
));
329 static DBusHandlerResult
api_bus_message_filter(DBusConnection
*connection
, DBusMessage
*message
, void *data
) {
332 DBusMessage
*reply
= NULL
;
338 dbus_error_init(&error
);
340 log_debug("Got D-Bus request: %s.%s() on %s",
341 dbus_message_get_interface(message
),
342 dbus_message_get_member(message
),
343 dbus_message_get_path(message
));
345 if (dbus_message_is_signal(message
, DBUS_INTERFACE_LOCAL
, "Disconnected")) {
346 log_error("Warning! API D-Bus connection terminated.");
349 } else if (dbus_message_is_signal(message
, DBUS_INTERFACE_DBUS
, "NameOwnerChanged")) {
350 const char *name
, *old_owner
, *new_owner
;
352 if (!dbus_message_get_args(message
, &error
,
353 DBUS_TYPE_STRING
, &name
,
354 DBUS_TYPE_STRING
, &old_owner
,
355 DBUS_TYPE_STRING
, &new_owner
,
357 log_error("Failed to parse NameOwnerChanged message: %s", error
.message
);
359 if (set_remove(BUS_CONNECTION_SUBSCRIBED(m
, connection
), (char*) name
))
360 log_debug("Subscription client vanished: %s (left: %u)", name
, set_size(BUS_CONNECTION_SUBSCRIBED(m
, connection
)));
362 if (old_owner
[0] == 0)
365 if (new_owner
[0] == 0)
368 manager_dispatch_bus_name_owner_changed(m
, name
, old_owner
, new_owner
);
370 } else if (dbus_message_is_signal(message
, "org.freedesktop.systemd1.Activator", "ActivationRequest")) {
373 if (!dbus_message_get_args(message
, &error
,
374 DBUS_TYPE_STRING
, &name
,
376 log_error("Failed to parse ActivationRequest message: %s", error
.message
);
381 log_debug("Got D-Bus activation request for %s", name
);
383 r
= manager_load_unit(m
, name
, NULL
, &u
);
385 if (r
>= 0 && u
->meta
.only_by_dependency
)
389 r
= manager_add_job(m
, JOB_START
, u
, JOB_REPLACE
, true, NULL
);
392 const char *id
, *text
;
394 log_warning("D-Bus activation failed for %s: %s", name
, strerror(-r
));
396 if (!(reply
= dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Activator", "ActivationFailure")))
399 id
= error_to_dbus(r
);
402 if (!dbus_message_set_destination(reply
, DBUS_SERVICE_DBUS
) ||
403 !dbus_message_append_args(reply
,
404 DBUS_TYPE_STRING
, &name
,
405 DBUS_TYPE_STRING
, &id
,
406 DBUS_TYPE_STRING
, &text
,
411 /* On success we don't do anything, the service will be spwaned now */
415 dbus_error_free(&error
);
418 if (!dbus_connection_send(connection
, reply
, NULL
))
421 dbus_message_unref(reply
);
424 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
428 dbus_message_unref(reply
);
430 dbus_error_free(&error
);
432 return DBUS_HANDLER_RESULT_NEED_MEMORY
;
435 static DBusHandlerResult
system_bus_message_filter(DBusConnection
*connection
, DBusMessage
*message
, void *data
) {
443 dbus_error_init(&error
);
445 log_debug("Got D-Bus request: %s.%s() on %s",
446 dbus_message_get_interface(message
),
447 dbus_message_get_member(message
),
448 dbus_message_get_path(message
));
450 if (dbus_message_is_signal(message
, DBUS_INTERFACE_LOCAL
, "Disconnected")) {
451 log_error("Warning! System D-Bus connection terminated.");
454 } else if (dbus_message_is_signal(message
, "org.freedesktop.systemd1.Agent", "Released")) {
457 if (!dbus_message_get_args(message
, &error
,
458 DBUS_TYPE_STRING
, &cgroup
,
460 log_error("Failed to parse Released message: %s", error
.message
);
462 cgroup_notify_empty(m
, cgroup
);
465 dbus_error_free(&error
);
466 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
469 static DBusHandlerResult
private_bus_message_filter(DBusConnection
*connection
, DBusMessage
*message
, void *data
) {
476 log_debug("Got D-Bus request: %s.%s() on %s",
477 dbus_message_get_interface(message
),
478 dbus_message_get_member(message
),
479 dbus_message_get_path(message
));
481 if (dbus_message_is_signal(message
, DBUS_INTERFACE_LOCAL
, "Disconnected"))
482 shutdown_connection(m
, connection
);
484 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
487 unsigned bus_dispatch(Manager
*m
) {
492 if (m
->queued_message
) {
493 /* If we cannot get rid of this message we won't
494 * dispatch any D-Bus messages, so that we won't end
495 * up wanting to queue another message. */
497 if (m
->queued_message_connection
)
498 if (!dbus_connection_send(m
->queued_message_connection
, m
->queued_message
, NULL
))
501 dbus_message_unref(m
->queued_message
);
502 m
->queued_message
= NULL
;
503 m
->queued_message_connection
= NULL
;
506 if ((c
= set_first(m
->bus_connections_for_dispatch
))) {
507 if (dbus_connection_dispatch(c
) == DBUS_DISPATCH_COMPLETE
)
508 set_move_one(m
->bus_connections
, m
->bus_connections_for_dispatch
, c
);
516 static void request_name_pending_cb(DBusPendingCall
*pending
, void *userdata
) {
520 dbus_error_init(&error
);
522 assert_se(reply
= dbus_pending_call_steal_reply(pending
));
524 switch (dbus_message_get_type(reply
)) {
526 case DBUS_MESSAGE_TYPE_ERROR
:
528 assert_se(dbus_set_error_from_message(&error
, reply
));
529 log_warning("RequestName() failed: %s", error
.message
);
532 case DBUS_MESSAGE_TYPE_METHOD_RETURN
: {
535 if (!dbus_message_get_args(reply
,
537 DBUS_TYPE_UINT32
, &r
,
538 DBUS_TYPE_INVALID
)) {
539 log_error("Failed to parse RequestName() reply: %s", error
.message
);
544 log_debug("Successfully acquired name.");
546 log_error("Name already owned.");
552 assert_not_reached("Invalid reply message");
555 dbus_message_unref(reply
);
556 dbus_error_free(&error
);
559 static int request_name(Manager
*m
) {
560 const char *name
= "org.freedesktop.systemd1";
562 DBusMessage
*message
= NULL
;
563 DBusPendingCall
*pending
= NULL
;
565 if (!(message
= dbus_message_new_method_call(
572 if (!dbus_message_append_args(
574 DBUS_TYPE_STRING
, &name
,
575 DBUS_TYPE_UINT32
, &flags
,
579 if (!dbus_connection_send_with_reply(m
->api_bus
, message
, &pending
, -1))
582 if (!dbus_pending_call_set_notify(pending
, request_name_pending_cb
, m
, NULL
))
585 dbus_message_unref(message
);
586 dbus_pending_call_unref(pending
);
588 /* We simple ask for the name and don't wait for it. Sooner or
589 * later we'll have it. */
595 dbus_pending_call_cancel(pending
);
596 dbus_pending_call_unref(pending
);
600 dbus_message_unref(message
);
605 static void query_name_list_pending_cb(DBusPendingCall
*pending
, void *userdata
) {
608 Manager
*m
= userdata
;
612 dbus_error_init(&error
);
614 assert_se(reply
= dbus_pending_call_steal_reply(pending
));
616 switch (dbus_message_get_type(reply
)) {
618 case DBUS_MESSAGE_TYPE_ERROR
:
620 assert_se(dbus_set_error_from_message(&error
, reply
));
621 log_warning("ListNames() failed: %s", error
.message
);
624 case DBUS_MESSAGE_TYPE_METHOD_RETURN
: {
628 if ((r
= bus_parse_strv(reply
, &l
)) < 0)
629 log_warning("Failed to parse ListNames() reply: %s", strerror(-r
));
634 /* This is a bit hacky, we say the
635 * owner of the name is the name
636 * itself, because we don't want the
637 * extra traffic to figure out the
639 manager_dispatch_bus_name_owner_changed(m
, *t
, NULL
, *t
);
648 assert_not_reached("Invalid reply message");
651 dbus_message_unref(reply
);
652 dbus_error_free(&error
);
655 static int query_name_list(Manager
*m
) {
656 DBusMessage
*message
= NULL
;
657 DBusPendingCall
*pending
= NULL
;
659 /* Asks for the currently installed bus names */
661 if (!(message
= dbus_message_new_method_call(
668 if (!dbus_connection_send_with_reply(m
->api_bus
, message
, &pending
, -1))
671 if (!dbus_pending_call_set_notify(pending
, query_name_list_pending_cb
, m
, NULL
))
674 dbus_message_unref(message
);
675 dbus_pending_call_unref(pending
);
677 /* We simple ask for the list and don't wait for it. Sooner or
678 * later we'll get it. */
684 dbus_pending_call_cancel(pending
);
685 dbus_pending_call_unref(pending
);
689 dbus_message_unref(message
);
694 static int bus_setup_loop(Manager
*m
, DBusConnection
*bus
) {
698 dbus_connection_set_exit_on_disconnect(bus
, FALSE
);
700 if (!dbus_connection_set_watch_functions(bus
, bus_add_watch
, bus_remove_watch
, bus_toggle_watch
, m
, NULL
) ||
701 !dbus_connection_set_timeout_functions(bus
, bus_add_timeout
, bus_remove_timeout
, bus_toggle_timeout
, m
, NULL
)) {
702 log_error("Not enough memory");
706 if (set_put(m
->bus_connections_for_dispatch
, bus
) < 0) {
707 log_error("Not enough memory");
711 dbus_connection_set_dispatch_status_function(bus
, bus_dispatch_status
, m
, NULL
);
715 static dbus_bool_t
allow_only_root(DBusConnection
*connection
, unsigned long uid
, void *data
) {
719 static void bus_new_connection(
721 DBusConnection
*new_connection
,
728 if (set_size(m
->bus_connections
) >= CONNECTIONS_MAX
) {
729 log_error("Too many concurrent connections.");
733 dbus_connection_set_unix_user_function(new_connection
, allow_only_root
, NULL
, NULL
);
735 if (bus_setup_loop(m
, new_connection
) < 0)
738 if (!dbus_connection_register_object_path(new_connection
, "/org/freedesktop/systemd1", &bus_manager_vtable
, m
) ||
739 !dbus_connection_register_fallback(new_connection
, "/org/freedesktop/systemd1/unit", &bus_unit_vtable
, m
) ||
740 !dbus_connection_register_fallback(new_connection
, "/org/freedesktop/systemd1/job", &bus_job_vtable
, m
) ||
741 !dbus_connection_add_filter(new_connection
, private_bus_message_filter
, m
, NULL
)) {
742 log_error("Not enough memory.");
746 log_debug("Accepted connection on private bus.");
748 dbus_connection_ref(new_connection
);
751 static int bus_init_system(Manager
*m
) {
758 dbus_error_init(&error
);
763 if (m
->running_as
== MANAGER_SYSTEM
&& m
->api_bus
)
764 m
->system_bus
= m
->api_bus
;
766 if (!(m
->system_bus
= dbus_bus_get_private(DBUS_BUS_SYSTEM
, &error
))) {
767 log_debug("Failed to get system D-Bus connection, retrying later: %s", error
.message
);
772 if ((r
= bus_setup_loop(m
, m
->system_bus
)) < 0)
776 if (!dbus_connection_add_filter(m
->system_bus
, system_bus_message_filter
, m
, NULL
)) {
777 log_error("Not enough memory");
782 dbus_bus_add_match(m
->system_bus
,
784 "interface='org.freedesktop.systemd1.Agent',"
786 "path='/org/freedesktop/systemd1/agent'",
789 if (dbus_error_is_set(&error
)) {
790 log_error("Failed to register match: %s", error
.message
);
795 log_info("Successfully connected to system D-Bus bus %s as %s",
796 strnull((id
= dbus_connection_get_server_id(m
->system_bus
))),
797 strnull(dbus_bus_get_unique_name(m
->system_bus
)));
804 dbus_error_free(&error
);
809 static int bus_init_api(Manager
*m
) {
816 dbus_error_init(&error
);
821 if (m
->running_as
== MANAGER_SYSTEM
&& m
->system_bus
)
822 m
->api_bus
= m
->system_bus
;
824 if (!(m
->api_bus
= dbus_bus_get_private(m
->running_as
== MANAGER_SESSION
? DBUS_BUS_SESSION
: DBUS_BUS_SYSTEM
, &error
))) {
825 log_debug("Failed to get API D-Bus connection, retrying later: %s", error
.message
);
830 if ((r
= bus_setup_loop(m
, m
->api_bus
)) < 0)
834 if (!dbus_connection_register_object_path(m
->api_bus
, "/org/freedesktop/systemd1", &bus_manager_vtable
, m
) ||
835 !dbus_connection_register_fallback(m
->api_bus
, "/org/freedesktop/systemd1/unit", &bus_unit_vtable
, m
) ||
836 !dbus_connection_register_fallback(m
->api_bus
, "/org/freedesktop/systemd1/job", &bus_job_vtable
, m
) ||
837 !dbus_connection_add_filter(m
->api_bus
, api_bus_message_filter
, m
, NULL
)) {
838 log_error("Not enough memory");
843 /* Get NameOwnerChange messages */
844 dbus_bus_add_match(m
->api_bus
,
846 "sender='"DBUS_SERVICE_DBUS
"',"
847 "interface='"DBUS_INTERFACE_DBUS
"',"
848 "member='NameOwnerChanged',"
849 "path='"DBUS_PATH_DBUS
"'",
852 if (dbus_error_is_set(&error
)) {
853 log_error("Failed to register match: %s", error
.message
);
858 /* Get activation requests */
859 dbus_bus_add_match(m
->api_bus
,
861 "sender='"DBUS_SERVICE_DBUS
"',"
862 "interface='org.freedesktop.systemd1.Activator',"
863 "member='ActivationRequest',"
864 "path='"DBUS_PATH_DBUS
"'",
867 if (dbus_error_is_set(&error
)) {
868 log_error("Failed to register match: %s", error
.message
);
873 if ((r
= request_name(m
)) < 0)
876 if ((r
= query_name_list(m
)) < 0)
879 log_info("Successfully connected to API D-Bus bus %s as %s",
880 strnull((id
= dbus_connection_get_server_id(m
->api_bus
))),
881 strnull(dbus_bus_get_unique_name(m
->api_bus
)));
888 dbus_error_free(&error
);
893 static int bus_init_private(Manager
*m
) {
896 const char *const external_only
[] = {
903 dbus_error_init(&error
);
908 /* We want the private bus only when running as init */
909 if (m
->running_as
!= MANAGER_SYSTEM
)
912 if (!(m
->private_bus
= dbus_server_listen("unix:abstract=/org/freedesktop/systemd1/private", &error
))) {
913 log_error("Failed to create private D-Bus server: %s", error
.message
);
918 if (!dbus_server_set_auth_mechanisms(m
->private_bus
, (const char**) external_only
) ||
919 !dbus_server_set_watch_functions(m
->private_bus
, bus_add_watch
, bus_remove_watch
, bus_toggle_watch
, m
, NULL
) ||
920 !dbus_server_set_timeout_functions(m
->private_bus
, bus_add_timeout
, bus_remove_timeout
, bus_toggle_timeout
, m
, NULL
)) {
921 log_error("Not enough memory");
926 dbus_server_set_new_connection_function(m
->private_bus
, bus_new_connection
, m
, NULL
);
928 log_debug("Successfully created private D-Bus server.");
934 dbus_error_free(&error
);
939 int bus_init(Manager
*m
) {
942 if (set_ensure_allocated(&m
->bus_connections
, trivial_hash_func
, trivial_compare_func
) < 0 ||
943 set_ensure_allocated(&m
->bus_connections_for_dispatch
, trivial_hash_func
, trivial_compare_func
) < 0) {
944 log_error("Not enough memory");
948 if (m
->name_data_slot
< 0)
949 if (!dbus_pending_call_allocate_data_slot(&m
->name_data_slot
)) {
950 log_error("Not enough memory");
954 if (m
->subscribed_data_slot
< 0)
955 if (!dbus_pending_call_allocate_data_slot(&m
->subscribed_data_slot
)) {
956 log_error("Not enough memory");
960 if ((r
= bus_init_system(m
)) < 0 ||
961 (r
= bus_init_api(m
)) < 0 ||
962 (r
= bus_init_private(m
)) < 0)
968 static void shutdown_connection(Manager
*m
, DBusConnection
*c
) {
973 HASHMAP_FOREACH(j
, m
->jobs
, i
)
976 j
->bus_client
= NULL
;
981 set_remove(m
->bus_connections
, c
);
982 set_remove(m
->bus_connections_for_dispatch
, c
);
984 if ((s
= BUS_CONNECTION_SUBSCRIBED(m
, c
))) {
987 while ((t
= set_steal_first(s
)))
993 if (m
->queued_message_connection
== c
) {
994 m
->queued_message_connection
= NULL
;
996 if (m
->queued_message
) {
997 dbus_message_unref(m
->queued_message
);
998 m
->queued_message
= NULL
;
1002 dbus_connection_set_dispatch_status_function(c
, NULL
, NULL
, NULL
);
1003 dbus_connection_flush(c
);
1004 dbus_connection_close(c
);
1005 dbus_connection_unref(c
);
1008 static void bus_done_api(Manager
*m
) {
1012 if (m
->system_bus
== m
->api_bus
)
1013 m
->system_bus
= NULL
;
1015 shutdown_connection(m
, m
->api_bus
);
1020 if (m
->queued_message
) {
1021 dbus_message_unref(m
->queued_message
);
1022 m
->queued_message
= NULL
;
1026 static void bus_done_system(Manager
*m
) {
1029 if (m
->system_bus
== m
->api_bus
)
1032 if (m
->system_bus
) {
1033 shutdown_connection(m
, m
->system_bus
);
1034 m
->system_bus
= NULL
;
1038 static void bus_done_private(Manager
*m
) {
1040 if (m
->private_bus
) {
1041 dbus_server_disconnect(m
->private_bus
);
1042 dbus_server_unref(m
->private_bus
);
1043 m
->private_bus
= NULL
;
1047 void bus_done(Manager
*m
) {
1052 bus_done_private(m
);
1054 while ((c
= set_steal_first(m
->bus_connections
)))
1055 shutdown_connection(m
, c
);
1057 while ((c
= set_steal_first(m
->bus_connections_for_dispatch
)))
1058 shutdown_connection(m
, c
);
1060 set_free(m
->bus_connections
);
1061 set_free(m
->bus_connections_for_dispatch
);
1063 if (m
->name_data_slot
>= 0)
1064 dbus_pending_call_free_data_slot(&m
->name_data_slot
);
1066 if (m
->subscribed_data_slot
>= 0)
1067 dbus_pending_call_free_data_slot(&m
->subscribed_data_slot
);
1070 static void query_pid_pending_cb(DBusPendingCall
*pending
, void *userdata
) {
1071 Manager
*m
= userdata
;
1076 dbus_error_init(&error
);
1078 assert_se(name
= BUS_PENDING_CALL_NAME(m
, pending
));
1079 assert_se(reply
= dbus_pending_call_steal_reply(pending
));
1081 switch (dbus_message_get_type(reply
)) {
1083 case DBUS_MESSAGE_TYPE_ERROR
:
1085 assert_se(dbus_set_error_from_message(&error
, reply
));
1086 log_warning("GetConnectionUnixProcessID() failed: %s", error
.message
);
1089 case DBUS_MESSAGE_TYPE_METHOD_RETURN
: {
1092 if (!dbus_message_get_args(reply
,
1094 DBUS_TYPE_UINT32
, &r
,
1095 DBUS_TYPE_INVALID
)) {
1096 log_error("Failed to parse GetConnectionUnixProcessID() reply: %s", error
.message
);
1100 manager_dispatch_bus_query_pid_done(m
, name
, (pid_t
) r
);
1105 assert_not_reached("Invalid reply message");
1108 dbus_message_unref(reply
);
1109 dbus_error_free(&error
);
1112 int bus_query_pid(Manager
*m
, const char *name
) {
1113 DBusMessage
*message
= NULL
;
1114 DBusPendingCall
*pending
= NULL
;
1120 if (!(message
= dbus_message_new_method_call(
1123 DBUS_INTERFACE_DBUS
,
1124 "GetConnectionUnixProcessID")))
1127 if (!(dbus_message_append_args(
1129 DBUS_TYPE_STRING
, &name
,
1130 DBUS_TYPE_INVALID
)))
1133 if (!dbus_connection_send_with_reply(m
->api_bus
, message
, &pending
, -1))
1136 if (!(n
= strdup(name
)))
1139 if (!dbus_pending_call_set_data(pending
, m
->name_data_slot
, n
, free
))
1144 if (!dbus_pending_call_set_notify(pending
, query_pid_pending_cb
, m
, NULL
))
1147 dbus_message_unref(message
);
1148 dbus_pending_call_unref(pending
);
1156 dbus_pending_call_cancel(pending
);
1157 dbus_pending_call_unref(pending
);
1161 dbus_message_unref(message
);
1166 DBusHandlerResult
bus_default_message_handler(Manager
*m
, DBusConnection
*c
, DBusMessage
*message
, const char*introspection
, const BusProperty
*properties
) {
1168 DBusMessage
*reply
= NULL
;
1174 dbus_error_init(&error
);
1176 if (dbus_message_is_method_call(message
, "org.freedesktop.DBus.Introspectable", "Introspect") && introspection
) {
1178 if (!(reply
= dbus_message_new_method_return(message
)))
1181 if (!dbus_message_append_args(reply
, DBUS_TYPE_STRING
, &introspection
, DBUS_TYPE_INVALID
))
1184 } else if (dbus_message_is_method_call(message
, "org.freedesktop.DBus.Properties", "Get") && properties
) {
1185 const char *interface
, *property
;
1186 const BusProperty
*p
;
1188 if (!dbus_message_get_args(
1191 DBUS_TYPE_STRING
, &interface
,
1192 DBUS_TYPE_STRING
, &property
,
1194 return bus_send_error_reply(m
, c
, message
, &error
, -EINVAL
);
1196 for (p
= properties
; p
->property
; p
++)
1197 if (streq(p
->interface
, interface
) && streq(p
->property
, property
))
1201 DBusMessageIter iter
, sub
;
1203 if (!(reply
= dbus_message_new_method_return(message
)))
1206 dbus_message_iter_init_append(reply
, &iter
);
1208 if (!dbus_message_iter_open_container(&iter
, DBUS_TYPE_VARIANT
, p
->signature
, &sub
))
1211 if ((r
= p
->append(m
, &sub
, property
, (void*) p
->data
)) < 0) {
1216 dbus_message_unref(reply
);
1217 return bus_send_error_reply(m
, c
, message
, NULL
, r
);
1220 if (!dbus_message_iter_close_container(&iter
, &sub
))
1223 } else if (dbus_message_is_method_call(message
, "org.freedesktop.DBus.Properties", "GetAll") && properties
) {
1224 const char *interface
;
1225 const BusProperty
*p
;
1226 DBusMessageIter iter
, sub
, sub2
, sub3
;
1228 if (!dbus_message_get_args(
1231 DBUS_TYPE_STRING
, &interface
,
1233 return bus_send_error_reply(m
, c
, message
, &error
, -EINVAL
);
1235 if (!(reply
= dbus_message_new_method_return(message
)))
1238 dbus_message_iter_init_append(reply
, &iter
);
1240 if (!dbus_message_iter_open_container(&iter
, DBUS_TYPE_ARRAY
, "{sv}", &sub
))
1243 for (p
= properties
; p
->property
; p
++) {
1244 if (interface
[0] && !streq(p
->interface
, interface
))
1247 if (!dbus_message_iter_open_container(&sub
, DBUS_TYPE_DICT_ENTRY
, NULL
, &sub2
) ||
1248 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &p
->property
) ||
1249 !dbus_message_iter_open_container(&sub2
, DBUS_TYPE_VARIANT
, p
->signature
, &sub3
))
1252 if ((r
= p
->append(m
, &sub3
, p
->property
, (void*) p
->data
)) < 0) {
1257 dbus_message_unref(reply
);
1258 return bus_send_error_reply(m
, c
, message
, NULL
, r
);
1261 if (!dbus_message_iter_close_container(&sub2
, &sub3
) ||
1262 !dbus_message_iter_close_container(&sub
, &sub2
))
1266 if (!dbus_message_iter_close_container(&iter
, &sub
))
1271 if (!dbus_connection_send(c
, reply
, NULL
))
1274 dbus_message_unref(reply
);
1275 return DBUS_HANDLER_RESULT_HANDLED
;
1278 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
1282 dbus_message_unref(reply
);
1284 dbus_error_free(&error
);
1286 return DBUS_HANDLER_RESULT_NEED_MEMORY
;
1289 static const char *error_to_dbus(int error
) {
1294 return DBUS_ERROR_INVALID_ARGS
;
1297 return DBUS_ERROR_NO_MEMORY
;
1301 return DBUS_ERROR_ACCESS_DENIED
;
1304 return DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN
;
1307 return DBUS_ERROR_FILE_NOT_FOUND
;
1310 return DBUS_ERROR_FILE_EXISTS
;
1313 return DBUS_ERROR_TIMEOUT
;
1316 return DBUS_ERROR_IO_ERROR
;
1321 return DBUS_ERROR_DISCONNECTED
;
1324 return DBUS_ERROR_FAILED
;
1327 DBusHandlerResult
bus_send_error_reply(Manager
*m
, DBusConnection
*c
, DBusMessage
*message
, DBusError
*bus_error
, int error
) {
1328 DBusMessage
*reply
= NULL
;
1329 const char *name
, *text
;
1331 if (bus_error
&& dbus_error_is_set(bus_error
)) {
1332 name
= bus_error
->name
;
1333 text
= bus_error
->message
;
1335 name
= error_to_dbus(error
);
1336 text
= strerror(-error
);
1339 if (!(reply
= dbus_message_new_error(message
, name
, text
)))
1342 if (!dbus_connection_send(c
, reply
, NULL
))
1345 dbus_message_unref(reply
);
1348 dbus_error_free(bus_error
);
1350 return DBUS_HANDLER_RESULT_HANDLED
;
1354 dbus_message_unref(reply
);
1357 dbus_error_free(bus_error
);
1359 return DBUS_HANDLER_RESULT_NEED_MEMORY
;
1362 int bus_broadcast(Manager
*m
, DBusMessage
*message
) {
1370 SET_FOREACH(c
, m
->bus_connections_for_dispatch
, i
)
1371 if (c
!= m
->system_bus
|| m
->running_as
== MANAGER_SYSTEM
)
1372 oom
= !dbus_connection_send(c
, message
, NULL
);
1374 SET_FOREACH(c
, m
->bus_connections
, i
)
1375 if (c
!= m
->system_bus
|| m
->running_as
== MANAGER_SYSTEM
)
1376 oom
= !dbus_connection_send(c
, message
, NULL
);
1378 return oom
? -ENOMEM
: 0;
1381 int bus_property_append_string(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
1382 const char *t
= data
;
1391 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_STRING
, &t
))
1397 int bus_property_append_strv(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
1398 DBusMessageIter sub
;
1405 if (!dbus_message_iter_open_container(i
, DBUS_TYPE_ARRAY
, "s", &sub
))
1409 if (!dbus_message_iter_append_basic(&sub
, DBUS_TYPE_STRING
, t
))
1412 if (!dbus_message_iter_close_container(i
, &sub
))
1418 int bus_property_append_bool(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
1429 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_BOOLEAN
, &db
))
1435 int bus_property_append_uint64(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
1441 /* Let's ensure that pid_t is actually 64bit, and hence this
1442 * function can be used for usec_t */
1443 assert_cc(sizeof(uint64_t) == sizeof(usec_t
));
1445 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_UINT64
, data
))
1451 int bus_property_append_uint32(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
1457 /* Let's ensure that pid_t and mode_t is actually 32bit, and
1458 * hence this function can be used for pid_t/mode_t */
1459 assert_cc(sizeof(uint32_t) == sizeof(pid_t
));
1460 assert_cc(sizeof(uint32_t) == sizeof(mode_t
));
1461 assert_cc(sizeof(uint32_t) == sizeof(unsigned));
1463 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_UINT32
, data
))
1469 int bus_property_append_int32(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
1475 assert_cc(sizeof(int32_t) == sizeof(int));
1477 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_INT32
, data
))
1483 int bus_property_append_size(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
1491 u
= (uint64_t) *(size_t*) data
;
1493 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_UINT64
, &u
))
1499 int bus_property_append_ul(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
1507 u
= (uint64_t) *(unsigned long*) data
;
1509 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_UINT64
, &u
))
1515 int bus_parse_strv(DBusMessage
*m
, char ***_l
) {
1516 DBusMessageIter iter
, sub
;
1517 unsigned n
= 0, i
= 0;
1523 if (!dbus_message_iter_init(m
, &iter
) ||
1524 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_ARRAY
||
1525 dbus_message_iter_get_element_type(&iter
) != DBUS_TYPE_STRING
)
1528 dbus_message_iter_recurse(&iter
, &sub
);
1530 while (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
1532 dbus_message_iter_next(&sub
);
1535 if (!(l
= new(char*, n
+1)))
1538 assert_se(dbus_message_iter_init(m
, &iter
));
1539 dbus_message_iter_recurse(&iter
, &sub
);
1541 while (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
1544 assert_se(dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_STRING
);
1545 dbus_message_iter_get_basic(&sub
, &s
);
1547 if (!(l
[i
++] = strdup(s
))) {
1552 dbus_message_iter_next(&sub
);
1564 bool bus_has_subscriber(Manager
*m
) {
1570 SET_FOREACH(c
, m
->bus_connections_for_dispatch
, i
)
1571 if (bus_connection_has_subscriber(m
, c
))
1574 SET_FOREACH(c
, m
->bus_connections
, i
)
1575 if (bus_connection_has_subscriber(m
, c
))
1581 bool bus_connection_has_subscriber(Manager
*m
, DBusConnection
*c
) {
1585 return !set_isempty(BUS_CONNECTION_SUBSCRIBED(m
, c
));