Coverage Report

Created: 2025-06-13 06:55

/src/glib/gio/gdbusconnection.c
Line
Count
Source (jump to first uncovered line)
1
/* GDBus - GLib D-Bus Library
2
 *
3
 * Copyright (C) 2008-2010 Red Hat, Inc.
4
 *
5
 * SPDX-License-Identifier: LGPL-2.1-or-later
6
 *
7
 * This library is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * This library is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General
18
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
19
 *
20
 * Author: David Zeuthen <davidz@redhat.com>
21
 */
22
23
/*
24
 * TODO for GDBus:
25
 *
26
 * - would be nice to expose GDBusAuthMechanism and an extension point
27
 *
28
 * - Need to rewrite GDBusAuth and rework GDBusAuthMechanism. In particular
29
 *   the mechanism VFuncs need to be able to set an error.
30
 *
31
 * - Need to document other mechanisms/sources for determining the D-Bus
32
 *   address of a well-known bus.
33
 *
34
 *   - e.g. on Win32 we need code like from here
35
 *
36
 *     http://cgit.freedesktop.org/~david/gdbus-standalone/tree/gdbus/gdbusaddress.c#n900
37
 *
38
 *     that was never copied over here because it originally was copy-paste
39
 *     from the GPLv2 / AFL 2.1 libdbus sources.
40
 *
41
 *   - on OS X we need to look in launchd for the address
42
 *
43
 *     https://bugs.freedesktop.org/show_bug.cgi?id=14259
44
 *
45
 *   - on X11 we need to look in a X11 property on the X server
46
 *     - (we can also just use dbus-launch(1) from the D-Bus
47
 *        distribution)
48
 *
49
 *   - (ideally) this requires D-Bus spec work because none of
50
 *     this has never really been specced out properly (except
51
 *     the X11 bits)
52
 *
53
 * - Related to the above, we also need to be able to launch a message bus
54
 *   instance.... Since we don't want to write our own bus daemon we should
55
 *   launch dbus-daemon(1) (thus: Win32 and OS X need to bundle it)
56
 *
57
 * - probably want a G_DBUS_NONCE_TCP_TMPDIR environment variable
58
 *   to specify where the nonce is stored. This will allow people to use
59
 *   G_DBUS_NONCE_TCP_TMPDIR=/mnt/secure.company.server/dbus-nonce-dir
60
 *   to easily achieve secure RPC via nonce-tcp.
61
 *
62
 * - need to expose an extension point for resolving D-Bus address and
63
 *   turning them into GIOStream objects. This will allow us to implement
64
 *   e.g. X11 D-Bus transports without dlopen()'ing or linking against
65
 *   libX11 from libgio.
66
 *   - see g_dbus_address_connect() in gdbusaddress.c
67
 *
68
 * - would be cute to use kernel-specific APIs to resolve fds for
69
 *   debug output when using G_DBUS_DEBUG=message, e.g. in addition to
70
 *
71
 *     fd 21: dev=8:1,mode=0100644,ino=1171231,uid=0,gid=0,rdev=0:0,size=234,atime=1273070640,mtime=1267126160,ctime=1267126160
72
 *
73
 *   maybe we can show more information about what fd 21 really is.
74
 *   Ryan suggests looking in /proc/self/fd for clues / symlinks!
75
 *   Initial experiments on Linux 2.6 suggests that the symlink looks
76
 *   like this:
77
 *
78
 *    3 -> /proc/18068/fd
79
 *
80
 *   e.g. not of much use.
81
 *
82
 *  - GDBus High-Level docs
83
 *    - Proxy: properties, signals...
84
 *    - Connection: IOStream based, ::close, connection setup steps
85
 *                  mainloop integration, threading
86
 *    - Differences from libdbus (extend "Migrating from")
87
 *      - the message handling thread
88
 *      - Using GVariant instead of GValue
89
 *    - Explain why the high-level API is a good thing and what
90
 *      kind of pitfalls it avoids
91
 *      - Export objects before claiming names
92
 *    - Talk about auto-starting services (cf. GBusNameWatcherFlags)
93
 */
94
95
#include "config.h"
96
97
#include <stdlib.h>
98
#include <string.h>
99
100
#include "gdbusauth.h"
101
#include "gdbusutils.h"
102
#include "gdbusaddress.h"
103
#include "gdbusmessage.h"
104
#include "gdbusconnection.h"
105
#include "gdbuserror.h"
106
#include "gioenumtypes.h"
107
#include "gdbusintrospection.h"
108
#include "gdbusmethodinvocation.h"
109
#include "gdbusprivate.h"
110
#include "gdbusauthobserver.h"
111
#include "ginitable.h"
112
#include "gasyncinitable.h"
113
#include "giostream.h"
114
#include "gasyncresult.h"
115
#include "gtask.h"
116
#include "gmarshal-internal.h"
117
118
#ifdef G_OS_UNIX
119
#include "gunixconnection.h"
120
#include "gunixfdmessage.h"
121
#endif
122
123
#include "glibintl.h"
124
125
#define G_DBUS_CONNECTION_FLAGS_ALL \
126
  (G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT | \
127
   G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER | \
128
   G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS | \
129
   G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION | \
130
   G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING | \
131
   G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_REQUIRE_SAME_USER)
132
133
/**
134
 * SECTION:gdbusconnection
135
 * @short_description: D-Bus Connections
136
 * @include: gio/gio.h
137
 *
138
 * The #GDBusConnection type is used for D-Bus connections to remote
139
 * peers such as a message buses. It is a low-level API that offers a
140
 * lot of flexibility. For instance, it lets you establish a connection
141
 * over any transport that can by represented as a #GIOStream.
142
 *
143
 * This class is rarely used directly in D-Bus clients. If you are writing
144
 * a D-Bus client, it is often easier to use the g_bus_own_name(),
145
 * g_bus_watch_name() or g_dbus_proxy_new_for_bus() APIs.
146
 *
147
 * As an exception to the usual GLib rule that a particular object must not
148
 * be used by two threads at the same time, #GDBusConnection's methods may be
149
 * called from any thread. This is so that g_bus_get() and g_bus_get_sync()
150
 * can safely return the same #GDBusConnection when called from any thread.
151
 *
152
 * Most of the ways to obtain a #GDBusConnection automatically initialize it
153
 * (i.e. connect to D-Bus): for instance, g_dbus_connection_new() and
154
 * g_bus_get(), and the synchronous versions of those methods, give you an
155
 * initialized connection. Language bindings for GIO should use
156
 * g_initable_new() or g_async_initable_new_async(), which also initialize the
157
 * connection.
158
 *
159
 * If you construct an uninitialized #GDBusConnection, such as via
160
 * g_object_new(), you must initialize it via g_initable_init() or
161
 * g_async_initable_init_async() before using its methods or properties.
162
 * Calling methods or accessing properties on a #GDBusConnection that has not
163
 * completed initialization successfully is considered to be invalid, and leads
164
 * to undefined behaviour. In particular, if initialization fails with a
165
 * #GError, the only valid thing you can do with that #GDBusConnection is to
166
 * free it with g_object_unref().
167
 *
168
 * ## An example D-Bus server # {#gdbus-server}
169
 *
170
 * Here is an example for a D-Bus server:
171
 * [gdbus-example-server.c](https://gitlab.gnome.org/GNOME/glib/-/blob/HEAD/gio/tests/gdbus-example-server.c)
172
 *
173
 * ## An example for exporting a subtree # {#gdbus-subtree-server}
174
 *
175
 * Here is an example for exporting a subtree:
176
 * [gdbus-example-subtree.c](https://gitlab.gnome.org/GNOME/glib/-/blob/HEAD/gio/tests/gdbus-example-subtree.c)
177
 *
178
 * ## An example for file descriptor passing # {#gdbus-unix-fd-client}
179
 *
180
 * Here is an example for passing UNIX file descriptors:
181
 * [gdbus-unix-fd-client.c](https://gitlab.gnome.org/GNOME/glib/-/blob/HEAD/gio/tests/gdbus-example-unix-fd-client.c)
182
 *
183
 * ## An example for exporting a GObject # {#gdbus-export}
184
 *
185
 * Here is an example for exporting a #GObject:
186
 * [gdbus-example-export.c](https://gitlab.gnome.org/GNOME/glib/-/blob/HEAD/gio/tests/gdbus-example-export.c)
187
 */
188
189
/* ---------------------------------------------------------------------------------------------------- */
190
191
typedef struct _GDBusConnectionClass GDBusConnectionClass;
192
193
/**
194
 * GDBusConnectionClass:
195
 * @closed: Signal class handler for the #GDBusConnection::closed signal.
196
 *
197
 * Class structure for #GDBusConnection.
198
 *
199
 * Since: 2.26
200
 */
201
struct _GDBusConnectionClass
202
{
203
  /*< private >*/
204
  GObjectClass parent_class;
205
206
  /*< public >*/
207
  /* Signals */
208
  void (*closed) (GDBusConnection *connection,
209
                  gboolean         remote_peer_vanished,
210
                  GError          *error);
211
};
212
213
G_LOCK_DEFINE_STATIC (message_bus_lock);
214
215
static GWeakRef the_session_bus;
216
static GWeakRef the_system_bus;
217
218
/* Extra pseudo-member of GDBusSendMessageFlags.
219
 * Set by initable_init() to indicate that despite not being initialized yet,
220
 * enough of the only-valid-after-init members are set that we can send a
221
 * message, and we're being called from its thread, so no memory barrier is
222
 * required before accessing them.
223
 */
224
0
#define SEND_MESSAGE_FLAGS_INITIALIZING (1u << 31)
225
226
/* Same as SEND_MESSAGE_FLAGS_INITIALIZING, but in GDBusCallFlags */
227
0
#define CALL_FLAGS_INITIALIZING (1u << 31)
228
229
/* ---------------------------------------------------------------------------------------------------- */
230
231
typedef struct
232
{
233
  GDestroyNotify              callback;
234
  gpointer                    user_data;
235
} CallDestroyNotifyData;
236
237
static gboolean
238
call_destroy_notify_data_in_idle (gpointer user_data)
239
0
{
240
0
  CallDestroyNotifyData *data = user_data;
241
0
  data->callback (data->user_data);
242
0
  return FALSE;
243
0
}
244
245
static void
246
call_destroy_notify_data_free (CallDestroyNotifyData *data)
247
0
{
248
0
  g_free (data);
249
0
}
250
251
/*
252
 * call_destroy_notify: <internal>
253
 * @context: (nullable): A #GMainContext or %NULL.
254
 * @callback: (nullable): A #GDestroyNotify or %NULL.
255
 * @user_data: Data to pass to @callback.
256
 *
257
 * Schedules @callback to run in @context.
258
 */
259
static void
260
call_destroy_notify (GMainContext  *context,
261
                     GDestroyNotify callback,
262
                     gpointer       user_data)
263
0
{
264
0
  GSource *idle_source;
265
0
  CallDestroyNotifyData *data;
266
267
0
  if (callback == NULL)
268
0
    return;
269
270
0
  data = g_new0 (CallDestroyNotifyData, 1);
271
0
  data->callback = callback;
272
0
  data->user_data = user_data;
273
274
0
  idle_source = g_idle_source_new ();
275
0
  g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
276
0
  g_source_set_callback (idle_source,
277
0
                         call_destroy_notify_data_in_idle,
278
0
                         data,
279
0
                         (GDestroyNotify) call_destroy_notify_data_free);
280
0
  g_source_set_static_name (idle_source, "[gio] call_destroy_notify_data_in_idle");
281
0
  g_source_attach (idle_source, context);
282
0
  g_source_unref (idle_source);
283
0
}
284
285
/* ---------------------------------------------------------------------------------------------------- */
286
287
#ifdef G_OS_WIN32
288
#define CONNECTION_ENSURE_LOCK(obj) do { ; } while (FALSE)
289
#else
290
// TODO: for some reason this doesn't work on Windows
291
0
#define CONNECTION_ENSURE_LOCK(obj) do {                                \
292
0
    if (G_UNLIKELY (g_mutex_trylock(&(obj)->lock)))                     \
293
0
      {                                                                 \
294
0
        g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
295
0
                             "CONNECTION_ENSURE_LOCK: GDBusConnection object lock is not locked"); \
296
0
      }                                                                 \
297
0
  } while (FALSE)
298
#endif
299
300
0
#define CONNECTION_LOCK(obj) do {                                       \
301
0
    g_mutex_lock (&(obj)->lock);                                        \
302
0
  } while (FALSE)
303
304
0
#define CONNECTION_UNLOCK(obj) do {                                     \
305
0
    g_mutex_unlock (&(obj)->lock);                                      \
306
0
  } while (FALSE)
307
308
/* Flags in connection->atomic_flags */
309
enum {
310
    FLAG_INITIALIZED = 1 << 0,
311
    FLAG_EXIT_ON_CLOSE = 1 << 1,
312
    FLAG_CLOSED = 1 << 2
313
};
314
315
/**
316
 * GDBusConnection:
317
 *
318
 * The #GDBusConnection structure contains only private data and
319
 * should only be accessed using the provided API.
320
 *
321
 * Since: 2.26
322
 */
323
struct _GDBusConnection
324
{
325
  /*< private >*/
326
  GObject parent_instance;
327
328
  /* ------------------------------------------------------------------------ */
329
  /* -- General object state ------------------------------------------------ */
330
  /* ------------------------------------------------------------------------ */
331
332
  /* General-purpose lock for most fields */
333
  GMutex lock;
334
335
  /* A lock used in the init() method of the GInitable interface - see comments
336
   * in initable_init() for why a separate lock is needed.
337
   *
338
   * If you need both @lock and @init_lock, you must take @init_lock first.
339
   */
340
  GMutex init_lock;
341
342
  /* Set (by loading the contents of /var/lib/dbus/machine-id) the first time
343
   * someone calls org.freedesktop.DBus.Peer.GetMachineId(). Protected by @lock.
344
   */
345
  gchar *machine_id;
346
347
  /* The underlying stream used for communication
348
   * Read-only after initable_init(), so it may be read if you either
349
   * hold @init_lock or check for initialization first.
350
   */
351
  GIOStream *stream;
352
353
  /* The object used for authentication (if any).
354
   * Read-only after initable_init(), so it may be read if you either
355
   * hold @init_lock or check for initialization first.
356
   */
357
  GDBusAuth *auth;
358
359
  /* Last serial used. Protected by @lock. */
360
  guint32 last_serial;
361
362
  /* The object used to send/receive messages.
363
   * Read-only after initable_init(), so it may be read if you either
364
   * hold @init_lock or check for initialization first.
365
   */
366
  GDBusWorker *worker;
367
368
  /* If connected to a message bus, this contains the unique name assigned to
369
   * us by the bus (e.g. ":1.42").
370
   * Read-only after initable_init(), so it may be read if you either
371
   * hold @init_lock or check for initialization first.
372
   */
373
  gchar *bus_unique_name;
374
375
  /* The GUID returned by the other side if we authenticated as a client or
376
   * the GUID to use if authenticating as a server.
377
   * Read-only after initable_init(), so it may be read if you either
378
   * hold @init_lock or check for initialization first.
379
   */
380
  gchar *guid;
381
382
  /* FLAG_INITIALIZED is set exactly when initable_init() has finished running.
383
   * Inspect @initialization_error to see whether it succeeded or failed.
384
   *
385
   * FLAG_EXIT_ON_CLOSE is the exit-on-close property.
386
   *
387
   * FLAG_CLOSED is the closed property. It may be read at any time, but
388
   * may only be written while holding @lock.
389
   */
390
  gint atomic_flags;  /* (atomic) */
391
392
  /* If the connection could not be established during initable_init(),
393
   * this GError will be set.
394
   * Read-only after initable_init(), so it may be read if you either
395
   * hold @init_lock or check for initialization first.
396
   */
397
  GError *initialization_error;
398
399
  /* The result of g_main_context_ref_thread_default() when the object
400
   * was created (the GObject _init() function) - this is used for delivery
401
   * of the :closed GObject signal.
402
   *
403
   * Only set in the GObject init function, so no locks are needed.
404
   */
405
  GMainContext *main_context_at_construction;
406
407
  /* Read-only construct properties, no locks needed */
408
  gchar *address;
409
  GDBusConnectionFlags flags;
410
411
  /* Map used for managing method replies, protected by @lock */
412
  GHashTable *map_method_serial_to_task;  /* guint32 -> owned GTask* */
413
414
  /* Maps used for managing signal subscription, protected by @lock */
415
  GHashTable *map_rule_to_signal_data;                      /* match rule (gchar*)    -> SignalData */
416
  GHashTable *map_id_to_signal_data;                        /* id (guint)             -> SignalData */
417
  GHashTable *map_sender_unique_name_to_signal_data_array;  /* unique sender (gchar*) -> GPtrArray* of SignalData */
418
419
  /* Maps used for managing exported objects and subtrees,
420
   * protected by @lock
421
   */
422
  GHashTable *map_object_path_to_eo;  /* gchar* -> ExportedObject* */
423
  GHashTable *map_id_to_ei;           /* guint  -> ExportedInterface* */
424
  GHashTable *map_object_path_to_es;  /* gchar* -> ExportedSubtree* */
425
  GHashTable *map_id_to_es;           /* guint  -> ExportedSubtree* */
426
427
  /* Map used for storing last used serials for each thread, protected by @lock */
428
  GHashTable *map_thread_to_last_serial;
429
430
  /* Structure used for message filters, protected by @lock */
431
  GPtrArray *filters;
432
433
  /* Capabilities negotiated during authentication
434
   * Read-only after initable_init(), so it may be read without holding a
435
   * lock, if you check for initialization first.
436
   */
437
  GDBusCapabilityFlags capabilities;
438
439
  /* Protected by @init_lock */
440
  GDBusAuthObserver *authentication_observer;
441
442
  /* Read-only after initable_init(), so it may be read if you either
443
   * hold @init_lock or check for initialization first.
444
   */
445
  GCredentials *credentials;
446
447
  /* set to TRUE when finalizing */
448
  gboolean finalizing;
449
};
450
451
typedef struct ExportedObject ExportedObject;
452
static void exported_object_free (ExportedObject *eo);
453
454
typedef struct ExportedSubtree ExportedSubtree;
455
static ExportedSubtree *exported_subtree_ref (ExportedSubtree *es);
456
static void exported_subtree_unref (ExportedSubtree *es);
457
458
enum
459
{
460
  CLOSED_SIGNAL,
461
  LAST_SIGNAL,
462
};
463
464
enum
465
{
466
  PROP_0,
467
  PROP_STREAM,
468
  PROP_ADDRESS,
469
  PROP_FLAGS,
470
  PROP_GUID,
471
  PROP_UNIQUE_NAME,
472
  PROP_CLOSED,
473
  PROP_EXIT_ON_CLOSE,
474
  PROP_CAPABILITY_FLAGS,
475
  PROP_AUTHENTICATION_OBSERVER,
476
};
477
478
static void distribute_signals (GDBusConnection  *connection,
479
                                GDBusMessage     *message);
480
481
static void distribute_method_call (GDBusConnection  *connection,
482
                                    GDBusMessage     *message);
483
484
static gboolean handle_generic_unlocked (GDBusConnection *connection,
485
                                         GDBusMessage    *message);
486
487
488
static void purge_all_signal_subscriptions (GDBusConnection *connection);
489
static void purge_all_filters (GDBusConnection *connection);
490
491
static void schedule_method_call (GDBusConnection            *connection,
492
                                  GDBusMessage               *message,
493
                                  guint                       registration_id,
494
                                  guint                       subtree_registration_id,
495
                                  const GDBusInterfaceInfo   *interface_info,
496
                                  const GDBusMethodInfo      *method_info,
497
                                  const GDBusPropertyInfo    *property_info,
498
                                  GVariant                   *parameters,
499
                                  const GDBusInterfaceVTable *vtable,
500
                                  GMainContext               *main_context,
501
                                  gpointer                    user_data);
502
503
#define _G_ENSURE_LOCK(name) do {                                       \
504
    if (G_UNLIKELY (G_TRYLOCK(name)))                                   \
505
      {                                                                 \
506
        g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
507
                             "_G_ENSURE_LOCK: Lock '" #name "' is not locked"); \
508
      }                                                                 \
509
  } while (FALSE)                                                       \
510
511
static guint signals[LAST_SIGNAL] = { 0 };
512
513
static void initable_iface_init       (GInitableIface      *initable_iface);
514
static void async_initable_iface_init (GAsyncInitableIface *async_initable_iface);
515
516
G_DEFINE_TYPE_WITH_CODE (GDBusConnection, g_dbus_connection, G_TYPE_OBJECT,
517
                         G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init)
518
                         G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, async_initable_iface_init)
519
                         );
520
521
/*
522
 * Check that all members of @connection that can only be accessed after
523
 * the connection is initialized can safely be accessed. If not,
524
 * log a critical warning. This function is a memory barrier.
525
 *
526
 * Returns: %TRUE if initialized
527
 */
528
static gboolean
529
check_initialized (GDBusConnection *connection)
530
0
{
531
  /* The access to @atomic_flags isn't conditional, so that this function
532
   * provides a memory barrier for thread-safety even if checks are disabled.
533
   * (If you don't want this stricter guarantee, you can call
534
   * g_return_if_fail (check_initialized (c)).)
535
   *
536
   * This isn't strictly necessary now that we've decided use of an
537
   * uninitialized GDBusConnection is undefined behaviour, but it seems
538
   * better to be as deterministic as is feasible.
539
   *
540
   * (Anything that could suffer a crash from seeing undefined values
541
   * must have a race condition - thread A initializes the connection while
542
   * thread B calls a method without initialization, hoping that thread A will
543
   * win the race - so its behaviour is undefined anyway.)
544
   */
545
0
  gint flags = g_atomic_int_get (&connection->atomic_flags);
546
547
0
  g_return_val_if_fail (flags & FLAG_INITIALIZED, FALSE);
548
549
  /* We can safely access this, due to the memory barrier above */
550
0
  g_return_val_if_fail (connection->initialization_error == NULL, FALSE);
551
552
0
  return TRUE;
553
0
}
554
555
typedef enum {
556
    MAY_BE_UNINITIALIZED = (1<<1)
557
} CheckUnclosedFlags;
558
559
/*
560
 * Check the same thing as check_initialized(), and also that the
561
 * connection is not closed. If the connection is uninitialized,
562
 * raise a critical warning (it's programmer error); if it's closed,
563
 * raise a recoverable GError (it's a runtime error).
564
 *
565
 * This function is a memory barrier.
566
 *
567
 * Returns: %TRUE if initialized and not closed
568
 */
569
static gboolean
570
check_unclosed (GDBusConnection     *connection,
571
                CheckUnclosedFlags   check,
572
                GError             **error)
573
0
{
574
  /* check_initialized() is effectively inlined, so we don't waste time
575
   * doing two memory barriers
576
   */
577
0
  gint flags = g_atomic_int_get (&connection->atomic_flags);
578
579
0
  if (!(check & MAY_BE_UNINITIALIZED))
580
0
    {
581
0
      g_return_val_if_fail (flags & FLAG_INITIALIZED, FALSE);
582
0
      g_return_val_if_fail (connection->initialization_error == NULL, FALSE);
583
0
    }
584
585
0
  if (flags & FLAG_CLOSED)
586
0
    {
587
0
      g_set_error_literal (error,
588
0
                           G_IO_ERROR,
589
0
                           G_IO_ERROR_CLOSED,
590
0
                           _("The connection is closed"));
591
0
      return FALSE;
592
0
    }
593
594
0
  return TRUE;
595
0
}
596
597
static GHashTable *alive_connections = NULL;
598
599
static void
600
g_dbus_connection_dispose (GObject *object)
601
0
{
602
0
  GDBusConnection *connection = G_DBUS_CONNECTION (object);
603
604
0
  G_LOCK (message_bus_lock);
605
0
  CONNECTION_LOCK (connection);
606
0
  if (connection->worker != NULL)
607
0
    {
608
0
      _g_dbus_worker_stop (connection->worker);
609
0
      connection->worker = NULL;
610
0
      if (alive_connections != NULL)
611
0
        g_warn_if_fail (g_hash_table_remove (alive_connections, connection));
612
0
    }
613
0
  else
614
0
    {
615
0
      if (alive_connections != NULL)
616
0
        g_warn_if_fail (!g_hash_table_contains (alive_connections, connection));
617
0
    }
618
0
  CONNECTION_UNLOCK (connection);
619
0
  G_UNLOCK (message_bus_lock);
620
621
0
  if (G_OBJECT_CLASS (g_dbus_connection_parent_class)->dispose != NULL)
622
0
    G_OBJECT_CLASS (g_dbus_connection_parent_class)->dispose (object);
623
0
}
624
625
static void
626
g_dbus_connection_finalize (GObject *object)
627
0
{
628
0
  GDBusConnection *connection = G_DBUS_CONNECTION (object);
629
630
0
  connection->finalizing = TRUE;
631
632
0
  purge_all_signal_subscriptions (connection);
633
634
0
  purge_all_filters (connection);
635
0
  g_ptr_array_unref (connection->filters);
636
637
0
  if (connection->authentication_observer != NULL)
638
0
    g_object_unref (connection->authentication_observer);
639
640
0
  if (connection->auth != NULL)
641
0
    g_object_unref (connection->auth);
642
643
0
  if (connection->credentials)
644
0
    g_object_unref (connection->credentials);
645
646
0
  if (connection->stream != NULL)
647
0
    {
648
0
      g_object_unref (connection->stream);
649
0
      connection->stream = NULL;
650
0
    }
651
652
0
  g_free (connection->address);
653
654
0
  g_free (connection->guid);
655
0
  g_free (connection->bus_unique_name);
656
657
0
  if (connection->initialization_error != NULL)
658
0
    g_error_free (connection->initialization_error);
659
660
0
  g_hash_table_unref (connection->map_method_serial_to_task);
661
662
0
  g_hash_table_unref (connection->map_rule_to_signal_data);
663
0
  g_hash_table_unref (connection->map_id_to_signal_data);
664
0
  g_hash_table_unref (connection->map_sender_unique_name_to_signal_data_array);
665
666
0
  g_hash_table_unref (connection->map_id_to_ei);
667
0
  g_hash_table_unref (connection->map_object_path_to_eo);
668
0
  g_hash_table_unref (connection->map_id_to_es);
669
0
  g_hash_table_unref (connection->map_object_path_to_es);
670
671
0
  g_hash_table_unref (connection->map_thread_to_last_serial);
672
673
0
  g_main_context_unref (connection->main_context_at_construction);
674
675
0
  g_free (connection->machine_id);
676
677
0
  g_mutex_clear (&connection->init_lock);
678
0
  g_mutex_clear (&connection->lock);
679
680
0
  G_OBJECT_CLASS (g_dbus_connection_parent_class)->finalize (object);
681
0
}
682
683
/* called in any user thread, with the connection's lock not held */
684
static void
685
g_dbus_connection_get_property (GObject    *object,
686
                                guint       prop_id,
687
                                GValue     *value,
688
                                GParamSpec *pspec)
689
0
{
690
0
  GDBusConnection *connection = G_DBUS_CONNECTION (object);
691
692
0
  switch (prop_id)
693
0
    {
694
0
    case PROP_STREAM:
695
0
      g_value_set_object (value, g_dbus_connection_get_stream (connection));
696
0
      break;
697
698
0
    case PROP_GUID:
699
0
      g_value_set_string (value, g_dbus_connection_get_guid (connection));
700
0
      break;
701
702
0
    case PROP_UNIQUE_NAME:
703
0
      g_value_set_string (value, g_dbus_connection_get_unique_name (connection));
704
0
      break;
705
706
0
    case PROP_CLOSED:
707
0
      g_value_set_boolean (value, g_dbus_connection_is_closed (connection));
708
0
      break;
709
710
0
    case PROP_EXIT_ON_CLOSE:
711
0
      g_value_set_boolean (value, g_dbus_connection_get_exit_on_close (connection));
712
0
      break;
713
714
0
    case PROP_CAPABILITY_FLAGS:
715
0
      g_value_set_flags (value, g_dbus_connection_get_capabilities (connection));
716
0
      break;
717
718
0
    case PROP_FLAGS:
719
0
      g_value_set_flags (value, g_dbus_connection_get_flags (connection));
720
0
      break;
721
722
0
    default:
723
0
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
724
0
      break;
725
0
    }
726
0
}
727
728
/* called in any user thread, with the connection's lock not held */
729
static void
730
g_dbus_connection_set_property (GObject      *object,
731
                                guint         prop_id,
732
                                const GValue *value,
733
                                GParamSpec   *pspec)
734
0
{
735
0
  GDBusConnection *connection = G_DBUS_CONNECTION (object);
736
737
0
  switch (prop_id)
738
0
    {
739
0
    case PROP_STREAM:
740
0
      connection->stream = g_value_dup_object (value);
741
0
      break;
742
743
0
    case PROP_GUID:
744
0
      connection->guid = g_value_dup_string (value);
745
0
      break;
746
747
0
    case PROP_ADDRESS:
748
0
      connection->address = g_value_dup_string (value);
749
0
      break;
750
751
0
    case PROP_FLAGS:
752
0
      connection->flags = g_value_get_flags (value);
753
0
      break;
754
755
0
    case PROP_EXIT_ON_CLOSE:
756
0
      g_dbus_connection_set_exit_on_close (connection, g_value_get_boolean (value));
757
0
      break;
758
759
0
    case PROP_AUTHENTICATION_OBSERVER:
760
0
      connection->authentication_observer = g_value_dup_object (value);
761
0
      break;
762
763
0
    default:
764
0
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
765
0
      break;
766
0
    }
767
0
}
768
769
/* Base-class implementation of GDBusConnection::closed.
770
 *
771
 * Called in a user thread, by the main context that was thread-default when
772
 * the object was constructed.
773
 */
774
static void
775
g_dbus_connection_real_closed (GDBusConnection *connection,
776
                               gboolean         remote_peer_vanished,
777
                               GError          *error)
778
0
{
779
0
  gint flags = g_atomic_int_get (&connection->atomic_flags);
780
781
  /* Because atomic int access is a memory barrier, we can safely read
782
   * initialization_error without a lock, as long as we do it afterwards.
783
   */
784
0
  if (remote_peer_vanished &&
785
0
      (flags & FLAG_EXIT_ON_CLOSE) != 0 &&
786
0
      (flags & FLAG_INITIALIZED) != 0 &&
787
0
      connection->initialization_error == NULL)
788
0
    {
789
0
      raise (SIGTERM);
790
0
    }
791
0
}
792
793
static void
794
g_dbus_connection_class_init (GDBusConnectionClass *klass)
795
0
{
796
0
  GObjectClass *gobject_class;
797
798
0
  gobject_class = G_OBJECT_CLASS (klass);
799
800
0
  gobject_class->finalize     = g_dbus_connection_finalize;
801
0
  gobject_class->dispose      = g_dbus_connection_dispose;
802
0
  gobject_class->set_property = g_dbus_connection_set_property;
803
0
  gobject_class->get_property = g_dbus_connection_get_property;
804
805
0
  klass->closed = g_dbus_connection_real_closed;
806
807
  /**
808
   * GDBusConnection:stream:
809
   *
810
   * The underlying #GIOStream used for I/O.
811
   *
812
   * If this is passed on construction and is a #GSocketConnection,
813
   * then the corresponding #GSocket will be put into non-blocking mode.
814
   *
815
   * While the #GDBusConnection is active, it will interact with this
816
   * stream from a worker thread, so it is not safe to interact with
817
   * the stream directly.
818
   *
819
   * Since: 2.26
820
   */
821
0
  g_object_class_install_property (gobject_class,
822
0
                                   PROP_STREAM,
823
0
                                   g_param_spec_object ("stream",
824
0
                                                        P_("IO Stream"),
825
0
                                                        P_("The underlying streams used for I/O"),
826
0
                                                        G_TYPE_IO_STREAM,
827
0
                                                        G_PARAM_READABLE |
828
0
                                                        G_PARAM_WRITABLE |
829
0
                                                        G_PARAM_CONSTRUCT_ONLY |
830
0
                                                        G_PARAM_STATIC_NAME |
831
0
                                                        G_PARAM_STATIC_BLURB |
832
0
                                                        G_PARAM_STATIC_NICK));
833
834
  /**
835
   * GDBusConnection:address:
836
   *
837
   * A D-Bus address specifying potential endpoints that can be used
838
   * when establishing the connection.
839
   *
840
   * Since: 2.26
841
   */
842
0
  g_object_class_install_property (gobject_class,
843
0
                                   PROP_ADDRESS,
844
0
                                   g_param_spec_string ("address",
845
0
                                                        P_("Address"),
846
0
                                                        P_("D-Bus address specifying potential socket endpoints"),
847
0
                                                        NULL,
848
0
                                                        G_PARAM_WRITABLE |
849
0
                                                        G_PARAM_CONSTRUCT_ONLY |
850
0
                                                        G_PARAM_STATIC_NAME |
851
0
                                                        G_PARAM_STATIC_BLURB |
852
0
                                                        G_PARAM_STATIC_NICK));
853
854
  /**
855
   * GDBusConnection:flags:
856
   *
857
   * Flags from the #GDBusConnectionFlags enumeration.
858
   *
859
   * Since: 2.26
860
   */
861
0
  g_object_class_install_property (gobject_class,
862
0
                                   PROP_FLAGS,
863
0
                                   g_param_spec_flags ("flags",
864
0
                                                       P_("Flags"),
865
0
                                                       P_("Flags"),
866
0
                                                       G_TYPE_DBUS_CONNECTION_FLAGS,
867
0
                                                       G_DBUS_CONNECTION_FLAGS_NONE,
868
0
                                                       G_PARAM_READABLE |
869
0
                                                       G_PARAM_WRITABLE |
870
0
                                                       G_PARAM_CONSTRUCT_ONLY |
871
0
                                                       G_PARAM_STATIC_NAME |
872
0
                                                       G_PARAM_STATIC_BLURB |
873
0
                                                       G_PARAM_STATIC_NICK));
874
875
  /**
876
   * GDBusConnection:guid:
877
   *
878
   * The GUID of the peer performing the role of server when
879
   * authenticating.
880
   *
881
   * If you are constructing a #GDBusConnection and pass
882
   * %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER in the
883
   * #GDBusConnection:flags property then you **must** also set this
884
   * property to a valid guid.
885
   *
886
   * If you are constructing a #GDBusConnection and pass
887
   * %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT in the
888
   * #GDBusConnection:flags property you will be able to read the GUID
889
   * of the other peer here after the connection has been successfully
890
   * initialized.
891
   *
892
   * Note that the
893
   * [D-Bus specification](https://dbus.freedesktop.org/doc/dbus-specification.html#addresses)
894
   * uses the term ā€˜UUID’ to refer to this, whereas GLib consistently uses the
895
   * term ā€˜GUID’ for historical reasons.
896
   *
897
   * Despite its name, the format of #GDBusConnection:guid does not follow
898
   * [RFC 4122](https://datatracker.ietf.org/doc/html/rfc4122) or the Microsoft
899
   * GUID format.
900
   *
901
   * Since: 2.26
902
   */
903
0
  g_object_class_install_property (gobject_class,
904
0
                                   PROP_GUID,
905
0
                                   g_param_spec_string ("guid",
906
0
                                                        P_("GUID"),
907
0
                                                        P_("GUID of the server peer"),
908
0
                                                        NULL,
909
0
                                                        G_PARAM_READABLE |
910
0
                                                        G_PARAM_WRITABLE |
911
0
                                                        G_PARAM_CONSTRUCT_ONLY |
912
0
                                                        G_PARAM_STATIC_NAME |
913
0
                                                        G_PARAM_STATIC_BLURB |
914
0
                                                        G_PARAM_STATIC_NICK));
915
916
  /**
917
   * GDBusConnection:unique-name:
918
   *
919
   * The unique name as assigned by the message bus or %NULL if the
920
   * connection is not open or not a message bus connection.
921
   *
922
   * Since: 2.26
923
   */
924
0
  g_object_class_install_property (gobject_class,
925
0
                                   PROP_UNIQUE_NAME,
926
0
                                   g_param_spec_string ("unique-name",
927
0
                                                        P_("unique-name"),
928
0
                                                        P_("Unique name of bus connection"),
929
0
                                                        NULL,
930
0
                                                        G_PARAM_READABLE |
931
0
                                                        G_PARAM_STATIC_NAME |
932
0
                                                        G_PARAM_STATIC_BLURB |
933
0
                                                        G_PARAM_STATIC_NICK));
934
935
  /**
936
   * GDBusConnection:closed:
937
   *
938
   * A boolean specifying whether the connection has been closed.
939
   *
940
   * Since: 2.26
941
   */
942
0
  g_object_class_install_property (gobject_class,
943
0
                                   PROP_CLOSED,
944
0
                                   g_param_spec_boolean ("closed",
945
0
                                                         P_("Closed"),
946
0
                                                         P_("Whether the connection is closed"),
947
0
                                                         FALSE,
948
0
                                                         G_PARAM_READABLE |
949
0
                                                         G_PARAM_STATIC_NAME |
950
0
                                                         G_PARAM_STATIC_BLURB |
951
0
                                                         G_PARAM_STATIC_NICK));
952
953
  /**
954
   * GDBusConnection:exit-on-close:
955
   *
956
   * A boolean specifying whether the process will be terminated (by
957
   * calling `raise(SIGTERM)`) if the connection is closed by the
958
   * remote peer.
959
   *
960
   * Note that #GDBusConnection objects returned by g_bus_get_finish()
961
   * and g_bus_get_sync() will (usually) have this property set to %TRUE.
962
   *
963
   * Since: 2.26
964
   */
965
0
  g_object_class_install_property (gobject_class,
966
0
                                   PROP_EXIT_ON_CLOSE,
967
0
                                   g_param_spec_boolean ("exit-on-close",
968
0
                                                         P_("Exit on close"),
969
0
                                                         P_("Whether the process is terminated when the connection is closed"),
970
0
                                                         FALSE,
971
0
                                                         G_PARAM_READABLE |
972
0
                                                         G_PARAM_WRITABLE |
973
0
                                                         G_PARAM_STATIC_NAME |
974
0
                                                         G_PARAM_STATIC_BLURB |
975
0
                                                         G_PARAM_STATIC_NICK));
976
977
  /**
978
   * GDBusConnection:capabilities:
979
   *
980
   * Flags from the #GDBusCapabilityFlags enumeration
981
   * representing connection features negotiated with the other peer.
982
   *
983
   * Since: 2.26
984
   */
985
0
  g_object_class_install_property (gobject_class,
986
0
                                   PROP_CAPABILITY_FLAGS,
987
0
                                   g_param_spec_flags ("capabilities",
988
0
                                                       P_("Capabilities"),
989
0
                                                       P_("Capabilities"),
990
0
                                                       G_TYPE_DBUS_CAPABILITY_FLAGS,
991
0
                                                       G_DBUS_CAPABILITY_FLAGS_NONE,
992
0
                                                       G_PARAM_READABLE |
993
0
                                                       G_PARAM_STATIC_NAME |
994
0
                                                       G_PARAM_STATIC_BLURB |
995
0
                                                       G_PARAM_STATIC_NICK));
996
997
  /**
998
   * GDBusConnection:authentication-observer:
999
   *
1000
   * A #GDBusAuthObserver object to assist in the authentication process or %NULL.
1001
   *
1002
   * Since: 2.26
1003
   */
1004
0
  g_object_class_install_property (gobject_class,
1005
0
                                   PROP_AUTHENTICATION_OBSERVER,
1006
0
                                   g_param_spec_object ("authentication-observer",
1007
0
                                                        P_("Authentication Observer"),
1008
0
                                                        P_("Object used to assist in the authentication process"),
1009
0
                                                        G_TYPE_DBUS_AUTH_OBSERVER,
1010
0
                                                        G_PARAM_WRITABLE |
1011
0
                                                        G_PARAM_CONSTRUCT_ONLY |
1012
0
                                                        G_PARAM_STATIC_NAME |
1013
0
                                                        G_PARAM_STATIC_BLURB |
1014
0
                                                        G_PARAM_STATIC_NICK));
1015
1016
  /**
1017
   * GDBusConnection::closed:
1018
   * @connection: the #GDBusConnection emitting the signal
1019
   * @remote_peer_vanished: %TRUE if @connection is closed because the
1020
   *     remote peer closed its end of the connection
1021
   * @error: (nullable): a #GError with more details about the event or %NULL
1022
   *
1023
   * Emitted when the connection is closed.
1024
   *
1025
   * The cause of this event can be
1026
   *
1027
   * - If g_dbus_connection_close() is called. In this case
1028
   *   @remote_peer_vanished is set to %FALSE and @error is %NULL.
1029
   *
1030
   * - If the remote peer closes the connection. In this case
1031
   *   @remote_peer_vanished is set to %TRUE and @error is set.
1032
   *
1033
   * - If the remote peer sends invalid or malformed data. In this
1034
   *   case @remote_peer_vanished is set to %FALSE and @error is set.
1035
   *
1036
   * Upon receiving this signal, you should give up your reference to
1037
   * @connection. You are guaranteed that this signal is emitted only
1038
   * once.
1039
   *
1040
   * Since: 2.26
1041
   */
1042
0
  signals[CLOSED_SIGNAL] = g_signal_new (I_("closed"),
1043
0
                                         G_TYPE_DBUS_CONNECTION,
1044
0
                                         G_SIGNAL_RUN_LAST,
1045
0
                                         G_STRUCT_OFFSET (GDBusConnectionClass, closed),
1046
0
                                         NULL,
1047
0
                                         NULL,
1048
0
                                         _g_cclosure_marshal_VOID__BOOLEAN_BOXED,
1049
0
                                         G_TYPE_NONE,
1050
0
                                         2,
1051
0
                                         G_TYPE_BOOLEAN,
1052
0
                                         G_TYPE_ERROR);
1053
0
  g_signal_set_va_marshaller (signals[CLOSED_SIGNAL],
1054
0
                              G_TYPE_FROM_CLASS (klass),
1055
0
                              _g_cclosure_marshal_VOID__BOOLEAN_BOXEDv);
1056
0
}
1057
1058
static void
1059
g_dbus_connection_init (GDBusConnection *connection)
1060
0
{
1061
0
  g_mutex_init (&connection->lock);
1062
0
  g_mutex_init (&connection->init_lock);
1063
1064
0
  connection->map_method_serial_to_task = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_object_unref);
