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