Coverage Report

Created: 2025-07-01 07:09

/src/glib/gio/gdbusnamewatching.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
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2.1 of the License, or (at your option) any later version.
9
 *
10
 * This library is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General
16
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
17
 *
18
 * Author: David Zeuthen <davidz@redhat.com>
19
 */
20
21
#include "config.h"
22
23
#include <stdlib.h>
24
#include <string.h>
25
26
#include "gdbusutils.h"
27
#include "gdbusnamewatching.h"
28
#include "gdbuserror.h"
29
#include "gdbusprivate.h"
30
#include "gdbusconnection.h"
31
32
#include "glibintl.h"
33
34
/**
35
 * SECTION:gdbusnamewatching
36
 * @title: Watching Bus Names
37
 * @short_description: Simple API for watching bus names
38
 * @include: gio/gio.h
39
 *
40
 * Convenience API for watching bus names.
41
 *
42
 * A simple example for watching a name can be found in
43
 * [gdbus-example-watch-name.c](https://git.gnome.org/browse/glib/tree/gio/tests/gdbus-example-watch-name.c)
44
 */
45
46
G_LOCK_DEFINE_STATIC (lock);
47
48
/* ---------------------------------------------------------------------------------------------------- */
49
50
typedef enum
51
{
52
  PREVIOUS_CALL_NONE = 0,
53
  PREVIOUS_CALL_APPEARED,
54
  PREVIOUS_CALL_VANISHED,
55
} PreviousCall;
56
57
typedef struct
58
{
59
  gint                      ref_count;  /* (atomic) */
60
  guint                     id;
61
  gchar                    *name;
62
  GBusNameWatcherFlags      flags;
63
  gchar                    *name_owner;
64
  GBusNameAppearedCallback  name_appeared_handler;
65
  GBusNameVanishedCallback  name_vanished_handler;
66
  gpointer                  user_data;
67
  GDestroyNotify            user_data_free_func;
68
  GMainContext             *main_context;
69
70
  GDBusConnection          *connection;
71
  gulong                    disconnected_signal_handler_id;
72
  guint                     name_owner_changed_subscription_id;
73
74
  PreviousCall              previous_call;
75
76
  gboolean                  cancelled;
77
  gboolean                  initialized;
78
} Client;
79
80
/* Must be accessed atomically. */
81
static guint next_global_id = 1;  /* (atomic) */
82
83
/* Must be accessed with @lock held. */
84
static GHashTable *map_id_to_client = NULL;
85
86
static Client *
87
client_ref (Client *client)
88
0
{
89
0
  g_atomic_int_inc (&client->ref_count);
90
0
  return client;
91
0
}
92
93
static void
94
client_unref (Client *client)
95
0
{
96
0
  if (g_atomic_int_dec_and_test (&client->ref_count))
97
0
    {
98
0
      if (client->connection != NULL)
99
0
        {
100
0
          if (client->name_owner_changed_subscription_id > 0)
101
0
            g_dbus_connection_signal_unsubscribe (client->connection, client->name_owner_changed_subscription_id);
102
0
          if (client->disconnected_signal_handler_id > 0)
103
0
            g_signal_handler_disconnect (client->connection, client->disconnected_signal_handler_id);
104
0
          g_object_unref (client->connection);
105
0
        }
106
0
      g_free (client->name);
107
0
      g_free (client->name_owner);
108
0
      g_main_context_unref (client->main_context);
109
0
      if (client->user_data_free_func != NULL)
110
0
        client->user_data_free_func (client->user_data);
111
0
      g_free (client);
112
0
    }
113
0
}
114
115
/* ---------------------------------------------------------------------------------------------------- */
116
117
typedef enum
118
{
119
  CALL_TYPE_NAME_APPEARED,
120
  CALL_TYPE_NAME_VANISHED
121
} CallType;
122
123
typedef struct
124
{
125
  Client *client;
126
127
  /* keep this separate because client->connection may
128
   * be set to NULL after scheduling the call
129
   */
130
  GDBusConnection *connection;
131
132
  /* ditto */
133
  gchar *name_owner;
134
135
  CallType call_type;
136
} CallHandlerData;
137
138
static void
139
call_handler_data_free (CallHandlerData *data)
140
0
{
141
0
  if (data->connection != NULL)
142
0
    g_object_unref (data->connection);
143
0
  g_free (data->name_owner);
144
0
  client_unref (data->client);
145
0
  g_free (data);
146
0
}
147
148
static void
149
actually_do_call (Client *client, GDBusConnection *connection, const gchar *name_owner, CallType call_type)
150
0
{
151
  /* The client might have been cancelled (g_bus_unwatch_name()) while we were
152
   * sitting in the #GMainContext dispatch queue. */
153
0
  if (client->cancelled)
154
0
    return;
155
156
0
  switch (call_type)
157
0
    {
158
0
    case CALL_TYPE_NAME_APPEARED:
159
0
      if (client->name_appeared_handler != NULL)
160
0
        {
161
0
          client->name_appeared_handler (connection,
162
0
                                         client->name,
163
0
                                         name_owner,
164
0
                                         client->user_data);
165
0
        }
166
0
      break;
167
168
0
    case CALL_TYPE_NAME_VANISHED:
169
0
      if (client->name_vanished_handler != NULL)
170
0
        {
171
0
          client->name_vanished_handler (connection,
172
0
                                         client->name,
173
0
                                         client->user_data);
174
0
        }
175
0
      break;
176
177
0
    default:
178
0
      g_assert_not_reached ();
179
0
      break;
180
0
    }
181
0
}
182
183
static gboolean
184
call_in_idle_cb (gpointer _data)
185
0
{
186
0
  CallHandlerData *data = _data;
187
0
  actually_do_call (data->client, data->connection, data->name_owner, data->call_type);
188
0
  return FALSE;
189
0
}
190
191
static void
192
schedule_call_in_idle (Client *client, CallType call_type)
193
0
{
194
0
  CallHandlerData *data;
195
0
  GSource *idle_source;
196
197
0
  data = g_new0 (CallHandlerData, 1);
198
0
  data->client = client_ref (client);
199
0
  data->connection = client->connection != NULL ? g_object_ref (client->connection) : NULL;
200
0
  data->name_owner = g_strdup (client->name_owner);
201
0
  data->call_type = call_type;
202
203
0
  idle_source = g_idle_source_new ();
204
0
  g_source_set_priority (idle_source, G_PRIORITY_HIGH);
205
0
  g_source_set_callback (idle_source,
206
0
                         call_in_idle_cb,
207
0
                         data,
208
0
                         (GDestroyNotify) call_handler_data_free);
209
0
  g_source_set_name (idle_source, "[gio, gdbusnamewatching.c] call_in_idle_cb");
210
0
  g_source_attach (idle_source, client->main_context);
211
0
  g_source_unref (idle_source);
212
0
}
213
214
static void
215
do_call (Client *client, CallType call_type)
216
0
{
217
0
  GMainContext *current_context;
218
219
  /* only schedule in idle if we're not in the right thread */
220
0
  current_context = g_main_context_ref_thread_default ();
221
0
  if (current_context != client->main_context)
222
0
    schedule_call_in_idle (client, call_type);
223
0
  else
224
0
    actually_do_call (client, client->connection, client->name_owner, call_type);
225
0
  g_main_context_unref (current_context);
226
0
}
227
228
static void
229
call_appeared_handler (Client *client)
230
0
{
231
0
  if (client->previous_call != PREVIOUS_CALL_APPEARED)
232
0
    {
233
0
      client->previous_call = PREVIOUS_CALL_APPEARED;
234
0
      if (!client->cancelled && client->name_appeared_handler != NULL)
235
0
        {
236
0
          do_call (client, CALL_TYPE_NAME_APPEARED);
237
0
        }
238
0
    }
239
0
}
240
241
static void
242
call_vanished_handler (Client *client)
243
0
{
244
0
  if (client->previous_call != PREVIOUS_CALL_VANISHED)
245
0
    {
246
0
      client->previous_call = PREVIOUS_CALL_VANISHED;
247
0
      if (!client->cancelled && client->name_vanished_handler != NULL)
248
0
        {
249
0
          do_call (client, CALL_TYPE_NAME_VANISHED);
250
0
        }
251
0
    }
252
0
}
253
254
/* ---------------------------------------------------------------------------------------------------- */
255
256
/* Return a reference to the #Client for @watcher_id, or %NULL if it’s been
257
 * unwatched. This is safe to call from any thread. */