1065
1066
0
  connection->map_rule_to_signal_data = g_hash_table_new (g_str_hash,
1067
0
                                                          g_str_equal);
1068
0
  connection->map_id_to_signal_data = g_hash_table_new (g_direct_hash,
1069
0
                                                        g_direct_equal);
1070
0
  connection->map_sender_unique_name_to_signal_data_array = g_hash_table_new_full (g_str_hash,
1071
0
                                                                                   g_str_equal,
1072
0
                                                                                   g_free,
1073
0
                                                                                   (GDestroyNotify) g_ptr_array_unref);
1074
1075
0
  connection->map_object_path_to_eo = g_hash_table_new_full (g_str_hash,
1076
0
                                                             g_str_equal,
1077
0
                                                             NULL,
1078
0
                                                             (GDestroyNotify) exported_object_free);
1079
1080
0
  connection->map_id_to_ei = g_hash_table_new (g_direct_hash,
1081
0
                                               g_direct_equal);
1082
1083
0
  connection->map_object_path_to_es = g_hash_table_new_full (g_str_hash,
1084
0
                                                             g_str_equal,
1085
0
                                                             NULL,
1086
0
                                                             (GDestroyNotify) exported_subtree_unref);
1087
1088
0
  connection->map_id_to_es = g_hash_table_new (g_direct_hash,
1089
0
                                               g_direct_equal);
1090
1091
0
  connection->map_thread_to_last_serial = g_hash_table_new (g_direct_hash,
1092
0
                                                            g_direct_equal);
1093
1094
0
  connection->main_context_at_construction = g_main_context_ref_thread_default ();
1095
1096
0
  connection->filters = g_ptr_array_new ();
1097
0
}
1098
1099
/**
1100
 * g_dbus_connection_get_stream:
1101
 * @connection: a #GDBusConnection
1102
 *
1103
 * Gets the underlying stream used for IO.
1104
 *
1105
 * While the #GDBusConnection is active, it will interact with this
1106
 * stream from a worker thread, so it is not safe to interact with
1107
 * the stream directly.
1108
 *
1109
 * Returns: (transfer none) (not nullable): the stream used for IO
1110
 *
1111
 * Since: 2.26
1112
 */
1113
GIOStream *
1114
g_dbus_connection_get_stream (GDBusConnection *connection)
1115
0
{
1116
0
  g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
1117
1118
  /* do not use g_return_val_if_fail(), we want the memory barrier */
1119
0
  if (!check_initialized (connection))
1120
0
    return NULL;
1121
1122
0
  return connection->stream;
1123
0
}
1124
1125
/**
1126
 * g_dbus_connection_start_message_processing:
1127
 * @connection: a #GDBusConnection
1128
 *
1129
 * If @connection was created with
1130
 * %G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING, this method
1131
 * starts processing messages. Does nothing on if @connection wasn't
1132
 * created with this flag or if the method has already been called.
1133
 *
1134
 * Since: 2.26
1135
 */
1136
void
1137
g_dbus_connection_start_message_processing (GDBusConnection *connection)
1138
0
{
1139
0
  g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
1140
1141
  /* do not use g_return_val_if_fail(), we want the memory barrier */
1142
0
  if (!check_initialized (connection))
1143
0
    return;
1144
1145
0
  g_assert (connection->worker != NULL);
1146
0
  _g_dbus_worker_unfreeze (connection->worker);
1147
0
}
1148
1149
/**
1150
 * g_dbus_connection_is_closed:
1151
 * @connection: a #GDBusConnection
1152
 *
1153
 * Gets whether @connection is closed.
1154
 *
1155
 * Returns: %TRUE if the connection is closed, %FALSE otherwise
1156
 *
1157
 * Since: 2.26
1158
 */
1159
gboolean
1160
g_dbus_connection_is_closed (GDBusConnection *connection)
1161
0
{
1162
0
  gint flags;
1163
1164
0
  g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
1165
1166
0
  flags = g_atomic_int_get (&connection->atomic_flags);
1167
1168
0
  return (flags & FLAG_CLOSED) ? TRUE : FALSE;
1169
0
}
1170
1171
/**
1172
 * g_dbus_connection_get_capabilities:
1173
 * @connection: a #GDBusConnection
1174
 *
1175
 * Gets the capabilities negotiated with the remote peer
1176
 *
1177
 * Returns: zero or more flags from the #GDBusCapabilityFlags enumeration
1178
 *
1179
 * Since: 2.26
1180
 */
1181
GDBusCapabilityFlags
1182
g_dbus_connection_get_capabilities (GDBusConnection *connection)
1183
0
{
1184
0
  g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), G_DBUS_CAPABILITY_FLAGS_NONE);
1185
1186
  /* do not use g_return_val_if_fail(), we want the memory barrier */
1187
0
  if (!check_initialized (connection))
1188
0
    return G_DBUS_CAPABILITY_FLAGS_NONE;
1189
1190
0
  return connection->capabilities;
1191
0
}
1192
1193
/**
1194
 * g_dbus_connection_get_flags:
1195
 * @connection: a #GDBusConnection
1196
 *
1197
 * Gets the flags used to construct this connection
1198
 *
1199
 * Returns: zero or more flags from the #GDBusConnectionFlags enumeration
1200
 *
1201
 * Since: 2.60
1202
 */
1203
GDBusConnectionFlags
1204
g_dbus_connection_get_flags (GDBusConnection *connection)
1205
0
{
1206
0
  g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), G_DBUS_CONNECTION_FLAGS_NONE);
1207
1208
  /* do not use g_return_val_if_fail(), we want the memory barrier */
1209
0
  if (!check_initialized (connection))
1210
0
    return G_DBUS_CONNECTION_FLAGS_NONE;
1211
1212
0
  return connection->flags;
1213
0
}
1214
1215
/* ---------------------------------------------------------------------------------------------------- */
1216
1217
/* Called in a temporary thread without holding locks. */
1218
static void
1219
flush_in_thread_func (GTask         *task,
1220
                      gpointer       source_object,
1221
                      gpointer       task_data,
1222
                      GCancellable  *cancellable)
1223
0
{
1224
0
  GError *error = NULL;
1225
1226
0
  if (g_dbus_connection_flush_sync (source_object,
1227
0
                                    cancellable,
1228
0
                                    &error))
1229
0
    g_task_return_boolean (task, TRUE);
1230
0
  else
1231
0
    g_task_return_error (task, error);
1232
0
}
1233
1234
/**
1235
 * g_dbus_connection_flush:
1236
 * @connection: a #GDBusConnection
1237
 * @cancellable: (nullable): a #GCancellable or %NULL
1238
 * @callback: (nullable): a #GAsyncReadyCallback to call when the
1239
 *     request is satisfied or %NULL if you don't care about the result
1240
 * @user_data: The data to pass to @callback
1241
 *
1242
 * Asynchronously flushes @connection, that is, writes all queued
1243
 * outgoing message to the transport and then flushes the transport
1244
 * (using g_output_stream_flush_async()). This is useful in programs
1245
 * that wants to emit a D-Bus signal and then exit immediately. Without
1246
 * flushing the connection, there is no guaranteed that the message has
1247
 * been sent to the networking buffers in the OS kernel.
1248
 *
1249
 * This is an asynchronous method. When the operation is finished,
1250
 * @callback will be invoked in the
1251
 * [thread-default main context][g-main-context-push-thread-default]
1252
 * of the thread you are calling this method from. You can
1253
 * then call g_dbus_connection_flush_finish() to get the result of the
1254
 * operation. See g_dbus_connection_flush_sync() for the synchronous
1255
 * version.
1256
 *
1257
 * Since: 2.26
1258
 */
1259
void
1260
g_dbus_connection_flush (GDBusConnection     *connection,
1261
                         GCancellable        *cancellable,
1262
                         GAsyncReadyCallback  callback,
1263
                         gpointer             user_data)
1264
0
{
1265
0
  GTask *task;
1266
1267
0
  g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
1268
1269
0
  task = g_task_new (connection, cancellable, callback, user_data);
1270
0
  g_task_set_source_tag (task, g_dbus_connection_flush);
1271
0
  g_task_run_in_thread (task, flush_in_thread_func);
1272
0
  g_object_unref (task);
1273
0
}
1274
1275
/**
1276
 * g_dbus_connection_flush_finish:
1277
 * @connection: a #GDBusConnection
1278
 * @res: a #GAsyncResult obtained from the #GAsyncReadyCallback passed
1279
 *     to g_dbus_connection_flush()
1280
 * @error: return location for error or %NULL
1281
 *
1282
 * Finishes an operation started with g_dbus_connection_flush().
1283
 *
1284
 * Returns: %TRUE if the operation succeeded, %FALSE if @error is set
1285
 *
1286
 * Since: 2.26
1287
 */
1288
gboolean
1289
g_dbus_connection_flush_finish (GDBusConnection  *connection,
1290
                                GAsyncResult     *res,
1291
                                GError          **error)
1292
0
{
1293
0
  g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
1294
0
  g_return_val_if_fail (g_task_is_valid (res, connection), FALSE);
1295
0
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1296
1297
0
  return g_task_propagate_boolean (G_TASK (res), error);
1298
0
}
1299
1300
/**
1301
 * g_dbus_connection_flush_sync:
1302
 * @connection: a #GDBusConnection
1303
 * @cancellable: (nullable): a #GCancellable or %NULL
1304
 * @error: return location for error or %NULL
1305
 *
1306
 * Synchronously flushes @connection. The calling thread is blocked
1307
 * until this is done. See g_dbus_connection_flush() for the
1308
 * asynchronous version of this method and more details about what it
1309
 * does.
1310
 *
1311
 * Returns: %TRUE if the operation succeeded, %FALSE if @error is set
1312
 *
1313
 * Since: 2.26
1314
 */
1315
gboolean
1316
g_dbus_connection_flush_sync (GDBusConnection  *connection,
1317
                              GCancellable     *cancellable,
1318
                              GError          **error)
1319
0
{
1320
0
  gboolean ret;
1321
1322
0
  g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
1323
0
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1324
1325
0
  ret = FALSE;
1326
1327
  /* This is only a best-effort attempt to see whether the connection is
1328
   * closed, so it doesn't need the lock. If the connection closes just
1329
   * after this check, but before scheduling the flush operation, the
1330
   * result will be more or less the same as if the connection closed while
1331
   * the flush operation was pending - it'll fail with either CLOSED or
1332
   * CANCELLED.
1333
   */
1334
0
  if (!check_unclosed (connection, 0, error))
1335
0
    goto out;
1336
1337
0
  g_assert (connection->worker != NULL);
1338
1339
0
  ret = _g_dbus_worker_flush_sync (connection->worker,
1340
0
                                   cancellable,
1341
0
                                   error);
1342
1343
0
 out:
1344
0
  return ret;
1345
0
}
1346
1347
/* ---------------------------------------------------------------------------------------------------- */
1348
1349
typedef struct
1350
{
1351
  GDBusConnection *connection;
1352
  GError *error;
1353
  gboolean remote_peer_vanished;
1354
} EmitClosedData;
1355
1356
static void
1357
emit_closed_data_free (EmitClosedData *data)
1358
0
{
1359
0
  g_object_unref (data->connection);
1360
0
  if (data->error != NULL)
1361
0
    g_error_free (data->error);
1362
0
  g_free (data);
1363
0
}
1364
1365
/* Called in a user thread that has acquired the main context that was
1366
 * thread-default when the object was constructed
1367
 */
1368
static gboolean
1369
emit_closed_in_idle (gpointer user_data)
1370
0
{
1371
0
  EmitClosedData *data = user_data;
1372
0
  gboolean result;
1373
1374
0
  g_object_notify (G_OBJECT (data->connection), "closed");
1375
0
  g_signal_emit (data->connection,
1376
0
                 signals[CLOSED_SIGNAL],
1377
0
                 0,
1378
0
                 data->remote_peer_vanished,
1379
0
                 data->error,
1380
0
                 &result);
1381
0
  return FALSE;
1382
0
}
1383
1384
/* Can be called from any thread, must hold lock.
1385
 * FLAG_CLOSED must already have been set.
1386
 */
1387
static void
1388
schedule_closed_unlocked (GDBusConnection *connection,
1389
                          gboolean         remote_peer_vanished,
1390
                          GError          *error)
1391
0
{
1392
0
  GSource *idle_source;
1393
0
  EmitClosedData *data;
1394
1395
0
  CONNECTION_ENSURE_LOCK (connection);
1396
1397
0
  data = g_new0 (EmitClosedData, 1);
1398
0
  data->connection = g_object_ref (connection);
1399
0
  data->remote_peer_vanished = remote_peer_vanished;
1400
0
  data->error = error != NULL ? g_error_copy (error) : NULL;
1401
1402
0
  idle_source = g_idle_source_new ();
1403
0
  g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
1404
0
  g_source_set_callback (idle_source,
1405
0
                         emit_closed_in_idle,
1406
0
                         data,
1407
0
                         (GDestroyNotify) emit_closed_data_free);
1408
0
  g_source_set_static_name (idle_source, "[gio] emit_closed_in_idle");
1409
0
  g_source_attach (idle_source, connection->main_context_at_construction);
1410
0
  g_source_unref (idle_source);
1411
0
}
1412
1413
/* ---------------------------------------------------------------------------------------------------- */
1414
1415
/**
1416
 * g_dbus_connection_close:
1417
 * @connection: a #GDBusConnection
1418
 * @cancellable: (nullable): a #GCancellable or %NULL
1419
 * @callback: (nullable): a #GAsyncReadyCallback to call when the request is
1420
 *     satisfied or %NULL if you don't care about the result
1421
 * @user_data: The data to pass to @callback
1422
 *
1423
 * Closes @connection. Note that this never causes the process to
1424
 * exit (this might only happen if the other end of a shared message
1425
 * bus connection disconnects, see #GDBusConnection:exit-on-close).
1426
 *
1427
 * Once the connection is closed, operations such as sending a message
1428
 * will return with the error %G_IO_ERROR_CLOSED. Closing a connection
1429
 * will not automatically flush the connection so queued messages may
1430
 * be lost. Use g_dbus_connection_flush() if you need such guarantees.
1431
 *
1432
 * If @connection is already closed, this method fails with
1433
 * %G_IO_ERROR_CLOSED.
1434
 *
1435
 * When @connection has been closed, the #GDBusConnection::closed
1436
 * signal is emitted in the
1437
 * [thread-default main context][g-main-context-push-thread-default]
1438
 * of the thread that @connection was constructed in.
1439
 *
1440
 * This is an asynchronous method. When the operation is finished,
1441
 * @callback will be invoked in the 
1442
 * [thread-default main context][g-main-context-push-thread-default]
1443
 * of the thread you are calling this method from. You can
1444
 * then call g_dbus_connection_close_finish() to get the result of the
1445
 * operation. See g_dbus_connection_close_sync() for the synchronous
1446
 * version.
1447
 *
1448
 * Since: 2.26
1449
 */
1450
void
1451
g_dbus_connection_close (GDBusConnection     *connection,
1452
                         GCancellable        *cancellable,
1453
                         GAsyncReadyCallback  callback,
1454
                         gpointer             user_data)
1455
0
{
1456
0
  GTask *task;
1457
1458
0
  g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
1459
1460
  /* do not use g_return_val_if_fail(), we want the memory barrier */
1461
0
  if (!check_initialized (connection))
1462
0
    return;
1463
1464
0
  g_assert (connection->worker != NULL);
1465
1466
0
  task = g_task_new (connection, cancellable, callback, user_data);
1467
0
  g_task_set_source_tag (task, g_dbus_connection_close);
1468
0
  _g_dbus_worker_close (connection->worker, task);
1469
0
  g_object_unref (task);
1470
0
}
1471
1472
/**
1473
 * g_dbus_connection_close_finish:
1474
 * @connection: a #GDBusConnection
1475
 * @res: a #GAsyncResult obtained from the #GAsyncReadyCallback passed
1476
 *     to g_dbus_connection_close()
1477
 * @error: return location for error or %NULL
1478
 *
1479
 * Finishes an operation started with g_dbus_connection_close().
1480
 *
1481
 * Returns: %TRUE if the operation succeeded, %FALSE if @error is set
1482
 *
1483
 * Since: 2.26
1484
 */
1485
gboolean
1486
g_dbus_connection_close_finish (GDBusConnection  *connection,
1487
                                GAsyncResult     *res,
1488
                                GError          **error)
1489
0
{
1490
0
  g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
1491
0
  g_return_val_if_fail (g_task_is_valid (res, connection), FALSE);
1492
0
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1493
1494
0
  return g_task_propagate_boolean (G_TASK (res), error);
1495
0
}
1496
1497
typedef struct {
1498
    GMainLoop *loop;
1499
    GAsyncResult *result;
1500
} SyncCloseData;
1501
1502
/* Can be called by any thread, without the connection lock */
1503
static void
1504
sync_close_cb (GObject *source_object,
1505
               GAsyncResult *res,
1506
               gpointer user_data)
1507
0
{
1508
0
  SyncCloseData *data = user_data;
1509
1510
0
  data->result = g_object_ref (res);
1511
0
  g_main_loop_quit (data->loop);
1512
0
}
1513
1514
/**
1515
 * g_dbus_connection_close_sync:
1516
 * @connection: a #GDBusConnection
1517
 * @cancellable: (nullable): a #GCancellable or %NULL
1518
 * @error: return location for error or %NULL
1519
 *
1520
 * Synchronously closes @connection. The calling thread is blocked
1521
 * until this is done. See g_dbus_connection_close() for the
1522
 * asynchronous version of this method and more details about what it
1523
 * does.
1524
 *
1525
 * Returns: %TRUE if the operation succeeded, %FALSE if @error is set
1526
 *
1527
 * Since: 2.26
1528
 */
1529
gboolean
1530
g_dbus_connection_close_sync (GDBusConnection  *connection,
1531
                              GCancellable     *cancellable,
1532
                              GError          **error)
1533
0
{
1534
0
  gboolean ret;
1535
1536
0
  g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
1537
0
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1538
1539
0
  ret = FALSE;
1540
1541
0
  if (check_unclosed (connection, 0, error))
1542
0
    {
1543
0
      GMainContext *context;
1544
0
      SyncCloseData data;
1545
1546
0
      context = g_main_context_new ();
1547
0
      g_main_context_push_thread_default (context);
1548
0
      data.loop = g_main_loop_new (context, TRUE);
1549
0
      data.result = NULL;
1550
1551
0
      g_dbus_connection_close (connection, cancellable, sync_close_cb, &data);
1552
0
      g_main_loop_run (data.loop);
1553
0
      ret = g_dbus_connection_close_finish (connection, data.result, error);
1554
1555
0
      g_object_unref (data.result);
1556
0
      g_main_loop_unref (data.loop);
1557
0
      g_main_context_pop_thread_default (context);
1558
0
      g_main_context_unref (context);
1559
0
    }
1560
1561
0
  return ret;
1562
0
}
1563
1564
/* ---------------------------------------------------------------------------------------------------- */
1565
1566
/**
1567
 * g_dbus_connection_get_last_serial:
1568
 * @connection: a #GDBusConnection
1569
 *
1570
 * Retrieves the last serial number assigned to a #GDBusMessage on
1571
 * the current thread. This includes messages sent via both low-level
1572
 * API such as g_dbus_connection_send_message() as well as
1573
 * high-level API such as g_dbus_connection_emit_signal(),
1574
 * g_dbus_connection_call() or g_dbus_proxy_call().
1575
 *
1576
 * Returns: the last used serial or zero when no message has been sent
1577
 *     within the current thread
1578
 *
1579
 * Since: 2.34
1580
 */
1581
guint32
1582
g_dbus_connection_get_last_serial (GDBusConnection *connection)
1583
0
{
1584
0
  guint32 ret;
1585
1586
0
  g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), 0);
1587
1588
0
  CONNECTION_LOCK (connection);
1589
0
  ret = GPOINTER_TO_UINT (g_hash_table_lookup (connection->map_thread_to_last_serial,
1590
0
                                               g_thread_self ()));
1591
0
  CONNECTION_UNLOCK (connection);
1592
1593
0
  return ret;
1594
0
}
1595
1596
/* ---------------------------------------------------------------------------------------------------- */
1597
1598
/* Can be called by any thread, with the connection lock held */
1599
static gboolean
1600
g_dbus_connection_send_message_unlocked (GDBusConnection   *connection,
1601
                                         GDBusMessage      *message,
1602
                                         GDBusSendMessageFlags flags,
1603
                                         guint32           *out_serial,
1604
                                         GError           **error)
1605
0
{
1606
0
  guchar *blob;
1607
0
  gsize blob_size;
1608
0
  guint32 serial_to_use;
1609
1610
0
  CONNECTION_ENSURE_LOCK (connection);
1611
1612
0
  g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
1613
0
  g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), FALSE);
1614
1615
  /* TODO: check all necessary headers are present */
1616
1617
0
  if (out_serial != NULL)
1618
0
    *out_serial = 0;
1619
1620
  /* If we're in initable_init(), don't check for being initialized, to avoid
1621
   * chicken-and-egg problems. initable_init() is responsible for setting up
1622
   * our prerequisites (mainly connection->worker), and only calling us
1623
   * from its own thread (so no memory barrier is needed).
1624
   */
1625
0
  if (!check_unclosed (connection,
1626
0
                       (flags & SEND_MESSAGE_FLAGS_INITIALIZING) ? MAY_BE_UNINITIALIZED : 0,
1627
0
                       error))
1628
0
    return FALSE;
1629
1630
0
  blob = g_dbus_message_to_blob (message,
1631
0
                                 &blob_size,
1632
0
                                 connection->capabilities,
1633
0
                                 error);
1634
0
  if (blob == NULL)
1635
0
    return FALSE;
1636
1637
0
  if (flags & G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL)
1638
0
    serial_to_use = g_dbus_message_get_serial (message);
1639
0
  else
1640
0
    serial_to_use = ++connection->last_serial; /* TODO: handle overflow */
1641
1642
0
  switch (blob[0])
1643
0
    {
1644
0
    case 'l':
1645
0
      ((guint32 *) blob)[2] = GUINT32_TO_LE (serial_to_use);
1646
0
      break;
1647
0
    case 'B':
1648
0
      ((guint32 *) blob)[2] = GUINT32_TO_BE (serial_to_use);
1649
0
      break;
1650
0
    default:
1651
0
      g_assert_not_reached ();
1652
0
      break;
1653
0
    }
1654
1655
#if 0
1656
  g_printerr ("Writing message of %" G_GSIZE_FORMAT " bytes (serial %d) on %p:\n",
1657
              blob_size, serial_to_use, connection);
1658
  g_printerr ("----\n");
1659
  hexdump (blob, blob_size);
1660
  g_printerr ("----\n");
1661
#endif
1662
1663
  /* TODO: use connection->auth to encode the blob */
1664
1665
0
  if (out_serial != NULL)
1666
0
    *out_serial = serial_to_use;
1667
1668
  /* store used serial for the current thread */
1669
  /* TODO: watch the thread disposal and remove associated record
1670
   *       from hashtable
1671
   *  - see https://bugzilla.gnome.org/show_bug.cgi?id=676825#c7
1672
   */
1673
0
  g_hash_table_replace (connection->map_thread_to_last_serial,
1674
0
                        g_thread_self (),
1675
0
                        GUINT_TO_POINTER (serial_to_use));
1676
1677
0
  if (!(flags & G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL))
1678
0
    g_dbus_message_set_serial (message, serial_to_use);
1679
1680
0
  g_dbus_message_lock (message);
1681
1682
0
  _g_dbus_worker_send_message (connection->worker,
1683
0
                               message,
1684
0
                               (gchar*) blob, /* transfer ownership */
1685
0
                               blob_size);
1686
1687
0
  return TRUE;
1688
0
}
1689
1690
/**
1691
 * g_dbus_connection_send_message:
1692
 * @connection: a #GDBusConnection
1693
 * @message: a #GDBusMessage
1694
 * @flags: flags affecting how the message is sent
1695
 * @out_serial: (out) (optional): return location for serial number assigned
1696
 *     to @message when sending it or %NULL
1697
 * @error: Return location for error or %NULL
1698
 *
1699
 * Asynchronously sends @message to the peer represented by @connection.
1700
 *
1701
 * Unless @flags contain the
1702
 * %G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL flag, the serial number
1703
 * will be assigned by @connection and set on @message via
1704
 * g_dbus_message_set_serial(). If @out_serial is not %NULL, then the
1705
 * serial number used will be written to this location prior to
1706
 * submitting the message to the underlying transport. While it has a `volatile`
1707
 * qualifier, this is a historical artifact and the argument passed to it should
1708
 * not be `volatile`.
1709
 *
1710
 * If @connection is closed then the operation will fail with
1711
 * %G_IO_ERROR_CLOSED. If @message is not well-formed,
1712
 * the operation fails with %G_IO_ERROR_INVALID_ARGUMENT.
1713
 *
1714
 * See this [server][gdbus-server] and [client][gdbus-unix-fd-client]
1715
 * for an example of how to use this low-level API to send and receive
1716
 * UNIX file descriptors.
1717
 *
1718
 * Note that @message must be unlocked, unless @flags contain the
1719
 * %G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL flag.
1720
 *
1721
 * Returns: %TRUE if the message was well-formed and queued for
1722
 *     transmission, %FALSE if @error is set
1723
 *
1724
 * Since: 2.26
1725
 */
1726
gboolean
1727
g_dbus_connection_send_message (GDBusConnection        *connection,
1728
                                GDBusMessage           *message,
1729
                                GDBusSendMessageFlags   flags,
1730
                                volatile guint32       *out_serial,
1731
                                GError                **error)
1732
0
{
1733
0
  gboolean ret;
1734
1735
0
  g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
1736
0
  g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), FALSE);
1737
0
  g_return_val_if_fail ((flags & G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL) || !g_dbus_message_get_locked (message), FALSE);
1738
0
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1739
1740
0
  CONNECTION_LOCK (connection);
1741
0
  ret = g_dbus_connection_send_message_unlocked (connection, message, flags, (guint32 *) out_serial, error);
1742
0
  CONNECTION_UNLOCK (connection);
1743
0
  return ret;
1744
0
}
1745
1746
/* ---------------------------------------------------------------------------------------------------- */
1747
1748
typedef struct
1749
{
1750
  guint32 serial;
1751
1752
  gulong cancellable_handler_id;
1753
  GSource *cancelled_idle_source;  /* (owned) (nullable) */
1754
1755
  GSource *timeout_source;  /* (owned) (nullable) */
1756
1757
  gboolean delivered;
1758
} SendMessageData;
1759
1760
/* Can be called from any thread with or without lock held */
1761
static void
1762
send_message_data_free (SendMessageData *data)
1763
0
{
1764
  /* These should already have been cleared by send_message_with_reply_cleanup(). */
1765
0
  g_assert (data->timeout_source == NULL);
1766
0
  g_assert (data->cancellable_handler_id == 0);
1767
1768
0
  g_slice_free (SendMessageData, data);
1769
0
}
1770
1771
/* ---------------------------------------------------------------------------------------------------- */
1772
1773
/* can be called from any thread with lock held; @task is (transfer none) */
1774
static void
1775
send_message_with_reply_cleanup (GTask *task, gboolean remove)
1776
0
{
1777
0
  GDBusConnection *connection = g_task_get_source_object (task);
1778
0
  SendMessageData *data = g_task_get_task_data (task);
1779
1780
0
  CONNECTION_ENSURE_LOCK (connection);
1781
1782
0
  g_assert (!data->delivered);
1783
1784
0
  data->delivered = TRUE;
1785
1786
0
  if (data->timeout_source != NULL)
1787
0
    {
1788
0
      g_source_destroy (data->timeout_source);
1789
0
      g_clear_pointer (&data->timeout_source, g_source_unref);
1790
0
    }
1791
0
  if (data->cancellable_handler_id > 0)
1792
0
    {
1793
0
      g_cancellable_disconnect (g_task_get_cancellable (task), data->cancellable_handler_id);
1794
0
      data->cancellable_handler_id = 0;
1795
0
    }
1796
0
  if (data->cancelled_idle_source != NULL)
1797
0
    {
1798
0
      g_source_destroy (data->cancelled_idle_source);
1799
0
      g_clear_pointer (&data->cancelled_idle_source, g_source_unref);
1800
0
    }
1801
1802
0
  if (remove)
1803
0
    {
1804
0
      gboolean removed = g_hash_table_remove (connection->map_method_serial_to_task,
1805
0
                                              GUINT_TO_POINTER (data->serial));
1806
0
      g_warn_if_fail (removed);
1807
0
    }
1808
0
}
1809
1810
/* ---------------------------------------------------------------------------------------------------- */
1811
1812
/* Called from GDBus worker thread with lock held; @task is (transfer none). */
1813
static void
1814
send_message_data_deliver_reply_unlocked (GTask           *task,
1815
                                          GDBusMessage    *reply)
1816
0
{
1817
0
  SendMessageData *data = g_task_get_task_data (task);
1818
1819
0
  if (data->delivered)
1820
0
    goto out;
1821
1822
0
  g_task_return_pointer (task, g_object_ref (reply), g_object_unref);
1823
1824
0
  send_message_with_reply_cleanup (task, TRUE);
1825
1826
0
 out:
1827
0
  ;
1828
0
}
1829
1830
/* Called from a user thread, lock is not held; @task is (transfer none) */
1831
static void
1832
send_message_data_deliver_error (GTask      *task,
1833
                                 GQuark      domain,
1834
                                 gint        code,
1835
                                 const char *message)
1836
0
{
1837
0
  GDBusConnection *connection = g_task_get_source_object (task);
1838
0
  SendMessageData *data = g_task_get_task_data (task);
1839
1840
0
  CONNECTION_LOCK (connection);
1841
0
  if (data->delivered)
1842
0
    {
1843
0
      CONNECTION_UNLOCK (connection);
1844
0
      return;
1845
0
    }
1846
1847
  /* Hold a ref on @task as send_message_with_reply_cleanup() will remove it
1848
   * from the task map and could end up dropping the last reference */
1849
0
  g_object_ref (task);
1850
1851
0
  send_message_with_reply_cleanup (task, TRUE);
1852
0
  CONNECTION_UNLOCK (connection);
1853
1854
0
  g_task_return_new_error (task, domain, code, "%s", message);
1855
0
  g_object_unref (task);
1856
0
}
1857
1858
/* ---------------------------------------------------------------------------------------------------- */
1859
1860
/* Called from a user thread, lock is not held; @task is (transfer none) */
1861
static gboolean
1862
send_message_with_reply_cancelled_idle_cb (gpointer user_data)
1863
0
{
1864
0
  GTask *task = user_data;
1865
1866
0
  send_message_data_deliver_error (task, G_IO_ERROR, G_IO_ERROR_CANCELLED,
1867
0
                                   _("Operation was cancelled"));
1868
0
  return G_SOURCE_REMOVE;
1869
0
}
1870
1871
/* Can be called from any thread with or without lock held */
1872
static void
1873
send_message_with_reply_cancelled_cb (GCancellable *cancellable,
1874
                                      gpointer      user_data)
1875
0
{
1876
0
  GTask *task = user_data;
1877
0
  SendMessageData *data = g_task_get_task_data (task);
1878
1879
  /* postpone cancellation to idle handler since we may be called directly
1880
   * via g_cancellable_connect() (e.g. holding lock)
1881
   */
1882
0
  if (data->cancelled_idle_source != NULL)
1883
0
    return;
1884
1885
0
  data->cancelled_idle_source = g_idle_source_new ();
1886
0
  g_source_set_static_name (data->cancelled_idle_source, "[gio] send_message_with_reply_cancelled_idle_cb");
1887
0
  g_task_attach_source (task, data->cancelled_idle_source, send_message_with_reply_cancelled_idle_cb);
1888
0
}
1889
1890
/* ---------------------------------------------------------------------------------------------------- */
1891
1892
/* Called from a user thread, lock is not held; @task is (transfer none) */
1893
static gboolean
1894
send_message_with_reply_timeout_cb (gpointer user_data)
1895
0
{
1896
0
  GTask *task = user_data;
1897
1898
0
  send_message_data_deliver_error (task, G_IO_ERROR, G_IO_ERROR_TIMED_OUT,
1899
0
                                   _("Timeout was reached"));
1900
0
  return G_SOURCE_REMOVE;
1901
0
}
1902
1903
/* ---------------------------------------------------------------------------------------------------- */
1904
1905
/* Called from a user thread, connection's lock is held */
1906
static void
1907
g_dbus_connection_send_message_with_reply_unlocked (GDBusConnection     *connection,
1908
                                                    GDBusMessage        *message,
1909
                                                    GDBusSendMessageFlags flags,
1910
                                                    gint                 timeout_msec,
1911
                                                    guint32             *out_serial,
1912
                                                    GCancellable        *cancellable,
1913
                                                    GAsyncReadyCallback  callback,
1914
                                                    gpointer             user_data)
1915
0
{
1916
0
  GTask *task;
1917
0
  SendMessageData *data;
1918
0
  GError *error = NULL;
1919
0
  guint32 serial;
1920
1921
0
  if (out_serial == NULL)
1922
0
    out_serial = &serial;
1923
1924
0
  if (timeout_msec == -1)
1925
0
    timeout_msec = 25 * 1000;
1926
1927
0
  data = g_slice_new0 (SendMessageData);
1928
0
  task = g_task_new (connection, cancellable, callback, user_data);
1929
0
  g_task_set_source_tag (task,
1930
0
                         g_dbus_connection_send_message_with_reply_unlocked);
1931
0
  g_task_set_task_data (task, data, (GDestroyNotify) send_message_data_free);
1932
1933
0
  if (g_task_return_error_if_cancelled (task))
1934
0
    {
1935
0
      g_object_unref (task);
1936
0
      return;
1937
0
    }
1938
1939
0
  if (!g_dbus_connection_send_message_unlocked (connection, message, flags, out_serial, &error))
1940
0
    {
1941
0
      g_task_return_error (task, error);
1942
0
      g_object_unref (task);
1943
0
      return;
1944
0
    }
1945
0
  data->serial = *out_serial;
1946
1947
0
  if (cancellable != NULL)
1948
0
    {
1949
0
      data->cancellable_handler_id = g_cancellable_connect (cancellable,
1950
0
                                                            G_CALLBACK (send_message_with_reply_cancelled_cb),
1951
0
                                                            g_object_ref (task),
1952
0
                                                            g_object_unref);
1953
0
    }
1954
1955
0
  if (timeout_msec != G_MAXINT)
1956
0
    {
1957
0
      data->timeout_source = g_timeout_source_new (timeout_msec);
1958
0
      g_source_set_static_name (data->timeout_source, "[gio] send_message_with_reply_unlocked");
1959
0
      g_task_attach_source (task, data->timeout_source,
1960
0
                            (GSourceFunc) send_message_with_reply_timeout_cb);
1961
0
    }
1962
1963
0
  g_hash_table_insert (connection->map_method_serial_to_task,
1964
0
                       GUINT_TO_POINTER (*out_serial),
1965
0
                       g_steal_pointer (&task));
1966
0
}
1967
1968
/**
1969
 * g_dbus_connection_send_message_with_reply:
1970
 * @connection: a #GDBusConnection
1971
 * @message: a #GDBusMessage
1972
 * @flags: flags affecting how the message is sent
1973
 * @timeout_msec: the timeout in milliseconds, -1 to use the default
1974
 *     timeout or %G_MAXINT for no timeout
1975
 * @out_serial: (out) (optional): return location for serial number assigned
1976
 *     to @message when sending it or %NULL
1977
 * @cancellable: (nullable): a #GCancellable or %NULL
1978
 * @callback: (nullable): a #GAsyncReadyCallback to call when the request
1979
 *     is satisfied or %NULL if you don't care about the result
1980
 * @user_data: The data to pass to @callback
1981
 *
1982
 * Asynchronously sends @message to the peer represented by @connection.
1983
 *
1984
 * Unless @flags contain the
1985
 * %G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL flag, the serial number
1986
 * will be assigned by @connection and set on @message via
1987
 * g_dbus_message_set_serial(). If @out_serial is not %NULL, then the
1988
 * serial number used will be written to this location prior to
1989
 * submitting the message to the underlying transport. While it has a `volatile`
1990
 * qualifier, this is a historical artifact and the argument passed to it should
1991
 * not be `volatile`.
1992
 *
1993
 * If @connection is closed then the operation will fail with
1994
 * %G_IO_ERROR_CLOSED. If @cancellable is canceled, the operation will
1995
 * fail with %G_IO_ERROR_CANCELLED. If @message is not well-formed,
1996
 * the operation fails with %G_IO_ERROR_INVALID_ARGUMENT.
1997
 *
1998
 * This is an asynchronous method. When the operation is finished, @callback
1999
 * will be invoked in the 
2000
 * [thread-default main context][g-main-context-push-thread-default]
2001
 * of the thread you are calling this method from. You can then call
2002
 * g_dbus_connection_send_message_with_reply_finish() to get the result of the operation.
2003
 * See g_dbus_connection_send_message_with_reply_sync() for the synchronous version.
2004
 *
2005
 * Note that @message must be unlocked, unless @flags contain the
2006
 * %G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL flag.
2007
 *
2008
 * See this [server][gdbus-server] and [client][gdbus-unix-fd-client]
2009
 * for an example of how to use this low-level API to send and receive
2010
 * UNIX file descriptors.
2011
 *
2012
 * Since: 2.26
2013
 */
