Coverage Report

Created: 2025-07-23 06:49

/src/rauc/subprojects/glib-2.76.5/gio/gdbusnameowning.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
27
#include "gdbusutils.h"
28
#include "gdbusnameowning.h"
29
#include "gdbuserror.h"
30
#include "gdbusprivate.h"
31
#include "gdbusconnection.h"
32
33
#include "glibintl.h"
34
35
/**
36
 * SECTION:gdbusnameowning
37
 * @title: Owning Bus Names
38
 * @short_description: Simple API for owning bus names
39
 * @include: gio/gio.h
40
 *
41
 * Convenience API for owning bus names.
42
 *
43
 * A simple example for owning a name can be found in
44
 * [gdbus-example-own-name.c](https://gitlab.gnome.org/GNOME/glib/-/blob/HEAD/gio/tests/gdbus-example-own-name.c)
45
 */
46
47
G_LOCK_DEFINE_STATIC (lock);
48
49
/* ---------------------------------------------------------------------------------------------------- */
50
51
typedef enum
52
{
53
  PREVIOUS_CALL_NONE = 0,
54
  PREVIOUS_CALL_ACQUIRED,
55
  PREVIOUS_CALL_LOST,
56
} PreviousCall;
57
58
typedef struct
59
{
60
  gint                      ref_count;  /* (atomic) */
61
  guint                     id;
62
  GBusNameOwnerFlags        flags;
63
  gchar                    *name;
64
  GBusAcquiredCallback      bus_acquired_handler;
65
  GBusNameAcquiredCallback  name_acquired_handler;
66
  GBusNameLostCallback      name_lost_handler;
67
  gpointer                  user_data;
68
  GDestroyNotify            user_data_free_func;
69
  GMainContext             *main_context;
70
71
  PreviousCall              previous_call;
72
73
  GDBusConnection          *connection;
74
  gulong                    disconnected_signal_handler_id;
75
  guint                     name_acquired_subscription_id;
76
  guint                     name_lost_subscription_id;
77
78
  gboolean                  cancelled; /* must hold lock when reading or modifying */
79
80
  gboolean                  needs_release;
81
} Client;
82
83
static guint next_global_id = 1;
84
static GHashTable *map_id_to_client = NULL;
85
86
87
static Client *
88
client_ref (Client *client)
89
0
{
90
0
  g_atomic_int_inc (&client->ref_count);
91
0
  return client;
92
0
}
93
94
static void
95
client_unref (Client *client)
96
0
{
97
0
  if (g_atomic_int_dec_and_test (&client->ref_count))
98
0
    {
99
0
      if (client->connection != NULL)
100
0
        {
101
0
          if (client->disconnected_signal_handler_id > 0)
102
0
            g_signal_handler_disconnect (client->connection, client->disconnected_signal_handler_id);
103
0
          if (client->name_acquired_subscription_id > 0)
104
0
            g_dbus_connection_signal_unsubscribe (client->connection, client->name_acquired_subscription_id);
105
0
          if (client->name_lost_subscription_id > 0)
106
0
            g_dbus_connection_signal_unsubscribe (client->connection, client->name_lost_subscription_id);
107
0
          g_object_unref (client->connection);
108
0
        }
109
0
      g_main_context_unref (client->main_context);
110
0
      g_free (client->name);
111
0
      if (client->user_data_free_func != NULL)
112
0
        client->user_data_free_func (client->user_data);
113
0
      g_free (client);
114
0
    }
115
0
}
116
117
/* ---------------------------------------------------------------------------------------------------- */
118
119
120
typedef enum
121
{
122
  CALL_TYPE_NAME_ACQUIRED,
123
  CALL_TYPE_NAME_LOST
124
} CallType;
125
126
typedef struct
127
{
128
  Client *client;
129
130
  /* keep this separate because client->connection may
131
   * be set to NULL after scheduling the call
132
   */
133
  GDBusConnection *connection;
134
135
  /* set to TRUE to call acquired */
136
  CallType call_type;
137
} CallHandlerData;
138
139
static void
140
call_handler_data_free (CallHandlerData *data)
141
0
{
142
0
  if (data->connection != NULL)
143
0
    g_object_unref (data->connection);
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, CallType call_type)
150
0
{
151
0
  switch (call_type)
152
0
    {
153
0
    case CALL_TYPE_NAME_ACQUIRED:
154
0
      if (client->name_acquired_handler != NULL)
155
0
        {
156
0
          client->name_acquired_handler (connection,
157
0
                                         client->name,
158
0
                                         client->user_data);
159
0
        }
160
0
      break;
161
162
0
    case CALL_TYPE_NAME_LOST:
163
0
      if (client->name_lost_handler != NULL)
164
0
        {
165
0
          client->name_lost_handler (connection,
166
0
                                     client->name,
167
0
                                     client->user_data);
168
0
        }
169
0
      break;
170
171
0
    default:
172
0
      g_assert_not_reached ();
173
0
      break;
174
0
    }
175
0
}
176
177
static gboolean
178
call_in_idle_cb (gpointer _data)
179
0
{
180
0
  CallHandlerData *data = _data;
181
0
  actually_do_call (data->client, data->connection, data->call_type);
182
0
  return FALSE;
183
0
}
184
185
static void
186
schedule_call_in_idle (Client *client, CallType  call_type)
187
0
{
188
0
  CallHandlerData *data;
189
0
  GSource *idle_source;
190
191
0
  data = g_new0 (CallHandlerData, 1);
192
0
  data->client = client_ref (client);
193
0
  data->connection = client->connection != NULL ? g_object_ref (client->connection) : NULL;
194
0
  data->call_type = call_type;
195
196
0
  idle_source = g_idle_source_new ();
197
0
  g_source_set_priority (idle_source, G_PRIORITY_HIGH);
198
0
  g_source_set_callback (idle_source,
199
0
                         call_in_idle_cb,
200
0
                         data,
201
0
                         (GDestroyNotify) call_handler_data_free);
202
0
  g_source_set_static_name (idle_source, "[gio, gdbusnameowning.c] call_in_idle_cb");
203
0
  g_source_attach (idle_source, client->main_context);
204
0
  g_source_unref (idle_source);
205
0
}
206
207
static void
208
do_call (Client *client, CallType call_type)
209
0
{
210
0
  GMainContext *current_context;
211
212
  /* only schedule in idle if we're not in the right thread */
213
0
  current_context = g_main_context_ref_thread_default ();
214
0
  if (current_context != client->main_context)
215
0
    schedule_call_in_idle (client, call_type);
216
0
  else
217
0
    actually_do_call (client, client->connection, call_type);
218
0
  g_main_context_unref (current_context);
219
0
}
220
221
static void
222
call_acquired_handler (Client *client)
223
0
{
224
0
  G_LOCK (lock);
225
0
  if (client->previous_call != PREVIOUS_CALL_ACQUIRED)
226
0
    {
227
0
      client->previous_call = PREVIOUS_CALL_ACQUIRED;
228
0
      if (!client->cancelled)
229
0
        {
230
0
          G_UNLOCK (lock);
231
0
          do_call (client, CALL_TYPE_NAME_ACQUIRED);
232
0
          goto out;
233
0
        }
234
0
    }
235
0
  G_UNLOCK (lock);
236
0
 out:
237
0
  ;
238
0
}
239
240
static void
241
call_lost_handler (Client  *client)
242
0
{
243
0
  G_LOCK (lock);
244
0
  if (client->previous_call != PREVIOUS_CALL_LOST)
245
0
    {
246
0
      client->previous_call = PREVIOUS_CALL_LOST;
247
0
      if (!client->cancelled)
248
0
        {
249
0
          G_UNLOCK (lock);
250
0
          do_call (client, CALL_TYPE_NAME_LOST);
251
0
          goto out;
252
0
        }
253
0
    }
254
0
  G_UNLOCK (lock);
255
0
 out:
256
0
  ;
257
0
}
258
259
/* ---------------------------------------------------------------------------------------------------- */
260
261
static void
262
on_name_lost_or_acquired (GDBusConnection  *connection,
263
                          const gchar      *sender_name,
264
                          const gchar      *object_path,
265
                          const gchar      *interface_name,
266
                          const gchar      *signal_name,
267
                          GVariant         *parameters,
268
                          gpointer          user_data)
