1 /* GDBus - GLib D-Bus Library
3 * Copyright (C) 2008-2010 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 * Author: David Zeuthen <davidz@redhat.com>
24 * - would be nice to expose GDBusAuthMechanism and an extension point
26 * - Need to rewrite GDBusAuth and rework GDBusAuthMechanism. In particular
27 * the mechanism VFuncs need to be able to set an error.
29 * - Need to document other mechanisms/sources for determining the D-Bus
30 * address of a well-known bus.
32 * - e.g. on Win32 we need code like from here
34 * http://cgit.freedesktop.org/~david/gdbus-standalone/tree/gdbus/gdbusaddress.c#n900
36 * that was never copied over here because it originally was copy-paste
37 * from the GPLv2 / AFL 2.1 libdbus sources.
39 * - on OS X we need to look in launchd for the address
41 * https://bugs.freedesktop.org/show_bug.cgi?id=14259
43 * - on X11 we need to look in a X11 property on the X server
44 * - (we can also just use dbus-launch(1) from the D-Bus
47 * - (ideally) this requires D-Bus spec work because none of
48 * this has never really been specced out properly (except
51 * - Related to the above, we also need to be able to launch a message bus
52 * instance.... Since we don't want to write our own bus daemon we should
53 * launch dbus-daemon(1) (thus: Win32 and OS X need to bundle it)
55 * - probably want a G_DBUS_NONCE_TCP_TMPDIR environment variable
56 * to specify where the nonce is stored. This will allow people to use
57 * G_DBUS_NONCE_TCP_TMPDIR=/mnt/secure.company.server/dbus-nonce-dir
58 * to easily achieve secure RPC via nonce-tcp.
60 * - need to expose an extension point for resolving D-Bus address and
61 * turning them into GIOStream objects. This will allow us to implement
62 * e.g. X11 D-Bus transports without dlopen()'ing or linking against
64 * - see g_dbus_address_connect() in gdbusaddress.c
66 * - would be cute to use kernel-specific APIs to resolve fds for
67 * debug output when using G_DBUS_DEBUG=message, e.g. in addition to
69 * fd 21: dev=8:1,mode=0100644,ino=1171231,uid=0,gid=0,rdev=0:0,size=234,atime=1273070640,mtime=1267126160,ctime=1267126160
71 * maybe we can show more information about what fd 21 really is.
72 * Ryan suggests looking in /proc/self/fd for clues / symlinks!
73 * Initial experiments on Linux 2.6 suggests that the symlink looks
78 * e.g. not of much use.
80 * - GDBus High-Level docs
81 * - Proxy: properties, signals...
82 * - Connection: IOStream based, ::close, connection setup steps
83 * mainloop integration, threading
84 * - Differences from libdbus (extend "Migrating from")
85 * - the message handling thread
86 * - Using GVariant instead of GValue
87 * - Explain why the high-level API is a good thing and what
88 * kind of pitfalls it avoids
89 * - Export objects before claiming names
90 * - Talk about auto-starting services (cf. GBusNameWatcherFlags)
92 * - use abstract sockets in test code
93 * - right now it doesn't work, dbus-daemon(1) fails with
95 * /gdbus/connection/filter: Failed to start message bus: Failed to bind
96 * socket "/tmp/g-dbus-tests-pid-28531": Address already in use
97 * ** WARNING **: Error reading address from dbus daemon, 0 bytes read
107 #include "gdbusauth.h"
108 #include "gdbusutils.h"
109 #include "gdbusaddress.h"
110 #include "gdbusmessage.h"
111 #include "gdbusconnection.h"
112 #include "gdbuserror.h"
113 #include "gioenumtypes.h"
114 #include "gdbusintrospection.h"
115 #include "gdbusmethodinvocation.h"
116 #include "gdbusprivate.h"
117 #include "gdbusauthobserver.h"
118 #include "ginitable.h"
119 #include "gasyncinitable.h"
120 #include "giostream.h"
121 #include "gasyncresult.h"
125 #include "gunixconnection.h"
126 #include "gunixfdmessage.h"
129 #include "glibintl.h"
132 * SECTION:gdbusconnection
133 * @short_description: D-Bus Connections
134 * @include: gio/gio.h
136 * The #GDBusConnection type is used for D-Bus connections to remote
137 * peers such as a message buses. It is a low-level API that offers a
138 * lot of flexibility. For instance, it lets you establish a connection
139 * over any transport that can by represented as an #GIOStream.
141 * This class is rarely used directly in D-Bus clients. If you are writing
142 * a D-Bus client, it is often easier to use the g_bus_own_name(),
143 * g_bus_watch_name() or g_dbus_proxy_new_for_bus() APIs.
145 * As an exception to the usual GLib rule that a particular object must not
146 * be used by two threads at the same time, #GDBusConnection's methods may be
147 * called from any thread. This is so that g_bus_get() and g_bus_get_sync()
148 * can safely return the same #GDBusConnection when called from any thread.
150 * Most of the ways to obtain a #GDBusConnection automatically initialize it
151 * (i.e. connect to D-Bus): for instance, g_dbus_connection_new() and
152 * g_bus_get(), and the synchronous versions of those methods, give you an
153 * initialized connection. Language bindings for GIO should use
154 * g_initable_new() or g_async_initable_new_async(), which also initialize the
157 * If you construct an uninitialized #GDBusConnection, such as via
158 * g_object_new(), you must initialize it via g_initable_init() or
159 * g_async_initable_init_async() before using its methods or properties.
160 * Calling methods or accessing properties on a #GDBusConnection that has not
161 * completed initialization successfully is considered to be invalid, and leads
162 * to undefined behaviour. In particular, if initialization fails with a
163 * #GError, the only valid thing you can do with that #GDBusConnection is to
164 * free it with g_object_unref().
166 * ## An example D-Bus server # {#gdbus-server}
168 * Here is an example for a D-Bus server:
169 * [gdbus-example-server.c](https://git.gnome.org/browse/glib/tree/gio/tests/gdbus-example-server.c)
171 * ## An example for exporting a subtree # {#gdbus-subtree-server}
173 * Here is an example for exporting a subtree:
174 * [gdbus-example-subtree.c](https://git.gnome.org/browse/glib/tree/gio/tests/gdbus-example-subtree.c)
176 * ## An example for file descriptor passing # {#gdbus-unix-fd-client}
178 * Here is an example for passing UNIX file descriptors:
179 * [gdbus-unix-fd-client.c](https://git.gnome.org/browse/glib/tree/gio/tests/gdbus-unix-fd-client.c)
181 * ## An example for exporting a GObject # {#gdbus-export}
183 * Here is an example for exporting a #GObject:
184 * [gdbus-example-export.c](https://git.gnome.org/browse/glib/tree/gio/tests/gdbus-example-export.c)
187 /* ---------------------------------------------------------------------------------------------------- */
189 typedef struct _GDBusConnectionClass GDBusConnectionClass
;
192 * GDBusConnectionClass:
193 * @closed: Signal class handler for the #GDBusConnection::closed signal.
195 * Class structure for #GDBusConnection.
199 struct _GDBusConnectionClass
202 GObjectClass parent_class
;
206 void (*closed
) (GDBusConnection
*connection
,
207 gboolean remote_peer_vanished
,
211 G_LOCK_DEFINE_STATIC (message_bus_lock
);
213 static GWeakRef the_session_bus
;
214 static GWeakRef the_system_bus
;
216 /* Extra pseudo-member of GDBusSendMessageFlags.
217 * Set by initable_init() to indicate that despite not being initialized yet,
218 * enough of the only-valid-after-init members are set that we can send a
219 * message, and we're being called from its thread, so no memory barrier is
220 * required before accessing them.
222 #define SEND_MESSAGE_FLAGS_INITIALIZING (1u << 31)
224 /* Same as SEND_MESSAGE_FLAGS_INITIALIZING, but in GDBusCallFlags */
225 #define CALL_FLAGS_INITIALIZING (1u << 31)
227 /* ---------------------------------------------------------------------------------------------------- */
231 GDestroyNotify callback
;
233 GMainContext
*context
;
234 } CallDestroyNotifyData
;
237 call_destroy_notify_data_in_idle (gpointer user_data
)
239 CallDestroyNotifyData
*data
= user_data
;
240 data
->callback (data
->user_data
);
245 call_destroy_notify_data_free (CallDestroyNotifyData
*data
)
247 if (data
->context
!= NULL
)
248 g_main_context_unref (data
->context
);
253 * call_destroy_notify: <internal>
254 * @context: (nullable): A #GMainContext or %NULL.
255 * @callback: (nullable): A #GDestroyNotify or %NULL.
256 * @user_data: Data to pass to @callback.
258 * Schedules @callback to run in @context.
261 call_destroy_notify (GMainContext
*context
,
262 GDestroyNotify callback
,
265 GSource
*idle_source
;
266 CallDestroyNotifyData
*data
;
268 if (callback
== NULL
)
271 data
= g_new0 (CallDestroyNotifyData
, 1);
272 data
->callback
= callback
;
273 data
->user_data
= user_data
;
274 data
->context
= context
;
275 if (data
->context
!= NULL
)
276 g_main_context_ref (data
->context
);
278 idle_source
= g_idle_source_new ();
279 g_source_set_priority (idle_source
, G_PRIORITY_DEFAULT
);
280 g_source_set_callback (idle_source
,
281 call_destroy_notify_data_in_idle
,
283 (GDestroyNotify
) call_destroy_notify_data_free
);
284 g_source_set_name (idle_source
, "[gio] call_destroy_notify_data_in_idle");
285 g_source_attach (idle_source
, data
->context
);
286 g_source_unref (idle_source
);
292 /* ---------------------------------------------------------------------------------------------------- */
295 _g_strv_has_string (const gchar
* const *haystack
,
300 for (n
= 0; haystack
!= NULL
&& haystack
[n
] != NULL
; n
++)
302 if (g_strcmp0 (haystack
[n
], needle
) == 0)
308 /* ---------------------------------------------------------------------------------------------------- */
311 #define CONNECTION_ENSURE_LOCK(obj) do { ; } while (FALSE)
313 // TODO: for some reason this doesn't work on Windows
314 #define CONNECTION_ENSURE_LOCK(obj) do { \
315 if (G_UNLIKELY (g_mutex_trylock(&(obj)->lock))) \
317 g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
318 "CONNECTION_ENSURE_LOCK: GDBusConnection object lock is not locked"); \
323 #define CONNECTION_LOCK(obj) do { \
324 g_mutex_lock (&(obj)->lock); \
327 #define CONNECTION_UNLOCK(obj) do { \
328 g_mutex_unlock (&(obj)->lock); \
331 /* Flags in connection->atomic_flags */
333 FLAG_INITIALIZED
= 1 << 0,
334 FLAG_EXIT_ON_CLOSE
= 1 << 1,
341 * The #GDBusConnection structure contains only private data and
342 * should only be accessed using the provided API.
346 struct _GDBusConnection
349 GObject parent_instance
;
351 /* ------------------------------------------------------------------------ */
352 /* -- General object state ------------------------------------------------ */
353 /* ------------------------------------------------------------------------ */
355 /* General-purpose lock for most fields */
358 /* A lock used in the init() method of the GInitable interface - see comments
359 * in initable_init() for why a separate lock is needed.
361 * If you need both @lock and @init_lock, you must take @init_lock first.
365 /* Set (by loading the contents of /var/lib/dbus/machine-id) the first time
366 * someone calls org.freedesktop.DBus.Peer.GetMachineId(). Protected by @lock.
370 /* The underlying stream used for communication
371 * Read-only after initable_init(), so it may be read if you either
372 * hold @init_lock or check for initialization first.
376 /* The object used for authentication (if any).
377 * Read-only after initable_init(), so it may be read if you either
378 * hold @init_lock or check for initialization first.
382 /* Last serial used. Protected by @lock. */
385 /* The object used to send/receive messages.
386 * Read-only after initable_init(), so it may be read if you either
387 * hold @init_lock or check for initialization first.
391 /* If connected to a message bus, this contains the unique name assigned to
392 * us by the bus (e.g. ":1.42").
393 * Read-only after initable_init(), so it may be read if you either
394 * hold @init_lock or check for initialization first.
396 gchar
*bus_unique_name
;
398 /* The GUID returned by the other side if we authenticed as a client or
399 * the GUID to use if authenticating as a server.
400 * Read-only after initable_init(), so it may be read if you either
401 * hold @init_lock or check for initialization first.
405 /* FLAG_INITIALIZED is set exactly when initable_init() has finished running.
406 * Inspect @initialization_error to see whether it succeeded or failed.
408 * FLAG_EXIT_ON_CLOSE is the exit-on-close property.
410 * FLAG_CLOSED is the closed property. It may be read at any time, but
411 * may only be written while holding @lock.
413 volatile gint atomic_flags
;
415 /* If the connection could not be established during initable_init(),
416 * this GError will be set.
417 * Read-only after initable_init(), so it may be read if you either
418 * hold @init_lock or check for initialization first.
420 GError
*initialization_error
;
422 /* The result of g_main_context_ref_thread_default() when the object
423 * was created (the GObject _init() function) - this is used for delivery
424 * of the :closed GObject signal.
426 * Only set in the GObject init function, so no locks are needed.
428 GMainContext
*main_context_at_construction
;
430 /* Read-only construct properties, no locks needed */
432 GDBusConnectionFlags flags
;
434 /* Map used for managing method replies, protected by @lock */
435 GHashTable
*map_method_serial_to_task
; /* guint32 -> GTask* */
437 /* Maps used for managing signal subscription, protected by @lock */
438 GHashTable
*map_rule_to_signal_data
; /* match rule (gchar*) -> SignalData */
439 GHashTable
*map_id_to_signal_data
; /* id (guint) -> SignalData */
440 GHashTable
*map_sender_unique_name_to_signal_data_array
; /* unique sender (gchar*) -> GPtrArray* of SignalData */
442 /* Maps used for managing exported objects and subtrees,
445 GHashTable
*map_object_path_to_eo
; /* gchar* -> ExportedObject* */
446 GHashTable
*map_id_to_ei
; /* guint -> ExportedInterface* */
447 GHashTable
*map_object_path_to_es
; /* gchar* -> ExportedSubtree* */
448 GHashTable
*map_id_to_es
; /* guint -> ExportedSubtree* */
450 /* Map used for storing last used serials for each thread, protected by @lock */
451 GHashTable
*map_thread_to_last_serial
;
453 /* Structure used for message filters, protected by @lock */
456 /* Capabilities negotiated during authentication
457 * Read-only after initable_init(), so it may be read without holding a
458 * lock, if you check for initialization first.
460 GDBusCapabilityFlags capabilities
;
462 /* Protected by @init_lock */
463 GDBusAuthObserver
*authentication_observer
;
465 /* Read-only after initable_init(), so it may be read if you either
466 * hold @init_lock or check for initialization first.
468 GCredentials
*credentials
;
470 /* set to TRUE when finalizing */
474 typedef struct ExportedObject ExportedObject
;
475 static void exported_object_free (ExportedObject
*eo
);
477 typedef struct ExportedSubtree ExportedSubtree
;
478 static void exported_subtree_free (ExportedSubtree
*es
);
496 PROP_CAPABILITY_FLAGS
,
497 PROP_AUTHENTICATION_OBSERVER
,
500 static void distribute_signals (GDBusConnection
*connection
,
501 GDBusMessage
*message
);
503 static void distribute_method_call (GDBusConnection
*connection
,
504 GDBusMessage
*message
);
506 static gboolean
handle_generic_unlocked (GDBusConnection
*connection
,
507 GDBusMessage
*message
);
510 static void purge_all_signal_subscriptions (GDBusConnection
*connection
);
511 static void purge_all_filters (GDBusConnection
*connection
);
513 static void schedule_method_call (GDBusConnection
*connection
,
514 GDBusMessage
*message
,
515 guint registration_id
,
516 guint subtree_registration_id
,
517 const GDBusInterfaceInfo
*interface_info
,
518 const GDBusMethodInfo
*method_info
,
519 const GDBusPropertyInfo
*property_info
,
520 GVariant
*parameters
,
521 const GDBusInterfaceVTable
*vtable
,
522 GMainContext
*main_context
,
525 #define _G_ENSURE_LOCK(name) do { \
526 if (G_UNLIKELY (G_TRYLOCK(name))) \
528 g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
529 "_G_ENSURE_LOCK: Lock '" #name "' is not locked"); \
533 static guint signals[LAST_SIGNAL] = { 0 };
535 static void initable_iface_init (GInitableIface
*initable_iface
);
536 static void async_initable_iface_init (GAsyncInitableIface
*async_initable_iface
);
538 G_DEFINE_TYPE_WITH_CODE (GDBusConnection
, g_dbus_connection
, G_TYPE_OBJECT
,
539 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE
, initable_iface_init
)
540 G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE
, async_initable_iface_init
)
544 * Check that all members of @connection that can only be accessed after
545 * the connection is initialized can safely be accessed. If not,
546 * log a critical warning. This function is a memory barrier.
548 * Returns: %TRUE if initialized
551 check_initialized (GDBusConnection
*connection
)
553 /* The access to @atomic_flags isn't conditional, so that this function
554 * provides a memory barrier for thread-safety even if checks are disabled.
555 * (If you don't want this stricter guarantee, you can call
556 * g_return_if_fail (check_initialized (c)).)
558 * This isn't strictly necessary now that we've decided use of an
559 * uninitialized GDBusConnection is undefined behaviour, but it seems
560 * better to be as deterministic as is feasible.
562 * (Anything that could suffer a crash from seeing undefined values
563 * must have a race condition - thread A initializes the connection while
564 * thread B calls a method without initialization, hoping that thread A will
565 * win the race - so its behaviour is undefined anyway.)
567 gint flags
= g_atomic_int_get (&connection
->atomic_flags
);
569 g_return_val_if_fail (flags
& FLAG_INITIALIZED
, FALSE
);
571 /* We can safely access this, due to the memory barrier above */
572 g_return_val_if_fail (connection
->initialization_error
== NULL
, FALSE
);
578 MAY_BE_UNINITIALIZED
= (1<<1)
579 } CheckUnclosedFlags
;
582 * Check the same thing as check_initialized(), and also that the
583 * connection is not closed. If the connection is uninitialized,
584 * raise a critical warning (it's programmer error); if it's closed,
585 * raise a recoverable GError (it's a runtime error).
587 * This function is a memory barrier.
589 * Returns: %TRUE if initialized and not closed
592 check_unclosed (GDBusConnection
*connection
,
593 CheckUnclosedFlags check
,
596 /* check_initialized() is effectively inlined, so we don't waste time
597 * doing two memory barriers
599 gint flags
= g_atomic_int_get (&connection
->atomic_flags
);
601 if (!(check
& MAY_BE_UNINITIALIZED
))
603 g_return_val_if_fail (flags
& FLAG_INITIALIZED
, FALSE
);
604 g_return_val_if_fail (connection
->initialization_error
== NULL
, FALSE
);
607 if (flags
& FLAG_CLOSED
)
609 g_set_error_literal (error
,
612 _("The connection is closed"));
619 static GHashTable
*alive_connections
= NULL
;
622 g_dbus_connection_dispose (GObject
*object
)
624 GDBusConnection
*connection
= G_DBUS_CONNECTION (object
);
626 G_LOCK (message_bus_lock
);
627 CONNECTION_LOCK (connection
);
628 if (connection
->worker
!= NULL
)
630 _g_dbus_worker_stop (connection
->worker
);
631 connection
->worker
= NULL
;
632 if (alive_connections
!= NULL
)
633 g_warn_if_fail (g_hash_table_remove (alive_connections
, connection
));
637 if (alive_connections
!= NULL
)
638 g_warn_if_fail (g_hash_table_lookup (alive_connections
, connection
) == NULL
);
640 CONNECTION_UNLOCK (connection
);
641 G_UNLOCK (message_bus_lock
);
643 if (G_OBJECT_CLASS (g_dbus_connection_parent_class
)->dispose
!= NULL
)
644 G_OBJECT_CLASS (g_dbus_connection_parent_class
)->dispose (object
);
648 g_dbus_connection_finalize (GObject
*object
)
650 GDBusConnection
*connection
= G_DBUS_CONNECTION (object
);
652 connection
->finalizing
= TRUE
;
654 purge_all_signal_subscriptions (connection
);
656 purge_all_filters (connection
);
657 g_ptr_array_unref (connection
->filters
);
659 if (connection
->authentication_observer
!= NULL
)
660 g_object_unref (connection
->authentication_observer
);
662 if (connection
->auth
!= NULL
)
663 g_object_unref (connection
->auth
);
665 if (connection
->credentials
)
666 g_object_unref (connection
->credentials
);
668 if (connection
->stream
!= NULL
)
670 g_object_unref (connection
->stream
);
671 connection
->stream
= NULL
;
674 g_free (connection
->address
);
676 g_free (connection
->guid
);
677 g_free (connection
->bus_unique_name
);
679 if (connection
->initialization_error
!= NULL
)
680 g_error_free (connection
->initialization_error
);
682 g_hash_table_unref (connection
->map_method_serial_to_task
);
684 g_hash_table_unref (connection
->map_rule_to_signal_data
);
685 g_hash_table_unref (connection
->map_id_to_signal_data
);
686 g_hash_table_unref (connection
->map_sender_unique_name_to_signal_data_array
);
688 g_hash_table_unref (connection
->map_id_to_ei
);
689 g_hash_table_unref (connection
->map_object_path_to_eo
);
690 g_hash_table_unref (connection
->map_id_to_es
);
691 g_hash_table_unref (connection
->map_object_path_to_es
);
693 g_hash_table_unref (connection
->map_thread_to_last_serial
);
695 g_main_context_unref (connection
->main_context_at_construction
);
697 g_free (connection
->machine_id
);
699 g_mutex_clear (&connection
->init_lock
);
700 g_mutex_clear (&connection
->lock
);
702 G_OBJECT_CLASS (g_dbus_connection_parent_class
)->finalize (object
);
705 /* called in any user thread, with the connection's lock not held */
707 g_dbus_connection_get_property (GObject
*object
,
712 GDBusConnection
*connection
= G_DBUS_CONNECTION (object
);
717 g_value_set_object (value
, g_dbus_connection_get_stream (connection
));
721 g_value_set_string (value
, g_dbus_connection_get_guid (connection
));
724 case PROP_UNIQUE_NAME
:
725 g_value_set_string (value
, g_dbus_connection_get_unique_name (connection
));
729 g_value_set_boolean (value
, g_dbus_connection_is_closed (connection
));
732 case PROP_EXIT_ON_CLOSE
:
733 g_value_set_boolean (value
, g_dbus_connection_get_exit_on_close (connection
));
736 case PROP_CAPABILITY_FLAGS
:
737 g_value_set_flags (value
, g_dbus_connection_get_capabilities (connection
));
741 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
746 /* called in any user thread, with the connection's lock not held */
748 g_dbus_connection_set_property (GObject
*object
,
753 GDBusConnection
*connection
= G_DBUS_CONNECTION (object
);
758 connection
->stream
= g_value_dup_object (value
);
762 connection
->guid
= g_value_dup_string (value
);
766 connection
->address
= g_value_dup_string (value
);
770 connection
->flags
= g_value_get_flags (value
);
773 case PROP_EXIT_ON_CLOSE
:
774 g_dbus_connection_set_exit_on_close (connection
, g_value_get_boolean (value
));
777 case PROP_AUTHENTICATION_OBSERVER
:
778 connection
->authentication_observer
= g_value_dup_object (value
);
782 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
787 /* Base-class implementation of GDBusConnection::closed.
789 * Called in a user thread, by the main context that was thread-default when
790 * the object was constructed.
793 g_dbus_connection_real_closed (GDBusConnection
*connection
,
794 gboolean remote_peer_vanished
,
797 gint flags
= g_atomic_int_get (&connection
->atomic_flags
);
799 /* Because atomic int access is a memory barrier, we can safely read
800 * initialization_error without a lock, as long as we do it afterwards.
802 if (remote_peer_vanished
&&
803 (flags
& FLAG_EXIT_ON_CLOSE
) != 0 &&
804 (flags
& FLAG_INITIALIZED
) != 0 &&
805 connection
->initialization_error
== NULL
)
812 g_dbus_connection_class_init (GDBusConnectionClass
*klass
)
814 GObjectClass
*gobject_class
;
816 gobject_class
= G_OBJECT_CLASS (klass
);
818 gobject_class
->finalize
= g_dbus_connection_finalize
;
819 gobject_class
->dispose
= g_dbus_connection_dispose
;
820 gobject_class
->set_property
= g_dbus_connection_set_property
;
821 gobject_class
->get_property
= g_dbus_connection_get_property
;
823 klass
->closed
= g_dbus_connection_real_closed
;
826 * GDBusConnection:stream:
828 * The underlying #GIOStream used for I/O.
830 * If this is passed on construction and is a #GSocketConnection,
831 * then the corresponding #GSocket will be put into non-blocking mode.
833 * While the #GDBusConnection is active, it will interact with this
834 * stream from a worker thread, so it is not safe to interact with
835 * the stream directly.
839 g_object_class_install_property (gobject_class
,
841 g_param_spec_object ("stream",
843 P_("The underlying streams used for I/O"),
847 G_PARAM_CONSTRUCT_ONLY
|
848 G_PARAM_STATIC_NAME
|
849 G_PARAM_STATIC_BLURB
|
850 G_PARAM_STATIC_NICK
));
853 * GDBusConnection:address:
855 * A D-Bus address specifying potential endpoints that can be used
856 * when establishing the connection.
860 g_object_class_install_property (gobject_class
,
862 g_param_spec_string ("address",
864 P_("D-Bus address specifying potential socket endpoints"),
867 G_PARAM_CONSTRUCT_ONLY
|
868 G_PARAM_STATIC_NAME
|
869 G_PARAM_STATIC_BLURB
|
870 G_PARAM_STATIC_NICK
));
873 * GDBusConnection:flags:
875 * Flags from the #GDBusConnectionFlags enumeration.
879 g_object_class_install_property (gobject_class
,
881 g_param_spec_flags ("flags",
884 G_TYPE_DBUS_CONNECTION_FLAGS
,
885 G_DBUS_CONNECTION_FLAGS_NONE
,
887 G_PARAM_CONSTRUCT_ONLY
|
888 G_PARAM_STATIC_NAME
|
889 G_PARAM_STATIC_BLURB
|
890 G_PARAM_STATIC_NICK
));
893 * GDBusConnection:guid:
895 * The GUID of the peer performing the role of server when
898 * If you are constructing a #GDBusConnection and pass
899 * %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER in the
900 * #GDBusConnection:flags property then you MUST also set this
901 * property to a valid guid.
903 * If you are constructing a #GDBusConnection and pass
904 * %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT in the
905 * #GDBusConnection:flags property you will be able to read the GUID
906 * of the other peer here after the connection has been successfully
911 g_object_class_install_property (gobject_class
,
913 g_param_spec_string ("guid",
915 P_("GUID of the server peer"),
919 G_PARAM_CONSTRUCT_ONLY
|
920 G_PARAM_STATIC_NAME
|
921 G_PARAM_STATIC_BLURB
|
922 G_PARAM_STATIC_NICK
));
925 * GDBusConnection:unique-name:
927 * The unique name as assigned by the message bus or %NULL if the
928 * connection is not open or not a message bus connection.
932 g_object_class_install_property (gobject_class
,
934 g_param_spec_string ("unique-name",
936 P_("Unique name of bus connection"),
939 G_PARAM_STATIC_NAME
|
940 G_PARAM_STATIC_BLURB
|
941 G_PARAM_STATIC_NICK
));
944 * GDBusConnection:closed:
946 * A boolean specifying whether the connection has been closed.
950 g_object_class_install_property (gobject_class
,
952 g_param_spec_boolean ("closed",
954 P_("Whether the connection is closed"),
957 G_PARAM_STATIC_NAME
|
958 G_PARAM_STATIC_BLURB
|
959 G_PARAM_STATIC_NICK
));
962 * GDBusConnection:exit-on-close:
964 * A boolean specifying whether the process will be terminated (by
965 * calling `raise(SIGTERM)`) if the connection is closed by the
968 * Note that #GDBusConnection objects returned by g_bus_get_finish()
969 * and g_bus_get_sync() will (usually) have this property set to %TRUE.
973 g_object_class_install_property (gobject_class
,
975 g_param_spec_boolean ("exit-on-close",
977 P_("Whether the process is terminated when the connection is closed"),
981 G_PARAM_STATIC_NAME
|
982 G_PARAM_STATIC_BLURB
|
983 G_PARAM_STATIC_NICK
));
986 * GDBusConnection:capabilities:
988 * Flags from the #GDBusCapabilityFlags enumeration
989 * representing connection features negotiated with the other peer.
993 g_object_class_install_property (gobject_class
,
994 PROP_CAPABILITY_FLAGS
,
995 g_param_spec_flags ("capabilities",
998 G_TYPE_DBUS_CAPABILITY_FLAGS
,
999 G_DBUS_CAPABILITY_FLAGS_NONE
,
1001 G_PARAM_STATIC_NAME
|
1002 G_PARAM_STATIC_BLURB
|
1003 G_PARAM_STATIC_NICK
));
1006 * GDBusConnection:authentication-observer:
1008 * A #GDBusAuthObserver object to assist in the authentication process or %NULL.
1012 g_object_class_install_property (gobject_class
,
1013 PROP_AUTHENTICATION_OBSERVER
,
1014 g_param_spec_object ("authentication-observer",
1015 P_("Authentication Observer"),
1016 P_("Object used to assist in the authentication process"),
1017 G_TYPE_DBUS_AUTH_OBSERVER
,
1019 G_PARAM_CONSTRUCT_ONLY
|
1020 G_PARAM_STATIC_NAME
|
1021 G_PARAM_STATIC_BLURB
|
1022 G_PARAM_STATIC_NICK
));
1025 * GDBusConnection::closed:
1026 * @connection: the #GDBusConnection emitting the signal
1027 * @remote_peer_vanished: %TRUE if @connection is closed because the
1028 * remote peer closed its end of the connection
1029 * @error: (nullable): a #GError with more details about the event or %NULL
1031 * Emitted when the connection is closed.
1033 * The cause of this event can be
1035 * - If g_dbus_connection_close() is called. In this case
1036 * @remote_peer_vanished is set to %FALSE and @error is %NULL.
1038 * - If the remote peer closes the connection. In this case
1039 * @remote_peer_vanished is set to %TRUE and @error is set.
1041 * - If the remote peer sends invalid or malformed data. In this
1042 * case @remote_peer_vanished is set to %FALSE and @error is set.
1044 * Upon receiving this signal, you should give up your reference to
1045 * @connection. You are guaranteed that this signal is emitted only
1050 signals
[CLOSED_SIGNAL
] = g_signal_new (I_("closed"),
1051 G_TYPE_DBUS_CONNECTION
,
1053 G_STRUCT_OFFSET (GDBusConnectionClass
, closed
),
1064 g_dbus_connection_init (GDBusConnection
*connection
)
1066 g_mutex_init (&connection
->lock
);
1067 g_mutex_init (&connection
->init_lock
);
1069 connection
->map_method_serial_to_task
= g_hash_table_new (g_direct_hash
, g_direct_equal
);
1071 connection
->map_rule_to_signal_data
= g_hash_table_new (g_str_hash
,
1073 connection
->map_id_to_signal_data
= g_hash_table_new (g_direct_hash
,
1075 connection
->map_sender_unique_name_to_signal_data_array
= g_hash_table_new_full (g_str_hash
,
1078 (GDestroyNotify
) g_ptr_array_unref
);
1080 connection
->map_object_path_to_eo
= g_hash_table_new_full (g_str_hash
,
1083 (GDestroyNotify
) exported_object_free
);
1085 connection
->map_id_to_ei
= g_hash_table_new (g_direct_hash
,
1088 connection
->map_object_path_to_es
= g_hash_table_new_full (g_str_hash
,
1091 (GDestroyNotify
) exported_subtree_free
);
1093 connection
->map_id_to_es
= g_hash_table_new (g_direct_hash
,
1096 connection
->map_thread_to_last_serial
= g_hash_table_new (g_direct_hash
,
1099 connection
->main_context_at_construction
= g_main_context_ref_thread_default ();
1101 connection
->filters
= g_ptr_array_new ();
1105 * g_dbus_connection_get_stream:
1106 * @connection: a #GDBusConnection
1108 * Gets the underlying stream used for IO.
1110 * While the #GDBusConnection is active, it will interact with this
1111 * stream from a worker thread, so it is not safe to interact with
1112 * the stream directly.
1114 * Returns: (transfer none): the stream used for IO
1119 g_dbus_connection_get_stream (GDBusConnection
*connection
)
1121 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), NULL
);
1123 /* do not use g_return_val_if_fail(), we want the memory barrier */
1124 if (!check_initialized (connection
))
1127 return connection
->stream
;
1131 * g_dbus_connection_start_message_processing:
1132 * @connection: a #GDBusConnection
1134 * If @connection was created with
1135 * %G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING, this method
1136 * starts processing messages. Does nothing on if @connection wasn't
1137 * created with this flag or if the method has already been called.
1142 g_dbus_connection_start_message_processing (GDBusConnection
*connection
)
1144 g_return_if_fail (G_IS_DBUS_CONNECTION (connection
));
1146 /* do not use g_return_val_if_fail(), we want the memory barrier */
1147 if (!check_initialized (connection
))
1150 g_assert (connection
->worker
!= NULL
);
1151 _g_dbus_worker_unfreeze (connection
->worker
);
1155 * g_dbus_connection_is_closed:
1156 * @connection: a #GDBusConnection
1158 * Gets whether @connection is closed.
1160 * Returns: %TRUE if the connection is closed, %FALSE otherwise
1165 g_dbus_connection_is_closed (GDBusConnection
*connection
)
1169 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), FALSE
);
1171 flags
= g_atomic_int_get (&connection
->atomic_flags
);
1173 return (flags
& FLAG_CLOSED
) ? TRUE
: FALSE
;
1177 * g_dbus_connection_get_capabilities:
1178 * @connection: a #GDBusConnection
1180 * Gets the capabilities negotiated with the remote peer
1182 * Returns: zero or more flags from the #GDBusCapabilityFlags enumeration
1186 GDBusCapabilityFlags
1187 g_dbus_connection_get_capabilities (GDBusConnection
*connection
)
1189 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), G_DBUS_CAPABILITY_FLAGS_NONE
);
1191 /* do not use g_return_val_if_fail(), we want the memory barrier */
1192 if (!check_initialized (connection
))
1193 return G_DBUS_CAPABILITY_FLAGS_NONE
;
1195 return connection
->capabilities
;
1198 /* ---------------------------------------------------------------------------------------------------- */
1200 /* Called in a temporary thread without holding locks. */
1202 flush_in_thread_func (GTask
*task
,
1203 gpointer source_object
,
1205 GCancellable
*cancellable
)
1207 GError
*error
= NULL
;
1209 if (g_dbus_connection_flush_sync (source_object
,
1212 g_task_return_boolean (task
, TRUE
);
1214 g_task_return_error (task
, error
);
1218 * g_dbus_connection_flush:
1219 * @connection: a #GDBusConnection
1220 * @cancellable: (nullable): a #GCancellable or %NULL
1221 * @callback: (nullable): a #GAsyncReadyCallback to call when the
1222 * request is satisfied or %NULL if you don't care about the result
1223 * @user_data: The data to pass to @callback
1225 * Asynchronously flushes @connection, that is, writes all queued
1226 * outgoing message to the transport and then flushes the transport
1227 * (using g_output_stream_flush_async()). This is useful in programs
1228 * that wants to emit a D-Bus signal and then exit immediately. Without
1229 * flushing the connection, there is no guaranteed that the message has
1230 * been sent to the networking buffers in the OS kernel.
1232 * This is an asynchronous method. When the operation is finished,
1233 * @callback will be invoked in the
1234 * [thread-default main context][g-main-context-push-thread-default]
1235 * of the thread you are calling this method from. You can
1236 * then call g_dbus_connection_flush_finish() to get the result of the
1237 * operation. See g_dbus_connection_flush_sync() for the synchronous
1243 g_dbus_connection_flush (GDBusConnection
*connection
,
1244 GCancellable
*cancellable
,
1245 GAsyncReadyCallback callback
,
1250 g_return_if_fail (G_IS_DBUS_CONNECTION (connection
));
1252 task
= g_task_new (connection
, cancellable
, callback
, user_data
);
1253 g_task_set_source_tag (task
, g_dbus_connection_flush
);
1254 g_task_run_in_thread (task
, flush_in_thread_func
);
1255 g_object_unref (task
);
1259 * g_dbus_connection_flush_finish:
1260 * @connection: a #GDBusConnection
1261 * @res: a #GAsyncResult obtained from the #GAsyncReadyCallback passed
1262 * to g_dbus_connection_flush()
1263 * @error: return location for error or %NULL
1265 * Finishes an operation started with g_dbus_connection_flush().
1267 * Returns: %TRUE if the operation succeeded, %FALSE if @error is set
1272 g_dbus_connection_flush_finish (GDBusConnection
*connection
,
1276 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), FALSE
);
1277 g_return_val_if_fail (g_task_is_valid (res
, connection
), FALSE
);
1278 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, FALSE
);
1280 return g_task_propagate_boolean (G_TASK (res
), error
);
1284 * g_dbus_connection_flush_sync:
1285 * @connection: a #GDBusConnection
1286 * @cancellable: (nullable): a #GCancellable or %NULL
1287 * @error: return location for error or %NULL
1289 * Synchronously flushes @connection. The calling thread is blocked
1290 * until this is done. See g_dbus_connection_flush() for the
1291 * asynchronous version of this method and more details about what it
1294 * Returns: %TRUE if the operation succeeded, %FALSE if @error is set
1299 g_dbus_connection_flush_sync (GDBusConnection
*connection
,
1300 GCancellable
*cancellable
,
1305 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), FALSE
);
1306 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, FALSE
);
1310 /* This is only a best-effort attempt to see whether the connection is
1311 * closed, so it doesn't need the lock. If the connection closes just
1312 * after this check, but before scheduling the flush operation, the
1313 * result will be more or less the same as if the connection closed while
1314 * the flush operation was pending - it'll fail with either CLOSED or
1317 if (!check_unclosed (connection
, 0, error
))
1320 g_assert (connection
->worker
!= NULL
);
1322 ret
= _g_dbus_worker_flush_sync (connection
->worker
,
1330 /* ---------------------------------------------------------------------------------------------------- */
1334 GDBusConnection
*connection
;
1336 gboolean remote_peer_vanished
;
1340 emit_closed_data_free (EmitClosedData
*data
)
1342 g_object_unref (data
->connection
);
1343 if (data
->error
!= NULL
)
1344 g_error_free (data
->error
);
1348 /* Called in a user thread that has acquired the main context that was
1349 * thread-default when the object was constructed
1352 emit_closed_in_idle (gpointer user_data
)
1354 EmitClosedData
*data
= user_data
;
1357 g_object_notify (G_OBJECT (data
->connection
), "closed");
1358 g_signal_emit (data
->connection
,
1359 signals
[CLOSED_SIGNAL
],
1361 data
->remote_peer_vanished
,
1367 /* Can be called from any thread, must hold lock.
1368 * FLAG_CLOSED must already have been set.
1371 schedule_closed_unlocked (GDBusConnection
*connection
,
1372 gboolean remote_peer_vanished
,
1375 GSource
*idle_source
;
1376 EmitClosedData
*data
;
1378 CONNECTION_ENSURE_LOCK (connection
);
1380 data
= g_new0 (EmitClosedData
, 1);
1381 data
->connection
= g_object_ref (connection
);
1382 data
->remote_peer_vanished
= remote_peer_vanished
;
1383 data
->error
= error
!= NULL
? g_error_copy (error
) : NULL
;
1385 idle_source
= g_idle_source_new ();
1386 g_source_set_priority (idle_source
, G_PRIORITY_DEFAULT
);
1387 g_source_set_callback (idle_source
,
1388 emit_closed_in_idle
,
1390 (GDestroyNotify
) emit_closed_data_free
);
1391 g_source_set_name (idle_source
, "[gio] emit_closed_in_idle");
1392 g_source_attach (idle_source
, connection
->main_context_at_construction
);
1393 g_source_unref (idle_source
);
1396 /* ---------------------------------------------------------------------------------------------------- */
1399 * g_dbus_connection_close:
1400 * @connection: a #GDBusConnection
1401 * @cancellable: (nullable): a #GCancellable or %NULL
1402 * @callback: (nullable): a #GAsyncReadyCallback to call when the request is
1403 * satisfied or %NULL if you don't care about the result
1404 * @user_data: The data to pass to @callback
1406 * Closes @connection. Note that this never causes the process to
1407 * exit (this might only happen if the other end of a shared message
1408 * bus connection disconnects, see #GDBusConnection:exit-on-close).
1410 * Once the connection is closed, operations such as sending a message
1411 * will return with the error %G_IO_ERROR_CLOSED. Closing a connection
1412 * will not automatically flush the connection so queued messages may
1413 * be lost. Use g_dbus_connection_flush() if you need such guarantees.
1415 * If @connection is already closed, this method fails with
1416 * %G_IO_ERROR_CLOSED.
1418 * When @connection has been closed, the #GDBusConnection::closed
1419 * signal is emitted in the
1420 * [thread-default main context][g-main-context-push-thread-default]
1421 * of the thread that @connection was constructed in.
1423 * This is an asynchronous method. When the operation is finished,
1424 * @callback will be invoked in the
1425 * [thread-default main context][g-main-context-push-thread-default]
1426 * of the thread you are calling this method from. You can
1427 * then call g_dbus_connection_close_finish() to get the result of the
1428 * operation. See g_dbus_connection_close_sync() for the synchronous
1434 g_dbus_connection_close (GDBusConnection
*connection
,
1435 GCancellable
*cancellable
,
1436 GAsyncReadyCallback callback
,
1441 g_return_if_fail (G_IS_DBUS_CONNECTION (connection
));
1443 /* do not use g_return_val_if_fail(), we want the memory barrier */
1444 if (!check_initialized (connection
))
1447 g_assert (connection
->worker
!= NULL
);
1449 task
= g_task_new (connection
, cancellable
, callback
, user_data
);
1450 g_task_set_source_tag (task
, g_dbus_connection_close
);
1451 _g_dbus_worker_close (connection
->worker
, task
);
1452 g_object_unref (task
);
1456 * g_dbus_connection_close_finish:
1457 * @connection: a #GDBusConnection
1458 * @res: a #GAsyncResult obtained from the #GAsyncReadyCallback passed
1459 * to g_dbus_connection_close()
1460 * @error: return location for error or %NULL
1462 * Finishes an operation started with g_dbus_connection_close().
1464 * Returns: %TRUE if the operation succeeded, %FALSE if @error is set
1469 g_dbus_connection_close_finish (GDBusConnection
*connection
,
1473 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), FALSE
);
1474 g_return_val_if_fail (g_task_is_valid (res
, connection
), FALSE
);
1475 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, FALSE
);
1477 return g_task_propagate_boolean (G_TASK (res
), error
);
1482 GAsyncResult
*result
;
1485 /* Can be called by any thread, without the connection lock */
1487 sync_close_cb (GObject
*source_object
,
1491 SyncCloseData
*data
= user_data
;
1493 data
->result
= g_object_ref (res
);
1494 g_main_loop_quit (data
->loop
);
1498 * g_dbus_connection_close_sync:
1499 * @connection: a #GDBusConnection
1500 * @cancellable: (nullable): a #GCancellable or %NULL
1501 * @error: return location for error or %NULL
1503 * Synchronously closees @connection. The calling thread is blocked
1504 * until this is done. See g_dbus_connection_close() for the
1505 * asynchronous version of this method and more details about what it
1508 * Returns: %TRUE if the operation succeeded, %FALSE if @error is set
1513 g_dbus_connection_close_sync (GDBusConnection
*connection
,
1514 GCancellable
*cancellable
,
1519 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), FALSE
);
1520 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, FALSE
);
1524 if (check_unclosed (connection
, 0, error
))
1526 GMainContext
*context
;
1529 context
= g_main_context_new ();
1530 g_main_context_push_thread_default (context
);
1531 data
.loop
= g_main_loop_new (context
, TRUE
);
1534 g_dbus_connection_close (connection
, cancellable
, sync_close_cb
, &data
);
1535 g_main_loop_run (data
.loop
);
1536 ret
= g_dbus_connection_close_finish (connection
, data
.result
, error
);
1538 g_object_unref (data
.result
);
1539 g_main_loop_unref (data
.loop
);
1540 g_main_context_pop_thread_default (context
);
1541 g_main_context_unref (context
);
1547 /* ---------------------------------------------------------------------------------------------------- */
1550 * g_dbus_connection_get_last_serial:
1551 * @connection: a #GDBusConnection
1553 * Retrieves the last serial number assigned to a #GDBusMessage on
1554 * the current thread. This includes messages sent via both low-level
1555 * API such as g_dbus_connection_send_message() as well as
1556 * high-level API such as g_dbus_connection_emit_signal(),
1557 * g_dbus_connection_call() or g_dbus_proxy_call().
1559 * Returns: the last used serial or zero when no message has been sent
1560 * within the current thread
1565 g_dbus_connection_get_last_serial (GDBusConnection
*connection
)
1569 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), 0);
1571 CONNECTION_LOCK (connection
);
1572 ret
= GPOINTER_TO_UINT (g_hash_table_lookup (connection
->map_thread_to_last_serial
,
1574 CONNECTION_UNLOCK (connection
);
1579 /* ---------------------------------------------------------------------------------------------------- */
1581 /* Can be called by any thread, with the connection lock held */
1583 g_dbus_connection_send_message_unlocked (GDBusConnection
*connection
,
1584 GDBusMessage
*message
,
1585 GDBusSendMessageFlags flags
,
1586 volatile guint32
*out_serial
,
1591 guint32 serial_to_use
;
1594 CONNECTION_ENSURE_LOCK (connection
);
1596 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), FALSE
);
1597 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), FALSE
);
1599 /* TODO: check all necessary headers are present */
1604 if (out_serial
!= NULL
)
1607 /* If we're in initable_init(), don't check for being initialized, to avoid
1608 * chicken-and-egg problems. initable_init() is responsible for setting up
1609 * our prerequisites (mainly connection->worker), and only calling us
1610 * from its own thread (so no memory barrier is needed).
1612 if (!check_unclosed (connection
,
1613 (flags
& SEND_MESSAGE_FLAGS_INITIALIZING
) ? MAY_BE_UNINITIALIZED
: 0,
1617 blob
= g_dbus_message_to_blob (message
,
1619 connection
->capabilities
,
1624 if (flags
& G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL
)
1625 serial_to_use
= g_dbus_message_get_serial (message
);
1627 serial_to_use
= ++connection
->last_serial
; /* TODO: handle overflow */
1632 ((guint32
*) blob
)[2] = GUINT32_TO_LE (serial_to_use
);
1635 ((guint32
*) blob
)[2] = GUINT32_TO_BE (serial_to_use
);
1638 g_assert_not_reached ();
1643 g_printerr ("Writing message of %" G_GSIZE_FORMAT
" bytes (serial %d) on %p:\n",
1644 blob_size
, serial_to_use
, connection
);
1645 g_printerr ("----\n");
1646 hexdump (blob
, blob_size
);
1647 g_printerr ("----\n");
1650 /* TODO: use connection->auth to encode the blob */
1652 if (out_serial
!= NULL
)
1653 *out_serial
= serial_to_use
;
1655 /* store used serial for the current thread */
1656 /* TODO: watch the thread disposal and remove associated record
1658 * - see https://bugzilla.gnome.org/show_bug.cgi?id=676825#c7
1660 g_hash_table_replace (connection
->map_thread_to_last_serial
,
1662 GUINT_TO_POINTER (serial_to_use
));
1664 if (!(flags
& G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL
))
1665 g_dbus_message_set_serial (message
, serial_to_use
);
1667 g_dbus_message_lock (message
);
1668 _g_dbus_worker_send_message (connection
->worker
,
1672 blob
= NULL
; /* since _g_dbus_worker_send_message() steals the blob */
1683 * g_dbus_connection_send_message:
1684 * @connection: a #GDBusConnection
1685 * @message: a #GDBusMessage
1686 * @flags: flags affecting how the message is sent
1687 * @out_serial: (out) (optional): return location for serial number assigned
1688 * to @message when sending it or %NULL
1689 * @error: Return location for error or %NULL
1691 * Asynchronously sends @message to the peer represented by @connection.
1693 * Unless @flags contain the
1694 * %G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL flag, the serial number
1695 * will be assigned by @connection and set on @message via
1696 * g_dbus_message_set_serial(). If @out_serial is not %NULL, then the
1697 * serial number used will be written to this location prior to
1698 * submitting the message to the underlying transport.
1700 * If @connection is closed then the operation will fail with
1701 * %G_IO_ERROR_CLOSED. If @message is not well-formed,
1702 * the operation fails with %G_IO_ERROR_INVALID_ARGUMENT.
1704 * See this [server][gdbus-server] and [client][gdbus-unix-fd-client]
1705 * for an example of how to use this low-level API to send and receive
1706 * UNIX file descriptors.
1708 * Note that @message must be unlocked, unless @flags contain the
1709 * %G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL flag.
1711 * Returns: %TRUE if the message was well-formed and queued for
1712 * transmission, %FALSE if @error is set
1717 g_dbus_connection_send_message (GDBusConnection
*connection
,
1718 GDBusMessage
*message
,
1719 GDBusSendMessageFlags flags
,
1720 volatile guint32
*out_serial
,
1725 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), FALSE
);
1726 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), FALSE
);
1727 g_return_val_if_fail ((flags
& G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL
) || !g_dbus_message_get_locked (message
), FALSE
);
1728 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, FALSE
);
1730 CONNECTION_LOCK (connection
);
1731 ret
= g_dbus_connection_send_message_unlocked (connection
, message
, flags
, out_serial
, error
);
1732 CONNECTION_UNLOCK (connection
);
1736 /* ---------------------------------------------------------------------------------------------------- */
1742 gulong cancellable_handler_id
;
1744 GSource
*timeout_source
;
1749 /* Can be called from any thread with or without lock held */
1751 send_message_data_free (SendMessageData
*data
)
1753 g_assert (data
->timeout_source
== NULL
);
1754 g_assert (data
->cancellable_handler_id
== 0);
1756 g_slice_free (SendMessageData
, data
);
1759 /* ---------------------------------------------------------------------------------------------------- */
1761 /* can be called from any thread with lock held */
1763 send_message_with_reply_cleanup (GTask
*task
, gboolean remove
)
1765 GDBusConnection
*connection
= g_task_get_source_object (task
);
1766 SendMessageData
*data
= g_task_get_task_data (task
);
1768 CONNECTION_ENSURE_LOCK (connection
);
1770 g_assert (!data
->delivered
);
1772 data
->delivered
= TRUE
;
1774 if (data
->timeout_source
!= NULL
)
1776 g_source_destroy (data
->timeout_source
);
1777 data
->timeout_source
= NULL
;
1779 if (data
->cancellable_handler_id
> 0)
1781 g_cancellable_disconnect (g_task_get_cancellable (task
), data
->cancellable_handler_id
);
1782 data
->cancellable_handler_id
= 0;
1787 gboolean removed
= g_hash_table_remove (connection
->map_method_serial_to_task
,
1788 GUINT_TO_POINTER (data
->serial
));
1789 g_warn_if_fail (removed
);
1792 g_object_unref (task
);
1795 /* ---------------------------------------------------------------------------------------------------- */
1797 /* Called from GDBus worker thread with lock held */
1799 send_message_data_deliver_reply_unlocked (GTask
*task
,
1800 GDBusMessage
*reply
)
1802 SendMessageData
*data
= g_task_get_task_data (task
);
1804 if (data
->delivered
)
1807 g_task_return_pointer (task
, g_object_ref (reply
), g_object_unref
);
1809 send_message_with_reply_cleanup (task
, TRUE
);
1815 /* Called from a user thread, lock is not held */
1817 send_message_data_deliver_error (GTask
*task
,
1820 const char *message
)
1822 GDBusConnection
*connection
= g_task_get_source_object (task
);
1823 SendMessageData
*data
= g_task_get_task_data (task
);
1825 CONNECTION_LOCK (connection
);
1826 if (data
->delivered
)
1828 CONNECTION_UNLOCK (connection
);
1832 g_object_ref (task
);
1833 send_message_with_reply_cleanup (task
, TRUE
);
1834 CONNECTION_UNLOCK (connection
);
1836 g_task_return_new_error (task
, domain
, code
, "%s", message
);
1837 g_object_unref (task
);
1840 /* ---------------------------------------------------------------------------------------------------- */
1842 /* Called from a user thread, lock is not held */
1844 send_message_with_reply_cancelled_idle_cb (gpointer user_data
)
1846 GTask
*task
= user_data
;
1848 send_message_data_deliver_error (task
, G_IO_ERROR
, G_IO_ERROR_CANCELLED
,
1849 _("Operation was cancelled"));
1853 /* Can be called from any thread with or without lock held */
1855 send_message_with_reply_cancelled_cb (GCancellable
*cancellable
,
1858 GTask
*task
= user_data
;
1859 GSource
*idle_source
;
1861 /* postpone cancellation to idle handler since we may be called directly
1862 * via g_cancellable_connect() (e.g. holding lock)
1864 idle_source
= g_idle_source_new ();
1865 g_source_set_name (idle_source
, "[gio] send_message_with_reply_cancelled_idle_cb");
1866 g_task_attach_source (task
, idle_source
, send_message_with_reply_cancelled_idle_cb
);
1867 g_source_unref (idle_source
);
1870 /* ---------------------------------------------------------------------------------------------------- */
1872 /* Called from a user thread, lock is not held */
1874 send_message_with_reply_timeout_cb (gpointer user_data
)
1876 GTask
*task
= user_data
;
1878 send_message_data_deliver_error (task
, G_IO_ERROR
, G_IO_ERROR_TIMED_OUT
,
1879 _("Timeout was reached"));
1883 /* ---------------------------------------------------------------------------------------------------- */
1885 /* Called from a user thread, connection's lock is held */
1887 g_dbus_connection_send_message_with_reply_unlocked (GDBusConnection
*connection
,
1888 GDBusMessage
*message
,
1889 GDBusSendMessageFlags flags
,
1891 volatile guint32
*out_serial
,
1892 GCancellable
*cancellable
,
1893 GAsyncReadyCallback callback
,
1897 SendMessageData
*data
;
1898 GError
*error
= NULL
;
1899 volatile guint32 serial
;
1901 if (out_serial
== NULL
)
1902 out_serial
= &serial
;
1904 if (timeout_msec
== -1)
1905 timeout_msec
= 25 * 1000;
1907 data
= g_slice_new0 (SendMessageData
);
1908 task
= g_task_new (connection
, cancellable
, callback
, user_data
);
1909 g_task_set_source_tag (task
,
1910 g_dbus_connection_send_message_with_reply_unlocked
);
1911 g_task_set_task_data (task
, data
, (GDestroyNotify
) send_message_data_free
);
1913 if (g_task_return_error_if_cancelled (task
))
1915 g_object_unref (task
);
1919 if (!g_dbus_connection_send_message_unlocked (connection
, message
, flags
, out_serial
, &error
))
1921 g_task_return_error (task
, error
);
1922 g_object_unref (task
);
1925 data
->serial
= *out_serial
;
1927 if (cancellable
!= NULL
)
1929 data
->cancellable_handler_id
= g_cancellable_connect (cancellable
,
1930 G_CALLBACK (send_message_with_reply_cancelled_cb
),
1931 g_object_ref (task
),
1935 if (timeout_msec
!= G_MAXINT
)
1937 data
->timeout_source
= g_timeout_source_new (timeout_msec
);
1938 g_task_attach_source (task
, data
->timeout_source
,
1939 (GSourceFunc
) send_message_with_reply_timeout_cb
);
1940 g_source_unref (data
->timeout_source
);
1943 g_hash_table_insert (connection
->map_method_serial_to_task
,
1944 GUINT_TO_POINTER (*out_serial
),
1949 * g_dbus_connection_send_message_with_reply:
1950 * @connection: a #GDBusConnection
1951 * @message: a #GDBusMessage
1952 * @flags: flags affecting how the message is sent
1953 * @timeout_msec: the timeout in milliseconds, -1 to use the default
1954 * timeout or %G_MAXINT for no timeout
1955 * @out_serial: (out) (optional): return location for serial number assigned
1956 * to @message when sending it or %NULL
1957 * @cancellable: (nullable): a #GCancellable or %NULL
1958 * @callback: (nullable): a #GAsyncReadyCallback to call when the request
1959 * is satisfied or %NULL if you don't care about the result
1960 * @user_data: The data to pass to @callback
1962 * Asynchronously sends @message to the peer represented by @connection.
1964 * Unless @flags contain the
1965 * %G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL flag, the serial number
1966 * will be assigned by @connection and set on @message via
1967 * g_dbus_message_set_serial(). If @out_serial is not %NULL, then the
1968 * serial number used will be written to this location prior to
1969 * submitting the message to the underlying transport.
1971 * If @connection is closed then the operation will fail with
1972 * %G_IO_ERROR_CLOSED. If @cancellable is canceled, the operation will
1973 * fail with %G_IO_ERROR_CANCELLED. If @message is not well-formed,
1974 * the operation fails with %G_IO_ERROR_INVALID_ARGUMENT.
1976 * This is an asynchronous method. When the operation is finished, @callback
1977 * will be invoked in the
1978 * [thread-default main context][g-main-context-push-thread-default]
1979 * of the thread you are calling this method from. You can then call
1980 * g_dbus_connection_send_message_with_reply_finish() to get the result of the operation.
1981 * See g_dbus_connection_send_message_with_reply_sync() for the synchronous version.
1983 * Note that @message must be unlocked, unless @flags contain the
1984 * %G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL flag.
1986 * See this [server][gdbus-server] and [client][gdbus-unix-fd-client]
1987 * for an example of how to use this low-level API to send and receive
1988 * UNIX file descriptors.
1993 g_dbus_connection_send_message_with_reply (GDBusConnection
*connection
,
1994 GDBusMessage
*message
,
1995 GDBusSendMessageFlags flags
,
1997 volatile guint32
*out_serial
,
1998 GCancellable
*cancellable
,
1999 GAsyncReadyCallback callback
,
2002 g_return_if_fail (G_IS_DBUS_CONNECTION (connection
));
2003 g_return_if_fail (G_IS_DBUS_MESSAGE (message
));
2004 g_return_if_fail ((flags
& G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL
) || !g_dbus_message_get_locked (message
));
2005 g_return_if_fail (timeout_msec
>= 0 || timeout_msec
== -1);
2007 CONNECTION_LOCK (connection
);
2008 g_dbus_connection_send_message_with_reply_unlocked (connection
,
2016 CONNECTION_UNLOCK (connection
);
2020 * g_dbus_connection_send_message_with_reply_finish:
2021 * @connection: a #GDBusConnection
2022 * @res: a #GAsyncResult obtained from the #GAsyncReadyCallback passed to
2023 * g_dbus_connection_send_message_with_reply()
2024 * @error: teturn location for error or %NULL
2026 * Finishes an operation started with g_dbus_connection_send_message_with_reply().
2028 * Note that @error is only set if a local in-process error
2029 * occurred. That is to say that the returned #GDBusMessage object may
2030 * be of type %G_DBUS_MESSAGE_TYPE_ERROR. Use
2031 * g_dbus_message_to_gerror() to transcode this to a #GError.
2033 * See this [server][gdbus-server] and [client][gdbus-unix-fd-client]
2034 * for an example of how to use this low-level API to send and receive
2035 * UNIX file descriptors.
2037 * Returns: (transfer full): a locked #GDBusMessage or %NULL if @error is set
2042 g_dbus_connection_send_message_with_reply_finish (GDBusConnection
*connection
,
2046 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), NULL
);
2047 g_return_val_if_fail (g_task_is_valid (res
, connection
), NULL
);
2048 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, NULL
);
2050 return g_task_propagate_pointer (G_TASK (res
), error
);
2053 /* ---------------------------------------------------------------------------------------------------- */
2058 GMainContext
*context
;
2060 } SendMessageSyncData
;
2062 /* Called from a user thread, lock is not held */
2064 send_message_with_reply_sync_cb (GDBusConnection
*connection
,
2068 SendMessageSyncData
*data
= user_data
;
2069 data
->res
= g_object_ref (res
);
2070 g_main_loop_quit (data
->loop
);
2074 * g_dbus_connection_send_message_with_reply_sync:
2075 * @connection: a #GDBusConnection
2076 * @message: a #GDBusMessage
2077 * @flags: flags affecting how the message is sent.
2078 * @timeout_msec: the timeout in milliseconds, -1 to use the default
2079 * timeout or %G_MAXINT for no timeout
2080 * @out_serial: (out) (optional): return location for serial number
2081 * assigned to @message when sending it or %NULL
2082 * @cancellable: (nullable): a #GCancellable or %NULL
2083 * @error: return location for error or %NULL
2085 * Synchronously sends @message to the peer represented by @connection
2086 * and blocks the calling thread until a reply is received or the
2087 * timeout is reached. See g_dbus_connection_send_message_with_reply()
2088 * for the asynchronous version of this method.
2090 * Unless @flags contain the
2091 * %G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL flag, the serial number
2092 * will be assigned by @connection and set on @message via
2093 * g_dbus_message_set_serial(). If @out_serial is not %NULL, then the
2094 * serial number used will be written to this location prior to
2095 * submitting the message to the underlying transport.
2097 * If @connection is closed then the operation will fail with
2098 * %G_IO_ERROR_CLOSED. If @cancellable is canceled, the operation will
2099 * fail with %G_IO_ERROR_CANCELLED. If @message is not well-formed,
2100 * the operation fails with %G_IO_ERROR_INVALID_ARGUMENT.
2102 * Note that @error is only set if a local in-process error
2103 * occurred. That is to say that the returned #GDBusMessage object may
2104 * be of type %G_DBUS_MESSAGE_TYPE_ERROR. Use
2105 * g_dbus_message_to_gerror() to transcode this to a #GError.
2107 * See this [server][gdbus-server] and [client][gdbus-unix-fd-client]
2108 * for an example of how to use this low-level API to send and receive
2109 * UNIX file descriptors.
2111 * Note that @message must be unlocked, unless @flags contain the
2112 * %G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL flag.
2114 * Returns: (transfer full): a locked #GDBusMessage that is the reply
2115 * to @message or %NULL if @error is set
2120 g_dbus_connection_send_message_with_reply_sync (GDBusConnection
*connection
,
2121 GDBusMessage
*message
,
2122 GDBusSendMessageFlags flags
,
2124 volatile guint32
*out_serial
,
2125 GCancellable
*cancellable
,
2128 SendMessageSyncData data
;
2129 GDBusMessage
*reply
;
2131 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), NULL
);
2132 g_return_val_if_fail (G_IS_DBUS_MESSAGE (message
), NULL
);
2133 g_return_val_if_fail ((flags
& G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL
) || !g_dbus_message_get_locked (message
), NULL
);
2134 g_return_val_if_fail (timeout_msec
>= 0 || timeout_msec
== -1, NULL
);
2135 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, NULL
);
2138 data
.context
= g_main_context_new ();
2139 data
.loop
= g_main_loop_new (data
.context
, FALSE
);
2141 g_main_context_push_thread_default (data
.context
);
2143 g_dbus_connection_send_message_with_reply (connection
,
2149 (GAsyncReadyCallback
) send_message_with_reply_sync_cb
,
2151 g_main_loop_run (data
.loop
);
2152 reply
= g_dbus_connection_send_message_with_reply_finish (connection
,
2156 g_main_context_pop_thread_default (data
.context
);
2158 g_main_context_unref (data
.context
);
2159 g_main_loop_unref (data
.loop
);
2161 g_object_unref (data
.res
);
2166 /* ---------------------------------------------------------------------------------------------------- */
2172 GDBusMessageFilterFunction filter_function
;
2174 GDestroyNotify user_data_free_func
;
2175 GMainContext
*context
;
2178 /* requires CONNECTION_LOCK */
2179 static FilterData
**
2180 copy_filter_list (GPtrArray
*filters
)
2185 copy
= g_new (FilterData
*, filters
->len
+ 1);
2186 for (n
= 0; n
< filters
->len
; n
++)
2188 copy
[n
] = filters
->pdata
[n
];
2189 copy
[n
]->ref_count
++;
2196 /* requires CONNECTION_LOCK */
2198 free_filter_list (FilterData
**filters
)
2202 for (n
= 0; filters
[n
]; n
++)
2204 filters
[n
]->ref_count
--;
2205 if (filters
[n
]->ref_count
== 0)
2207 call_destroy_notify (filters
[n
]->context
,
2208 filters
[n
]->user_data_free_func
,
2209 filters
[n
]->user_data
);
2210 g_main_context_unref (filters
[n
]->context
);
2211 g_free (filters
[n
]);
2217 /* Called in GDBusWorker's thread - we must not block - with no lock held */
2219 on_worker_message_received (GDBusWorker
*worker
,
2220 GDBusMessage
*message
,
2223 GDBusConnection
*connection
;
2224 FilterData
**filters
;
2228 G_LOCK (message_bus_lock
);
2229 alive
= (g_hash_table_lookup (alive_connections
, user_data
) != NULL
);
2232 G_UNLOCK (message_bus_lock
);
2235 connection
= G_DBUS_CONNECTION (user_data
);
2236 g_object_ref (connection
);
2237 G_UNLOCK (message_bus_lock
);
2239 //g_debug ("in on_worker_message_received");
2241 g_object_ref (message
);
2242 g_dbus_message_lock (message
);
2244 //g_debug ("boo ref_count = %d %p %p", G_OBJECT (connection)->ref_count, connection, connection->worker);
2246 /* First collect the set of callback functions */
2247 CONNECTION_LOCK (connection
);
2248 filters
= copy_filter_list (connection
->filters
);
2249 CONNECTION_UNLOCK (connection
);
2251 /* then call the filters in order (without holding the lock) */
2252 for (n
= 0; filters
[n
]; n
++)
2254 message
= filters
[n
]->filter_function (connection
,
2257 filters
[n
]->user_data
);
2258 if (message
== NULL
)
2260 g_dbus_message_lock (message
);
2263 CONNECTION_LOCK (connection
);
2264 free_filter_list (filters
);
2265 CONNECTION_UNLOCK (connection
);
2267 /* Standard dispatch unless the filter ate the message - no need to
2268 * do anything if the message was altered
2270 if (message
!= NULL
)
2272 GDBusMessageType message_type
;
2274 message_type
= g_dbus_message_get_message_type (message
);
2275 if (message_type
== G_DBUS_MESSAGE_TYPE_METHOD_RETURN
|| message_type
== G_DBUS_MESSAGE_TYPE_ERROR
)
2277 guint32 reply_serial
;
2280 reply_serial
= g_dbus_message_get_reply_serial (message
);
2281 CONNECTION_LOCK (connection
);
2282 task
= g_hash_table_lookup (connection
->map_method_serial_to_task
,
2283 GUINT_TO_POINTER (reply_serial
));
2286 //g_debug ("delivering reply/error for serial %d for %p", reply_serial, connection);
2287 send_message_data_deliver_reply_unlocked (task
, message
);
2291 //g_debug ("message reply/error for serial %d but no SendMessageData found for %p", reply_serial, connection);
2293 CONNECTION_UNLOCK (connection
);
2295 else if (message_type
== G_DBUS_MESSAGE_TYPE_SIGNAL
)
2297 CONNECTION_LOCK (connection
);
2298 distribute_signals (connection
, message
);
2299 CONNECTION_UNLOCK (connection
);
2301 else if (message_type
== G_DBUS_MESSAGE_TYPE_METHOD_CALL
)
2303 CONNECTION_LOCK (connection
);
2304 distribute_method_call (connection
, message
);
2305 CONNECTION_UNLOCK (connection
);
2309 if (message
!= NULL
)
2310 g_object_unref (message
);
2311 g_object_unref (connection
);
2314 /* Called in GDBusWorker's thread, lock is not held */
2315 static GDBusMessage
*
2316 on_worker_message_about_to_be_sent (GDBusWorker
*worker
,
2317 GDBusMessage
*message
,
2320 GDBusConnection
*connection
;
2321 FilterData
**filters
;
2325 G_LOCK (message_bus_lock
);
2326 alive
= (g_hash_table_lookup (alive_connections
, user_data
) != NULL
);
2329 G_UNLOCK (message_bus_lock
);
2332 connection
= G_DBUS_CONNECTION (user_data
);
2333 g_object_ref (connection
);
2334 G_UNLOCK (message_bus_lock
);
2336 //g_debug ("in on_worker_message_about_to_be_sent");
2338 /* First collect the set of callback functions */
2339 CONNECTION_LOCK (connection
);
2340 filters
= copy_filter_list (connection
->filters
);
2341 CONNECTION_UNLOCK (connection
);
2343 /* then call the filters in order (without holding the lock) */
2344 for (n
= 0; filters
[n
]; n
++)
2346 g_dbus_message_lock (message
);
2347 message
= filters
[n
]->filter_function (connection
,
2350 filters
[n
]->user_data
);
2351 if (message
== NULL
)
2355 CONNECTION_LOCK (connection
);
2356 free_filter_list (filters
);
2357 CONNECTION_UNLOCK (connection
);
2359 g_object_unref (connection
);
2364 /* called with connection lock held, in GDBusWorker thread */
2366 cancel_method_on_close (gpointer key
, gpointer value
, gpointer user_data
)
2368 GTask
*task
= value
;
2369 SendMessageData
*data
= g_task_get_task_data (task
);
2371 if (data
->delivered
)
2374 g_task_return_new_error (task
,
2377 _("The connection is closed"));
2379 /* Ask send_message_with_reply_cleanup not to remove the element from the
2380 * hash table - we're in the middle of a foreach; that would be unsafe.
2381 * Instead, return TRUE from this function so that it gets removed safely.
2383 send_message_with_reply_cleanup (task
, FALSE
);
2387 /* Called in GDBusWorker's thread - we must not block - without lock held */
2389 on_worker_closed (GDBusWorker
*worker
,
2390 gboolean remote_peer_vanished
,
2394 GDBusConnection
*connection
;
2396 guint old_atomic_flags
;
2398 G_LOCK (message_bus_lock
);
2399 alive
= (g_hash_table_lookup (alive_connections
, user_data
) != NULL
);
2402 G_UNLOCK (message_bus_lock
);
2405 connection
= G_DBUS_CONNECTION (user_data
);
2406 g_object_ref (connection
);
2407 G_UNLOCK (message_bus_lock
);
2409 //g_debug ("in on_worker_closed: %s", error->message);
2411 CONNECTION_LOCK (connection
);
2412 /* Even though this is atomic, we do it inside the lock to avoid breaking
2413 * assumptions in remove_match_rule(). We'd need the lock in a moment
2414 * anyway, so, no loss.
2416 old_atomic_flags
= g_atomic_int_or (&connection
->atomic_flags
, FLAG_CLOSED
);
2418 if (!(old_atomic_flags
& FLAG_CLOSED
))
2420 g_hash_table_foreach_remove (connection
->map_method_serial_to_task
, cancel_method_on_close
, NULL
);
2421 schedule_closed_unlocked (connection
, remote_peer_vanished
, error
);
2423 CONNECTION_UNLOCK (connection
);
2425 g_object_unref (connection
);
2428 /* ---------------------------------------------------------------------------------------------------- */
2430 /* Determines the biggest set of capabilities we can support on this
2433 * Called with the init_lock held.
2435 static GDBusCapabilityFlags
2436 get_offered_capabilities_max (GDBusConnection
*connection
)
2438 GDBusCapabilityFlags ret
;
2439 ret
= G_DBUS_CAPABILITY_FLAGS_NONE
;
2441 if (G_IS_UNIX_CONNECTION (connection
->stream
))
2442 ret
|= G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING
;
2447 /* Called in a user thread, lock is not held */
2449 initable_init (GInitable
*initable
,
2450 GCancellable
*cancellable
,
2453 GDBusConnection
*connection
= G_DBUS_CONNECTION (initable
);
2456 /* This method needs to be idempotent to work with the singleton
2457 * pattern. See the docs for g_initable_init(). We implement this by
2460 * Unfortunately we can't use the main lock since the on_worker_*()
2461 * callbacks above needs the lock during initialization (for message
2462 * bus connections we do a synchronous Hello() call on the bus).
2464 g_mutex_lock (&connection
->init_lock
);
2468 /* Make this a no-op if we're already initialized (successfully or
2471 if ((g_atomic_int_get (&connection
->atomic_flags
) & FLAG_INITIALIZED
))
2473 ret
= (connection
->initialization_error
== NULL
);
2477 /* Because of init_lock, we can't get here twice in different threads */
2478 g_assert (connection
->initialization_error
== NULL
);
2480 /* The user can pass multiple (but mutally exclusive) construct
2483 * - stream (of type GIOStream)
2484 * - address (of type gchar*)
2486 * At the end of the day we end up with a non-NULL GIOStream
2487 * object in connection->stream.
2489 if (connection
->address
!= NULL
)
2491 g_assert (connection
->stream
== NULL
);
2493 if ((connection
->flags
& G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER
) ||
2494 (connection
->flags
& G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS
))
2496 g_set_error_literal (&connection
->initialization_error
,
2498 G_IO_ERROR_INVALID_ARGUMENT
,
2499 _("Unsupported flags encountered when constructing a client-side connection"));
2503 connection
->stream
= g_dbus_address_get_stream_sync (connection
->address
,
2504 NULL
, /* TODO: out_guid */
2506 &connection
->initialization_error
);
2507 if (connection
->stream
== NULL
)
2510 else if (connection
->stream
!= NULL
)
2516 g_assert_not_reached ();
2519 /* Authenticate the connection */
2520 if (connection
->flags
& G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER
)
2522 g_assert (!(connection
->flags
& G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT
));
2523 g_assert (connection
->guid
!= NULL
);
2524 connection
->auth
= _g_dbus_auth_new (connection
->stream
);
2525 if (!_g_dbus_auth_run_server (connection
->auth
,
2526 connection
->authentication_observer
,
2528 (connection
->flags
& G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS
),
2529 get_offered_capabilities_max (connection
),
2530 &connection
->capabilities
,
2531 &connection
->credentials
,
2533 &connection
->initialization_error
))
2536 else if (connection
->flags
& G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT
)
2538 g_assert (!(connection
->flags
& G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER
));
2539 g_assert (connection
->guid
== NULL
);
2540 connection
->auth
= _g_dbus_auth_new (connection
->stream
);
2541 connection
->guid
= _g_dbus_auth_run_client (connection
->auth
,
2542 connection
->authentication_observer
,
2543 get_offered_capabilities_max (connection
),
2544 &connection
->capabilities
,
2546 &connection
->initialization_error
);
2547 if (connection
->guid
== NULL
)
2551 if (connection
->authentication_observer
!= NULL
)
2553 g_object_unref (connection
->authentication_observer
);
2554 connection
->authentication_observer
= NULL
;
2557 //g_output_stream_flush (G_SOCKET_CONNECTION (connection->stream)
2559 //g_debug ("haz unix fd passing powers: %d", connection->capabilities & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);
2562 /* We want all IO operations to be non-blocking since they happen in
2563 * the worker thread which is shared by _all_ connections.
2565 if (G_IS_SOCKET_CONNECTION (connection
->stream
))
2567 g_socket_set_blocking (g_socket_connection_get_socket (G_SOCKET_CONNECTION (connection
->stream
)), FALSE
);
2571 G_LOCK (message_bus_lock
);
2572 if (alive_connections
== NULL
)
2573 alive_connections
= g_hash_table_new (g_direct_hash
, g_direct_equal
);
2574 g_hash_table_insert (alive_connections
, connection
, connection
);
2575 G_UNLOCK (message_bus_lock
);
2577 connection
->worker
= _g_dbus_worker_new (connection
->stream
,
2578 connection
->capabilities
,
2579 ((connection
->flags
& G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING
) != 0),
2580 on_worker_message_received
,
2581 on_worker_message_about_to_be_sent
,
2585 /* if a bus connection, call org.freedesktop.DBus.Hello - this is how we're getting a name */
2586 if (connection
->flags
& G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION
)
2588 GVariant
*hello_result
;
2590 /* we could lift this restriction by adding code in gdbusprivate.c */
2591 if (connection
->flags
& G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING
)
2593 g_set_error_literal (&connection
->initialization_error
,
2596 "Cannot use DELAY_MESSAGE_PROCESSING with MESSAGE_BUS_CONNECTION");
2600 hello_result
= g_dbus_connection_call_sync (connection
,
2601 "org.freedesktop.DBus", /* name */
2602 "/org/freedesktop/DBus", /* path */
2603 "org.freedesktop.DBus", /* interface */
2605 NULL
, /* parameters */
2606 G_VARIANT_TYPE ("(s)"),
2607 CALL_FLAGS_INITIALIZING
,
2609 NULL
, /* TODO: cancellable */
2610 &connection
->initialization_error
);
2611 if (hello_result
== NULL
)
2614 g_variant_get (hello_result
, "(s)", &connection
->bus_unique_name
);
2615 g_variant_unref (hello_result
);
2616 //g_debug ("unique name is '%s'", connection->bus_unique_name);
2623 g_assert (connection
->initialization_error
!= NULL
);
2624 g_propagate_error (error
, g_error_copy (connection
->initialization_error
));
2627 g_atomic_int_or (&connection
->atomic_flags
, FLAG_INITIALIZED
);
2628 g_mutex_unlock (&connection
->init_lock
);
2634 initable_iface_init (GInitableIface
*initable_iface
)
2636 initable_iface
->init
= initable_init
;
2639 /* ---------------------------------------------------------------------------------------------------- */
2642 async_initable_iface_init (GAsyncInitableIface
*async_initable_iface
)
2647 /* ---------------------------------------------------------------------------------------------------- */
2650 * g_dbus_connection_new:
2651 * @stream: a #GIOStream
2652 * @guid: (nullable): the GUID to use if a authenticating as a server or %NULL
2653 * @flags: flags describing how to make the connection
2654 * @observer: (nullable): a #GDBusAuthObserver or %NULL
2655 * @cancellable: (nullable): a #GCancellable or %NULL
2656 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2657 * @user_data: the data to pass to @callback
2659 * Asynchronously sets up a D-Bus connection for exchanging D-Bus messages
2660 * with the end represented by @stream.
2662 * If @stream is a #GSocketConnection, then the corresponding #GSocket
2663 * will be put into non-blocking mode.
2665 * The D-Bus connection will interact with @stream from a worker thread.
2666 * As a result, the caller should not interact with @stream after this
2667 * method has been called, except by calling g_object_unref() on it.
2669 * If @observer is not %NULL it may be used to control the
2670 * authentication process.
2672 * When the operation is finished, @callback will be invoked. You can
2673 * then call g_dbus_connection_new_finish() to get the result of the
2676 * This is a asynchronous failable constructor. See
2677 * g_dbus_connection_new_sync() for the synchronous
2683 g_dbus_connection_new (GIOStream
*stream
,
2685 GDBusConnectionFlags flags
,
2686 GDBusAuthObserver
*observer
,
2687 GCancellable
*cancellable
,
2688 GAsyncReadyCallback callback
,
2691 g_return_if_fail (G_IS_IO_STREAM (stream
));
2692 g_async_initable_new_async (G_TYPE_DBUS_CONNECTION
,
2700 "authentication-observer", observer
,
2705 * g_dbus_connection_new_finish:
2706 * @res: a #GAsyncResult obtained from the #GAsyncReadyCallback
2707 * passed to g_dbus_connection_new().
2708 * @error: return location for error or %NULL
2710 * Finishes an operation started with g_dbus_connection_new().
2712 * Returns: a #GDBusConnection or %NULL if @error is set. Free
2713 * with g_object_unref().
2718 g_dbus_connection_new_finish (GAsyncResult
*res
,
2722 GObject
*source_object
;
2724 g_return_val_if_fail (G_IS_ASYNC_RESULT (res
), NULL
);
2725 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, NULL
);
2727 source_object
= g_async_result_get_source_object (res
);
2728 g_assert (source_object
!= NULL
);
2729 object
= g_async_initable_new_finish (G_ASYNC_INITABLE (source_object
),
2732 g_object_unref (source_object
);
2734 return G_DBUS_CONNECTION (object
);
2740 * g_dbus_connection_new_sync:
2741 * @stream: a #GIOStream
2742 * @guid: (nullable): the GUID to use if a authenticating as a server or %NULL
2743 * @flags: flags describing how to make the connection
2744 * @observer: (nullable): a #GDBusAuthObserver or %NULL
2745 * @cancellable: (nullable): a #GCancellable or %NULL
2746 * @error: return location for error or %NULL
2748 * Synchronously sets up a D-Bus connection for exchanging D-Bus messages
2749 * with the end represented by @stream.
2751 * If @stream is a #GSocketConnection, then the corresponding #GSocket
2752 * will be put into non-blocking mode.
2754 * The D-Bus connection will interact with @stream from a worker thread.
2755 * As a result, the caller should not interact with @stream after this
2756 * method has been called, except by calling g_object_unref() on it.
2758 * If @observer is not %NULL it may be used to control the
2759 * authentication process.
2761 * This is a synchronous failable constructor. See
2762 * g_dbus_connection_new() for the asynchronous version.
2764 * Returns: a #GDBusConnection or %NULL if @error is set. Free with g_object_unref().
2769 g_dbus_connection_new_sync (GIOStream
*stream
,
2771 GDBusConnectionFlags flags
,
2772 GDBusAuthObserver
*observer
,
2773 GCancellable
*cancellable
,
2776 g_return_val_if_fail (G_IS_IO_STREAM (stream
), NULL
);
2777 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, NULL
);
2778 return g_initable_new (G_TYPE_DBUS_CONNECTION
,
2784 "authentication-observer", observer
,
2788 /* ---------------------------------------------------------------------------------------------------- */
2791 * g_dbus_connection_new_for_address:
2792 * @address: a D-Bus address
2793 * @flags: flags describing how to make the connection
2794 * @observer: (nullable): a #GDBusAuthObserver or %NULL
2795 * @cancellable: (nullable): a #GCancellable or %NULL
2796 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2797 * @user_data: the data to pass to @callback
2799 * Asynchronously connects and sets up a D-Bus client connection for
2800 * exchanging D-Bus messages with an endpoint specified by @address
2801 * which must be in the
2802 * [D-Bus address format](https://dbus.freedesktop.org/doc/dbus-specification.html#addresses).
2804 * This constructor can only be used to initiate client-side
2805 * connections - use g_dbus_connection_new() if you need to act as the
2806 * server. In particular, @flags cannot contain the
2807 * %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER or
2808 * %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS flags.
2810 * When the operation is finished, @callback will be invoked. You can
2811 * then call g_dbus_connection_new_finish() to get the result of the
2814 * If @observer is not %NULL it may be used to control the
2815 * authentication process.
2817 * This is a asynchronous failable constructor. See
2818 * g_dbus_connection_new_for_address_sync() for the synchronous
2824 g_dbus_connection_new_for_address (const gchar
*address
,
2825 GDBusConnectionFlags flags
,
2826 GDBusAuthObserver
*observer
,
2827 GCancellable
*cancellable
,
2828 GAsyncReadyCallback callback
,
2831 g_return_if_fail (address
!= NULL
);
2832 g_async_initable_new_async (G_TYPE_DBUS_CONNECTION
,
2839 "authentication-observer", observer
,
2844 * g_dbus_connection_new_for_address_finish:
2845 * @res: a #GAsyncResult obtained from the #GAsyncReadyCallback passed
2846 * to g_dbus_connection_new()
2847 * @error: return location for error or %NULL
2849 * Finishes an operation started with g_dbus_connection_new_for_address().
2851 * Returns: a #GDBusConnection or %NULL if @error is set. Free with
2857 g_dbus_connection_new_for_address_finish (GAsyncResult
*res
,
2861 GObject
*source_object
;
2863 g_return_val_if_fail (G_IS_ASYNC_RESULT (res
), NULL
);
2864 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, NULL
);
2866 source_object
= g_async_result_get_source_object (res
);
2867 g_assert (source_object
!= NULL
);
2868 object
= g_async_initable_new_finish (G_ASYNC_INITABLE (source_object
),
2871 g_object_unref (source_object
);
2873 return G_DBUS_CONNECTION (object
);
2879 * g_dbus_connection_new_for_address_sync:
2880 * @address: a D-Bus address
2881 * @flags: flags describing how to make the connection
2882 * @observer: (nullable): a #GDBusAuthObserver or %NULL
2883 * @cancellable: (nullable): a #GCancellable or %NULL
2884 * @error: return location for error or %NULL
2886 * Synchronously connects and sets up a D-Bus client connection for
2887 * exchanging D-Bus messages with an endpoint specified by @address
2888 * which must be in the
2889 * [D-Bus address format](https://dbus.freedesktop.org/doc/dbus-specification.html#addresses).
2891 * This constructor can only be used to initiate client-side
2892 * connections - use g_dbus_connection_new_sync() if you need to act
2893 * as the server. In particular, @flags cannot contain the
2894 * %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER or
2895 * %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS flags.
2897 * This is a synchronous failable constructor. See
2898 * g_dbus_connection_new_for_address() for the asynchronous version.
2900 * If @observer is not %NULL it may be used to control the
2901 * authentication process.
2903 * Returns: a #GDBusConnection or %NULL if @error is set. Free with
2909 g_dbus_connection_new_for_address_sync (const gchar
*address
,
2910 GDBusConnectionFlags flags
,
2911 GDBusAuthObserver
*observer
,
2912 GCancellable
*cancellable
,
2915 g_return_val_if_fail (address
!= NULL
, NULL
);
2916 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, NULL
);
2917 return g_initable_new (G_TYPE_DBUS_CONNECTION
,
2922 "authentication-observer", observer
,
2926 /* ---------------------------------------------------------------------------------------------------- */
2929 * g_dbus_connection_set_exit_on_close:
2930 * @connection: a #GDBusConnection
2931 * @exit_on_close: whether the process should be terminated
2932 * when @connection is closed by the remote peer
2934 * Sets whether the process should be terminated when @connection is
2935 * closed by the remote peer. See #GDBusConnection:exit-on-close for
2938 * Note that this function should be used with care. Most modern UNIX
2939 * desktops tie the notion of a user session the session bus, and expect
2940 * all of a users applications to quit when their bus connection goes away.
2941 * If you are setting @exit_on_close to %FALSE for the shared session
2942 * bus connection, you should make sure that your application exits
2943 * when the user session ends.
2948 g_dbus_connection_set_exit_on_close (GDBusConnection
*connection
,
2949 gboolean exit_on_close
)
2951 g_return_if_fail (G_IS_DBUS_CONNECTION (connection
));
2954 g_atomic_int_or (&connection
->atomic_flags
, FLAG_EXIT_ON_CLOSE
);
2956 g_atomic_int_and (&connection
->atomic_flags
, ~FLAG_EXIT_ON_CLOSE
);
2961 * g_dbus_connection_get_exit_on_close:
2962 * @connection: a #GDBusConnection
2964 * Gets whether the process is terminated when @connection is
2965 * closed by the remote peer. See
2966 * #GDBusConnection:exit-on-close for more details.
2968 * Returns: whether the process is terminated when @connection is
2969 * closed by the remote peer
2974 g_dbus_connection_get_exit_on_close (GDBusConnection
*connection
)
2976 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), FALSE
);
2978 if (g_atomic_int_get (&connection
->atomic_flags
) & FLAG_EXIT_ON_CLOSE
)
2985 * g_dbus_connection_get_guid:
2986 * @connection: a #GDBusConnection
2988 * The GUID of the peer performing the role of server when
2989 * authenticating. See #GDBusConnection:guid for more details.
2991 * Returns: The GUID. Do not free this string, it is owned by
2997 g_dbus_connection_get_guid (GDBusConnection
*connection
)
2999 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), NULL
);
3000 return connection
->guid
;
3004 * g_dbus_connection_get_unique_name:
3005 * @connection: a #GDBusConnection
3007 * Gets the unique name of @connection as assigned by the message
3008 * bus. This can also be used to figure out if @connection is a
3009 * message bus connection.
3011 * Returns: the unique name or %NULL if @connection is not a message
3012 * bus connection. Do not free this string, it is owned by
3018 g_dbus_connection_get_unique_name (GDBusConnection
*connection
)
3020 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), NULL
);
3022 /* do not use g_return_val_if_fail(), we want the memory barrier */
3023 if (!check_initialized (connection
))
3026 return connection
->bus_unique_name
;
3030 * g_dbus_connection_get_peer_credentials:
3031 * @connection: a #GDBusConnection
3033 * Gets the credentials of the authenticated peer. This will always
3034 * return %NULL unless @connection acted as a server
3035 * (e.g. %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER was passed)
3036 * when set up and the client passed credentials as part of the
3037 * authentication process.
3039 * In a message bus setup, the message bus is always the server and
3040 * each application is a client. So this method will always return
3041 * %NULL for message bus clients.
3043 * Returns: (transfer none) (nullable): a #GCredentials or %NULL if not
3044 * available. Do not free this object, it is owned by @connection.
3049 g_dbus_connection_get_peer_credentials (GDBusConnection
*connection
)
3051 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), NULL
);
3053 /* do not use g_return_val_if_fail(), we want the memory barrier */
3054 if (!check_initialized (connection
))
3057 return connection
->credentials
;
3060 /* ---------------------------------------------------------------------------------------------------- */
3062 static volatile guint _global_filter_id
= 1;
3065 * g_dbus_connection_add_filter:
3066 * @connection: a #GDBusConnection
3067 * @filter_function: a filter function
3068 * @user_data: user data to pass to @filter_function
3069 * @user_data_free_func: function to free @user_data with when filter
3070 * is removed or %NULL
3072 * Adds a message filter. Filters are handlers that are run on all
3073 * incoming and outgoing messages, prior to standard dispatch. Filters
3074 * are run in the order that they were added. The same handler can be
3075 * added as a filter more than once, in which case it will be run more
3076 * than once. Filters added during a filter callback won't be run on
3077 * the message being processed. Filter functions are allowed to modify
3078 * and even drop messages.
3080 * Note that filters are run in a dedicated message handling thread so
3081 * they can't block and, generally, can't do anything but signal a
3082 * worker thread. Also note that filters are rarely needed - use API
3083 * such as g_dbus_connection_send_message_with_reply(),
3084 * g_dbus_connection_signal_subscribe() or g_dbus_connection_call() instead.
3086 * If a filter consumes an incoming message the message is not
3087 * dispatched anywhere else - not even the standard dispatch machinery
3088 * (that API such as g_dbus_connection_signal_subscribe() and
3089 * g_dbus_connection_send_message_with_reply() relies on) will see the
3090 * message. Similary, if a filter consumes an outgoing message, the
3091 * message will not be sent to the other peer.
3093 * If @user_data_free_func is non-%NULL, it will be called (in the
3094 * thread-default main context of the thread you are calling this
3095 * method from) at some point after @user_data is no longer
3096 * needed. (It is not guaranteed to be called synchronously when the
3097 * filter is removed, and may be called after @connection has been
3100 * Returns: a filter identifier that can be used with
3101 * g_dbus_connection_remove_filter()
3106 g_dbus_connection_add_filter (GDBusConnection
*connection
,
3107 GDBusMessageFilterFunction filter_function
,
3109 GDestroyNotify user_data_free_func
)
3113 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), 0);
3114 g_return_val_if_fail (filter_function
!= NULL
, 0);
3115 g_return_val_if_fail (check_initialized (connection
), 0);
3117 CONNECTION_LOCK (connection
);
3118 data
= g_new0 (FilterData
, 1);
3119 data
->id
= g_atomic_int_add (&_global_filter_id
, 1); /* TODO: overflow etc. */
3120 data
->ref_count
= 1;
3121 data
->filter_function
= filter_function
;
3122 data
->user_data
= user_data
;
3123 data
->user_data_free_func
= user_data_free_func
;
3124 data
->context
= g_main_context_ref_thread_default ();
3125 g_ptr_array_add (connection
->filters
, data
);
3126 CONNECTION_UNLOCK (connection
);
3131 /* only called from finalize(), removes all filters */
3133 purge_all_filters (GDBusConnection
*connection
)
3136 for (n
= 0; n
< connection
->filters
->len
; n
++)
3138 FilterData
*data
= connection
->filters
->pdata
[n
];
3140 call_destroy_notify (data
->context
,
3141 data
->user_data_free_func
,
3143 g_main_context_unref (data
->context
);
3149 * g_dbus_connection_remove_filter:
3150 * @connection: a #GDBusConnection
3151 * @filter_id: an identifier obtained from g_dbus_connection_add_filter()
3155 * Note that since filters run in a different thread, there is a race
3156 * condition where it is possible that the filter will be running even
3157 * after calling g_dbus_connection_remove_filter(), so you cannot just
3158 * free data that the filter might be using. Instead, you should pass
3159 * a #GDestroyNotify to g_dbus_connection_add_filter(), which will be
3160 * called when it is guaranteed that the data is no longer needed.
3165 g_dbus_connection_remove_filter (GDBusConnection
*connection
,
3169 FilterData
*to_destroy
;
3171 g_return_if_fail (G_IS_DBUS_CONNECTION (connection
));
3172 g_return_if_fail (check_initialized (connection
));
3174 CONNECTION_LOCK (connection
);
3176 for (n
= 0; n
< connection
->filters
->len
; n
++)
3178 FilterData
*data
= connection
->filters
->pdata
[n
];
3179 if (data
->id
== filter_id
)
3181 g_ptr_array_remove_index (connection
->filters
, n
);
3183 if (data
->ref_count
== 0)
3188 CONNECTION_UNLOCK (connection
);
3190 /* do free without holding lock */
3191 if (to_destroy
!= NULL
)
3193 if (to_destroy
->user_data_free_func
!= NULL
)
3194 to_destroy
->user_data_free_func (to_destroy
->user_data
);
3195 g_main_context_unref (to_destroy
->context
);
3196 g_free (to_destroy
);
3200 g_warning ("g_dbus_connection_remove_filter: No filter found for filter_id %d", filter_id
);
3204 /* ---------------------------------------------------------------------------------------------------- */
3210 gchar
*sender_unique_name
; /* if sender is unique or org.freedesktop.DBus, then that name... otherwise blank */
3211 gchar
*interface_name
;
3215 GDBusSignalFlags flags
;
3216 GArray
*subscribers
;
3221 GDBusSignalCallback callback
;
3223 GDestroyNotify user_data_free_func
;
3225 GMainContext
*context
;
3229 signal_data_free (SignalData
*signal_data
)
3231 g_free (signal_data
->rule
);
3232 g_free (signal_data
->sender
);
3233 g_free (signal_data
->sender_unique_name
);
3234 g_free (signal_data
->interface_name
);
3235 g_free (signal_data
->member
);
3236 g_free (signal_data
->object_path
);
3237 g_free (signal_data
->arg0
);
3238 g_array_free (signal_data
->subscribers
, TRUE
);
3239 g_free (signal_data
);
3243 args_to_rule (const gchar
*sender
,
3244 const gchar
*interface_name
,
3245 const gchar
*member
,
3246 const gchar
*object_path
,
3248 GDBusSignalFlags flags
)
3252 rule
= g_string_new ("type='signal'");
3253 if (flags
& G_DBUS_SIGNAL_FLAGS_NO_MATCH_RULE
)
3254 g_string_prepend_c (rule
, '-');
3256 g_string_append_printf (rule
, ",sender='%s'", sender
);
3257 if (interface_name
!= NULL
)
3258 g_string_append_printf (rule
, ",interface='%s'", interface_name
);
3260 g_string_append_printf (rule
, ",member='%s'", member
);
3261 if (object_path
!= NULL
)
3262 g_string_append_printf (rule
, ",path='%s'", object_path
);
3266 if (flags
& G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH
)
3267 g_string_append_printf (rule
, ",arg0path='%s'", arg0
);
3268 else if (flags
& G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_NAMESPACE
)
3269 g_string_append_printf (rule
, ",arg0namespace='%s'", arg0
);
3271 g_string_append_printf (rule
, ",arg0='%s'", arg0
);
3274 return g_string_free (rule
, FALSE
);
3277 static volatile guint _global_subscriber_id
= 1;
3278 static volatile guint _global_registration_id
= 1;
3279 static volatile guint _global_subtree_registration_id
= 1;
3281 /* ---------------------------------------------------------------------------------------------------- */
3283 /* Called in a user thread, lock is held */
3285 add_match_rule (GDBusConnection
*connection
,
3286 const gchar
*match_rule
)
3289 GDBusMessage
*message
;
3291 if (match_rule
[0] == '-')
3294 message
= g_dbus_message_new_method_call ("org.freedesktop.DBus", /* name */
3295 "/org/freedesktop/DBus", /* path */
3296 "org.freedesktop.DBus", /* interface */
3298 g_dbus_message_set_body (message
, g_variant_new ("(s)", match_rule
));
3300 if (!g_dbus_connection_send_message_unlocked (connection
,
3302 G_DBUS_SEND_MESSAGE_FLAGS_NONE
,
3306 g_critical ("Error while sending AddMatch() message: %s", error
->message
);
3307 g_error_free (error
);
3309 g_object_unref (message
);
3312 /* ---------------------------------------------------------------------------------------------------- */
3314 /* Called in a user thread, lock is held */
3316 remove_match_rule (GDBusConnection
*connection
,
3317 const gchar
*match_rule
)
3320 GDBusMessage
*message
;
3322 if (match_rule
[0] == '-')
3325 message
= g_dbus_message_new_method_call ("org.freedesktop.DBus", /* name */
3326 "/org/freedesktop/DBus", /* path */
3327 "org.freedesktop.DBus", /* interface */
3329 g_dbus_message_set_body (message
, g_variant_new ("(s)", match_rule
));
3332 if (!g_dbus_connection_send_message_unlocked (connection
,
3334 G_DBUS_SEND_MESSAGE_FLAGS_NONE
,
3338 /* If we could get G_IO_ERROR_CLOSED here, it wouldn't be reasonable to
3339 * critical; but we're holding the lock, and our caller checked whether
3340 * we were already closed, so we can't get that error.
3342 g_critical ("Error while sending RemoveMatch() message: %s", error
->message
);
3343 g_error_free (error
);
3345 g_object_unref (message
);
3348 /* ---------------------------------------------------------------------------------------------------- */
3351 is_signal_data_for_name_lost_or_acquired (SignalData
*signal_data
)
3353 return g_strcmp0 (signal_data
->sender_unique_name
, "org.freedesktop.DBus") == 0 &&
3354 g_strcmp0 (signal_data
->interface_name
, "org.freedesktop.DBus") == 0 &&
3355 g_strcmp0 (signal_data
->object_path
, "/org/freedesktop/DBus") == 0 &&
3356 (g_strcmp0 (signal_data
->member
, "NameLost") == 0 ||
3357 g_strcmp0 (signal_data
->member
, "NameAcquired") == 0);
3360 /* ---------------------------------------------------------------------------------------------------- */
3363 * g_dbus_connection_signal_subscribe:
3364 * @connection: a #GDBusConnection
3365 * @sender: (nullable): sender name to match on (unique or well-known name)
3366 * or %NULL to listen from all senders
3367 * @interface_name: (nullable): D-Bus interface name to match on or %NULL to
3368 * match on all interfaces
3369 * @member: (nullable): D-Bus signal name to match on or %NULL to match on
3371 * @object_path: (nullable): object path to match on or %NULL to match on
3373 * @arg0: (nullable): contents of first string argument to match on or %NULL
3374 * to match on all kinds of arguments
3375 * @flags: #GDBusSignalFlags describing how arg0 is used in subscribing to the
3377 * @callback: callback to invoke when there is a signal matching the requested data
3378 * @user_data: user data to pass to @callback
3379 * @user_data_free_func: (nullable): function to free @user_data with when
3380 * subscription is removed or %NULL
3382 * Subscribes to signals on @connection and invokes @callback with a whenever
3383 * the signal is received. Note that @callback will be invoked in the
3384 * [thread-default main context][g-main-context-push-thread-default]
3385 * of the thread you are calling this method from.
3387 * If @connection is not a message bus connection, @sender must be
3390 * If @sender is a well-known name note that @callback is invoked with
3391 * the unique name for the owner of @sender, not the well-known name
3392 * as one would expect. This is because the message bus rewrites the
3393 * name. As such, to avoid certain race conditions, users should be
3394 * tracking the name owner of the well-known name and use that when
3395 * processing the received signal.
3397 * If one of %G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_NAMESPACE or
3398 * %G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH are given, @arg0 is
3399 * interpreted as part of a namespace or path. The first argument
3400 * of a signal is matched against that part as specified by D-Bus.
3402 * If @user_data_free_func is non-%NULL, it will be called (in the
3403 * thread-default main context of the thread you are calling this
3404 * method from) at some point after @user_data is no longer
3405 * needed. (It is not guaranteed to be called synchronously when the
3406 * signal is unsubscribed from, and may be called after @connection
3407 * has been destroyed.)
3409 * Returns: a subscription identifier that can be used with g_dbus_connection_signal_unsubscribe()
3414 g_dbus_connection_signal_subscribe (GDBusConnection
*connection
,
3415 const gchar
*sender
,
3416 const gchar
*interface_name
,
3417 const gchar
*member
,
3418 const gchar
*object_path
,
3420 GDBusSignalFlags flags
,
3421 GDBusSignalCallback callback
,
3423 GDestroyNotify user_data_free_func
)
3426 SignalData
*signal_data
;
3427 SignalSubscriber subscriber
;
3428 GPtrArray
*signal_data_array
;
3429 const gchar
*sender_unique_name
;
3431 /* Right now we abort if AddMatch() fails since it can only fail with the bus being in
3432 * an OOM condition. We might want to change that but that would involve making
3433 * g_dbus_connection_signal_subscribe() asynchronous and having the call sites
3434 * handle that. And there's really no sensible way of handling this short of retrying
3435 * to add the match rule... and then there's the little thing that, hey, maybe there's
3436 * a reason the bus in an OOM condition.
3438 * Doable, but not really sure it's worth it...
3441 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), 0);
3442 g_return_val_if_fail (sender
== NULL
|| (g_dbus_is_name (sender
) && (connection
->flags
& G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION
)), 0);
3443 g_return_val_if_fail (interface_name
== NULL
|| g_dbus_is_interface_name (interface_name
), 0);
3444 g_return_val_if_fail (member
== NULL
|| g_dbus_is_member_name (member
), 0);
3445 g_return_val_if_fail (object_path
== NULL
|| g_variant_is_object_path (object_path
), 0);
3446 g_return_val_if_fail (callback
!= NULL
, 0);
3447 g_return_val_if_fail (check_initialized (connection
), 0);
3448 g_return_val_if_fail (!((flags
& G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH
) && (flags
& G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_NAMESPACE
)), 0);
3449 g_return_val_if_fail (!(arg0
== NULL
&& (flags
& (G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH
| G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_NAMESPACE
))), 0);
3451 CONNECTION_LOCK (connection
);
3453 /* If G_DBUS_SIGNAL_FLAGS_NO_MATCH_RULE was specified, we will end up
3454 * with a '-' character to prefix the rule (which will otherwise be
3457 * This allows us to hash the rule and do our lifecycle tracking in
3458 * the usual way, but the '-' prevents the match rule from ever
3459 * actually being send to the bus (either for add or remove).
3461 rule
= args_to_rule (sender
, interface_name
, member
, object_path
, arg0
, flags
);
3463 if (sender
!= NULL
&& (g_dbus_is_unique_name (sender
) || g_strcmp0 (sender
, "org.freedesktop.DBus") == 0))
3464 sender_unique_name
= sender
;
3466 sender_unique_name
= "";
3468 subscriber
.callback
= callback
;
3469 subscriber
.user_data
= user_data
;
3470 subscriber
.user_data_free_func
= user_data_free_func
;
3471 subscriber
.id
= g_atomic_int_add (&_global_subscriber_id
, 1); /* TODO: overflow etc. */
3472 subscriber
.context
= g_main_context_ref_thread_default ();
3474 /* see if we've already have this rule */
3475 signal_data
= g_hash_table_lookup (connection
->map_rule_to_signal_data
, rule
);
3476 if (signal_data
!= NULL
)
3478 g_array_append_val (signal_data
->subscribers
, subscriber
);
3483 signal_data
= g_new0 (SignalData
, 1);
3484 signal_data
->rule
= rule
;
3485 signal_data
->sender
= g_strdup (sender
);
3486 signal_data
->sender_unique_name
= g_strdup (sender_unique_name
);
3487 signal_data
->interface_name
= g_strdup (interface_name
);
3488 signal_data
->member
= g_strdup (member
);
3489 signal_data
->object_path
= g_strdup (object_path
);
3490 signal_data
->arg0
= g_strdup (arg0
);
3491 signal_data
->flags
= flags
;
3492 signal_data
->subscribers
= g_array_new (FALSE
, FALSE
, sizeof (SignalSubscriber
));
3493 g_array_append_val (signal_data
->subscribers
, subscriber
);
3495 g_hash_table_insert (connection
->map_rule_to_signal_data
,
3499 /* Add the match rule to the bus...
3501 * Avoid adding match rules for NameLost and NameAcquired messages - the bus will
3502 * always send such messages to us.
3504 if (connection
->flags
& G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION
)
3506 if (!is_signal_data_for_name_lost_or_acquired (signal_data
))
3507 add_match_rule (connection
, signal_data
->rule
);
3510 signal_data_array
= g_hash_table_lookup (connection
->map_sender_unique_name_to_signal_data_array
,
3511 signal_data
->sender_unique_name
);
3512 if (signal_data_array
== NULL
)
3514 signal_data_array
= g_ptr_array_new ();
3515 g_hash_table_insert (connection
->map_sender_unique_name_to_signal_data_array
,
3516 g_strdup (signal_data
->sender_unique_name
),
3519 g_ptr_array_add (signal_data_array
, signal_data
);
3522 g_hash_table_insert (connection
->map_id_to_signal_data
,
3523 GUINT_TO_POINTER (subscriber
.id
),
3526 CONNECTION_UNLOCK (connection
);
3528 return subscriber
.id
;
3531 /* ---------------------------------------------------------------------------------------------------- */
3533 /* called in any thread */
3534 /* must hold lock when calling this (except if connection->finalizing is TRUE) */
3536 unsubscribe_id_internal (GDBusConnection
*connection
,
3537 guint subscription_id
,
3538 GArray
*out_removed_subscribers
)
3540 SignalData
*signal_data
;
3541 GPtrArray
*signal_data_array
;
3544 signal_data
= g_hash_table_lookup (connection
->map_id_to_signal_data
,
3545 GUINT_TO_POINTER (subscription_id
));
3546 if (signal_data
== NULL
)
3548 /* Don't warn here, we may have thrown all subscriptions out when the connection was closed */
3552 for (n
= 0; n
< signal_data
->subscribers
->len
; n
++)
3554 SignalSubscriber
*subscriber
;
3556 subscriber
= &(g_array_index (signal_data
->subscribers
, SignalSubscriber
, n
));
3557 if (subscriber
->id
!= subscription_id
)
3560 g_warn_if_fail (g_hash_table_remove (connection
->map_id_to_signal_data
,
3561 GUINT_TO_POINTER (subscription_id
)));
3562 g_array_append_val (out_removed_subscribers
, *subscriber
);
3563 g_array_remove_index (signal_data
->subscribers
, n
);
3565 if (signal_data
->subscribers
->len
== 0)
3567 g_warn_if_fail (g_hash_table_remove (connection
->map_rule_to_signal_data
, signal_data
->rule
));
3569 signal_data_array
= g_hash_table_lookup (connection
->map_sender_unique_name_to_signal_data_array
,
3570 signal_data
->sender_unique_name
);
3571 g_warn_if_fail (signal_data_array
!= NULL
);
3572 g_warn_if_fail (g_ptr_array_remove (signal_data_array
, signal_data
));
3574 if (signal_data_array
->len
== 0)
3576 g_warn_if_fail (g_hash_table_remove (connection
->map_sender_unique_name_to_signal_data_array
,
3577 signal_data
->sender_unique_name
));
3580 /* remove the match rule from the bus unless NameLost or NameAcquired (see subscribe()) */
3581 if ((connection
->flags
& G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION
) &&
3582 !is_signal_data_for_name_lost_or_acquired (signal_data
) &&
3583 !g_dbus_connection_is_closed (connection
) &&
3584 !connection
->finalizing
)
3586 /* The check for g_dbus_connection_is_closed() means that
3587 * sending the RemoveMatch message can't fail with
3588 * G_IO_ERROR_CLOSED, because we're holding the lock,
3589 * so on_worker_closed() can't happen between the check we just
3590 * did, and releasing the lock later.
3592 remove_match_rule (connection
, signal_data
->rule
);
3595 signal_data_free (signal_data
);
3601 g_assert_not_reached ();
3608 * g_dbus_connection_signal_unsubscribe:
3609 * @connection: a #GDBusConnection
3610 * @subscription_id: a subscription id obtained from
3611 * g_dbus_connection_signal_subscribe()
3613 * Unsubscribes from signals.
3618 g_dbus_connection_signal_unsubscribe (GDBusConnection
*connection
,
3619 guint subscription_id
)
3621 GArray
*subscribers
;
3624 g_return_if_fail (G_IS_DBUS_CONNECTION (connection
));
3625 g_return_if_fail (check_initialized (connection
));
3627 subscribers
= g_array_new (FALSE
, FALSE
, sizeof (SignalSubscriber
));
3629 CONNECTION_LOCK (connection
);
3630 unsubscribe_id_internal (connection
,
3633 CONNECTION_UNLOCK (connection
);
3636 g_assert (subscribers
->len
== 0 || subscribers
->len
== 1);
3638 /* call GDestroyNotify without lock held */
3639 for (n
= 0; n
< subscribers
->len
; n
++)
3641 SignalSubscriber
*subscriber
;
3642 subscriber
= &(g_array_index (subscribers
, SignalSubscriber
, n
));
3643 call_destroy_notify (subscriber
->context
,
3644 subscriber
->user_data_free_func
,
3645 subscriber
->user_data
);
3646 g_main_context_unref (subscriber
->context
);
3649 g_array_free (subscribers
, TRUE
);
3652 /* ---------------------------------------------------------------------------------------------------- */
3656 guint subscription_id
;
3657 GDBusSignalCallback callback
;
3659 GDBusMessage
*message
;
3660 GDBusConnection
*connection
;
3661 const gchar
*sender
;
3663 const gchar
*interface
;
3664 const gchar
*member
;
3667 /* called on delivery thread (e.g. where g_dbus_connection_signal_subscribe() was called) with
3671 emit_signal_instance_in_idle_cb (gpointer data
)
3673 SignalInstance
*signal_instance
= data
;
3674 GVariant
*parameters
;
3675 gboolean has_subscription
;
3677 parameters
= g_dbus_message_get_body (signal_instance
->message
);
3678 if (parameters
== NULL
)
3680 parameters
= g_variant_new ("()");
3681 g_variant_ref_sink (parameters
);
3685 g_variant_ref_sink (parameters
);
3689 g_print ("in emit_signal_instance_in_idle_cb (id=%d sender=%s path=%s interface=%s member=%s params=%s)\n",
3690 signal_instance
->subscription_id
,
3691 signal_instance
->sender
,
3692 signal_instance
->path
,
3693 signal_instance
->interface
,
3694 signal_instance
->member
,
3695 g_variant_print (parameters
, TRUE
));
3698 /* Careful here, don't do the callback if we no longer has the subscription */
3699 CONNECTION_LOCK (signal_instance
->connection
);
3700 has_subscription
= FALSE
;
3701 if (g_hash_table_lookup (signal_instance
->connection
->map_id_to_signal_data
,
3702 GUINT_TO_POINTER (signal_instance
->subscription_id
)) != NULL
)
3703 has_subscription
= TRUE
;
3704 CONNECTION_UNLOCK (signal_instance
->connection
);
3706 if (has_subscription
)
3707 signal_instance
->callback (signal_instance
->connection
,
3708 signal_instance
->sender
,
3709 signal_instance
->path
,
3710 signal_instance
->interface
,
3711 signal_instance
->member
,
3713 signal_instance
->user_data
);
3715 g_variant_unref (parameters
);
3721 signal_instance_free (SignalInstance
*signal_instance
)
3723 g_object_unref (signal_instance
->message
);
3724 g_object_unref (signal_instance
->connection
);
3725 g_free (signal_instance
);
3729 namespace_rule_matches (const gchar
*namespace,
3735 len_namespace
= strlen (namespace);
3736 len_name
= strlen (name
);
3738 if (len_name
< len_namespace
)
3741 if (memcmp (namespace, name
, len_namespace
) != 0)
3744 return len_namespace
== len_name
|| name
[len_namespace
] == '.';
3748 path_rule_matches (const gchar
*path_a
,
3749 const gchar
*path_b
)
3753 len_a
= strlen (path_a
);
3754 len_b
= strlen (path_b
);
3756 if (len_a
< len_b
&& (len_a
== 0 || path_a
[len_a
- 1] != '/'))
3759 if (len_b
< len_a
&& (len_b
== 0 || path_b
[len_b
- 1] != '/'))
3762 return memcmp (path_a
, path_b
, MIN (len_a
, len_b
)) == 0;
3765 /* called in GDBusWorker thread WITH lock held */
3767 schedule_callbacks (GDBusConnection
*connection
,
3768 GPtrArray
*signal_data_array
,
3769 GDBusMessage
*message
,
3770 const gchar
*sender
)
3773 const gchar
*interface
;
3774 const gchar
*member
;
3783 interface
= g_dbus_message_get_interface (message
);
3784 member
= g_dbus_message_get_member (message
);
3785 path
= g_dbus_message_get_path (message
);
3786 arg0
= g_dbus_message_get_arg0 (message
);
3789 g_print ("In schedule_callbacks:\n"
3791 " interface = '%s'\n"
3802 /* TODO: if this is slow, then we can change signal_data_array into
3803 * map_object_path_to_signal_data_array or something.
3805 for (n
= 0; n
< signal_data_array
->len
; n
++)
3807 SignalData
*signal_data
= signal_data_array
->pdata
[n
];
3809 if (signal_data
->interface_name
!= NULL
&& g_strcmp0 (signal_data
->interface_name
, interface
) != 0)
3812 if (signal_data
->member
!= NULL
&& g_strcmp0 (signal_data
->member
, member
) != 0)
3815 if (signal_data
->object_path
!= NULL
&& g_strcmp0 (signal_data
->object_path
, path
) != 0)
3818 if (signal_data
->arg0
!= NULL
)
3823 if (signal_data
->flags
& G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_NAMESPACE
)
3825 if (!namespace_rule_matches (signal_data
->arg0
, arg0
))
3828 else if (signal_data
->flags
& G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH
)
3830 if (!path_rule_matches (signal_data
->arg0
, arg0
))
3833 else if (!g_str_equal (signal_data
->arg0
, arg0
))
3837 for (m
= 0; m
< signal_data
->subscribers
->len
; m
++)
3839 SignalSubscriber
*subscriber
;
3840 GSource
*idle_source
;
3841 SignalInstance
*signal_instance
;
3843 subscriber
= &(g_array_index (signal_data
->subscribers
, SignalSubscriber
, m
));
3845 signal_instance
= g_new0 (SignalInstance
, 1);
3846 signal_instance
->subscription_id
= subscriber
->id
;
3847 signal_instance
->callback
= subscriber
->callback
;
3848 signal_instance
->user_data
= subscriber
->user_data
;
3849 signal_instance
->message
= g_object_ref (message
);
3850 signal_instance
->connection
= g_object_ref (connection
);
3851 signal_instance
->sender
= sender
;
3852 signal_instance
->path
= path
;
3853 signal_instance
->interface
= interface
;
3854 signal_instance
->member
= member
;
3856 idle_source
= g_idle_source_new ();
3857 g_source_set_priority (idle_source
, G_PRIORITY_DEFAULT
);
3858 g_source_set_callback (idle_source
,
3859 emit_signal_instance_in_idle_cb
,
3861 (GDestroyNotify
) signal_instance_free
);
3862 g_source_set_name (idle_source
, "[gio] emit_signal_instance_in_idle_cb");
3863 g_source_attach (idle_source
, subscriber
->context
);
3864 g_source_unref (idle_source
);
3869 /* called in GDBusWorker thread with lock held */
3871 distribute_signals (GDBusConnection
*connection
,
3872 GDBusMessage
*message
)
3874 GPtrArray
*signal_data_array
;
3875 const gchar
*sender
;
3877 sender
= g_dbus_message_get_sender (message
);
3879 if (G_UNLIKELY (_g_dbus_debug_signal ()))
3881 _g_dbus_debug_print_lock ();
3882 g_print ("========================================================================\n"
3883 "GDBus-debug:Signal:\n"
3884 " <<<< RECEIVED SIGNAL %s.%s\n"
3886 " sent by name %s\n",
3887 g_dbus_message_get_interface (message
),
3888 g_dbus_message_get_member (message
),
3889 g_dbus_message_get_path (message
),
3890 sender
!= NULL
? sender
: "(none)");
3891 _g_dbus_debug_print_unlock ();
3894 /* collect subscribers that match on sender */
3897 signal_data_array
= g_hash_table_lookup (connection
->map_sender_unique_name_to_signal_data_array
, sender
);
3898 if (signal_data_array
!= NULL
)
3899 schedule_callbacks (connection
, signal_data_array
, message
, sender
);
3902 /* collect subscribers not matching on sender */
3903 signal_data_array
= g_hash_table_lookup (connection
->map_sender_unique_name_to_signal_data_array
, "");
3904 if (signal_data_array
!= NULL
)
3905 schedule_callbacks (connection
, signal_data_array
, message
, sender
);
3908 /* ---------------------------------------------------------------------------------------------------- */
3910 /* only called from finalize(), removes all subscriptions */
3912 purge_all_signal_subscriptions (GDBusConnection
*connection
)
3914 GHashTableIter iter
;
3917 GArray
*subscribers
;
3920 ids
= g_array_new (FALSE
, FALSE
, sizeof (guint
));
3921 g_hash_table_iter_init (&iter
, connection
->map_id_to_signal_data
);
3922 while (g_hash_table_iter_next (&iter
, &key
, NULL
))
3924 guint subscription_id
= GPOINTER_TO_UINT (key
);
3925 g_array_append_val (ids
, subscription_id
);
3928 subscribers
= g_array_new (FALSE
, FALSE
, sizeof (SignalSubscriber
));
3929 for (n
= 0; n
< ids
->len
; n
++)
3931 guint subscription_id
= g_array_index (ids
, guint
, n
);
3932 unsubscribe_id_internal (connection
,
3936 g_array_free (ids
, TRUE
);
3938 /* call GDestroyNotify without lock held */
3939 for (n
= 0; n
< subscribers
->len
; n
++)
3941 SignalSubscriber
*subscriber
;
3942 subscriber
= &(g_array_index (subscribers
, SignalSubscriber
, n
));
3943 call_destroy_notify (subscriber
->context
,
3944 subscriber
->user_data_free_func
,
3945 subscriber
->user_data
);
3946 g_main_context_unref (subscriber
->context
);
3949 g_array_free (subscribers
, TRUE
);
3952 /* ---------------------------------------------------------------------------------------------------- */
3954 static GDBusInterfaceVTable
*
3955 _g_dbus_interface_vtable_copy (const GDBusInterfaceVTable
*vtable
)
3957 /* Don't waste memory by copying padding - remember to update this
3958 * when changing struct _GDBusInterfaceVTable in gdbusconnection.h
3960 return g_memdup ((gconstpointer
) vtable
, 3 * sizeof (gpointer
));
3964 _g_dbus_interface_vtable_free (GDBusInterfaceVTable
*vtable
)
3969 /* ---------------------------------------------------------------------------------------------------- */
3971 static GDBusSubtreeVTable
*
3972 _g_dbus_subtree_vtable_copy (const GDBusSubtreeVTable
*vtable
)
3974 /* Don't waste memory by copying padding - remember to update this
3975 * when changing struct _GDBusSubtreeVTable in gdbusconnection.h
3977 return g_memdup ((gconstpointer
) vtable
, 3 * sizeof (gpointer
));
3981 _g_dbus_subtree_vtable_free (GDBusSubtreeVTable
*vtable
)
3986 /* ---------------------------------------------------------------------------------------------------- */
3988 struct ExportedObject
3991 GDBusConnection
*connection
;
3993 /* maps gchar* -> ExportedInterface* */
3994 GHashTable
*map_if_name_to_ei
;
3997 /* only called with lock held */
3999 exported_object_free (ExportedObject
*eo
)
4001 g_free (eo
->object_path
);
4002 g_hash_table_unref (eo
->map_if_name_to_ei
);
4011 gchar
*interface_name
;
4012 GDBusInterfaceVTable
*vtable
;
4013 GDBusInterfaceInfo
*interface_info
;
4015 GMainContext
*context
;
4017 GDestroyNotify user_data_free_func
;
4018 } ExportedInterface
;
4020 /* called with lock held */
4022 exported_interface_free (ExportedInterface
*ei
)
4024 g_dbus_interface_info_cache_release (ei
->interface_info
);
4025 g_dbus_interface_info_unref ((GDBusInterfaceInfo
*) ei
->interface_info
);
4027 call_destroy_notify (ei
->context
,
4028 ei
->user_data_free_func
,
4031 g_main_context_unref (ei
->context
);
4033 g_free (ei
->interface_name
);
4034 _g_dbus_interface_vtable_free (ei
->vtable
);
4038 /* ---------------------------------------------------------------------------------------------------- */
4040 /* Convenience function to check if @registration_id (if not zero) or
4041 * @subtree_registration_id (if not zero) has been unregistered. If
4042 * so, returns %TRUE.
4044 * May be called by any thread. Caller must *not* hold lock.
4047 has_object_been_unregistered (GDBusConnection
*connection
,
4048 guint registration_id
,
4049 guint subtree_registration_id
)
4053 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), FALSE
);
4057 CONNECTION_LOCK (connection
);
4058 if (registration_id
!= 0 && g_hash_table_lookup (connection
->map_id_to_ei
,
4059 GUINT_TO_POINTER (registration_id
)) == NULL
)
4063 else if (subtree_registration_id
!= 0 && g_hash_table_lookup (connection
->map_id_to_es
,
4064 GUINT_TO_POINTER (subtree_registration_id
)) == NULL
)
4068 CONNECTION_UNLOCK (connection
);
4073 /* ---------------------------------------------------------------------------------------------------- */
4077 GDBusConnection
*connection
;
4078 GDBusMessage
*message
;
4080 const gchar
*property_name
;
4081 const GDBusInterfaceVTable
*vtable
;
4082 GDBusInterfaceInfo
*interface_info
;
4083 const GDBusPropertyInfo
*property_info
;
4084 guint registration_id
;
4085 guint subtree_registration_id
;
4089 property_data_free (PropertyData
*data
)
4091 g_object_unref (data
->connection
);
4092 g_object_unref (data
->message
);
4096 /* called in thread where object was registered - no locks held */
4098 invoke_get_property_in_idle_cb (gpointer _data
)
4100 PropertyData
*data
= _data
;
4103 GDBusMessage
*reply
;
4105 if (has_object_been_unregistered (data
->connection
,
4106 data
->registration_id
,
4107 data
->subtree_registration_id
))
4109 reply
= g_dbus_message_new_method_error (data
->message
,
4110 "org.freedesktop.DBus.Error.UnknownMethod",
4111 _("No such interface 'org.freedesktop.DBus.Properties' on object at path %s"),
4112 g_dbus_message_get_path (data
->message
));
4113 g_dbus_connection_send_message (data
->connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
4114 g_object_unref (reply
);
4119 value
= data
->vtable
->get_property (data
->connection
,
4120 g_dbus_message_get_sender (data
->message
),
4121 g_dbus_message_get_path (data
->message
),
4122 data
->interface_info
->name
,
4123 data
->property_name
,
4130 g_assert_no_error (error
);
4132 g_variant_take_ref (value
);
4133 reply
= g_dbus_message_new_method_reply (data
->message
);
4134 g_dbus_message_set_body (reply
, g_variant_new ("(v)", value
));
4135 g_dbus_connection_send_message (data
->connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
4136 g_variant_unref (value
);
4137 g_object_unref (reply
);
4141 gchar
*dbus_error_name
;
4142 g_assert (error
!= NULL
);
4143 dbus_error_name
= g_dbus_error_encode_gerror (error
);
4144 reply
= g_dbus_message_new_method_error_literal (data
->message
,
4147 g_dbus_connection_send_message (data
->connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
4148 g_free (dbus_error_name
);
4149 g_error_free (error
);
4150 g_object_unref (reply
);
4157 /* called in thread where object was registered - no locks held */
4159 invoke_set_property_in_idle_cb (gpointer _data
)
4161 PropertyData
*data
= _data
;
4163 GDBusMessage
*reply
;
4169 g_variant_get (g_dbus_message_get_body (data
->message
),
4175 if (!data
->vtable
->set_property (data
->connection
,
4176 g_dbus_message_get_sender (data
->message
),
4177 g_dbus_message_get_path (data
->message
),
4178 data
->interface_info
->name
,
4179 data
->property_name
,
4184 gchar
*dbus_error_name
;
4185 g_assert (error
!= NULL
);
4186 dbus_error_name
= g_dbus_error_encode_gerror (error
);
4187 reply
= g_dbus_message_new_method_error_literal (data
->message
,
4190 g_free (dbus_error_name
);
4191 g_error_free (error
);
4195 reply
= g_dbus_message_new_method_reply (data
->message
);
4198 g_assert (reply
!= NULL
);
4199 g_dbus_connection_send_message (data
->connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
4200 g_object_unref (reply
);
4201 g_variant_unref (value
);
4206 /* called in any thread with connection's lock held */
4208 validate_and_maybe_schedule_property_getset (GDBusConnection
*connection
,
4209 GDBusMessage
*message
,
4210 guint registration_id
,
4211 guint subtree_registration_id
,
4213 GDBusInterfaceInfo
*interface_info
,
4214 const GDBusInterfaceVTable
*vtable
,
4215 GMainContext
*main_context
,
4219 const char *interface_name
;
4220 const char *property_name
;
4221 const GDBusPropertyInfo
*property_info
;
4222 GSource
*idle_source
;
4223 PropertyData
*property_data
;
4224 GDBusMessage
*reply
;
4229 g_variant_get (g_dbus_message_get_body (message
),
4234 g_variant_get (g_dbus_message_get_body (message
),
4243 /* Check that the property exists - if not fail with org.freedesktop.DBus.Error.InvalidArgs
4245 property_info
= NULL
;
4247 /* TODO: the cost of this is O(n) - it might be worth caching the result */
4248 property_info
= g_dbus_interface_info_lookup_property (interface_info
, property_name
);
4249 if (property_info
== NULL
)
4251 reply
= g_dbus_message_new_method_error (message
,
4252 "org.freedesktop.DBus.Error.InvalidArgs",
4253 _("No such property '%s'"),
4255 g_dbus_connection_send_message_unlocked (connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
4256 g_object_unref (reply
);
4261 if (is_get
&& !(property_info
->flags
& G_DBUS_PROPERTY_INFO_FLAGS_READABLE
))
4263 reply
= g_dbus_message_new_method_error (message
,
4264 "org.freedesktop.DBus.Error.InvalidArgs",
4265 _("Property '%s' is not readable"),
4267 g_dbus_connection_send_message_unlocked (connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
4268 g_object_unref (reply
);
4272 else if (!is_get
&& !(property_info
->flags
& G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE
))
4274 reply
= g_dbus_message_new_method_error (message
,
4275 "org.freedesktop.DBus.Error.InvalidArgs",
4276 _("Property '%s' is not writable"),
4278 g_dbus_connection_send_message_unlocked (connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
4279 g_object_unref (reply
);
4288 /* Fail with org.freedesktop.DBus.Error.InvalidArgs if the type
4289 * of the given value is wrong
4291 g_variant_get_child (g_dbus_message_get_body (message
), 2, "v", &value
);
4292 if (g_strcmp0 (g_variant_get_type_string (value
), property_info
->signature
) != 0)
4294 reply
= g_dbus_message_new_method_error (message
,
4295 "org.freedesktop.DBus.Error.InvalidArgs",
4296 _("Error setting property '%s': Expected type '%s' but got '%s'"),
4297 property_name
, property_info
->signature
,
4298 g_variant_get_type_string (value
));
4299 g_dbus_connection_send_message_unlocked (connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
4300 g_variant_unref (value
);
4301 g_object_unref (reply
);
4306 g_variant_unref (value
);
4309 /* If the vtable pointer for get_property() resp. set_property() is
4310 * NULL then dispatch the call via the method_call() handler.
4314 if (vtable
->get_property
== NULL
)
4316 schedule_method_call (connection
, message
, registration_id
, subtree_registration_id
,
4317 interface_info
, NULL
, property_info
, g_dbus_message_get_body (message
),
4318 vtable
, main_context
, user_data
);
4325 if (vtable
->set_property
== NULL
)
4327 schedule_method_call (connection
, message
, registration_id
, subtree_registration_id
,
4328 interface_info
, NULL
, property_info
, g_dbus_message_get_body (message
),
4329 vtable
, main_context
, user_data
);
4335 /* ok, got the property info - call user code in an idle handler */
4336 property_data
= g_new0 (PropertyData
, 1);
4337 property_data
->connection
= g_object_ref (connection
);
4338 property_data
->message
= g_object_ref (message
);
4339 property_data
->user_data
= user_data
;
4340 property_data
->property_name
= property_name
;
4341 property_data
->vtable
= vtable
;
4342 property_data
->interface_info
= interface_info
;
4343 property_data
->property_info
= property_info
;
4344 property_data
->registration_id
= registration_id
;
4345 property_data
->subtree_registration_id
= subtree_registration_id
;
4347 idle_source
= g_idle_source_new ();
4348 g_source_set_priority (idle_source
, G_PRIORITY_DEFAULT
);
4349 g_source_set_callback (idle_source
,
4350 is_get
? invoke_get_property_in_idle_cb
: invoke_set_property_in_idle_cb
,
4352 (GDestroyNotify
) property_data_free
);
4354 g_source_set_name (idle_source
, "[gio] invoke_get_property_in_idle_cb");
4356 g_source_set_name (idle_source
, "[gio] invoke_set_property_in_idle_cb");
4357 g_source_attach (idle_source
, main_context
);
4358 g_source_unref (idle_source
);
4366 /* called in GDBusWorker thread with connection's lock held */
4368 handle_getset_property (GDBusConnection
*connection
,
4370 GDBusMessage
*message
,
4373 ExportedInterface
*ei
;
4375 const char *interface_name
;
4376 const char *property_name
;
4381 g_variant_get (g_dbus_message_get_body (message
),
4386 g_variant_get (g_dbus_message_get_body (message
),
4392 /* Fail with org.freedesktop.DBus.Error.InvalidArgs if there is
4393 * no such interface registered
4395 ei
= g_hash_table_lookup (eo
->map_if_name_to_ei
, interface_name
);
4398 GDBusMessage
*reply
;
4399 reply
= g_dbus_message_new_method_error (message
,
4400 "org.freedesktop.DBus.Error.InvalidArgs",
4401 _("No such interface '%s'"),
4403 g_dbus_connection_send_message_unlocked (eo
->connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
4404 g_object_unref (reply
);
4409 handled
= validate_and_maybe_schedule_property_getset (eo
->connection
,
4422 /* ---------------------------------------------------------------------------------------------------- */
4426 GDBusConnection
*connection
;
4427 GDBusMessage
*message
;
4429 const GDBusInterfaceVTable
*vtable
;
4430 GDBusInterfaceInfo
*interface_info
;
4431 guint registration_id
;
4432 guint subtree_registration_id
;
4433 } PropertyGetAllData
;
4436 property_get_all_data_free (PropertyData
*data
)
4438 g_object_unref (data
->connection
);
4439 g_object_unref (data
->message
);
4443 /* called in thread where object was registered - no locks held */
4445 invoke_get_all_properties_in_idle_cb (gpointer _data
)
4447 PropertyGetAllData
*data
= _data
;
4448 GVariantBuilder builder
;
4449 GDBusMessage
*reply
;
4452 if (has_object_been_unregistered (data
->connection
,
4453 data
->registration_id
,
4454 data
->subtree_registration_id
))
4456 reply
= g_dbus_message_new_method_error (data
->message
,
4457 "org.freedesktop.DBus.Error.UnknownMethod",
4458 _("No such interface 'org.freedesktop.DBus.Properties' on object at path %s"),
4459 g_dbus_message_get_path (data
->message
));
4460 g_dbus_connection_send_message (data
->connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
4461 g_object_unref (reply
);
4465 /* TODO: Right now we never fail this call - we just omit values if
4466 * a get_property() call is failing.
4468 * We could fail the whole call if just a single get_property() call
4469 * returns an error. We need clarification in the D-Bus spec about this.
4471 g_variant_builder_init (&builder
, G_VARIANT_TYPE ("(a{sv})"));
4472 g_variant_builder_open (&builder
, G_VARIANT_TYPE ("a{sv}"));
4473 for (n
= 0; data
->interface_info
->properties
!= NULL
&& data
->interface_info
->properties
[n
] != NULL
; n
++)
4475 const GDBusPropertyInfo
*property_info
= data
->interface_info
->properties
[n
];
4478 if (!(property_info
->flags
& G_DBUS_PROPERTY_INFO_FLAGS_READABLE
))
4481 value
= data
->vtable
->get_property (data
->connection
,
4482 g_dbus_message_get_sender (data
->message
),
4483 g_dbus_message_get_path (data
->message
),
4484 data
->interface_info
->name
,
4485 property_info
->name
,
4492 g_variant_take_ref (value
);
4493 g_variant_builder_add (&builder
,
4495 property_info
->name
,
4497 g_variant_unref (value
);
4499 g_variant_builder_close (&builder
);
4501 reply
= g_dbus_message_new_method_reply (data
->message
);
4502 g_dbus_message_set_body (reply
, g_variant_builder_end (&builder
));
4503 g_dbus_connection_send_message (data
->connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
4504 g_object_unref (reply
);
4511 interface_has_readable_properties (GDBusInterfaceInfo
*interface_info
)
4515 if (!interface_info
->properties
)
4518 for (i
= 0; interface_info
->properties
[i
]; i
++)
4519 if (interface_info
->properties
[i
]->flags
& G_DBUS_PROPERTY_INFO_FLAGS_READABLE
)
4525 /* called in any thread with connection's lock held */
4527 validate_and_maybe_schedule_property_get_all (GDBusConnection
*connection
,
4528 GDBusMessage
*message
,
4529 guint registration_id
,
4530 guint subtree_registration_id
,
4531 GDBusInterfaceInfo
*interface_info
,
4532 const GDBusInterfaceVTable
*vtable
,
4533 GMainContext
*main_context
,
4537 GSource
*idle_source
;
4538 PropertyGetAllData
*property_get_all_data
;
4545 /* If the vtable pointer for get_property() is NULL but we have a
4546 * non-zero number of readable properties, then dispatch the call via
4547 * the method_call() handler.
4549 if (vtable
->get_property
== NULL
&& interface_has_readable_properties (interface_info
))
4551 schedule_method_call (connection
, message
, registration_id
, subtree_registration_id
,
4552 interface_info
, NULL
, NULL
, g_dbus_message_get_body (message
),
4553 vtable
, main_context
, user_data
);
4558 /* ok, got the property info - call user in an idle handler */
4559 property_get_all_data
= g_new0 (PropertyGetAllData
, 1);
4560 property_get_all_data
->connection
= g_object_ref (connection
);
4561 property_get_all_data
->message
= g_object_ref (message
);
4562 property_get_all_data
->user_data
= user_data
;
4563 property_get_all_data
->vtable
= vtable
;
4564 property_get_all_data
->interface_info
= interface_info
;
4565 property_get_all_data
->registration_id
= registration_id
;
4566 property_get_all_data
->subtree_registration_id
= subtree_registration_id
;
4568 idle_source
= g_idle_source_new ();
4569 g_source_set_priority (idle_source
, G_PRIORITY_DEFAULT
);
4570 g_source_set_callback (idle_source
,
4571 invoke_get_all_properties_in_idle_cb
,
4572 property_get_all_data
,
4573 (GDestroyNotify
) property_get_all_data_free
);
4574 g_source_set_name (idle_source
, "[gio] invoke_get_all_properties_in_idle_cb");
4575 g_source_attach (idle_source
, main_context
);
4576 g_source_unref (idle_source
);
4584 /* called in GDBusWorker thread with connection's lock held */
4586 handle_get_all_properties (GDBusConnection
*connection
,
4588 GDBusMessage
*message
)
4590 ExportedInterface
*ei
;
4592 const char *interface_name
;
4596 g_variant_get (g_dbus_message_get_body (message
),
4600 /* Fail with org.freedesktop.DBus.Error.InvalidArgs if there is
4601 * no such interface registered
4603 ei
= g_hash_table_lookup (eo
->map_if_name_to_ei
, interface_name
);
4606 GDBusMessage
*reply
;
4607 reply
= g_dbus_message_new_method_error (message
,
4608 "org.freedesktop.DBus.Error.InvalidArgs",
4609 _("No such interface"),
4611 g_dbus_connection_send_message_unlocked (eo
->connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
4612 g_object_unref (reply
);
4617 handled
= validate_and_maybe_schedule_property_get_all (eo
->connection
,
4629 /* ---------------------------------------------------------------------------------------------------- */
4631 static const gchar introspect_header
[] =
4632 "<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\"\n"
4633 " \"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n"
4634 "<!-- GDBus " PACKAGE_VERSION
" -->\n"
4637 static const gchar introspect_tail
[] =
4640 static const gchar introspect_properties_interface
[] =
4641 " <interface name=\"org.freedesktop.DBus.Properties\">\n"
4642 " <method name=\"Get\">\n"
4643 " <arg type=\"s\" name=\"interface_name\" direction=\"in\"/>\n"
4644 " <arg type=\"s\" name=\"property_name\" direction=\"in\"/>\n"
4645 " <arg type=\"v\" name=\"value\" direction=\"out\"/>\n"
4647 " <method name=\"GetAll\">\n"
4648 " <arg type=\"s\" name=\"interface_name\" direction=\"in\"/>\n"
4649 " <arg type=\"a{sv}\" name=\"properties\" direction=\"out\"/>\n"
4651 " <method name=\"Set\">\n"
4652 " <arg type=\"s\" name=\"interface_name\" direction=\"in\"/>\n"
4653 " <arg type=\"s\" name=\"property_name\" direction=\"in\"/>\n"
4654 " <arg type=\"v\" name=\"value\" direction=\"in\"/>\n"
4656 " <signal name=\"PropertiesChanged\">\n"
4657 " <arg type=\"s\" name=\"interface_name\"/>\n"
4658 " <arg type=\"a{sv}\" name=\"changed_properties\"/>\n"
4659 " <arg type=\"as\" name=\"invalidated_properties\"/>\n"
4663 static const gchar introspect_introspectable_interface
[] =
4664 " <interface name=\"org.freedesktop.DBus.Introspectable\">\n"
4665 " <method name=\"Introspect\">\n"
4666 " <arg type=\"s\" name=\"xml_data\" direction=\"out\"/>\n"
4669 " <interface name=\"org.freedesktop.DBus.Peer\">\n"
4670 " <method name=\"Ping\"/>\n"
4671 " <method name=\"GetMachineId\">\n"
4672 " <arg type=\"s\" name=\"machine_uuid\" direction=\"out\"/>\n"
4677 introspect_append_header (GString
*s
)
4679 g_string_append (s
, introspect_header
);
4683 maybe_add_path (const gchar
*path
, gsize path_len
, const gchar
*object_path
, GHashTable
*set
)
4685 if (g_str_has_prefix (object_path
, path
) && strlen (object_path
) > path_len
&& object_path
[path_len
-1] == '/')
4691 begin
= object_path
+ path_len
;
4692 end
= strchr (begin
, '/');
4694 s
= g_strndup (begin
, end
- begin
);
4696 s
= g_strdup (begin
);
4698 if (g_hash_table_lookup (set
, s
) == NULL
)
4699 g_hash_table_insert (set
, s
, GUINT_TO_POINTER (1));
4705 /* TODO: we want a nicer public interface for this */
4706 /* called in any thread with connection's lock held */
4708 g_dbus_connection_list_registered_unlocked (GDBusConnection
*connection
,
4713 GHashTableIter hash_iter
;
4714 const gchar
*object_path
;
4720 CONNECTION_ENSURE_LOCK (connection
);
4722 path_len
= strlen (path
);
4726 set
= g_hash_table_new (g_str_hash
, g_str_equal
);
4728 g_hash_table_iter_init (&hash_iter
, connection
->map_object_path_to_eo
);
4729 while (g_hash_table_iter_next (&hash_iter
, (gpointer
) &object_path
, NULL
))
4730 maybe_add_path (path
, path_len
, object_path
, set
);
4732 g_hash_table_iter_init (&hash_iter
, connection
->map_object_path_to_es
);
4733 while (g_hash_table_iter_next (&hash_iter
, (gpointer
) &object_path
, NULL
))
4734 maybe_add_path (path
, path_len
, object_path
, set
);
4736 p
= g_ptr_array_new ();
4737 keys
= g_hash_table_get_keys (set
);
4738 for (l
= keys
; l
!= NULL
; l
= l
->next
)
4739 g_ptr_array_add (p
, l
->data
);
4740 g_hash_table_unref (set
);
4743 g_ptr_array_add (p
, NULL
);
4744 ret
= (gchar
**) g_ptr_array_free (p
, FALSE
);
4748 /* called in any thread with connection's lock not held */
4750 g_dbus_connection_list_registered (GDBusConnection
*connection
,
4754 CONNECTION_LOCK (connection
);
4755 ret
= g_dbus_connection_list_registered_unlocked (connection
, path
);
4756 CONNECTION_UNLOCK (connection
);
4760 /* called in GDBusWorker thread with connection's lock held */
4762 handle_introspect (GDBusConnection
*connection
,
4764 GDBusMessage
*message
)
4768 GDBusMessage
*reply
;
4769 GHashTableIter hash_iter
;
4770 ExportedInterface
*ei
;
4773 /* first the header with the standard interfaces */
4774 s
= g_string_sized_new (sizeof (introspect_header
) +
4775 sizeof (introspect_properties_interface
) +
4776 sizeof (introspect_introspectable_interface
) +
4777 sizeof (introspect_tail
));
4778 introspect_append_header (s
);
4779 if (!g_hash_table_lookup (eo
->map_if_name_to_ei
,
4780 "org.freedesktop.DBus.Properties"))
4781 g_string_append (s
, introspect_properties_interface
);
4783 if (!g_hash_table_lookup (eo
->map_if_name_to_ei
,
4784 "org.freedesktop.DBus.Introspectable"))
4785 g_string_append (s
, introspect_introspectable_interface
);
4787 /* then include the registered interfaces */
4788 g_hash_table_iter_init (&hash_iter
, eo
->map_if_name_to_ei
);
4789 while (g_hash_table_iter_next (&hash_iter
, NULL
, (gpointer
) &ei
))
4790 g_dbus_interface_info_generate_xml (ei
->interface_info
, 2, s
);
4792 /* finally include nodes registered below us */
4793 registered
= g_dbus_connection_list_registered_unlocked (connection
, eo
->object_path
);
4794 for (n
= 0; registered
!= NULL
&& registered
[n
] != NULL
; n
++)
4795 g_string_append_printf (s
, " <node name=\"%s\"/>\n", registered
[n
]);
4796 g_strfreev (registered
);
4797 g_string_append (s
, introspect_tail
);
4799 reply
= g_dbus_message_new_method_reply (message
);
4800 g_dbus_message_set_body (reply
, g_variant_new ("(s)", s
->str
));
4801 g_dbus_connection_send_message_unlocked (connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
4802 g_object_unref (reply
);
4803 g_string_free (s
, TRUE
);
4808 /* called in thread where object was registered - no locks held */
4810 call_in_idle_cb (gpointer user_data
)
4812 GDBusMethodInvocation
*invocation
= G_DBUS_METHOD_INVOCATION (user_data
);
4813 GDBusInterfaceVTable
*vtable
;
4814 guint registration_id
;
4815 guint subtree_registration_id
;
4817 registration_id
= GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (invocation
), "g-dbus-registration-id"));
4818 subtree_registration_id
= GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (invocation
), "g-dbus-subtree-registration-id"));
4820 if (has_object_been_unregistered (g_dbus_method_invocation_get_connection (invocation
),
4822 subtree_registration_id
))
4824 GDBusMessage
*reply
;
4825 reply
= g_dbus_message_new_method_error (g_dbus_method_invocation_get_message (invocation
),
4826 "org.freedesktop.DBus.Error.UnknownMethod",
4827 _("No such interface '%s' on object at path %s"),
4828 g_dbus_method_invocation_get_interface_name (invocation
),
4829 g_dbus_method_invocation_get_object_path (invocation
));
4830 g_dbus_connection_send_message (g_dbus_method_invocation_get_connection (invocation
), reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
4831 g_object_unref (reply
);
4835 vtable
= g_object_get_data (G_OBJECT (invocation
), "g-dbus-interface-vtable");
4836 g_assert (vtable
!= NULL
&& vtable
->method_call
!= NULL
);
4838 vtable
->method_call (g_dbus_method_invocation_get_connection (invocation
),
4839 g_dbus_method_invocation_get_sender (invocation
),
4840 g_dbus_method_invocation_get_object_path (invocation
),
4841 g_dbus_method_invocation_get_interface_name (invocation
),
4842 g_dbus_method_invocation_get_method_name (invocation
),
4843 g_dbus_method_invocation_get_parameters (invocation
),
4844 g_object_ref (invocation
),
4845 g_dbus_method_invocation_get_user_data (invocation
));
4851 /* called in GDBusWorker thread with connection's lock held */
4853 schedule_method_call (GDBusConnection
*connection
,
4854 GDBusMessage
*message
,
4855 guint registration_id
,
4856 guint subtree_registration_id
,
4857 const GDBusInterfaceInfo
*interface_info
,
4858 const GDBusMethodInfo
*method_info
,
4859 const GDBusPropertyInfo
*property_info
,
4860 GVariant
*parameters
,
4861 const GDBusInterfaceVTable
*vtable
,
4862 GMainContext
*main_context
,
4865 GDBusMethodInvocation
*invocation
;
4866 GSource
*idle_source
;
4868 invocation
= _g_dbus_method_invocation_new (g_dbus_message_get_sender (message
),
4869 g_dbus_message_get_path (message
),
4870 g_dbus_message_get_interface (message
),
4871 g_dbus_message_get_member (message
),
4879 /* TODO: would be nicer with a real MethodData like we already
4880 * have PropertyData and PropertyGetAllData... */
4881 g_object_set_data (G_OBJECT (invocation
), "g-dbus-interface-vtable", (gpointer
) vtable
);
4882 g_object_set_data (G_OBJECT (invocation
), "g-dbus-registration-id", GUINT_TO_POINTER (registration_id
));
4883 g_object_set_data (G_OBJECT (invocation
), "g-dbus-subtree-registration-id", GUINT_TO_POINTER (subtree_registration_id
));
4885 idle_source
= g_idle_source_new ();
4886 g_source_set_priority (idle_source
, G_PRIORITY_DEFAULT
);
4887 g_source_set_callback (idle_source
,
4891 g_source_set_name (idle_source
, "[gio] call_in_idle_cb");
4892 g_source_attach (idle_source
, main_context
);
4893 g_source_unref (idle_source
);
4896 /* called in GDBusWorker thread with connection's lock held */
4898 validate_and_maybe_schedule_method_call (GDBusConnection
*connection
,
4899 GDBusMessage
*message
,
4900 guint registration_id
,
4901 guint subtree_registration_id
,
4902 GDBusInterfaceInfo
*interface_info
,
4903 const GDBusInterfaceVTable
*vtable
,
4904 GMainContext
*main_context
,
4907 GDBusMethodInfo
*method_info
;
4908 GDBusMessage
*reply
;
4909 GVariant
*parameters
;
4911 GVariantType
*in_type
;
4915 /* TODO: the cost of this is O(n) - it might be worth caching the result */
4916 method_info
= g_dbus_interface_info_lookup_method (interface_info
, g_dbus_message_get_member (message
));
4918 /* if the method doesn't exist, return the org.freedesktop.DBus.Error.UnknownMethod
4919 * error to the caller
4921 if (method_info
== NULL
)
4923 reply
= g_dbus_message_new_method_error (message
,
4924 "org.freedesktop.DBus.Error.UnknownMethod",
4925 _("No such method '%s'"),
4926 g_dbus_message_get_member (message
));
4927 g_dbus_connection_send_message_unlocked (connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
4928 g_object_unref (reply
);
4933 parameters
= g_dbus_message_get_body (message
);
4934 if (parameters
== NULL
)
4936 parameters
= g_variant_new ("()");
4937 g_variant_ref_sink (parameters
);
4941 g_variant_ref (parameters
);
4944 /* Check that the incoming args are of the right type - if they are not, return
4945 * the org.freedesktop.DBus.Error.InvalidArgs error to the caller
4947 in_type
= _g_dbus_compute_complete_signature (method_info
->in_args
);
4948 if (!g_variant_is_of_type (parameters
, in_type
))
4952 type_string
= g_variant_type_dup_string (in_type
);
4954 reply
= g_dbus_message_new_method_error (message
,
4955 "org.freedesktop.DBus.Error.InvalidArgs",
4956 _("Type of message, '%s', does not match expected type '%s'"),
4957 g_variant_get_type_string (parameters
),
4959 g_dbus_connection_send_message_unlocked (connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
4960 g_variant_type_free (in_type
);
4961 g_variant_unref (parameters
);
4962 g_object_unref (reply
);
4963 g_free (type_string
);
4967 g_variant_type_free (in_type
);
4969 /* schedule the call in idle */
4970 schedule_method_call (connection
, message
, registration_id
, subtree_registration_id
,
4971 interface_info
, method_info
, NULL
, parameters
,
4972 vtable
, main_context
, user_data
);
4973 g_variant_unref (parameters
);
4980 /* ---------------------------------------------------------------------------------------------------- */
4982 /* called in GDBusWorker thread with connection's lock held */
4984 obj_message_func (GDBusConnection
*connection
,
4986 GDBusMessage
*message
)
4988 const gchar
*interface_name
;
4989 const gchar
*member
;
4990 const gchar
*signature
;
4995 interface_name
= g_dbus_message_get_interface (message
);
4996 member
= g_dbus_message_get_member (message
);
4997 signature
= g_dbus_message_get_signature (message
);
4999 /* see if we have an interface for handling this call */
5000 if (interface_name
!= NULL
)
5002 ExportedInterface
*ei
;
5003 ei
= g_hash_table_lookup (eo
->map_if_name_to_ei
, interface_name
);
5006 /* we do - invoke the handler in idle in the right thread */
5008 /* handle no vtable or handler being present */
5009 if (ei
->vtable
== NULL
|| ei
->vtable
->method_call
== NULL
)
5012 handled
= validate_and_maybe_schedule_method_call (connection
,
5024 if (g_strcmp0 (interface_name
, "org.freedesktop.DBus.Introspectable") == 0 &&
5025 g_strcmp0 (member
, "Introspect") == 0 &&
5026 g_strcmp0 (signature
, "") == 0)
5028 handled
= handle_introspect (connection
, eo
, message
);
5031 else if (g_strcmp0 (interface_name
, "org.freedesktop.DBus.Properties") == 0 &&
5032 g_strcmp0 (member
, "Get") == 0 &&
5033 g_strcmp0 (signature
, "ss") == 0)
5035 handled
= handle_getset_property (connection
, eo
, message
, TRUE
);
5038 else if (g_strcmp0 (interface_name
, "org.freedesktop.DBus.Properties") == 0 &&
5039 g_strcmp0 (member
, "Set") == 0 &&
5040 g_strcmp0 (signature
, "ssv") == 0)
5042 handled
= handle_getset_property (connection
, eo
, message
, FALSE
);
5045 else if (g_strcmp0 (interface_name
, "org.freedesktop.DBus.Properties") == 0 &&
5046 g_strcmp0 (member
, "GetAll") == 0 &&
5047 g_strcmp0 (signature
, "s") == 0)
5049 handled
= handle_get_all_properties (connection
, eo
, message
);
5058 * g_dbus_connection_register_object:
5059 * @connection: a #GDBusConnection
5060 * @object_path: the object path to register at
5061 * @interface_info: introspection data for the interface
5062 * @vtable: (nullable): a #GDBusInterfaceVTable to call into or %NULL
5063 * @user_data: (nullable): data to pass to functions in @vtable
5064 * @user_data_free_func: function to call when the object path is unregistered
5065 * @error: return location for error or %NULL
5067 * Registers callbacks for exported objects at @object_path with the
5068 * D-Bus interface that is described in @interface_info.
5070 * Calls to functions in @vtable (and @user_data_free_func) will happen
5072 * [thread-default main context][g-main-context-push-thread-default]
5073 * of the thread you are calling this method from.
5075 * Note that all #GVariant values passed to functions in @vtable will match
5076 * the signature given in @interface_info - if a remote caller passes
5077 * incorrect values, the `org.freedesktop.DBus.Error.InvalidArgs`
5078 * is returned to the remote caller.
5080 * Additionally, if the remote caller attempts to invoke methods or
5081 * access properties not mentioned in @interface_info the
5082 * `org.freedesktop.DBus.Error.UnknownMethod` resp.
5083 * `org.freedesktop.DBus.Error.InvalidArgs` errors
5084 * are returned to the caller.
5086 * It is considered a programming error if the
5087 * #GDBusInterfaceGetPropertyFunc function in @vtable returns a
5088 * #GVariant of incorrect type.
5090 * If an existing callback is already registered at @object_path and
5091 * @interface_name, then @error is set to #G_IO_ERROR_EXISTS.
5093 * GDBus automatically implements the standard D-Bus interfaces
5094 * org.freedesktop.DBus.Properties, org.freedesktop.DBus.Introspectable
5095 * and org.freedesktop.Peer, so you don't have to implement those for the
5096 * objects you export. You can implement org.freedesktop.DBus.Properties
5097 * yourself, e.g. to handle getting and setting of properties asynchronously.
5099 * Note that the reference count on @interface_info will be
5100 * incremented by 1 (unless allocated statically, e.g. if the
5101 * reference count is -1, see g_dbus_interface_info_ref()) for as long
5102 * as the object is exported. Also note that @vtable will be copied.
5104 * See this [server][gdbus-server] for an example of how to use this method.
5106 * Returns: 0 if @error is set, otherwise a registration id (never 0)
5107 * that can be used with g_dbus_connection_unregister_object()
5112 g_dbus_connection_register_object (GDBusConnection
*connection
,
5113 const gchar
*object_path
,
5114 GDBusInterfaceInfo
*interface_info
,
5115 const GDBusInterfaceVTable
*vtable
,
5117 GDestroyNotify user_data_free_func
,
5121 ExportedInterface
*ei
;
5124 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), 0);
5125 g_return_val_if_fail (object_path
!= NULL
&& g_variant_is_object_path (object_path
), 0);
5126 g_return_val_if_fail (interface_info
!= NULL
, 0);
5127 g_return_val_if_fail (g_dbus_is_interface_name (interface_info
->name
), 0);
5128 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, 0);
5129 g_return_val_if_fail (check_initialized (connection
), 0);
5133 CONNECTION_LOCK (connection
);
5135 eo
= g_hash_table_lookup (connection
->map_object_path_to_eo
, object_path
);
5138 eo
= g_new0 (ExportedObject
, 1);
5139 eo
->object_path
= g_strdup (object_path
);
5140 eo
->connection
= connection
;
5141 eo
->map_if_name_to_ei
= g_hash_table_new_full (g_str_hash
,
5144 (GDestroyNotify
) exported_interface_free
);
5145 g_hash_table_insert (connection
->map_object_path_to_eo
, eo
->object_path
, eo
);
5148 ei
= g_hash_table_lookup (eo
->map_if_name_to_ei
, interface_info
->name
);
5154 _("An object is already exported for the interface %s at %s"),
5155 interface_info
->name
,
5160 ei
= g_new0 (ExportedInterface
, 1);
5161 ei
->id
= g_atomic_int_add (&_global_registration_id
, 1); /* TODO: overflow etc. */
5163 ei
->user_data
= user_data
;
5164 ei
->user_data_free_func
= user_data_free_func
;
5165 ei
->vtable
= _g_dbus_interface_vtable_copy (vtable
);
5166 ei
->interface_info
= g_dbus_interface_info_ref (interface_info
);
5167 g_dbus_interface_info_cache_build (ei
->interface_info
);
5168 ei
->interface_name
= g_strdup (interface_info
->name
);
5169 ei
->context
= g_main_context_ref_thread_default ();
5171 g_hash_table_insert (eo
->map_if_name_to_ei
,
5172 (gpointer
) ei
->interface_name
,
5174 g_hash_table_insert (connection
->map_id_to_ei
,
5175 GUINT_TO_POINTER (ei
->id
),
5181 CONNECTION_UNLOCK (connection
);
5187 * g_dbus_connection_unregister_object:
5188 * @connection: a #GDBusConnection
5189 * @registration_id: a registration id obtained from
5190 * g_dbus_connection_register_object()
5192 * Unregisters an object.
5194 * Returns: %TRUE if the object was unregistered, %FALSE otherwise
5199 g_dbus_connection_unregister_object (GDBusConnection
*connection
,
5200 guint registration_id
)
5202 ExportedInterface
*ei
;
5206 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), FALSE
);
5207 g_return_val_if_fail (check_initialized (connection
), FALSE
);
5211 CONNECTION_LOCK (connection
);
5213 ei
= g_hash_table_lookup (connection
->map_id_to_ei
,
5214 GUINT_TO_POINTER (registration_id
));
5220 g_warn_if_fail (g_hash_table_remove (connection
->map_id_to_ei
, GUINT_TO_POINTER (ei
->id
)));
5221 g_warn_if_fail (g_hash_table_remove (eo
->map_if_name_to_ei
, ei
->interface_name
));
5222 /* unregister object path if we have no more exported interfaces */
5223 if (g_hash_table_size (eo
->map_if_name_to_ei
) == 0)
5224 g_warn_if_fail (g_hash_table_remove (connection
->map_object_path_to_eo
,
5230 CONNECTION_UNLOCK (connection
);
5236 GClosure
*method_call_closure
;
5237 GClosure
*get_property_closure
;
5238 GClosure
*set_property_closure
;
5239 } RegisterObjectData
;
5241 static RegisterObjectData
*
5242 register_object_data_new (GClosure
*method_call_closure
,
5243 GClosure
*get_property_closure
,
5244 GClosure
*set_property_closure
)
5246 RegisterObjectData
*data
;
5248 data
= g_new0 (RegisterObjectData
, 1);
5250 if (method_call_closure
!= NULL
)
5252 data
->method_call_closure
= g_closure_ref (method_call_closure
);
5253 g_closure_sink (method_call_closure
);
5254 if (G_CLOSURE_NEEDS_MARSHAL (method_call_closure
))
5255 g_closure_set_marshal (method_call_closure
, g_cclosure_marshal_generic
);
5258 if (get_property_closure
!= NULL
)
5260 data
->get_property_closure
= g_closure_ref (get_property_closure
);
5261 g_closure_sink (get_property_closure
);
5262 if (G_CLOSURE_NEEDS_MARSHAL (get_property_closure
))
5263 g_closure_set_marshal (get_property_closure
, g_cclosure_marshal_generic
);
5266 if (set_property_closure
!= NULL
)
5268 data
->set_property_closure
= g_closure_ref (set_property_closure
);
5269 g_closure_sink (set_property_closure
);
5270 if (G_CLOSURE_NEEDS_MARSHAL (set_property_closure
))
5271 g_closure_set_marshal (set_property_closure
, g_cclosure_marshal_generic
);
5278 register_object_free_func (gpointer user_data
)
5280 RegisterObjectData
*data
= user_data
;
5282 g_clear_pointer (&data
->method_call_closure
, g_closure_unref
);
5283 g_clear_pointer (&data
->get_property_closure
, g_closure_unref
);
5284 g_clear_pointer (&data
->set_property_closure
, g_closure_unref
);
5290 register_with_closures_on_method_call (GDBusConnection
*connection
,
5291 const gchar
*sender
,
5292 const gchar
*object_path
,
5293 const gchar
*interface_name
,
5294 const gchar
*method_name
,
5295 GVariant
*parameters
,
5296 GDBusMethodInvocation
*invocation
,
5299 RegisterObjectData
*data
= user_data
;
5300 GValue params
[] = { G_VALUE_INIT
, G_VALUE_INIT
, G_VALUE_INIT
, G_VALUE_INIT
, G_VALUE_INIT
, G_VALUE_INIT
, G_VALUE_INIT
};
5302 g_value_init (¶ms
[0], G_TYPE_DBUS_CONNECTION
);
5303 g_value_set_object (¶ms
[0], connection
);
5305 g_value_init (¶ms
[1], G_TYPE_STRING
);
5306 g_value_set_string (¶ms
[1], sender
);
5308 g_value_init (¶ms
[2], G_TYPE_STRING
);
5309 g_value_set_string (¶ms
[2], object_path
);
5311 g_value_init (¶ms
[3], G_TYPE_STRING
);
5312 g_value_set_string (¶ms
[3], interface_name
);
5314 g_value_init (¶ms
[4], G_TYPE_STRING
);
5315 g_value_set_string (¶ms
[4], method_name
);
5317 g_value_init (¶ms
[5], G_TYPE_VARIANT
);
5318 g_value_set_variant (¶ms
[5], parameters
);
5320 g_value_init (¶ms
[6], G_TYPE_DBUS_METHOD_INVOCATION
);
5321 g_value_set_object (¶ms
[6], invocation
);
5323 g_closure_invoke (data
->method_call_closure
, NULL
, G_N_ELEMENTS (params
), params
, NULL
);
5325 g_value_unset (params
+ 0);
5326 g_value_unset (params
+ 1);
5327 g_value_unset (params
+ 2);
5328 g_value_unset (params
+ 3);
5329 g_value_unset (params
+ 4);
5330 g_value_unset (params
+ 5);
5331 g_value_unset (params
+ 6);
5335 register_with_closures_on_get_property (GDBusConnection
*connection
,
5336 const gchar
*sender
,
5337 const gchar
*object_path
,
5338 const gchar
*interface_name
,
5339 const gchar
*property_name
,
5343 RegisterObjectData
*data
= user_data
;
5344 GValue params
[] = { G_VALUE_INIT
, G_VALUE_INIT
, G_VALUE_INIT
, G_VALUE_INIT
, G_VALUE_INIT
};
5345 GValue result_value
= G_VALUE_INIT
;
5348 g_value_init (¶ms
[0], G_TYPE_DBUS_CONNECTION
);
5349 g_value_set_object (¶ms
[0], connection
);
5351 g_value_init (¶ms
[1], G_TYPE_STRING
);
5352 g_value_set_string (¶ms
[1], sender
);
5354 g_value_init (¶ms
[2], G_TYPE_STRING
);
5355 g_value_set_string (¶ms
[2], object_path
);
5357 g_value_init (¶ms
[3], G_TYPE_STRING
);
5358 g_value_set_string (¶ms
[3], interface_name
);
5360 g_value_init (¶ms
[4], G_TYPE_STRING
);
5361 g_value_set_string (¶ms
[4], property_name
);
5363 g_value_init (&result_value
, G_TYPE_VARIANT
);
5365 g_closure_invoke (data
->get_property_closure
, &result_value
, G_N_ELEMENTS (params
), params
, NULL
);
5367 result
= g_value_get_variant (&result_value
);
5369 g_variant_ref (result
);
5371 g_value_unset (params
+ 0);
5372 g_value_unset (params
+ 1);
5373 g_value_unset (params
+ 2);
5374 g_value_unset (params
+ 3);
5375 g_value_unset (params
+ 4);
5376 g_value_unset (&result_value
);
5379 g_set_error (error
, G_DBUS_ERROR
, G_DBUS_ERROR_FAILED
,
5380 _("Unable to retrieve property %s.%s"),
5381 interface_name
, property_name
);
5387 register_with_closures_on_set_property (GDBusConnection
*connection
,
5388 const gchar
*sender
,
5389 const gchar
*object_path
,
5390 const gchar
*interface_name
,
5391 const gchar
*property_name
,
5396 RegisterObjectData
*data
= user_data
;
5397 GValue params
[] = { G_VALUE_INIT
, G_VALUE_INIT
, G_VALUE_INIT
, G_VALUE_INIT
, G_VALUE_INIT
, G_VALUE_INIT
};
5398 GValue result_value
= G_VALUE_INIT
;
5401 g_value_init (¶ms
[0], G_TYPE_DBUS_CONNECTION
);
5402 g_value_set_object (¶ms
[0], connection
);
5404 g_value_init (¶ms
[1], G_TYPE_STRING
);
5405 g_value_set_string (¶ms
[1], sender
);
5407 g_value_init (¶ms
[2], G_TYPE_STRING
);
5408 g_value_set_string (¶ms
[2], object_path
);
5410 g_value_init (¶ms
[3], G_TYPE_STRING
);
5411 g_value_set_string (¶ms
[3], interface_name
);
5413 g_value_init (¶ms
[4], G_TYPE_STRING
);
5414 g_value_set_string (¶ms
[4], property_name
);
5416 g_value_init (¶ms
[5], G_TYPE_VARIANT
);
5417 g_value_set_variant (¶ms
[5], value
);
5419 g_value_init (&result_value
, G_TYPE_BOOLEAN
);
5421 g_closure_invoke (data
->set_property_closure
, &result_value
, G_N_ELEMENTS (params
), params
, NULL
);
5423 result
= g_value_get_boolean (&result_value
);
5425 g_value_unset (params
+ 0);
5426 g_value_unset (params
+ 1);
5427 g_value_unset (params
+ 2);
5428 g_value_unset (params
+ 3);
5429 g_value_unset (params
+ 4);
5430 g_value_unset (params
+ 5);
5431 g_value_unset (&result_value
);
5435 G_DBUS_ERROR
, G_DBUS_ERROR_FAILED
,
5436 _("Unable to set property %s.%s"),
5437 interface_name
, property_name
);
5443 * g_dbus_connection_register_object_with_closures: (rename-to g_dbus_connection_register_object)
5444 * @connection: A #GDBusConnection.
5445 * @object_path: The object path to register at.
5446 * @interface_info: Introspection data for the interface.
5447 * @method_call_closure: (nullable): #GClosure for handling incoming method calls.
5448 * @get_property_closure: (nullable): #GClosure for getting a property.
5449 * @set_property_closure: (nullable): #GClosure for setting a property.
5450 * @error: Return location for error or %NULL.
5452 * Version of g_dbus_connection_register_object() using closures instead of a
5453 * #GDBusInterfaceVTable for easier binding in other languages.
5455 * Returns: 0 if @error is set, otherwise a registration id (never 0)
5456 * that can be used with g_dbus_connection_unregister_object() .
5461 g_dbus_connection_register_object_with_closures (GDBusConnection
*connection
,
5462 const gchar
*object_path
,
5463 GDBusInterfaceInfo
*interface_info
,
5464 GClosure
*method_call_closure
,
5465 GClosure
*get_property_closure
,
5466 GClosure
*set_property_closure
,
5469 RegisterObjectData
*data
;
5470 GDBusInterfaceVTable vtable
=
5472 method_call_closure
!= NULL
? register_with_closures_on_method_call
: NULL
,
5473 get_property_closure
!= NULL
? register_with_closures_on_get_property
: NULL
,
5474 set_property_closure
!= NULL
? register_with_closures_on_set_property
: NULL
5477 data
= register_object_data_new (method_call_closure
, get_property_closure
, set_property_closure
);
5479 return g_dbus_connection_register_object (connection
,
5484 register_object_free_func
,
5488 /* ---------------------------------------------------------------------------------------------------- */
5491 * g_dbus_connection_emit_signal:
5492 * @connection: a #GDBusConnection
5493 * @destination_bus_name: (nullable): the unique bus name for the destination
5494 * for the signal or %NULL to emit to all listeners
5495 * @object_path: path of remote object
5496 * @interface_name: D-Bus interface to emit a signal on
5497 * @signal_name: the name of the signal to emit
5498 * @parameters: (nullable): a #GVariant tuple with parameters for the signal
5499 * or %NULL if not passing parameters
5500 * @error: Return location for error or %NULL
5504 * If the parameters GVariant is floating, it is consumed.
5506 * This can only fail if @parameters is not compatible with the D-Bus protocol.
5508 * Returns: %TRUE unless @error is set
5513 g_dbus_connection_emit_signal (GDBusConnection
*connection
,
5514 const gchar
*destination_bus_name
,
5515 const gchar
*object_path
,
5516 const gchar
*interface_name
,
5517 const gchar
*signal_name
,
5518 GVariant
*parameters
,
5521 GDBusMessage
*message
;
5527 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), FALSE
);
5528 g_return_val_if_fail (destination_bus_name
== NULL
|| g_dbus_is_name (destination_bus_name
), FALSE
);
5529 g_return_val_if_fail (object_path
!= NULL
&& g_variant_is_object_path (object_path
), FALSE
);
5530 g_return_val_if_fail (interface_name
!= NULL
&& g_dbus_is_interface_name (interface_name
), FALSE
);
5531 g_return_val_if_fail (signal_name
!= NULL
&& g_dbus_is_member_name (signal_name
), FALSE
);
5532 g_return_val_if_fail (parameters
== NULL
|| g_variant_is_of_type (parameters
, G_VARIANT_TYPE_TUPLE
), FALSE
);
5533 g_return_val_if_fail (check_initialized (connection
), FALSE
);
5535 if (G_UNLIKELY (_g_dbus_debug_emission ()))
5537 _g_dbus_debug_print_lock ();
5538 g_print ("========================================================================\n"
5539 "GDBus-debug:Emission:\n"
5540 " >>>> SIGNAL EMISSION %s.%s()\n"
5542 " destination %s\n",
5543 interface_name
, signal_name
,
5545 destination_bus_name
!= NULL
? destination_bus_name
: "(none)");
5546 _g_dbus_debug_print_unlock ();
5549 message
= g_dbus_message_new_signal (object_path
,
5553 if (destination_bus_name
!= NULL
)
5554 g_dbus_message_set_header (message
,
5555 G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION
,
5556 g_variant_new_string (destination_bus_name
));
5558 if (parameters
!= NULL
)
5559 g_dbus_message_set_body (message
, parameters
);
5561 ret
= g_dbus_connection_send_message (connection
, message
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, error
);
5562 g_object_unref (message
);
5568 add_call_flags (GDBusMessage
*message
,
5569 GDBusCallFlags flags
)
5571 GDBusMessageFlags msg_flags
= 0;
5573 if (flags
& G_DBUS_CALL_FLAGS_NO_AUTO_START
)
5574 msg_flags
|= G_DBUS_MESSAGE_FLAGS_NO_AUTO_START
;
5575 if (flags
& G_DBUS_CALL_FLAGS_ALLOW_INTERACTIVE_AUTHORIZATION
)
5576 msg_flags
|= G_DBUS_MESSAGE_FLAGS_ALLOW_INTERACTIVE_AUTHORIZATION
;
5578 g_dbus_message_set_flags (message
, msg_flags
);
5582 decode_method_reply (GDBusMessage
*reply
,
5583 const gchar
*method_name
,
5584 const GVariantType
*reply_type
,
5585 GUnixFDList
**out_fd_list
,
5591 switch (g_dbus_message_get_message_type (reply
))
5593 case G_DBUS_MESSAGE_TYPE_METHOD_RETURN
:
5594 result
= g_dbus_message_get_body (reply
);
5597 result
= g_variant_new ("()");
5598 g_variant_ref_sink (result
);
5602 g_variant_ref (result
);
5605 if (!g_variant_is_of_type (result
, reply_type
))
5607 gchar
*type_string
= g_variant_type_dup_string (reply_type
);
5611 G_IO_ERROR_INVALID_ARGUMENT
,
5612 _("Method '%s' returned type '%s', but expected '%s'"),
5613 method_name
, g_variant_get_type_string (result
), type_string
);
5615 g_variant_unref (result
);
5616 g_free (type_string
);
5623 if (out_fd_list
!= NULL
)
5625 *out_fd_list
= g_dbus_message_get_unix_fd_list (reply
);
5626 if (*out_fd_list
!= NULL
)
5627 g_object_ref (*out_fd_list
);
5633 case G_DBUS_MESSAGE_TYPE_ERROR
:
5634 g_dbus_message_to_gerror (reply
, error
);
5638 g_assert_not_reached ();
5648 GVariantType
*reply_type
;
5649 gchar
*method_name
; /* for error message */
5652 GUnixFDList
*fd_list
;
5656 call_state_free (CallState
*state
)
5658 g_variant_type_free (state
->reply_type
);
5659 g_free (state
->method_name
);
5661 if (state
->fd_list
!= NULL
)
5662 g_object_unref (state
->fd_list
);
5663 g_slice_free (CallState
, state
);
5666 /* called in any thread, with the connection's lock not held */
5668 g_dbus_connection_call_done (GObject
*source
,
5669 GAsyncResult
*result
,
5672 GDBusConnection
*connection
= G_DBUS_CONNECTION (source
);
5673 GTask
*task
= user_data
;
5674 CallState
*state
= g_task_get_task_data (task
);
5675 GError
*error
= NULL
;
5676 GDBusMessage
*reply
;
5677 GVariant
*value
= NULL
;
5679 reply
= g_dbus_connection_send_message_with_reply_finish (connection
,
5683 if (G_UNLIKELY (_g_dbus_debug_call ()))
5685 _g_dbus_debug_print_lock ();
5686 g_print ("========================================================================\n"
5687 "GDBus-debug:Call:\n"
5688 " <<<< ASYNC COMPLETE %s() (serial %d)\n"
5694 g_print ("SUCCESS\n");
5698 g_print ("FAILED: %s\n",
5701 _g_dbus_debug_print_unlock ();
5705 value
= decode_method_reply (reply
, state
->method_name
, state
->reply_type
, &state
->fd_list
, &error
);
5708 g_task_return_error (task
, error
);
5710 g_task_return_pointer (task
, value
, (GDestroyNotify
) g_variant_unref
);
5712 g_clear_object (&reply
);
5713 g_object_unref (task
);
5716 /* called in any thread, with the connection's lock not held */
5718 g_dbus_connection_call_internal (GDBusConnection
*connection
,
5719 const gchar
*bus_name
,
5720 const gchar
*object_path
,
5721 const gchar
*interface_name
,
5722 const gchar
*method_name
,
5723 GVariant
*parameters
,
5724 const GVariantType
*reply_type
,
5725 GDBusCallFlags flags
,
5727 GUnixFDList
*fd_list
,
5728 GCancellable
*cancellable
,
5729 GAsyncReadyCallback callback
,
5732 GDBusMessage
*message
;
5735 g_return_if_fail (G_IS_DBUS_CONNECTION (connection
));
5736 g_return_if_fail (bus_name
== NULL
|| g_dbus_is_name (bus_name
));
5737 g_return_if_fail (object_path
!= NULL
&& g_variant_is_object_path (object_path
));
5738 g_return_if_fail (interface_name
!= NULL
&& g_dbus_is_interface_name (interface_name
));
5739 g_return_if_fail (method_name
!= NULL
&& g_dbus_is_member_name (method_name
));
5740 g_return_if_fail (timeout_msec
>= 0 || timeout_msec
== -1);
5741 g_return_if_fail ((parameters
== NULL
) || g_variant_is_of_type (parameters
, G_VARIANT_TYPE_TUPLE
));
5742 g_return_if_fail (check_initialized (connection
));
5744 g_return_if_fail (fd_list
== NULL
|| G_IS_UNIX_FD_LIST (fd_list
));
5746 g_return_if_fail (fd_list
== NULL
);
5749 message
= g_dbus_message_new_method_call (bus_name
,
5753 add_call_flags (message
, flags
);
5754 if (parameters
!= NULL
)
5755 g_dbus_message_set_body (message
, parameters
);
5758 if (fd_list
!= NULL
)
5759 g_dbus_message_set_unix_fd_list (message
, fd_list
);
5762 /* If the user has no callback then we can just send the message with
5763 * the G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED flag set and skip all
5764 * the logic for processing the reply. If the service sends the reply
5765 * anyway then it will just be ignored.
5767 if (callback
!= NULL
)
5772 state
= g_slice_new0 (CallState
);
5773 state
->method_name
= g_strjoin (".", interface_name
, method_name
, NULL
);
5775 if (reply_type
== NULL
)
5776 reply_type
= G_VARIANT_TYPE_ANY
;
5778 state
->reply_type
= g_variant_type_copy (reply_type
);
5780 task
= g_task_new (connection
, cancellable
, callback
, user_data
);
5781 g_task_set_source_tag (task
, g_dbus_connection_call_internal
);
5782 g_task_set_task_data (task
, state
, (GDestroyNotify
) call_state_free
);
5784 g_dbus_connection_send_message_with_reply (connection
,
5786 G_DBUS_SEND_MESSAGE_FLAGS_NONE
,
5790 g_dbus_connection_call_done
,
5792 serial
= state
->serial
;
5796 GDBusMessageFlags flags
;
5798 flags
= g_dbus_message_get_flags (message
);
5799 flags
|= G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED
;
5800 g_dbus_message_set_flags (message
, flags
);
5802 g_dbus_connection_send_message (connection
,
5804 G_DBUS_SEND_MESSAGE_FLAGS_NONE
,
5808 if (G_UNLIKELY (_g_dbus_debug_call ()))
5810 _g_dbus_debug_print_lock ();
5811 g_print ("========================================================================\n"
5812 "GDBus-debug:Call:\n"
5813 " >>>> ASYNC %s.%s()\n"
5815 " owned by name %s (serial %d)\n",
5819 bus_name
!= NULL
? bus_name
: "(none)",
5821 _g_dbus_debug_print_unlock ();
5824 if (message
!= NULL
)
5825 g_object_unref (message
);
5828 /* called in any thread, with the connection's lock not held */
5830 g_dbus_connection_call_finish_internal (GDBusConnection
*connection
,
5831 GUnixFDList
**out_fd_list
,
5839 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), NULL
);
5840 g_return_val_if_fail (g_task_is_valid (res
, connection
), NULL
);
5841 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, NULL
);
5843 task
= G_TASK (res
);
5844 state
= g_task_get_task_data (task
);
5846 ret
= g_task_propagate_pointer (task
, error
);
5850 if (out_fd_list
!= NULL
)
5851 *out_fd_list
= state
->fd_list
!= NULL
? g_object_ref (state
->fd_list
) : NULL
;
5855 /* called in any user thread, with the connection's lock not held */
5857 g_dbus_connection_call_sync_internal (GDBusConnection
*connection
,
5858 const gchar
*bus_name
,
5859 const gchar
*object_path
,
5860 const gchar
*interface_name
,
5861 const gchar
*method_name
,
5862 GVariant
*parameters
,
5863 const GVariantType
*reply_type
,
5864 GDBusCallFlags flags
,
5866 GUnixFDList
*fd_list
,
5867 GUnixFDList
**out_fd_list
,
5868 GCancellable
*cancellable
,
5871 GDBusMessage
*message
;
5872 GDBusMessage
*reply
;
5874 GError
*local_error
;
5875 GDBusSendMessageFlags send_flags
;
5881 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), NULL
);
5882 g_return_val_if_fail (bus_name
== NULL
|| g_dbus_is_name (bus_name
), NULL
);
5883 g_return_val_if_fail (object_path
!= NULL
&& g_variant_is_object_path (object_path
), NULL
);
5884 g_return_val_if_fail (interface_name
!= NULL
&& g_dbus_is_interface_name (interface_name
), NULL
);
5885 g_return_val_if_fail (method_name
!= NULL
&& g_dbus_is_member_name (method_name
), NULL
);
5886 g_return_val_if_fail (timeout_msec
>= 0 || timeout_msec
== -1, NULL
);
5887 g_return_val_if_fail ((parameters
== NULL
) || g_variant_is_of_type (parameters
, G_VARIANT_TYPE_TUPLE
), NULL
);
5889 g_return_val_if_fail (fd_list
== NULL
|| G_IS_UNIX_FD_LIST (fd_list
), NULL
);
5891 g_return_val_if_fail (fd_list
== NULL
, NULL
);
5893 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, NULL
);
5895 if (!(flags
& CALL_FLAGS_INITIALIZING
))
5896 g_return_val_if_fail (check_initialized (connection
), FALSE
);
5898 if (reply_type
== NULL
)
5899 reply_type
= G_VARIANT_TYPE_ANY
;
5901 message
= g_dbus_message_new_method_call (bus_name
,
5905 add_call_flags (message
, flags
);
5906 if (parameters
!= NULL
)
5907 g_dbus_message_set_body (message
, parameters
);
5910 if (fd_list
!= NULL
)
5911 g_dbus_message_set_unix_fd_list (message
, fd_list
);
5914 if (G_UNLIKELY (_g_dbus_debug_call ()))
5916 _g_dbus_debug_print_lock ();
5917 g_print ("========================================================================\n"
5918 "GDBus-debug:Call:\n"
5919 " >>>> SYNC %s.%s()\n"
5921 " owned by name %s\n",
5925 bus_name
!= NULL
? bus_name
: "(none)");
5926 _g_dbus_debug_print_unlock ();
5931 send_flags
= G_DBUS_SEND_MESSAGE_FLAGS_NONE
;
5933 /* translate from one flavour of flags to another... */
5934 if (flags
& CALL_FLAGS_INITIALIZING
)
5935 send_flags
|= SEND_MESSAGE_FLAGS_INITIALIZING
;
5937 reply
= g_dbus_connection_send_message_with_reply_sync (connection
,
5941 NULL
, /* volatile guint32 *out_serial */
5945 if (G_UNLIKELY (_g_dbus_debug_call ()))
5947 _g_dbus_debug_print_lock ();
5948 g_print ("========================================================================\n"
5949 "GDBus-debug:Call:\n"
5950 " <<<< SYNC COMPLETE %s.%s()\n"
5956 g_print ("SUCCESS\n");
5960 g_print ("FAILED: %s\n",
5961 local_error
->message
);
5963 _g_dbus_debug_print_unlock ();
5969 *error
= local_error
;
5971 g_error_free (local_error
);
5975 result
= decode_method_reply (reply
, method_name
, reply_type
, out_fd_list
, error
);
5978 if (message
!= NULL
)
5979 g_object_unref (message
);
5981 g_object_unref (reply
);
5986 /* ---------------------------------------------------------------------------------------------------- */
5989 * g_dbus_connection_call:
5990 * @connection: a #GDBusConnection
5991 * @bus_name: (nullable): a unique or well-known bus name or %NULL if
5992 * @connection is not a message bus connection
5993 * @object_path: path of remote object
5994 * @interface_name: D-Bus interface to invoke method on
5995 * @method_name: the name of the method to invoke
5996 * @parameters: (nullable): a #GVariant tuple with parameters for the method
5997 * or %NULL if not passing parameters
5998 * @reply_type: (nullable): the expected type of the reply, or %NULL
5999 * @flags: flags from the #GDBusCallFlags enumeration
6000 * @timeout_msec: the timeout in milliseconds, -1 to use the default
6001 * timeout or %G_MAXINT for no timeout
6002 * @cancellable: (nullable): a #GCancellable or %NULL
6003 * @callback: (nullable): a #GAsyncReadyCallback to call when the request
6004 * is satisfied or %NULL if you don't care about the result of the
6006 * @user_data: the data to pass to @callback
6008 * Asynchronously invokes the @method_name method on the
6009 * @interface_name D-Bus interface on the remote object at
6010 * @object_path owned by @bus_name.
6012 * If @connection is closed then the operation will fail with
6013 * %G_IO_ERROR_CLOSED. If @cancellable is canceled, the operation will
6014 * fail with %G_IO_ERROR_CANCELLED. If @parameters contains a value
6015 * not compatible with the D-Bus protocol, the operation fails with
6016 * %G_IO_ERROR_INVALID_ARGUMENT.
6018 * If @reply_type is non-%NULL then the reply will be checked for having this type and an
6019 * error will be raised if it does not match. Said another way, if you give a @reply_type
6020 * then any non-%NULL return value will be of this type.
6022 * If the @parameters #GVariant is floating, it is consumed. This allows
6023 * convenient 'inline' use of g_variant_new(), e.g.:
6024 * |[<!-- language="C" -->
6025 * g_dbus_connection_call (connection,
6026 * "org.freedesktop.StringThings",
6027 * "/org/freedesktop/StringThings",
6028 * "org.freedesktop.StringThings",
6030 * g_variant_new ("(ss)",
6034 * G_DBUS_CALL_FLAGS_NONE,
6037 * (GAsyncReadyCallback) two_strings_done,
6041 * This is an asynchronous method. When the operation is finished,
6042 * @callback will be invoked in the
6043 * [thread-default main context][g-main-context-push-thread-default]
6044 * of the thread you are calling this method from. You can then call
6045 * g_dbus_connection_call_finish() to get the result of the operation.
6046 * See g_dbus_connection_call_sync() for the synchronous version of this
6049 * If @callback is %NULL then the D-Bus method call message will be sent with
6050 * the %G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED flag set.
6055 g_dbus_connection_call (GDBusConnection
*connection
,
6056 const gchar
*bus_name
,
6057 const gchar
*object_path
,
6058 const gchar
*interface_name
,
6059 const gchar
*method_name
,
6060 GVariant
*parameters
,
6061 const GVariantType
*reply_type
,
6062 GDBusCallFlags flags
,
6064 GCancellable
*cancellable
,
6065 GAsyncReadyCallback callback
,
6068 g_dbus_connection_call_internal (connection
, bus_name
, object_path
, interface_name
, method_name
, parameters
, reply_type
, flags
, timeout_msec
, NULL
, cancellable
, callback
, user_data
);
6072 * g_dbus_connection_call_finish:
6073 * @connection: a #GDBusConnection
6074 * @res: a #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_connection_call()
6075 * @error: return location for error or %NULL
6077 * Finishes an operation started with g_dbus_connection_call().
6079 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
6080 * return values. Free with g_variant_unref().
6085 g_dbus_connection_call_finish (GDBusConnection
*connection
,
6089 return g_dbus_connection_call_finish_internal (connection
, NULL
, res
, error
);
6093 * g_dbus_connection_call_sync:
6094 * @connection: a #GDBusConnection
6095 * @bus_name: (nullable): a unique or well-known bus name or %NULL if
6096 * @connection is not a message bus connection
6097 * @object_path: path of remote object
6098 * @interface_name: D-Bus interface to invoke method on
6099 * @method_name: the name of the method to invoke
6100 * @parameters: (nullable): a #GVariant tuple with parameters for the method
6101 * or %NULL if not passing parameters
6102 * @reply_type: (nullable): the expected type of the reply, or %NULL
6103 * @flags: flags from the #GDBusCallFlags enumeration
6104 * @timeout_msec: the timeout in milliseconds, -1 to use the default
6105 * timeout or %G_MAXINT for no timeout
6106 * @cancellable: (nullable): a #GCancellable or %NULL
6107 * @error: return location for error or %NULL
6109 * Synchronously invokes the @method_name method on the
6110 * @interface_name D-Bus interface on the remote object at
6111 * @object_path owned by @bus_name.
6113 * If @connection is closed then the operation will fail with
6114 * %G_IO_ERROR_CLOSED. If @cancellable is canceled, the
6115 * operation will fail with %G_IO_ERROR_CANCELLED. If @parameters
6116 * contains a value not compatible with the D-Bus protocol, the operation
6117 * fails with %G_IO_ERROR_INVALID_ARGUMENT.
6119 * If @reply_type is non-%NULL then the reply will be checked for having
6120 * this type and an error will be raised if it does not match. Said
6121 * another way, if you give a @reply_type then any non-%NULL return
6122 * value will be of this type.
6124 * If the @parameters #GVariant is floating, it is consumed.
6125 * This allows convenient 'inline' use of g_variant_new(), e.g.:
6126 * |[<!-- language="C" -->
6127 * g_dbus_connection_call_sync (connection,
6128 * "org.freedesktop.StringThings",
6129 * "/org/freedesktop/StringThings",
6130 * "org.freedesktop.StringThings",
6132 * g_variant_new ("(ss)",
6136 * G_DBUS_CALL_FLAGS_NONE,
6142 * The calling thread is blocked until a reply is received. See
6143 * g_dbus_connection_call() for the asynchronous version of
6146 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
6147 * return values. Free with g_variant_unref().
6152 g_dbus_connection_call_sync (GDBusConnection
*connection
,
6153 const gchar
*bus_name
,
6154 const gchar
*object_path
,
6155 const gchar
*interface_name
,
6156 const gchar
*method_name
,
6157 GVariant
*parameters
,
6158 const GVariantType
*reply_type
,
6159 GDBusCallFlags flags
,
6161 GCancellable
*cancellable
,
6164 return g_dbus_connection_call_sync_internal (connection
, bus_name
, object_path
, interface_name
, method_name
, parameters
, reply_type
, flags
, timeout_msec
, NULL
, NULL
, cancellable
, error
);
6167 /* ---------------------------------------------------------------------------------------------------- */
6172 * g_dbus_connection_call_with_unix_fd_list:
6173 * @connection: a #GDBusConnection
6174 * @bus_name: (nullable): a unique or well-known bus name or %NULL if
6175 * @connection is not a message bus connection
6176 * @object_path: path of remote object
6177 * @interface_name: D-Bus interface to invoke method on
6178 * @method_name: the name of the method to invoke
6179 * @parameters: (nullable): a #GVariant tuple with parameters for the method
6180 * or %NULL if not passing parameters
6181 * @reply_type: (nullable): the expected type of the reply, or %NULL
6182 * @flags: flags from the #GDBusCallFlags enumeration
6183 * @timeout_msec: the timeout in milliseconds, -1 to use the default
6184 * timeout or %G_MAXINT for no timeout
6185 * @fd_list: (nullable): a #GUnixFDList or %NULL
6186 * @cancellable: (nullable): a #GCancellable or %NULL
6187 * @callback: (nullable): a #GAsyncReadyCallback to call when the request is
6188 * satisfied or %NULL if you don't * care about the result of the
6190 * @user_data: The data to pass to @callback.
6192 * Like g_dbus_connection_call() but also takes a #GUnixFDList object.
6194 * This method is only available on UNIX.
6199 g_dbus_connection_call_with_unix_fd_list (GDBusConnection
*connection
,
6200 const gchar
*bus_name
,
6201 const gchar
*object_path
,
6202 const gchar
*interface_name
,
6203 const gchar
*method_name
,
6204 GVariant
*parameters
,
6205 const GVariantType
*reply_type
,
6206 GDBusCallFlags flags
,
6208 GUnixFDList
*fd_list
,
6209 GCancellable
*cancellable
,
6210 GAsyncReadyCallback callback
,
6213 g_dbus_connection_call_internal (connection
, bus_name
, object_path
, interface_name
, method_name
, parameters
, reply_type
, flags
, timeout_msec
, fd_list
, cancellable
, callback
, user_data
);
6217 * g_dbus_connection_call_with_unix_fd_list_finish:
6218 * @connection: a #GDBusConnection
6219 * @out_fd_list: (out) (optional): return location for a #GUnixFDList or %NULL
6220 * @res: a #GAsyncResult obtained from the #GAsyncReadyCallback passed to
6221 * g_dbus_connection_call_with_unix_fd_list()
6222 * @error: return location for error or %NULL
6224 * Finishes an operation started with g_dbus_connection_call_with_unix_fd_list().
6226 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
6227 * return values. Free with g_variant_unref().
6232 g_dbus_connection_call_with_unix_fd_list_finish (GDBusConnection
*connection
,
6233 GUnixFDList
**out_fd_list
,
6237 return g_dbus_connection_call_finish_internal (connection
, out_fd_list
, res
, error
);
6241 * g_dbus_connection_call_with_unix_fd_list_sync:
6242 * @connection: a #GDBusConnection
6243 * @bus_name: (nullable): a unique or well-known bus name or %NULL
6244 * if @connection is not a message bus connection
6245 * @object_path: path of remote object
6246 * @interface_name: D-Bus interface to invoke method on
6247 * @method_name: the name of the method to invoke
6248 * @parameters: (nullable): a #GVariant tuple with parameters for
6249 * the method or %NULL if not passing parameters
6250 * @reply_type: (nullable): the expected type of the reply, or %NULL
6251 * @flags: flags from the #GDBusCallFlags enumeration
6252 * @timeout_msec: the timeout in milliseconds, -1 to use the default
6253 * timeout or %G_MAXINT for no timeout
6254 * @fd_list: (nullable): a #GUnixFDList or %NULL
6255 * @out_fd_list: (out) (optional): return location for a #GUnixFDList or %NULL
6256 * @cancellable: (nullable): a #GCancellable or %NULL
6257 * @error: return location for error or %NULL
6259 * Like g_dbus_connection_call_sync() but also takes and returns #GUnixFDList objects.
6261 * This method is only available on UNIX.
6263 * Returns: %NULL if @error is set. Otherwise a #GVariant tuple with
6264 * return values. Free with g_variant_unref().
6269 g_dbus_connection_call_with_unix_fd_list_sync (GDBusConnection
*connection
,
6270 const gchar
*bus_name
,
6271 const gchar
*object_path
,
6272 const gchar
*interface_name
,
6273 const gchar
*method_name
,
6274 GVariant
*parameters
,
6275 const GVariantType
*reply_type
,
6276 GDBusCallFlags flags
,
6278 GUnixFDList
*fd_list
,
6279 GUnixFDList
**out_fd_list
,
6280 GCancellable
*cancellable
,
6283 return g_dbus_connection_call_sync_internal (connection
, bus_name
, object_path
, interface_name
, method_name
, parameters
, reply_type
, flags
, timeout_msec
, fd_list
, out_fd_list
, cancellable
, error
);
6286 #endif /* G_OS_UNIX */
6288 /* ---------------------------------------------------------------------------------------------------- */
6290 struct ExportedSubtree
6294 GDBusConnection
*connection
;
6295 GDBusSubtreeVTable
*vtable
;
6296 GDBusSubtreeFlags flags
;
6298 GMainContext
*context
;
6300 GDestroyNotify user_data_free_func
;
6304 exported_subtree_free (ExportedSubtree
*es
)
6306 call_destroy_notify (es
->context
,
6307 es
->user_data_free_func
,
6310 g_main_context_unref (es
->context
);
6312 _g_dbus_subtree_vtable_free (es
->vtable
);
6313 g_free (es
->object_path
);
6317 /* called without lock held in the thread where the caller registered
6321 handle_subtree_introspect (GDBusConnection
*connection
,
6322 ExportedSubtree
*es
,
6323 GDBusMessage
*message
)
6327 GDBusMessage
*reply
;
6330 const gchar
*sender
;
6331 const gchar
*requested_object_path
;
6332 const gchar
*requested_node
;
6333 GDBusInterfaceInfo
**interfaces
;
6335 gchar
**subnode_paths
;
6336 gboolean has_properties_interface
;
6337 gboolean has_introspectable_interface
;
6341 requested_object_path
= g_dbus_message_get_path (message
);
6342 sender
= g_dbus_message_get_sender (message
);
6343 is_root
= (g_strcmp0 (requested_object_path
, es
->object_path
) == 0);
6345 s
= g_string_new (NULL
);
6346 introspect_append_header (s
);
6348 /* Strictly we don't need the children in dynamic mode, but we avoid the
6349 * conditionals to preserve code clarity
6351 children
= es
->vtable
->enumerate (es
->connection
,
6358 requested_node
= strrchr (requested_object_path
, '/') + 1;
6360 /* Assert existence of object if we are not dynamic */
6361 if (!(es
->flags
& G_DBUS_SUBTREE_FLAGS_DISPATCH_TO_UNENUMERATED_NODES
) &&
6362 !_g_strv_has_string ((const gchar
* const *) children
, requested_node
))
6367 requested_node
= NULL
;
6370 interfaces
= es
->vtable
->introspect (es
->connection
,
6375 if (interfaces
!= NULL
)
6377 has_properties_interface
= FALSE
;
6378 has_introspectable_interface
= FALSE
;
6380 for (n
= 0; interfaces
[n
] != NULL
; n
++)
6382 if (strcmp (interfaces
[n
]->name
, "org.freedesktop.DBus.Properties") == 0)
6383 has_properties_interface
= TRUE
;
6384 else if (strcmp (interfaces
[n
]->name
, "org.freedesktop.DBus.Introspectable") == 0)
6385 has_introspectable_interface
= TRUE
;
6387 if (!has_properties_interface
)
6388 g_string_append (s
, introspect_properties_interface
);
6389 if (!has_introspectable_interface
)
6390 g_string_append (s
, introspect_introspectable_interface
);
6392 for (n
= 0; interfaces
[n
] != NULL
; n
++)
6394 g_dbus_interface_info_generate_xml (interfaces
[n
], 2, s
);
6395 g_dbus_interface_info_unref (interfaces
[n
]);
6397 g_free (interfaces
);
6400 /* then include <node> entries from the Subtree for the root */
6403 for (n
= 0; children
!= NULL
&& children
[n
] != NULL
; n
++)
6404 g_string_append_printf (s
, " <node name=\"%s\"/>\n", children
[n
]);
6407 /* finally include nodes registered below us */
6408 subnode_paths
= g_dbus_connection_list_registered (es
->connection
, requested_object_path
);
6409 for (n
= 0; subnode_paths
!= NULL
&& subnode_paths
[n
] != NULL
; n
++)
6410 g_string_append_printf (s
, " <node name=\"%s\"/>\n", subnode_paths
[n
]);
6411 g_strfreev (subnode_paths
);
6413 g_string_append (s
, "</node>\n");
6415 reply
= g_dbus_message_new_method_reply (message
);
6416 g_dbus_message_set_body (reply
, g_variant_new ("(s)", s
->str
));
6417 g_dbus_connection_send_message (connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
6418 g_object_unref (reply
);
6423 g_string_free (s
, TRUE
);
6424 g_strfreev (children
);
6428 /* called without lock held in the thread where the caller registered
6432 handle_subtree_method_invocation (GDBusConnection
*connection
,
6433 ExportedSubtree
*es
,
6434 GDBusMessage
*message
)
6437 const gchar
*sender
;
6438 const gchar
*interface_name
;
6439 const gchar
*member
;
6440 const gchar
*signature
;
6441 const gchar
*requested_object_path
;
6442 const gchar
*requested_node
;
6444 GDBusInterfaceInfo
*interface_info
;
6445 const GDBusInterfaceVTable
*interface_vtable
;
6446 gpointer interface_user_data
;
6448 GDBusInterfaceInfo
**interfaces
;
6449 gboolean is_property_get
;
6450 gboolean is_property_set
;
6451 gboolean is_property_get_all
;
6456 requested_object_path
= g_dbus_message_get_path (message
);
6457 sender
= g_dbus_message_get_sender (message
);
6458 interface_name
= g_dbus_message_get_interface (message
);
6459 member
= g_dbus_message_get_member (message
);
6460 signature
= g_dbus_message_get_signature (message
);
6461 is_root
= (g_strcmp0 (requested_object_path
, es
->object_path
) == 0);
6463 is_property_get
= FALSE
;
6464 is_property_set
= FALSE
;
6465 is_property_get_all
= FALSE
;
6466 if (g_strcmp0 (interface_name
, "org.freedesktop.DBus.Properties") == 0)
6468 if (g_strcmp0 (member
, "Get") == 0 && g_strcmp0 (signature
, "ss") == 0)
6469 is_property_get
= TRUE
;
6470 else if (g_strcmp0 (member
, "Set") == 0 && g_strcmp0 (signature
, "ssv") == 0)
6471 is_property_set
= TRUE
;
6472 else if (g_strcmp0 (member
, "GetAll") == 0 && g_strcmp0 (signature
, "s") == 0)
6473 is_property_get_all
= TRUE
;
6478 requested_node
= strrchr (requested_object_path
, '/') + 1;
6480 if (~es
->flags
& G_DBUS_SUBTREE_FLAGS_DISPATCH_TO_UNENUMERATED_NODES
)
6482 /* We don't want to dispatch to unenumerated
6483 * nodes, so ensure that the child exists.
6488 children
= es
->vtable
->enumerate (es
->connection
,
6493 exists
= _g_strv_has_string ((const gchar
* const *) children
, requested_node
);
6494 g_strfreev (children
);
6502 requested_node
= NULL
;
6505 /* get introspection data for the node */
6506 interfaces
= es
->vtable
->introspect (es
->connection
,
6508 requested_object_path
,
6512 if (interfaces
== NULL
)
6515 interface_info
= NULL
;
6516 for (n
= 0; interfaces
[n
] != NULL
; n
++)
6518 if (g_strcmp0 (interfaces
[n
]->name
, interface_name
) == 0)
6519 interface_info
= interfaces
[n
];
6522 /* dispatch the call if the user wants to handle it */
6523 if (interface_info
!= NULL
)
6525 /* figure out where to dispatch the method call */
6526 interface_user_data
= NULL
;
6527 interface_vtable
= es
->vtable
->dispatch (es
->connection
,
6532 &interface_user_data
,
6534 if (interface_vtable
== NULL
)
6537 CONNECTION_LOCK (connection
);
6538 handled
= validate_and_maybe_schedule_method_call (es
->connection
,
6545 interface_user_data
);
6546 CONNECTION_UNLOCK (connection
);
6548 /* handle org.freedesktop.DBus.Properties interface if not explicitly handled */
6549 else if (is_property_get
|| is_property_set
|| is_property_get_all
)
6551 if (is_property_get
)
6552 g_variant_get (g_dbus_message_get_body (message
), "(&s&s)", &interface_name
, NULL
);
6553 else if (is_property_set
)
6554 g_variant_get (g_dbus_message_get_body (message
), "(&s&sv)", &interface_name
, NULL
, NULL
);
6555 else if (is_property_get_all
)
6556 g_variant_get (g_dbus_message_get_body (message
), "(&s)", &interface_name
, NULL
, NULL
);
6558 g_assert_not_reached ();
6560 /* see if the object supports this interface at all */
6561 for (n
= 0; interfaces
[n
] != NULL
; n
++)
6563 if (g_strcmp0 (interfaces
[n
]->name
, interface_name
) == 0)
6564 interface_info
= interfaces
[n
];
6567 /* Fail with org.freedesktop.DBus.Error.InvalidArgs if the user-code
6568 * claims it won't support the interface
6570 if (interface_info
== NULL
)
6572 GDBusMessage
*reply
;
6573 reply
= g_dbus_message_new_method_error (message
,
6574 "org.freedesktop.DBus.Error.InvalidArgs",
6575 _("No such interface '%s'"),
6577 g_dbus_connection_send_message (es
->connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
6578 g_object_unref (reply
);
6583 /* figure out where to dispatch the property get/set/getall calls */
6584 interface_user_data
= NULL
;
6585 interface_vtable
= es
->vtable
->dispatch (es
->connection
,
6590 &interface_user_data
,
6592 if (interface_vtable
== NULL
)
6594 g_warning ("The subtree introspection function indicates that '%s' "
6595 "is a valid interface name, but calling the dispatch "
6596 "function on that interface gave us NULL", interface_name
);
6600 if (is_property_get
|| is_property_set
)
6602 CONNECTION_LOCK (connection
);
6603 handled
= validate_and_maybe_schedule_property_getset (es
->connection
,
6611 interface_user_data
);
6612 CONNECTION_UNLOCK (connection
);
6614 else if (is_property_get_all
)
6616 CONNECTION_LOCK (connection
);
6617 handled
= validate_and_maybe_schedule_property_get_all (es
->connection
,
6624 interface_user_data
);
6625 CONNECTION_UNLOCK (connection
);
6630 if (interfaces
!= NULL
)
6632 for (n
= 0; interfaces
[n
] != NULL
; n
++)
6633 g_dbus_interface_info_unref (interfaces
[n
]);
6634 g_free (interfaces
);
6642 GDBusMessage
*message
;
6643 ExportedSubtree
*es
;
6644 } SubtreeDeferredData
;
6647 subtree_deferred_data_free (SubtreeDeferredData
*data
)
6649 g_object_unref (data
->message
);
6653 /* called without lock held in the thread where the caller registered the subtree */
6655 process_subtree_vtable_message_in_idle_cb (gpointer _data
)
6657 SubtreeDeferredData
*data
= _data
;
6662 if (g_strcmp0 (g_dbus_message_get_interface (data
->message
), "org.freedesktop.DBus.Introspectable") == 0 &&
6663 g_strcmp0 (g_dbus_message_get_member (data
->message
), "Introspect") == 0 &&
6664 g_strcmp0 (g_dbus_message_get_signature (data
->message
), "") == 0)
6665 handled
= handle_subtree_introspect (data
->es
->connection
,
6669 handled
= handle_subtree_method_invocation (data
->es
->connection
,
6675 CONNECTION_LOCK (data
->es
->connection
);
6676 handled
= handle_generic_unlocked (data
->es
->connection
, data
->message
);
6677 CONNECTION_UNLOCK (data
->es
->connection
);
6680 /* if we couldn't handle the request, just bail with the UnknownMethod error */
6683 GDBusMessage
*reply
;
6684 reply
= g_dbus_message_new_method_error (data
->message
,
6685 "org.freedesktop.DBus.Error.UnknownMethod",
6686 _("Method '%s' on interface '%s' with signature '%s' does not exist"),
6687 g_dbus_message_get_member (data
->message
),
6688 g_dbus_message_get_interface (data
->message
),
6689 g_dbus_message_get_signature (data
->message
));
6690 g_dbus_connection_send_message (data
->es
->connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
6691 g_object_unref (reply
);
6697 /* called in GDBusWorker thread with connection's lock held */
6699 subtree_message_func (GDBusConnection
*connection
,
6700 ExportedSubtree
*es
,
6701 GDBusMessage
*message
)
6703 GSource
*idle_source
;
6704 SubtreeDeferredData
*data
;
6706 data
= g_new0 (SubtreeDeferredData
, 1);
6707 data
->message
= g_object_ref (message
);
6710 /* defer this call to an idle handler in the right thread */
6711 idle_source
= g_idle_source_new ();
6712 g_source_set_priority (idle_source
, G_PRIORITY_HIGH
);
6713 g_source_set_callback (idle_source
,
6714 process_subtree_vtable_message_in_idle_cb
,
6716 (GDestroyNotify
) subtree_deferred_data_free
);
6717 g_source_set_name (idle_source
, "[gio] process_subtree_vtable_message_in_idle_cb");
6718 g_source_attach (idle_source
, es
->context
);
6719 g_source_unref (idle_source
);
6721 /* since we own the entire subtree, handlers for objects not in the subtree have been
6722 * tried already by libdbus-1 - so we just need to ensure that we're always going
6723 * to reply to the message
6729 * g_dbus_connection_register_subtree:
6730 * @connection: a #GDBusConnection
6731 * @object_path: the object path to register the subtree at
6732 * @vtable: a #GDBusSubtreeVTable to enumerate, introspect and
6733 * dispatch nodes in the subtree
6734 * @flags: flags used to fine tune the behavior of the subtree
6735 * @user_data: data to pass to functions in @vtable
6736 * @user_data_free_func: function to call when the subtree is unregistered
6737 * @error: return location for error or %NULL
6739 * Registers a whole subtree of dynamic objects.
6741 * The @enumerate and @introspection functions in @vtable are used to
6742 * convey, to remote callers, what nodes exist in the subtree rooted
6745 * When handling remote calls into any node in the subtree, first the
6746 * @enumerate function is used to check if the node exists. If the node exists
6747 * or the #G_DBUS_SUBTREE_FLAGS_DISPATCH_TO_UNENUMERATED_NODES flag is set
6748 * the @introspection function is used to check if the node supports the
6749 * requested method. If so, the @dispatch function is used to determine
6750 * where to dispatch the call. The collected #GDBusInterfaceVTable and
6751 * #gpointer will be used to call into the interface vtable for processing
6754 * All calls into user-provided code will be invoked in the
6755 * [thread-default main context][g-main-context-push-thread-default]
6756 * of the thread you are calling this method from.
6758 * If an existing subtree is already registered at @object_path or
6759 * then @error is set to #G_IO_ERROR_EXISTS.
6761 * Note that it is valid to register regular objects (using
6762 * g_dbus_connection_register_object()) in a subtree registered with
6763 * g_dbus_connection_register_subtree() - if so, the subtree handler
6764 * is tried as the last resort. One way to think about a subtree
6765 * handler is to consider it a fallback handler for object paths not
6766 * registered via g_dbus_connection_register_object() or other bindings.
6768 * Note that @vtable will be copied so you cannot change it after
6771 * See this [server][gdbus-subtree-server] for an example of how to use
6774 * Returns: 0 if @error is set, otherwise a subtree registration id (never 0)
6775 * that can be used with g_dbus_connection_unregister_subtree() .
6780 g_dbus_connection_register_subtree (GDBusConnection
*connection
,
6781 const gchar
*object_path
,
6782 const GDBusSubtreeVTable
*vtable
,
6783 GDBusSubtreeFlags flags
,
6785 GDestroyNotify user_data_free_func
,
6789 ExportedSubtree
*es
;
6791 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), 0);
6792 g_return_val_if_fail (object_path
!= NULL
&& g_variant_is_object_path (object_path
), 0);
6793 g_return_val_if_fail (vtable
!= NULL
, 0);
6794 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, 0);
6795 g_return_val_if_fail (check_initialized (connection
), 0);
6799 CONNECTION_LOCK (connection
);
6801 es
= g_hash_table_lookup (connection
->map_object_path_to_es
, object_path
);
6807 _("A subtree is already exported for %s"),
6812 es
= g_new0 (ExportedSubtree
, 1);
6813 es
->object_path
= g_strdup (object_path
);
6814 es
->connection
= connection
;
6816 es
->vtable
= _g_dbus_subtree_vtable_copy (vtable
);
6818 es
->id
= g_atomic_int_add (&_global_subtree_registration_id
, 1); /* TODO: overflow etc. */
6819 es
->user_data
= user_data
;
6820 es
->user_data_free_func
= user_data_free_func
;
6821 es
->context
= g_main_context_ref_thread_default ();
6823 g_hash_table_insert (connection
->map_object_path_to_es
, es
->object_path
, es
);
6824 g_hash_table_insert (connection
->map_id_to_es
,
6825 GUINT_TO_POINTER (es
->id
),
6831 CONNECTION_UNLOCK (connection
);
6836 /* ---------------------------------------------------------------------------------------------------- */
6839 * g_dbus_connection_unregister_subtree:
6840 * @connection: a #GDBusConnection
6841 * @registration_id: a subtree registration id obtained from
6842 * g_dbus_connection_register_subtree()
6844 * Unregisters a subtree.
6846 * Returns: %TRUE if the subtree was unregistered, %FALSE otherwise
6851 g_dbus_connection_unregister_subtree (GDBusConnection
*connection
,
6852 guint registration_id
)
6854 ExportedSubtree
*es
;
6857 g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection
), FALSE
);
6858 g_return_val_if_fail (check_initialized (connection
), FALSE
);
6862 CONNECTION_LOCK (connection
);
6864 es
= g_hash_table_lookup (connection
->map_id_to_es
,
6865 GUINT_TO_POINTER (registration_id
));
6869 g_warn_if_fail (g_hash_table_remove (connection
->map_id_to_es
, GUINT_TO_POINTER (es
->id
)));
6870 g_warn_if_fail (g_hash_table_remove (connection
->map_object_path_to_es
, es
->object_path
));
6875 CONNECTION_UNLOCK (connection
);
6880 /* ---------------------------------------------------------------------------------------------------- */
6882 /* may be called in any thread, with connection's lock held */
6884 handle_generic_ping_unlocked (GDBusConnection
*connection
,
6885 const gchar
*object_path
,
6886 GDBusMessage
*message
)
6888 GDBusMessage
*reply
;
6889 reply
= g_dbus_message_new_method_reply (message
);
6890 g_dbus_connection_send_message_unlocked (connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
6891 g_object_unref (reply
);
6894 /* may be called in any thread, with connection's lock held */
6896 handle_generic_get_machine_id_unlocked (GDBusConnection
*connection
,
6897 const gchar
*object_path
,
6898 GDBusMessage
*message
)
6900 GDBusMessage
*reply
;
6903 if (connection
->machine_id
== NULL
)
6908 connection
->machine_id
= _g_dbus_get_machine_id (&error
);
6909 if (connection
->machine_id
== NULL
)
6911 reply
= g_dbus_message_new_method_error_literal (message
,
6912 "org.freedesktop.DBus.Error.Failed",
6914 g_error_free (error
);
6920 reply
= g_dbus_message_new_method_reply (message
);
6921 g_dbus_message_set_body (reply
, g_variant_new ("(s)", connection
->machine_id
));
6923 g_dbus_connection_send_message_unlocked (connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
6924 g_object_unref (reply
);
6927 /* may be called in any thread, with connection's lock held */
6929 handle_generic_introspect_unlocked (GDBusConnection
*connection
,
6930 const gchar
*object_path
,
6931 GDBusMessage
*message
)
6936 GDBusMessage
*reply
;
6938 /* first the header */
6939 s
= g_string_new (NULL
);
6940 introspect_append_header (s
);
6942 registered
= g_dbus_connection_list_registered_unlocked (connection
, object_path
);
6943 for (n
= 0; registered
!= NULL
&& registered
[n
] != NULL
; n
++)
6944 g_string_append_printf (s
, " <node name=\"%s\"/>\n", registered
[n
]);
6945 g_strfreev (registered
);
6946 g_string_append (s
, "</node>\n");
6948 reply
= g_dbus_message_new_method_reply (message
);
6949 g_dbus_message_set_body (reply
, g_variant_new ("(s)", s
->str
));
6950 g_dbus_connection_send_message_unlocked (connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
6951 g_object_unref (reply
);
6952 g_string_free (s
, TRUE
);
6955 /* may be called in any thread, with connection's lock held */
6957 handle_generic_unlocked (GDBusConnection
*connection
,
6958 GDBusMessage
*message
)
6961 const gchar
*interface_name
;
6962 const gchar
*member
;
6963 const gchar
*signature
;
6966 CONNECTION_ENSURE_LOCK (connection
);
6970 interface_name
= g_dbus_message_get_interface (message
);
6971 member
= g_dbus_message_get_member (message
);
6972 signature
= g_dbus_message_get_signature (message
);
6973 path
= g_dbus_message_get_path (message
);
6975 if (g_strcmp0 (interface_name
, "org.freedesktop.DBus.Introspectable") == 0 &&
6976 g_strcmp0 (member
, "Introspect") == 0 &&
6977 g_strcmp0 (signature
, "") == 0)
6979 handle_generic_introspect_unlocked (connection
, path
, message
);
6982 else if (g_strcmp0 (interface_name
, "org.freedesktop.DBus.Peer") == 0 &&
6983 g_strcmp0 (member
, "Ping") == 0 &&
6984 g_strcmp0 (signature
, "") == 0)
6986 handle_generic_ping_unlocked (connection
, path
, message
);
6989 else if (g_strcmp0 (interface_name
, "org.freedesktop.DBus.Peer") == 0 &&
6990 g_strcmp0 (member
, "GetMachineId") == 0 &&
6991 g_strcmp0 (signature
, "") == 0)
6993 handle_generic_get_machine_id_unlocked (connection
, path
, message
);
7000 /* ---------------------------------------------------------------------------------------------------- */
7002 /* called in GDBusWorker thread with connection's lock held */
7004 distribute_method_call (GDBusConnection
*connection
,
7005 GDBusMessage
*message
)
7007 GDBusMessage
*reply
;
7009 ExportedSubtree
*es
;
7010 const gchar
*object_path
;
7011 const gchar
*interface_name
;
7012 const gchar
*member
;
7014 gchar
*subtree_path
;
7017 g_assert (g_dbus_message_get_message_type (message
) == G_DBUS_MESSAGE_TYPE_METHOD_CALL
);
7019 interface_name
= g_dbus_message_get_interface (message
);
7020 member
= g_dbus_message_get_member (message
);
7021 path
= g_dbus_message_get_path (message
);
7022 subtree_path
= g_strdup (path
);
7023 needle
= strrchr (subtree_path
, '/');
7024 if (needle
!= NULL
&& needle
!= subtree_path
)
7030 g_free (subtree_path
);
7031 subtree_path
= NULL
;
7035 if (G_UNLIKELY (_g_dbus_debug_incoming ()))
7037 _g_dbus_debug_print_lock ();
7038 g_print ("========================================================================\n"
7039 "GDBus-debug:Incoming:\n"
7040 " <<<< METHOD INVOCATION %s.%s()\n"
7042 " invoked by name %s\n"
7044 interface_name
, member
,
7046 g_dbus_message_get_sender (message
) != NULL
? g_dbus_message_get_sender (message
) : "(none)",
7047 g_dbus_message_get_serial (message
));
7048 _g_dbus_debug_print_unlock ();
7051 object_path
= g_dbus_message_get_path (message
);
7052 g_assert (object_path
!= NULL
);
7054 eo
= g_hash_table_lookup (connection
->map_object_path_to_eo
, object_path
);
7057 if (obj_message_func (connection
, eo
, message
))
7061 es
= g_hash_table_lookup (connection
->map_object_path_to_es
, object_path
);
7064 if (subtree_message_func (connection
, es
, message
))
7068 if (subtree_path
!= NULL
)
7070 es
= g_hash_table_lookup (connection
->map_object_path_to_es
, subtree_path
);
7073 if (subtree_message_func (connection
, es
, message
))
7078 if (handle_generic_unlocked (connection
, message
))
7081 /* if we end up here, the message has not been not handled - so return an error saying this */
7082 reply
= g_dbus_message_new_method_error (message
,
7083 "org.freedesktop.DBus.Error.UnknownMethod",
7084 _("No such interface '%s' on object at path %s"),
7087 g_dbus_connection_send_message_unlocked (connection
, reply
, G_DBUS_SEND_MESSAGE_FLAGS_NONE
, NULL
, NULL
);
7088 g_object_unref (reply
);
7091 g_free (subtree_path
);
7094 /* ---------------------------------------------------------------------------------------------------- */
7096 /* Called in any user thread, with the message_bus_lock held. */
7098 message_bus_get_singleton (GBusType bus_type
,
7102 const gchar
*starter_bus
;
7108 case G_BUS_TYPE_SESSION
:
7109 ret
= &the_session_bus
;
7112 case G_BUS_TYPE_SYSTEM
:
7113 ret
= &the_system_bus
;
7116 case G_BUS_TYPE_STARTER
:
7117 starter_bus
= g_getenv ("DBUS_STARTER_BUS_TYPE");
7118 if (g_strcmp0 (starter_bus
, "session") == 0)
7120 ret
= message_bus_get_singleton (G_BUS_TYPE_SESSION
, error
);
7123 else if (g_strcmp0 (starter_bus
, "system") == 0)
7125 ret
= message_bus_get_singleton (G_BUS_TYPE_SYSTEM
, error
);
7130 if (starter_bus
!= NULL
)
7134 G_IO_ERROR_INVALID_ARGUMENT
,
7135 _("Cannot determine bus address from DBUS_STARTER_BUS_TYPE environment variable"
7136 " - unknown value '%s'"),
7141 g_set_error_literal (error
,
7143 G_IO_ERROR_INVALID_ARGUMENT
,
7144 _("Cannot determine bus address because the DBUS_STARTER_BUS_TYPE environment "
7145 "variable is not set"));
7151 g_assert_not_reached ();
7159 /* Called in any user thread, without holding locks. */
7160 static GDBusConnection
*
7161 get_uninitialized_connection (GBusType bus_type
,
7162 GCancellable
*cancellable
,
7165 GWeakRef
*singleton
;
7166 GDBusConnection
*ret
;
7170 G_LOCK (message_bus_lock
);
7171 singleton
= message_bus_get_singleton (bus_type
, error
);
7172 if (singleton
== NULL
)
7175 ret
= g_weak_ref_get (singleton
);
7180 address
= g_dbus_address_get_for_bus_sync (bus_type
, cancellable
, error
);
7181 if (address
== NULL
)
7183 ret
= g_object_new (G_TYPE_DBUS_CONNECTION
,
7185 "flags", G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT
|
7186 G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION
,
7187 "exit-on-close", TRUE
,
7190 g_weak_ref_set (singleton
, ret
);
7194 g_assert (ret
!= NULL
);
7197 G_UNLOCK (message_bus_lock
);
7201 /* May be called from any thread. Must not hold message_bus_lock. */
7203 _g_bus_get_singleton_if_exists (GBusType bus_type
)
7205 GWeakRef
*singleton
;
7206 GDBusConnection
*ret
= NULL
;
7208 G_LOCK (message_bus_lock
);
7209 singleton
= message_bus_get_singleton (bus_type
, NULL
);
7210 if (singleton
== NULL
)
7213 ret
= g_weak_ref_get (singleton
);
7216 G_UNLOCK (message_bus_lock
);
7222 * @bus_type: a #GBusType
7223 * @cancellable: (nullable): a #GCancellable or %NULL
7224 * @error: return location for error or %NULL
7226 * Synchronously connects to the message bus specified by @bus_type.
7227 * Note that the returned object may shared with other callers,
7228 * e.g. if two separate parts of a process calls this function with
7229 * the same @bus_type, they will share the same object.
7231 * This is a synchronous failable function. See g_bus_get() and
7232 * g_bus_get_finish() for the asynchronous version.
7234 * The returned object is a singleton, that is, shared with other
7235 * callers of g_bus_get() and g_bus_get_sync() for @bus_type. In the
7236 * event that you need a private message bus connection, use
7237 * g_dbus_address_get_for_bus_sync() and
7238 * g_dbus_connection_new_for_address().
7240 * Note that the returned #GDBusConnection object will (usually) have
7241 * the #GDBusConnection:exit-on-close property set to %TRUE.
7243 * Returns: (transfer full): a #GDBusConnection or %NULL if @error is set.
7244 * Free with g_object_unref().
7249 g_bus_get_sync (GBusType bus_type
,
7250 GCancellable
*cancellable
,
7253 GDBusConnection
*connection
;
7255 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, NULL
);
7257 connection
= get_uninitialized_connection (bus_type
, cancellable
, error
);
7258 if (connection
== NULL
)
7261 if (!g_initable_init (G_INITABLE (connection
), cancellable
, error
))
7263 g_object_unref (connection
);
7272 bus_get_async_initable_cb (GObject
*source_object
,
7276 GTask
*task
= user_data
;
7277 GError
*error
= NULL
;
7279 if (!g_async_initable_init_finish (G_ASYNC_INITABLE (source_object
),
7283 g_assert (error
!= NULL
);
7284 g_task_return_error (task
, error
);
7285 g_object_unref (source_object
);
7289 g_task_return_pointer (task
, source_object
, g_object_unref
);
7291 g_object_unref (task
);
7296 * @bus_type: a #GBusType
7297 * @cancellable: (nullable): a #GCancellable or %NULL
7298 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7299 * @user_data: the data to pass to @callback
7301 * Asynchronously connects to the message bus specified by @bus_type.
7303 * When the operation is finished, @callback will be invoked. You can
7304 * then call g_bus_get_finish() to get the result of the operation.
7306 * This is a asynchronous failable function. See g_bus_get_sync() for
7307 * the synchronous version.
7312 g_bus_get (GBusType bus_type
,
7313 GCancellable
*cancellable
,
7314 GAsyncReadyCallback callback
,
7317 GDBusConnection
*connection
;
7319 GError
*error
= NULL
;
7321 task
= g_task_new (NULL
, cancellable
, callback
, user_data
);
7322 g_task_set_source_tag (task
, g_bus_get
);
7324 connection
= get_uninitialized_connection (bus_type
, cancellable
, &error
);
7325 if (connection
== NULL
)
7327 g_assert (error
!= NULL
);
7328 g_task_return_error (task
, error
);
7329 g_object_unref (task
);
7333 g_async_initable_init_async (G_ASYNC_INITABLE (connection
),
7336 bus_get_async_initable_cb
,
7343 * @res: a #GAsyncResult obtained from the #GAsyncReadyCallback passed
7345 * @error: return location for error or %NULL
7347 * Finishes an operation started with g_bus_get().
7349 * The returned object is a singleton, that is, shared with other
7350 * callers of g_bus_get() and g_bus_get_sync() for @bus_type. In the
7351 * event that you need a private message bus connection, use
7352 * g_dbus_address_get_for_bus_sync() and
7353 * g_dbus_connection_new_for_address().
7355 * Note that the returned #GDBusConnection object will (usually) have
7356 * the #GDBusConnection:exit-on-close property set to %TRUE.
7358 * Returns: (transfer full): a #GDBusConnection or %NULL if @error is set.
7359 * Free with g_object_unref().
7364 g_bus_get_finish (GAsyncResult
*res
,
7367 g_return_val_if_fail (g_task_is_valid (res
, NULL
), NULL
);
7368 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, NULL
);
7370 return g_task_propagate_pointer (G_TASK (res
), error
);
7373 /* ---------------------------------------------------------------------------------------------------- */