Blame gio/gdbusnameowning.c

Packit ae235b
/* GDBus - GLib D-Bus Library
Packit ae235b
 *
Packit ae235b
 * Copyright (C) 2008-2010 Red Hat, Inc.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Author: David Zeuthen <davidz@redhat.com>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include <stdlib.h>
Packit ae235b
Packit ae235b
#include "gdbusutils.h"
Packit ae235b
#include "gdbusnameowning.h"
Packit ae235b
#include "gdbuserror.h"
Packit ae235b
#include "gdbusprivate.h"
Packit ae235b
#include "gdbusconnection.h"
Packit ae235b
Packit ae235b
#include "glibintl.h"
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:gdbusnameowning
Packit ae235b
 * @title: Owning Bus Names
Packit ae235b
 * @short_description: Simple API for owning bus names
Packit ae235b
 * @include: gio/gio.h
Packit ae235b
 *
Packit ae235b
 * Convenience API for owning bus names.
Packit ae235b
 *
Packit ae235b
 * A simple example for owning a name can be found in
Packit ae235b
 * [gdbus-example-own-name.c](https://git.gnome.org/browse/glib/tree/gio/tests/gdbus-example-own-name.c) 
Packit ae235b
 */
Packit ae235b
Packit ae235b
G_LOCK_DEFINE_STATIC (lock);
Packit ae235b
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
Packit ae235b
typedef enum
Packit ae235b
{
Packit ae235b
  PREVIOUS_CALL_NONE = 0,
Packit ae235b
  PREVIOUS_CALL_ACQUIRED,
Packit ae235b
  PREVIOUS_CALL_LOST,
Packit ae235b
} PreviousCall;
Packit ae235b
Packit ae235b
typedef struct
Packit ae235b
{
Packit ae235b
  volatile gint             ref_count;
Packit ae235b
  guint                     id;
Packit ae235b
  GBusNameOwnerFlags        flags;
Packit ae235b
  gchar                    *name;
Packit ae235b
  GBusAcquiredCallback      bus_acquired_handler;
Packit ae235b
  GBusNameAcquiredCallback  name_acquired_handler;
Packit ae235b
  GBusNameLostCallback      name_lost_handler;
Packit ae235b
  gpointer                  user_data;
Packit ae235b
  GDestroyNotify            user_data_free_func;
Packit ae235b
  GMainContext             *main_context;
Packit ae235b
Packit ae235b
  PreviousCall              previous_call;
Packit ae235b
Packit ae235b
  GDBusConnection          *connection;
Packit ae235b
  gulong                    disconnected_signal_handler_id;
Packit ae235b
  guint                     name_acquired_subscription_id;
Packit ae235b
  guint                     name_lost_subscription_id;
Packit ae235b
Packit ae235b
  volatile gboolean         cancelled; /* must hold lock when reading or modifying */
Packit ae235b
Packit ae235b
  gboolean                  needs_release;
Packit ae235b
} Client;
Packit ae235b
Packit ae235b
static guint next_global_id = 1;
Packit ae235b
static GHashTable *map_id_to_client = NULL;
Packit ae235b
Packit ae235b
Packit ae235b
static Client *
Packit ae235b
client_ref (Client *client)
Packit ae235b
{
Packit ae235b
  g_atomic_int_inc (&client->ref_count);
Packit ae235b
  return client;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
client_unref (Client *client)
Packit ae235b
{
Packit ae235b
  if (g_atomic_int_dec_and_test (&client->ref_count))
Packit ae235b
    {
Packit ae235b
      if (client->connection != NULL)
Packit ae235b
        {
Packit ae235b
          if (client->disconnected_signal_handler_id > 0)
Packit ae235b
            g_signal_handler_disconnect (client->connection, client->disconnected_signal_handler_id);
Packit ae235b
          if (client->name_acquired_subscription_id > 0)
Packit ae235b
            g_dbus_connection_signal_unsubscribe (client->connection, client->name_acquired_subscription_id);
Packit ae235b
          if (client->name_lost_subscription_id > 0)
Packit ae235b
            g_dbus_connection_signal_unsubscribe (client->connection, client->name_lost_subscription_id);
Packit ae235b
          g_object_unref (client->connection);
Packit ae235b
        }
Packit ae235b
      g_main_context_unref (client->main_context);
Packit ae235b
      g_free (client->name);
Packit ae235b
      if (client->user_data_free_func != NULL)
Packit ae235b
        client->user_data_free_func (client->user_data);
Packit ae235b
      g_free (client);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
Packit ae235b
Packit ae235b
typedef enum
Packit ae235b
{
Packit ae235b
  CALL_TYPE_NAME_ACQUIRED,
Packit ae235b
  CALL_TYPE_NAME_LOST
Packit ae235b
} CallType;
Packit ae235b
Packit ae235b
typedef struct
Packit ae235b
{
Packit ae235b
  Client *client;
Packit ae235b
Packit ae235b
  /* keep this separate because client->connection may
Packit ae235b
   * be set to NULL after scheduling the call
Packit ae235b
   */
Packit ae235b
  GDBusConnection *connection;
Packit ae235b
Packit ae235b
  /* set to TRUE to call acquired */
Packit ae235b
  CallType call_type;
Packit ae235b
} CallHandlerData;
Packit ae235b
Packit ae235b
static void
Packit ae235b
call_handler_data_free (CallHandlerData *data)
Packit ae235b
{
Packit ae235b
  if (data->connection != NULL)
Packit ae235b
    g_object_unref (data->connection);
Packit ae235b
  client_unref (data->client);
Packit ae235b
  g_free (data);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
actually_do_call (Client *client, GDBusConnection *connection, CallType call_type)
Packit ae235b
{
Packit ae235b
  switch (call_type)
Packit ae235b
    {
Packit ae235b
    case CALL_TYPE_NAME_ACQUIRED:
Packit ae235b
      if (client->name_acquired_handler != NULL)
Packit ae235b
        {
Packit ae235b
          client->name_acquired_handler (connection,
Packit ae235b
                                         client->name,
Packit ae235b
                                         client->user_data);
Packit ae235b
        }
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case CALL_TYPE_NAME_LOST:
Packit ae235b
      if (client->name_lost_handler != NULL)
Packit ae235b
        {
Packit ae235b
          client->name_lost_handler (connection,
Packit ae235b
                                     client->name,
Packit ae235b
                                     client->user_data);
Packit ae235b
        }
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    default:
Packit ae235b
      g_assert_not_reached ();
Packit ae235b
      break;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
call_in_idle_cb (gpointer _data)
Packit ae235b
{
Packit ae235b
  CallHandlerData *data = _data;
Packit ae235b
  actually_do_call (data->client, data->connection, data->call_type);
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
schedule_call_in_idle (Client *client, CallType  call_type)
Packit ae235b
{
Packit ae235b
  CallHandlerData *data;
Packit ae235b
  GSource *idle_source;
Packit ae235b
Packit ae235b
  data = g_new0 (CallHandlerData, 1);
Packit ae235b
  data->client = client_ref (client);
Packit ae235b
  data->connection = client->connection != NULL ? g_object_ref (client->connection) : NULL;
Packit ae235b
  data->call_type = call_type;
Packit ae235b
Packit ae235b
  idle_source = g_idle_source_new ();
Packit ae235b
  g_source_set_priority (idle_source, G_PRIORITY_HIGH);
Packit ae235b
  g_source_set_callback (idle_source,
Packit ae235b
                         call_in_idle_cb,
Packit ae235b
                         data,
Packit ae235b
                         (GDestroyNotify) call_handler_data_free);
Packit ae235b
  g_source_set_name (idle_source, "[gio, gdbusnameowning.c] call_in_idle_cb");
Packit ae235b
  g_source_attach (idle_source, client->main_context);
Packit ae235b
  g_source_unref (idle_source);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
do_call (Client *client, CallType call_type)
Packit ae235b
{
Packit ae235b
  GMainContext *current_context;
Packit ae235b
Packit ae235b
  /* only schedule in idle if we're not in the right thread */
Packit ae235b
  current_context = g_main_context_ref_thread_default ();
Packit ae235b
  if (current_context != client->main_context)
Packit ae235b
    schedule_call_in_idle (client, call_type);
Packit ae235b
  else
Packit ae235b
    actually_do_call (client, client->connection, call_type);
Packit ae235b
  g_main_context_unref (current_context);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
call_acquired_handler (Client *client)
Packit ae235b
{
Packit ae235b
  G_LOCK (lock);
Packit ae235b
  if (client->previous_call != PREVIOUS_CALL_ACQUIRED)
Packit ae235b
    {
Packit ae235b
      client->previous_call = PREVIOUS_CALL_ACQUIRED;
Packit ae235b
      if (!client->cancelled)
Packit ae235b
        {
Packit ae235b
          G_UNLOCK (lock);
Packit ae235b
          do_call (client, CALL_TYPE_NAME_ACQUIRED);
Packit ae235b
          goto out;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
  G_UNLOCK (lock);
Packit ae235b
 out:
Packit ae235b
  ;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
call_lost_handler (Client  *client)
Packit ae235b
{
Packit ae235b
  G_LOCK (lock);
Packit ae235b
  if (client->previous_call != PREVIOUS_CALL_LOST)
Packit ae235b
    {
Packit ae235b
      client->previous_call = PREVIOUS_CALL_LOST;
Packit ae235b
      if (!client->cancelled)
Packit ae235b
        {
Packit ae235b
          G_UNLOCK (lock);
Packit ae235b
          do_call (client, CALL_TYPE_NAME_LOST);
Packit ae235b
          goto out;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
  G_UNLOCK (lock);
Packit ae235b
 out:
Packit ae235b
  ;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
Packit ae235b
static void
Packit ae235b
on_name_lost_or_acquired (GDBusConnection  *connection,
Packit ae235b
                          const gchar      *sender_name,
Packit ae235b
                          const gchar      *object_path,
Packit ae235b
                          const gchar      *interface_name,
Packit ae235b
                          const gchar      *signal_name,
Packit ae235b
                          GVariant         *parameters,
Packit ae235b
                          gpointer          user_data)
Packit ae235b
{
Packit ae235b
  Client *client = user_data;
Packit ae235b
  const gchar *name;
Packit ae235b
Packit ae235b
  if (g_strcmp0 (object_path, "/org/freedesktop/DBus") != 0 ||
Packit ae235b
      g_strcmp0 (interface_name, "org.freedesktop.DBus") != 0 ||
Packit ae235b
      g_strcmp0 (sender_name, "org.freedesktop.DBus") != 0)
Packit ae235b
    goto out;
Packit ae235b
Packit ae235b
  if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(s)")))
Packit ae235b
    {
Packit ae235b
      g_warning ("%s signal had unexpected signature %s", signal_name,
Packit ae235b
                 g_variant_get_type_string (parameters));
Packit ae235b
      goto out;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (g_strcmp0 (signal_name, "NameLost") == 0)
Packit ae235b
    {
Packit ae235b
      g_variant_get (parameters, "(&s)", &name);
Packit ae235b
      if (g_strcmp0 (name, client->name) == 0)
Packit ae235b
        {
Packit ae235b
          call_lost_handler (client);
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
  else if (g_strcmp0 (signal_name, "NameAcquired") == 0)
Packit ae235b
    {
Packit ae235b
      g_variant_get (parameters, "(&s)", &name);
Packit ae235b
      if (g_strcmp0 (name, client->name) == 0)
Packit ae235b
        {
Packit ae235b
          call_acquired_handler (client);
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
 out:
Packit ae235b
  ;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
Packit ae235b
static void
Packit ae235b
request_name_cb (GObject      *source_object,
Packit ae235b
                 GAsyncResult *res,
Packit ae235b
                 gpointer      user_data)
Packit ae235b
{
Packit ae235b
  Client *client = user_data;
Packit ae235b
  GVariant *result;
Packit ae235b
  guint32 request_name_reply;
Packit ae235b
  gboolean subscribe;
Packit ae235b
Packit ae235b
  request_name_reply = 0;
Packit ae235b
  result = NULL;
Packit ae235b
Packit ae235b
  /* don't use client->connection - it may be NULL already */
Packit ae235b
  result = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object),
Packit ae235b
                                          res,
Packit ae235b
                                          NULL);
Packit ae235b
  if (result != NULL)
Packit ae235b
    {
Packit ae235b
      g_variant_get (result, "(u)", &request_name_reply);
Packit ae235b
      g_variant_unref (result);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  subscribe = FALSE;
Packit ae235b
Packit ae235b
  switch (request_name_reply)
Packit ae235b
    {
Packit ae235b
    case 1: /* DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER */
Packit ae235b
      /* We got the name - now listen for NameLost and NameAcquired */
Packit ae235b
      call_acquired_handler (client);
Packit ae235b
      subscribe = TRUE;
Packit ae235b
      client->needs_release = TRUE;
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case 2: /* DBUS_REQUEST_NAME_REPLY_IN_QUEUE */
Packit ae235b
      /* Waiting in line - listen for NameLost and NameAcquired */
Packit ae235b
      call_lost_handler (client);
Packit ae235b
      subscribe = TRUE;
Packit ae235b
      client->needs_release = TRUE;
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    default:
Packit ae235b
      /* assume we couldn't get the name - explicit fallthrough */
Packit ae235b
    case 3: /* DBUS_REQUEST_NAME_REPLY_EXISTS */
Packit ae235b
    case 4: /* DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER */
Packit ae235b
      /* Some other part of the process is already owning the name */
Packit ae235b
      call_lost_handler (client);
Packit ae235b
      break;
Packit ae235b
    }
Packit ae235b
Packit ae235b
Packit ae235b
  if (subscribe)
Packit ae235b
    {
Packit ae235b
      GDBusConnection *connection = NULL;
Packit ae235b
Packit ae235b
      /* if cancelled, there is no point in subscribing to signals - if not, make sure
Packit ae235b
       * we use a known good Connection object since it may be set to NULL at any point
Packit ae235b
       * after being cancelled
Packit ae235b
       */
Packit ae235b
      G_LOCK (lock);
Packit ae235b
      if (!client->cancelled)
Packit ae235b
        connection = g_object_ref (client->connection);
Packit ae235b
      G_UNLOCK (lock);
Packit ae235b
Packit ae235b
      /* start listening to NameLost and NameAcquired messages */
Packit ae235b
      if (connection != NULL)
Packit ae235b
        {
Packit ae235b
          client->name_lost_subscription_id =
Packit ae235b
            g_dbus_connection_signal_subscribe (connection,
Packit ae235b
                                                "org.freedesktop.DBus",
Packit ae235b
                                                "org.freedesktop.DBus",
Packit ae235b
                                                "NameLost",
Packit ae235b
                                                "/org/freedesktop/DBus",
Packit ae235b
                                                client->name,
Packit ae235b
                                                G_DBUS_SIGNAL_FLAGS_NONE,
Packit ae235b
                                                on_name_lost_or_acquired,
Packit ae235b
                                                client,
Packit ae235b
                                                NULL);
Packit ae235b
          client->name_acquired_subscription_id =
Packit ae235b
            g_dbus_connection_signal_subscribe (connection,
Packit ae235b
                                                "org.freedesktop.DBus",
Packit ae235b
                                                "org.freedesktop.DBus",
Packit ae235b
                                                "NameAcquired",
Packit ae235b
                                                "/org/freedesktop/DBus",
Packit ae235b
                                                client->name,
Packit ae235b
                                                G_DBUS_SIGNAL_FLAGS_NONE,
Packit ae235b
                                                on_name_lost_or_acquired,
Packit ae235b
                                                client,
Packit ae235b
                                                NULL);
Packit ae235b
          g_object_unref (connection);
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  client_unref (client);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
Packit ae235b
static void
Packit ae235b
on_connection_disconnected (GDBusConnection *connection,
Packit ae235b
                            gboolean         remote_peer_vanished,
Packit ae235b
                            GError          *error,
Packit ae235b
                            gpointer         user_data)
Packit ae235b
{
Packit ae235b
  Client *client = user_data;
Packit ae235b
Packit ae235b
  if (client->disconnected_signal_handler_id > 0)
Packit ae235b
    g_signal_handler_disconnect (client->connection, client->disconnected_signal_handler_id);
Packit ae235b
  if (client->name_acquired_subscription_id > 0)
Packit ae235b
    g_dbus_connection_signal_unsubscribe (client->connection, client->name_acquired_subscription_id);
Packit ae235b
  if (client->name_lost_subscription_id > 0)
Packit ae235b
    g_dbus_connection_signal_unsubscribe (client->connection, client->name_lost_subscription_id);
Packit ae235b
  g_object_unref (client->connection);
Packit ae235b
  client->disconnected_signal_handler_id = 0;
Packit ae235b
  client->name_acquired_subscription_id = 0;
Packit ae235b
  client->name_lost_subscription_id = 0;
Packit ae235b
  client->connection = NULL;
Packit ae235b
Packit ae235b
  call_lost_handler (client);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
Packit ae235b
static void
Packit ae235b
has_connection (Client *client)
Packit ae235b
{
Packit ae235b
  /* listen for disconnection */
Packit ae235b
  client->disconnected_signal_handler_id = g_signal_connect (client->connection,
Packit ae235b
                                                             "closed",
Packit ae235b
                                                             G_CALLBACK (on_connection_disconnected),
Packit ae235b
                                                             client);
Packit ae235b
Packit ae235b
  /* attempt to acquire the name */
Packit ae235b
  g_dbus_connection_call (client->connection,
Packit ae235b
                          "org.freedesktop.DBus",  /* bus name */
Packit ae235b
                          "/org/freedesktop/DBus", /* object path */
Packit ae235b
                          "org.freedesktop.DBus",  /* interface name */
Packit ae235b
                          "RequestName",           /* method name */
Packit ae235b
                          g_variant_new ("(su)",
Packit ae235b
                                         client->name,
Packit ae235b
                                         client->flags),
Packit ae235b
                          G_VARIANT_TYPE ("(u)"),
Packit ae235b
                          G_DBUS_CALL_FLAGS_NONE,
Packit ae235b
                          -1,
Packit ae235b
                          NULL,
Packit ae235b
                          (GAsyncReadyCallback) request_name_cb,
Packit ae235b
                          client_ref (client));
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
static void
Packit ae235b
connection_get_cb (GObject      *source_object,
Packit ae235b
                   GAsyncResult *res,
Packit ae235b
                   gpointer      user_data)
Packit ae235b
{
Packit ae235b
  Client *client = user_data;
Packit ae235b
Packit ae235b
  /* must not do anything if already cancelled */
Packit ae235b
  G_LOCK (lock);
Packit ae235b
  if (client->cancelled)
Packit ae235b
    {
Packit ae235b
      G_UNLOCK (lock);
Packit ae235b
      goto out;
Packit ae235b
    }
Packit ae235b
  G_UNLOCK (lock);
Packit ae235b
Packit ae235b
  client->connection = g_bus_get_finish (res, NULL);
Packit ae235b
  if (client->connection == NULL)
Packit ae235b
    {
Packit ae235b
      call_lost_handler (client);
Packit ae235b
      goto out;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* No need to schedule this in idle as we're already in the thread
Packit ae235b
   * that the user called g_bus_own_name() from. This is because
Packit ae235b
   * g_bus_get() guarantees that.
Packit ae235b
   *
Packit ae235b
   * Also, we need to ensure that the handler is invoked *before*
Packit ae235b
   * we call RequestName(). Otherwise there is a race.
Packit ae235b
   */
Packit ae235b
  if (client->bus_acquired_handler != NULL)
Packit ae235b
    {
Packit ae235b
      client->bus_acquired_handler (client->connection,
Packit ae235b
                                    client->name,
Packit ae235b
                                    client->user_data);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  has_connection (client);
Packit ae235b
Packit ae235b
 out:
Packit ae235b
  client_unref (client);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_bus_own_name_on_connection:
Packit ae235b
 * @connection: a #GDBusConnection
Packit ae235b
 * @name: the well-known name to own
Packit ae235b
 * @flags: a set of flags from the #GBusNameOwnerFlags enumeration
Packit ae235b
 * @name_acquired_handler: (nullable): handler to invoke when @name is acquired or %NULL
Packit ae235b
 * @name_lost_handler: (nullable): handler to invoke when @name is lost or %NULL
Packit ae235b
 * @user_data: user data to pass to handlers
Packit ae235b
 * @user_data_free_func: (nullable): function for freeing @user_data or %NULL
Packit ae235b
 *
Packit ae235b
 * Like g_bus_own_name() but takes a #GDBusConnection instead of a
Packit ae235b
 * #GBusType.
Packit ae235b
 *
Packit ae235b
 * Returns: an identifier (never 0) that an be used with
Packit ae235b
 *     g_bus_unown_name() to stop owning the name
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
guint
Packit ae235b
g_bus_own_name_on_connection (GDBusConnection          *connection,
Packit ae235b
                              const gchar              *name,
Packit ae235b
                              GBusNameOwnerFlags        flags,
Packit ae235b
                              GBusNameAcquiredCallback  name_acquired_handler,
Packit ae235b
                              GBusNameLostCallback      name_lost_handler,
Packit ae235b
                              gpointer                  user_data,
Packit ae235b
                              GDestroyNotify            user_data_free_func)
Packit ae235b
{
Packit ae235b
  Client *client;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), 0);
Packit ae235b
  g_return_val_if_fail (g_dbus_is_name (name) && !g_dbus_is_unique_name (name), 0);
Packit ae235b
Packit ae235b
  G_LOCK (lock);
Packit ae235b
Packit ae235b
  client = g_new0 (Client, 1);
Packit ae235b
  client->ref_count = 1;
Packit ae235b
  client->id = next_global_id++; /* TODO: uh oh, handle overflow */
Packit ae235b
  client->name = g_strdup (name);
Packit ae235b
  client->flags = flags;
Packit ae235b
  client->name_acquired_handler = name_acquired_handler;
Packit ae235b
  client->name_lost_handler = name_lost_handler;
Packit ae235b
  client->user_data = user_data;
Packit ae235b
  client->user_data_free_func = user_data_free_func;
Packit ae235b
  client->main_context = g_main_context_ref_thread_default ();
Packit ae235b
Packit ae235b
  client->connection = g_object_ref (connection);
Packit ae235b
Packit ae235b
  if (map_id_to_client == NULL)
Packit ae235b
    {
Packit ae235b
      map_id_to_client = g_hash_table_new (g_direct_hash, g_direct_equal);
Packit ae235b
    }
Packit ae235b
  g_hash_table_insert (map_id_to_client,
Packit ae235b
                       GUINT_TO_POINTER (client->id),
Packit ae235b
                       client);
Packit ae235b
Packit ae235b
  G_UNLOCK (lock);
Packit ae235b
Packit ae235b
  has_connection (client);
Packit ae235b
Packit ae235b
  return client->id;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_bus_own_name:
Packit ae235b
 * @bus_type: the type of bus to own a name on
Packit ae235b
 * @name: the well-known name to own
Packit ae235b
 * @flags: a set of flags from the #GBusNameOwnerFlags enumeration
Packit ae235b
 * @bus_acquired_handler: (nullable): handler to invoke when connected to the bus of type @bus_type or %NULL
Packit ae235b
 * @name_acquired_handler: (nullable): handler to invoke when @name is acquired or %NULL
Packit ae235b
 * @name_lost_handler: (nullable): handler to invoke when @name is lost or %NULL
Packit ae235b
 * @user_data: user data to pass to handlers
Packit ae235b
 * @user_data_free_func: (nullable): function for freeing @user_data or %NULL
Packit ae235b
 *
Packit ae235b
 * Starts acquiring @name on the bus specified by @bus_type and calls
Packit ae235b
 * @name_acquired_handler and @name_lost_handler when the name is
Packit ae235b
 * acquired respectively lost. Callbacks will be invoked in the 
Packit ae235b
 * [thread-default main context][g-main-context-push-thread-default]
Packit ae235b
 * of the thread you are calling this function from.
Packit ae235b
 *
Packit ae235b
 * You are guaranteed that one of the @name_acquired_handler and @name_lost_handler
Packit ae235b
 * callbacks will be invoked after calling this function - there are three
Packit ae235b
 * possible cases:
Packit ae235b
 * 
Packit ae235b
 * - @name_lost_handler with a %NULL connection (if a connection to the bus
Packit ae235b
 *   can't be made).
Packit ae235b
 *
Packit ae235b
 * - @bus_acquired_handler then @name_lost_handler (if the name can't be
Packit ae235b
 *   obtained)
Packit ae235b
 *
Packit ae235b
 * - @bus_acquired_handler then @name_acquired_handler (if the name was
Packit ae235b
 *   obtained).
Packit ae235b
 *
Packit ae235b
 * When you are done owning the name, just call g_bus_unown_name()
Packit ae235b
 * with the owner id this function returns.
Packit ae235b
 *
Packit ae235b
 * If the name is acquired or lost (for example another application
Packit ae235b
 * could acquire the name if you allow replacement or the application
Packit ae235b
 * currently owning the name exits), the handlers are also invoked.
Packit ae235b
 * If the #GDBusConnection that is used for attempting to own the name
Packit ae235b
 * closes, then @name_lost_handler is invoked since it is no longer
Packit ae235b
 * possible for other processes to access the process.
Packit ae235b
 *
Packit ae235b
 * You cannot use g_bus_own_name() several times for the same name (unless
Packit ae235b
 * interleaved with calls to g_bus_unown_name()) - only the first call
Packit ae235b
 * will work.
Packit ae235b
 *
Packit ae235b
 * Another guarantee is that invocations of @name_acquired_handler
Packit ae235b
 * and @name_lost_handler are guaranteed to alternate; that
Packit ae235b
 * is, if @name_acquired_handler is invoked then you are
Packit ae235b
 * guaranteed that the next time one of the handlers is invoked, it
Packit ae235b
 * will be @name_lost_handler. The reverse is also true.
Packit ae235b
 *
Packit ae235b
 * If you plan on exporting objects (using e.g.
Packit ae235b
 * g_dbus_connection_register_object()), note that it is generally too late
Packit ae235b
 * to export the objects in @name_acquired_handler. Instead, you can do this
Packit ae235b
 * in @bus_acquired_handler since you are guaranteed that this will run
Packit ae235b
 * before @name is requested from the bus.
Packit ae235b
 *
Packit ae235b
 * This behavior makes it very simple to write applications that wants
Packit ae235b
 * to [own names][gdbus-owning-names] and export objects.
Packit ae235b
 * Simply register objects to be exported in @bus_acquired_handler and
Packit ae235b
 * unregister the objects (if any) in @name_lost_handler.
Packit ae235b
 *
Packit ae235b
 * Returns: an identifier (never 0) that an be used with
Packit ae235b
 *     g_bus_unown_name() to stop owning the name.
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
guint
Packit ae235b
g_bus_own_name (GBusType                  bus_type,
Packit ae235b
                const gchar              *name,
Packit ae235b
                GBusNameOwnerFlags        flags,
Packit ae235b
                GBusAcquiredCallback      bus_acquired_handler,
Packit ae235b
                GBusNameAcquiredCallback  name_acquired_handler,
Packit ae235b
                GBusNameLostCallback      name_lost_handler,
Packit ae235b
                gpointer                  user_data,
Packit ae235b
                GDestroyNotify            user_data_free_func)
Packit ae235b
{
Packit ae235b
  Client *client;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (g_dbus_is_name (name) && !g_dbus_is_unique_name (name), 0);
Packit ae235b
Packit ae235b
  G_LOCK (lock);
Packit ae235b
Packit ae235b
  client = g_new0 (Client, 1);
Packit ae235b
  client->ref_count = 1;
Packit ae235b
  client->id = next_global_id++; /* TODO: uh oh, handle overflow */
Packit ae235b
  client->name = g_strdup (name);
Packit ae235b
  client->flags = flags;
Packit ae235b
  client->bus_acquired_handler = bus_acquired_handler;
Packit ae235b
  client->name_acquired_handler = name_acquired_handler;
Packit ae235b
  client->name_lost_handler = name_lost_handler;
Packit ae235b
  client->user_data = user_data;
Packit ae235b
  client->user_data_free_func = user_data_free_func;
Packit ae235b
  client->main_context = g_main_context_ref_thread_default ();
Packit ae235b
Packit ae235b
  if (map_id_to_client == NULL)
Packit ae235b
    {
Packit ae235b
      map_id_to_client = g_hash_table_new (g_direct_hash, g_direct_equal);
Packit ae235b
    }
Packit ae235b
  g_hash_table_insert (map_id_to_client,
Packit ae235b
                       GUINT_TO_POINTER (client->id),
Packit ae235b
                       client);
Packit ae235b
Packit ae235b
  g_bus_get (bus_type,
Packit ae235b
             NULL,
Packit ae235b
             connection_get_cb,
Packit ae235b
             client_ref (client));
Packit ae235b
Packit ae235b
  G_UNLOCK (lock);
Packit ae235b
Packit ae235b
  return client->id;
Packit ae235b
}
Packit ae235b
Packit ae235b
typedef struct {
Packit ae235b
  GClosure *bus_acquired_closure;
Packit ae235b
  GClosure *name_acquired_closure;
Packit ae235b
  GClosure *name_lost_closure;
Packit ae235b
} OwnNameData;
Packit ae235b
Packit ae235b
static OwnNameData *
Packit ae235b
own_name_data_new (GClosure *bus_acquired_closure,
Packit ae235b
                   GClosure *name_acquired_closure,
Packit ae235b
                   GClosure *name_lost_closure)
Packit ae235b
{
Packit ae235b
  OwnNameData *data;
Packit ae235b
Packit ae235b
  data = g_new0 (OwnNameData, 1);
Packit ae235b
Packit ae235b
  if (bus_acquired_closure != NULL)
Packit ae235b
    {
Packit ae235b
      data->bus_acquired_closure = g_closure_ref (bus_acquired_closure);
Packit ae235b
      g_closure_sink (bus_acquired_closure);
Packit ae235b
      if (G_CLOSURE_NEEDS_MARSHAL (bus_acquired_closure))
Packit ae235b
        g_closure_set_marshal (bus_acquired_closure, g_cclosure_marshal_generic);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (name_acquired_closure != NULL)
Packit ae235b
    {
Packit ae235b
      data->name_acquired_closure = g_closure_ref (name_acquired_closure);
Packit ae235b
      g_closure_sink (name_acquired_closure);
Packit ae235b
      if (G_CLOSURE_NEEDS_MARSHAL (name_acquired_closure))
Packit ae235b
        g_closure_set_marshal (name_acquired_closure, g_cclosure_marshal_generic);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (name_lost_closure != NULL)
Packit ae235b
    {
Packit ae235b
      data->name_lost_closure = g_closure_ref (name_lost_closure);
Packit ae235b
      g_closure_sink (name_lost_closure);
Packit ae235b
      if (G_CLOSURE_NEEDS_MARSHAL (name_lost_closure))
Packit ae235b
        g_closure_set_marshal (name_lost_closure, g_cclosure_marshal_generic);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return data;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
own_with_closures_on_bus_acquired (GDBusConnection *connection,
Packit ae235b
                                   const gchar     *name,
Packit ae235b
                                   gpointer         user_data)
Packit ae235b
{
Packit ae235b
  OwnNameData *data = user_data;
Packit ae235b
  GValue params[2] = { G_VALUE_INIT, G_VALUE_INIT };
Packit ae235b
Packit ae235b
  g_value_init (&params[0], G_TYPE_DBUS_CONNECTION);
Packit ae235b
  g_value_set_object (&params[0], connection);
Packit ae235b
Packit ae235b
  g_value_init (&params[1], G_TYPE_STRING);
Packit ae235b
  g_value_set_string (&params[1], name);
Packit ae235b
Packit ae235b
  g_closure_invoke (data->bus_acquired_closure, NULL, 2, params, NULL);
Packit ae235b
Packit ae235b
  g_value_unset (params + 0);
Packit ae235b
  g_value_unset (params + 1);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
own_with_closures_on_name_acquired (GDBusConnection *connection,
Packit ae235b
                                    const gchar     *name,
Packit ae235b
                                    gpointer         user_data)
Packit ae235b
{
Packit ae235b
  OwnNameData *data = user_data;
Packit ae235b
  GValue params[2] = { G_VALUE_INIT, G_VALUE_INIT };
Packit ae235b
Packit ae235b
  g_value_init (&params[0], G_TYPE_DBUS_CONNECTION);
Packit ae235b
  g_value_set_object (&params[0], connection);
Packit ae235b
Packit ae235b
  g_value_init (&params[1], G_TYPE_STRING);
Packit ae235b
  g_value_set_string (&params[1], name);
Packit ae235b
Packit ae235b
  g_closure_invoke (data->name_acquired_closure, NULL, 2, params, NULL);
Packit ae235b
Packit ae235b
  g_value_unset (params + 0);
Packit ae235b
  g_value_unset (params + 1);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
own_with_closures_on_name_lost (GDBusConnection *connection,
Packit ae235b
                                const gchar     *name,
Packit ae235b
                                gpointer         user_data)
Packit ae235b
{
Packit ae235b
  OwnNameData *data = user_data;
Packit ae235b
  GValue params[2] = { G_VALUE_INIT, G_VALUE_INIT };
Packit ae235b
Packit ae235b
  g_value_init (&params[0], G_TYPE_DBUS_CONNECTION);
Packit ae235b
  g_value_set_object (&params[0], connection);
Packit ae235b
Packit ae235b
  g_value_init (&params[1], G_TYPE_STRING);
Packit ae235b
  g_value_set_string (&params[1], name);
Packit ae235b
Packit ae235b
  g_closure_invoke (data->name_lost_closure, NULL, 2, params, NULL);
Packit ae235b
Packit ae235b
  g_value_unset (params + 0);
Packit ae235b
  g_value_unset (params + 1);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
bus_own_name_free_func (gpointer user_data)
Packit ae235b
{
Packit ae235b
  OwnNameData *data = user_data;
Packit ae235b
Packit ae235b
  if (data->bus_acquired_closure != NULL)
Packit ae235b
    g_closure_unref (data->bus_acquired_closure);
Packit ae235b
Packit ae235b
  if (data->name_acquired_closure != NULL)
Packit ae235b
    g_closure_unref (data->name_acquired_closure);
Packit ae235b
Packit ae235b
  if (data->name_lost_closure != NULL)
Packit ae235b
    g_closure_unref (data->name_lost_closure);
Packit ae235b
Packit ae235b
  g_free (data);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_bus_own_name_with_closures: (rename-to g_bus_own_name)
Packit ae235b
 * @bus_type: the type of bus to own a name on
Packit ae235b
 * @name: the well-known name to own
Packit ae235b
 * @flags: a set of flags from the #GBusNameOwnerFlags enumeration
Packit ae235b
 * @bus_acquired_closure: (nullable): #GClosure to invoke when connected to
Packit ae235b
 *     the bus of type @bus_type or %NULL
Packit ae235b
 * @name_acquired_closure: (nullable): #GClosure to invoke when @name is
Packit ae235b
 *     acquired or %NULL
Packit ae235b
 * @name_lost_closure: (nullable): #GClosure to invoke when @name is lost or
Packit ae235b
 *     %NULL
Packit ae235b
 *
Packit ae235b
 * Version of g_bus_own_name() using closures instead of callbacks for
Packit ae235b
 * easier binding in other languages.
Packit ae235b
 *
Packit ae235b
 * Returns: an identifier (never 0) that an be used with
Packit ae235b
 *     g_bus_unown_name() to stop owning the name.
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
guint
Packit ae235b
g_bus_own_name_with_closures (GBusType            bus_type,
Packit ae235b
                              const gchar        *name,
Packit ae235b
                              GBusNameOwnerFlags  flags,
Packit ae235b
                              GClosure           *bus_acquired_closure,
Packit ae235b
                              GClosure           *name_acquired_closure,
Packit ae235b
                              GClosure           *name_lost_closure)
Packit ae235b
{
Packit ae235b
  return g_bus_own_name (bus_type,
Packit ae235b
          name,
Packit ae235b
          flags,
Packit ae235b
          bus_acquired_closure != NULL ? own_with_closures_on_bus_acquired : NULL,
Packit ae235b
          name_acquired_closure != NULL ? own_with_closures_on_name_acquired : NULL,
Packit ae235b
          name_lost_closure != NULL ? own_with_closures_on_name_lost : NULL,
Packit ae235b
          own_name_data_new (bus_acquired_closure,
Packit ae235b
                             name_acquired_closure,
Packit ae235b
                             name_lost_closure),
Packit ae235b
          bus_own_name_free_func);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_bus_own_name_on_connection_with_closures: (rename-to g_bus_own_name_on_connection)
Packit ae235b
 * @connection: a #GDBusConnection
Packit ae235b
 * @name: the well-known name to own
Packit ae235b
 * @flags: a set of flags from the #GBusNameOwnerFlags enumeration
Packit ae235b
 * @name_acquired_closure: (nullable): #GClosure to invoke when @name is
Packit ae235b
 *     acquired or %NULL
Packit ae235b
 * @name_lost_closure: (nullable): #GClosure to invoke when @name is lost
Packit ae235b
 *     or %NULL
Packit ae235b
 *
Packit ae235b
 * Version of g_bus_own_name_on_connection() using closures instead of
Packit ae235b
 * callbacks for easier binding in other languages.
Packit ae235b
 *
Packit ae235b
 * Returns: an identifier (never 0) that an be used with
Packit ae235b
 *     g_bus_unown_name() to stop owning the name.
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
guint
Packit ae235b
g_bus_own_name_on_connection_with_closures (GDBusConnection    *connection,
Packit ae235b
                                            const gchar        *name,
Packit ae235b
                                            GBusNameOwnerFlags  flags,
Packit ae235b
                                            GClosure           *name_acquired_closure,
Packit ae235b
                                            GClosure           *name_lost_closure)
Packit ae235b
{
Packit ae235b
  return g_bus_own_name_on_connection (connection,
Packit ae235b
          name,
Packit ae235b
          flags,
Packit ae235b
          name_acquired_closure != NULL ? own_with_closures_on_name_acquired : NULL,
Packit ae235b
          name_lost_closure != NULL ? own_with_closures_on_name_lost : NULL,
Packit ae235b
          own_name_data_new (NULL,
Packit ae235b
                             name_acquired_closure,
Packit ae235b
                             name_lost_closure),
Packit ae235b
          bus_own_name_free_func);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_bus_unown_name:
Packit ae235b
 * @owner_id: an identifier obtained from g_bus_own_name()
Packit ae235b
 *
Packit ae235b
 * Stops owning a name.
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_bus_unown_name (guint owner_id)
Packit ae235b
{
Packit ae235b
  Client *client;
Packit ae235b
Packit ae235b
  g_return_if_fail (owner_id > 0);
Packit ae235b
Packit ae235b
  client = NULL;
Packit ae235b
Packit ae235b
  G_LOCK (lock);
Packit ae235b
  if (owner_id == 0 || map_id_to_client == NULL ||
Packit ae235b
      (client = g_hash_table_lookup (map_id_to_client, GUINT_TO_POINTER (owner_id))) == NULL)
Packit ae235b
    {
Packit ae235b
      g_warning ("Invalid id %d passed to g_bus_unown_name()", owner_id);
Packit ae235b
      goto out;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  client->cancelled = TRUE;
Packit ae235b
  g_warn_if_fail (g_hash_table_remove (map_id_to_client, GUINT_TO_POINTER (owner_id)));
Packit ae235b
Packit ae235b
 out:
Packit ae235b
  G_UNLOCK (lock);
Packit ae235b
Packit ae235b
  /* do callback without holding lock */
Packit ae235b
  if (client != NULL)
Packit ae235b
    {
Packit ae235b
      /* Release the name if needed */
Packit ae235b
      if (client->needs_release &&
Packit ae235b
          client->connection != NULL &&
Packit ae235b
          !g_dbus_connection_is_closed (client->connection))
Packit ae235b
        {
Packit ae235b
          GVariant *result;
Packit ae235b
          GError *error;
Packit ae235b
          guint32 release_name_reply;
Packit ae235b
Packit ae235b
          /* TODO: it kinda sucks having to do a sync call to release the name - but if
Packit ae235b
           * we don't, then a subsequent grab of the name will make the bus daemon return
Packit ae235b
           * IN_QUEUE which will trigger name_lost().
Packit ae235b
           *
Packit ae235b
           * I believe this is a bug in the bus daemon.
Packit ae235b
           */
Packit ae235b
          error = NULL;
Packit ae235b
          result = g_dbus_connection_call_sync (client->connection,
Packit ae235b
                                                "org.freedesktop.DBus",  /* bus name */
Packit ae235b
                                                "/org/freedesktop/DBus", /* object path */
Packit ae235b
                                                "org.freedesktop.DBus",  /* interface name */
Packit ae235b
                                                "ReleaseName",           /* method name */
Packit ae235b
                                                g_variant_new ("(s)", client->name),
Packit ae235b
                                                G_VARIANT_TYPE ("(u)"),
Packit ae235b
                                                G_DBUS_CALL_FLAGS_NONE,
Packit ae235b
                                                -1,
Packit ae235b
                                                NULL,
Packit ae235b
                                                &error);
Packit ae235b
          if (result == NULL)
Packit ae235b
            {
Packit ae235b
              g_warning ("Error releasing name %s: %s", client->name, error->message);
Packit ae235b
              g_error_free (error);
Packit ae235b
            }
Packit ae235b
          else
Packit ae235b
            {
Packit ae235b
              g_variant_get (result, "(u)", &release_name_reply);
Packit ae235b
              if (release_name_reply != 1 /* DBUS_RELEASE_NAME_REPLY_RELEASED */)
Packit ae235b
                {
Packit ae235b
                  g_warning ("Unexpected reply %d when releasing name %s", release_name_reply, client->name);
Packit ae235b
                }
Packit ae235b
              g_variant_unref (result);
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
Packit ae235b
      if (client->disconnected_signal_handler_id > 0)
Packit ae235b
        g_signal_handler_disconnect (client->connection, client->disconnected_signal_handler_id);
Packit ae235b
      if (client->name_acquired_subscription_id > 0)
Packit ae235b
        g_dbus_connection_signal_unsubscribe (client->connection, client->name_acquired_subscription_id);
Packit ae235b
      if (client->name_lost_subscription_id > 0)
Packit ae235b
        g_dbus_connection_signal_unsubscribe (client->connection, client->name_lost_subscription_id);
Packit ae235b
      client->disconnected_signal_handler_id = 0;
Packit ae235b
      client->name_acquired_subscription_id = 0;
Packit ae235b
      client->name_lost_subscription_id = 0;
Packit ae235b
      if (client->connection != NULL)
Packit ae235b
        {
Packit ae235b
          g_object_unref (client->connection);
Packit ae235b
          client->connection = NULL;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      client_unref (client);
Packit ae235b
    }
Packit ae235b
}