2014
void
2015
g_dbus_connection_send_message_with_reply (GDBusConnection       *connection,
2016
                                           GDBusMessage          *message,
2017
                                           GDBusSendMessageFlags  flags,
2018
                                           gint                   timeout_msec,
2019
                                           volatile guint32      *out_serial,
2020
                                           GCancellable          *cancellable,
2021
                                           GAsyncReadyCallback    callback,
2022
                                           gpointer               user_data)
2023
0
{
2024
0
  g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
2025
0
  g_return_if_fail (G_IS_DBUS_MESSAGE (message));
2026
0
  g_return_if_fail ((flags & G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL) || !g_dbus_message_get_locked (message));
2027
0
  g_return_if_fail (timeout_msec >= 0 || timeout_msec == -1);
2028
2029
0
  CONNECTION_LOCK (connection);
2030
0
  g_dbus_connection_send_message_with_reply_unlocked (connection,
2031
0
                                                      message,
2032
0
                                                      flags,
2033
0
                                                      timeout_msec,
2034
0
                                                      (guint32 *) out_serial,
2035
0
                                                      cancellable,
2036
0
                                                      callback,
2037
0
                                                      user_data);
2038
0
  CONNECTION_UNLOCK (connection);
2039
0
}
2040
2041
/**
2042
 * g_dbus_connection_send_message_with_reply_finish:
2043
 * @connection: a #GDBusConnection
2044
 * @res: a #GAsyncResult obtained from the #GAsyncReadyCallback passed to
2045
 *     g_dbus_connection_send_message_with_reply()
2046
 * @error: teturn location for error or %NULL
2047
 *
2048
 * Finishes an operation started with g_dbus_connection_send_message_with_reply().
2049
 *
2050
 * Note that @error is only set if a local in-process error
2051
 * occurred. That is to say that the returned #GDBusMessage object may
2052
 * be of type %G_DBUS_MESSAGE_TYPE_ERROR. Use
2053
 * g_dbus_message_to_gerror() to transcode this to a #GError.
2054
 *
2055
 * See this [server][gdbus-server] and [client][gdbus-unix-fd-client]
2056
 * for an example of how to use this low-level API to send and receive
2057
 * UNIX file descriptors.
2058
 *
2059
 * Returns: (transfer full): a locked #GDBusMessage or %NULL if @error is set
2060
 *
2061
 * Since: 2.26
2062
 */
2063
GDBusMessage *
2064
g_dbus_connection_send_message_with_reply_finish (GDBusConnection  *connection,
2065
                                                  GAsyncResult     *res,
2066
                                                  GError          **error)
2067
0
{
2068
0
  g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
2069
0
  g_return_val_if_fail (g_task_is_valid (res, connection), NULL);
2070
0
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2071
2072
0
  return g_task_propagate_pointer (G_TASK (res), error);
2073
0
}
2074
2075
/* ---------------------------------------------------------------------------------------------------- */
2076
2077
typedef struct
2078
{
2079
  GAsyncResult *res;
2080
  GMainContext *context;
2081
  GMainLoop *loop;
2082
} SendMessageSyncData;
2083
2084
/* Called from a user thread, lock is not held */
2085
static void
2086
send_message_with_reply_sync_cb (GDBusConnection *connection,
2087
                                 GAsyncResult    *res,
2088
                                 gpointer         user_data)
2089
0
{
2090
0
  SendMessageSyncData *data = user_data;
2091
0
  data->res = g_object_ref (res);
2092
0
  g_main_loop_quit (data->loop);
2093
0
}
2094
2095
/**
2096
 * g_dbus_connection_send_message_with_reply_sync:
2097
 * @connection: a #GDBusConnection
2098
 * @message: a #GDBusMessage
2099
 * @flags: flags affecting how the message is sent.
2100
 * @timeout_msec: the timeout in milliseconds, -1 to use the default
2101
 *     timeout or %G_MAXINT for no timeout
2102
 * @out_serial: (out) (optional): return location for serial number
2103
 *     assigned to @message when sending it or %NULL
2104
 * @cancellable: (nullable): a #GCancellable or %NULL
2105
 * @error: return location for error or %NULL
2106
 *
2107
 * Synchronously sends @message to the peer represented by @connection
2108
 * and blocks the calling thread until a reply is received or the
2109
 * timeout is reached. See g_dbus_connection_send_message_with_reply()
2110
 * for the asynchronous version of this method.
2111
 *
2112
 * Unless @flags contain the
2113
 * %G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL flag, the serial number
2114
 * will be assigned by @connection and set on @message via
2115
 * g_dbus_message_set_serial(). If @out_serial is not %NULL, then the
2116
 * serial number used will be written to this location prior to
2117
 * submitting the message to the underlying transport. While it has a `volatile`
2118
 * qualifier, this is a historical artifact and the argument passed to it should
2119
 * not be `volatile`.
2120
 *
2121
 * If @connection is closed then the operation will fail with
2122
 * %G_IO_ERROR_CLOSED. If @cancellable is canceled, the operation will
2123
 * fail with %G_IO_ERROR_CANCELLED. If @message is not well-formed,
2124
 * the operation fails with %G_IO_ERROR_INVALID_ARGUMENT.
2125
 *
2126
 * Note that @error is only set if a local in-process error
2127
 * occurred. That is to say that the returned #GDBusMessage object may
2128
 * be of type %G_DBUS_MESSAGE_TYPE_ERROR. Use
2129
 * g_dbus_message_to_gerror() to transcode this to a #GError.
2130
 *
2131
 * See this [server][gdbus-server] and [client][gdbus-unix-fd-client]
2132
 * for an example of how to use this low-level API to send and receive
2133
 * UNIX file descriptors.
2134
 *
2135
 * Note that @message must be unlocked, unless @flags contain the
2136
 * %G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL flag.
2137
 *
2138
 * Returns: (transfer full): a locked #GDBusMessage that is the reply
2139
 *     to @message or %NULL if @error is set
2140
 *
2141
 * Since: 2.26
2142
 */
2143
GDBusMessage *
2144
g_dbus_connection_send_message_with_reply_sync (GDBusConnection        *connection,
2145
                                                GDBusMessage           *message,
2146
                                                GDBusSendMessageFlags   flags,
2147
                                                gint                    timeout_msec,
2148
                                                volatile guint32       *out_serial,
2149
                                                GCancellable           *cancellable,
2150
                                                GError                **error)
2151
0
{
2152
0
  SendMessageSyncData data;
2153
0
  GDBusMessage *reply;
2154
2155
0
  g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
2156
0
  g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
2157
0
  g_return_val_if_fail ((flags & G_DBUS_SEND_MESSAGE_FLAGS_PRESERVE_SERIAL) || !g_dbus_message_get_locked (message), NULL);
2158
0
  g_return_val_if_fail (timeout_msec >= 0 || timeout_msec == -1, NULL);
2159
0
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2160
2161
0
  data.res = NULL;
2162
0
  data.context = g_main_context_new ();
2163
0
  data.loop = g_main_loop_new (data.context, FALSE);
2164
2165
0
  g_main_context_push_thread_default (data.context);
2166
2167
0
  g_dbus_connection_send_message_with_reply (connection,
2168
0
                                             message,
2169
0
                                             flags,
2170
0
                                             timeout_msec,
2171
0
                                             out_serial,
2172
0
                                             cancellable,
2173
0
                                             (GAsyncReadyCallback) send_message_with_reply_sync_cb,
2174
0
                                             &data);
2175
0
  g_main_loop_run (data.loop);
2176
0
  reply = g_dbus_connection_send_message_with_reply_finish (connection,
2177
0
                                                            data.res,
2178
0
                                                            error);
2179
2180
0
  g_main_context_pop_thread_default (data.context);
2181
2182
0
  g_main_context_unref (data.context);
2183
0
  g_main_loop_unref (data.loop);
2184
0
  if (data.res)
2185
0
    g_object_unref (data.res);
2186
2187
0
  return reply;
2188
0
}
2189
2190
/* ---------------------------------------------------------------------------------------------------- */
2191
2192
typedef struct
2193
{
2194
  guint                       id;
2195
  guint                       ref_count;
2196
  GDBusMessageFilterFunction  filter_function;
2197
  gpointer                    user_data;
2198
  GDestroyNotify              user_data_free_func;
2199
  GMainContext               *context;
2200
} FilterData;
2201
2202
static void
2203
filter_data_destroy (FilterData *filter, gboolean notify_sync)
2204
0
{
2205
0
  if (notify_sync)
2206
0
    {
2207
0
      if (filter->user_data_free_func != NULL)
2208
0
        filter->user_data_free_func (filter->user_data);
2209
0
    }
2210
0
  else
2211
0
    {
2212
0
      call_destroy_notify (filter->context,
2213
0
                           filter->user_data_free_func,
2214
0
                           filter->user_data);
2215
0
    }
2216
0
  g_main_context_unref (filter->context);
2217
0
  g_free (filter);
2218
0
}
2219
2220
/* requires CONNECTION_LOCK */
2221
static FilterData **
2222
copy_filter_list (GPtrArray *filters)
2223
0
{
2224
0
  FilterData **copy;
2225
0
  guint n;
2226
2227
0
  copy = g_new (FilterData *, filters->len + 1);
2228
0
  for (n = 0; n < filters->len; n++)
2229
0
    {
2230
0
      copy[n] = filters->pdata[n];
2231
0
      copy[n]->ref_count++;
2232
0
    }
2233
0
  copy[n] = NULL;
2234
2235
0
  return copy;
2236
0
}
2237
2238
/* requires CONNECTION_LOCK */
2239
static void
2240
free_filter_list (FilterData **filters)
2241
0
{
2242
0
  guint n;
2243
2244
0
  for (n = 0; filters[n]; n++)
2245
0
    {
2246
0
      filters[n]->ref_count--;
2247
0
      if (filters[n]->ref_count == 0)
2248
0
        filter_data_destroy (filters[n], FALSE);
2249
0
    }
2250
0
  g_free (filters);
2251
0
}
2252
2253
/* Called in GDBusWorker's thread - we must not block - with no lock held */
2254
static void
2255
on_worker_message_received (GDBusWorker  *worker,
2256
                            GDBusMessage *message,
2257
                            gpointer      user_data)
2258
0
{
2259
0
  GDBusConnection *connection;
2260
0
  FilterData **filters;
2261
0
  guint n;
2262
0
  gboolean alive;
2263
2264
0
  G_LOCK (message_bus_lock);
2265
0
  alive = g_hash_table_contains (alive_connections, user_data);
2266
0
  if (!alive)
2267
0
    {
2268
0
      G_UNLOCK (message_bus_lock);
2269
0
      return;
2270
0
    }
2271
0
  connection = G_DBUS_CONNECTION (user_data);
2272
0
  g_object_ref (connection);
2273
0
  G_UNLOCK (message_bus_lock);
2274
2275
  //g_debug ("in on_worker_message_received");
2276
2277
0
  g_object_ref (message);
2278
0
  g_dbus_message_lock (message);
2279
2280
  //g_debug ("boo ref_count = %d %p %p", G_OBJECT (connection)->ref_count, connection, connection->worker);
2281
2282
  /* First collect the set of callback functions */
2283
0
  CONNECTION_LOCK (connection);
2284
0
  filters = copy_filter_list (connection->filters);
2285
0
  CONNECTION_UNLOCK (connection);
2286
2287
  /* then call the filters in order (without holding the lock) */
2288
0
  for (n = 0; filters[n]; n++)
2289
0
    {
2290
0
      message = filters[n]->filter_function (connection,
2291
0
                                             message,
2292
0
                                             TRUE,
2293
0
                                             filters[n]->user_data);
2294
0
      if (message == NULL)
2295
0
        break;
2296
0
      g_dbus_message_lock (message);
2297
0
    }
2298
2299
0
  CONNECTION_LOCK (connection);
2300
0
  free_filter_list (filters);
2301
0
  CONNECTION_UNLOCK (connection);
2302
2303
  /* Standard dispatch unless the filter ate the message - no need to
2304
   * do anything if the message was altered
2305
   */
2306
0
  if (message != NULL)
2307
0
    {
2308
0
      GDBusMessageType message_type;
2309
2310
0
      message_type = g_dbus_message_get_message_type (message);
2311
0
      if (message_type == G_DBUS_MESSAGE_TYPE_METHOD_RETURN || message_type == G_DBUS_MESSAGE_TYPE_ERROR)
2312
0
        {
2313
0
          guint32 reply_serial;
2314
0
          GTask *task;
2315
2316
0
          reply_serial = g_dbus_message_get_reply_serial (message);
2317
0
          CONNECTION_LOCK (connection);
2318
0
          task = g_hash_table_lookup (connection->map_method_serial_to_task,
2319
0
                                      GUINT_TO_POINTER (reply_serial));
2320
0
          if (task != NULL)
2321
0
            {
2322
              /* This removes @task from @map_method_serial_to_task. */
2323
              //g_debug ("delivering reply/error for serial %d for %p", reply_serial, connection);
2324
0
              send_message_data_deliver_reply_unlocked (task, message);
2325
0
            }
2326
0
          else
2327
0
            {
2328
              //g_debug ("message reply/error for serial %d but no SendMessageData found for %p", reply_serial, connection);
2329
0
            }
2330
0
          CONNECTION_UNLOCK (connection);
2331
0
        }
2332
0
      else if (message_type == G_DBUS_MESSAGE_TYPE_SIGNAL)
2333
0
        {
2334
0
          CONNECTION_LOCK (connection);
2335
0
          distribute_signals (connection, message);
2336
0
          CONNECTION_UNLOCK (connection);
2337
0
        }
2338
0
      else if (message_type == G_DBUS_MESSAGE_TYPE_METHOD_CALL)
2339
0
        {
2340
0
          CONNECTION_LOCK (connection);
2341
0
          distribute_method_call (connection, message);
2342
0
          CONNECTION_UNLOCK (connection);
2343
0
        }
2344
0
    }
2345
2346
0
  if (message != NULL)
2347
0
    g_object_unref (message);
2348
0
  g_object_unref (connection);
2349
0
}
2350
2351
/* Called in GDBusWorker's thread, lock is not held */
2352
static GDBusMessage *
2353
on_worker_message_about_to_be_sent (GDBusWorker  *worker,
2354
                                    GDBusMessage *message,
2355
                                    gpointer      user_data)
2356
0
{
2357
0
  GDBusConnection *connection;
2358
0
  FilterData **filters;
2359
0
  guint n;
2360
0
  gboolean alive;
2361
2362
0
  G_LOCK (message_bus_lock);
2363
0
  alive = g_hash_table_contains (alive_connections, user_data);
2364
0
  if (!alive)
2365
0
    {
2366
0
      G_UNLOCK (message_bus_lock);
2367
0
      return message;
2368
0
    }
2369
0
  connection = G_DBUS_CONNECTION (user_data);
2370
0
  g_object_ref (connection);
2371
0
  G_UNLOCK (message_bus_lock);
2372
2373
  //g_debug ("in on_worker_message_about_to_be_sent");
2374
2375
  /* First collect the set of callback functions */
2376
0
  CONNECTION_LOCK (connection);
2377
0
  filters = copy_filter_list (connection->filters);
2378
0
  CONNECTION_UNLOCK (connection);
2379
2380
  /* then call the filters in order (without holding the lock) */
2381
0
  for (n = 0; filters[n]; n++)
2382
0
    {
2383
0
      g_dbus_message_lock (message);
2384
0
      message = filters[n]->filter_function (connection,
2385
0
                                             message,
2386
0
                                             FALSE,
2387
0
                                             filters[n]->user_data);
2388
0
      if (message == NULL)
2389
0
        break;
2390
0
    }
2391
2392
0
  CONNECTION_LOCK (connection);
2393
0
  free_filter_list (filters);
2394
0
  CONNECTION_UNLOCK (connection);
2395
2396
0
  g_object_unref (connection);
2397
2398
0
  return message;
2399
0
}
2400
2401
/* called with connection lock held, in GDBusWorker thread
2402
 * @key, @value and @user_data are (transfer none) */
2403
static gboolean
2404
cancel_method_on_close (gpointer key, gpointer value, gpointer user_data)
2405
0
{
2406
0
  GTask *task = value;
2407
0
  SendMessageData *data = g_task_get_task_data (task);
2408
2409
0
  if (data->delivered)
2410
0
    return FALSE;
2411
2412
0
  g_task_return_new_error (task,
2413
0
                           G_IO_ERROR,
2414
0
                           G_IO_ERROR_CLOSED,
2415
0
                           _("The connection is closed"));
2416
2417
  /* Ask send_message_with_reply_cleanup not to remove the element from the
2418
   * hash table - we're in the middle of a foreach; that would be unsafe.
2419
   * Instead, return TRUE from this function so that it gets removed safely.
2420
   */
2421
0
  send_message_with_reply_cleanup (task, FALSE);
2422
0
  return TRUE;
2423
0
}
2424
2425
/* Called in GDBusWorker's thread - we must not block - without lock held */
2426
static void
2427
on_worker_closed (GDBusWorker *worker,
2428
                  gboolean     remote_peer_vanished,
2429
                  GError      *error,
2430
                  gpointer     user_data)
2431
0
{
2432
0
  GDBusConnection *connection;
2433
0
  gboolean alive;
2434
0
  guint old_atomic_flags;
2435
2436
0
  G_LOCK (message_bus_lock);
2437
0
  alive = g_hash_table_contains (alive_connections, user_data);
2438
0
  if (!alive)
2439
0
    {
2440
0
      G_UNLOCK (message_bus_lock);
2441
0
      return;
2442
0
    }
2443
0
  connection = G_DBUS_CONNECTION (user_data);
2444
0
  g_object_ref (connection);
2445
0
  G_UNLOCK (message_bus_lock);
2446
2447
  //g_debug ("in on_worker_closed: %s", error->message);
2448
2449
0
  CONNECTION_LOCK (connection);
2450
  /* Even though this is atomic, we do it inside the lock to avoid breaking
2451
   * assumptions in remove_match_rule(). We'd need the lock in a moment
2452
   * anyway, so, no loss.
2453
   */
2454
0
  old_atomic_flags = g_atomic_int_or (&connection->atomic_flags, FLAG_CLOSED);
2455
2456
0
  if (!(old_atomic_flags & FLAG_CLOSED))
2457
0
    {
2458
0
      g_hash_table_foreach_remove (connection->map_method_serial_to_task, cancel_method_on_close, NULL);
2459
0
      schedule_closed_unlocked (connection, remote_peer_vanished, error);
2460
0
    }
2461
0
  CONNECTION_UNLOCK (connection);
2462
2463
0
  g_object_unref (connection);
2464
0
}
2465
2466
/* ---------------------------------------------------------------------------------------------------- */
2467
2468
/* Determines the biggest set of capabilities we can support on this
2469
 * connection.
2470
 *
2471
 * Called with the init_lock held.
2472
 */
2473
static GDBusCapabilityFlags
2474
get_offered_capabilities_max (GDBusConnection *connection)
2475
0
{
2476
0
      GDBusCapabilityFlags ret;
2477
0
      ret = G_DBUS_CAPABILITY_FLAGS_NONE;
2478
0
#ifdef G_OS_UNIX
2479
0
      if (G_IS_UNIX_CONNECTION (connection->stream))
2480
0
        ret |= G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING;
2481
0
#endif
2482
0
      return ret;
2483
0
}
2484
2485
/* Called in a user thread, lock is not held */
2486
static gboolean
2487
initable_init (GInitable     *initable,
2488
               GCancellable  *cancellable,
2489
               GError       **error)
2490
0
{
2491
0
  GDBusConnection *connection = G_DBUS_CONNECTION (initable);
2492
0
  gboolean ret;
2493
2494
  /* This method needs to be idempotent to work with the singleton
2495
   * pattern. See the docs for g_initable_init(). We implement this by
2496
   * locking.
2497
   *
2498
   * Unfortunately we can't use the main lock since the on_worker_*()
2499
   * callbacks above needs the lock during initialization (for message
2500
   * bus connections we do a synchronous Hello() call on the bus).
2501
   */
2502
0
  g_mutex_lock (&connection->init_lock);
2503
2504
0
  ret = FALSE;
2505
2506
  /* Make this a no-op if we're already initialized (successfully or
2507
   * unsuccessfully)
2508
   */
2509
0
  if ((g_atomic_int_get (&connection->atomic_flags) & FLAG_INITIALIZED))
2510
0
    {
2511
0
      ret = (connection->initialization_error == NULL);
2512
0
      goto out;
2513
0
    }
2514
2515
  /* Because of init_lock, we can't get here twice in different threads */
2516
0
  g_assert (connection->initialization_error == NULL);
2517
2518
  /* The user can pass multiple (but mutally exclusive) construct
2519
   * properties:
2520
   *
2521
   *  - stream (of type GIOStream)
2522
   *  - address (of type gchar*)
2523
   *
2524
   * At the end of the day we end up with a non-NULL GIOStream
2525
   * object in connection->stream.
2526
   */
2527
0
  if (connection->address != NULL)
2528
0
    {
2529
0
      g_assert (connection->stream == NULL);
2530
2531
0
      if ((connection->flags & G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER) ||
2532
0
          (connection->flags & G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS) ||
2533
0
          (connection->flags & G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_REQUIRE_SAME_USER))
2534
0
        {
2535
0
          g_set_error_literal (&connection->initialization_error,
2536
0
                               G_IO_ERROR,
2537
0
                               G_IO_ERROR_INVALID_ARGUMENT,
2538
0
                               _("Unsupported flags encountered when constructing a client-side connection"));
2539
0
          goto out;
2540
0
        }
2541
2542
0
      connection->stream = g_dbus_address_get_stream_sync (connection->address,
2543
0
                                                           NULL, /* TODO: out_guid */
2544
0
                                                           cancellable,
2545
0
                                                           &connection->initialization_error);
2546
0
      if (connection->stream == NULL)
2547
0
        goto out;
2548
0
    }
2549
0
  else if (connection->stream != NULL)
2550
0
    {
2551
      /* nothing to do */
2552
0
    }
2553
0
  else
2554
0
    {
2555
0
      g_assert_not_reached ();
2556
0
    }
2557
2558
  /* Authenticate the connection */
2559
0
  if (connection->flags & G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER)
2560
0
    {
2561
0
      g_assert (!(connection->flags & G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT));
2562
0
      g_assert (connection->guid != NULL);
2563
0
      connection->auth = _g_dbus_auth_new (connection->stream);
2564
0
      if (!_g_dbus_auth_run_server (connection->auth,
2565
0
                                    connection->authentication_observer,
2566
0
                                    connection->guid,
2567
0
                                    (connection->flags & G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS),
2568
0
                                    (connection->flags & G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_REQUIRE_SAME_USER),
2569
0
                                    get_offered_capabilities_max (connection),
2570
0
                                    &connection->capabilities,
2571
0
                                    &connection->credentials,
2572
0
                                    cancellable,
2573
0
                                    &connection->initialization_error))
2574
0
        goto out;
2575
0
    }
2576
0
  else if (connection->flags & G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT)
2577
0
    {
2578
0
      g_assert (!(connection->flags & G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER));
2579
0
      g_assert (connection->guid == NULL);
2580
0
      connection->auth = _g_dbus_auth_new (connection->stream);
2581
0
      connection->guid = _g_dbus_auth_run_client (connection->auth,
2582
0
                                                  connection->authentication_observer,
2583
0
                                                  connection->flags,
2584
0
                                                  get_offered_capabilities_max (connection),
2585
0
                                                  &connection->capabilities,
2586
0
                                                  cancellable,
2587
0
                                                  &connection->initialization_error);
2588
0
      if (connection->guid == NULL)
2589
0
        goto out;
2590
0
    }
2591
2592
0
  if (connection->authentication_observer != NULL)
2593
0
    {
2594
0
      g_object_unref (connection->authentication_observer);
2595
0
      connection->authentication_observer = NULL;
2596
0
    }
2597
2598
  //g_output_stream_flush (G_SOCKET_CONNECTION (connection->stream)
2599
2600
  //g_debug ("haz unix fd passing powers: %d", connection->capabilities & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);
2601
2602
0
#ifdef G_OS_UNIX
2603
  /* We want all IO operations to be non-blocking since they happen in
2604
   * the worker thread which is shared by _all_ connections.
2605
   */
2606
0
  if (G_IS_SOCKET_CONNECTION (connection->stream))
2607
0
    {
2608
0
      g_socket_set_blocking (g_socket_connection_get_socket (G_SOCKET_CONNECTION (connection->stream)), FALSE);
2609
0
    }
2610
0
#endif
2611
2612
0
  G_LOCK (message_bus_lock);
2613
0
  if (alive_connections == NULL)
2614
0
    alive_connections = g_hash_table_new (g_direct_hash, g_direct_equal);
2615
0
  g_hash_table_add (alive_connections, connection);
2616
0
  G_UNLOCK (message_bus_lock);
2617
2618
0
  connection->worker = _g_dbus_worker_new (connection->stream,
2619
0
                                           connection->capabilities,
2620
0
                                           ((connection->flags & G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING) != 0),
2621
0
                                           on_worker_message_received,
2622
0
                                           on_worker_message_about_to_be_sent,
2623
0
                                           on_worker_closed,
2624
0
                                           connection);
2625
2626
  /* if a bus connection, call org.freedesktop.DBus.Hello - this is how we're getting a name */
2627
0
  if (connection->flags & G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION)
2628
0
    {
2629
0
      GVariant *hello_result;
2630
2631
      /* we could lift this restriction by adding code in gdbusprivate.c */
2632
0
      if (connection->flags & G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING)
2633
0
        {
2634
0
          g_set_error_literal (&connection->initialization_error,
2635
0
                               G_IO_ERROR,
2636
0
                               G_IO_ERROR_FAILED,
2637
0
                               "Cannot use DELAY_MESSAGE_PROCESSING with MESSAGE_BUS_CONNECTION");
2638
0
          goto out;
2639
0
        }
2640
2641
0
      hello_result = g_dbus_connection_call_sync (connection,
2642
0
                                                  "org.freedesktop.DBus", /* name */
2643
0
                                                  "/org/freedesktop/DBus", /* path */
2644
0
                                                  "org.freedesktop.DBus", /* interface */
2645
0
                                                  "Hello",
2646
0
                                                  NULL, /* parameters */
2647
0
                                                  G_VARIANT_TYPE ("(s)"),
2648
0
                                                  CALL_FLAGS_INITIALIZING,
2649
0
                                                  -1,
2650
0
                                                  NULL, /* TODO: cancellable */
2651
0
                                                  &connection->initialization_error);
2652
0
      if (hello_result == NULL)
2653
0
        goto out;
2654
2655
0
      g_variant_get (hello_result, "(s)", &connection->bus_unique_name);
2656
0
      g_variant_unref (hello_result);
2657
      //g_debug ("unique name is '%s'", connection->bus_unique_name);
2658
0
    }
2659
2660
0
  ret = TRUE;
2661
0
 out:
2662
0
  if (!ret)
2663
0
    {
2664
0
      g_assert (connection->initialization_error != NULL);
2665
0
      g_propagate_error (error, g_error_copy (connection->initialization_error));
2666
0
    }
2667
2668
0
  g_atomic_int_or (&connection->atomic_flags, FLAG_INITIALIZED);
2669
0
  g_mutex_unlock (&connection->init_lock);
2670
2671
0
  return ret;
2672
0
}
2673
2674
static void
2675
initable_iface_init (GInitableIface *initable_iface)
2676
0
{
2677
0
  initable_iface->init = initable_init;
2678
0
}
2679
2680
/* ---------------------------------------------------------------------------------------------------- */
2681
2682
static void
2683
async_initable_iface_init (GAsyncInitableIface *async_initable_iface)
2684
0
{
2685
  /* Use default */
2686
0
}
2687
2688
/* ---------------------------------------------------------------------------------------------------- */
2689
2690
/**
2691
 * g_dbus_connection_new:
2692
 * @stream: a #GIOStream
2693
 * @guid: (nullable): the GUID to use if authenticating as a server or %NULL
2694
 * @flags: flags describing how to make the connection
2695
 * @observer: (nullable): a #GDBusAuthObserver or %NULL
2696
 * @cancellable: (nullable): a #GCancellable or %NULL
2697
 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2698
 * @user_data: the data to pass to @callback
2699
 *
2700
 * Asynchronously sets up a D-Bus connection for exchanging D-Bus messages
2701
 * with the end represented by @stream.
2702
 *
2703
 * If @stream is a #GSocketConnection, then the corresponding #GSocket
2704
 * will be put into non-blocking mode.
2705
 *
2706
 * The D-Bus connection will interact with @stream from a worker thread.
2707
 * As a result, the caller should not interact with @stream after this
2708
 * method has been called, except by calling g_object_unref() on it.
2709
 *
2710
 * If @observer is not %NULL it may be used to control the
2711
 * authentication process.
2712
 *
2713
 * When the operation is finished, @callback will be invoked. You can
2714
 * then call g_dbus_connection_new_finish() to get the result of the
2715
 * operation.
2716
 *
2717
 * This is an asynchronous failable constructor. See
2718
 * g_dbus_connection_new_sync() for the synchronous
2719
 * version.
2720
 *
2721
 * Since: 2.26
2722
 */
2723
void
2724
g_dbus_connection_new (GIOStream            *stream,
2725
                       const gchar          *guid,
2726
                       GDBusConnectionFlags  flags,
2727
                       GDBusAuthObserver    *observer,
2728
                       GCancellable         *cancellable,
2729
                       GAsyncReadyCallback   callback,
2730
                       gpointer              user_data)
2731
0
{
2732
0
  _g_dbus_initialize ();
2733
2734
0
  g_return_if_fail (G_IS_IO_STREAM (stream));
2735
0
  g_return_if_fail ((flags & ~G_DBUS_CONNECTION_FLAGS_ALL) == 0);
2736
2737
0
  g_async_initable_new_async (G_TYPE_DBUS_CONNECTION,
2738
0
                              G_PRIORITY_DEFAULT,
2739
0
                              cancellable,
2740
0
                              callback,
2741
0
                              user_data,
2742
0
                              "stream", stream,
2743
0
                              "guid", guid,
2744
0
                              "flags", flags,
2745
0
                              "authentication-observer", observer,
2746
0
                              NULL);
2747
0
}
2748
2749
/**
2750
 * g_dbus_connection_new_finish:
2751
 * @res: a #GAsyncResult obtained from the #GAsyncReadyCallback
2752
 *     passed to g_dbus_connection_new().
2753
 * @error: return location for error or %NULL
2754
 *
2755
 * Finishes an operation started with g_dbus_connection_new().
2756
 *
2757
 * Returns: (transfer full): a #GDBusConnection or %NULL if @error is set. Free
2758
 *     with g_object_unref().
2759
 *
2760
 * Since: 2.26
2761
 */
2762
GDBusConnection *
2763
g_dbus_connection_new_finish (GAsyncResult  *res,
2764
                              GError       **error)
2765
0
{
2766
0
  GObject *object;
2767
0
  GObject *source_object;
2768
2769
0
  g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2770
0
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2771
2772
0
  source_object = g_async_result_get_source_object (res);
2773
0
  g_assert (source_object != NULL);
2774
0
  object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
2775
0
                                        res,
2776
0
                                        error);
2777
0
  g_object_unref (source_object);
2778
0
  if (object != NULL)
2779
0
    return G_DBUS_CONNECTION (object);
2780
0
  else
2781
0
    return NULL;
2782
0
}
2783
2784
/**
2785
 * g_dbus_connection_new_sync:
2786
 * @stream: a #GIOStream
2787
 * @guid: (nullable): the GUID to use if authenticating as a server or %NULL
2788
 * @flags: flags describing how to make the connection
2789
 * @observer: (nullable): a #GDBusAuthObserver or %NULL
2790
 * @cancellable: (nullable): a #GCancellable or %NULL
2791
 * @error: return location for error or %NULL
2792
 *
2793
 * Synchronously sets up a D-Bus connection for exchanging D-Bus messages
2794
 * with the end represented by @stream.
2795
 *
2796
 * If @stream is a #GSocketConnection, then the corresponding #GSocket
2797
 * will be put into non-blocking mode.
2798
 *
2799
 * The D-Bus connection will interact with @stream from a worker thread.
2800
 * As a result, the caller should not interact with @stream after this
2801
 * method has been called, except by calling g_object_unref() on it.
2802
 *
2803
 * If @observer is not %NULL it may be used to control the
2804
 * authentication process.
2805
 *
2806
 * This is a synchronous failable constructor. See
2807
 * g_dbus_connection_new() for the asynchronous version.
2808
 *
2809
 * Returns: (transfer full): a #GDBusConnection or %NULL if @error is set.
2810
 *     Free with g_object_unref().
2811
 *
2812
 * Since: 2.26
2813
 */
2814
GDBusConnection *
2815
g_dbus_connection_new_sync (GIOStream             *stream,
2816
                            const gchar           *guid,
2817
                            GDBusConnectionFlags   flags,
2818
                            GDBusAuthObserver     *observer,
2819
                            GCancellable          *cancellable,
2820
                            GError               **error)
2821
0
{
2822
0
  _g_dbus_initialize ();
2823
0
  g_return_val_if_fail (G_IS_IO_STREAM (stream), NULL);
2824
0
  g_return_val_if_fail ((flags & ~G_DBUS_CONNECTION_FLAGS_ALL) == 0, NULL);
2825
0
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2826
0
  return g_initable_new (G_TYPE_DBUS_CONNECTION,
2827
0
                         cancellable,
2828
0
                         error,
2829
0
                         "stream", stream,
2830
0
                         "guid", guid,
2831
0
                         "flags", flags,
2832
0
                         "authentication-observer", observer,
2833
0
                         NULL);
2834
0
}
2835
2836
/* ---------------------------------------------------------------------------------------------------- */
2837
2838
/**
2839
 * g_dbus_connection_new_for_address:
2840
 * @address: a D-Bus address
2841
 * @flags: flags describing how to make the connection
2842
 * @observer: (nullable): a #GDBusAuthObserver or %NULL
2843
 * @cancellable: (nullable): a #GCancellable or %NULL
2844
 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2845
 * @user_data: the data to pass to @callback
2846
 *
2847
 * Asynchronously connects and sets up a D-Bus client connection for
2848
 * exchanging D-Bus messages with an endpoint specified by @address
2849
 * which must be in the
2850
 * [D-Bus address format](https://dbus.freedesktop.org/doc/dbus-specification.html#addresses).
2851
 *
2852
 * This constructor can only be used to initiate client-side
2853
 * connections - use g_dbus_connection_new() if you need to act as the
2854
 * server. In particular, @flags cannot contain the
2855
 * %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER,
2856
 * %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS or
2857
 * %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_REQUIRE_SAME_USER flags.
2858
 *
2859
 * When the operation is finished, @callback will be invoked. You can
2860
 * then call g_dbus_connection_new_for_address_finish() to get the result of
2861
 * the operation.
2862
 *
2863
 * If @observer is not %NULL it may be used to control the
2864
 * authentication process.
2865
 *
2866
 * This is an asynchronous failable constructor. See
2867
 * g_dbus_connection_new_for_address_sync() for the synchronous
2868
 * version.
2869
 *
2870
 * Since: 2.26
2871
 */
2872
void
2873
g_dbus_connection_new_for_address (const gchar          *address,
2874
                                   GDBusConnectionFlags  flags,
2875
                                   GDBusAuthObserver    *observer,
2876
                                   GCancellable         *cancellable,
2877
                                   GAsyncReadyCallback   callback,
2878
                                   gpointer              user_data)
2879
0
{
2880
0
  _g_dbus_initialize ();
2881
2882
0
  g_return_if_fail (address != NULL);
2883
0
  g_return_if_fail ((flags & ~G_DBUS_CONNECTION_FLAGS_ALL) == 0);
2884
2885
0
  g_async_initable_new_async (G_TYPE_DBUS_CONNECTION,
2886
0
                              G_PRIORITY_DEFAULT,
2887
0
                              cancellable,
2888
0
                              callback,
2889
0
                              user_data,
2890
0
                              "address", address,
2891
0
                              "flags", flags,
2892
0
                              "authentication-observer", observer,
2893
0
                              NULL);
2894
0
}
2895
2896
/**
2897
 * g_dbus_connection_new_for_address_finish:
2898
 * @res: a #GAsyncResult obtained from the #GAsyncReadyCallback passed
2899
 *     to g_dbus_connection_new()
2900
 * @error: return location for error or %NULL
2901
 *
2902
 * Finishes an operation started with g_dbus_connection_new_for_address().
2903
 *
2904
 * Returns: (transfer full): a #GDBusConnection or %NULL if @error is set.
2905
 *     Free with g_object_unref().
2906
 *
2907
 * Since: 2.26
2908
 */
2909
GDBusConnection *
2910
g_dbus_connection_new_for_address_finish (GAsyncResult  *res,
2911
                                          GError       **error)
2912
0
{
2913
0
  GObject *object;
2914
0
  GObject *source_object;
2915
2916
0
  g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2917
0
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2918
2919
0
  source_object = g_async_result_get_source_object (res);
2920
0
  g_assert (source_object != NULL);
2921
0
  object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
2922
0
                                        res,
2923
0
                                        error);
2924
0
  g_object_unref (source_object);
2925
0
  if (object != NULL)
2926
0
    return G_DBUS_CONNECTION (object);
2927
0
  else
2928
0
    return NULL;
2929
0
}
2930
2931
/**
2932
 * g_dbus_connection_new_for_address_sync:
2933
 * @address: a D-Bus address
2934
 * @flags: flags describing how to make the connection
2935
 * @observer: (nullable): a #GDBusAuthObserver or %NULL
2936
 * @cancellable: (nullable): a #GCancellable or %NULL
2937
 * @error: return location for error or %NULL
2938
 *
2939
 * Synchronously connects and sets up a D-Bus client connection for
2940
 * exchanging D-Bus messages with an endpoint specified by @address
2941
 * which must be in the
2942
 * [D-Bus address format](https://dbus.freedesktop.org/doc/dbus-specification.html#addresses).
2943
 *
2944
 * This constructor can only be used to initiate client-side
2945
 * connections - use g_dbus_connection_new_sync() if you need to act
2946
 * as the server. In particular, @flags cannot contain the
2947
 * %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER,
2948
 * %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS or
2949
 * %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_REQUIRE_SAME_USER flags.
2950
 *
2951
 * This is a synchronous failable constructor. See
2952
 * g_dbus_connection_new_for_address() for the asynchronous version.
2953
 *
2954
 * If @observer is not %NULL it may be used to control the
2955
 * authentication process.
2956
 *
2957
 * Returns: (transfer full): a #GDBusConnection or %NULL if @error is set.
2958
 *     Free with g_object_unref().
2959
 *
2960
 * Since: 2.26
2961
 */