269
0
{
270
0
  Client *client = user_data;
271
0
  const gchar *name;
272
273
0
  if (g_strcmp0 (object_path, "/org/freedesktop/DBus") != 0 ||
274
0
      g_strcmp0 (interface_name, "org.freedesktop.DBus") != 0 ||
275
0
      g_strcmp0 (sender_name, "org.freedesktop.DBus") != 0)
276
0
    goto out;
277
278
0
  if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(s)")))
279
0
    {
280
0
      g_warning ("%s signal had unexpected signature %s", signal_name,
281
0
                 g_variant_get_type_string (parameters));
282
0
      goto out;
283
0
    }
284
285
0
  if (g_strcmp0 (signal_name, "NameLost") == 0)
286
0
    {
287
0
      g_variant_get (parameters, "(&s)", &name);
288
0
      if (g_strcmp0 (name, client->name) == 0)
289
0
        {
290
0
          call_lost_handler (client);
291
0
        }
292
0
    }
293
0
  else if (g_strcmp0 (signal_name, "NameAcquired") == 0)
294
0
    {
295
0
      g_variant_get (parameters, "(&s)", &name);
296
0
      if (g_strcmp0 (name, client->name) == 0)
297
0
        {
298
0
          call_acquired_handler (client);
299
0
        }
300
0
    }
301
0
 out:
302
0
  ;
303
0
}
304
305
/* ---------------------------------------------------------------------------------------------------- */
306
307
static void
308
request_name_cb (GObject      *source_object,
309
                 GAsyncResult *res,
310
                 gpointer      user_data)
