Coverage Report

Created: 2025-06-13 06:55

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