258
static Client *
259
dup_client (guint watcher_id)
260
0
{
261
0
  Client *client;
262
263
0
  G_LOCK (lock);
264
265
0
  g_assert (watcher_id != 0);
266
0
  g_assert (map_id_to_client != NULL);
267
268
0
  client = g_hash_table_lookup (map_id_to_client, GUINT_TO_POINTER (watcher_id));
269
270
0
  if (client != NULL)
271
0
    client_ref (client);
272
273
0
  G_UNLOCK (lock);
274
275
0
  return client;
276
0
}
277
278
/* Could be called from any thread, so it could be called after client_unref()
279
 * has started finalising the #Client. Avoid that by looking up the #Client
280
 * atomically. */
281
static void
282
on_connection_disconnected (GDBusConnection *connection,
283
                            gboolean         remote_peer_vanished,
284
                            GError          *error,
285
                            gpointer         user_data)
286
0
{
287
0
  guint watcher_id = GPOINTER_TO_UINT (user_data);
288
0
  Client *client = NULL;
289
290
0
  client = dup_client (watcher_id);
291
0
  if (client == NULL)
292
0
    return;
293
294
0
  if (client->name_owner_changed_subscription_id > 0)
295
0
    g_dbus_connection_signal_unsubscribe (client->connection, client->name_owner_changed_subscription_id);
296
0
  if (client->disconnected_signal_handler_id > 0)
297
0
    g_signal_handler_disconnect (client->connection, client->disconnected_signal_handler_id);
298
0
  g_object_unref (client->connection);
299
0
  client->disconnected_signal_handler_id = 0;
300
0
  client->name_owner_changed_subscription_id = 0;
301
0
  client->connection = NULL;
302
303
0
  call_vanished_handler (client);
304
305
0
  client_unref (client);
306
0
}
307
308
/* ---------------------------------------------------------------------------------------------------- */
309
310
/* Will always be called from the thread which acquired client->main_context. */
311
static void
312
on_name_owner_changed (GDBusConnection *connection,
313
                       const gchar      *sender_name,
314
                       const gchar      *object_path,
315
                       const gchar      *interface_name,
316
                       const gchar      *signal_name,
317
                       GVariant         *parameters,
318
                       gpointer          user_data)