311
0
{
312
0
  Client *client = user_data;
313
0
  GVariant *result;
314
0
  guint32 request_name_reply;
315
0
  gboolean unsubscribe;
316
317
0
  request_name_reply = 0;
318
0
  result = NULL;
319
320
  /* don't use client->connection - it may be NULL already */
321
0
  result = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object),
322
0
                                          res,
323
0
                                          NULL);
324
0
  if (result != NULL)
325
0
    {
326
0
      g_variant_get (result, "(u)", &request_name_reply);
327
0
      g_variant_unref (result);
328
0
    }
329
330
0
  unsubscribe = FALSE;
331
332
0
  switch (request_name_reply)
333
0
    {
334
0
    case 1: /* DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER */
335
      /* We got the name - now listen for NameLost and NameAcquired */
336
0
      call_acquired_handler (client);
337
0
      break;
338
339
0
    case 2: /* DBUS_REQUEST_NAME_REPLY_IN_QUEUE */
340
      /* Waiting in line - listen for NameLost and NameAcquired */
341
0
      call_lost_handler (client);
342
0
      break;
343
344
0
    default:
345
      /* assume we couldn't get the name - explicit fallthrough */
346
0
    case 3: /* DBUS_REQUEST_NAME_REPLY_EXISTS */
347
0
    case 4: /* DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER */
348
      /* Some other part of the process is already owning the name */
349
0
      call_lost_handler (client);
350
0
      unsubscribe = TRUE;
351
0
      client->needs_release = FALSE;
352
0
      break;
353
0
    }
354
355
  /* If we’re not the owner and not in the queue, there’s no point in continuing
356
   * to listen to NameAcquired or NameLost. */
357
0
  if (unsubscribe)
358
0
    {
359
0
      GDBusConnection *connection = NULL;
360
361
      /* make sure we use a known good Connection object since it may be set to
362
       * NULL at any point after being cancelled
363
       */
364
0
      G_LOCK (lock);
365
0
      if (!client->cancelled)
366
0
        connection = g_object_ref (client->connection);
367
0
      G_UNLOCK (lock);
368
369
0
      if (connection != NULL)
370
0
        {
371
0
          if (client->name_acquired_subscription_id > 0)
372
0
            g_dbus_connection_signal_unsubscribe (client->connection, client->name_acquired_subscription_id);
373
0
          if (client->name_lost_subscription_id > 0)
374
0
            g_dbus_connection_signal_unsubscribe (client->connection, client->name_lost_subscription_id);
375
0
          client->name_acquired_subscription_id = 0;
376
0
          client->name_lost_subscription_id = 0;
377
378
0
          g_object_unref (connection);
379
0
        }
380
0
    }
381
382
0
  client_unref (client);
383
0
}
384
385
/* ---------------------------------------------------------------------------------------------------- */
386
387
static void
388
on_connection_disconnected (GDBusConnection *connection,
389
                            gboolean         remote_peer_vanished,
390
                            GError          *error,
391
                            gpointer         user_data)
