forked from GNOME/glib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgdbusconnection.c
7580 lines (6641 loc) · 272 KB
/
gdbusconnection.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* GDBus - GLib D-Bus Library
*
* Copyright (C) 2008-2010 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
*
* Author: David Zeuthen <[email protected]>
*/
/*
* TODO for GDBus:
*
* - would be nice to expose GDBusAuthMechanism and an extension point
*
* - Need to rewrite GDBusAuth and rework GDBusAuthMechanism. In particular
* the mechanism VFuncs need to be able to set an error.
*
* - Need to document other mechanisms/sources for determining the D-Bus
* address of a well-known bus.
*
* - e.g. on Win32 we need code like from here
*
* http://cgit.freedesktop.org/~david/gdbus-standalone/tree/gdbus/gdbusaddress.c#n900
*
* that was never copied over here because it originally was copy-paste
* from the GPLv2 / AFL 2.1 libdbus sources.
*
* - on OS X we need to look in launchd for the address
*
* https://bugs.freedesktop.org/show_bug.cgi?id=14259
*
* - on X11 we need to look in a X11 property on the X server
* - (we can also just use dbus-launch(1) from the D-Bus
* distribution)
*
* - (ideally) this requires D-Bus spec work because none of
* this has never really been specced out properly (except
* the X11 bits)
*
* - Related to the above, we also need to be able to launch a message bus
* instance.... Since we don't want to write our own bus daemon we should
* launch dbus-daemon(1) (thus: Win32 and OS X need to bundle it)
*
* - probably want a G_DBUS_NONCE_TCP_TMPDIR environment variable
* to specify where the nonce is stored. This will allow people to use
* G_DBUS_NONCE_TCP_TMPDIR=/mnt/secure.company.server/dbus-nonce-dir
* to easily achieve secure RPC via nonce-tcp.
*
* - need to expose an extension point for resolving D-Bus address and
* turning them into GIOStream objects. This will allow us to implement
* e.g. X11 D-Bus transports without dlopen()'ing or linking against
* libX11 from libgio.
* - see g_dbus_address_connect() in gdbusaddress.c
*
* - would be cute to use kernel-specific APIs to resolve fds for
* debug output when using G_DBUS_DEBUG=message, e.g. in addition to
*
* fd 21: dev=8:1,mode=0100644,ino=1171231,uid=0,gid=0,rdev=0:0,size=234,atime=1273070640,mtime=1267126160,ctime=1267126160
*
* maybe we can show more information about what fd 21 really is.
* Ryan suggests looking in /proc/self/fd for clues / symlinks!
* Initial experiments on Linux 2.6 suggests that the symlink looks
* like this:
*
* 3 -> /proc/18068/fd
*
* e.g. not of much use.
*
* - GDBus High-Level docs
* - Proxy: properties, signals...
* - Connection: IOStream based, ::close, connection setup steps
* mainloop integration, threading
* - Differences from libdbus (extend "Migrating from")
* - the message handling thread
* - Using GVariant instead of GValue
* - Explain why the high-level API is a good thing and what
* kind of pitfalls it avoids
* - Export objects before claiming names
* - Talk about auto-starting services (cf. GBusNameWatcherFlags)
*/
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include "gdbusauth.h"
#include "gdbusutils.h"
#include "gdbusaddress.h"
#include "gdbusmessage.h"
#include "gdbusconnection.h"
#include "gdbuserror.h"
#include "gioenumtypes.h"
#include "gdbusintrospection.h"
#include "gdbusmethodinvocation.h"
#include "gdbusprivate.h"
#include "gdbusauthobserver.h"
#include "ginitable.h"
#include "gasyncinitable.h"
#include "giostream.h"
#include "gasyncresult.h"
#include "gtask.h"
#include "gmarshal-internal.h"
#ifdef G_OS_UNIX
#include "gunixconnection.h"
#include "gunixfdmessage.h"
#endif
#include "glibintl.h"
#define G_DBUS_CONNECTION_FLAGS_ALL \
(G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT | \
G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER | \
G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS | \
G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION | \
G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING | \
G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_REQUIRE_SAME_USER)
/**
* SECTION:gdbusconnection
* @short_description: D-Bus Connections
* @include: gio/gio.h
*
* The #GDBusConnection type is used for D-Bus connections to remote
* peers such as a message buses. It is a low-level API that offers a
* lot of flexibility. For instance, it lets you establish a connection
* over any transport that can by represented as a #GIOStream.
*
* This class is rarely used directly in D-Bus clients. If you are writing
* a D-Bus client, it is often easier to use the g_bus_own_name(),
* g_bus_watch_name() or g_dbus_proxy_new_for_bus() APIs.
*
* As an exception to the usual GLib rule that a particular object must not
* be used by two threads at the same time, #GDBusConnection's methods may be
* called from any thread. This is so that g_bus_get() and g_bus_get_sync()
* can safely return the same #GDBusConnection when called from any thread.
*
* Most of the ways to obtain a #GDBusConnection automatically initialize it
* (i.e. connect to D-Bus): for instance, g_dbus_connection_new() and
* g_bus_get(), and the synchronous versions of those methods, give you an
* initialized connection. Language bindings for GIO should use
* g_initable_new() or g_async_initable_new_async(), which also initialize the
* connection.
*
* If you construct an uninitialized #GDBusConnection, such as via
* g_object_new(), you must initialize it via g_initable_init() or
* g_async_initable_init_async() before using its methods or properties.
* Calling methods or accessing properties on a #GDBusConnection that has not
* completed initialization successfully is considered to be invalid, and leads
* to undefined behaviour. In particular, if initialization fails with a
* #GError, the only valid thing you can do with that #GDBusConnection is to
* free it with g_object_unref().
*
* ## An example D-Bus server # {#gdbus-server}
*
* Here is an example for a D-Bus server:
* [gdbus-example-server.c](https://gitlab.gnome.org/GNOME/glib/-/blob/HEAD/gio/tests/gdbus-example-server.c)
*
* ## An example for exporting a subtree # {#gdbus-subtree-server}
*
* Here is an example for exporting a subtree:
* [gdbus-example-subtree.c](https://gitlab.gnome.org/GNOME/glib/-/blob/HEAD/gio/tests/gdbus-example-subtree.c)
*
* ## An example for file descriptor passing # {#gdbus-unix-fd-client}
*
* Here is an example for passing UNIX file descriptors:
* [gdbus-unix-fd-client.c](https://gitlab.gnome.org/GNOME/glib/-/blob/HEAD/gio/tests/gdbus-example-unix-fd-client.c)
*
* ## An example for exporting a GObject # {#gdbus-export}
*
* Here is an example for exporting a #GObject:
* [gdbus-example-export.c](https://gitlab.gnome.org/GNOME/glib/-/blob/HEAD/gio/tests/gdbus-example-export.c)
*/
/* ---------------------------------------------------------------------------------------------------- */
typedef struct _GDBusConnectionClass GDBusConnectionClass;
/**
* GDBusConnectionClass:
* @closed: Signal class handler for the #GDBusConnection::closed signal.
*
* Class structure for #GDBusConnection.
*
* Since: 2.26
*/
struct _GDBusConnectionClass
{
/*< private >*/
GObjectClass parent_class;
/*< public >*/
/* Signals */
void (*closed) (GDBusConnection *connection,
gboolean remote_peer_vanished,
GError *error);
};
G_LOCK_DEFINE_STATIC (message_bus_lock);
static GWeakRef the_session_bus;
static GWeakRef the_system_bus;
/* Extra pseudo-member of GDBusSendMessageFlags.
* Set by initable_init() to indicate that despite not being initialized yet,
* enough of the only-valid-after-init members are set that we can send a
* message, and we're being called from its thread, so no memory barrier is
* required before accessing them.
*/
#define SEND_MESSAGE_FLAGS_INITIALIZING (1u << 31)
/* Same as SEND_MESSAGE_FLAGS_INITIALIZING, but in GDBusCallFlags */
#define CALL_FLAGS_INITIALIZING (1u << 31)
/* ---------------------------------------------------------------------------------------------------- */
typedef struct
{
GDestroyNotify callback;
gpointer user_data;
} CallDestroyNotifyData;
static gboolean
call_destroy_notify_data_in_idle (gpointer user_data)
{
CallDestroyNotifyData *data = user_data;
data->callback (data->user_data);
return FALSE;
}
static void
call_destroy_notify_data_free (CallDestroyNotifyData *data)
{
g_free (data);
}
/*
* call_destroy_notify: <internal>
* @context: (nullable): A #GMainContext or %NULL.
* @callback: (nullable): A #GDestroyNotify or %NULL.
* @user_data: Data to pass to @callback.
*
* Schedules @callback to run in @context.
*/
static void
call_destroy_notify (GMainContext *context,
GDestroyNotify callback,
gpointer user_data)
{
GSource *idle_source;
CallDestroyNotifyData *data;
if (callback == NULL)
return;
data = g_new0 (CallDestroyNotifyData, 1);
data->callback = callback;
data->user_data = user_data;
idle_source = g_idle_source_new ();
g_source_set_priority (idle_source, G_PRIORITY_DEFAULT);
g_source_set_callback (idle_source,
call_destroy_notify_data_in_idle,
data,
(GDestroyNotify) call_destroy_notify_data_free);
g_source_set_static_name (idle_source, "[gio] call_destroy_notify_data_in_idle");
g_source_attach (idle_source, context);
g_source_unref (idle_source);
}
/* ---------------------------------------------------------------------------------------------------- */
#ifdef G_OS_WIN32
#define CONNECTION_ENSURE_LOCK(obj) do { ; } while (FALSE)
#else
// TODO: for some reason this doesn't work on Windows
#define CONNECTION_ENSURE_LOCK(obj) do { \
if (G_UNLIKELY (g_mutex_trylock(&(obj)->lock))) \
{ \
g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
"CONNECTION_ENSURE_LOCK: GDBusConnection object lock is not locked"); \
} \
} while (FALSE)
#endif
#define CONNECTION_LOCK(obj) do { \
g_mutex_lock (&(obj)->lock); \
} while (FALSE)
#define CONNECTION_UNLOCK(obj) do { \
g_mutex_unlock (&(obj)->lock); \
} while (FALSE)
/* Flags in connection->atomic_flags */
enum {
FLAG_INITIALIZED = 1 << 0,
FLAG_EXIT_ON_CLOSE = 1 << 1,
FLAG_CLOSED = 1 << 2
};
/**
* GDBusConnection:
*
* The #GDBusConnection structure contains only private data and
* should only be accessed using the provided API.
*
* Since: 2.26
*/
struct _GDBusConnection
{
/*< private >*/
GObject parent_instance;
/* ------------------------------------------------------------------------ */
/* -- General object state ------------------------------------------------ */
/* ------------------------------------------------------------------------ */
/* General-purpose lock for most fields */
GMutex lock;
/* A lock used in the init() method of the GInitable interface - see comments
* in initable_init() for why a separate lock is needed.
*
* If you need both @lock and @init_lock, you must take @init_lock first.
*/
GMutex init_lock;
/* Set (by loading the contents of /var/lib/dbus/machine-id) the first time
* someone calls org.freedesktop.DBus.Peer.GetMachineId(). Protected by @lock.
*/
gchar *machine_id;
/* The underlying stream used for communication
* Read-only after initable_init(), so it may be read if you either
* hold @init_lock or check for initialization first.
*/
GIOStream *stream;
/* The object used for authentication (if any).
* Read-only after initable_init(), so it may be read if you either
* hold @init_lock or check for initialization first.
*/
GDBusAuth *auth;
/* Last serial used. Protected by @lock. */
guint32 last_serial;
/* The object used to send/receive messages.
* Read-only after initable_init(), so it may be read if you either
* hold @init_lock or check for initialization first.
*/
GDBusWorker *worker;
/* If connected to a message bus, this contains the unique name assigned to
* us by the bus (e.g. ":1.42").
* Read-only after initable_init(), so it may be read if you either
* hold @init_lock or check for initialization first.
*/
gchar *bus_unique_name;
/* The GUID returned by the other side if we authenticated as a client or
* the GUID to use if authenticating as a server.
* Read-only after initable_init(), so it may be read if you either
* hold @init_lock or check for initialization first.
*/
gchar *guid;
/* FLAG_INITIALIZED is set exactly when initable_init() has finished running.
* Inspect @initialization_error to see whether it succeeded or failed.
*
* FLAG_EXIT_ON_CLOSE is the exit-on-close property.
*
* FLAG_CLOSED is the closed property. It may be read at any time, but
* may only be written while holding @lock.
*/
gint atomic_flags; /* (atomic) */
/* If the connection could not be established during initable_init(),
* this GError will be set.
* Read-only after initable_init(), so it may be read if you either
* hold @init_lock or check for initialization first.
*/
GError *initialization_error;
/* The result of g_main_context_ref_thread_default() when the object
* was created (the GObject _init() function) - this is used for delivery
* of the :closed GObject signal.
*
* Only set in the GObject init function, so no locks are needed.
*/
GMainContext *main_context_at_construction;
/* Read-only construct properties, no locks needed */
gchar *address;
GDBusConnectionFlags flags;
/* Map used for managing method replies, protected by @lock */
GHashTable *map_method_serial_to_task; /* guint32 -> GTask* */
/* Maps used for managing signal subscription, protected by @lock */
GHashTable *map_rule_to_signal_data; /* match rule (gchar*) -> SignalData */
GHashTable *map_id_to_signal_data; /* id (guint) -> SignalData */
GHashTable *map_sender_unique_name_to_signal_data_array; /* unique sender (gchar*) -> GPtrArray* of SignalData */
/* Maps used for managing exported objects and subtrees,
* protected by @lock
*/
GHashTable *map_object_path_to_eo; /* gchar* -> ExportedObject* */
GHashTable *map_id_to_ei; /* guint -> ExportedInterface* */
GHashTable *map_object_path_to_es; /* gchar* -> ExportedSubtree* */
GHashTable *map_id_to_es; /* guint -> ExportedSubtree* */
/* Map used for storing last used serials for each thread, protected by @lock */
GHashTable *map_thread_to_last_serial;
/* Structure used for message filters, protected by @lock */
GPtrArray *filters;
/* Capabilities negotiated during authentication
* Read-only after initable_init(), so it may be read without holding a
* lock, if you check for initialization first.
*/
GDBusCapabilityFlags capabilities;
/* Protected by @init_lock */
GDBusAuthObserver *authentication_observer;
/* Read-only after initable_init(), so it may be read if you either
* hold @init_lock or check for initialization first.
*/
GCredentials *credentials;
/* set to TRUE when finalizing */
gboolean finalizing;
};
typedef struct ExportedObject ExportedObject;
static void exported_object_free (ExportedObject *eo);
typedef struct ExportedSubtree ExportedSubtree;
static ExportedSubtree *exported_subtree_ref (ExportedSubtree *es);
static void exported_subtree_unref (ExportedSubtree *es);
enum
{
CLOSED_SIGNAL,
LAST_SIGNAL,
};
enum
{
PROP_0,
PROP_STREAM,
PROP_ADDRESS,
PROP_FLAGS,
PROP_GUID,
PROP_UNIQUE_NAME,
PROP_CLOSED,
PROP_EXIT_ON_CLOSE,
PROP_CAPABILITY_FLAGS,
PROP_AUTHENTICATION_OBSERVER,
};
static void distribute_signals (GDBusConnection *connection,
GDBusMessage *message);
static void distribute_method_call (GDBusConnection *connection,
GDBusMessage *message);
static gboolean handle_generic_unlocked (GDBusConnection *connection,
GDBusMessage *message);
static void purge_all_signal_subscriptions (GDBusConnection *connection);
static void purge_all_filters (GDBusConnection *connection);
static void schedule_method_call (GDBusConnection *connection,
GDBusMessage *message,
guint registration_id,
guint subtree_registration_id,
const GDBusInterfaceInfo *interface_info,
const GDBusMethodInfo *method_info,
const GDBusPropertyInfo *property_info,
GVariant *parameters,
const GDBusInterfaceVTable *vtable,
GMainContext *main_context,
gpointer user_data);
#define _G_ENSURE_LOCK(name) do { \
if (G_UNLIKELY (G_TRYLOCK(name))) \
{ \
g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
"_G_ENSURE_LOCK: Lock '" #name "' is not locked"); \
} \
} while (FALSE) \
static guint signals[LAST_SIGNAL] = { 0 };
static void initable_iface_init (GInitableIface *initable_iface);
static void async_initable_iface_init (GAsyncInitableIface *async_initable_iface);
G_DEFINE_TYPE_WITH_CODE (GDBusConnection, g_dbus_connection, G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init)
G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE, async_initable_iface_init)
);
/*
* Check that all members of @connection that can only be accessed after
* the connection is initialized can safely be accessed. If not,
* log a critical warning. This function is a memory barrier.
*
* Returns: %TRUE if initialized
*/
static gboolean
check_initialized (GDBusConnection *connection)
{
/* The access to @atomic_flags isn't conditional, so that this function
* provides a memory barrier for thread-safety even if checks are disabled.
* (If you don't want this stricter guarantee, you can call
* g_return_if_fail (check_initialized (c)).)
*
* This isn't strictly necessary now that we've decided use of an
* uninitialized GDBusConnection is undefined behaviour, but it seems
* better to be as deterministic as is feasible.
*
* (Anything that could suffer a crash from seeing undefined values
* must have a race condition - thread A initializes the connection while
* thread B calls a method without initialization, hoping that thread A will
* win the race - so its behaviour is undefined anyway.)
*/
gint flags = g_atomic_int_get (&connection->atomic_flags);
g_return_val_if_fail (flags & FLAG_INITIALIZED, FALSE);
/* We can safely access this, due to the memory barrier above */
g_return_val_if_fail (connection->initialization_error == NULL, FALSE);
return TRUE;
}
typedef enum {
MAY_BE_UNINITIALIZED = (1<<1)
} CheckUnclosedFlags;
/*
* Check the same thing as check_initialized(), and also that the
* connection is not closed. If the connection is uninitialized,
* raise a critical warning (it's programmer error); if it's closed,
* raise a recoverable GError (it's a runtime error).
*
* This function is a memory barrier.
*
* Returns: %TRUE if initialized and not closed
*/
static gboolean
check_unclosed (GDBusConnection *connection,
CheckUnclosedFlags check,
GError **error)
{
/* check_initialized() is effectively inlined, so we don't waste time
* doing two memory barriers
*/
gint flags = g_atomic_int_get (&connection->atomic_flags);
if (!(check & MAY_BE_UNINITIALIZED))
{
g_return_val_if_fail (flags & FLAG_INITIALIZED, FALSE);
g_return_val_if_fail (connection->initialization_error == NULL, FALSE);
}
if (flags & FLAG_CLOSED)
{
g_set_error_literal (error,
G_IO_ERROR,
G_IO_ERROR_CLOSED,
_("The connection is closed"));
return FALSE;
}
return TRUE;
}
static GHashTable *alive_connections = NULL;
static void
g_dbus_connection_dispose (GObject *object)
{
GDBusConnection *connection = G_DBUS_CONNECTION (object);
G_LOCK (message_bus_lock);
CONNECTION_LOCK (connection);
if (connection->worker != NULL)
{
_g_dbus_worker_stop (connection->worker);
connection->worker = NULL;
if (alive_connections != NULL)
g_warn_if_fail (g_hash_table_remove (alive_connections, connection));
}
else
{
if (alive_connections != NULL)
g_warn_if_fail (!g_hash_table_contains (alive_connections, connection));
}
CONNECTION_UNLOCK (connection);
G_UNLOCK (message_bus_lock);
if (G_OBJECT_CLASS (g_dbus_connection_parent_class)->dispose != NULL)
G_OBJECT_CLASS (g_dbus_connection_parent_class)->dispose (object);
}
static void
g_dbus_connection_finalize (GObject *object)
{
GDBusConnection *connection = G_DBUS_CONNECTION (object);
connection->finalizing = TRUE;
purge_all_signal_subscriptions (connection);
purge_all_filters (connection);
g_ptr_array_unref (connection->filters);
if (connection->authentication_observer != NULL)
g_object_unref (connection->authentication_observer);
if (connection->auth != NULL)
g_object_unref (connection->auth);
if (connection->credentials)
g_object_unref (connection->credentials);
if (connection->stream != NULL)
{
g_object_unref (connection->stream);
connection->stream = NULL;
}
g_free (connection->address);
g_free (connection->guid);
g_free (connection->bus_unique_name);
if (connection->initialization_error != NULL)
g_error_free (connection->initialization_error);
g_hash_table_unref (connection->map_method_serial_to_task);
g_hash_table_unref (connection->map_rule_to_signal_data);
g_hash_table_unref (connection->map_id_to_signal_data);
g_hash_table_unref (connection->map_sender_unique_name_to_signal_data_array);
g_hash_table_unref (connection->map_id_to_ei);
g_hash_table_unref (connection->map_object_path_to_eo);
g_hash_table_unref (connection->map_id_to_es);
g_hash_table_unref (connection->map_object_path_to_es);
g_hash_table_unref (connection->map_thread_to_last_serial);
g_main_context_unref (connection->main_context_at_construction);
g_free (connection->machine_id);
g_mutex_clear (&connection->init_lock);
g_mutex_clear (&connection->lock);
G_OBJECT_CLASS (g_dbus_connection_parent_class)->finalize (object);
}
/* called in any user thread, with the connection's lock not held */
static void
g_dbus_connection_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GDBusConnection *connection = G_DBUS_CONNECTION (object);
switch (prop_id)
{
case PROP_STREAM:
g_value_set_object (value, g_dbus_connection_get_stream (connection));
break;
case PROP_GUID:
g_value_set_string (value, g_dbus_connection_get_guid (connection));
break;
case PROP_UNIQUE_NAME:
g_value_set_string (value, g_dbus_connection_get_unique_name (connection));
break;
case PROP_CLOSED:
g_value_set_boolean (value, g_dbus_connection_is_closed (connection));
break;
case PROP_EXIT_ON_CLOSE:
g_value_set_boolean (value, g_dbus_connection_get_exit_on_close (connection));
break;
case PROP_CAPABILITY_FLAGS:
g_value_set_flags (value, g_dbus_connection_get_capabilities (connection));
break;
case PROP_FLAGS:
g_value_set_flags (value, g_dbus_connection_get_flags (connection));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
/* called in any user thread, with the connection's lock not held */
static void
g_dbus_connection_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GDBusConnection *connection = G_DBUS_CONNECTION (object);
switch (prop_id)
{
case PROP_STREAM:
connection->stream = g_value_dup_object (value);
break;
case PROP_GUID:
connection->guid = g_value_dup_string (value);
break;
case PROP_ADDRESS:
connection->address = g_value_dup_string (value);
break;
case PROP_FLAGS:
connection->flags = g_value_get_flags (value);
break;
case PROP_EXIT_ON_CLOSE:
g_dbus_connection_set_exit_on_close (connection, g_value_get_boolean (value));
break;
case PROP_AUTHENTICATION_OBSERVER:
connection->authentication_observer = g_value_dup_object (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
/* Base-class implementation of GDBusConnection::closed.
*
* Called in a user thread, by the main context that was thread-default when
* the object was constructed.
*/
static void
g_dbus_connection_real_closed (GDBusConnection *connection,
gboolean remote_peer_vanished,
GError *error)
{
gint flags = g_atomic_int_get (&connection->atomic_flags);
/* Because atomic int access is a memory barrier, we can safely read
* initialization_error without a lock, as long as we do it afterwards.
*/
if (remote_peer_vanished &&
(flags & FLAG_EXIT_ON_CLOSE) != 0 &&
(flags & FLAG_INITIALIZED) != 0 &&
connection->initialization_error == NULL)
{
raise (SIGTERM);
}
}
static void
g_dbus_connection_class_init (GDBusConnectionClass *klass)
{
GObjectClass *gobject_class;
gobject_class = G_OBJECT_CLASS (klass);
gobject_class->finalize = g_dbus_connection_finalize;
gobject_class->dispose = g_dbus_connection_dispose;
gobject_class->set_property = g_dbus_connection_set_property;
gobject_class->get_property = g_dbus_connection_get_property;
klass->closed = g_dbus_connection_real_closed;
/**
* GDBusConnection:stream:
*
* The underlying #GIOStream used for I/O.
*
* If this is passed on construction and is a #GSocketConnection,
* then the corresponding #GSocket will be put into non-blocking mode.
*
* While the #GDBusConnection is active, it will interact with this
* stream from a worker thread, so it is not safe to interact with
* the stream directly.
*
* Since: 2.26
*/
g_object_class_install_property (gobject_class,
PROP_STREAM,
g_param_spec_object ("stream",
P_("IO Stream"),
P_("The underlying streams used for I/O"),
G_TYPE_IO_STREAM,
G_PARAM_READABLE |
G_PARAM_WRITABLE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_NAME |
G_PARAM_STATIC_BLURB |
G_PARAM_STATIC_NICK));
/**
* GDBusConnection:address:
*
* A D-Bus address specifying potential endpoints that can be used
* when establishing the connection.
*
* Since: 2.26
*/
g_object_class_install_property (gobject_class,
PROP_ADDRESS,
g_param_spec_string ("address",
P_("Address"),
P_("D-Bus address specifying potential socket endpoints"),
NULL,
G_PARAM_WRITABLE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_NAME |
G_PARAM_STATIC_BLURB |
G_PARAM_STATIC_NICK));
/**
* GDBusConnection:flags:
*
* Flags from the #GDBusConnectionFlags enumeration.
*
* Since: 2.26
*/
g_object_class_install_property (gobject_class,
PROP_FLAGS,
g_param_spec_flags ("flags",
P_("Flags"),
P_("Flags"),
G_TYPE_DBUS_CONNECTION_FLAGS,
G_DBUS_CONNECTION_FLAGS_NONE,
G_PARAM_READABLE |
G_PARAM_WRITABLE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_NAME |
G_PARAM_STATIC_BLURB |
G_PARAM_STATIC_NICK));
/**
* GDBusConnection:guid:
*
* The GUID of the peer performing the role of server when
* authenticating.
*
* If you are constructing a #GDBusConnection and pass
* %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER in the
* #GDBusConnection:flags property then you **must** also set this
* property to a valid guid.
*
* If you are constructing a #GDBusConnection and pass
* %G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT in the
* #GDBusConnection:flags property you will be able to read the GUID
* of the other peer here after the connection has been successfully
* initialized.
*
* Note that the
* [D-Bus specification](https://dbus.freedesktop.org/doc/dbus-specification.html#addresses)
* uses the term ‘UUID’ to refer to this, whereas GLib consistently uses the
* term ‘GUID’ for historical reasons.
*
* Despite its name, the format of #GDBusConnection:guid does not follow
* [RFC 4122](https://datatracker.ietf.org/doc/html/rfc4122) or the Microsoft
* GUID format.
*
* Since: 2.26
*/
g_object_class_install_property (gobject_class,
PROP_GUID,
g_param_spec_string ("guid",
P_("GUID"),
P_("GUID of the server peer"),
NULL,
G_PARAM_READABLE |
G_PARAM_WRITABLE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_NAME |
G_PARAM_STATIC_BLURB |
G_PARAM_STATIC_NICK));
/**
* GDBusConnection:unique-name:
*
* The unique name as assigned by the message bus or %NULL if the
* connection is not open or not a message bus connection.
*
* Since: 2.26
*/
g_object_class_install_property (gobject_class,
PROP_UNIQUE_NAME,
g_param_spec_string ("unique-name",
P_("unique-name"),
P_("Unique name of bus connection"),
NULL,
G_PARAM_READABLE |
G_PARAM_STATIC_NAME |
G_PARAM_STATIC_BLURB |
G_PARAM_STATIC_NICK));
/**
* GDBusConnection:closed:
*
* A boolean specifying whether the connection has been closed.
*
* Since: 2.26
*/
g_object_class_install_property (gobject_class,
PROP_CLOSED,
g_param_spec_boolean ("closed",
P_("Closed"),
P_("Whether the connection is closed"),
FALSE,
G_PARAM_READABLE |
G_PARAM_STATIC_NAME |
G_PARAM_STATIC_BLURB |
G_PARAM_STATIC_NICK));
/**
* GDBusConnection:exit-on-close:
*
* A boolean specifying whether the process will be terminated (by
* calling `raise(SIGTERM)`) if the connection is closed by the
* remote peer.
*
* Note that #GDBusConnection objects returned by g_bus_get_finish()
* and g_bus_get_sync() will (usually) have this property set to %TRUE.
*
* Since: 2.26
*/
g_object_class_install_property (gobject_class,
PROP_EXIT_ON_CLOSE,
g_param_spec_boolean ("exit-on-close",
P_("Exit on close"),
P_("Whether the process is terminated when the connection is closed"),
FALSE,
G_PARAM_READABLE |
G_PARAM_WRITABLE |
G_PARAM_STATIC_NAME |
G_PARAM_STATIC_BLURB |
G_PARAM_STATIC_NICK));
/**
* GDBusConnection:capabilities:
*
* Flags from the #GDBusCapabilityFlags enumeration
* representing connection features negotiated with the other peer.
*
* Since: 2.26
*/
g_object_class_install_property (gobject_class,
PROP_CAPABILITY_FLAGS,
g_param_spec_flags ("capabilities",
P_("Capabilities"),
P_("Capabilities"),
G_TYPE_DBUS_CAPABILITY_FLAGS,
G_DBUS_CAPABILITY_FLAGS_NONE,
G_PARAM_READABLE |
G_PARAM_STATIC_NAME |
G_PARAM_STATIC_BLURB |
G_PARAM_STATIC_NICK));
/**
* GDBusConnection:authentication-observer:
*
* A #GDBusAuthObserver object to assist in the authentication process or %NULL.
*
* Since: 2.26