2962
GDBusConnection *
2963
g_dbus_connection_new_for_address_sync (const gchar           *address,
2964
                                        GDBusConnectionFlags   flags,
2965
                                        GDBusAuthObserver     *observer,
2966
                                        GCancellable          *cancellable,
2967
                                        GError               **error)
2968
0
{
2969
0
  _g_dbus_initialize ();
2970
2971
0
  g_return_val_if_fail (address != NULL, NULL);
2972
0
  g_return_val_if_fail ((flags & ~G_DBUS_CONNECTION_FLAGS_ALL) == 0, NULL);
2973
0
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2974
0
  return g_initable_new (G_TYPE_DBUS_CONNECTION,
2975
0
                         cancellable,
2976
0
                         error,
2977
0
                         "address", address,
2978
0
                         "flags", flags,
2979
0
                         "authentication-observer", observer,
2980
0
                         NULL);
2981
0
}
2982
2983
/* ---------------------------------------------------------------------------------------------------- */
2984
2985
/**
2986
 * g_dbus_connection_set_exit_on_close:
2987
 * @connection: a #GDBusConnection
2988
 * @exit_on_close: whether the process should be terminated
2989
 *     when @connection is closed by the remote peer
2990
 *
2991
 * Sets whether the process should be terminated when @connection is
2992
 * closed by the remote peer. See #GDBusConnection:exit-on-close for
2993
 * more details.
2994
 *
2995
 * Note that this function should be used with care. Most modern UNIX
2996
 * desktops tie the notion of a user session with the session bus, and expect
2997
 * all of a user's applications to quit when their bus connection goes away.
2998
 * If you are setting @exit_on_close to %FALSE for the shared session
2999
 * bus connection, you should make sure that your application exits
3000
 * when the user session ends.
3001
 *
3002
 * Since: 2.26
3003
 */
3004
void
3005
g_dbus_connection_set_exit_on_close (GDBusConnection *connection,
3006
                                     gboolean         exit_on_close)
3007
0
{
3008
0
  g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
3009
3010
0
  if (exit_on_close)
3011
0
    g_atomic_int_or (&connection->atomic_flags, FLAG_EXIT_ON_CLOSE);
3012
0
  else
3013
0
    g_atomic_int_and (&connection->atomic_flags, ~FLAG_EXIT_ON_CLOSE);
3014
3015
0
}
3016
3017
/**
3018
 * g_dbus_connection_get_exit_on_close:
3019
 * @connection: a #GDBusConnection
3020
 *
3021
 * Gets whether the process is terminated when @connection is
3022
 * closed by the remote peer. See
3023
 * #GDBusConnection:exit-on-close for more details.
3024
 *
3025
 * Returns: whether the process is terminated when @connection is
3026
 *     closed by the remote peer
3027
 *
3028
 * Since: 2.26
3029
 */
3030
gboolean
3031
g_dbus_connection_get_exit_on_close (GDBusConnection *connection)
3032
0
{
3033
0
  g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
3034
3035
0
  if (g_atomic_int_get (&connection->atomic_flags) & FLAG_EXIT_ON_CLOSE)
3036
0
    return TRUE;
3037
0
  else
3038
0
    return FALSE;
3039
0
}
3040
3041
/**
3042
 * g_dbus_connection_get_guid:
3043
 * @connection: a #GDBusConnection
3044
 *
3045
 * The GUID of the peer performing the role of server when
3046
 * authenticating. See #GDBusConnection:guid for more details.
3047
 *
3048
 * Returns: (not nullable): The GUID. Do not free this string, it is owned by
3049
 *     @connection.
3050
 *
3051
 * Since: 2.26
3052
 */
3053
const gchar *
3054
g_dbus_connection_get_guid (GDBusConnection *connection)
3055
0
{
3056
0
  g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
3057
0
  return connection->guid;
3058
0
}
3059
3060
/**
3061
 * g_dbus_connection_get_unique_name:
3062
 * @connection: a #GDBusConnection
3063
 *
3064
 * Gets the unique name of @connection as assigned by the message
3065
 * bus. This can also be used to figure out if @connection is a
3066
 * message bus connection.
3067
 *
3068
 * Returns: (nullable): the unique name or %NULL if @connection is not a message
3069
 *     bus connection. Do not free this string, it is owned by
3070
 *     @connection.
3071
 *
3072
 * Since: 2.26
3073
 */
3074
const gchar *
3075
g_dbus_connection_get_unique_name (GDBusConnection *connection)
3076
0
{
3077
0
  g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
3078
3079
  /* do not use g_return_val_if_fail(), we want the memory barrier */
3080
0
  if (!check_initialized (connection))
3081
0
    return NULL;
3082
3083
0
  return connection->bus_unique_name;
3084
0
}
3085
3086
/**
3087
 * g_dbus_connection_get_peer_credentials:
3088
 * @connection: a #GDBusConnection
3089
 *
3090
 * Gets the credentials of the authenticated peer. This will always
3091
 * return %NULL unless @connection acted as a server
3092
 * (e.g. %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER was passed)
3093
 * when set up and the client passed credentials as part of the
3094
 * authentication process.
3095
 *
3096
 * In a message bus setup, the message bus is always the server and
3097
 * each application is a client. So this method will always return
3098
 * %NULL for message bus clients.
3099
 *
3100
 * Returns: (transfer none) (nullable): a #GCredentials or %NULL if not
3101
 *     available. Do not free this object, it is owned by @connection.
3102
 *
3103
 * Since: 2.26
3104
 */
3105
GCredentials *
3106
g_dbus_connection_get_peer_credentials (GDBusConnection *connection)
3107
0
{
3108
0
  g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
3109
3110
  /* do not use g_return_val_if_fail(), we want the memory barrier */
3111
0
  if (!check_initialized (connection))
3112
0
    return NULL;
3113
3114
0
  return connection->credentials;
3115
0
}
3116
3117
/* ---------------------------------------------------------------------------------------------------- */
3118
3119
static guint _global_filter_id = 1;  /* (atomic) */
3120
3121
/**
3122
 * g_dbus_connection_add_filter:
3123
 * @connection: a #GDBusConnection
3124
 * @filter_function: a filter function
3125
 * @user_data: user data to pass to @filter_function
3126
 * @user_data_free_func: function to free @user_data with when filter
3127
 *     is removed or %NULL
3128
 *
3129
 * Adds a message filter. Filters are handlers that are run on all
3130
 * incoming and outgoing messages, prior to standard dispatch. Filters
3131
 * are run in the order that they were added.  The same handler can be
3132
 * added as a filter more than once, in which case it will be run more
3133
 * than once.  Filters added during a filter callback won't be run on
3134
 * the message being processed. Filter functions are allowed to modify
3135
 * and even drop messages.
3136
 *
3137
 * Note that filters are run in a dedicated message handling thread so
3138
 * they can't block and, generally, can't do anything but signal a
3139
 * worker thread. Also note that filters are rarely needed - use API
3140
 * such as g_dbus_connection_send_message_with_reply(),
3141
 * g_dbus_connection_signal_subscribe() or g_dbus_connection_call() instead.
3142
 *
3143
 * If a filter consumes an incoming message the message is not
3144
 * dispatched anywhere else - not even the standard dispatch machinery
3145
 * (that API such as g_dbus_connection_signal_subscribe() and
3146
 * g_dbus_connection_send_message_with_reply() relies on) will see the
3147
 * message. Similarly, if a filter consumes an outgoing message, the
3148
 * message will not be sent to the other peer.
3149
 *
3150
 * If @user_data_free_func is non-%NULL, it will be called (in the
3151
 * thread-default main context of the thread you are calling this
3152
 * method from) at some point after @user_data is no longer
3153
 * needed. (It is not guaranteed to be called synchronously when the
3154
 * filter is removed, and may be called after @connection has been
3155
 * destroyed.)
3156
 *
3157
 * Returns: a filter identifier that can be used with
3158
 *     g_dbus_connection_remove_filter()
3159
 *
3160
 * Since: 2.26
3161
 */
3162
guint
3163
g_dbus_connection_add_filter (GDBusConnection            *connection,
3164
                              GDBusMessageFilterFunction  filter_function,
3165
                              gpointer                    user_data,
3166
                              GDestroyNotify              user_data_free_func)
3167
0
{
3168
0
  FilterData *data;
3169
3170
0
  g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), 0);
3171
0
  g_return_val_if_fail (filter_function != NULL, 0);
3172
0
  g_return_val_if_fail (check_initialized (connection), 0);
3173
3174
0
  CONNECTION_LOCK (connection);
3175
0
  data = g_new0 (FilterData, 1);
3176
0
  data->id = (guint) g_atomic_int_add (&_global_filter_id, 1); /* TODO: overflow etc. */
3177
0
  data->ref_count = 1;
3178
0
  data->filter_function = filter_function;
3179
0
  data->user_data = user_data;
3180
0
  data->user_data_free_func = user_data_free_func;
3181
0
  data->context = g_main_context_ref_thread_default ();
3182
0
  g_ptr_array_add (connection->filters, data);
3183
0
  CONNECTION_UNLOCK (connection);
3184
3185
0
  return data->id;
3186
0
}
3187
3188
/* only called from finalize(), removes all filters */
3189
static void
3190
purge_all_filters (GDBusConnection *connection)
3191
0
{
3192
0
  guint n;
3193
3194
0
  for (n = 0; n < connection->filters->len; n++)
3195
0
    filter_data_destroy (connection->filters->pdata[n], FALSE);
3196
0
}
3197
3198
/**
3199
 * g_dbus_connection_remove_filter:
3200
 * @connection: a #GDBusConnection
3201
 * @filter_id: an identifier obtained from g_dbus_connection_add_filter()
3202
 *
3203
 * Removes a filter.
3204
 *
3205
 * Note that since filters run in a different thread, there is a race
3206
 * condition where it is possible that the filter will be running even
3207
 * after calling g_dbus_connection_remove_filter(), so you cannot just
3208
 * free data that the filter might be using. Instead, you should pass
3209
 * a #GDestroyNotify to g_dbus_connection_add_filter(), which will be
3210
 * called when it is guaranteed that the data is no longer needed.
3211
 *
3212
 * Since: 2.26
3213
 */
3214
void
3215
g_dbus_connection_remove_filter (GDBusConnection *connection,
3216
                                 guint            filter_id)
3217
0
{
3218
0
  guint n;
3219
0
  gboolean found;
3220
0
  FilterData *to_destroy;
3221
3222
0
  g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
3223
0
  g_return_if_fail (check_initialized (connection));
3224
3225
0
  CONNECTION_LOCK (connection);
3226
0
  found = FALSE;
3227
0
  to_destroy = NULL;
3228
0
  for (n = 0; n < connection->filters->len; n++)
3229
0
    {
3230
0
      FilterData *data = connection->filters->pdata[n];
3231
0
      if (data->id == filter_id)
3232
0
        {
3233
0
          found = TRUE;
3234
0
          g_ptr_array_remove_index (connection->filters, n);
3235
0
          data->ref_count--;
3236
0
          if (data->ref_count == 0)
3237
0
            to_destroy = data;
3238
0
          break;
3239
0
        }
3240
0
    }
3241
0
  CONNECTION_UNLOCK (connection);
3242
3243
  /* do free without holding lock */
3244
0
  if (to_destroy != NULL)
3245
0
    filter_data_destroy (to_destroy, TRUE);
3246
0
  else if (!found)
3247
0
    {
3248
0
      g_warning ("g_dbus_connection_remove_filter: No filter found for filter_id %d", filter_id);
3249
0
    }
3250
0
}
3251
3252
/* ---------------------------------------------------------------------------------------------------- */
3253
3254
typedef struct
3255
{
3256
  gchar *rule;
3257
  gchar *sender;
3258
  gchar *sender_unique_name; /* if sender is unique or org.freedesktop.DBus, then that name... otherwise blank */
3259
  gchar *interface_name;
3260
  gchar *member;
3261
  gchar *object_path;
3262
  gchar *arg0;
3263
  GDBusSignalFlags flags;
3264
  GPtrArray *subscribers;  /* (owned) (element-type SignalSubscriber) */
3265
} SignalData;
3266
3267
static void
3268
signal_data_free (SignalData *signal_data)
3269
0
{
3270
0
  g_free (signal_data->rule);
3271
0
  g_free (signal_data->sender);
3272
0
  g_free (signal_data->sender_unique_name);
3273
0
  g_free (signal_data->interface_name);
3274
0
  g_free (signal_data->member);
3275
0
  g_free (signal_data->object_path);
3276
0
  g_free (signal_data->arg0);
3277
0
  g_ptr_array_unref (signal_data->subscribers);
3278
0
  g_free (signal_data);
3279
0
}
3280
3281
typedef struct
3282
{
3283
  /* All fields are immutable after construction. */
3284
  gatomicrefcount ref_count;
3285
  GDBusSignalCallback callback;
3286
  gpointer user_data;
3287
  GDestroyNotify user_data_free_func;
3288
  guint id;
3289
  GMainContext *context;
3290
} SignalSubscriber;
3291
3292
static SignalSubscriber *
3293
signal_subscriber_ref (SignalSubscriber *subscriber)
3294
0
{
3295
0
  g_atomic_ref_count_inc (&subscriber->ref_count);
3296
0
  return subscriber;
3297
0
}
3298
3299
static void
3300
signal_subscriber_unref (SignalSubscriber *subscriber)
3301
0
{
3302
0
  if (g_atomic_ref_count_dec (&subscriber->ref_count))
3303
0
    {
3304
      /* Destroy the user data. It doesn’t matter which thread
3305
       * signal_subscriber_unref() is called in (or whether it’s called with a
3306
       * lock held), as call_destroy_notify() always defers to the next
3307
       * #GMainContext iteration. */
3308
0
      call_destroy_notify (subscriber->context,
3309
0
                           subscriber->user_data_free_func,
3310
0
                           subscriber->user_data);
3311
3312
0
      g_main_context_unref (subscriber->context);
3313
0
      g_free (subscriber);
3314
0
    }
3315
0
}
3316
3317
static gchar *
3318
args_to_rule (const gchar      *sender,
3319
              const gchar      *interface_name,
3320
              const gchar      *member,
3321
              const gchar      *object_path,
3322
              const gchar      *arg0,
3323
              GDBusSignalFlags  flags)
3324
0
{
3325
0
  GString *rule;
3326
3327
0
  rule = g_string_new ("type='signal'");
3328
0
  if (flags & G_DBUS_SIGNAL_FLAGS_NO_MATCH_RULE)
3329
0
    g_string_prepend_c (rule, '-');
3330
0
  if (sender != NULL)
3331
0
    g_string_append_printf (rule, ",sender='%s'", sender);
3332
0
  if (interface_name != NULL)
3333
0
    g_string_append_printf (rule, ",interface='%s'", interface_name);
3334
0
  if (member != NULL)
3335
0
    g_string_append_printf (rule, ",member='%s'", member);
3336
0
  if (object_path != NULL)
3337
0
    g_string_append_printf (rule, ",path='%s'", object_path);
3338
3339
0
  if (arg0 != NULL)
3340
0
    {
3341
0
      if (flags & G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH)
3342
0
        g_string_append_printf (rule, ",arg0path='%s'", arg0);
3343
0
      else if (flags & G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_NAMESPACE)
3344
0
        g_string_append_printf (rule, ",arg0namespace='%s'", arg0);
3345
0
      else
3346
0
        g_string_append_printf (rule, ",arg0='%s'", arg0);
3347
0
    }
3348
3349
0
  return g_string_free (rule, FALSE);
3350
0
}
3351
3352
static guint _global_subscriber_id = 1;  /* (atomic) */
3353
static guint _global_registration_id = 1;  /* (atomic) */
3354
static guint _global_subtree_registration_id = 1;  /* (atomic) */
3355
3356
/* ---------------------------------------------------------------------------------------------------- */
3357
3358
/* Called in a user thread, lock is held */
3359
static void
3360
add_match_rule (GDBusConnection *connection,
3361
                const gchar     *match_rule)
3362
0
{
3363
0
  GError *error;
3364
0
  GDBusMessage *message;
3365
3366
0
  if (match_rule[0] == '-')
3367
0
    return;
3368
3369
0
  message = g_dbus_message_new_method_call ("org.freedesktop.DBus", /* name */
3370
0
                                            "/org/freedesktop/DBus", /* path */
3371
0
                                            "org.freedesktop.DBus", /* interface */
3372
0
                                            "AddMatch");
3373
0
  g_dbus_message_set_body (message, g_variant_new ("(s)", match_rule));
3374
0
  error = NULL;
3375
0
  if (!g_dbus_connection_send_message_unlocked (connection,
3376
0
                                                message,
3377
0
                                                G_DBUS_SEND_MESSAGE_FLAGS_NONE,
3378
0
                                                NULL,
3379
0
                                                &error))
3380
0
    {
3381
0
      g_critical ("Error while sending AddMatch() message: %s", error->message);
3382
0
      g_error_free (error);
3383
0
    }
3384
0
  g_object_unref (message);
3385
0
}
3386
3387
/* ---------------------------------------------------------------------------------------------------- */
3388
3389
/* Called in a user thread, lock is held */
3390
static void
3391
remove_match_rule (GDBusConnection *connection,
3392
                   const gchar     *match_rule)
3393
0
{
3394
0
  GError *error;
3395
0
  GDBusMessage *message;
3396
3397
0
  if (match_rule[0] == '-')
3398
0
    return;
3399
3400
0
  message = g_dbus_message_new_method_call ("org.freedesktop.DBus", /* name */
3401
0
                                            "/org/freedesktop/DBus", /* path */
3402
0
                                            "org.freedesktop.DBus", /* interface */
3403
0
                                            "RemoveMatch");
3404
0
  g_dbus_message_set_body (message, g_variant_new ("(s)", match_rule));
3405
3406
0
  error = NULL;
3407
0
  if (!g_dbus_connection_send_message_unlocked (connection,
3408
0
                                                message,
3409
0
                                                G_DBUS_SEND_MESSAGE_FLAGS_NONE,
3410
0
                                                NULL,
3411
0
                                                &error))
3412
0
    {
3413
      /* If we could get G_IO_ERROR_CLOSED here, it wouldn't be reasonable to
3414
       * critical; but we're holding the lock, and our caller checked whether
3415
       * we were already closed, so we can't get that error.
3416
       */
3417
0
      g_critical ("Error while sending RemoveMatch() message: %s", error->message);
3418
0
      g_error_free (error);
3419
0
    }
3420
0
  g_object_unref (message);
3421
0
}
3422
3423
/* ---------------------------------------------------------------------------------------------------- */
3424
3425
static gboolean
3426
is_signal_data_for_name_lost_or_acquired (SignalData *signal_data)
3427
0
{
3428
0
  return g_strcmp0 (signal_data->sender_unique_name, "org.freedesktop.DBus") == 0 &&
3429
0
         g_strcmp0 (signal_data->interface_name, "org.freedesktop.DBus") == 0 &&
3430
0
         g_strcmp0 (signal_data->object_path, "/org/freedesktop/DBus") == 0 &&
3431
0
         (g_strcmp0 (signal_data->member, "NameLost") == 0 ||
3432
0
          g_strcmp0 (signal_data->member, "NameAcquired") == 0);
3433
0
}
3434
3435
/* ---------------------------------------------------------------------------------------------------- */
3436
3437
/**
3438
 * g_dbus_connection_signal_subscribe:
3439
 * @connection: a #GDBusConnection
3440
 * @sender: (nullable): sender name to match on (unique or well-known name)
3441
 *     or %NULL to listen from all senders
3442
 * @interface_name: (nullable): D-Bus interface name to match on or %NULL to
3443
 *     match on all interfaces
3444
 * @member: (nullable): D-Bus signal name to match on or %NULL to match on
3445
 *     all signals
3446
 * @object_path: (nullable): object path to match on or %NULL to match on
3447
 *     all object paths
3448
 * @arg0: (nullable): contents of first string argument to match on or %NULL
3449
 *     to match on all kinds of arguments
3450
 * @flags: #GDBusSignalFlags describing how arg0 is used in subscribing to the
3451
 *     signal
3452
 * @callback: callback to invoke when there is a signal matching the requested data
3453
 * @user_data: user data to pass to @callback
3454
 * @user_data_free_func: (nullable): function to free @user_data with when
3455
 *     subscription is removed or %NULL
3456
 *
3457
 * Subscribes to signals on @connection and invokes @callback whenever
3458
 * the signal is received. Note that @callback will be invoked in the 
3459
 * [thread-default main context][g-main-context-push-thread-default]
3460
 * of the thread you are calling this method from.
3461
 *
3462
 * If @connection is not a message bus connection, @sender must be
3463
 * %NULL.
3464
 *
3465
 * If @sender is a well-known name note that @callback is invoked with
3466
 * the unique name for the owner of @sender, not the well-known name
3467
 * as one would expect. This is because the message bus rewrites the
3468
 * name. As such, to avoid certain race conditions, users should be
3469
 * tracking the name owner of the well-known name and use that when
3470
 * processing the received signal.
3471
 *
3472
 * If one of %G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_NAMESPACE or
3473
 * %G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH are given, @arg0 is
3474
 * interpreted as part of a namespace or path.  The first argument
3475
 * of a signal is matched against that part as specified by D-Bus.
3476
 *
3477
 * If @user_data_free_func is non-%NULL, it will be called (in the
3478
 * thread-default main context of the thread you are calling this
3479
 * method from) at some point after @user_data is no longer
3480
 * needed. (It is not guaranteed to be called synchronously when the
3481
 * signal is unsubscribed from, and may be called after @connection
3482
 * has been destroyed.)
3483
 *
3484
 * As @callback is potentially invoked in a different thread from where it’s
3485
 * emitted, it’s possible for this to happen after
3486
 * g_dbus_connection_signal_unsubscribe() has been called in another thread.
3487
 * Due to this, @user_data should have a strong reference which is freed with
3488
 * @user_data_free_func, rather than pointing to data whose lifecycle is tied
3489
 * to the signal subscription. For example, if a #GObject is used to store the
3490
 * subscription ID from g_dbus_connection_signal_subscribe(), a strong reference
3491
 * to that #GObject must be passed to @user_data, and g_object_unref() passed to
3492
 * @user_data_free_func. You are responsible for breaking the resulting
3493
 * reference count cycle by explicitly unsubscribing from the signal when
3494
 * dropping the last external reference to the #GObject. Alternatively, a weak
3495
 * reference may be used.
3496
 *
3497
 * It is guaranteed that if you unsubscribe from a signal using
3498
 * g_dbus_connection_signal_unsubscribe() from the same thread which made the
3499
 * corresponding g_dbus_connection_signal_subscribe() call, @callback will not
3500
 * be invoked after g_dbus_connection_signal_unsubscribe() returns.
3501
 *
3502
 * The returned subscription identifier is an opaque value which is guaranteed
3503
 * to never be zero.
3504
 *
3505
 * This function can never fail.
3506
 *
3507
 * Returns: a subscription identifier that can be used with g_dbus_connection_signal_unsubscribe()
3508
 *
3509
 * Since: 2.26
3510
 */
3511
guint
3512
g_dbus_connection_signal_subscribe (GDBusConnection     *connection,
3513
                                    const gchar         *sender,
3514
                                    const gchar         *interface_name,
3515
                                    const gchar         *member,
3516
                                    const gchar         *object_path,
3517
                                    const gchar         *arg0,
3518
                                    GDBusSignalFlags     flags,
3519
                                    GDBusSignalCallback  callback,
3520
                                    gpointer             user_data,
3521
                                    GDestroyNotify       user_data_free_func)
3522
0
{
3523
0
  gchar *rule;
3524
0
  SignalData *signal_data;
3525
0
  SignalSubscriber *subscriber;
3526
0
  GPtrArray *signal_data_array;
3527
0
  const gchar *sender_unique_name;
3528
3529
  /* Right now we abort if AddMatch() fails since it can only fail with the bus being in
3530
   * an OOM condition. We might want to change that but that would involve making
3531
   * g_dbus_connection_signal_subscribe() asynchronous and having the call sites
3532
   * handle that. And there's really no sensible way of handling this short of retrying
3533
   * to add the match rule... and then there's the little thing that, hey, maybe there's
3534
   * a reason the bus in an OOM condition.
3535
   *
3536
   * Doable, but not really sure it's worth it...
3537
   */
3538
3539
0
  g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), 0);
3540
0
  g_return_val_if_fail (sender == NULL || (g_dbus_is_name (sender) && (connection->flags & G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION)), 0);
3541
0
  g_return_val_if_fail (interface_name == NULL || g_dbus_is_interface_name (interface_name), 0);
3542
0
  g_return_val_if_fail (member == NULL || g_dbus_is_member_name (member), 0);
3543
0
  g_return_val_if_fail (object_path == NULL || g_variant_is_object_path (object_path), 0);
3544
0
  g_return_val_if_fail (callback != NULL, 0);
3545
0
  g_return_val_if_fail (check_initialized (connection), 0);
3546
0
  g_return_val_if_fail (!((flags & G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH) && (flags & G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_NAMESPACE)), 0);
3547
0
  g_return_val_if_fail (!(arg0 == NULL && (flags & (G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH | G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_NAMESPACE))), 0);
3548
3549
0
  CONNECTION_LOCK (connection);
3550
3551
  /* If G_DBUS_SIGNAL_FLAGS_NO_MATCH_RULE was specified, we will end up
3552
   * with a '-' character to prefix the rule (which will otherwise be
3553
   * normal).
3554
   *
3555
   * This allows us to hash the rule and do our lifecycle tracking in
3556
   * the usual way, but the '-' prevents the match rule from ever
3557
   * actually being send to the bus (either for add or remove).
3558
   */
3559
0
  rule = args_to_rule (sender, interface_name, member, object_path, arg0, flags);
3560
3561
0
  if (sender != NULL && (g_dbus_is_unique_name (sender) || g_strcmp0 (sender, "org.freedesktop.DBus") == 0))
3562
0
    sender_unique_name = sender;
3563
0
  else
3564
0
    sender_unique_name = "";
3565
3566
0
  subscriber = g_new0 (SignalSubscriber, 1);
3567
0
  subscriber->ref_count = 1;
3568
0
  subscriber->callback = callback;
3569
0
  subscriber->user_data = user_data;
3570
0
  subscriber->user_data_free_func = user_data_free_func;
3571
0
  subscriber->id = (guint) g_atomic_int_add (&_global_subscriber_id, 1); /* TODO: overflow etc. */
3572
0
  subscriber->context = g_main_context_ref_thread_default ();
3573
3574
  /* see if we've already have this rule */
3575
0
  signal_data = g_hash_table_lookup (connection->map_rule_to_signal_data, rule);
3576
0
  if (signal_data != NULL)
3577
0
    {
3578
0
      g_ptr_array_add (signal_data->subscribers, subscriber);
3579
0
      g_free (rule);
3580
0
      goto out;
3581
0
    }
3582
3583
0
  signal_data = g_new0 (SignalData, 1);
3584
0
  signal_data->rule                  = rule;
3585
0
  signal_data->sender                = g_strdup (sender);
3586
0
  signal_data->sender_unique_name    = g_strdup (sender_unique_name);
3587
0
  signal_data->interface_name        = g_strdup (interface_name);
3588
0
  signal_data->member                = g_strdup (member);
3589
0
  signal_data->object_path           = g_strdup (object_path);
3590
0
  signal_data->arg0                  = g_strdup (arg0);
3591
0
  signal_data->flags                 = flags;
3592
0
  signal_data->subscribers           = g_ptr_array_new_with_free_func ((GDestroyNotify) signal_subscriber_unref);
3593
0
  g_ptr_array_add (signal_data->subscribers, subscriber);
3594
3595
0
  g_hash_table_insert (connection->map_rule_to_signal_data,
3596
0
                       signal_data->rule,
3597
0
                       signal_data);
3598
3599
  /* Add the match rule to the bus...
3600
   *
3601
   * Avoid adding match rules for NameLost and NameAcquired messages - the bus will
3602
   * always send such messages to us.
3603
   */
3604
0
  if (connection->flags & G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION)
3605
0
    {
3606
0
      if (!is_signal_data_for_name_lost_or_acquired (signal_data))
3607
0
        add_match_rule (connection, signal_data->rule);
3608
0
    }
3609
3610
0
  signal_data_array = g_hash_table_lookup (connection->map_sender_unique_name_to_signal_data_array,
3611
0
                                           signal_data->sender_unique_name);
3612
0
  if (signal_data_array == NULL)
3613
0
    {
3614
0
      signal_data_array = g_ptr_array_new ();
3615
0
      g_hash_table_insert (connection->map_sender_unique_name_to_signal_data_array,
3616
0
                           g_strdup (signal_data->sender_unique_name),
3617
0
                           signal_data_array);
3618
0
    }
3619
0
  g_ptr_array_add (signal_data_array, signal_data);
3620
3621
0
 out:
3622
0
  g_hash_table_insert (connection->map_id_to_signal_data,
3623
0
                       GUINT_TO_POINTER (subscriber->id),
3624
0
                       signal_data);
3625
3626
0
  CONNECTION_UNLOCK (connection);
3627
3628
0
  return subscriber->id;
3629
0
}
3630
3631
/* ---------------------------------------------------------------------------------------------------- */
3632
3633
/* called in any thread */
3634
/* must hold lock when calling this (except if connection->finalizing is TRUE)
3635
 * returns the number of removed subscribers */
3636
static guint
3637
unsubscribe_id_internal (GDBusConnection *connection,
3638
                         guint            subscription_id)
3639
0
{
3640
0
  SignalData *signal_data;
3641
0
  GPtrArray *signal_data_array;
3642
0
  guint n;
3643
0
  guint n_removed = 0;
3644
3645
0
  signal_data = g_hash_table_lookup (connection->map_id_to_signal_data,
3646
0
                                     GUINT_TO_POINTER (subscription_id));
3647
0
  if (signal_data == NULL)
3648
0
    {
3649
      /* Don't warn here, we may have thrown all subscriptions out when the connection was closed */
3650
0
      goto out;
3651
0
    }
3652
3653
0
  for (n = 0; n < signal_data->subscribers->len; n++)
3654
0
    {
3655
0
      SignalSubscriber *subscriber = signal_data->subscribers->pdata[n];
3656
3657
0
      if (subscriber->id != subscription_id)
3658
0
        continue;
3659
3660
      /* It’s OK to rearrange the array order using the ā€˜fast’ #GPtrArray
3661
       * removal functions, since we’re going to exit the loop below anyway — we
3662
       * never move on to the next element. Secondly, subscription IDs are
3663
       * guaranteed to be unique. */
3664
0
      g_warn_if_fail (g_hash_table_remove (connection->map_id_to_signal_data,
3665
0
                                           GUINT_TO_POINTER (subscription_id)));
3666
0
      n_removed++;
3667
0
      g_ptr_array_remove_index_fast (signal_data->subscribers, n);
3668
3669
0
      if (signal_data->subscribers->len == 0)
3670
0
        {
3671
0
          g_warn_if_fail (g_hash_table_remove (connection->map_rule_to_signal_data, signal_data->rule));
3672
3673
0
          signal_data_array = g_hash_table_lookup (connection->map_sender_unique_name_to_signal_data_array,
3674
0
                                                   signal_data->sender_unique_name);
3675
0
          g_warn_if_fail (signal_data_array != NULL);
3676
0
          g_warn_if_fail (g_ptr_array_remove (signal_data_array, signal_data));
3677
3678
0
          if (signal_data_array->len == 0)
3679
0
            {
3680
0
              g_warn_if_fail (g_hash_table_remove (connection->map_sender_unique_name_to_signal_data_array,
3681
0
                                                   signal_data->sender_unique_name));
3682
0
            }
3683
3684
          /* remove the match rule from the bus unless NameLost or NameAcquired (see subscribe()) */
3685
0
          if ((connection->flags & G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION) &&
3686
0
              !is_signal_data_for_name_lost_or_acquired (signal_data) &&
3687
0
              !g_dbus_connection_is_closed (connection) &&
3688
0
              !connection->finalizing)
3689
0
            {
3690
              /* The check for g_dbus_connection_is_closed() means that
3691
               * sending the RemoveMatch message can't fail with
3692
               * G_IO_ERROR_CLOSED, because we're holding the lock,
3693
               * so on_worker_closed() can't happen between the check we just
3694
               * did, and releasing the lock later.
3695
               */
3696
0
              remove_match_rule (connection, signal_data->rule);
3697
0
            }
3698
3699
0
          signal_data_free (signal_data);
3700
0
        }
3701
3702
0
      goto out;
3703
0
    }
3704
3705
0
  g_assert_not_reached ();
3706
3707
0
 out:
3708
0
  return n_removed;
3709
0
}
3710
3711
/**
3712
 * g_dbus_connection_signal_unsubscribe:
3713
 * @connection: a #GDBusConnection
3714
 * @subscription_id: a subscription id obtained from
3715
 *     g_dbus_connection_signal_subscribe()
3716
 *
3717
 * Unsubscribes from signals.
3718
 *
3719
 * Note that there may still be D-Bus traffic to process (relating to this
3720
 * signal subscription) in the current thread-default #GMainContext after this
3721
 * function has returned. You should continue to iterate the #GMainContext
3722
 * until the #GDestroyNotify function passed to
3723
 * g_dbus_connection_signal_subscribe() is called, in order to avoid memory
3724
 * leaks through callbacks queued on the #GMainContext after it’s stopped being
3725
 * iterated.
3726
 * Alternatively, any idle source with a priority lower than %G_PRIORITY_DEFAULT
3727
 * that was scheduled after unsubscription, also indicates that all resources
3728
 * of this subscription are released.
3729
 *
3730
 * Since: 2.26
3731
 */
3732
void
3733
g_dbus_connection_signal_unsubscribe (GDBusConnection *connection,
3734
                                      guint            subscription_id)
3735
0
{
3736
0
  guint n_subscribers_removed G_GNUC_UNUSED  /* when compiling with G_DISABLE_ASSERT */;
3737
3738
0
  g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
3739
0
  g_return_if_fail (check_initialized (connection));
3740
3741
0
  CONNECTION_LOCK (connection);
3742
0
  n_subscribers_removed = unsubscribe_id_internal (connection, subscription_id);
3743
0
  CONNECTION_UNLOCK (connection);
3744
3745
  /* invariant */
3746
0
  g_assert (n_subscribers_removed == 0 || n_subscribers_removed == 1);
3747
0
}
3748
3749
/* ---------------------------------------------------------------------------------------------------- */
3750
3751
typedef struct
3752
{
3753
  SignalSubscriber    *subscriber;  /* (owned) */
3754
  GDBusMessage        *message;  /* (owned) */
3755
  GDBusConnection     *connection;
3756
  const gchar         *sender;  /* (nullable) for peer-to-peer connections */
3757
  const gchar         *path;
3758
  const gchar         *interface;
3759
  const gchar         *member;
3760
} SignalInstance;
3761
3762
/* called on delivery thread (e.g. where g_dbus_connection_signal_subscribe() was called) with
3763
 * no locks held
3764
 */
3765
static gboolean
3766
emit_signal_instance_in_idle_cb (gpointer data)
3767
0
{
3768
0
  SignalInstance *signal_instance = data;
3769
0
  GVariant *parameters;
3770
0
  gboolean has_subscription;
3771
3772
0
  parameters = g_dbus_message_get_body (signal_instance->message);
3773
0
  if (parameters == NULL)
3774
0
    {
3775
0
      parameters = g_variant_new ("()");
3776
0
      g_variant_ref_sink (parameters);
3777
0
    }
3778
0
  else
3779
0
    {
3780
0
      g_variant_ref_sink (parameters);
3781
0
    }
3782
3783
#if 0
3784
  g_print ("in emit_signal_instance_in_idle_cb (id=%d sender=%s path=%s interface=%s member=%s params=%s)\n",
3785
           signal_instance->subscriber->id,
3786
           signal_instance->sender,
3787
           signal_instance->path,
3788
           signal_instance->interface,
3789
           signal_instance->member,
3790
           g_variant_print (parameters, TRUE));
3791
#endif
3792
3793
  /* Careful here, don't do the callback if we no longer has the subscription */
3794
0
  CONNECTION_LOCK (signal_instance->connection);
3795
0
  has_subscription = FALSE;
3796
0
  if (g_hash_table_lookup (signal_instance->connection->map_id_to_signal_data,
3797
0
                           GUINT_TO_POINTER (signal_instance->subscriber->id)) != NULL)
3798
0
    has_subscription = TRUE;
3799
0
  CONNECTION_UNLOCK (signal_instance->connection);
3800
3801
0
  if (has_subscription)
3802
0
    signal_instance->subscriber->callback (signal_instance->connection,
3803
0
                                           signal_instance->sender,
3804
0
                                           signal_instance->path,
3805
0
                                           signal_instance->interface,
3806
0
                                           signal_instance->member,
3807
0
                                           parameters,
3808
0
                                           signal_instance->subscriber->user_data);
3809
3810
0
  g_variant_unref (parameters);
3811
3812
0
  return FALSE;
3813
0
}
3814
3815
static void
3816
signal_instance_free (SignalInstance *signal_instance)
3817
0
{
3818
0
  g_clear_object (&signal_instance->message);
3819
0
  g_object_unref (signal_instance->connection);
3820
0
  signal_subscriber_unref (signal_instance->subscriber);
3821
0
  g_free (signal_instance);
3822
0
}
3823
3824
static gboolean
3825
namespace_rule_matches (const gchar *namespace,
3826
                        const gchar *name)
3827
0
{
3828
0
  gint len_namespace;
3829
0
  gint len_name;
3830
3831
0
  len_namespace = strlen (namespace);
3832
0
  len_name = strlen (name);
3833
3834
0
  if (len_name < len_namespace)
3835
0
    return FALSE;
3836
3837
0
  if (memcmp (namespace, name, len_namespace) != 0)
3838
0
    return FALSE;
3839
3840
0
  return len_namespace == len_name || name[len_namespace] == '.';
3841
0
}
3842
3843
static gboolean
3844
path_rule_matches (const gchar *path_a,
3845
                   const gchar *path_b)
3846
0
{
3847
0
  gint len_a, len_b;
3848
3849
0
  len_a = strlen (path_a);
3850
0
  len_b = strlen (path_b);
3851
3852
0
  if (len_a < len_b && (len_a == 0 || path_a[len_a - 1] != '/'))
3853
0
    return FALSE;
3854
3855
0
  if (len_b < len_a && (len_b == 0 || path_b[len_b - 1] != '/'))
3856
0
    return FALSE;
3857
3858
0
  return memcmp (path_a, path_b, MIN (len_a, len_b)) == 0;
3859
0
}
3860
3861
/* called in GDBusWorker thread WITH lock held
3862
 *
3863
 * @sender is (nullable) for peer-to-peer connections */