392
0
{
393
0
  Client *client = user_data;
394
395
0
  if (client->disconnected_signal_handler_id > 0)
396
0
    g_signal_handler_disconnect (client->connection, client->disconnected_signal_handler_id);
397
0
  if (client->name_acquired_subscription_id > 0)
398
0
    g_dbus_connection_signal_unsubscribe (client->connection, client->name_acquired_subscription_id);
399
0
  if (client->name_lost_subscription_id > 0)
400
0
    g_dbus_connection_signal_unsubscribe (client->connection, client->name_lost_subscription_id);
401
0
  g_object_unref (client->connection);
402
0
  client->disconnected_signal_handler_id = 0;
403
0
  client->name_acquired_subscription_id = 0;
404
0
  client->name_lost_subscription_id = 0;
405
0
  client->connection = NULL;
406
407
0
  call_lost_handler (client);
408
0
}
409
410
/* ---------------------------------------------------------------------------------------------------- */
411
412
static void
413
has_connection (Client *client)
414
0
{
415
  /* listen for disconnection */
416
0
  client->disconnected_signal_handler_id = g_signal_connect (client->connection,
417
0
                                                             "closed",
418
0
                                                             G_CALLBACK (on_connection_disconnected),
419
0
                                                             client);
420
421
  /* Start listening to NameLost and NameAcquired messages. We hold
422
   * references to the Client in the signal closures, since it’s possible
423
   * for a signal to be in-flight after unsubscribing the signal handler.
424
   * This creates a reference count cycle, but that’s explicitly broken by
425
   * disconnecting the signal handlers before calling client_unref() in
426
   * g_bus_unown_name().
427
   *
428
   * Subscribe to NameLost and NameAcquired before calling RequestName() to
429
   * avoid the potential race of losing the name between receiving a reply to
430
   * RequestName() and subscribing to NameLost. The #PreviousCall state will
431
   * ensure that the user callbacks get called an appropriate number of times. */
432
0
  client->name_lost_subscription_id =
433
0
    g_dbus_connection_signal_subscribe (client->connection,
434
0
                                        "org.freedesktop.DBus",
435
0
                                        "org.freedesktop.DBus",
436
0
                                        "NameLost",
437
0
                                        "/org/freedesktop/DBus",
438
0
                                        client->name,
439
0
                                        G_DBUS_SIGNAL_FLAGS_NONE,
440
0
                                        on_name_lost_or_acquired,
441
0
                                        client_ref (client),
442
0
                                        (GDestroyNotify) client_unref);
443
0
  client->name_acquired_subscription_id =
444
0
    g_dbus_connection_signal_subscribe (client->connection,
445
0
                                        "org.freedesktop.DBus",
446
0
                                        "org.freedesktop.DBus",
447
0
                                        "NameAcquired",
448
0
                                        "/org/freedesktop/DBus",
449
0
                                        client->name,
450
0
                                        G_DBUS_SIGNAL_FLAGS_NONE,
451
0
                                        on_name_lost_or_acquired,
452
0
                                        client_ref (client),
453
0
                                        (GDestroyNotify) client_unref);
454
455
  /* attempt to acquire the name */
456
0
  client->needs_release = TRUE;
457
0
  g_dbus_connection_call (client->connection,
458
0
                          "org.freedesktop.DBus",  /* bus name */
459
0
                          "/org/freedesktop/DBus", /* object path */
460
0
                          "org.freedesktop.DBus",  /* interface name */
461
0
                          "RequestName",           /* method name */
462
0
                          g_variant_new ("(su)",
463
0
                                         client->name,
464
0
                                         client->flags),
465
0
                          G_VARIANT_TYPE ("(u)"),
466
0
                          G_DBUS_CALL_FLAGS_NONE,
467
0
                          -1,
468
0
                          NULL,
469
0
                          (GAsyncReadyCallback) request_name_cb,
470
0
                          client_ref (client));
471
0
}
472
473
474
static void
475
connection_get_cb (GObject      *source_object,
476
                   GAsyncResult *res,
477
                   gpointer      user_data)
478
0
{
479
0
  Client *client = user_data;
480
481
  /* must not do anything if already cancelled */
482
0
  G_LOCK (lock);
483
0
  if (client->cancelled)
484
0
    {
485
0
      G_UNLOCK (lock);
486
0
      goto out;
487
0
    }
488
0
  G_UNLOCK (lock);
489
490
0
  client->connection = g_bus_get_finish (res, NULL);
491
0
  if (client->connection == NULL)
492
0
    {
493
0
      call_lost_handler (client);
494
0
      goto out;
495
0
    }
496
497
  /* No need to schedule this in idle as we're already in the thread
498
   * that the user called g_bus_own_name() from. This is because
499
   * g_bus_get() guarantees that.
500
   *
501
   * Also, we need to ensure that the handler is invoked *before*
502
   * we call RequestName(). Otherwise there is a race.
503
   */
504
0
  if (client->bus_acquired_handler != NULL)
505
0
    {
506
0
      client->bus_acquired_handler (client->connection,
507
0
                                    client->name,
508
0
                                    client->user_data);
509
0
    }
510
511
0
  has_connection (client);
512
513
0
 out:
514
0
  client_unref (client);
515
0
}
516
517
/* ---------------------------------------------------------------------------------------------------- */
518
519
/**
520
 * g_bus_own_name_on_connection:
521
 * @connection: a #GDBusConnection
522
 * @name: the well-known name to own
523
 * @flags: a set of flags from the #GBusNameOwnerFlags enumeration
524
 * @name_acquired_handler: (nullable): handler to invoke when @name is acquired or %NULL
525
 * @name_lost_handler: (nullable): handler to invoke when @name is lost or %NULL
526
 * @user_data: user data to pass to handlers
527
 * @user_data_free_func: (nullable): function for freeing @user_data or %NULL
528
 *
529
 * Like g_bus_own_name() but takes a #GDBusConnection instead of a
530
 * #GBusType.
531
 *
532
 * Returns: an identifier (never 0) that can be used with
533
 *     g_bus_unown_name() to stop owning the name
534
 *
535
 * Since: 2.26
536
 */