319
0
{
320
0
  guint watcher_id = GPOINTER_TO_UINT (user_data);
321
0
  Client *client = NULL;
322
0
  const gchar *name;
323
0
  const gchar *old_owner;
324
0
  const gchar *new_owner;
325
326
0
  client = dup_client (watcher_id);
327
0
  if (client == NULL)
328
0
    return;
329
330
0
  if (!client->initialized)
331
0
    goto out;
332
333
0
  if (g_strcmp0 (object_path, "/org/freedesktop/DBus") != 0 ||
334
0
      g_strcmp0 (interface_name, "org.freedesktop.DBus") != 0 ||
335
0
      g_strcmp0 (sender_name, "org.freedesktop.DBus") != 0)
336
0
    goto out;
337
338
0
  g_variant_get (parameters,
339
0
                 "(&s&s&s)",
340
0
                 &name,
341
0
                 &old_owner,
342
0
                 &new_owner);
343
344
  /* we only care about a specific name */
345
0
  if (g_strcmp0 (name, client->name) != 0)
346
0
    goto out;
347
348
0
  if ((old_owner != NULL && strlen (old_owner) > 0) && client->name_owner != NULL)
349
0
    {
350
0
      g_free (client->name_owner);
351
0
      client->name_owner = NULL;
352
0
      call_vanished_handler (client);
353
0
    }
354
355
0
  if (new_owner != NULL && strlen (new_owner) > 0)
356
0
    {
357
0
      g_warn_if_fail (client->name_owner == NULL);
358
0
      g_free (client->name_owner);
359
0
      client->name_owner = g_strdup (new_owner);
360
0
      call_appeared_handler (client);
361
0
    }
362
363
0
 out:
364
0
  client_unref (client);
365
0
}
366
367
/* ---------------------------------------------------------------------------------------------------- */
368
369
static void
370
get_name_owner_cb (GObject      *source_object,
371
                   GAsyncResult *res,
372
                   gpointer      user_data)
