| /* GDBus - GLib D-Bus Library |
| * |
| * Copyright (C) 2008-2010 Red Hat, Inc. |
| * |
| * This library is free software; you can redistribute it and/or |
| * modify it under the terms of the GNU Lesser General Public |
| * License as published by the Free Software Foundation; either |
| * version 2.1 of the License, or (at your option) any later version. |
| * |
| * This library is distributed in the hope that it will be useful, |
| * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| * Lesser General Public License for more details. |
| * |
| * You should have received a copy of the GNU Lesser General |
| * Public License along with this library; if not, see <http://www.gnu.org/licenses/>. |
| * |
| * Author: David Zeuthen <davidz@redhat.com> |
| */ |
| |
| /* |
| * TODO for GDBus: |
| * |
| * - would be nice to expose GDBusAuthMechanism and an extension point |
| * |
| * - Need to rewrite GDBusAuth and rework GDBusAuthMechanism. In particular |
| * the mechanism VFuncs need to be able to set an error. |
| * |
| * - Need to document other mechanisms/sources for determining the D-Bus |
| * address of a well-known bus. |
| * |
| * - e.g. on Win32 we need code like from here |
| * |
| * http://cgit.freedesktop.org/~david/gdbus-standalone/tree/gdbus/gdbusaddress.c#n900 |
| * |
| * that was never copied over here because it originally was copy-paste |
| * from the GPLv2 / AFL 2.1 libdbus sources. |
| * |
| * - on OS X we need to look in launchd for the address |
| * |
| * https://bugs.freedesktop.org/show_bug.cgi?id=14259 |
| * |
| * - on X11 we need to look in a X11 property on the X server |
| * - (we can also just use dbus-launch(1) from the D-Bus |
| * distribution) |
| * |
| * - (ideally) this requires D-Bus spec work because none of |
| * this has never really been specced out properly (except |
| * the X11 bits) |
| * |
| * - Related to the above, we also need to be able to launch a message bus |
| * instance.... Since we don't want to write our own bus daemon we should |
| * launch dbus-daemon(1) (thus: Win32 and OS X need to bundle it) |
| * |
| * - probably want a G_DBUS_NONCE_TCP_TMPDIR environment variable |
| * to specify where the nonce is stored. This will allow people to use |
| * G_DBUS_NONCE_TCP_TMPDIR=/mnt/secure.company.server/dbus-nonce-dir |
| * to easily achieve secure RPC via nonce-tcp. |
| * |
| * - need to expose an extension point for resolving D-Bus address and |
| * turning them into GIOStream objects. This will allow us to implement |
| * e.g. X11 D-Bus transports without dlopen()'ing or linking against |
| * libX11 from libgio. |
| * - see g_dbus_address_connect() in gdbusaddress.c |
| * |
| * - would be cute to use kernel-specific APIs to resolve fds for |
| * debug output when using G_DBUS_DEBUG=message, e.g. in addition to |
| * |
| * fd 21: dev=8:1,mode=0100644,ino=1171231,uid=0,gid=0,rdev=0:0,size=234,atime=1273070640,mtime=1267126160,ctime=1267126160 |
| * |
| * maybe we can show more information about what fd 21 really is. |
| * Ryan suggests looking in /proc/self/fd for clues / symlinks! |
| * Initial experiments on Linux 2.6 suggests that the symlink looks |
| * like this: |
| * |
| * 3 -> /proc/18068/fd |
| * |
| * e.g. not of much use. |
| * |
| * - GDBus High-Level docs |
| * - Proxy: properties, signals... |
| * - Connection: IOStream based, ::close, connection setup steps |
| * mainloop integration, threading |
| * - Differences from libdbus (extend "Migrating from") |
| * - the message handling thread |
| * - Using GVariant instead of GValue |
| * - Explain why the high-level API is a good thing and what |
| * kind of pitfalls it avoids |
| * - Export objects before claiming names |
| * - Talk about auto-starting services (cf. GBusNameWatcherFlags) |
| */ |
| |
| #include "config.h" |
| |
| #include <stdlib.h> |
| #include <string.h> |
| |
| #include "gdbusauth.h" |
| #include "gdbusutils.h" |
| #include "gdbusaddress.h" |
| #include "gdbusmessage.h" |
| #include "gdbusconnection.h" |
| #include "gdbuserror.h" |
| #include "gioenumtypes.h" |
| #include "gdbusintrospection.h" |
| #include "gdbusmethodinvocation.h" |
| #include "gdbusprivate.h" |
| #include "gdbusauthobserver.h" |
| #include "ginitable.h" |
| #include "gasyncinitable.h" |
| #include "giostream.h" |
| #include "gasyncresult.h" |
| #include "gtask.h" |
| |
| #ifdef G_OS_UNIX |
| #include "gunixconnection.h" |
| #include "gunixfdmessage.h" |
| #endif |
| |
| #include "glibintl.h" |
| |
| /** |
| * SECTION:gdbusconnection |
| * @short_description: D-Bus Connections |
| * @include: gio/gio.h |
| * |
| * The #GDBusConnection type is used for D-Bus connections to remote |
| * peers such as a message buses. It is a low-level API that offers a |
| * lot of flexibility. For instance, it lets you establish a connection |
| * over any transport that can by represented as an #GIOStream. |
| * |
| * This class is rarely used directly in D-Bus clients. If you are writing |
| * a D-Bus client, it is often easier to use the g_bus_own_name(), |
| * g_bus_watch_name() or g_dbus_proxy_new_for_bus() APIs. |
| * |
| * As an exception to the usual GLib rule that a particular object must not |
| * be used by two threads at the same time, #GDBusConnection's methods may be |
| * called from any thread. This is so that g_bus_get() and g_bus_get_sync() |
| * can safely return the same #GDBusConnection when called from any thread. |
| * |
| * Most of the ways to obtain a #GDBusConnection automatically initialize it |
| * (i.e. connect to D-Bus): for instance, g_dbus_connection_new() and |
| * g_bus_get(), and the synchronous versions of those methods, give you an |
| * initialized connection. Language bindings for GIO should use |
| * g_initable_new() or g_async_initable_new_async(), which also initialize the |
| * connection. |
| * |
| * If you construct an uninitialized #GDBusConnection, such as via |
| * g_object_new(), you must initialize it via g_initable_init() or |
| * g_async_initable_init_async() before using its methods or properties. |
| * Calling methods or accessing properties on a #GDBusConnection that has not |
| * completed initialization successfully is considered to be invalid, and leads |
| * to undefined behaviour. In particular, if initialization fails with a |
| * #GError, the only valid thing you can do with that #GDBusConnection is to |
| * free it with g_object_unref(). |
| * |
| * ## An example D-Bus server # {#gdbus-server} |
| * |
| * Here is an example for a D-Bus server: |
| * [gdbus-example-server.c](https://git.gnome.org/browse/glib/tree/gio/tests/gdbus-example-server.c) |
| * |
| * ## An example for exporting a subtree # {#gdbus-subtree-server} |
| * |
| * Here is an example for exporting a subtree: |
| * [gdbus-example-subtree.c](https://git.gnome.org/browse/glib/tree/gio/tests/gdbus-example-subtree.c) |
| * |
| * ## An example for file descriptor passing # {#gdbus-unix-fd-client} |
| * |
| * Here is an example for passing UNIX file descriptors: |
| * [gdbus-unix-fd-client.c](https://git.gnome.org/browse/glib/tree/gio/tests/gdbus-example-unix-fd-client.c) |
| * |
| * ## An example for exporting a GObject # {#gdbus-export} |
| * |
| * Here is an example for exporting a #GObject: |
| * [gdbus-example-export.c](https://git.gnome.org/browse/glib/tree/gio/tests/gdbus-example-export.c) |
| */ |
| |
| /* ---------------------------------------------------------------------------------------------------- */ |
| |
| typedef struct _GDBusConnectionClass GDBusConnectionClass; |
| |
| /** |
| * GDBusConnectionClass: |
| * @closed: Signal class handler for the #GDBusConnection::closed signal. |
| * |
| * Class structure for #GDBusConnection. |
| * |
| * Since: 2.26 |
| */ |
| struct _GDBusConnectionClass |
| { |
| /*< private >*/ |
| GObjectClass parent_class; |
| |
| /*< public >*/ |
| /* Signals */ |
| void (*closed) (GDBusConnection *connection, |
| gboolean remote_peer_vanished, |
| GError *error); |
| }; |
| |
| G_LOCK_DEFINE_STATIC (message_bus_lock); |
| |
| static GWeakRef the_session_bus; |
| static GWeakRef the_system_bus; |
| |
| /* Extra pseudo-member of GDBusSendMessageFlags. |
| * Set by initable_init() to indicate that despite not being initialized yet, |
| * enough of the only-valid-after-init members are set that we can send a |
| * message, and we're being called from its thread, so no memory barrier is |
| * required before accessing them. |
| */ |
| #define SEND_MESSAGE_FLAGS_INITIALIZING (1u << 31) |
| |
| /* Same as SEND_MESSAGE_FLAGS_INITIALIZING, but in GDBusCallFlags */ |
| #define CALL_FLAGS_INITIALIZING (1u << 31) |
| |
| /* ---------------------------------------------------------------------------------------------------- */ |
| |
| typedef struct |
| { |
| GDestroyNotify callback; |
| gpointer user_data; |
| GMainContext *context; |
| } CallDestroyNotifyData; |
| |
| static gboolean |
| call_destroy_notify_data_in_idle (gpointer user_data) |
| { |
| CallDestroyNotifyData *data = user_data; |
| data->callback (data->user_data); |
| return FALSE; |
| } |
| |
| static void |
| call_destroy_notify_data_free (CallDestroyNotifyData *data) |
| { |
| if (data->context != NULL) |
| g_main_context_unref (data->context); |
| g_free (data); |
| } |
| |
| /* |
| * call_destroy_notify: <internal> |
| * @context: (nullable): A #GMainContext or %NULL. |
| * @callback: (nullable): A #GDestroyNotify or %NULL. |
| * @user_data: Data to pass to @callback. |
| * |
| * Schedules @callback to run in @context. |
| */ |
| static void |
| call_destroy_notify (GMainContext *context, |
| GDestroyNotify callback, |
| gpointer user_data) |
| { |
| GSource *idle_source; |
| CallDestroyNotifyData *data; |
| |
| if (callback == NULL) |
| goto out; |
| |
| data = g_new0 (CallDestroyNotifyData, 1); |
| data->callback = callback; |
| data->user_data = user_data; |
| data->context = context; |
| if (data->context != NULL) |
| g_main_context_ref (data->context); |
| |
| idle_source = g_idle_source_new (); |
| g_source_set_priority (idle_source, G_PRIORITY_DEFAULT); |
| g_source_set_callback (idle_source, |
| call_destroy_notify_data_in_idle, |
| data, |
| (GDestroyNotify) call_destroy_notify_data_free); |
| g_source_set_name (idle_source, "[gio] call_destroy_notify_data_in_idle"); |
| g_source_attach (idle_source, data->context); |
| g_source_unref (idle_source); |
| |
| out: |
| ; |
| } |
| |
| /* ---------------------------------------------------------------------------------------------------- */ |
| |
| static gboolean |
| _g_strv_has_string (const gchar* const *haystack, |
| const gchar *needle) |
| { |
| guint n; |
| |
| for (n = 0; haystack != NULL && haystack[n] != NULL; n++) |
| { |
| if (g_strcmp0 (haystack[n], needle) == 0) |
| return TRUE; |
| } |
| return FALSE; |
| } |
| |
| /* ---------------------------------------------------------------------------------------------------- */ |
| |
| #ifdef G_OS_WIN32 |
| #define CONNECTION_ENSURE_LOCK(obj) do { ; } while (FALSE) |
| #else |
| // TODO: for some reason this doesn't work on Windows |
| #define CONNECTION_ENSURE_LOCK(obj) do { \ |
| if (G_UNLIKELY (g_mutex_trylock(&(obj)->lock))) \ |
| { \ |
| g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \ |
| "CONNECTION_ENSURE_LOCK: GDBusConnection object lock is not locked"); \ |
| } \ |
| } while (FALSE) |
| #endif |
| |
| #define CONNECTION_LOCK(obj) do { \ |
| g_mutex_lock (&(obj)->lock); \ |
| } while (FALSE) |
| |
| #define CONNECTION_UNLOCK(obj) do { \ |
| g_mutex_unlock (&(obj)->lock); \ |
| } while (FALSE) |
| |
| /* Flags in connection->atomic_flags */ |
| enum { |
| FLAG_INITIALIZED = 1 << 0, |
| FLAG_EXIT_ON_CLOSE = 1 << 1, |
| FLAG_CLOSED = 1 << 2 |
| }; |
| |
| /** |
| * GDBusConnection: |
| * |
| * The #GDBusConnection structure contains only private data and |
| * should only be accessed using the provided API. |
| * |
| * Since: 2.26 |
| */ |
| struct _GDBusConnection |
| { |
| /*< private >*/ |
| GObject parent_instance; |
| |
| /* ------------------------------------------------------------------------ */ |
| /* -- General object state ------------------------------------------------ */ |
| /* ------------------------------------------------------------------------ */ |
| |
| /* General-purpose lock for most fields */ |
| GMutex lock; |
| |
| /* A lock used in the init() method of the GInitable interface - see comments |
| * in initable_init() for why a separate lock is needed. |
| * |
| * If you need both @lock and @init_lock, you must take @init_lock first. |
| */ |
| GMutex init_lock; |
| |
| /* Set (by loading the contents of /var/lib/dbus/machine-id) the first time |
| * someone calls org.freedesktop.DBus.Peer.GetMachineId(). Protected by @lock. |
| */ |
| gchar *machine_id; |
| |
| /* The underlying stream used for communication |
| * Read-only after initable_init(), so it may be read if you either |
| * hold @init_lock or check for initialization first. |
| */ |
| GIOStream *stream; |
| |
| /* The object used for authentication (if any). |
| * Read-only after initable_init(), so it may be read if you either |
| * hold @init_lock or check for initialization first. |
| */ |
| GDBusAuth *auth; |
| |
| /* Last serial used. Protected by @lock. */ |
| guint32 last_serial; |
| |
| /* The object used to send/receive messages. |
| * Read-only after initable_init(), so it may be read if you either |
| * hold @init_lock or check for initialization first. |
| */ |
| GDBusWorker *worker; |
| |
| /* If connected to a message bus, this contains the unique name assigned to |
| * us by the bus (e.g. ":1.42"). |
| * Read-only after initable_init(), so it may be read if you either |
| * hold @init_lock or check for initialization first. |
| */ |
| gchar *bus_unique_name; |
| |
| /* The GUID returned by the other side if we authenticed as a client or |
| * the GUID to use if authenticating as a server. |
| * Read-only after initable_init(), so it may be read if you either |
| * hold @init_lock or check for initialization first. |
| */ |
| gchar *guid; |
| |
| /* FLAG_INITIALIZED is set exactly when initable_init() has finished running. |
| * Inspect @initialization_error to see whether it succeeded or failed. |
| * |
| * FLAG_EXIT_ON_CLOSE is the exit-on-close property. |
| * |
| * FLAG_CLOSED is the closed property. It may be read at any time, but |
| * may only be written while holding @lock. |
| */ |
| volatile gint atomic_flags; |
| |
| /* If the connection could not be established during initable_init(), |
| * this GError will be set. |
| * Read-only after initable_init(), so it may be read if you either |
| * hold @init_lock or check for initialization first. |
| */ |
| GError *initialization_error; |
| |
| /* The result of g_main_context_ref_thread_default() when the object |
| * was created (the GObject _init() function) - this is used for delivery |
| * of the :closed GObject signal. |
| * |
| * Only set in the GObject init function, so no locks are needed. |
| */ |
| GMainContext *main_context_at_construction; |
| |
| /* Read-only construct properties, no locks needed */ |
| gchar *address; |
| GDBusConnectionFlags flags; |
| |
| /* Map used for managing method replies, protected by @lock */ |
| GHashTable *map_method_serial_to_task; /* guint32 -> GTask* */ |
| |
| /* Maps used for managing signal subscription, protected by @lock */ |
| GHashTable *map_rule_to_signal_data; /* match rule (gchar*) -> SignalData */ |
| GHashTable *map_id_to_signal_data; /* id (guint) -> SignalData */ |
| GHashTable *map_sender_unique_name_to_signal_data_array; /* unique sender (gchar*) -> GPtrArray* of SignalData */ |
| |
| /* Maps used for managing exported objects and subtrees, |
| * protected by @lock |
| */ |
| GHashTable *map_object_path_to_eo; /* gchar* -> ExportedObject* */ |
| GHashTable *map_id_to_ei; /* guint -> ExportedInterface* */ |
| GHashTable *map_object_path_to_es; /* gchar* -> ExportedSubtree* */ |
| GHashTable *map_id_to_es; /* guint -> ExportedSubtree* */ |
| |
| /* Map used for storing last used serials for each thread, protected by @lock */ |
| GHashTable *map_thread_to_last_serial; |
| |
| /* Structure used for message filters, protected by @lock */ |
| GPtrArray *filters; |
| |
| /* Capabilities negotiated during authentication |
| * Read-only after initable_init(), so it may be read without holding a |
| * lock, if you check for initialization first. |
| */ |
| GDBusCapabilityFlags capabilities; |
| |
| /* Protected by @init_lock */ |
| GDBusAuthObserver *authentication_observer; |
| |
| /* Read-only after initable_init(), so it may be read if you either |
| * hold @init_lock or check for initialization first. |
| */ |
| GCredentials *credentials; |
| |
| /* set to TRUE when finalizing */ |
| gboolean finalizing; |
| }; |
| |
| typedef struct ExportedObject ExportedObject; |
| static void exported_object_free (ExportedObject *eo); |
| |
| typedef struct ExportedSubtree ExportedSubtree; |
| static void exported_subtree_free (ExportedSubtree *es); |
| |
| enum |
| { |
| CLOSED_SIGNAL, |
| LAST_SIGNAL, |
| }; |
| |
| enum |
| { |
| PROP_0, |
| PROP_STREAM, |
| PROP_ADDRESS, |
| PROP_FLAGS, |
| PROP_GUID, |
| PROP_UNIQUE_NAME, |
| PROP_CLOSED, |
| PROP_EXIT_ON_CLOSE, |
| PROP_CAPABILITY_FLAGS, |
| PROP_AUTHENTICATION_OBSERVER, |
| }; |
| |
| static void distribute_signals (GDBusConnection *connection, |
| GDBusMessage *message); |
| |
| static void distribute_method_call (GDBusConnection *connection, |
| GDBusMessage *message); |
| |
| static gboolean handle_generic_unlocked (GDBusConnection *connection, |
| GDBusMessage *message); |
| |
| |
| static void purge_all_signal_subscriptions (GDBusConnection *connection); |
| static void purge_all_filters (GDBusConnection *connection); |
| |
| static void schedule_method_call (GDBusConnection *connection, |
| GDBusMessage *message, |
| guint registration_id, |
| guint subtree_registration_id, |
| const GDBusInterfaceInfo *interface_info, |
| const GDBusMethodInfo *method_info, |
| const GDBusPropertyInfo *property_info, |
| GVariant *parameters, |
| const GDBusInterfaceVTable *vtable, |
| GMainContext *main_context, |
| gpointer user_data); |
| |
| #define _G_ENSURE_LOCK(name) do { \ |
| if (G_UNLIKELY (G_TRYLOCK(name))) \ |
| { \ |
| g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \ |
| "_G_ENSURE_LOCK: Lock '" #name "' is not locked"); \ |
| } \ |
| } while (FALSE) \ |
| |
| static guint signals[LAST_SIGNAL] = { 0 }; |
| |
| static void initable_iface_init (GInitableIface *initable_iface); |
| static void async_initable_iface_init (GAsyncInitableIface *async_initable_iface); |
| |
| G_DEFINE_TYPE_WITH_CODE (GDBusConnection, g_dbus_connection, G_TYPE_OBJECT, |
| G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init) |
| G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, async_initable_iface_init) |
| ); |
| |
| /* |
| * Check that all members of @connection that can only be accessed after |
| * the connection is initialized can safely be accessed. If not, |
| * log a critical warning. This function is a memory barrier. |
| * |
| * Returns: %TRUE if initialized |
| */ |
| static gboolean |
| check_initialized (GDBusConnection *connection) |
| { |
| /* The access to @atomic_flags isn't conditional, so that this function |
| * provides a memory barrier for thread-safety even if checks are disabled. |
| * (If you don't want this stricter guarantee, you can call |
| * g_return_if_fail (check_initialized (c)).) |
| * |
| * This isn't strictly necessary now that we've decided use of an |
| * uninitialized GDBusConnection is undefined behaviour, but it seems |
| * better to be as deterministic as is feasible. |
| * |
| * (Anything that could suffer a crash from seeing undefined values |
| * must have a race condition - thread A initializes the connection while |
| * thread B calls a method without initialization, hoping that thread A will |
| * win the race - so its behaviour is undefined anyway.) |
| */ |
| gint flags = g_atomic_int_get (&connection->atomic_flags); |
| |
| g_return_val_if_fail (flags & FLAG_INITIALIZED, FALSE); |
| |
| /* We can safely access this, due to the memory barrier above */ |
| g_return_val_if_fail (connection->initialization_error == NULL, FALSE); |
| |
| return TRUE; |
| } |
| |
| typedef enum { |
| MAY_BE_UNINITIALIZED = (1<<1) |
| } CheckUnclosedFlags; |
| |
| /* |
| * Check the same thing as check_initialized(), and also that the |
| * connection is not closed. If the connection is uninitialized, |
| * raise a critical warning (it's programmer error); if it's closed, |
| * raise a recoverable GError (it's a runtime error). |
| * |
| * This function is a memory barrier. |
| * |
| * Returns: %TRUE if initialized and not closed |
| */ |
| static gboolean |
| check_unclosed (GDBusConnection *connection, |
| CheckUnclosedFlags check, |
| GError **error) |
| { |
| /* check_initialized() is effectively inlined, so we don't waste time |
| * doing two memory barriers |
| */ |
| gint flags = g_atomic_int_get (&connection->atomic_flags); |
| |
| if (!(check & MAY_BE_UNINITIALIZED)) |
| { |
| g_return_val_if_fail (flags & FLAG_INITIALIZED, FALSE); |
| g_return_val_if_fail (connection->initialization_error == NULL, FALSE); |
| } |
| |
| if (flags & FLAG_CLOSED) |
| { |
| g_set_error_literal (error, |
| G_IO_ERROR, |
| G_IO_ERROR_CLOSED, |
| _("The connection is closed")); |
| return FALSE; |
| } |
| |
| return TRUE; |
| } |
| |
| static GHashTable *alive_connections = NULL; |
| |
| static void |
| g_dbus_connection_dispose (GObject *object) |
| { |
| GDBusConnection *connection = G_DBUS_CONNECTION (object); |
| |
| G_LOCK (message_bus_lock); |
| CONNECTION_LOCK (connection); |
| if (connection->worker != NULL) |
| { |
| _g_dbus_worker_stop (connection->worker); |
| connection->worker = NULL; |
| if (alive_connections != NULL) |
| g_warn_if_fail (g_hash_table_remove (alive_connections, connection)); |
| } |
| else |
| { |
| if (alive_connections != NULL) |
| g_warn_if_fail (!g_hash_table_contains (alive_connections, connection)); |
| } |
| CONNECTION_UNLOCK (connection); |
| G_UNLOCK (message_bus_lock); |
| |
| if (G_OBJECT_CLASS (g_dbus_connection_parent_class)->dispose != NULL) |
| G_OBJECT_CLASS (g_dbus_connection_parent_class)->dispose (object); |
| } |
| |
| static void |
| g_dbus_connection_finalize (GObject *object) |
| { |
| GDBusConnection *connection = G_DBUS_CONNECTION (object); |
| |
| connection->finalizing = TRUE; |
| |
| purge_all_signal_subscriptions (connection); |
| |
| purge_all_filters (connection); |
| g_ptr_array_unref (connection->filters); |
| |
| if (connection->authentication_observer != NULL) |
| g_object_unref (connection->authentication_observer); |
| |
| if (connection->auth != NULL) |
| g_object_unref (connection->auth); |
| |
| if (connection->credentials) |
| g_object_unref (connection->credentials); |
| |
| if (connection->stream != NULL) |
| { |
| g_object_unref (connection->stream); |
| connection->stream = NULL; |
| } |
| |
| g_free (connection->address); |
| |
| g_free (connection->guid); |
| g_free (connection->bus_unique_name); |
| |
| if (connection->initialization_error != NULL) |
| g_error_free (connection->initialization_error); |
| |
| g_hash_table_unref (connection->map_method_serial_to_task); |
| |
| g_hash_table_unref (connection->map_rule_to_signal_data); |
| g_hash_table_unref (connection->map_id_to_signal_data); |
| g_hash_table_unref (connection->map_sender_unique_name_to_signal_data_array); |
| |
| g_hash_table_unref (connection->map_id_to_ei); |
| g_hash_table_unref (connection->map_object_path_to_eo); |
| g_hash_table_unref (connection->map_id_to_es); |
| g_hash_table_unref (connection->map_object_path_to_es); |
| |
| g_hash_table_unref (connection->map_thread_to_last_serial); |
| |
| g_main_context_unref (connection->main_context_at_construction); |
| |
| g_free (connection->machine_id); |
| |
| g_mutex_clear (&connection->init_lock); |
| g_mutex_clear (&connection->lock); |
| |
| G_OBJECT_CLASS (g_dbus_connection_parent_class)->finalize (object); |
| } |
| |
| /* called in any user thread, with the connection's lock not held */ |
| static void |
| g_dbus_connection_get_property (GObject *object, |
| guint prop_id, |
| GValue *value, |
| GParamSpec *pspec) |
| { |
| GDBusConnection *connection = G_DBUS_CONNECTION (object); |
| |
| switch (prop_id) |
| { |
| case PROP_STREAM: |
| g_value_set_object (value, g_dbus_connection_get_stream (connection)); |
| break; |
| |
| case PROP_GUID: |
| g_value_set_string (value, g_dbus_connection_get_guid (connection)); |
| break; |
| |
| case PROP_UNIQUE_NAME: |
| g_value_set_string (value, g_dbus_connection_get_unique_name (connection)); |
| break; |
| |
| case PROP_CLOSED: |
| g_value_set_boolean (value, g_dbus_connection_is_closed (connection)); |
| break; |
| |
| case PROP_EXIT_ON_CLOSE: |
| g_value_set_boolean (value, g_dbus_connection_get_exit_on_close (connection)); |
| break; |
| |
| case PROP_CAPABILITY_FLAGS: |
| g_value_set_flags (value, g_dbus_connection_get_capabilities (connection)); |
| break; |
| |
| default: |
| G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| break; |
| } |
| } |
| |
| /* called in any user thread, with the connection's lock not held */ |
| static void |
| g_dbus_connection_set_property (GObject *object, |
| guint prop_id, |
| const GValue *value, |
| GParamSpec *pspec) |
| { |
| GDBusConnection *connection = G_DBUS_CONNECTION (object); |
| |
| switch (prop_id) |
| { |
| case PROP_STREAM: |
| connection->stream = g_value_dup_object (value); |
| break; |
| |
| case PROP_GUID: |
| connection->guid = g_value_dup_string (value); |
| break; |
| |
| case PROP_ADDRESS: |
| connection->address = g_value_dup_string (value); |
| break; |
| |
| case PROP_FLAGS: |
| connection->flags = g_value_get_flags (value); |
| break; |
| |
| case PROP_EXIT_ON_CLOSE: |
| g_dbus_connection_set_exit_on_close (connection, g_value_get_boolean (value)); |
| break; |
| |
| case PROP_AUTHENTICATION_OBSERVER: |
| connection->authentication_observer = g_value_dup_object (value); |
| break; |
| |
| default: |
| G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| break; |
| } |
| } |
| |
| /* Base-class implementation of GDBusConnection::closed. |
| * |
| * Called in a user thread, by the main context that was thread-default when |
| * the object was constructed. |
| */ |
| static void |
| g_dbus_connection_real_closed (GDBusConnection *connection, |
| gboolean remote_peer_vanished, |
| GError *error) |
| { |
| gint flags = g_atomic_int_get (&connection->atomic_flags); |
| |
| /* Because atomic int access is a memory barrier, we can safely read |
| * initialization_error without a lock, as long as we do it afterwards. |
| */ |
| if (remote_peer_vanished && |
| (flags & FLAG_EXIT_ON_CLOSE) != 0 && |
| (flags & FLAG_INITIALIZED) != 0 && |
| connection->initialization_error == NULL) |
| { |
| raise (SIGTERM); |
| } |
| } |
| |
| static void |
| g_dbus_connection_class_init (GDBusConnectionClass *klass) |
| { |
| GObjectClass *gobject_class; |
| |
| gobject_class = G_OBJECT_CLASS (klass); |
| |
| gobject_class->finalize = g_dbus_connection_finalize; |
| gobject_class->dispose = g_dbus_connection_dispose; |
| gobject_class->set_property = g_dbus_connection_set_property; |
| gobject_class->get_property = g_dbus_connection_get_property; |
| |
| klass->closed = g_dbus_connection_real_closed; |
| |
| /** |
| * GDBusConnection:stream: |
| * |
| * The underlying #GIOStream used for I/O. |
| * |
| * If this is passed on construction and is a #GSocketConnection, |
| * then the corresponding #GSocket will be put into non-blocking mode. |
| * |
| * While the #GDBusConnection is active, it will interact with this |
| * stream from a worker thread, so it is not safe to interact with |
| * the stream directly. |
| * |
| * Since: 2.26 |
| */ |
| g_object_class_install_property (gobject_class, |
| PROP_STREAM, |
| g_param_spec_object ("stream", |
| P_("IO Stream"), |
| P_("The underlying streams used for I/O"), |
| G_TYPE_IO_STREAM, |
| G_PARAM_READABLE | |
| G_PARAM_WRITABLE | |
| G_PARAM_CONSTRUCT_ONLY | |
| G_PARAM_STATIC_NAME | |
| G_PARAM_STATIC_BLURB | |
| G_PARAM_STATIC_NICK)); |
| |
| /** |
| * GDBusConnection:address: |
| * |
| * A D-Bus address specifying potential endpoints that can be used |
| * when establishing the connection. |
| * |
| * Since: 2.26 |
| */ |
| g_object_class_install_property (gobject_class, |
| PROP_ADDRESS, |
| g_param_spec_string ("address", |
| P_("Address"), |
| P_("D-Bus address specifying potential socket endpoints"), |
| NULL, |
| G_PARAM_WRITABLE | |
| G_PARAM_CONSTRUCT_ONLY | |
| G_PARAM_STATIC_NAME | |
| G_PARAM_STATIC_BLURB | |
| G_PARAM_STATIC_NICK)); |
| |
| /** |
| * GDBusConnection:flags: |
| * |
| * Flags from the #GDBusConnectionFlags enumeration. |
| * |
| * Since: 2.26 |
| */ |
| g_object_class_install_property (gobject_class, |
| PROP_FLAGS, |
| g_param_spec_flags ("flags", |
| P_("Flags"), |
| P_("Flags"), |
| G_TYPE_DBUS_CONNECTION_FLAGS, |
| G_DBUS_CONNECTION_FLAGS_NONE, |
| G_PARAM_WRITABLE | |
| G_PARAM_CONSTRUCT_ONLY | |
| G_PARAM_STATIC_NAME | |
| G_PARAM_STATIC_BLURB | |
| G_PARAM_STATIC_NICK)); |
| |
| /** |
| * GDBusConnection:guid: |
| * |
| * The GUID of the peer performing the role of server when |
| * authenticating. |
| * |
| * If you are constructing a #GDBusConnection and pass |
| * %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER in the |
| * #GDBusConnection:flags property then you MUST also set this |
| * property to a valid guid. |
| * |
| * If you are constructing a #GDBusConnection and pass |
| * %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT in the |
| * #GDBusConnection:flags property you will be able to read the GUID |
| * of the other peer here after the connection has been successfully |
| * initialized. |
| * |
| * Since: 2.26 |
| */ |
| g_object_class_install_property (gobject_class, |
| PROP_GUID, |
| g_param_spec_string ("guid", |
| P_("GUID"), |
| P_("GUID of the server peer"), |
| NULL, |
| G_PARAM_READABLE | |
| G_PARAM_WRITABLE | |
| G_PARAM_CONSTRUCT_ONLY | |
| G_PARAM_STATIC_NAME | |
| G_PARAM_STATIC_BLURB | |
| G_PARAM_STATIC_NICK)); |
| |
| /** |
| * GDBusConnection:unique-name: |
| * |
| * The unique name as assigned by the message bus or %NULL if the |
| * connection is not open or not a message bus connection. |
| * |
| * Since: 2.26 |
| */ |
| g_object_class_install_property (gobject_class, |
| PROP_UNIQUE_NAME, |
| g_param_spec_string ("unique-name", |
| P_("unique-name"), |
| P_("Unique name of bus connection"), |
| NULL, |
| G_PARAM_READABLE | |
| G_PARAM_STATIC_NAME | |
| G_PARAM_STATIC_BLURB | |
| G_PARAM_STATIC_NICK)); |
| |
| /** |
| * GDBusConnection:closed: |
| * |
| * A boolean specifying whether the connection has been closed. |
| * |
| * Since: 2.26 |
| */ |
| g_object_class_install_property (gobject_class, |
| PROP_CLOSED, |
| g_param_spec_boolean ("closed", |
| P_("Closed"), |
| P_("Whether the connection is closed"), |
| FALSE, |
| G_PARAM_READABLE | |
| G_PARAM_STATIC_NAME | |
| G_PARAM_STATIC_BLURB | |
| G_PARAM_STATIC_NICK)); |
| |
| /** |
| * GDBusConnection:exit-on-close: |
| * |
| * A boolean specifying whether the process will be terminated (by |
| * calling `raise(SIGTERM)`) if the connection is closed by the |
| * remote peer. |
| * |
| * Note that #GDBusConnection objects returned by g_bus_get_finish() |
| * and g_bus_get_sync() will (usually) have this property set to %TRUE. |
| * |
| * Since: 2.26 |
| */ |
| g_object_class_install_property (gobject_class, |
| PROP_EXIT_ON_CLOSE, |
| g_param_spec_boolean ("exit-on-close", |
| P_("Exit on close"), |
| P_("Whether the process is terminated when the connection is closed"), |
| FALSE, |
| G_PARAM_READABLE | |
| G_PARAM_WRITABLE | |
| G_PARAM_STATIC_NAME | |
| G_PARAM_STATIC_BLURB | |
| G_PARAM_STATIC_NICK)); |
| |
| /** |
| * GDBusConnection:capabilities: |
| * |
| * Flags from the #GDBusCapabilityFlags enumeration |
| * representing connection features negotiated with the other peer. |
| * |
| * Since: 2.26 |
| */ |
| g_object_class_install_property (gobject_class, |
| PROP_CAPABILITY_FLAGS, |
| g_param_spec_flags ("capabilities", |
| P_("Capabilities"), |
| P_("Capabilities"), |
| G_TYPE_DBUS_CAPABILITY_FLAGS, |
| G_DBUS_CAPABILITY_FLAGS_NONE, |
| G_PARAM_READABLE | |
| G_PARAM_STATIC_NAME | |
| G_PARAM_STATIC_BLURB | |
| G_PARAM_STATIC_NICK)); |
| |
| /** |
| * GDBusConnection:authentication-observer: |
| * |
| * A #GDBusAuthObserver object to assist in the authentication process or %NULL. |
| * |
| * Since: 2.26 |
| */ |
| g_object_class_install_property (gobject_class, |
| PROP_AUTHENTICATION_OBSERVER, |
| g_param_spec_object ("authentication-observer", |
| P_("Authentication Observer"), |
| P_("Object used to assist in the authentication process"), |
| G_TYPE_DBUS_AUTH_OBSERVER, |
| G_PARAM_WRITABLE | |
| G_PARAM_CONSTRUCT_ONLY | |
| G_PARAM_STATIC_NAME | |
| G_PARAM_STATIC_BLURB | |
| G_PARAM_STATIC_NICK)); |
| |
| /** |
| * GDBusConnection::closed: |
| * @connection: the #GDBusConnection emitting the signal |
| * @remote_peer_vanished: %TRUE if @connection is closed because the |
| * remote peer closed its end of the connection |
| * @error: (nullable): a #GError with more details about the event or %NULL |
| * |
| * Emitted when the connection is closed. |
| * |
| * The cause of this event can be |
| * |
| * - If g_dbus_connection_close() is called. In this case |
| * @remote_peer_vanished is set to %FALSE and @error is %NULL. |
| * |
| * - If the remote peer closes the connection. In this case |
| * @remote_peer_vanished is set to %TRUE and @error is set. |
| * |
| * - If the remote peer sends invalid or malformed data. In this |
| * case @remote_peer_vanished is set to %FALSE and @error is set. |
| * |
| * Upon receiving this signal, you should give up your reference to |
| * @connection. You are guaranteed that this signal is emitted only |
| * once. |
| * |
| * Since: 2.26 |
| */ |
| signals[CLOSED_SIGNAL] = g_signal_new (I_("closed"), |
| G_TYPE_DBUS_CONNECTION, |
| G_SIGNAL_RUN_LAST, |
| G_STRUCT_OFFSET (GDBusConnectionClass, closed), |
| NULL, |
| NULL, |
| NULL, |
| G_TYPE_NONE, |
| 2, |
| G_TYPE_BOOLEAN, |
| G_TYPE_ERROR); |
| } |
| |
| static void |
| g_dbus_connection_init (GDBusConnection *connection) |
| { |
| g_mutex_init (&connection->lock); |
| g_mutex_init (&connection->init_lock); |
| |
| connection->map_method_serial_to_task = g_hash_table_new (g_direct_hash, g_direct_equal); |
| |
| connection->map_rule_to_signal_data = g_hash_table_new (g_str_hash, |
| g_str_equal); |
| connection->map_id_to_signal_data = g_hash_table_new (g_direct_hash, |
| g_direct_equal); |
| connection->map_sender_unique_name_to_signal_data_array = g_hash_table_new_full (g_str_hash, |
| g_str_equal, |
| g_free, |
| (GDestroyNotify) g_ptr_array_unref); |
| |
| connection->map_object_path_to_eo = g_hash_table_new_full (g_str_hash, |
| g_str_equal, |
| NULL, |
| (GDestroyNotify) exported_object_free); |
| |
| connection->map_id_to_ei = g_hash_table_new (g_direct_hash, |
| g_direct_equal); |
| |
| connection->map_object_path_to_es = g_hash_table_new_full (g_str_hash, |
| g_str_equal, |
| NULL, |
| (GDestroyNotify) exported_subtree_free); |
| |
| connection->map_id_to_es = g_hash_table_new (g_direct_hash, |
| g_direct_equal); |
| |
| connection->map_thread_to_last_serial = g_hash_table_new (g_direct_hash, |
| g_direct_equal); |
| |
| connection->main_context_at_construction = g_main_context_ref_thread_default (); |
| |
| connection->filters = g_ptr_array_new (); |
| } |
| |
| /** |
| * g_dbus_connection_get_stream: |
| * @connection: a #GDBusConnection |
| * |
| * Gets the underlying stream used for IO. |
| * |
| * While the #GDBusConnection is active, it will interact with this |
| * stream from a worker thread, so it is not safe to interact with |
| * the stream directly. |
| * |
| * Returns: (transfer none): the stream used for IO |
| * |
| * Since: 2.26 |
| */ |
| GIOStream * |
| g_dbus_connection_get_stream (GDBusConnection *connection) |
| { |
| g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL); |
| |
| /* do not use g_return_val_if_fail(), we want the memory barrier */ |
| if (!check_initialized (connection)) |
| return NULL; |
| |
| return connection->stream; |
| } |
| |
| /** |
| * g_dbus_connection_start_message_processing: |
| * @connection: a #GDBusConnection |
| * |
| * If @connection was created with |
| * %G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING, this method |
| * starts processing messages. Does nothing on if @connection wasn't |
| * created with this flag or if the method has already been called. |
| * |
| * Since: 2.26 |
| */ |
| void |
| g_dbus_connection_start_message_processing (GDBusConnection *connection) |
| { |
| g_return_if_fail (G_IS_DBUS_CONNECTION (connection)); |
| |
| /* do not use g_return_val_if_fail(), we want the memory barrier */ |
| if (!check_initialized (connection)) |
| return; |
| |
| g_assert (connection->worker != NULL); |
| _g_dbus_worker_unfreeze (connection->worker); |
| } |
| |
| /** |
| * g_dbus_connection_is_closed: |
| * @connection: a #GDBusConnection |
| * |
| * Gets whether @connection is closed. |
| * |
| * Returns: %TRUE if the connection is closed, %FALSE otherwise |
| * |
| * Since: 2.26 |
| */ |
| gboolean |
| g_dbus_connection_is_closed (GDBusConnection *connection) |
| { |
| gint flags; |
| |
| g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE); |
| |
| flags = g_atomic_int_get (&connection->atomic_flags); |
| |
| return (flags & FLAG_CLOSED) ? TRUE : FALSE; |
| } |
| |
| /** |
| * g_dbus_connection_get_capabilities: |
| * @connection: a #GDBusConnection |
| * |
| * Gets the capabilities negotiated with the remote peer |
| * |
| * Returns: zero or more flags from the #GDBusCapabilityFlags enumeration |
| * |
| * Since: 2.26 |
| */ |
| GDBusCapabilityFlags |
| g_dbus_connection_get_capabilities (GDBusConnection *connection) |
| { |
| g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), G_DBUS_CAPABILITY_FLAGS_NONE); |
| |
| /* do not use g_return_val_if_fail(), we want the memory barrier */ |
| if (!check_initialized (connection)) |
| return G_DBUS_CAPABILITY_FLAGS_NONE; |
| |
| return connection->capabilities; |
| } |
| |
| /* ---------------------------------------------------------------------------------------------------- */ |
| |
| /* Called in a temporary thread without holding locks. */ |
| static void |
| flush_in_thread_func (GTask *task, |
| gpointer source_object, |
| gpointer task_data, |
| GCancellable *cancellable) |
| { |
| GError *error = NULL; |
| |
| if (g_dbus_connection_flush_sync (source_object, |
| cancellable, |
| &error)) |
| g_task_return_boolean (task, TRUE); |
| else |
| g_task_return_error (task, error); |
| } |
| |
| /** |
| * g_dbus_connection_flush: |
| * @connection: a #GDBusConnection |
| * @cancellable: (nullable): a #GCancellable or %NULL |
| * @callback: (nullable): a #GAsyncReadyCallback to call when the |
| * request is satisfied or %NULL if you don't care about the result |
| * @user_data: The data to pass to @callback |
| * |
| * Asynchronously flushes @connection, that is, writes all queued |
| * outgoing message to the transport and then flushes the transport |
| * (using g_output_stream_flush_async()). This is useful in programs |
| * that wants to emit a D-Bus signal and then exit immediately. Without |
| * flushing the connection, there is no guaranteed that the message has |
| * been sent to the networking buffers in the OS kernel. |
| * |
| * This is an asynchronous method. When the operation is finished, |
| * @callback will be invoked in the |
| * [thread-default main context][g-main-context-push-thread-default] |
| * of the thread you are calling this method from. You can |
| * then call g_dbus_connection_flush_finish() to get the result of the |
| * operation. See g_dbus_connection_flush_sync() for the synchronous |
| * version. |
| * |
| * Since: 2.26 |
| */ |
| void |
| g_dbus_connection_flush (GDBusConnection *connection, |
| GCancellable *cancellable, |
| GAsyncReadyCallback callback, |
| gpointer user_data) |
| { |
| GTask *task; |
| |
| g_return_if_fail (G_IS_DBUS_CONNECTION (connection)); |
| |
| task = g_task_new (connection, cancellable, callback, user_data); |
| g_task_set_source_tag (task, g_dbus_connection_flush); |
| g_task_run_in_thread (task, flush_in_thread_func); |
| g_object_unref (task); |
| } |
| |
| /** |
| * g_dbus_connection_flush_finish: |
| * @connection: a #GDBusConnection |
| * @res: a #GAsyncResult obtained from the #GAsyncReadyCallback passed |
| * to g_dbus_connection_flush() |
| * @error: return location for error or %NULL |
| * |
| * Finishes an operation started with g_dbus_connection_flush(). |
| * |
| * Returns: %TRUE if the operation succeeded, %FALSE if @error is set |
| * |
| * Since: 2.26 |
| */ |
| gboolean |
| g_dbus_connection_flush_finish (GDBusConnection *connection, |
| GAsyncResult *res, |
| GError **error) |
| { |
| g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE); |
| g_return_val_if_fail (g_task_is_valid (res, connection), FALSE); |
| g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
| |
| return g_task_propagate_boolean (G_TASK (res), error); |
| } |
| |
| /** |
| * g_dbus_connection_flush_sync: |
| * @connection: a #GDBusConnection |
| * @cancellable: (nullable): a #GCancellable or %NULL |
| * @error: return location for error or %NULL |
| * |
| * Synchronously flushes @connection. The calling thread is blocked |
| * until this is done. See g_dbus_connection_flush() for the |
| * asynchronous version of this method and more details about what it |
| * does. |
| * |
| * Returns: %TRUE if the operation succeeded, %FALSE if @error is set |
| * |
| * Since: 2.26 |
| */ |
| gboolean |
| g_dbus_connection_flush_sync (GDBusConnection *connection, |
| GCancellable *cancellable, |
| GError **error) |
| { |
| gboolean ret; |
| |
| g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE); |
| g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
| |
| ret = FALSE; |
| |
| /* This is only a best-effort attempt to see whether the connection is |
| * closed, so it doesn't need the lock. If the connection closes just |
| * after this check, but before scheduling the flush operation, the |
| * result will be more or less the same as if the connection closed while |
| * the flush operation was pending - it'll fail with either CLOSED or |
| * CANCELLED. |
| */ |
| if (!check_unclosed (connection, 0, error)) |
| goto out; |
| |
| g_assert (connection->worker != NULL); |
| |
| ret = _g_dbus_worker_flush_sync (connection->worker, |
| cancellable, |
| error); |
| |
| out: |
| return ret; |
| } |
| |
| /* ---------------------------------------------------------------------------------------------------- */ |
| |
| typedef struct |
| { |
| GDBusConnection *connection; |
| GError *error; |
| gboolean remote_peer_vanished; |
| } EmitClosedData; |
| |
| static void |
| emit_closed_data_free (EmitClosedData *data) |
| { |
| g_object_unref (data->connection); |
| if (data->error != NULL) |
| g_error_free (data->error); |
| g_free (data); |
| } |
| |
| /* Called in a user thread that has acquired the main context that was |
| * thread-default when the object was constructed |
| */ |
| static gboolean |
| emit_closed_in_idle (gpointer user_data) |
| { |
| EmitClosedData *data = user_data; |
| gboolean result; |
| |
| g_object_notify (G_OBJECT (data->connection), "closed"); |
| g_signal_emit (data->connection, |
| signals[CLOSED_SIGNAL], |
| 0, |
| data->remote_peer_vanished, |
| data->error, |
| &result); |
| return FALSE; |
| } |
| |
| /* Can be called from any thread, must hold lock. |
| * FLAG_CLOSED must already have been set. |
| */ |
| static void |
| schedule_closed_unlocked (GDBusConnection *connection, |
| gboolean remote_peer_vanished, |
| GError *error) |
| { |
| GSource *idle_source; |
| EmitClosedData *data; |
| |
| CONNECTION_ENSURE_LOCK (connection); |
| |
| data = g_new0 (EmitClosedData, 1); |
| data->connection = g_object_ref (connection); |
| data->remote_peer_vanished = remote_peer_vanished; |
| data->error = error != NULL ? g_error_copy (error) : NULL; |
| |
| idle_source = g_idle_source_new (); |
| g_source_set_priority (idle_source, G_PRIORITY_DEFAULT); |
| g_source_set_callback (idle_source, |
| emit_closed_in_idle, |
| data, |
| (GDestroyNotify) emit_closed_data_free); |
| g_source_set_name (idle_source, "[gio] emit_closed_in_idle"); |
| g_source_attach (idle_source, connection->main_context_at_construction); |
| g_source_unref (idle_source); |
| } |
| |
| /* ---------------------------------------------------------------------------------------------------- */ |
| |
| /** |
| * g_dbus_connection_close: |
| * @connection: a #GDBusConnection |
| * @cancellable: (nullable): a #GCancellable or %NULL |
| * @callback: (nullable): a #GAsyncReadyCallback to call when the request is |
| * satisfied or %NULL if you don't care about the result |
| * @user_data: The data to pass to @callback |
| * |
| * Closes @connection. Note that this never causes the process to |
| * exit (this might only happen if the other end of a shared message |
| * bus connection disconnects, see #GDBusConnection:exit-on-close). |
| * |
| * Once the connection is closed, operations such as sending a message |
| * will return with the error %G_IO_ERROR_CLOSED. Closing a connection |
| * will not automatically flush the connection so queued messages may |
| * be lost. Use g_dbus_connection_flush() if you need such guarantees. |
| * |
| * If @connection is already closed, this method fails with |
| * %G_IO_ERROR_CLOSED. |
| * |
| * When @connection has been closed, the #GDBusConnection::closed |
| * signal is emitted in the |
| * [thread-default main context][g-main-context-push-thread-default] |
| * of the thread that @connection was constructed in. |
| * |
| * This is an asynchronous method. When the operation is finished, |
| * @callback will be invoked in the |
| * [thread-default main context][g-main-context-push-thread-default] |
| * of the thread you are calling this method from. You can |
| * then call g_dbus_connection_close_finish() to get the result of the |
| * operation. See g_dbus_connection_close_sync() for the synchronous |
| * version. |
| * |
| * Since: 2.26 |
| */ |
| void |
| g_dbus_connection_close (GDBusConnection *connection, |
| GCancellable *cancellable, |
| GAsyncReadyCallback callback, |
| gpointer user_data) |
| { |
| GTask *task; |
| |
| g_return_if_fail (G_IS_DBUS_CONNECTION (connection)); |
| |
| /* do not use g_return_val_if_fail(), we want the memory barrier */ |
| if (!check_initialized (connection)) |
| return; |
| |
| g_assert (connection->worker != NULL); |
| |
| task = g_task_new (connection, cancellable, callback, user_data); |
| g_task_set_source_tag (task, g_dbus_connection_close); |
| _g_dbus_worker_close (connection->worker, task); |
| g_object_unref (task); |
| } |
| |
| /** |
| * g_dbus_connection_close_finish: |
| * @connection: a #GDBusConnection |
| * @res: a #GAsyncResult obtained from the #GAsyncReadyCallback passed |
| * to g_dbus_connection_close() |
| * @error: return location for error or %NULL |
| * |
| * Finishes an operation started with g_dbus_connection_close(). |
| * |
| * Returns: %TRUE if the operation succeeded, %FALSE if @error is set |
| * |
| * Since: 2.26 |
| */ |
| gboolean |
| g_dbus_connection_close_finish (GDBusConnection *connection, |
| GAsyncResult *res, |
| GError **error) |
| { |
| g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE); |
| g_return_val_if_fail (g_task_is_valid (res, connection), FALSE); |
| g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
| |
| return g_task_propagate_boolean (G_TASK (res), error); |
| } |
| |
| typedef struct { |
| GMainLoop *loop; |
| GAsyncResult *result; |
| } SyncCloseData; |
| |
| /* Can be called by any thread, without the connection lock */ |
| static void |
| sync_close_cb (GObject *source_object, |
| GAsyncResult *res, |
| gpointer user_data) |
| { |
| SyncCloseData *data = user_data; |
| |
| data->result = g_object_ref (res); |
| g_main_loop_quit (data->loop); |
| } |
| |
| /** |
| * g_dbus_connection_close_sync: |
| * @connection: a #GDBusConnection |
| * @cancellable: (nullable): a #GCancellable or %NULL |
| * @error: return location for error or %NULL |
| * |
| * Synchronously closes @connection. The calling thread is blocked |
| * until this is done. See g_dbus_connection_close() for the |
| * asynchronous version of this method and more details about what it |
| * does. |
| * |
| * Returns: %TRUE if the operation succeeded, %FALSE if @error is set |
| * |
| * Since: 2.26 |
| */ |
| gboolean |
| g_dbus_connection_close_sync (GDBusConnection *connection, |
| GCancellable *cancellable, |
| GError **error) |
| { |
| gboolean ret; |
| |
| g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE); |
| g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
| |
| ret = FALSE; |
| |
| if (check_unclosed (connection, 0, error)) |
| { |
| GMainContext *context; |
| SyncCloseData data; |
| |
| context = g_main_context_new (); |
| g_main_context_push_thread_default (context); |
| data.loop = g_main_loop_new (context, TRUE); |
| data.result = NULL; |
| |
| g_dbus_connection_close (connection, cancellable, sync_close_cb, &data); |
| g_main_loop_run (data.loop); |
| ret = g_dbus_connection_close_finish (connection, data.result, error); |
| |
| g_object_unref (data.result); |
| g_main_loop_unref (data.loop); |
| g_main_context_pop_thread_default (context); |
| g_main_context_unref (context); |
| } |
| |
| return ret; |
| } |
| |
| /* ---------------------------------------------------------------------------------------------------- */ |
| |
| /** |
| * g_dbus_connection_get_last_serial: |
| * @connection: a #GDBusConnection |
| * |
| * Retrieves the last serial number assigned to a #GDBusMessage on |
| * the current thread. This includes messages sent via both low-level |
| * API such as g_dbus_connection_send_message() as well as |
| * high-level API such as g_dbus_connection_emit_signal(), |
| * g_dbus_connection_call() or g_dbus_proxy_call(). |
| * |
| * Returns: the last used serial or zero when no message has been sent |
| * within the current thread |
| * |
| * Since: 2.34 |
| */ |
| guint32 |
| g_dbus_connection_get_last_serial (GDBusConnection *connection) |
| { |
| guint32 ret; |
| |
| g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), 0); |
| |
| CONNECTION_LOCK (connection); |
| ret = GPOINTER_TO_UINT (g_hash_table_lookup (connection->map_thread_to_last_serial, |
| g_thread_self ())); |
| CONNECTION_UNLOCK (connection); |
| |
| return ret; |
| } |
| |
| /* ---------------------------------------------------------------------------------------------------- */ |
| |
| /* Can be called by any thread, with the connection lock held */ |
| static gboolean |
| g_dbus_connection_send_message_unlocked (GDBusConnection *connection, |
| GDBusMessage *message, |
| GDBusSendMessageFlags flags, |
| volatile guint32 *out_serial, |
| GError **error) |
| { |
| guchar *blob; |
| gsize blob_size; |
| guint32 serial_to_use; |
| gboolean ret; |
| |
| CONNECTION_ENSURE_LOCK (connection); |
| |
| g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE); |
| g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), FALSE); |
| |
| /* TODO: check all necessary headers are present */ |
| |
| ret = FALSE; |
| blob = NULL; |
| |
| if (out_serial != NULL) |
| *out_serial = 0; |
| |
| /* If we're in initable_init(), don't check for being initialized, to avoid |
| * chicken-and-egg problems. initable_init() is responsible for setting up |
| * our prerequisites (mainly connection->worker), and only calling us |
| * from its own thread (so no memory barrier is needed). |
| */ |
| if (!check_unclosed (connection, |
| (flags & SEND_MESSAGE_FLAGS_INITIALIZING) ? MAY_BE_UNINITIALIZED : 0, |
| error)) |
| goto out; |
| |
| blob = g_dbus_message_to_blob (message, |
| &blob_size, |
| connection->capabilities, |
| error); |
| if (blob == NULL) |
| goto out; |
| |
| if (flags & G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL) |
| serial_to_use = g_dbus_message_get_serial (message); |
| else |
| serial_to_use = ++connection->last_serial; /* TODO: handle overflow */ |
| |
| switch (blob[0]) |
| { |
| case 'l': |
| ((guint32 *) blob)[2] = GUINT32_TO_LE (serial_to_use); |
| break; |
| case 'B': |
| ((guint32 *) blob)[2] = GUINT32_TO_BE (serial_to_use); |
| break; |
| default: |
| g_assert_not_reached (); |
| break; |
| } |
| |
| #if 0 |
| g_printerr ("Writing message of %" G_GSIZE_FORMAT " bytes (serial %d) on %p:\n", |
| blob_size, serial_to_use, connection); |
| g_printerr ("----\n"); |
| hexdump (blob, blob_size); |
| g_printerr ("----\n"); |
| #endif |
| |
| /* TODO: use connection->auth to encode the blob */ |
| |
| if (out_serial != NULL) |
| *out_serial = serial_to_use; |
| |
| /* store used serial for the current thread */ |
| /* TODO: watch the thread disposal and remove associated record |
| * from hashtable |
| * - see https://bugzilla.gnome.org/show_bug.cgi?id=676825#c7 |
| */ |
| g_hash_table_replace (connection->map_thread_to_last_serial, |
| g_thread_self (), |
| GUINT_TO_POINTER (serial_to_use)); |
| |
| if (!(flags & G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL)) |
| g_dbus_message_set_serial (message, serial_to_use); |
| |
| g_dbus_message_lock (message); |
| _g_dbus_worker_send_message (connection->worker, |
| message, |
| (gchar*) blob, |
| blob_size); |
| blob = NULL; /* since _g_dbus_worker_send_message() steals the blob */ |
| |
| ret = TRUE; |
| |
| out: |
| g_free (blob); |
| |
| return ret; |
| } |
| |
| /** |
| * g_dbus_connection_send_message: |
| * @connection: a #GDBusConnection |
| * @message: a #GDBusMessage |
| * @flags: flags affecting how the message is sent |
| * @out_serial: (out) (optional): return location for serial number assigned |
| * to @message when sending it or %NULL |
| * @error: Return location for error or %NULL |
| * |
| * Asynchronously sends @message to the peer represented by @connection. |
| * |
| * Unless @flags contain the |
| * %G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL flag, the serial number |
| * will be assigned by @connection and set on @message via |
| * g_dbus_message_set_serial(). If @out_serial is not %NULL, then the |
| * serial number used will be written to this location prior to |
| * submitting the message to the underlying transport. |
| * |
| * If @connection is closed then the operation will fail with |
| * %G_IO_ERROR_CLOSED. If @message is not well-formed, |
| * the operation fails with %G_IO_ERROR_INVALID_ARGUMENT. |
| * |
| * See this [server][gdbus-server] and [client][gdbus-unix-fd-client] |
| * for an example of how to use this low-level API to send and receive |
| * UNIX file descriptors. |
| * |
| * Note that @message must be unlocked, unless @flags contain the |
| * %G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL flag. |
| * |
| * Returns: %TRUE if the message was well-formed and queued for |
| * transmission, %FALSE if @error is set |
| * |
| * Since: 2.26 |
| */ |
| gboolean |
| g_dbus_connection_send_message (GDBusConnection *connection, |
| GDBusMessage *message, |
| GDBusSendMessageFlags flags, |
| volatile guint32 *out_serial, |
| GError **error) |
| { |
| gboolean ret; |
| |
| g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE); |
| g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), FALSE); |
| g_return_val_if_fail ((flags & G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL) || !g_dbus_message_get_locked (message), FALSE); |
| g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
| |
| CONNECTION_LOCK (connection); |
| ret = g_dbus_connection_send_message_unlocked (connection, message, flags, out_serial, error); |
| CONNECTION_UNLOCK (connection); |
| return ret; |
| } |
| |
| /* ---------------------------------------------------------------------------------------------------- */ |
| |
| typedef struct |
| { |
| guint32 serial; |
| |
| gulong cancellable_handler_id; |
| |
| GSource *timeout_source; |
| |
| gboolean delivered; |
| } SendMessageData; |
| |
| /* Can be called from any thread with or without lock held */ |
| static void |
| send_message_data_free (SendMessageData *data) |
| { |
| g_assert (data->timeout_source == NULL); |
| g_assert (data->cancellable_handler_id == 0); |
| |
| g_slice_free (SendMessageData, data); |
| } |
| |
| /* ---------------------------------------------------------------------------------------------------- */ |
| |
| /* can be called from any thread with lock held; @task is (transfer full) */ |
| static void |
| send_message_with_reply_cleanup (GTask *task, gboolean remove) |
| { |
| GDBusConnection *connection = g_task_get_source_object (task); |
| SendMessageData *data = g_task_get_task_data (task); |
| |
| CONNECTION_ENSURE_LOCK (connection); |
| |
| g_assert (!data->delivered); |
| |
| data->delivered = TRUE; |
| |
| if (data->timeout_source != NULL) |
| { |
| g_source_destroy (data->timeout_source); |
| data->timeout_source = NULL; |
| } |
| if (data->cancellable_handler_id > 0) |
| { |
| g_cancellable_disconnect (g_task_get_cancellable (task), data->cancellable_handler_id); |
| data->cancellable_handler_id = 0; |
| } |
| |
| if (remove) |
| { |
| gboolean removed = g_hash_table_remove (connection->map_method_serial_to_task, |
| GUINT_TO_POINTER (data->serial)); |
| g_warn_if_fail (removed); |
| } |
| |
| g_object_unref (task); |
| } |
| |
| /* ---------------------------------------------------------------------------------------------------- */ |
| |
| /* Called from GDBus worker thread with lock held; @task is (transfer full). */ |
| static void |
| send_message_data_deliver_reply_unlocked (GTask *task, |
| GDBusMessage *reply) |
| { |
| SendMessageData *data = g_task_get_task_data (task); |
| |
| if (data->delivered) |
| goto out; |
| |
| g_task_return_pointer (task, g_object_ref (reply), g_object_unref); |
| |
| send_message_with_reply_cleanup (task, TRUE); |
| |
| out: |
| ; |
| } |
| |
| /* Called from a user thread, lock is not held */ |
| static void |
| send_message_data_deliver_error (GTask *task, |
| GQuark domain, |
| gint code, |
| const char *message) |
| { |
| GDBusConnection *connection = g_task_get_source_object (task); |
| SendMessageData *data = g_task_get_task_data (task); |
| |
| CONNECTION_LOCK (connection); |
| if (data->delivered) |
| { |
| CONNECTION_UNLOCK (connection); |
| return; |
| } |
| |
| g_object_ref (task); |
| send_message_with_reply_cleanup (task, TRUE); |
| CONNECTION_UNLOCK (connection); |
| |
| g_task_return_new_error (task, domain, code, "%s", message); |
| g_object_unref (task); |
| } |
| |
| /* ---------------------------------------------------------------------------------------------------- */ |
| |
| /* Called from a user thread, lock is not held; @task is (transfer full) */ |
| static gboolean |
| send_message_with_reply_cancelled_idle_cb (gpointer user_data) |
| { |
| GTask *task = user_data; |
| |
| send_message_data_deliver_error (task, G_IO_ERROR, G_IO_ERROR_CANCELLED, |
| _("Operation was cancelled")); |
| return FALSE; |
| } |
| |
| /* Can be called from any thread with or without lock held */ |
| static void |
| send_message_with_reply_cancelled_cb (GCancellable *cancellable, |
| gpointer user_data) |
| { |
| GTask *task = user_data; |
| GSource *idle_source; |
| |
| /* postpone cancellation to idle handler since we may be called directly |
| * via g_cancellable_connect() (e.g. holding lock) |
| */ |
| idle_source = g_idle_source_new (); |
| g_source_set_name (idle_source, "[gio] send_message_with_reply_cancelled_idle_cb"); |
| g_task_attach_source (task, idle_source, send_message_with_reply_cancelled_idle_cb); |
| g_source_unref (idle_source); |
| } |
| |
| /* ---------------------------------------------------------------------------------------------------- */ |
| |
| /* Called from a user thread, lock is not held; @task is (transfer full) */ |
| static gboolean |
| send_message_with_reply_timeout_cb (gpointer user_data) |
| { |
| GTask *task = user_data; |
| |
| send_message_data_deliver_error (task, G_IO_ERROR, G_IO_ERROR_TIMED_OUT, |
| _("Timeout was reached")); |
| return FALSE; |
| } |
| |
| /* ---------------------------------------------------------------------------------------------------- */ |
| |
| /* Called from a user thread, connection's lock is held */ |
| static void |
| g_dbus_connection_send_message_with_reply_unlocked (GDBusConnection *connection, |
| GDBusMessage *message, |
| GDBusSendMessageFlags flags, |
| gint timeout_msec, |
| volatile guint32 *out_serial, |
| GCancellable *cancellable, |
| GAsyncReadyCallback callback, |
| gpointer user_data) |
| { |
| GTask *task; |
| SendMessageData *data; |
| GError *error = NULL; |
| volatile guint32 serial; |
| |
| if (out_serial == NULL) |
| out_serial = &serial; |
| |
| if (timeout_msec == -1) |
| timeout_msec = 25 * 1000; |
| |
| data = g_slice_new0 (SendMessageData); |
| task = g_task_new (connection, cancellable, callback, user_data); |
| g_task_set_source_tag (task, |
| g_dbus_connection_send_message_with_reply_unlocked); |
| g_task_set_task_data (task, data, (GDestroyNotify) send_message_data_free); |
| |
| if (g_task_return_error_if_cancelled (task)) |
| { |
| g_object_unref (task); |
| return; |
| } |
| |
| if (!g_dbus_connection_send_message_unlocked (connection, message, flags, out_serial, &error)) |
| { |
| g_task_return_error (task, error); |
| g_object_unref (task); |
| return; |
| } |
| data->serial = *out_serial; |
| |
| if (cancellable != NULL) |
| { |
| data->cancellable_handler_id = g_cancellable_connect (cancellable, |
| G_CALLBACK (send_message_with_reply_cancelled_cb), |
| g_object_ref (task), |
| g_object_unref); |
| } |
| |
| if (timeout_msec != G_MAXINT) |
| { |
| data->timeout_source = g_timeout_source_new (timeout_msec); |
| g_task_attach_source (task, data->timeout_source, |
| (GSourceFunc) send_message_with_reply_timeout_cb); |
| g_source_unref (data->timeout_source); |
| } |
| |
| g_hash_table_insert (connection->map_method_serial_to_task, |
| GUINT_TO_POINTER (*out_serial), |
| g_steal_pointer (&task)); |
| } |
| |
| /** |
| * g_dbus_connection_send_message_with_reply: |
| * @connection: a #GDBusConnection |
| * @message: a #GDBusMessage |
| * @flags: flags affecting how the message is sent |
| * @timeout_msec: the timeout in milliseconds, -1 to use the default |
| * timeout or %G_MAXINT for no timeout |
| * @out_serial: (out) (optional): return location for serial number assigned |
| * to @message when sending it or %NULL |
| * @cancellable: (nullable): a #GCancellable or %NULL |
| * @callback: (nullable): a #GAsyncReadyCallback to call when the request |
| * is satisfied or %NULL if you don't care about the result |
| * @user_data: The data to pass to @callback |
| * |
| * Asynchronously sends @message to the peer represented by @connection. |
| * |
| * Unless @flags contain the |
| * %G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL flag, the serial number |
| * will be assigned by @connection and set on @message via |
| * g_dbus_message_set_serial(). If @out_serial is not %NULL, then the |
| * serial number used will be written to this location prior to |
| * submitting the message to the underlying transport. |
| * |
| * If @connection is closed then the operation will fail with |
| * %G_IO_ERROR_CLOSED. If @cancellable is canceled, the operation will |
| * fail with %G_IO_ERROR_CANCELLED. If @message is not well-formed, |
| * the operation fails with %G_IO_ERROR_INVALID_ARGUMENT. |
| * |
| * This is an asynchronous method. When the operation is finished, @callback |
| * will be invoked in the |
| * [thread-default main context][g-main-context-push-thread-default] |
| * of the thread you are calling this method from. You can then call |
| * g_dbus_connection_send_message_with_reply_finish() to get the result of the operation. |
| * See g_dbus_connection_send_message_with_reply_sync() for the synchronous version. |
| * |
| * Note that @message must be unlocked, unless @flags contain the |
| * %G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL flag. |
| * |
| * See this [server][gdbus-server] and [client][gdbus-unix-fd-client] |
| * for an example of how to use this low-level API to send and receive |
| * UNIX file descriptors. |
| * |
| * Since: 2.26 |
| */ |
| void |
| g_dbus_connection_send_message_with_reply (GDBusConnection *connection, |
| GDBusMessage *message, |
| GDBusSendMessageFlags flags, |
| gint timeout_msec, |
| volatile guint32 *out_serial, |
| GCancellable *cancellable, |
| GAsyncReadyCallback callback, |
| gpointer user_data) |
| { |
| g_return_if_fail (G_IS_DBUS_CONNECTION (connection)); |
| g_return_if_fail (G_IS_DBUS_MESSAGE (message)); |
| g_return_if_fail ((flags & G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL) || !g_dbus_message_get_locked (message)); |
| g_return_if_fail (timeout_msec >= 0 || timeout_msec == -1); |
| |
| CONNECTION_LOCK (connection); |
| g_dbus_connection_send_message_with_reply_unlocked (connection, |
| message, |
| flags, |
| timeout_msec, |
| out_serial, |
| cancellable, |
| callback, |
| user_data); |
| CONNECTION_UNLOCK (connection); |
| } |
| |
| /** |
| * g_dbus_connection_send_message_with_reply_finish: |
| * @connection: a #GDBusConnection |
| * @res: a #GAsyncResult obtained from the #GAsyncReadyCallback passed to |
| * g_dbus_connection_send_message_with_reply() |
| * @error: teturn location for error or %NULL |
| * |
| * Finishes an operation started with g_dbus_connection_send_message_with_reply(). |
| * |
| * Note that @error is only set if a local in-process error |
| * occurred. That is to say that the returned #GDBusMessage object may |
| * be of type %G_DBUS_MESSAGE_TYPE_ERROR. Use |
| * g_dbus_message_to_gerror() to transcode this to a #GError. |
| * |
| * See this [server][gdbus-server] and [client][gdbus-unix-fd-client] |
| * for an example of how to use this low-level API to send and receive |
| * UNIX file descriptors. |
| * |
| * Returns: (transfer full): a locked #GDBusMessage or %NULL if @error is set |
| * |
| * Since: 2.26 |
| */ |
| GDBusMessage * |
| g_dbus_connection_send_message_with_reply_finish (GDBusConnection *connection, |
| GAsyncResult *res, |
| GError **error) |
| { |
| g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL); |
| g_return_val_if_fail (g_task_is_valid (res, connection), NULL); |
| g_return_val_if_fail (error == NULL || *error == NULL, NULL); |
| |
| return g_task_propagate_pointer (G_TASK (res), error); |
| } |
| |
| /* ---------------------------------------------------------------------------------------------------- */ |
| |
| typedef struct |
| { |
| GAsyncResult *res; |
| GMainContext *context; |
| GMainLoop *loop; |
| } SendMessageSyncData; |
| |
| /* Called from a user thread, lock is not held */ |
| static void |
| send_message_with_reply_sync_cb (GDBusConnection *connection, |
| GAsyncResult *res, |
| gpointer user_data) |
| { |
| SendMessageSyncData *data = user_data; |
| data->res = g_object_ref (res); |
| g_main_loop_quit (data->loop); |
| } |
| |
| /** |
| * g_dbus_connection_send_message_with_reply_sync: |
| * @connection: a #GDBusConnection |
| * @message: a #GDBusMessage |
| * @flags: flags affecting how the message is sent. |
| * @timeout_msec: the timeout in milliseconds, -1 to use the default |
| * timeout or %G_MAXINT for no timeout |
| * @out_serial: (out) (optional): return location for serial number |
| * assigned to @message when sending it or %NULL |
| * @cancellable: (nullable): a #GCancellable or %NULL |
| * @error: return location for error or %NULL |
| * |
| * Synchronously sends @message to the peer represented by @connection |
| * and blocks the calling thread until a reply is received or the |
| * timeout is reached. See g_dbus_connection_send_message_with_reply() |
| * for the asynchronous version of this method. |
| * |
| * Unless @flags contain the |
| * %G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL flag, the serial number |
| * will be assigned by @connection and set on @message via |
| * g_dbus_message_set_serial(). If @out_serial is not %NULL, then the |
| * serial number used will be written to this location prior to |
| * submitting the message to the underlying transport. |
| * |
| * If @connection is closed then the operation will fail with |
| * %G_IO_ERROR_CLOSED. If @cancellable is canceled, the operation will |
| * fail with %G_IO_ERROR_CANCELLED. If @message is not well-formed, |
| * the operation fails with %G_IO_ERROR_INVALID_ARGUMENT. |
| * |
| * Note that @error is only set if a local in-process error |
| * occurred. That is to say that the returned #GDBusMessage object may |
| * be of type %G_DBUS_MESSAGE_TYPE_ERROR. Use |
| * g_dbus_message_to_gerror() to transcode this to a #GError. |
| * |
| * See this [server][gdbus-server] and [client][gdbus-unix-fd-client] |
| * for an example of how to use this low-level API to send and receive |
| * UNIX file descriptors. |
| * |
| * Note that @message must be unlocked, unless @flags contain the |
| * %G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL flag. |
| * |
| * Returns: (transfer full): a locked #GDBusMessage that is the reply |
| * to @message or %NULL if @error is set |
| * |
| * Since: 2.26 |
| */ |
| GDBusMessage * |
| g_dbus_connection_send_message_with_reply_sync (GDBusConnection *connection, |
| GDBusMessage *message, |
| GDBusSendMessageFlags flags, |
| gint timeout_msec, |
| volatile guint32 *out_serial, |
| GCancellable *cancellable, |
| GError **error) |
| { |
| SendMessageSyncData data; |
| GDBusMessage *reply; |
| |
| g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL); |
| g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL); |
| g_return_val_if_fail ((flags & G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL) || !g_dbus_message_get_locked (message), NULL); |
| g_return_val_if_fail (timeout_msec >= 0 || timeout_msec == -1, NULL); |
| g_return_val_if_fail (error == NULL || *error == NULL, NULL); |
| |
| data.res = NULL; |
| data.context = g_main_context_new (); |
| data.loop = g_main_loop_new (data.context, FALSE); |
| |
| g_main_context_push_thread_default (data.context); |
| |
| g_dbus_connection_send_message_with_reply (connection, |
| message, |
| flags, |
| timeout_msec, |
| out_serial, |
| cancellable, |
| (GAsyncReadyCallback) send_message_with_reply_sync_cb, |
| &data); |
| g_main_loop_run (data.loop); |
| reply = g_dbus_connection_send_message_with_reply_finish (connection, |
| data.res, |
| error); |
| |
| g_main_context_pop_thread_default (data.context); |
| |
| g_main_context_unref (data.context); |
| g_main_loop_unref (data.loop); |
| if (data.res) |
| g_object_unref (data.res); |
| |
| return reply; |
| } |
| |
| /* ---------------------------------------------------------------------------------------------------- */ |
| |
| typedef struct |
| { |
| guint id; |
| guint ref_count; |
| GDBusMessageFilterFunction filter_function; |
| gpointer user_data; |
| GDestroyNotify user_data_free_func; |
| GMainContext *context; |
| } FilterData; |
| |
| /* requires CONNECTION_LOCK */ |
| static FilterData ** |
| copy_filter_list (GPtrArray *filters) |
| { |
| FilterData **copy; |
| guint n; |
| |
| copy = g_new (FilterData *, filters->len + 1); |
| for (n = 0; n < filters->len; n++) |
| { |
| copy[n] = filters->pdata[n]; |
| copy[n]->ref_count++; |
| } |
| copy[n] = NULL; |
| |
| return copy; |
| } |
| |
| /* requires CONNECTION_LOCK */ |
| static void |
| free_filter_list (FilterData **filters) |
| { |
| guint n; |
| |
| for (n = 0; filters[n]; n++) |
| { |
| filters[n]->ref_count--; |
| if (filters[n]->ref_count == 0) |
| { |
| call_destroy_notify (filters[n]->context, |
| filters[n]->user_data_free_func, |
| filters[n]->user_data); |
| g_main_context_unref (filters[n]->context); |
| g_free (filters[n]); |
| } |
| } |
| g_free (filters); |
| } |
| |
| /* Called in GDBusWorker's thread - we must not block - with no lock held */ |
| static void |
| on_worker_message_received (GDBusWorker *worker, |
| GDBusMessage *message, |
| gpointer user_data) |
| { |
| GDBusConnection *connection; |
| FilterData **filters; |
| guint n; |
| gboolean alive; |
| |
| G_LOCK (message_bus_lock); |
| alive = g_hash_table_contains (alive_connections, user_data); |
| if (!alive) |
| { |
| G_UNLOCK (message_bus_lock); |
| return; |
| } |
| connection = G_DBUS_CONNECTION (user_data); |
| g_object_ref (connection); |
| G_UNLOCK (message_bus_lock); |
| |
| //g_debug ("in on_worker_message_received"); |
| |
| g_object_ref (message); |
| g_dbus_message_lock (message); |
| |
| //g_debug ("boo ref_count = %d %p %p", G_OBJECT (connection)->ref_count, connection, connection->worker); |
| |
| /* First collect the set of callback functions */ |
| CONNECTION_LOCK (connection); |
| filters = copy_filter_list (connection->filters); |
| CONNECTION_UNLOCK (connection); |
| |
| /* then call the filters in order (without holding the lock) */ |
| for (n = 0; filters[n]; n++) |
| { |
| message = filters[n]->filter_function (connection, |
| message, |
| TRUE, |
| filters[n]->user_data); |
| if (message == NULL) |
| break; |
| g_dbus_message_lock (message); |
| } |
| |
| CONNECTION_LOCK (connection); |
| free_filter_list (filters); |
| CONNECTION_UNLOCK (connection); |
| |
| /* Standard dispatch unless the filter ate the message - no need to |
| * do anything if the message was altered |
| */ |
| if (message != NULL) |
| { |
| GDBusMessageType message_type; |
| |
| message_type = g_dbus_message_get_message_type (message); |
| if (message_type == G_DBUS_MESSAGE_TYPE_METHOD_RETURN || message_type == G_DBUS_MESSAGE_TYPE_ERROR) |
| { |
| guint32 reply_serial; |
| GTask *task; |
| |
| reply_serial = g_dbus_message_get_reply_serial (message); |
| CONNECTION_LOCK (connection); |
| task = g_hash_table_lookup (connection->map_method_serial_to_task, |
| GUINT_TO_POINTER (reply_serial)); |
| if (task != NULL) |
| { |
| /* This removes @task from @map_method_serial_to_task. */ |
| //g_debug ("delivering reply/error for serial %d for %p", reply_serial, connection); |
| send_message_data_deliver_reply_unlocked (task, message); |
| } |
| else |
| { |
| //g_debug ("message reply/error for serial %d but no SendMessageData found for %p", reply_serial, connection); |
| } |
| CONNECTION_UNLOCK (connection); |
| } |
| else if (message_type == G_DBUS_MESSAGE_TYPE_SIGNAL) |
| { |
| CONNECTION_LOCK (connection); |
| distribute_signals (connection, message); |
| CONNECTION_UNLOCK (connection); |
| } |
| else if (message_type == G_DBUS_MESSAGE_TYPE_METHOD_CALL) |
| { |
| CONNECTION_LOCK (connection); |
| distribute_method_call (connection, message); |
| CONNECTION_UNLOCK (connection); |
| } |
| } |
| |
| if (message != NULL) |
| g_object_unref (message); |
| g_object_unref (connection); |
| } |
| |
| /* Called in GDBusWorker's thread, lock is not held */ |
| static GDBusMessage * |
| on_worker_message_about_to_be_sent (GDBusWorker *worker, |
| GDBusMessage *message, |
| gpointer user_data) |
| { |
| GDBusConnection *connection; |
| FilterData **filters; |
| guint n; |
| gboolean alive; |
| |
| G_LOCK (message_bus_lock); |
| alive = g_hash_table_contains (alive_connections, user_data); |
| if (!alive) |
| { |
| G_UNLOCK (message_bus_lock); |
| return message; |
| } |
| connection = G_DBUS_CONNECTION (user_data); |
| g_object_ref (connection); |
| G_UNLOCK (message_bus_lock); |
| |
| //g_debug ("in on_worker_message_about_to_be_sent"); |
| |
| /* First collect the set of callback functions */ |
| CONNECTION_LOCK (connection); |
| filters = copy_filter_list (connection->filters); |
| CONNECTION_UNLOCK (connection); |
| |
| /* then call the filters in order (without holding the lock) */ |
| for (n = 0; filters[n]; n++) |
| { |
| g_dbus_message_lock (message); |
| message = filters[n]->filter_function (connection, |
| message, |
| FALSE, |
| filters[n]->user_data); |
| if (message == NULL) |
| break; |
| } |
| |
| CONNECTION_LOCK (connection); |
| free_filter_list (filters); |
| CONNECTION_UNLOCK (connection); |
| |
| g_object_unref (connection); |
| |
| return message; |
| } |
| |
| /* called with connection lock held, in GDBusWorker thread */ |
| static gboolean |
| cancel_method_on_close (gpointer key, gpointer value, gpointer user_data) |
| { |
| GTask *task = value; |
| SendMessageData *data = g_task_get_task_data (task); |
| |
| if (data->delivered) |
| return FALSE; |
| |
| g_task_return_new_error (task, |
| G_IO_ERROR, |
| G_IO_ERROR_CLOSED, |
| _("The connection is closed")); |
| |
| /* Ask send_message_with_reply_cleanup not to remove the element from the |
| * hash table - we're in the middle of a foreach; that would be unsafe. |
| * Instead, return TRUE from this function so that it gets removed safely. |
| */ |
| send_message_with_reply_cleanup (task, FALSE); |
| return TRUE; |
| } |
| |
| /* Called in GDBusWorker's thread - we must not block - without lock held */ |
| static void |
| on_worker_closed (GDBusWorker *worker, |
| gboolean remote_peer_vanished, |
| GError *error, |
| gpointer user_data) |
| { |
| GDBusConnection *connection; |
| gboolean alive; |
| guint old_atomic_flags; |
| |
| G_LOCK (message_bus_lock); |
| alive = g_hash_table_contains (alive_connections, user_data); |
| if (!alive) |
| { |
| G_UNLOCK (message_bus_lock); |
| return; |
| } |
| connection = G_DBUS_CONNECTION (user_data); |
| g_object_ref (connection); |
| G_UNLOCK (message_bus_lock); |
| |
| //g_debug ("in on_worker_closed: %s", error->message); |
| |
| CONNECTION_LOCK (connection); |
| /* Even though this is atomic, we do it inside the lock to avoid breaking |
| * assumptions in remove_match_rule(). We'd need the lock in a moment |
| * anyway, so, no loss. |
| */ |
| old_atomic_flags = g_atomic_int_or (&connection->atomic_flags, FLAG_CLOSED); |
| |
| if (!(old_atomic_flags & FLAG_CLOSED)) |
| { |
| g_hash_table_foreach_remove (connection->map_method_serial_to_task, cancel_method_on_close, NULL); |
| schedule_closed_unlocked (connection, remote_peer_vanished, error); |
| } |
| CONNECTION_UNLOCK (connection); |
| |
| g_object_unref (connection); |
| } |
| |
| /* ---------------------------------------------------------------------------------------------------- */ |
| |
| /* Determines the biggest set of capabilities we can support on this |
| * connection. |
| * |
| * Called with the init_lock held. |
| */ |
| static GDBusCapabilityFlags |
| get_offered_capabilities_max (GDBusConnection *connection) |
| { |
| GDBusCapabilityFlags ret; |
| ret = G_DBUS_CAPABILITY_FLAGS_NONE; |
| #ifdef G_OS_UNIX |
| if (G_IS_UNIX_CONNECTION (connection->stream)) |
| ret |= G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING; |
| #endif |
| return ret; |
| } |
| |
| /* Called in a user thread, lock is not held */ |
| static gboolean |
| initable_init (GInitable *initable, |
| GCancellable *cancellable, |
| GError **error) |
| { |
| GDBusConnection *connection = G_DBUS_CONNECTION (initable); |
| gboolean ret; |
| |
| /* This method needs to be idempotent to work with the singleton |
| * pattern. See the docs for g_initable_init(). We implement this by |
| * locking. |
| * |
| * Unfortunately we can't use the main lock since the on_worker_*() |
| * callbacks above needs the lock during initialization (for message |
| * bus connections we do a synchronous Hello() call on the bus). |
| */ |
| g_mutex_lock (&connection->init_lock); |
| |
| ret = FALSE; |
| |
| /* Make this a no-op if we're already initialized (successfully or |
| * unsuccessfully) |
| */ |
| if ((g_atomic_int_get (&connection->atomic_flags) & FLAG_INITIALIZED)) |
| { |
| ret = (connection->initialization_error == NULL); |
| goto out; |
| } |
| |
| /* Because of init_lock, we can't get here twice in different threads */ |
| g_assert (connection->initialization_error == NULL); |
| |
| /* The user can pass multiple (but mutally exclusive) construct |
| * properties: |
| * |
| * - stream (of type GIOStream) |
| * - address (of type gchar*) |
| * |
| * At the end of the day we end up with a non-NULL GIOStream |
| * object in connection->stream. |
| */ |
| if (connection->address != NULL) |
| { |
| g_assert (connection->stream == NULL); |
| |
| if ((connection->flags & G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER) || |
| (connection->flags & G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS)) |
| { |
| g_set_error_literal (&connection->initialization_error, |
| G_IO_ERROR, |
| G_IO_ERROR_INVALID_ARGUMENT, |
| _("Unsupported flags encountered when constructing a client-side connection")); |
| goto out; |
| } |
| |
| connection->stream = g_dbus_address_get_stream_sync (connection->address, |
| NULL, /* TODO: out_guid */ |
| cancellable, |
| &connection->initialization_error); |
| if (connection->stream == NULL) |
| goto out; |
| } |
| else if (connection->stream != NULL) |
| { |
| /* nothing to do */ |
| } |
| else |
| { |
| g_assert_not_reached (); |
| } |
| |
| /* Authenticate the connection */ |
| if (connection->flags & G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER) |
| { |
| g_assert (!(connection->flags & G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT)); |
| g_assert (connection->guid != NULL); |
| connection->auth = _g_dbus_auth_new (connection->stream); |
| if (!_g_dbus_auth_run_server (connection->auth, |
| connection->authentication_observer, |
| connection->guid, |
| (connection->flags & G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS), |
| get_offered_capabilities_max (connection), |
| &connection->capabilities, |
| &connection->credentials, |
| cancellable, |
| &connection->initialization_error)) |
| goto out; |
| } |
| else if (connection->flags & G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT) |
| { |
| g_assert (!(connection->flags & G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER)); |
| g_assert (connection->guid == NULL); |
| connection->auth = _g_dbus_auth_new (connection->stream); |
| connection->guid = _g_dbus_auth_run_client (connection->auth, |
| connection->authentication_observer, |
| get_offered_capabilities_max (connection), |
| &connection->capabilities, |
| cancellable, |
| &connection->initialization_error); |
| if (connection->guid == NULL) |
| goto out; |
| } |
| |
| if (connection->authentication_observer != NULL) |
| { |
| g_object_unref (connection->authentication_observer); |
| connection->authentication_observer = NULL; |
| } |
| |
| //g_output_stream_flush (G_SOCKET_CONNECTION (connection->stream) |
| |
| //g_debug ("haz unix fd passing powers: %d", connection->capabilities & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING); |
| |
| #ifdef G_OS_UNIX |
| /* We want all IO operations to be non-blocking since they happen in |
| * the worker thread which is shared by _all_ connections. |
| */ |
| if (G_IS_SOCKET_CONNECTION (connection->stream)) |
| { |
| g_socket_set_blocking (g_socket_connection_get_socket (G_SOCKET_CONNECTION (connection->stream)), FALSE); |
| } |
| #endif |
| |
| G_LOCK (message_bus_lock); |
| if (alive_connections == NULL) |
| alive_connections = g_hash_table_new (g_direct_hash, g_direct_equal); |
| g_hash_table_add (alive_connections, connection); |
| G_UNLOCK (message_bus_lock); |
| |
| connection->worker = _g_dbus_worker_new (connection->stream, |
| connection->capabilities, |
| ((connection->flags & G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING) != 0), |
| on_worker_message_received, |
| on_worker_message_about_to_be_sent, |
| on_worker_closed, |
| connection); |
| |
| /* if a bus connection, call org.freedesktop.DBus.Hello - this is how we're getting a name */ |
| if (connection->flags & G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION) |
| { |
| GVariant *hello_result; |
| |
| /* we could lift this restriction by adding code in gdbusprivate.c */ |
| if (connection->flags & G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING) |
| { |
| g_set_error_literal (&connection->initialization_error, |
| G_IO_ERROR, |
| G_IO_ERROR_FAILED, |
| "Cannot use DELAY_MESSAGE_PROCESSING with MESSAGE_BUS_CONNECTION"); |
| goto out; |
| } |
| |
| hello_result = g_dbus_connection_call_sync (connection, |
| "org.freedesktop.DBus", /* name */ |
| "/org/freedesktop/DBus", /* path */ |
| "org.freedesktop.DBus", /* interface */ |
| "Hello", |
| NULL, /* parameters */ |
| G_VARIANT_TYPE ("(s)"), |
| CALL_FLAGS_INITIALIZING, |
| -1, |
| NULL, /* TODO: cancellable */ |
| &connection->initialization_error); |
| if (hello_result == NULL) |
| goto out; |
| |
| g_variant_get (hello_result, "(s)", &connection->bus_unique_name); |
| g_variant_unref (hello_result); |
| //g_debug ("unique name is '%s'", connection->bus_unique_name); |
| } |
| |
| ret = TRUE; |
| out: |
| if (!ret) |
| { |
| g_assert (connection->initialization_error != NULL); |
| g_propagate_error (error, g_error_copy (connection->initialization_error)); |
| } |
| |
| g_atomic_int_or (&connection->atomic_flags, FLAG_INITIALIZED); |
| g_mutex_unlock (&connection->init_lock); |
| |
| return ret; |
| } |
| |
| static void |
| initable_iface_init (GInitableIface *initable_iface) |
| { |
| initable_iface->init = initable_init; |
| } |
| |
| /* ---------------------------------------------------------------------------------------------------- */ |
| |
| static void |
| async_initable_iface_init (GAsyncInitableIface *async_initable_iface) |
| { |
| /* Use default */ |
| } |
| |
| /* ---------------------------------------------------------------------------------------------------- */ |
| |
| /** |
| * g_dbus_connection_new: |
| * @stream: a #GIOStream |
| * @guid: (nullable): the GUID to use if a authenticating as a server or %NULL |
| * @flags: flags describing how to make the connection |
| * @observer: (nullable): a #GDBusAuthObserver or %NULL |
| * @cancellable: (nullable): a #GCancellable or %NULL |
| * @callback: a #GAsyncReadyCallback to call when the request is satisfied |
| * @user_data: the data to pass to @callback |
| * |
| * Asynchronously sets up a D-Bus connection for exchanging D-Bus messages |
| * with the end represented by @stream. |
| * |
| * If @stream is a #GSocketConnection, then the corresponding #GSocket |
| * will be put into non-blocking mode. |
| * |
| * The D-Bus connection will interact with @stream from a worker thread. |
| * As a result, the caller should not interact with @stream after this |
| * method has been called, except by calling g_object_unref() on it. |
| * |
| * If @observer is not %NULL it may be used to control the |
| * authentication process. |
| * |
| * When the operation is finished, @callback will be invoked. You can |
| * then call g_dbus_connection_new_finish() to get the result of the |
| * operation. |
| * |
| * This is a asynchronous failable constructor. See |
| * g_dbus_connection_new_sync() for the synchronous |
| * version. |
| * |
| * Since: 2.26 |
| */ |
| void |
| g_dbus_connection_new (GIOStream *stream, |
| const gchar *guid, |
| GDBusConnectionFlags flags, |
| GDBusAuthObserver *observer, |
| GCancellable *cancellable, |
| GAsyncReadyCallback callback, |
| gpointer user_data) |
| { |
| _g_dbus_initialize (); |
| |
| g_return_if_fail (G_IS_IO_STREAM (stream)); |
| |
| g_async_initable_new_async (G_TYPE_DBUS_CONNECTION, |
| G_PRIORITY_DEFAULT, |
| cancellable, |
| callback, |
| user_data, |
| "stream", stream, |
| "guid", guid, |
| "flags", flags, |
| "authentication-observer", observer, |
| NULL); |
| } |
| |
| /** |
| * g_dbus_connection_new_finish: |
| * @res: a #GAsyncResult obtained from the #GAsyncReadyCallback |
| * passed to g_dbus_connection_new(). |
| * @error: return location for error or %NULL |
| * |
| * Finishes an operation started with g_dbus_connection_new(). |
| * |
| * Returns: a #GDBusConnection or %NULL if @error is set. Free |
| * with g_object_unref(). |
| * |
| * Since: 2.26 |
| */ |
| GDBusConnection * |
| g_dbus_connection_new_finish (GAsyncResult *res, |
| GError **error) |
| { |
| GObject *object; |
| GObject *source_object; |
| |
| g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL); |
| g_return_val_if_fail (error == NULL || *error == NULL, NULL); |
| |
| source_object = g_async_result_get_source_object (res); |
| g_assert (source_object != NULL); |
| object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object), |
| res, |
| error); |
| g_object_unref (source_object); |
| if (object != NULL) |
| return G_DBUS_CONNECTION (object); |
| else |
| return NULL; |
| } |
| |
| /** |
| * g_dbus_connection_new_sync: |
| * @stream: a #GIOStream |
| * @guid: (nullable): the GUID to use if a authenticating as a server or %NULL |
| * @flags: flags describing how to make the connection |
| * @observer: (nullable): a #GDBusAuthObserver or %NULL |
| * @cancellable: (nullable): a #GCancellable or %NULL |
| * @error: return location for error or %NULL |
| * |
| * Synchronously sets up a D-Bus connection for exchanging D-Bus messages |
| * with the end represented by @stream. |
| * |
| * If @stream is a #GSocketConnection, then the corresponding #GSocket |
| * will be put into non-blocking mode. |
| * |
| * The D-Bus connection will interact with @stream from a worker thread. |
| * As a result, the caller should not interact with @stream after this |
| * method has been called, except by calling g_object_unref() on it. |
| * |
| * If @observer is not %NULL it may be used to control the |
| * authentication process. |
| * |
| * This is a synchronous failable constructor. See |
| * g_dbus_connection_new() for the asynchronous version. |
| * |
| * Returns: a #GDBusConnection or %NULL if @error is set. Free with g_object_unref(). |
| * |
| * Since: 2.26 |
| */ |
| GDBusConnection * |
| g_dbus_connection_new_sync (GIOStream *stream, |
| const gchar *guid, |
| GDBusConnectionFlags flags, |
| GDBusAuthObserver *observer, |
| GCancellable *cancellable, |
| GError **error) |
| { |
| _g_dbus_initialize (); |
| g_return_val_if_fail (G_IS_IO_STREAM (stream), NULL); |
| g_return_val_if_fail (error == NULL || *error == NULL, NULL); |
| return g_initable_new (G_TYPE_DBUS_CONNECTION, |
| cancellable, |
| error, |
| "stream", stream, |
| "guid", guid, |
| "flags", flags, |
| "authentication-observer", observer, |
| NULL); |
| } |
| |
| /* ---------------------------------------------------------------------------------------------------- */ |
| |
| /** |
| * g_dbus_connection_new_for_address: |
| * @address: a D-Bus address |
| * @flags: flags describing how to make the connection |
| * @observer: (nullable): a #GDBusAuthObserver or %NULL |
| * @cancellable: (nullable): a #GCancellable or %NULL |
| * @callback: a #GAsyncReadyCallback to call when the request is satisfied |
| * @user_data: the data to pass to @callback |
| * |
| * Asynchronously connects and sets up a D-Bus client connection for |
| * exchanging D-Bus messages with an endpoint specified by @address |
| * which must be in the |
| * [D-Bus address format](https://dbus.freedesktop.org/doc/dbus-specification.html#addresses). |
| * |
| * This constructor can only be used to initiate client-side |
| * connections - use g_dbus_connection_new() if you need to act as the |
| * server. In particular, @flags cannot contain the |
| * %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER or |
| * %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS flags. |
| * |
| * When the operation is finished, @callback will be invoked. You can |
| * then call g_dbus_connection_new_finish() to get the result of the |
| * operation. |
| * |
| * If @observer is not %NULL it may be used to control the |
| * authentication process. |
| * |
| * This is a asynchronous failable constructor. See |
| * g_dbus_connection_new_for_address_sync() for the synchronous |
| * version. |
| * |
| * Since: 2.26 |
| */ |
| void |
| g_dbus_connection_new_for_address (const gchar *address, |
| GDBusConnectionFlags flags, |
| GDBusAuthObserver *observer, |
| GCancellable *cancellable, |
| GAsyncReadyCallback callback, |
| gpointer user_data) |
| { |
| _g_dbus_initialize (); |
| |
| g_return_if_fail (address != NULL); |
| |
| g_async_initable_new_async (G_TYPE_DBUS_CONNECTION, |
| G_PRIORITY_DEFAULT, |
| cancellable, |
| callback, |
| user_data, |
| "address", address, |
| "flags", flags, |
| "authentication-observer", observer, |
| NULL); |
| } |
| |
| /** |
| * g_dbus_connection_new_for_address_finish: |
| * @res: a #GAsyncResult obtained from the #GAsyncReadyCallback passed |
| * to g_dbus_connection_new() |
| * @error: return location for error or %NULL |
| * |
| * Finishes an operation started with g_dbus_connection_new_for_address(). |
| * |
| * Returns: a #GDBusConnection or %NULL if @error is set. Free with |
| * g_object_unref(). |
| * |
| * Since: 2.26 |
| */ |
| GDBusConnection * |
| g_dbus_connection_new_for_address_finish (GAsyncResult *res, |
| GError **error) |
| { |
| GObject *object; |
| GObject *source_object; |
| |
| g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL); |
| g_return_val_if_fail (error == NULL || *error == NULL, NULL); |
| |
| source_object = g_async_result_get_source_object (res); |
| g_assert (source_object != NULL); |
| object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object), |
| res, |
| error); |
| g_object_unref (source_object); |
| if (object != NULL) |
| return G_DBUS_CONNECTION (object); |
| else |
| return NULL; |
| } |
| |
| /** |
| * g_dbus_connection_new_for_address_sync: |
| * @address: a D-Bus address |
| * @flags: flags describing how to make the connection |
| * @observer: (nullable): a #GDBusAuthObserver or %NULL |
| * @cancellable: (nullable): a #GCancellable or %NULL |
| * @error: return location for error or %NULL |
| * |
| * Synchronously connects and sets up a D-Bus client connection for |
| * exchanging D-Bus messages with an endpoint specified by @address |
| * which must be in the |
| * [D-Bus address format](https://dbus.freedesktop.org/doc/dbus-specification.html#addresses). |
| * |
| * This constructor can only be used to initiate client-side |
| * connections - use g_dbus_connection_new_sync() if you need to act |
| * as the server. In particular, @flags cannot contain the |
| * %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER or |
| * %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS flags. |
| * |
| * This is a synchronous failable constructor. See |
| * g_dbus_connection_new_for_address() for the asynchronous version. |
| * |
| * If @observer is not %NULL it may be used to control the |
| * authentication process. |
| * |
| * Returns: a #GDBusConnection or %NULL if @error is set. Free with |
| * g_object_unref(). |
| * |
| * Since: 2.26 |
| */ |
| GDBusConnection * |
| g_dbus_connection_new_for_address_sync (const gchar *address, |
| GDBusConnectionFlags flags, |
| GDBusAuthObserver *observer, |
| GCancellable *cancellable, |
| GError **error) |
| { |
| _g_dbus_initialize (); |
| |
| g_return_val_if_fail (address != NULL, NULL); |
| g_return_val_if_fail (error == NULL || *error == NULL, NULL); |
| return g_initable_new (G_TYPE_DBUS_CONNECTION, |
| cancellable, |
| error, |
| "address", address, |
| "flags", flags, |
| "authentication-observer", observer, |
| NULL); |
| } |
| |
| /* ---------------------------------------------------------------------------------------------------- */ |
| |
| /** |
| * g_dbus_connection_set_exit_on_close: |
| * @connection: a #GDBusConnection |
| * @exit_on_close: whether the process should be terminated |
| * when @connection is closed by the remote peer |
| * |
| * Sets whether the process should be terminated when @connection is |
| * closed by the remote peer. See #GDBusConnection:exit-on-close for |
| * more details. |
| * |
| * Note that this function should be used with care. Most modern UNIX |
| * desktops tie the notion of a user session the session bus, and expect |
| * all of a users applications to quit when their bus connection goes away. |
| * If you are setting @exit_on_close to %FALSE for the shared session |
| * bus connection, you should make sure that your application exits |
| * when the user session ends. |
| * |
| * Since: 2.26 |
| */ |
| void |
| g_dbus_connection_set_exit_on_close (GDBusConnection *connection, |
| gboolean exit_on_close) |
| { |
| g_return_if_fail (G_IS_DBUS_CONNECTION (connection)); |
| |
| if (exit_on_close) |
| g_atomic_int_or (&connection->atomic_flags, FLAG_EXIT_ON_CLOSE); |
| else |
| g_atomic_int_and (&connection->atomic_flags, ~FLAG_EXIT_ON_CLOSE); |
| |
| } |
| |
| /** |
| * g_dbus_connection_get_exit_on_close: |
| * @connection: a #GDBusConnection |
| * |
| * Gets whether the process is terminated when @connection is |
| * closed by the remote peer. See |
| * #GDBusConnection:exit-on-close for more details. |
| * |
| * Returns: whether the process is terminated when @connection is |
| * closed by the remote peer |
| * |
| * Since: 2.26 |
| */ |
| gboolean |
| g_dbus_connection_get_exit_on_close (GDBusConnection *connection) |
| { |
| g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE); |
| |
| if (g_atomic_int_get (&connection->atomic_flags) & FLAG_EXIT_ON_CLOSE) |
| return TRUE; |
| else |
| return FALSE; |
| } |
| |
| /** |
| * g_dbus_connection_get_guid: |
| * @connection: a #GDBusConnection |
| * |
| * The GUID of the peer performing the role of server when |
| * authenticating. See #GDBusConnection:guid for more details. |
| * |
| * Returns: The GUID. Do not free this string, it is owned by |
| * @connection. |
| * |
| * Since: 2.26 |
| */ |
| const gchar * |
| g_dbus_connection_get_guid (GDBusConnection *connection) |
| { |
| g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL); |
| return connection->guid; |
| } |
| |
| /** |
| * g_dbus_connection_get_unique_name: |
| * @connection: a #GDBusConnection |
| * |
| * Gets the unique name of @connection as assigned by the message |
| * bus. This can also be used to figure out if @connection is a |
| * message bus connection. |
| * |
| * Returns: the unique name or %NULL if @connection is not a message |
| * bus connection. Do not free this string, it is owned by |
| * @connection. |
| * |
| * Since: 2.26 |
| */ |
| const gchar * |
| g_dbus_connection_get_unique_name (GDBusConnection *connection) |
| { |
| g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL); |
| |
| /* do not use g_return_val_if_fail(), we want the memory barrier */ |
| if (!check_initialized (connection)) |
| return NULL; |
| |
| return connection->bus_unique_name; |
| } |
| |
| /** |
| * g_dbus_connection_get_peer_credentials: |
| * @connection: a #GDBusConnection |
| * |
| * Gets the credentials of the authenticated peer. This will always |
| * return %NULL unless @connection acted as a server |
| * (e.g. %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER was passed) |
| * when set up and the client passed credentials as part of the |
| * authentication process. |
| * |
| * In a message bus setup, the message bus is always the server and |
| * each application is a client. So this method will always return |
| * %NULL for message bus clients. |
| * |
| * Returns: (transfer none) (nullable): a #GCredentials or %NULL if not |
| * available. Do not free this object, it is owned by @connection. |
| * |
| * Since: 2.26 |
| */ |
| GCredentials * |
| g_dbus_connection_get_peer_credentials (GDBusConnection *connection) |
| { |
| g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL); |
| |
| /* do not use g_return_val_if_fail(), we want the memory barrier */ |
| if (!check_initialized (connection)) |
| return NULL; |
| |
| return connection->credentials; |
| } |
| |
| /* ---------------------------------------------------------------------------------------------------- */ |
| |
| static volatile guint _global_filter_id = 1; |
| |
| /** |
| * g_dbus_connection_add_filter: |
| * @connection: a #GDBusConnection |
| * @filter_function: a filter function |
| * @user_data: user data to pass to @filter_function |
| * @user_data_free_func: function to free @user_data with when filter |
| * is removed or %NULL |
| * |
| * Adds a message filter. Filters are handlers that are run on all |
| * incoming and outgoing messages, prior to standard dispatch. Filters |
| * are run in the order that they were added. The same handler can be |
| * added as a filter more than once, in which case it will be run more |
| * than once. Filters added during a filter callback won't be run on |
| * the message being processed. Filter functions are allowed to modify |
| * and even drop messages. |
| * |
| * Note that filters are run in a dedicated message handling thread so |
| * they can't block and, generally, can't do anything but signal a |
| * worker thread. Also note that filters are rarely needed - use API |
| * such as g_dbus_connection_send_message_with_reply(), |
| * g_dbus_connection_signal_subscribe() or g_dbus_connection_call() instead. |
| * |
| * If a filter consumes an incoming message the message is not |
| * dispatched anywhere else - not even the standard dispatch machinery |
| * (that API such as g_dbus_connection_signal_subscribe() and |
| * g_dbus_connection_send_message_with_reply() relies on) will see the |
| * message. Similary, if a filter consumes an outgoing message, the |
| * message will not be sent to the other peer. |
| * |
| * If @user_data_free_func is non-%NULL, it will be called (in the |
| * thread-default main context of the thread you are calling this |
| * method from) at some point after @user_data is no longer |
| * needed. (It is not guaranteed to be called synchronously when the |
| * filter is removed, and may be called after @connection has been |
| * destroyed.) |
| * |
| * Returns: a filter identifier that can be used with |
| * g_dbus_connection_remove_filter() |
| * |
| * Since: 2.26 |
| */ |
| guint |
| g_dbus_connection_add_filter (GDBusConnection *connection, |
| GDBusMessageFilterFunction filter_function, |
| gpointer user_data, |
| GDestroyNotify user_data_free_func) |
| { |
| FilterData *data; |
| |
| g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), 0); |
| g_return_val_if_fail (filter_function != NULL, 0); |
| g_return_val_if_fail (check_initialized (connection), 0); |
| |
| CONNECTION_LOCK (connection); |
| data = g_new0 (FilterData, 1); |
| data->id = g_atomic_int_add (&_global_filter_id, 1); /* TODO: overflow etc. */ |
| data->ref_count = 1; |
| data->filter_function = filter_function; |
| data->user_data = user_data; |
| data->user_data_free_func = user_data_free_func; |
| data->context = g_main_context_ref_thread_default (); |
| g_ptr_array_add (connection->filters, data); |
| CONNECTION_UNLOCK (connection); |
| |
| return data->id; |
| } |
| |
| /* only called from finalize(), removes all filters */ |
| static void |
| purge_all_filters (GDBusConnection *connection) |
| { |
| guint n; |
| for (n = 0; n < connection->filters->len; n++) |
| { |
| FilterData *data = connection->filters->pdata[n]; |
| |
| call_destroy_notify (data->context, |
| data->user_data_free_func, |
| data->user_data); |
| g_main_context_unref (data->context); |
| g_free (data); |
| } |
| } |
| |
| /** |
| * g_dbus_connection_remove_filter: |
| * @connection: a #GDBusConnection |
| * @filter_id: an identifier obtained from g_dbus_connection_add_filter() |
| * |
| * Removes a filter. |
| * |
| * Note that since filters run in a different thread, there is a race |
| * condition where it is possible that the filter will be running even |
| * after calling g_dbus_connection_remove_filter(), so you cannot just |
| * free data that the filter might be using. Instead, you should pass |
| * a #GDestroyNotify to g_dbus_connection_add_filter(), which will be |
| * called when it is guaranteed that the data is no longer needed. |
| * |
| * Since: 2.26 |
| */ |
| void |
| g_dbus_connection_remove_filter (GDBusConnection *connection, |
| guint filter_id) |
| { |
| guint n; |
| gboolean found; |
| FilterData *to_destroy; |
| |
| g_return_if_fail (G_IS_DBUS_CONNECTION (connection)); |
| g_return_if_fail (check_initialized (connection)); |
| |
| CONNECTION_LOCK (connection); |
| found = FALSE; |
| to_destroy = NULL; |
| for (n = 0; n < connection->filters->len; n++) |
| { |
| FilterData *data = connection->filters->pdata[n]; |
| if (data->id == filter_id) |
| { |
| found = TRUE; |
| g_ptr_array_remove_index (connection->filters, n); |
| data->ref_count--; |
| if (data->ref_count == 0) |
| to_destroy = data; |
| break; |
| } |
| } |
| CONNECTION_UNLOCK (connection); |
| |
| /* do free without holding lock */ |
| if (to_destroy != NULL) |
| { |
| if (to_destroy->user_data_free_func != NULL) |
| to_destroy->user_data_free_func (to_destroy->user_data); |
| g_main_context_unref (to_destroy->context); |
| g_free (to_destroy); |
| } |
| else if (!found) |
| { |
| g_warning ("g_dbus_connection_remove_filter: No filter found for filter_id %d", filter_id); |
| } |
| } |
| |
| /* ---------------------------------------------------------------------------------------------------- */ |
| |
| typedef struct |
| { |
| gchar *rule; |
| gchar *sender; |
| gchar *sender_unique_name; /* if sender is unique or org.freedesktop.DBus, then that name... otherwise blank */ |
| gchar *interface_name; |
| gchar *member; |
| gchar *object_path; |
| gchar *arg0; |
| GDBusSignalFlags flags; |
| GArray *subscribers; |
| } SignalData; |
| |
| typedef struct |
| { |
| GDBusSignalCallback callback; |
| gpointer user_data; |
| GDestroyNotify user_data_free_func; |
| guint id; |
| GMainContext *context; |
| } SignalSubscriber; |
| |
| static void |
| signal_data_free (SignalData *signal_data) |
| { |
| g_free (signal_data->rule); |
| g_free (signal_data->sender); |
| g_free (signal_data->sender_unique_name); |
| g_free (signal_data->interface_name); |
| g_free (signal_data->member); |
| g_free (signal_data->object_path); |
| g_free (signal_data->arg0); |
| g_array_free (signal_data->subscribers, TRUE); |
| g_free (signal_data); |
| } |
| |
| static gchar * |
| args_to_rule (const gchar *sender, |
| const gchar *interface_name, |
| const gchar *member, |
| const gchar *object_path, |
| const gchar *arg0, |
| GDBusSignalFlags flags) |
| { |
| GString *rule; |
| |
| rule = g_string_new ("type='signal'"); |
| if (flags & G_DBUS_SIGNAL_FLAGS_NO_MATCH_RULE) |
| g_string_prepend_c (rule, '-'); |
| if (sender != NULL) |
| g_string_append_printf (rule, ",sender='%s'", sender); |
| if (interface_name != NULL) |
| g_string_append_printf (rule, ",interface='%s'", interface_name); |
| if (member != NULL) |
| g_string_append_printf (rule, ",member='%s'", member); |
| if (object_path != NULL) |
| g_string_append_printf (rule, ",path='%s'", object_path); |
| |
| if (arg0 != NULL) |
| { |
| if (flags & G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH) |
| g_string_append_printf (rule, ",arg0path='%s'", arg0); |
| else if (flags & G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_NAMESPACE) |
| g_string_append_printf (rule, ",arg0namespace='%s'", arg0); |
| else |
| g_string_append_printf (rule, ",arg0='%s'", arg0); |
| } |
| |
| return g_string_free (rule, FALSE); |
| } |
| |
| static volatile guint _global_subscriber_id = 1; |
| static volatile guint _global_registration_id = 1; |
| static volatile guint _global_subtree_registration_id = 1; |
| |
| /* ---------------------------------------------------------------------------------------------------- */ |
| |
| /* Called in a user thread, lock is held */ |
| static void |
| add_match_rule (GDBusConnection *connection, |
| const gchar *match_rule) |
| { |
| GError *error; |
| GDBusMessage *message; |
| |
| if (match_rule[0] == '-') |
| return; |
| |
| message = g_dbus_message_new_method_call ("org.freedesktop.DBus", /* name */ |
| "/org/freedesktop/DBus", /* path */ |
| "org.freedesktop.DBus", /* interface */ |
| "AddMatch"); |
| g_dbus_message_set_body (message, g_variant_new ("(s)", match_rule)); |
| error = NULL; |
| if (!g_dbus_connection_send_message_unlocked (connection, |
| message, |
| G_DBUS_SEND_MESSAGE_FLAGS_NONE, |
| NULL, |
| &error)) |
| { |
| g_critical ("Error while sending AddMatch() message: %s", error->message); |
| g_error_free (error); |
| } |
| g_object_unref (message); |
| } |
| |
| /* ---------------------------------------------------------------------------------------------------- */ |
| |
| /* Called in a user thread, lock is held */ |
| static void |
| remove_match_rule (GDBusConnection *connection, |
| const gchar *match_rule) |
| { |
| GError *error; |
| GDBusMessage *message; |
| |
| if (match_rule[0] == '-') |
| return; |
| |
| message = g_dbus_message_new_method_call ("org.freedesktop.DBus", /* name */ |
| "/org/freedesktop/DBus", /* path */ |
| "org.freedesktop.DBus", /* interface */ |
| "RemoveMatch"); |
| g_dbus_message_set_body (message, g_variant_new ("(s)", match_rule)); |
| |
| error = NULL; |
| if (!g_dbus_connection_send_message_unlocked (connection, |
| message, |
| G_DBUS_SEND_MESSAGE_FLAGS_NONE, |
| NULL, |
| &error)) |
| { |
| /* If we could get G_IO_ERROR_CLOSED here, it wouldn't be reasonable to |
| * critical; but we're holding the lock, and our caller checked whether |
| * we were already closed, so we can't get that error. |
| */ |
| g_critical ("Error while sending RemoveMatch() message: %s", error->message); |
| g_error_free (error); |
| } |
| g_object_unref (message); |
| } |
| |
| /* ---------------------------------------------------------------------------------------------------- */ |
| |
| static gboolean |
| is_signal_data_for_name_lost_or_acquired (SignalData *signal_data) |
| { |
| return g_strcmp0 (signal_data->sender_unique_name, "org.freedesktop.DBus") == 0 && |
| g_strcmp0 (signal_data->interface_name, "org.freedesktop.DBus") == 0 && |
| g_strcmp0 (signal_data->object_path, "/org/freedesktop/DBus") == 0 && |
| (g_strcmp0 (signal_data->member, "NameLost") == 0 || |
| g_strcmp0 (signal_data->member, "NameAcquired") == 0); |
| } |
| |
| /* ---------------------------------------------------------------------------------------------------- */ |
| |
| /** |
| * g_dbus_connection_signal_subscribe: |
| * @connection: a #GDBusConnection |
| * @sender: (nullable): sender name to match on (unique or well-known name) |
| * or %NULL to listen from all senders |
| * @interface_name: (nullable): D-Bus interface name to match on or %NULL to |
| * match on all interfaces |
| * @member: (nullable): D-Bus signal name to match on or %NULL to match on |
| * all signals |
| * @object_path: (nullable): object path to match on or %NULL to match on |
| * all object paths |
| * @arg0: (nullable): contents of first string argument to match on or %NULL |
| * to match on all kinds of arguments |
| * @flags: #GDBusSignalFlags describing how arg0 is used in subscribing to the |
| * signal |
| * @callback: callback to invoke when there is a signal matching the requested data |
| * @user_data: user data to pass to @callback |
| * @user_data_free_func: (nullable): function to free @user_data with when |
| * subscription is removed or %NULL |
| * |
| * Subscribes to signals on @connection and invokes @callback with a whenever |
| * the signal is received. Note that @callback will be invoked in the |
| * [thread-default main context][g-main-context-push-thread-default] |
| * of the thread you are calling this method from. |
| * |
| * If @connection is not a message bus connection, @sender must be |
| * %NULL. |
| * |
| * If @sender is a well-known name note that @callback is invoked with |
| * the unique name for the owner of @sender, not the well-known name |
| * as one would expect. This is because the message bus rewrites the |
| * name. As such, to avoid certain race conditions, users should be |
| * tracking the name owner of the well-known name and use that when |
| * processing the received signal. |
| * |
| * If one of %G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_NAMESPACE or |
| * %G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH are given, @arg0 is |
| * interpreted as part of a namespace or path. The first argument |
| * of a signal is matched against that part as specified by D-Bus. |
| * |
| * If @user_data_free_func is non-%NULL, it will be called (in the |
| * thread-default main context of the thread you are calling this |
| * method from) at some point after @user_data is no longer |
| * needed. (It is not guaranteed to be called synchronously when the |
| * signal is unsubscribed from, and may be called after @connection |
| * has been destroyed.) |
| * |
| * Returns: a subscription identifier that can be used with g_dbus_connection_signal_unsubscribe() |
| * |
| * Since: 2.26 |
| */ |
| guint |
| g_dbus_connection_signal_subscribe (GDBusConnection *connection, |
| const gchar *sender, |
| const gchar *interface_name, |
| const gchar *member, |
| const gchar *object_path, |
| const gchar *arg0, |
| GDBusSignalFlags flags, |
| GDBusSignalCallback callback, |
| gpointer user_data, |
| GDestroyNotify user_data_free_func) |
| { |
| gchar *rule; |
| SignalData *signal_data; |
| SignalSubscriber subscriber; |
| GPtrArray *signal_data_array; |
| const gchar *sender_unique_name; |
| |
| /* Right now we abort if AddMatch() fails since it can only fail with the bus being in |
| * an OOM condition. We might want to change that but that would involve making |
| * g_dbus_connection_signal_subscribe() asynchronous and having the call sites |
| * handle that. And there's really no sensible way of handling this short of retrying |
| * to add the match rule... and then there's the little thing that, hey, maybe there's |
| * a reason the bus in an OOM condition. |
| * |
| * Doable, but not really sure it's worth it... |
| */ |
| |
| g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), 0); |
| g_return_val_if_fail (sender == NULL || (g_dbus_is_name (sender) && (connection->flags & G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION)), 0); |
| g_return_val_if_fail (interface_name == NULL || g_dbus_is_interface_name (interface_name), 0); |
| g_return_val_if_fail (member == NULL || g_dbus_is_member_name (member), 0); |
| g_return_val_if_fail (object_path == NULL || g_variant_is_object_path (object_path), 0); |
| g_return_val_if_fail (callback != NULL, 0); |
| g_return_val_if_fail (check_initialized (connection), 0); |
| g_return_val_if_fail (!((flags & G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH) && (flags & G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_NAMESPACE)), 0); |
| g_return_val_if_fail (!(arg0 == NULL && (flags & (G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH | G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_NAMESPACE))), 0); |
| |
| CONNECTION_LOCK (connection); |
| |
| /* If G_DBUS_SIGNAL_FLAGS_NO_MATCH_RULE was specified, we will end up |
| * with a '-' character to prefix the rule (which will otherwise be |
| * normal). |
| * |
| * This allows us to hash the rule and do our lifecycle tracking in |
| * the usual way, but the '-' prevents the match rule from ever |
| * actually being send to the bus (either for add or remove). |
| */ |
| rule = args_to_rule (sender, interface_name, member, object_path, arg0, flags); |
| |
| if (sender != NULL && (g_dbus_is_unique_name (sender) || g_strcmp0 (sender, "org.freedesktop.DBus") == 0)) |
| sender_unique_name = sender; |
| else |
| sender_unique_name = ""; |
| |
| subscriber.callback = callback; |
| subscriber.user_data = user_data; |
| subscriber.user_data_free_func = user_data_free_func; |
| subscriber.id = g_atomic_int_add (&_global_subscriber_id, 1); /* TODO: overflow etc. */ |
| subscriber.context = g_main_context_ref_thread_default (); |
| |
| /* see if we've already have this rule */ |
| signal_data = g_hash_table_lookup (connection->map_rule_to_signal_data, rule); |
| if (signal_data != NULL) |
| { |
| g_array_append_val (signal_data->subscribers, subscriber); |
| g_free (rule); |
| goto out; |
| } |
| |
| signal_data = g_new0 (SignalData, 1); |
| signal_data->rule = rule; |
| signal_data->sender = g_strdup (sender); |
| signal_data->sender_unique_name = g_strdup (sender_unique_name); |
| signal_data->interface_name = g_strdup (interface_name); |
| signal_data->member = g_strdup (member); |
| signal_data->object_path = g_strdup (object_path); |
| signal_data->arg0 = g_strdup (arg0); |
| signal_data->flags = flags; |
| signal_data->subscribers = g_array_new (FALSE, FALSE, sizeof (SignalSubscriber)); |
| g_array_append_val (signal_data->subscribers, subscriber); |
| |
| g_hash_table_insert (connection->map_rule_to_signal_data, |
|