537
guint
538
g_bus_own_name_on_connection (GDBusConnection          *connection,
539
                              const gchar              *name,
540
                              GBusNameOwnerFlags        flags,
541
                              GBusNameAcquiredCallback  name_acquired_handler,
542
                              GBusNameLostCallback      name_lost_handler,
543
                              gpointer                  user_data,
544
                              GDestroyNotify            user_data_free_func)
545
0
{
546
0
  Client *client;
547
548
0
  g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), 0);
549
0
  g_return_val_if_fail (g_dbus_is_name (name) && !g_dbus_is_unique_name (name), 0);
550
551
0
  G_LOCK (lock);
552
553
0
  client = g_new0 (Client, 1);
554
0
  client->ref_count = 1;
555
0
  client->id = next_global_id++; /* TODO: uh oh, handle overflow */
556
0
  client->name = g_strdup (name);
557
0
  client->flags = flags;
558
0
  client->name_acquired_handler = name_acquired_handler;
559
0
  client->name_lost_handler = name_lost_handler;
560
0
  client->user_data = user_data;
561
0
  client->user_data_free_func = user_data_free_func;
562
0
  client->main_context = g_main_context_ref_thread_default ();
563
564
0
  client->connection = g_object_ref (connection);
565
566
0
  if (map_id_to_client == NULL)
567
0
    {
568
0
      map_id_to_client = g_hash_table_new (g_direct_hash, g_direct_equal);
569
0
    }
570
0
  g_hash_table_insert (map_id_to_client,
571
0
                       GUINT_TO_POINTER (client->id),
572
0
                       client);
573
574
0
  G_UNLOCK (lock);
575
576
0
  has_connection (client);
577
578
0
  return client->id;
579
0
}
580
581
/**
582
 * g_bus_own_name:
583
 * @bus_type: the type of bus to own a name on
584
 * @name: the well-known name to own
585
 * @flags: a set of flags from the #GBusNameOwnerFlags enumeration
586
 * @bus_acquired_handler: (nullable): handler to invoke when connected to the bus of type @bus_type or %NULL
587
 * @name_acquired_handler: (nullable): handler to invoke when @name is acquired or %NULL
588
 * @name_lost_handler: (nullable): handler to invoke when @name is lost or %NULL
589
 * @user_data: user data to pass to handlers
590
 * @user_data_free_func: (nullable): function for freeing @user_data or %NULL
591
 *
592
 * Starts acquiring @name on the bus specified by @bus_type and calls
593
 * @name_acquired_handler and @name_lost_handler when the name is
594
 * acquired respectively lost. Callbacks will be invoked in the 
595
 * [thread-default main context][g-main-context-push-thread-default]
596
 * of the thread you are calling this function from.
597
 *
598
 * You are guaranteed that one of the @name_acquired_handler and @name_lost_handler
599
 * callbacks will be invoked after calling this function - there are three
600
 * possible cases:
601
 * 
602
 * - @name_lost_handler with a %NULL connection (if a connection to the bus
603
 *   can't be made).
604
 *
605
 * - @bus_acquired_handler then @name_lost_handler (if the name can't be
606
 *   obtained)
607
 *
608
 * - @bus_acquired_handler then @name_acquired_handler (if the name was
609
 *   obtained).
610
 *
611
 * When you are done owning the name, just call g_bus_unown_name()
612
 * with the owner id this function returns.
613
 *
614
 * If the name is acquired or lost (for example another application
615
 * could acquire the name if you allow replacement or the application
616
 * currently owning the name exits), the handlers are also invoked.
617
 * If the #GDBusConnection that is used for attempting to own the name
618
 * closes, then @name_lost_handler is invoked since it is no longer
619
 * possible for other processes to access the process.
620
 *
621
 * You cannot use g_bus_own_name() several times for the same name (unless
622
 * interleaved with calls to g_bus_unown_name()) - only the first call
623
 * will work.
624
 *
625
 * Another guarantee is that invocations of @name_acquired_handler
626
 * and @name_lost_handler are guaranteed to alternate; that
627
 * is, if @name_acquired_handler is invoked then you are
628
 * guaranteed that the next time one of the handlers is invoked, it
629
 * will be @name_lost_handler. The reverse is also true.
630
 *
631
 * If you plan on exporting objects (using e.g.
632
 * g_dbus_connection_register_object()), note that it is generally too late
633
 * to export the objects in @name_acquired_handler. Instead, you can do this
634
 * in @bus_acquired_handler since you are guaranteed that this will run
635
 * before @name is requested from the bus.
636
 *
637
 * This behavior makes it very simple to write applications that wants
638
 * to [own names][gdbus-owning-names] and export objects.
639
 * Simply register objects to be exported in @bus_acquired_handler and
640
 * unregister the objects (if any) in @name_lost_handler.
641
 *
642
 * Returns: an identifier (never 0) that can be used with
643
 *     g_bus_unown_name() to stop owning the name.
644
 *
645
 * Since: 2.26
646
 */