373
0
{
374
0
  Client *client = user_data;
375
0
  GVariant *result;
376
0
  const char *name_owner;
377
378
0
  name_owner = NULL;
379
0
  result = NULL;
380
381
0
  result = g_dbus_connection_call_finish (client->connection,
382
0
                                          res,
383
0
                                          NULL);
384
0
  if (result != NULL)
385
0
    {
386
0
      g_variant_get (result, "(&s)", &name_owner);
387
0
    }
388
389
0
  if (name_owner != NULL)
390
0
    {
391
0
      g_warn_if_fail (client->name_owner == NULL);
392
0
      client->name_owner = g_strdup (name_owner);
393
0
      call_appeared_handler (client);
394
0
    }
395
0
  else
396
0
    {
397
0
      call_vanished_handler (client);
398
0
    }
399
400
0
  client->initialized = TRUE;
401
402
0
  if (result != NULL)
403
0
    g_variant_unref (result);
404
0
  client_unref (client);
405
0
}
406
407
/* ---------------------------------------------------------------------------------------------------- */
408
409
static void
410
invoke_get_name_owner (Client *client)
411
0
{
412
0
  g_dbus_connection_call (client->connection,
413
0
                          "org.freedesktop.DBus",  /* bus name */
414
0
                          "/org/freedesktop/DBus", /* object path */
415
0
                          "org.freedesktop.DBus",  /* interface name */
416
0
                          "GetNameOwner",          /* method name */
417
0
                          g_variant_new ("(s)", client->name),
418
0
                          G_VARIANT_TYPE ("(s)"),
419
0
                          G_DBUS_CALL_FLAGS_NONE,
420
0
                          -1,
421
0
                          NULL,
422
0
                          (GAsyncReadyCallback) get_name_owner_cb,
423
0
                          client_ref (client));
424
0
}
425
426
/* ---------------------------------------------------------------------------------------------------- */
427
428
static void
429
start_service_by_name_cb (GObject      *source_object,
430
                          GAsyncResult *res,
431
                          gpointer      user_data)
432
0
{
433
0
  Client *client = user_data;
434
0
  GVariant *result;
435
436
0
  result = NULL;
437
438
0
  result = g_dbus_connection_call_finish (client->connection,
439
0
                                          res,
440
0
                                          NULL);
441
0
  if (result != NULL)
442
0
    {
443
0
      guint32 start_service_result;
444
0
      g_variant_get (result, "(u)", &start_service_result);
445
446
0
      if (start_service_result == 1) /* DBUS_START_REPLY_SUCCESS */
447
0
        {
448
0
          invoke_get_name_owner (client);
449
0
        }
450
0
      else if (start_service_result == 2) /* DBUS_START_REPLY_ALREADY_RUNNING */
451
0
        {
452
0
          invoke_get_name_owner (client);
453
0
        }
454
0
      else
455
0
        {
456
0
          g_warning ("Unexpected reply %d from StartServiceByName() method", start_service_result);
457
0
          call_vanished_handler (client);
458
0
          client->initialized = TRUE;
459
0
        }
460
0
    }
461
0
  else
462
0
    {
463
      /* Errors are not unexpected; the bus will reply e.g.
464
       *
465
       *   org.freedesktop.DBus.Error.ServiceUnknown: The name org.gnome.Epiphany2
466
       *   was not provided by any .service files
467
       *
468
       * This doesn't mean that the name doesn't have an owner, just
469
       * that it's not provided by a .service file. So proceed to
470
       * invoke GetNameOwner().
471
       */
472
0
      invoke_get_name_owner (client);
473
0
    }
474
475
0
  if (result != NULL)
476
0
    g_variant_unref (result);
477
0
  client_unref (client);
478
0
}
479
480
/* ---------------------------------------------------------------------------------------------------- */
481
482
static void
483
has_connection (Client *client)
484
0
{
485
  /* listen for disconnection */
486
0
  client->disconnected_signal_handler_id = g_signal_connect (client->connection,
487
0
                                                             "closed",
488
0
                                                             G_CALLBACK (on_connection_disconnected),
489
0
                                                             GUINT_TO_POINTER (client->id));
490
491
  /* start listening to NameOwnerChanged messages immediately */
492
0
  client->name_owner_changed_subscription_id = g_dbus_connection_signal_subscribe (client->connection,
493
0
                                                                                   "org.freedesktop.DBus",  /* name */
494
0
                                                                                   "org.freedesktop.DBus",  /* if */
495
0
                                                                                   "NameOwnerChanged",      /* signal */
496
0
                                                                                   "/org/freedesktop/DBus", /* path */
497
0
                                                                                   client->name,
498
0
                                                                                   G_DBUS_SIGNAL_FLAGS_NONE,
499
0
                                                                                   on_name_owner_changed,
500
0
                                                                                   GUINT_TO_POINTER (client->id),
501
0
                                                                                   NULL);
502
503
0
  if (client->flags & G_BUS_NAME_WATCHER_FLAGS_AUTO_START)
504
0
    {
505
0
      g_dbus_connection_call (client->connection,
506
0
                              "org.freedesktop.DBus",  /* bus name */
507
0
                              "/org/freedesktop/DBus", /* object path */
508
0
                              "org.freedesktop.DBus",  /* interface name */
509
0
                              "StartServiceByName",    /* method name */
510
0
                              g_variant_new ("(su)", client->name, 0),
511
0
                              G_VARIANT_TYPE ("(u)"),
512
0
                              G_DBUS_CALL_FLAGS_NONE,
513
0
                              -1,
514
0
                              NULL,
515
0
                              (GAsyncReadyCallback) start_service_by_name_cb,
516
0
                              client_ref (client));
517
0
    }
518
0
  else
519
0
    {
520
      /* check owner */
521
0
      invoke_get_name_owner (client);
522
0
    }
523
0
}
524
525
526
static void
527
connection_get_cb (GObject      *source_object,
528
                   GAsyncResult *res,
529
                   gpointer      user_data)