3864
static void
3865
schedule_callbacks (GDBusConnection *connection,
3866
                    GPtrArray       *signal_data_array,
3867
                    GDBusMessage    *message,
3868
                    const gchar     *sender)
3869
0
{
3870
0
  guint n, m;
3871
0
  const gchar *interface;
3872
0
  const gchar *member;
3873
0
  const gchar *path;
3874
0
  const gchar *arg0;
3875
3876
0
  interface = NULL;
3877
0
  member = NULL;
3878
0
  path = NULL;
3879
0
  arg0 = NULL;
3880
3881
0
  interface = g_dbus_message_get_interface (message);
3882
0
  member = g_dbus_message_get_member (message);
3883
0
  path = g_dbus_message_get_path (message);
3884
0
  arg0 = g_dbus_message_get_arg0 (message);
3885
3886
#if 0
3887
  g_print ("In schedule_callbacks:\n"
3888
           "  sender    = '%s'\n"
3889
           "  interface = '%s'\n"
3890
           "  member    = '%s'\n"
3891
           "  path      = '%s'\n"
3892
           "  arg0      = '%s'\n",
3893
           sender,
3894
           interface,
3895
           member,
3896
           path,
3897
           arg0);
3898
#endif
3899
3900
  /* TODO: if this is slow, then we can change signal_data_array into
3901
   *       map_object_path_to_signal_data_array or something.
3902
   */
3903
0
  for (n = 0; n < signal_data_array->len; n++)
3904
0
    {
3905
0
      SignalData *signal_data = signal_data_array->pdata[n];
3906
3907
0
      if (signal_data->interface_name != NULL && g_strcmp0 (signal_data->interface_name, interface) != 0)
3908
0
        continue;
3909
3910
0
      if (signal_data->member != NULL && g_strcmp0 (signal_data->member, member) != 0)
3911
0
        continue;
3912
3913
0
      if (signal_data->object_path != NULL && g_strcmp0 (signal_data->object_path, path) != 0)
3914
0
        continue;
3915
3916
0
      if (signal_data->arg0 != NULL)
3917
0
        {
3918
0
          if (arg0 == NULL)
3919
0
            continue;
3920
3921
0
          if (signal_data->flags & G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_NAMESPACE)
3922
0
            {
3923
0
              if (!namespace_rule_matches (signal_data->arg0, arg0))
3924
0
                continue;
3925
0
            }
3926
0
          else if (signal_data->flags & G_DBUS_SIGNAL_FLAGS_MATCH_ARG0_PATH)
3927
0
            {
3928
0
              if (!path_rule_matches (signal_data->arg0, arg0))
3929
0
                continue;
3930
0
            }
3931
0
          else if (!g_str_equal (signal_data->arg0, arg0))
3932
0
            continue;
3933
0
        }
3934
3935
0
      for (m = 0; m < signal_data->subscribers->len; m++)
3936
0
        {
3937
0
          SignalSubscriber *subscriber = signal_data->subscribers->pdata[m];
3938
0
          GSource *idle_source;
3939
0
          SignalInstance *signal_instance;
3940
3941
0
          signal_instance = g_new0 (SignalInstance, 1);
3942
0
          signal_instance->subscriber = signal_subscriber_ref (subscriber);
3943
0
          signal_instance->message = g_object_ref (message);
3944
0
          signal_instance->connection = g_object_ref (connection);
3945
0
          signal_instance->sender = sender;
3946
0
          signal_instance->path = path;
3947
0
          signal_instance->interface = interface;
3948
0
          signal_instance->member = member;
3949
3950
0
          idle_source = g_idle_source_new ();
3951
0
          g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
3952
0
          g_source_set_callback (idle_source,
3953
0
                                 emit_signal_instance_in_idle_cb,
3954
0
                                 signal_instance,
3955
0
                                 (GDestroyNotify) signal_instance_free);
3956
0
          g_source_set_static_name (idle_source, "[gio] emit_signal_instance_in_idle_cb");
3957
0
          g_source_attach (idle_source, subscriber->context);
3958
0
          g_source_unref (idle_source);
3959
0
        }
3960
0
    }
3961
0
}
3962
3963
/* called in GDBusWorker thread with lock held */
3964
static void
3965
distribute_signals (GDBusConnection *connection,
3966
                    GDBusMessage    *message)
3967
0
{
3968
0
  GPtrArray *signal_data_array;
3969
0
  const gchar *sender, *interface, *member, *path;
3970
3971
0
  g_assert (g_dbus_message_get_message_type (message) == G_DBUS_MESSAGE_TYPE_SIGNAL);
3972
3973
0
  sender = g_dbus_message_get_sender (message);
3974
3975
  /* all three of these are required, but should have been validated already
3976
   * by validate_headers() in gdbusmessage.c */
3977
0
  interface = g_dbus_message_get_interface (message);
3978
0
  member = g_dbus_message_get_member (message);
3979
0
  path = g_dbus_message_get_path (message);
3980
3981
0
  g_assert (interface != NULL);
3982
0
  g_assert (member != NULL);
3983
0
  g_assert (path != NULL);
3984
3985
0
  if (G_UNLIKELY (_g_dbus_debug_signal ()))
3986
0
    {
3987
0
      _g_dbus_debug_print_lock ();
3988
0
      g_print ("========================================================================\n"
3989
0
               "GDBus-debug:Signal:\n"
3990
0
               " <<<< RECEIVED SIGNAL %s.%s\n"
3991
0
               "      on object %s\n"
3992
0
               "      sent by name %s\n",
3993
0
               interface, member, path,
3994
0
               sender != NULL ? sender : "(none)");
3995
0
      _g_dbus_debug_print_unlock ();
3996
0
    }
3997
3998
  /* collect subscribers that match on sender */
3999
0
  if (sender != NULL)
4000
0
    {
4001
0
      signal_data_array = g_hash_table_lookup (connection->map_sender_unique_name_to_signal_data_array, sender);
4002
0
      if (signal_data_array != NULL)
4003
0
        schedule_callbacks (connection, signal_data_array, message, sender);
4004
0
    }
4005
4006
  /* collect subscribers not matching on sender */
4007
0
  signal_data_array = g_hash_table_lookup (connection->map_sender_unique_name_to_signal_data_array, "");
4008
0
  if (signal_data_array != NULL)
4009
0
    schedule_callbacks (connection, signal_data_array, message, sender);
4010
0
}
4011
4012
/* ---------------------------------------------------------------------------------------------------- */
4013
4014
/* only called from finalize(), removes all subscriptions */
4015
static void
4016
purge_all_signal_subscriptions (GDBusConnection *connection)
4017
0
{
4018
0
  GHashTableIter iter;
4019
0
  gpointer key;
4020
0
  GArray *ids;
4021
0
  guint n;
4022
4023
0
  ids = g_array_new (FALSE, FALSE, sizeof (guint));
4024
0
  g_hash_table_iter_init (&iter, connection->map_id_to_signal_data);
4025
0
  while (g_hash_table_iter_next (&iter, &key, NULL))
4026
0
    {
4027
0
      guint subscription_id = GPOINTER_TO_UINT (key);
4028
0
      g_array_append_val (ids, subscription_id);
4029
0
    }
4030
4031
0
  for (n = 0; n < ids->len; n++)
4032
0
    {
4033
0
      guint subscription_id = g_array_index (ids, guint, n);
4034
0
      unsubscribe_id_internal (connection, subscription_id);
4035
0
    }
4036
0
  g_array_free (ids, TRUE);
4037
0
}
4038
4039
/* ---------------------------------------------------------------------------------------------------- */
4040
4041
static GDBusInterfaceVTable *
4042
_g_dbus_interface_vtable_copy (const GDBusInterfaceVTable *vtable)
4043
0
{
4044
  /* Don't waste memory by copying padding - remember to update this
4045
   * when changing struct _GDBusInterfaceVTable in gdbusconnection.h
4046
   */
4047
0
  return g_memdup2 ((gconstpointer) vtable, 3 * sizeof (gpointer));
4048
0
}
4049
4050
static void
4051
_g_dbus_interface_vtable_free (GDBusInterfaceVTable *vtable)
4052
0
{
4053
0
  g_free (vtable);
4054
0
}
4055
4056
/* ---------------------------------------------------------------------------------------------------- */
4057
4058
static GDBusSubtreeVTable *
4059
_g_dbus_subtree_vtable_copy (const GDBusSubtreeVTable *vtable)
4060
0
{
4061
  /* Don't waste memory by copying padding - remember to update this
4062
   * when changing struct _GDBusSubtreeVTable in gdbusconnection.h
4063
   */
4064
0
  return g_memdup2 ((gconstpointer) vtable, 3 * sizeof (gpointer));
4065
0
}
4066
4067
static void
4068
_g_dbus_subtree_vtable_free (GDBusSubtreeVTable *vtable)
4069
0
{
4070
0
  g_free (vtable);
4071
0
}
4072
4073
/* ---------------------------------------------------------------------------------------------------- */
4074
4075
struct ExportedObject
4076
{
4077
  gchar *object_path;
4078
  GDBusConnection *connection;
4079
4080
  /* maps gchar* -> ExportedInterface* */
4081
  GHashTable *map_if_name_to_ei;
4082
};
4083
4084
/* only called with lock held */
4085
static void
4086
exported_object_free (ExportedObject *eo)
4087
0
{
4088
0
  g_free (eo->object_path);
4089
0
  g_hash_table_unref (eo->map_if_name_to_ei);
4090
0
  g_free (eo);
4091
0
}
4092
4093
typedef struct
4094
{
4095
  ExportedObject *eo;
4096
4097
  gint                        refcount;  /* (atomic) */
4098
4099
  guint                       id;
4100
  gchar                      *interface_name;  /* (owned) */
4101
  GDBusInterfaceVTable       *vtable;  /* (owned) */
4102
  GDBusInterfaceInfo         *interface_info;  /* (owned) */
4103
4104
  GMainContext               *context;  /* (owned) */
4105
  gpointer                    user_data;
4106
  GDestroyNotify              user_data_free_func;
4107
} ExportedInterface;
4108
4109
static ExportedInterface *
4110
exported_interface_ref (ExportedInterface *ei)
4111
0
{
4112
0
  g_atomic_int_inc (&ei->refcount);
4113
4114
0
  return ei;
4115
0
}
4116
4117
/* May be called with lock held */
4118
static void
4119
exported_interface_unref (ExportedInterface *ei)
4120
0
{
4121
0
  if (!g_atomic_int_dec_and_test (&ei->refcount))
4122
0
    return;
4123
4124
0
  g_dbus_interface_info_cache_release (ei->interface_info);
4125
0
  g_dbus_interface_info_unref ((GDBusInterfaceInfo *) ei->interface_info);
4126
4127
  /* All uses of ei->vtable from callbacks scheduled in idle functions must
4128
   * have completed by this call_destroy_notify() call, as language bindings
4129
   * may destroy function closures in this callback. */
4130
0
  call_destroy_notify (ei->context,
4131
0
                       ei->user_data_free_func,
4132
0
                       ei->user_data);
4133
4134
0
  g_main_context_unref (ei->context);
4135
4136
0
  g_free (ei->interface_name);
4137
0
  _g_dbus_interface_vtable_free (ei->vtable);
4138
0
  g_free (ei);
4139
0
}
4140
4141
struct ExportedSubtree
4142
{
4143
  gint                      refcount;  /* (atomic) */
4144
4145
  guint                     id;
4146
  gchar                    *object_path;  /* (owned) */
4147
  GDBusConnection          *connection;  /* (unowned) */
4148
  GDBusSubtreeVTable       *vtable;  /* (owned) */
4149
  GDBusSubtreeFlags         flags;
4150
4151
  GMainContext             *context;  /* (owned) */
4152
  gpointer                  user_data;
4153
  GDestroyNotify            user_data_free_func;
4154
};
4155
4156
static ExportedSubtree *
4157
exported_subtree_ref (ExportedSubtree *es)
4158
0
{
4159
0
  g_atomic_int_inc (&es->refcount);
4160
4161
0
  return es;
4162
0
}
4163
4164
/* May be called with lock held */
4165
static void
4166
exported_subtree_unref (ExportedSubtree *es)
4167
0
{
4168
0
  if (!g_atomic_int_dec_and_test (&es->refcount))
4169
0
    return;
4170
4171
  /* All uses of es->vtable from callbacks scheduled in idle functions must
4172
   * have completed by this call_destroy_notify() call, as language bindings
4173
   * may destroy function closures in this callback. */
4174
0
  call_destroy_notify (es->context,
4175
0
                       es->user_data_free_func,
4176
0
                       es->user_data);
4177
4178
0
  g_main_context_unref (es->context);
4179
4180
0
  _g_dbus_subtree_vtable_free (es->vtable);
4181
0
  g_free (es->object_path);
4182
0
  g_free (es);
4183
0
}
4184
4185
/* ---------------------------------------------------------------------------------------------------- */
4186
4187
/* Convenience function to check if @registration_id (if not zero) or
4188
 * @subtree_registration_id (if not zero) has been unregistered. If
4189
 * so, returns %TRUE.
4190
 *
4191
 * If not, sets @out_ei and/or @out_es to a strong reference to the relevant
4192
 * #ExportedInterface/#ExportedSubtree and returns %FALSE.
4193
 *
4194
 * May be called by any thread. Caller must *not* hold lock.
4195
 */
4196
static gboolean
4197
has_object_been_unregistered (GDBusConnection    *connection,
4198
                              guint               registration_id,
4199
                              ExportedInterface **out_ei,
4200
                              guint               subtree_registration_id,
4201
                              ExportedSubtree   **out_es)
