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 static const char bus_properties_interface
[] = BUS_PROPERTIES_INTERFACE
;
47 static const char bus_introspectable_interface
[] = BUS_INTROSPECTABLE_INTERFACE
;
49 const char *const bus_interface_table
[] = {
50 "org.freedesktop.DBus.Properties", bus_properties_interface
,
51 "org.freedesktop.DBus.Introspectable", bus_introspectable_interface
,
52 "org.freedesktop.systemd1.Manager", bus_manager_interface
,
53 "org.freedesktop.systemd1.Job", bus_job_interface
,
54 "org.freedesktop.systemd1.Unit", bus_unit_interface
,
55 "org.freedesktop.systemd1.Service", bus_service_interface
,
56 "org.freedesktop.systemd1.Socket", bus_socket_interface
,
57 "org.freedesktop.systemd1.Target", bus_target_interface
,
58 "org.freedesktop.systemd1.Device", bus_device_interface
,
59 "org.freedesktop.systemd1.Mount", bus_mount_interface
,
60 "org.freedesktop.systemd1.Automount", bus_automount_interface
,
61 "org.freedesktop.systemd1.Snapshot", bus_snapshot_interface
,
62 "org.freedesktop.systemd1.Swap", bus_swap_interface
,
63 "org.freedesktop.systemd1.Timer", bus_timer_interface
,
64 "org.freedesktop.systemd1.Path", bus_path_interface
,
68 static const char *error_to_dbus(int error
);
70 static void api_bus_dispatch_status(DBusConnection
*bus
, DBusDispatchStatus status
, void *data
) {
79 assert(m
->api_bus
== bus
);
81 m
->request_api_bus_dispatch
= status
!= DBUS_DISPATCH_COMPLETE
;
84 static void system_bus_dispatch_status(DBusConnection
*bus
, DBusDispatchStatus status
, void *data
) {
93 assert(m
->system_bus
== bus
);
95 m
->request_system_bus_dispatch
= status
!= DBUS_DISPATCH_COMPLETE
;
98 static uint32_t bus_flags_to_events(DBusWatch
*bus_watch
) {
104 /* no watch flags for disabled watches */
105 if (!dbus_watch_get_enabled(bus_watch
))
108 flags
= dbus_watch_get_flags(bus_watch
);
110 if (flags
& DBUS_WATCH_READABLE
)
112 if (flags
& DBUS_WATCH_WRITABLE
)
115 return events
| EPOLLHUP
| EPOLLERR
;
118 static unsigned events_to_bus_flags(uint32_t events
) {
121 if (events
& EPOLLIN
)
122 flags
|= DBUS_WATCH_READABLE
;
123 if (events
& EPOLLOUT
)
124 flags
|= DBUS_WATCH_WRITABLE
;
125 if (events
& EPOLLHUP
)
126 flags
|= DBUS_WATCH_HANGUP
;
127 if (events
& EPOLLERR
)
128 flags
|= DBUS_WATCH_ERROR
;
133 void bus_watch_event(Manager
*m
, Watch
*w
, int events
) {
137 /* This is called by the event loop whenever there is
138 * something happening on D-Bus' file handles. */
140 if (!dbus_watch_get_enabled(w
->data
.bus_watch
))
143 dbus_watch_handle(w
->data
.bus_watch
, events_to_bus_flags(events
));
146 static dbus_bool_t
bus_add_watch(DBusWatch
*bus_watch
, void *data
) {
149 struct epoll_event ev
;
154 if (!(w
= new0(Watch
, 1)))
157 w
->fd
= dbus_watch_get_unix_fd(bus_watch
);
158 w
->type
= WATCH_DBUS_WATCH
;
159 w
->data
.bus_watch
= bus_watch
;
162 ev
.events
= bus_flags_to_events(bus_watch
);
165 if (epoll_ctl(m
->epoll_fd
, EPOLL_CTL_ADD
, w
->fd
, &ev
) < 0) {
167 if (errno
!= EEXIST
) {
172 /* Hmm, bloody D-Bus creates multiple watches on the
173 * same fd. epoll() does not like that. As a dirty
174 * hack we simply dup() the fd and hence get a second
175 * one we can safely add to the epoll(). */
177 if ((w
->fd
= dup(w
->fd
)) < 0) {
182 if (epoll_ctl(m
->epoll_fd
, EPOLL_CTL_ADD
, w
->fd
, &ev
) < 0) {
184 close_nointr_nofail(w
->fd
);
188 w
->fd_is_dupped
= true;
191 dbus_watch_set_data(bus_watch
, w
, NULL
);
196 static void bus_remove_watch(DBusWatch
*bus_watch
, void *data
) {
203 if (!(w
= dbus_watch_get_data(bus_watch
)))
206 assert(w
->type
== WATCH_DBUS_WATCH
);
207 assert_se(epoll_ctl(m
->epoll_fd
, EPOLL_CTL_DEL
, w
->fd
, NULL
) >= 0);
210 close_nointr_nofail(w
->fd
);
215 static void bus_toggle_watch(DBusWatch
*bus_watch
, void *data
) {
218 struct epoll_event ev
;
223 assert_se(w
= dbus_watch_get_data(bus_watch
));
224 assert(w
->type
== WATCH_DBUS_WATCH
);
227 ev
.events
= bus_flags_to_events(bus_watch
);
230 assert_se(epoll_ctl(m
->epoll_fd
, EPOLL_CTL_MOD
, w
->fd
, &ev
) == 0);
233 static int bus_timeout_arm(Manager
*m
, Watch
*w
) {
234 struct itimerspec its
;
241 if (dbus_timeout_get_enabled(w
->data
.bus_timeout
)) {
242 timespec_store(&its
.it_value
, dbus_timeout_get_interval(w
->data
.bus_timeout
) * USEC_PER_MSEC
);
243 its
.it_interval
= its
.it_interval
;
246 if (timerfd_settime(w
->fd
, 0, &its
, NULL
) < 0)
252 void bus_timeout_event(Manager
*m
, Watch
*w
, int events
) {
256 /* This is called by the event loop whenever there is
257 * something happening on D-Bus' file handles. */
259 if (!(dbus_timeout_get_enabled(w
->data
.bus_timeout
)))
262 dbus_timeout_handle(w
->data
.bus_timeout
);
265 static dbus_bool_t
bus_add_timeout(DBusTimeout
*timeout
, void *data
) {
268 struct epoll_event ev
;
273 if (!(w
= new0(Watch
, 1)))
276 if (!(w
->fd
= timerfd_create(CLOCK_MONOTONIC
, TFD_NONBLOCK
|TFD_CLOEXEC
)) < 0)
279 w
->type
= WATCH_DBUS_TIMEOUT
;
280 w
->data
.bus_timeout
= timeout
;
282 if (bus_timeout_arm(m
, w
) < 0)
289 if (epoll_ctl(m
->epoll_fd
, EPOLL_CTL_ADD
, w
->fd
, &ev
) < 0)
292 dbus_timeout_set_data(timeout
, w
, NULL
);
298 close_nointr_nofail(w
->fd
);
304 static void bus_remove_timeout(DBusTimeout
*timeout
, void *data
) {
311 if (!(w
= dbus_timeout_get_data(timeout
)))
314 assert(w
->type
== WATCH_DBUS_TIMEOUT
);
315 assert_se(epoll_ctl(m
->epoll_fd
, EPOLL_CTL_DEL
, w
->fd
, NULL
) >= 0);
316 close_nointr_nofail(w
->fd
);
320 static void bus_toggle_timeout(DBusTimeout
*timeout
, void *data
) {
328 assert_se(w
= dbus_timeout_get_data(timeout
));
329 assert(w
->type
== WATCH_DBUS_TIMEOUT
);
331 if ((r
= bus_timeout_arm(m
, w
)) < 0)
332 log_error("Failed to rearm timer: %s", strerror(-r
));
335 static DBusHandlerResult
api_bus_message_filter(DBusConnection
*connection
, DBusMessage
*message
, void *data
) {
338 DBusMessage
*reply
= NULL
;
344 dbus_error_init(&error
);
346 /* log_debug("Got D-Bus request: %s.%s() on %s", */
347 /* dbus_message_get_interface(message), */
348 /* dbus_message_get_member(message), */
349 /* dbus_message_get_path(message)); */
351 if (dbus_message_is_signal(message
, DBUS_INTERFACE_LOCAL
, "Disconnected")) {
352 log_error("Warning! API D-Bus connection terminated.");
355 } else if (dbus_message_is_signal(message
, DBUS_INTERFACE_DBUS
, "NameOwnerChanged")) {
356 const char *name
, *old_owner
, *new_owner
;
358 if (!dbus_message_get_args(message
, &error
,
359 DBUS_TYPE_STRING
, &name
,
360 DBUS_TYPE_STRING
, &old_owner
,
361 DBUS_TYPE_STRING
, &new_owner
,
363 log_error("Failed to parse NameOwnerChanged message: %s", error
.message
);
365 if (set_remove(m
->subscribed
, (char*) name
))
366 log_debug("Subscription client vanished: %s (left: %u)", name
, set_size(m
->subscribed
));
368 if (old_owner
[0] == 0)
371 if (new_owner
[0] == 0)
374 manager_dispatch_bus_name_owner_changed(m
, name
, old_owner
, new_owner
);
376 } else if (dbus_message_is_signal(message
, "org.freedesktop.systemd1.Activator", "ActivationRequest")) {
379 if (!dbus_message_get_args(message
, &error
,
380 DBUS_TYPE_STRING
, &name
,
382 log_error("Failed to parse ActivationRequest message: %s", error
.message
);
387 log_debug("Got D-Bus activation request for %s", name
);
389 r
= manager_load_unit(m
, name
, NULL
, &u
);
391 if (r
>= 0 && u
->meta
.only_by_dependency
)
395 r
= manager_add_job(m
, JOB_START
, u
, JOB_REPLACE
, true, NULL
);
398 const char *id
, *text
;
400 log_warning("D-Bus activation failed for %s: %s", name
, strerror(-r
));
402 if (!(reply
= dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Activator", "ActivationFailure")))
405 id
= error_to_dbus(r
);
408 if (!dbus_message_set_destination(reply
, DBUS_SERVICE_DBUS
) ||
409 !dbus_message_append_args(reply
,
410 DBUS_TYPE_STRING
, &name
,
411 DBUS_TYPE_STRING
, &id
,
412 DBUS_TYPE_STRING
, &text
,
417 /* On success we don't do anything, the service will be spwaned now */
421 dbus_error_free(&error
);
424 if (!dbus_connection_send(connection
, reply
, NULL
))
427 dbus_message_unref(reply
);
430 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
434 dbus_message_unref(reply
);
436 dbus_error_free(&error
);
438 return DBUS_HANDLER_RESULT_NEED_MEMORY
;
441 static DBusHandlerResult
system_bus_message_filter(DBusConnection
*connection
, DBusMessage
*message
, void *data
) {
449 dbus_error_init(&error
);
451 /* log_debug("Got D-Bus request: %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 } 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 unsigned bus_dispatch(Manager
*m
) {
478 if (m
->queued_message
) {
479 /* If we cannot get rid of this message we won't
480 * dispatch any D-Bus messages, so that we won't end
481 * up wanting to queue another message. */
483 if (!dbus_connection_send(m
->api_bus
, m
->queued_message
, NULL
))
486 dbus_message_unref(m
->queued_message
);
487 m
->queued_message
= NULL
;
490 if (m
->request_api_bus_dispatch
) {
491 if (dbus_connection_dispatch(m
->api_bus
) == DBUS_DISPATCH_COMPLETE
)
492 m
->request_api_bus_dispatch
= false;
497 if (m
->request_system_bus_dispatch
) {
498 if (dbus_connection_dispatch(m
->system_bus
) == DBUS_DISPATCH_COMPLETE
)
499 m
->request_system_bus_dispatch
= false;
507 static void request_name_pending_cb(DBusPendingCall
*pending
, void *userdata
) {
511 dbus_error_init(&error
);
513 assert_se(reply
= dbus_pending_call_steal_reply(pending
));
515 switch (dbus_message_get_type(reply
)) {
517 case DBUS_MESSAGE_TYPE_ERROR
:
519 assert_se(dbus_set_error_from_message(&error
, reply
));
520 log_warning("RequestName() failed: %s", error
.message
);
523 case DBUS_MESSAGE_TYPE_METHOD_RETURN
: {
526 if (!dbus_message_get_args(reply
,
528 DBUS_TYPE_UINT32
, &r
,
529 DBUS_TYPE_INVALID
)) {
530 log_error("Failed to parse RequestName() reply: %s", error
.message
);
535 log_debug("Successfully acquired name.");
537 log_error("Name already owned.");
543 assert_not_reached("Invalid reply message");
546 dbus_message_unref(reply
);
547 dbus_error_free(&error
);
550 static int request_name(Manager
*m
) {
551 const char *name
= "org.freedesktop.systemd1";
553 DBusMessage
*message
= NULL
;
554 DBusPendingCall
*pending
= NULL
;
556 if (!(message
= dbus_message_new_method_call(
563 if (!dbus_message_append_args(
565 DBUS_TYPE_STRING
, &name
,
566 DBUS_TYPE_UINT32
, &flags
,
570 if (!dbus_connection_send_with_reply(m
->api_bus
, message
, &pending
, -1))
573 if (!dbus_pending_call_set_notify(pending
, request_name_pending_cb
, m
, NULL
))
576 dbus_message_unref(message
);
577 dbus_pending_call_unref(pending
);
579 /* We simple ask for the name and don't wait for it. Sooner or
580 * later we'll have it. */
586 dbus_pending_call_cancel(pending
);
587 dbus_pending_call_unref(pending
);
591 dbus_message_unref(message
);
596 static void query_name_list_pending_cb(DBusPendingCall
*pending
, void *userdata
) {
599 Manager
*m
= userdata
;
603 dbus_error_init(&error
);
605 assert_se(reply
= dbus_pending_call_steal_reply(pending
));
607 switch (dbus_message_get_type(reply
)) {
609 case DBUS_MESSAGE_TYPE_ERROR
:
611 assert_se(dbus_set_error_from_message(&error
, reply
));
612 log_warning("ListNames() failed: %s", error
.message
);
615 case DBUS_MESSAGE_TYPE_METHOD_RETURN
: {
619 if ((r
= bus_parse_strv(reply
, &l
)) < 0)
620 log_warning("Failed to parse ListNames() reply: %s", strerror(-r
));
625 /* This is a bit hacky, we say the
626 * owner of the name is the name
627 * itself, because we don't want the
628 * extra traffic to figure out the
630 manager_dispatch_bus_name_owner_changed(m
, *t
, NULL
, *t
);
639 assert_not_reached("Invalid reply message");
642 dbus_message_unref(reply
);
643 dbus_error_free(&error
);
646 static int query_name_list(Manager
*m
) {
647 DBusMessage
*message
= NULL
;
648 DBusPendingCall
*pending
= NULL
;
650 /* Asks for the currently installed bus names */
652 if (!(message
= dbus_message_new_method_call(
659 if (!dbus_connection_send_with_reply(m
->api_bus
, message
, &pending
, -1))
662 if (!dbus_pending_call_set_notify(pending
, query_name_list_pending_cb
, m
, NULL
))
665 dbus_message_unref(message
);
666 dbus_pending_call_unref(pending
);
668 /* We simple ask for the list and don't wait for it. Sooner or
669 * later we'll get it. */
675 dbus_pending_call_cancel(pending
);
676 dbus_pending_call_unref(pending
);
680 dbus_message_unref(message
);
685 static int bus_setup_loop(Manager
*m
, DBusConnection
*bus
) {
689 dbus_connection_set_exit_on_disconnect(bus
, FALSE
);
691 if (!dbus_connection_set_watch_functions(bus
, bus_add_watch
, bus_remove_watch
, bus_toggle_watch
, m
, NULL
) ||
692 !dbus_connection_set_timeout_functions(bus
, bus_add_timeout
, bus_remove_timeout
, bus_toggle_timeout
, m
, NULL
))
698 int bus_init_system(Manager
*m
) {
705 dbus_error_init(&error
);
710 if (m
->running_as
!= MANAGER_SESSION
&& m
->api_bus
)
711 m
->system_bus
= m
->api_bus
;
713 if (!(m
->system_bus
= dbus_bus_get_private(DBUS_BUS_SYSTEM
, &error
))) {
714 log_debug("Failed to get system D-Bus connection, retrying later: %s", error
.message
);
715 dbus_error_free(&error
);
719 dbus_connection_set_dispatch_status_function(m
->system_bus
, system_bus_dispatch_status
, m
, NULL
);
720 m
->request_system_bus_dispatch
= true;
722 if ((r
= bus_setup_loop(m
, m
->system_bus
)) < 0) {
728 if (!dbus_connection_add_filter(m
->system_bus
, system_bus_message_filter
, m
, NULL
)) {
733 dbus_bus_add_match(m
->system_bus
,
735 "interface='org.freedesktop.systemd1.Agent',"
736 "path='/org/freedesktop/systemd1/agent'",
739 if (dbus_error_is_set(&error
)) {
740 log_error("Failed to register match: %s", error
.message
);
741 dbus_error_free(&error
);
746 log_debug("Successfully connected to system D-Bus bus %s as %s",
747 strnull((id
= dbus_connection_get_server_id(m
->system_bus
))),
748 strnull(dbus_bus_get_unique_name(m
->system_bus
)));
754 int bus_init_api(Manager
*m
) {
761 dbus_error_init(&error
);
766 if (m
->name_data_slot
< 0)
767 if (!dbus_pending_call_allocate_data_slot(&m
->name_data_slot
))
770 if (m
->running_as
!= MANAGER_SESSION
&& m
->system_bus
)
771 m
->api_bus
= m
->system_bus
;
773 if (!(m
->api_bus
= dbus_bus_get_private(m
->running_as
== MANAGER_SESSION
? DBUS_BUS_SESSION
: DBUS_BUS_SYSTEM
, &error
))) {
774 log_debug("Failed to get API D-Bus connection, retrying later: %s", error
.message
);
775 dbus_error_free(&error
);
779 dbus_connection_set_dispatch_status_function(m
->api_bus
, api_bus_dispatch_status
, m
, NULL
);
780 m
->request_api_bus_dispatch
= true;
782 if ((r
= bus_setup_loop(m
, m
->api_bus
)) < 0) {
788 if (!dbus_connection_register_object_path(m
->api_bus
, "/org/freedesktop/systemd1", &bus_manager_vtable
, m
) ||
789 !dbus_connection_register_fallback(m
->api_bus
, "/org/freedesktop/systemd1/unit", &bus_unit_vtable
, m
) ||
790 !dbus_connection_register_fallback(m
->api_bus
, "/org/freedesktop/systemd1/job", &bus_job_vtable
, m
) ||
791 !dbus_connection_add_filter(m
->api_bus
, api_bus_message_filter
, m
, NULL
)) {
796 /* Get NameOwnerChange messages */
797 dbus_bus_add_match(m
->api_bus
,
799 "sender='"DBUS_SERVICE_DBUS
"',"
800 "interface='"DBUS_INTERFACE_DBUS
"',"
801 "path='"DBUS_PATH_DBUS
"'",
804 if (dbus_error_is_set(&error
)) {
805 log_error("Failed to register match: %s", error
.message
);
806 dbus_error_free(&error
);
811 /* Get activation requests */
812 dbus_bus_add_match(m
->api_bus
,
814 "sender='"DBUS_SERVICE_DBUS
"',"
815 "interface='org.freedesktop.systemd1.Activator',"
816 "path='"DBUS_PATH_DBUS
"'",
819 if (dbus_error_is_set(&error
)) {
820 log_error("Failed to register match: %s", error
.message
);
821 dbus_error_free(&error
);
826 if ((r
= request_name(m
)) < 0) {
831 if ((r
= query_name_list(m
)) < 0) {
836 log_debug("Successfully connected to API D-Bus bus %s as %s",
837 strnull((id
= dbus_connection_get_server_id(m
->api_bus
))),
838 strnull(dbus_bus_get_unique_name(m
->api_bus
)));
842 if (!(m
->subscribed
= set_new(string_hash_func
, string_compare_func
)))
848 void bus_done_api(Manager
*m
) {
852 if (m
->system_bus
== m
->api_bus
)
853 m
->system_bus
= NULL
;
855 dbus_connection_set_dispatch_status_function(m
->api_bus
, NULL
, NULL
, NULL
);
856 dbus_connection_flush(m
->api_bus
);
857 dbus_connection_close(m
->api_bus
);
858 dbus_connection_unref(m
->api_bus
);
865 while ((c
= set_steal_first(m
->subscribed
)))
868 set_free(m
->subscribed
);
869 m
->subscribed
= NULL
;
872 if (m
->name_data_slot
>= 0)
873 dbus_pending_call_free_data_slot(&m
->name_data_slot
);
875 if (m
->queued_message
) {
876 dbus_message_unref(m
->queued_message
);
877 m
->queued_message
= NULL
;
881 void bus_done_system(Manager
*m
) {
884 if (m
->system_bus
== m
->api_bus
)
888 dbus_connection_set_dispatch_status_function(m
->system_bus
, NULL
, NULL
, NULL
);
889 dbus_connection_flush(m
->system_bus
);
890 dbus_connection_close(m
->system_bus
);
891 dbus_connection_unref(m
->system_bus
);
892 m
->system_bus
= NULL
;
896 static void query_pid_pending_cb(DBusPendingCall
*pending
, void *userdata
) {
897 Manager
*m
= userdata
;
902 dbus_error_init(&error
);
904 assert_se(name
= dbus_pending_call_get_data(pending
, m
->name_data_slot
));
905 assert_se(reply
= dbus_pending_call_steal_reply(pending
));
907 switch (dbus_message_get_type(reply
)) {
909 case DBUS_MESSAGE_TYPE_ERROR
:
911 assert_se(dbus_set_error_from_message(&error
, reply
));
912 log_warning("GetConnectionUnixProcessID() failed: %s", error
.message
);
915 case DBUS_MESSAGE_TYPE_METHOD_RETURN
: {
918 if (!dbus_message_get_args(reply
,
920 DBUS_TYPE_UINT32
, &r
,
921 DBUS_TYPE_INVALID
)) {
922 log_error("Failed to parse GetConnectionUnixProcessID() reply: %s", error
.message
);
926 manager_dispatch_bus_query_pid_done(m
, name
, (pid_t
) r
);
931 assert_not_reached("Invalid reply message");
934 dbus_message_unref(reply
);
935 dbus_error_free(&error
);
938 int bus_query_pid(Manager
*m
, const char *name
) {
939 DBusMessage
*message
= NULL
;
940 DBusPendingCall
*pending
= NULL
;
946 if (!(message
= dbus_message_new_method_call(
950 "GetConnectionUnixProcessID")))
953 if (!(dbus_message_append_args(
955 DBUS_TYPE_STRING
, &name
,
959 if (!dbus_connection_send_with_reply(m
->api_bus
, message
, &pending
, -1))
962 if (!(n
= strdup(name
)))
965 if (!dbus_pending_call_set_data(pending
, m
->name_data_slot
, n
, free
))
970 if (!dbus_pending_call_set_notify(pending
, query_pid_pending_cb
, m
, NULL
))
973 dbus_message_unref(message
);
974 dbus_pending_call_unref(pending
);
982 dbus_pending_call_cancel(pending
);
983 dbus_pending_call_unref(pending
);
987 dbus_message_unref(message
);
992 DBusHandlerResult
bus_default_message_handler(Manager
*m
, DBusMessage
*message
, const char*introspection
, const BusProperty
*properties
) {
994 DBusMessage
*reply
= NULL
;
1000 dbus_error_init(&error
);
1002 if (dbus_message_is_method_call(message
, "org.freedesktop.DBus.Introspectable", "Introspect") && introspection
) {
1004 if (!(reply
= dbus_message_new_method_return(message
)))
1007 if (!dbus_message_append_args(reply
, DBUS_TYPE_STRING
, &introspection
, DBUS_TYPE_INVALID
))
1010 } else if (dbus_message_is_method_call(message
, "org.freedesktop.DBus.Properties", "Get") && properties
) {
1011 const char *interface
, *property
;
1012 const BusProperty
*p
;
1014 if (!dbus_message_get_args(
1017 DBUS_TYPE_STRING
, &interface
,
1018 DBUS_TYPE_STRING
, &property
,
1020 return bus_send_error_reply(m
, message
, &error
, -EINVAL
);
1022 for (p
= properties
; p
->property
; p
++)
1023 if (streq(p
->interface
, interface
) && streq(p
->property
, property
))
1027 DBusMessageIter iter
, sub
;
1029 if (!(reply
= dbus_message_new_method_return(message
)))
1032 dbus_message_iter_init_append(reply
, &iter
);
1034 if (!dbus_message_iter_open_container(&iter
, DBUS_TYPE_VARIANT
, p
->signature
, &sub
))
1037 if ((r
= p
->append(m
, &sub
, property
, (void*) p
->data
)) < 0) {
1042 dbus_message_unref(reply
);
1043 return bus_send_error_reply(m
, message
, NULL
, r
);
1046 if (!dbus_message_iter_close_container(&iter
, &sub
))
1049 } else if (dbus_message_is_method_call(message
, "org.freedesktop.DBus.Properties", "GetAll") && properties
) {
1050 const char *interface
;
1051 const BusProperty
*p
;
1052 DBusMessageIter iter
, sub
, sub2
, sub3
;
1055 if (!dbus_message_get_args(
1058 DBUS_TYPE_STRING
, &interface
,
1060 return bus_send_error_reply(m
, message
, &error
, -EINVAL
);
1062 if (!(reply
= dbus_message_new_method_return(message
)))
1065 dbus_message_iter_init_append(reply
, &iter
);
1067 if (!dbus_message_iter_open_container(&iter
, DBUS_TYPE_ARRAY
, "{sv}", &sub
))
1070 for (p
= properties
; p
->property
; p
++) {
1071 if (!streq(p
->interface
, interface
))
1074 if (!dbus_message_iter_open_container(&sub
, DBUS_TYPE_DICT_ENTRY
, NULL
, &sub2
) ||
1075 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &p
->property
) ||
1076 !dbus_message_iter_open_container(&sub2
, DBUS_TYPE_VARIANT
, p
->signature
, &sub3
))
1079 if ((r
= p
->append(m
, &sub3
, p
->property
, (void*) p
->data
)) < 0) {
1084 dbus_message_unref(reply
);
1085 return bus_send_error_reply(m
, message
, NULL
, r
);
1088 if (!dbus_message_iter_close_container(&sub2
, &sub3
) ||
1089 !dbus_message_iter_close_container(&sub
, &sub2
))
1095 if (!dbus_message_iter_close_container(&iter
, &sub
))
1100 if (!dbus_connection_send(m
->api_bus
, reply
, NULL
))
1103 dbus_message_unref(reply
);
1104 return DBUS_HANDLER_RESULT_HANDLED
;
1107 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
1111 dbus_message_unref(reply
);
1113 dbus_error_free(&error
);
1115 return DBUS_HANDLER_RESULT_NEED_MEMORY
;
1118 static const char *error_to_dbus(int error
) {
1123 return DBUS_ERROR_INVALID_ARGS
;
1126 return DBUS_ERROR_NO_MEMORY
;
1130 return DBUS_ERROR_ACCESS_DENIED
;
1133 return DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN
;
1136 return DBUS_ERROR_FILE_NOT_FOUND
;
1139 return DBUS_ERROR_FILE_EXISTS
;
1142 return DBUS_ERROR_TIMEOUT
;
1145 return DBUS_ERROR_IO_ERROR
;
1150 return DBUS_ERROR_DISCONNECTED
;
1153 return DBUS_ERROR_FAILED
;
1156 DBusHandlerResult
bus_send_error_reply(Manager
*m
, DBusMessage
*message
, DBusError
*bus_error
, int error
) {
1157 DBusMessage
*reply
= NULL
;
1158 const char *name
, *text
;
1160 if (bus_error
&& dbus_error_is_set(bus_error
)) {
1161 name
= bus_error
->name
;
1162 text
= bus_error
->message
;
1164 name
= error_to_dbus(error
);
1165 text
= strerror(-error
);
1168 if (!(reply
= dbus_message_new_error(message
, name
, text
)))
1171 if (!dbus_connection_send(m
->api_bus
, reply
, NULL
))
1174 dbus_message_unref(reply
);
1177 dbus_error_free(bus_error
);
1179 return DBUS_HANDLER_RESULT_HANDLED
;
1183 dbus_message_unref(reply
);
1186 dbus_error_free(bus_error
);
1188 return DBUS_HANDLER_RESULT_NEED_MEMORY
;
1191 int bus_property_append_string(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
1192 const char *t
= data
;
1201 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_STRING
, &t
))
1207 int bus_property_append_strv(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
1208 DBusMessageIter sub
;
1215 if (!dbus_message_iter_open_container(i
, DBUS_TYPE_ARRAY
, "s", &sub
))
1219 if (!dbus_message_iter_append_basic(&sub
, DBUS_TYPE_STRING
, t
))
1222 if (!dbus_message_iter_close_container(i
, &sub
))
1228 int bus_property_append_bool(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
1239 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_BOOLEAN
, &db
))
1245 int bus_property_append_uint64(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
1251 /* Let's ensure that pid_t is actually 64bit, and hence this
1252 * function can be used for usec_t */
1253 assert_cc(sizeof(uint64_t) == sizeof(usec_t
));
1255 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_UINT64
, data
))
1261 int bus_property_append_uint32(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
1267 /* Let's ensure that pid_t and mode_t is actually 32bit, and
1268 * hence this function can be used for pid_t/mode_t */
1269 assert_cc(sizeof(uint32_t) == sizeof(pid_t
));
1270 assert_cc(sizeof(uint32_t) == sizeof(mode_t
));
1271 assert_cc(sizeof(uint32_t) == sizeof(unsigned));
1273 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_UINT32
, data
))
1279 int bus_property_append_int32(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
1285 assert_cc(sizeof(int32_t) == sizeof(int));
1287 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_INT32
, data
))
1293 int bus_parse_strv(DBusMessage
*m
, char ***_l
) {
1294 DBusMessageIter iter
, sub
;
1295 unsigned n
= 0, i
= 0;
1301 if (!dbus_message_iter_init(m
, &iter
) ||
1302 dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_ARRAY
||
1303 dbus_message_iter_get_element_type(&iter
) != DBUS_TYPE_STRING
)
1306 dbus_message_iter_recurse(&iter
, &sub
);
1308 while (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
1310 dbus_message_iter_next(&sub
);
1313 if (!(l
= new(char*, n
+1)))
1316 assert_se(dbus_message_iter_init(m
, &iter
));
1317 dbus_message_iter_recurse(&iter
, &sub
);
1319 while (dbus_message_iter_get_arg_type(&sub
) != DBUS_TYPE_INVALID
) {
1322 assert_se(dbus_message_iter_get_arg_type(&sub
) == DBUS_TYPE_STRING
);
1323 dbus_message_iter_get_basic(&sub
, &s
);
1325 if (!(l
[i
++] = strdup(s
))) {
1330 dbus_message_iter_next(&sub
);