530
0
{
531
0
  Client *client = user_data;
532
533
0
  client->connection = g_bus_get_finish (res, NULL);
534
0
  if (client->connection == NULL)
535
0
    {
536
0
      call_vanished_handler (client);
537
0
      goto out;
538
0
    }
539
540
0
  has_connection (client);
541
542
0
 out:
543
0
  client_unref (client);
544
0
}
545
546
/* ---------------------------------------------------------------------------------------------------- */
547
548
/**
549
 * g_bus_watch_name:
550
 * @bus_type: The type of bus to watch a name on.
551
 * @name: The name (well-known or unique) to watch.
552
 * @flags: Flags from the #GBusNameWatcherFlags enumeration.
553
 * @name_appeared_handler: (nullable): Handler to invoke when @name is known to exist or %NULL.
554
 * @name_vanished_handler: (nullable): Handler to invoke when @name is known to not exist or %NULL.
555
 * @user_data: User data to pass to handlers.
556
 * @user_data_free_func: (nullable): Function for freeing @user_data or %NULL.
557
 *
558
 * Starts watching @name on the bus specified by @bus_type and calls
559
 * @name_appeared_handler and @name_vanished_handler when the name is
560
 * known to have an owner respectively known to lose its
561
 * owner. Callbacks will be invoked in the
562
 * [thread-default main context][g-main-context-push-thread-default]
563
 * of the thread you are calling this function from.
564
 *
565
 * You are guaranteed that one of the handlers will be invoked after
566
 * calling this function. When you are done watching the name, just
567
 * call g_bus_unwatch_name() with the watcher id this function
568
 * returns.
569
 *
570
 * If the name vanishes or appears (for example the application owning
571
 * the name could restart), the handlers are also invoked. If the
572
 * #GDBusConnection that is used for watching the name disconnects, then
573
 * @name_vanished_handler is invoked since it is no longer
574
 * possible to access the name.
575
 *
576
 * Another guarantee is that invocations of @name_appeared_handler
577
 * and @name_vanished_handler are guaranteed to alternate; that
578
 * is, if @name_appeared_handler is invoked then you are
579
 * guaranteed that the next time one of the handlers is invoked, it
580
 * will be @name_vanished_handler. The reverse is also true.
581
 *
582
 * This behavior makes it very simple to write applications that want
583
 * to take action when a certain [name exists][gdbus-watching-names].
584
 * Basically, the application should create object proxies in
585
 * @name_appeared_handler and destroy them again (if any) in
586
 * @name_vanished_handler.
587
 *
588
 * Returns: An identifier (never 0) that can be used with
589
 * g_bus_unwatch_name() to stop watching the name.
590
 *
591
 * Since: 2.26
592
 */