647
guint
648
g_bus_own_name (GBusType                  bus_type,
649
                const gchar              *name,
650
                GBusNameOwnerFlags        flags,
651
                GBusAcquiredCallback      bus_acquired_handler,
652
                GBusNameAcquiredCallback  name_acquired_handler,
653
                GBusNameLostCallback      name_lost_handler,
654
                gpointer                  user_data,
655
                GDestroyNotify            user_data_free_func)
656
0
{
657
0
  Client *client;
658
659
0
  g_return_val_if_fail (g_dbus_is_name (name) && !g_dbus_is_unique_name (name), 0);
660
661
0
  G_LOCK (lock);
662
663
0
  client = g_new0 (Client, 1);
664
0
  client->ref_count = 1;
665
0
  client->id = next_global_id++; /* TODO: uh oh, handle overflow */
666
0
  client->name = g_strdup (name);
667
0
  client->flags = flags;
668
0
  client->bus_acquired_handler = bus_acquired_handler;
669
0
  client->name_acquired_handler = name_acquired_handler;
670
0
  client->name_lost_handler = name_lost_handler;
671
0
  client->user_data = user_data;
672
0
  client->user_data_free_func = user_data_free_func;
673
0
  client->main_context = g_main_context_ref_thread_default ();
674
675
0
  if (map_id_to_client == NULL)
676
0
    {
677
0
      map_id_to_client = g_hash_table_new (g_direct_hash, g_direct_equal);
678
0
    }
679
0
  g_hash_table_insert (map_id_to_client,
680
0
                       GUINT_TO_POINTER (client->id),
681
0
                       client);
682
683
0
  g_bus_get (bus_type,
684
0
             NULL,
685
0
             connection_get_cb,
686
0
             client_ref (client));
687
688
0
  G_UNLOCK (lock);
689
690
0
  return client->id;
691
0
}
692
693
typedef struct {
694
  GClosure *bus_acquired_closure;
695
  GClosure *name_acquired_closure;
696
  GClosure *name_lost_closure;
697
} OwnNameData;
698
699
static OwnNameData *
700
own_name_data_new (GClosure *bus_acquired_closure,
701
                   GClosure *name_acquired_closure,
702
                   GClosure *name_lost_closure)
703
0
{
704
0
  OwnNameData *data;
705
706
0
  data = g_new0 (OwnNameData, 1);
707
708
0
  if (bus_acquired_closure != NULL)
709
0
    {
710
0
      data->bus_acquired_closure = g_closure_ref (bus_acquired_closure);
711
0
      g_closure_sink (bus_acquired_closure);
712
0
      if (G_CLOSURE_NEEDS_MARSHAL (bus_acquired_closure))
713
0
        g_closure_set_marshal (bus_acquired_closure, g_cclosure_marshal_generic);
714
0
    }
715
716
0
  if (name_acquired_closure != NULL)
717
0
    {
718
0
      data->name_acquired_closure = g_closure_ref (name_acquired_closure);
719
0
      g_closure_sink (name_acquired_closure);
720
0
      if (G_CLOSURE_NEEDS_MARSHAL (name_acquired_closure))
721
0
        g_closure_set_marshal (name_acquired_closure, g_cclosure_marshal_generic);
722
0
    }
723
724
0
  if (name_lost_closure != NULL)
725
0
    {
726
0
      data->name_lost_closure = g_closure_ref (name_lost_closure);
727
0
      g_closure_sink (name_lost_closure);
728
0
      if (G_CLOSURE_NEEDS_MARSHAL (name_lost_closure))
729
0
        g_closure_set_marshal (name_lost_closure, g_cclosure_marshal_generic);
730
0
    }
731
732
0
  return data;
733
0
}
734
735
static void
736
own_with_closures_on_bus_acquired (GDBusConnection *connection,
737
                                   const gchar     *name,
738
                                   gpointer         user_data)
739
0
{
740
0
  OwnNameData *data = user_data;
741
0
  GValue params[2] = { G_VALUE_INIT, G_VALUE_INIT };
742
743
0
  g_value_init (&params[0], G_TYPE_DBUS_CONNECTION);
744
0
  g_value_set_object (&params[0], connection);
745
746
0
  g_value_init (&params[1], G_TYPE_STRING);
747
0
  g_value_set_string (&params[1], name);
748
749
0
  g_closure_invoke (data->bus_acquired_closure, NULL, 2, params, NULL);
750
751
0
  g_value_unset (params + 0);
752
0
  g_value_unset (params + 1);
753
0
}
754
755
static void
756
own_with_closures_on_name_acquired (GDBusConnection *connection,
757
                                    const gchar     *name,
758
                                    gpointer         user_data)
