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
);
75 static void bus_dispatch_status(DBusConnection
*bus
, DBusDispatchStatus status
, void *data
) {
81 /* We maintain two sets, one for those connections where we
82 * requested a dispatch, and another where we didn't. And then,
83 * we move the connections between the two sets. */
85 if (status
== DBUS_DISPATCH_COMPLETE
)
86 set_move_one(m
->bus_connections
, m
->bus_connections_for_dispatch
, bus
);
88 set_move_one(m
->bus_connections_for_dispatch
, m
->bus_connections
, bus
);
91 static uint32_t bus_flags_to_events(DBusWatch
*bus_watch
) {
97 /* no watch flags for disabled watches */
98 if (!dbus_watch_get_enabled(bus_watch
))
101 flags
= dbus_watch_get_flags(bus_watch
);
103 if (flags
& DBUS_WATCH_READABLE
)
105 if (flags
& DBUS_WATCH_WRITABLE
)
108 return events
| EPOLLHUP
| EPOLLERR
;
111 static unsigned events_to_bus_flags(uint32_t events
) {
114 if (events
& EPOLLIN
)
115 flags
|= DBUS_WATCH_READABLE
;
116 if (events
& EPOLLOUT
)
117 flags
|= DBUS_WATCH_WRITABLE
;
118 if (events
& EPOLLHUP
)
119 flags
|= DBUS_WATCH_HANGUP
;
120 if (events
& EPOLLERR
)
121 flags
|= DBUS_WATCH_ERROR
;
126 void bus_watch_event(Manager
*m
, Watch
*w
, int events
) {
130 /* This is called by the event loop whenever there is
131 * something happening on D-Bus' file handles. */
133 if (!dbus_watch_get_enabled(w
->data
.bus_watch
))
136 dbus_watch_handle(w
->data
.bus_watch
, events_to_bus_flags(events
));
139 static dbus_bool_t
bus_add_watch(DBusWatch
*bus_watch
, void *data
) {
142 struct epoll_event ev
;
147 if (!(w
= new0(Watch
, 1)))
150 w
->fd
= dbus_watch_get_unix_fd(bus_watch
);
151 w
->type
= WATCH_DBUS_WATCH
;
152 w
->data
.bus_watch
= bus_watch
;
155 ev
.events
= bus_flags_to_events(bus_watch
);
158 if (epoll_ctl(m
->epoll_fd
, EPOLL_CTL_ADD
, w
->fd
, &ev
) < 0) {
160 if (errno
!= EEXIST
) {
165 /* Hmm, bloody D-Bus creates multiple watches on the
166 * same fd. epoll() does not like that. As a dirty
167 * hack we simply dup() the fd and hence get a second
168 * one we can safely add to the epoll(). */
170 if ((w
->fd
= dup(w
->fd
)) < 0) {
175 if (epoll_ctl(m
->epoll_fd
, EPOLL_CTL_ADD
, w
->fd
, &ev
) < 0) {
177 close_nointr_nofail(w
->fd
);
181 w
->fd_is_dupped
= true;
184 dbus_watch_set_data(bus_watch
, w
, NULL
);
189 static void bus_remove_watch(DBusWatch
*bus_watch
, void *data
) {
196 if (!(w
= dbus_watch_get_data(bus_watch
)))
199 assert(w
->type
== WATCH_DBUS_WATCH
);
200 assert_se(epoll_ctl(m
->epoll_fd
, EPOLL_CTL_DEL
, w
->fd
, NULL
) >= 0);
203 close_nointr_nofail(w
->fd
);
208 static void bus_toggle_watch(DBusWatch
*bus_watch
, void *data
) {
211 struct epoll_event ev
;
216 assert_se(w
= dbus_watch_get_data(bus_watch
));
217 assert(w
->type
== WATCH_DBUS_WATCH
);
220 ev
.events
= bus_flags_to_events(bus_watch
);
223 assert_se(epoll_ctl(m
->epoll_fd
, EPOLL_CTL_MOD
, w
->fd
, &ev
) == 0);
226 static int bus_timeout_arm(Manager
*m
, Watch
*w
) {
227 struct itimerspec its
;
234 if (dbus_timeout_get_enabled(w
->data
.bus_timeout
)) {
235 timespec_store(&its
.it_value
, dbus_timeout_get_interval(w
->data
.bus_timeout
) * USEC_PER_MSEC
);
236 its
.it_interval
= its
.it_interval
;
239 if (timerfd_settime(w
->fd
, 0, &its
, NULL
) < 0)
245 void bus_timeout_event(Manager
*m
, Watch
*w
, int events
) {
249 /* This is called by the event loop whenever there is
250 * something happening on D-Bus' file handles. */
252 if (!(dbus_timeout_get_enabled(w
->data
.bus_timeout
)))
255 dbus_timeout_handle(w
->data
.bus_timeout
);
258 static dbus_bool_t
bus_add_timeout(DBusTimeout
*timeout
, void *data
) {
261 struct epoll_event ev
;
266 if (!(w
= new0(Watch
, 1)))
269 if (!(w
->fd
= timerfd_create(CLOCK_MONOTONIC
, TFD_NONBLOCK
|TFD_CLOEXEC
)) < 0)
272 w
->type
= WATCH_DBUS_TIMEOUT
;
273 w
->data
.bus_timeout
= timeout
;
275 if (bus_timeout_arm(m
, w
) < 0)
282 if (epoll_ctl(m
->epoll_fd
, EPOLL_CTL_ADD
, w
->fd
, &ev
) < 0)
285 dbus_timeout_set_data(timeout
, w
, NULL
);
291 close_nointr_nofail(w
->fd
);
297 static void bus_remove_timeout(DBusTimeout
*timeout
, void *data
) {
304 if (!(w
= dbus_timeout_get_data(timeout
)))
307 assert(w
->type
== WATCH_DBUS_TIMEOUT
);
308 assert_se(epoll_ctl(m
->epoll_fd
, EPOLL_CTL_DEL
, w
->fd
, NULL
) >= 0);
309 close_nointr_nofail(w
->fd
);
313 static void bus_toggle_timeout(DBusTimeout
*timeout
, void *data
) {
321 assert_se(w
= dbus_timeout_get_data(timeout
));
322 assert(w
->type
== WATCH_DBUS_TIMEOUT
);
324 if ((r
= bus_timeout_arm(m
, w
)) < 0)
325 log_error("Failed to rearm timer: %s", strerror(-r
));
328 static DBusHandlerResult
api_bus_message_filter(DBusConnection
*connection
, DBusMessage
*message
, void *data
) {
331 DBusMessage
*reply
= NULL
;
337 dbus_error_init(&error
);
339 /* log_debug("Got D-Bus request: %s.%s() on %s", */
340 /* dbus_message_get_interface(message), */
341 /* dbus_message_get_member(message), */
342 /* dbus_message_get_path(message)); */
344 if (dbus_message_is_signal(message
, DBUS_INTERFACE_LOCAL
, "Disconnected")) {
345 log_error("Warning! API D-Bus connection terminated.");
348 } else if (dbus_message_is_signal(message
, DBUS_INTERFACE_DBUS
, "NameOwnerChanged")) {
349 const char *name
, *old_owner
, *new_owner
;
351 if (!dbus_message_get_args(message
, &error
,
352 DBUS_TYPE_STRING
, &name
,
353 DBUS_TYPE_STRING
, &old_owner
,
354 DBUS_TYPE_STRING
, &new_owner
,
356 log_error("Failed to parse NameOwnerChanged message: %s", error
.message
);
358 if (set_remove(m
->subscribed
, (char*) name
))
359 log_debug("Subscription client vanished: %s (left: %u)", name
, set_size(m
->subscribed
));
361 if (old_owner
[0] == 0)
364 if (new_owner
[0] == 0)
367 manager_dispatch_bus_name_owner_changed(m
, name
, old_owner
, new_owner
);
369 } else if (dbus_message_is_signal(message
, "org.freedesktop.systemd1.Activator", "ActivationRequest")) {
372 if (!dbus_message_get_args(message
, &error
,
373 DBUS_TYPE_STRING
, &name
,
375 log_error("Failed to parse ActivationRequest message: %s", error
.message
);
380 log_debug("Got D-Bus activation request for %s", name
);
382 r
= manager_load_unit(m
, name
, NULL
, &u
);
384 if (r
>= 0 && u
->meta
.only_by_dependency
)
388 r
= manager_add_job(m
, JOB_START
, u
, JOB_REPLACE
, true, NULL
);
391 const char *id
, *text
;
393 log_warning("D-Bus activation failed for %s: %s", name
, strerror(-r
));
395 if (!(reply
= dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Activator", "ActivationFailure")))
398 id
= error_to_dbus(r
);
401 if (!dbus_message_set_destination(reply
, DBUS_SERVICE_DBUS
) ||
402 !dbus_message_append_args(reply
,
403 DBUS_TYPE_STRING
, &name
,
404 DBUS_TYPE_STRING
, &id
,
405 DBUS_TYPE_STRING
, &text
,
410 /* On success we don't do anything, the service will be spwaned now */
414 dbus_error_free(&error
);
417 if (!dbus_connection_send(connection
, reply
, NULL
))
420 dbus_message_unref(reply
);
423 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
427 dbus_message_unref(reply
);
429 dbus_error_free(&error
);
431 return DBUS_HANDLER_RESULT_NEED_MEMORY
;
434 static DBusHandlerResult
system_bus_message_filter(DBusConnection
*connection
, DBusMessage
*message
, void *data
) {
442 dbus_error_init(&error
);
444 /* log_debug("Got D-Bus request: %s.%s() on %s", */
445 /* dbus_message_get_interface(message), */
446 /* dbus_message_get_member(message), */
447 /* dbus_message_get_path(message)); */
449 if (dbus_message_is_signal(message
, DBUS_INTERFACE_LOCAL
, "Disconnected")) {
450 log_error("Warning! System D-Bus connection terminated.");
453 } else if (dbus_message_is_signal(message
, "org.freedesktop.systemd1.Agent", "Released")) {
456 if (!dbus_message_get_args(message
, &error
,
457 DBUS_TYPE_STRING
, &cgroup
,
459 log_error("Failed to parse Released message: %s", error
.message
);
461 cgroup_notify_empty(m
, cgroup
);
464 dbus_error_free(&error
);
465 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
468 static DBusHandlerResult
private_bus_message_filter(DBusConnection
*connection
, DBusMessage
*message
, void *data
) {
475 /* log_debug("Got D-Bus request: %s.%s() on %s", */
476 /* dbus_message_get_interface(message), */
477 /* dbus_message_get_member(message), */
478 /* dbus_message_get_path(message)); */
480 if (dbus_message_is_signal(message
, DBUS_INTERFACE_LOCAL
, "Disconnected")) {
481 set_remove(m
->bus_connections
, connection
);
482 set_remove(m
->bus_connections_for_dispatch
, connection
);
483 dbus_connection_unref(connection
);
486 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
489 unsigned bus_dispatch(Manager
*m
) {
494 if (m
->queued_message
) {
495 /* If we cannot get rid of this message we won't
496 * dispatch any D-Bus messages, so that we won't end
497 * up wanting to queue another message. */
500 if (!dbus_connection_send(m
->api_bus
, m
->queued_message
, NULL
))
503 dbus_message_unref(m
->queued_message
);
504 m
->queued_message
= NULL
;
507 if ((c
= set_first(m
->bus_connections_for_dispatch
))) {
508 if (dbus_connection_dispatch(c
) == DBUS_DISPATCH_COMPLETE
)
509 set_move_one(m
->bus_connections
, m
->bus_connections_for_dispatch
, c
);
517 static void request_name_pending_cb(DBusPendingCall
*pending
, void *userdata
) {
521 dbus_error_init(&error
);
523 assert_se(reply
= dbus_pending_call_steal_reply(pending
));
525 switch (dbus_message_get_type(reply
)) {
527 case DBUS_MESSAGE_TYPE_ERROR
:
529 assert_se(dbus_set_error_from_message(&error
, reply
));
530 log_warning("RequestName() failed: %s", error
.message
);
533 case DBUS_MESSAGE_TYPE_METHOD_RETURN
: {
536 if (!dbus_message_get_args(reply
,
538 DBUS_TYPE_UINT32
, &r
,
539 DBUS_TYPE_INVALID
)) {
540 log_error("Failed to parse RequestName() reply: %s", error
.message
);
545 log_debug("Successfully acquired name.");
547 log_error("Name already owned.");
553 assert_not_reached("Invalid reply message");
556 dbus_message_unref(reply
);
557 dbus_error_free(&error
);
560 static int request_name(Manager
*m
) {
561 const char *name
= "org.freedesktop.systemd1";
563 DBusMessage
*message
= NULL
;
564 DBusPendingCall
*pending
= NULL
;
566 if (!(message
= dbus_message_new_method_call(
573 if (!dbus_message_append_args(
575 DBUS_TYPE_STRING
, &name
,
576 DBUS_TYPE_UINT32
, &flags
,
580 if (!dbus_connection_send_with_reply(m
->api_bus
, message
, &pending
, -1))
583 if (!dbus_pending_call_set_notify(pending
, request_name_pending_cb
, m
, NULL
))
586 dbus_message_unref(message
);
587 dbus_pending_call_unref(pending
);
589 /* We simple ask for the name and don't wait for it. Sooner or
590 * later we'll have it. */
596 dbus_pending_call_cancel(pending
);
597 dbus_pending_call_unref(pending
);
601 dbus_message_unref(message
);
606 static void query_name_list_pending_cb(DBusPendingCall
*pending
, void *userdata
) {
609 Manager
*m
= userdata
;
613 dbus_error_init(&error
);
615 assert_se(reply
= dbus_pending_call_steal_reply(pending
));
617 switch (dbus_message_get_type(reply
)) {
619 case DBUS_MESSAGE_TYPE_ERROR
:
621 assert_se(dbus_set_error_from_message(&error
, reply
));
622 log_warning("ListNames() failed: %s", error
.message
);
625 case DBUS_MESSAGE_TYPE_METHOD_RETURN
: {
629 if ((r
= bus_parse_strv(reply
, &l
)) < 0)
630 log_warning("Failed to parse ListNames() reply: %s", strerror(-r
));
635 /* This is a bit hacky, we say the
636 * owner of the name is the name
637 * itself, because we don't want the
638 * extra traffic to figure out the
640 manager_dispatch_bus_name_owner_changed(m
, *t
, NULL
, *t
);
649 assert_not_reached("Invalid reply message");
652 dbus_message_unref(reply
);
653 dbus_error_free(&error
);
656 static int query_name_list(Manager
*m
) {
657 DBusMessage
*message
= NULL
;
658 DBusPendingCall
*pending
= NULL
;
660 /* Asks for the currently installed bus names */
662 if (!(message
= dbus_message_new_method_call(
669 if (!dbus_connection_send_with_reply(m
->api_bus
, message
, &pending
, -1))
672 if (!dbus_pending_call_set_notify(pending
, query_name_list_pending_cb
, m
, NULL
))
675 dbus_message_unref(message
);
676 dbus_pending_call_unref(pending
);
678 /* We simple ask for the list and don't wait for it. Sooner or
679 * later we'll get it. */
685 dbus_pending_call_cancel(pending
);
686 dbus_pending_call_unref(pending
);
690 dbus_message_unref(message
);
695 static int bus_setup_loop(Manager
*m
, DBusConnection
*bus
) {
699 dbus_connection_set_exit_on_disconnect(bus
, FALSE
);
701 if (!dbus_connection_set_watch_functions(bus
, bus_add_watch
, bus_remove_watch
, bus_toggle_watch
, m
, NULL
) ||
702 !dbus_connection_set_timeout_functions(bus
, bus_add_timeout
, bus_remove_timeout
, bus_toggle_timeout
, m
, NULL
)) {
703 log_error("Not enough memory");
707 if (set_put(m
->bus_connections_for_dispatch
, bus
) < 0) {
708 log_error("Not enough memory");
712 dbus_connection_set_dispatch_status_function(bus
, bus_dispatch_status
, m
, NULL
);
716 static dbus_bool_t
allow_only_root(DBusConnection
*connection
, unsigned long uid
, void *data
) {
720 static void bus_new_connection(
722 DBusConnection
*new_connection
,
729 if (set_size(m
->bus_connections
) >= CONNECTIONS_MAX
) {
730 log_error("Too many concurrent connections.");
734 dbus_connection_set_unix_user_function(new_connection
, allow_only_root
, NULL
, NULL
);
736 if (bus_setup_loop(m
, new_connection
) < 0)
739 if (!dbus_connection_register_object_path(new_connection
, "/org/freedesktop/systemd1", &bus_manager_vtable
, m
) ||
740 !dbus_connection_register_fallback(new_connection
, "/org/freedesktop/systemd1/unit", &bus_unit_vtable
, m
) ||
741 !dbus_connection_register_fallback(new_connection
, "/org/freedesktop/systemd1/job", &bus_job_vtable
, m
) ||
742 !dbus_connection_add_filter(new_connection
, private_bus_message_filter
, m
, NULL
)) {
743 log_error("Not enough memory.");
747 log_debug("Accepted connection on private bus.");
749 dbus_connection_ref(new_connection
);
752 static int bus_init_system(Manager
*m
) {
759 dbus_error_init(&error
);
764 if (m
->running_as
!= MANAGER_SESSION
&& m
->api_bus
)
765 m
->system_bus
= m
->api_bus
;
767 if (!(m
->system_bus
= dbus_bus_get_private(DBUS_BUS_SYSTEM
, &error
))) {
768 log_debug("Failed to get system D-Bus connection, retrying later: %s", error
.message
);
773 if ((r
= bus_setup_loop(m
, m
->system_bus
)) < 0)
777 if (!dbus_connection_add_filter(m
->system_bus
, system_bus_message_filter
, m
, NULL
)) {
778 log_error("Not enough memory");
783 dbus_bus_add_match(m
->system_bus
,
785 "interface='org.freedesktop.systemd1.Agent',"
787 "path='/org/freedesktop/systemd1/agent'",
790 if (dbus_error_is_set(&error
)) {
791 log_error("Failed to register match: %s", error
.message
);
796 log_debug("Successfully connected to system D-Bus bus %s as %s",
797 strnull((id
= dbus_connection_get_server_id(m
->system_bus
))),
798 strnull(dbus_bus_get_unique_name(m
->system_bus
)));
805 dbus_error_free(&error
);
810 static int bus_init_api(Manager
*m
) {
817 dbus_error_init(&error
);
822 if (m
->running_as
!= MANAGER_SESSION
&& m
->system_bus
)
823 m
->api_bus
= m
->system_bus
;
825 if (!(m
->api_bus
= dbus_bus_get_private(m
->running_as
== MANAGER_SESSION
? DBUS_BUS_SESSION
: DBUS_BUS_SYSTEM
, &error
))) {
826 log_debug("Failed to get API D-Bus connection, retrying later: %s", error
.message
);
831 if ((r
= bus_setup_loop(m
, m
->api_bus
)) < 0)
835 if (!dbus_connection_register_object_path(m
->api_bus
, "/org/freedesktop/systemd1", &bus_manager_vtable
, m
) ||
836 !dbus_connection_register_fallback(m
->api_bus
, "/org/freedesktop/systemd1/unit", &bus_unit_vtable
, m
) ||
837 !dbus_connection_register_fallback(m
->api_bus
, "/org/freedesktop/systemd1/job", &bus_job_vtable
, m
) ||
838 !dbus_connection_add_filter(m
->api_bus
, api_bus_message_filter
, m
, NULL
)) {
839 log_error("Not enough memory");
844 /* Get NameOwnerChange messages */
845 dbus_bus_add_match(m
->api_bus
,
847 "sender='"DBUS_SERVICE_DBUS
"',"
848 "interface='"DBUS_INTERFACE_DBUS
"',"
849 "member='NameOwnerChanged',"
850 "path='"DBUS_PATH_DBUS
"'",
853 if (dbus_error_is_set(&error
)) {
854 log_error("Failed to register match: %s", error
.message
);
859 /* Get activation requests */
860 dbus_bus_add_match(m
->api_bus
,
862 "sender='"DBUS_SERVICE_DBUS
"',"
863 "interface='org.freedesktop.systemd1.Activator',"
864 "member='ActivationRequest',"
865 "path='"DBUS_PATH_DBUS
"'",
868 if (dbus_error_is_set(&error
)) {
869 log_error("Failed to register match: %s", error
.message
);
874 if ((r
= request_name(m
)) < 0)
877 if ((r
= query_name_list(m
)) < 0)
880 log_debug("Successfully connected to API D-Bus bus %s as %s",
881 strnull((id
= dbus_connection_get_server_id(m
->api_bus
))),
882 strnull(dbus_bus_get_unique_name(m
->api_bus
)));
886 if (!(m
->subscribed
= set_new(string_hash_func
, string_compare_func
))) {
887 log_error("Not enough memory");
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 */
917 if (m
->running_as
!= MANAGER_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 create 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 ((r
= bus_init_system(m
)) < 0 ||
963 (r
= bus_init_api(m
)) < 0 ||
964 (r
= bus_init_private(m
)) < 0)
970 static void shutdown_connection(DBusConnection
*c
) {
971 dbus_connection_set_dispatch_status_function(c
, NULL
, NULL
, NULL
);
972 dbus_connection_flush(c
);
973 dbus_connection_close(c
);
974 dbus_connection_unref(c
);
977 static void bus_done_api(Manager
*m
) {
981 if (m
->system_bus
== m
->api_bus
)
982 m
->system_bus
= NULL
;
984 set_remove(m
->bus_connections
, m
->api_bus
);
985 shutdown_connection(m
->api_bus
);
992 while ((c
= set_steal_first(m
->subscribed
)))
995 set_free(m
->subscribed
);
996 m
->subscribed
= NULL
;
999 if (m
->queued_message
) {
1000 dbus_message_unref(m
->queued_message
);
1001 m
->queued_message
= NULL
;
1005 static void bus_done_system(Manager
*m
) {
1008 if (m
->system_bus
== m
->api_bus
)
1011 if (m
->system_bus
) {
1012 set_remove(m
->bus_connections
, m
->system_bus
);
1013 shutdown_connection(m
->system_bus
);
1014 m
->system_bus
= NULL
;
1018 static void bus_done_private(Manager
*m
) {
1020 if (m
->private_bus
) {
1021 dbus_server_disconnect(m
->private_bus
);
1022 dbus_server_unref(m
->private_bus
);
1023 m
->private_bus
= NULL
;
1027 void bus_done(Manager
*m
) {
1032 bus_done_private(m
);
1034 while ((c
= set_steal_first(m
->bus_connections
)))
1035 shutdown_connection(c
);
1037 while ((c
= set_steal_first(m
->bus_connections_for_dispatch
)))
1038 shutdown_connection(c
);
1040 set_free(m
->bus_connections
);
1041 set_free(m
->bus_connections_for_dispatch
);
1043 if (m
->name_data_slot
>= 0)
1044 dbus_pending_call_free_data_slot(&m
->name_data_slot
);
1047 static void query_pid_pending_cb(DBusPendingCall
*pending
, void *userdata
) {
1048 Manager
*m
= userdata
;
1053 dbus_error_init(&error
);
1055 assert_se(name
= dbus_pending_call_get_data(pending
, m
->name_data_slot
));
1056 assert_se(reply
= dbus_pending_call_steal_reply(pending
));
1058 switch (dbus_message_get_type(reply
)) {
1060 case DBUS_MESSAGE_TYPE_ERROR
:
1062 assert_se(dbus_set_error_from_message(&error
, reply
));
1063 log_warning("GetConnectionUnixProcessID() failed: %s", error
.message
);
1066 case DBUS_MESSAGE_TYPE_METHOD_RETURN
: {
1069 if (!dbus_message_get_args(reply
,
1071 DBUS_TYPE_UINT32
, &r
,
1072 DBUS_TYPE_INVALID
)) {
1073 log_error("Failed to parse GetConnectionUnixProcessID() reply: %s", error
.message
);
1077 manager_dispatch_bus_query_pid_done(m
, name
, (pid_t
) r
);
1082 assert_not_reached("Invalid reply message");
1085 dbus_message_unref(reply
);
1086 dbus_error_free(&error
);
1089 int bus_query_pid(Manager
*m
, const char *name
) {
1090 DBusMessage
*message
= NULL
;
1091 DBusPendingCall
*pending
= NULL
;
1097 if (!(message
= dbus_message_new_method_call(
1100 DBUS_INTERFACE_DBUS
,
1101 "GetConnectionUnixProcessID")))
1104 if (!(dbus_message_append_args(
1106 DBUS_TYPE_STRING
, &name
,
1107 DBUS_TYPE_INVALID
)))
1110 if (!dbus_connection_send_with_reply(m
->api_bus
, message
, &pending
, -1))
1113 if (!(n
= strdup(name
)))
1116 if (!dbus_pending_call_set_data(pending
, m
->name_data_slot
, n
, free
))
1121 if (!dbus_pending_call_set_notify(pending
, query_pid_pending_cb
, m
, NULL
))
1124 dbus_message_unref(message
);
1125 dbus_pending_call_unref(pending
);
1133 dbus_pending_call_cancel(pending
);
1134 dbus_pending_call_unref(pending
);
1138 dbus_message_unref(message
);
1143 DBusHandlerResult
bus_default_message_handler(Manager
*m
, DBusConnection
*c
, DBusMessage
*message
, const char*introspection
, const BusProperty
*properties
) {
1145 DBusMessage
*reply
= NULL
;
1151 dbus_error_init(&error
);
1153 if (dbus_message_is_method_call(message
, "org.freedesktop.DBus.Introspectable", "Introspect") && introspection
) {
1155 if (!(reply
= dbus_message_new_method_return(message
)))
1158 if (!dbus_message_append_args(reply
, DBUS_TYPE_STRING
, &introspection
, DBUS_TYPE_INVALID
))
1161 } else if (dbus_message_is_method_call(message
, "org.freedesktop.DBus.Properties", "Get") && properties
) {
1162 const char *interface
, *property
;
1163 const BusProperty
*p
;
1165 if (!dbus_message_get_args(
1168 DBUS_TYPE_STRING
, &interface
,
1169 DBUS_TYPE_STRING
, &property
,
1171 return bus_send_error_reply(m
, c
, message
, &error
, -EINVAL
);
1173 for (p
= properties
; p
->property
; p
++)
1174 if (streq(p
->interface
, interface
) && streq(p
->property
, property
))
1178 DBusMessageIter iter
, sub
;
1180 if (!(reply
= dbus_message_new_method_return(message
)))
1183 dbus_message_iter_init_append(reply
, &iter
);
1185 if (!dbus_message_iter_open_container(&iter
, DBUS_TYPE_VARIANT
, p
->signature
, &sub
))
1188 if ((r
= p
->append(m
, &sub
, property
, (void*) p
->data
)) < 0) {
1193 dbus_message_unref(reply
);
1194 return bus_send_error_reply(m
, c
, message
, NULL
, r
);
1197 if (!dbus_message_iter_close_container(&iter
, &sub
))
1200 } else if (dbus_message_is_method_call(message
, "org.freedesktop.DBus.Properties", "GetAll") && properties
) {
1201 const char *interface
;
1202 const BusProperty
*p
;
1203 DBusMessageIter iter
, sub
, sub2
, sub3
;
1206 if (!dbus_message_get_args(
1209 DBUS_TYPE_STRING
, &interface
,
1211 return bus_send_error_reply(m
, c
, message
, &error
, -EINVAL
);
1213 if (!(reply
= dbus_message_new_method_return(message
)))
1216 dbus_message_iter_init_append(reply
, &iter
);
1218 if (!dbus_message_iter_open_container(&iter
, DBUS_TYPE_ARRAY
, "{sv}", &sub
))
1221 for (p
= properties
; p
->property
; p
++) {
1222 if (!streq(p
->interface
, interface
))
1225 if (!dbus_message_iter_open_container(&sub
, DBUS_TYPE_DICT_ENTRY
, NULL
, &sub2
) ||
1226 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &p
->property
) ||
1227 !dbus_message_iter_open_container(&sub2
, DBUS_TYPE_VARIANT
, p
->signature
, &sub3
))
1230 if ((r
= p
->append(m
, &sub3
, p
->property
, (void*) p
->data
)) < 0) {
1235 dbus_message_unref(reply
);
1236 return bus_send_error_reply(m
, c
, message
, NULL
, r
);
1239 if (!dbus_message_iter_close_container(&sub2
, &sub3
) ||
1240 !dbus_message_iter_close_container(&sub
, &sub2
))
1246 if (!dbus_message_iter_close_container(&iter
, &sub
))
1251 if (!dbus_connection_send(c
, reply
, NULL
))
1254 dbus_message_unref(reply
);
1255 return DBUS_HANDLER_RESULT_HANDLED
;
1258 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
1262 dbus_message_unref(reply
);
1264 dbus_error_free(&error
);
1266 return DBUS_HANDLER_RESULT_NEED_MEMORY
;
1269 static const char *error_to_dbus(int error
) {
1274 return DBUS_ERROR_INVALID_ARGS
;
1277 return DBUS_ERROR_NO_MEMORY
;
1281 return DBUS_ERROR_ACCESS_DENIED
;
1284 return DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN
;
1287 return DBUS_ERROR_FILE_NOT_FOUND
;
1290 return DBUS_ERROR_FILE_EXISTS
;
1293 return DBUS_ERROR_TIMEOUT
;
1296 return DBUS_ERROR_IO_ERROR
;
1301 return DBUS_ERROR_DISCONNECTED
;
1304 return DBUS_ERROR_FAILED
;
1307 DBusHandlerResult
bus_send_error_reply(Manager
*m
, DBusConnection
*c
, DBusMessage
*message
, DBusError
*bus_error
, int error
) {
1308 DBusMessage
*reply
= NULL
;
1309 const char *name
, *text
;
1311 if (bus_error
&& dbus_error_is_set(bus_error
)) {
1312 name
= bus_error
->name
;
1313 text
= bus_error
->message
;
1315 name
= error_to_dbus(error
);
1316 text
= strerror(-error
);
1319 if (!(reply
= dbus_message_new_error(message
, name
, text
)))
1322 if (!dbus_connection_send(c
, reply
, NULL
))
1325 dbus_message_unref(reply
);
1328 dbus_error_free(bus_error
);
1330 return DBUS_HANDLER_RESULT_HANDLED
;
1334 dbus_message_unref(reply
);
1337 dbus_error_free(bus_error
);
1339 return DBUS_HANDLER_RESULT_NEED_MEMORY
;
1342 int bus_broadcast(Manager
*m
, DBusMessage
*message
) {
1350 SET_FOREACH(c
, m
->bus_connections_for_dispatch
, i
)
1351 if (c
!= m
->system_bus
|| m
->running_as
!= MANAGER_SESSION
)
1352 oom
= !dbus_connection_send(c
, message
, NULL
);
1354 SET_FOREACH(c
, m
->bus_connections
, i
)
1355 if (c
!= m
->system_bus
|| m
->running_as
!= MANAGER_SESSION
)
1356 oom
= !dbus_connection_send(c
, message
, NULL
);
1358 return oom
? -ENOMEM
: 0;
1361 int bus_property_append_string(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
1362 const char *t
= data
;
1371 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_STRING
, &t
))
1377 int bus_property_append_strv(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
1378 DBusMessageIter sub
;
1385 if (!dbus_message_iter_open_container(i
, DBUS_TYPE_ARRAY
, "s", &sub
))
1389 if (!dbus_message_iter_append_basic(&sub
, DBUS_TYPE_STRING
, t
))
1392 if (!dbus_message_iter_close_container(i
, &sub
))
1398 int bus_property_append_bool(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
1409 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_BOOLEAN
, &db
))
1415 int bus_property_append_uint64(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
1421 /* Let's ensure that pid_t is actually 64bit, and hence this
1422 * function can be used for usec_t */
1423 assert_cc(sizeof(uint64_t) == sizeof(usec_t
));
1425 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_UINT64
, data
))
1431 int bus_property_append_uint32(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
1437 /* Let's ensure that pid_t and mode_t is actually 32bit, and
1438 * hence this function can be used for pid_t/mode_t */
1439 assert_cc(sizeof(uint32_t) == sizeof(pid_t
));
1440 assert_cc(sizeof(uint32_t) == sizeof(mode_t
));
1441 assert_cc(sizeof(uint32_t) == sizeof(unsigned));
1443 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_UINT32
, data
))
1449 int bus_property_append_int32(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
1455 assert_cc(sizeof(int32_t) == sizeof(int));
1457 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_INT32
, data
))
1463 int bus_parse_strv(DBusMessage
*m
, char ***_l
) {
1464 DBusMessageIter iter
, sub
;
1465 unsigned n
= 0, i
= 0;
1471 if (!dbus_message_iter_init(m
, &iter
) ||
1472 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_ARRAY
||
1473 dbus_message_iter_get_element_type(&iter
) != DBUS_TYPE_STRING
)
1476 dbus_message_iter_recurse(&iter
, &sub
);
1478 while (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
1480 dbus_message_iter_next(&sub
);
1483 if (!(l
= new(char*, n
+1)))
1486 assert_se(dbus_message_iter_init(m
, &iter
));
1487 dbus_message_iter_recurse(&iter
, &sub
);
1489 while (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
1492 assert_se(dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_STRING
);
1493 dbus_message_iter_get_basic(&sub
, &s
);
1495 if (!(l
[i
++] = strdup(s
))) {
1500 dbus_message_iter_next(&sub
);