4202
0
{
4203
0
  gboolean ret;
4204
0
  ExportedInterface *ei = NULL;
4205
0
  gpointer es = NULL;
4206
4207
0
  g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
4208
4209
0
  ret = FALSE;
4210
4211
0
  CONNECTION_LOCK (connection);
4212
4213
0
  if (registration_id != 0)
4214
0
    {
4215
0
      ei = g_hash_table_lookup (connection->map_id_to_ei, GUINT_TO_POINTER (registration_id));
4216
0
      if (ei == NULL)
4217
0
        ret = TRUE;
4218
0
      else if (out_ei != NULL)
4219
0
        *out_ei = exported_interface_ref (ei);
4220
0
    }
4221
0
  if (subtree_registration_id != 0)
4222
0
    {
4223
0
      es = g_hash_table_lookup (connection->map_id_to_es, GUINT_TO_POINTER (subtree_registration_id));
4224
0
      if (es == NULL)
4225
0
        ret = TRUE;
4226
0
      else if (out_es != NULL)
4227
0
        *out_es = exported_subtree_ref (es);
4228
0
    }
4229
4230
0
  CONNECTION_UNLOCK (connection);
4231
4232
0
  return ret;
4233
0
}
4234
4235
/* ---------------------------------------------------------------------------------------------------- */
4236
4237
typedef struct
4238
{
4239
  GDBusConnection *connection;
4240
  GDBusMessage *message;  /* (owned) */
4241
  gpointer user_data;
4242
  const gchar *property_name;
4243
  const GDBusInterfaceVTable *vtable;
4244
  GDBusInterfaceInfo *interface_info;
4245
  const GDBusPropertyInfo *property_info;
4246
  guint registration_id;
4247
  guint subtree_registration_id;
4248
} PropertyData;
4249
4250
static void
4251
property_data_free (PropertyData *data)
4252
0
{
4253
0
  g_object_unref (data->connection);
4254
0
  g_clear_object (&data->message);
4255
0
  g_free (data);
4256
0
}
4257
4258
/* called in thread where object was registered - no locks held */
4259
static gboolean
4260
invoke_get_property_in_idle_cb (gpointer _data)
4261
0
{
4262
0
  PropertyData *data = _data;
4263
0
  GVariant *value;
4264
0
  GError *error;
4265
0
  GDBusMessage *reply;
4266
0
  ExportedInterface *ei = NULL;
4267
0
  ExportedSubtree *es = NULL;
4268
4269
0
  if (has_object_been_unregistered (data->connection,
4270
0
                                    data->registration_id,
4271
0
                                    &ei,
4272
0
                                    data->subtree_registration_id,
4273
0
                                    &es))
4274
0
    {
4275
0
      reply = g_dbus_message_new_method_error (data->message,
4276
0
                                               "org.freedesktop.DBus.Error.UnknownMethod",
4277
0
                                               _("No such interface ā€œorg.freedesktop.DBus.Propertiesā€ on object at path %s"),
4278
0
                                               g_dbus_message_get_path (data->message));
4279
0
      g_dbus_connection_send_message (data->connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
4280
0
      g_object_unref (reply);
4281
0
      goto out;
4282
0
    }
4283
4284
0
  error = NULL;
4285
0
  value = data->vtable->get_property (data->connection,
4286
0
                                      g_dbus_message_get_sender (data->message),
4287
0
                                      g_dbus_message_get_path (data->message),
4288
0
                                      data->interface_info->name,
4289
0
                                      data->property_name,
4290
0
                                      &error,
4291
0
                                      data->user_data);
4292
4293
4294
0
  if (value != NULL)
4295
0
    {
4296
0
      g_assert_no_error (error);
4297
4298
0
      g_variant_take_ref (value);
4299
0
      reply = g_dbus_message_new_method_reply (data->message);
4300
0
      g_dbus_message_set_body (reply, g_variant_new ("(v)", value));
4301
0
      g_dbus_connection_send_message (data->connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
4302
0
      g_variant_unref (value);
4303
0
      g_object_unref (reply);
4304
0
    }
4305
0
  else
4306
0
    {
4307
0
      gchar *dbus_error_name;
4308
0
      g_assert (error != NULL);
4309
0
      dbus_error_name = g_dbus_error_encode_gerror (error);
4310
0
      reply = g_dbus_message_new_method_error_literal (data->message,
4311
0
                                                       dbus_error_name,
4312
0
                                                       error->message);
4313
0
      g_dbus_connection_send_message (data->connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
4314
0
      g_free (dbus_error_name);
4315
0
      g_error_free (error);
4316
0
      g_object_unref (reply);
4317
0
    }
4318
4319
0
 out:
4320
0
  g_clear_pointer (&ei, exported_interface_unref);
4321
0
  g_clear_pointer (&es, exported_subtree_unref);
4322
4323
0
  return FALSE;
4324
0
}
4325
4326
/* called in thread where object was registered - no locks held */
4327
static gboolean
4328
invoke_set_property_in_idle_cb (gpointer _data)
4329
0
{
4330
0
  PropertyData *data = _data;
4331
0
  GError *error;
4332
0
  GDBusMessage *reply;
4333
0
  GVariant *value;
4334
4335
0
  error = NULL;
4336
0
  value = NULL;
4337
4338
0
  g_variant_get (g_dbus_message_get_body (data->message),
4339
0
                 "(ssv)",
4340
0
                 NULL,
4341
0
                 NULL,
4342
0
                 &value);
4343
4344
0
  if (!data->vtable->set_property (data->connection,
4345
0
                                   g_dbus_message_get_sender (data->message),
4346
0
                                   g_dbus_message_get_path (data->message),
4347
0
                                   data->interface_info->name,
4348
0
                                   data->property_name,
4349
0
                                   value,
4350
0
                                   &error,
4351
0
                                   data->user_data))
4352
0
    {
4353
0
      gchar *dbus_error_name;
4354
0
      g_assert (error != NULL);
4355
0
      dbus_error_name = g_dbus_error_encode_gerror (error);
4356
0
      reply = g_dbus_message_new_method_error_literal (data->message,
4357
0
                                                       dbus_error_name,
4358
0
                                                       error->message);
4359
0
      g_free (dbus_error_name);
4360
0
      g_error_free (error);
4361
0
    }
4362
0
  else
4363
0
    {
4364
0
      reply = g_dbus_message_new_method_reply (data->message);
4365
0
    }
4366
4367
0
  g_assert (reply != NULL);
4368
0
  g_dbus_connection_send_message (data->connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
4369
0
  g_object_unref (reply);
4370
0
  g_variant_unref (value);
4371
4372
0
  return FALSE;
4373
0
}
4374
4375
/* called in any thread with connection's lock held */
4376
static gboolean
4377
validate_and_maybe_schedule_property_getset (GDBusConnection            *connection,
4378
                                             GDBusMessage               *message,
4379
                                             guint                       registration_id,
4380
                                             guint                       subtree_registration_id,
4381
                                             gboolean                    is_get,
4382
                                             GDBusInterfaceInfo         *interface_info,
4383
                                             const GDBusInterfaceVTable *vtable,
4384
                                             GMainContext               *main_context,
4385
                                             gpointer                    user_data)
4386
0
{
4387
0
  gboolean handled;
4388
0
  const char *interface_name;
4389
0
  const char *property_name;
4390
0
  const GDBusPropertyInfo *property_info;
4391
0
  GSource *idle_source;
4392
0
  PropertyData *property_data;
4393
0
  GDBusMessage *reply;
4394
4395
0
  handled = FALSE;
4396
4397
0
  if (is_get)
4398
0
    g_variant_get (g_dbus_message_get_body (message),
4399
0
                   "(&s&s)",
4400
0
                   &interface_name,
4401
0
                   &property_name);
4402
0
  else
4403
0
    g_variant_get (g_dbus_message_get_body (message),
4404
0
                   "(&s&sv)",
4405
0
                   &interface_name,
4406
0
                   &property_name,
4407
0
                   NULL);
4408
4409
0
  if (vtable == NULL)
4410
0
    goto out;
4411
4412
  /* Check that the property exists - if not fail with org.freedesktop.DBus.Error.InvalidArgs
4413
   */
4414
0
  property_info = NULL;
4415
4416
  /* TODO: the cost of this is O(n) - it might be worth caching the result */
4417
0
  property_info = g_dbus_interface_info_lookup_property (interface_info, property_name);
4418
0
  if (property_info == NULL)
4419
0
    {
4420
0
      reply = g_dbus_message_new_method_error (message,
4421
0
                                               "org.freedesktop.DBus.Error.InvalidArgs",
4422
0
                                               _("No such property ā€œ%sā€"),
4423
0
                                               property_name);
4424
0
      g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
4425
0
      g_object_unref (reply);
4426
0
      handled = TRUE;
4427
0
      goto out;
4428
0
    }
4429
4430
0
  if (is_get && !(property_info->flags & G_DBUS_PROPERTY_INFO_FLAGS_READABLE))
4431
0
    {
4432
0
      reply = g_dbus_message_new_method_error (message,
4433
0
                                               "org.freedesktop.DBus.Error.InvalidArgs",
4434
0
                                               _("Property ā€œ%sā€ is not readable"),
4435
0
                                               property_name);
4436
0
      g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
4437
0
      g_object_unref (reply);
4438
0
      handled = TRUE;
4439
0
      goto out;
4440
0
    }
4441
0
  else if (!is_get && !(property_info->flags & G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE))
4442
0
    {
4443
0
      reply = g_dbus_message_new_method_error (message,
4444
0
                                               "org.freedesktop.DBus.Error.InvalidArgs",
4445
0
                                               _("Property ā€œ%sā€ is not writable"),
4446
0
                                               property_name);
4447
0
      g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
4448
0
      g_object_unref (reply);
4449
0
      handled = TRUE;
4450
0
      goto out;
4451
0
    }
4452
4453
0
  if (!is_get)
4454
0
    {
4455
0
      GVariant *value;
4456
4457
      /* Fail with org.freedesktop.DBus.Error.InvalidArgs if the type
4458
       * of the given value is wrong
4459
       */
4460
0
      g_variant_get_child (g_dbus_message_get_body (message), 2, "v", &value);
4461
0
      if (g_strcmp0 (g_variant_get_type_string (value), property_info->signature) != 0)
4462
0
        {
4463
0
          reply = g_dbus_message_new_method_error (message,
4464
0
                                                   "org.freedesktop.DBus.Error.InvalidArgs",
4465
0
                                                   _("Error setting property ā€œ%sā€: Expected type ā€œ%sā€ but got ā€œ%sā€"),
4466
0
                                                   property_name, property_info->signature,
4467
0
                                                   g_variant_get_type_string (value));
4468
0
          g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
4469
0
          g_variant_unref (value);
4470
0
          g_object_unref (reply);
4471
0
          handled = TRUE;
4472
0
          goto out;
4473
0
        }
4474
4475
0
      g_variant_unref (value);
4476
0
    }
4477
4478
  /* If the vtable pointer for get_property() resp. set_property() is
4479
   * NULL then dispatch the call via the method_call() handler.
4480
   */
4481
0
  if (is_get)
4482
0
    {
4483
0
      if (vtable->get_property == NULL)
4484
0
        {
4485
0
          schedule_method_call (connection, message, registration_id, subtree_registration_id,
4486
0
                                interface_info, NULL, property_info, g_dbus_message_get_body (message),
4487
0
                                vtable, main_context, user_data);
4488
0
          handled = TRUE;
4489
0
          goto out;
4490
0
        }
4491
0
    }
4492
0
  else
4493
0
    {
4494
0
      if (vtable->set_property == NULL)
4495
0
        {
4496
0
          schedule_method_call (connection, message, registration_id, subtree_registration_id,
4497
0
                                interface_info, NULL, property_info, g_dbus_message_get_body (message),
4498
0
                                vtable, main_context, user_data);
4499
0
          handled = TRUE;
4500
0
          goto out;
4501
0
        }
4502
0
    }
4503
4504
  /* ok, got the property info - call user code in an idle handler */
4505
0
  property_data = g_new0 (PropertyData, 1);
4506
0
  property_data->connection = g_object_ref (connection);
4507
0
  property_data->message = g_object_ref (message);
4508
0
  property_data->user_data = user_data;
4509
0
  property_data->property_name = property_name;
4510
0
  property_data->vtable = vtable;
4511
0
  property_data->interface_info = interface_info;
4512
0
  property_data->property_info = property_info;
4513
0
  property_data->registration_id = registration_id;
4514
0
  property_data->subtree_registration_id = subtree_registration_id;
4515
4516
0
  idle_source = g_idle_source_new ();
4517
0
  g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
4518
0
  g_source_set_callback (idle_source,
4519
0
                         is_get ? invoke_get_property_in_idle_cb : invoke_set_property_in_idle_cb,
4520
0
                         property_data,
4521
0
                         (GDestroyNotify) property_data_free);
4522
0
  if (is_get)
4523
0
    g_source_set_static_name (idle_source, "[gio] invoke_get_property_in_idle_cb");
4524
0
  else
4525
0
    g_source_set_static_name (idle_source, "[gio] invoke_set_property_in_idle_cb");
4526
0
  g_source_attach (idle_source, main_context);
4527
0
  g_source_unref (idle_source);
4528
4529
0
  handled = TRUE;
4530
4531
0
 out:
4532
0
  return handled;
4533
0
}
4534
4535
/* called in GDBusWorker thread with connection's lock held */
4536
static gboolean
4537
handle_getset_property (GDBusConnection *connection,
4538
                        ExportedObject  *eo,
4539
                        GDBusMessage    *message,
4540
                        gboolean         is_get)
4541
0
{
4542
0
  ExportedInterface *ei;
4543
0
  gboolean handled;
4544
0
  const char *interface_name;
4545
0
  const char *property_name;
4546
4547
0
  handled = FALSE;
4548
4549
0
  if (is_get)
4550
0
    g_variant_get (g_dbus_message_get_body (message),
4551
0
                   "(&s&s)",
4552
0
                   &interface_name,
4553
0
                   &property_name);
4554
0
  else
4555
0
    g_variant_get (g_dbus_message_get_body (message),
4556
0
                   "(&s&sv)",
4557
0
                   &interface_name,
4558
0
                   &property_name,
4559
0
                   NULL);
4560
4561
  /* Fail with org.freedesktop.DBus.Error.InvalidArgs if there is
4562
   * no such interface registered
4563
   */
4564
0
  ei = g_hash_table_lookup (eo->map_if_name_to_ei, interface_name);
4565
0
  if (ei == NULL)
4566
0
    {
4567
0
      GDBusMessage *reply;
4568
0
      reply = g_dbus_message_new_method_error (message,
4569
0
                                               "org.freedesktop.DBus.Error.InvalidArgs",
4570
0
                                               _("No such interface ā€œ%sā€"),
4571
0
                                               interface_name);
4572
0
      g_dbus_connection_send_message_unlocked (eo->connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
4573
0
      g_object_unref (reply);
4574
0
      handled = TRUE;
4575
0
      goto out;
4576
0
    }
4577
4578
0
  handled = validate_and_maybe_schedule_property_getset (eo->connection,
4579
0
                                                         message,
4580
0
                                                         ei->id,
4581
0
                                                         0,
4582
0
                                                         is_get,
4583
0
                                                         ei->interface_info,
4584
0
                                                         ei->vtable,
4585
0
                                                         ei->context,
4586
0
                                                         ei->user_data);
4587
0
 out:
4588
0
  return handled;
4589
0
}
4590
4591
/* ---------------------------------------------------------------------------------------------------- */
4592
4593
typedef struct
4594
{
4595
  GDBusConnection *connection;
4596
  GDBusMessage *message;  /* (owned) */
4597
  gpointer user_data;
4598
  const GDBusInterfaceVTable *vtable;
4599
  GDBusInterfaceInfo *interface_info;
4600
  guint registration_id;
4601
  guint subtree_registration_id;
4602
} PropertyGetAllData;
4603
4604
static void
4605
property_get_all_data_free (PropertyGetAllData *data)
4606
0
{
4607
0
  g_object_unref (data->connection);
4608
0
  g_clear_object (&data->message);
4609
0
  g_free (data);
4610
0
}
4611
4612
/* called in thread where object was registered - no locks held */
4613
static gboolean
4614
invoke_get_all_properties_in_idle_cb (gpointer _data)
4615
0
{
4616
0
  PropertyGetAllData *data = _data;
4617
0
  GVariantBuilder builder;
4618
0
  GDBusMessage *reply;
4619
0
  guint n;
4620
0
  ExportedInterface *ei = NULL;
4621
0
  ExportedSubtree *es = NULL;
4622
4623
0
  if (has_object_been_unregistered (data->connection,
4624
0
                                    data->registration_id,
4625
0
                                    &ei,
4626
0
                                    data->subtree_registration_id,
4627
0
                                    &es))
4628
0
    {
4629
0
      reply = g_dbus_message_new_method_error (data->message,
4630
0
                                               "org.freedesktop.DBus.Error.UnknownMethod",
4631
0
                                               _("No such interface ā€œorg.freedesktop.DBus.Propertiesā€ on object at path %s"),
4632
0
                                               g_dbus_message_get_path (data->message));
4633
0
      g_dbus_connection_send_message (data->connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
4634
0
      g_object_unref (reply);
4635
0
      goto out;
4636
0
    }
4637
4638
  /* TODO: Right now we never fail this call - we just omit values if
4639
   *       a get_property() call is failing.
4640
   *
4641
   *       We could fail the whole call if just a single get_property() call
4642
   *       returns an error. We need clarification in the D-Bus spec about this.
4643
   */
4644
0
  g_variant_builder_init (&builder, G_VARIANT_TYPE ("(a{sv})"));
4645
0
  g_variant_builder_open (&builder, G_VARIANT_TYPE ("a{sv}"));
4646
0
  for (n = 0; data->interface_info->properties != NULL && data->interface_info->properties[n] != NULL; n++)
4647
0
    {
4648
0
      const GDBusPropertyInfo *property_info = data->interface_info->properties[n];
4649
0
      GVariant *value;
4650
4651
0
      if (!(property_info->flags & G_DBUS_PROPERTY_INFO_FLAGS_READABLE))
4652
0
        continue;
4653
4654
0
      value = data->vtable->get_property (data->connection,
4655
0
                                          g_dbus_message_get_sender (data->message),
4656
0
                                          g_dbus_message_get_path (data->message),
4657
0
                                          data->interface_info->name,
4658
0
                                          property_info->name,
4659
0
                                          NULL,
4660
0
                                          data->user_data);
4661
4662
0
      if (value == NULL)
4663
0
        continue;
4664
4665
0
      g_variant_take_ref (value);
4666
0
      g_variant_builder_add (&builder,
4667
0
                             "{sv}",
4668
0
                             property_info->name,
4669
0
                             value);
4670
0
      g_variant_unref (value);
4671
0
    }
4672
0
  g_variant_builder_close (&builder);
4673
4674
0
  reply = g_dbus_message_new_method_reply (data->message);
4675
0
  g_dbus_message_set_body (reply, g_variant_builder_end (&builder));
4676
0
  g_dbus_connection_send_message (data->connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
4677
0
  g_object_unref (reply);
4678
4679
0
 out:
4680
0
  g_clear_pointer (&ei, exported_interface_unref);
4681
0
  g_clear_pointer (&es, exported_subtree_unref);
4682
4683
0
  return FALSE;
4684
0
}
4685
4686
static gboolean
4687
interface_has_readable_properties (GDBusInterfaceInfo *interface_info)
4688
0
{
4689
0
  gint i;
4690
4691
0
  if (!interface_info->properties)
4692
0
    return FALSE;
4693
4694
0
  for (i = 0; interface_info->properties[i]; i++)
4695
0
    if (interface_info->properties[i]->flags & G_DBUS_PROPERTY_INFO_FLAGS_READABLE)
4696
0
      return TRUE;
4697
4698
0
  return FALSE;
4699
0
}
4700
4701
/* called in any thread with connection's lock held */
4702
static gboolean
4703
validate_and_maybe_schedule_property_get_all (GDBusConnection            *connection,
4704
                                              GDBusMessage               *message,
4705
                                              guint                       registration_id,
4706
                                              guint                       subtree_registration_id,
4707
                                              GDBusInterfaceInfo         *interface_info,
4708
                                              const GDBusInterfaceVTable *vtable,
4709
                                              GMainContext               *main_context,
4710
                                              gpointer                    user_data)
4711
0
{
4712
0
  gboolean handled;
4713
0
  GSource *idle_source;
4714
0
  PropertyGetAllData *property_get_all_data;
4715
4716
0
  handled = FALSE;
4717
4718
0
  if (vtable == NULL)
4719
0
    goto out;
4720
4721
  /* If the vtable pointer for get_property() is NULL but we have a
4722
   * non-zero number of readable properties, then dispatch the call via
4723
   * the method_call() handler.
4724
   */
4725
0
  if (vtable->get_property == NULL && interface_has_readable_properties (interface_info))
4726
0
    {
4727
0
      schedule_method_call (connection, message, registration_id, subtree_registration_id,
4728
0
                            interface_info, NULL, NULL, g_dbus_message_get_body (message),
4729
0
                            vtable, main_context, user_data);
4730
0
      handled = TRUE;
4731
0
      goto out;
4732
0
    }
4733
4734
  /* ok, got the property info - call user in an idle handler */
4735
0
  property_get_all_data = g_new0 (PropertyGetAllData, 1);
4736
0
  property_get_all_data->connection = g_object_ref (connection);
4737
0
  property_get_all_data->message = g_object_ref (message);
4738
0
  property_get_all_data->user_data = user_data;
4739
0
  property_get_all_data->vtable = vtable;
4740
0
  property_get_all_data->interface_info = interface_info;
4741
0
  property_get_all_data->registration_id = registration_id;
4742
0
  property_get_all_data->subtree_registration_id = subtree_registration_id;
4743
4744
0
  idle_source = g_idle_source_new ();
4745
0
  g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
4746
0
  g_source_set_callback (idle_source,
4747
0
                         invoke_get_all_properties_in_idle_cb,
4748
0
                         property_get_all_data,
4749
0
                         (GDestroyNotify) property_get_all_data_free);
4750
0
  g_source_set_static_name (idle_source, "[gio] invoke_get_all_properties_in_idle_cb");
4751
0
  g_source_attach (idle_source, main_context);
4752
0
  g_source_unref (idle_source);
4753
4754
0
  handled = TRUE;
4755
4756
0
 out:
4757
0
  return handled;
4758
0
}
4759
4760
/* called in GDBusWorker thread with connection's lock held */
4761
static gboolean
4762
handle_get_all_properties (GDBusConnection *connection,
4763
                           ExportedObject  *eo,
4764
                           GDBusMessage    *message)
4765
0
{
4766
0
  ExportedInterface *ei;
4767
0
  gboolean handled;
4768
0
  const char *interface_name;
4769
4770
0
  handled = FALSE;
4771
4772
0
  g_variant_get (g_dbus_message_get_body (message),
4773
0
                 "(&s)",
4774
0
                 &interface_name);
4775
4776
  /* Fail with org.freedesktop.DBus.Error.InvalidArgs if there is
4777
   * no such interface registered
4778
   */
4779
0
  ei = g_hash_table_lookup (eo->map_if_name_to_ei, interface_name);
4780
0
  if (ei == NULL)
4781
0
    {
4782
0
      GDBusMessage *reply;
4783
0
      reply = g_dbus_message_new_method_error (message,
4784
0
                                               "org.freedesktop.DBus.Error.InvalidArgs",
4785
0
                                               _("No such interface ā€œ%sā€"),
4786
0
                                               interface_name);
4787
0
      g_dbus_connection_send_message_unlocked (eo->connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
4788
0
      g_object_unref (reply);
4789
0
      handled = TRUE;
4790
0
      goto out;
4791
0
    }
4792
4793
0
  handled = validate_and_maybe_schedule_property_get_all (eo->connection,
4794
0
                                                          message,
4795
0
                                                          ei->id,
4796
0
                                                          0,
4797
0
                                                          ei->interface_info,
4798
0
                                                          ei->vtable,
4799
0
                                                          ei->context,
4800
0
                                                          ei->user_data);
4801
0
 out:
4802
0
  return handled;
4803
0
}
4804
4805
/* ---------------------------------------------------------------------------------------------------- */
4806
4807
static const gchar introspect_header[] =
4808
  "<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\"\n"
4809
  "                      \"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n"
4810
  "<!-- GDBus " PACKAGE_VERSION " -->\n"
4811
  "<node>\n";
4812
4813
static const gchar introspect_tail[] =
4814
  "</node>\n";
4815
4816
static const gchar introspect_properties_interface[] =
4817
  "  <interface name=\"org.freedesktop.DBus.Properties\">\n"
4818
  "    <method name=\"Get\">\n"
4819
  "      <arg type=\"s\" name=\"interface_name\" direction=\"in\"/>\n"
4820
  "      <arg type=\"s\" name=\"property_name\" direction=\"in\"/>\n"
4821
  "      <arg type=\"v\" name=\"value\" direction=\"out\"/>\n"
4822
  "    </method>\n"
4823
  "    <method name=\"GetAll\">\n"
4824
  "      <arg type=\"s\" name=\"interface_name\" direction=\"in\"/>\n"
4825
  "      <arg type=\"a{sv}\" name=\"properties\" direction=\"out\"/>\n"
4826
  "    </method>\n"
4827
  "    <method name=\"Set\">\n"
4828
  "      <arg type=\"s\" name=\"interface_name\" direction=\"in\"/>\n"
4829
  "      <arg type=\"s\" name=\"property_name\" direction=\"in\"/>\n"
4830
  "      <arg type=\"v\" name=\"value\" direction=\"in\"/>\n"
4831
  "    </method>\n"
4832
  "    <signal name=\"PropertiesChanged\">\n"
4833
  "      <arg type=\"s\" name=\"interface_name\"/>\n"
4834
  "      <arg type=\"a{sv}\" name=\"changed_properties\"/>\n"
4835
  "      <arg type=\"as\" name=\"invalidated_properties\"/>\n"
4836
  "    </signal>\n"
4837
  "  </interface>\n";
4838
4839
static const gchar introspect_introspectable_interface[] =
4840
  "  <interface name=\"org.freedesktop.DBus.Introspectable\">\n"
4841
  "    <method name=\"Introspect\">\n"
4842
  "      <arg type=\"s\" name=\"xml_data\" direction=\"out\"/>\n"
4843
  "    </method>\n"
4844
  "  </interface>\n"
4845
  "  <interface name=\"org.freedesktop.DBus.Peer\">\n"
4846
  "    <method name=\"Ping\"/>\n"
4847
  "    <method name=\"GetMachineId\">\n"
4848
  "      <arg type=\"s\" name=\"machine_uuid\" direction=\"out\"/>\n"
4849
  "    </method>\n"
4850
  "  </interface>\n";
4851
4852
static void
4853
introspect_append_header (GString *s)
4854
0
{
4855
0
  g_string_append (s, introspect_header);
4856
0
}
4857
4858
static void
4859
maybe_add_path (const gchar *path, gsize path_len, const gchar *object_path, GHashTable *set)
4860
0
{
4861
0
  if (g_str_has_prefix (object_path, path) && strlen (object_path) > path_len && object_path[path_len-1] == '/')
4862
0
    {
4863
0
      const gchar *begin;
4864
0
      const gchar *end;
4865
0
      gchar *s;
4866
4867
0
      begin = object_path + path_len;
4868
0
      end = strchr (begin, '/');
4869
0
      if (end != NULL)
4870
0
        s = g_strndup (begin, end - begin);
4871
0
      else
4872
0
        s = g_strdup (begin);
4873
4874
0
      if (!g_hash_table_contains (set, s))
4875
0
        g_hash_table_add (set, s);
4876
0
      else
4877
0
        g_free (s);
4878
0
    }
4879
0
}
4880
4881
/* TODO: we want a nicer public interface for this */
4882
/* called in any thread with connection's lock held */
4883
static gchar **
4884
g_dbus_connection_list_registered_unlocked (GDBusConnection *connection,
4885
                                            const gchar     *path)
4886
0
{
4887
0
  GPtrArray *p;
4888
0
  gchar **ret;
4889
0
  GHashTableIter hash_iter;
4890
0
  const gchar *object_path;
4891
0
  gsize path_len;
4892
0
  GHashTable *set;
4893
4894
0
  CONNECTION_ENSURE_LOCK (connection);
4895
4896
0
  path_len = strlen (path);
4897
0
  if (path_len > 1)
4898
0
    path_len++;
4899
4900
0
  set = g_hash_table_new (g_str_hash, g_str_equal);
4901
4902
0
  g_hash_table_iter_init (&hash_iter, connection->map_object_path_to_eo);
4903
0
  while (g_hash_table_iter_next (&hash_iter, (gpointer) &object_path, NULL))
4904
0
    maybe_add_path (path, path_len, object_path, set);
4905
4906
0
  g_hash_table_iter_init (&hash_iter, connection->map_object_path_to_es);
4907
0
  while (g_hash_table_iter_next (&hash_iter, (gpointer) &object_path, NULL))
4908
0
    maybe_add_path (path, path_len, object_path, set);
4909
4910
0
  p = g_hash_table_steal_all_keys (set);
4911
0
  g_hash_table_unref (set);
4912
4913
0
  g_ptr_array_add (p, NULL);
4914
0
  ret = (gchar **) g_ptr_array_free (p, FALSE);
4915
0
  return ret;
4916
0
}
4917
4918
/* called in any thread with connection's lock not held */
4919
static gchar **
4920
g_dbus_connection_list_registered (GDBusConnection *connection,
4921
                                   const gchar     *path)
4922
0
{
4923
0
  gchar **ret;
4924
0
  CONNECTION_LOCK (connection);
4925
0
  ret = g_dbus_connection_list_registered_unlocked (connection, path);
4926
0
  CONNECTION_UNLOCK (connection);
4927
0
  return ret;
4928
0
}
4929
4930
/* called in GDBusWorker thread with connection's lock held */
4931
static gboolean
4932
handle_introspect (GDBusConnection *connection,
4933
                   ExportedObject  *eo,
4934
                   GDBusMessage    *message)
4935
0
{
4936
0
  guint n;
4937
0
  GString *s;
4938
0
  GDBusMessage *reply;
4939
0
  GHashTableIter hash_iter;
4940
0
  ExportedInterface *ei;
4941
0
  gchar **registered;
4942
4943
  /* first the header with the standard interfaces */
4944
0
  s = g_string_sized_new (sizeof (introspect_header) +
4945
0
                          sizeof (introspect_properties_interface) +
4946
0
                          sizeof (introspect_introspectable_interface) +
4947
0
                          sizeof (introspect_tail));
4948
0
  introspect_append_header (s);
4949
0
  if (!g_hash_table_lookup (eo->map_if_name_to_ei,
4950
0
                            "org.freedesktop.DBus.Properties"))
4951
0
    g_string_append (s, introspect_properties_interface);
4952
4953
0
  if (!g_hash_table_lookup (eo->map_if_name_to_ei,
4954
0
                            "org.freedesktop.DBus.Introspectable"))
4955
0
    g_string_append (s, introspect_introspectable_interface);
4956
4957
  /* then include the registered interfaces */
4958
0
  g_hash_table_iter_init (&hash_iter, eo->map_if_name_to_ei);
4959
0
  while (g_hash_table_iter_next (&hash_iter, NULL, (gpointer) &ei))
4960
0
    g_dbus_interface_info_generate_xml (ei->interface_info, 2, s);
4961
4962
  /* finally include nodes registered below us */
4963
0
  registered = g_dbus_connection_list_registered_unlocked (connection, eo->object_path);
4964
0
  for (n = 0; registered != NULL && registered[n] != NULL; n++)
4965
0
    g_string_append_printf (s, "  <node name=\"%s\"/>\n", registered[n]);
4966
0
  g_strfreev (registered);
4967
0
  g_string_append (s, introspect_tail);
4968
4969
0
  reply = g_dbus_message_new_method_reply (message);
4970
0
  g_dbus_message_set_body (reply, g_variant_new ("(s)", s->str));
4971
0
  g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
4972
0
  g_object_unref (reply);
4973
0
  g_string_free (s, TRUE);
4974
4975
0
  return TRUE;
4976
0
}
4977
4978
/* called in thread where object was registered - no locks held */
4979
static gboolean
4980
call_in_idle_cb (gpointer user_data)
4981
0
{
4982
0
  GDBusMethodInvocation *invocation = G_DBUS_METHOD_INVOCATION (user_data);
4983
0
  GDBusInterfaceVTable *vtable;
4984
0
  guint registration_id;
4985
0
  guint subtree_registration_id;
4986
0
  ExportedInterface *ei = NULL;
4987
0
  ExportedSubtree *es = NULL;
4988
4989
0
  registration_id = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (invocation), "g-dbus-registration-id"));
4990
0
  subtree_registration_id = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (invocation), "g-dbus-subtree-registration-id"));
4991
4992
0
  if (has_object_been_unregistered (g_dbus_method_invocation_get_connection (invocation),
4993
0
                                    registration_id,
4994
0
                                    &ei,
4995
0
                                    subtree_registration_id,
4996
0
                                    &es))
4997
0
    {
4998
0
      GDBusMessage *reply;
4999
0
      reply = g_dbus_message_new_method_error (g_dbus_method_invocation_get_message (invocation),
5000
0
                                               "org.freedesktop.DBus.Error.UnknownMethod",
5001
0
                                               _("No such interface ā€œ%sā€ on object at path %s"),
5002
0
                                               g_dbus_method_invocation_get_interface_name (invocation),
5003
0
                                               g_dbus_method_invocation_get_object_path (invocation));
5004
0
      g_dbus_connection_send_message (g_dbus_method_invocation_get_connection (invocation), reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
5005
0
      g_object_unref (reply);
5006
0
      goto out;
5007
0
    }
5008
5009
0
  vtable = g_object_get_data (G_OBJECT (invocation), "g-dbus-interface-vtable");
5010
0
  g_assert (vtable != NULL && vtable->method_call != NULL);
5011
5012
0
  vtable->method_call (g_dbus_method_invocation_get_connection (invocation),
5013
0
                       g_dbus_method_invocation_get_sender (invocation),
5014
0
                       g_dbus_method_invocation_get_object_path (invocation),
5015
0
                       g_dbus_method_invocation_get_interface_name (invocation),
5016
0
                       g_dbus_method_invocation_get_method_name (invocation),
5017
0
                       g_dbus_method_invocation_get_parameters (invocation),
5018
0
                       g_object_ref (invocation),
5019
0
                       g_dbus_method_invocation_get_user_data (invocation));
5020
5021
0
 out:
5022
0
  g_clear_pointer (&ei, exported_interface_unref);
5023
0
  g_clear_pointer (&es, exported_subtree_unref);
5024
5025
0
  return FALSE;
5026
0
}
5027
5028
/* called in GDBusWorker thread with connection's lock held */
5029
static void
5030
schedule_method_call (GDBusConnection            *connection,
5031
                      GDBusMessage               *message,
5032
                      guint                       registration_id,
5033
                      guint                       subtree_registration_id,
5034
                      const GDBusInterfaceInfo   *interface_info,
5035
                      const GDBusMethodInfo      *method_info,
5036
                      const GDBusPropertyInfo    *property_info,
5037
                      GVariant                   *parameters,
5038
                      const GDBusInterfaceVTable *vtable,
5039
                      GMainContext               *main_context,
5040
                      gpointer                    user_data)
5041
0
{
5042
0
  GDBusMethodInvocation *invocation;
5043
0
  GSource *idle_source;
5044
5045
0
  invocation = _g_dbus_method_invocation_new (g_dbus_message_get_sender (message),
5046
0
                                              g_dbus_message_get_path (message),
5047
0
                                              g_dbus_message_get_interface (message),
5048
0
                                              g_dbus_message_get_member (message),
5049
0
                                              method_info,
5050
0
                                              property_info,
5051
0
                                              connection,
5052
0
                                              message,
5053
0
                                              parameters,
5054
0
                                              user_data);
5055
5056
  /* TODO: would be nicer with a real MethodData like we already
5057
   * have PropertyData and PropertyGetAllData... */
5058
0
  g_object_set_data (G_OBJECT (invocation), "g-dbus-interface-vtable", (gpointer) vtable);
5059
0
  g_object_set_data (G_OBJECT (invocation), "g-dbus-registration-id", GUINT_TO_POINTER (registration_id));
5060
0
  g_object_set_data (G_OBJECT (invocation), "g-dbus-subtree-registration-id", GUINT_TO_POINTER (subtree_registration_id));
5061
5062
0
  idle_source = g_idle_source_new ();
5063
0
  g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
5064
0
  g_source_set_callback (idle_source,
5065
0
                         call_in_idle_cb,
5066
0
                         g_steal_pointer (&invocation),
5067
0
                         g_object_unref);
5068
0
  g_source_set_static_name (idle_source, "[gio, " __FILE__ "] call_in_idle_cb");
5069
0
  g_source_attach (idle_source, main_context);
5070
0
  g_source_unref (idle_source);
5071
0
}
5072
5073
/* called in GDBusWorker thread with connection's lock held */
5074
static gboolean
5075
validate_and_maybe_schedule_method_call (GDBusConnection            *connection,
5076
                                         GDBusMessage               *message,
5077
                                         guint                       registration_id,
5078
                                         guint                       subtree_registration_id,
5079
                                         GDBusInterfaceInfo         *interface_info,
5080
                                         const GDBusInterfaceVTable *vtable,
5081
                                         GMainContext               *main_context,
5082
                                         gpointer                    user_data)
5083
0
{
5084
0
  GDBusMethodInfo *method_info;
5085
0
  GDBusMessage *reply;
5086
0
  GVariant *parameters;
5087
0
  gboolean handled;
5088
0
  GVariantType *in_type;
5089
5090
0
  handled = FALSE;
5091
5092
  /* TODO: the cost of this is O(n) - it might be worth caching the result */
5093
0
  method_info = g_dbus_interface_info_lookup_method (interface_info, g_dbus_message_get_member (message));
5094
5095
  /* if the method doesn't exist, return the org.freedesktop.DBus.Error.UnknownMethod
5096
   * error to the caller
5097
   */
5098
0
  if (method_info == NULL)
5099
0
    {
5100
0
      reply = g_dbus_message_new_method_error (message,
5101
0
                                               "org.freedesktop.DBus.Error.UnknownMethod",
5102
0
                                               _("No such method ā€œ%sā€"),
5103
0
                                               g_dbus_message_get_member (message));
5104
0
      g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
5105
0
      g_object_unref (reply);
5106
0
      handled = TRUE;
5107
0
      goto out;
5108
0
    }
5109
5110
0
  parameters = g_dbus_message_get_body (message);
5111
0
  if (parameters == NULL)
5112
0
    {
5113
0
      parameters = g_variant_new ("()");
5114
0
      g_variant_ref_sink (parameters);
5115
0
    }
5116
0
  else
5117
0
    {
5118
0
      g_variant_ref (parameters);
5119
0
    }
5120
5121
  /* Check that the incoming args are of the right type - if they are not, return
5122
   * the org.freedesktop.DBus.Error.InvalidArgs error to the caller
5123
   */
5124
0
  in_type = _g_dbus_compute_complete_signature (method_info->in_args);
5125
0
  if (!g_variant_is_of_type (parameters, in_type))
5126
0
    {
5127
0
      gchar *type_string;
5128
5129
0
      type_string = g_variant_type_dup_string (in_type);
5130
5131
0
      reply = g_dbus_message_new_method_error (message,
5132
0
                                               "org.freedesktop.DBus.Error.InvalidArgs",
5133
0
                                               _("Type of message, ā€œ%sā€, does not match expected type ā€œ%sā€"),
5134
0
                                               g_variant_get_type_string (parameters),
5135
0
                                               type_string);
5136
0
      g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
5137
0
      g_variant_type_free (in_type);
5138
0
      g_variant_unref (parameters);
5139
0
      g_object_unref (reply);
5140
0
      g_free (type_string);
5141
0
      handled = TRUE;
5142
0
      goto out;
5143
0
    }
5144
0
  g_variant_type_free (in_type);
5145
5146
  /* schedule the call in idle */
5147
0
  schedule_method_call (connection, message, registration_id, subtree_registration_id,
5148
0
                        interface_info, method_info, NULL, parameters,
5149
0
                        vtable, main_context, user_data);
5150
0
  g_variant_unref (parameters);
5151
0
  handled = TRUE;
5152
5153
0
 out:
5154
0
  return handled;
5155
0
}
5156
5157
/* ---------------------------------------------------------------------------------------------------- */
5158
5159
/* called in GDBusWorker thread with connection's lock held */
5160
static gboolean
5161
obj_message_func (GDBusConnection *connection,
5162
                  ExportedObject  *eo,
5163
                  GDBusMessage    *message,
5164
                  gboolean        *object_found)
5165
0
{
5166
0
  const gchar *interface_name;
5167
0
  const gchar *member;
5168
0
  const gchar *signature;
5169
0
  gboolean handled;
5170
5171
0
  handled = FALSE;
5172
5173
0
  interface_name = g_dbus_message_get_interface (message);
5174
0
  member = g_dbus_message_get_member (message);
5175
0
  signature = g_dbus_message_get_signature (message);
5176
5177
  /* see if we have an interface for handling this call */
5178
0
  if (interface_name != NULL)
5179
0
    {
5180
0
      ExportedInterface *ei;
5181
0
      ei = g_hash_table_lookup (eo->map_if_name_to_ei, interface_name);
5182
0
      if (ei != NULL)
5183
0
        {
5184
          /* we do - invoke the handler in idle in the right thread */
5185
5186
          /* handle no vtable or handler being present */
5187
0
          if (ei->vtable == NULL || ei->vtable->method_call == NULL)
5188
0
            goto out;
5189
5190
0
          handled = validate_and_maybe_schedule_method_call (connection,
5191
0
                                                             message,
5192
0
                                                             ei->id,
5193
0
                                                             0,
5194
0
                                                             ei->interface_info,
5195
0
                                                             ei->vtable,
5196
0
                                                             ei->context,
5197
0
                                                             ei->user_data);
5198
0
          goto out;
5199
0
        }
5200
0
      else
5201
0
        {
5202
0
          *object_found = TRUE;
5203
0
        }
5204
0
    }
5205
5206
0
  if (g_strcmp0 (interface_name, "org.freedesktop.DBus.Introspectable") == 0 &&
5207
0
      g_strcmp0 (member, "Introspect") == 0 &&
5208
0
      g_strcmp0 (signature, "") == 0)
5209
0
    {
5210
0
      handled = handle_introspect (connection, eo, message);
5211
0
      goto out;
5212
0
    }
5213
0
  else if (g_strcmp0 (interface_name, "org.freedesktop.DBus.Properties") == 0 &&
5214
0
           g_strcmp0 (member, "Get") == 0 &&
5215
0
           g_strcmp0 (signature, "ss") == 0)
5216
0
    {
5217
0
      handled = handle_getset_property (connection, eo, message, TRUE);
5218
0
      goto out;
5219
0
    }
5220
0
  else if (g_strcmp0 (interface_name, "org.freedesktop.DBus.Properties") == 0 &&
5221
0
           g_strcmp0 (member, "Set") == 0 &&
5222
0
           g_strcmp0 (signature, "ssv") == 0)
5223
0
    {
5224
0
      handled = handle_getset_property (connection, eo, message, FALSE);
5225
0
      goto out;
5226
0
    }
5227
0
  else if (g_strcmp0 (interface_name, "org.freedesktop.DBus.Properties") == 0 &&
5228
0
           g_strcmp0 (member, "GetAll") == 0 &&
5229
0
           g_strcmp0 (signature, "s") == 0)
5230
0
    {
5231
0
      handled = handle_get_all_properties (connection, eo, message);
5232
0
      goto out;
5233
0
    }
5234
5235
0
 out:
5236
0
  return handled;
5237
0
}
5238
5239
/**
5240
 * g_dbus_connection_register_object:
5241
 * @connection: a #GDBusConnection
5242
 * @object_path: the object path to register at
5243
 * @interface_info: introspection data for the interface
5244
 * @vtable: (nullable): a #GDBusInterfaceVTable to call into or %NULL
5245
 * @user_data: (nullable): data to pass to functions in @vtable
5246
 * @user_data_free_func: function to call when the object path is unregistered
5247
 * @error: return location for error or %NULL
5248
 *
5249
 * Registers callbacks for exported objects at @object_path with the
5250
 * D-Bus interface that is described in @interface_info.
5251
 *
5252
 * Calls to functions in @vtable (and @user_data_free_func) will happen
5253
 * in the 
5254
 * [thread-default main context][g-main-context-push-thread-default]
5255
 * of the thread you are calling this method from.
5256
 *
5257
 * Note that all #GVariant values passed to functions in @vtable will match
5258
 * the signature given in @interface_info - if a remote caller passes
5259
 * incorrect values, the `org.freedesktop.DBus.Error.InvalidArgs`
5260
 * is returned to the remote caller.
5261
 *
5262
 * Additionally, if the remote caller attempts to invoke methods or
5263
 * access properties not mentioned in @interface_info the
5264
 * `org.freedesktop.DBus.Error.UnknownMethod` resp.
5265
 * `org.freedesktop.DBus.Error.InvalidArgs` errors
5266
 * are returned to the caller.
5267
 *
5268
 * It is considered a programming error if the
5269
 * #GDBusInterfaceGetPropertyFunc function in @vtable returns a
5270
 * #GVariant of incorrect type.
5271
 *
5272
 * If an existing callback is already registered at @object_path and
5273
 * @interface_name, then @error is set to %G_IO_ERROR_EXISTS.
5274
 *
5275
 * GDBus automatically implements the standard D-Bus interfaces
5276
 * org.freedesktop.DBus.Properties, org.freedesktop.DBus.Introspectable
5277
 * and org.freedesktop.Peer, so you don't have to implement those for the
5278
 * objects you export. You can implement org.freedesktop.DBus.Properties
5279
 * yourself, e.g. to handle getting and setting of properties asynchronously.
5280
 *
5281
 * Note that the reference count on @interface_info will be
5282
 * incremented by 1 (unless allocated statically, e.g. if the
5283
 * reference count is -1, see g_dbus_interface_info_ref()) for as long
5284
 * as the object is exported. Also note that @vtable will be copied.
5285
 *
5286
 * See this [server][gdbus-server] for an example of how to use this method.
5287
 *
5288
 * Returns: 0 if @error is set, otherwise a registration id (never 0)
5289
 *     that can be used with g_dbus_connection_unregister_object()
5290
 *
5291
 * Since: 2.26
5292
 */
5293
guint
5294
g_dbus_connection_register_object (GDBusConnection             *connection,
5295
                                   const gchar                 *object_path,
5296
                                   GDBusInterfaceInfo          *interface_info,
5297
                                   const GDBusInterfaceVTable  *vtable,
5298
                                   gpointer                     user_data,
5299
                                   GDestroyNotify               user_data_free_func,
5300
                                   GError                     **error)
5301
0
{
5302
0
  ExportedObject *eo;
5303
0
  ExportedInterface *ei;
5304
0
  guint ret;
5305
5306
0
  g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), 0);
5307
0
  g_return_val_if_fail (object_path != NULL && g_variant_is_object_path (object_path), 0);
5308
0
  g_return_val_if_fail (interface_info != NULL, 0);
5309
0
  g_return_val_if_fail (g_dbus_is_interface_name (interface_info->name), 0);
5310
0
  g_return_val_if_fail (error == NULL || *error == NULL, 0);
5311
0
  g_return_val_if_fail (check_initialized (connection), 0);
5312
5313
0
  ret = 0;
5314
5315
0
  CONNECTION_LOCK (connection);
5316
5317
0
  eo = g_hash_table_lookup (connection->map_object_path_to_eo, object_path);
5318
0
  if (eo == NULL)
5319
0
    {
5320
0
      eo = g_new0 (ExportedObject, 1);
5321
0
      eo->object_path = g_strdup (object_path);
5322
0
      eo->connection = connection;
5323
0
      eo->map_if_name_to_ei = g_hash_table_new_full (g_str_hash,
5324
0
                                                     g_str_equal,
5325
0
                                                     NULL,
5326
0
                                                     (GDestroyNotify) exported_interface_unref);
5327
0
      g_hash_table_insert (connection->map_object_path_to_eo, eo->object_path, eo);
5328
0
    }
5329
5330
0
  ei = g_hash_table_lookup (eo->map_if_name_to_ei, interface_info->name);
5331
0
  if (ei != NULL)
5332
0
    {
5333
0
      g_set_error (error,
5334
0
                   G_IO_ERROR,
5335
0
                   G_IO_ERROR_EXISTS,
5336
0
                   _("An object is already exported for the interface %s at %s"),
5337
0
                   interface_info->name,
5338
0
                   object_path);
5339
0
      goto out;
5340
0
    }
5341
5342
0
  ei = g_new0 (ExportedInterface, 1);
5343
0
  ei->refcount = 1;
5344
0
  ei->id = (guint) g_atomic_int_add (&_global_registration_id, 1); /* TODO: overflow etc. */
5345
0
  ei->eo = eo;
5346
0
  ei->user_data = user_data;
5347
0
  ei->user_data_free_func = user_data_free_func;
5348
0
  ei->vtable = _g_dbus_interface_vtable_copy (vtable);
5349
0
  ei->interface_info = g_dbus_interface_info_ref (interface_info);
5350
0
  g_dbus_interface_info_cache_build (ei->interface_info);
5351
0
  ei->interface_name = g_strdup (interface_info->name);
5352
0
  ei->context = g_main_context_ref_thread_default ();
5353
5354
0
  g_hash_table_insert (eo->map_if_name_to_ei,
5355
0
                       (gpointer) ei->interface_name,
5356
0
                       ei);
5357
0
  g_hash_table_insert (connection->map_id_to_ei,
5358
0
                       GUINT_TO_POINTER (ei->id),
5359
0
                       ei);
5360
5361
0
  ret = ei->id;
5362
5363
0
 out:
5364
0
  CONNECTION_UNLOCK (connection);
5365
5366
0
  return ret;
5367
0
}
5368
5369
/**
5370
 * g_dbus_connection_unregister_object:
5371
 * @connection: a #GDBusConnection
5372
 * @registration_id: a registration id obtained from
5373
 *     g_dbus_connection_register_object()
5374
 *
5375
 * Unregisters an object.
5376
 *
5377
 * Returns: %TRUE if the object was unregistered, %FALSE otherwise
5378
 *
5379
 * Since: 2.26
5380
 */
5381
gboolean
5382
g_dbus_connection_unregister_object (GDBusConnection *connection,
5383
                                     guint            registration_id)
5384
0
{
5385
0
  ExportedInterface *ei;
5386
0
  ExportedObject *eo;
5387
0
  gboolean ret;
5388
5389
0
  g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
5390
0
  g_return_val_if_fail (check_initialized (connection), FALSE);
5391
5392
0
  ret = FALSE;
5393
5394
0
  CONNECTION_LOCK (connection);
5395
5396
0
  ei = g_hash_table_lookup (connection->map_id_to_ei,
5397
0
                            GUINT_TO_POINTER (registration_id));
5398
0
  if (ei == NULL)
5399
0
    goto out;
5400
5401
0
  eo = ei->eo;
5402
5403
0
  g_warn_if_fail (g_hash_table_remove (connection->map_id_to_ei, GUINT_TO_POINTER (ei->id)));
5404
0
  g_warn_if_fail (g_hash_table_remove (eo->map_if_name_to_ei, ei->interface_name));
5405
  /* unregister object path if we have no more exported interfaces */
5406
0
  if (g_hash_table_size (eo->map_if_name_to_ei) == 0)
5407
0
    g_warn_if_fail (g_hash_table_remove (connection->map_object_path_to_eo,
5408
0
                                         eo->object_path));
5409
5410
0
  ret = TRUE;
5411
5412
0
 out:
5413
0
  CONNECTION_UNLOCK (connection);
5414
5415
0
  return ret;
5416
0
}
5417
5418
typedef struct {
5419
  GClosure *method_call_closure;
5420
  GClosure *get_property_closure;
5421
  GClosure *set_property_closure;
5422
} RegisterObjectData;
5423
5424
static RegisterObjectData *
5425
register_object_data_new (GClosure *method_call_closure,
5426
                          GClosure *get_property_closure,
5427
                          GClosure *set_property_closure)
5428
0
{
5429
0
  RegisterObjectData *data;
5430
5431
0
  data = g_new0 (RegisterObjectData, 1);
5432
5433
0
  if (method_call_closure != NULL)
5434
0
    {
5435
0
      data->method_call_closure = g_closure_ref (method_call_closure);
5436
0
      g_closure_sink (method_call_closure);
5437
0
      if (G_CLOSURE_NEEDS_MARSHAL (method_call_closure))
5438
0
        g_closure_set_marshal (method_call_closure, g_cclosure_marshal_generic);
5439
0
    }
5440
5441
0
  if (get_property_closure != NULL)
5442
0
    {
5443
0
      data->get_property_closure = g_closure_ref (get_property_closure);
5444
0
      g_closure_sink (get_property_closure);
5445
0
      if (G_CLOSURE_NEEDS_MARSHAL (get_property_closure))
5446
0
        g_closure_set_marshal (get_property_closure, g_cclosure_marshal_generic);
5447
0
    }
5448
5449
0
  if (set_property_closure != NULL)
5450
0
    {
5451
0
      data->set_property_closure = g_closure_ref (set_property_closure);
5452
0
      g_closure_sink (set_property_closure);
5453
0
      if (G_CLOSURE_NEEDS_MARSHAL (set_property_closure))
5454
0
        g_closure_set_marshal (set_property_closure, g_cclosure_marshal_generic);
5455
0
    }
5456
5457
0
  return data;
5458
0
}
5459
5460
static void
5461
register_object_free_func (gpointer user_data)
5462
0
{
5463
0
  RegisterObjectData *data = user_data;
5464
5465
0
  g_clear_pointer (&data->method_call_closure, g_closure_unref);
5466
0
  g_clear_pointer (&data->get_property_closure, g_closure_unref);
5467
0
  g_clear_pointer (&data->set_property_closure, g_closure_unref);
5468
5469
0
  g_free (data);
5470
0
}
5471
5472
static void
5473
register_with_closures_on_method_call (GDBusConnection       *connection,
5474
                                       const gchar           *sender,
5475
                                       const gchar           *object_path,
5476
                                       const gchar           *interface_name,
5477
                                       const gchar           *method_name,
5478
                                       GVariant              *parameters,
5479
                                       GDBusMethodInvocation *invocation,
5480
                                       gpointer               user_data)
5481
0
{
5482
0
  RegisterObjectData *data = user_data;
5483
0
  GValue params[] = { G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT };
5484
5485
0
  g_value_init (&params[0], G_TYPE_DBUS_CONNECTION);
5486
0
  g_value_set_object (&params[0], connection);
5487
5488
0
  g_value_init (&params[1], G_TYPE_STRING);
5489
0
  g_value_set_string (&params[1], sender);
5490
5491
0
  g_value_init (&params[2], G_TYPE_STRING);
5492
0
  g_value_set_string (&params[2], object_path);
5493
5494
0
  g_value_init (&params[3], G_TYPE_STRING);
5495
0
  g_value_set_string (&params[3], interface_name);
5496
5497
0
  g_value_init (&params[4], G_TYPE_STRING);
5498
0
  g_value_set_string (&params[4], method_name);
5499
5500
0
  g_value_init (&params[5], G_TYPE_VARIANT);
5501
0
  g_value_set_variant (&params[5], parameters);
5502
5503
0
  g_value_init (&params[6], G_TYPE_DBUS_METHOD_INVOCATION);
5504
0
  g_value_set_object (&params[6], invocation);
5505
5506
0
  g_closure_invoke (data->method_call_closure, NULL, G_N_ELEMENTS (params), params, NULL);
5507
5508
0
  g_value_unset (params + 0);
5509
0
  g_value_unset (params + 1);
5510
0
  g_value_unset (params + 2);
5511
0
  g_value_unset (params + 3);
5512
0
  g_value_unset (params + 4);
5513
0
  g_value_unset (params + 5);
5514
0
  g_value_unset (params + 6);
5515
0
}
5516
5517
static GVariant *
5518
register_with_closures_on_get_property (GDBusConnection *connection,
5519
                                        const gchar     *sender,
5520
                                        const gchar     *object_path,
5521
                                        const gchar     *interface_name,
5522
                                        const gchar     *property_name,
5523
                                        GError         **error,
5524
                                        gpointer         user_data)
5525
0
{
5526
0
  RegisterObjectData *data = user_data;
5527
0
  GValue params[] = { G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT };
5528
0
  GValue result_value = G_VALUE_INIT;
5529
0
  GVariant *result;
5530
5531
0
  g_value_init (&params[0], G_TYPE_DBUS_CONNECTION);
5532
0
  g_value_set_object (&params[0], connection);
5533
5534
0
  g_value_init (&params[1], G_TYPE_STRING);
5535
0
  g_value_set_string (&params[1], sender);
5536
5537
0
  g_value_init (&params[2], G_TYPE_STRING);
5538
0
  g_value_set_string (&params[2], object_path);
5539
5540
0
  g_value_init (&params[3], G_TYPE_STRING);
5541
0
  g_value_set_string (&params[3], interface_name);
5542
5543
0
  g_value_init (&params[4], G_TYPE_STRING);
5544
0
  g_value_set_string (&params[4], property_name);
5545
5546
0
  g_value_init (&result_value, G_TYPE_VARIANT);
5547
5548
0
  g_closure_invoke (data->get_property_closure, &result_value, G_N_ELEMENTS (params), params, NULL);
5549
5550
0
  result = g_value_get_variant (&result_value);
5551
0
  if (result)
5552
0
    g_variant_ref (result);
5553
5554
0
  g_value_unset (params + 0);
5555
0
  g_value_unset (params + 1);
5556
0
  g_value_unset (params + 2);
5557
0
  g_value_unset (params + 3);
5558
0
  g_value_unset (params + 4);
5559
0
  g_value_unset (&result_value);
5560
5561
0
  if (!result)
5562
0
    g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
5563
0
                 _("Unable to retrieve property %s.%s"),
5564
0
                 interface_name, property_name);
5565
5566
0
  return result;
5567
0
}
5568
5569
static gboolean
5570
register_with_closures_on_set_property (GDBusConnection *connection,
5571
                                        const gchar     *sender,
5572
                                        const gchar     *object_path,
5573
                                        const gchar     *interface_name,
5574
                                        const gchar     *property_name,
5575
                                        GVariant        *value,
5576
                                        GError         **error,
5577
                                        gpointer         user_data)
5578
0
{
5579
0
  RegisterObjectData *data = user_data;
5580
0
  GValue params[] = { G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT };
5581
0
  GValue result_value = G_VALUE_INIT;
5582
0
  gboolean result;
5583
5584
0
  g_value_init (&params[0], G_TYPE_DBUS_CONNECTION);
5585
0
  g_value_set_object (&params[0], connection);
5586
5587
0
  g_value_init (&params[1], G_TYPE_STRING);
5588
0
  g_value_set_string (&params[1], sender);
5589
5590
0
  g_value_init (&params[2], G_TYPE_STRING);
5591
0
  g_value_set_string (&params[2], object_path);
5592
5593
0
  g_value_init (&params[3], G_TYPE_STRING);
5594
0
  g_value_set_string (&params[3], interface_name);
5595
5596
0
  g_value_init (&params[4], G_TYPE_STRING);
5597
0
  g_value_set_string (&params[4], property_name);
5598
5599
0
  g_value_init (&params[5], G_TYPE_VARIANT);
5600
0
  g_value_set_variant (&params[5], value);
5601
5602
0
  g_value_init (&result_value, G_TYPE_BOOLEAN);
5603
5604
0
  g_closure_invoke (data->set_property_closure, &result_value, G_N_ELEMENTS (params), params, NULL);
5605
5606
0
  result = g_value_get_boolean (&result_value);
5607
5608
0
  g_value_unset (params + 0);
5609
0
  g_value_unset (params + 1);
5610
0
  g_value_unset (params + 2);
5611
0
  g_value_unset (params + 3);
5612
0
  g_value_unset (params + 4);
5613
0
  g_value_unset (params + 5);
5614
0
  g_value_unset (&result_value);
5615
5616
0
  if (!result)
5617
0
    g_set_error (error,
5618
0
                 G_DBUS_ERROR, G_DBUS_ERROR_FAILED,
5619
0
                 _("Unable to set property %s.%s"),
5620
0
                 interface_name, property_name);
5621
5622
0
  return result;
5623
0
}
5624
5625
/**
5626
 * g_dbus_connection_register_object_with_closures: (rename-to g_dbus_connection_register_object)
5627
 * @connection: A #GDBusConnection.
5628
 * @object_path: The object path to register at.
5629
 * @interface_info: Introspection data for the interface.
5630
 * @method_call_closure: (nullable): #GClosure for handling incoming method calls.
5631
 * @get_property_closure: (nullable): #GClosure for getting a property.
5632
 * @set_property_closure: (nullable): #GClosure for setting a property.
5633
 * @error: Return location for error or %NULL.
5634
 *
5635
 * Version of g_dbus_connection_register_object() using closures instead of a
5636
 * #GDBusInterfaceVTable for easier binding in other languages.
5637
 *
5638
 * Returns: 0 if @error is set, otherwise a registration ID (never 0)
5639
 * that can be used with g_dbus_connection_unregister_object() .
5640
 *
5641
 * Since: 2.46
5642
 */
5643
guint
5644
g_dbus_connection_register_object_with_closures (GDBusConnection     *connection,
5645
                                                 const gchar         *object_path,
5646
                                                 GDBusInterfaceInfo  *interface_info,
5647
                                                 GClosure            *method_call_closure,
5648
                                                 GClosure            *get_property_closure,
5649
                                                 GClosure            *set_property_closure,
5650
                                                 GError             **error)
5651
0
{
5652
0
  RegisterObjectData *data;
5653
0
  GDBusInterfaceVTable vtable =
5654
0
    {
5655
0
      method_call_closure != NULL  ? register_with_closures_on_method_call  : NULL,
5656
0
      get_property_closure != NULL ? register_with_closures_on_get_property : NULL,
5657
0
      set_property_closure != NULL ? register_with_closures_on_set_property : NULL,
5658
0
      { 0 }
5659
0
    };
5660
5661
0
  data = register_object_data_new (method_call_closure, get_property_closure, set_property_closure);
5662
5663
0
  return g_dbus_connection_register_object (connection,
5664
0
                                            object_path,
5665
0
                                            interface_info,
5666
0
                                            &vtable,
5667
0
                                            data,
5668
0
                                            register_object_free_func,
5669
0
                                            error);
5670
0
}
5671
5672
/* ---------------------------------------------------------------------------------------------------- */
5673
5674
/**
5675
 * g_dbus_connection_emit_signal:
5676
 * @connection: a #GDBusConnection
5677
 * @destination_bus_name: (nullable): the unique bus name for the destination
5678
 *     for the signal or %NULL to emit to all listeners
5679
 * @object_path: path of remote object
5680
 * @interface_name: D-Bus interface to emit a signal on
5681
 * @signal_name: the name of the signal to emit
5682
 * @parameters: (nullable): a #GVariant tuple with parameters for the signal
5683
 *              or %NULL if not passing parameters
5684
 * @error: Return location for error or %NULL
5685
 *
5686
 * Emits a signal.
5687
 *
5688
 * If the parameters GVariant is floating, it is consumed.
5689
 *
5690
 * This can only fail if @parameters is not compatible with the D-Bus protocol
5691
 * (%G_IO_ERROR_INVALID_ARGUMENT), or if @connection has been closed
5692
 * (%G_IO_ERROR_CLOSED).
5693
 *
5694
 * Returns: %TRUE unless @error is set
5695
 *
5696
 * Since: 2.26
5697
 */
5698
gboolean
5699
g_dbus_connection_emit_signal (GDBusConnection  *connection,
5700
                               const gchar      *destination_bus_name,
5701
                               const gchar      *object_path,
5702
                               const gchar      *interface_name,
5703
                               const gchar      *signal_name,
5704
                               GVariant         *parameters,
5705
                               GError          **error)
5706
0
{
5707
0
  GDBusMessage *message;
5708
0
  gboolean ret;
5709
5710
0
  message = NULL;
5711
0
  ret = FALSE;
5712
5713
0
  g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
5714
0
  g_return_val_if_fail (destination_bus_name == NULL || g_dbus_is_name (destination_bus_name), FALSE);
5715
0
  g_return_val_if_fail (object_path != NULL && g_variant_is_object_path (object_path), FALSE);
5716
0
  g_return_val_if_fail (interface_name != NULL && g_dbus_is_interface_name (interface_name), FALSE);
5717
0
  g_return_val_if_fail (signal_name != NULL && g_dbus_is_member_name (signal_name), FALSE);
5718
0
  g_return_val_if_fail (parameters == NULL || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE), FALSE);
5719
0
  g_return_val_if_fail (check_initialized (connection), FALSE);
5720
5721
0
  if (G_UNLIKELY (_g_dbus_debug_emission ()))
5722
0
    {
5723
0
      _g_dbus_debug_print_lock ();
5724
0
      g_print ("========================================================================\n"
5725
0
               "GDBus-debug:Emission:\n"
5726
0
               " >>>> SIGNAL EMISSION %s.%s()\n"
5727
0
               "      on object %s\n"
5728
0
               "      destination %s\n",
5729
0
               interface_name, signal_name,
5730
0
               object_path,
5731
0
               destination_bus_name != NULL ? destination_bus_name : "(none)");
5732
0
      _g_dbus_debug_print_unlock ();
5733
0
    }
5734
5735
0
  message = g_dbus_message_new_signal (object_path,
5736
0
                                       interface_name,
5737
0
                                       signal_name);
5738
5739
0
  if (destination_bus_name != NULL)
5740
0
    g_dbus_message_set_header (message,
5741
0
                               G_DBUS_MESSAGE_HEADER_FIELD_DESTINATION,
5742
0
                               g_variant_new_string (destination_bus_name));
5743
5744
0
  if (parameters != NULL)
5745
0
    g_dbus_message_set_body (message, parameters);
5746
5747
0
  ret = g_dbus_connection_send_message (connection, message, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, error);
5748
0
  g_object_unref (message);
5749
5750
0
  return ret;
5751
0
}
5752
5753
static void
5754
add_call_flags (GDBusMessage           *message,
5755
                         GDBusCallFlags  flags)
5756
0
{
5757
0
  GDBusMessageFlags msg_flags = 0;
5758
5759
0
  if (flags & G_DBUS_CALL_FLAGS_NO_AUTO_START)
5760
0
    msg_flags |= G_DBUS_MESSAGE_FLAGS_NO_AUTO_START;
5761
0
  if (flags & G_DBUS_CALL_FLAGS_ALLOW_INTERACTIVE_AUTHORIZATION)
5762
0
    msg_flags |= G_DBUS_MESSAGE_FLAGS_ALLOW_INTERACTIVE_AUTHORIZATION;
5763
0
  if (msg_flags)
5764
0
    g_dbus_message_set_flags (message, msg_flags);
5765
0
}
5766
5767
static GVariant *
5768
decode_method_reply (GDBusMessage        *reply,
5769
                     const gchar         *method_name,
5770
                     const GVariantType  *reply_type,
5771
                     GUnixFDList        **out_fd_list,
5772
                     GError             **error)
5773
0
{
5774
0
  GVariant *result;
5775
5776
0
  result = NULL;
5777
0
  switch (g_dbus_message_get_message_type (reply))
5778
0
    {
5779
0
    case G_DBUS_MESSAGE_TYPE_METHOD_RETURN:
5780
0
      result = g_dbus_message_get_body (reply);
5781
0
      if (result == NULL)
5782
0
        {
5783
0
          result = g_variant_new ("()");
5784
0
          g_variant_ref_sink (result);
5785
0
        }
5786
0
      else
5787
0
        {
5788
0
          g_variant_ref (result);
5789
0
        }
5790
5791
0
      if (!g_variant_is_of_type (result, reply_type))
5792
0
        {
5793
0
          gchar *type_string = g_variant_type_dup_string (reply_type);
5794
5795
0
          g_set_error (error,
5796
0
                       G_IO_ERROR,
5797
0
                       G_IO_ERROR_INVALID_ARGUMENT,
5798
0
                       _("Method ā€œ%sā€ returned type ā€œ%sā€, but expected ā€œ%sā€"),
5799
0
                       method_name, g_variant_get_type_string (result), type_string);
5800
5801
0
          g_variant_unref (result);
5802
0
          g_free (type_string);
5803
0
          result = NULL;
5804
0
        }
5805
5806
0
#ifdef G_OS_UNIX
5807
0
      if (result != NULL)
5808
0
        {
5809
0
          if (out_fd_list != NULL)
5810
0
            {
5811
0
              *out_fd_list = g_dbus_message_get_unix_fd_list (reply);
5812
0
              if (*out_fd_list != NULL)
5813
0
                g_object_ref (*out_fd_list);
5814
0
            }
5815
0
        }
5816
0
#endif
5817
0
      break;
5818
5819
0
    case G_DBUS_MESSAGE_TYPE_ERROR:
5820
0
      g_dbus_message_to_gerror (reply, error);
5821
0
      break;
5822
5823
0
    default:
5824
0
      g_assert_not_reached ();
5825
0
      break;
5826
0
    }
5827
5828
0
  return result;
5829
0
}
5830
5831
5832
typedef struct
5833
{
5834
  GVariantType *reply_type;
5835
  gchar *method_name; /* for error message */
5836
5837
  GUnixFDList *fd_list;
5838
} CallState;
5839
5840
static void
5841
call_state_free (CallState *state)
5842
0
{
5843
0
  g_variant_type_free (state->reply_type);
5844
0
  g_free (state->method_name);
5845
5846
0
  if (state->fd_list != NULL)
5847
0
    g_object_unref (state->fd_list);
5848
0
  g_slice_free (CallState, state);
5849
0
}
5850
5851
/* called in any thread, with the connection's lock not held */
5852
static void
5853
g_dbus_connection_call_done (GObject      *source,
5854
                             GAsyncResult *result,
5855
                             gpointer      user_data)
5856
0
{
5857
0
  GDBusConnection *connection = G_DBUS_CONNECTION (source);
5858
0
  GTask *task = user_data;
5859
0
  CallState *state = g_task_get_task_data (task);
5860
0
  GError *error = NULL;
5861
0
  GDBusMessage *reply;
5862
0
  GVariant *value = NULL;
5863
5864
0
  reply = g_dbus_connection_send_message_with_reply_finish (connection,
5865
0
                                                            result,
5866
0
                                                            &error);
5867
5868
0
  if (G_UNLIKELY (_g_dbus_debug_call ()))
5869
0
    {
5870
0
      _g_dbus_debug_print_lock ();
5871
0
      g_print ("========================================================================\n"
5872
0
               "GDBus-debug:Call:\n"
5873
0
               " <<<< ASYNC COMPLETE %s()",
5874
0
               state->method_name);
5875
5876
0
      if (reply != NULL)
5877
0
        {
5878
0
          g_print (" (serial %d)\n"
5879
0
                   "      SUCCESS\n",
5880
0
                   g_dbus_message_get_reply_serial (reply));
5881
0
        }
5882
0
      else
5883
0
        {
5884
0
          g_print ("\n"
5885
0
                   "      FAILED: %s\n",
5886
0
                   error->message);
5887
0
        }
5888
0
      _g_dbus_debug_print_unlock ();
5889
0
    }
5890
5891
0
  if (reply != NULL)
5892
0
    value = decode_method_reply (reply, state->method_name, state->reply_type, &state->fd_list, &error);
5893
5894
0
  if (error != NULL)
5895
0
    g_task_return_error (task, error);
5896
0
  else
5897
0
    g_task_return_pointer (task, value, (GDestroyNotify) g_variant_unref);
5898
5899
0
  g_clear_object (&reply);
5900
0
  g_object_unref (task);
5901
0
}
5902
5903
/* called in any thread, with the connection's lock not held */
5904
static void
5905
g_dbus_connection_call_internal (GDBusConnection        *connection,
5906
                                 const gchar            *bus_name,
5907
                                 const gchar            *object_path,
5908
                                 const gchar            *interface_name,
5909
                                 const gchar            *method_name,
5910
                                 GVariant               *parameters,
5911
                                 const GVariantType     *reply_type,
5912
                                 GDBusCallFlags          flags,
5913
                                 gint                    timeout_msec,
5914
                                 GUnixFDList            *fd_list,
5915
                                 GCancellable           *cancellable,
5916
                                 GAsyncReadyCallback     callback,
5917
                                 gpointer                user_data)
5918
0
{
5919
0
  GDBusMessage *message;
5920
0
  guint32 serial;
5921
5922
0
  g_return_if_fail (G_IS_DBUS_CONNECTION (connection));
5923
0
  g_return_if_fail (bus_name == NULL || g_dbus_is_name (bus_name));
5924
0
  g_return_if_fail (object_path != NULL && g_variant_is_object_path (object_path));
5925
0
  g_return_if_fail (interface_name != NULL && g_dbus_is_interface_name (interface_name));
5926
0
  g_return_if_fail (method_name != NULL && g_dbus_is_member_name (method_name));
5927
0
  g_return_if_fail (timeout_msec >= 0 || timeout_msec == -1);
5928
0
  g_return_if_fail ((parameters == NULL) || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE));
5929
0
  g_return_if_fail (check_initialized (connection));
5930
0
#ifdef G_OS_UNIX
5931
0
  g_return_if_fail (fd_list == NULL || G_IS_UNIX_FD_LIST (fd_list));
5932
#else
5933
  g_return_if_fail (fd_list == NULL);
5934
#endif
5935
5936
0
  message = g_dbus_message_new_method_call (bus_name,
5937
0
                                            object_path,
5938
0
                                            interface_name,
5939
0
                                            method_name);
5940
0
  add_call_flags (message, flags);
5941
0
  if (parameters != NULL)
5942
0
    g_dbus_message_set_body (message, parameters);
5943
5944
0
#ifdef G_OS_UNIX
5945
0
  if (fd_list != NULL)
5946
0
    g_dbus_message_set_unix_fd_list (message, fd_list);
5947
0
#endif
5948
5949
  /* If the user has no callback then we can just send the message with
5950
   * the G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED flag set and skip all
5951
   * the logic for processing the reply.  If the service sends the reply
5952
   * anyway then it will just be ignored.
5953
   */
5954
0
  if (callback != NULL)
5955
0
    {
5956
0
      CallState *state;
5957
0
      GTask *task;
5958
5959
0
      state = g_slice_new0 (CallState);
5960
0
      state->method_name = g_strjoin (".", interface_name, method_name, NULL);
5961
5962
0
      if (reply_type == NULL)
5963
0
        reply_type = G_VARIANT_TYPE_ANY;
5964
5965
0
      state->reply_type = g_variant_type_copy (reply_type);
5966
5967
0
      task = g_task_new (connection, cancellable, callback, user_data);
5968
0
      g_task_set_source_tag (task, g_dbus_connection_call_internal);
5969
0
      g_task_set_task_data (task, state, (GDestroyNotify) call_state_free);
5970
5971
0
      g_dbus_connection_send_message_with_reply (connection,
5972
0
                                                 message,
5973
0
                                                 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
5974
0
                                                 timeout_msec,
5975
0
                                                 &serial,
5976
0
                                                 cancellable,
5977
0
                                                 g_dbus_connection_call_done,
5978
0
                                                 task);
5979
0
    }
5980
0
  else
5981
0
    {
5982
0
      GDBusMessageFlags msg_flags;
5983
5984
0
      msg_flags = g_dbus_message_get_flags (message);
5985
0
      msg_flags |= G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED;
5986
0
      g_dbus_message_set_flags (message, msg_flags);
5987
5988
0
      g_dbus_connection_send_message (connection,
5989
0
                                      message,
5990
0
                                      G_DBUS_SEND_MESSAGE_FLAGS_NONE,
5991
0
                                      &serial, NULL);
5992
0
    }
5993
5994
0
  if (G_UNLIKELY (_g_dbus_debug_call ()))
5995
0
    {
5996
0
      _g_dbus_debug_print_lock ();
5997
0
      g_print ("========================================================================\n"
5998
0
               "GDBus-debug:Call:\n"
5999
0
               " >>>> ASYNC %s.%s()\n"
6000
0
               "      on object %s\n"
6001
0
               "      owned by name %s (serial %d)\n",
6002
0
               interface_name,
6003
0
               method_name,
6004
0
               object_path,
6005
0
               bus_name != NULL ? bus_name : "(none)",
6006
0
               serial);
6007
0
      _g_dbus_debug_print_unlock ();
6008
0
    }
6009
6010
0
  if (message != NULL)
6011
0
    g_object_unref (message);
6012
0
}
6013
6014
/* called in any thread, with the connection's lock not held */
6015
static GVariant *
6016
g_dbus_connection_call_finish_internal (GDBusConnection  *connection,
6017
                                        GUnixFDList     **out_fd_list,
6018
                                        GAsyncResult     *res,
6019
                                        GError          **error)