759
0
{
760
0
  OwnNameData *data = user_data;
761
0
  GValue params[2] = { 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_closure_invoke (data->name_acquired_closure, NULL, 2, params, NULL);
770
771
0
  g_value_unset (params + 0);
772
0
  g_value_unset (params + 1);
773
0
}
774
775
static void
776
own_with_closures_on_name_lost (GDBusConnection *connection,
777
                                const gchar     *name,
778
                                gpointer         user_data)
779
0
{
780
0
  OwnNameData *data = user_data;
781
0
  GValue params[2] = { G_VALUE_INIT, G_VALUE_INIT };
782
783
0
  g_value_init (&params[0], G_TYPE_DBUS_CONNECTION);
784
0
  g_value_set_object (&params[0], connection);
785
786
0
  g_value_init (&params[1], G_TYPE_STRING);
787
0
  g_value_set_string (&params[1], name);
788
789
0
  g_closure_invoke (data->name_lost_closure, NULL, 2, params, NULL);
790
791
0
  g_value_unset (params + 0);
792
0
  g_value_unset (params + 1);
793
0
}
794
795
static void
796
bus_own_name_free_func (gpointer user_data)
797
0
{
798
0
  OwnNameData *data = user_data;
799
800
0
  if (data->bus_acquired_closure != NULL)
801
0
    g_closure_unref (data->bus_acquired_closure);
802
803
0
  if (data->name_acquired_closure != NULL)
804
0
    g_closure_unref (data->name_acquired_closure);
805
806
0
  if (data->name_lost_closure != NULL)
807
0
    g_closure_unref (data->name_lost_closure);
808
809
0
  g_free (data);
810
0
}
811
812
/**
813
 * g_bus_own_name_with_closures: (rename-to g_bus_own_name)
814
 * @bus_type: the type of bus to own a name on
815
 * @name: the well-known name to own
816
 * @flags: a set of flags from the #GBusNameOwnerFlags enumeration
817
 * @bus_acquired_closure: (nullable): #GClosure to invoke when connected to
818
 *     the bus of type @bus_type or %NULL
819
 * @name_acquired_closure: (nullable): #GClosure to invoke when @name is
820
 *     acquired or %NULL
821
 * @name_lost_closure: (nullable): #GClosure to invoke when @name is lost or
822
 *     %NULL
823
 *
824
 * Version of g_bus_own_name() using closures instead of callbacks for
825
 * easier binding in other languages.
826
 *
827
 * Returns: an identifier (never 0) that can be used with
828
 *     g_bus_unown_name() to stop owning the name.
829
 *
830
 * Since: 2.26
831
 */
832
guint
833
g_bus_own_name_with_closures (GBusType            bus_type,
834
                              const gchar        *name,
835
                              GBusNameOwnerFlags  flags,
836
                              GClosure           *bus_acquired_closure,
837
                              GClosure           *name_acquired_closure,
838
                              GClosure           *name_lost_closure)
839
0
{
840
0
  return g_bus_own_name (bus_type,
841
0
          name,
842
0
          flags,
843
0
          bus_acquired_closure != NULL ? own_with_closures_on_bus_acquired : NULL,
844
0
          name_acquired_closure != NULL ? own_with_closures_on_name_acquired : NULL,
845
0
          name_lost_closure != NULL ? own_with_closures_on_name_lost : NULL,
846
0
          own_name_data_new (bus_acquired_closure,
847
0
                             name_acquired_closure,
848
0
                             name_lost_closure),
849
0
          bus_own_name_free_func);
850
0
}
851
852
/**
853
 * g_bus_own_name_on_connection_with_closures: (rename-to g_bus_own_name_on_connection)
854
 * @connection: a #GDBusConnection
855
 * @name: the well-known name to own
856
 * @flags: a set of flags from the #GBusNameOwnerFlags enumeration
857
 * @name_acquired_closure: (nullable): #GClosure to invoke when @name is
858
 *     acquired or %NULL
859
 * @name_lost_closure: (nullable): #GClosure to invoke when @name is lost
860
 *     or %NULL
861
 *
862
 * Version of g_bus_own_name_on_connection() using closures instead of
863
 * callbacks for easier binding in other languages.
864
 *
865
 * Returns: an identifier (never 0) that can be used with
866
 *     g_bus_unown_name() to stop owning the name.
867
 *
868
 * Since: 2.26
869
 */
870
guint
871
g_bus_own_name_on_connection_with_closures (GDBusConnection    *connection,
872
                                            const gchar        *name,
873
                                            GBusNameOwnerFlags  flags,
874
                                            GClosure           *name_acquired_closure,
875
                                            GClosure           *name_lost_closure)
876
0
{
877
0
  return g_bus_own_name_on_connection (connection,
878
0
          name,
879
0
          flags,
880
0
          name_acquired_closure != NULL ? own_with_closures_on_name_acquired : NULL,
881
0
          name_lost_closure != NULL ? own_with_closures_on_name_lost : NULL,
882
0
          own_name_data_new (NULL,
883
0
                             name_acquired_closure,
884
0
                             name_lost_closure),
885
0
          bus_own_name_free_func);
886
0
}
887
888
/**
889
 * g_bus_unown_name:
890
 * @owner_id: an identifier obtained from g_bus_own_name()
891
 *
892
 * Stops owning a name.
893
 *
894
 * Note that there may still be D-Bus traffic to process (relating to owning
895
 * and unowning the name) in the current thread-default #GMainContext after
896
 * this function has returned. You should continue to iterate the #GMainContext
897
 * until the #GDestroyNotify function passed to g_bus_own_name() is called, in
898
 * order to avoid memory leaks through callbacks queued on the #GMainContext
899
 * after it’s stopped being iterated.
900
 *
901
 * Since: 2.26
902
 */
903
void
904
g_bus_unown_name (guint owner_id)
905
0
{
906
0
  Client *client;
907
908
0
  g_return_if_fail (owner_id > 0);
909
910
0
  client = NULL;
911
912
0
  G_LOCK (lock);
913
0
  if (owner_id == 0 || map_id_to_client == NULL ||
914
0
      (client = g_hash_table_lookup (map_id_to_client, GUINT_TO_POINTER (owner_id))) == NULL)
915
0
    {
916
0
      g_warning ("Invalid id %d passed to g_bus_unown_name()", owner_id);
917
0
      goto out;
918
0
    }
919
920
0
  client->cancelled = TRUE;
921
0
  g_warn_if_fail (g_hash_table_remove (map_id_to_client, GUINT_TO_POINTER (owner_id)));
922
923
0
 out:
924
0
  G_UNLOCK (lock);
925
926
  /* do callback without holding lock */
927
0
  if (client != NULL)
928
0
    {
929
      /* Release the name if needed */
930
0
      if (client->needs_release &&
931
0
          client->connection != NULL &&
932
0
          !g_dbus_connection_is_closed (client->connection))
933
0
        {
934
0
          GVariant *result;
935
0
          GError *error;
936
0
          guint32 release_name_reply;
937
938
          /* TODO: it kinda sucks having to do a sync call to release the name - but if
939
           * we don't, then a subsequent grab of the name will make the bus daemon return
940
           * IN_QUEUE which will trigger name_lost().
941
           *
942
           * I believe this is a bug in the bus daemon.
943
           */
944
0
          error = NULL;
945
0
          result = g_dbus_connection_call_sync (client->connection,
946
0
                                                "org.freedesktop.DBus",  /* bus name */
947
0
                                                "/org/freedesktop/DBus", /* object path */
948
0
                                                "org.freedesktop.DBus",  /* interface name */
949
0
                                                "ReleaseName",           /* method name */
950
0
                                                g_variant_new ("(s)", client->name),
951
0
                                                G_VARIANT_TYPE ("(u)"),
952
0
                                                G_DBUS_CALL_FLAGS_NONE,
953
0
                                                -1,
954
0
                                                NULL,
955
0
                                                &error);
956
0
          if (result == NULL)
957
0
            {
958
0
              g_warning ("Error releasing name %s: %s", client->name, error->message);
959
0
              g_error_free (error);
960
0
            }
961
0
          else
962
0
            {
963
0
              g_variant_get (result, "(u)", &release_name_reply);
964
0
              if (release_name_reply != 1 /* DBUS_RELEASE_NAME_REPLY_RELEASED */)
965
0
                {
966
0
                  g_warning ("Unexpected reply %d when releasing name %s", release_name_reply, client->name);
967
0
                }
968
0
              else
969
0
                {
970
0
                  client->needs_release = FALSE;
971
0
                }
972
0
              g_variant_unref (result);
973
0
            }
974
0
        }
975
976
0
      if (client->disconnected_signal_handler_id > 0)
977
0
        g_signal_handler_disconnect (client->connection, client->disconnected_signal_handler_id);
978
0
      if (client->name_acquired_subscription_id > 0)
979
0
        g_dbus_connection_signal_unsubscribe (client->connection, client->name_acquired_subscription_id);
980
0
      if (client->name_lost_subscription_id > 0)
981
0
        g_dbus_connection_signal_unsubscribe (client->connection, client->name_lost_subscription_id);
982
0
      client->disconnected_signal_handler_id = 0;
983
0
      client->name_acquired_subscription_id = 0;
984
0
      client->name_lost_subscription_id = 0;
985
0
      if (client->connection != NULL)
986
0
        {
987
0
          g_object_unref (client->connection);
988
0
          client->connection = NULL;
989
0
        }
990
991
0
      client_unref (client);
992
0
    }
993
0
}