593
guint
594
g_bus_watch_name (GBusType                  bus_type,
595
                  const gchar              *name,
596
                  GBusNameWatcherFlags      flags,
597
                  GBusNameAppearedCallback  name_appeared_handler,
598
                  GBusNameVanishedCallback  name_vanished_handler,
599
                  gpointer                  user_data,
600
                  GDestroyNotify            user_data_free_func)
601
0
{
602
0
  Client *client;
603
604
0
  g_return_val_if_fail (g_dbus_is_name (name), 0);
605
606
0
  G_LOCK (lock);
607
608
0
  client = g_new0 (Client, 1);
609
0
  client->ref_count = 1;
610
0
  client->id = (guint) g_atomic_int_add (&next_global_id, 1); /* TODO: uh oh, handle overflow */
611
0
  client->name = g_strdup (name);
612
0
  client->flags = flags;
613
0
  client->name_appeared_handler = name_appeared_handler;
614
0
  client->name_vanished_handler = name_vanished_handler;
615
0
  client->user_data = user_data;
616
0
  client->user_data_free_func = user_data_free_func;
617
0
  client->main_context = g_main_context_ref_thread_default ();
618
619
0
  if (map_id_to_client == NULL)
620
0
    {
621
0
      map_id_to_client = g_hash_table_new (g_direct_hash, g_direct_equal);
622
0
    }
623
0
  g_hash_table_insert (map_id_to_client,
624
0
                       GUINT_TO_POINTER (client->id),
625
0
                       client);
626
627
0
  g_bus_get (bus_type,
628
0
             NULL,
629
0
             connection_get_cb,
630
0
             client_ref (client));
631
632
0
  G_UNLOCK (lock);
633
634
0
  return client->id;
635
0
}
636
637
/**
638
 * g_bus_watch_name_on_connection:
639
 * @connection: A #GDBusConnection.
640
 * @name: The name (well-known or unique) to watch.
641
 * @flags: Flags from the #GBusNameWatcherFlags enumeration.
642
 * @name_appeared_handler: (nullable): Handler to invoke when @name is known to exist or %NULL.
643
 * @name_vanished_handler: (nullable): Handler to invoke when @name is known to not exist or %NULL.
644
 * @user_data: User data to pass to handlers.
645
 * @user_data_free_func: (nullable): Function for freeing @user_data or %NULL.
646
 *
647
 * Like g_bus_watch_name() but takes a #GDBusConnection instead of a
648
 * #GBusType.
649
 *
650
 * Returns: An identifier (never 0) that can be used with
651
 * g_bus_unwatch_name() to stop watching the name.
652
 *
653
 * Since: 2.26
654
 */
655
guint g_bus_watch_name_on_connection (GDBusConnection          *connection,
656
                                      const gchar              *name,
657
                                      GBusNameWatcherFlags      flags,
658
                                      GBusNameAppearedCallback  name_appeared_handler,
659
                                      GBusNameVanishedCallback  name_vanished_handler,
660
                                      gpointer                  user_data,
661
                                      GDestroyNotify            user_data_free_func)
662
0
{
663
0
  Client *client;
664
665
0
  g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), 0);
666
0
  g_return_val_if_fail (g_dbus_is_name (name), 0);
667
668
0
  G_LOCK (lock);
669
670
0
  client = g_new0 (Client, 1);
671
0
  client->ref_count = 1;
672
0
  client->id = (guint) g_atomic_int_add (&next_global_id, 1); /* TODO: uh oh, handle overflow */
673
0
  client->name = g_strdup (name);
674
0
  client->flags = flags;
675
0
  client->name_appeared_handler = name_appeared_handler;
676
0
  client->name_vanished_handler = name_vanished_handler;
677
0
  client->user_data = user_data;
678
0
  client->user_data_free_func = user_data_free_func;
679
0
  client->main_context = g_main_context_ref_thread_default ();
680
681
0
  if (map_id_to_client == NULL)
682
0
    map_id_to_client = g_hash_table_new (g_direct_hash, g_direct_equal);
683
684
0
  g_hash_table_insert (map_id_to_client,
685
0
                       GUINT_TO_POINTER (client->id),
686
0
                       client);
687
688
0
  client->connection = g_object_ref (connection);
689
0
  G_UNLOCK (lock);
690
691
0
  has_connection (client);