6020
0
{
6021
0
  GTask *task;
6022
0
  CallState *state;
6023
0
  GVariant *ret;
6024
6025
0
  g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
6026
0
  g_return_val_if_fail (g_task_is_valid (res, connection), NULL);
6027
0
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
6028
6029
0
  task = G_TASK (res);
6030
0
  state = g_task_get_task_data (task);
6031
6032
0
  ret = g_task_propagate_pointer (task, error);
6033
0
  if (!ret)
6034
0
    return NULL;
6035
6036
0
  if (out_fd_list != NULL)
6037
0
    *out_fd_list = state->fd_list != NULL ? g_object_ref (state->fd_list) : NULL;
6038
0
  return ret;
6039
0
}
6040
6041
/* called in any user thread, with the connection's lock not held */
6042
static GVariant *
6043
g_dbus_connection_call_sync_internal (GDBusConnection         *connection,
6044
                                      const gchar             *bus_name,
6045
                                      const gchar             *object_path,
6046
                                      const gchar             *interface_name,
6047
                                      const gchar             *method_name,
6048
                                      GVariant                *parameters,
6049
                                      const GVariantType      *reply_type,
6050
                                      GDBusCallFlags           flags,
6051
                                      gint                     timeout_msec,
6052
                                      GUnixFDList             *fd_list,
6053
                                      GUnixFDList            **out_fd_list,
6054
                                      GCancellable            *cancellable,
6055
                                      GError                 **error)
6056
0
{
6057
0
  GDBusMessage *message;
6058
0
  GDBusMessage *reply;
6059
0
  GVariant *result;
6060
0
  GError *local_error;
6061
0
  GDBusSendMessageFlags send_flags;
6062
6063
0
  message = NULL;
6064
0
  reply = NULL;
6065
0
  result = NULL;
6066
6067
0
  g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
6068
0
  g_return_val_if_fail (bus_name == NULL || g_dbus_is_name (bus_name), NULL);
6069
0
  g_return_val_if_fail (object_path != NULL && g_variant_is_object_path (object_path), NULL);
6070
0
  g_return_val_if_fail (interface_name != NULL && g_dbus_is_interface_name (interface_name), NULL);
6071
0
  g_return_val_if_fail (method_name != NULL && g_dbus_is_member_name (method_name), NULL);
6072
0
  g_return_val_if_fail (timeout_msec >= 0 || timeout_msec == -1, NULL);
6073
0
  g_return_val_if_fail ((parameters == NULL) || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE), NULL);
6074
0
#ifdef G_OS_UNIX
6075
0
  g_return_val_if_fail (fd_list == NULL || G_IS_UNIX_FD_LIST (fd_list), NULL);
6076
#else
6077
  g_return_val_if_fail (fd_list == NULL, NULL);
6078
#endif
6079
0
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
6080
6081
0
  if (!(flags & CALL_FLAGS_INITIALIZING))
6082
0
    g_return_val_if_fail (check_initialized (connection), FALSE);
6083
6084
0
  if (reply_type == NULL)
6085
0
    reply_type = G_VARIANT_TYPE_ANY;
6086
6087
0
  message = g_dbus_message_new_method_call (bus_name,
6088
0
                                            object_path,
6089
0
                                            interface_name,
6090
0
                                            method_name);
6091
0
  add_call_flags (message, flags);
6092
0
  if (parameters != NULL)
6093
0
    g_dbus_message_set_body (message, parameters);
6094
6095
0
#ifdef G_OS_UNIX
6096
0
  if (fd_list != NULL)
6097
0
    g_dbus_message_set_unix_fd_list (message, fd_list);
6098
0
#endif
6099
6100
0
  if (G_UNLIKELY (_g_dbus_debug_call ()))
6101
0
    {
6102
0
      _g_dbus_debug_print_lock ();
6103
0
      g_print ("========================================================================\n"
6104
0
               "GDBus-debug:Call:\n"
6105
0
               " >>>> SYNC %s.%s()\n"
6106
0
               "      on object %s\n"
6107
0
               "      owned by name %s\n",
6108
0
               interface_name,
6109
0
               method_name,
6110
0
               object_path,
6111
0
               bus_name != NULL ? bus_name : "(none)");
6112
0
      _g_dbus_debug_print_unlock ();
6113
0
    }
6114
6115
0
  local_error = NULL;
6116
6117
0
  send_flags = G_DBUS_SEND_MESSAGE_FLAGS_NONE;
6118
6119
  /* translate from one flavour of flags to another... */
6120
0
  if (flags & CALL_FLAGS_INITIALIZING)
6121
0
    send_flags |= SEND_MESSAGE_FLAGS_INITIALIZING;
6122
6123
0
  reply = g_dbus_connection_send_message_with_reply_sync (connection,
6124
0
                                                          message,
6125
0
                                                          send_flags,
6126
0
                                                          timeout_msec,
6127
0
                                                          NULL, /* guint32 *out_serial */
6128
0
                                                          cancellable,
6129
0
                                                          &local_error);
6130
6131
0
  if (G_UNLIKELY (_g_dbus_debug_call ()))
6132
0
    {
6133
0
      _g_dbus_debug_print_lock ();
6134
0
      g_print ("========================================================================\n"
6135
0
               "GDBus-debug:Call:\n"
6136
0
               " <<<< SYNC COMPLETE %s.%s()\n"
6137
0
               "      ",
6138
0
               interface_name,
6139
0
               method_name);
6140
0
      if (reply != NULL)
6141
0
        {
6142
0
          g_print ("SUCCESS\n");
6143
0
        }
6144
0
      else
6145
0
        {
6146
0
          g_print ("FAILED: %s\n",
6147
0
                   local_error->message);
6148
0
        }
6149
0
      _g_dbus_debug_print_unlock ();
6150
0
    }
6151
6152
0
  if (reply == NULL)
6153
0
    {
6154
0
      if (error != NULL)
6155
0
        *error = local_error;
6156
0
      else
6157
0
        g_error_free (local_error);
6158
0
      goto out;
6159
0
    }
6160
6161
0
  result = decode_method_reply (reply, method_name, reply_type, out_fd_list, error);
6162
6163
0
 out:
6164
0
  if (message != NULL)
6165
0
    g_object_unref (message);
6166
0
  if (reply != NULL)
6167
0
    g_object_unref (reply);
6168
6169
0
  return result;
6170
0
}
6171
6172
/* ---------------------------------------------------------------------------------------------------- */
6173
6174
/**
6175
 * g_dbus_connection_call:
6176
 * @connection: a #GDBusConnection
6177
 * @bus_name: (nullable): a unique or well-known bus name or %NULL if
6178
 *     @connection is not a message bus connection
6179
 * @object_path: path of remote object
6180
 * @interface_name: D-Bus interface to invoke method on
6181
 * @method_name: the name of the method to invoke
6182
 * @parameters: (nullable): a #GVariant tuple with parameters for the method
6183
 *     or %NULL if not passing parameters
6184
 * @reply_type: (nullable): the expected type of the reply (which will be a
6185
 *     tuple), or %NULL
6186
 * @flags: flags from the #GDBusCallFlags enumeration
6187
 * @timeout_msec: the timeout in milliseconds, -1 to use the default
6188
 *     timeout or %G_MAXINT for no timeout
6189
 * @cancellable: (nullable): a #GCancellable or %NULL
6190
 * @callback: (nullable): a #GAsyncReadyCallback to call when the request
6191
 *     is satisfied or %NULL if you don't care about the result of the
6192
 *     method invocation
6193
 * @user_data: the data to pass to @callback
6194
 *
6195
 * Asynchronously invokes the @method_name method on the
6196
 * @interface_name D-Bus interface on the remote object at
6197
 * @object_path owned by @bus_name.
6198
 *
6199
 * If @connection is closed then the operation will fail with
6200
 * %G_IO_ERROR_CLOSED. If @cancellable is canceled, the operation will
6201
 * fail with %G_IO_ERROR_CANCELLED. If @parameters contains a value
6202
 * not compatible with the D-Bus protocol, the operation fails with
6203
 * %G_IO_ERROR_INVALID_ARGUMENT.
6204
 *
6205
 * If @reply_type is non-%NULL then the reply will be checked for having this type and an
6206
 * error will be raised if it does not match.  Said another way, if you give a @reply_type
6207
 * then any non-%NULL return value will be of this type. Unless it’s
6208
 * %G_VARIANT_TYPE_UNIT, the @reply_type will be a tuple containing one or more
6209
 * values.
6210
 *
6211
 * If the @parameters #GVariant is floating, it is consumed. This allows
6212
 * convenient 'inline' use of g_variant_new(), e.g.:
6213
 * |[<!-- language="C" -->
6214
 *  g_dbus_connection_call (connection,
6215
 *                          "org.freedesktop.StringThings",
6216
 *                          "/org/freedesktop/StringThings",
6217
 *                          "org.freedesktop.StringThings",
6218
 *                          "TwoStrings",
6219
 *                          g_variant_new ("(ss)",
6220
 *                                         "Thing One",
6221
 *                                         "Thing Two"),
6222
 *                          NULL,
6223
 *                          G_DBUS_CALL_FLAGS_NONE,
6224
 *                          -1,
6225
 *                          NULL,
6226
 *                          (GAsyncReadyCallback) two_strings_done,
6227
 *                          NULL);
6228
 * ]|
6229
 *
6230
 * This is an asynchronous method. When the operation is finished,
6231
 * @callback will be invoked in the
6232
 * [thread-default main context][g-main-context-push-thread-default]
6233
 * of the thread you are calling this method from. You can then call
6234
 * g_dbus_connection_call_finish() to get the result of the operation.
6235
 * See g_dbus_connection_call_sync() for the synchronous version of this
6236
 * function.
6237
 *
6238
 * If @callback is %NULL then the D-Bus method call message will be sent with
6239
 * the %G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED flag set.
6240
 *
6241
 * Since: 2.26
6242
 */
6243
void
6244
g_dbus_connection_call (GDBusConnection     *connection,
6245
                        const gchar         *bus_name,
6246
                        const gchar         *object_path,
6247
                        const gchar         *interface_name,
6248
                        const gchar         *method_name,
6249
                        GVariant            *parameters,
6250
                        const GVariantType  *reply_type,
6251
                        GDBusCallFlags       flags,
6252
                        gint                 timeout_msec,
6253
                        GCancellable        *cancellable,
6254
                        GAsyncReadyCallback  callback,
6255
                        gpointer             user_data)
6256
0
{
6257
0
  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);
6258
0
}
6259
6260
/**
6261
 * g_dbus_connection_call_finish:
6262
 * @connection: a #GDBusConnection
6263
 * @res: a #GAsyncResult obtained from the #GAsyncReadyCallback passed to g_dbus_connection_call()
6264
 * @error: return location for error or %NULL
6265
 *
6266
 * Finishes an operation started with g_dbus_connection_call().
6267
 *
6268
 * Returns: (transfer full): %NULL if @error is set. Otherwise a non-floating
6269
 *     #GVariant tuple with return values. Free with g_variant_unref().
6270
 *
6271
 * Since: 2.26
6272
 */
6273
GVariant *
6274
g_dbus_connection_call_finish (GDBusConnection  *connection,
6275
                               GAsyncResult     *res,
6276
                               GError          **error)
6277
0
{
6278
0
  return g_dbus_connection_call_finish_internal (connection, NULL, res, error);
6279
0
}
6280
6281
/**
6282
 * g_dbus_connection_call_sync:
6283
 * @connection: a #GDBusConnection
6284
 * @bus_name: (nullable): a unique or well-known bus name or %NULL if
6285
 *     @connection is not a message bus connection
6286
 * @object_path: path of remote object
6287
 * @interface_name: D-Bus interface to invoke method on
6288
 * @method_name: the name of the method to invoke
6289
 * @parameters: (nullable): a #GVariant tuple with parameters for the method
6290
 *     or %NULL if not passing parameters
6291
 * @reply_type: (nullable): the expected type of the reply, or %NULL
6292
 * @flags: flags from the #GDBusCallFlags enumeration
6293
 * @timeout_msec: the timeout in milliseconds, -1 to use the default
6294
 *     timeout or %G_MAXINT for no timeout
6295
 * @cancellable: (nullable): a #GCancellable or %NULL
6296
 * @error: return location for error or %NULL
6297
 *
6298
 * Synchronously invokes the @method_name method on the
6299
 * @interface_name D-Bus interface on the remote object at
6300
 * @object_path owned by @bus_name.
6301
 *
6302
 * If @connection is closed then the operation will fail with
6303
 * %G_IO_ERROR_CLOSED. If @cancellable is canceled, the
6304
 * operation will fail with %G_IO_ERROR_CANCELLED. If @parameters
6305
 * contains a value not compatible with the D-Bus protocol, the operation
6306
 * fails with %G_IO_ERROR_INVALID_ARGUMENT.
6307
 *
6308
 * If @reply_type is non-%NULL then the reply will be checked for having
6309
 * this type and an error will be raised if it does not match.  Said
6310
 * another way, if you give a @reply_type then any non-%NULL return
6311
 * value will be of this type.
6312
 *
6313
 * If the @parameters #GVariant is floating, it is consumed.
6314
 * This allows convenient 'inline' use of g_variant_new(), e.g.:
6315
 * |[<!-- language="C" -->
6316
 *  g_dbus_connection_call_sync (connection,
6317
 *                               "org.freedesktop.StringThings",
6318
 *                               "/org/freedesktop/StringThings",
6319
 *                               "org.freedesktop.StringThings",
6320
 *                               "TwoStrings",
6321
 *                               g_variant_new ("(ss)",
6322
 *                                              "Thing One",
6323
 *                                              "Thing Two"),
6324
 *                               NULL,
6325
 *                               G_DBUS_CALL_FLAGS_NONE,
6326
 *                               -1,
6327
 *                               NULL,
6328
 *                               &error);
6329
 * ]|
6330
 *
6331
 * The calling thread is blocked until a reply is received. See
6332
 * g_dbus_connection_call() for the asynchronous version of
6333
 * this method.
6334
 *
6335
 * Returns: (transfer full): %NULL if @error is set. Otherwise a non-floating
6336
 *     #GVariant tuple with return values. Free with g_variant_unref().
6337
 *
6338
 * Since: 2.26
6339
 */
6340
GVariant *
6341
g_dbus_connection_call_sync (GDBusConnection     *connection,
6342
                             const gchar         *bus_name,
6343
                             const gchar         *object_path,
6344
                             const gchar         *interface_name,
6345
                             const gchar         *method_name,
6346
                             GVariant            *parameters,
6347
                             const GVariantType  *reply_type,
6348
                             GDBusCallFlags       flags,
6349
                             gint                 timeout_msec,
6350
                             GCancellable        *cancellable,
6351
                             GError             **error)
6352
0
{
6353
0
  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);
6354
0
}
6355
6356
/* ---------------------------------------------------------------------------------------------------- */
6357
6358
#ifdef G_OS_UNIX
6359
6360
/**
6361
 * g_dbus_connection_call_with_unix_fd_list:
6362
 * @connection: a #GDBusConnection
6363
 * @bus_name: (nullable): a unique or well-known bus name or %NULL if
6364
 *     @connection is not a message bus connection
6365
 * @object_path: path of remote object
6366
 * @interface_name: D-Bus interface to invoke method on
6367
 * @method_name: the name of the method to invoke
6368
 * @parameters: (nullable): a #GVariant tuple with parameters for the method
6369
 *     or %NULL if not passing parameters
6370
 * @reply_type: (nullable): the expected type of the reply, or %NULL
6371
 * @flags: flags from the #GDBusCallFlags enumeration
6372
 * @timeout_msec: the timeout in milliseconds, -1 to use the default
6373
 *     timeout or %G_MAXINT for no timeout
6374
 * @fd_list: (nullable): a #GUnixFDList or %NULL
6375
 * @cancellable: (nullable): a #GCancellable or %NULL
6376
 * @callback: (nullable): a #GAsyncReadyCallback to call when the request is
6377
 *     satisfied or %NULL if you don't * care about the result of the
6378
 *     method invocation
6379
 * @user_data: The data to pass to @callback.
6380
 *
6381
 * Like g_dbus_connection_call() but also takes a #GUnixFDList object.
6382
 *
6383
 * The file descriptors normally correspond to %G_VARIANT_TYPE_HANDLE
6384
 * values in the body of the message. For example, if a message contains
6385
 * two file descriptors, @fd_list would have length 2, and
6386
 * `g_variant_new_handle (0)` and `g_variant_new_handle (1)` would appear
6387
 * somewhere in the body of the message (not necessarily in that order!)
6388
 * to represent the file descriptors at indexes 0 and 1 respectively.
6389
 *
6390
 * When designing D-Bus APIs that are intended to be interoperable,
6391
 * please note that non-GDBus implementations of D-Bus can usually only
6392
 * access file descriptors if they are referenced in this way by a
6393
 * value of type %G_VARIANT_TYPE_HANDLE in the body of the message.
6394
 *
6395
 * This method is only available on UNIX.
6396
 *
6397
 * Since: 2.30
6398
 */
6399
void
6400
g_dbus_connection_call_with_unix_fd_list (GDBusConnection     *connection,
6401
                                          const gchar         *bus_name,
6402
                                          const gchar         *object_path,
6403
                                          const gchar         *interface_name,
6404
                                          const gchar         *method_name,
6405
                                          GVariant            *parameters,
6406
                                          const GVariantType  *reply_type,
6407
                                          GDBusCallFlags       flags,
6408
                                          gint                 timeout_msec,
6409
                                          GUnixFDList         *fd_list,
6410
                                          GCancellable        *cancellable,
6411
                                          GAsyncReadyCallback  callback,
6412
                                          gpointer             user_data)
6413
0
{
6414
0
  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);
6415
0
}
6416
6417
/**
6418
 * g_dbus_connection_call_with_unix_fd_list_finish:
6419
 * @connection: a #GDBusConnection
6420
 * @out_fd_list: (out) (optional): return location for a #GUnixFDList or %NULL
6421
 * @res: a #GAsyncResult obtained from the #GAsyncReadyCallback passed to
6422
 *     g_dbus_connection_call_with_unix_fd_list()
6423
 * @error: return location for error or %NULL
6424
 *
6425
 * Finishes an operation started with g_dbus_connection_call_with_unix_fd_list().
6426
 *
6427
 * The file descriptors normally correspond to %G_VARIANT_TYPE_HANDLE
6428
 * values in the body of the message. For example,
6429
 * if g_variant_get_handle() returns 5, that is intended to be a reference
6430
 * to the file descriptor that can be accessed by
6431
 * `g_unix_fd_list_get (*out_fd_list, 5, ...)`.
6432
 *
6433
 * When designing D-Bus APIs that are intended to be interoperable,
6434
 * please note that non-GDBus implementations of D-Bus can usually only
6435
 * access file descriptors if they are referenced in this way by a
6436
 * value of type %G_VARIANT_TYPE_HANDLE in the body of the message.
6437
 *
6438
 * Returns: (transfer full): %NULL if @error is set. Otherwise a non-floating
6439
 *     #GVariant tuple with return values. Free with g_variant_unref().
6440
 *
6441
 * Since: 2.30
6442
 */
6443
GVariant *
6444
g_dbus_connection_call_with_unix_fd_list_finish (GDBusConnection  *connection,
6445
                                                 GUnixFDList     **out_fd_list,
6446
                                                 GAsyncResult     *res,
6447
                                                 GError          **error)
6448
0
{
6449
0
  return g_dbus_connection_call_finish_internal (connection, out_fd_list, res, error);
6450
0
}
6451
6452
/**
6453
 * g_dbus_connection_call_with_unix_fd_list_sync:
6454
 * @connection: a #GDBusConnection
6455
 * @bus_name: (nullable): a unique or well-known bus name or %NULL
6456
 *     if @connection is not a message bus connection
6457
 * @object_path: path of remote object
6458
 * @interface_name: D-Bus interface to invoke method on
6459
 * @method_name: the name of the method to invoke
6460
 * @parameters: (nullable): a #GVariant tuple with parameters for
6461
 *     the method or %NULL if not passing parameters
6462
 * @reply_type: (nullable): the expected type of the reply, or %NULL
6463
 * @flags: flags from the #GDBusCallFlags enumeration
6464
 * @timeout_msec: the timeout in milliseconds, -1 to use the default
6465
 *     timeout or %G_MAXINT for no timeout
6466
 * @fd_list: (nullable): a #GUnixFDList or %NULL
6467
 * @out_fd_list: (out) (optional): return location for a #GUnixFDList or %NULL
6468
 * @cancellable: (nullable): a #GCancellable or %NULL
6469
 * @error: return location for error or %NULL
6470
 *
6471
 * Like g_dbus_connection_call_sync() but also takes and returns #GUnixFDList objects.
6472
 * See g_dbus_connection_call_with_unix_fd_list() and
6473
 * g_dbus_connection_call_with_unix_fd_list_finish() for more details.
6474
 *
6475
 * This method is only available on UNIX.
6476
 *
6477
 * Returns: (transfer full): %NULL if @error is set. Otherwise a non-floating
6478
 *     #GVariant tuple with return values. Free with g_variant_unref().
6479
 *
6480
 * Since: 2.30
6481
 */
6482
GVariant *
6483
g_dbus_connection_call_with_unix_fd_list_sync (GDBusConnection     *connection,
6484
                                               const gchar         *bus_name,
6485
                                               const gchar         *object_path,
6486
                                               const gchar         *interface_name,
6487
                                               const gchar         *method_name,
6488
                                               GVariant            *parameters,
6489
                                               const GVariantType  *reply_type,
6490
                                               GDBusCallFlags       flags,
6491
                                               gint                 timeout_msec,
6492
                                               GUnixFDList         *fd_list,
6493
                                               GUnixFDList        **out_fd_list,
6494
                                               GCancellable        *cancellable,
6495
                                               GError             **error)
6496
0
{
6497
0
  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);
6498
0
}
6499
6500
#endif /* G_OS_UNIX */
6501
6502
/* ---------------------------------------------------------------------------------------------------- */
6503
6504
/* called without lock held in the thread where the caller registered
6505
 * the subtree
6506
 */
6507
static gboolean
6508
handle_subtree_introspect (GDBusConnection *connection,
6509
                           ExportedSubtree *es,
6510
                           GDBusMessage    *message)
