2010-05-06 20:13:59 +02:00
|
|
|
|
/* GDBus - GLib D-Bus Library
|
|
|
|
|
*
|
2010-05-09 19:14:55 +02:00
|
|
|
|
* Copyright (C) 2008-2010 Red Hat, Inc.
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
2022-05-18 10:12:45 +02:00
|
|
|
|
* SPDX-License-Identifier: LGPL-2.1-or-later
|
|
|
|
|
*
|
2010-05-06 20:13:59 +02:00
|
|
|
|
* 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
|
2017-05-27 18:21:30 +02:00
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
|
|
|
|
* 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
|
2014-01-23 12:58:29 +01:00
|
|
|
|
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
|
|
|
|
* Author: David Zeuthen <davidz@redhat.com>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* TODO for GDBus:
|
|
|
|
|
*
|
|
|
|
|
* - would be nice to expose GDBusAuthMechanism and an extension point
|
|
|
|
|
*
|
2010-05-07 20:57:20 +02:00
|
|
|
|
* - Need to rewrite GDBusAuth and rework GDBusAuthMechanism. In particular
|
|
|
|
|
* the mechanism VFuncs need to be able to set an error.
|
|
|
|
|
*
|
2010-05-12 04:50:40 +02:00
|
|
|
|
* - Need to document other mechanisms/sources for determining the D-Bus
|
2010-05-10 20:07:13 +02:00
|
|
|
|
* 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
|
2011-07-24 03:07:39 +02:00
|
|
|
|
* this has never really been specced out properly (except
|
2010-05-10 20:07:13 +02:00
|
|
|
|
* 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)
|
|
|
|
|
*
|
2010-05-07 20:56:01 +02:00
|
|
|
|
* - 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
|
2011-08-29 20:49:32 +02:00
|
|
|
|
* to easily achieve secure RPC via nonce-tcp.
|
2010-05-07 20:56:01 +02:00
|
|
|
|
*
|
2010-05-06 20:13:59 +02:00
|
|
|
|
* - 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
|
2010-07-01 20:37:16 +02:00
|
|
|
|
* debug output when using G_DBUS_DEBUG=message, e.g. in addition to
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
|
|
|
|
* 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.
|
2010-05-11 18:04:37 +02:00
|
|
|
|
*
|
|
|
|
|
* - 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)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2010-05-06 22:34:23 +02:00
|
|
|
|
#include <string.h>
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
#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"
|
2010-05-06 22:34:23 +02:00
|
|
|
|
#include "ginitable.h"
|
|
|
|
|
#include "gasyncinitable.h"
|
|
|
|
|
#include "giostream.h"
|
|
|
|
|
#include "gasyncresult.h"
|
2012-08-02 21:46:32 +02:00
|
|
|
|
#include "gtask.h"
|
2019-05-31 04:29:18 +02:00
|
|
|
|
#include "gmarshal-internal.h"
|
2010-05-06 22:34:23 +02:00
|
|
|
|
|
|
|
|
|
#ifdef G_OS_UNIX
|
2010-05-14 14:38:07 +02:00
|
|
|
|
#include "gunixconnection.h"
|
|
|
|
|
#include "gunixfdmessage.h"
|
2010-05-06 22:34:23 +02:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include "glibintl.h"
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2024-11-25 16:52:06 +01:00
|
|
|
|
#define G_DBUS_CONNECTION_FLAGS_ALL \
|
|
|
|
|
(G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT | \
|
|
|
|
|
G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER | \
|
|
|
|
|
G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS | \
|
|
|
|
|
G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION | \
|
|
|
|
|
G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING | \
|
|
|
|
|
G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_REQUIRE_SAME_USER | \
|
|
|
|
|
G_DBUS_CONNECTION_FLAGS_CROSS_NAMESPACE)
|
2021-02-08 12:57:45 +01:00
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
/**
|
2023-09-27 03:32:08 +02:00
|
|
|
|
* GDBusConnection:
|
|
|
|
|
*
|
|
|
|
|
* The `GDBusConnection` type is used for D-Bus connections to remote
|
|
|
|
|
* peers such as a message buses.
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
2023-09-27 03:32:08 +02:00
|
|
|
|
* 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
|
2023-10-23 12:17:24 +02:00
|
|
|
|
* as a [class@Gio.IOStream].
|
2010-05-09 07:44:11 +02:00
|
|
|
|
*
|
|
|
|
|
* This class is rarely used directly in D-Bus clients. If you are writing
|
2023-10-23 12:17:24 +02:00
|
|
|
|
* a D-Bus client, it is often easier to use the [func@Gio.bus_own_name],
|
|
|
|
|
* [func@Gio.bus_watch_name] or [func@Gio.DBusProxy.new_for_bus] APIs.
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
2014-01-31 05:52:58 +01:00
|
|
|
|
* As an exception to the usual GLib rule that a particular object must not
|
2023-09-27 03:32:08 +02:00
|
|
|
|
* be used by two threads at the same time, `GDBusConnection`s methods may be
|
2023-10-23 12:17:24 +02:00
|
|
|
|
* called from any thread. This is so that [func@Gio.bus_get] and
|
|
|
|
|
* [func@Gio.bus_get_sync] can safely return the same `GDBusConnection` when
|
|
|
|
|
* called from any thread.
|
2011-10-21 12:42:10 +02:00
|
|
|
|
*
|
2023-09-27 03:32:08 +02:00
|
|
|
|
* Most of the ways to obtain a `GDBusConnection` automatically initialize it
|
|
|
|
|
* (i.e. connect to D-Bus): for instance, [func@Gio.DBusConnection.new] and
|
2023-10-23 12:17:24 +02:00
|
|
|
|
* [func@Gio.bus_get], and the synchronous versions of those methods, give you
|
|
|
|
|
* an initialized connection. Language bindings for GIO should use
|
|
|
|
|
* [func@Gio.Initable.new] or [func@Gio.AsyncInitable.new_async], which also
|
2023-09-27 03:32:08 +02:00
|
|
|
|
* initialize the connection.
|
2011-10-20 21:02:02 +02:00
|
|
|
|
*
|
2023-09-27 03:32:08 +02:00
|
|
|
|
* If you construct an uninitialized `GDBusConnection`, such as via
|
2023-10-23 12:17:24 +02:00
|
|
|
|
* [ctor@GObject.Object.new], you must initialize it via [method@Gio.Initable.init] or
|
2023-09-27 03:32:08 +02:00
|
|
|
|
* [method@Gio.AsyncInitable.init_async] before using its methods or properties.
|
|
|
|
|
* Calling methods or accessing properties on a `GDBusConnection` that has not
|
2012-02-19 17:30:10 +01:00
|
|
|
|
* completed initialization successfully is considered to be invalid, and leads
|
|
|
|
|
* to undefined behaviour. In particular, if initialization fails with a
|
2023-09-27 03:32:08 +02:00
|
|
|
|
* `GError`, the only valid thing you can do with that `GDBusConnection` is to
|
2023-10-23 12:17:24 +02:00
|
|
|
|
* free it with [method@GObject.Object.unref].
|
2011-10-20 21:02:02 +02:00
|
|
|
|
*
|
2023-09-27 03:32:08 +02:00
|
|
|
|
* ## An example D-Bus server
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
2014-02-08 23:52:21 +01:00
|
|
|
|
* Here is an example for a D-Bus server:
|
2021-06-07 14:16:50 +02:00
|
|
|
|
* [gdbus-example-server.c](https://gitlab.gnome.org/GNOME/glib/-/blob/HEAD/gio/tests/gdbus-example-server.c)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
2023-09-27 03:32:08 +02:00
|
|
|
|
* ## An example for exporting a subtree
|
2010-05-13 06:39:26 +02:00
|
|
|
|
*
|
2014-02-08 23:52:21 +01:00
|
|
|
|
* Here is an example for exporting a subtree:
|
2021-06-07 14:16:50 +02:00
|
|
|
|
* [gdbus-example-subtree.c](https://gitlab.gnome.org/GNOME/glib/-/blob/HEAD/gio/tests/gdbus-example-subtree.c)
|
2014-02-08 23:52:21 +01:00
|
|
|
|
*
|
2023-09-27 03:32:08 +02:00
|
|
|
|
* ## An example for file descriptor passing
|
2014-02-08 23:52:21 +01:00
|
|
|
|
*
|
|
|
|
|
* Here is an example for passing UNIX file descriptors:
|
2021-06-07 14:16:50 +02:00
|
|
|
|
* [gdbus-unix-fd-client.c](https://gitlab.gnome.org/GNOME/glib/-/blob/HEAD/gio/tests/gdbus-example-unix-fd-client.c)
|
2014-02-08 23:52:21 +01:00
|
|
|
|
*
|
2023-09-27 03:32:08 +02:00
|
|
|
|
* ## An example for exporting a GObject
|
2014-02-08 23:52:21 +01:00
|
|
|
|
*
|
|
|
|
|
* Here is an example for exporting a #GObject:
|
2021-06-07 14:16:50 +02:00
|
|
|
|
* [gdbus-example-export.c](https://gitlab.gnome.org/GNOME/glib/-/blob/HEAD/gio/tests/gdbus-example-export.c)
|
2023-09-27 03:32:08 +02:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.26
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
2010-07-07 22:35:17 +02:00
|
|
|
|
typedef struct _GDBusConnectionClass GDBusConnectionClass;
|
|
|
|
|
|
2010-07-07 21:57:37 +02:00
|
|
|
|
/**
|
|
|
|
|
* 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);
|
|
|
|
|
};
|
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
G_LOCK_DEFINE_STATIC (message_bus_lock);
|
|
|
|
|
|
2011-12-06 13:44:36 +01:00
|
|
|
|
static GWeakRef the_session_bus;
|
|
|
|
|
static GWeakRef the_system_bus;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2011-10-20 14:12:26 +02:00
|
|
|
|
/* 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.
|
|
|
|
|
*/
|
2016-02-29 15:31:14 +01:00
|
|
|
|
#define SEND_MESSAGE_FLAGS_INITIALIZING (1u << 31)
|
2011-10-20 14:12:26 +02:00
|
|
|
|
|
|
|
|
|
/* Same as SEND_MESSAGE_FLAGS_INITIALIZING, but in GDBusCallFlags */
|
2016-02-29 15:31:14 +01:00
|
|
|
|
#define CALL_FLAGS_INITIALIZING (1u << 31)
|
2011-10-20 14:12:26 +02:00
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
2010-09-23 23:23:30 +02:00
|
|
|
|
typedef struct
|
|
|
|
|
{
|
|
|
|
|
GDestroyNotify callback;
|
|
|
|
|
gpointer user_data;
|
|
|
|
|
} 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)
|
|
|
|
|
{
|
|
|
|
|
g_free (data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* call_destroy_notify: <internal>
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @context: (nullable): A #GMainContext or %NULL.
|
|
|
|
|
* @callback: (nullable): A #GDestroyNotify or %NULL.
|
2010-09-23 23:23:30 +02:00
|
|
|
|
* @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)
|
|
|
|
|
{
|
2011-12-07 16:25:24 +01:00
|
|
|
|
GSource *idle_source;
|
|
|
|
|
CallDestroyNotifyData *data;
|
2011-10-05 16:46:57 +02:00
|
|
|
|
|
2010-09-23 23:23:30 +02:00
|
|
|
|
if (callback == NULL)
|
2020-02-20 14:20:41 +01:00
|
|
|
|
return;
|
2010-09-23 23:23:30 +02:00
|
|
|
|
|
2011-12-07 16:25:24 +01:00
|
|
|
|
data = g_new0 (CallDestroyNotifyData, 1);
|
|
|
|
|
data->callback = callback;
|
|
|
|
|
data->user_data = user_data;
|
2010-09-23 23:23:30 +02:00
|
|
|
|
|
2011-12-07 16:25:24 +01:00
|
|
|
|
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);
|
2021-07-26 11:53:02 +02:00
|
|
|
|
g_source_set_static_name (idle_source, "[gio] call_destroy_notify_data_in_idle");
|
2020-02-20 13:40:30 +01:00
|
|
|
|
g_source_attach (idle_source, context);
|
2011-12-07 16:25:24 +01:00
|
|
|
|
g_source_unref (idle_source);
|
2010-09-23 23:23:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
2024-03-14 20:24:24 +01:00
|
|
|
|
typedef struct
|
|
|
|
|
{
|
|
|
|
|
gatomicrefcount ref_count;
|
2024-05-10 15:11:32 +02:00
|
|
|
|
/* All remaining fields are immutable after construction. */
|
2024-03-14 20:24:24 +01:00
|
|
|
|
GDBusSignalCallback callback;
|
|
|
|
|
gpointer user_data;
|
|
|
|
|
GDestroyNotify user_data_free_func;
|
|
|
|
|
guint id;
|
|
|
|
|
GMainContext *context;
|
|
|
|
|
} SignalSubscriber;
|
|
|
|
|
|
|
|
|
|
static SignalSubscriber *
|
|
|
|
|
signal_subscriber_ref (SignalSubscriber *subscriber)
|
|
|
|
|
{
|
|
|
|
|
g_atomic_ref_count_inc (&subscriber->ref_count);
|
|
|
|
|
return subscriber;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
signal_subscriber_unref (SignalSubscriber *subscriber)
|
|
|
|
|
{
|
|
|
|
|
if (g_atomic_ref_count_dec (&subscriber->ref_count))
|
|
|
|
|
{
|
|
|
|
|
/* Destroy the user data. It doesn’t matter which thread
|
|
|
|
|
* signal_subscriber_unref() is called in (or whether it’s called with a
|
|
|
|
|
* lock held), as call_destroy_notify() always defers to the next
|
|
|
|
|
* #GMainContext iteration. */
|
|
|
|
|
call_destroy_notify (subscriber->context,
|
|
|
|
|
subscriber->user_data_free_func,
|
|
|
|
|
subscriber->user_data);
|
|
|
|
|
|
|
|
|
|
g_main_context_unref (subscriber->context);
|
|
|
|
|
g_free (subscriber);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
typedef struct
|
2024-04-23 21:42:17 +02:00
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* 1 reference while waiting for GetNameOwner() to finish
|
|
|
|
|
* 1 reference for each SignalData that points to this one as its
|
|
|
|
|
* shared_name_watcher
|
|
|
|
|
*/
|
|
|
|
|
grefcount ref_count;
|
|
|
|
|
|
|
|
|
|
gchar *owner;
|
|
|
|
|
guint32 get_name_owner_serial;
|
|
|
|
|
} WatchedName;
|
|
|
|
|
|
|
|
|
|
static WatchedName *
|
|
|
|
|
watched_name_new (void)
|
|
|
|
|
{
|
|
|
|
|
WatchedName *watched_name = g_new0 (WatchedName, 1);
|
|
|
|
|
|
|
|
|
|
g_ref_count_init (&watched_name->ref_count);
|
|
|
|
|
watched_name->owner = NULL;
|
|
|
|
|
return g_steal_pointer (&watched_name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
typedef struct SignalData SignalData;
|
|
|
|
|
|
|
|
|
|
struct SignalData
|
2024-03-14 20:24:24 +01:00
|
|
|
|
{
|
|
|
|
|
gchar *rule;
|
|
|
|
|
gchar *sender;
|
|
|
|
|
gchar *interface_name;
|
|
|
|
|
gchar *member;
|
|
|
|
|
gchar *object_path;
|
|
|
|
|
gchar *arg0;
|
|
|
|
|
GDBusSignalFlags flags;
|
|
|
|
|
GPtrArray *subscribers; /* (owned) (element-type SignalSubscriber) */
|
2024-04-23 21:42:17 +02:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* If the sender is a well-known name, this is an unowned SignalData
|
|
|
|
|
* representing the NameOwnerChanged signal that tracks its owner.
|
|
|
|
|
* NULL if sender is NULL.
|
|
|
|
|
* NULL if sender is its own owner (a unique name or DBUS_SERVICE_DBUS).
|
|
|
|
|
*
|
|
|
|
|
* Invariants: if not NULL, then
|
|
|
|
|
* shared_name_watcher->sender == DBUS_SERVICE_DBUS
|
|
|
|
|
* shared_name_watcher->interface_name == DBUS_INTERFACE_DBUS
|
|
|
|
|
* shared_name_watcher->member == "NameOwnerChanged"
|
|
|
|
|
* shared_name_watcher->object_path == DBUS_PATH_DBUS
|
|
|
|
|
* shared_name_watcher->arg0 == sender
|
|
|
|
|
* shared_name_watcher->flags == NONE
|
|
|
|
|
* shared_name_watcher->watched_name == NULL
|
|
|
|
|
*/
|
|
|
|
|
SignalData *shared_name_watcher;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Non-NULL if this SignalData is another SignalData's shared_name_watcher.
|
|
|
|
|
* One reference for each SignalData that has this one as its
|
|
|
|
|
* shared_name_watcher.
|
|
|
|
|
* Otherwise NULL.
|
|
|
|
|
*/
|
|
|
|
|
WatchedName *watched_name;
|
|
|
|
|
};
|
2024-03-14 20:24:24 +01:00
|
|
|
|
|
2024-03-14 20:30:12 +01:00
|
|
|
|
static SignalData *
|
|
|
|
|
signal_data_new_take (gchar *rule,
|
|
|
|
|
gchar *sender,
|
|
|
|
|
gchar *interface_name,
|
|
|
|
|
gchar *member,
|
|
|
|
|
gchar *object_path,
|
|
|
|
|
gchar *arg0,
|
|
|
|
|
GDBusSignalFlags flags)
|
|
|
|
|
{
|
|
|
|
|
SignalData *signal_data = g_new0 (SignalData, 1);
|
|
|
|
|
|
|
|
|
|
signal_data->rule = rule;
|
|
|
|
|
signal_data->sender = sender;
|
|
|
|
|
signal_data->interface_name = interface_name;
|
|
|
|
|
signal_data->member = member;
|
|
|
|
|
signal_data->object_path = object_path;
|
|
|
|
|
signal_data->arg0 = arg0;
|
|
|
|
|
signal_data->flags = flags;
|
|
|
|
|
signal_data->subscribers = g_ptr_array_new_with_free_func ((GDestroyNotify) signal_subscriber_unref);
|
|
|
|
|
return g_steal_pointer (&signal_data);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-14 20:24:24 +01:00
|
|
|
|
static void
|
|
|
|
|
signal_data_free (SignalData *signal_data)
|
|
|
|
|
{
|
2024-04-23 21:42:17 +02:00
|
|
|
|
/* The SignalData should not be freed while it still has subscribers */
|
|
|
|
|
g_assert (signal_data->subscribers->len == 0);
|
|
|
|
|
|
|
|
|
|
/* The SignalData should not be freed while it is watching for
|
|
|
|
|
* NameOwnerChanged on behalf of another SignalData */
|
|
|
|
|
g_assert (signal_data->watched_name == NULL);
|
|
|
|
|
|
|
|
|
|
/* The SignalData should be detached from its name watcher, if any,
|
|
|
|
|
* before it is freed */
|
|
|
|
|
g_assert (signal_data->shared_name_watcher == NULL);
|
|
|
|
|
|
2024-03-14 20:24:24 +01:00
|
|
|
|
g_free (signal_data->rule);
|
|
|
|
|
g_free (signal_data->sender);
|
|
|
|
|
g_free (signal_data->interface_name);
|
|
|
|
|
g_free (signal_data->member);
|
|
|
|
|
g_free (signal_data->object_path);
|
|
|
|
|
g_free (signal_data->arg0);
|
|
|
|
|
g_ptr_array_unref (signal_data->subscribers);
|
2024-04-23 21:42:17 +02:00
|
|
|
|
|
2024-03-14 20:24:24 +01:00
|
|
|
|
g_free (signal_data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
#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 { \
|
2011-10-04 05:26:55 +02:00
|
|
|
|
if (G_UNLIKELY (g_mutex_trylock(&(obj)->lock))) \
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{ \
|
|
|
|
|
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 { \
|
2011-10-04 05:26:55 +02:00
|
|
|
|
g_mutex_lock (&(obj)->lock); \
|
2010-05-06 20:13:59 +02:00
|
|
|
|
} while (FALSE)
|
|
|
|
|
|
|
|
|
|
#define CONNECTION_UNLOCK(obj) do { \
|
2011-10-04 05:26:55 +02:00
|
|
|
|
g_mutex_unlock (&(obj)->lock); \
|
2010-05-06 20:13:59 +02:00
|
|
|
|
} while (FALSE)
|
|
|
|
|
|
2011-10-21 17:02:22 +02:00
|
|
|
|
/* Flags in connection->atomic_flags */
|
|
|
|
|
enum {
|
2011-10-17 20:27:23 +02:00
|
|
|
|
FLAG_INITIALIZED = 1 << 0,
|
GDBusConnection: make the closed flag atomic (but still lock to write)
Strictly speaking, neither of the two uses that aren't under the lock
*needs* to be atomic, but it seems better to be obviously correct (and
we save another 4 bytes of struct).
One of these uses is in g_dbus_connection_is_closed(), any use of which
is inherently a race condition anyway.
The other is g_dbus_connection_flush_sync, which as far as I can tell
just needs a best-effort check, to not waste effort on a connection that
has been closed for a while (but I could be wrong).
I removed the check for the closed flag altogether in
g_dbus_connection_send_message_with_reply_unlocked, because it turns out
to be redundant with one in g_dbus_connection_send_message_unlocked,
which is called immediately after.
g_dbus_connection_close_sync held the lock to check the closed flag,
which is no longer needed.
As far as I can tell, the only reason why the lock is still desirable
when setting the closed flag is so that remove_match_rule can't fail
by racing with close notification from the worker thread - but
on_worker_closed needs to hold the lock anyway, to deal with other
data structures, so there's no point in trying to eliminate the
requirement to hold the lock.
Bug: https://bugzilla.gnome.org/show_bug.cgi?id=661992
Signed-off-by: Simon McVittie <simon.mcvittie@collabora.co.uk>
Reviewed-by: David Zeuthen <davidz@redhat.com>
2011-10-21 16:46:00 +02:00
|
|
|
|
FLAG_EXIT_ON_CLOSE = 1 << 1,
|
|
|
|
|
FLAG_CLOSED = 1 << 2
|
2011-10-21 17:02:22 +02:00
|
|
|
|
};
|
|
|
|
|
|
2010-07-07 22:35:17 +02:00
|
|
|
|
struct _GDBusConnection
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
2010-07-07 22:35:17 +02:00
|
|
|
|
/*< private >*/
|
|
|
|
|
GObject parent_instance;
|
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
|
/* -- General object state ------------------------------------------------ */
|
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
|
|
2011-10-21 17:24:29 +02:00
|
|
|
|
/* General-purpose lock for most fields */
|
2011-10-04 05:26:55 +02:00
|
|
|
|
GMutex lock;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
/* A lock used in the init() method of the GInitable interface - see comments
|
2011-10-21 17:24:29 +02:00
|
|
|
|
* in initable_init() for why a separate lock is needed.
|
|
|
|
|
*
|
|
|
|
|
* If you need both @lock and @init_lock, you must take @init_lock first.
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*/
|
2011-10-04 05:26:55 +02:00
|
|
|
|
GMutex init_lock;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
/* Set (by loading the contents of /var/lib/dbus/machine-id) the first time
|
2024-05-13 14:00:34 +02:00
|
|
|
|
* someone calls DBUS_INTERFACE_PEER.GetMachineId(). Protected by @lock.
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*/
|
|
|
|
|
gchar *machine_id;
|
|
|
|
|
|
2011-10-20 14:12:26 +02:00
|
|
|
|
/* 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.
|
|
|
|
|
*/
|
2010-05-06 20:13:59 +02:00
|
|
|
|
GIOStream *stream;
|
|
|
|
|
|
2011-10-20 14:12:26 +02:00
|
|
|
|
/* 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.
|
|
|
|
|
*/
|
2010-05-06 20:13:59 +02:00
|
|
|
|
GDBusAuth *auth;
|
|
|
|
|
|
2011-10-21 17:24:29 +02:00
|
|
|
|
/* Last serial used. Protected by @lock. */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
guint32 last_serial;
|
|
|
|
|
|
2011-10-20 14:12:26 +02:00
|
|
|
|
/* 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.
|
|
|
|
|
*/
|
2010-05-06 20:13:59 +02:00
|
|
|
|
GDBusWorker *worker;
|
|
|
|
|
|
|
|
|
|
/* If connected to a message bus, this contains the unique name assigned to
|
2011-10-20 14:12:26 +02:00
|
|
|
|
* 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.
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*/
|
|
|
|
|
gchar *bus_unique_name;
|
|
|
|
|
|
2021-06-10 13:37:23 +02:00
|
|
|
|
/* The GUID returned by the other side if we authenticated as a client or
|
2011-10-20 14:12:26 +02:00
|
|
|
|
* 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.
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*/
|
|
|
|
|
gchar *guid;
|
|
|
|
|
|
2011-10-21 17:02:22 +02:00
|
|
|
|
/* FLAG_INITIALIZED is set exactly when initable_init() has finished running.
|
|
|
|
|
* Inspect @initialization_error to see whether it succeeded or failed.
|
2011-10-17 20:27:23 +02:00
|
|
|
|
*
|
|
|
|
|
* FLAG_EXIT_ON_CLOSE is the exit-on-close property.
|
GDBusConnection: make the closed flag atomic (but still lock to write)
Strictly speaking, neither of the two uses that aren't under the lock
*needs* to be atomic, but it seems better to be obviously correct (and
we save another 4 bytes of struct).
One of these uses is in g_dbus_connection_is_closed(), any use of which
is inherently a race condition anyway.
The other is g_dbus_connection_flush_sync, which as far as I can tell
just needs a best-effort check, to not waste effort on a connection that
has been closed for a while (but I could be wrong).
I removed the check for the closed flag altogether in
g_dbus_connection_send_message_with_reply_unlocked, because it turns out
to be redundant with one in g_dbus_connection_send_message_unlocked,
which is called immediately after.
g_dbus_connection_close_sync held the lock to check the closed flag,
which is no longer needed.
As far as I can tell, the only reason why the lock is still desirable
when setting the closed flag is so that remove_match_rule can't fail
by racing with close notification from the worker thread - but
on_worker_closed needs to hold the lock anyway, to deal with other
data structures, so there's no point in trying to eliminate the
requirement to hold the lock.
Bug: https://bugzilla.gnome.org/show_bug.cgi?id=661992
Signed-off-by: Simon McVittie <simon.mcvittie@collabora.co.uk>
Reviewed-by: David Zeuthen <davidz@redhat.com>
2011-10-21 16:46:00 +02:00
|
|
|
|
*
|
|
|
|
|
* FLAG_CLOSED is the closed property. It may be read at any time, but
|
|
|
|
|
* may only be written while holding @lock.
|
2011-10-21 17:02:22 +02:00
|
|
|
|
*/
|
2020-11-11 19:24:28 +01:00
|
|
|
|
gint atomic_flags; /* (atomic) */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2011-10-21 17:02:22 +02:00
|
|
|
|
/* 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.
|
|
|
|
|
*/
|
2010-05-06 20:13:59 +02:00
|
|
|
|
GError *initialization_error;
|
|
|
|
|
|
2011-10-05 16:46:57 +02:00
|
|
|
|
/* The result of g_main_context_ref_thread_default() when the object
|
2010-05-06 20:13:59 +02:00
|
|
|
|
* was created (the GObject _init() function) - this is used for delivery
|
|
|
|
|
* of the :closed GObject signal.
|
2011-10-21 17:24:29 +02:00
|
|
|
|
*
|
|
|
|
|
* Only set in the GObject init function, so no locks are needed.
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*/
|
|
|
|
|
GMainContext *main_context_at_construction;
|
|
|
|
|
|
2011-10-21 17:24:29 +02:00
|
|
|
|
/* Read-only construct properties, no locks needed */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
gchar *address;
|
|
|
|
|
GDBusConnectionFlags flags;
|
|
|
|
|
|
2011-10-21 17:24:29 +02:00
|
|
|
|
/* Map used for managing method replies, protected by @lock */
|
2023-02-22 13:19:16 +01:00
|
|
|
|
GHashTable *map_method_serial_to_task; /* guint32 -> owned GTask* */
|
2024-04-23 21:42:17 +02:00
|
|
|
|
GHashTable *map_method_serial_to_name_watcher; /* guint32 -> unowned SignalData* */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2011-10-21 17:24:29 +02:00
|
|
|
|
/* Maps used for managing signal subscription, protected by @lock */
|
2010-06-11 21:45:18 +02:00
|
|
|
|
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 */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2011-10-21 17:24:29 +02:00
|
|
|
|
/* Maps used for managing exported objects and subtrees,
|
|
|
|
|
* protected by @lock
|
|
|
|
|
*/
|
2010-05-06 20:13:59 +02:00
|
|
|
|
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* */
|
|
|
|
|
|
2012-06-06 19:44:39 +02:00
|
|
|
|
/* Map used for storing last used serials for each thread, protected by @lock */
|
|
|
|
|
GHashTable *map_thread_to_last_serial;
|
|
|
|
|
|
2011-10-21 17:24:29 +02:00
|
|
|
|
/* Structure used for message filters, protected by @lock */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
GPtrArray *filters;
|
|
|
|
|
|
2011-10-20 14:12:26 +02:00
|
|
|
|
/* Capabilities negotiated during authentication
|
|
|
|
|
* Read-only after initable_init(), so it may be read without holding a
|
|
|
|
|
* lock, if you check for initialization first.
|
|
|
|
|
*/
|
2010-05-06 20:13:59 +02:00
|
|
|
|
GDBusCapabilityFlags capabilities;
|
|
|
|
|
|
2011-10-21 17:24:29 +02:00
|
|
|
|
/* Protected by @init_lock */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
GDBusAuthObserver *authentication_observer;
|
2011-10-20 14:12:26 +02:00
|
|
|
|
|
|
|
|
|
/* Read-only after initable_init(), so it may be read if you either
|
|
|
|
|
* hold @init_lock or check for initialization first.
|
|
|
|
|
*/
|
2010-07-31 05:44:30 +02:00
|
|
|
|
GCredentials *credentials;
|
2010-07-30 22:01:03 +02:00
|
|
|
|
|
|
|
|
|
/* set to TRUE when finalizing */
|
|
|
|
|
gboolean finalizing;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
typedef struct ExportedObject ExportedObject;
|
|
|
|
|
static void exported_object_free (ExportedObject *eo);
|
|
|
|
|
|
|
|
|
|
typedef struct ExportedSubtree ExportedSubtree;
|
2021-09-24 10:03:40 +02:00
|
|
|
|
static ExportedSubtree *exported_subtree_ref (ExportedSubtree *es);
|
|
|
|
|
static void exported_subtree_unref (ExportedSubtree *es);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
2013-04-17 15:24:36 +02:00
|
|
|
|
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);
|
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
#define _G_ENSURE_LOCK(name) do { \
|
|
|
|
|
if (G_UNLIKELY (G_TRYLOCK(name))) \
|
|
|
|
|
{ \
|
|
|
|
|
g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
|
2013-05-20 22:54:48 +02:00
|
|
|
|
"_G_ENSURE_LOCK: Lock '" #name "' is not locked"); \
|
2010-05-06 20:13:59 +02:00
|
|
|
|
} \
|
|
|
|
|
} while (FALSE) \
|
|
|
|
|
|
|
|
|
|
static guint signals[LAST_SIGNAL] = { 0 };
|
|
|
|
|
|
2010-05-10 04:13:18 +02:00
|
|
|
|
static void initable_iface_init (GInitableIface *initable_iface);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
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)
|
|
|
|
|
);
|
|
|
|
|
|
2011-10-20 14:12:26 +02:00
|
|
|
|
/*
|
|
|
|
|
* 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;
|
|
|
|
|
}
|
|
|
|
|
|
GDBusConnection: make the closed flag atomic (but still lock to write)
Strictly speaking, neither of the two uses that aren't under the lock
*needs* to be atomic, but it seems better to be obviously correct (and
we save another 4 bytes of struct).
One of these uses is in g_dbus_connection_is_closed(), any use of which
is inherently a race condition anyway.
The other is g_dbus_connection_flush_sync, which as far as I can tell
just needs a best-effort check, to not waste effort on a connection that
has been closed for a while (but I could be wrong).
I removed the check for the closed flag altogether in
g_dbus_connection_send_message_with_reply_unlocked, because it turns out
to be redundant with one in g_dbus_connection_send_message_unlocked,
which is called immediately after.
g_dbus_connection_close_sync held the lock to check the closed flag,
which is no longer needed.
As far as I can tell, the only reason why the lock is still desirable
when setting the closed flag is so that remove_match_rule can't fail
by racing with close notification from the worker thread - but
on_worker_closed needs to hold the lock anyway, to deal with other
data structures, so there's no point in trying to eliminate the
requirement to hold the lock.
Bug: https://bugzilla.gnome.org/show_bug.cgi?id=661992
Signed-off-by: Simon McVittie <simon.mcvittie@collabora.co.uk>
Reviewed-by: David Zeuthen <davidz@redhat.com>
2011-10-21 16:46:00 +02:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-23 21:10:50 +02:00
|
|
|
|
static GHashTable *alive_connections = NULL;
|
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
static void
|
|
|
|
|
g_dbus_connection_dispose (GObject *object)
|
|
|
|
|
{
|
|
|
|
|
GDBusConnection *connection = G_DBUS_CONNECTION (object);
|
|
|
|
|
|
|
|
|
|
G_LOCK (message_bus_lock);
|
2010-09-23 21:10:50 +02:00
|
|
|
|
CONNECTION_LOCK (connection);
|
2010-07-07 22:35:17 +02:00
|
|
|
|
if (connection->worker != NULL)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
2010-07-07 22:35:17 +02:00
|
|
|
|
_g_dbus_worker_stop (connection->worker);
|
|
|
|
|
connection->worker = NULL;
|
2010-09-23 21:10:50 +02:00
|
|
|
|
if (alive_connections != NULL)
|
|
|
|
|
g_warn_if_fail (g_hash_table_remove (alive_connections, connection));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (alive_connections != NULL)
|
2017-10-25 11:53:14 +02:00
|
|
|
|
g_warn_if_fail (!g_hash_table_contains (alive_connections, connection));
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
2010-09-23 21:10:50 +02:00
|
|
|
|
CONNECTION_UNLOCK (connection);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
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);
|
|
|
|
|
|
2010-07-30 22:01:03 +02:00
|
|
|
|
connection->finalizing = TRUE;
|
|
|
|
|
|
|
|
|
|
purge_all_signal_subscriptions (connection);
|
|
|
|
|
|
|
|
|
|
purge_all_filters (connection);
|
|
|
|
|
g_ptr_array_unref (connection->filters);
|
|
|
|
|
|
2010-07-07 22:35:17 +02:00
|
|
|
|
if (connection->authentication_observer != NULL)
|
|
|
|
|
g_object_unref (connection->authentication_observer);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2010-07-07 22:35:17 +02:00
|
|
|
|
if (connection->auth != NULL)
|
|
|
|
|
g_object_unref (connection->auth);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2010-09-14 19:38:14 +02:00
|
|
|
|
if (connection->credentials)
|
|
|
|
|
g_object_unref (connection->credentials);
|
|
|
|
|
|
2010-07-07 22:35:17 +02:00
|
|
|
|
if (connection->stream != NULL)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
2010-07-07 22:35:17 +02:00
|
|
|
|
g_object_unref (connection->stream);
|
|
|
|
|
connection->stream = NULL;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
2010-07-07 22:35:17 +02:00
|
|
|
|
g_free (connection->address);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2010-07-07 22:35:17 +02:00
|
|
|
|
g_free (connection->guid);
|
|
|
|
|
g_free (connection->bus_unique_name);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2010-07-07 22:35:17 +02:00
|
|
|
|
if (connection->initialization_error != NULL)
|
|
|
|
|
g_error_free (connection->initialization_error);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2012-08-02 21:46:32 +02:00
|
|
|
|
g_hash_table_unref (connection->map_method_serial_to_task);
|
2024-04-23 21:42:17 +02:00
|
|
|
|
g_hash_table_unref (connection->map_method_serial_to_name_watcher);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2010-07-07 22:35:17 +02:00
|
|
|
|
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);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2010-07-07 22:35:17 +02:00
|
|
|
|
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);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2012-06-06 19:44:39 +02:00
|
|
|
|
g_hash_table_unref (connection->map_thread_to_last_serial);
|
|
|
|
|
|
2011-10-05 16:46:57 +02:00
|
|
|
|
g_main_context_unref (connection->main_context_at_construction);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2010-07-07 22:35:17 +02:00
|
|
|
|
g_free (connection->machine_id);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2011-10-04 05:26:55 +02:00
|
|
|
|
g_mutex_clear (&connection->init_lock);
|
|
|
|
|
g_mutex_clear (&connection->lock);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2010-05-10 04:13:18 +02:00
|
|
|
|
G_OBJECT_CLASS (g_dbus_connection_parent_class)->finalize (object);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-10-17 14:27:03 +02:00
|
|
|
|
/* called in any user thread, with the connection's lock not held */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
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;
|
|
|
|
|
|
2018-12-20 01:33:50 +01:00
|
|
|
|
case PROP_FLAGS:
|
|
|
|
|
g_value_set_flags (value, g_dbus_connection_get_flags (connection));
|
|
|
|
|
break;
|
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
default:
|
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-17 14:27:03 +02:00
|
|
|
|
/* called in any user thread, with the connection's lock not held */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
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:
|
2010-07-07 22:35:17 +02:00
|
|
|
|
connection->stream = g_value_dup_object (value);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case PROP_GUID:
|
2010-07-07 22:35:17 +02:00
|
|
|
|
connection->guid = g_value_dup_string (value);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case PROP_ADDRESS:
|
2010-07-07 22:35:17 +02:00
|
|
|
|
connection->address = g_value_dup_string (value);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case PROP_FLAGS:
|
2010-07-07 22:35:17 +02:00
|
|
|
|
connection->flags = g_value_get_flags (value);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case PROP_EXIT_ON_CLOSE:
|
|
|
|
|
g_dbus_connection_set_exit_on_close (connection, g_value_get_boolean (value));
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case PROP_AUTHENTICATION_OBSERVER:
|
2010-07-07 22:35:17 +02:00
|
|
|
|
connection->authentication_observer = g_value_dup_object (value);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-17 14:27:03 +02:00
|
|
|
|
/* Base-class implementation of GDBusConnection::closed.
|
|
|
|
|
*
|
|
|
|
|
* Called in a user thread, by the main context that was thread-default when
|
|
|
|
|
* the object was constructed.
|
|
|
|
|
*/
|
2010-05-06 20:13:59 +02:00
|
|
|
|
static void
|
|
|
|
|
g_dbus_connection_real_closed (GDBusConnection *connection,
|
|
|
|
|
gboolean remote_peer_vanished,
|
|
|
|
|
GError *error)
|
|
|
|
|
{
|
2011-10-21 17:02:22 +02:00
|
|
|
|
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.
|
|
|
|
|
*/
|
2011-10-17 20:27:23 +02:00
|
|
|
|
if (remote_peer_vanished &&
|
|
|
|
|
(flags & FLAG_EXIT_ON_CLOSE) != 0 &&
|
2011-10-21 17:02:22 +02:00
|
|
|
|
(flags & FLAG_INITIALIZED) != 0 &&
|
|
|
|
|
connection->initialization_error == NULL)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
|
|
|
|
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.
|
2010-05-06 22:02:08 +02:00
|
|
|
|
*
|
2011-05-12 21:36:34 +02:00
|
|
|
|
* If this is passed on construction and is a #GSocketConnection,
|
|
|
|
|
* then the corresponding #GSocket will be put into non-blocking mode.
|
|
|
|
|
*
|
2011-10-13 18:39:03 +02:00
|
|
|
|
* 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.
|
|
|
|
|
*
|
2010-05-06 22:02:08 +02:00
|
|
|
|
* Since: 2.26
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*/
|
|
|
|
|
g_object_class_install_property (gobject_class,
|
|
|
|
|
PROP_STREAM,
|
2023-04-28 01:59:26 +02:00
|
|
|
|
g_param_spec_object ("stream", NULL, NULL,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
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.
|
2010-05-06 22:02:08 +02:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.26
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*/
|
|
|
|
|
g_object_class_install_property (gobject_class,
|
|
|
|
|
PROP_ADDRESS,
|
2023-04-28 01:59:26 +02:00
|
|
|
|
g_param_spec_string ("address", NULL, NULL,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
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.
|
2010-05-06 22:02:08 +02:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.26
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*/
|
|
|
|
|
g_object_class_install_property (gobject_class,
|
|
|
|
|
PROP_FLAGS,
|
2023-04-28 01:59:26 +02:00
|
|
|
|
g_param_spec_flags ("flags", NULL, NULL,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
G_TYPE_DBUS_CONNECTION_FLAGS,
|
|
|
|
|
G_DBUS_CONNECTION_FLAGS_NONE,
|
2018-12-20 01:33:50 +01:00
|
|
|
|
G_PARAM_READABLE |
|
2010-05-06 20:13:59 +02:00
|
|
|
|
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
|
2020-12-15 10:14:20 +01:00
|
|
|
|
* #GDBusConnection:flags property then you **must** also set this
|
2010-05-06 20:13:59 +02:00
|
|
|
|
* 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
|
2010-05-10 14:07:07 +02:00
|
|
|
|
* of the other peer here after the connection has been successfully
|
2010-05-06 20:13:59 +02:00
|
|
|
|
* initialized.
|
2010-05-06 22:02:08 +02:00
|
|
|
|
*
|
2021-06-10 13:36:25 +02:00
|
|
|
|
* Note that the
|
|
|
|
|
* [D-Bus specification](https://dbus.freedesktop.org/doc/dbus-specification.html#addresses)
|
|
|
|
|
* uses the term ‘UUID’ to refer to this, whereas GLib consistently uses the
|
|
|
|
|
* term ‘GUID’ for historical reasons.
|
|
|
|
|
*
|
|
|
|
|
* Despite its name, the format of #GDBusConnection:guid does not follow
|
|
|
|
|
* [RFC 4122](https://datatracker.ietf.org/doc/html/rfc4122) or the Microsoft
|
|
|
|
|
* GUID format.
|
|
|
|
|
*
|
2010-05-06 22:02:08 +02:00
|
|
|
|
* Since: 2.26
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*/
|
|
|
|
|
g_object_class_install_property (gobject_class,
|
|
|
|
|
PROP_GUID,
|
2023-04-28 01:59:26 +02:00
|
|
|
|
g_param_spec_string ("guid", NULL, NULL,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
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.
|
2010-05-06 22:02:08 +02:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.26
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*/
|
|
|
|
|
g_object_class_install_property (gobject_class,
|
|
|
|
|
PROP_UNIQUE_NAME,
|
2023-04-28 01:59:26 +02:00
|
|
|
|
g_param_spec_string ("unique-name", NULL, NULL,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
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.
|
2010-05-06 22:02:08 +02:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.26
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*/
|
|
|
|
|
g_object_class_install_property (gobject_class,
|
|
|
|
|
PROP_CLOSED,
|
2023-04-28 01:59:26 +02:00
|
|
|
|
g_param_spec_boolean ("closed", NULL, NULL,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
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
|
2014-02-06 14:04:52 +01:00
|
|
|
|
* calling `raise(SIGTERM)`) if the connection is closed by the
|
|
|
|
|
* remote peer.
|
2010-05-06 22:02:08 +02:00
|
|
|
|
*
|
2014-02-06 14:04:52 +01:00
|
|
|
|
* Note that #GDBusConnection objects returned by g_bus_get_finish()
|
|
|
|
|
* and g_bus_get_sync() will (usually) have this property set to %TRUE.
|
2012-01-18 11:14:37 +01:00
|
|
|
|
*
|
2010-05-06 22:02:08 +02:00
|
|
|
|
* Since: 2.26
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*/
|
|
|
|
|
g_object_class_install_property (gobject_class,
|
|
|
|
|
PROP_EXIT_ON_CLOSE,
|
2023-04-28 01:59:26 +02:00
|
|
|
|
g_param_spec_boolean ("exit-on-close", NULL, NULL,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
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.
|
2010-05-06 22:02:08 +02:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.26
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*/
|
|
|
|
|
g_object_class_install_property (gobject_class,
|
|
|
|
|
PROP_CAPABILITY_FLAGS,
|
2023-04-28 01:59:26 +02:00
|
|
|
|
g_param_spec_flags ("capabilities", NULL, NULL,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
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.
|
2010-05-06 22:02:08 +02:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.26
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*/
|
|
|
|
|
g_object_class_install_property (gobject_class,
|
|
|
|
|
PROP_AUTHENTICATION_OBSERVER,
|
2023-04-28 01:59:26 +02:00
|
|
|
|
g_param_spec_object ("authentication-observer", NULL, NULL,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
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:
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @connection: the #GDBusConnection emitting the signal
|
2010-05-06 20:13:59 +02:00
|
|
|
|
* @remote_peer_vanished: %TRUE if @connection is closed because the
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* remote peer closed its end of the connection
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @error: (nullable): a #GError with more details about the event or %NULL
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
|
|
|
|
* Emitted when the connection is closed.
|
|
|
|
|
*
|
|
|
|
|
* The cause of this event can be
|
2014-02-01 05:42:40 +01:00
|
|
|
|
*
|
|
|
|
|
* - 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.
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
|
|
|
|
* Upon receiving this signal, you should give up your reference to
|
|
|
|
|
* @connection. You are guaranteed that this signal is emitted only
|
|
|
|
|
* once.
|
2010-05-06 22:02:08 +02:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.26
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*/
|
2015-09-12 06:00:40 +02:00
|
|
|
|
signals[CLOSED_SIGNAL] = g_signal_new (I_("closed"),
|
2010-05-06 20:13:59 +02:00
|
|
|
|
G_TYPE_DBUS_CONNECTION,
|
|
|
|
|
G_SIGNAL_RUN_LAST,
|
|
|
|
|
G_STRUCT_OFFSET (GDBusConnectionClass, closed),
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
2019-05-31 04:29:18 +02:00
|
|
|
|
_g_cclosure_marshal_VOID__BOOLEAN_BOXED,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
G_TYPE_NONE,
|
|
|
|
|
2,
|
|
|
|
|
G_TYPE_BOOLEAN,
|
|
|
|
|
G_TYPE_ERROR);
|
2019-05-31 04:29:18 +02:00
|
|
|
|
g_signal_set_va_marshaller (signals[CLOSED_SIGNAL],
|
|
|
|
|
G_TYPE_FROM_CLASS (klass),
|
|
|
|
|
_g_cclosure_marshal_VOID__BOOLEAN_BOXEDv);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
g_dbus_connection_init (GDBusConnection *connection)
|
|
|
|
|
{
|
2011-10-04 05:26:55 +02:00
|
|
|
|
g_mutex_init (&connection->lock);
|
|
|
|
|
g_mutex_init (&connection->init_lock);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2023-02-22 13:19:16 +01:00
|
|
|
|
connection->map_method_serial_to_task = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_object_unref);
|
2024-04-23 21:42:17 +02:00
|
|
|
|
connection->map_method_serial_to_name_watcher = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, NULL);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2010-07-07 22:35:17 +02:00
|
|
|
|
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,
|
2010-08-31 19:42:32 +02:00
|
|
|
|
(GDestroyNotify) g_ptr_array_unref);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2010-07-07 22:35:17 +02:00
|
|
|
|
connection->map_object_path_to_eo = g_hash_table_new_full (g_str_hash,
|
|
|
|
|
g_str_equal,
|
|
|
|
|
NULL,
|
|
|
|
|
(GDestroyNotify) exported_object_free);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2010-07-07 22:35:17 +02:00
|
|
|
|
connection->map_id_to_ei = g_hash_table_new (g_direct_hash,
|
|
|
|
|
g_direct_equal);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2010-07-07 22:35:17 +02:00
|
|
|
|
connection->map_object_path_to_es = g_hash_table_new_full (g_str_hash,
|
|
|
|
|
g_str_equal,
|
|
|
|
|
NULL,
|
2021-09-24 10:03:40 +02:00
|
|
|
|
(GDestroyNotify) exported_subtree_unref);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2010-07-07 22:35:17 +02:00
|
|
|
|
connection->map_id_to_es = g_hash_table_new (g_direct_hash,
|
|
|
|
|
g_direct_equal);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2012-06-06 19:44:39 +02:00
|
|
|
|
connection->map_thread_to_last_serial = g_hash_table_new (g_direct_hash,
|
|
|
|
|
g_direct_equal);
|
|
|
|
|
|
2011-10-05 16:46:57 +02:00
|
|
|
|
connection->main_context_at_construction = g_main_context_ref_thread_default ();
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2010-07-07 22:35:17 +02:00
|
|
|
|
connection->filters = g_ptr_array_new ();
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
2010-05-10 14:07:07 +02:00
|
|
|
|
/**
|
|
|
|
|
* g_dbus_connection_get_stream:
|
|
|
|
|
* @connection: a #GDBusConnection
|
|
|
|
|
*
|
|
|
|
|
* Gets the underlying stream used for IO.
|
|
|
|
|
*
|
2011-10-13 18:39:03 +02:00
|
|
|
|
* 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.
|
|
|
|
|
*
|
2020-12-15 10:13:22 +01:00
|
|
|
|
* Returns: (transfer none) (not nullable): the stream used for IO
|
2010-05-10 14:07:07 +02:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.26
|
|
|
|
|
*/
|
2010-05-06 20:13:59 +02:00
|
|
|
|
GIOStream *
|
|
|
|
|
g_dbus_connection_get_stream (GDBusConnection *connection)
|
|
|
|
|
{
|
|
|
|
|
g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
|
2011-10-20 14:12:26 +02:00
|
|
|
|
|
|
|
|
|
/* do not use g_return_val_if_fail(), we want the memory barrier */
|
|
|
|
|
if (!check_initialized (connection))
|
|
|
|
|
return NULL;
|
|
|
|
|
|
2010-07-07 22:35:17 +02:00
|
|
|
|
return connection->stream;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
2010-06-30 17:43:42 +02:00
|
|
|
|
/**
|
|
|
|
|
* g_dbus_connection_start_message_processing:
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @connection: a #GDBusConnection
|
2010-06-30 17:43:42 +02:00
|
|
|
|
*
|
|
|
|
|
* 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));
|
2011-10-20 14:12:26 +02:00
|
|
|
|
|
|
|
|
|
/* do not use g_return_val_if_fail(), we want the memory barrier */
|
|
|
|
|
if (!check_initialized (connection))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
g_assert (connection->worker != NULL);
|
2010-07-07 22:35:17 +02:00
|
|
|
|
_g_dbus_worker_unfreeze (connection->worker);
|
2010-06-30 17:43:42 +02:00
|
|
|
|
}
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_dbus_connection_is_closed:
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @connection: a #GDBusConnection
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
|
|
|
|
* Gets whether @connection is closed.
|
|
|
|
|
*
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* Returns: %TRUE if the connection is closed, %FALSE otherwise
|
2010-05-06 22:02:08 +02:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.26
|
|
|
|
|
*/
|
2010-05-06 20:13:59 +02:00
|
|
|
|
gboolean
|
|
|
|
|
g_dbus_connection_is_closed (GDBusConnection *connection)
|
|
|
|
|
{
|
GDBusConnection: make the closed flag atomic (but still lock to write)
Strictly speaking, neither of the two uses that aren't under the lock
*needs* to be atomic, but it seems better to be obviously correct (and
we save another 4 bytes of struct).
One of these uses is in g_dbus_connection_is_closed(), any use of which
is inherently a race condition anyway.
The other is g_dbus_connection_flush_sync, which as far as I can tell
just needs a best-effort check, to not waste effort on a connection that
has been closed for a while (but I could be wrong).
I removed the check for the closed flag altogether in
g_dbus_connection_send_message_with_reply_unlocked, because it turns out
to be redundant with one in g_dbus_connection_send_message_unlocked,
which is called immediately after.
g_dbus_connection_close_sync held the lock to check the closed flag,
which is no longer needed.
As far as I can tell, the only reason why the lock is still desirable
when setting the closed flag is so that remove_match_rule can't fail
by racing with close notification from the worker thread - but
on_worker_closed needs to hold the lock anyway, to deal with other
data structures, so there's no point in trying to eliminate the
requirement to hold the lock.
Bug: https://bugzilla.gnome.org/show_bug.cgi?id=661992
Signed-off-by: Simon McVittie <simon.mcvittie@collabora.co.uk>
Reviewed-by: David Zeuthen <davidz@redhat.com>
2011-10-21 16:46:00 +02:00
|
|
|
|
gint flags;
|
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
|
GDBusConnection: make the closed flag atomic (but still lock to write)
Strictly speaking, neither of the two uses that aren't under the lock
*needs* to be atomic, but it seems better to be obviously correct (and
we save another 4 bytes of struct).
One of these uses is in g_dbus_connection_is_closed(), any use of which
is inherently a race condition anyway.
The other is g_dbus_connection_flush_sync, which as far as I can tell
just needs a best-effort check, to not waste effort on a connection that
has been closed for a while (but I could be wrong).
I removed the check for the closed flag altogether in
g_dbus_connection_send_message_with_reply_unlocked, because it turns out
to be redundant with one in g_dbus_connection_send_message_unlocked,
which is called immediately after.
g_dbus_connection_close_sync held the lock to check the closed flag,
which is no longer needed.
As far as I can tell, the only reason why the lock is still desirable
when setting the closed flag is so that remove_match_rule can't fail
by racing with close notification from the worker thread - but
on_worker_closed needs to hold the lock anyway, to deal with other
data structures, so there's no point in trying to eliminate the
requirement to hold the lock.
Bug: https://bugzilla.gnome.org/show_bug.cgi?id=661992
Signed-off-by: Simon McVittie <simon.mcvittie@collabora.co.uk>
Reviewed-by: David Zeuthen <davidz@redhat.com>
2011-10-21 16:46:00 +02:00
|
|
|
|
|
|
|
|
|
flags = g_atomic_int_get (&connection->atomic_flags);
|
|
|
|
|
|
|
|
|
|
return (flags & FLAG_CLOSED) ? TRUE : FALSE;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_dbus_connection_get_capabilities:
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @connection: a #GDBusConnection
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
|
|
|
|
* Gets the capabilities negotiated with the remote peer
|
|
|
|
|
*
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* Returns: zero or more flags from the #GDBusCapabilityFlags enumeration
|
2010-05-06 22:02:08 +02:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.26
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*/
|
|
|
|
|
GDBusCapabilityFlags
|
|
|
|
|
g_dbus_connection_get_capabilities (GDBusConnection *connection)
|
|
|
|
|
{
|
|
|
|
|
g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), G_DBUS_CAPABILITY_FLAGS_NONE);
|
2011-10-20 14:12:26 +02:00
|
|
|
|
|
|
|
|
|
/* do not use g_return_val_if_fail(), we want the memory barrier */
|
|
|
|
|
if (!check_initialized (connection))
|
|
|
|
|
return G_DBUS_CAPABILITY_FLAGS_NONE;
|
|
|
|
|
|
2010-07-07 22:35:17 +02:00
|
|
|
|
return connection->capabilities;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-20 01:33:50 +01:00
|
|
|
|
/**
|
|
|
|
|
* g_dbus_connection_get_flags:
|
|
|
|
|
* @connection: a #GDBusConnection
|
|
|
|
|
*
|
|
|
|
|
* Gets the flags used to construct this connection
|
|
|
|
|
*
|
|
|
|
|
* Returns: zero or more flags from the #GDBusConnectionFlags enumeration
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.60
|
|
|
|
|
*/
|
|
|
|
|
GDBusConnectionFlags
|
|
|
|
|
g_dbus_connection_get_flags (GDBusConnection *connection)
|
|
|
|
|
{
|
|
|
|
|
g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), G_DBUS_CONNECTION_FLAGS_NONE);
|
|
|
|
|
|
|
|
|
|
/* do not use g_return_val_if_fail(), we want the memory barrier */
|
|
|
|
|
if (!check_initialized (connection))
|
|
|
|
|
return G_DBUS_CONNECTION_FLAGS_NONE;
|
|
|
|
|
|
|
|
|
|
return connection->flags;
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-07 21:00:23 +02:00
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
2011-10-17 14:27:03 +02:00
|
|
|
|
/* Called in a temporary thread without holding locks. */
|
2010-07-07 21:00:23 +02:00
|
|
|
|
static void
|
2012-08-02 21:46:32 +02:00
|
|
|
|
flush_in_thread_func (GTask *task,
|
|
|
|
|
gpointer source_object,
|
|
|
|
|
gpointer task_data,
|
|
|
|
|
GCancellable *cancellable)
|
2010-07-07 21:00:23 +02:00
|
|
|
|
{
|
2012-08-02 21:46:32 +02:00
|
|
|
|
GError *error = NULL;
|
2010-07-07 21:00:23 +02:00
|
|
|
|
|
2012-08-02 21:46:32 +02:00
|
|
|
|
if (g_dbus_connection_flush_sync (source_object,
|
|
|
|
|
cancellable,
|
|
|
|
|
&error))
|
|
|
|
|
g_task_return_boolean (task, TRUE);
|
|
|
|
|
else
|
|
|
|
|
g_task_return_error (task, error);
|
2010-07-07 21:00:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_dbus_connection_flush:
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @connection: a #GDBusConnection
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @cancellable: (nullable): a #GCancellable or %NULL
|
|
|
|
|
* @callback: (nullable): a #GAsyncReadyCallback to call when the
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* request is satisfied or %NULL if you don't care about the result
|
|
|
|
|
* @user_data: The data to pass to @callback
|
2010-07-07 21:00:23 +02:00
|
|
|
|
*
|
|
|
|
|
* 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
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* 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.
|
2010-07-07 21:00:23 +02:00
|
|
|
|
*
|
|
|
|
|
* This is an asynchronous method. When the operation is finished,
|
2014-02-08 18:26:56 +01:00
|
|
|
|
* @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
|
2010-07-07 21:00:23 +02:00
|
|
|
|
* then call g_dbus_connection_flush_finish() to get the result of the
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* operation. See g_dbus_connection_flush_sync() for the synchronous
|
2010-07-07 21:00:23 +02:00
|
|
|
|
* version.
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.26
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
g_dbus_connection_flush (GDBusConnection *connection,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
2012-08-02 21:46:32 +02:00
|
|
|
|
GTask *task;
|
2010-07-07 21:00:23 +02:00
|
|
|
|
|
|
|
|
|
g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
|
|
|
|
|
|
2012-08-02 21:46:32 +02:00
|
|
|
|
task = g_task_new (connection, cancellable, callback, user_data);
|
2016-06-17 01:39:38 +02:00
|
|
|
|
g_task_set_source_tag (task, g_dbus_connection_flush);
|
2012-08-02 21:46:32 +02:00
|
|
|
|
g_task_run_in_thread (task, flush_in_thread_func);
|
|
|
|
|
g_object_unref (task);
|
2010-07-07 21:00:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_dbus_connection_flush_finish:
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @connection: a #GDBusConnection
|
|
|
|
|
* @res: a #GAsyncResult obtained from the #GAsyncReadyCallback passed
|
|
|
|
|
* to g_dbus_connection_flush()
|
|
|
|
|
* @error: return location for error or %NULL
|
2010-07-07 21:00:23 +02:00
|
|
|
|
*
|
|
|
|
|
* Finishes an operation started with g_dbus_connection_flush().
|
|
|
|
|
*
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* Returns: %TRUE if the operation succeeded, %FALSE if @error is set
|
2010-07-07 21:00:23 +02:00
|
|
|
|
*
|
|
|
|
|
* 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);
|
2012-08-02 21:46:32 +02:00
|
|
|
|
g_return_val_if_fail (g_task_is_valid (res, connection), FALSE);
|
2010-07-07 21:00:23 +02:00
|
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
|
|
|
|
|
|
2012-08-02 21:46:32 +02:00
|
|
|
|
return g_task_propagate_boolean (G_TASK (res), error);
|
2010-07-07 21:00:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_dbus_connection_flush_sync:
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @connection: a #GDBusConnection
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @cancellable: (nullable): a #GCancellable or %NULL
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @error: return location for error or %NULL
|
2010-07-07 21:00:23 +02:00
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* Returns: %TRUE if the operation succeeded, %FALSE if @error is set
|
2010-07-07 21:00:23 +02:00
|
|
|
|
*
|
|
|
|
|
* 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);
|
2010-07-14 18:37:32 +02:00
|
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
|
2010-07-07 21:00:23 +02:00
|
|
|
|
|
|
|
|
|
ret = FALSE;
|
|
|
|
|
|
GDBusConnection: make the closed flag atomic (but still lock to write)
Strictly speaking, neither of the two uses that aren't under the lock
*needs* to be atomic, but it seems better to be obviously correct (and
we save another 4 bytes of struct).
One of these uses is in g_dbus_connection_is_closed(), any use of which
is inherently a race condition anyway.
The other is g_dbus_connection_flush_sync, which as far as I can tell
just needs a best-effort check, to not waste effort on a connection that
has been closed for a while (but I could be wrong).
I removed the check for the closed flag altogether in
g_dbus_connection_send_message_with_reply_unlocked, because it turns out
to be redundant with one in g_dbus_connection_send_message_unlocked,
which is called immediately after.
g_dbus_connection_close_sync held the lock to check the closed flag,
which is no longer needed.
As far as I can tell, the only reason why the lock is still desirable
when setting the closed flag is so that remove_match_rule can't fail
by racing with close notification from the worker thread - but
on_worker_closed needs to hold the lock anyway, to deal with other
data structures, so there's no point in trying to eliminate the
requirement to hold the lock.
Bug: https://bugzilla.gnome.org/show_bug.cgi?id=661992
Signed-off-by: Simon McVittie <simon.mcvittie@collabora.co.uk>
Reviewed-by: David Zeuthen <davidz@redhat.com>
2011-10-21 16:46:00 +02:00
|
|
|
|
/* 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))
|
2011-10-20 14:12:26 +02:00
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
|
|
g_assert (connection->worker != NULL);
|
|
|
|
|
|
2010-07-07 22:35:17 +02:00
|
|
|
|
ret = _g_dbus_worker_flush_sync (connection->worker,
|
2010-07-07 21:00:23 +02:00
|
|
|
|
cancellable,
|
|
|
|
|
error);
|
|
|
|
|
|
|
|
|
|
out:
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-17 14:27:03 +02:00
|
|
|
|
/* Called in a user thread that has acquired the main context that was
|
|
|
|
|
* thread-default when the object was constructed
|
|
|
|
|
*/
|
2010-05-06 20:13:59 +02:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
GDBusConnection: make the closed flag atomic (but still lock to write)
Strictly speaking, neither of the two uses that aren't under the lock
*needs* to be atomic, but it seems better to be obviously correct (and
we save another 4 bytes of struct).
One of these uses is in g_dbus_connection_is_closed(), any use of which
is inherently a race condition anyway.
The other is g_dbus_connection_flush_sync, which as far as I can tell
just needs a best-effort check, to not waste effort on a connection that
has been closed for a while (but I could be wrong).
I removed the check for the closed flag altogether in
g_dbus_connection_send_message_with_reply_unlocked, because it turns out
to be redundant with one in g_dbus_connection_send_message_unlocked,
which is called immediately after.
g_dbus_connection_close_sync held the lock to check the closed flag,
which is no longer needed.
As far as I can tell, the only reason why the lock is still desirable
when setting the closed flag is so that remove_match_rule can't fail
by racing with close notification from the worker thread - but
on_worker_closed needs to hold the lock anyway, to deal with other
data structures, so there's no point in trying to eliminate the
requirement to hold the lock.
Bug: https://bugzilla.gnome.org/show_bug.cgi?id=661992
Signed-off-by: Simon McVittie <simon.mcvittie@collabora.co.uk>
Reviewed-by: David Zeuthen <davidz@redhat.com>
2011-10-21 16:46:00 +02:00
|
|
|
|
/* Can be called from any thread, must hold lock.
|
|
|
|
|
* FLAG_CLOSED must already have been set.
|
|
|
|
|
*/
|
2010-05-06 20:13:59 +02:00
|
|
|
|
static void
|
GDBusConnection: make the closed flag atomic (but still lock to write)
Strictly speaking, neither of the two uses that aren't under the lock
*needs* to be atomic, but it seems better to be obviously correct (and
we save another 4 bytes of struct).
One of these uses is in g_dbus_connection_is_closed(), any use of which
is inherently a race condition anyway.
The other is g_dbus_connection_flush_sync, which as far as I can tell
just needs a best-effort check, to not waste effort on a connection that
has been closed for a while (but I could be wrong).
I removed the check for the closed flag altogether in
g_dbus_connection_send_message_with_reply_unlocked, because it turns out
to be redundant with one in g_dbus_connection_send_message_unlocked,
which is called immediately after.
g_dbus_connection_close_sync held the lock to check the closed flag,
which is no longer needed.
As far as I can tell, the only reason why the lock is still desirable
when setting the closed flag is so that remove_match_rule can't fail
by racing with close notification from the worker thread - but
on_worker_closed needs to hold the lock anyway, to deal with other
data structures, so there's no point in trying to eliminate the
requirement to hold the lock.
Bug: https://bugzilla.gnome.org/show_bug.cgi?id=661992
Signed-off-by: Simon McVittie <simon.mcvittie@collabora.co.uk>
Reviewed-by: David Zeuthen <davidz@redhat.com>
2011-10-21 16:46:00 +02:00
|
|
|
|
schedule_closed_unlocked (GDBusConnection *connection,
|
|
|
|
|
gboolean remote_peer_vanished,
|
|
|
|
|
GError *error)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
|
|
|
|
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);
|
2021-07-26 11:53:02 +02:00
|
|
|
|
g_source_set_static_name (idle_source, "[gio] emit_closed_in_idle");
|
2010-07-07 22:35:17 +02:00
|
|
|
|
g_source_attach (idle_source, connection->main_context_at_construction);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_source_unref (idle_source);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_dbus_connection_close:
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @connection: a #GDBusConnection
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @cancellable: (nullable): a #GCancellable or %NULL
|
|
|
|
|
* @callback: (nullable): a #GAsyncReadyCallback to call when the request is
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* satisfied or %NULL if you don't care about the result
|
|
|
|
|
* @user_data: The data to pass to @callback
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
|
|
|
|
* Closes @connection. Note that this never causes the process to
|
|
|
|
|
* exit (this might only happen if the other end of a shared message
|
2010-07-07 21:00:23 +02:00
|
|
|
|
* bus connection disconnects, see #GDBusConnection:exit-on-close).
|
|
|
|
|
*
|
2010-07-14 18:37:32 +02:00
|
|
|
|
* 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
|
2010-07-07 21:00:23 +02:00
|
|
|
|
* %G_IO_ERROR_CLOSED.
|
|
|
|
|
*
|
2010-07-14 18:37:32 +02:00
|
|
|
|
* When @connection has been closed, the #GDBusConnection::closed
|
2014-02-08 18:26:56 +01:00
|
|
|
|
* signal is emitted in the
|
|
|
|
|
* [thread-default main context][g-main-context-push-thread-default]
|
|
|
|
|
* of the thread that @connection was constructed in.
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
2010-07-14 18:37:32 +02:00
|
|
|
|
* This is an asynchronous method. When the operation is finished,
|
2014-02-08 18:26:56 +01:00
|
|
|
|
* @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
|
2010-07-14 18:37:32 +02:00
|
|
|
|
* then call g_dbus_connection_close_finish() to get the result of the
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* operation. See g_dbus_connection_close_sync() for the synchronous
|
2010-07-14 18:37:32 +02:00
|
|
|
|
* version.
|
2010-05-06 22:02:08 +02:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.26
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*/
|
|
|
|
|
void
|
2010-07-14 18:37:32 +02:00
|
|
|
|
g_dbus_connection_close (GDBusConnection *connection,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
|
gpointer user_data)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
2012-08-02 21:46:32 +02:00
|
|
|
|
GTask *task;
|
2010-07-14 18:37:32 +02:00
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
|
|
|
|
|
|
2011-10-20 14:12:26 +02:00
|
|
|
|
/* do not use g_return_val_if_fail(), we want the memory barrier */
|
|
|
|
|
if (!check_initialized (connection))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
g_assert (connection->worker != NULL);
|
|
|
|
|
|
2012-08-02 21:46:32 +02:00
|
|
|
|
task = g_task_new (connection, cancellable, callback, user_data);
|
2016-06-17 01:39:38 +02:00
|
|
|
|
g_task_set_source_tag (task, g_dbus_connection_close);
|
2012-08-02 21:46:32 +02:00
|
|
|
|
_g_dbus_worker_close (connection->worker, task);
|
|
|
|
|
g_object_unref (task);
|
2010-07-14 18:37:32 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_dbus_connection_close_finish:
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @connection: a #GDBusConnection
|
|
|
|
|
* @res: a #GAsyncResult obtained from the #GAsyncReadyCallback passed
|
|
|
|
|
* to g_dbus_connection_close()
|
|
|
|
|
* @error: return location for error or %NULL
|
2010-07-14 18:37:32 +02:00
|
|
|
|
*
|
|
|
|
|
* Finishes an operation started with g_dbus_connection_close().
|
|
|
|
|
*
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* Returns: %TRUE if the operation succeeded, %FALSE if @error is set
|
2010-07-14 18:37:32 +02:00
|
|
|
|
*
|
|
|
|
|
* 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);
|
2012-08-02 21:46:32 +02:00
|
|
|
|
g_return_val_if_fail (g_task_is_valid (res, connection), FALSE);
|
2010-07-14 18:37:32 +02:00
|
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
|
|
|
|
|
|
2012-08-02 21:46:32 +02:00
|
|
|
|
return g_task_propagate_boolean (G_TASK (res), error);
|
2010-07-14 18:37:32 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-09-13 18:31:27 +02:00
|
|
|
|
typedef struct {
|
|
|
|
|
GMainLoop *loop;
|
|
|
|
|
GAsyncResult *result;
|
|
|
|
|
} SyncCloseData;
|
|
|
|
|
|
2011-10-17 14:27:03 +02:00
|
|
|
|
/* Can be called by any thread, without the connection lock */
|
2011-09-13 18:31:27 +02:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-14 18:37:32 +02:00
|
|
|
|
/**
|
|
|
|
|
* g_dbus_connection_close_sync:
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @connection: a #GDBusConnection
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @cancellable: (nullable): a #GCancellable or %NULL
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @error: return location for error or %NULL
|
2010-07-14 18:37:32 +02:00
|
|
|
|
*
|
2018-04-19 17:36:16 +02:00
|
|
|
|
* Synchronously closes @connection. The calling thread is blocked
|
2010-07-14 18:37:32 +02:00
|
|
|
|
* until this is done. See g_dbus_connection_close() for the
|
|
|
|
|
* asynchronous version of this method and more details about what it
|
|
|
|
|
* does.
|
|
|
|
|
*
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* Returns: %TRUE if the operation succeeded, %FALSE if @error is set
|
2010-07-14 18:37:32 +02:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.26
|
|
|
|
|
*/
|
|
|
|
|
gboolean
|
2014-02-01 05:42:40 +01:00
|
|
|
|
g_dbus_connection_close_sync (GDBusConnection *connection,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GError **error)
|
2010-07-14 18:37:32 +02:00
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
|
GDBusConnection: make the closed flag atomic (but still lock to write)
Strictly speaking, neither of the two uses that aren't under the lock
*needs* to be atomic, but it seems better to be obviously correct (and
we save another 4 bytes of struct).
One of these uses is in g_dbus_connection_is_closed(), any use of which
is inherently a race condition anyway.
The other is g_dbus_connection_flush_sync, which as far as I can tell
just needs a best-effort check, to not waste effort on a connection that
has been closed for a while (but I could be wrong).
I removed the check for the closed flag altogether in
g_dbus_connection_send_message_with_reply_unlocked, because it turns out
to be redundant with one in g_dbus_connection_send_message_unlocked,
which is called immediately after.
g_dbus_connection_close_sync held the lock to check the closed flag,
which is no longer needed.
As far as I can tell, the only reason why the lock is still desirable
when setting the closed flag is so that remove_match_rule can't fail
by racing with close notification from the worker thread - but
on_worker_closed needs to hold the lock anyway, to deal with other
data structures, so there's no point in trying to eliminate the
requirement to hold the lock.
Bug: https://bugzilla.gnome.org/show_bug.cgi?id=661992
Signed-off-by: Simon McVittie <simon.mcvittie@collabora.co.uk>
Reviewed-by: David Zeuthen <davidz@redhat.com>
2011-10-21 16:46:00 +02:00
|
|
|
|
if (check_unclosed (connection, 0, error))
|
2010-05-10 04:13:18 +02:00
|
|
|
|
{
|
2011-09-13 18:31:27 +02:00
|
|
|
|
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);
|
2010-07-14 18:37:32 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
2012-06-06 19:44:39 +02:00
|
|
|
|
/**
|
|
|
|
|
* g_dbus_connection_get_last_serial:
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @connection: a #GDBusConnection
|
2012-06-06 19:44:39 +02:00
|
|
|
|
*
|
|
|
|
|
* 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
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* within the current thread
|
2012-06-06 19:44:39 +02:00
|
|
|
|
*
|
|
|
|
|
* 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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
2011-10-17 14:27:03 +02:00
|
|
|
|
/* Can be called by any thread, with the connection lock held */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
static gboolean
|
2010-05-10 04:13:18 +02:00
|
|
|
|
g_dbus_connection_send_message_unlocked (GDBusConnection *connection,
|
|
|
|
|
GDBusMessage *message,
|
2010-07-19 22:07:57 +02:00
|
|
|
|
GDBusSendMessageFlags flags,
|
2020-11-11 19:24:28 +01:00
|
|
|
|
guint32 *out_serial,
|
2010-05-10 04:13:18 +02:00
|
|
|
|
GError **error)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
|
|
|
|
guchar *blob;
|
|
|
|
|
gsize blob_size;
|
|
|
|
|
guint32 serial_to_use;
|
|
|
|
|
|
|
|
|
|
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 */
|
|
|
|
|
|
|
|
|
|
if (out_serial != NULL)
|
|
|
|
|
*out_serial = 0;
|
|
|
|
|
|
2011-10-20 14:12:26 +02:00
|
|
|
|
/* 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).
|
|
|
|
|
*/
|
GDBusConnection: make the closed flag atomic (but still lock to write)
Strictly speaking, neither of the two uses that aren't under the lock
*needs* to be atomic, but it seems better to be obviously correct (and
we save another 4 bytes of struct).
One of these uses is in g_dbus_connection_is_closed(), any use of which
is inherently a race condition anyway.
The other is g_dbus_connection_flush_sync, which as far as I can tell
just needs a best-effort check, to not waste effort on a connection that
has been closed for a while (but I could be wrong).
I removed the check for the closed flag altogether in
g_dbus_connection_send_message_with_reply_unlocked, because it turns out
to be redundant with one in g_dbus_connection_send_message_unlocked,
which is called immediately after.
g_dbus_connection_close_sync held the lock to check the closed flag,
which is no longer needed.
As far as I can tell, the only reason why the lock is still desirable
when setting the closed flag is so that remove_match_rule can't fail
by racing with close notification from the worker thread - but
on_worker_closed needs to hold the lock anyway, to deal with other
data structures, so there's no point in trying to eliminate the
requirement to hold the lock.
Bug: https://bugzilla.gnome.org/show_bug.cgi?id=661992
Signed-off-by: Simon McVittie <simon.mcvittie@collabora.co.uk>
Reviewed-by: David Zeuthen <davidz@redhat.com>
2011-10-21 16:46:00 +02:00
|
|
|
|
if (!check_unclosed (connection,
|
|
|
|
|
(flags & SEND_MESSAGE_FLAGS_INITIALIZING) ? MAY_BE_UNINITIALIZED : 0,
|
|
|
|
|
error))
|
2021-03-28 09:08:27 +02:00
|
|
|
|
return FALSE;
|
2011-10-20 14:12:26 +02:00
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
blob = g_dbus_message_to_blob (message,
|
|
|
|
|
&blob_size,
|
2010-07-07 22:35:17 +02:00
|
|
|
|
connection->capabilities,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
error);
|
|
|
|
|
if (blob == NULL)
|
2021-03-28 09:08:27 +02:00
|
|
|
|
return FALSE;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2010-07-19 23:03:07 +02:00
|
|
|
|
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 */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
2010-07-07 22:35:17 +02:00
|
|
|
|
/* TODO: use connection->auth to encode the blob */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
if (out_serial != NULL)
|
2010-05-10 04:13:18 +02:00
|
|
|
|
*out_serial = serial_to_use;
|
|
|
|
|
|
2012-06-06 19:44:39 +02:00
|
|
|
|
/* 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));
|
|
|
|
|
|
2010-12-13 19:09:38 +01:00
|
|
|
|
if (!(flags & G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL))
|
|
|
|
|
g_dbus_message_set_serial (message, serial_to_use);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2010-09-09 19:21:35 +02:00
|
|
|
|
g_dbus_message_lock (message);
|
2021-03-28 09:08:27 +02:00
|
|
|
|
|
2010-07-07 22:35:17 +02:00
|
|
|
|
_g_dbus_worker_send_message (connection->worker,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
message,
|
2021-03-28 09:08:27 +02:00
|
|
|
|
(gchar*) blob, /* transfer ownership */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
blob_size);
|
|
|
|
|
|
2021-03-28 09:08:27 +02:00
|
|
|
|
return TRUE;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_dbus_connection_send_message:
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @connection: a #GDBusConnection
|
|
|
|
|
* @message: a #GDBusMessage
|
|
|
|
|
* @flags: flags affecting how the message is sent
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @out_serial: (out) (optional): return location for serial number assigned
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* to @message when sending it or %NULL
|
|
|
|
|
* @error: Return location for error or %NULL
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
|
|
|
|
* Asynchronously sends @message to the peer represented by @connection.
|
|
|
|
|
*
|
2010-07-19 23:03:07 +02:00
|
|
|
|
* 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
|
2020-11-17 11:15:15 +01:00
|
|
|
|
* submitting the message to the underlying transport. While it has a `volatile`
|
|
|
|
|
* qualifier, this is a historical artifact and the argument passed to it should
|
|
|
|
|
* not be `volatile`.
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
|
|
|
|
* If @connection is closed then the operation will fail with
|
2010-07-07 21:00:23 +02:00
|
|
|
|
* %G_IO_ERROR_CLOSED. If @message is not well-formed,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
* the operation fails with %G_IO_ERROR_INVALID_ARGUMENT.
|
|
|
|
|
*
|
2014-02-08 18:26:56 +01:00
|
|
|
|
* 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.
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
2010-09-09 19:21:35 +02:00
|
|
|
|
* Note that @message must be unlocked, unless @flags contain the
|
|
|
|
|
* %G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL flag.
|
|
|
|
|
*
|
2010-05-06 20:13:59 +02:00
|
|
|
|
* Returns: %TRUE if the message was well-formed and queued for
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* transmission, %FALSE if @error is set
|
2010-05-06 22:02:08 +02:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.26
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*/
|
|
|
|
|
gboolean
|
2014-02-01 05:42:40 +01:00
|
|
|
|
g_dbus_connection_send_message (GDBusConnection *connection,
|
|
|
|
|
GDBusMessage *message,
|
|
|
|
|
GDBusSendMessageFlags flags,
|
|
|
|
|
volatile guint32 *out_serial,
|
|
|
|
|
GError **error)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
|
|
|
|
gboolean ret;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
|
|
|
|
|
g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), FALSE);
|
2010-09-09 19:21:35 +02:00
|
|
|
|
g_return_val_if_fail ((flags & G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL) || !g_dbus_message_get_locked (message), FALSE);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
|
|
|
|
|
|
|
|
|
|
CONNECTION_LOCK (connection);
|
2020-11-11 19:24:28 +01:00
|
|
|
|
ret = g_dbus_connection_send_message_unlocked (connection, message, flags, (guint32 *) out_serial, error);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
CONNECTION_UNLOCK (connection);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
|
{
|
|
|
|
|
guint32 serial;
|
|
|
|
|
|
|
|
|
|
gulong cancellable_handler_id;
|
2023-02-22 16:11:56 +01:00
|
|
|
|
GSource *cancelled_idle_source; /* (owned) (nullable) */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2023-02-22 15:55:40 +01:00
|
|
|
|
GSource *timeout_source; /* (owned) (nullable) */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
gboolean delivered;
|
|
|
|
|
} SendMessageData;
|
|
|
|
|
|
2011-10-17 14:27:03 +02:00
|
|
|
|
/* Can be called from any thread with or without lock held */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
static void
|
2012-08-02 21:46:32 +02:00
|
|
|
|
send_message_data_free (SendMessageData *data)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
2023-02-22 15:55:40 +01:00
|
|
|
|
/* These should already have been cleared by send_message_with_reply_cleanup(). */
|
2012-08-02 21:46:32 +02:00
|
|
|
|
g_assert (data->timeout_source == NULL);
|
|
|
|
|
g_assert (data->cancellable_handler_id == 0);
|
|
|
|
|
|
|
|
|
|
g_slice_free (SendMessageData, data);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
2023-02-22 13:19:16 +01:00
|
|
|
|
/* can be called from any thread with lock held; @task is (transfer none) */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
static void
|
2012-08-02 21:46:32 +02:00
|
|
|
|
send_message_with_reply_cleanup (GTask *task, gboolean remove)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
2012-08-02 21:46:32 +02:00
|
|
|
|
GDBusConnection *connection = g_task_get_source_object (task);
|
|
|
|
|
SendMessageData *data = g_task_get_task_data (task);
|
|
|
|
|
|
|
|
|
|
CONNECTION_ENSURE_LOCK (connection);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
g_assert (!data->delivered);
|
|
|
|
|
|
|
|
|
|
data->delivered = TRUE;
|
|
|
|
|
|
|
|
|
|
if (data->timeout_source != NULL)
|
|
|
|
|
{
|
|
|
|
|
g_source_destroy (data->timeout_source);
|
2023-02-22 15:55:40 +01:00
|
|
|
|
g_clear_pointer (&data->timeout_source, g_source_unref);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
if (data->cancellable_handler_id > 0)
|
|
|
|
|
{
|
2012-08-02 21:46:32 +02:00
|
|
|
|
g_cancellable_disconnect (g_task_get_cancellable (task), data->cancellable_handler_id);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
data->cancellable_handler_id = 0;
|
|
|
|
|
}
|
2023-02-22 16:11:56 +01:00
|
|
|
|
if (data->cancelled_idle_source != NULL)
|
|
|
|
|
{
|
|
|
|
|
g_source_destroy (data->cancelled_idle_source);
|
|
|
|
|
g_clear_pointer (&data->cancelled_idle_source, g_source_unref);
|
|
|
|
|
}
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2011-10-07 20:20:03 +02:00
|
|
|
|
if (remove)
|
|
|
|
|
{
|
2012-08-02 21:46:32 +02:00
|
|
|
|
gboolean removed = g_hash_table_remove (connection->map_method_serial_to_task,
|
|
|
|
|
GUINT_TO_POINTER (data->serial));
|
|
|
|
|
g_warn_if_fail (removed);
|
2011-10-07 20:20:03 +02:00
|
|
|
|
}
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
2023-02-22 13:19:16 +01:00
|
|
|
|
/* Called from GDBus worker thread with lock held; @task is (transfer none). */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
static void
|
2012-08-02 21:46:32 +02:00
|
|
|
|
send_message_data_deliver_reply_unlocked (GTask *task,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
GDBusMessage *reply)
|
|
|
|
|
{
|
2012-08-02 21:46:32 +02:00
|
|
|
|
SendMessageData *data = g_task_get_task_data (task);
|
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
if (data->delivered)
|
|
|
|
|
goto out;
|
|
|
|
|
|
2012-08-02 21:46:32 +02:00
|
|
|
|
g_task_return_pointer (task, g_object_ref (reply), g_object_unref);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2012-08-02 21:46:32 +02:00
|
|
|
|
send_message_with_reply_cleanup (task, TRUE);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
out:
|
|
|
|
|
;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-22 13:19:16 +01:00
|
|
|
|
/* Called from a user thread, lock is not held; @task is (transfer none) */
|
2015-04-06 16:09:04 +02:00
|
|
|
|
static void
|
|
|
|
|
send_message_data_deliver_error (GTask *task,
|
|
|
|
|
GQuark domain,
|
|
|
|
|
gint code,
|
|
|
|
|
const char *message)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
2012-08-02 21:46:32 +02:00
|
|
|
|
GDBusConnection *connection = g_task_get_source_object (task);
|
|
|
|
|
SendMessageData *data = g_task_get_task_data (task);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2012-08-02 21:46:32 +02:00
|
|
|
|
CONNECTION_LOCK (connection);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
if (data->delivered)
|
2015-04-06 16:09:04 +02:00
|
|
|
|
{
|
|
|
|
|
CONNECTION_UNLOCK (connection);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2023-02-22 13:19:16 +01:00
|
|
|
|
/* Hold a ref on @task as send_message_with_reply_cleanup() will remove it
|
|
|
|
|
* from the task map and could end up dropping the last reference */
|
2015-04-06 16:09:04 +02:00
|
|
|
|
g_object_ref (task);
|
2023-02-22 13:19:16 +01:00
|
|
|
|
|
2012-08-02 21:46:32 +02:00
|
|
|
|
send_message_with_reply_cleanup (task, TRUE);
|
|
|
|
|
CONNECTION_UNLOCK (connection);
|
2015-04-06 16:09:04 +02:00
|
|
|
|
|
2023-12-19 21:03:28 +01:00
|
|
|
|
g_task_return_new_error_literal (task, domain, code, message);
|
2015-04-06 16:09:04 +02:00
|
|
|
|
g_object_unref (task);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
2023-02-22 03:40:35 +01:00
|
|
|
|
/* Called from a user thread, lock is not held; @task is (transfer none) */
|
2015-04-06 16:09:04 +02:00
|
|
|
|
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"));
|
2023-02-22 16:11:56 +01:00
|
|
|
|
return G_SOURCE_REMOVE;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Can be called from any thread with or without lock held */
|
|
|
|
|
static void
|
|
|
|
|
send_message_with_reply_cancelled_cb (GCancellable *cancellable,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
2012-08-02 21:46:32 +02:00
|
|
|
|
GTask *task = user_data;
|
2023-02-22 16:11:56 +01:00
|
|
|
|
SendMessageData *data = g_task_get_task_data (task);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
/* postpone cancellation to idle handler since we may be called directly
|
|
|
|
|
* via g_cancellable_connect() (e.g. holding lock)
|
|
|
|
|
*/
|
2023-02-22 16:11:56 +01:00
|
|
|
|
if (data->cancelled_idle_source != NULL)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
data->cancelled_idle_source = g_idle_source_new ();
|
|
|
|
|
g_source_set_static_name (data->cancelled_idle_source, "[gio] send_message_with_reply_cancelled_idle_cb");
|
|
|
|
|
g_task_attach_source (task, data->cancelled_idle_source, send_message_with_reply_cancelled_idle_cb);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
2023-02-22 03:40:35 +01:00
|
|
|
|
/* Called from a user thread, lock is not held; @task is (transfer none) */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
static gboolean
|
|
|
|
|
send_message_with_reply_timeout_cb (gpointer user_data)
|
|
|
|
|
{
|
2012-08-02 21:46:32 +02:00
|
|
|
|
GTask *task = user_data;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2015-04-06 16:09:04 +02:00
|
|
|
|
send_message_data_deliver_error (task, G_IO_ERROR, G_IO_ERROR_TIMED_OUT,
|
|
|
|
|
_("Timeout was reached"));
|
2023-02-22 15:55:40 +01:00
|
|
|
|
return G_SOURCE_REMOVE;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
2011-10-17 14:27:03 +02:00
|
|
|
|
/* Called from a user thread, connection's lock is held */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
static void
|
|
|
|
|
g_dbus_connection_send_message_with_reply_unlocked (GDBusConnection *connection,
|
|
|
|
|
GDBusMessage *message,
|
2010-07-19 22:07:57 +02:00
|
|
|
|
GDBusSendMessageFlags flags,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
gint timeout_msec,
|
2020-11-11 19:24:28 +01:00
|
|
|
|
guint32 *out_serial,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
2012-08-02 21:46:32 +02:00
|
|
|
|
GTask *task;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
SendMessageData *data;
|
2012-08-02 21:46:32 +02:00
|
|
|
|
GError *error = NULL;
|
2020-11-11 19:24:28 +01:00
|
|
|
|
guint32 serial;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
if (out_serial == NULL)
|
|
|
|
|
out_serial = &serial;
|
2024-04-09 17:11:09 +02:00
|
|
|
|
*out_serial = 0;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
if (timeout_msec == -1)
|
2010-05-14 04:15:47 +02:00
|
|
|
|
timeout_msec = 25 * 1000;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2012-08-02 21:46:32 +02:00
|
|
|
|
data = g_slice_new0 (SendMessageData);
|
|
|
|
|
task = g_task_new (connection, cancellable, callback, user_data);
|
2016-06-17 01:39:38 +02:00
|
|
|
|
g_task_set_source_tag (task,
|
|
|
|
|
g_dbus_connection_send_message_with_reply_unlocked);
|
2012-08-02 21:46:32 +02:00
|
|
|
|
g_task_set_task_data (task, data, (GDestroyNotify) send_message_data_free);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2012-08-02 21:46:32 +02:00
|
|
|
|
if (g_task_return_error_if_cancelled (task))
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
2012-08-02 21:46:32 +02:00
|
|
|
|
g_object_unref (task);
|
|
|
|
|
return;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
2010-07-19 22:07:57 +02:00
|
|
|
|
if (!g_dbus_connection_send_message_unlocked (connection, message, flags, out_serial, &error))
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
2012-08-02 21:46:32 +02:00
|
|
|
|
g_task_return_error (task, error);
|
|
|
|
|
g_object_unref (task);
|
|
|
|
|
return;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
data->serial = *out_serial;
|
|
|
|
|
|
|
|
|
|
if (cancellable != NULL)
|
|
|
|
|
{
|
|
|
|
|
data->cancellable_handler_id = g_cancellable_connect (cancellable,
|
|
|
|
|
G_CALLBACK (send_message_with_reply_cancelled_cb),
|
2012-08-02 21:46:32 +02:00
|
|
|
|
g_object_ref (task),
|
|
|
|
|
g_object_unref);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
2010-10-28 22:14:24 +02:00
|
|
|
|
if (timeout_msec != G_MAXINT)
|
|
|
|
|
{
|
|
|
|
|
data->timeout_source = g_timeout_source_new (timeout_msec);
|
2022-08-28 03:33:50 +02:00
|
|
|
|
g_source_set_static_name (data->timeout_source, "[gio] send_message_with_reply_unlocked");
|
2012-08-02 21:46:32 +02:00
|
|
|
|
g_task_attach_source (task, data->timeout_source,
|
|
|
|
|
(GSourceFunc) send_message_with_reply_timeout_cb);
|
2010-10-28 22:14:24 +02:00
|
|
|
|
}
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2012-08-02 21:46:32 +02:00
|
|
|
|
g_hash_table_insert (connection->map_method_serial_to_task,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
GUINT_TO_POINTER (*out_serial),
|
2017-04-28 13:05:42 +02:00
|
|
|
|
g_steal_pointer (&task));
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_dbus_connection_send_message_with_reply:
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @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
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @out_serial: (out) (optional): return location for serial number assigned
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* to @message when sending it or %NULL
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @cancellable: (nullable): a #GCancellable or %NULL
|
|
|
|
|
* @callback: (nullable): a #GAsyncReadyCallback to call when the request
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* is satisfied or %NULL if you don't care about the result
|
|
|
|
|
* @user_data: The data to pass to @callback
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
|
|
|
|
* Asynchronously sends @message to the peer represented by @connection.
|
|
|
|
|
*
|
2010-07-19 23:03:07 +02:00
|
|
|
|
* 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
|
2020-11-17 11:15:15 +01:00
|
|
|
|
* submitting the message to the underlying transport. While it has a `volatile`
|
|
|
|
|
* qualifier, this is a historical artifact and the argument passed to it should
|
|
|
|
|
* not be `volatile`.
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
2014-02-08 18:26:56 +01:00
|
|
|
|
* 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]
|
2010-05-06 20:13:59 +02:00
|
|
|
|
* 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.
|
|
|
|
|
*
|
2010-09-09 19:21:35 +02:00
|
|
|
|
* Note that @message must be unlocked, unless @flags contain the
|
|
|
|
|
* %G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL flag.
|
|
|
|
|
*
|
2014-02-08 18:26:56 +01:00
|
|
|
|
* 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.
|
2010-05-06 22:02:08 +02:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.26
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*/
|
|
|
|
|
void
|
2014-02-01 05:42:40 +01:00
|
|
|
|
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)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
|
|
|
|
g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
|
|
|
|
|
g_return_if_fail (G_IS_DBUS_MESSAGE (message));
|
2010-09-09 19:21:35 +02:00
|
|
|
|
g_return_if_fail ((flags & G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL) || !g_dbus_message_get_locked (message));
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_return_if_fail (timeout_msec >= 0 || timeout_msec == -1);
|
|
|
|
|
|
|
|
|
|
CONNECTION_LOCK (connection);
|
|
|
|
|
g_dbus_connection_send_message_with_reply_unlocked (connection,
|
|
|
|
|
message,
|
2010-07-19 22:07:57 +02:00
|
|
|
|
flags,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
timeout_msec,
|
2020-11-11 19:24:28 +01:00
|
|
|
|
(guint32 *) out_serial,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
cancellable,
|
|
|
|
|
callback,
|
|
|
|
|
user_data);
|
|
|
|
|
CONNECTION_UNLOCK (connection);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_dbus_connection_send_message_with_reply_finish:
|
2010-05-12 02:57:44 +02:00
|
|
|
|
* @connection: a #GDBusConnection
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @res: a #GAsyncResult obtained from the #GAsyncReadyCallback passed to
|
|
|
|
|
* g_dbus_connection_send_message_with_reply()
|
|
|
|
|
* @error: teturn location for error or %NULL
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
|
|
|
|
* Finishes an operation started with g_dbus_connection_send_message_with_reply().
|
|
|
|
|
*
|
|
|
|
|
* Note that @error is only set if a local in-process error
|
2011-08-29 20:49:32 +02:00
|
|
|
|
* occurred. That is to say that the returned #GDBusMessage object may
|
2010-05-06 20:13:59 +02:00
|
|
|
|
* be of type %G_DBUS_MESSAGE_TYPE_ERROR. Use
|
|
|
|
|
* g_dbus_message_to_gerror() to transcode this to a #GError.
|
|
|
|
|
*
|
2014-02-08 18:26:56 +01:00
|
|
|
|
* 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.
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* Returns: (transfer full): a locked #GDBusMessage or %NULL if @error is set
|
2010-05-06 22:02:08 +02:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.26
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*/
|
|
|
|
|
GDBusMessage *
|
2010-05-10 04:13:18 +02:00
|
|
|
|
g_dbus_connection_send_message_with_reply_finish (GDBusConnection *connection,
|
|
|
|
|
GAsyncResult *res,
|
|
|
|
|
GError **error)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
|
|
|
|
g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
|
2012-08-02 21:46:32 +02:00
|
|
|
|
g_return_val_if_fail (g_task_is_valid (res, connection), NULL);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
|
|
|
|
|
|
2012-08-02 21:46:32 +02:00
|
|
|
|
return g_task_propagate_pointer (G_TASK (res), error);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
|
{
|
|
|
|
|
GAsyncResult *res;
|
|
|
|
|
GMainContext *context;
|
|
|
|
|
GMainLoop *loop;
|
|
|
|
|
} SendMessageSyncData;
|
|
|
|
|
|
2011-10-17 14:27:03 +02:00
|
|
|
|
/* Called from a user thread, lock is not held */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
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:
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @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
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @out_serial: (out) (optional): return location for serial number
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* assigned to @message when sending it or %NULL
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @cancellable: (nullable): a #GCancellable or %NULL
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @error: return location for error or %NULL
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
2010-07-19 23:03:07 +02:00
|
|
|
|
* 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
|
2020-11-17 11:15:15 +01:00
|
|
|
|
* submitting the message to the underlying transport. While it has a `volatile`
|
|
|
|
|
* qualifier, this is a historical artifact and the argument passed to it should
|
|
|
|
|
* not be `volatile`.
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
|
|
|
|
* 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
|
2011-08-29 20:49:32 +02:00
|
|
|
|
* occurred. That is to say that the returned #GDBusMessage object may
|
2010-05-06 20:13:59 +02:00
|
|
|
|
* be of type %G_DBUS_MESSAGE_TYPE_ERROR. Use
|
|
|
|
|
* g_dbus_message_to_gerror() to transcode this to a #GError.
|
|
|
|
|
*
|
2014-02-08 18:26:56 +01:00
|
|
|
|
* 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.
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
2010-09-09 19:21:35 +02:00
|
|
|
|
* Note that @message must be unlocked, unless @flags contain the
|
|
|
|
|
* %G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL flag.
|
|
|
|
|
*
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* Returns: (transfer full): a locked #GDBusMessage that is the reply
|
|
|
|
|
* to @message or %NULL if @error is set
|
2010-05-06 22:02:08 +02:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.26
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*/
|
|
|
|
|
GDBusMessage *
|
2014-02-01 05:42:40 +01:00
|
|
|
|
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)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
2012-08-02 21:46:32 +02:00
|
|
|
|
SendMessageSyncData data;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
GDBusMessage *reply;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
|
|
|
|
|
g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
|
2011-04-27 04:51:54 +02:00
|
|
|
|
g_return_val_if_fail ((flags & G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL) || !g_dbus_message_get_locked (message), NULL);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_return_val_if_fail (timeout_msec >= 0 || timeout_msec == -1, NULL);
|
|
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
|
|
|
|
|
|
2012-08-02 21:46:32 +02:00
|
|
|
|
data.res = NULL;
|
|
|
|
|
data.context = g_main_context_new ();
|
|
|
|
|
data.loop = g_main_loop_new (data.context, FALSE);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2012-08-02 21:46:32 +02:00
|
|
|
|
g_main_context_push_thread_default (data.context);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
g_dbus_connection_send_message_with_reply (connection,
|
|
|
|
|
message,
|
2010-07-19 22:07:57 +02:00
|
|
|
|
flags,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
timeout_msec,
|
|
|
|
|
out_serial,
|
|
|
|
|
cancellable,
|
|
|
|
|
(GAsyncReadyCallback) send_message_with_reply_sync_cb,
|
2012-08-02 21:46:32 +02:00
|
|
|
|
&data);
|
|
|
|
|
g_main_loop_run (data.loop);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
reply = g_dbus_connection_send_message_with_reply_finish (connection,
|
2012-08-02 21:46:32 +02:00
|
|
|
|
data.res,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
error);
|
|
|
|
|
|
2012-08-02 21:46:32 +02:00
|
|
|
|
g_main_context_pop_thread_default (data.context);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2012-08-02 21:46:32 +02:00
|
|
|
|
g_main_context_unref (data.context);
|
|
|
|
|
g_main_loop_unref (data.loop);
|
|
|
|
|
if (data.res)
|
|
|
|
|
g_object_unref (data.res);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
return reply;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
2024-04-23 21:42:17 +02:00
|
|
|
|
/*
|
|
|
|
|
* Called in any thread.
|
|
|
|
|
* Must hold the connection lock when calling this, unless
|
|
|
|
|
* connection->finalizing is TRUE.
|
|
|
|
|
*/
|
|
|
|
|
static void
|
|
|
|
|
name_watcher_unref_watched_name (GDBusConnection *connection,
|
|
|
|
|
SignalData *name_watcher)
|
|
|
|
|
{
|
|
|
|
|
WatchedName *watched_name = name_watcher->watched_name;
|
|
|
|
|
|
|
|
|
|
g_assert (watched_name != NULL);
|
|
|
|
|
|
|
|
|
|
if (!g_ref_count_dec (&watched_name->ref_count))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
/* Removing watched_name from the name_watcher may result in
|
|
|
|
|
* name_watcher being freed, so we must make sure name_watcher is no
|
|
|
|
|
* longer in map_method_serial_to_name_watcher.
|
|
|
|
|
*
|
|
|
|
|
* If we stop watching the name while our GetNameOwner call was still
|
|
|
|
|
* in-flight, then when the reply eventually arrives, we will not find
|
|
|
|
|
* its serial number in the map and harmlessly ignore it as a result. */
|
|
|
|
|
if (watched_name->get_name_owner_serial != 0)
|
|
|
|
|
g_hash_table_remove (connection->map_method_serial_to_name_watcher,
|
|
|
|
|
GUINT_TO_POINTER (watched_name->get_name_owner_serial));
|
|
|
|
|
|
|
|
|
|
name_watcher->watched_name = NULL;
|
|
|
|
|
g_free (watched_name->owner);
|
|
|
|
|
g_free (watched_name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* called in GDBusWorker thread with lock held */
|
|
|
|
|
static void
|
|
|
|
|
name_watcher_set_name_owner_unlocked (SignalData *name_watcher,
|
|
|
|
|
const char *new_owner)
|
|
|
|
|
{
|
|
|
|
|
if (new_owner != NULL && new_owner[0] == '\0')
|
|
|
|
|
new_owner = NULL;
|
|
|
|
|
|
|
|
|
|
g_assert (name_watcher->watched_name != NULL);
|
|
|
|
|
g_set_str (&name_watcher->watched_name->owner, new_owner);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* called in GDBusWorker thread with lock held */
|
|
|
|
|
static void
|
|
|
|
|
name_watcher_deliver_name_owner_changed_unlocked (SignalData *name_watcher,
|
|
|
|
|
GDBusMessage *message)
|
|
|
|
|
{
|
|
|
|
|
GVariant *body;
|
|
|
|
|
|
|
|
|
|
body = g_dbus_message_get_body (message);
|
|
|
|
|
|
|
|
|
|
if (G_LIKELY (body != NULL && g_variant_is_of_type (body, G_VARIANT_TYPE ("(sss)"))))
|
|
|
|
|
{
|
|
|
|
|
const char *name;
|
|
|
|
|
const char *new_owner;
|
|
|
|
|
|
|
|
|
|
g_variant_get (body, "(&s&s&s)", &name, NULL, &new_owner);
|
|
|
|
|
|
|
|
|
|
/* Our caller already checked this */
|
|
|
|
|
g_assert (g_strcmp0 (name_watcher->arg0, name) == 0);
|
|
|
|
|
|
2024-05-08 16:46:08 +02:00
|
|
|
|
/* FIXME: This should be validating that `new_owner` is a unique name,
|
|
|
|
|
* but IBus’ implementation of a message bus is not compliant with the spec.
|
|
|
|
|
* See https://gitlab.gnome.org/GNOME/glib/-/issues/3353 */
|
|
|
|
|
if (G_LIKELY (new_owner[0] == '\0' || g_dbus_is_name (new_owner)))
|
2024-04-23 21:42:17 +02:00
|
|
|
|
name_watcher_set_name_owner_unlocked (name_watcher, new_owner);
|
|
|
|
|
else
|
|
|
|
|
g_warning ("Received NameOwnerChanged signal with invalid owner \"%s\" for \"%s\"",
|
|
|
|
|
new_owner, name);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
g_warning ("Received NameOwnerChanged signal with unexpected "
|
|
|
|
|
"signature %s",
|
|
|
|
|
body == NULL ? "()" : g_variant_get_type_string (body));
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* called in GDBusWorker thread with lock held */
|
|
|
|
|
static void
|
|
|
|
|
name_watcher_deliver_get_name_owner_reply_unlocked (SignalData *name_watcher,
|
|
|
|
|
GDBusConnection *connection,
|
|
|
|
|
GDBusMessage *message)
|
|
|
|
|
{
|
|
|
|
|
GDBusMessageType type;
|
|
|
|
|
GVariant *body;
|
|
|
|
|
WatchedName *watched_name;
|
|
|
|
|
|
|
|
|
|
watched_name = name_watcher->watched_name;
|
|
|
|
|
g_assert (watched_name != NULL);
|
|
|
|
|
g_assert (watched_name->get_name_owner_serial != 0);
|
|
|
|
|
|
|
|
|
|
type = g_dbus_message_get_message_type (message);
|
|
|
|
|
body = g_dbus_message_get_body (message);
|
|
|
|
|
|
|
|
|
|
if (type == G_DBUS_MESSAGE_TYPE_ERROR)
|
|
|
|
|
{
|
|
|
|
|
if (g_strcmp0 (g_dbus_message_get_error_name (message),
|
2024-05-13 14:10:15 +02:00
|
|
|
|
DBUS_ERROR_NAME_HAS_NO_OWNER))
|
2024-04-23 21:42:17 +02:00
|
|
|
|
name_watcher_set_name_owner_unlocked (name_watcher, NULL);
|
|
|
|
|
/* else it's something like NoReply or AccessDenied, which tells
|
|
|
|
|
* us nothing - leave the owner set to whatever we most recently
|
|
|
|
|
* learned from NameOwnerChanged, or NULL */
|
|
|
|
|
}
|
|
|
|
|
else if (type != G_DBUS_MESSAGE_TYPE_METHOD_RETURN)
|
|
|
|
|
{
|
|
|
|
|
g_warning ("Received GetNameOwner reply with unexpected type %d",
|
|
|
|
|
type);
|
|
|
|
|
}
|
|
|
|
|
else if (G_LIKELY (body != NULL && g_variant_is_of_type (body, G_VARIANT_TYPE ("(s)"))))
|
|
|
|
|
{
|
|
|
|
|
const char *new_owner;
|
|
|
|
|
|
|
|
|
|
g_variant_get (body, "(&s)", &new_owner);
|
|
|
|
|
|
2024-05-08 16:46:08 +02:00
|
|
|
|
/* FIXME: This should be validating that `new_owner` is a unique name,
|
|
|
|
|
* but IBus’ implementation of a message bus is not compliant with the spec.
|
|
|
|
|
* See https://gitlab.gnome.org/GNOME/glib/-/issues/3353 */
|
|
|
|
|
if (G_LIKELY (g_dbus_is_name (new_owner)))
|
2024-04-23 21:42:17 +02:00
|
|
|
|
name_watcher_set_name_owner_unlocked (name_watcher, new_owner);
|
|
|
|
|
else
|
|
|
|
|
g_warning ("Received GetNameOwner reply with invalid owner \"%s\" for \"%s\"",
|
|
|
|
|
new_owner, name_watcher->arg0);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
g_warning ("Received GetNameOwner reply with unexpected signature %s",
|
|
|
|
|
body == NULL ? "()" : g_variant_get_type_string (body));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g_hash_table_remove (connection->map_method_serial_to_name_watcher,
|
|
|
|
|
GUINT_TO_POINTER (watched_name->get_name_owner_serial));
|
|
|
|
|
watched_name->get_name_owner_serial = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Called in a user thread, lock is held */
|
|
|
|
|
static void
|
|
|
|
|
name_watcher_call_get_name_owner_unlocked (GDBusConnection *connection,
|
|
|
|
|
SignalData *name_watcher)
|
|
|
|
|
{
|
|
|
|
|
GDBusMessage *message;
|
|
|
|
|
GError *local_error = NULL;
|
|
|
|
|
WatchedName *watched_name;
|
|
|
|
|
|
|
|
|
|
g_assert (g_strcmp0 (name_watcher->sender, DBUS_SERVICE_DBUS) == 0);
|
|
|
|
|
g_assert (g_strcmp0 (name_watcher->interface_name, DBUS_INTERFACE_DBUS) == 0);
|
|
|
|
|
g_assert (g_strcmp0 (name_watcher->member, "NameOwnerChanged") == 0);
|
|
|
|
|
g_assert (g_strcmp0 (name_watcher->object_path, DBUS_PATH_DBUS) == 0);
|
|
|
|
|
/* arg0 of the NameOwnerChanged message is the well-known name whose owner
|
|
|
|
|
* we are interested in */
|
|
|
|
|
g_assert (g_dbus_is_name (name_watcher->arg0));
|
|
|
|
|
g_assert (name_watcher->flags == G_DBUS_SIGNAL_FLAGS_NONE);
|
|
|
|
|
|
|
|
|
|
watched_name = name_watcher->watched_name;
|
|
|
|
|
g_assert (watched_name != NULL);
|
|
|
|
|
g_assert (watched_name->owner == NULL);
|
|
|
|
|
g_assert (watched_name->get_name_owner_serial == 0);
|
|
|
|
|
g_assert (name_watcher->shared_name_watcher == NULL);
|
|
|
|
|
|
|
|
|
|
message = g_dbus_message_new_method_call (DBUS_SERVICE_DBUS,
|
|
|
|
|
DBUS_PATH_DBUS,
|
|
|
|
|
DBUS_INTERFACE_DBUS,
|
|
|
|
|
"GetNameOwner");
|
|
|
|
|
g_dbus_message_set_body (message, g_variant_new ("(s)", name_watcher->arg0));
|
|
|
|
|
|
|
|
|
|
if (g_dbus_connection_send_message_unlocked (connection, message,
|
|
|
|
|
G_DBUS_SEND_MESSAGE_FLAGS_NONE,
|
|
|
|
|
&watched_name->get_name_owner_serial,
|
|
|
|
|
&local_error))
|
|
|
|
|
{
|
|
|
|
|
g_assert (watched_name->get_name_owner_serial != 0);
|
|
|
|
|
g_hash_table_insert (connection->map_method_serial_to_name_watcher,
|
|
|
|
|
GUINT_TO_POINTER (watched_name->get_name_owner_serial),
|
|
|
|
|
name_watcher);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
g_critical ("Error while sending GetNameOwner() message: %s",
|
|
|
|
|
local_error->message);
|
|
|
|
|
g_clear_error (&local_error);
|
|
|
|
|
g_assert (watched_name->get_name_owner_serial == 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g_object_unref (message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
typedef struct
|
|
|
|
|
{
|
|
|
|
|
guint id;
|
2015-08-21 23:39:44 +02:00
|
|
|
|
guint ref_count;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
GDBusMessageFilterFunction filter_function;
|
|
|
|
|
gpointer user_data;
|
|
|
|
|
GDestroyNotify user_data_free_func;
|
2015-08-21 23:39:44 +02:00
|
|
|
|
GMainContext *context;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
} FilterData;
|
|
|
|
|
|
2021-03-28 08:53:57 +02:00
|
|
|
|
static void
|
|
|
|
|
filter_data_destroy (FilterData *filter, gboolean notify_sync)
|
|
|
|
|
{
|
|
|
|
|
if (notify_sync)
|
|
|
|
|
{
|
|
|
|
|
if (filter->user_data_free_func != NULL)
|
|
|
|
|
filter->user_data_free_func (filter->user_data);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
call_destroy_notify (filter->context,
|
|
|
|
|
filter->user_data_free_func,
|
|
|
|
|
filter->user_data);
|
|
|
|
|
}
|
|
|
|
|
g_main_context_unref (filter->context);
|
|
|
|
|
g_free (filter);
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-21 23:39:44 +02:00
|
|
|
|
/* 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)
|
2021-03-28 08:53:57 +02:00
|
|
|
|
filter_data_destroy (filters[n], FALSE);
|
2015-08-21 23:39:44 +02:00
|
|
|
|
}
|
|
|
|
|
g_free (filters);
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-17 14:27:03 +02:00
|
|
|
|
/* Called in GDBusWorker's thread - we must not block - with no lock held */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
static void
|
|
|
|
|
on_worker_message_received (GDBusWorker *worker,
|
|
|
|
|
GDBusMessage *message,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
2010-09-23 21:10:50 +02:00
|
|
|
|
GDBusConnection *connection;
|
2015-08-21 23:39:44 +02:00
|
|
|
|
FilterData **filters;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
guint n;
|
2010-09-23 21:10:50 +02:00
|
|
|
|
gboolean alive;
|
|
|
|
|
|
|
|
|
|
G_LOCK (message_bus_lock);
|
2017-10-25 11:53:14 +02:00
|
|
|
|
alive = g_hash_table_contains (alive_connections, user_data);
|
2010-09-23 21:10:50 +02:00
|
|
|
|
if (!alive)
|
|
|
|
|
{
|
|
|
|
|
G_UNLOCK (message_bus_lock);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
connection = G_DBUS_CONNECTION (user_data);
|
|
|
|
|
g_object_ref (connection);
|
|
|
|
|
G_UNLOCK (message_bus_lock);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
//g_debug ("in on_worker_message_received");
|
|
|
|
|
|
2010-09-09 19:21:35 +02:00
|
|
|
|
g_object_ref (message);
|
|
|
|
|
g_dbus_message_lock (message);
|
|
|
|
|
|
2010-09-23 21:10:50 +02:00
|
|
|
|
//g_debug ("boo ref_count = %d %p %p", G_OBJECT (connection)->ref_count, connection, connection->worker);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
/* First collect the set of callback functions */
|
|
|
|
|
CONNECTION_LOCK (connection);
|
2015-08-21 23:39:44 +02:00
|
|
|
|
filters = copy_filter_list (connection->filters);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
CONNECTION_UNLOCK (connection);
|
|
|
|
|
|
2010-09-09 19:21:35 +02:00
|
|
|
|
/* then call the filters in order (without holding the lock) */
|
2015-08-21 23:39:44 +02:00
|
|
|
|
for (n = 0; filters[n]; n++)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
2015-08-21 23:39:44 +02:00
|
|
|
|
message = filters[n]->filter_function (connection,
|
|
|
|
|
message,
|
|
|
|
|
TRUE,
|
|
|
|
|
filters[n]->user_data);
|
2010-09-09 19:21:35 +02:00
|
|
|
|
if (message == NULL)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
break;
|
2010-09-09 19:21:35 +02:00
|
|
|
|
g_dbus_message_lock (message);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-08-21 23:39:44 +02:00
|
|
|
|
CONNECTION_LOCK (connection);
|
|
|
|
|
free_filter_list (filters);
|
|
|
|
|
CONNECTION_UNLOCK (connection);
|
|
|
|
|
|
2010-08-06 02:37:27 +02:00
|
|
|
|
/* Standard dispatch unless the filter ate the message - no need to
|
|
|
|
|
* do anything if the message was altered
|
|
|
|
|
*/
|
2010-09-09 19:21:35 +02:00
|
|
|
|
if (message != NULL)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
|
|
|
|
GDBusMessageType message_type;
|
|
|
|
|
|
2010-05-06 23:31:51 +02:00
|
|
|
|
message_type = g_dbus_message_get_message_type (message);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
if (message_type == G_DBUS_MESSAGE_TYPE_METHOD_RETURN || message_type == G_DBUS_MESSAGE_TYPE_ERROR)
|
|
|
|
|
{
|
|
|
|
|
guint32 reply_serial;
|
2012-08-02 21:46:32 +02:00
|
|
|
|
GTask *task;
|
2024-04-23 21:42:17 +02:00
|
|
|
|
SignalData *name_watcher;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
reply_serial = g_dbus_message_get_reply_serial (message);
|
|
|
|
|
CONNECTION_LOCK (connection);
|
2012-08-02 21:46:32 +02:00
|
|
|
|
task = g_hash_table_lookup (connection->map_method_serial_to_task,
|
|
|
|
|
GUINT_TO_POINTER (reply_serial));
|
|
|
|
|
if (task != NULL)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
2017-04-28 13:05:42 +02:00
|
|
|
|
/* This removes @task from @map_method_serial_to_task. */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
//g_debug ("delivering reply/error for serial %d for %p", reply_serial, connection);
|
2012-08-02 21:46:32 +02:00
|
|
|
|
send_message_data_deliver_reply_unlocked (task, message);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
//g_debug ("message reply/error for serial %d but no SendMessageData found for %p", reply_serial, connection);
|
|
|
|
|
}
|
2024-04-23 21:42:17 +02:00
|
|
|
|
|
|
|
|
|
name_watcher = g_hash_table_lookup (connection->map_method_serial_to_name_watcher,
|
|
|
|
|
GUINT_TO_POINTER (reply_serial));
|
|
|
|
|
|
|
|
|
|
if (name_watcher != NULL)
|
|
|
|
|
{
|
|
|
|
|
g_assert (name_watcher->watched_name != NULL);
|
|
|
|
|
g_assert (name_watcher->watched_name->get_name_owner_serial == reply_serial);
|
|
|
|
|
name_watcher_deliver_get_name_owner_reply_unlocked (name_watcher,
|
|
|
|
|
connection,
|
|
|
|
|
message);
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-09 19:21:35 +02:00
|
|
|
|
if (message != NULL)
|
|
|
|
|
g_object_unref (message);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_object_unref (connection);
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-17 14:27:03 +02:00
|
|
|
|
/* Called in GDBusWorker's thread, lock is not held */
|
2010-09-09 19:21:35 +02:00
|
|
|
|
static GDBusMessage *
|
2010-06-21 22:08:53 +02:00
|
|
|
|
on_worker_message_about_to_be_sent (GDBusWorker *worker,
|
|
|
|
|
GDBusMessage *message,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
2010-09-23 21:10:50 +02:00
|
|
|
|
GDBusConnection *connection;
|
2015-08-21 23:39:44 +02:00
|
|
|
|
FilterData **filters;
|
2010-06-21 22:08:53 +02:00
|
|
|
|
guint n;
|
2010-09-23 21:10:50 +02:00
|
|
|
|
gboolean alive;
|
2010-06-21 22:08:53 +02:00
|
|
|
|
|
2010-09-23 21:10:50 +02:00
|
|
|
|
G_LOCK (message_bus_lock);
|
2017-10-25 11:53:14 +02:00
|
|
|
|
alive = g_hash_table_contains (alive_connections, user_data);
|
2010-09-23 21:10:50 +02:00
|
|
|
|
if (!alive)
|
|
|
|
|
{
|
|
|
|
|
G_UNLOCK (message_bus_lock);
|
|
|
|
|
return message;
|
|
|
|
|
}
|
|
|
|
|
connection = G_DBUS_CONNECTION (user_data);
|
2010-06-21 22:08:53 +02:00
|
|
|
|
g_object_ref (connection);
|
2010-09-23 21:10:50 +02:00
|
|
|
|
G_UNLOCK (message_bus_lock);
|
|
|
|
|
|
|
|
|
|
//g_debug ("in on_worker_message_about_to_be_sent");
|
2010-06-21 22:08:53 +02:00
|
|
|
|
|
|
|
|
|
/* First collect the set of callback functions */
|
|
|
|
|
CONNECTION_LOCK (connection);
|
2015-08-21 23:39:44 +02:00
|
|
|
|
filters = copy_filter_list (connection->filters);
|
2010-06-21 22:08:53 +02:00
|
|
|
|
CONNECTION_UNLOCK (connection);
|
|
|
|
|
|
2010-08-06 02:37:27 +02:00
|
|
|
|
/* then call the filters in order (without holding the lock) */
|
2015-08-21 23:39:44 +02:00
|
|
|
|
for (n = 0; filters[n]; n++)
|
2010-06-21 22:08:53 +02:00
|
|
|
|
{
|
2010-09-09 19:21:35 +02:00
|
|
|
|
g_dbus_message_lock (message);
|
2015-08-21 23:39:44 +02:00
|
|
|
|
message = filters[n]->filter_function (connection,
|
|
|
|
|
message,
|
|
|
|
|
FALSE,
|
|
|
|
|
filters[n]->user_data);
|
2010-09-09 19:21:35 +02:00
|
|
|
|
if (message == NULL)
|
|
|
|
|
break;
|
2010-06-21 22:08:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-08-21 23:39:44 +02:00
|
|
|
|
CONNECTION_LOCK (connection);
|
|
|
|
|
free_filter_list (filters);
|
|
|
|
|
CONNECTION_UNLOCK (connection);
|
|
|
|
|
|
2010-06-21 22:08:53 +02:00
|
|
|
|
g_object_unref (connection);
|
|
|
|
|
|
2010-09-09 19:21:35 +02:00
|
|
|
|
return message;
|
2010-06-21 22:08:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-22 13:19:16 +01:00
|
|
|
|
/* called with connection lock held, in GDBusWorker thread
|
|
|
|
|
* @key, @value and @user_data are (transfer none) */
|
2011-10-07 20:20:03 +02:00
|
|
|
|
static gboolean
|
|
|
|
|
cancel_method_on_close (gpointer key, gpointer value, gpointer user_data)
|
|
|
|
|
{
|
2012-08-02 21:46:32 +02:00
|
|
|
|
GTask *task = value;
|
|
|
|
|
SendMessageData *data = g_task_get_task_data (task);
|
2011-10-07 20:20:03 +02:00
|
|
|
|
|
|
|
|
|
if (data->delivered)
|
|
|
|
|
return FALSE;
|
|
|
|
|
|
2023-12-19 21:03:28 +01:00
|
|
|
|
g_task_return_new_error_literal (task,
|
|
|
|
|
G_IO_ERROR,
|
|
|
|
|
G_IO_ERROR_CLOSED,
|
|
|
|
|
_("The connection is closed"));
|
2011-10-07 20:20:03 +02:00
|
|
|
|
|
2012-08-02 21:46:32 +02:00
|
|
|
|
/* Ask send_message_with_reply_cleanup not to remove the element from the
|
2011-10-07 20:20:03 +02:00
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2012-08-02 21:46:32 +02:00
|
|
|
|
send_message_with_reply_cleanup (task, FALSE);
|
2011-10-07 20:20:03 +02:00
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-17 14:27:03 +02:00
|
|
|
|
/* Called in GDBusWorker's thread - we must not block - without lock held */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
static void
|
|
|
|
|
on_worker_closed (GDBusWorker *worker,
|
|
|
|
|
gboolean remote_peer_vanished,
|
|
|
|
|
GError *error,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
2010-09-23 21:10:50 +02:00
|
|
|
|
GDBusConnection *connection;
|
|
|
|
|
gboolean alive;
|
GDBusConnection: make the closed flag atomic (but still lock to write)
Strictly speaking, neither of the two uses that aren't under the lock
*needs* to be atomic, but it seems better to be obviously correct (and
we save another 4 bytes of struct).
One of these uses is in g_dbus_connection_is_closed(), any use of which
is inherently a race condition anyway.
The other is g_dbus_connection_flush_sync, which as far as I can tell
just needs a best-effort check, to not waste effort on a connection that
has been closed for a while (but I could be wrong).
I removed the check for the closed flag altogether in
g_dbus_connection_send_message_with_reply_unlocked, because it turns out
to be redundant with one in g_dbus_connection_send_message_unlocked,
which is called immediately after.
g_dbus_connection_close_sync held the lock to check the closed flag,
which is no longer needed.
As far as I can tell, the only reason why the lock is still desirable
when setting the closed flag is so that remove_match_rule can't fail
by racing with close notification from the worker thread - but
on_worker_closed needs to hold the lock anyway, to deal with other
data structures, so there's no point in trying to eliminate the
requirement to hold the lock.
Bug: https://bugzilla.gnome.org/show_bug.cgi?id=661992
Signed-off-by: Simon McVittie <simon.mcvittie@collabora.co.uk>
Reviewed-by: David Zeuthen <davidz@redhat.com>
2011-10-21 16:46:00 +02:00
|
|
|
|
guint old_atomic_flags;
|
2010-09-23 21:10:50 +02:00
|
|
|
|
|
|
|
|
|
G_LOCK (message_bus_lock);
|
2017-10-25 11:53:14 +02:00
|
|
|
|
alive = g_hash_table_contains (alive_connections, user_data);
|
2010-09-23 21:10:50 +02:00
|
|
|
|
if (!alive)
|
|
|
|
|
{
|
|
|
|
|
G_UNLOCK (message_bus_lock);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
connection = G_DBUS_CONNECTION (user_data);
|
|
|
|
|
g_object_ref (connection);
|
|
|
|
|
G_UNLOCK (message_bus_lock);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
//g_debug ("in on_worker_closed: %s", error->message);
|
|
|
|
|
|
|
|
|
|
CONNECTION_LOCK (connection);
|
GDBusConnection: make the closed flag atomic (but still lock to write)
Strictly speaking, neither of the two uses that aren't under the lock
*needs* to be atomic, but it seems better to be obviously correct (and
we save another 4 bytes of struct).
One of these uses is in g_dbus_connection_is_closed(), any use of which
is inherently a race condition anyway.
The other is g_dbus_connection_flush_sync, which as far as I can tell
just needs a best-effort check, to not waste effort on a connection that
has been closed for a while (but I could be wrong).
I removed the check for the closed flag altogether in
g_dbus_connection_send_message_with_reply_unlocked, because it turns out
to be redundant with one in g_dbus_connection_send_message_unlocked,
which is called immediately after.
g_dbus_connection_close_sync held the lock to check the closed flag,
which is no longer needed.
As far as I can tell, the only reason why the lock is still desirable
when setting the closed flag is so that remove_match_rule can't fail
by racing with close notification from the worker thread - but
on_worker_closed needs to hold the lock anyway, to deal with other
data structures, so there's no point in trying to eliminate the
requirement to hold the lock.
Bug: https://bugzilla.gnome.org/show_bug.cgi?id=661992
Signed-off-by: Simon McVittie <simon.mcvittie@collabora.co.uk>
Reviewed-by: David Zeuthen <davidz@redhat.com>
2011-10-21 16:46:00 +02:00
|
|
|
|
/* 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))
|
2011-10-07 20:20:03 +02:00
|
|
|
|
{
|
2012-08-02 21:46:32 +02:00
|
|
|
|
g_hash_table_foreach_remove (connection->map_method_serial_to_task, cancel_method_on_close, NULL);
|
GDBusConnection: make the closed flag atomic (but still lock to write)
Strictly speaking, neither of the two uses that aren't under the lock
*needs* to be atomic, but it seems better to be obviously correct (and
we save another 4 bytes of struct).
One of these uses is in g_dbus_connection_is_closed(), any use of which
is inherently a race condition anyway.
The other is g_dbus_connection_flush_sync, which as far as I can tell
just needs a best-effort check, to not waste effort on a connection that
has been closed for a while (but I could be wrong).
I removed the check for the closed flag altogether in
g_dbus_connection_send_message_with_reply_unlocked, because it turns out
to be redundant with one in g_dbus_connection_send_message_unlocked,
which is called immediately after.
g_dbus_connection_close_sync held the lock to check the closed flag,
which is no longer needed.
As far as I can tell, the only reason why the lock is still desirable
when setting the closed flag is so that remove_match_rule can't fail
by racing with close notification from the worker thread - but
on_worker_closed needs to hold the lock anyway, to deal with other
data structures, so there's no point in trying to eliminate the
requirement to hold the lock.
Bug: https://bugzilla.gnome.org/show_bug.cgi?id=661992
Signed-off-by: Simon McVittie <simon.mcvittie@collabora.co.uk>
Reviewed-by: David Zeuthen <davidz@redhat.com>
2011-10-21 16:46:00 +02:00
|
|
|
|
schedule_closed_unlocked (connection, remote_peer_vanished, error);
|
2011-10-07 20:20:03 +02:00
|
|
|
|
}
|
2010-05-06 20:13:59 +02:00
|
|
|
|
CONNECTION_UNLOCK (connection);
|
2010-09-23 21:10:50 +02:00
|
|
|
|
|
|
|
|
|
g_object_unref (connection);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
2011-10-17 14:27:03 +02:00
|
|
|
|
/* Determines the biggest set of capabilities we can support on this
|
|
|
|
|
* connection.
|
|
|
|
|
*
|
|
|
|
|
* Called with the init_lock held.
|
|
|
|
|
*/
|
2010-05-06 20:13:59 +02:00
|
|
|
|
static GDBusCapabilityFlags
|
|
|
|
|
get_offered_capabilities_max (GDBusConnection *connection)
|
|
|
|
|
{
|
|
|
|
|
GDBusCapabilityFlags ret;
|
|
|
|
|
ret = G_DBUS_CAPABILITY_FLAGS_NONE;
|
|
|
|
|
#ifdef G_OS_UNIX
|
2010-07-07 22:35:17 +02:00
|
|
|
|
if (G_IS_UNIX_CONNECTION (connection->stream))
|
2010-05-10 14:07:07 +02:00
|
|
|
|
ret |= G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
#endif
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-17 14:27:03 +02:00
|
|
|
|
/* Called in a user thread, lock is not held */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
static gboolean
|
2010-05-10 04:13:18 +02:00
|
|
|
|
initable_init (GInitable *initable,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GError **error)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
|
|
|
|
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).
|
|
|
|
|
*/
|
2011-10-04 05:26:55 +02:00
|
|
|
|
g_mutex_lock (&connection->init_lock);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
ret = FALSE;
|
|
|
|
|
|
2011-10-21 17:02:22 +02:00
|
|
|
|
/* Make this a no-op if we're already initialized (successfully or
|
|
|
|
|
* unsuccessfully)
|
2011-04-13 20:03:58 +02:00
|
|
|
|
*/
|
2011-10-21 17:02:22 +02:00
|
|
|
|
if ((g_atomic_int_get (&connection->atomic_flags) & FLAG_INITIALIZED))
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
2011-10-21 17:02:22 +02:00
|
|
|
|
ret = (connection->initialization_error == NULL);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
goto out;
|
|
|
|
|
}
|
2011-04-13 20:03:58 +02:00
|
|
|
|
|
2011-10-21 17:02:22 +02:00
|
|
|
|
/* Because of init_lock, we can't get here twice in different threads */
|
|
|
|
|
g_assert (connection->initialization_error == NULL);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
/* 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
|
2010-07-07 22:35:17 +02:00
|
|
|
|
* object in connection->stream.
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*/
|
2010-07-07 22:35:17 +02:00
|
|
|
|
if (connection->address != NULL)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
2010-07-07 22:35:17 +02:00
|
|
|
|
g_assert (connection->stream == NULL);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2010-07-07 22:35:17 +02:00
|
|
|
|
if ((connection->flags & G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER) ||
|
2020-12-15 14:00:28 +01:00
|
|
|
|
(connection->flags & G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS) ||
|
|
|
|
|
(connection->flags & G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_REQUIRE_SAME_USER))
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
2011-11-28 18:54:46 +01:00
|
|
|
|
g_set_error_literal (&connection->initialization_error,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
G_IO_ERROR,
|
|
|
|
|
G_IO_ERROR_INVALID_ARGUMENT,
|
|
|
|
|
_("Unsupported flags encountered when constructing a client-side connection"));
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-07 22:35:17 +02:00
|
|
|
|
connection->stream = g_dbus_address_get_stream_sync (connection->address,
|
|
|
|
|
NULL, /* TODO: out_guid */
|
|
|
|
|
cancellable,
|
|
|
|
|
&connection->initialization_error);
|
|
|
|
|
if (connection->stream == NULL)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
goto out;
|
|
|
|
|
}
|
2010-07-07 22:35:17 +02:00
|
|
|
|
else if (connection->stream != NULL)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
|
|
|
|
/* nothing to do */
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
g_assert_not_reached ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Authenticate the connection */
|
2010-07-07 22:35:17 +02:00
|
|
|
|
if (connection->flags & G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
2010-07-07 22:35:17 +02:00
|
|
|
|
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),
|
2020-12-15 14:00:28 +01:00
|
|
|
|
(connection->flags & G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_REQUIRE_SAME_USER),
|
2010-05-06 20:13:59 +02:00
|
|
|
|
get_offered_capabilities_max (connection),
|
2010-07-07 22:35:17 +02:00
|
|
|
|
&connection->capabilities,
|
2010-07-31 05:44:30 +02:00
|
|
|
|
&connection->credentials,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
cancellable,
|
2010-07-07 22:35:17 +02:00
|
|
|
|
&connection->initialization_error))
|
2010-05-06 20:13:59 +02:00
|
|
|
|
goto out;
|
|
|
|
|
}
|
2010-07-07 22:35:17 +02:00
|
|
|
|
else if (connection->flags & G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
2010-07-07 22:35:17 +02:00
|
|
|
|
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,
|
2012-04-12 05:30:48 +02:00
|
|
|
|
connection->authentication_observer,
|
gdbusauthmechanismexternal: Optionally send empty authorization identity
When using a GDBus client in a non-trivial user namespace, the result of
geteuid() can differ from the uid in the namespace where the server is
running. This would result in connection attempts being rejected, because
the identity that the client claims to have does not match the identity
that the server derives from its credentials.
RFC 4422 allows us to send an empty authorization identity, which means we
want to authenticate as whatever identity the server can derive from our
out-of-band credentials. In particular, this resolves the authentication
failure when crossing between different Linux user namespaces.
Because D-Bus does not have a way to represent an empty initial response
as distinct from the absence of an initial response, we cannot use the
initial-response optimization (RFC 4422 §4.3.a) in this case, and must
fall back to waiting for the server to send a challenge.
Unfortunately, GDBus versions older than glib!2826 did not implement
the server side of this protocol correctly, and would respond to the
missing initial response in a way that breaks the SASL state machine
(expecting a response without sending a challenge), causing client and
server to deadlock with each waiting for the other to respond. Until
fixed versions of GDBus are widespread, we can't rely on having a server
that can cope with this, so gate it behind a flag, which can be set for
connections that are known to cross non-trivial namespace boundaries.
Originally inspired by
<https://github.com/systemd/systemd/commit/1ed4723d38cd0d1423c8fe650f90fa86007ddf55>,
and based on earlier work by Giuseppe Scrivano (in which the
cross-namespace behaviour was unconditional, rather than gated by a
flag).
Co-authored-by: Giuseppe Scrivano <giuseppe@scrivano.org>
Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-07-24 14:02:51 +02:00
|
|
|
|
connection->flags,
|
2010-07-07 22:35:17 +02:00
|
|
|
|
get_offered_capabilities_max (connection),
|
|
|
|
|
&connection->capabilities,
|
|
|
|
|
cancellable,
|
|
|
|
|
&connection->initialization_error);
|
|
|
|
|
if (connection->guid == NULL)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-07 22:35:17 +02:00
|
|
|
|
if (connection->authentication_observer != NULL)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
2010-07-07 22:35:17 +02:00
|
|
|
|
g_object_unref (connection->authentication_observer);
|
|
|
|
|
connection->authentication_observer = NULL;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
2010-07-07 22:35:17 +02:00
|
|
|
|
//g_output_stream_flush (G_SOCKET_CONNECTION (connection->stream)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2010-07-07 22:35:17 +02:00
|
|
|
|
//g_debug ("haz unix fd passing powers: %d", connection->capabilities & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2010-05-20 16:51:00 +02:00
|
|
|
|
#ifdef G_OS_UNIX
|
2011-05-12 21:36:34 +02:00
|
|
|
|
/* We want all IO operations to be non-blocking since they happen in
|
|
|
|
|
* the worker thread which is shared by _all_ connections.
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*/
|
2010-07-07 22:35:17 +02:00
|
|
|
|
if (G_IS_SOCKET_CONNECTION (connection->stream))
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
2010-07-07 22:35:17 +02:00
|
|
|
|
g_socket_set_blocking (g_socket_connection_get_socket (G_SOCKET_CONNECTION (connection->stream)), FALSE);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
2010-05-20 16:51:00 +02:00
|
|
|
|
#endif
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2010-09-23 21:10:50 +02:00
|
|
|
|
G_LOCK (message_bus_lock);
|
|
|
|
|
if (alive_connections == NULL)
|
|
|
|
|
alive_connections = g_hash_table_new (g_direct_hash, g_direct_equal);
|
2017-10-25 11:53:14 +02:00
|
|
|
|
g_hash_table_add (alive_connections, connection);
|
2010-09-23 21:10:50 +02:00
|
|
|
|
G_UNLOCK (message_bus_lock);
|
|
|
|
|
|
2010-07-07 22:35:17 +02:00
|
|
|
|
connection->worker = _g_dbus_worker_new (connection->stream,
|
|
|
|
|
connection->capabilities,
|
2011-11-22 13:39:54 +01:00
|
|
|
|
((connection->flags & G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING) != 0),
|
2010-07-07 22:35:17 +02:00
|
|
|
|
on_worker_message_received,
|
|
|
|
|
on_worker_message_about_to_be_sent,
|
|
|
|
|
on_worker_closed,
|
|
|
|
|
connection);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2010-06-30 17:43:42 +02:00
|
|
|
|
/* if a bus connection, call org.freedesktop.DBus.Hello - this is how we're getting a name */
|
2010-07-07 22:35:17 +02:00
|
|
|
|
if (connection->flags & G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
|
|
|
|
GVariant *hello_result;
|
|
|
|
|
|
2010-06-30 17:43:42 +02:00
|
|
|
|
/* we could lift this restriction by adding code in gdbusprivate.c */
|
2010-07-07 22:35:17 +02:00
|
|
|
|
if (connection->flags & G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING)
|
2010-06-30 17:43:42 +02:00
|
|
|
|
{
|
2010-07-07 22:35:17 +02:00
|
|
|
|
g_set_error_literal (&connection->initialization_error,
|
2010-06-30 17:43:42 +02:00
|
|
|
|
G_IO_ERROR,
|
|
|
|
|
G_IO_ERROR_FAILED,
|
|
|
|
|
"Cannot use DELAY_MESSAGE_PROCESSING with MESSAGE_BUS_CONNECTION");
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-10 17:47:08 +02:00
|
|
|
|
hello_result = g_dbus_connection_call_sync (connection,
|
2024-05-13 13:24:16 +02:00
|
|
|
|
DBUS_SERVICE_DBUS,
|
|
|
|
|
DBUS_PATH_DBUS,
|
|
|
|
|
DBUS_INTERFACE_DBUS,
|
2010-05-10 17:47:08 +02:00
|
|
|
|
"Hello",
|
|
|
|
|
NULL, /* parameters */
|
2010-05-24 22:46:24 +02:00
|
|
|
|
G_VARIANT_TYPE ("(s)"),
|
2011-10-20 14:12:26 +02:00
|
|
|
|
CALL_FLAGS_INITIALIZING,
|
2010-05-10 17:47:08 +02:00
|
|
|
|
-1,
|
|
|
|
|
NULL, /* TODO: cancellable */
|
2010-07-07 22:35:17 +02:00
|
|
|
|
&connection->initialization_error);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
if (hello_result == NULL)
|
|
|
|
|
goto out;
|
|
|
|
|
|
2010-07-07 22:35:17 +02:00
|
|
|
|
g_variant_get (hello_result, "(s)", &connection->bus_unique_name);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_variant_unref (hello_result);
|
2013-05-20 22:54:48 +02:00
|
|
|
|
//g_debug ("unique name is '%s'", connection->bus_unique_name);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret = TRUE;
|
|
|
|
|
out:
|
|
|
|
|
if (!ret)
|
|
|
|
|
{
|
2010-07-07 22:35:17 +02:00
|
|
|
|
g_assert (connection->initialization_error != NULL);
|
|
|
|
|
g_propagate_error (error, g_error_copy (connection->initialization_error));
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-11 15:17:14 +01:00
|
|
|
|
/* Don't cache canceled errors. Otherwise other concurrent users of the same connection
|
|
|
|
|
* object will be canceled as well. */
|
|
|
|
|
if (g_error_matches (connection->initialization_error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
|
|
|
|
|
{
|
|
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
g_clear_error (&connection->initialization_error);
|
|
|
|
|
g_clear_object (&connection->stream);
|
|
|
|
|
g_clear_object (&connection->auth);
|
|
|
|
|
g_clear_object (&connection->credentials);
|
|
|
|
|
g_clear_pointer (&connection->guid, g_free);
|
|
|
|
|
connection->capabilities = 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
g_atomic_int_or (&connection->atomic_flags, FLAG_INITIALIZED);
|
2011-10-04 05:26:55 +02:00
|
|
|
|
g_mutex_unlock (&connection->init_lock);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2010-06-08 17:21:48 +02:00
|
|
|
|
/* Use default */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_dbus_connection_new:
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @stream: a #GIOStream
|
2019-08-23 23:45:16 +02:00
|
|
|
|
* @guid: (nullable): the GUID to use if authenticating as a server or %NULL
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @flags: flags describing how to make the connection
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @observer: (nullable): a #GDBusAuthObserver or %NULL
|
|
|
|
|
* @cancellable: (nullable): a #GCancellable or %NULL
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @callback: a #GAsyncReadyCallback to call when the request is satisfied
|
|
|
|
|
* @user_data: the data to pass to @callback
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
|
|
|
|
* Asynchronously sets up a D-Bus connection for exchanging D-Bus messages
|
|
|
|
|
* with the end represented by @stream.
|
|
|
|
|
*
|
2011-05-12 21:36:34 +02:00
|
|
|
|
* If @stream is a #GSocketConnection, then the corresponding #GSocket
|
|
|
|
|
* will be put into non-blocking mode.
|
|
|
|
|
*
|
2011-10-13 18:39:03 +02:00
|
|
|
|
* 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.
|
|
|
|
|
*
|
2010-05-13 22:32:11 +02:00
|
|
|
|
* If @observer is not %NULL it may be used to control the
|
2010-05-06 20:13:59 +02:00
|
|
|
|
* 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.
|
|
|
|
|
*
|
2019-07-16 01:02:09 +02:00
|
|
|
|
* This is an asynchronous failable constructor. See
|
2010-05-06 20:13:59 +02:00
|
|
|
|
* g_dbus_connection_new_sync() for the synchronous
|
|
|
|
|
* version.
|
2010-05-06 22:02:08 +02:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.26
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*/
|
|
|
|
|
void
|
2010-05-10 04:13:18 +02:00
|
|
|
|
g_dbus_connection_new (GIOStream *stream,
|
|
|
|
|
const gchar *guid,
|
|
|
|
|
GDBusConnectionFlags flags,
|
2010-05-12 02:57:44 +02:00
|
|
|
|
GDBusAuthObserver *observer,
|
2010-05-10 04:13:18 +02:00
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
|
gpointer user_data)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
2016-10-31 08:36:18 +01:00
|
|
|
|
_g_dbus_initialize ();
|
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_return_if_fail (G_IS_IO_STREAM (stream));
|
2021-02-08 12:57:45 +01:00
|
|
|
|
g_return_if_fail ((flags & ~G_DBUS_CONNECTION_FLAGS_ALL) == 0);
|
2016-10-31 08:36:18 +01:00
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_async_initable_new_async (G_TYPE_DBUS_CONNECTION,
|
|
|
|
|
G_PRIORITY_DEFAULT,
|
|
|
|
|
cancellable,
|
|
|
|
|
callback,
|
|
|
|
|
user_data,
|
|
|
|
|
"stream", stream,
|
|
|
|
|
"guid", guid,
|
|
|
|
|
"flags", flags,
|
2010-05-12 02:57:44 +02:00
|
|
|
|
"authentication-observer", observer,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_dbus_connection_new_finish:
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @res: a #GAsyncResult obtained from the #GAsyncReadyCallback
|
|
|
|
|
* passed to g_dbus_connection_new().
|
|
|
|
|
* @error: return location for error or %NULL
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
|
|
|
|
* Finishes an operation started with g_dbus_connection_new().
|
|
|
|
|
*
|
2020-12-15 10:13:22 +01:00
|
|
|
|
* Returns: (transfer full): a #GDBusConnection or %NULL if @error is set. Free
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* with g_object_unref().
|
2010-05-06 22:02:08 +02:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.26
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*/
|
|
|
|
|
GDBusConnection *
|
2010-05-10 04:13:18 +02:00
|
|
|
|
g_dbus_connection_new_finish (GAsyncResult *res,
|
|
|
|
|
GError **error)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
|
|
|
|
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:
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @stream: a #GIOStream
|
2019-08-23 23:45:16 +02:00
|
|
|
|
* @guid: (nullable): the GUID to use if authenticating as a server or %NULL
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @flags: flags describing how to make the connection
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @observer: (nullable): a #GDBusAuthObserver or %NULL
|
|
|
|
|
* @cancellable: (nullable): a #GCancellable or %NULL
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @error: return location for error or %NULL
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
|
|
|
|
* Synchronously sets up a D-Bus connection for exchanging D-Bus messages
|
|
|
|
|
* with the end represented by @stream.
|
|
|
|
|
*
|
2011-05-12 21:36:34 +02:00
|
|
|
|
* If @stream is a #GSocketConnection, then the corresponding #GSocket
|
|
|
|
|
* will be put into non-blocking mode.
|
|
|
|
|
*
|
2011-10-13 18:39:03 +02:00
|
|
|
|
* 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.
|
|
|
|
|
*
|
2010-05-13 22:32:11 +02:00
|
|
|
|
* If @observer is not %NULL it may be used to control the
|
2010-05-06 20:13:59 +02:00
|
|
|
|
* authentication process.
|
|
|
|
|
*
|
|
|
|
|
* This is a synchronous failable constructor. See
|
|
|
|
|
* g_dbus_connection_new() for the asynchronous version.
|
|
|
|
|
*
|
2020-12-15 10:13:22 +01:00
|
|
|
|
* Returns: (transfer full): a #GDBusConnection or %NULL if @error is set.
|
|
|
|
|
* Free with g_object_unref().
|
2010-05-06 22:02:08 +02:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.26
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*/
|
|
|
|
|
GDBusConnection *
|
2010-05-10 04:13:18 +02:00
|
|
|
|
g_dbus_connection_new_sync (GIOStream *stream,
|
|
|
|
|
const gchar *guid,
|
|
|
|
|
GDBusConnectionFlags flags,
|
2010-05-12 02:57:44 +02:00
|
|
|
|
GDBusAuthObserver *observer,
|
2010-05-10 04:13:18 +02:00
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GError **error)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
2016-10-31 08:36:18 +01:00
|
|
|
|
_g_dbus_initialize ();
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_return_val_if_fail (G_IS_IO_STREAM (stream), NULL);
|
2021-02-08 12:57:45 +01:00
|
|
|
|
g_return_val_if_fail ((flags & ~G_DBUS_CONNECTION_FLAGS_ALL) == 0, NULL);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
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,
|
2010-05-12 02:57:44 +02:00
|
|
|
|
"authentication-observer", observer,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_dbus_connection_new_for_address:
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @address: a D-Bus address
|
|
|
|
|
* @flags: flags describing how to make the connection
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @observer: (nullable): a #GDBusAuthObserver or %NULL
|
|
|
|
|
* @cancellable: (nullable): a #GCancellable or %NULL
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @callback: a #GAsyncReadyCallback to call when the request is satisfied
|
|
|
|
|
* @user_data: the data to pass to @callback
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
|
|
|
|
* Asynchronously connects and sets up a D-Bus client connection for
|
|
|
|
|
* exchanging D-Bus messages with an endpoint specified by @address
|
2017-02-08 16:06:00 +01:00
|
|
|
|
* which must be in the
|
|
|
|
|
* [D-Bus address format](https://dbus.freedesktop.org/doc/dbus-specification.html#addresses).
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
|
|
|
|
* 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
|
2020-12-15 14:00:28 +01:00
|
|
|
|
* %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER,
|
|
|
|
|
* %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS or
|
|
|
|
|
* %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_REQUIRE_SAME_USER flags.
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
|
|
|
|
* When the operation is finished, @callback will be invoked. You can
|
2019-07-16 01:02:09 +02:00
|
|
|
|
* then call g_dbus_connection_new_for_address_finish() to get the result of
|
|
|
|
|
* the operation.
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
2010-05-13 22:32:11 +02:00
|
|
|
|
* If @observer is not %NULL it may be used to control the
|
|
|
|
|
* authentication process.
|
|
|
|
|
*
|
2019-07-16 01:02:09 +02:00
|
|
|
|
* This is an asynchronous failable constructor. See
|
2010-05-06 20:13:59 +02:00
|
|
|
|
* g_dbus_connection_new_for_address_sync() for the synchronous
|
|
|
|
|
* version.
|
2010-05-06 22:02:08 +02:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.26
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*/
|
|
|
|
|
void
|
2010-05-10 04:13:18 +02:00
|
|
|
|
g_dbus_connection_new_for_address (const gchar *address,
|
|
|
|
|
GDBusConnectionFlags flags,
|
2010-05-13 22:32:11 +02:00
|
|
|
|
GDBusAuthObserver *observer,
|
2010-05-10 04:13:18 +02:00
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
|
gpointer user_data)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
2016-10-31 08:36:18 +01:00
|
|
|
|
_g_dbus_initialize ();
|
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_return_if_fail (address != NULL);
|
2021-02-08 12:57:45 +01:00
|
|
|
|
g_return_if_fail ((flags & ~G_DBUS_CONNECTION_FLAGS_ALL) == 0);
|
2016-10-31 08:36:18 +01:00
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_async_initable_new_async (G_TYPE_DBUS_CONNECTION,
|
|
|
|
|
G_PRIORITY_DEFAULT,
|
|
|
|
|
cancellable,
|
|
|
|
|
callback,
|
|
|
|
|
user_data,
|
|
|
|
|
"address", address,
|
|
|
|
|
"flags", flags,
|
2010-05-13 22:32:11 +02:00
|
|
|
|
"authentication-observer", observer,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_dbus_connection_new_for_address_finish:
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @res: a #GAsyncResult obtained from the #GAsyncReadyCallback passed
|
|
|
|
|
* to g_dbus_connection_new()
|
|
|
|
|
* @error: return location for error or %NULL
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
|
|
|
|
* Finishes an operation started with g_dbus_connection_new_for_address().
|
|
|
|
|
*
|
2020-12-15 10:13:22 +01:00
|
|
|
|
* Returns: (transfer full): a #GDBusConnection or %NULL if @error is set.
|
|
|
|
|
* Free with g_object_unref().
|
2010-05-06 22:02:08 +02:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.26
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*/
|
|
|
|
|
GDBusConnection *
|
2010-05-10 04:13:18 +02:00
|
|
|
|
g_dbus_connection_new_for_address_finish (GAsyncResult *res,
|
|
|
|
|
GError **error)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
|
|
|
|
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:
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @address: a D-Bus address
|
|
|
|
|
* @flags: flags describing how to make the connection
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @observer: (nullable): a #GDBusAuthObserver or %NULL
|
|
|
|
|
* @cancellable: (nullable): a #GCancellable or %NULL
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @error: return location for error or %NULL
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
|
|
|
|
* Synchronously connects and sets up a D-Bus client connection for
|
|
|
|
|
* exchanging D-Bus messages with an endpoint specified by @address
|
2017-02-08 16:06:00 +01:00
|
|
|
|
* which must be in the
|
|
|
|
|
* [D-Bus address format](https://dbus.freedesktop.org/doc/dbus-specification.html#addresses).
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
|
|
|
|
* 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
|
2020-12-15 14:00:28 +01:00
|
|
|
|
* %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER,
|
|
|
|
|
* %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS or
|
|
|
|
|
* %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_REQUIRE_SAME_USER flags.
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
|
|
|
|
* This is a synchronous failable constructor. See
|
|
|
|
|
* g_dbus_connection_new_for_address() for the asynchronous version.
|
|
|
|
|
*
|
2010-05-13 22:32:11 +02:00
|
|
|
|
* If @observer is not %NULL it may be used to control the
|
|
|
|
|
* authentication process.
|
|
|
|
|
*
|
2020-12-15 10:13:22 +01:00
|
|
|
|
* Returns: (transfer full): a #GDBusConnection or %NULL if @error is set.
|
|
|
|
|
* Free with g_object_unref().
|
2010-05-06 22:02:08 +02:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.26
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*/
|
|
|
|
|
GDBusConnection *
|
2010-05-10 04:13:18 +02:00
|
|
|
|
g_dbus_connection_new_for_address_sync (const gchar *address,
|
|
|
|
|
GDBusConnectionFlags flags,
|
2010-05-13 22:32:11 +02:00
|
|
|
|
GDBusAuthObserver *observer,
|
2010-05-10 04:13:18 +02:00
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GError **error)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
2016-10-31 08:36:18 +01:00
|
|
|
|
_g_dbus_initialize ();
|
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_return_val_if_fail (address != NULL, NULL);
|
2021-02-08 12:57:45 +01:00
|
|
|
|
g_return_val_if_fail ((flags & ~G_DBUS_CONNECTION_FLAGS_ALL) == 0, NULL);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
|
|
|
|
|
return g_initable_new (G_TYPE_DBUS_CONNECTION,
|
|
|
|
|
cancellable,
|
|
|
|
|
error,
|
|
|
|
|
"address", address,
|
|
|
|
|
"flags", flags,
|
2010-05-13 22:32:11 +02:00
|
|
|
|
"authentication-observer", observer,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_dbus_connection_set_exit_on_close:
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @connection: a #GDBusConnection
|
|
|
|
|
* @exit_on_close: whether the process should be terminated
|
|
|
|
|
* when @connection is closed by the remote peer
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
|
|
|
|
* Sets whether the process should be terminated when @connection is
|
|
|
|
|
* closed by the remote peer. See #GDBusConnection:exit-on-close for
|
|
|
|
|
* more details.
|
2010-05-06 22:02:08 +02:00
|
|
|
|
*
|
2011-04-07 23:14:36 +02:00
|
|
|
|
* Note that this function should be used with care. Most modern UNIX
|
2019-06-25 19:56:29 +02:00
|
|
|
|
* desktops tie the notion of a user session with the session bus, and expect
|
|
|
|
|
* all of a user's applications to quit when their bus connection goes away.
|
2011-04-07 23:14:36 +02:00
|
|
|
|
* 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.
|
|
|
|
|
*
|
2010-05-06 22:02:08 +02:00
|
|
|
|
* Since: 2.26
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
g_dbus_connection_set_exit_on_close (GDBusConnection *connection,
|
|
|
|
|
gboolean exit_on_close)
|
|
|
|
|
{
|
|
|
|
|
g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
|
2011-10-17 20:27:23 +02:00
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_dbus_connection_get_exit_on_close:
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @connection: a #GDBusConnection
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
|
|
|
|
* Gets whether the process is terminated when @connection is
|
|
|
|
|
* closed by the remote peer. See
|
|
|
|
|
* #GDBusConnection:exit-on-close for more details.
|
|
|
|
|
*
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* Returns: whether the process is terminated when @connection is
|
|
|
|
|
* closed by the remote peer
|
2010-05-06 22:02:08 +02:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.26
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*/
|
|
|
|
|
gboolean
|
|
|
|
|
g_dbus_connection_get_exit_on_close (GDBusConnection *connection)
|
|
|
|
|
{
|
|
|
|
|
g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
|
2011-10-17 20:27:23 +02:00
|
|
|
|
|
|
|
|
|
if (g_atomic_int_get (&connection->atomic_flags) & FLAG_EXIT_ON_CLOSE)
|
|
|
|
|
return TRUE;
|
|
|
|
|
else
|
|
|
|
|
return FALSE;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_dbus_connection_get_guid:
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @connection: a #GDBusConnection
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
|
|
|
|
* The GUID of the peer performing the role of server when
|
|
|
|
|
* authenticating. See #GDBusConnection:guid for more details.
|
|
|
|
|
*
|
2020-12-15 10:13:22 +01:00
|
|
|
|
* Returns: (not nullable): The GUID. Do not free this string, it is owned by
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @connection.
|
2010-05-06 22:02:08 +02:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.26
|
|
|
|
|
*/
|
2010-05-06 20:13:59 +02:00
|
|
|
|
const gchar *
|
|
|
|
|
g_dbus_connection_get_guid (GDBusConnection *connection)
|
|
|
|
|
{
|
|
|
|
|
g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
|
2010-07-07 22:35:17 +02:00
|
|
|
|
return connection->guid;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_dbus_connection_get_unique_name:
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @connection: a #GDBusConnection
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
2018-11-15 10:42:08 +01:00
|
|
|
|
* Returns: (nullable): the unique name or %NULL if @connection is not a message
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* bus connection. Do not free this string, it is owned by
|
|
|
|
|
* @connection.
|
2010-05-06 22:02:08 +02:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.26
|
|
|
|
|
*/
|
2010-05-06 20:13:59 +02:00
|
|
|
|
const gchar *
|
|
|
|
|
g_dbus_connection_get_unique_name (GDBusConnection *connection)
|
|
|
|
|
{
|
|
|
|
|
g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
|
2011-10-20 14:12:26 +02:00
|
|
|
|
|
|
|
|
|
/* do not use g_return_val_if_fail(), we want the memory barrier */
|
|
|
|
|
if (!check_initialized (connection))
|
|
|
|
|
return NULL;
|
|
|
|
|
|
2010-07-07 22:35:17 +02:00
|
|
|
|
return connection->bus_unique_name;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_dbus_connection_get_peer_credentials:
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @connection: a #GDBusConnection
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
2015-10-06 08:59:19 +02:00
|
|
|
|
* Returns: (transfer none) (nullable): a #GCredentials or %NULL if not
|
|
|
|
|
* available. Do not free this object, it is owned by @connection.
|
2010-05-06 22:02:08 +02:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.26
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*/
|
|
|
|
|
GCredentials *
|
|
|
|
|
g_dbus_connection_get_peer_credentials (GDBusConnection *connection)
|
|
|
|
|
{
|
|
|
|
|
g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
|
2011-10-20 14:12:26 +02:00
|
|
|
|
|
|
|
|
|
/* do not use g_return_val_if_fail(), we want the memory barrier */
|
|
|
|
|
if (!check_initialized (connection))
|
|
|
|
|
return NULL;
|
|
|
|
|
|
2010-07-31 05:44:30 +02:00
|
|
|
|
return connection->credentials;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
2020-11-11 19:24:28 +01:00
|
|
|
|
static guint _global_filter_id = 1; /* (atomic) */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_dbus_connection_add_filter:
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @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
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
|
|
|
|
* Adds a message filter. Filters are handlers that are run on all
|
2010-06-21 22:08:53 +02:00
|
|
|
|
* 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
|
2010-08-06 02:37:27 +02:00
|
|
|
|
* the message being processed. Filter functions are allowed to modify
|
2011-06-05 00:44:44 +02:00
|
|
|
|
* and even drop messages.
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
|
|
|
|
* 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(),
|
2011-06-05 00:44:44 +02:00
|
|
|
|
* g_dbus_connection_signal_subscribe() or g_dbus_connection_call() instead.
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
2010-08-06 02:37:27 +02:00
|
|
|
|
* 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
|
2010-06-21 22:08:53 +02:00
|
|
|
|
* g_dbus_connection_send_message_with_reply() relies on) will see the
|
2018-04-05 12:22:33 +02:00
|
|
|
|
* message. Similarly, if a filter consumes an outgoing message, the
|
2010-06-21 22:08:53 +02:00
|
|
|
|
* message will not be sent to the other peer.
|
|
|
|
|
*
|
2015-08-21 23:39:44 +02:00
|
|
|
|
* 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.)
|
|
|
|
|
*
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* Returns: a filter identifier that can be used with
|
|
|
|
|
* g_dbus_connection_remove_filter()
|
2010-05-06 22:02:08 +02:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.26
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*/
|
|
|
|
|
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);
|
2011-10-19 18:23:30 +02:00
|
|
|
|
g_return_val_if_fail (check_initialized (connection), 0);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
CONNECTION_LOCK (connection);
|
|
|
|
|
data = g_new0 (FilterData, 1);
|
2019-09-21 10:45:36 +02:00
|
|
|
|
data->id = (guint) g_atomic_int_add (&_global_filter_id, 1); /* TODO: overflow etc. */
|
2015-08-21 23:39:44 +02:00
|
|
|
|
data->ref_count = 1;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
data->filter_function = filter_function;
|
|
|
|
|
data->user_data = user_data;
|
|
|
|
|
data->user_data_free_func = user_data_free_func;
|
2015-08-21 23:39:44 +02:00
|
|
|
|
data->context = g_main_context_ref_thread_default ();
|
2010-07-07 22:35:17 +02:00
|
|
|
|
g_ptr_array_add (connection->filters, data);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
CONNECTION_UNLOCK (connection);
|
|
|
|
|
|
|
|
|
|
return data->id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* only called from finalize(), removes all filters */
|
|
|
|
|
static void
|
|
|
|
|
purge_all_filters (GDBusConnection *connection)
|
|
|
|
|
{
|
|
|
|
|
guint n;
|
2015-08-21 23:39:44 +02:00
|
|
|
|
|
2021-03-28 08:53:57 +02:00
|
|
|
|
for (n = 0; n < connection->filters->len; n++)
|
|
|
|
|
filter_data_destroy (connection->filters->pdata[n], FALSE);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
2010-05-12 04:50:40 +02:00
|
|
|
|
/**
|
|
|
|
|
* g_dbus_connection_remove_filter:
|
|
|
|
|
* @connection: a #GDBusConnection
|
2010-05-12 05:18:48 +02:00
|
|
|
|
* @filter_id: an identifier obtained from g_dbus_connection_add_filter()
|
2010-05-12 04:50:40 +02:00
|
|
|
|
*
|
|
|
|
|
* Removes a filter.
|
|
|
|
|
*
|
2015-08-21 23:39:44 +02:00
|
|
|
|
* 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.
|
|
|
|
|
*
|
2010-05-12 04:50:40 +02:00
|
|
|
|
* Since: 2.26
|
|
|
|
|
*/
|
2010-05-06 20:13:59 +02:00
|
|
|
|
void
|
2010-05-10 04:13:18 +02:00
|
|
|
|
g_dbus_connection_remove_filter (GDBusConnection *connection,
|
|
|
|
|
guint filter_id)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
|
|
|
|
guint n;
|
2017-03-01 11:18:21 +01:00
|
|
|
|
gboolean found;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
FilterData *to_destroy;
|
|
|
|
|
|
|
|
|
|
g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
|
2011-10-19 18:23:30 +02:00
|
|
|
|
g_return_if_fail (check_initialized (connection));
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
CONNECTION_LOCK (connection);
|
2017-03-01 11:18:21 +01:00
|
|
|
|
found = FALSE;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
to_destroy = NULL;
|
2010-07-07 22:35:17 +02:00
|
|
|
|
for (n = 0; n < connection->filters->len; n++)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
2010-07-07 22:35:17 +02:00
|
|
|
|
FilterData *data = connection->filters->pdata[n];
|
2010-05-06 20:13:59 +02:00
|
|
|
|
if (data->id == filter_id)
|
|
|
|
|
{
|
2017-03-01 11:18:21 +01:00
|
|
|
|
found = TRUE;
|
2010-07-07 22:35:17 +02:00
|
|
|
|
g_ptr_array_remove_index (connection->filters, n);
|
2015-08-21 23:39:44 +02:00
|
|
|
|
data->ref_count--;
|
|
|
|
|
if (data->ref_count == 0)
|
|
|
|
|
to_destroy = data;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
CONNECTION_UNLOCK (connection);
|
|
|
|
|
|
|
|
|
|
/* do free without holding lock */
|
|
|
|
|
if (to_destroy != NULL)
|
2021-03-28 08:53:57 +02:00
|
|
|
|
filter_data_destroy (to_destroy, TRUE);
|
2017-03-01 11:18:21 +01:00
|
|
|
|
else if (!found)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
|
|
|
|
g_warning ("g_dbus_connection_remove_filter: No filter found for filter_id %d", filter_id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
static gchar *
|
2013-04-08 08:13:10 +02:00
|
|
|
|
args_to_rule (const gchar *sender,
|
|
|
|
|
const gchar *interface_name,
|
|
|
|
|
const gchar *member,
|
|
|
|
|
const gchar *object_path,
|
|
|
|
|
const gchar *arg0,
|
|
|
|
|
GDBusSignalFlags flags)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
|
|
|
|
GString *rule;
|
|
|
|
|
|
|
|
|
|
rule = g_string_new ("type='signal'");
|
2013-04-08 08:13:10 +02:00
|
|
|
|
if (flags & G_DBUS_SIGNAL_FLAGS_NO_MATCH_RULE)
|
2010-11-06 03:07:07 +01:00
|
|
|
|
g_string_prepend_c (rule, '-');
|
2010-05-06 20:13:59 +02:00
|
|
|
|
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);
|
2013-04-08 08:13:10 +02:00
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
if (arg0 != NULL)
|
2013-04-08 08:13:10 +02:00
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
}
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
return g_string_free (rule, FALSE);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-11 19:24:28 +01:00
|
|
|
|
static guint _global_subscriber_id = 1; /* (atomic) */
|
|
|
|
|
static guint _global_registration_id = 1; /* (atomic) */
|
|
|
|
|
static guint _global_subtree_registration_id = 1; /* (atomic) */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
2011-10-17 14:27:03 +02:00
|
|
|
|
/* Called in a user thread, lock is held */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
static void
|
|
|
|
|
add_match_rule (GDBusConnection *connection,
|
|
|
|
|
const gchar *match_rule)
|
|
|
|
|
{
|
|
|
|
|
GError *error;
|
|
|
|
|
GDBusMessage *message;
|
|
|
|
|
|
2010-11-06 03:07:07 +01:00
|
|
|
|
if (match_rule[0] == '-')
|
|
|
|
|
return;
|
|
|
|
|
|
2024-05-13 13:24:16 +02:00
|
|
|
|
message = g_dbus_message_new_method_call (DBUS_SERVICE_DBUS,
|
|
|
|
|
DBUS_PATH_DBUS,
|
|
|
|
|
DBUS_INTERFACE_DBUS,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
"AddMatch");
|
|
|
|
|
g_dbus_message_set_body (message, g_variant_new ("(s)", match_rule));
|
|
|
|
|
error = NULL;
|
|
|
|
|
if (!g_dbus_connection_send_message_unlocked (connection,
|
|
|
|
|
message,
|
2010-07-19 22:07:57 +02:00
|
|
|
|
G_DBUS_SEND_MESSAGE_FLAGS_NONE,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
NULL,
|
|
|
|
|
&error))
|
|
|
|
|
{
|
|
|
|
|
g_critical ("Error while sending AddMatch() message: %s", error->message);
|
|
|
|
|
g_error_free (error);
|
|
|
|
|
}
|
|
|
|
|
g_object_unref (message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
2011-10-17 14:27:03 +02:00
|
|
|
|
/* Called in a user thread, lock is held */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
static void
|
|
|
|
|
remove_match_rule (GDBusConnection *connection,
|
|
|
|
|
const gchar *match_rule)
|
|
|
|
|
{
|
|
|
|
|
GError *error;
|
|
|
|
|
GDBusMessage *message;
|
|
|
|
|
|
2010-11-06 03:07:07 +01:00
|
|
|
|
if (match_rule[0] == '-')
|
|
|
|
|
return;
|
|
|
|
|
|
2024-05-13 13:24:16 +02:00
|
|
|
|
message = g_dbus_message_new_method_call (DBUS_SERVICE_DBUS,
|
|
|
|
|
DBUS_PATH_DBUS,
|
|
|
|
|
DBUS_INTERFACE_DBUS,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
"RemoveMatch");
|
|
|
|
|
g_dbus_message_set_body (message, g_variant_new ("(s)", match_rule));
|
|
|
|
|
|
|
|
|
|
error = NULL;
|
|
|
|
|
if (!g_dbus_connection_send_message_unlocked (connection,
|
|
|
|
|
message,
|
2010-07-19 22:07:57 +02:00
|
|
|
|
G_DBUS_SEND_MESSAGE_FLAGS_NONE,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
NULL,
|
|
|
|
|
&error))
|
|
|
|
|
{
|
GDBusConnection: make the closed flag atomic (but still lock to write)
Strictly speaking, neither of the two uses that aren't under the lock
*needs* to be atomic, but it seems better to be obviously correct (and
we save another 4 bytes of struct).
One of these uses is in g_dbus_connection_is_closed(), any use of which
is inherently a race condition anyway.
The other is g_dbus_connection_flush_sync, which as far as I can tell
just needs a best-effort check, to not waste effort on a connection that
has been closed for a while (but I could be wrong).
I removed the check for the closed flag altogether in
g_dbus_connection_send_message_with_reply_unlocked, because it turns out
to be redundant with one in g_dbus_connection_send_message_unlocked,
which is called immediately after.
g_dbus_connection_close_sync held the lock to check the closed flag,
which is no longer needed.
As far as I can tell, the only reason why the lock is still desirable
when setting the closed flag is so that remove_match_rule can't fail
by racing with close notification from the worker thread - but
on_worker_closed needs to hold the lock anyway, to deal with other
data structures, so there's no point in trying to eliminate the
requirement to hold the lock.
Bug: https://bugzilla.gnome.org/show_bug.cgi?id=661992
Signed-off-by: Simon McVittie <simon.mcvittie@collabora.co.uk>
Reviewed-by: David Zeuthen <davidz@redhat.com>
2011-10-21 16:46:00 +02:00
|
|
|
|
/* 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.
|
|
|
|
|
*/
|
2010-05-06 20:13:59 +02:00
|
|
|
|
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)
|
|
|
|
|
{
|
2024-05-13 13:50:42 +02:00
|
|
|
|
return g_strcmp0 (signal_data->sender, DBUS_SERVICE_DBUS) == 0 &&
|
|
|
|
|
g_strcmp0 (signal_data->interface_name, DBUS_INTERFACE_DBUS) == 0 &&
|
|
|
|
|
g_strcmp0 (signal_data->object_path, DBUS_PATH_DBUS) == 0 &&
|
2010-05-10 04:13:18 +02:00
|
|
|
|
(g_strcmp0 (signal_data->member, "NameLost") == 0 ||
|
|
|
|
|
g_strcmp0 (signal_data->member, "NameAcquired") == 0);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
2024-04-23 21:31:57 +02:00
|
|
|
|
/* called in any thread, connection lock is held */
|
|
|
|
|
static void
|
|
|
|
|
add_signal_data (GDBusConnection *connection,
|
2024-04-23 21:39:05 +02:00
|
|
|
|
SignalData *signal_data,
|
|
|
|
|
const char *sender_unique_name)
|
2024-04-23 21:31:57 +02:00
|
|
|
|
{
|
|
|
|
|
GPtrArray *signal_data_array;
|
|
|
|
|
|
|
|
|
|
g_hash_table_insert (connection->map_rule_to_signal_data,
|
|
|
|
|
signal_data->rule,
|
|
|
|
|
signal_data);
|
|
|
|
|
|
|
|
|
|
/* Add the match rule to the bus...
|
|
|
|
|
*
|
|
|
|
|
* Avoid adding match rules for NameLost and NameAcquired messages - the bus will
|
|
|
|
|
* always send such messages to us.
|
|
|
|
|
*/
|
|
|
|
|
if (connection->flags & G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION)
|
|
|
|
|
{
|
|
|
|
|
if (!is_signal_data_for_name_lost_or_acquired (signal_data))
|
|
|
|
|
add_match_rule (connection, signal_data->rule);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
signal_data_array = g_hash_table_lookup (connection->map_sender_unique_name_to_signal_data_array,
|
2024-04-23 21:39:05 +02:00
|
|
|
|
sender_unique_name);
|
2024-04-23 21:31:57 +02:00
|
|
|
|
if (signal_data_array == NULL)
|
|
|
|
|
{
|
|
|
|
|
signal_data_array = g_ptr_array_new ();
|
|
|
|
|
g_hash_table_insert (connection->map_sender_unique_name_to_signal_data_array,
|
2024-04-23 21:39:05 +02:00
|
|
|
|
g_strdup (sender_unique_name),
|
2024-04-23 21:31:57 +02:00
|
|
|
|
signal_data_array);
|
|
|
|
|
}
|
|
|
|
|
g_ptr_array_add (signal_data_array, signal_data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
/**
|
|
|
|
|
* g_dbus_connection_signal_subscribe:
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @connection: a #GDBusConnection
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @sender: (nullable): sender name to match on (unique or well-known name)
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* or %NULL to listen from all senders
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @interface_name: (nullable): D-Bus interface name to match on or %NULL to
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* match on all interfaces
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @member: (nullable): D-Bus signal name to match on or %NULL to match on
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* all signals
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @object_path: (nullable): object path to match on or %NULL to match on
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* all object paths
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @arg0: (nullable): contents of first string argument to match on or %NULL
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* to match on all kinds of arguments
|
2015-07-21 08:39:16 +02:00
|
|
|
|
* @flags: #GDBusSignalFlags describing how arg0 is used in subscribing to the
|
|
|
|
|
* signal
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @callback: callback to invoke when there is a signal matching the requested data
|
|
|
|
|
* @user_data: user data to pass to @callback
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @user_data_free_func: (nullable): function to free @user_data with when
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* subscription is removed or %NULL
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
2022-11-26 17:37:45 +01:00
|
|
|
|
* Subscribes to signals on @connection and invokes @callback whenever
|
2014-02-08 18:26:56 +01:00
|
|
|
|
* 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.
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
2010-06-11 21:45:18 +02:00
|
|
|
|
* If @connection is not a message bus connection, @sender must be
|
|
|
|
|
* %NULL.
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
2010-06-11 21:45:18 +02:00
|
|
|
|
* 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.
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
2013-04-08 08:13:10 +02:00
|
|
|
|
* 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.
|
|
|
|
|
*
|
2015-08-21 23:39:44 +02:00
|
|
|
|
* 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.)
|
|
|
|
|
*
|
2020-01-20 12:37:48 +01:00
|
|
|
|
* As @callback is potentially invoked in a different thread from where it’s
|
|
|
|
|
* emitted, it’s possible for this to happen after
|
|
|
|
|
* g_dbus_connection_signal_unsubscribe() has been called in another thread.
|
|
|
|
|
* Due to this, @user_data should have a strong reference which is freed with
|
|
|
|
|
* @user_data_free_func, rather than pointing to data whose lifecycle is tied
|
|
|
|
|
* to the signal subscription. For example, if a #GObject is used to store the
|
|
|
|
|
* subscription ID from g_dbus_connection_signal_subscribe(), a strong reference
|
|
|
|
|
* to that #GObject must be passed to @user_data, and g_object_unref() passed to
|
|
|
|
|
* @user_data_free_func. You are responsible for breaking the resulting
|
|
|
|
|
* reference count cycle by explicitly unsubscribing from the signal when
|
|
|
|
|
* dropping the last external reference to the #GObject. Alternatively, a weak
|
|
|
|
|
* reference may be used.
|
|
|
|
|
*
|
|
|
|
|
* It is guaranteed that if you unsubscribe from a signal using
|
|
|
|
|
* g_dbus_connection_signal_unsubscribe() from the same thread which made the
|
|
|
|
|
* corresponding g_dbus_connection_signal_subscribe() call, @callback will not
|
|
|
|
|
* be invoked after g_dbus_connection_signal_unsubscribe() returns.
|
|
|
|
|
*
|
2018-11-13 13:44:34 +01:00
|
|
|
|
* The returned subscription identifier is an opaque value which is guaranteed
|
|
|
|
|
* to never be zero.
|
|
|
|
|
*
|
|
|
|
|
* This function can never fail.
|
|
|
|
|
*
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* Returns: a subscription identifier that can be used with g_dbus_connection_signal_unsubscribe()
|
2010-05-06 22:02:08 +02:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.26
|
|
|
|
|
*/
|
2010-05-06 20:13:59 +02:00
|
|
|
|
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,
|
2010-07-19 21:03:00 +02:00
|
|
|
|
GDBusSignalFlags flags,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
GDBusSignalCallback callback,
|
|
|
|
|
gpointer user_data,
|
|
|
|
|
GDestroyNotify user_data_free_func)
|
|
|
|
|
{
|
|
|
|
|
gchar *rule;
|
|
|
|
|
SignalData *signal_data;
|
2024-04-23 21:42:17 +02:00
|
|
|
|
SignalData *name_watcher = NULL;
|
gdbusconnection: Allocate SignalSubscriber structs individually
The `SignalSubscriber` structs contain the callback and `user_data` of each
subscriber to a signal, along with the `guint id` token held by that
subscriber to identify their subscription. There are one or more
`SignalSubscriber` structs for a given signal match rule, which is
represented as a `SignalData` struct.
Previously, the `SignalSubscriber` structs were stored in a `GArray` in
the `SignalData` struct, to reduce the number of allocations needed
when subscribing to a signal.
However, this means that a `SignalSubscriber` struct cannot have a
lifetime which exceeds the `SignalData` which contains it. In order to
fix the race in #978, one thread needs to be able to unsubscribe from a
signal (destroying the `SignalData` struct) while zero or more other
threads are in the process of calling the callbacks from a previous
emission of that signal (using the callback and `user_data` from zero or
more `SignalSubscriber` structs). Multiple threads could be calling
callbacks because callbacks are invoked in the `GMainContext` which
originally made a subscription, and GDBus supports subscribing to a
signal from multiple threads. In that case, the callbacks are dispatched
to multiple threads.
In order to allow the `SignalSubscriber` structs to outlive the
`SignalData` which contained their old match rule, store them in a
`GPtrArray` in the `SignalData` struct, and refcount them individually.
This commit in itself should make no functional changes to how GDBus
works, but will allow following commits to do so.
Signed-off-by: Philip Withnall <withnall@endlessm.com>
Helps: #978
2020-01-17 17:11:06 +01:00
|
|
|
|
SignalSubscriber *subscriber;
|
2024-04-23 21:39:05 +02:00
|
|
|
|
gboolean sender_is_its_own_owner;
|
2010-06-11 21:45:18 +02:00
|
|
|
|
const gchar *sender_unique_name;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
/* 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);
|
2010-07-07 22:35:17 +02:00
|
|
|
|
g_return_val_if_fail (sender == NULL || (g_dbus_is_name (sender) && (connection->flags & G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION)), 0);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
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);
|
2011-10-19 18:23:30 +02:00
|
|
|
|
g_return_val_if_fail (check_initialized (connection), 0);
|
2013-04-08 08:13:10 +02:00
|
|
|
|
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);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
CONNECTION_LOCK (connection);
|
|
|
|
|
|
2010-11-06 03:07:07 +01:00
|
|
|
|
/* 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).
|
|
|
|
|
*/
|
2013-04-08 08:13:10 +02:00
|
|
|
|
rule = args_to_rule (sender, interface_name, member, object_path, arg0, flags);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2024-05-13 13:50:42 +02:00
|
|
|
|
if (sender != NULL && (g_dbus_is_unique_name (sender) || g_strcmp0 (sender, DBUS_SERVICE_DBUS) == 0))
|
2024-04-23 21:39:05 +02:00
|
|
|
|
sender_is_its_own_owner = TRUE;
|
|
|
|
|
else
|
|
|
|
|
sender_is_its_own_owner = FALSE;
|
|
|
|
|
|
|
|
|
|
if (sender_is_its_own_owner)
|
2010-06-11 21:45:18 +02:00
|
|
|
|
sender_unique_name = sender;
|
|
|
|
|
else
|
|
|
|
|
sender_unique_name = "";
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
gdbusconnection: Allocate SignalSubscriber structs individually
The `SignalSubscriber` structs contain the callback and `user_data` of each
subscriber to a signal, along with the `guint id` token held by that
subscriber to identify their subscription. There are one or more
`SignalSubscriber` structs for a given signal match rule, which is
represented as a `SignalData` struct.
Previously, the `SignalSubscriber` structs were stored in a `GArray` in
the `SignalData` struct, to reduce the number of allocations needed
when subscribing to a signal.
However, this means that a `SignalSubscriber` struct cannot have a
lifetime which exceeds the `SignalData` which contains it. In order to
fix the race in #978, one thread needs to be able to unsubscribe from a
signal (destroying the `SignalData` struct) while zero or more other
threads are in the process of calling the callbacks from a previous
emission of that signal (using the callback and `user_data` from zero or
more `SignalSubscriber` structs). Multiple threads could be calling
callbacks because callbacks are invoked in the `GMainContext` which
originally made a subscription, and GDBus supports subscribing to a
signal from multiple threads. In that case, the callbacks are dispatched
to multiple threads.
In order to allow the `SignalSubscriber` structs to outlive the
`SignalData` which contained their old match rule, store them in a
`GPtrArray` in the `SignalData` struct, and refcount them individually.
This commit in itself should make no functional changes to how GDBus
works, but will allow following commits to do so.
Signed-off-by: Philip Withnall <withnall@endlessm.com>
Helps: #978
2020-01-17 17:11:06 +01:00
|
|
|
|
subscriber = g_new0 (SignalSubscriber, 1);
|
|
|
|
|
subscriber->ref_count = 1;
|
|
|
|
|
subscriber->callback = callback;
|
|
|
|
|
subscriber->user_data = user_data;
|
|
|
|
|
subscriber->user_data_free_func = user_data_free_func;
|
|
|
|
|
subscriber->id = (guint) g_atomic_int_add (&_global_subscriber_id, 1); /* TODO: overflow etc. */
|
|
|
|
|
subscriber->context = g_main_context_ref_thread_default ();
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
/* see if we've already have this rule */
|
2010-07-07 22:35:17 +02:00
|
|
|
|
signal_data = g_hash_table_lookup (connection->map_rule_to_signal_data, rule);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
if (signal_data != NULL)
|
|
|
|
|
{
|
gdbusconnection: Allocate SignalSubscriber structs individually
The `SignalSubscriber` structs contain the callback and `user_data` of each
subscriber to a signal, along with the `guint id` token held by that
subscriber to identify their subscription. There are one or more
`SignalSubscriber` structs for a given signal match rule, which is
represented as a `SignalData` struct.
Previously, the `SignalSubscriber` structs were stored in a `GArray` in
the `SignalData` struct, to reduce the number of allocations needed
when subscribing to a signal.
However, this means that a `SignalSubscriber` struct cannot have a
lifetime which exceeds the `SignalData` which contains it. In order to
fix the race in #978, one thread needs to be able to unsubscribe from a
signal (destroying the `SignalData` struct) while zero or more other
threads are in the process of calling the callbacks from a previous
emission of that signal (using the callback and `user_data` from zero or
more `SignalSubscriber` structs). Multiple threads could be calling
callbacks because callbacks are invoked in the `GMainContext` which
originally made a subscription, and GDBus supports subscribing to a
signal from multiple threads. In that case, the callbacks are dispatched
to multiple threads.
In order to allow the `SignalSubscriber` structs to outlive the
`SignalData` which contained their old match rule, store them in a
`GPtrArray` in the `SignalData` struct, and refcount them individually.
This commit in itself should make no functional changes to how GDBus
works, but will allow following commits to do so.
Signed-off-by: Philip Withnall <withnall@endlessm.com>
Helps: #978
2020-01-17 17:11:06 +01:00
|
|
|
|
g_ptr_array_add (signal_data->subscribers, subscriber);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_free (rule);
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-14 20:30:12 +01:00
|
|
|
|
signal_data = signal_data_new_take (g_steal_pointer (&rule),
|
|
|
|
|
g_strdup (sender),
|
|
|
|
|
g_strdup (interface_name),
|
|
|
|
|
g_strdup (member),
|
|
|
|
|
g_strdup (object_path),
|
|
|
|
|
g_strdup (arg0),
|
|
|
|
|
flags);
|
gdbusconnection: Allocate SignalSubscriber structs individually
The `SignalSubscriber` structs contain the callback and `user_data` of each
subscriber to a signal, along with the `guint id` token held by that
subscriber to identify their subscription. There are one or more
`SignalSubscriber` structs for a given signal match rule, which is
represented as a `SignalData` struct.
Previously, the `SignalSubscriber` structs were stored in a `GArray` in
the `SignalData` struct, to reduce the number of allocations needed
when subscribing to a signal.
However, this means that a `SignalSubscriber` struct cannot have a
lifetime which exceeds the `SignalData` which contains it. In order to
fix the race in #978, one thread needs to be able to unsubscribe from a
signal (destroying the `SignalData` struct) while zero or more other
threads are in the process of calling the callbacks from a previous
emission of that signal (using the callback and `user_data` from zero or
more `SignalSubscriber` structs). Multiple threads could be calling
callbacks because callbacks are invoked in the `GMainContext` which
originally made a subscription, and GDBus supports subscribing to a
signal from multiple threads. In that case, the callbacks are dispatched
to multiple threads.
In order to allow the `SignalSubscriber` structs to outlive the
`SignalData` which contained their old match rule, store them in a
`GPtrArray` in the `SignalData` struct, and refcount them individually.
This commit in itself should make no functional changes to how GDBus
works, but will allow following commits to do so.
Signed-off-by: Philip Withnall <withnall@endlessm.com>
Helps: #978
2020-01-17 17:11:06 +01:00
|
|
|
|
g_ptr_array_add (signal_data->subscribers, subscriber);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2024-04-23 21:42:17 +02:00
|
|
|
|
/* If subscribing to a signal from a specific sender with a well-known
|
|
|
|
|
* name, we must first subscribe to NameOwnerChanged signals for that
|
|
|
|
|
* well-known name, so that we can match the current owner of the name
|
|
|
|
|
* against the sender of each signal. */
|
|
|
|
|
if (sender != NULL && !sender_is_its_own_owner)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
2024-04-23 21:42:17 +02:00
|
|
|
|
gchar *name_owner_rule = NULL;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2024-04-23 21:42:17 +02:00
|
|
|
|
/* We already checked that sender != NULL implies MESSAGE_BUS_CONNECTION */
|
|
|
|
|
g_assert (connection->flags & G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION);
|
|
|
|
|
|
|
|
|
|
name_owner_rule = args_to_rule (DBUS_SERVICE_DBUS,
|
|
|
|
|
DBUS_INTERFACE_DBUS,
|
|
|
|
|
"NameOwnerChanged",
|
|
|
|
|
DBUS_PATH_DBUS,
|
|
|
|
|
sender,
|
|
|
|
|
G_DBUS_SIGNAL_FLAGS_NONE);
|
|
|
|
|
name_watcher = g_hash_table_lookup (connection->map_rule_to_signal_data, name_owner_rule);
|
|
|
|
|
|
|
|
|
|
if (name_watcher == NULL)
|
|
|
|
|
{
|
|
|
|
|
name_watcher = signal_data_new_take (g_steal_pointer (&name_owner_rule),
|
|
|
|
|
g_strdup (DBUS_SERVICE_DBUS),
|
|
|
|
|
g_strdup (DBUS_INTERFACE_DBUS),
|
|
|
|
|
g_strdup ("NameOwnerChanged"),
|
|
|
|
|
g_strdup (DBUS_PATH_DBUS),
|
|
|
|
|
g_strdup (sender),
|
|
|
|
|
G_DBUS_SIGNAL_FLAGS_NONE);
|
|
|
|
|
add_signal_data (connection, name_watcher, DBUS_SERVICE_DBUS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (name_watcher->watched_name == NULL)
|
|
|
|
|
{
|
|
|
|
|
name_watcher->watched_name = watched_name_new ();
|
|
|
|
|
name_watcher_call_get_name_owner_unlocked (connection, name_watcher);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
g_ref_count_inc (&name_watcher->watched_name->ref_count);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
signal_data->shared_name_watcher = name_watcher;
|
|
|
|
|
|
|
|
|
|
g_clear_pointer (&name_owner_rule, g_free);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
2024-04-23 21:42:17 +02:00
|
|
|
|
|
2024-04-23 21:39:05 +02:00
|
|
|
|
add_signal_data (connection, signal_data, sender_unique_name);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2010-06-11 21:45:18 +02:00
|
|
|
|
out:
|
2010-07-07 22:35:17 +02:00
|
|
|
|
g_hash_table_insert (connection->map_id_to_signal_data,
|
gdbusconnection: Allocate SignalSubscriber structs individually
The `SignalSubscriber` structs contain the callback and `user_data` of each
subscriber to a signal, along with the `guint id` token held by that
subscriber to identify their subscription. There are one or more
`SignalSubscriber` structs for a given signal match rule, which is
represented as a `SignalData` struct.
Previously, the `SignalSubscriber` structs were stored in a `GArray` in
the `SignalData` struct, to reduce the number of allocations needed
when subscribing to a signal.
However, this means that a `SignalSubscriber` struct cannot have a
lifetime which exceeds the `SignalData` which contains it. In order to
fix the race in #978, one thread needs to be able to unsubscribe from a
signal (destroying the `SignalData` struct) while zero or more other
threads are in the process of calling the callbacks from a previous
emission of that signal (using the callback and `user_data` from zero or
more `SignalSubscriber` structs). Multiple threads could be calling
callbacks because callbacks are invoked in the `GMainContext` which
originally made a subscription, and GDBus supports subscribing to a
signal from multiple threads. In that case, the callbacks are dispatched
to multiple threads.
In order to allow the `SignalSubscriber` structs to outlive the
`SignalData` which contained their old match rule, store them in a
`GPtrArray` in the `SignalData` struct, and refcount them individually.
This commit in itself should make no functional changes to how GDBus
works, but will allow following commits to do so.
Signed-off-by: Philip Withnall <withnall@endlessm.com>
Helps: #978
2020-01-17 17:11:06 +01:00
|
|
|
|
GUINT_TO_POINTER (subscriber->id),
|
2010-06-11 21:45:18 +02:00
|
|
|
|
signal_data);
|
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
CONNECTION_UNLOCK (connection);
|
|
|
|
|
|
gdbusconnection: Allocate SignalSubscriber structs individually
The `SignalSubscriber` structs contain the callback and `user_data` of each
subscriber to a signal, along with the `guint id` token held by that
subscriber to identify their subscription. There are one or more
`SignalSubscriber` structs for a given signal match rule, which is
represented as a `SignalData` struct.
Previously, the `SignalSubscriber` structs were stored in a `GArray` in
the `SignalData` struct, to reduce the number of allocations needed
when subscribing to a signal.
However, this means that a `SignalSubscriber` struct cannot have a
lifetime which exceeds the `SignalData` which contains it. In order to
fix the race in #978, one thread needs to be able to unsubscribe from a
signal (destroying the `SignalData` struct) while zero or more other
threads are in the process of calling the callbacks from a previous
emission of that signal (using the callback and `user_data` from zero or
more `SignalSubscriber` structs). Multiple threads could be calling
callbacks because callbacks are invoked in the `GMainContext` which
originally made a subscription, and GDBus supports subscribing to a
signal from multiple threads. In that case, the callbacks are dispatched
to multiple threads.
In order to allow the `SignalSubscriber` structs to outlive the
`SignalData` which contained their old match rule, store them in a
`GPtrArray` in the `SignalData` struct, and refcount them individually.
This commit in itself should make no functional changes to how GDBus
works, but will allow following commits to do so.
Signed-off-by: Philip Withnall <withnall@endlessm.com>
Helps: #978
2020-01-17 17:11:06 +01:00
|
|
|
|
return subscriber->id;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
2024-03-14 20:51:59 +01:00
|
|
|
|
/*
|
|
|
|
|
* Called in any thread.
|
|
|
|
|
* Must hold the connection lock when calling this, unless
|
|
|
|
|
* connection->finalizing is TRUE.
|
|
|
|
|
* May free signal_data, so do not dereference it after this.
|
|
|
|
|
*/
|
|
|
|
|
static void
|
|
|
|
|
remove_signal_data_if_unused (GDBusConnection *connection,
|
|
|
|
|
SignalData *signal_data)
|
|
|
|
|
{
|
2024-04-23 21:39:05 +02:00
|
|
|
|
const gchar *sender_unique_name;
|
2024-03-14 20:51:59 +01:00
|
|
|
|
GPtrArray *signal_data_array;
|
|
|
|
|
|
2024-04-23 21:42:17 +02:00
|
|
|
|
/* Cannot remove while there are still subscribers */
|
2024-03-14 20:51:59 +01:00
|
|
|
|
if (signal_data->subscribers->len != 0)
|
|
|
|
|
return;
|
|
|
|
|
|
2024-04-23 21:42:17 +02:00
|
|
|
|
/* Cannot remove while another SignalData is still using this one
|
|
|
|
|
* as its shared_name_watcher, which holds watched_name->ref_count > 0 */
|
|
|
|
|
if (signal_data->watched_name != NULL)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
/* Point of no return: we have committed to removing it */
|
|
|
|
|
|
|
|
|
|
if (signal_data->sender != NULL && signal_data->shared_name_watcher == NULL)
|
2024-04-23 21:39:05 +02:00
|
|
|
|
sender_unique_name = signal_data->sender;
|
|
|
|
|
else
|
|
|
|
|
sender_unique_name = "";
|
|
|
|
|
|
2024-03-14 20:51:59 +01:00
|
|
|
|
g_warn_if_fail (g_hash_table_remove (connection->map_rule_to_signal_data, signal_data->rule));
|
|
|
|
|
|
|
|
|
|
signal_data_array = g_hash_table_lookup (connection->map_sender_unique_name_to_signal_data_array,
|
2024-04-23 21:39:05 +02:00
|
|
|
|
sender_unique_name);
|
2024-03-14 20:51:59 +01:00
|
|
|
|
g_warn_if_fail (signal_data_array != NULL);
|
|
|
|
|
g_warn_if_fail (g_ptr_array_remove (signal_data_array, signal_data));
|
|
|
|
|
|
|
|
|
|
if (signal_data_array->len == 0)
|
|
|
|
|
{
|
|
|
|
|
g_warn_if_fail (g_hash_table_remove (connection->map_sender_unique_name_to_signal_data_array,
|
2024-04-23 21:39:05 +02:00
|
|
|
|
sender_unique_name));
|
2024-03-14 20:51:59 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* remove the match rule from the bus unless NameLost or NameAcquired (see subscribe()) */
|
|
|
|
|
if ((connection->flags & G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION) &&
|
|
|
|
|
!is_signal_data_for_name_lost_or_acquired (signal_data) &&
|
|
|
|
|
!g_dbus_connection_is_closed (connection) &&
|
|
|
|
|
!connection->finalizing)
|
|
|
|
|
{
|
|
|
|
|
/* The check for g_dbus_connection_is_closed() means that
|
|
|
|
|
* sending the RemoveMatch message can't fail with
|
|
|
|
|
* G_IO_ERROR_CLOSED, because we're holding the lock,
|
|
|
|
|
* so on_worker_closed() can't happen between the check we just
|
|
|
|
|
* did, and releasing the lock later.
|
|
|
|
|
*/
|
|
|
|
|
remove_match_rule (connection, signal_data->rule);
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-23 21:42:17 +02:00
|
|
|
|
if (signal_data->shared_name_watcher != NULL)
|
|
|
|
|
{
|
|
|
|
|
SignalData *name_watcher = g_steal_pointer (&signal_data->shared_name_watcher);
|
|
|
|
|
|
|
|
|
|
name_watcher_unref_watched_name (connection, name_watcher);
|
|
|
|
|
/* May free signal_data */
|
|
|
|
|
remove_signal_data_if_unused (connection, name_watcher);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-14 20:51:59 +01:00
|
|
|
|
signal_data_free (signal_data);
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-17 14:27:03 +02:00
|
|
|
|
/* called in any thread */
|
2020-01-17 20:56:58 +01:00
|
|
|
|
/* must hold lock when calling this (except if connection->finalizing is TRUE)
|
|
|
|
|
* returns the number of removed subscribers */
|
|
|
|
|
static guint
|
2010-05-10 04:13:18 +02:00
|
|
|
|
unsubscribe_id_internal (GDBusConnection *connection,
|
2020-01-17 20:56:58 +01:00
|
|
|
|
guint subscription_id)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
|
|
|
|
SignalData *signal_data;
|
|
|
|
|
guint n;
|
2020-01-17 20:56:58 +01:00
|
|
|
|
guint n_removed = 0;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2010-07-07 22:35:17 +02:00
|
|
|
|
signal_data = g_hash_table_lookup (connection->map_id_to_signal_data,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
GUINT_TO_POINTER (subscription_id));
|
|
|
|
|
if (signal_data == NULL)
|
|
|
|
|
{
|
|
|
|
|
/* Don't warn here, we may have thrown all subscriptions out when the connection was closed */
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (n = 0; n < signal_data->subscribers->len; n++)
|
|
|
|
|
{
|
gdbusconnection: Allocate SignalSubscriber structs individually
The `SignalSubscriber` structs contain the callback and `user_data` of each
subscriber to a signal, along with the `guint id` token held by that
subscriber to identify their subscription. There are one or more
`SignalSubscriber` structs for a given signal match rule, which is
represented as a `SignalData` struct.
Previously, the `SignalSubscriber` structs were stored in a `GArray` in
the `SignalData` struct, to reduce the number of allocations needed
when subscribing to a signal.
However, this means that a `SignalSubscriber` struct cannot have a
lifetime which exceeds the `SignalData` which contains it. In order to
fix the race in #978, one thread needs to be able to unsubscribe from a
signal (destroying the `SignalData` struct) while zero or more other
threads are in the process of calling the callbacks from a previous
emission of that signal (using the callback and `user_data` from zero or
more `SignalSubscriber` structs). Multiple threads could be calling
callbacks because callbacks are invoked in the `GMainContext` which
originally made a subscription, and GDBus supports subscribing to a
signal from multiple threads. In that case, the callbacks are dispatched
to multiple threads.
In order to allow the `SignalSubscriber` structs to outlive the
`SignalData` which contained their old match rule, store them in a
`GPtrArray` in the `SignalData` struct, and refcount them individually.
This commit in itself should make no functional changes to how GDBus
works, but will allow following commits to do so.
Signed-off-by: Philip Withnall <withnall@endlessm.com>
Helps: #978
2020-01-17 17:11:06 +01:00
|
|
|
|
SignalSubscriber *subscriber = signal_data->subscribers->pdata[n];
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
if (subscriber->id != subscription_id)
|
|
|
|
|
continue;
|
|
|
|
|
|
gdbusconnection: Allocate SignalSubscriber structs individually
The `SignalSubscriber` structs contain the callback and `user_data` of each
subscriber to a signal, along with the `guint id` token held by that
subscriber to identify their subscription. There are one or more
`SignalSubscriber` structs for a given signal match rule, which is
represented as a `SignalData` struct.
Previously, the `SignalSubscriber` structs were stored in a `GArray` in
the `SignalData` struct, to reduce the number of allocations needed
when subscribing to a signal.
However, this means that a `SignalSubscriber` struct cannot have a
lifetime which exceeds the `SignalData` which contains it. In order to
fix the race in #978, one thread needs to be able to unsubscribe from a
signal (destroying the `SignalData` struct) while zero or more other
threads are in the process of calling the callbacks from a previous
emission of that signal (using the callback and `user_data` from zero or
more `SignalSubscriber` structs). Multiple threads could be calling
callbacks because callbacks are invoked in the `GMainContext` which
originally made a subscription, and GDBus supports subscribing to a
signal from multiple threads. In that case, the callbacks are dispatched
to multiple threads.
In order to allow the `SignalSubscriber` structs to outlive the
`SignalData` which contained their old match rule, store them in a
`GPtrArray` in the `SignalData` struct, and refcount them individually.
This commit in itself should make no functional changes to how GDBus
works, but will allow following commits to do so.
Signed-off-by: Philip Withnall <withnall@endlessm.com>
Helps: #978
2020-01-17 17:11:06 +01:00
|
|
|
|
/* It’s OK to rearrange the array order using the ‘fast’ #GPtrArray
|
|
|
|
|
* removal functions, since we’re going to exit the loop below anyway — we
|
|
|
|
|
* never move on to the next element. Secondly, subscription IDs are
|
|
|
|
|
* guaranteed to be unique. */
|
2010-07-07 22:35:17 +02:00
|
|
|
|
g_warn_if_fail (g_hash_table_remove (connection->map_id_to_signal_data,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
GUINT_TO_POINTER (subscription_id)));
|
2020-01-17 20:56:58 +01:00
|
|
|
|
n_removed++;
|
gdbusconnection: Allocate SignalSubscriber structs individually
The `SignalSubscriber` structs contain the callback and `user_data` of each
subscriber to a signal, along with the `guint id` token held by that
subscriber to identify their subscription. There are one or more
`SignalSubscriber` structs for a given signal match rule, which is
represented as a `SignalData` struct.
Previously, the `SignalSubscriber` structs were stored in a `GArray` in
the `SignalData` struct, to reduce the number of allocations needed
when subscribing to a signal.
However, this means that a `SignalSubscriber` struct cannot have a
lifetime which exceeds the `SignalData` which contains it. In order to
fix the race in #978, one thread needs to be able to unsubscribe from a
signal (destroying the `SignalData` struct) while zero or more other
threads are in the process of calling the callbacks from a previous
emission of that signal (using the callback and `user_data` from zero or
more `SignalSubscriber` structs). Multiple threads could be calling
callbacks because callbacks are invoked in the `GMainContext` which
originally made a subscription, and GDBus supports subscribing to a
signal from multiple threads. In that case, the callbacks are dispatched
to multiple threads.
In order to allow the `SignalSubscriber` structs to outlive the
`SignalData` which contained their old match rule, store them in a
`GPtrArray` in the `SignalData` struct, and refcount them individually.
This commit in itself should make no functional changes to how GDBus
works, but will allow following commits to do so.
Signed-off-by: Philip Withnall <withnall@endlessm.com>
Helps: #978
2020-01-17 17:11:06 +01:00
|
|
|
|
g_ptr_array_remove_index_fast (signal_data->subscribers, n);
|
2024-03-14 20:51:59 +01:00
|
|
|
|
/* May free signal_data */
|
|
|
|
|
remove_signal_data_if_unused (connection, signal_data);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g_assert_not_reached ();
|
|
|
|
|
|
|
|
|
|
out:
|
2020-01-17 20:56:58 +01:00
|
|
|
|
return n_removed;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_dbus_connection_signal_unsubscribe:
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @connection: a #GDBusConnection
|
|
|
|
|
* @subscription_id: a subscription id obtained from
|
|
|
|
|
* g_dbus_connection_signal_subscribe()
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
|
|
|
|
* Unsubscribes from signals.
|
2010-05-06 22:02:08 +02:00
|
|
|
|
*
|
2020-02-21 16:12:08 +01:00
|
|
|
|
* Note that there may still be D-Bus traffic to process (relating to this
|
|
|
|
|
* signal subscription) in the current thread-default #GMainContext after this
|
|
|
|
|
* function has returned. You should continue to iterate the #GMainContext
|
|
|
|
|
* until the #GDestroyNotify function passed to
|
|
|
|
|
* g_dbus_connection_signal_subscribe() is called, in order to avoid memory
|
|
|
|
|
* leaks through callbacks queued on the #GMainContext after it’s stopped being
|
|
|
|
|
* iterated.
|
2021-04-18 08:54:33 +02:00
|
|
|
|
* Alternatively, any idle source with a priority lower than %G_PRIORITY_DEFAULT
|
|
|
|
|
* that was scheduled after unsubscription, also indicates that all resources
|
|
|
|
|
* of this subscription are released.
|
2020-02-21 16:12:08 +01:00
|
|
|
|
*
|
2010-05-06 22:02:08 +02:00
|
|
|
|
* Since: 2.26
|
|
|
|
|
*/
|
2010-05-06 20:13:59 +02:00
|
|
|
|
void
|
2010-05-10 04:13:18 +02:00
|
|
|
|
g_dbus_connection_signal_unsubscribe (GDBusConnection *connection,
|
|
|
|
|
guint subscription_id)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
2020-01-17 20:56:58 +01:00
|
|
|
|
guint n_subscribers_removed G_GNUC_UNUSED /* when compiling with G_DISABLE_ASSERT */;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
|
2011-10-19 18:23:30 +02:00
|
|
|
|
g_return_if_fail (check_initialized (connection));
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
CONNECTION_LOCK (connection);
|
2020-01-17 20:56:58 +01:00
|
|
|
|
n_subscribers_removed = unsubscribe_id_internal (connection, subscription_id);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
CONNECTION_UNLOCK (connection);
|
|
|
|
|
|
|
|
|
|
/* invariant */
|
2020-01-17 20:56:58 +01:00
|
|
|
|
g_assert (n_subscribers_removed == 0 || n_subscribers_removed == 1);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
|
{
|
2020-01-17 20:38:55 +01:00
|
|
|
|
SignalSubscriber *subscriber; /* (owned) */
|
2023-02-22 03:46:55 +01:00
|
|
|
|
GDBusMessage *message; /* (owned) */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
GDBusConnection *connection;
|
2020-04-03 13:16:49 +02:00
|
|
|
|
const gchar *sender; /* (nullable) for peer-to-peer connections */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
const gchar *path;
|
|
|
|
|
const gchar *interface;
|
|
|
|
|
const gchar *member;
|
|
|
|
|
} SignalInstance;
|
|
|
|
|
|
|
|
|
|
/* called on delivery thread (e.g. where g_dbus_connection_signal_subscribe() was called) with
|
|
|
|
|
* no locks held
|
|
|
|
|
*/
|
|
|
|
|
static gboolean
|
|
|
|
|
emit_signal_instance_in_idle_cb (gpointer data)
|
|
|
|
|
{
|
|
|
|
|
SignalInstance *signal_instance = data;
|
|
|
|
|
GVariant *parameters;
|
|
|
|
|
gboolean has_subscription;
|
|
|
|
|
|
|
|
|
|
parameters = g_dbus_message_get_body (signal_instance->message);
|
|
|
|
|
if (parameters == NULL)
|
|
|
|
|
{
|
|
|
|
|
parameters = g_variant_new ("()");
|
|
|
|
|
g_variant_ref_sink (parameters);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
g_variant_ref_sink (parameters);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if 0
|
2010-06-11 21:45:18 +02:00
|
|
|
|
g_print ("in emit_signal_instance_in_idle_cb (id=%d sender=%s path=%s interface=%s member=%s params=%s)\n",
|
2020-01-17 20:38:55 +01:00
|
|
|
|
signal_instance->subscriber->id,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
signal_instance->sender,
|
|
|
|
|
signal_instance->path,
|
|
|
|
|
signal_instance->interface,
|
|
|
|
|
signal_instance->member,
|
|
|
|
|
g_variant_print (parameters, TRUE));
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/* Careful here, don't do the callback if we no longer has the subscription */
|
|
|
|
|
CONNECTION_LOCK (signal_instance->connection);
|
|
|
|
|
has_subscription = FALSE;
|
2010-07-07 22:35:17 +02:00
|
|
|
|
if (g_hash_table_lookup (signal_instance->connection->map_id_to_signal_data,
|
2020-01-17 20:38:55 +01:00
|
|
|
|
GUINT_TO_POINTER (signal_instance->subscriber->id)) != NULL)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
has_subscription = TRUE;
|
|
|
|
|
CONNECTION_UNLOCK (signal_instance->connection);
|
|
|
|
|
|
|
|
|
|
if (has_subscription)
|
2020-01-17 20:38:55 +01:00
|
|
|
|
signal_instance->subscriber->callback (signal_instance->connection,
|
|
|
|
|
signal_instance->sender,
|
|
|
|
|
signal_instance->path,
|
|
|
|
|
signal_instance->interface,
|
|
|
|
|
signal_instance->member,
|
|
|
|
|
parameters,
|
|
|
|
|
signal_instance->subscriber->user_data);
|
2010-05-10 04:13:18 +02:00
|
|
|
|
|
2011-06-14 15:53:26 +02:00
|
|
|
|
g_variant_unref (parameters);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
signal_instance_free (SignalInstance *signal_instance)
|
|
|
|
|
{
|
2023-02-22 03:46:55 +01:00
|
|
|
|
g_clear_object (&signal_instance->message);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_object_unref (signal_instance->connection);
|
2020-01-17 20:38:55 +01:00
|
|
|
|
signal_subscriber_unref (signal_instance->subscriber);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_free (signal_instance);
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-08 08:13:10 +02:00
|
|
|
|
static gboolean
|
|
|
|
|
namespace_rule_matches (const gchar *namespace,
|
|
|
|
|
const gchar *name)
|
|
|
|
|
{
|
2024-04-25 01:37:47 +02:00
|
|
|
|
size_t len_namespace;
|
|
|
|
|
size_t len_name;
|
2013-04-08 08:13:10 +02:00
|
|
|
|
|
|
|
|
|
len_namespace = strlen (namespace);
|
|
|
|
|
len_name = strlen (name);
|
|
|
|
|
|
|
|
|
|
if (len_name < len_namespace)
|
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
|
|
if (memcmp (namespace, name, len_namespace) != 0)
|
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
|
|
return len_namespace == len_name || name[len_namespace] == '.';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
|
path_rule_matches (const gchar *path_a,
|
|
|
|
|
const gchar *path_b)
|
|
|
|
|
{
|
2024-04-25 01:37:47 +02:00
|
|
|
|
size_t len_a, len_b;
|
2013-04-08 08:13:10 +02:00
|
|
|
|
|
|
|
|
|
len_a = strlen (path_a);
|
|
|
|
|
len_b = strlen (path_b);
|
|
|
|
|
|
2015-03-06 15:22:33 +01:00
|
|
|
|
if (len_a < len_b && (len_a == 0 || path_a[len_a - 1] != '/'))
|
2013-04-08 08:13:10 +02:00
|
|
|
|
return FALSE;
|
|
|
|
|
|
2015-03-06 15:22:33 +01:00
|
|
|
|
if (len_b < len_a && (len_b == 0 || path_b[len_b - 1] != '/'))
|
2013-04-08 08:13:10 +02:00
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
|
|
return memcmp (path_a, path_b, MIN (len_a, len_b)) == 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-03 13:16:49 +02:00
|
|
|
|
/* called in GDBusWorker thread WITH lock held
|
|
|
|
|
*
|
|
|
|
|
* @sender is (nullable) for peer-to-peer connections */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
static void
|
|
|
|
|
schedule_callbacks (GDBusConnection *connection,
|
|
|
|
|
GPtrArray *signal_data_array,
|
|
|
|
|
GDBusMessage *message,
|
|
|
|
|
const gchar *sender)
|
|
|
|
|
{
|
|
|
|
|
guint n, m;
|
|
|
|
|
const gchar *interface;
|
|
|
|
|
const gchar *member;
|
|
|
|
|
const gchar *path;
|
|
|
|
|
const gchar *arg0;
|
2023-11-22 13:40:07 +01:00
|
|
|
|
const gchar *arg0_path;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
interface = NULL;
|
|
|
|
|
member = NULL;
|
|
|
|
|
path = NULL;
|
|
|
|
|
arg0 = NULL;
|
2023-11-22 13:40:07 +01:00
|
|
|
|
arg0_path = NULL;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
interface = g_dbus_message_get_interface (message);
|
|
|
|
|
member = g_dbus_message_get_member (message);
|
|
|
|
|
path = g_dbus_message_get_path (message);
|
|
|
|
|
arg0 = g_dbus_message_get_arg0 (message);
|
2023-11-22 13:40:07 +01:00
|
|
|
|
arg0_path = g_dbus_message_get_arg0_path (message);
|
|
|
|
|
|
|
|
|
|
/* These two are mutually exclusive through the type system. */
|
|
|
|
|
g_assert (arg0 == NULL || arg0_path == NULL);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
#if 0
|
2010-06-11 21:45:18 +02:00
|
|
|
|
g_print ("In schedule_callbacks:\n"
|
2013-05-20 22:54:48 +02:00
|
|
|
|
" sender = '%s'\n"
|
|
|
|
|
" interface = '%s'\n"
|
|
|
|
|
" member = '%s'\n"
|
|
|
|
|
" path = '%s'\n"
|
|
|
|
|
" arg0 = '%s'\n",
|
2010-06-11 21:45:18 +02:00
|
|
|
|
sender,
|
|
|
|
|
interface,
|
|
|
|
|
member,
|
|
|
|
|
path,
|
|
|
|
|
arg0);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/* TODO: if this is slow, then we can change signal_data_array into
|
|
|
|
|
* map_object_path_to_signal_data_array or something.
|
|
|
|
|
*/
|
|
|
|
|
for (n = 0; n < signal_data_array->len; n++)
|
|
|
|
|
{
|
|
|
|
|
SignalData *signal_data = signal_data_array->pdata[n];
|
|
|
|
|
|
|
|
|
|
if (signal_data->interface_name != NULL && g_strcmp0 (signal_data->interface_name, interface) != 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (signal_data->member != NULL && g_strcmp0 (signal_data->member, member) != 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (signal_data->object_path != NULL && g_strcmp0 (signal_data->object_path, path) != 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
2024-03-14 21:42:41 +01:00
|
|
|
|
if (signal_data->shared_name_watcher != NULL)
|
|
|
|
|
{
|
|
|
|
|
/* We want signals from a specified well-known name, which means
|
|
|
|
|
* the signal's sender needs to be the unique name that currently
|
|
|
|
|
* owns that well-known name, and we will have found this
|
|
|
|
|
* SignalData in
|
|
|
|
|
* connection->map_sender_unique_name_to_signal_data_array[""]. */
|
|
|
|
|
const WatchedName *watched_name;
|
|
|
|
|
const char *current_owner;
|
|
|
|
|
|
|
|
|
|
g_assert (signal_data->sender != NULL);
|
|
|
|
|
/* Invariant: We never need to watch for the owner of a unique
|
|
|
|
|
* name, or for the owner of DBUS_SERVICE_DBUS, either of which
|
|
|
|
|
* is always its own owner */
|
|
|
|
|
g_assert (!g_dbus_is_unique_name (signal_data->sender));
|
|
|
|
|
g_assert (g_strcmp0 (signal_data->sender, DBUS_SERVICE_DBUS) != 0);
|
|
|
|
|
|
|
|
|
|
watched_name = signal_data->shared_name_watcher->watched_name;
|
|
|
|
|
g_assert (watched_name != NULL);
|
|
|
|
|
current_owner = watched_name->owner;
|
|
|
|
|
|
|
|
|
|
/* Skip the signal if the actual sender is not known to own
|
|
|
|
|
* the required name */
|
|
|
|
|
if (current_owner == NULL || g_strcmp0 (current_owner, sender) != 0)
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
else if (signal_data->sender != NULL)
|
|
|
|
|
{
|
|
|
|
|
/* We want signals from a unique name or o.fd.DBus... */
|
|
|
|
|
g_assert (g_dbus_is_unique_name (signal_data->sender)
|
|
|
|
|
|| g_str_equal (signal_data->sender, DBUS_SERVICE_DBUS));
|
|
|
|
|
|
|
|
|
|
/* ... which means we must have found this SignalData in
|
|
|
|
|
* connection->map_sender_unique_name_to_signal_data_array[signal_data->sender],
|
|
|
|
|
* therefore we would only have found it if the signal's
|
|
|
|
|
* actual sender matches the required signal_data->sender */
|
|
|
|
|
g_assert (g_strcmp0 (signal_data->sender, sender) == 0);
|
|
|
|
|
}
|
|
|
|
|
/* else the sender is unspecified and we will accept anything */
|
|
|
|
|
|
2013-04-08 08:13:10 +02:00
|
|
|
|
if (signal_data->arg0 != NULL)
|
|
|
|
|
{
|
|
|
|
|
if (signal_data->flags & G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_NAMESPACE)
|
|
|
|
|
{
|
2023-11-22 13:40:07 +01:00
|
|
|
|
if (arg0 == NULL || !namespace_rule_matches (signal_data->arg0, arg0))
|
2013-04-08 08:13:10 +02:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
else if (signal_data->flags & G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH)
|
|
|
|
|
{
|
2023-11-22 13:40:07 +01:00
|
|
|
|
if ((arg0 == NULL || !path_rule_matches (signal_data->arg0, arg0)) &&
|
|
|
|
|
(arg0_path == NULL || !path_rule_matches (signal_data->arg0, arg0_path)))
|
2013-04-08 08:13:10 +02:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2024-04-30 19:33:57 +02:00
|
|
|
|
else if (arg0 == NULL || !g_str_equal (signal_data->arg0, arg0))
|
2013-04-08 08:13:10 +02:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2024-04-23 21:42:17 +02:00
|
|
|
|
if (signal_data->watched_name != NULL)
|
|
|
|
|
{
|
|
|
|
|
/* Invariant: SignalData should only have a watched_name if it
|
|
|
|
|
* represents the NameOwnerChanged signal */
|
|
|
|
|
g_assert (g_strcmp0 (sender, DBUS_SERVICE_DBUS) == 0);
|
|
|
|
|
g_assert (g_strcmp0 (interface, DBUS_INTERFACE_DBUS) == 0);
|
|
|
|
|
g_assert (g_strcmp0 (path, DBUS_PATH_DBUS) == 0);
|
|
|
|
|
g_assert (g_strcmp0 (member, "NameOwnerChanged") == 0);
|
|
|
|
|
name_watcher_deliver_name_owner_changed_unlocked (signal_data, message);
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
for (m = 0; m < signal_data->subscribers->len; m++)
|
|
|
|
|
{
|
gdbusconnection: Allocate SignalSubscriber structs individually
The `SignalSubscriber` structs contain the callback and `user_data` of each
subscriber to a signal, along with the `guint id` token held by that
subscriber to identify their subscription. There are one or more
`SignalSubscriber` structs for a given signal match rule, which is
represented as a `SignalData` struct.
Previously, the `SignalSubscriber` structs were stored in a `GArray` in
the `SignalData` struct, to reduce the number of allocations needed
when subscribing to a signal.
However, this means that a `SignalSubscriber` struct cannot have a
lifetime which exceeds the `SignalData` which contains it. In order to
fix the race in #978, one thread needs to be able to unsubscribe from a
signal (destroying the `SignalData` struct) while zero or more other
threads are in the process of calling the callbacks from a previous
emission of that signal (using the callback and `user_data` from zero or
more `SignalSubscriber` structs). Multiple threads could be calling
callbacks because callbacks are invoked in the `GMainContext` which
originally made a subscription, and GDBus supports subscribing to a
signal from multiple threads. In that case, the callbacks are dispatched
to multiple threads.
In order to allow the `SignalSubscriber` structs to outlive the
`SignalData` which contained their old match rule, store them in a
`GPtrArray` in the `SignalData` struct, and refcount them individually.
This commit in itself should make no functional changes to how GDBus
works, but will allow following commits to do so.
Signed-off-by: Philip Withnall <withnall@endlessm.com>
Helps: #978
2020-01-17 17:11:06 +01:00
|
|
|
|
SignalSubscriber *subscriber = signal_data->subscribers->pdata[m];
|
2010-05-06 20:13:59 +02:00
|
|
|
|
GSource *idle_source;
|
|
|
|
|
SignalInstance *signal_instance;
|
|
|
|
|
|
|
|
|
|
signal_instance = g_new0 (SignalInstance, 1);
|
2020-01-17 20:38:55 +01:00
|
|
|
|
signal_instance->subscriber = signal_subscriber_ref (subscriber);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
signal_instance->message = g_object_ref (message);
|
|
|
|
|
signal_instance->connection = g_object_ref (connection);
|
|
|
|
|
signal_instance->sender = sender;
|
|
|
|
|
signal_instance->path = path;
|
|
|
|
|
signal_instance->interface = interface;
|
|
|
|
|
signal_instance->member = member;
|
|
|
|
|
|
|
|
|
|
idle_source = g_idle_source_new ();
|
|
|
|
|
g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
|
|
|
|
|
g_source_set_callback (idle_source,
|
|
|
|
|
emit_signal_instance_in_idle_cb,
|
|
|
|
|
signal_instance,
|
|
|
|
|
(GDestroyNotify) signal_instance_free);
|
2021-07-26 11:53:02 +02:00
|
|
|
|
g_source_set_static_name (idle_source, "[gio] emit_signal_instance_in_idle_cb");
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_source_attach (idle_source, subscriber->context);
|
|
|
|
|
g_source_unref (idle_source);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-17 14:27:03 +02:00
|
|
|
|
/* called in GDBusWorker thread with lock held */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
static void
|
2010-05-10 04:13:18 +02:00
|
|
|
|
distribute_signals (GDBusConnection *connection,
|
|
|
|
|
GDBusMessage *message)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
|
|
|
|
GPtrArray *signal_data_array;
|
2023-08-16 17:24:26 +02:00
|
|
|
|
const gchar *sender, *interface, *member, *path;
|
|
|
|
|
|
|
|
|
|
g_assert (g_dbus_message_get_message_type (message) == G_DBUS_MESSAGE_TYPE_SIGNAL);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
sender = g_dbus_message_get_sender (message);
|
|
|
|
|
|
2023-08-16 17:24:26 +02:00
|
|
|
|
/* all three of these are required, but should have been validated already
|
|
|
|
|
* by validate_headers() in gdbusmessage.c */
|
|
|
|
|
interface = g_dbus_message_get_interface (message);
|
|
|
|
|
member = g_dbus_message_get_member (message);
|
|
|
|
|
path = g_dbus_message_get_path (message);
|
|
|
|
|
|
|
|
|
|
g_assert (interface != NULL);
|
|
|
|
|
g_assert (member != NULL);
|
|
|
|
|
g_assert (path != NULL);
|
|
|
|
|
|
2010-07-01 20:37:16 +02:00
|
|
|
|
if (G_UNLIKELY (_g_dbus_debug_signal ()))
|
|
|
|
|
{
|
|
|
|
|
_g_dbus_debug_print_lock ();
|
|
|
|
|
g_print ("========================================================================\n"
|
|
|
|
|
"GDBus-debug:Signal:\n"
|
2010-08-03 21:36:16 +02:00
|
|
|
|
" <<<< RECEIVED SIGNAL %s.%s\n"
|
2010-07-01 20:37:16 +02:00
|
|
|
|
" on object %s\n"
|
|
|
|
|
" sent by name %s\n",
|
2023-08-16 17:24:26 +02:00
|
|
|
|
interface, member, path,
|
2010-07-01 20:37:16 +02:00
|
|
|
|
sender != NULL ? sender : "(none)");
|
|
|
|
|
_g_dbus_debug_print_unlock ();
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-10 14:07:07 +02:00
|
|
|
|
/* collect subscribers that match on sender */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
if (sender != NULL)
|
|
|
|
|
{
|
2010-07-07 22:35:17 +02:00
|
|
|
|
signal_data_array = g_hash_table_lookup (connection->map_sender_unique_name_to_signal_data_array, sender);
|
2010-05-10 04:13:18 +02:00
|
|
|
|
if (signal_data_array != NULL)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
schedule_callbacks (connection, signal_data_array, message, sender);
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-23 21:42:17 +02:00
|
|
|
|
/* collect subscribers not matching on sender, or matching a well-known name */
|
2010-07-07 22:35:17 +02:00
|
|
|
|
signal_data_array = g_hash_table_lookup (connection->map_sender_unique_name_to_signal_data_array, "");
|
2010-05-06 20:13:59 +02:00
|
|
|
|
if (signal_data_array != NULL)
|
2010-05-10 04:13:18 +02:00
|
|
|
|
schedule_callbacks (connection, signal_data_array, message, sender);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
/* only called from finalize(), removes all subscriptions */
|
|
|
|
|
static void
|
|
|
|
|
purge_all_signal_subscriptions (GDBusConnection *connection)
|
|
|
|
|
{
|
|
|
|
|
GHashTableIter iter;
|
|
|
|
|
gpointer key;
|
|
|
|
|
GArray *ids;
|
|
|
|
|
guint n;
|
|
|
|
|
|
|
|
|
|
ids = g_array_new (FALSE, FALSE, sizeof (guint));
|
2010-07-07 22:35:17 +02:00
|
|
|
|
g_hash_table_iter_init (&iter, connection->map_id_to_signal_data);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
while (g_hash_table_iter_next (&iter, &key, NULL))
|
|
|
|
|
{
|
|
|
|
|
guint subscription_id = GPOINTER_TO_UINT (key);
|
|
|
|
|
g_array_append_val (ids, subscription_id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (n = 0; n < ids->len; n++)
|
|
|
|
|
{
|
|
|
|
|
guint subscription_id = g_array_index (ids, guint, n);
|
2020-01-17 20:56:58 +01:00
|
|
|
|
unsubscribe_id_internal (connection, subscription_id);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
g_array_free (ids, TRUE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
2010-07-14 20:46:12 +02:00
|
|
|
|
static GDBusInterfaceVTable *
|
|
|
|
|
_g_dbus_interface_vtable_copy (const GDBusInterfaceVTable *vtable)
|
|
|
|
|
{
|
|
|
|
|
/* Don't waste memory by copying padding - remember to update this
|
|
|
|
|
* when changing struct _GDBusInterfaceVTable in gdbusconnection.h
|
|
|
|
|
*/
|
2021-02-04 14:37:56 +01:00
|
|
|
|
return g_memdup2 ((gconstpointer) vtable, 3 * sizeof (gpointer));
|
2010-07-14 20:46:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
_g_dbus_interface_vtable_free (GDBusInterfaceVTable *vtable)
|
|
|
|
|
{
|
|
|
|
|
g_free (vtable);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
static GDBusSubtreeVTable *
|
|
|
|
|
_g_dbus_subtree_vtable_copy (const GDBusSubtreeVTable *vtable)
|
|
|
|
|
{
|
|
|
|
|
/* Don't waste memory by copying padding - remember to update this
|
|
|
|
|
* when changing struct _GDBusSubtreeVTable in gdbusconnection.h
|
|
|
|
|
*/
|
2021-02-04 14:37:56 +01:00
|
|
|
|
return g_memdup2 ((gconstpointer) vtable, 3 * sizeof (gpointer));
|
2010-07-14 20:46:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
_g_dbus_subtree_vtable_free (GDBusSubtreeVTable *vtable)
|
|
|
|
|
{
|
|
|
|
|
g_free (vtable);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
struct ExportedObject
|
|
|
|
|
{
|
|
|
|
|
gchar *object_path;
|
|
|
|
|
GDBusConnection *connection;
|
|
|
|
|
|
|
|
|
|
/* maps gchar* -> ExportedInterface* */
|
|
|
|
|
GHashTable *map_if_name_to_ei;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* only called with lock held */
|
|
|
|
|
static void
|
|
|
|
|
exported_object_free (ExportedObject *eo)
|
|
|
|
|
{
|
|
|
|
|
g_free (eo->object_path);
|
|
|
|
|
g_hash_table_unref (eo->map_if_name_to_ei);
|
|
|
|
|
g_free (eo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
|
{
|
|
|
|
|
ExportedObject *eo;
|
|
|
|
|
|
2021-09-24 10:03:40 +02:00
|
|
|
|
gint refcount; /* (atomic) */
|
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
guint id;
|
2021-09-24 09:58:42 +02:00
|
|
|
|
gchar *interface_name; /* (owned) */
|
|
|
|
|
GDBusInterfaceVTable *vtable; /* (owned) */
|
|
|
|
|
GDBusInterfaceInfo *interface_info; /* (owned) */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2021-09-24 09:58:42 +02:00
|
|
|
|
GMainContext *context; /* (owned) */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
gpointer user_data;
|
|
|
|
|
GDestroyNotify user_data_free_func;
|
|
|
|
|
} ExportedInterface;
|
|
|
|
|
|
2021-09-24 10:03:40 +02:00
|
|
|
|
static ExportedInterface *
|
|
|
|
|
exported_interface_ref (ExportedInterface *ei)
|
|
|
|
|
{
|
|
|
|
|
g_atomic_int_inc (&ei->refcount);
|
|
|
|
|
|
|
|
|
|
return ei;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* May be called with lock held */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
static void
|
2021-09-24 10:03:40 +02:00
|
|
|
|
exported_interface_unref (ExportedInterface *ei)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
2021-09-24 10:03:40 +02:00
|
|
|
|
if (!g_atomic_int_dec_and_test (&ei->refcount))
|
|
|
|
|
return;
|
|
|
|
|
|
2011-03-28 17:33:36 +02:00
|
|
|
|
g_dbus_interface_info_cache_release (ei->interface_info);
|
2010-06-30 18:19:28 +02:00
|
|
|
|
g_dbus_interface_info_unref ((GDBusInterfaceInfo *) ei->interface_info);
|
|
|
|
|
|
2021-09-24 11:52:41 +02:00
|
|
|
|
/* All uses of ei->vtable from callbacks scheduled in idle functions must
|
|
|
|
|
* have completed by this call_destroy_notify() call, as language bindings
|
|
|
|
|
* may destroy function closures in this callback. */
|
2010-09-23 23:23:30 +02:00
|
|
|
|
call_destroy_notify (ei->context,
|
|
|
|
|
ei->user_data_free_func,
|
|
|
|
|
ei->user_data);
|
2010-05-10 04:13:18 +02:00
|
|
|
|
|
2011-10-05 16:46:57 +02:00
|
|
|
|
g_main_context_unref (ei->context);
|
2010-05-10 04:13:18 +02:00
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_free (ei->interface_name);
|
2010-07-14 20:46:12 +02:00
|
|
|
|
_g_dbus_interface_vtable_free (ei->vtable);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_free (ei);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-24 09:28:19 +02:00
|
|
|
|
struct ExportedSubtree
|
|
|
|
|
{
|
2021-09-24 10:03:40 +02:00
|
|
|
|
gint refcount; /* (atomic) */
|
|
|
|
|
|
2021-09-24 09:28:19 +02:00
|
|
|
|
guint id;
|
2021-09-24 09:58:42 +02:00
|
|
|
|
gchar *object_path; /* (owned) */
|
|
|
|
|
GDBusConnection *connection; /* (unowned) */
|
|
|
|
|
GDBusSubtreeVTable *vtable; /* (owned) */
|
2021-09-24 09:28:19 +02:00
|
|
|
|
GDBusSubtreeFlags flags;
|
|
|
|
|
|
2021-09-24 09:58:42 +02:00
|
|
|
|
GMainContext *context; /* (owned) */
|
2021-09-24 09:28:19 +02:00
|
|
|
|
gpointer user_data;
|
|
|
|
|
GDestroyNotify user_data_free_func;
|
|
|
|
|
};
|
|
|
|
|
|
2021-09-24 10:03:40 +02:00
|
|
|
|
static ExportedSubtree *
|
|
|
|
|
exported_subtree_ref (ExportedSubtree *es)
|
|
|
|
|
{
|
|
|
|
|
g_atomic_int_inc (&es->refcount);
|
|
|
|
|
|
|
|
|
|
return es;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* May be called with lock held */
|
2021-09-24 09:28:19 +02:00
|
|
|
|
static void
|
2021-09-24 10:03:40 +02:00
|
|
|
|
exported_subtree_unref (ExportedSubtree *es)
|
2021-09-24 09:28:19 +02:00
|
|
|
|
{
|
2021-09-24 10:03:40 +02:00
|
|
|
|
if (!g_atomic_int_dec_and_test (&es->refcount))
|
|
|
|
|
return;
|
|
|
|
|
|
2021-09-24 11:52:41 +02:00
|
|
|
|
/* All uses of es->vtable from callbacks scheduled in idle functions must
|
|
|
|
|
* have completed by this call_destroy_notify() call, as language bindings
|
|
|
|
|
* may destroy function closures in this callback. */
|
2021-09-24 09:28:19 +02:00
|
|
|
|
call_destroy_notify (es->context,
|
|
|
|
|
es->user_data_free_func,
|
|
|
|
|
es->user_data);
|
|
|
|
|
|
|
|
|
|
g_main_context_unref (es->context);
|
|
|
|
|
|
|
|
|
|
_g_dbus_subtree_vtable_free (es->vtable);
|
|
|
|
|
g_free (es->object_path);
|
|
|
|
|
g_free (es);
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
2010-06-09 23:57:04 +02:00
|
|
|
|
/* Convenience function to check if @registration_id (if not zero) or
|
|
|
|
|
* @subtree_registration_id (if not zero) has been unregistered. If
|
|
|
|
|
* so, returns %TRUE.
|
|
|
|
|
*
|
2021-09-24 11:52:41 +02:00
|
|
|
|
* If not, sets @out_ei and/or @out_es to a strong reference to the relevant
|
|
|
|
|
* #ExportedInterface/#ExportedSubtree and returns %FALSE.
|
|
|
|
|
*
|
2011-10-17 14:27:03 +02:00
|
|
|
|
* May be called by any thread. Caller must *not* hold lock.
|
2010-06-09 23:57:04 +02:00
|
|
|
|
*/
|
|
|
|
|
static gboolean
|
2021-09-24 11:52:41 +02:00
|
|
|
|
has_object_been_unregistered (GDBusConnection *connection,
|
|
|
|
|
guint registration_id,
|
|
|
|
|
ExportedInterface **out_ei,
|
|
|
|
|
guint subtree_registration_id,
|
|
|
|
|
ExportedSubtree **out_es)
|
2010-06-09 23:57:04 +02:00
|
|
|
|
{
|
|
|
|
|
gboolean ret;
|
2021-09-24 11:52:41 +02:00
|
|
|
|
ExportedInterface *ei = NULL;
|
|
|
|
|
gpointer es = NULL;
|
2010-06-09 23:57:04 +02:00
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
|
|
|
|
|
|
|
|
|
|
ret = FALSE;
|
|
|
|
|
|
|
|
|
|
CONNECTION_LOCK (connection);
|
2021-09-24 11:52:41 +02:00
|
|
|
|
|
|
|
|
|
if (registration_id != 0)
|
2010-06-09 23:57:04 +02:00
|
|
|
|
{
|
2021-09-24 11:52:41 +02:00
|
|
|
|
ei = g_hash_table_lookup (connection->map_id_to_ei, GUINT_TO_POINTER (registration_id));
|
|
|
|
|
if (ei == NULL)
|
|
|
|
|
ret = TRUE;
|
|
|
|
|
else if (out_ei != NULL)
|
|
|
|
|
*out_ei = exported_interface_ref (ei);
|
2010-06-09 23:57:04 +02:00
|
|
|
|
}
|
2021-09-24 11:52:41 +02:00
|
|
|
|
if (subtree_registration_id != 0)
|
2010-06-09 23:57:04 +02:00
|
|
|
|
{
|
2021-09-24 11:52:41 +02:00
|
|
|
|
es = g_hash_table_lookup (connection->map_id_to_es, GUINT_TO_POINTER (subtree_registration_id));
|
|
|
|
|
if (es == NULL)
|
|
|
|
|
ret = TRUE;
|
|
|
|
|
else if (out_es != NULL)
|
|
|
|
|
*out_es = exported_subtree_ref (es);
|
2010-06-09 23:57:04 +02:00
|
|
|
|
}
|
2021-09-24 11:52:41 +02:00
|
|
|
|
|
2010-06-09 23:57:04 +02:00
|
|
|
|
CONNECTION_UNLOCK (connection);
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
typedef struct
|
|
|
|
|
{
|
|
|
|
|
GDBusConnection *connection;
|
2023-02-22 03:46:55 +01:00
|
|
|
|
GDBusMessage *message; /* (owned) */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
gpointer user_data;
|
2010-06-09 23:57:04 +02:00
|
|
|
|
const gchar *property_name;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
const GDBusInterfaceVTable *vtable;
|
2010-07-19 21:45:27 +02:00
|
|
|
|
GDBusInterfaceInfo *interface_info;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
const GDBusPropertyInfo *property_info;
|
2010-06-09 23:57:04 +02:00
|
|
|
|
guint registration_id;
|
|
|
|
|
guint subtree_registration_id;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
} PropertyData;
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
property_data_free (PropertyData *data)
|
|
|
|
|
{
|
|
|
|
|
g_object_unref (data->connection);
|
2023-02-22 03:46:55 +01:00
|
|
|
|
g_clear_object (&data->message);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_free (data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* called in thread where object was registered - no locks held */
|
|
|
|
|
static gboolean
|
|
|
|
|
invoke_get_property_in_idle_cb (gpointer _data)
|
|
|
|
|
{
|
|
|
|
|
PropertyData *data = _data;
|
|
|
|
|
GVariant *value;
|
|
|
|
|
GError *error;
|
|
|
|
|
GDBusMessage *reply;
|
2021-09-24 11:52:41 +02:00
|
|
|
|
ExportedInterface *ei = NULL;
|
|
|
|
|
ExportedSubtree *es = NULL;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2010-06-09 23:57:04 +02:00
|
|
|
|
if (has_object_been_unregistered (data->connection,
|
|
|
|
|
data->registration_id,
|
2021-09-24 11:52:41 +02:00
|
|
|
|
&ei,
|
|
|
|
|
data->subtree_registration_id,
|
|
|
|
|
&es))
|
2010-06-09 23:57:04 +02:00
|
|
|
|
{
|
|
|
|
|
reply = g_dbus_message_new_method_error (data->message,
|
2024-05-13 14:10:15 +02:00
|
|
|
|
DBUS_ERROR_UNKNOWN_METHOD,
|
2024-06-13 20:30:58 +02:00
|
|
|
|
/* Translators: The first placeholder is a D-Bus interface,
|
|
|
|
|
* the second is the path of an object. */
|
|
|
|
|
_("No such interface “%s” on object at path %s"),
|
|
|
|
|
DBUS_INTERFACE_PROPERTIES,
|
2010-06-09 23:57:04 +02:00
|
|
|
|
g_dbus_message_get_path (data->message));
|
2010-07-19 22:07:57 +02:00
|
|
|
|
g_dbus_connection_send_message (data->connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
|
2010-06-09 23:57:04 +02:00
|
|
|
|
g_object_unref (reply);
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
error = NULL;
|
|
|
|
|
value = data->vtable->get_property (data->connection,
|
|
|
|
|
g_dbus_message_get_sender (data->message),
|
|
|
|
|
g_dbus_message_get_path (data->message),
|
|
|
|
|
data->interface_info->name,
|
|
|
|
|
data->property_name,
|
|
|
|
|
&error,
|
|
|
|
|
data->user_data);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (value != NULL)
|
|
|
|
|
{
|
|
|
|
|
g_assert_no_error (error);
|
|
|
|
|
|
2011-07-11 14:27:53 +02:00
|
|
|
|
g_variant_take_ref (value);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
reply = g_dbus_message_new_method_reply (data->message);
|
|
|
|
|
g_dbus_message_set_body (reply, g_variant_new ("(v)", value));
|
2010-07-19 22:07:57 +02:00
|
|
|
|
g_dbus_connection_send_message (data->connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_variant_unref (value);
|
|
|
|
|
g_object_unref (reply);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
gchar *dbus_error_name;
|
|
|
|
|
g_assert (error != NULL);
|
|
|
|
|
dbus_error_name = g_dbus_error_encode_gerror (error);
|
|
|
|
|
reply = g_dbus_message_new_method_error_literal (data->message,
|
|
|
|
|
dbus_error_name,
|
|
|
|
|
error->message);
|
2010-07-19 22:07:57 +02:00
|
|
|
|
g_dbus_connection_send_message (data->connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_free (dbus_error_name);
|
|
|
|
|
g_error_free (error);
|
|
|
|
|
g_object_unref (reply);
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-09 23:57:04 +02:00
|
|
|
|
out:
|
2021-09-24 11:52:41 +02:00
|
|
|
|
g_clear_pointer (&ei, exported_interface_unref);
|
|
|
|
|
g_clear_pointer (&es, exported_subtree_unref);
|
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* called in thread where object was registered - no locks held */
|
|
|
|
|
static gboolean
|
|
|
|
|
invoke_set_property_in_idle_cb (gpointer _data)
|
|
|
|
|
{
|
|
|
|
|
PropertyData *data = _data;
|
|
|
|
|
GError *error;
|
|
|
|
|
GDBusMessage *reply;
|
|
|
|
|
GVariant *value;
|
|
|
|
|
|
|
|
|
|
error = NULL;
|
|
|
|
|
value = NULL;
|
|
|
|
|
|
|
|
|
|
g_variant_get (g_dbus_message_get_body (data->message),
|
|
|
|
|
"(ssv)",
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
&value);
|
|
|
|
|
|
|
|
|
|
if (!data->vtable->set_property (data->connection,
|
|
|
|
|
g_dbus_message_get_sender (data->message),
|
|
|
|
|
g_dbus_message_get_path (data->message),
|
|
|
|
|
data->interface_info->name,
|
|
|
|
|
data->property_name,
|
|
|
|
|
value,
|
|
|
|
|
&error,
|
|
|
|
|
data->user_data))
|
|
|
|
|
{
|
|
|
|
|
gchar *dbus_error_name;
|
|
|
|
|
g_assert (error != NULL);
|
|
|
|
|
dbus_error_name = g_dbus_error_encode_gerror (error);
|
|
|
|
|
reply = g_dbus_message_new_method_error_literal (data->message,
|
|
|
|
|
dbus_error_name,
|
|
|
|
|
error->message);
|
|
|
|
|
g_free (dbus_error_name);
|
|
|
|
|
g_error_free (error);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
reply = g_dbus_message_new_method_reply (data->message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g_assert (reply != NULL);
|
2010-07-19 22:07:57 +02:00
|
|
|
|
g_dbus_connection_send_message (data->connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_object_unref (reply);
|
2010-08-30 19:31:09 +02:00
|
|
|
|
g_variant_unref (value);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-17 14:27:03 +02:00
|
|
|
|
/* called in any thread with connection's lock held */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
static gboolean
|
|
|
|
|
validate_and_maybe_schedule_property_getset (GDBusConnection *connection,
|
|
|
|
|
GDBusMessage *message,
|
2010-06-09 23:57:04 +02:00
|
|
|
|
guint registration_id,
|
|
|
|
|
guint subtree_registration_id,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
gboolean is_get,
|
2010-07-19 21:45:27 +02:00
|
|
|
|
GDBusInterfaceInfo *interface_info,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
const GDBusInterfaceVTable *vtable,
|
|
|
|
|
GMainContext *main_context,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
gboolean handled;
|
|
|
|
|
const char *interface_name;
|
|
|
|
|
const char *property_name;
|
|
|
|
|
const GDBusPropertyInfo *property_info;
|
|
|
|
|
GSource *idle_source;
|
|
|
|
|
PropertyData *property_data;
|
|
|
|
|
GDBusMessage *reply;
|
|
|
|
|
|
|
|
|
|
handled = FALSE;
|
|
|
|
|
|
|
|
|
|
if (is_get)
|
|
|
|
|
g_variant_get (g_dbus_message_get_body (message),
|
2010-05-13 19:20:26 +02:00
|
|
|
|
"(&s&s)",
|
2010-05-06 20:13:59 +02:00
|
|
|
|
&interface_name,
|
|
|
|
|
&property_name);
|
|
|
|
|
else
|
|
|
|
|
g_variant_get (g_dbus_message_get_body (message),
|
2010-05-13 19:20:26 +02:00
|
|
|
|
"(&s&sv)",
|
2010-05-06 20:13:59 +02:00
|
|
|
|
&interface_name,
|
|
|
|
|
&property_name,
|
|
|
|
|
NULL);
|
|
|
|
|
|
2013-04-17 15:30:15 +02:00
|
|
|
|
if (vtable == NULL)
|
|
|
|
|
goto out;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2024-05-13 14:10:15 +02:00
|
|
|
|
/* Check that the property exists - if not fail with DBUS_ERROR_INVALID_ARGS
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*/
|
|
|
|
|
property_info = NULL;
|
|
|
|
|
|
|
|
|
|
/* TODO: the cost of this is O(n) - it might be worth caching the result */
|
2010-06-30 18:10:00 +02:00
|
|
|
|
property_info = g_dbus_interface_info_lookup_property (interface_info, property_name);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
if (property_info == NULL)
|
|
|
|
|
{
|
|
|
|
|
reply = g_dbus_message_new_method_error (message,
|
2024-05-13 14:10:15 +02:00
|
|
|
|
DBUS_ERROR_INVALID_ARGS,
|
2018-06-08 16:15:54 +02:00
|
|
|
|
_("No such property “%s”"),
|
2010-05-06 20:13:59 +02:00
|
|
|
|
property_name);
|
2010-07-19 22:07:57 +02:00
|
|
|
|
g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_object_unref (reply);
|
|
|
|
|
handled = TRUE;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (is_get && !(property_info->flags & G_DBUS_PROPERTY_INFO_FLAGS_READABLE))
|
|
|
|
|
{
|
|
|
|
|
reply = g_dbus_message_new_method_error (message,
|
2024-05-13 14:10:15 +02:00
|
|
|
|
DBUS_ERROR_INVALID_ARGS,
|
2018-06-08 16:15:54 +02:00
|
|
|
|
_("Property “%s” is not readable"),
|
2010-05-06 20:13:59 +02:00
|
|
|
|
property_name);
|
2010-07-19 22:07:57 +02:00
|
|
|
|
g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_object_unref (reply);
|
|
|
|
|
handled = TRUE;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
else if (!is_get && !(property_info->flags & G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE))
|
|
|
|
|
{
|
|
|
|
|
reply = g_dbus_message_new_method_error (message,
|
2024-05-13 14:10:15 +02:00
|
|
|
|
DBUS_ERROR_INVALID_ARGS,
|
2018-06-08 16:15:54 +02:00
|
|
|
|
_("Property “%s” is not writable"),
|
2010-05-06 20:13:59 +02:00
|
|
|
|
property_name);
|
2010-07-19 22:07:57 +02:00
|
|
|
|
g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_object_unref (reply);
|
|
|
|
|
handled = TRUE;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-22 19:37:54 +02:00
|
|
|
|
if (!is_get)
|
|
|
|
|
{
|
|
|
|
|
GVariant *value;
|
|
|
|
|
|
2024-05-13 14:10:15 +02:00
|
|
|
|
/* Fail with DBUS_ERROR_INVALID_ARGS if the type
|
2013-06-22 19:37:54 +02:00
|
|
|
|
* of the given value is wrong
|
|
|
|
|
*/
|
|
|
|
|
g_variant_get_child (g_dbus_message_get_body (message), 2, "v", &value);
|
|
|
|
|
if (g_strcmp0 (g_variant_get_type_string (value), property_info->signature) != 0)
|
|
|
|
|
{
|
|
|
|
|
reply = g_dbus_message_new_method_error (message,
|
2024-05-13 14:10:15 +02:00
|
|
|
|
DBUS_ERROR_INVALID_ARGS,
|
2018-06-08 16:15:54 +02:00
|
|
|
|
_("Error setting property “%s”: Expected type “%s” but got “%s”"),
|
2013-06-22 19:37:54 +02:00
|
|
|
|
property_name, property_info->signature,
|
|
|
|
|
g_variant_get_type_string (value));
|
|
|
|
|
g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
|
|
|
|
|
g_variant_unref (value);
|
|
|
|
|
g_object_unref (reply);
|
|
|
|
|
handled = TRUE;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g_variant_unref (value);
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-17 15:30:15 +02:00
|
|
|
|
/* If the vtable pointer for get_property() resp. set_property() is
|
|
|
|
|
* NULL then dispatch the call via the method_call() handler.
|
|
|
|
|
*/
|
|
|
|
|
if (is_get)
|
|
|
|
|
{
|
|
|
|
|
if (vtable->get_property == NULL)
|
|
|
|
|
{
|
|
|
|
|
schedule_method_call (connection, message, registration_id, subtree_registration_id,
|
|
|
|
|
interface_info, NULL, property_info, g_dbus_message_get_body (message),
|
|
|
|
|
vtable, main_context, user_data);
|
|
|
|
|
handled = TRUE;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (vtable->set_property == NULL)
|
|
|
|
|
{
|
|
|
|
|
schedule_method_call (connection, message, registration_id, subtree_registration_id,
|
|
|
|
|
interface_info, NULL, property_info, g_dbus_message_get_body (message),
|
|
|
|
|
vtable, main_context, user_data);
|
|
|
|
|
handled = TRUE;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
/* ok, got the property info - call user code in an idle handler */
|
|
|
|
|
property_data = g_new0 (PropertyData, 1);
|
|
|
|
|
property_data->connection = g_object_ref (connection);
|
|
|
|
|
property_data->message = g_object_ref (message);
|
|
|
|
|
property_data->user_data = user_data;
|
|
|
|
|
property_data->property_name = property_name;
|
|
|
|
|
property_data->vtable = vtable;
|
2010-06-30 18:10:00 +02:00
|
|
|
|
property_data->interface_info = interface_info;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
property_data->property_info = property_info;
|
2010-06-09 23:57:04 +02:00
|
|
|
|
property_data->registration_id = registration_id;
|
|
|
|
|
property_data->subtree_registration_id = subtree_registration_id;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
idle_source = g_idle_source_new ();
|
|
|
|
|
g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
|
|
|
|
|
g_source_set_callback (idle_source,
|
|
|
|
|
is_get ? invoke_get_property_in_idle_cb : invoke_set_property_in_idle_cb,
|
|
|
|
|
property_data,
|
|
|
|
|
(GDestroyNotify) property_data_free);
|
2014-03-22 13:15:45 +01:00
|
|
|
|
if (is_get)
|
2021-07-26 11:53:02 +02:00
|
|
|
|
g_source_set_static_name (idle_source, "[gio] invoke_get_property_in_idle_cb");
|
2014-03-22 13:15:45 +01:00
|
|
|
|
else
|
2021-07-26 11:53:02 +02:00
|
|
|
|
g_source_set_static_name (idle_source, "[gio] invoke_set_property_in_idle_cb");
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_source_attach (idle_source, main_context);
|
|
|
|
|
g_source_unref (idle_source);
|
|
|
|
|
|
|
|
|
|
handled = TRUE;
|
|
|
|
|
|
|
|
|
|
out:
|
|
|
|
|
return handled;
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-17 14:27:03 +02:00
|
|
|
|
/* called in GDBusWorker thread with connection's lock held */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
static gboolean
|
|
|
|
|
handle_getset_property (GDBusConnection *connection,
|
|
|
|
|
ExportedObject *eo,
|
|
|
|
|
GDBusMessage *message,
|
|
|
|
|
gboolean is_get)
|
|
|
|
|
{
|
|
|
|
|
ExportedInterface *ei;
|
|
|
|
|
gboolean handled;
|
|
|
|
|
const char *interface_name;
|
|
|
|
|
const char *property_name;
|
|
|
|
|
|
|
|
|
|
handled = FALSE;
|
|
|
|
|
|
|
|
|
|
if (is_get)
|
|
|
|
|
g_variant_get (g_dbus_message_get_body (message),
|
2010-05-13 19:20:26 +02:00
|
|
|
|
"(&s&s)",
|
2010-05-06 20:13:59 +02:00
|
|
|
|
&interface_name,
|
|
|
|
|
&property_name);
|
|
|
|
|
else
|
|
|
|
|
g_variant_get (g_dbus_message_get_body (message),
|
2010-05-13 19:20:26 +02:00
|
|
|
|
"(&s&sv)",
|
2010-05-06 20:13:59 +02:00
|
|
|
|
&interface_name,
|
|
|
|
|
&property_name,
|
|
|
|
|
NULL);
|
|
|
|
|
|
2024-05-13 14:10:15 +02:00
|
|
|
|
/* Fail with DBUS_ERROR_INVALID_ARGS if there is
|
2010-05-06 20:13:59 +02:00
|
|
|
|
* no such interface registered
|
|
|
|
|
*/
|
|
|
|
|
ei = g_hash_table_lookup (eo->map_if_name_to_ei, interface_name);
|
|
|
|
|
if (ei == NULL)
|
|
|
|
|
{
|
|
|
|
|
GDBusMessage *reply;
|
|
|
|
|
reply = g_dbus_message_new_method_error (message,
|
2024-05-13 14:10:15 +02:00
|
|
|
|
DBUS_ERROR_INVALID_ARGS,
|
2018-06-08 16:15:54 +02:00
|
|
|
|
_("No such interface “%s”"),
|
2010-05-06 20:13:59 +02:00
|
|
|
|
interface_name);
|
2010-07-19 22:07:57 +02:00
|
|
|
|
g_dbus_connection_send_message_unlocked (eo->connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_object_unref (reply);
|
|
|
|
|
handled = TRUE;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handled = validate_and_maybe_schedule_property_getset (eo->connection,
|
|
|
|
|
message,
|
2010-06-09 23:57:04 +02:00
|
|
|
|
ei->id,
|
|
|
|
|
0,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
is_get,
|
2010-06-30 18:10:00 +02:00
|
|
|
|
ei->interface_info,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
ei->vtable,
|
|
|
|
|
ei->context,
|
|
|
|
|
ei->user_data);
|
|
|
|
|
out:
|
|
|
|
|
return handled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
|
{
|
|
|
|
|
GDBusConnection *connection;
|
2023-02-22 03:46:55 +01:00
|
|
|
|
GDBusMessage *message; /* (owned) */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
gpointer user_data;
|
|
|
|
|
const GDBusInterfaceVTable *vtable;
|
2010-07-19 21:45:27 +02:00
|
|
|
|
GDBusInterfaceInfo *interface_info;
|
2010-06-09 23:57:04 +02:00
|
|
|
|
guint registration_id;
|
|
|
|
|
guint subtree_registration_id;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
} PropertyGetAllData;
|
|
|
|
|
|
|
|
|
|
static void
|
2023-02-22 03:45:15 +01:00
|
|
|
|
property_get_all_data_free (PropertyGetAllData *data)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
|
|
|
|
g_object_unref (data->connection);
|
2023-02-22 03:46:55 +01:00
|
|
|
|
g_clear_object (&data->message);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_free (data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* called in thread where object was registered - no locks held */
|
|
|
|
|
static gboolean
|
|
|
|
|
invoke_get_all_properties_in_idle_cb (gpointer _data)
|
|
|
|
|
{
|
|
|
|
|
PropertyGetAllData *data = _data;
|
2010-05-14 14:00:24 +02:00
|
|
|
|
GVariantBuilder builder;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
GDBusMessage *reply;
|
|
|
|
|
guint n;
|
2021-09-24 11:52:41 +02:00
|
|
|
|
ExportedInterface *ei = NULL;
|
|
|
|
|
ExportedSubtree *es = NULL;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2010-06-09 23:57:04 +02:00
|
|
|
|
if (has_object_been_unregistered (data->connection,
|
|
|
|
|
data->registration_id,
|
2021-09-24 11:52:41 +02:00
|
|
|
|
&ei,
|
|
|
|
|
data->subtree_registration_id,
|
|
|
|
|
&es))
|
2010-06-09 23:57:04 +02:00
|
|
|
|
{
|
|
|
|
|
reply = g_dbus_message_new_method_error (data->message,
|
2024-05-13 14:10:15 +02:00
|
|
|
|
DBUS_ERROR_UNKNOWN_METHOD,
|
2024-06-13 20:30:58 +02:00
|
|
|
|
_("No such interface “%s” on object at path %s"),
|
|
|
|
|
DBUS_INTERFACE_PROPERTIES,
|
2010-06-09 23:57:04 +02:00
|
|
|
|
g_dbus_message_get_path (data->message));
|
2010-07-19 22:07:57 +02:00
|
|
|
|
g_dbus_connection_send_message (data->connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
|
2010-06-09 23:57:04 +02:00
|
|
|
|
g_object_unref (reply);
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
/* TODO: Right now we never fail this call - we just omit values if
|
|
|
|
|
* a get_property() call is failing.
|
|
|
|
|
*
|
|
|
|
|
* We could fail the whole call if just a single get_property() call
|
|
|
|
|
* returns an error. We need clarification in the D-Bus spec about this.
|
|
|
|
|
*/
|
2024-09-25 20:09:07 +02:00
|
|
|
|
g_variant_builder_init_static (&builder, G_VARIANT_TYPE ("(a{sv})"));
|
2010-05-14 14:00:24 +02:00
|
|
|
|
g_variant_builder_open (&builder, G_VARIANT_TYPE ("a{sv}"));
|
2010-05-06 20:13:59 +02:00
|
|
|
|
for (n = 0; data->interface_info->properties != NULL && data->interface_info->properties[n] != NULL; n++)
|
|
|
|
|
{
|
|
|
|
|
const GDBusPropertyInfo *property_info = data->interface_info->properties[n];
|
|
|
|
|
GVariant *value;
|
|
|
|
|
|
|
|
|
|
if (!(property_info->flags & G_DBUS_PROPERTY_INFO_FLAGS_READABLE))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
value = data->vtable->get_property (data->connection,
|
|
|
|
|
g_dbus_message_get_sender (data->message),
|
|
|
|
|
g_dbus_message_get_path (data->message),
|
|
|
|
|
data->interface_info->name,
|
|
|
|
|
property_info->name,
|
|
|
|
|
NULL,
|
|
|
|
|
data->user_data);
|
|
|
|
|
|
|
|
|
|
if (value == NULL)
|
|
|
|
|
continue;
|
|
|
|
|
|
2011-07-11 14:27:53 +02:00
|
|
|
|
g_variant_take_ref (value);
|
2010-05-14 14:00:24 +02:00
|
|
|
|
g_variant_builder_add (&builder,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
"{sv}",
|
|
|
|
|
property_info->name,
|
|
|
|
|
value);
|
2010-08-25 20:45:28 +02:00
|
|
|
|
g_variant_unref (value);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
2010-05-14 14:00:24 +02:00
|
|
|
|
g_variant_builder_close (&builder);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
reply = g_dbus_message_new_method_reply (data->message);
|
2010-05-14 14:00:24 +02:00
|
|
|
|
g_dbus_message_set_body (reply, g_variant_builder_end (&builder));
|
2010-07-19 22:07:57 +02:00
|
|
|
|
g_dbus_connection_send_message (data->connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_object_unref (reply);
|
|
|
|
|
|
2010-06-09 23:57:04 +02:00
|
|
|
|
out:
|
2021-09-24 11:52:41 +02:00
|
|
|
|
g_clear_pointer (&ei, exported_interface_unref);
|
|
|
|
|
g_clear_pointer (&es, exported_subtree_unref);
|
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-01 23:42:43 +02:00
|
|
|
|
static gboolean
|
|
|
|
|
interface_has_readable_properties (GDBusInterfaceInfo *interface_info)
|
|
|
|
|
{
|
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
|
|
if (!interface_info->properties)
|
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
|
|
for (i = 0; interface_info->properties[i]; i++)
|
|
|
|
|
if (interface_info->properties[i]->flags & G_DBUS_PROPERTY_INFO_FLAGS_READABLE)
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-17 14:27:03 +02:00
|
|
|
|
/* called in any thread with connection's lock held */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
static gboolean
|
|
|
|
|
validate_and_maybe_schedule_property_get_all (GDBusConnection *connection,
|
|
|
|
|
GDBusMessage *message,
|
2010-06-09 23:57:04 +02:00
|
|
|
|
guint registration_id,
|
|
|
|
|
guint subtree_registration_id,
|
2010-07-19 21:45:27 +02:00
|
|
|
|
GDBusInterfaceInfo *interface_info,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
const GDBusInterfaceVTable *vtable,
|
|
|
|
|
GMainContext *main_context,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
gboolean handled;
|
|
|
|
|
GSource *idle_source;
|
|
|
|
|
PropertyGetAllData *property_get_all_data;
|
|
|
|
|
|
|
|
|
|
handled = FALSE;
|
|
|
|
|
|
2013-04-17 15:30:15 +02:00
|
|
|
|
if (vtable == NULL)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
goto out;
|
|
|
|
|
|
2013-07-01 23:42:43 +02:00
|
|
|
|
/* If the vtable pointer for get_property() is NULL but we have a
|
|
|
|
|
* non-zero number of readable properties, then dispatch the call via
|
|
|
|
|
* the method_call() handler.
|
2013-04-17 15:30:15 +02:00
|
|
|
|
*/
|
2013-07-01 23:42:43 +02:00
|
|
|
|
if (vtable->get_property == NULL && interface_has_readable_properties (interface_info))
|
2013-04-17 15:30:15 +02:00
|
|
|
|
{
|
|
|
|
|
schedule_method_call (connection, message, registration_id, subtree_registration_id,
|
|
|
|
|
interface_info, NULL, NULL, g_dbus_message_get_body (message),
|
|
|
|
|
vtable, main_context, user_data);
|
|
|
|
|
handled = TRUE;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
/* ok, got the property info - call user in an idle handler */
|
|
|
|
|
property_get_all_data = g_new0 (PropertyGetAllData, 1);
|
|
|
|
|
property_get_all_data->connection = g_object_ref (connection);
|
|
|
|
|
property_get_all_data->message = g_object_ref (message);
|
|
|
|
|
property_get_all_data->user_data = user_data;
|
|
|
|
|
property_get_all_data->vtable = vtable;
|
2010-06-30 18:10:00 +02:00
|
|
|
|
property_get_all_data->interface_info = interface_info;
|
2010-06-09 23:57:04 +02:00
|
|
|
|
property_get_all_data->registration_id = registration_id;
|
|
|
|
|
property_get_all_data->subtree_registration_id = subtree_registration_id;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
idle_source = g_idle_source_new ();
|
|
|
|
|
g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
|
|
|
|
|
g_source_set_callback (idle_source,
|
|
|
|
|
invoke_get_all_properties_in_idle_cb,
|
|
|
|
|
property_get_all_data,
|
|
|
|
|
(GDestroyNotify) property_get_all_data_free);
|
2021-07-26 11:53:02 +02:00
|
|
|
|
g_source_set_static_name (idle_source, "[gio] invoke_get_all_properties_in_idle_cb");
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_source_attach (idle_source, main_context);
|
|
|
|
|
g_source_unref (idle_source);
|
|
|
|
|
|
|
|
|
|
handled = TRUE;
|
|
|
|
|
|
|
|
|
|
out:
|
|
|
|
|
return handled;
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-17 14:27:03 +02:00
|
|
|
|
/* called in GDBusWorker thread with connection's lock held */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
static gboolean
|
|
|
|
|
handle_get_all_properties (GDBusConnection *connection,
|
|
|
|
|
ExportedObject *eo,
|
|
|
|
|
GDBusMessage *message)
|
|
|
|
|
{
|
|
|
|
|
ExportedInterface *ei;
|
|
|
|
|
gboolean handled;
|
|
|
|
|
const char *interface_name;
|
|
|
|
|
|
|
|
|
|
handled = FALSE;
|
|
|
|
|
|
|
|
|
|
g_variant_get (g_dbus_message_get_body (message),
|
2010-05-13 19:20:26 +02:00
|
|
|
|
"(&s)",
|
2010-05-06 20:13:59 +02:00
|
|
|
|
&interface_name);
|
|
|
|
|
|
2024-05-13 14:10:15 +02:00
|
|
|
|
/* Fail with DBUS_ERROR_INVALID_ARGS if there is
|
2010-05-06 20:13:59 +02:00
|
|
|
|
* no such interface registered
|
|
|
|
|
*/
|
|
|
|
|
ei = g_hash_table_lookup (eo->map_if_name_to_ei, interface_name);
|
|
|
|
|
if (ei == NULL)
|
|
|
|
|
{
|
|
|
|
|
GDBusMessage *reply;
|
|
|
|
|
reply = g_dbus_message_new_method_error (message,
|
2024-05-13 14:10:15 +02:00
|
|
|
|
DBUS_ERROR_INVALID_ARGS,
|
2018-06-25 16:51:00 +02:00
|
|
|
|
_("No such interface “%s”"),
|
2010-05-06 20:13:59 +02:00
|
|
|
|
interface_name);
|
2010-07-19 22:07:57 +02:00
|
|
|
|
g_dbus_connection_send_message_unlocked (eo->connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_object_unref (reply);
|
|
|
|
|
handled = TRUE;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handled = validate_and_maybe_schedule_property_get_all (eo->connection,
|
|
|
|
|
message,
|
2010-06-09 23:57:04 +02:00
|
|
|
|
ei->id,
|
|
|
|
|
0,
|
2010-06-30 18:10:00 +02:00
|
|
|
|
ei->interface_info,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
ei->vtable,
|
|
|
|
|
ei->context,
|
|
|
|
|
ei->user_data);
|
|
|
|
|
out:
|
|
|
|
|
return handled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
2010-05-09 19:09:54 +02:00
|
|
|
|
static const gchar introspect_header[] =
|
|
|
|
|
"<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\"\n"
|
|
|
|
|
" \"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n"
|
|
|
|
|
"<!-- GDBus " PACKAGE_VERSION " -->\n"
|
|
|
|
|
"<node>\n";
|
|
|
|
|
|
|
|
|
|
static const gchar introspect_tail[] =
|
|
|
|
|
"</node>\n";
|
|
|
|
|
|
2011-02-16 01:50:55 +01:00
|
|
|
|
static const gchar introspect_properties_interface[] =
|
2024-05-13 14:00:34 +02:00
|
|
|
|
" <interface name=\"" DBUS_INTERFACE_PROPERTIES "\">\n"
|
2010-05-09 19:09:54 +02:00
|
|
|
|
" <method name=\"Get\">\n"
|
|
|
|
|
" <arg type=\"s\" name=\"interface_name\" direction=\"in\"/>\n"
|
|
|
|
|
" <arg type=\"s\" name=\"property_name\" direction=\"in\"/>\n"
|
|
|
|
|
" <arg type=\"v\" name=\"value\" direction=\"out\"/>\n"
|
|
|
|
|
" </method>\n"
|
|
|
|
|
" <method name=\"GetAll\">\n"
|
|
|
|
|
" <arg type=\"s\" name=\"interface_name\" direction=\"in\"/>\n"
|
|
|
|
|
" <arg type=\"a{sv}\" name=\"properties\" direction=\"out\"/>\n"
|
|
|
|
|
" </method>\n"
|
|
|
|
|
" <method name=\"Set\">\n"
|
|
|
|
|
" <arg type=\"s\" name=\"interface_name\" direction=\"in\"/>\n"
|
|
|
|
|
" <arg type=\"s\" name=\"property_name\" direction=\"in\"/>\n"
|
|
|
|
|
" <arg type=\"v\" name=\"value\" direction=\"in\"/>\n"
|
|
|
|
|
" </method>\n"
|
|
|
|
|
" <signal name=\"PropertiesChanged\">\n"
|
|
|
|
|
" <arg type=\"s\" name=\"interface_name\"/>\n"
|
|
|
|
|
" <arg type=\"a{sv}\" name=\"changed_properties\"/>\n"
|
2010-05-13 17:56:15 +02:00
|
|
|
|
" <arg type=\"as\" name=\"invalidated_properties\"/>\n"
|
2010-05-09 19:09:54 +02:00
|
|
|
|
" </signal>\n"
|
2011-02-16 01:50:55 +01:00
|
|
|
|
" </interface>\n";
|
|
|
|
|
|
|
|
|
|
static const gchar introspect_introspectable_interface[] =
|
2024-05-13 14:00:34 +02:00
|
|
|
|
" <interface name=\"" DBUS_INTERFACE_INTROSPECTABLE "\">\n"
|
2010-05-09 19:09:54 +02:00
|
|
|
|
" <method name=\"Introspect\">\n"
|
|
|
|
|
" <arg type=\"s\" name=\"xml_data\" direction=\"out\"/>\n"
|
|
|
|
|
" </method>\n"
|
|
|
|
|
" </interface>\n"
|
2024-05-13 14:00:34 +02:00
|
|
|
|
" <interface name=\"" DBUS_INTERFACE_PEER "\">\n"
|
2010-05-09 19:09:54 +02:00
|
|
|
|
" <method name=\"Ping\"/>\n"
|
|
|
|
|
" <method name=\"GetMachineId\">\n"
|
|
|
|
|
" <arg type=\"s\" name=\"machine_uuid\" direction=\"out\"/>\n"
|
|
|
|
|
" </method>\n"
|
|
|
|
|
" </interface>\n";
|
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
static void
|
|
|
|
|
introspect_append_header (GString *s)
|
|
|
|
|
{
|
2010-05-09 19:09:54 +02:00
|
|
|
|
g_string_append (s, introspect_header);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
maybe_add_path (const gchar *path, gsize path_len, const gchar *object_path, GHashTable *set)
|
|
|
|
|
{
|
2010-07-01 19:06:56 +02:00
|
|
|
|
if (g_str_has_prefix (object_path, path) && strlen (object_path) > path_len && object_path[path_len-1] == '/')
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
|
|
|
|
const gchar *begin;
|
|
|
|
|
const gchar *end;
|
|
|
|
|
gchar *s;
|
|
|
|
|
|
|
|
|
|
begin = object_path + path_len;
|
|
|
|
|
end = strchr (begin, '/');
|
|
|
|
|
if (end != NULL)
|
2010-05-10 04:13:18 +02:00
|
|
|
|
s = g_strndup (begin, end - begin);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
else
|
2010-05-10 04:13:18 +02:00
|
|
|
|
s = g_strdup (begin);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2017-10-25 11:53:14 +02:00
|
|
|
|
if (!g_hash_table_contains (set, s))
|
|
|
|
|
g_hash_table_add (set, s);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
else
|
2010-05-10 04:13:18 +02:00
|
|
|
|
g_free (s);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* TODO: we want a nicer public interface for this */
|
2011-10-17 14:27:03 +02:00
|
|
|
|
/* called in any thread with connection's lock held */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
static gchar **
|
|
|
|
|
g_dbus_connection_list_registered_unlocked (GDBusConnection *connection,
|
|
|
|
|
const gchar *path)
|
|
|
|
|
{
|
|
|
|
|
GPtrArray *p;
|
|
|
|
|
gchar **ret;
|
|
|
|
|
GHashTableIter hash_iter;
|
|
|
|
|
const gchar *object_path;
|
|
|
|
|
gsize path_len;
|
|
|
|
|
GHashTable *set;
|
|
|
|
|
|
|
|
|
|
CONNECTION_ENSURE_LOCK (connection);
|
|
|
|
|
|
|
|
|
|
path_len = strlen (path);
|
|
|
|
|
if (path_len > 1)
|
|
|
|
|
path_len++;
|
|
|
|
|
|
|
|
|
|
set = g_hash_table_new (g_str_hash, g_str_equal);
|
|
|
|
|
|
2010-07-07 22:35:17 +02:00
|
|
|
|
g_hash_table_iter_init (&hash_iter, connection->map_object_path_to_eo);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
while (g_hash_table_iter_next (&hash_iter, (gpointer) &object_path, NULL))
|
|
|
|
|
maybe_add_path (path, path_len, object_path, set);
|
|
|
|
|
|
2010-07-07 22:35:17 +02:00
|
|
|
|
g_hash_table_iter_init (&hash_iter, connection->map_object_path_to_es);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
while (g_hash_table_iter_next (&hash_iter, (gpointer) &object_path, NULL))
|
|
|
|
|
maybe_add_path (path, path_len, object_path, set);
|
|
|
|
|
|
2022-12-08 02:47:39 +01:00
|
|
|
|
p = g_hash_table_steal_all_keys (set);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_hash_table_unref (set);
|
|
|
|
|
|
|
|
|
|
g_ptr_array_add (p, NULL);
|
|
|
|
|
ret = (gchar **) g_ptr_array_free (p, FALSE);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-17 14:27:03 +02:00
|
|
|
|
/* called in any thread with connection's lock not held */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
static gchar **
|
|
|
|
|
g_dbus_connection_list_registered (GDBusConnection *connection,
|
|
|
|
|
const gchar *path)
|
|
|
|
|
{
|
|
|
|
|
gchar **ret;
|
|
|
|
|
CONNECTION_LOCK (connection);
|
|
|
|
|
ret = g_dbus_connection_list_registered_unlocked (connection, path);
|
|
|
|
|
CONNECTION_UNLOCK (connection);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-17 14:27:03 +02:00
|
|
|
|
/* called in GDBusWorker thread with connection's lock held */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
static gboolean
|
|
|
|
|
handle_introspect (GDBusConnection *connection,
|
|
|
|
|
ExportedObject *eo,
|
|
|
|
|
GDBusMessage *message)
|
|
|
|
|
{
|
|
|
|
|
guint n;
|
|
|
|
|
GString *s;
|
|
|
|
|
GDBusMessage *reply;
|
|
|
|
|
GHashTableIter hash_iter;
|
|
|
|
|
ExportedInterface *ei;
|
|
|
|
|
gchar **registered;
|
|
|
|
|
|
|
|
|
|
/* first the header with the standard interfaces */
|
2010-05-09 19:09:54 +02:00
|
|
|
|
s = g_string_sized_new (sizeof (introspect_header) +
|
2011-02-16 01:50:55 +01:00
|
|
|
|
sizeof (introspect_properties_interface) +
|
|
|
|
|
sizeof (introspect_introspectable_interface) +
|
2010-05-09 19:09:54 +02:00
|
|
|
|
sizeof (introspect_tail));
|
2010-05-06 20:13:59 +02:00
|
|
|
|
introspect_append_header (s);
|
2011-02-16 01:50:55 +01:00
|
|
|
|
if (!g_hash_table_lookup (eo->map_if_name_to_ei,
|
2024-05-13 14:00:34 +02:00
|
|
|
|
DBUS_INTERFACE_PROPERTIES))
|
2011-02-16 01:50:55 +01:00
|
|
|
|
g_string_append (s, introspect_properties_interface);
|
|
|
|
|
|
|
|
|
|
if (!g_hash_table_lookup (eo->map_if_name_to_ei,
|
2024-05-13 14:00:34 +02:00
|
|
|
|
DBUS_INTERFACE_INTROSPECTABLE))
|
2011-02-16 01:50:55 +01:00
|
|
|
|
g_string_append (s, introspect_introspectable_interface);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
/* then include the registered interfaces */
|
|
|
|
|
g_hash_table_iter_init (&hash_iter, eo->map_if_name_to_ei);
|
|
|
|
|
while (g_hash_table_iter_next (&hash_iter, NULL, (gpointer) &ei))
|
2010-06-30 18:10:00 +02:00
|
|
|
|
g_dbus_interface_info_generate_xml (ei->interface_info, 2, s);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
/* finally include nodes registered below us */
|
|
|
|
|
registered = g_dbus_connection_list_registered_unlocked (connection, eo->object_path);
|
|
|
|
|
for (n = 0; registered != NULL && registered[n] != NULL; n++)
|
2010-05-10 04:13:18 +02:00
|
|
|
|
g_string_append_printf (s, " <node name=\"%s\"/>\n", registered[n]);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_strfreev (registered);
|
2010-05-09 19:09:54 +02:00
|
|
|
|
g_string_append (s, introspect_tail);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
reply = g_dbus_message_new_method_reply (message);
|
|
|
|
|
g_dbus_message_set_body (reply, g_variant_new ("(s)", s->str));
|
2010-07-19 22:07:57 +02:00
|
|
|
|
g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_object_unref (reply);
|
|
|
|
|
g_string_free (s, TRUE);
|
|
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* called in thread where object was registered - no locks held */
|
|
|
|
|
static gboolean
|
2010-05-10 17:47:08 +02:00
|
|
|
|
call_in_idle_cb (gpointer user_data)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
|
|
|
|
GDBusMethodInvocation *invocation = G_DBUS_METHOD_INVOCATION (user_data);
|
|
|
|
|
GDBusInterfaceVTable *vtable;
|
2010-06-09 23:57:04 +02:00
|
|
|
|
guint registration_id;
|
|
|
|
|
guint subtree_registration_id;
|
2021-09-24 11:52:41 +02:00
|
|
|
|
ExportedInterface *ei = NULL;
|
|
|
|
|
ExportedSubtree *es = NULL;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2010-06-09 23:57:04 +02:00
|
|
|
|
registration_id = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (invocation), "g-dbus-registration-id"));
|
|
|
|
|
subtree_registration_id = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (invocation), "g-dbus-subtree-registration-id"));
|
|
|
|
|
|
|
|
|
|
if (has_object_been_unregistered (g_dbus_method_invocation_get_connection (invocation),
|
|
|
|
|
registration_id,
|
2021-09-24 11:52:41 +02:00
|
|
|
|
&ei,
|
|
|
|
|
subtree_registration_id,
|
|
|
|
|
&es))
|
2010-06-09 23:57:04 +02:00
|
|
|
|
{
|
|
|
|
|
GDBusMessage *reply;
|
|
|
|
|
reply = g_dbus_message_new_method_error (g_dbus_method_invocation_get_message (invocation),
|
2024-05-13 14:10:15 +02:00
|
|
|
|
DBUS_ERROR_UNKNOWN_METHOD,
|
2018-06-08 16:15:54 +02:00
|
|
|
|
_("No such interface “%s” on object at path %s"),
|
2010-06-09 23:57:04 +02:00
|
|
|
|
g_dbus_method_invocation_get_interface_name (invocation),
|
|
|
|
|
g_dbus_method_invocation_get_object_path (invocation));
|
2010-07-19 22:07:57 +02:00
|
|
|
|
g_dbus_connection_send_message (g_dbus_method_invocation_get_connection (invocation), reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
|
2010-06-09 23:57:04 +02:00
|
|
|
|
g_object_unref (reply);
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-19 20:13:24 +01:00
|
|
|
|
vtable = g_object_get_data (G_OBJECT (invocation), "g-dbus-interface-vtable");
|
|
|
|
|
g_assert (vtable != NULL && vtable->method_call != NULL);
|
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
vtable->method_call (g_dbus_method_invocation_get_connection (invocation),
|
|
|
|
|
g_dbus_method_invocation_get_sender (invocation),
|
|
|
|
|
g_dbus_method_invocation_get_object_path (invocation),
|
|
|
|
|
g_dbus_method_invocation_get_interface_name (invocation),
|
|
|
|
|
g_dbus_method_invocation_get_method_name (invocation),
|
|
|
|
|
g_dbus_method_invocation_get_parameters (invocation),
|
|
|
|
|
g_object_ref (invocation),
|
|
|
|
|
g_dbus_method_invocation_get_user_data (invocation));
|
|
|
|
|
|
2010-06-09 23:57:04 +02:00
|
|
|
|
out:
|
2021-09-24 11:52:41 +02:00
|
|
|
|
g_clear_pointer (&ei, exported_interface_unref);
|
|
|
|
|
g_clear_pointer (&es, exported_subtree_unref);
|
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-17 15:24:36 +02:00
|
|
|
|
/* called in GDBusWorker thread with connection's lock held */
|
|
|
|
|
static void
|
|
|
|
|
schedule_method_call (GDBusConnection *connection,
|
|
|
|
|
GDBusMessage *message,
|
|
|
|
|
guint registration_id,
|
|
|
|
|
guint subtree_registration_id,
|
|
|
|
|
const GDBusInterfaceInfo *interface_info,
|
|
|
|
|
const GDBusMethodInfo *method_info,
|
2013-04-17 15:28:05 +02:00
|
|
|
|
const GDBusPropertyInfo *property_info,
|
2013-04-17 15:24:36 +02:00
|
|
|
|
GVariant *parameters,
|
|
|
|
|
const GDBusInterfaceVTable *vtable,
|
|
|
|
|
GMainContext *main_context,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
GDBusMethodInvocation *invocation;
|
|
|
|
|
GSource *idle_source;
|
|
|
|
|
|
|
|
|
|
invocation = _g_dbus_method_invocation_new (g_dbus_message_get_sender (message),
|
|
|
|
|
g_dbus_message_get_path (message),
|
|
|
|
|
g_dbus_message_get_interface (message),
|
|
|
|
|
g_dbus_message_get_member (message),
|
|
|
|
|
method_info,
|
2013-04-17 15:28:05 +02:00
|
|
|
|
property_info,
|
2013-04-17 15:24:36 +02:00
|
|
|
|
connection,
|
|
|
|
|
message,
|
|
|
|
|
parameters,
|
|
|
|
|
user_data);
|
|
|
|
|
|
|
|
|
|
/* TODO: would be nicer with a real MethodData like we already
|
|
|
|
|
* have PropertyData and PropertyGetAllData... */
|
|
|
|
|
g_object_set_data (G_OBJECT (invocation), "g-dbus-interface-vtable", (gpointer) vtable);
|
|
|
|
|
g_object_set_data (G_OBJECT (invocation), "g-dbus-registration-id", GUINT_TO_POINTER (registration_id));
|
|
|
|
|
g_object_set_data (G_OBJECT (invocation), "g-dbus-subtree-registration-id", GUINT_TO_POINTER (subtree_registration_id));
|
|
|
|
|
|
|
|
|
|
idle_source = g_idle_source_new ();
|
|
|
|
|
g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
|
|
|
|
|
g_source_set_callback (idle_source,
|
|
|
|
|
call_in_idle_cb,
|
2023-02-22 13:50:10 +01:00
|
|
|
|
g_steal_pointer (&invocation),
|
2013-04-17 15:24:36 +02:00
|
|
|
|
g_object_unref);
|
2021-07-26 11:53:02 +02:00
|
|
|
|
g_source_set_static_name (idle_source, "[gio, " __FILE__ "] call_in_idle_cb");
|
2013-04-17 15:24:36 +02:00
|
|
|
|
g_source_attach (idle_source, main_context);
|
|
|
|
|
g_source_unref (idle_source);
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-17 14:27:03 +02:00
|
|
|
|
/* called in GDBusWorker thread with connection's lock held */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
static gboolean
|
|
|
|
|
validate_and_maybe_schedule_method_call (GDBusConnection *connection,
|
|
|
|
|
GDBusMessage *message,
|
2010-06-09 23:57:04 +02:00
|
|
|
|
guint registration_id,
|
|
|
|
|
guint subtree_registration_id,
|
2010-07-19 21:45:27 +02:00
|
|
|
|
GDBusInterfaceInfo *interface_info,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
const GDBusInterfaceVTable *vtable,
|
|
|
|
|
GMainContext *main_context,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
2013-04-17 15:24:36 +02:00
|
|
|
|
GDBusMethodInfo *method_info;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
GDBusMessage *reply;
|
|
|
|
|
GVariant *parameters;
|
|
|
|
|
gboolean handled;
|
2010-05-24 22:46:24 +02:00
|
|
|
|
GVariantType *in_type;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
handled = FALSE;
|
|
|
|
|
|
|
|
|
|
/* TODO: the cost of this is O(n) - it might be worth caching the result */
|
2010-06-30 18:10:00 +02:00
|
|
|
|
method_info = g_dbus_interface_info_lookup_method (interface_info, g_dbus_message_get_member (message));
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2024-05-13 14:10:15 +02:00
|
|
|
|
/* if the method doesn't exist, return the DBUS_ERROR_UNKNOWN_METHOD
|
2010-05-06 20:13:59 +02:00
|
|
|
|
* error to the caller
|
|
|
|
|
*/
|
|
|
|
|
if (method_info == NULL)
|
|
|
|
|
{
|
|
|
|
|
reply = g_dbus_message_new_method_error (message,
|
2024-05-13 14:10:15 +02:00
|
|
|
|
DBUS_ERROR_UNKNOWN_METHOD,
|
2018-06-08 16:15:54 +02:00
|
|
|
|
_("No such method “%s”"),
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_dbus_message_get_member (message));
|
2010-07-19 22:07:57 +02:00
|
|
|
|
g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_object_unref (reply);
|
|
|
|
|
handled = TRUE;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-24 22:46:24 +02:00
|
|
|
|
parameters = g_dbus_message_get_body (message);
|
|
|
|
|
if (parameters == NULL)
|
|
|
|
|
{
|
|
|
|
|
parameters = g_variant_new ("()");
|
|
|
|
|
g_variant_ref_sink (parameters);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
g_variant_ref (parameters);
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
/* Check that the incoming args are of the right type - if they are not, return
|
2024-05-13 14:10:15 +02:00
|
|
|
|
* the DBUS_ERROR_INVALID_ARGS error to the caller
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*/
|
2010-05-24 22:46:24 +02:00
|
|
|
|
in_type = _g_dbus_compute_complete_signature (method_info->in_args);
|
|
|
|
|
if (!g_variant_is_of_type (parameters, in_type))
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
2010-05-24 22:46:24 +02:00
|
|
|
|
gchar *type_string;
|
|
|
|
|
|
|
|
|
|
type_string = g_variant_type_dup_string (in_type);
|
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
reply = g_dbus_message_new_method_error (message,
|
2024-05-13 14:10:15 +02:00
|
|
|
|
DBUS_ERROR_INVALID_ARGS,
|
2018-06-08 16:15:54 +02:00
|
|
|
|
_("Type of message, “%s”, does not match expected type “%s”"),
|
2010-05-24 22:46:24 +02:00
|
|
|
|
g_variant_get_type_string (parameters),
|
|
|
|
|
type_string);
|
2010-07-19 22:07:57 +02:00
|
|
|
|
g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
|
2010-05-24 22:46:24 +02:00
|
|
|
|
g_variant_type_free (in_type);
|
|
|
|
|
g_variant_unref (parameters);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_object_unref (reply);
|
2010-05-24 22:46:24 +02:00
|
|
|
|
g_free (type_string);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
handled = TRUE;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
2010-05-24 22:46:24 +02:00
|
|
|
|
g_variant_type_free (in_type);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
/* schedule the call in idle */
|
2013-04-17 15:24:36 +02:00
|
|
|
|
schedule_method_call (connection, message, registration_id, subtree_registration_id,
|
2013-04-17 15:28:05 +02:00
|
|
|
|
interface_info, method_info, NULL, parameters,
|
2013-04-17 15:24:36 +02:00
|
|
|
|
vtable, main_context, user_data);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_variant_unref (parameters);
|
|
|
|
|
handled = TRUE;
|
|
|
|
|
|
|
|
|
|
out:
|
|
|
|
|
return handled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
2011-10-17 14:27:03 +02:00
|
|
|
|
/* called in GDBusWorker thread with connection's lock held */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
static gboolean
|
|
|
|
|
obj_message_func (GDBusConnection *connection,
|
|
|
|
|
ExportedObject *eo,
|
2021-05-27 10:48:34 +02:00
|
|
|
|
GDBusMessage *message,
|
|
|
|
|
gboolean *object_found)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
|
|
|
|
const gchar *interface_name;
|
|
|
|
|
const gchar *member;
|
|
|
|
|
const gchar *signature;
|
|
|
|
|
gboolean handled;
|
|
|
|
|
|
|
|
|
|
handled = FALSE;
|
|
|
|
|
|
|
|
|
|
interface_name = g_dbus_message_get_interface (message);
|
|
|
|
|
member = g_dbus_message_get_member (message);
|
|
|
|
|
signature = g_dbus_message_get_signature (message);
|
|
|
|
|
|
|
|
|
|
/* see if we have an interface for handling this call */
|
|
|
|
|
if (interface_name != NULL)
|
|
|
|
|
{
|
|
|
|
|
ExportedInterface *ei;
|
|
|
|
|
ei = g_hash_table_lookup (eo->map_if_name_to_ei, interface_name);
|
|
|
|
|
if (ei != NULL)
|
|
|
|
|
{
|
|
|
|
|
/* we do - invoke the handler in idle in the right thread */
|
|
|
|
|
|
|
|
|
|
/* handle no vtable or handler being present */
|
|
|
|
|
if (ei->vtable == NULL || ei->vtable->method_call == NULL)
|
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
|
|
handled = validate_and_maybe_schedule_method_call (connection,
|
|
|
|
|
message,
|
2010-06-09 23:57:04 +02:00
|
|
|
|
ei->id,
|
|
|
|
|
0,
|
2010-06-30 18:10:00 +02:00
|
|
|
|
ei->interface_info,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
ei->vtable,
|
|
|
|
|
ei->context,
|
|
|
|
|
ei->user_data);
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
2021-05-27 10:48:34 +02:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
*object_found = TRUE;
|
|
|
|
|
}
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-13 14:00:34 +02:00
|
|
|
|
if (g_strcmp0 (interface_name, DBUS_INTERFACE_INTROSPECTABLE) == 0 &&
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_strcmp0 (member, "Introspect") == 0 &&
|
|
|
|
|
g_strcmp0 (signature, "") == 0)
|
|
|
|
|
{
|
|
|
|
|
handled = handle_introspect (connection, eo, message);
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
2024-05-13 14:00:34 +02:00
|
|
|
|
else if (g_strcmp0 (interface_name, DBUS_INTERFACE_PROPERTIES) == 0 &&
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_strcmp0 (member, "Get") == 0 &&
|
|
|
|
|
g_strcmp0 (signature, "ss") == 0)
|
|
|
|
|
{
|
|
|
|
|
handled = handle_getset_property (connection, eo, message, TRUE);
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
2024-05-13 14:00:34 +02:00
|
|
|
|
else if (g_strcmp0 (interface_name, DBUS_INTERFACE_PROPERTIES) == 0 &&
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_strcmp0 (member, "Set") == 0 &&
|
|
|
|
|
g_strcmp0 (signature, "ssv") == 0)
|
|
|
|
|
{
|
|
|
|
|
handled = handle_getset_property (connection, eo, message, FALSE);
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
2024-05-13 14:00:34 +02:00
|
|
|
|
else if (g_strcmp0 (interface_name, DBUS_INTERFACE_PROPERTIES) == 0 &&
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_strcmp0 (member, "GetAll") == 0 &&
|
|
|
|
|
g_strcmp0 (signature, "s") == 0)
|
|
|
|
|
{
|
|
|
|
|
handled = handle_get_all_properties (connection, eo, message);
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
out:
|
|
|
|
|
return handled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_dbus_connection_register_object:
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @connection: a #GDBusConnection
|
|
|
|
|
* @object_path: the object path to register at
|
|
|
|
|
* @interface_info: introspection data for the interface
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @vtable: (nullable): a #GDBusInterfaceVTable to call into or %NULL
|
|
|
|
|
* @user_data: (nullable): data to pass to functions in @vtable
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @user_data_free_func: function to call when the object path is unregistered
|
|
|
|
|
* @error: return location for error or %NULL
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
|
|
|
|
* Registers callbacks for exported objects at @object_path with the
|
2010-06-30 18:10:00 +02:00
|
|
|
|
* D-Bus interface that is described in @interface_info.
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
2014-02-08 18:26:56 +01:00
|
|
|
|
* Calls to functions in @vtable (and @user_data_free_func) will happen
|
|
|
|
|
* in the
|
|
|
|
|
* [thread-default main context][g-main-context-push-thread-default]
|
|
|
|
|
* of the thread you are calling this method from.
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
|
|
|
|
* Note that all #GVariant values passed to functions in @vtable will match
|
2010-06-30 18:10:00 +02:00
|
|
|
|
* the signature given in @interface_info - if a remote caller passes
|
2014-02-06 14:04:52 +01:00
|
|
|
|
* incorrect values, the `org.freedesktop.DBus.Error.InvalidArgs`
|
2010-05-06 20:13:59 +02:00
|
|
|
|
* is returned to the remote caller.
|
|
|
|
|
*
|
|
|
|
|
* Additionally, if the remote caller attempts to invoke methods or
|
2010-06-30 18:10:00 +02:00
|
|
|
|
* access properties not mentioned in @interface_info the
|
2014-02-06 14:04:52 +01:00
|
|
|
|
* `org.freedesktop.DBus.Error.UnknownMethod` resp.
|
|
|
|
|
* `org.freedesktop.DBus.Error.InvalidArgs` errors
|
2010-05-06 20:13:59 +02:00
|
|
|
|
* are returned to the caller.
|
|
|
|
|
*
|
|
|
|
|
* It is considered a programming error if the
|
|
|
|
|
* #GDBusInterfaceGetPropertyFunc function in @vtable returns a
|
|
|
|
|
* #GVariant of incorrect type.
|
|
|
|
|
*
|
|
|
|
|
* If an existing callback is already registered at @object_path and
|
2022-03-04 17:21:55 +01:00
|
|
|
|
* @interface_name, then @error is set to %G_IO_ERROR_EXISTS.
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
2010-05-13 03:51:06 +02:00
|
|
|
|
* GDBus automatically implements the standard D-Bus interfaces
|
|
|
|
|
* org.freedesktop.DBus.Properties, org.freedesktop.DBus.Introspectable
|
2014-02-01 02:34:33 +01:00
|
|
|
|
* and org.freedesktop.Peer, so you don't have to implement those for the
|
|
|
|
|
* objects you export. You can implement org.freedesktop.DBus.Properties
|
|
|
|
|
* yourself, e.g. to handle getting and setting of properties asynchronously.
|
2010-05-13 03:51:06 +02:00
|
|
|
|
*
|
2010-07-14 20:46:12 +02:00
|
|
|
|
* Note that the reference count on @interface_info will be
|
|
|
|
|
* incremented by 1 (unless allocated statically, e.g. if the
|
|
|
|
|
* reference count is -1, see g_dbus_interface_info_ref()) for as long
|
|
|
|
|
* as the object is exported. Also note that @vtable will be copied.
|
|
|
|
|
*
|
2014-02-08 18:26:56 +01:00
|
|
|
|
* See this [server][gdbus-server] for an example of how to use this method.
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
|
|
|
|
* Returns: 0 if @error is set, otherwise a registration id (never 0)
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* that can be used with g_dbus_connection_unregister_object()
|
2010-05-06 22:02:08 +02:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.26
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*/
|
|
|
|
|
guint
|
2014-02-01 05:42:40 +01:00
|
|
|
|
g_dbus_connection_register_object (GDBusConnection *connection,
|
|
|
|
|
const gchar *object_path,
|
|
|
|
|
GDBusInterfaceInfo *interface_info,
|
|
|
|
|
const GDBusInterfaceVTable *vtable,
|
|
|
|
|
gpointer user_data,
|
|
|
|
|
GDestroyNotify user_data_free_func,
|
|
|
|
|
GError **error)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
|
|
|
|
ExportedObject *eo;
|
|
|
|
|
ExportedInterface *ei;
|
|
|
|
|
guint ret;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), 0);
|
|
|
|
|
g_return_val_if_fail (object_path != NULL && g_variant_is_object_path (object_path), 0);
|
2010-06-30 18:10:00 +02:00
|
|
|
|
g_return_val_if_fail (interface_info != NULL, 0);
|
|
|
|
|
g_return_val_if_fail (g_dbus_is_interface_name (interface_info->name), 0);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, 0);
|
2011-10-19 18:23:30 +02:00
|
|
|
|
g_return_val_if_fail (check_initialized (connection), 0);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
|
|
CONNECTION_LOCK (connection);
|
|
|
|
|
|
2010-07-07 22:35:17 +02:00
|
|
|
|
eo = g_hash_table_lookup (connection->map_object_path_to_eo, object_path);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
if (eo == NULL)
|
|
|
|
|
{
|
|
|
|
|
eo = g_new0 (ExportedObject, 1);
|
|
|
|
|
eo->object_path = g_strdup (object_path);
|
|
|
|
|
eo->connection = connection;
|
|
|
|
|
eo->map_if_name_to_ei = g_hash_table_new_full (g_str_hash,
|
|
|
|
|
g_str_equal,
|
|
|
|
|
NULL,
|
2021-09-24 10:03:40 +02:00
|
|
|
|
(GDestroyNotify) exported_interface_unref);
|
2010-07-07 22:35:17 +02:00
|
|
|
|
g_hash_table_insert (connection->map_object_path_to_eo, eo->object_path, eo);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
2010-06-30 18:10:00 +02:00
|
|
|
|
ei = g_hash_table_lookup (eo->map_if_name_to_ei, interface_info->name);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
if (ei != NULL)
|
|
|
|
|
{
|
|
|
|
|
g_set_error (error,
|
|
|
|
|
G_IO_ERROR,
|
|
|
|
|
G_IO_ERROR_EXISTS,
|
|
|
|
|
_("An object is already exported for the interface %s at %s"),
|
2010-06-30 18:10:00 +02:00
|
|
|
|
interface_info->name,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
object_path);
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ei = g_new0 (ExportedInterface, 1);
|
2021-09-24 10:03:40 +02:00
|
|
|
|
ei->refcount = 1;
|
2019-09-21 10:45:36 +02:00
|
|
|
|
ei->id = (guint) g_atomic_int_add (&_global_registration_id, 1); /* TODO: overflow etc. */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
ei->eo = eo;
|
|
|
|
|
ei->user_data = user_data;
|
|
|
|
|
ei->user_data_free_func = user_data_free_func;
|
2010-07-14 20:46:12 +02:00
|
|
|
|
ei->vtable = _g_dbus_interface_vtable_copy (vtable);
|
2010-07-19 21:45:27 +02:00
|
|
|
|
ei->interface_info = g_dbus_interface_info_ref (interface_info);
|
2011-03-28 17:33:36 +02:00
|
|
|
|
g_dbus_interface_info_cache_build (ei->interface_info);
|
2010-06-30 18:10:00 +02:00
|
|
|
|
ei->interface_name = g_strdup (interface_info->name);
|
2011-10-05 16:46:57 +02:00
|
|
|
|
ei->context = g_main_context_ref_thread_default ();
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
g_hash_table_insert (eo->map_if_name_to_ei,
|
|
|
|
|
(gpointer) ei->interface_name,
|
|
|
|
|
ei);
|
2010-07-07 22:35:17 +02:00
|
|
|
|
g_hash_table_insert (connection->map_id_to_ei,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
GUINT_TO_POINTER (ei->id),
|
|
|
|
|
ei);
|
|
|
|
|
|
|
|
|
|
ret = ei->id;
|
|
|
|
|
|
|
|
|
|
out:
|
|
|
|
|
CONNECTION_UNLOCK (connection);
|
|
|
|
|
|
2024-04-10 01:44:40 +02:00
|
|
|
|
if (ret == 0 && user_data_free_func != NULL)
|
|
|
|
|
user_data_free_func (user_data);
|
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_dbus_connection_unregister_object:
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @connection: a #GDBusConnection
|
|
|
|
|
* @registration_id: a registration id obtained from
|
|
|
|
|
* g_dbus_connection_register_object()
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
|
|
|
|
* Unregisters an object.
|
|
|
|
|
*
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* Returns: %TRUE if the object was unregistered, %FALSE otherwise
|
2010-05-06 22:02:08 +02:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.26
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*/
|
|
|
|
|
gboolean
|
|
|
|
|
g_dbus_connection_unregister_object (GDBusConnection *connection,
|
|
|
|
|
guint registration_id)
|
|
|
|
|
{
|
|
|
|
|
ExportedInterface *ei;
|
|
|
|
|
ExportedObject *eo;
|
|
|
|
|
gboolean ret;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
|
2011-10-19 18:23:30 +02:00
|
|
|
|
g_return_val_if_fail (check_initialized (connection), FALSE);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
ret = FALSE;
|
|
|
|
|
|
|
|
|
|
CONNECTION_LOCK (connection);
|
|
|
|
|
|
2010-07-07 22:35:17 +02:00
|
|
|
|
ei = g_hash_table_lookup (connection->map_id_to_ei,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
GUINT_TO_POINTER (registration_id));
|
|
|
|
|
if (ei == NULL)
|
2010-05-10 04:13:18 +02:00
|
|
|
|
goto out;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
eo = ei->eo;
|
|
|
|
|
|
2010-07-07 22:35:17 +02:00
|
|
|
|
g_warn_if_fail (g_hash_table_remove (connection->map_id_to_ei, GUINT_TO_POINTER (ei->id)));
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_warn_if_fail (g_hash_table_remove (eo->map_if_name_to_ei, ei->interface_name));
|
|
|
|
|
/* unregister object path if we have no more exported interfaces */
|
|
|
|
|
if (g_hash_table_size (eo->map_if_name_to_ei) == 0)
|
2010-07-07 22:35:17 +02:00
|
|
|
|
g_warn_if_fail (g_hash_table_remove (connection->map_object_path_to_eo,
|
2010-05-10 04:13:18 +02:00
|
|
|
|
eo->object_path));
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
ret = TRUE;
|
|
|
|
|
|
|
|
|
|
out:
|
|
|
|
|
CONNECTION_UNLOCK (connection);
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-10 22:58:20 +02:00
|
|
|
|
typedef struct {
|
|
|
|
|
GClosure *method_call_closure;
|
|
|
|
|
GClosure *get_property_closure;
|
|
|
|
|
GClosure *set_property_closure;
|
|
|
|
|
} RegisterObjectData;
|
|
|
|
|
|
|
|
|
|
static RegisterObjectData *
|
|
|
|
|
register_object_data_new (GClosure *method_call_closure,
|
|
|
|
|
GClosure *get_property_closure,
|
|
|
|
|
GClosure *set_property_closure)
|
|
|
|
|
{
|
|
|
|
|
RegisterObjectData *data;
|
|
|
|
|
|
|
|
|
|
data = g_new0 (RegisterObjectData, 1);
|
|
|
|
|
|
|
|
|
|
if (method_call_closure != NULL)
|
|
|
|
|
{
|
|
|
|
|
data->method_call_closure = g_closure_ref (method_call_closure);
|
|
|
|
|
g_closure_sink (method_call_closure);
|
|
|
|
|
if (G_CLOSURE_NEEDS_MARSHAL (method_call_closure))
|
|
|
|
|
g_closure_set_marshal (method_call_closure, g_cclosure_marshal_generic);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (get_property_closure != NULL)
|
|
|
|
|
{
|
|
|
|
|
data->get_property_closure = g_closure_ref (get_property_closure);
|
|
|
|
|
g_closure_sink (get_property_closure);
|
|
|
|
|
if (G_CLOSURE_NEEDS_MARSHAL (get_property_closure))
|
|
|
|
|
g_closure_set_marshal (get_property_closure, g_cclosure_marshal_generic);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (set_property_closure != NULL)
|
|
|
|
|
{
|
|
|
|
|
data->set_property_closure = g_closure_ref (set_property_closure);
|
|
|
|
|
g_closure_sink (set_property_closure);
|
|
|
|
|
if (G_CLOSURE_NEEDS_MARSHAL (set_property_closure))
|
|
|
|
|
g_closure_set_marshal (set_property_closure, g_cclosure_marshal_generic);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
register_object_free_func (gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
RegisterObjectData *data = user_data;
|
|
|
|
|
|
|
|
|
|
g_clear_pointer (&data->method_call_closure, g_closure_unref);
|
|
|
|
|
g_clear_pointer (&data->get_property_closure, g_closure_unref);
|
|
|
|
|
g_clear_pointer (&data->set_property_closure, g_closure_unref);
|
|
|
|
|
|
|
|
|
|
g_free (data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
register_with_closures_on_method_call (GDBusConnection *connection,
|
|
|
|
|
const gchar *sender,
|
|
|
|
|
const gchar *object_path,
|
|
|
|
|
const gchar *interface_name,
|
|
|
|
|
const gchar *method_name,
|
|
|
|
|
GVariant *parameters,
|
|
|
|
|
GDBusMethodInvocation *invocation,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
RegisterObjectData *data = user_data;
|
|
|
|
|
GValue params[] = { G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT };
|
|
|
|
|
|
|
|
|
|
g_value_init (¶ms[0], G_TYPE_DBUS_CONNECTION);
|
|
|
|
|
g_value_set_object (¶ms[0], connection);
|
|
|
|
|
|
|
|
|
|
g_value_init (¶ms[1], G_TYPE_STRING);
|
|
|
|
|
g_value_set_string (¶ms[1], sender);
|
|
|
|
|
|
|
|
|
|
g_value_init (¶ms[2], G_TYPE_STRING);
|
|
|
|
|
g_value_set_string (¶ms[2], object_path);
|
|
|
|
|
|
|
|
|
|
g_value_init (¶ms[3], G_TYPE_STRING);
|
|
|
|
|
g_value_set_string (¶ms[3], interface_name);
|
|
|
|
|
|
|
|
|
|
g_value_init (¶ms[4], G_TYPE_STRING);
|
|
|
|
|
g_value_set_string (¶ms[4], method_name);
|
|
|
|
|
|
|
|
|
|
g_value_init (¶ms[5], G_TYPE_VARIANT);
|
|
|
|
|
g_value_set_variant (¶ms[5], parameters);
|
|
|
|
|
|
|
|
|
|
g_value_init (¶ms[6], G_TYPE_DBUS_METHOD_INVOCATION);
|
2024-12-02 17:46:17 +01:00
|
|
|
|
g_value_take_object (¶ms[6], g_steal_pointer (&invocation));
|
2014-10-10 22:58:20 +02:00
|
|
|
|
|
|
|
|
|
g_closure_invoke (data->method_call_closure, NULL, G_N_ELEMENTS (params), params, NULL);
|
|
|
|
|
|
|
|
|
|
g_value_unset (params + 0);
|
|
|
|
|
g_value_unset (params + 1);
|
|
|
|
|
g_value_unset (params + 2);
|
|
|
|
|
g_value_unset (params + 3);
|
|
|
|
|
g_value_unset (params + 4);
|
|
|
|
|
g_value_unset (params + 5);
|
|
|
|
|
g_value_unset (params + 6);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static GVariant *
|
|
|
|
|
register_with_closures_on_get_property (GDBusConnection *connection,
|
|
|
|
|
const gchar *sender,
|
|
|
|
|
const gchar *object_path,
|
|
|
|
|
const gchar *interface_name,
|
|
|
|
|
const gchar *property_name,
|
|
|
|
|
GError **error,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
RegisterObjectData *data = user_data;
|
|
|
|
|
GValue params[] = { G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT };
|
|
|
|
|
GValue result_value = G_VALUE_INIT;
|
|
|
|
|
GVariant *result;
|
|
|
|
|
|
|
|
|
|
g_value_init (¶ms[0], G_TYPE_DBUS_CONNECTION);
|
|
|
|
|
g_value_set_object (¶ms[0], connection);
|
|
|
|
|
|
|
|
|
|
g_value_init (¶ms[1], G_TYPE_STRING);
|
|
|
|
|
g_value_set_string (¶ms[1], sender);
|
|
|
|
|
|
|
|
|
|
g_value_init (¶ms[2], G_TYPE_STRING);
|
|
|
|
|
g_value_set_string (¶ms[2], object_path);
|
|
|
|
|
|
|
|
|
|
g_value_init (¶ms[3], G_TYPE_STRING);
|
|
|
|
|
g_value_set_string (¶ms[3], interface_name);
|
|
|
|
|
|
|
|
|
|
g_value_init (¶ms[4], G_TYPE_STRING);
|
|
|
|
|
g_value_set_string (¶ms[4], property_name);
|
|
|
|
|
|
|
|
|
|
g_value_init (&result_value, G_TYPE_VARIANT);
|
|
|
|
|
|
|
|
|
|
g_closure_invoke (data->get_property_closure, &result_value, G_N_ELEMENTS (params), params, NULL);
|
|
|
|
|
|
|
|
|
|
result = g_value_get_variant (&result_value);
|
|
|
|
|
if (result)
|
|
|
|
|
g_variant_ref (result);
|
|
|
|
|
|
|
|
|
|
g_value_unset (params + 0);
|
|
|
|
|
g_value_unset (params + 1);
|
|
|
|
|
g_value_unset (params + 2);
|
|
|
|
|
g_value_unset (params + 3);
|
|
|
|
|
g_value_unset (params + 4);
|
|
|
|
|
g_value_unset (&result_value);
|
|
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
|
g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
|
|
|
|
|
_("Unable to retrieve property %s.%s"),
|
|
|
|
|
interface_name, property_name);
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
|
register_with_closures_on_set_property (GDBusConnection *connection,
|
|
|
|
|
const gchar *sender,
|
|
|
|
|
const gchar *object_path,
|
|
|
|
|
const gchar *interface_name,
|
|
|
|
|
const gchar *property_name,
|
|
|
|
|
GVariant *value,
|
|
|
|
|
GError **error,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
RegisterObjectData *data = user_data;
|
|
|
|
|
GValue params[] = { G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT };
|
|
|
|
|
GValue result_value = G_VALUE_INIT;
|
|
|
|
|
gboolean result;
|
|
|
|
|
|
|
|
|
|
g_value_init (¶ms[0], G_TYPE_DBUS_CONNECTION);
|
|
|
|
|
g_value_set_object (¶ms[0], connection);
|
|
|
|
|
|
|
|
|
|
g_value_init (¶ms[1], G_TYPE_STRING);
|
|
|
|
|
g_value_set_string (¶ms[1], sender);
|
|
|
|
|
|
|
|
|
|
g_value_init (¶ms[2], G_TYPE_STRING);
|
|
|
|
|
g_value_set_string (¶ms[2], object_path);
|
|
|
|
|
|
|
|
|
|
g_value_init (¶ms[3], G_TYPE_STRING);
|
|
|
|
|
g_value_set_string (¶ms[3], interface_name);
|
|
|
|
|
|
|
|
|
|
g_value_init (¶ms[4], G_TYPE_STRING);
|
|
|
|
|
g_value_set_string (¶ms[4], property_name);
|
|
|
|
|
|
|
|
|
|
g_value_init (¶ms[5], G_TYPE_VARIANT);
|
|
|
|
|
g_value_set_variant (¶ms[5], value);
|
|
|
|
|
|
|
|
|
|
g_value_init (&result_value, G_TYPE_BOOLEAN);
|
|
|
|
|
|
|
|
|
|
g_closure_invoke (data->set_property_closure, &result_value, G_N_ELEMENTS (params), params, NULL);
|
|
|
|
|
|
|
|
|
|
result = g_value_get_boolean (&result_value);
|
|
|
|
|
|
|
|
|
|
g_value_unset (params + 0);
|
|
|
|
|
g_value_unset (params + 1);
|
|
|
|
|
g_value_unset (params + 2);
|
|
|
|
|
g_value_unset (params + 3);
|
|
|
|
|
g_value_unset (params + 4);
|
|
|
|
|
g_value_unset (params + 5);
|
|
|
|
|
g_value_unset (&result_value);
|
|
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
|
g_set_error (error,
|
|
|
|
|
G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
|
|
|
|
|
_("Unable to set property %s.%s"),
|
|
|
|
|
interface_name, property_name);
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_dbus_connection_register_object_with_closures: (rename-to g_dbus_connection_register_object)
|
|
|
|
|
* @connection: A #GDBusConnection.
|
|
|
|
|
* @object_path: The object path to register at.
|
|
|
|
|
* @interface_info: Introspection data for the interface.
|
|
|
|
|
* @method_call_closure: (nullable): #GClosure for handling incoming method calls.
|
|
|
|
|
* @get_property_closure: (nullable): #GClosure for getting a property.
|
|
|
|
|
* @set_property_closure: (nullable): #GClosure for setting a property.
|
|
|
|
|
* @error: Return location for error or %NULL.
|
|
|
|
|
*
|
|
|
|
|
* Version of g_dbus_connection_register_object() using closures instead of a
|
|
|
|
|
* #GDBusInterfaceVTable for easier binding in other languages.
|
|
|
|
|
*
|
2020-12-15 10:14:20 +01:00
|
|
|
|
* Returns: 0 if @error is set, otherwise a registration ID (never 0)
|
2014-10-10 22:58:20 +02:00
|
|
|
|
* that can be used with g_dbus_connection_unregister_object() .
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.46
|
|
|
|
|
*/
|
|
|
|
|
guint
|
|
|
|
|
g_dbus_connection_register_object_with_closures (GDBusConnection *connection,
|
|
|
|
|
const gchar *object_path,
|
|
|
|
|
GDBusInterfaceInfo *interface_info,
|
|
|
|
|
GClosure *method_call_closure,
|
|
|
|
|
GClosure *get_property_closure,
|
|
|
|
|
GClosure *set_property_closure,
|
|
|
|
|
GError **error)
|
|
|
|
|
{
|
|
|
|
|
RegisterObjectData *data;
|
|
|
|
|
GDBusInterfaceVTable vtable =
|
|
|
|
|
{
|
|
|
|
|
method_call_closure != NULL ? register_with_closures_on_method_call : NULL,
|
|
|
|
|
get_property_closure != NULL ? register_with_closures_on_get_property : NULL,
|
2020-11-17 20:57:14 +01:00
|
|
|
|
set_property_closure != NULL ? register_with_closures_on_set_property : NULL,
|
|
|
|
|
{ 0 }
|
2014-10-10 22:58:20 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
data = register_object_data_new (method_call_closure, get_property_closure, set_property_closure);
|
|
|
|
|
|
|
|
|
|
return g_dbus_connection_register_object (connection,
|
|
|
|
|
object_path,
|
|
|
|
|
interface_info,
|
|
|
|
|
&vtable,
|
2024-04-10 01:07:08 +02:00
|
|
|
|
g_steal_pointer (&data),
|
2014-10-10 22:58:20 +02:00
|
|
|
|
register_object_free_func,
|
|
|
|
|
error);
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_dbus_connection_emit_signal:
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @connection: a #GDBusConnection
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @destination_bus_name: (nullable): the unique bus name for the destination
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* for the signal or %NULL to emit to all listeners
|
|
|
|
|
* @object_path: path of remote object
|
|
|
|
|
* @interface_name: D-Bus interface to emit a signal on
|
|
|
|
|
* @signal_name: the name of the signal to emit
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @parameters: (nullable): a #GVariant tuple with parameters for the signal
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* or %NULL if not passing parameters
|
|
|
|
|
* @error: Return location for error or %NULL
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
|
|
|
|
* Emits a signal.
|
|
|
|
|
*
|
2010-05-14 03:53:51 +02:00
|
|
|
|
* If the parameters GVariant is floating, it is consumed.
|
|
|
|
|
*
|
2018-02-15 17:42:26 +01:00
|
|
|
|
* This can only fail if @parameters is not compatible with the D-Bus protocol
|
|
|
|
|
* (%G_IO_ERROR_INVALID_ARGUMENT), or if @connection has been closed
|
|
|
|
|
* (%G_IO_ERROR_CLOSED).
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* Returns: %TRUE unless @error is set
|
2010-05-06 22:02:08 +02:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.26
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*/
|
|
|
|
|
gboolean
|
2010-05-10 04:13:18 +02:00
|
|
|
|
g_dbus_connection_emit_signal (GDBusConnection *connection,
|
|
|
|
|
const gchar *destination_bus_name,
|
|
|
|
|
const gchar *object_path,
|
|
|
|
|
const gchar *interface_name,
|
|
|
|
|
const gchar *signal_name,
|
|
|
|
|
GVariant *parameters,
|
|
|
|
|
GError **error)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
|
|
|
|
GDBusMessage *message;
|
|
|
|
|
gboolean ret;
|
|
|
|
|
|
|
|
|
|
message = NULL;
|
|
|
|
|
ret = FALSE;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
|
|
|
|
|
g_return_val_if_fail (destination_bus_name == NULL || g_dbus_is_name (destination_bus_name), FALSE);
|
|
|
|
|
g_return_val_if_fail (object_path != NULL && g_variant_is_object_path (object_path), FALSE);
|
|
|
|
|
g_return_val_if_fail (interface_name != NULL && g_dbus_is_interface_name (interface_name), FALSE);
|
|
|
|
|
g_return_val_if_fail (signal_name != NULL && g_dbus_is_member_name (signal_name), FALSE);
|
|
|
|
|
g_return_val_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE), FALSE);
|
2011-10-19 18:23:30 +02:00
|
|
|
|
g_return_val_if_fail (check_initialized (connection), FALSE);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2010-07-01 20:37:16 +02:00
|
|
|
|
if (G_UNLIKELY (_g_dbus_debug_emission ()))
|
|
|
|
|
{
|
|
|
|
|
_g_dbus_debug_print_lock ();
|
|
|
|
|
g_print ("========================================================================\n"
|
|
|
|
|
"GDBus-debug:Emission:\n"
|
|
|
|
|
" >>>> SIGNAL EMISSION %s.%s()\n"
|
|
|
|
|
" on object %s\n"
|
|
|
|
|
" destination %s\n",
|
|
|
|
|
interface_name, signal_name,
|
|
|
|
|
object_path,
|
|
|
|
|
destination_bus_name != NULL ? destination_bus_name : "(none)");
|
|
|
|
|
_g_dbus_debug_print_unlock ();
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
message = g_dbus_message_new_signal (object_path,
|
|
|
|
|
interface_name,
|
|
|
|
|
signal_name);
|
|
|
|
|
|
|
|
|
|
if (destination_bus_name != NULL)
|
2010-05-10 04:13:18 +02:00
|
|
|
|
g_dbus_message_set_header (message,
|
|
|
|
|
G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION,
|
|
|
|
|
g_variant_new_string (destination_bus_name));
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
if (parameters != NULL)
|
|
|
|
|
g_dbus_message_set_body (message, parameters);
|
|
|
|
|
|
2010-07-19 22:07:57 +02:00
|
|
|
|
ret = g_dbus_connection_send_message (connection, message, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, error);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_object_unref (message);
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2010-05-10 17:47:08 +02:00
|
|
|
|
add_call_flags (GDBusMessage *message,
|
|
|
|
|
GDBusCallFlags flags)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
2014-11-04 12:54:00 +01:00
|
|
|
|
GDBusMessageFlags msg_flags = 0;
|
|
|
|
|
|
2010-05-10 17:47:08 +02:00
|
|
|
|
if (flags & G_DBUS_CALL_FLAGS_NO_AUTO_START)
|
2014-11-04 12:54:00 +01:00
|
|
|
|
msg_flags |= G_DBUS_MESSAGE_FLAGS_NO_AUTO_START;
|
|
|
|
|
if (flags & G_DBUS_CALL_FLAGS_ALLOW_INTERACTIVE_AUTHORIZATION)
|
|
|
|
|
msg_flags |= G_DBUS_MESSAGE_FLAGS_ALLOW_INTERACTIVE_AUTHORIZATION;
|
|
|
|
|
if (msg_flags)
|
|
|
|
|
g_dbus_message_set_flags (message, msg_flags);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
2010-05-24 22:46:24 +02:00
|
|
|
|
static GVariant *
|
|
|
|
|
decode_method_reply (GDBusMessage *reply,
|
|
|
|
|
const gchar *method_name,
|
|
|
|
|
const GVariantType *reply_type,
|
2011-07-21 22:03:27 +02:00
|
|
|
|
GUnixFDList **out_fd_list,
|
2010-05-24 22:46:24 +02:00
|
|
|
|
GError **error)
|
|
|
|
|
{
|
|
|
|
|
GVariant *result;
|
|
|
|
|
|
|
|
|
|
result = NULL;
|
|
|
|
|
switch (g_dbus_message_get_message_type (reply))
|
|
|
|
|
{
|
|
|
|
|
case G_DBUS_MESSAGE_TYPE_METHOD_RETURN:
|
|
|
|
|
result = g_dbus_message_get_body (reply);
|
|
|
|
|
if (result == NULL)
|
|
|
|
|
{
|
|
|
|
|
result = g_variant_new ("()");
|
|
|
|
|
g_variant_ref_sink (result);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
g_variant_ref (result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!g_variant_is_of_type (result, reply_type))
|
|
|
|
|
{
|
|
|
|
|
gchar *type_string = g_variant_type_dup_string (reply_type);
|
|
|
|
|
|
|
|
|
|
g_set_error (error,
|
|
|
|
|
G_IO_ERROR,
|
|
|
|
|
G_IO_ERROR_INVALID_ARGUMENT,
|
2018-06-08 16:15:54 +02:00
|
|
|
|
_("Method “%s” returned type “%s”, but expected “%s”"),
|
2010-05-24 22:46:24 +02:00
|
|
|
|
method_name, g_variant_get_type_string (result), type_string);
|
|
|
|
|
|
|
|
|
|
g_variant_unref (result);
|
|
|
|
|
g_free (type_string);
|
|
|
|
|
result = NULL;
|
|
|
|
|
}
|
2011-07-21 22:03:27 +02:00
|
|
|
|
|
|
|
|
|
#ifdef G_OS_UNIX
|
|
|
|
|
if (result != NULL)
|
|
|
|
|
{
|
|
|
|
|
if (out_fd_list != NULL)
|
|
|
|
|
{
|
|
|
|
|
*out_fd_list = g_dbus_message_get_unix_fd_list (reply);
|
|
|
|
|
if (*out_fd_list != NULL)
|
|
|
|
|
g_object_ref (*out_fd_list);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2010-05-24 22:46:24 +02:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case G_DBUS_MESSAGE_TYPE_ERROR:
|
|
|
|
|
g_dbus_message_to_gerror (reply, error);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
g_assert_not_reached ();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
|
{
|
|
|
|
|
GVariantType *reply_type;
|
|
|
|
|
gchar *method_name; /* for error message */
|
2011-07-21 22:03:27 +02:00
|
|
|
|
|
|
|
|
|
GUnixFDList *fd_list;
|
2010-05-24 22:46:24 +02:00
|
|
|
|
} CallState;
|
|
|
|
|
|
2011-07-21 22:03:27 +02:00
|
|
|
|
static void
|
|
|
|
|
call_state_free (CallState *state)
|
|
|
|
|
{
|
|
|
|
|
g_variant_type_free (state->reply_type);
|
|
|
|
|
g_free (state->method_name);
|
|
|
|
|
|
|
|
|
|
if (state->fd_list != NULL)
|
|
|
|
|
g_object_unref (state->fd_list);
|
|
|
|
|
g_slice_free (CallState, state);
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-17 14:27:03 +02:00
|
|
|
|
/* called in any thread, with the connection's lock not held */
|
2010-05-24 22:46:24 +02:00
|
|
|
|
static void
|
|
|
|
|
g_dbus_connection_call_done (GObject *source,
|
|
|
|
|
GAsyncResult *result,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
GDBusConnection *connection = G_DBUS_CONNECTION (source);
|
2012-08-02 21:46:32 +02:00
|
|
|
|
GTask *task = user_data;
|
|
|
|
|
CallState *state = g_task_get_task_data (task);
|
|
|
|
|
GError *error = NULL;
|
2010-05-24 22:46:24 +02:00
|
|
|
|
GDBusMessage *reply;
|
2012-08-02 21:46:32 +02:00
|
|
|
|
GVariant *value = NULL;
|
2010-05-24 22:46:24 +02:00
|
|
|
|
|
|
|
|
|
reply = g_dbus_connection_send_message_with_reply_finish (connection,
|
2010-07-01 20:37:16 +02:00
|
|
|
|
result,
|
|
|
|
|
&error);
|
|
|
|
|
|
|
|
|
|
if (G_UNLIKELY (_g_dbus_debug_call ()))
|
|
|
|
|
{
|
|
|
|
|
_g_dbus_debug_print_lock ();
|
|
|
|
|
g_print ("========================================================================\n"
|
|
|
|
|
"GDBus-debug:Call:\n"
|
2019-12-11 11:38:24 +01:00
|
|
|
|
" <<<< ASYNC COMPLETE %s()",
|
|
|
|
|
state->method_name);
|
|
|
|
|
|
2010-07-01 20:37:16 +02:00
|
|
|
|
if (reply != NULL)
|
|
|
|
|
{
|
2019-12-11 11:38:24 +01:00
|
|
|
|
g_print (" (serial %d)\n"
|
|
|
|
|
" SUCCESS\n",
|
|
|
|
|
g_dbus_message_get_reply_serial (reply));
|
2010-07-01 20:37:16 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-12-11 11:38:24 +01:00
|
|
|
|
g_print ("\n"
|
|
|
|
|
" FAILED: %s\n",
|
2010-07-01 20:37:16 +02:00
|
|
|
|
error->message);
|
|
|
|
|
}
|
|
|
|
|
_g_dbus_debug_print_unlock ();
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-24 22:46:24 +02:00
|
|
|
|
if (reply != NULL)
|
2012-08-02 21:46:32 +02:00
|
|
|
|
value = decode_method_reply (reply, state->method_name, state->reply_type, &state->fd_list, &error);
|
2011-07-21 22:03:27 +02:00
|
|
|
|
|
|
|
|
|
if (error != NULL)
|
2012-08-02 21:46:32 +02:00
|
|
|
|
g_task_return_error (task, error);
|
2010-05-24 22:46:24 +02:00
|
|
|
|
else
|
2012-08-02 21:46:32 +02:00
|
|
|
|
g_task_return_pointer (task, value, (GDestroyNotify) g_variant_unref);
|
|
|
|
|
|
2012-02-27 20:53:13 +01:00
|
|
|
|
g_clear_object (&reply);
|
2012-08-02 21:46:32 +02:00
|
|
|
|
g_object_unref (task);
|
2011-07-21 22:03:27 +02:00
|
|
|
|
}
|
2010-05-24 22:46:24 +02:00
|
|
|
|
|
2011-10-17 14:27:03 +02:00
|
|
|
|
/* called in any thread, with the connection's lock not held */
|
2011-07-21 22:03:27 +02:00
|
|
|
|
static void
|
|
|
|
|
g_dbus_connection_call_internal (GDBusConnection *connection,
|
|
|
|
|
const gchar *bus_name,
|
|
|
|
|
const gchar *object_path,
|
|
|
|
|
const gchar *interface_name,
|
|
|
|
|
const gchar *method_name,
|
|
|
|
|
GVariant *parameters,
|
|
|
|
|
const GVariantType *reply_type,
|
|
|
|
|
GDBusCallFlags flags,
|
|
|
|
|
gint timeout_msec,
|
|
|
|
|
GUnixFDList *fd_list,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
GDBusMessage *message;
|
2012-03-16 18:32:38 +01:00
|
|
|
|
guint32 serial;
|
2010-05-24 22:46:24 +02:00
|
|
|
|
|
2011-07-21 22:03:27 +02:00
|
|
|
|
g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
|
|
|
|
|
g_return_if_fail (bus_name == NULL || g_dbus_is_name (bus_name));
|
|
|
|
|
g_return_if_fail (object_path != NULL && g_variant_is_object_path (object_path));
|
|
|
|
|
g_return_if_fail (interface_name != NULL && g_dbus_is_interface_name (interface_name));
|
|
|
|
|
g_return_if_fail (method_name != NULL && g_dbus_is_member_name (method_name));
|
|
|
|
|
g_return_if_fail (timeout_msec >= 0 || timeout_msec == -1);
|
|
|
|
|
g_return_if_fail ((parameters == NULL) || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE));
|
2011-10-19 18:23:30 +02:00
|
|
|
|
g_return_if_fail (check_initialized (connection));
|
2011-07-25 16:31:17 +02:00
|
|
|
|
#ifdef G_OS_UNIX
|
2011-07-21 22:03:27 +02:00
|
|
|
|
g_return_if_fail (fd_list == NULL || G_IS_UNIX_FD_LIST (fd_list));
|
2011-07-25 16:31:17 +02:00
|
|
|
|
#else
|
|
|
|
|
g_return_if_fail (fd_list == NULL);
|
|
|
|
|
#endif
|
2010-05-24 22:46:24 +02:00
|
|
|
|
|
2011-07-21 22:03:27 +02:00
|
|
|
|
message = g_dbus_message_new_method_call (bus_name,
|
|
|
|
|
object_path,
|
|
|
|
|
interface_name,
|
|
|
|
|
method_name);
|
|
|
|
|
add_call_flags (message, flags);
|
|
|
|
|
if (parameters != NULL)
|
|
|
|
|
g_dbus_message_set_body (message, parameters);
|
|
|
|
|
|
|
|
|
|
#ifdef G_OS_UNIX
|
|
|
|
|
if (fd_list != NULL)
|
|
|
|
|
g_dbus_message_set_unix_fd_list (message, fd_list);
|
|
|
|
|
#endif
|
|
|
|
|
|
2012-03-16 18:32:38 +01:00
|
|
|
|
/* If the user has no callback then we can just send the message with
|
|
|
|
|
* the G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED flag set and skip all
|
|
|
|
|
* the logic for processing the reply. If the service sends the reply
|
|
|
|
|
* anyway then it will just be ignored.
|
|
|
|
|
*/
|
|
|
|
|
if (callback != NULL)
|
|
|
|
|
{
|
|
|
|
|
CallState *state;
|
2012-08-02 21:46:32 +02:00
|
|
|
|
GTask *task;
|
2012-03-16 18:32:38 +01:00
|
|
|
|
|
|
|
|
|
state = g_slice_new0 (CallState);
|
|
|
|
|
state->method_name = g_strjoin (".", interface_name, method_name, NULL);
|
|
|
|
|
|
|
|
|
|
if (reply_type == NULL)
|
|
|
|
|
reply_type = G_VARIANT_TYPE_ANY;
|
|
|
|
|
|
|
|
|
|
state->reply_type = g_variant_type_copy (reply_type);
|
|
|
|
|
|
2012-08-02 21:46:32 +02:00
|
|
|
|
task = g_task_new (connection, cancellable, callback, user_data);
|
2016-06-17 01:39:38 +02:00
|
|
|
|
g_task_set_source_tag (task, g_dbus_connection_call_internal);
|
2012-08-02 21:46:32 +02:00
|
|
|
|
g_task_set_task_data (task, state, (GDestroyNotify) call_state_free);
|
|
|
|
|
|
2012-03-16 18:32:38 +01:00
|
|
|
|
g_dbus_connection_send_message_with_reply (connection,
|
|
|
|
|
message,
|
|
|
|
|
G_DBUS_SEND_MESSAGE_FLAGS_NONE,
|
|
|
|
|
timeout_msec,
|
2016-01-28 15:39:18 +01:00
|
|
|
|
&serial,
|
2012-03-16 18:32:38 +01:00
|
|
|
|
cancellable,
|
|
|
|
|
g_dbus_connection_call_done,
|
2012-08-02 21:46:32 +02:00
|
|
|
|
task);
|
2012-03-16 18:32:38 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-01-19 18:30:49 +01:00
|
|
|
|
GDBusMessageFlags msg_flags;
|
2012-03-16 18:32:38 +01:00
|
|
|
|
|
2022-01-19 18:30:49 +01:00
|
|
|
|
msg_flags = g_dbus_message_get_flags (message);
|
|
|
|
|
msg_flags |= G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED;
|
|
|
|
|
g_dbus_message_set_flags (message, msg_flags);
|
2012-03-16 18:32:38 +01:00
|
|
|
|
|
|
|
|
|
g_dbus_connection_send_message (connection,
|
|
|
|
|
message,
|
|
|
|
|
G_DBUS_SEND_MESSAGE_FLAGS_NONE,
|
|
|
|
|
&serial, NULL);
|
|
|
|
|
}
|
2011-07-21 22:03:27 +02:00
|
|
|
|
|
|
|
|
|
if (G_UNLIKELY (_g_dbus_debug_call ()))
|
|
|
|
|
{
|
|
|
|
|
_g_dbus_debug_print_lock ();
|
|
|
|
|
g_print ("========================================================================\n"
|
|
|
|
|
"GDBus-debug:Call:\n"
|
|
|
|
|
" >>>> ASYNC %s.%s()\n"
|
|
|
|
|
" on object %s\n"
|
|
|
|
|
" owned by name %s (serial %d)\n",
|
|
|
|
|
interface_name,
|
|
|
|
|
method_name,
|
|
|
|
|
object_path,
|
|
|
|
|
bus_name != NULL ? bus_name : "(none)",
|
2012-03-16 18:32:38 +01:00
|
|
|
|
serial);
|
2011-07-21 22:03:27 +02:00
|
|
|
|
_g_dbus_debug_print_unlock ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (message != NULL)
|
|
|
|
|
g_object_unref (message);
|
2010-05-24 22:46:24 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-10-17 14:27:03 +02:00
|
|
|
|
/* called in any thread, with the connection's lock not held */
|
2011-07-21 22:03:27 +02:00
|
|
|
|
static GVariant *
|
|
|
|
|
g_dbus_connection_call_finish_internal (GDBusConnection *connection,
|
|
|
|
|
GUnixFDList **out_fd_list,
|
|
|
|
|
GAsyncResult *res,
|
|
|
|
|
GError **error)
|
|
|
|
|
{
|
2012-08-02 21:46:32 +02:00
|
|
|
|
GTask *task;
|
2011-07-21 22:03:27 +02:00
|
|
|
|
CallState *state;
|
2012-08-02 21:46:32 +02:00
|
|
|
|
GVariant *ret;
|
2011-07-21 22:03:27 +02:00
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
|
2012-08-02 21:46:32 +02:00
|
|
|
|
g_return_val_if_fail (g_task_is_valid (res, connection), NULL);
|
2011-07-21 22:03:27 +02:00
|
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
|
|
|
|
|
|
2012-08-02 21:46:32 +02:00
|
|
|
|
task = G_TASK (res);
|
|
|
|
|
state = g_task_get_task_data (task);
|
2011-07-21 22:03:27 +02:00
|
|
|
|
|
2012-08-02 21:46:32 +02:00
|
|
|
|
ret = g_task_propagate_pointer (task, error);
|
|
|
|
|
if (!ret)
|
2011-07-21 22:03:27 +02:00
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
if (out_fd_list != NULL)
|
|
|
|
|
*out_fd_list = state->fd_list != NULL ? g_object_ref (state->fd_list) : NULL;
|
2012-08-02 21:46:32 +02:00
|
|
|
|
return ret;
|
2011-07-21 22:03:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-10-17 14:27:03 +02:00
|
|
|
|
/* called in any user thread, with the connection's lock not held */
|
2011-07-21 22:03:27 +02:00
|
|
|
|
static GVariant *
|
|
|
|
|
g_dbus_connection_call_sync_internal (GDBusConnection *connection,
|
|
|
|
|
const gchar *bus_name,
|
|
|
|
|
const gchar *object_path,
|
|
|
|
|
const gchar *interface_name,
|
|
|
|
|
const gchar *method_name,
|
|
|
|
|
GVariant *parameters,
|
|
|
|
|
const GVariantType *reply_type,
|
|
|
|
|
GDBusCallFlags flags,
|
|
|
|
|
gint timeout_msec,
|
|
|
|
|
GUnixFDList *fd_list,
|
|
|
|
|
GUnixFDList **out_fd_list,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GError **error)
|
|
|
|
|
{
|
|
|
|
|
GDBusMessage *message;
|
|
|
|
|
GDBusMessage *reply;
|
|
|
|
|
GVariant *result;
|
|
|
|
|
GError *local_error;
|
2011-10-20 14:12:26 +02:00
|
|
|
|
GDBusSendMessageFlags send_flags;
|
2011-07-21 22:03:27 +02:00
|
|
|
|
|
|
|
|
|
message = NULL;
|
|
|
|
|
reply = NULL;
|
|
|
|
|
result = NULL;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
|
|
|
|
|
g_return_val_if_fail (bus_name == NULL || g_dbus_is_name (bus_name), NULL);
|
|
|
|
|
g_return_val_if_fail (object_path != NULL && g_variant_is_object_path (object_path), NULL);
|
|
|
|
|
g_return_val_if_fail (interface_name != NULL && g_dbus_is_interface_name (interface_name), NULL);
|
|
|
|
|
g_return_val_if_fail (method_name != NULL && g_dbus_is_member_name (method_name), NULL);
|
|
|
|
|
g_return_val_if_fail (timeout_msec >= 0 || timeout_msec == -1, NULL);
|
|
|
|
|
g_return_val_if_fail ((parameters == NULL) || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE), NULL);
|
2011-07-25 16:31:17 +02:00
|
|
|
|
#ifdef G_OS_UNIX
|
2011-07-21 22:03:27 +02:00
|
|
|
|
g_return_val_if_fail (fd_list == NULL || G_IS_UNIX_FD_LIST (fd_list), NULL);
|
2011-07-25 16:31:17 +02:00
|
|
|
|
#else
|
|
|
|
|
g_return_val_if_fail (fd_list == NULL, NULL);
|
|
|
|
|
#endif
|
2011-07-21 22:03:27 +02:00
|
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
|
|
|
|
|
|
2011-10-19 18:23:30 +02:00
|
|
|
|
if (!(flags & CALL_FLAGS_INITIALIZING))
|
|
|
|
|
g_return_val_if_fail (check_initialized (connection), FALSE);
|
|
|
|
|
|
2011-07-21 22:03:27 +02:00
|
|
|
|
if (reply_type == NULL)
|
|
|
|
|
reply_type = G_VARIANT_TYPE_ANY;
|
|
|
|
|
|
|
|
|
|
message = g_dbus_message_new_method_call (bus_name,
|
|
|
|
|
object_path,
|
|
|
|
|
interface_name,
|
|
|
|
|
method_name);
|
|
|
|
|
add_call_flags (message, flags);
|
|
|
|
|
if (parameters != NULL)
|
|
|
|
|
g_dbus_message_set_body (message, parameters);
|
|
|
|
|
|
|
|
|
|
#ifdef G_OS_UNIX
|
|
|
|
|
if (fd_list != NULL)
|
|
|
|
|
g_dbus_message_set_unix_fd_list (message, fd_list);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
if (G_UNLIKELY (_g_dbus_debug_call ()))
|
|
|
|
|
{
|
|
|
|
|
_g_dbus_debug_print_lock ();
|
|
|
|
|
g_print ("========================================================================\n"
|
|
|
|
|
"GDBus-debug:Call:\n"
|
|
|
|
|
" >>>> SYNC %s.%s()\n"
|
|
|
|
|
" on object %s\n"
|
|
|
|
|
" owned by name %s\n",
|
|
|
|
|
interface_name,
|
|
|
|
|
method_name,
|
|
|
|
|
object_path,
|
|
|
|
|
bus_name != NULL ? bus_name : "(none)");
|
|
|
|
|
_g_dbus_debug_print_unlock ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
local_error = NULL;
|
2011-10-20 14:12:26 +02:00
|
|
|
|
|
|
|
|
|
send_flags = G_DBUS_SEND_MESSAGE_FLAGS_NONE;
|
|
|
|
|
|
|
|
|
|
/* translate from one flavour of flags to another... */
|
|
|
|
|
if (flags & CALL_FLAGS_INITIALIZING)
|
|
|
|
|
send_flags |= SEND_MESSAGE_FLAGS_INITIALIZING;
|
|
|
|
|
|
2011-07-21 22:03:27 +02:00
|
|
|
|
reply = g_dbus_connection_send_message_with_reply_sync (connection,
|
|
|
|
|
message,
|
2011-10-20 14:12:26 +02:00
|
|
|
|
send_flags,
|
2011-07-21 22:03:27 +02:00
|
|
|
|
timeout_msec,
|
2020-11-11 19:24:28 +01:00
|
|
|
|
NULL, /* guint32 *out_serial */
|
2011-07-21 22:03:27 +02:00
|
|
|
|
cancellable,
|
|
|
|
|
&local_error);
|
|
|
|
|
|
|
|
|
|
if (G_UNLIKELY (_g_dbus_debug_call ()))
|
|
|
|
|
{
|
|
|
|
|
_g_dbus_debug_print_lock ();
|
|
|
|
|
g_print ("========================================================================\n"
|
|
|
|
|
"GDBus-debug:Call:\n"
|
|
|
|
|
" <<<< SYNC COMPLETE %s.%s()\n"
|
|
|
|
|
" ",
|
|
|
|
|
interface_name,
|
|
|
|
|
method_name);
|
|
|
|
|
if (reply != NULL)
|
|
|
|
|
{
|
|
|
|
|
g_print ("SUCCESS\n");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
g_print ("FAILED: %s\n",
|
|
|
|
|
local_error->message);
|
|
|
|
|
}
|
|
|
|
|
_g_dbus_debug_print_unlock ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (reply == NULL)
|
|
|
|
|
{
|
|
|
|
|
if (error != NULL)
|
|
|
|
|
*error = local_error;
|
|
|
|
|
else
|
|
|
|
|
g_error_free (local_error);
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result = decode_method_reply (reply, method_name, reply_type, out_fd_list, error);
|
|
|
|
|
|
|
|
|
|
out:
|
|
|
|
|
if (message != NULL)
|
|
|
|
|
g_object_unref (message);
|
|
|
|
|
if (reply != NULL)
|
|
|
|
|
g_object_unref (reply);
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
/**
|
2010-05-10 17:47:08 +02:00
|
|
|
|
* g_dbus_connection_call:
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @connection: a #GDBusConnection
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @bus_name: (nullable): a unique or well-known bus name or %NULL if
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @connection is not a message bus connection
|
|
|
|
|
* @object_path: path of remote object
|
|
|
|
|
* @interface_name: D-Bus interface to invoke method on
|
|
|
|
|
* @method_name: the name of the method to invoke
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @parameters: (nullable): a #GVariant tuple with parameters for the method
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* or %NULL if not passing parameters
|
2018-01-09 16:17:02 +01:00
|
|
|
|
* @reply_type: (nullable): the expected type of the reply (which will be a
|
|
|
|
|
* tuple), or %NULL
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @flags: flags from the #GDBusCallFlags enumeration
|
|
|
|
|
* @timeout_msec: the timeout in milliseconds, -1 to use the default
|
|
|
|
|
* timeout or %G_MAXINT for no timeout
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @cancellable: (nullable): a #GCancellable or %NULL
|
|
|
|
|
* @callback: (nullable): a #GAsyncReadyCallback to call when the request
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* is satisfied or %NULL if you don't care about the result of the
|
|
|
|
|
* method invocation
|
|
|
|
|
* @user_data: the data to pass to @callback
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
|
|
|
|
* Asynchronously invokes the @method_name method on the
|
|
|
|
|
* @interface_name D-Bus interface on the remote object at
|
|
|
|
|
* @object_path owned by @bus_name.
|
|
|
|
|
*
|
|
|
|
|
* 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 @parameters contains a value
|
|
|
|
|
* not compatible with the D-Bus protocol, the operation fails with
|
|
|
|
|
* %G_IO_ERROR_INVALID_ARGUMENT.
|
|
|
|
|
*
|
2010-05-24 22:46:24 +02:00
|
|
|
|
* If @reply_type is non-%NULL then the reply will be checked for having this type and an
|
|
|
|
|
* error will be raised if it does not match. Said another way, if you give a @reply_type
|
2018-01-09 16:17:02 +01:00
|
|
|
|
* then any non-%NULL return value will be of this type. Unless it’s
|
|
|
|
|
* %G_VARIANT_TYPE_UNIT, the @reply_type will be a tuple containing one or more
|
|
|
|
|
* values.
|
2010-05-24 22:46:24 +02:00
|
|
|
|
*
|
2010-05-12 05:18:48 +02:00
|
|
|
|
* If the @parameters #GVariant is floating, it is consumed. This allows
|
|
|
|
|
* convenient 'inline' use of g_variant_new(), e.g.:
|
2014-02-01 21:11:49 +01:00
|
|
|
|
* |[<!-- language="C" -->
|
2010-05-12 05:18:48 +02:00
|
|
|
|
* g_dbus_connection_call (connection,
|
|
|
|
|
* "org.freedesktop.StringThings",
|
|
|
|
|
* "/org/freedesktop/StringThings",
|
|
|
|
|
* "org.freedesktop.StringThings",
|
|
|
|
|
* "TwoStrings",
|
|
|
|
|
* g_variant_new ("(ss)",
|
|
|
|
|
* "Thing One",
|
|
|
|
|
* "Thing Two"),
|
2010-05-24 22:46:24 +02:00
|
|
|
|
* NULL,
|
2010-05-12 05:18:48 +02:00
|
|
|
|
* G_DBUS_CALL_FLAGS_NONE,
|
|
|
|
|
* -1,
|
|
|
|
|
* NULL,
|
|
|
|
|
* (GAsyncReadyCallback) two_strings_done,
|
|
|
|
|
* NULL);
|
|
|
|
|
* ]|
|
|
|
|
|
*
|
2014-02-08 18:26:56 +01:00
|
|
|
|
* 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]
|
2010-05-06 20:13:59 +02:00
|
|
|
|
* of the thread you are calling this method from. You can then call
|
2010-05-10 17:47:08 +02:00
|
|
|
|
* g_dbus_connection_call_finish() to get the result of the operation.
|
|
|
|
|
* See g_dbus_connection_call_sync() for the synchronous version of this
|
2010-05-06 20:13:59 +02:00
|
|
|
|
* function.
|
2010-05-06 22:02:08 +02:00
|
|
|
|
*
|
2012-03-16 18:32:38 +01:00
|
|
|
|
* If @callback is %NULL then the D-Bus method call message will be sent with
|
|
|
|
|
* the %G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED flag set.
|
|
|
|
|
*
|
2010-05-06 22:02:08 +02:00
|
|
|
|
* Since: 2.26
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*/
|
|
|
|
|
void
|
2014-02-01 05:42:40 +01:00
|
|
|
|
g_dbus_connection_call (GDBusConnection *connection,
|
|
|
|
|
const gchar *bus_name,
|
|
|
|
|
const gchar *object_path,
|
|
|
|
|
const gchar *interface_name,
|
|
|
|
|
const gchar *method_name,
|
|
|
|
|
GVariant *parameters,
|
|
|
|
|
const GVariantType *reply_type,
|
|
|
|
|
GDBusCallFlags flags,
|
|
|
|
|
gint timeout_msec,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
|
gpointer user_data)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
2011-08-17 10:01:14 +02:00
|
|
|
|
g_dbus_connection_call_internal (connection, bus_name, object_path, interface_name, method_name, parameters, reply_type, flags, timeout_msec, NULL, cancellable, callback, user_data);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2010-05-10 17:47:08 +02:00
|
|
|
|
* g_dbus_connection_call_finish:
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @connection: a #GDBusConnection
|
|
|
|
|
* @res: a #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_connection_call()
|
|
|
|
|
* @error: return location for error or %NULL
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
2010-05-10 17:47:08 +02:00
|
|
|
|
* Finishes an operation started with g_dbus_connection_call().
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
2020-12-15 10:13:22 +01:00
|
|
|
|
* Returns: (transfer full): %NULL if @error is set. Otherwise a non-floating
|
|
|
|
|
* #GVariant tuple with return values. Free with g_variant_unref().
|
2010-05-06 22:02:08 +02:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.26
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*/
|
|
|
|
|
GVariant *
|
2010-05-10 17:47:08 +02:00
|
|
|
|
g_dbus_connection_call_finish (GDBusConnection *connection,
|
|
|
|
|
GAsyncResult *res,
|
|
|
|
|
GError **error)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
2011-07-21 22:03:27 +02:00
|
|
|
|
return g_dbus_connection_call_finish_internal (connection, NULL, res, error);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2010-05-10 17:47:08 +02:00
|
|
|
|
* g_dbus_connection_call_sync:
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @connection: a #GDBusConnection
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @bus_name: (nullable): a unique or well-known bus name or %NULL if
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @connection is not a message bus connection
|
|
|
|
|
* @object_path: path of remote object
|
|
|
|
|
* @interface_name: D-Bus interface to invoke method on
|
|
|
|
|
* @method_name: the name of the method to invoke
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @parameters: (nullable): a #GVariant tuple with parameters for the method
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* or %NULL if not passing parameters
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @reply_type: (nullable): the expected type of the reply, or %NULL
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @flags: flags from the #GDBusCallFlags enumeration
|
|
|
|
|
* @timeout_msec: the timeout in milliseconds, -1 to use the default
|
|
|
|
|
* timeout or %G_MAXINT for no timeout
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @cancellable: (nullable): a #GCancellable or %NULL
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @error: return location for error or %NULL
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
|
|
|
|
* Synchronously invokes the @method_name method on the
|
|
|
|
|
* @interface_name D-Bus interface on the remote object at
|
|
|
|
|
* @object_path owned by @bus_name.
|
|
|
|
|
*
|
|
|
|
|
* 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 @parameters
|
|
|
|
|
* contains a value not compatible with the D-Bus protocol, the operation
|
|
|
|
|
* fails with %G_IO_ERROR_INVALID_ARGUMENT.
|
2017-11-10 22:18:20 +01:00
|
|
|
|
*
|
2010-05-24 22:46:24 +02:00
|
|
|
|
* If @reply_type is non-%NULL then the reply will be checked for having
|
|
|
|
|
* this type and an error will be raised if it does not match. Said
|
|
|
|
|
* another way, if you give a @reply_type then any non-%NULL return
|
|
|
|
|
* value will be of this type.
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
2010-05-12 05:18:48 +02:00
|
|
|
|
* If the @parameters #GVariant is floating, it is consumed.
|
|
|
|
|
* This allows convenient 'inline' use of g_variant_new(), e.g.:
|
2014-02-01 21:11:49 +01:00
|
|
|
|
* |[<!-- language="C" -->
|
2010-05-12 05:18:48 +02:00
|
|
|
|
* g_dbus_connection_call_sync (connection,
|
|
|
|
|
* "org.freedesktop.StringThings",
|
|
|
|
|
* "/org/freedesktop/StringThings",
|
|
|
|
|
* "org.freedesktop.StringThings",
|
|
|
|
|
* "TwoStrings",
|
|
|
|
|
* g_variant_new ("(ss)",
|
|
|
|
|
* "Thing One",
|
|
|
|
|
* "Thing Two"),
|
2010-05-24 22:46:24 +02:00
|
|
|
|
* NULL,
|
2010-05-12 05:18:48 +02:00
|
|
|
|
* G_DBUS_CALL_FLAGS_NONE,
|
|
|
|
|
* -1,
|
|
|
|
|
* NULL,
|
2014-02-01 17:57:13 +01:00
|
|
|
|
* &error);
|
2010-05-12 05:18:48 +02:00
|
|
|
|
* ]|
|
|
|
|
|
*
|
2010-05-06 20:13:59 +02:00
|
|
|
|
* The calling thread is blocked until a reply is received. See
|
2010-05-10 17:47:08 +02:00
|
|
|
|
* g_dbus_connection_call() for the asynchronous version of
|
2010-05-06 20:13:59 +02:00
|
|
|
|
* this method.
|
|
|
|
|
*
|
2020-12-15 10:13:22 +01:00
|
|
|
|
* Returns: (transfer full): %NULL if @error is set. Otherwise a non-floating
|
|
|
|
|
* #GVariant tuple with return values. Free with g_variant_unref().
|
2010-05-06 22:02:08 +02:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.26
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*/
|
|
|
|
|
GVariant *
|
2014-02-01 05:42:40 +01:00
|
|
|
|
g_dbus_connection_call_sync (GDBusConnection *connection,
|
|
|
|
|
const gchar *bus_name,
|
|
|
|
|
const gchar *object_path,
|
|
|
|
|
const gchar *interface_name,
|
|
|
|
|
const gchar *method_name,
|
|
|
|
|
GVariant *parameters,
|
|
|
|
|
const GVariantType *reply_type,
|
|
|
|
|
GDBusCallFlags flags,
|
|
|
|
|
gint timeout_msec,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GError **error)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
2011-07-21 22:03:27 +02:00
|
|
|
|
return g_dbus_connection_call_sync_internal (connection, bus_name, object_path, interface_name, method_name, parameters, reply_type, flags, timeout_msec, NULL, NULL, cancellable, error);
|
|
|
|
|
}
|
2010-07-01 20:37:16 +02:00
|
|
|
|
|
2011-07-21 22:03:27 +02:00
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2011-07-21 22:03:27 +02:00
|
|
|
|
#ifdef G_OS_UNIX
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2011-07-21 22:03:27 +02:00
|
|
|
|
/**
|
|
|
|
|
* g_dbus_connection_call_with_unix_fd_list:
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @connection: a #GDBusConnection
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @bus_name: (nullable): a unique or well-known bus name or %NULL if
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @connection is not a message bus connection
|
|
|
|
|
* @object_path: path of remote object
|
|
|
|
|
* @interface_name: D-Bus interface to invoke method on
|
|
|
|
|
* @method_name: the name of the method to invoke
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @parameters: (nullable): a #GVariant tuple with parameters for the method
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* or %NULL if not passing parameters
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @reply_type: (nullable): the expected type of the reply, or %NULL
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @flags: flags from the #GDBusCallFlags enumeration
|
|
|
|
|
* @timeout_msec: the timeout in milliseconds, -1 to use the default
|
|
|
|
|
* timeout or %G_MAXINT for no timeout
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @fd_list: (nullable): a #GUnixFDList or %NULL
|
|
|
|
|
* @cancellable: (nullable): a #GCancellable or %NULL
|
|
|
|
|
* @callback: (nullable): a #GAsyncReadyCallback to call when the request is
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* satisfied or %NULL if you don't * care about the result of the
|
|
|
|
|
* method invocation
|
2011-07-21 22:03:27 +02:00
|
|
|
|
* @user_data: The data to pass to @callback.
|
|
|
|
|
*
|
|
|
|
|
* Like g_dbus_connection_call() but also takes a #GUnixFDList object.
|
|
|
|
|
*
|
2020-10-28 12:42:13 +01:00
|
|
|
|
* The file descriptors normally correspond to %G_VARIANT_TYPE_HANDLE
|
|
|
|
|
* values in the body of the message. For example, if a message contains
|
|
|
|
|
* two file descriptors, @fd_list would have length 2, and
|
|
|
|
|
* `g_variant_new_handle (0)` and `g_variant_new_handle (1)` would appear
|
|
|
|
|
* somewhere in the body of the message (not necessarily in that order!)
|
|
|
|
|
* to represent the file descriptors at indexes 0 and 1 respectively.
|
|
|
|
|
*
|
|
|
|
|
* When designing D-Bus APIs that are intended to be interoperable,
|
|
|
|
|
* please note that non-GDBus implementations of D-Bus can usually only
|
|
|
|
|
* access file descriptors if they are referenced in this way by a
|
|
|
|
|
* value of type %G_VARIANT_TYPE_HANDLE in the body of the message.
|
|
|
|
|
*
|
2011-07-21 22:03:27 +02:00
|
|
|
|
* This method is only available on UNIX.
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.30
|
|
|
|
|
*/
|
|
|
|
|
void
|
2014-02-01 05:42:40 +01:00
|
|
|
|
g_dbus_connection_call_with_unix_fd_list (GDBusConnection *connection,
|
|
|
|
|
const gchar *bus_name,
|
|
|
|
|
const gchar *object_path,
|
|
|
|
|
const gchar *interface_name,
|
|
|
|
|
const gchar *method_name,
|
|
|
|
|
GVariant *parameters,
|
|
|
|
|
const GVariantType *reply_type,
|
|
|
|
|
GDBusCallFlags flags,
|
|
|
|
|
gint timeout_msec,
|
|
|
|
|
GUnixFDList *fd_list,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
|
gpointer user_data)
|
2011-07-21 22:03:27 +02:00
|
|
|
|
{
|
2011-08-17 10:01:14 +02:00
|
|
|
|
g_dbus_connection_call_internal (connection, bus_name, object_path, interface_name, method_name, parameters, reply_type, flags, timeout_msec, fd_list, cancellable, callback, user_data);
|
2011-07-21 22:03:27 +02:00
|
|
|
|
}
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2011-07-21 22:03:27 +02:00
|
|
|
|
/**
|
|
|
|
|
* g_dbus_connection_call_with_unix_fd_list_finish:
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @connection: a #GDBusConnection
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @out_fd_list: (out) (optional): return location for a #GUnixFDList or %NULL
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @res: a #GAsyncResult obtained from the #GAsyncReadyCallback passed to
|
|
|
|
|
* g_dbus_connection_call_with_unix_fd_list()
|
|
|
|
|
* @error: return location for error or %NULL
|
2011-07-21 22:03:27 +02:00
|
|
|
|
*
|
|
|
|
|
* Finishes an operation started with g_dbus_connection_call_with_unix_fd_list().
|
|
|
|
|
*
|
2020-10-28 12:42:13 +01:00
|
|
|
|
* The file descriptors normally correspond to %G_VARIANT_TYPE_HANDLE
|
|
|
|
|
* values in the body of the message. For example,
|
|
|
|
|
* if g_variant_get_handle() returns 5, that is intended to be a reference
|
|
|
|
|
* to the file descriptor that can be accessed by
|
|
|
|
|
* `g_unix_fd_list_get (*out_fd_list, 5, ...)`.
|
|
|
|
|
*
|
|
|
|
|
* When designing D-Bus APIs that are intended to be interoperable,
|
|
|
|
|
* please note that non-GDBus implementations of D-Bus can usually only
|
|
|
|
|
* access file descriptors if they are referenced in this way by a
|
|
|
|
|
* value of type %G_VARIANT_TYPE_HANDLE in the body of the message.
|
|
|
|
|
*
|
2020-12-15 10:13:22 +01:00
|
|
|
|
* Returns: (transfer full): %NULL if @error is set. Otherwise a non-floating
|
|
|
|
|
* #GVariant tuple with return values. Free with g_variant_unref().
|
2011-07-21 22:03:27 +02:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.30
|
|
|
|
|
*/
|
|
|
|
|
GVariant *
|
|
|
|
|
g_dbus_connection_call_with_unix_fd_list_finish (GDBusConnection *connection,
|
|
|
|
|
GUnixFDList **out_fd_list,
|
|
|
|
|
GAsyncResult *res,
|
|
|
|
|
GError **error)
|
|
|
|
|
{
|
|
|
|
|
return g_dbus_connection_call_finish_internal (connection, out_fd_list, res, error);
|
|
|
|
|
}
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2011-07-21 22:03:27 +02:00
|
|
|
|
/**
|
|
|
|
|
* g_dbus_connection_call_with_unix_fd_list_sync:
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @connection: a #GDBusConnection
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @bus_name: (nullable): a unique or well-known bus name or %NULL
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* if @connection is not a message bus connection
|
|
|
|
|
* @object_path: path of remote object
|
|
|
|
|
* @interface_name: D-Bus interface to invoke method on
|
|
|
|
|
* @method_name: the name of the method to invoke
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @parameters: (nullable): a #GVariant tuple with parameters for
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* the method or %NULL if not passing parameters
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @reply_type: (nullable): the expected type of the reply, or %NULL
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @flags: flags from the #GDBusCallFlags enumeration
|
|
|
|
|
* @timeout_msec: the timeout in milliseconds, -1 to use the default
|
|
|
|
|
* timeout or %G_MAXINT for no timeout
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @fd_list: (nullable): a #GUnixFDList or %NULL
|
|
|
|
|
* @out_fd_list: (out) (optional): return location for a #GUnixFDList or %NULL
|
|
|
|
|
* @cancellable: (nullable): a #GCancellable or %NULL
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @error: return location for error or %NULL
|
2011-07-21 22:03:27 +02:00
|
|
|
|
*
|
|
|
|
|
* Like g_dbus_connection_call_sync() but also takes and returns #GUnixFDList objects.
|
2020-10-28 12:42:13 +01:00
|
|
|
|
* See g_dbus_connection_call_with_unix_fd_list() and
|
|
|
|
|
* g_dbus_connection_call_with_unix_fd_list_finish() for more details.
|
2011-07-21 22:03:27 +02:00
|
|
|
|
*
|
|
|
|
|
* This method is only available on UNIX.
|
|
|
|
|
*
|
2020-12-15 10:13:22 +01:00
|
|
|
|
* Returns: (transfer full): %NULL if @error is set. Otherwise a non-floating
|
|
|
|
|
* #GVariant tuple with return values. Free with g_variant_unref().
|
2011-07-21 22:03:27 +02:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.30
|
|
|
|
|
*/
|
|
|
|
|
GVariant *
|
2014-02-01 05:42:40 +01:00
|
|
|
|
g_dbus_connection_call_with_unix_fd_list_sync (GDBusConnection *connection,
|
|
|
|
|
const gchar *bus_name,
|
|
|
|
|
const gchar *object_path,
|
|
|
|
|
const gchar *interface_name,
|
|
|
|
|
const gchar *method_name,
|
|
|
|
|
GVariant *parameters,
|
|
|
|
|
const GVariantType *reply_type,
|
|
|
|
|
GDBusCallFlags flags,
|
|
|
|
|
gint timeout_msec,
|
|
|
|
|
GUnixFDList *fd_list,
|
|
|
|
|
GUnixFDList **out_fd_list,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GError **error)
|
2011-07-21 22:03:27 +02:00
|
|
|
|
{
|
|
|
|
|
return g_dbus_connection_call_sync_internal (connection, bus_name, object_path, interface_name, method_name, parameters, reply_type, flags, timeout_msec, fd_list, out_fd_list, cancellable, error);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-07-21 22:03:27 +02:00
|
|
|
|
#endif /* G_OS_UNIX */
|
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
2011-10-17 14:27:03 +02:00
|
|
|
|
/* called without lock held in the thread where the caller registered
|
|
|
|
|
* the subtree
|
|
|
|
|
*/
|
2010-05-06 20:13:59 +02:00
|
|
|
|
static gboolean
|
2010-05-10 04:13:18 +02:00
|
|
|
|
handle_subtree_introspect (GDBusConnection *connection,
|
|
|
|
|
ExportedSubtree *es,
|
|
|
|
|
GDBusMessage *message)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
|
|
|
|
GString *s;
|
|
|
|
|
gboolean handled;
|
|
|
|
|
GDBusMessage *reply;
|
|
|
|
|
gchar **children;
|
|
|
|
|
gboolean is_root;
|
|
|
|
|
const gchar *sender;
|
|
|
|
|
const gchar *requested_object_path;
|
|
|
|
|
const gchar *requested_node;
|
2010-07-15 22:26:42 +02:00
|
|
|
|
GDBusInterfaceInfo **interfaces;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
guint n;
|
|
|
|
|
gchar **subnode_paths;
|
2011-02-16 01:50:55 +01:00
|
|
|
|
gboolean has_properties_interface;
|
|
|
|
|
gboolean has_introspectable_interface;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
handled = FALSE;
|
|
|
|
|
|
|
|
|
|
requested_object_path = g_dbus_message_get_path (message);
|
|
|
|
|
sender = g_dbus_message_get_sender (message);
|
|
|
|
|
is_root = (g_strcmp0 (requested_object_path, es->object_path) == 0);
|
|
|
|
|
|
|
|
|
|
s = g_string_new (NULL);
|
|
|
|
|
introspect_append_header (s);
|
|
|
|
|
|
|
|
|
|
/* Strictly we don't need the children in dynamic mode, but we avoid the
|
|
|
|
|
* conditionals to preserve code clarity
|
|
|
|
|
*/
|
|
|
|
|
children = es->vtable->enumerate (es->connection,
|
|
|
|
|
sender,
|
|
|
|
|
es->object_path,
|
|
|
|
|
es->user_data);
|
|
|
|
|
|
|
|
|
|
if (!is_root)
|
|
|
|
|
{
|
|
|
|
|
requested_node = strrchr (requested_object_path, '/') + 1;
|
|
|
|
|
|
|
|
|
|
/* Assert existence of object if we are not dynamic */
|
|
|
|
|
if (!(es->flags & G_DBUS_SUBTREE_FLAGS_DISPATCH_TO_UNENUMERATED_NODES) &&
|
2022-03-17 17:24:08 +01:00
|
|
|
|
!g_strv_contains ((const gchar * const *) children, requested_node))
|
2010-05-06 20:13:59 +02:00
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2010-07-15 22:53:43 +02:00
|
|
|
|
requested_node = NULL;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interfaces = es->vtable->introspect (es->connection,
|
|
|
|
|
sender,
|
|
|
|
|
es->object_path,
|
|
|
|
|
requested_node,
|
|
|
|
|
es->user_data);
|
|
|
|
|
if (interfaces != NULL)
|
|
|
|
|
{
|
2011-02-16 01:50:55 +01:00
|
|
|
|
has_properties_interface = FALSE;
|
|
|
|
|
has_introspectable_interface = FALSE;
|
|
|
|
|
|
|
|
|
|
for (n = 0; interfaces[n] != NULL; n++)
|
|
|
|
|
{
|
2024-05-13 14:00:34 +02:00
|
|
|
|
if (strcmp (interfaces[n]->name, DBUS_INTERFACE_PROPERTIES) == 0)
|
2011-02-16 01:50:55 +01:00
|
|
|
|
has_properties_interface = TRUE;
|
2024-05-13 14:00:34 +02:00
|
|
|
|
else if (strcmp (interfaces[n]->name, DBUS_INTERFACE_INTROSPECTABLE) == 0)
|
2011-02-16 01:50:55 +01:00
|
|
|
|
has_introspectable_interface = TRUE;
|
|
|
|
|
}
|
|
|
|
|
if (!has_properties_interface)
|
|
|
|
|
g_string_append (s, introspect_properties_interface);
|
|
|
|
|
if (!has_introspectable_interface)
|
|
|
|
|
g_string_append (s, introspect_introspectable_interface);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2010-07-15 22:26:42 +02:00
|
|
|
|
for (n = 0; interfaces[n] != NULL; n++)
|
|
|
|
|
{
|
|
|
|
|
g_dbus_interface_info_generate_xml (interfaces[n], 2, s);
|
|
|
|
|
g_dbus_interface_info_unref (interfaces[n]);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
2010-07-15 22:26:42 +02:00
|
|
|
|
g_free (interfaces);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* then include <node> entries from the Subtree for the root */
|
|
|
|
|
if (is_root)
|
|
|
|
|
{
|
|
|
|
|
for (n = 0; children != NULL && children[n] != NULL; n++)
|
2010-05-10 04:13:18 +02:00
|
|
|
|
g_string_append_printf (s, " <node name=\"%s\"/>\n", children[n]);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* finally include nodes registered below us */
|
|
|
|
|
subnode_paths = g_dbus_connection_list_registered (es->connection, requested_object_path);
|
|
|
|
|
for (n = 0; subnode_paths != NULL && subnode_paths[n] != NULL; n++)
|
2010-05-10 04:13:18 +02:00
|
|
|
|
g_string_append_printf (s, " <node name=\"%s\"/>\n", subnode_paths[n]);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_strfreev (subnode_paths);
|
|
|
|
|
|
|
|
|
|
g_string_append (s, "</node>\n");
|
|
|
|
|
|
|
|
|
|
reply = g_dbus_message_new_method_reply (message);
|
|
|
|
|
g_dbus_message_set_body (reply, g_variant_new ("(s)", s->str));
|
2010-07-19 22:07:57 +02:00
|
|
|
|
g_dbus_connection_send_message (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_object_unref (reply);
|
|
|
|
|
|
|
|
|
|
handled = TRUE;
|
|
|
|
|
|
|
|
|
|
out:
|
|
|
|
|
g_string_free (s, TRUE);
|
|
|
|
|
g_strfreev (children);
|
|
|
|
|
return handled;
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-17 14:27:03 +02:00
|
|
|
|
/* called without lock held in the thread where the caller registered
|
|
|
|
|
* the subtree
|
|
|
|
|
*/
|
2010-05-06 20:13:59 +02:00
|
|
|
|
static gboolean
|
2010-05-10 04:13:18 +02:00
|
|
|
|
handle_subtree_method_invocation (GDBusConnection *connection,
|
|
|
|
|
ExportedSubtree *es,
|
|
|
|
|
GDBusMessage *message)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
2010-06-22 11:13:21 +02:00
|
|
|
|
gboolean handled;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
const gchar *sender;
|
|
|
|
|
const gchar *interface_name;
|
|
|
|
|
const gchar *member;
|
|
|
|
|
const gchar *signature;
|
|
|
|
|
const gchar *requested_object_path;
|
|
|
|
|
const gchar *requested_node;
|
|
|
|
|
gboolean is_root;
|
2010-07-19 21:45:27 +02:00
|
|
|
|
GDBusInterfaceInfo *interface_info;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
const GDBusInterfaceVTable *interface_vtable;
|
|
|
|
|
gpointer interface_user_data;
|
|
|
|
|
guint n;
|
2010-07-15 22:26:42 +02:00
|
|
|
|
GDBusInterfaceInfo **interfaces;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
gboolean is_property_get;
|
|
|
|
|
gboolean is_property_set;
|
|
|
|
|
gboolean is_property_get_all;
|
|
|
|
|
|
|
|
|
|
handled = FALSE;
|
|
|
|
|
interfaces = NULL;
|
|
|
|
|
|
|
|
|
|
requested_object_path = g_dbus_message_get_path (message);
|
|
|
|
|
sender = g_dbus_message_get_sender (message);
|
|
|
|
|
interface_name = g_dbus_message_get_interface (message);
|
|
|
|
|
member = g_dbus_message_get_member (message);
|
|
|
|
|
signature = g_dbus_message_get_signature (message);
|
|
|
|
|
is_root = (g_strcmp0 (requested_object_path, es->object_path) == 0);
|
|
|
|
|
|
|
|
|
|
is_property_get = FALSE;
|
|
|
|
|
is_property_set = FALSE;
|
|
|
|
|
is_property_get_all = FALSE;
|
2024-05-13 14:00:34 +02:00
|
|
|
|
if (g_strcmp0 (interface_name, DBUS_INTERFACE_PROPERTIES) == 0)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
|
|
|
|
if (g_strcmp0 (member, "Get") == 0 && g_strcmp0 (signature, "ss") == 0)
|
2010-05-10 04:13:18 +02:00
|
|
|
|
is_property_get = TRUE;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
else if (g_strcmp0 (member, "Set") == 0 && g_strcmp0 (signature, "ssv") == 0)
|
2010-05-10 04:13:18 +02:00
|
|
|
|
is_property_set = TRUE;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
else if (g_strcmp0 (member, "GetAll") == 0 && g_strcmp0 (signature, "s") == 0)
|
2010-05-10 04:13:18 +02:00
|
|
|
|
is_property_get_all = TRUE;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!is_root)
|
|
|
|
|
{
|
|
|
|
|
requested_node = strrchr (requested_object_path, '/') + 1;
|
|
|
|
|
|
2010-07-15 22:26:42 +02:00
|
|
|
|
if (~es->flags & G_DBUS_SUBTREE_FLAGS_DISPATCH_TO_UNENUMERATED_NODES)
|
|
|
|
|
{
|
|
|
|
|
/* We don't want to dispatch to unenumerated
|
|
|
|
|
* nodes, so ensure that the child exists.
|
|
|
|
|
*/
|
|
|
|
|
gchar **children;
|
|
|
|
|
gboolean exists;
|
|
|
|
|
|
|
|
|
|
children = es->vtable->enumerate (es->connection,
|
|
|
|
|
sender,
|
|
|
|
|
es->object_path,
|
|
|
|
|
es->user_data);
|
|
|
|
|
|
2022-03-17 17:24:08 +01:00
|
|
|
|
exists = g_strv_contains ((const gchar * const *) children, requested_node);
|
2010-07-15 22:26:42 +02:00
|
|
|
|
g_strfreev (children);
|
|
|
|
|
|
|
|
|
|
if (!exists)
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2010-07-15 22:53:43 +02:00
|
|
|
|
requested_node = NULL;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* get introspection data for the node */
|
|
|
|
|
interfaces = es->vtable->introspect (es->connection,
|
|
|
|
|
sender,
|
|
|
|
|
requested_object_path,
|
|
|
|
|
requested_node,
|
|
|
|
|
es->user_data);
|
2010-07-15 22:26:42 +02:00
|
|
|
|
|
|
|
|
|
if (interfaces == NULL)
|
|
|
|
|
goto out;
|
|
|
|
|
|
2010-06-30 18:10:00 +02:00
|
|
|
|
interface_info = NULL;
|
2010-07-15 22:26:42 +02:00
|
|
|
|
for (n = 0; interfaces[n] != NULL; n++)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
2010-07-15 22:26:42 +02:00
|
|
|
|
if (g_strcmp0 (interfaces[n]->name, interface_name) == 0)
|
|
|
|
|
interface_info = interfaces[n];
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* dispatch the call if the user wants to handle it */
|
2010-06-30 18:10:00 +02:00
|
|
|
|
if (interface_info != NULL)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
|
|
|
|
/* figure out where to dispatch the method call */
|
|
|
|
|
interface_user_data = NULL;
|
|
|
|
|
interface_vtable = es->vtable->dispatch (es->connection,
|
|
|
|
|
sender,
|
|
|
|
|
es->object_path,
|
|
|
|
|
interface_name,
|
|
|
|
|
requested_node,
|
|
|
|
|
&interface_user_data,
|
|
|
|
|
es->user_data);
|
|
|
|
|
if (interface_vtable == NULL)
|
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
|
|
CONNECTION_LOCK (connection);
|
|
|
|
|
handled = validate_and_maybe_schedule_method_call (es->connection,
|
|
|
|
|
message,
|
2010-06-09 23:57:04 +02:00
|
|
|
|
0,
|
|
|
|
|
es->id,
|
2010-06-30 18:10:00 +02:00
|
|
|
|
interface_info,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
interface_vtable,
|
|
|
|
|
es->context,
|
|
|
|
|
interface_user_data);
|
|
|
|
|
CONNECTION_UNLOCK (connection);
|
|
|
|
|
}
|
2024-05-13 14:00:34 +02:00
|
|
|
|
/* handle DBUS_INTERFACE_PROPERTIES if not explicitly handled */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
else if (is_property_get || is_property_set || is_property_get_all)
|
|
|
|
|
{
|
|
|
|
|
if (is_property_get)
|
2010-05-13 19:20:26 +02:00
|
|
|
|
g_variant_get (g_dbus_message_get_body (message), "(&s&s)", &interface_name, NULL);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
else if (is_property_set)
|
2010-05-13 19:20:26 +02:00
|
|
|
|
g_variant_get (g_dbus_message_get_body (message), "(&s&sv)", &interface_name, NULL, NULL);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
else if (is_property_get_all)
|
2010-05-13 19:20:26 +02:00
|
|
|
|
g_variant_get (g_dbus_message_get_body (message), "(&s)", &interface_name, NULL, NULL);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
else
|
|
|
|
|
g_assert_not_reached ();
|
|
|
|
|
|
|
|
|
|
/* see if the object supports this interface at all */
|
2010-07-15 22:26:42 +02:00
|
|
|
|
for (n = 0; interfaces[n] != NULL; n++)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
2010-07-15 22:26:42 +02:00
|
|
|
|
if (g_strcmp0 (interfaces[n]->name, interface_name) == 0)
|
|
|
|
|
interface_info = interfaces[n];
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-13 14:10:15 +02:00
|
|
|
|
/* Fail with DBUS_ERROR_INVALID_ARGS if the user-code
|
2010-05-06 20:13:59 +02:00
|
|
|
|
* claims it won't support the interface
|
|
|
|
|
*/
|
2010-06-30 18:10:00 +02:00
|
|
|
|
if (interface_info == NULL)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
|
|
|
|
GDBusMessage *reply;
|
|
|
|
|
reply = g_dbus_message_new_method_error (message,
|
2024-05-13 14:10:15 +02:00
|
|
|
|
DBUS_ERROR_INVALID_ARGS,
|
2018-06-08 16:15:54 +02:00
|
|
|
|
_("No such interface “%s”"),
|
2010-05-06 20:13:59 +02:00
|
|
|
|
interface_name);
|
2010-07-19 22:07:57 +02:00
|
|
|
|
g_dbus_connection_send_message (es->connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_object_unref (reply);
|
|
|
|
|
handled = TRUE;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* figure out where to dispatch the property get/set/getall calls */
|
|
|
|
|
interface_user_data = NULL;
|
|
|
|
|
interface_vtable = es->vtable->dispatch (es->connection,
|
|
|
|
|
sender,
|
|
|
|
|
es->object_path,
|
|
|
|
|
interface_name,
|
|
|
|
|
requested_node,
|
|
|
|
|
&interface_user_data,
|
|
|
|
|
es->user_data);
|
|
|
|
|
if (interface_vtable == NULL)
|
2010-07-16 01:37:26 +02:00
|
|
|
|
{
|
|
|
|
|
g_warning ("The subtree introspection function indicates that '%s' "
|
|
|
|
|
"is a valid interface name, but calling the dispatch "
|
|
|
|
|
"function on that interface gave us NULL", interface_name);
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
if (is_property_get || is_property_set)
|
|
|
|
|
{
|
|
|
|
|
CONNECTION_LOCK (connection);
|
|
|
|
|
handled = validate_and_maybe_schedule_property_getset (es->connection,
|
|
|
|
|
message,
|
2010-06-09 23:57:04 +02:00
|
|
|
|
0,
|
|
|
|
|
es->id,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
is_property_get,
|
2010-06-30 18:10:00 +02:00
|
|
|
|
interface_info,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
interface_vtable,
|
|
|
|
|
es->context,
|
|
|
|
|
interface_user_data);
|
|
|
|
|
CONNECTION_UNLOCK (connection);
|
|
|
|
|
}
|
|
|
|
|
else if (is_property_get_all)
|
|
|
|
|
{
|
|
|
|
|
CONNECTION_LOCK (connection);
|
|
|
|
|
handled = validate_and_maybe_schedule_property_get_all (es->connection,
|
|
|
|
|
message,
|
2010-06-09 23:57:04 +02:00
|
|
|
|
0,
|
|
|
|
|
es->id,
|
2010-06-30 18:10:00 +02:00
|
|
|
|
interface_info,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
interface_vtable,
|
|
|
|
|
es->context,
|
|
|
|
|
interface_user_data);
|
|
|
|
|
CONNECTION_UNLOCK (connection);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
out:
|
|
|
|
|
if (interfaces != NULL)
|
2010-07-15 22:26:42 +02:00
|
|
|
|
{
|
|
|
|
|
for (n = 0; interfaces[n] != NULL; n++)
|
|
|
|
|
g_dbus_interface_info_unref (interfaces[n]);
|
|
|
|
|
g_free (interfaces);
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
return handled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
|
{
|
2021-09-24 11:55:10 +02:00
|
|
|
|
GDBusMessage *message; /* (owned) */
|
|
|
|
|
ExportedSubtree *es; /* (owned) */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
} SubtreeDeferredData;
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
subtree_deferred_data_free (SubtreeDeferredData *data)
|
|
|
|
|
{
|
2023-02-22 03:46:55 +01:00
|
|
|
|
g_clear_object (&data->message);
|
2021-09-24 11:55:10 +02:00
|
|
|
|
exported_subtree_unref (data->es);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_free (data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* called without lock held in the thread where the caller registered the subtree */
|
|
|
|
|
static gboolean
|
|
|
|
|
process_subtree_vtable_message_in_idle_cb (gpointer _data)
|
|
|
|
|
{
|
|
|
|
|
SubtreeDeferredData *data = _data;
|
|
|
|
|
gboolean handled;
|
|
|
|
|
|
|
|
|
|
handled = FALSE;
|
|
|
|
|
|
2024-05-13 14:00:34 +02:00
|
|
|
|
if (g_strcmp0 (g_dbus_message_get_interface (data->message), DBUS_INTERFACE_INTROSPECTABLE) == 0 &&
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_strcmp0 (g_dbus_message_get_member (data->message), "Introspect") == 0 &&
|
|
|
|
|
g_strcmp0 (g_dbus_message_get_signature (data->message), "") == 0)
|
2010-05-10 04:13:18 +02:00
|
|
|
|
handled = handle_subtree_introspect (data->es->connection,
|
|
|
|
|
data->es,
|
|
|
|
|
data->message);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
else
|
2010-05-10 04:13:18 +02:00
|
|
|
|
handled = handle_subtree_method_invocation (data->es->connection,
|
|
|
|
|
data->es,
|
|
|
|
|
data->message);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
if (!handled)
|
|
|
|
|
{
|
|
|
|
|
CONNECTION_LOCK (data->es->connection);
|
|
|
|
|
handled = handle_generic_unlocked (data->es->connection, data->message);
|
|
|
|
|
CONNECTION_UNLOCK (data->es->connection);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* if we couldn't handle the request, just bail with the UnknownMethod error */
|
|
|
|
|
if (!handled)
|
|
|
|
|
{
|
|
|
|
|
GDBusMessage *reply;
|
|
|
|
|
reply = g_dbus_message_new_method_error (data->message,
|
2024-05-13 14:10:15 +02:00
|
|
|
|
DBUS_ERROR_UNKNOWN_METHOD,
|
2018-06-08 16:15:54 +02:00
|
|
|
|
_("Method “%s” on interface “%s” with signature “%s” does not exist"),
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_dbus_message_get_member (data->message),
|
|
|
|
|
g_dbus_message_get_interface (data->message),
|
|
|
|
|
g_dbus_message_get_signature (data->message));
|
2010-07-19 22:07:57 +02:00
|
|
|
|
g_dbus_connection_send_message (data->es->connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_object_unref (reply);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-17 14:27:03 +02:00
|
|
|
|
/* called in GDBusWorker thread with connection's lock held */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
static gboolean
|
|
|
|
|
subtree_message_func (GDBusConnection *connection,
|
|
|
|
|
ExportedSubtree *es,
|
|
|
|
|
GDBusMessage *message)
|
|
|
|
|
{
|
|
|
|
|
GSource *idle_source;
|
|
|
|
|
SubtreeDeferredData *data;
|
|
|
|
|
|
|
|
|
|
data = g_new0 (SubtreeDeferredData, 1);
|
|
|
|
|
data->message = g_object_ref (message);
|
2021-09-24 11:55:10 +02:00
|
|
|
|
data->es = exported_subtree_ref (es);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
/* defer this call to an idle handler in the right thread */
|
|
|
|
|
idle_source = g_idle_source_new ();
|
|
|
|
|
g_source_set_priority (idle_source, G_PRIORITY_HIGH);
|
|
|
|
|
g_source_set_callback (idle_source,
|
|
|
|
|
process_subtree_vtable_message_in_idle_cb,
|
|
|
|
|
data,
|
|
|
|
|
(GDestroyNotify) subtree_deferred_data_free);
|
2021-07-26 11:53:02 +02:00
|
|
|
|
g_source_set_static_name (idle_source, "[gio] process_subtree_vtable_message_in_idle_cb");
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_source_attach (idle_source, es->context);
|
|
|
|
|
g_source_unref (idle_source);
|
|
|
|
|
|
|
|
|
|
/* since we own the entire subtree, handlers for objects not in the subtree have been
|
|
|
|
|
* tried already by libdbus-1 - so we just need to ensure that we're always going
|
|
|
|
|
* to reply to the message
|
|
|
|
|
*/
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_dbus_connection_register_subtree:
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @connection: a #GDBusConnection
|
|
|
|
|
* @object_path: the object path to register the subtree at
|
|
|
|
|
* @vtable: a #GDBusSubtreeVTable to enumerate, introspect and
|
|
|
|
|
* dispatch nodes in the subtree
|
|
|
|
|
* @flags: flags used to fine tune the behavior of the subtree
|
|
|
|
|
* @user_data: data to pass to functions in @vtable
|
|
|
|
|
* @user_data_free_func: function to call when the subtree is unregistered
|
|
|
|
|
* @error: return location for error or %NULL
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
2014-02-02 03:18:29 +01:00
|
|
|
|
* Registers a whole subtree of dynamic objects.
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
|
|
|
|
* The @enumerate and @introspection functions in @vtable are used to
|
|
|
|
|
* convey, to remote callers, what nodes exist in the subtree rooted
|
|
|
|
|
* by @object_path.
|
|
|
|
|
*
|
|
|
|
|
* When handling remote calls into any node in the subtree, first the
|
|
|
|
|
* @enumerate function is used to check if the node exists. If the node exists
|
2022-03-04 17:21:55 +01:00
|
|
|
|
* or the %G_DBUS_SUBTREE_FLAGS_DISPATCH_TO_UNENUMERATED_NODES flag is set
|
2010-05-06 20:13:59 +02:00
|
|
|
|
* the @introspection function is used to check if the node supports the
|
|
|
|
|
* requested method. If so, the @dispatch function is used to determine
|
|
|
|
|
* where to dispatch the call. The collected #GDBusInterfaceVTable and
|
|
|
|
|
* #gpointer will be used to call into the interface vtable for processing
|
|
|
|
|
* the request.
|
|
|
|
|
*
|
2014-02-08 18:26:56 +01:00
|
|
|
|
* All calls into user-provided code will be invoked in the
|
|
|
|
|
* [thread-default main context][g-main-context-push-thread-default]
|
|
|
|
|
* of the thread you are calling this method from.
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
|
|
|
|
* If an existing subtree is already registered at @object_path or
|
2022-03-04 17:21:55 +01:00
|
|
|
|
* then @error is set to %G_IO_ERROR_EXISTS.
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
|
|
|
|
* Note that it is valid to register regular objects (using
|
|
|
|
|
* g_dbus_connection_register_object()) in a subtree registered with
|
|
|
|
|
* g_dbus_connection_register_subtree() - if so, the subtree handler
|
|
|
|
|
* is tried as the last resort. One way to think about a subtree
|
2014-02-02 03:18:29 +01:00
|
|
|
|
* handler is to consider it a fallback handler for object paths not
|
|
|
|
|
* registered via g_dbus_connection_register_object() or other bindings.
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
2010-07-14 20:46:12 +02:00
|
|
|
|
* Note that @vtable will be copied so you cannot change it after
|
|
|
|
|
* registration.
|
|
|
|
|
*
|
2014-02-08 18:26:56 +01:00
|
|
|
|
* See this [server][gdbus-subtree-server] for an example of how to use
|
|
|
|
|
* this method.
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
2020-12-15 10:14:20 +01:00
|
|
|
|
* Returns: 0 if @error is set, otherwise a subtree registration ID (never 0)
|
|
|
|
|
* that can be used with g_dbus_connection_unregister_subtree()
|
2010-05-06 22:02:08 +02:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.26
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*/
|
|
|
|
|
guint
|
2010-05-10 04:13:18 +02:00
|
|
|
|
g_dbus_connection_register_subtree (GDBusConnection *connection,
|
|
|
|
|
const gchar *object_path,
|
|
|
|
|
const GDBusSubtreeVTable *vtable,
|
|
|
|
|
GDBusSubtreeFlags flags,
|
|
|
|
|
gpointer user_data,
|
|
|
|
|
GDestroyNotify user_data_free_func,
|
|
|
|
|
GError **error)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
|
|
|
|
guint ret;
|
|
|
|
|
ExportedSubtree *es;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), 0);
|
|
|
|
|
g_return_val_if_fail (object_path != NULL && g_variant_is_object_path (object_path), 0);
|
|
|
|
|
g_return_val_if_fail (vtable != NULL, 0);
|
|
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, 0);
|
2011-10-19 18:23:30 +02:00
|
|
|
|
g_return_val_if_fail (check_initialized (connection), 0);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
|
|
CONNECTION_LOCK (connection);
|
|
|
|
|
|
2010-07-07 22:35:17 +02:00
|
|
|
|
es = g_hash_table_lookup (connection->map_object_path_to_es, object_path);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
if (es != NULL)
|
|
|
|
|
{
|
|
|
|
|
g_set_error (error,
|
|
|
|
|
G_IO_ERROR,
|
|
|
|
|
G_IO_ERROR_EXISTS,
|
|
|
|
|
_("A subtree is already exported for %s"),
|
|
|
|
|
object_path);
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
es = g_new0 (ExportedSubtree, 1);
|
2021-09-24 10:03:40 +02:00
|
|
|
|
es->refcount = 1;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
es->object_path = g_strdup (object_path);
|
|
|
|
|
es->connection = connection;
|
|
|
|
|
|
2010-07-14 20:46:12 +02:00
|
|
|
|
es->vtable = _g_dbus_subtree_vtable_copy (vtable);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
es->flags = flags;
|
2019-09-21 10:45:36 +02:00
|
|
|
|
es->id = (guint) g_atomic_int_add (&_global_subtree_registration_id, 1); /* TODO: overflow etc. */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
es->user_data = user_data;
|
|
|
|
|
es->user_data_free_func = user_data_free_func;
|
2011-10-05 16:46:57 +02:00
|
|
|
|
es->context = g_main_context_ref_thread_default ();
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2010-07-07 22:35:17 +02:00
|
|
|
|
g_hash_table_insert (connection->map_object_path_to_es, es->object_path, es);
|
|
|
|
|
g_hash_table_insert (connection->map_id_to_es,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
GUINT_TO_POINTER (es->id),
|
|
|
|
|
es);
|
|
|
|
|
|
|
|
|
|
ret = es->id;
|
|
|
|
|
|
|
|
|
|
out:
|
|
|
|
|
CONNECTION_UNLOCK (connection);
|
|
|
|
|
|
2024-04-10 01:44:40 +02:00
|
|
|
|
if (ret == 0 && user_data_free_func != NULL)
|
|
|
|
|
user_data_free_func (user_data);
|
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_dbus_connection_unregister_subtree:
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @connection: a #GDBusConnection
|
|
|
|
|
* @registration_id: a subtree registration id obtained from
|
|
|
|
|
* g_dbus_connection_register_subtree()
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
|
|
|
|
* Unregisters a subtree.
|
|
|
|
|
*
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* Returns: %TRUE if the subtree was unregistered, %FALSE otherwise
|
2010-05-06 22:02:08 +02:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.26
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*/
|
|
|
|
|
gboolean
|
|
|
|
|
g_dbus_connection_unregister_subtree (GDBusConnection *connection,
|
|
|
|
|
guint registration_id)
|
|
|
|
|
{
|
|
|
|
|
ExportedSubtree *es;
|
|
|
|
|
gboolean ret;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
|
2011-10-19 18:23:30 +02:00
|
|
|
|
g_return_val_if_fail (check_initialized (connection), FALSE);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
ret = FALSE;
|
|
|
|
|
|
|
|
|
|
CONNECTION_LOCK (connection);
|
|
|
|
|
|
2010-07-07 22:35:17 +02:00
|
|
|
|
es = g_hash_table_lookup (connection->map_id_to_es,
|
2010-05-06 20:13:59 +02:00
|
|
|
|
GUINT_TO_POINTER (registration_id));
|
|
|
|
|
if (es == NULL)
|
2010-05-10 04:13:18 +02:00
|
|
|
|
goto out;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2010-07-07 22:35:17 +02:00
|
|
|
|
g_warn_if_fail (g_hash_table_remove (connection->map_id_to_es, GUINT_TO_POINTER (es->id)));
|
|
|
|
|
g_warn_if_fail (g_hash_table_remove (connection->map_object_path_to_es, es->object_path));
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
ret = TRUE;
|
|
|
|
|
|
|
|
|
|
out:
|
|
|
|
|
CONNECTION_UNLOCK (connection);
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
2011-10-17 14:27:03 +02:00
|
|
|
|
/* may be called in any thread, with connection's lock held */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
static void
|
2010-05-10 04:13:18 +02:00
|
|
|
|
handle_generic_ping_unlocked (GDBusConnection *connection,
|
|
|
|
|
const gchar *object_path,
|
|
|
|
|
GDBusMessage *message)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
|
|
|
|
GDBusMessage *reply;
|
|
|
|
|
reply = g_dbus_message_new_method_reply (message);
|
2010-07-19 22:07:57 +02:00
|
|
|
|
g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_object_unref (reply);
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-17 14:27:03 +02:00
|
|
|
|
/* may be called in any thread, with connection's lock held */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
static void
|
2010-05-10 04:13:18 +02:00
|
|
|
|
handle_generic_get_machine_id_unlocked (GDBusConnection *connection,
|
|
|
|
|
const gchar *object_path,
|
|
|
|
|
GDBusMessage *message)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
|
|
|
|
GDBusMessage *reply;
|
|
|
|
|
|
|
|
|
|
reply = NULL;
|
2010-07-07 22:35:17 +02:00
|
|
|
|
if (connection->machine_id == NULL)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
|
|
|
|
GError *error;
|
GDBus: Handle autolaunching on UNIX/Freedesktop OSes
Also add a 'address' G_DBUS_DEBUG option that will print out useful
debug information such as
GDBus-debug:Address: In g_dbus_address_get_for_bus_sync() for bus type `session'
GDBus-debug:Address: env var DBUS_SESSION_BUS_ADDRESS is not set
GDBus-debug:Address: env var DBUS_SYSTEM_BUS_ADDRESS is not set
GDBus-debug:Address: env var DBUS_STARTER_BUS_TYPE is not set
GDBus-debug:Address: Running `dbus-launch --autolaunch=05e508961149264c9b750a4c494aa6f7 --binary-syntax --close-stderr' to get bus address (possibly autolaunching)
GDBus-debug:Address: dbus-launch output:
0000: 75 6e 69 78 3a 61 62 73 74 72 61 63 74 3d 2f 74 unix:abstract=/t
0010: 6d 70 2f 64 62 75 73 2d 77 42 41 6f 4b 59 49 52 mp/dbus-wBAoKYIR
0020: 7a 75 2c 67 75 69 64 3d 30 34 30 64 31 33 66 33 zu,guid=040d13f3
0030: 30 61 30 62 35 32 63 32 30 66 36 32 63 34 31 63 0a0b52c20f62c41c
0040: 30 30 30 30 35 30 38 64 00 d2 38 00 00 01 00 40 0000508d..8....@
0050: 05 00 00 00 00 .....
GDBus-debug:Address: dbus-launch stderr output:
14542: Autolaunch enabled (using X11).
14542: --exit-with-session automatically enabled
14542: Connected to X11 display ':0.0'
14542: === Parent dbus-launch continues
14542: Waiting for babysitter's intermediate parent
14542: Reading address from bus
14542: Reading PID from daemon
14542: Saving x11 address
14542: Created window 88080385
14542: session file: /root/.dbus/session-bus/05e508961149264c9b750a4c494aa6f7-0
14542: dbus-launch exiting
GDBus-debug:Address: Returning address `unix:abstract=/tmp/dbus-wBAoKYIRzu,guid=040d13f30a0b52c20f62c41c0000508d' for bus type `session'
and
GDBus-debug:Address: In g_dbus_address_get_for_bus_sync() for bus type `session'
GDBus-debug:Address: env var DBUS_SESSION_BUS_ADDRESS is not set
GDBus-debug:Address: env var DBUS_SYSTEM_BUS_ADDRESS is not set
GDBus-debug:Address: env var DBUS_STARTER_BUS_TYPE is not set
GDBus-debug:Address: Running `dbus-launch --autolaunch=05e508961149264c9b750a4c494aa6f7 --binary-syntax --close-stderr' to get bus address (possibly autolaunching)
GDBus-debug:Address: dbus-launch output:
0000: 75 6e 69 78 3a 61 62 73 74 72 61 63 74 3d 2f 74 unix:abstract=/t
0010: 6d 70 2f 64 62 75 73 2d 77 42 41 6f 4b 59 49 52 mp/dbus-wBAoKYIR
0020: 7a 75 2c 67 75 69 64 3d 30 34 30 64 31 33 66 33 zu,guid=040d13f3
0030: 30 61 30 62 35 32 63 32 30 66 36 32 63 34 31 63 0a0b52c20f62c41c
0040: 30 30 30 30 35 30 38 64 00 d2 38 00 00 01 00 40 0000508d..8....@
0050: 05 00 00 00 00 .....
GDBus-debug:Address: dbus-launch stderr output:
14549: Autolaunch enabled (using X11).
14549: --exit-with-session automatically enabled
14549: Connected to X11 display ':0.0'
14549: dbus-daemon is already running. Returning existing parameters.
14549: dbus-launch exiting
GDBus-debug:Address: Returning address `unix:abstract=/tmp/dbus-wBAoKYIRzu,guid=040d13f30a0b52c20f62c41c0000508d' for bus type `session'
Note that things work exactly like libdbus, e.g. from the
dbus-launch(1) man page:
Whenever an autolaunch occurs, the application that had to start a
new bus will be in its own little world; it can effectively end up
starting a whole new session if it tries to use a lot of bus
services. This can be suboptimal or even totally broken, depending
on the app and what it tries to do.
[...]
You can always avoid autolaunch by manually setting
DBUS_SESSION_BUS_ADDRESS. Autolaunch happens because the default
address if none is set is "autolaunch:", so if any other address is
set there will be no autolaunch. You can however include autolaunch
in an explicit session bus address as a fallback, for example
DBUS_SESSION_BUS_ADDRESS="something:,autolaunch:" - in that case if
the first address doesn't work, processes will autolaunch. (The bus
address variable contains a comma-separated list of addresses to
try.)
Signed-off-by: David Zeuthen <davidz@redhat.com>
2010-07-06 22:57:28 +02:00
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
error = NULL;
|
2010-07-07 22:35:17 +02:00
|
|
|
|
connection->machine_id = _g_dbus_get_machine_id (&error);
|
|
|
|
|
if (connection->machine_id == NULL)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
GDBus: Handle autolaunching on UNIX/Freedesktop OSes
Also add a 'address' G_DBUS_DEBUG option that will print out useful
debug information such as
GDBus-debug:Address: In g_dbus_address_get_for_bus_sync() for bus type `session'
GDBus-debug:Address: env var DBUS_SESSION_BUS_ADDRESS is not set
GDBus-debug:Address: env var DBUS_SYSTEM_BUS_ADDRESS is not set
GDBus-debug:Address: env var DBUS_STARTER_BUS_TYPE is not set
GDBus-debug:Address: Running `dbus-launch --autolaunch=05e508961149264c9b750a4c494aa6f7 --binary-syntax --close-stderr' to get bus address (possibly autolaunching)
GDBus-debug:Address: dbus-launch output:
0000: 75 6e 69 78 3a 61 62 73 74 72 61 63 74 3d 2f 74 unix:abstract=/t
0010: 6d 70 2f 64 62 75 73 2d 77 42 41 6f 4b 59 49 52 mp/dbus-wBAoKYIR
0020: 7a 75 2c 67 75 69 64 3d 30 34 30 64 31 33 66 33 zu,guid=040d13f3
0030: 30 61 30 62 35 32 63 32 30 66 36 32 63 34 31 63 0a0b52c20f62c41c
0040: 30 30 30 30 35 30 38 64 00 d2 38 00 00 01 00 40 0000508d..8....@
0050: 05 00 00 00 00 .....
GDBus-debug:Address: dbus-launch stderr output:
14542: Autolaunch enabled (using X11).
14542: --exit-with-session automatically enabled
14542: Connected to X11 display ':0.0'
14542: === Parent dbus-launch continues
14542: Waiting for babysitter's intermediate parent
14542: Reading address from bus
14542: Reading PID from daemon
14542: Saving x11 address
14542: Created window 88080385
14542: session file: /root/.dbus/session-bus/05e508961149264c9b750a4c494aa6f7-0
14542: dbus-launch exiting
GDBus-debug:Address: Returning address `unix:abstract=/tmp/dbus-wBAoKYIRzu,guid=040d13f30a0b52c20f62c41c0000508d' for bus type `session'
and
GDBus-debug:Address: In g_dbus_address_get_for_bus_sync() for bus type `session'
GDBus-debug:Address: env var DBUS_SESSION_BUS_ADDRESS is not set
GDBus-debug:Address: env var DBUS_SYSTEM_BUS_ADDRESS is not set
GDBus-debug:Address: env var DBUS_STARTER_BUS_TYPE is not set
GDBus-debug:Address: Running `dbus-launch --autolaunch=05e508961149264c9b750a4c494aa6f7 --binary-syntax --close-stderr' to get bus address (possibly autolaunching)
GDBus-debug:Address: dbus-launch output:
0000: 75 6e 69 78 3a 61 62 73 74 72 61 63 74 3d 2f 74 unix:abstract=/t
0010: 6d 70 2f 64 62 75 73 2d 77 42 41 6f 4b 59 49 52 mp/dbus-wBAoKYIR
0020: 7a 75 2c 67 75 69 64 3d 30 34 30 64 31 33 66 33 zu,guid=040d13f3
0030: 30 61 30 62 35 32 63 32 30 66 36 32 63 34 31 63 0a0b52c20f62c41c
0040: 30 30 30 30 35 30 38 64 00 d2 38 00 00 01 00 40 0000508d..8....@
0050: 05 00 00 00 00 .....
GDBus-debug:Address: dbus-launch stderr output:
14549: Autolaunch enabled (using X11).
14549: --exit-with-session automatically enabled
14549: Connected to X11 display ':0.0'
14549: dbus-daemon is already running. Returning existing parameters.
14549: dbus-launch exiting
GDBus-debug:Address: Returning address `unix:abstract=/tmp/dbus-wBAoKYIRzu,guid=040d13f30a0b52c20f62c41c0000508d' for bus type `session'
Note that things work exactly like libdbus, e.g. from the
dbus-launch(1) man page:
Whenever an autolaunch occurs, the application that had to start a
new bus will be in its own little world; it can effectively end up
starting a whole new session if it tries to use a lot of bus
services. This can be suboptimal or even totally broken, depending
on the app and what it tries to do.
[...]
You can always avoid autolaunch by manually setting
DBUS_SESSION_BUS_ADDRESS. Autolaunch happens because the default
address if none is set is "autolaunch:", so if any other address is
set there will be no autolaunch. You can however include autolaunch
in an explicit session bus address as a fallback, for example
DBUS_SESSION_BUS_ADDRESS="something:,autolaunch:" - in that case if
the first address doesn't work, processes will autolaunch. (The bus
address variable contains a comma-separated list of addresses to
try.)
Signed-off-by: David Zeuthen <davidz@redhat.com>
2010-07-06 22:57:28 +02:00
|
|
|
|
reply = g_dbus_message_new_method_error_literal (message,
|
2024-05-13 14:10:15 +02:00
|
|
|
|
DBUS_ERROR_FAILED,
|
GDBus: Handle autolaunching on UNIX/Freedesktop OSes
Also add a 'address' G_DBUS_DEBUG option that will print out useful
debug information such as
GDBus-debug:Address: In g_dbus_address_get_for_bus_sync() for bus type `session'
GDBus-debug:Address: env var DBUS_SESSION_BUS_ADDRESS is not set
GDBus-debug:Address: env var DBUS_SYSTEM_BUS_ADDRESS is not set
GDBus-debug:Address: env var DBUS_STARTER_BUS_TYPE is not set
GDBus-debug:Address: Running `dbus-launch --autolaunch=05e508961149264c9b750a4c494aa6f7 --binary-syntax --close-stderr' to get bus address (possibly autolaunching)
GDBus-debug:Address: dbus-launch output:
0000: 75 6e 69 78 3a 61 62 73 74 72 61 63 74 3d 2f 74 unix:abstract=/t
0010: 6d 70 2f 64 62 75 73 2d 77 42 41 6f 4b 59 49 52 mp/dbus-wBAoKYIR
0020: 7a 75 2c 67 75 69 64 3d 30 34 30 64 31 33 66 33 zu,guid=040d13f3
0030: 30 61 30 62 35 32 63 32 30 66 36 32 63 34 31 63 0a0b52c20f62c41c
0040: 30 30 30 30 35 30 38 64 00 d2 38 00 00 01 00 40 0000508d..8....@
0050: 05 00 00 00 00 .....
GDBus-debug:Address: dbus-launch stderr output:
14542: Autolaunch enabled (using X11).
14542: --exit-with-session automatically enabled
14542: Connected to X11 display ':0.0'
14542: === Parent dbus-launch continues
14542: Waiting for babysitter's intermediate parent
14542: Reading address from bus
14542: Reading PID from daemon
14542: Saving x11 address
14542: Created window 88080385
14542: session file: /root/.dbus/session-bus/05e508961149264c9b750a4c494aa6f7-0
14542: dbus-launch exiting
GDBus-debug:Address: Returning address `unix:abstract=/tmp/dbus-wBAoKYIRzu,guid=040d13f30a0b52c20f62c41c0000508d' for bus type `session'
and
GDBus-debug:Address: In g_dbus_address_get_for_bus_sync() for bus type `session'
GDBus-debug:Address: env var DBUS_SESSION_BUS_ADDRESS is not set
GDBus-debug:Address: env var DBUS_SYSTEM_BUS_ADDRESS is not set
GDBus-debug:Address: env var DBUS_STARTER_BUS_TYPE is not set
GDBus-debug:Address: Running `dbus-launch --autolaunch=05e508961149264c9b750a4c494aa6f7 --binary-syntax --close-stderr' to get bus address (possibly autolaunching)
GDBus-debug:Address: dbus-launch output:
0000: 75 6e 69 78 3a 61 62 73 74 72 61 63 74 3d 2f 74 unix:abstract=/t
0010: 6d 70 2f 64 62 75 73 2d 77 42 41 6f 4b 59 49 52 mp/dbus-wBAoKYIR
0020: 7a 75 2c 67 75 69 64 3d 30 34 30 64 31 33 66 33 zu,guid=040d13f3
0030: 30 61 30 62 35 32 63 32 30 66 36 32 63 34 31 63 0a0b52c20f62c41c
0040: 30 30 30 30 35 30 38 64 00 d2 38 00 00 01 00 40 0000508d..8....@
0050: 05 00 00 00 00 .....
GDBus-debug:Address: dbus-launch stderr output:
14549: Autolaunch enabled (using X11).
14549: --exit-with-session automatically enabled
14549: Connected to X11 display ':0.0'
14549: dbus-daemon is already running. Returning existing parameters.
14549: dbus-launch exiting
GDBus-debug:Address: Returning address `unix:abstract=/tmp/dbus-wBAoKYIRzu,guid=040d13f30a0b52c20f62c41c0000508d' for bus type `session'
Note that things work exactly like libdbus, e.g. from the
dbus-launch(1) man page:
Whenever an autolaunch occurs, the application that had to start a
new bus will be in its own little world; it can effectively end up
starting a whole new session if it tries to use a lot of bus
services. This can be suboptimal or even totally broken, depending
on the app and what it tries to do.
[...]
You can always avoid autolaunch by manually setting
DBUS_SESSION_BUS_ADDRESS. Autolaunch happens because the default
address if none is set is "autolaunch:", so if any other address is
set there will be no autolaunch. You can however include autolaunch
in an explicit session bus address as a fallback, for example
DBUS_SESSION_BUS_ADDRESS="something:,autolaunch:" - in that case if
the first address doesn't work, processes will autolaunch. (The bus
address variable contains a comma-separated list of addresses to
try.)
Signed-off-by: David Zeuthen <davidz@redhat.com>
2010-07-06 22:57:28 +02:00
|
|
|
|
error->message);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_error_free (error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (reply == NULL)
|
|
|
|
|
{
|
|
|
|
|
reply = g_dbus_message_new_method_reply (message);
|
2010-07-07 22:35:17 +02:00
|
|
|
|
g_dbus_message_set_body (reply, g_variant_new ("(s)", connection->machine_id));
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
2010-07-19 22:07:57 +02:00
|
|
|
|
g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_object_unref (reply);
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-17 14:27:03 +02:00
|
|
|
|
/* may be called in any thread, with connection's lock held */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
static void
|
2010-05-10 04:13:18 +02:00
|
|
|
|
handle_generic_introspect_unlocked (GDBusConnection *connection,
|
|
|
|
|
const gchar *object_path,
|
|
|
|
|
GDBusMessage *message)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
|
|
|
|
guint n;
|
|
|
|
|
GString *s;
|
|
|
|
|
gchar **registered;
|
|
|
|
|
GDBusMessage *reply;
|
|
|
|
|
|
|
|
|
|
/* first the header */
|
|
|
|
|
s = g_string_new (NULL);
|
|
|
|
|
introspect_append_header (s);
|
|
|
|
|
|
|
|
|
|
registered = g_dbus_connection_list_registered_unlocked (connection, object_path);
|
|
|
|
|
for (n = 0; registered != NULL && registered[n] != NULL; n++)
|
|
|
|
|
g_string_append_printf (s, " <node name=\"%s\"/>\n", registered[n]);
|
|
|
|
|
g_strfreev (registered);
|
|
|
|
|
g_string_append (s, "</node>\n");
|
|
|
|
|
|
|
|
|
|
reply = g_dbus_message_new_method_reply (message);
|
|
|
|
|
g_dbus_message_set_body (reply, g_variant_new ("(s)", s->str));
|
2010-07-19 22:07:57 +02:00
|
|
|
|
g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_object_unref (reply);
|
|
|
|
|
g_string_free (s, TRUE);
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-17 14:27:03 +02:00
|
|
|
|
/* may be called in any thread, with connection's lock held */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
static gboolean
|
|
|
|
|
handle_generic_unlocked (GDBusConnection *connection,
|
|
|
|
|
GDBusMessage *message)
|
|
|
|
|
{
|
|
|
|
|
gboolean handled;
|
|
|
|
|
const gchar *interface_name;
|
|
|
|
|
const gchar *member;
|
|
|
|
|
const gchar *signature;
|
|
|
|
|
const gchar *path;
|
|
|
|
|
|
|
|
|
|
CONNECTION_ENSURE_LOCK (connection);
|
|
|
|
|
|
|
|
|
|
handled = FALSE;
|
|
|
|
|
|
|
|
|
|
interface_name = g_dbus_message_get_interface (message);
|
|
|
|
|
member = g_dbus_message_get_member (message);
|
|
|
|
|
signature = g_dbus_message_get_signature (message);
|
|
|
|
|
path = g_dbus_message_get_path (message);
|
|
|
|
|
|
2024-05-13 14:00:34 +02:00
|
|
|
|
if (g_strcmp0 (interface_name, DBUS_INTERFACE_INTROSPECTABLE) == 0 &&
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_strcmp0 (member, "Introspect") == 0 &&
|
|
|
|
|
g_strcmp0 (signature, "") == 0)
|
|
|
|
|
{
|
|
|
|
|
handle_generic_introspect_unlocked (connection, path, message);
|
|
|
|
|
handled = TRUE;
|
|
|
|
|
}
|
2024-05-13 14:00:34 +02:00
|
|
|
|
else if (g_strcmp0 (interface_name, DBUS_INTERFACE_PEER) == 0 &&
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_strcmp0 (member, "Ping") == 0 &&
|
|
|
|
|
g_strcmp0 (signature, "") == 0)
|
|
|
|
|
{
|
|
|
|
|
handle_generic_ping_unlocked (connection, path, message);
|
|
|
|
|
handled = TRUE;
|
|
|
|
|
}
|
2024-05-13 14:00:34 +02:00
|
|
|
|
else if (g_strcmp0 (interface_name, DBUS_INTERFACE_PEER) == 0 &&
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_strcmp0 (member, "GetMachineId") == 0 &&
|
|
|
|
|
g_strcmp0 (signature, "") == 0)
|
|
|
|
|
{
|
|
|
|
|
handle_generic_get_machine_id_unlocked (connection, path, message);
|
|
|
|
|
handled = TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return handled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
2011-10-17 14:27:03 +02:00
|
|
|
|
/* called in GDBusWorker thread with connection's lock held */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
static void
|
2010-05-10 04:13:18 +02:00
|
|
|
|
distribute_method_call (GDBusConnection *connection,
|
|
|
|
|
GDBusMessage *message)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
2010-05-21 18:09:27 +02:00
|
|
|
|
GDBusMessage *reply;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
ExportedObject *eo;
|
|
|
|
|
ExportedSubtree *es;
|
2023-08-16 17:24:02 +02:00
|
|
|
|
const gchar *path;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
const gchar *interface_name;
|
|
|
|
|
const gchar *member;
|
|
|
|
|
gchar *subtree_path;
|
|
|
|
|
gchar *needle;
|
2021-05-27 10:48:34 +02:00
|
|
|
|
gboolean object_found = FALSE;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2010-05-06 23:31:51 +02:00
|
|
|
|
g_assert (g_dbus_message_get_message_type (message) == G_DBUS_MESSAGE_TYPE_METHOD_CALL);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2023-08-16 17:24:26 +02:00
|
|
|
|
/* these are required, and should have been validated by validate_headers()
|
|
|
|
|
* in gdbusmessage.c already */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
member = g_dbus_message_get_member (message);
|
|
|
|
|
path = g_dbus_message_get_path (message);
|
2023-08-16 17:24:26 +02:00
|
|
|
|
|
|
|
|
|
g_assert (member != NULL);
|
|
|
|
|
g_assert (path != NULL);
|
|
|
|
|
|
|
|
|
|
/* this is optional */
|
|
|
|
|
interface_name = g_dbus_message_get_interface (message);
|
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
subtree_path = g_strdup (path);
|
|
|
|
|
needle = strrchr (subtree_path, '/');
|
|
|
|
|
if (needle != NULL && needle != subtree_path)
|
|
|
|
|
{
|
|
|
|
|
*needle = '\0';
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
g_free (subtree_path);
|
|
|
|
|
subtree_path = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-01 20:37:16 +02:00
|
|
|
|
if (G_UNLIKELY (_g_dbus_debug_incoming ()))
|
|
|
|
|
{
|
|
|
|
|
_g_dbus_debug_print_lock ();
|
|
|
|
|
g_print ("========================================================================\n"
|
|
|
|
|
"GDBus-debug:Incoming:\n"
|
2010-08-03 21:36:16 +02:00
|
|
|
|
" <<<< METHOD INVOCATION %s.%s()\n"
|
2010-07-01 20:37:16 +02:00
|
|
|
|
" on object %s\n"
|
2010-08-04 22:59:26 +02:00
|
|
|
|
" invoked by name %s\n"
|
|
|
|
|
" serial %d\n",
|
2010-07-01 20:37:16 +02:00
|
|
|
|
interface_name, member,
|
|
|
|
|
path,
|
2010-08-04 22:59:26 +02:00
|
|
|
|
g_dbus_message_get_sender (message) != NULL ? g_dbus_message_get_sender (message) : "(none)",
|
|
|
|
|
g_dbus_message_get_serial (message));
|
2010-07-01 20:37:16 +02:00
|
|
|
|
_g_dbus_debug_print_unlock ();
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-16 17:24:02 +02:00
|
|
|
|
eo = g_hash_table_lookup (connection->map_object_path_to_eo, path);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
if (eo != NULL)
|
|
|
|
|
{
|
2021-05-27 10:48:34 +02:00
|
|
|
|
if (obj_message_func (connection, eo, message, &object_found))
|
2010-05-06 20:13:59 +02:00
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-16 17:24:02 +02:00
|
|
|
|
es = g_hash_table_lookup (connection->map_object_path_to_es, path);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
if (es != NULL)
|
|
|
|
|
{
|
|
|
|
|
if (subtree_message_func (connection, es, message))
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (subtree_path != NULL)
|
|
|
|
|
{
|
2010-07-07 22:35:17 +02:00
|
|
|
|
es = g_hash_table_lookup (connection->map_object_path_to_es, subtree_path);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
if (es != NULL)
|
|
|
|
|
{
|
|
|
|
|
if (subtree_message_func (connection, es, message))
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (handle_generic_unlocked (connection, message))
|
|
|
|
|
goto out;
|
|
|
|
|
|
2010-05-21 18:09:27 +02:00
|
|
|
|
/* if we end up here, the message has not been not handled - so return an error saying this */
|
2021-05-27 10:48:34 +02:00
|
|
|
|
if (object_found == TRUE)
|
|
|
|
|
{
|
|
|
|
|
reply = g_dbus_message_new_method_error (message,
|
2024-05-13 14:10:15 +02:00
|
|
|
|
DBUS_ERROR_UNKNOWN_METHOD,
|
2021-05-27 10:48:34 +02:00
|
|
|
|
_("No such interface “%s” on object at path %s"),
|
|
|
|
|
interface_name,
|
2023-08-16 17:24:02 +02:00
|
|
|
|
path);
|
2021-05-27 10:48:34 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
reply = g_dbus_message_new_method_error (message,
|
2024-05-13 14:10:15 +02:00
|
|
|
|
DBUS_ERROR_UNKNOWN_METHOD,
|
2021-05-27 10:48:34 +02:00
|
|
|
|
_("Object does not exist at path “%s”"),
|
2023-08-16 17:24:02 +02:00
|
|
|
|
path);
|
2021-05-27 10:48:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
2010-07-19 22:07:57 +02:00
|
|
|
|
g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
|
2010-05-21 18:09:27 +02:00
|
|
|
|
g_object_unref (reply);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
out:
|
|
|
|
|
g_free (subtree_path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
2011-10-17 14:27:03 +02:00
|
|
|
|
/* Called in any user thread, with the message_bus_lock held. */
|
2011-12-06 13:44:36 +01:00
|
|
|
|
static GWeakRef *
|
2010-05-06 20:13:59 +02:00
|
|
|
|
message_bus_get_singleton (GBusType bus_type,
|
|
|
|
|
GError **error)
|
|
|
|
|
{
|
2011-12-06 13:44:36 +01:00
|
|
|
|
GWeakRef *ret;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
const gchar *starter_bus;
|
|
|
|
|
|
|
|
|
|
ret = NULL;
|
|
|
|
|
|
|
|
|
|
switch (bus_type)
|
|
|
|
|
{
|
|
|
|
|
case G_BUS_TYPE_SESSION:
|
|
|
|
|
ret = &the_session_bus;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case G_BUS_TYPE_SYSTEM:
|
|
|
|
|
ret = &the_system_bus;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case G_BUS_TYPE_STARTER:
|
|
|
|
|
starter_bus = g_getenv ("DBUS_STARTER_BUS_TYPE");
|
|
|
|
|
if (g_strcmp0 (starter_bus, "session") == 0)
|
|
|
|
|
{
|
|
|
|
|
ret = message_bus_get_singleton (G_BUS_TYPE_SESSION, error);
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
else if (g_strcmp0 (starter_bus, "system") == 0)
|
|
|
|
|
{
|
|
|
|
|
ret = message_bus_get_singleton (G_BUS_TYPE_SYSTEM, error);
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (starter_bus != NULL)
|
|
|
|
|
{
|
|
|
|
|
g_set_error (error,
|
|
|
|
|
G_IO_ERROR,
|
|
|
|
|
G_IO_ERROR_INVALID_ARGUMENT,
|
|
|
|
|
_("Cannot determine bus address from DBUS_STARTER_BUS_TYPE environment variable"
|
2018-06-08 16:15:54 +02:00
|
|
|
|
" — unknown value “%s”"),
|
2010-05-06 20:13:59 +02:00
|
|
|
|
starter_bus);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
g_set_error_literal (error,
|
|
|
|
|
G_IO_ERROR,
|
|
|
|
|
G_IO_ERROR_INVALID_ARGUMENT,
|
|
|
|
|
_("Cannot determine bus address because the DBUS_STARTER_BUS_TYPE environment "
|
|
|
|
|
"variable is not set"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
2010-05-10 04:13:18 +02:00
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
g_assert_not_reached ();
|
|
|
|
|
break;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
2010-05-10 04:13:18 +02:00
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
out:
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-17 14:27:03 +02:00
|
|
|
|
/* Called in any user thread, without holding locks. */
|
2010-05-06 20:13:59 +02:00
|
|
|
|
static GDBusConnection *
|
2010-05-10 04:13:18 +02:00
|
|
|
|
get_uninitialized_connection (GBusType bus_type,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GError **error)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
2011-12-06 13:44:36 +01:00
|
|
|
|
GWeakRef *singleton;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
GDBusConnection *ret;
|
|
|
|
|
|
|
|
|
|
ret = NULL;
|
|
|
|
|
|
|
|
|
|
G_LOCK (message_bus_lock);
|
|
|
|
|
singleton = message_bus_get_singleton (bus_type, error);
|
|
|
|
|
if (singleton == NULL)
|
|
|
|
|
goto out;
|
|
|
|
|
|
2011-12-06 13:44:36 +01:00
|
|
|
|
ret = g_weak_ref_get (singleton);
|
|
|
|
|
|
|
|
|
|
if (ret == NULL)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
|
|
|
|
gchar *address;
|
|
|
|
|
address = g_dbus_address_get_for_bus_sync (bus_type, cancellable, error);
|
|
|
|
|
if (address == NULL)
|
|
|
|
|
goto out;
|
2011-12-06 13:44:36 +01:00
|
|
|
|
ret = g_object_new (G_TYPE_DBUS_CONNECTION,
|
|
|
|
|
"address", address,
|
|
|
|
|
"flags", G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
|
2022-07-24 14:36:50 +02:00
|
|
|
|
#ifdef __linux__
|
|
|
|
|
G_DBUS_CONNECTION_FLAGS_CROSS_NAMESPACE |
|
|
|
|
|
#endif
|
2011-12-06 13:44:36 +01:00
|
|
|
|
G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
|
|
|
|
|
"exit-on-close", TRUE,
|
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
|
|
g_weak_ref_set (singleton, ret);
|
2010-05-14 14:22:45 +02:00
|
|
|
|
g_free (address);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g_assert (ret != NULL);
|
|
|
|
|
|
|
|
|
|
out:
|
|
|
|
|
G_UNLOCK (message_bus_lock);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-06 14:51:09 +02:00
|
|
|
|
/* May be called from any thread. Must not hold message_bus_lock. */
|
|
|
|
|
GDBusConnection *
|
|
|
|
|
_g_bus_get_singleton_if_exists (GBusType bus_type)
|
|
|
|
|
{
|
|
|
|
|
GWeakRef *singleton;
|
|
|
|
|
GDBusConnection *ret = NULL;
|
|
|
|
|
|
|
|
|
|
G_LOCK (message_bus_lock);
|
|
|
|
|
singleton = message_bus_get_singleton (bus_type, NULL);
|
|
|
|
|
if (singleton == NULL)
|
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
|
|
ret = g_weak_ref_get (singleton);
|
|
|
|
|
|
|
|
|
|
out:
|
|
|
|
|
G_UNLOCK (message_bus_lock);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-04 09:35:39 +02:00
|
|
|
|
/* May be called from any thread. Must not hold message_bus_lock. */
|
|
|
|
|
void
|
|
|
|
|
_g_bus_forget_singleton (GBusType bus_type)
|
|
|
|
|
{
|
|
|
|
|
GWeakRef *singleton;
|
|
|
|
|
|
|
|
|
|
G_LOCK (message_bus_lock);
|
|
|
|
|
|
|
|
|
|
singleton = message_bus_get_singleton (bus_type, NULL);
|
|
|
|
|
|
|
|
|
|
if (singleton != NULL)
|
|
|
|
|
g_weak_ref_set (singleton, NULL);
|
|
|
|
|
|
|
|
|
|
G_UNLOCK (message_bus_lock);
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
/**
|
|
|
|
|
* g_bus_get_sync:
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @bus_type: a #GBusType
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @cancellable: (nullable): a #GCancellable or %NULL
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @error: return location for error or %NULL
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
|
|
|
|
* Synchronously connects to the message bus specified by @bus_type.
|
|
|
|
|
* Note that the returned object may shared with other callers,
|
|
|
|
|
* e.g. if two separate parts of a process calls this function with
|
|
|
|
|
* the same @bus_type, they will share the same object.
|
|
|
|
|
*
|
|
|
|
|
* This is a synchronous failable function. See g_bus_get() and
|
|
|
|
|
* g_bus_get_finish() for the asynchronous version.
|
|
|
|
|
*
|
|
|
|
|
* The returned object is a singleton, that is, shared with other
|
|
|
|
|
* callers of g_bus_get() and g_bus_get_sync() for @bus_type. In the
|
|
|
|
|
* event that you need a private message bus connection, use
|
2010-05-12 03:59:42 +02:00
|
|
|
|
* g_dbus_address_get_for_bus_sync() and
|
2022-01-28 14:11:34 +01:00
|
|
|
|
* g_dbus_connection_new_for_address() with
|
|
|
|
|
* G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT and
|
|
|
|
|
* G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION flags.
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
|
|
|
|
* Note that the returned #GDBusConnection object will (usually) have
|
|
|
|
|
* the #GDBusConnection:exit-on-close property set to %TRUE.
|
|
|
|
|
*
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* Returns: (transfer full): a #GDBusConnection or %NULL if @error is set.
|
|
|
|
|
* Free with g_object_unref().
|
2010-05-06 22:02:08 +02:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.26
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*/
|
|
|
|
|
GDBusConnection *
|
2010-05-10 04:13:18 +02:00
|
|
|
|
g_bus_get_sync (GBusType bus_type,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GError **error)
|
2010-05-06 20:13:59 +02:00
|
|
|
|
{
|
|
|
|
|
GDBusConnection *connection;
|
|
|
|
|
|
2016-10-31 08:36:18 +01:00
|
|
|
|
_g_dbus_initialize ();
|
|
|
|
|
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
|
|
|
|
|
|
|
|
|
|
connection = get_uninitialized_connection (bus_type, cancellable, error);
|
|
|
|
|
if (connection == NULL)
|
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
|
|
if (!g_initable_init (G_INITABLE (connection), cancellable, error))
|
|
|
|
|
{
|
|
|
|
|
g_object_unref (connection);
|
|
|
|
|
connection = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
out:
|
|
|
|
|
return connection;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
bus_get_async_initable_cb (GObject *source_object,
|
|
|
|
|
GAsyncResult *res,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
2012-08-02 21:46:32 +02:00
|
|
|
|
GTask *task = user_data;
|
|
|
|
|
GError *error = NULL;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
if (!g_async_initable_init_finish (G_ASYNC_INITABLE (source_object),
|
|
|
|
|
res,
|
|
|
|
|
&error))
|
|
|
|
|
{
|
|
|
|
|
g_assert (error != NULL);
|
2012-08-02 21:46:32 +02:00
|
|
|
|
g_task_return_error (task, error);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_object_unref (source_object);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2012-08-02 21:46:32 +02:00
|
|
|
|
g_task_return_pointer (task, source_object, g_object_unref);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
2012-08-02 21:46:32 +02:00
|
|
|
|
g_object_unref (task);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_bus_get:
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @bus_type: a #GBusType
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @cancellable: (nullable): a #GCancellable or %NULL
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @callback: a #GAsyncReadyCallback to call when the request is satisfied
|
|
|
|
|
* @user_data: the data to pass to @callback
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
|
|
|
|
* Asynchronously connects to the message bus specified by @bus_type.
|
|
|
|
|
*
|
|
|
|
|
* When the operation is finished, @callback will be invoked. You can
|
|
|
|
|
* then call g_bus_get_finish() to get the result of the operation.
|
|
|
|
|
*
|
2019-06-25 19:56:29 +02:00
|
|
|
|
* This is an asynchronous failable function. See g_bus_get_sync() for
|
2010-05-06 20:13:59 +02:00
|
|
|
|
* the synchronous version.
|
2010-05-06 22:02:08 +02:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.26
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
g_bus_get (GBusType bus_type,
|
|
|
|
|
GCancellable *cancellable,
|
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
GDBusConnection *connection;
|
2012-08-02 21:46:32 +02:00
|
|
|
|
GTask *task;
|
|
|
|
|
GError *error = NULL;
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
2016-10-31 08:36:18 +01:00
|
|
|
|
_g_dbus_initialize ();
|
|
|
|
|
|
2012-08-02 21:46:32 +02:00
|
|
|
|
task = g_task_new (NULL, cancellable, callback, user_data);
|
2016-06-17 01:39:38 +02:00
|
|
|
|
g_task_set_source_tag (task, g_bus_get);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
|
|
|
|
|
connection = get_uninitialized_connection (bus_type, cancellable, &error);
|
|
|
|
|
if (connection == NULL)
|
|
|
|
|
{
|
|
|
|
|
g_assert (error != NULL);
|
2012-08-02 21:46:32 +02:00
|
|
|
|
g_task_return_error (task, error);
|
|
|
|
|
g_object_unref (task);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
g_async_initable_init_async (G_ASYNC_INITABLE (connection),
|
|
|
|
|
G_PRIORITY_DEFAULT,
|
|
|
|
|
cancellable,
|
|
|
|
|
bus_get_async_initable_cb,
|
2012-08-02 21:46:32 +02:00
|
|
|
|
task);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_bus_get_finish:
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* @res: a #GAsyncResult obtained from the #GAsyncReadyCallback passed
|
|
|
|
|
* to g_bus_get()
|
|
|
|
|
* @error: return location for error or %NULL
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
|
|
|
|
* Finishes an operation started with g_bus_get().
|
|
|
|
|
*
|
|
|
|
|
* The returned object is a singleton, that is, shared with other
|
|
|
|
|
* callers of g_bus_get() and g_bus_get_sync() for @bus_type. In the
|
|
|
|
|
* event that you need a private message bus connection, use
|
2011-06-05 00:44:44 +02:00
|
|
|
|
* g_dbus_address_get_for_bus_sync() and
|
2022-01-28 14:11:34 +01:00
|
|
|
|
* g_dbus_connection_new_for_address() with
|
|
|
|
|
* G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT and
|
|
|
|
|
* G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION flags.
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*
|
|
|
|
|
* Note that the returned #GDBusConnection object will (usually) have
|
|
|
|
|
* the #GDBusConnection:exit-on-close property set to %TRUE.
|
|
|
|
|
*
|
2014-02-01 05:42:40 +01:00
|
|
|
|
* Returns: (transfer full): a #GDBusConnection or %NULL if @error is set.
|
|
|
|
|
* Free with g_object_unref().
|
2010-05-06 22:02:08 +02:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.26
|
2010-05-06 20:13:59 +02:00
|
|
|
|
*/
|
|
|
|
|
GDBusConnection *
|
|
|
|
|
g_bus_get_finish (GAsyncResult *res,
|
|
|
|
|
GError **error)
|
|
|
|
|
{
|
2012-08-02 21:46:32 +02:00
|
|
|
|
g_return_val_if_fail (g_task_is_valid (res, NULL), NULL);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
|
|
|
|
|
|
2012-08-02 21:46:32 +02:00
|
|
|
|
return g_task_propagate_pointer (G_TASK (res), error);
|
2010-05-06 20:13:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------------- */
|