692
693
0
  return client->id;
694
0
}
695
696
typedef struct {
697
  GClosure *name_appeared_closure;
698
  GClosure *name_vanished_closure;
699
} WatchNameData;
700
701
static WatchNameData *
702
watch_name_data_new (GClosure *name_appeared_closure,
703
                     GClosure *name_vanished_closure)
704
0
{
705
0
  WatchNameData *data;
706
707
0
  data = g_new0 (WatchNameData, 1);
708
709
0
  if (name_appeared_closure != NULL)
710
0
    {
711
0
      data->name_appeared_closure = g_closure_ref (name_appeared_closure);
712
0
      g_closure_sink (name_appeared_closure);
713
0
      if (G_CLOSURE_NEEDS_MARSHAL (name_appeared_closure))
714
0
        g_closure_set_marshal (name_appeared_closure, g_cclosure_marshal_generic);
715
0
    }
716
717
0
  if (name_vanished_closure != NULL)
718
0
    {
719
0
      data->name_vanished_closure = g_closure_ref (name_vanished_closure);
720
0
      g_closure_sink (name_vanished_closure);
721
0
      if (G_CLOSURE_NEEDS_MARSHAL (name_vanished_closure))
722
0
        g_closure_set_marshal (name_vanished_closure, g_cclosure_marshal_generic);
723
0
    }
724
725
0
  return data;
726
0
}
727
728
static void
729
watch_with_closures_on_name_appeared (GDBusConnection *connection,
730
                                      const gchar     *name,
731
                                      const gchar     *name_owner,
732
                                      gpointer         user_data)
733
0
{
734
0
  WatchNameData *data = user_data;
735
0
  GValue params[3] = { G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT };
736
737
0
  g_value_init (&params[0], G_TYPE_DBUS_CONNECTION);
738
0
  g_value_set_object (&params[0], connection);
739
740
0
  g_value_init (&params[1], G_TYPE_STRING);
741
0
  g_value_set_string (&params[1], name);
742
743
0
  g_value_init (&params[2], G_TYPE_STRING);
744
0
  g_value_set_string (&params[2], name_owner);
745
746
0
  g_closure_invoke (data->name_appeared_closure, NULL, 3, params, NULL);
747
748
0
  g_value_unset (params + 0);
749
0
  g_value_unset (params + 1);
750
0
  g_value_unset (params + 2);
751
0
}
752
753
static void
754
watch_with_closures_on_name_vanished (GDBusConnection *connection,
755
                                      const gchar     *name,
756
                                      gpointer         user_data)
757
0
{
758
0
  WatchNameData *data = user_data;
759
0
  GValue params[2] = { G_VALUE_INIT, G_VALUE_INIT };
760
761
0
  g_value_init (&params[0], G_TYPE_DBUS_CONNECTION);
762
0
  g_value_set_object (&params[0], connection);
763
764
0
  g_value_init (&params[1], G_TYPE_STRING);
765
0
  g_value_set_string (&params[1], name);
766
767
0
  g_closure_invoke (data->name_vanished_closure, NULL, 2, params, NULL);
768
769
0
  g_value_unset (params + 0);
770
0
  g_value_unset (params + 1);
771
0
}
772
773
static void
774
bus_watch_name_free_func (gpointer user_data)
775
0
{
776
0
  WatchNameData *data = user_data;
777
778
0
  if (data->name_appeared_closure != NULL)
779
0
    g_closure_unref (data->name_appeared_closure);
780
781
0
  if (data->name_vanished_closure != NULL)
782
0
    g_closure_unref (data->name_vanished_closure);
783
784
0
  g_free (data);
785
0
}
786
787
/**
788
 * g_bus_watch_name_with_closures: (rename-to g_bus_watch_name)
789
 * @bus_type: The type of bus to watch a name on.
790
 * @name: The name (well-known or unique) to watch.
791
 * @flags: Flags from the #GBusNameWatcherFlags enumeration.
792
 * @name_appeared_closure: (nullable): #GClosure to invoke when @name is known
793
 * to exist or %NULL.
794
 * @name_vanished_closure: (nullable): #GClosure to invoke when @name is known
795
 * to not exist or %NULL.
796
 *
797
 * Version of g_bus_watch_name() using closures instead of callbacks for
798
 * easier binding in other languages.
799
 *
800
 * Returns: An identifier (never 0) that can be used with
801
 * g_bus_unwatch_name() to stop watching the name.
802
 *
803
 * Since: 2.26
804
 */