6511
0
{
6512
0
  GString *s;
6513
0
  gboolean handled;
6514
0
  GDBusMessage *reply;
6515
0
  gchar **children;
6516
0
  gboolean is_root;
6517
0
  const gchar *sender;
6518
0
  const gchar *requested_object_path;
6519
0
  const gchar *requested_node;
6520
0
  GDBusInterfaceInfo **interfaces;
6521
0
  guint n;
6522
0
  gchar **subnode_paths;
6523
0
  gboolean has_properties_interface;
6524
0
  gboolean has_introspectable_interface;
6525
6526
0
  handled = FALSE;
6527
6528
0
  requested_object_path = g_dbus_message_get_path (message);
6529
0
  sender = g_dbus_message_get_sender (message);
6530
0
  is_root = (g_strcmp0 (requested_object_path, es->object_path) == 0);
6531
6532
0
  s = g_string_new (NULL);
6533
0
  introspect_append_header (s);
6534
6535
  /* Strictly we don't need the children in dynamic mode, but we avoid the
6536
   * conditionals to preserve code clarity
6537
   */
6538
0
  children = es->vtable->enumerate (es->connection,
6539
0
                                    sender,
6540
0
                                    es->object_path,
6541
0
                                    es->user_data);
6542
6543
0
  if (!is_root)
6544
0
    {
6545
0
      requested_node = strrchr (requested_object_path, '/') + 1;
6546
6547
      /* Assert existence of object if we are not dynamic */
6548
0
      if (!(es->flags & G_DBUS_SUBTREE_FLAGS_DISPATCH_TO_UNENUMERATED_NODES) &&
6549
0
          !g_strv_contains ((const gchar * const *) children, requested_node))
6550
0
        goto out;
6551
0
    }
6552
0
  else
6553
0
    {
6554
0
      requested_node = NULL;
6555
0
    }
6556
6557
0
  interfaces = es->vtable->introspect (es->connection,
6558
0
                                       sender,
6559
0
                                       es->object_path,
6560
0
                                       requested_node,
6561
0
                                       es->user_data);
6562
0
  if (interfaces != NULL)
6563
0
    {
6564
0
      has_properties_interface = FALSE;
6565
0
      has_introspectable_interface = FALSE;
6566
6567
0
      for (n = 0; interfaces[n] != NULL; n++)
6568
0
        {
6569
0
          if (strcmp (interfaces[n]->name, "org.freedesktop.DBus.Properties") == 0)
6570
0
            has_properties_interface = TRUE;
6571
0
          else if (strcmp (interfaces[n]->name, "org.freedesktop.DBus.Introspectable") == 0)
6572
0
            has_introspectable_interface = TRUE;
6573
0
        }
6574
0
      if (!has_properties_interface)
6575
0
        g_string_append (s, introspect_properties_interface);
6576
0
      if (!has_introspectable_interface)
6577
0
        g_string_append (s, introspect_introspectable_interface);
6578
6579
0
      for (n = 0; interfaces[n] != NULL; n++)
6580
0
        {
6581
0
          g_dbus_interface_info_generate_xml (interfaces[n], 2, s);
6582
0
          g_dbus_interface_info_unref (interfaces[n]);
6583
0
        }
6584
0
      g_free (interfaces);
6585
0
    }
6586
6587
  /* then include <node> entries from the Subtree for the root */
6588
0
  if (is_root)
6589
0
    {
6590
0
      for (n = 0; children != NULL && children[n] != NULL; n++)
6591
0
        g_string_append_printf (s, "  <node name=\"%s\"/>\n", children[n]);
6592
0
    }
6593
6594
  /* finally include nodes registered below us */
6595
0
  subnode_paths = g_dbus_connection_list_registered (es->connection, requested_object_path);
6596
0
  for (n = 0; subnode_paths != NULL && subnode_paths[n] != NULL; n++)
6597
0
    g_string_append_printf (s, "  <node name=\"%s\"/>\n", subnode_paths[n]);
6598
0
  g_strfreev (subnode_paths);
6599
6600
0
  g_string_append (s, "</node>\n");
6601
6602
0
  reply = g_dbus_message_new_method_reply (message);
6603
0
  g_dbus_message_set_body (reply, g_variant_new ("(s)", s->str));
6604
0
  g_dbus_connection_send_message (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
6605
0
  g_object_unref (reply);
6606
6607
0
  handled = TRUE;
6608
6609
0
 out:
6610
0
  g_string_free (s, TRUE);
6611
0
  g_strfreev (children);
6612
0
  return handled;
6613
0
}
6614
6615
/* called without lock held in the thread where the caller registered
6616
 * the subtree
6617
 */
6618
static gboolean
6619
handle_subtree_method_invocation (GDBusConnection *connection,
6620
                                  ExportedSubtree *es,
6621
                                  GDBusMessage    *message)
6622
0
{
6623
0
  gboolean handled;
6624
0
  const gchar *sender;
6625
0
  const gchar *interface_name;
6626
0
  const gchar *member;
6627
0
  const gchar *signature;
6628
0
  const gchar *requested_object_path;
6629
0
  const gchar *requested_node;
6630
0
  gboolean is_root;
6631
0
  GDBusInterfaceInfo *interface_info;
6632
0
  const GDBusInterfaceVTable *interface_vtable;
6633
0
  gpointer interface_user_data;
6634
0
  guint n;
6635
0
  GDBusInterfaceInfo **interfaces;
6636
0
  gboolean is_property_get;
6637
0
  gboolean is_property_set;
6638
0
  gboolean is_property_get_all;
6639
6640
0
  handled = FALSE;
6641
0
  interfaces = NULL;
6642
6643
0
  requested_object_path = g_dbus_message_get_path (message);
6644
0
  sender = g_dbus_message_get_sender (message);
6645
0
  interface_name = g_dbus_message_get_interface (message);
6646
0
  member = g_dbus_message_get_member (message);
6647
0
  signature = g_dbus_message_get_signature (message);
6648
0
  is_root = (g_strcmp0 (requested_object_path, es->object_path) == 0);
6649
6650
0
  is_property_get = FALSE;
6651
0
  is_property_set = FALSE;
6652
0
  is_property_get_all = FALSE;
6653
0
  if (g_strcmp0 (interface_name, "org.freedesktop.DBus.Properties") == 0)
6654
0
    {
6655
0
      if (g_strcmp0 (member, "Get") == 0 && g_strcmp0 (signature, "ss") == 0)
6656
0
        is_property_get = TRUE;
6657
0
      else if (g_strcmp0 (member, "Set") == 0 && g_strcmp0 (signature, "ssv") == 0)
6658
0
        is_property_set = TRUE;
6659
0
      else if (g_strcmp0 (member, "GetAll") == 0 && g_strcmp0 (signature, "s") == 0)
6660
0
        is_property_get_all = TRUE;
6661
0
    }
6662
6663
0
  if (!is_root)
6664
0
    {
6665
0
      requested_node = strrchr (requested_object_path, '/') + 1;
6666
6667
0
      if (~es->flags & G_DBUS_SUBTREE_FLAGS_DISPATCH_TO_UNENUMERATED_NODES)
6668
0
        {
6669
          /* We don't want to dispatch to unenumerated
6670
           * nodes, so ensure that the child exists.
6671
           */
6672
0
          gchar **children;
6673
0
          gboolean exists;
6674
6675
0
          children = es->vtable->enumerate (es->connection,
6676
0
                                            sender,
6677
0
                                            es->object_path,
6678
0
                                            es->user_data);
6679
6680
0
          exists = g_strv_contains ((const gchar * const *) children, requested_node);
6681
0
          g_strfreev (children);
6682
6683
0
          if (!exists)
6684
0
            goto out;
6685
0
        }
6686
0
    }
6687
0
  else
6688
0
    {
6689
0
      requested_node = NULL;
6690
0
    }
6691
6692
  /* get introspection data for the node */
6693
0
  interfaces = es->vtable->introspect (es->connection,
6694
0
                                       sender,
6695
0
                                       requested_object_path,
6696
0
                                       requested_node,
6697
0
                                       es->user_data);
6698
6699
0
  if (interfaces == NULL)
6700
0
    goto out;
6701
6702
0
  interface_info = NULL;
6703
0
  for (n = 0; interfaces[n] != NULL; n++)
6704
0
    {
6705
0
      if (g_strcmp0 (interfaces[n]->name, interface_name) == 0)
6706
0
        interface_info = interfaces[n];
6707
0
    }
6708
6709
  /* dispatch the call if the user wants to handle it */
6710
0
  if (interface_info != NULL)
6711
0
    {
6712
      /* figure out where to dispatch the method call */
6713
0
      interface_user_data = NULL;
6714
0
      interface_vtable = es->vtable->dispatch (es->connection,
6715
0
                                               sender,
6716
0
                                               es->object_path,
6717
0
                                               interface_name,
6718
0
                                               requested_node,
6719
0
                                               &interface_user_data,
6720
0
                                               es->user_data);
6721
0
      if (interface_vtable == NULL)
6722
0
        goto out;
6723
6724
0
      CONNECTION_LOCK (connection);
6725
0
      handled = validate_and_maybe_schedule_method_call (es->connection,
6726
0
                                                         message,
6727
0
                                                         0,
6728
0
                                                         es->id,
6729
0
                                                         interface_info,
6730
0
                                                         interface_vtable,
6731
0
                                                         es->context,
6732
0
                                                         interface_user_data);
6733
0
      CONNECTION_UNLOCK (connection);
6734
0
    }
6735
  /* handle org.freedesktop.DBus.Properties interface if not explicitly handled */
6736
0
  else if (is_property_get || is_property_set || is_property_get_all)
6737
0
    {
6738
0
      if (is_property_get)
6739
0
        g_variant_get (g_dbus_message_get_body (message), "(&s&s)", &interface_name, NULL);
6740
0
      else if (is_property_set)
6741
0
        g_variant_get (g_dbus_message_get_body (message), "(&s&sv)", &interface_name, NULL, NULL);
6742
0
      else if (is_property_get_all)
6743
0
        g_variant_get (g_dbus_message_get_body (message), "(&s)", &interface_name, NULL, NULL);
6744
0
      else
6745
0
        g_assert_not_reached ();
6746
6747
      /* see if the object supports this interface at all */
6748
0
      for (n = 0; interfaces[n] != NULL; n++)
6749
0
        {
6750
0
          if (g_strcmp0 (interfaces[n]->name, interface_name) == 0)
6751
0
            interface_info = interfaces[n];
6752
0
        }
6753
6754
      /* Fail with org.freedesktop.DBus.Error.InvalidArgs if the user-code
6755
       * claims it won't support the interface
6756
       */
6757
0
      if (interface_info == NULL)
6758
0
        {
6759
0
          GDBusMessage *reply;
6760
0
          reply = g_dbus_message_new_method_error (message,
6761
0
                                                   "org.freedesktop.DBus.Error.InvalidArgs",
6762
0
                                                   _("No such interface ā€œ%sā€"),
6763
0
                                                   interface_name);
6764
0
          g_dbus_connection_send_message (es->connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
6765
0
          g_object_unref (reply);
6766
0
          handled = TRUE;
6767
0
          goto out;
6768
0
        }
6769
6770
      /* figure out where to dispatch the property get/set/getall calls */
6771
0
      interface_user_data = NULL;
6772
0
      interface_vtable = es->vtable->dispatch (es->connection,
6773
0
                                               sender,
6774
0
                                               es->object_path,
6775
0
                                               interface_name,
6776
0
                                               requested_node,
6777
0
                                               &interface_user_data,
6778
0
                                               es->user_data);
6779
0
      if (interface_vtable == NULL)
6780
0
        {
6781
0
          g_warning ("The subtree introspection function indicates that '%s' "
6782
0
                     "is a valid interface name, but calling the dispatch "
6783
0
                     "function on that interface gave us NULL", interface_name);
6784
0
          goto out;
6785
0
        }
6786
6787
0
      if (is_property_get || is_property_set)
6788
0
        {
6789
0
          CONNECTION_LOCK (connection);
6790
0
          handled = validate_and_maybe_schedule_property_getset (es->connection,
6791
0
                                                                 message,
6792
0
                                                                 0,
6793
0
                                                                 es->id,
6794
0
                                                                 is_property_get,
6795
0
                                                                 interface_info,
6796
0
                                                                 interface_vtable,
6797
0
                                                                 es->context,
6798
0
                                                                 interface_user_data);
6799
0
          CONNECTION_UNLOCK (connection);
6800
0
        }
6801
0
      else if (is_property_get_all)
6802
0
        {
6803
0
          CONNECTION_LOCK (connection);
6804
0
          handled = validate_and_maybe_schedule_property_get_all (es->connection,
6805
0
                                                                  message,
6806
0
                                                                  0,
6807
0
                                                                  es->id,
6808
0
                                                                  interface_info,
6809
0
                                                                  interface_vtable,
6810
0
                                                                  es->context,
6811
0
                                                                  interface_user_data);
6812
0
          CONNECTION_UNLOCK (connection);
6813
0
        }
6814
0
    }
6815
6816
0
 out:
6817
0
  if (interfaces != NULL)
6818
0
    {
6819
0
      for (n = 0; interfaces[n] != NULL; n++)
6820
0
        g_dbus_interface_info_unref (interfaces[n]);
6821
0
      g_free (interfaces);
6822
0
    }
6823
6824
0
  return handled;
6825
0
}
6826
6827
typedef struct
6828
{
6829
  GDBusMessage *message;  /* (owned) */
6830
  ExportedSubtree *es;  /* (owned) */
6831
} SubtreeDeferredData;
6832
6833
static void
6834
subtree_deferred_data_free (SubtreeDeferredData *data)
6835
0
{
6836
0
  g_clear_object (&data->message);
6837
0
  exported_subtree_unref (data->es);
6838
0
  g_free (data);
6839
0
}
6840
6841
/* called without lock held in the thread where the caller registered the subtree */
6842
static gboolean
6843
process_subtree_vtable_message_in_idle_cb (gpointer _data)
6844
0
{
6845
0
  SubtreeDeferredData *data = _data;
6846
0
  gboolean handled;
6847
6848
0
  handled = FALSE;
6849
6850
0
  if (g_strcmp0 (g_dbus_message_get_interface (data->message), "org.freedesktop.DBus.Introspectable") == 0 &&
6851
0
      g_strcmp0 (g_dbus_message_get_member (data->message), "Introspect") == 0 &&
6852
0
      g_strcmp0 (g_dbus_message_get_signature (data->message), "") == 0)
6853
0
    handled = handle_subtree_introspect (data->es->connection,
6854
0
                                         data->es,
6855
0
                                         data->message);
6856
0
  else
6857
0
    handled = handle_subtree_method_invocation (data->es->connection,
6858
0
                                                data->es,
6859
0
                                                data->message);
6860
6861
0
  if (!handled)
6862
0
    {
6863
0
      CONNECTION_LOCK (data->es->connection);
6864
0
      handled = handle_generic_unlocked (data->es->connection, data->message);
6865
0
      CONNECTION_UNLOCK (data->es->connection);
6866
0
    }
6867
6868
  /* if we couldn't handle the request, just bail with the UnknownMethod error */
6869
0
  if (!handled)
6870
0
    {
6871
0
      GDBusMessage *reply;
6872
0
      reply = g_dbus_message_new_method_error (data->message,
6873
0
                                               "org.freedesktop.DBus.Error.UnknownMethod",
6874
0
                                               _("Method ā€œ%sā€ on interface ā€œ%sā€ with signature ā€œ%sā€ does not exist"),
6875
0
                                               g_dbus_message_get_member (data->message),
6876
0
                                               g_dbus_message_get_interface (data->message),
6877
0
                                               g_dbus_message_get_signature (data->message));
6878
0
      g_dbus_connection_send_message (data->es->connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
6879
0
      g_object_unref (reply);
6880
0
    }
6881
6882
0
  return FALSE;
6883
0
}
6884
6885
/* called in GDBusWorker thread with connection's lock held */
6886
static gboolean
6887
subtree_message_func (GDBusConnection *connection,
6888
                      ExportedSubtree *es,
6889
                      GDBusMessage    *message)
6890
0
{
6891
0
  GSource *idle_source;
6892
0
  SubtreeDeferredData *data;
6893
6894
0
  data = g_new0 (SubtreeDeferredData, 1);
6895
0
  data->message = g_object_ref (message);
6896
0
  data->es = exported_subtree_ref (es);
6897
6898
  /* defer this call to an idle handler in the right thread */
6899
0
  idle_source = g_idle_source_new ();
6900
0
  g_source_set_priority (idle_source, G_PRIORITY_HIGH);
6901
0
  g_source_set_callback (idle_source,
6902
0
                         process_subtree_vtable_message_in_idle_cb,
6903
0
                         data,
6904
0
                         (GDestroyNotify) subtree_deferred_data_free);
6905
0
  g_source_set_static_name (idle_source, "[gio] process_subtree_vtable_message_in_idle_cb");
6906
0
  g_source_attach (idle_source, es->context);
6907
0
  g_source_unref (idle_source);
6908
6909
  /* since we own the entire subtree, handlers for objects not in the subtree have been
6910
   * tried already by libdbus-1 - so we just need to ensure that we're always going
6911
   * to reply to the message
6912
   */
6913
0
  return TRUE;
6914
0
}
6915
6916
/**
6917
 * g_dbus_connection_register_subtree:
6918
 * @connection: a #GDBusConnection
6919
 * @object_path: the object path to register the subtree at
6920
 * @vtable: a #GDBusSubtreeVTable to enumerate, introspect and
6921
 *     dispatch nodes in the subtree
6922
 * @flags: flags used to fine tune the behavior of the subtree
6923
 * @user_data: data to pass to functions in @vtable
6924
 * @user_data_free_func: function to call when the subtree is unregistered
6925
 * @error: return location for error or %NULL
6926
 *
6927
 * Registers a whole subtree of dynamic objects.
6928
 *
6929
 * The @enumerate and @introspection functions in @vtable are used to
6930
 * convey, to remote callers, what nodes exist in the subtree rooted
6931
 * by @object_path.
6932
 *
6933
 * When handling remote calls into any node in the subtree, first the
6934
 * @enumerate function is used to check if the node exists. If the node exists
6935
 * or the %G_DBUS_SUBTREE_FLAGS_DISPATCH_TO_UNENUMERATED_NODES flag is set
6936
 * the @introspection function is used to check if the node supports the
6937
 * requested method. If so, the @dispatch function is used to determine
6938
 * where to dispatch the call. The collected #GDBusInterfaceVTable and
6939
 * #gpointer will be used to call into the interface vtable for processing
6940
 * the request.
6941
 *
6942
 * All calls into user-provided code will be invoked in the
6943
 * [thread-default main context][g-main-context-push-thread-default]
6944
 * of the thread you are calling this method from.
6945
 *
6946
 * If an existing subtree is already registered at @object_path or
6947
 * then @error is set to %G_IO_ERROR_EXISTS.
6948
 *
6949
 * Note that it is valid to register regular objects (using
6950
 * g_dbus_connection_register_object()) in a subtree registered with
6951
 * g_dbus_connection_register_subtree() - if so, the subtree handler
6952
 * is tried as the last resort. One way to think about a subtree
6953
 * handler is to consider it a fallback handler for object paths not
6954
 * registered via g_dbus_connection_register_object() or other bindings.
6955
 *
6956
 * Note that @vtable will be copied so you cannot change it after
6957
 * registration.
6958
 *
6959
 * See this [server][gdbus-subtree-server] for an example of how to use
6960
 * this method.
6961
 *
6962
 * Returns: 0 if @error is set, otherwise a subtree registration ID (never 0)
6963
 * that can be used with g_dbus_connection_unregister_subtree()
6964
 *
6965
 * Since: 2.26
6966
 */
6967
guint
6968
g_dbus_connection_register_subtree (GDBusConnection           *connection,
6969
                                    const gchar               *object_path,
6970
                                    const GDBusSubtreeVTable  *vtable,
6971
                                    GDBusSubtreeFlags          flags,
6972
                                    gpointer                   user_data,
6973
                                    GDestroyNotify             user_data_free_func,
6974
                                    GError                   **error)
6975
0
{
6976
0
  guint ret;
6977
0
  ExportedSubtree *es;
6978
6979
0
  g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), 0);
6980
0
  g_return_val_if_fail (object_path != NULL && g_variant_is_object_path (object_path), 0);
6981
0
  g_return_val_if_fail (vtable != NULL, 0);
6982
0
  g_return_val_if_fail (error == NULL || *error == NULL, 0);
6983
0
  g_return_val_if_fail (check_initialized (connection), 0);
6984
6985
0
  ret = 0;
6986
6987
0
  CONNECTION_LOCK (connection);
6988
6989
0
  es = g_hash_table_lookup (connection->map_object_path_to_es, object_path);
6990
0
  if (es != NULL)
6991
0
    {
6992
0
      g_set_error (error,
6993
0
                   G_IO_ERROR,
6994
0
                   G_IO_ERROR_EXISTS,
6995
0
                   _("A subtree is already exported for %s"),
6996
0
                   object_path);
6997
0
      goto out;
6998
0
    }
6999
7000
0
  es = g_new0 (ExportedSubtree, 1);
7001
0
  es->refcount = 1;
7002
0
  es->object_path = g_strdup (object_path);
7003
0
  es->connection = connection;
7004
7005
0
  es->vtable = _g_dbus_subtree_vtable_copy (vtable);
7006
0
  es->flags = flags;
7007
0
  es->id = (guint) g_atomic_int_add (&_global_subtree_registration_id, 1); /* TODO: overflow etc. */
7008
0
  es->user_data = user_data;
7009
0
  es->user_data_free_func = user_data_free_func;
7010
0
  es->context = g_main_context_ref_thread_default ();
7011
7012
0
  g_hash_table_insert (connection->map_object_path_to_es, es->object_path, es);
7013
0
  g_hash_table_insert (connection->map_id_to_es,
7014
0
                       GUINT_TO_POINTER (es->id),
7015
0
                       es);
7016
7017
0
  ret = es->id;
7018
7019
0
 out:
7020
0
  CONNECTION_UNLOCK (connection);
7021
7022
0
  return ret;
7023
0
}
7024
7025
/* ---------------------------------------------------------------------------------------------------- */
7026
7027
/**
7028
 * g_dbus_connection_unregister_subtree:
7029
 * @connection: a #GDBusConnection
7030
 * @registration_id: a subtree registration id obtained from
7031
 *     g_dbus_connection_register_subtree()
7032
 *
7033
 * Unregisters a subtree.
7034
 *
7035
 * Returns: %TRUE if the subtree was unregistered, %FALSE otherwise
7036
 *
7037
 * Since: 2.26
7038
 */
7039
gboolean
7040
g_dbus_connection_unregister_subtree (GDBusConnection *connection,
7041
                                      guint            registration_id)
7042
0
{
7043
0
  ExportedSubtree *es;
7044
0
  gboolean ret;
7045
7046
0
  g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), FALSE);
7047
0
  g_return_val_if_fail (check_initialized (connection), FALSE);
7048
7049
0
  ret = FALSE;
7050
7051
0
  CONNECTION_LOCK (connection);
7052
7053
0
  es = g_hash_table_lookup (connection->map_id_to_es,
7054
0
                            GUINT_TO_POINTER (registration_id));
7055
0
  if (es == NULL)
7056
0
    goto out;
7057
7058
0
  g_warn_if_fail (g_hash_table_remove (connection->map_id_to_es, GUINT_TO_POINTER (es->id)));
7059
0
  g_warn_if_fail (g_hash_table_remove (connection->map_object_path_to_es, es->object_path));
7060
7061
0
  ret = TRUE;
7062
7063
0
 out:
7064
0
  CONNECTION_UNLOCK (connection);
7065
7066
0
  return ret;
7067
0
}
7068
7069
/* ---------------------------------------------------------------------------------------------------- */
7070
7071
/* may be called in any thread, with connection's lock held */
7072
static void
7073
handle_generic_ping_unlocked (GDBusConnection *connection,
7074
                              const gchar     *object_path,
7075
                              GDBusMessage    *message)
7076
0
{
7077
0
  GDBusMessage *reply;
7078
0
  reply = g_dbus_message_new_method_reply (message);
7079
0
  g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
7080
0
  g_object_unref (reply);
7081
0
}
7082
7083
/* may be called in any thread, with connection's lock held */
7084
static void
7085
handle_generic_get_machine_id_unlocked (GDBusConnection *connection,
7086
                                        const gchar     *object_path,
7087
                                        GDBusMessage    *message)
7088
0
{
7089
0
  GDBusMessage *reply;
7090
7091
0
  reply = NULL;
7092
0
  if (connection->machine_id == NULL)
7093
0
    {
7094
0
      GError *error;
7095
7096
0
      error = NULL;
7097
0
      connection->machine_id = _g_dbus_get_machine_id (&error);
7098
0
      if (connection->machine_id == NULL)
7099
0
        {
7100
0
          reply = g_dbus_message_new_method_error_literal (message,
7101
0
                                                           "org.freedesktop.DBus.Error.Failed",
7102
0
                                                           error->message);
7103
0
          g_error_free (error);
7104
0
        }
7105
0
    }
7106
7107
0
  if (reply == NULL)
7108
0
    {
7109
0
      reply = g_dbus_message_new_method_reply (message);
7110
0
      g_dbus_message_set_body (reply, g_variant_new ("(s)", connection->machine_id));
7111
0
    }
7112
0
  g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
7113
0
  g_object_unref (reply);
7114
0
}
7115
7116
/* may be called in any thread, with connection's lock held */
7117
static void
7118
handle_generic_introspect_unlocked (GDBusConnection *connection,
7119
                                    const gchar     *object_path,
7120
                                    GDBusMessage    *message)
7121
0
{
7122
0
  guint n;
7123
0
  GString *s;
7124
0
  gchar **registered;
7125
0
  GDBusMessage *reply;
7126
7127
  /* first the header */
7128
0
  s = g_string_new (NULL);
7129
0
  introspect_append_header (s);
7130
7131
0
  registered = g_dbus_connection_list_registered_unlocked (connection, object_path);
7132
0
  for (n = 0; registered != NULL && registered[n] != NULL; n++)
7133
0
      g_string_append_printf (s, "  <node name=\"%s\"/>\n", registered[n]);
7134
0
  g_strfreev (registered);
7135
0
  g_string_append (s, "</node>\n");
7136
7137
0
  reply = g_dbus_message_new_method_reply (message);
7138
0
  g_dbus_message_set_body (reply, g_variant_new ("(s)", s->str));
7139
0
  g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
7140
0
  g_object_unref (reply);
7141
0
  g_string_free (s, TRUE);
7142
0
}
7143
7144
/* may be called in any thread, with connection's lock held */
7145
static gboolean
7146
handle_generic_unlocked (GDBusConnection *connection,
7147
                         GDBusMessage    *message)
7148
0
{
7149
0
  gboolean handled;
7150
0
  const gchar *interface_name;
7151
0
  const gchar *member;
7152
0
  const gchar *signature;
7153
0
  const gchar *path;
7154
7155
0
  CONNECTION_ENSURE_LOCK (connection);
7156
7157
0
  handled = FALSE;
7158
7159
0
  interface_name = g_dbus_message_get_interface (message);
7160
0
  member = g_dbus_message_get_member (message);
7161
0
  signature = g_dbus_message_get_signature (message);
7162
0
  path = g_dbus_message_get_path (message);
7163
7164
0
  if (g_strcmp0 (interface_name, "org.freedesktop.DBus.Introspectable") == 0 &&
7165
0
      g_strcmp0 (member, "Introspect") == 0 &&
7166
0
      g_strcmp0 (signature, "") == 0)
7167
0
    {
7168
0
      handle_generic_introspect_unlocked (connection, path, message);
7169
0
      handled = TRUE;
7170
0
    }
7171
0
  else if (g_strcmp0 (interface_name, "org.freedesktop.DBus.Peer") == 0 &&
7172
0
           g_strcmp0 (member, "Ping") == 0 &&
7173
0
           g_strcmp0 (signature, "") == 0)
7174
0
    {
7175
0
      handle_generic_ping_unlocked (connection, path, message);
7176
0
      handled = TRUE;
7177
0
    }
7178
0
  else if (g_strcmp0 (interface_name, "org.freedesktop.DBus.Peer") == 0 &&
7179
0
           g_strcmp0 (member, "GetMachineId") == 0 &&
7180
0
           g_strcmp0 (signature, "") == 0)
7181
0
    {
7182
0
      handle_generic_get_machine_id_unlocked (connection, path, message);
7183
0
      handled = TRUE;
7184
0
    }
7185
7186
0
  return handled;
7187
0
}
7188
7189
/* ---------------------------------------------------------------------------------------------------- */
7190
7191
/* called in GDBusWorker thread with connection's lock held */
7192
static void
7193
distribute_method_call (GDBusConnection *connection,
7194
                        GDBusMessage    *message)
7195
0
{
7196
0
  GDBusMessage *reply;
7197
0
  ExportedObject *eo;
7198
0
  ExportedSubtree *es;
7199
0
  const gchar *path;
7200
0
  const gchar *interface_name;
7201
0
  const gchar *member;
7202
0
  gchar *subtree_path;
7203
0
  gchar *needle;
7204
0
  gboolean object_found = FALSE;
7205
7206
0
  g_assert (g_dbus_message_get_message_type (message) == G_DBUS_MESSAGE_TYPE_METHOD_CALL);
7207
7208
  /* these are required, and should have been validated by validate_headers()
7209
   * in gdbusmessage.c already */
7210
0
  member = g_dbus_message_get_member (message);
7211
0
  path = g_dbus_message_get_path (message);
7212
7213
0
  g_assert (member != NULL);
7214
0
  g_assert (path != NULL);
7215
7216
  /* this is optional */
7217
0
  interface_name = g_dbus_message_get_interface (message);
7218
7219
0
  subtree_path = g_strdup (path);
7220
0
  needle = strrchr (subtree_path, '/');
7221
0
  if (needle != NULL && needle != subtree_path)
7222
0
    {
7223
0
      *needle = '\0';
7224
0
    }
7225
0
  else
7226
0
    {
7227
0
      g_free (subtree_path);
7228
0
      subtree_path = NULL;
7229
0
    }
7230
7231
0
  if (G_UNLIKELY (_g_dbus_debug_incoming ()))
7232
0
    {
7233
0
      _g_dbus_debug_print_lock ();
7234
0
      g_print ("========================================================================\n"
7235
0
               "GDBus-debug:Incoming:\n"
7236
0
               " <<<< METHOD INVOCATION %s.%s()\n"
7237
0
               "      on object %s\n"
7238
0
               "      invoked by name %s\n"
7239
0
               "      serial %d\n",
7240
0
               interface_name, member,
7241
0
               path,
7242
0
               g_dbus_message_get_sender (message) != NULL ? g_dbus_message_get_sender (message) : "(none)",
7243
0
               g_dbus_message_get_serial (message));
7244
0
      _g_dbus_debug_print_unlock ();
7245
0
    }
7246
7247
0
  eo = g_hash_table_lookup (connection->map_object_path_to_eo, path);
7248
0
  if (eo != NULL)
7249
0
    {
7250
0
      if (obj_message_func (connection, eo, message, &object_found))
7251
0
        goto out;
7252
0
    }
7253
7254
0
  es = g_hash_table_lookup (connection->map_object_path_to_es, path);
7255
0
  if (es != NULL)
7256
0
    {
7257
0
      if (subtree_message_func (connection, es, message))
7258
0
        goto out;
7259
0
    }
7260
7261
0
  if (subtree_path != NULL)
7262
0
    {
7263
0
      es = g_hash_table_lookup (connection->map_object_path_to_es, subtree_path);
7264
0
      if (es != NULL)
7265
0
        {
7266
0
          if (subtree_message_func (connection, es, message))
7267
0
            goto out;
7268
0
        }
7269
0
    }
7270
7271
0
  if (handle_generic_unlocked (connection, message))
7272
0
    goto out;
7273
7274
  /* if we end up here, the message has not been not handled - so return an error saying this */
7275
0
  if (object_found == TRUE)
7276
0
    {
7277
0
      reply = g_dbus_message_new_method_error (message,
7278
0
                                               "org.freedesktop.DBus.Error.UnknownMethod",
7279
0
                                               _("No such interface ā€œ%sā€ on object at path %s"),
7280
0
                                               interface_name,
7281
0
                                               path);
7282
0
    }
7283
0
  else
7284
0
    {
7285
0
      reply = g_dbus_message_new_method_error (message,
7286
0
                                           "org.freedesktop.DBus.Error.UnknownMethod",
7287
0
                                           _("Object does not exist at path ā€œ%sā€"),
7288
0
                                           path);
7289
0
    }
7290
7291
0
  g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
7292
0
  g_object_unref (reply);
7293
7294
0
 out:
7295
0
  g_free (subtree_path);
7296
0
}
7297
7298
/* ---------------------------------------------------------------------------------------------------- */
7299
7300
/* Called in any user thread, with the message_bus_lock held. */
7301
static GWeakRef *
7302
message_bus_get_singleton (GBusType   bus_type,
7303
                           GError   **error)
7304
0
{
7305
0
  GWeakRef *ret;
7306
0
  const gchar *starter_bus;
7307
7308
0
  ret = NULL;
7309
7310
0
  switch (bus_type)
7311
0
    {
7312
0
    case G_BUS_TYPE_SESSION:
7313
0
      ret = &the_session_bus;
7314
0
      break;
7315
7316
0
    case G_BUS_TYPE_SYSTEM:
7317
0
      ret = &the_system_bus;
7318
0
      break;
7319
7320
0
    case G_BUS_TYPE_STARTER:
7321
0
      starter_bus = g_getenv ("DBUS_STARTER_BUS_TYPE");
7322
0
      if (g_strcmp0 (starter_bus, "session") == 0)
7323
0
        {
7324
0
          ret = message_bus_get_singleton (G_BUS_TYPE_SESSION, error);
7325
0
          goto out;
7326
0
        }
7327
0
      else if (g_strcmp0 (starter_bus, "system") == 0)
7328
0
        {
7329
0
          ret = message_bus_get_singleton (G_BUS_TYPE_SYSTEM, error);
7330
0
          goto out;
7331
0
        }
7332
0
      else
7333
0
        {
7334
0
          if (starter_bus != NULL)
7335
0
            {
7336
0
              g_set_error (error,
7337
0
                           G_IO_ERROR,
7338
0
                           G_IO_ERROR_INVALID_ARGUMENT,
7339
0
                           _("Cannot determine bus address from DBUS_STARTER_BUS_TYPE environment variable"
7340
0
                             " — unknown value ā€œ%sā€"),
7341
0
                           starter_bus);
7342
0
            }
7343
0
          else
7344
0
            {
7345
0
              g_set_error_literal (error,
7346
0
                                   G_IO_ERROR,
7347
0
                                   G_IO_ERROR_INVALID_ARGUMENT,
7348
0
                                   _("Cannot determine bus address because the DBUS_STARTER_BUS_TYPE environment "
7349
0
                                     "variable is not set"));
7350
0
            }
7351
0
        }
7352
0
      break;
7353
7354
0
    default:
7355
0
      g_assert_not_reached ();
7356
0
      break;
7357
0
    }
7358
7359
0
 out:
7360
0
  return ret;
7361
0
}
7362
7363
/* Called in any user thread, without holding locks. */
7364
static GDBusConnection *
7365
get_uninitialized_connection (GBusType       bus_type,
7366
                              GCancellable  *cancellable,
7367
                              GError       **error)
7368
0
{
7369
0
  GWeakRef *singleton;
7370
0
  GDBusConnection *ret;
7371
7372
0
  ret = NULL;
7373
7374
0
  G_LOCK (message_bus_lock);
7375
0
  singleton = message_bus_get_singleton (bus_type, error);
7376
0
  if (singleton == NULL)
7377
0
    goto out;
7378
7379
0
  ret = g_weak_ref_get (singleton);
7380
7381
0
  if (ret == NULL)
7382
0
    {
7383
0
      gchar *address;
7384
0
      address = g_dbus_address_get_for_bus_sync (bus_type, cancellable, error);
7385
0
      if (address == NULL)
7386
0
        goto out;
7387
0
      ret = g_object_new (G_TYPE_DBUS_CONNECTION,
7388
0
                          "address", address,
7389
0
                          "flags", G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
7390
0
#ifdef __linux__
7391
0
                                   G_DBUS_CONNECTION_FLAGS_CROSS_NAMESPACE |
7392
0
#endif
7393
0
                                   G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
7394
0
                          "exit-on-close", TRUE,
7395
0
                          NULL);
7396
7397
0
      g_weak_ref_set (singleton, ret);
7398
0
      g_free (address);
7399
0
    }
7400
7401
0
  g_assert (ret != NULL);
7402
7403
0
 out:
7404
0
  G_UNLOCK (message_bus_lock);
7405
0
  return ret;
7406
0
}
7407
7408
/* May be called from any thread. Must not hold message_bus_lock. */
7409
GDBusConnection *
7410
_g_bus_get_singleton_if_exists (GBusType bus_type)
7411
0
{
7412
0
  GWeakRef *singleton;
7413
0
  GDBusConnection *ret = NULL;
7414
7415
0
  G_LOCK (message_bus_lock);
7416
0
  singleton = message_bus_get_singleton (bus_type, NULL);
7417
0
  if (singleton == NULL)
7418
0
    goto out;
7419
7420
0
  ret = g_weak_ref_get (singleton);
7421
7422
0
 out:
7423
0
  G_UNLOCK (message_bus_lock);
7424
0
  return ret;
7425
0
}
7426
7427
/* May be called from any thread. Must not hold message_bus_lock. */
7428
void
7429
_g_bus_forget_singleton (GBusType bus_type)
7430
0
{
7431
0
  GWeakRef *singleton;
7432
7433
0
  G_LOCK (message_bus_lock);
7434
7435
0
  singleton = message_bus_get_singleton (bus_type, NULL);
7436
7437
0
  if (singleton != NULL)
7438
0
    g_weak_ref_set (singleton, NULL);
7439
7440
0
  G_UNLOCK (message_bus_lock);
7441
0
}
7442
7443
/**
7444
 * g_bus_get_sync:
7445
 * @bus_type: a #GBusType
7446
 * @cancellable: (nullable): a #GCancellable or %NULL
7447
 * @error: return location for error or %NULL
7448
 *
7449
 * Synchronously connects to the message bus specified by @bus_type.
7450
 * Note that the returned object may shared with other callers,
7451
 * e.g. if two separate parts of a process calls this function with
7452
 * the same @bus_type, they will share the same object.
7453
 *
7454
 * This is a synchronous failable function. See g_bus_get() and
7455
 * g_bus_get_finish() for the asynchronous version.
7456
 *
7457
 * The returned object is a singleton, that is, shared with other
7458
 * callers of g_bus_get() and g_bus_get_sync() for @bus_type. In the
7459
 * event that you need a private message bus connection, use
7460
 * g_dbus_address_get_for_bus_sync() and
7461
 * g_dbus_connection_new_for_address() with
7462
 * G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT and
7463
 * G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION flags.
7464
 *
7465
 * Note that the returned #GDBusConnection object will (usually) have
7466
 * the #GDBusConnection:exit-on-close property set to %TRUE.
7467
 *
7468
 * Returns: (transfer full): a #GDBusConnection or %NULL if @error is set.
7469
 *     Free with g_object_unref().
7470
 *
7471
 * Since: 2.26
7472
 */
7473
GDBusConnection *
7474
g_bus_get_sync (GBusType       bus_type,
7475
                GCancellable  *cancellable,
7476
                GError       **error)
7477
0
{
7478
0
  GDBusConnection *connection;
7479
7480
0
  _g_dbus_initialize ();
7481
7482
0
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
7483
7484
0
  connection = get_uninitialized_connection (bus_type, cancellable, error);
7485
0
  if (connection == NULL)
7486
0
    goto out;
7487
7488
0
  if (!g_initable_init (G_INITABLE (connection), cancellable, error))
7489
0
    {
7490
0
      g_object_unref (connection);
7491
0
      connection = NULL;
7492
0
    }
7493
7494
0
 out:
7495
0
  return connection;
7496
0
}
7497
7498
static void
7499
bus_get_async_initable_cb (GObject      *source_object,
7500
                           GAsyncResult *res,
7501
                           gpointer      user_data)
7502
0
{
7503
0
  GTask *task = user_data;
7504
0
  GError *error = NULL;
7505
7506
0
  if (!g_async_initable_init_finish (G_ASYNC_INITABLE (source_object),
7507
0
                                     res,
7508
0
                                     &error))
7509
0
    {
7510
0
      g_assert (error != NULL);
7511
0
      g_task_return_error (task, error);
7512
0
      g_object_unref (source_object);
7513
0
    }
7514
0
  else
7515
0
    {
7516
0
      g_task_return_pointer (task, source_object, g_object_unref);
7517
0
    }
7518
0
  g_object_unref (task);
7519
0
}
7520
7521
/**
7522
 * g_bus_get:
7523
 * @bus_type: a #GBusType
7524
 * @cancellable: (nullable): a #GCancellable or %NULL
7525
 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7526
 * @user_data: the data to pass to @callback
7527
 *
7528
 * Asynchronously connects to the message bus specified by @bus_type.
7529
 *
7530
 * When the operation is finished, @callback will be invoked. You can
7531
 * then call g_bus_get_finish() to get the result of the operation.
7532
 *
7533
 * This is an asynchronous failable function. See g_bus_get_sync() for
7534
 * the synchronous version.
7535
 *
7536
 * Since: 2.26
7537
 */
7538
void
7539
g_bus_get (GBusType             bus_type,
7540
           GCancellable        *cancellable,
7541
           GAsyncReadyCallback  callback,
7542
           gpointer             user_data)
7543
0
{
7544
0
  GDBusConnection *connection;
7545
0
  GTask *task;
7546
0
  GError *error = NULL;
7547
7548
0
  _g_dbus_initialize ();
7549
7550
0
  task = g_task_new (NULL, cancellable, callback, user_data);
7551
0
  g_task_set_source_tag (task, g_bus_get);
7552
7553
0
  connection = get_uninitialized_connection (bus_type, cancellable, &error);
7554
0
  if (connection == NULL)
7555
0
    {
7556
0
      g_assert (error != NULL);
7557
0
      g_task_return_error (task, error);
7558
0
      g_object_unref (task);
7559
0
    }
7560
0
  else
7561
0
    {
7562
0
      g_async_initable_init_async (G_ASYNC_INITABLE (connection),
7563
0
                                   G_PRIORITY_DEFAULT,
7564
0
                                   cancellable,
7565
0
                                   bus_get_async_initable_cb,
7566
0
                                   task);
7567
0
    }
7568
0
}
7569
7570
/**
7571
 * g_bus_get_finish:
7572
 * @res: a #GAsyncResult obtained from the #GAsyncReadyCallback passed
7573
 *     to g_bus_get()
7574
 * @error: return location for error or %NULL
7575
 *
7576
 * Finishes an operation started with g_bus_get().
7577
 *
7578
 * The returned object is a singleton, that is, shared with other
7579
 * callers of g_bus_get() and g_bus_get_sync() for @bus_type. In the
7580
 * event that you need a private message bus connection, use
7581
 * g_dbus_address_get_for_bus_sync() and
7582
 * g_dbus_connection_new_for_address() with
7583
 * G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT and
7584
 * G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION flags.
7585
 *
7586
 * Note that the returned #GDBusConnection object will (usually) have
7587
 * the #GDBusConnection:exit-on-close property set to %TRUE.
7588
 *
7589
 * Returns: (transfer full): a #GDBusConnection or %NULL if @error is set.
7590
 *     Free with g_object_unref().
7591
 *
7592
 * Since: 2.26
7593
 */
7594
GDBusConnection *
7595
g_bus_get_finish (GAsyncResult  *res,
7596
                  GError       **error)
7597
0
{
7598
0
  g_return_val_if_fail (g_task_is_valid (res, NULL), NULL);
7599
0
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
7600
7601
0
  return g_task_propagate_pointer (G_TASK (res), error);
7602
0
}
7603
7604
/* ---------------------------------------------------------------------------------------------------- */