805
guint
806
g_bus_watch_name_with_closures (GBusType                 bus_type,
807
                                const gchar             *name,
808
                                GBusNameWatcherFlags     flags,
809
                                GClosure                *name_appeared_closure,
810
                                GClosure                *name_vanished_closure)
811
0
{
812
0
  return g_bus_watch_name (bus_type,
813
0
          name,
814
0
          flags,
815
0
          name_appeared_closure != NULL ? watch_with_closures_on_name_appeared : NULL,
816
0
          name_vanished_closure != NULL ? watch_with_closures_on_name_vanished : NULL,
817
0
          watch_name_data_new (name_appeared_closure, name_vanished_closure),
818
0
          bus_watch_name_free_func);
819
0
}
820
821
/**
822
 * g_bus_watch_name_on_connection_with_closures: (rename-to g_bus_watch_name_on_connection)
823
 * @connection: A #GDBusConnection.
824
 * @name: The name (well-known or unique) to watch.
825
 * @flags: Flags from the #GBusNameWatcherFlags enumeration.
826
 * @name_appeared_closure: (nullable): #GClosure to invoke when @name is known
827
 * to exist or %NULL.
828
 * @name_vanished_closure: (nullable): #GClosure to invoke when @name is known
829
 * to not exist or %NULL.
830
 *
831
 * Version of g_bus_watch_name_on_connection() using closures instead of callbacks for
832
 * easier binding in other languages.
833
 *
834
 * Returns: An identifier (never 0) that can be used with
835
 * g_bus_unwatch_name() to stop watching the name.
836
 *
837
 * Since: 2.26
838
 */
839
guint g_bus_watch_name_on_connection_with_closures (
840
                                      GDBusConnection          *connection,
841
                                      const gchar              *name,
842
                                      GBusNameWatcherFlags      flags,
843
                                      GClosure                 *name_appeared_closure,
844
                                      GClosure                 *name_vanished_closure)
845
0
{
846
0
  return g_bus_watch_name_on_connection (connection,
847
0
          name,
848
0
          flags,
849
0
          name_appeared_closure != NULL ? watch_with_closures_on_name_appeared : NULL,
850
0
          name_vanished_closure != NULL ? watch_with_closures_on_name_vanished : NULL,
851
0
          watch_name_data_new (name_appeared_closure, name_vanished_closure),
852
0
          bus_watch_name_free_func);
853
0
}
854
855
/**
856
 * g_bus_unwatch_name:
857
 * @watcher_id: An identifier obtained from g_bus_watch_name()
858
 *
859
 * Stops watching a name.
860
 *
861
 * Note that there may still be D-Bus traffic to process (relating to watching
862
 * and unwatching the name) in the current thread-default #GMainContext after
863
 * this function has returned. You should continue to iterate the #GMainContext
864
 * until the #GDestroyNotify function passed to g_bus_watch_name() is called, in
865
 * order to avoid memory leaks through callbacks queued on the #GMainContext
866
 * after it’s stopped being iterated.
867
 *
868
 * Since: 2.26
869
 */
870
void
871
g_bus_unwatch_name (guint watcher_id)
872
0
{
873
0
  Client *client;
874
875
0
  g_return_if_fail (watcher_id > 0);
876
877
0
  client = NULL;
878
879
0
  G_LOCK (lock);
880
0
  if (watcher_id == 0 ||
881
0
      map_id_to_client == NULL ||
882
0
      (client = g_hash_table_lookup (map_id_to_client, GUINT_TO_POINTER (watcher_id))) == NULL)
883
0
    {
884
0
      g_warning ("Invalid id %d passed to g_bus_unwatch_name()", watcher_id);
885
0
      goto out;
886
0
    }
887
888
0
  client->cancelled = TRUE;
889
0
  g_warn_if_fail (g_hash_table_remove (map_id_to_client, GUINT_TO_POINTER (watcher_id)));
890
891
0
 out:
892
0
  G_UNLOCK (lock);
893
894
  /* do callback without holding lock */
895
0
  if (client != NULL)
896
0
    {
897
0
      client_unref (client);
898
0
    }
899
0
}