Blame gio/gdbusaddress.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
#include <string.h>
Packit ae235b
#include <stdio.h>
Packit ae235b
#include <errno.h>
Packit ae235b
Packit ae235b
#include "gioerror.h"
Packit ae235b
#include "gdbusutils.h"
Packit ae235b
#include "gdbusaddress.h"
Packit ae235b
#include "gdbuserror.h"
Packit ae235b
#include "gioenumtypes.h"
Packit ae235b
#include "gnetworkaddress.h"
Packit ae235b
#include "gsocketclient.h"
Packit ae235b
#include "giostream.h"
Packit ae235b
#include "gasyncresult.h"
Packit ae235b
#include "gtask.h"
Packit ae235b
#include "glib-private.h"
Packit ae235b
#include "gdbusprivate.h"
Packit ae235b
#include "giomodule-priv.h"
Packit ae235b
#include "gdbusdaemon.h"
Packit ae235b
#include "gstdio.h"
Packit ae235b
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
#include <unistd.h>
Packit ae235b
#include <sys/stat.h>
Packit ae235b
#include <sys/types.h>
Packit ae235b
#include <gio/gunixsocketaddress.h>
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
#include <windows.h>
Packit ae235b
#include <io.h>
Packit ae235b
#include <conio.h>
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#include "glibintl.h"
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:gdbusaddress
Packit ae235b
 * @title: D-Bus Addresses
Packit ae235b
 * @short_description: D-Bus connection endpoints
Packit ae235b
 * @include: gio/gio.h
Packit ae235b
 *
Packit ae235b
 * Routines for working with D-Bus addresses. A D-Bus address is a string
Packit ae235b
 * like `unix:tmpdir=/tmp/my-app-name`. The exact format of addresses
Packit ae235b
 * is explained in detail in the
Packit ae235b
 * [D-Bus specification](http://dbus.freedesktop.org/doc/dbus-specification.html#addresses).
Packit ae235b
 *
Packit ae235b
 * TCP D-Bus connections are supported, but accessing them via a proxy is
Packit ae235b
 * currently not supported.
Packit ae235b
 */
Packit ae235b
Packit ae235b
static gchar *get_session_address_platform_specific (GError **error);
Packit ae235b
static gchar *get_session_address_dbus_launch       (GError **error);
Packit ae235b
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_dbus_is_address:
Packit ae235b
 * @string: A string.
Packit ae235b
 *
Packit ae235b
 * Checks if @string is a
Packit ae235b
 * [D-Bus address](https://dbus.freedesktop.org/doc/dbus-specification.html#addresses).
Packit ae235b
 *
Packit ae235b
 * This doesn't check if @string is actually supported by #GDBusServer
Packit ae235b
 * or #GDBusConnection - use g_dbus_is_supported_address() to do more
Packit ae235b
 * checks.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if @string is a valid D-Bus address, %FALSE otherwise.
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_dbus_is_address (const gchar *string)
Packit ae235b
{
Packit ae235b
  guint n;
Packit ae235b
  gchar **a;
Packit ae235b
  gboolean ret;
Packit ae235b
Packit ae235b
  ret = FALSE;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (string != NULL, FALSE);
Packit ae235b
Packit ae235b
  a = g_strsplit (string, ";", 0);
Packit ae235b
  if (a[0] == NULL)
Packit ae235b
    goto out;
Packit ae235b
Packit ae235b
  for (n = 0; a[n] != NULL; n++)
Packit ae235b
    {
Packit ae235b
      if (!_g_dbus_address_parse_entry (a[n],
Packit ae235b
                                        NULL,
Packit ae235b
                                        NULL,
Packit ae235b
                                        NULL))
Packit ae235b
        goto out;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  ret = TRUE;
Packit ae235b
Packit ae235b
 out:
Packit ae235b
  g_strfreev (a);
Packit ae235b
  return ret;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
is_valid_unix (const gchar  *address_entry,
Packit ae235b
               GHashTable   *key_value_pairs,
Packit ae235b
               GError      **error)
Packit ae235b
{
Packit ae235b
  gboolean ret;
Packit ae235b
  GList *keys;
Packit ae235b
  GList *l;
Packit ae235b
  const gchar *path;
Packit ae235b
  const gchar *tmpdir;
Packit ae235b
  const gchar *abstract;
Packit ae235b
Packit ae235b
  ret = FALSE;
Packit ae235b
  keys = NULL;
Packit ae235b
  path = NULL;
Packit ae235b
  tmpdir = NULL;
Packit ae235b
  abstract = NULL;
Packit ae235b
Packit ae235b
  keys = g_hash_table_get_keys (key_value_pairs);
Packit ae235b
  for (l = keys; l != NULL; l = l->next)
Packit ae235b
    {
Packit ae235b
      const gchar *key = l->data;
Packit ae235b
      if (g_strcmp0 (key, "path") == 0)
Packit ae235b
        path = g_hash_table_lookup (key_value_pairs, key);
Packit ae235b
      else if (g_strcmp0 (key, "tmpdir") == 0)
Packit ae235b
        tmpdir = g_hash_table_lookup (key_value_pairs, key);
Packit ae235b
      else if (g_strcmp0 (key, "abstract") == 0)
Packit ae235b
        abstract = g_hash_table_lookup (key_value_pairs, key);
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          g_set_error (error,
Packit ae235b
                       G_IO_ERROR,
Packit ae235b
                       G_IO_ERROR_INVALID_ARGUMENT,
Packit ae235b
                       _("Unsupported key “%s” in address entry “%s”"),
Packit ae235b
                       key,
Packit ae235b
                       address_entry);
Packit ae235b
          goto out;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (path != NULL)
Packit ae235b
    {
Packit ae235b
      if (tmpdir != NULL || abstract != NULL)
Packit ae235b
        goto meaningless;
Packit ae235b
    }
Packit ae235b
  else if (tmpdir != NULL)
Packit ae235b
    {
Packit ae235b
      if (path != NULL || abstract != NULL)
Packit ae235b
        goto meaningless;
Packit ae235b
    }
Packit ae235b
  else if (abstract != NULL)
Packit ae235b
    {
Packit ae235b
      if (path != NULL || tmpdir != NULL)
Packit ae235b
        goto meaningless;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      g_set_error (error,
Packit ae235b
                   G_IO_ERROR,
Packit ae235b
                   G_IO_ERROR_INVALID_ARGUMENT,
Packit ae235b
                   _("Address “%s” is invalid (need exactly one of path, tmpdir or abstract keys)"),
Packit ae235b
                   address_entry);
Packit ae235b
      goto out;
Packit ae235b
    }
Packit ae235b
Packit ae235b
Packit ae235b
  ret= TRUE;
Packit ae235b
  goto out;
Packit ae235b
Packit ae235b
 meaningless:
Packit ae235b
  g_set_error (error,
Packit ae235b
               G_IO_ERROR,
Packit ae235b
               G_IO_ERROR_INVALID_ARGUMENT,
Packit ae235b
               _("Meaningless key/value pair combination in address entry “%s”"),
Packit ae235b
               address_entry);
Packit ae235b
Packit ae235b
 out:
Packit ae235b
  g_list_free (keys);
Packit ae235b
Packit ae235b
  return ret;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
is_valid_nonce_tcp (const gchar  *address_entry,
Packit ae235b
                    GHashTable   *key_value_pairs,
Packit ae235b
                    GError      **error)
Packit ae235b
{
Packit ae235b
  gboolean ret;
Packit ae235b
  GList *keys;
Packit ae235b
  GList *l;
Packit ae235b
  const gchar *host;
Packit ae235b
  const gchar *port;
Packit ae235b
  const gchar *family;
Packit ae235b
  const gchar *nonce_file;
Packit ae235b
  gint port_num;
Packit ae235b
  gchar *endp;
Packit ae235b
Packit ae235b
  ret = FALSE;
Packit ae235b
  keys = NULL;
Packit ae235b
  host = NULL;
Packit ae235b
  port = NULL;
Packit ae235b
  family = NULL;
Packit ae235b
  nonce_file = NULL;
Packit ae235b
Packit ae235b
  keys = g_hash_table_get_keys (key_value_pairs);
Packit ae235b
  for (l = keys; l != NULL; l = l->next)
Packit ae235b
    {
Packit ae235b
      const gchar *key = l->data;
Packit ae235b
      if (g_strcmp0 (key, "host") == 0)
Packit ae235b
        host = g_hash_table_lookup (key_value_pairs, key);
Packit ae235b
      else if (g_strcmp0 (key, "port") == 0)
Packit ae235b
        port = g_hash_table_lookup (key_value_pairs, key);
Packit ae235b
      else if (g_strcmp0 (key, "family") == 0)
Packit ae235b
        family = g_hash_table_lookup (key_value_pairs, key);
Packit ae235b
      else if (g_strcmp0 (key, "noncefile") == 0)
Packit ae235b
        nonce_file = g_hash_table_lookup (key_value_pairs, key);
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          g_set_error (error,
Packit ae235b
                       G_IO_ERROR,
Packit ae235b
                       G_IO_ERROR_INVALID_ARGUMENT,
Packit ae235b
                       _("Unsupported key “%s” in address entry “%s”"),
Packit ae235b
                       key,
Packit ae235b
                       address_entry);
Packit ae235b
          goto out;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (port != NULL)
Packit ae235b
    {
Packit ae235b
      port_num = strtol (port, &endp, 10);
Packit ae235b
      if ((*port == '\0' || *endp != '\0') || port_num < 0 || port_num >= 65536)
Packit ae235b
        {
Packit ae235b
          g_set_error (error,
Packit ae235b
                       G_IO_ERROR,
Packit ae235b
                       G_IO_ERROR_INVALID_ARGUMENT,
Packit ae235b
                       _("Error in address “%s” — the port attribute is malformed"),
Packit ae235b
                       address_entry);
Packit ae235b
          goto out;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (family != NULL && !(g_strcmp0 (family, "ipv4") == 0 || g_strcmp0 (family, "ipv6") == 0))
Packit ae235b
    {
Packit ae235b
      g_set_error (error,
Packit ae235b
                   G_IO_ERROR,
Packit ae235b
                   G_IO_ERROR_INVALID_ARGUMENT,
Packit ae235b
                   _("Error in address “%s” — the family attribute is malformed"),
Packit ae235b
                   address_entry);
Packit ae235b
      goto out;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (host != NULL)
Packit ae235b
    {
Packit ae235b
      /* TODO: validate host */
Packit ae235b
    }
Packit ae235b
Packit ae235b
  nonce_file = nonce_file; /* To avoid -Wunused-but-set-variable */
Packit ae235b
Packit ae235b
  ret= TRUE;
Packit ae235b
Packit ae235b
 out:
Packit ae235b
  g_list_free (keys);
Packit ae235b
Packit ae235b
  return ret;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
is_valid_tcp (const gchar  *address_entry,
Packit ae235b
              GHashTable   *key_value_pairs,
Packit ae235b
              GError      **error)
Packit ae235b
{
Packit ae235b
  gboolean ret;
Packit ae235b
  GList *keys;
Packit ae235b
  GList *l;
Packit ae235b
  const gchar *host;
Packit ae235b
  const gchar *port;
Packit ae235b
  const gchar *family;
Packit ae235b
  gint port_num;
Packit ae235b
  gchar *endp;
Packit ae235b
Packit ae235b
  ret = FALSE;
Packit ae235b
  keys = NULL;
Packit ae235b
  host = NULL;
Packit ae235b
  port = NULL;
Packit ae235b
  family = NULL;
Packit ae235b
Packit ae235b
  keys = g_hash_table_get_keys (key_value_pairs);
Packit ae235b
  for (l = keys; l != NULL; l = l->next)
Packit ae235b
    {
Packit ae235b
      const gchar *key = l->data;
Packit ae235b
      if (g_strcmp0 (key, "host") == 0)
Packit ae235b
        host = g_hash_table_lookup (key_value_pairs, key);
Packit ae235b
      else if (g_strcmp0 (key, "port") == 0)
Packit ae235b
        port = g_hash_table_lookup (key_value_pairs, key);
Packit ae235b
      else if (g_strcmp0 (key, "family") == 0)
Packit ae235b
        family = g_hash_table_lookup (key_value_pairs, key);
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          g_set_error (error,
Packit ae235b
                       G_IO_ERROR,
Packit ae235b
                       G_IO_ERROR_INVALID_ARGUMENT,
Packit ae235b
                       _("Unsupported key “%s” in address entry “%s”"),
Packit ae235b
                       key,
Packit ae235b
                       address_entry);
Packit ae235b
          goto out;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (port != NULL)
Packit ae235b
    {
Packit ae235b
      port_num = strtol (port, &endp, 10);
Packit ae235b
      if ((*port == '\0' || *endp != '\0') || port_num < 0 || port_num >= 65536)
Packit ae235b
        {
Packit ae235b
          g_set_error (error,
Packit ae235b
                       G_IO_ERROR,
Packit ae235b
                       G_IO_ERROR_INVALID_ARGUMENT,
Packit ae235b
                       _("Error in address “%s” — the port attribute is malformed"),
Packit ae235b
                       address_entry);
Packit ae235b
          goto out;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (family != NULL && !(g_strcmp0 (family, "ipv4") == 0 || g_strcmp0 (family, "ipv6") == 0))
Packit ae235b
    {
Packit ae235b
      g_set_error (error,
Packit ae235b
                   G_IO_ERROR,
Packit ae235b
                   G_IO_ERROR_INVALID_ARGUMENT,
Packit ae235b
                   _("Error in address “%s” — the family attribute is malformed"),
Packit ae235b
                   address_entry);
Packit ae235b
      goto out;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (host != NULL)
Packit ae235b
    {
Packit ae235b
      /* TODO: validate host */
Packit ae235b
    }
Packit ae235b
Packit ae235b
  ret= TRUE;
Packit ae235b
Packit ae235b
 out:
Packit ae235b
  g_list_free (keys);
Packit ae235b
Packit ae235b
  return ret;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_dbus_is_supported_address:
Packit ae235b
 * @string: A string.
Packit ae235b
 * @error: Return location for error or %NULL.
Packit ae235b
 *
Packit ae235b
 * Like g_dbus_is_address() but also checks if the library supports the
Packit ae235b
 * transports in @string and that key/value pairs for each transport
Packit ae235b
 * are valid. See the specification of the
Packit ae235b
 * [D-Bus address format](https://dbus.freedesktop.org/doc/dbus-specification.html#addresses).
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if @string is a valid D-Bus address that is
Packit ae235b
 * supported by this library, %FALSE if @error is set.
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_dbus_is_supported_address (const gchar  *string,
Packit ae235b
                             GError      **error)
Packit ae235b
{
Packit ae235b
  guint n;
Packit ae235b
  gchar **a;
Packit ae235b
  gboolean ret;
Packit ae235b
Packit ae235b
  ret = FALSE;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (string != NULL, FALSE);
Packit ae235b
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
Packit ae235b
Packit ae235b
  a = g_strsplit (string, ";", 0);
Packit ae235b
  for (n = 0; a[n] != NULL; n++)
Packit ae235b
    {
Packit ae235b
      gchar *transport_name;
Packit ae235b
      GHashTable *key_value_pairs;
Packit ae235b
      gboolean supported;
Packit ae235b
Packit ae235b
      if (!_g_dbus_address_parse_entry (a[n],
Packit ae235b
                                        &transport_name,
Packit ae235b
                                        &key_value_pairs,
Packit ae235b
                                        error))
Packit ae235b
        goto out;
Packit ae235b
Packit ae235b
      supported = FALSE;
Packit ae235b
      if (g_strcmp0 (transport_name, "unix") == 0)
Packit ae235b
        supported = is_valid_unix (a[n], key_value_pairs, error);
Packit ae235b
      else if (g_strcmp0 (transport_name, "tcp") == 0)
Packit ae235b
        supported = is_valid_tcp (a[n], key_value_pairs, error);
Packit ae235b
      else if (g_strcmp0 (transport_name, "nonce-tcp") == 0)
Packit ae235b
        supported = is_valid_nonce_tcp (a[n], key_value_pairs, error);
Packit ae235b
      else if (g_strcmp0 (a[n], "autolaunch:") == 0)
Packit ae235b
        supported = TRUE;
Packit ae235b
Packit ae235b
      g_free (transport_name);
Packit ae235b
      g_hash_table_unref (key_value_pairs);
Packit ae235b
Packit ae235b
      if (!supported)
Packit ae235b
        goto out;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  ret = TRUE;
Packit ae235b
Packit ae235b
 out:
Packit ae235b
  g_strfreev (a);
Packit ae235b
Packit ae235b
  g_assert (ret || (!ret && (error == NULL || *error != NULL)));
Packit ae235b
Packit ae235b
  return ret;
Packit ae235b
}
Packit ae235b
Packit ae235b
gboolean
Packit ae235b
_g_dbus_address_parse_entry (const gchar  *address_entry,
Packit ae235b
                             gchar       **out_transport_name,
Packit ae235b
                             GHashTable  **out_key_value_pairs,
Packit ae235b
                             GError      **error)
Packit ae235b
{
Packit ae235b
  gboolean ret;
Packit ae235b
  GHashTable *key_value_pairs;
Packit ae235b
  gchar *transport_name;
Packit ae235b
  gchar **kv_pairs;
Packit ae235b
  const gchar *s;
Packit ae235b
  guint n;
Packit ae235b
Packit ae235b
  ret = FALSE;
Packit ae235b
  kv_pairs = NULL;
Packit ae235b
  transport_name = NULL;
Packit ae235b
  key_value_pairs = NULL;
Packit ae235b
Packit ae235b
  s = strchr (address_entry, ':');
Packit ae235b
  if (s == NULL)
Packit ae235b
    {
Packit ae235b
      g_set_error (error,
Packit ae235b
                   G_IO_ERROR,
Packit ae235b
                   G_IO_ERROR_INVALID_ARGUMENT,
Packit ae235b
                   _("Address element “%s” does not contain a colon (:)"),
Packit ae235b
                   address_entry);
Packit ae235b
      goto out;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  transport_name = g_strndup (address_entry, s - address_entry);
Packit ae235b
  key_value_pairs = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
Packit ae235b
Packit ae235b
  kv_pairs = g_strsplit (s + 1, ",", 0);
Packit ae235b
  for (n = 0; kv_pairs != NULL && kv_pairs[n] != NULL; n++)
Packit ae235b
    {
Packit ae235b
      const gchar *kv_pair = kv_pairs[n];
Packit ae235b
      gchar *key;
Packit ae235b
      gchar *value;
Packit ae235b
Packit ae235b
      s = strchr (kv_pair, '=');
Packit ae235b
      if (s == NULL)
Packit ae235b
        {
Packit ae235b
          g_set_error (error,
Packit ae235b
                       G_IO_ERROR,
Packit ae235b
                       G_IO_ERROR_INVALID_ARGUMENT,
Packit ae235b
                       _("Key/Value pair %d, “%s”, in address element “%s” does not contain an equal sign"),
Packit ae235b
                       n,
Packit ae235b
                       kv_pair,
Packit ae235b
                       address_entry);
Packit ae235b
          goto out;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      key = g_uri_unescape_segment (kv_pair, s, NULL);
Packit ae235b
      value = g_uri_unescape_segment (s + 1, kv_pair + strlen (kv_pair), NULL);
Packit ae235b
      if (key == NULL || value == NULL)
Packit ae235b
        {
Packit ae235b
          g_set_error (error,
Packit ae235b
                       G_IO_ERROR,
Packit ae235b
                       G_IO_ERROR_INVALID_ARGUMENT,
Packit ae235b
                       _("Error unescaping key or value in Key/Value pair %d, “%s”, in address element “%s”"),
Packit ae235b
                       n,
Packit ae235b
                       kv_pair,
Packit ae235b
                       address_entry);
Packit ae235b
          g_free (key);
Packit ae235b
          g_free (value);
Packit ae235b
          goto out;
Packit ae235b
        }
Packit ae235b
      g_hash_table_insert (key_value_pairs, key, value);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  ret = TRUE;
Packit ae235b
Packit ae235b
out:
Packit ae235b
  g_strfreev (kv_pairs);
Packit ae235b
  if (ret)
Packit ae235b
    {
Packit ae235b
      if (out_transport_name != NULL)
Packit ae235b
        *out_transport_name = transport_name;
Packit ae235b
      else
Packit ae235b
        g_free (transport_name);
Packit ae235b
      if (out_key_value_pairs != NULL)
Packit ae235b
        *out_key_value_pairs = key_value_pairs;
Packit ae235b
      else if (key_value_pairs != NULL)
Packit ae235b
        g_hash_table_unref (key_value_pairs);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      g_free (transport_name);
Packit ae235b
      if (key_value_pairs != NULL)
Packit ae235b
        g_hash_table_unref (key_value_pairs);
Packit ae235b
    }
Packit ae235b
  return ret;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
Packit ae235b
static GIOStream *
Packit ae235b
g_dbus_address_try_connect_one (const gchar   *address_entry,
Packit ae235b
                                gchar        **out_guid,
Packit ae235b
                                GCancellable  *cancellable,
Packit ae235b
                                GError       **error);
Packit ae235b
Packit ae235b
/* TODO: Declare an extension point called GDBusTransport (or similar)
Packit ae235b
 * and move code below to extensions implementing said extension
Packit ae235b
 * point. That way we can implement a D-Bus transport over X11 without
Packit ae235b
 * making libgio link to libX11...
Packit ae235b
 */
Packit ae235b
static GIOStream *
Packit ae235b
g_dbus_address_connect (const gchar   *address_entry,
Packit ae235b
                        const gchar   *transport_name,
Packit ae235b
                        GHashTable    *key_value_pairs,
Packit ae235b
                        GCancellable  *cancellable,
Packit ae235b
                        GError       **error)
Packit ae235b
{
Packit ae235b
  GIOStream *ret;
Packit ae235b
  GSocketConnectable *connectable;
Packit ae235b
  const gchar *nonce_file;
Packit ae235b
Packit ae235b
  connectable = NULL;
Packit ae235b
  ret = NULL;
Packit ae235b
  nonce_file = NULL;
Packit ae235b
Packit ae235b
  if (FALSE)
Packit ae235b
    {
Packit ae235b
    }
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
  else if (g_strcmp0 (transport_name, "unix") == 0)
Packit ae235b
    {
Packit ae235b
      const gchar *path;
Packit ae235b
      const gchar *abstract;
Packit ae235b
      path = g_hash_table_lookup (key_value_pairs, "path");
Packit ae235b
      abstract = g_hash_table_lookup (key_value_pairs, "abstract");
Packit ae235b
      if ((path == NULL && abstract == NULL) || (path != NULL && abstract != NULL))
Packit ae235b
        {
Packit ae235b
          g_set_error (error,
Packit ae235b
                       G_IO_ERROR,
Packit ae235b
                       G_IO_ERROR_INVALID_ARGUMENT,
Packit ae235b
                       _("Error in address “%s” — the unix transport requires exactly one of the "
Packit ae235b
                         "keys “path” or “abstract” to be set"),
Packit ae235b
                       address_entry);
Packit ae235b
        }
Packit ae235b
      else if (path != NULL)
Packit ae235b
        {
Packit ae235b
          connectable = G_SOCKET_CONNECTABLE (g_unix_socket_address_new (path));
Packit ae235b
        }
Packit ae235b
      else if (abstract != NULL)
Packit ae235b
        {
Packit ae235b
          connectable = G_SOCKET_CONNECTABLE (g_unix_socket_address_new_with_type (abstract,
Packit ae235b
                                                                                   -1,
Packit ae235b
                                                                                   G_UNIX_SOCKET_ADDRESS_ABSTRACT));
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          g_assert_not_reached ();
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
#endif
Packit ae235b
  else if (g_strcmp0 (transport_name, "tcp") == 0 || g_strcmp0 (transport_name, "nonce-tcp") == 0)
Packit ae235b
    {
Packit ae235b
      const gchar *s;
Packit ae235b
      const gchar *host;
Packit ae235b
      glong port;
Packit ae235b
      gchar *endp;
Packit ae235b
      gboolean is_nonce;
Packit ae235b
Packit ae235b
      is_nonce = (g_strcmp0 (transport_name, "nonce-tcp") == 0);
Packit ae235b
Packit ae235b
      host = g_hash_table_lookup (key_value_pairs, "host");
Packit ae235b
      if (host == NULL)
Packit ae235b
        {
Packit ae235b
          g_set_error (error,
Packit ae235b
                       G_IO_ERROR,
Packit ae235b
                       G_IO_ERROR_INVALID_ARGUMENT,
Packit ae235b
                       _("Error in address “%s” — the host attribute is missing or malformed"),
Packit ae235b
                       address_entry);
Packit ae235b
          goto out;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      s = g_hash_table_lookup (key_value_pairs, "port");
Packit ae235b
      if (s == NULL)
Packit ae235b
        s = "0";
Packit ae235b
      port = strtol (s, &endp, 10);
Packit ae235b
      if ((*s == '\0' || *endp != '\0') || port < 0 || port >= 65536)
Packit ae235b
        {
Packit ae235b
          g_set_error (error,
Packit ae235b
                       G_IO_ERROR,
Packit ae235b
                       G_IO_ERROR_INVALID_ARGUMENT,
Packit ae235b
                       _("Error in address “%s” — the port attribute is missing or malformed"),
Packit ae235b
                       address_entry);
Packit ae235b
          goto out;
Packit ae235b
        }
Packit ae235b
Packit ae235b
Packit ae235b
      if (is_nonce)
Packit ae235b
        {
Packit ae235b
          nonce_file = g_hash_table_lookup (key_value_pairs, "noncefile");
Packit ae235b
          if (nonce_file == NULL)
Packit ae235b
            {
Packit ae235b
              g_set_error (error,
Packit ae235b
                           G_IO_ERROR,
Packit ae235b
                           G_IO_ERROR_INVALID_ARGUMENT,
Packit ae235b
                           _("Error in address “%s” — the noncefile attribute is missing or malformed"),
Packit ae235b
                           address_entry);
Packit ae235b
              goto out;
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
Packit ae235b
      /* TODO: deal with family key/value-pair */
Packit ae235b
      connectable = g_network_address_new (host, port);
Packit ae235b
    }
Packit ae235b
  else if (g_strcmp0 (address_entry, "autolaunch:") == 0)
Packit ae235b
    {
Packit ae235b
      gchar *autolaunch_address;
Packit ae235b
      autolaunch_address = get_session_address_dbus_launch (error);
Packit ae235b
      if (autolaunch_address != NULL)
Packit ae235b
        {
Packit ae235b
          ret = g_dbus_address_try_connect_one (autolaunch_address, NULL, cancellable, error);
Packit ae235b
          g_free (autolaunch_address);
Packit ae235b
          goto out;
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          g_prefix_error (error, _("Error auto-launching: "));
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      g_set_error (error,
Packit ae235b
                   G_IO_ERROR,
Packit ae235b
                   G_IO_ERROR_INVALID_ARGUMENT,
Packit ae235b
                   _("Unknown or unsupported transport “%s” for address “%s”"),
Packit ae235b
                   transport_name,
Packit ae235b
                   address_entry);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (connectable != NULL)
Packit ae235b
    {
Packit ae235b
      GSocketClient *client;
Packit ae235b
      GSocketConnection *connection;
Packit ae235b
Packit ae235b
      g_assert (ret == NULL);
Packit ae235b
      client = g_socket_client_new ();
Packit ae235b
Packit ae235b
      /* Disable proxy support to prevent a deadlock on startup, since loading a
Packit ae235b
       * proxy resolver causes the GIO modules to be loaded, and there will
Packit ae235b
       * almost certainly be one of them which then tries to use GDBus.
Packit ae235b
       * See: https://bugzilla.gnome.org/show_bug.cgi?id=792499 */
Packit ae235b
      g_socket_client_set_enable_proxy (client, FALSE);
Packit ae235b
Packit ae235b
      connection = g_socket_client_connect (client,
Packit ae235b
                                            connectable,
Packit ae235b
                                            cancellable,
Packit ae235b
                                            error);
Packit ae235b
      g_object_unref (connectable);
Packit ae235b
      g_object_unref (client);
Packit ae235b
      if (connection == NULL)
Packit ae235b
        goto out;
Packit ae235b
Packit ae235b
      ret = G_IO_STREAM (connection);
Packit ae235b
Packit ae235b
      if (nonce_file != NULL)
Packit ae235b
        {
Packit ae235b
          gchar nonce_contents[16 + 1];
Packit ae235b
          size_t num_bytes_read;
Packit ae235b
          FILE *f;
Packit ae235b
          int errsv;
Packit ae235b
Packit ae235b
          /* be careful to read only 16 bytes - we also check that the file is only 16 bytes long */
Packit ae235b
          f = fopen (nonce_file, "rb");
Packit ae235b
          errsv = errno;
Packit ae235b
          if (f == NULL)
Packit ae235b
            {
Packit ae235b
              g_set_error (error,
Packit ae235b
                           G_IO_ERROR,
Packit ae235b
                           G_IO_ERROR_INVALID_ARGUMENT,
Packit ae235b
                           _("Error opening nonce file “%s”: %s"),
Packit ae235b
                           nonce_file,
Packit ae235b
                           g_strerror (errsv));
Packit ae235b
              g_object_unref (ret);
Packit ae235b
              ret = NULL;
Packit ae235b
              goto out;
Packit ae235b
            }
Packit ae235b
          num_bytes_read = fread (nonce_contents,
Packit ae235b
                                  sizeof (gchar),
Packit ae235b
                                  16 + 1,
Packit ae235b
                                  f);
Packit ae235b
          errsv = errno;
Packit ae235b
          if (num_bytes_read != 16)
Packit ae235b
            {
Packit ae235b
              if (num_bytes_read == 0)
Packit ae235b
                {
Packit ae235b
                  g_set_error (error,
Packit ae235b
                               G_IO_ERROR,
Packit ae235b
                               G_IO_ERROR_INVALID_ARGUMENT,
Packit ae235b
                               _("Error reading from nonce file “%s”: %s"),
Packit ae235b
                               nonce_file,
Packit ae235b
                               g_strerror (errsv));
Packit ae235b
                }
Packit ae235b
              else
Packit ae235b
                {
Packit ae235b
                  g_set_error (error,
Packit ae235b
                               G_IO_ERROR,
Packit ae235b
                               G_IO_ERROR_INVALID_ARGUMENT,
Packit ae235b
                               _("Error reading from nonce file “%s”, expected 16 bytes, got %d"),
Packit ae235b
                               nonce_file,
Packit ae235b
                               (gint) num_bytes_read);
Packit ae235b
                }
Packit ae235b
              g_object_unref (ret);
Packit ae235b
              ret = NULL;
Packit ae235b
              fclose (f);
Packit ae235b
              goto out;
Packit ae235b
            }
Packit ae235b
          fclose (f);
Packit ae235b
Packit ae235b
          if (!g_output_stream_write_all (g_io_stream_get_output_stream (ret),
Packit ae235b
                                          nonce_contents,
Packit ae235b
                                          16,
Packit ae235b
                                          NULL,
Packit ae235b
                                          cancellable,
Packit ae235b
                                          error))
Packit ae235b
            {
Packit ae235b
              g_prefix_error (error, _("Error writing contents of nonce file “%s” to stream:"), nonce_file);
Packit ae235b
              g_object_unref (ret);
Packit ae235b
              ret = NULL;
Packit ae235b
              goto out;
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
 out:
Packit ae235b
Packit ae235b
  return ret;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GIOStream *
Packit ae235b
g_dbus_address_try_connect_one (const gchar   *address_entry,
Packit ae235b
                                gchar        **out_guid,
Packit ae235b
                                GCancellable  *cancellable,
Packit ae235b
                                GError       **error)
Packit ae235b
{
Packit ae235b
  GIOStream *ret;
Packit ae235b
  GHashTable *key_value_pairs;
Packit ae235b
  gchar *transport_name;
Packit ae235b
  const gchar *guid;
Packit ae235b
Packit ae235b
  ret = NULL;
Packit ae235b
  transport_name = NULL;
Packit ae235b
  key_value_pairs = NULL;
Packit ae235b
Packit ae235b
  if (!_g_dbus_address_parse_entry (address_entry,
Packit ae235b
                                    &transport_name,
Packit ae235b
                                    &key_value_pairs,
Packit ae235b
                                    error))
Packit ae235b
    goto out;
Packit ae235b
Packit ae235b
  ret = g_dbus_address_connect (address_entry,
Packit ae235b
                                transport_name,
Packit ae235b
                                key_value_pairs,
Packit ae235b
                                cancellable,
Packit ae235b
                                error);
Packit ae235b
  if (ret == NULL)
Packit ae235b
    goto out;
Packit ae235b
Packit ae235b
  guid = g_hash_table_lookup (key_value_pairs, "guid");
Packit ae235b
  if (guid != NULL && out_guid != NULL)
Packit ae235b
    *out_guid = g_strdup (guid);
Packit ae235b
Packit ae235b
out:
Packit ae235b
  g_free (transport_name);
Packit ae235b
  if (key_value_pairs != NULL)
Packit ae235b
    g_hash_table_unref (key_value_pairs);
Packit ae235b
  return ret;
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
Packit ae235b
typedef struct {
Packit ae235b
  gchar *address;
Packit ae235b
  gchar *guid;
Packit ae235b
} GetStreamData;
Packit ae235b
Packit ae235b
static void
Packit ae235b
get_stream_data_free (GetStreamData *data)
Packit ae235b
{
Packit ae235b
  g_free (data->address);
Packit ae235b
  g_free (data->guid);
Packit ae235b
  g_free (data);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
get_stream_thread_func (GTask         *task,
Packit ae235b
                        gpointer       source_object,
Packit ae235b
                        gpointer       task_data,
Packit ae235b
                        GCancellable  *cancellable)
Packit ae235b
{
Packit ae235b
  GetStreamData *data = task_data;
Packit ae235b
  GIOStream *stream;
Packit ae235b
  GError *error = NULL;
Packit ae235b
Packit ae235b
  stream = g_dbus_address_get_stream_sync (data->address,
Packit ae235b
                                           &data->guid,
Packit ae235b
                                           cancellable,
Packit ae235b
                                           &error);
Packit ae235b
  if (stream)
Packit ae235b
    g_task_return_pointer (task, stream, g_object_unref);
Packit ae235b
  else
Packit ae235b
    g_task_return_error (task, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_dbus_address_get_stream:
Packit ae235b
 * @address: A valid D-Bus address.
Packit ae235b
 * @cancellable: (nullable): A #GCancellable or %NULL.
Packit ae235b
 * @callback: A #GAsyncReadyCallback to call when the request is satisfied.
Packit ae235b
 * @user_data: Data to pass to @callback.
Packit ae235b
 *
Packit ae235b
 * Asynchronously connects to an endpoint specified by @address and
Packit ae235b
 * sets up the connection so it is in a state to run the client-side
Packit ae235b
 * of the D-Bus authentication conversation. @address must be in the
Packit ae235b
 * [D-Bus address format](https://dbus.freedesktop.org/doc/dbus-specification.html#addresses).
Packit ae235b
 *
Packit ae235b
 * When the operation is finished, @callback will be invoked. You can
Packit ae235b
 * then call g_dbus_address_get_stream_finish() to get the result of
Packit ae235b
 * the operation.
Packit ae235b
 *
Packit ae235b
 * This is an asynchronous failable function. See
Packit ae235b
 * g_dbus_address_get_stream_sync() for the synchronous version.
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_dbus_address_get_stream (const gchar         *address,
Packit ae235b
                           GCancellable        *cancellable,
Packit ae235b
                           GAsyncReadyCallback  callback,
Packit ae235b
                           gpointer             user_data)
Packit ae235b
{
Packit ae235b
  GTask *task;
Packit ae235b
  GetStreamData *data;
Packit ae235b
Packit ae235b
  g_return_if_fail (address != NULL);
Packit ae235b
Packit ae235b
  data = g_new0 (GetStreamData, 1);
Packit ae235b
  data->address = g_strdup (address);
Packit ae235b
Packit ae235b
  task = g_task_new (NULL, cancellable, callback, user_data);
Packit ae235b
  g_task_set_source_tag (task, g_dbus_address_get_stream);
Packit ae235b
  g_task_set_task_data (task, data, (GDestroyNotify) get_stream_data_free);
Packit ae235b
  g_task_run_in_thread (task, get_stream_thread_func);
Packit ae235b
  g_object_unref (task);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_dbus_address_get_stream_finish:
Packit ae235b
 * @res: A #GAsyncResult obtained from the GAsyncReadyCallback passed to g_dbus_address_get_stream().
Packit ae235b
 * @out_guid: (optional) (out): %NULL or return location to store the GUID extracted from @address, if any.
Packit ae235b
 * @error: Return location for error or %NULL.
Packit ae235b
 *
Packit ae235b
 * Finishes an operation started with g_dbus_address_get_stream().
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): A #GIOStream or %NULL if @error is set.
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
GIOStream *
Packit ae235b
g_dbus_address_get_stream_finish (GAsyncResult        *res,
Packit ae235b
                                  gchar              **out_guid,
Packit ae235b
                                  GError             **error)
Packit ae235b
{
Packit ae235b
  GTask *task;
Packit ae235b
  GetStreamData *data;
Packit ae235b
  GIOStream *ret;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (g_task_is_valid (res, NULL), NULL);
Packit ae235b
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
Packit ae235b
Packit ae235b
  task = G_TASK (res);
Packit ae235b
  ret = g_task_propagate_pointer (task, error);
Packit ae235b
Packit ae235b
  if (ret != NULL && out_guid != NULL)
Packit ae235b
    {
Packit ae235b
      data = g_task_get_task_data (task);
Packit ae235b
      *out_guid = data->guid;
Packit ae235b
      data->guid = NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return ret;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_dbus_address_get_stream_sync:
Packit ae235b
 * @address: A valid D-Bus address.
Packit ae235b
 * @out_guid: (optional) (out): %NULL or return location to store the GUID extracted from @address, if any.
Packit ae235b
 * @cancellable: (nullable): A #GCancellable or %NULL.
Packit ae235b
 * @error: Return location for error or %NULL.
Packit ae235b
 *
Packit ae235b
 * Synchronously connects to an endpoint specified by @address and
Packit ae235b
 * sets up the connection so it is in a state to run the client-side
Packit ae235b
 * of the D-Bus authentication conversation. @address must be in the
Packit ae235b
 * [D-Bus address format](https://dbus.freedesktop.org/doc/dbus-specification.html#addresses).
Packit ae235b
 *
Packit ae235b
 * This is a synchronous failable function. See
Packit ae235b
 * g_dbus_address_get_stream() for the asynchronous version.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): A #GIOStream or %NULL if @error is set.
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
GIOStream *
Packit ae235b
g_dbus_address_get_stream_sync (const gchar   *address,
Packit ae235b
                                gchar        **out_guid,
Packit ae235b
                                GCancellable  *cancellable,
Packit ae235b
                                GError       **error)
Packit ae235b
{
Packit ae235b
  GIOStream *ret;
Packit ae235b
  gchar **addr_array;
Packit ae235b
  guint n;
Packit ae235b
  GError *last_error;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (address != NULL, NULL);
Packit ae235b
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
Packit ae235b
Packit ae235b
  ret = NULL;
Packit ae235b
  last_error = NULL;
Packit ae235b
Packit ae235b
  addr_array = g_strsplit (address, ";", 0);
Packit ae235b
  if (addr_array != NULL && addr_array[0] == NULL)
Packit ae235b
    {
Packit ae235b
      last_error = g_error_new_literal (G_IO_ERROR,
Packit ae235b
                                        G_IO_ERROR_INVALID_ARGUMENT,
Packit ae235b
                                        _("The given address is empty"));
Packit ae235b
      goto out;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  for (n = 0; addr_array != NULL && addr_array[n] != NULL; n++)
Packit ae235b
    {
Packit ae235b
      const gchar *addr = addr_array[n];
Packit ae235b
      GError *this_error;
Packit ae235b
Packit ae235b
      this_error = NULL;
Packit ae235b
      ret = g_dbus_address_try_connect_one (addr,
Packit ae235b
                                            out_guid,
Packit ae235b
                                            cancellable,
Packit ae235b
                                            &this_error);
Packit ae235b
      if (ret != NULL)
Packit ae235b
        {
Packit ae235b
          goto out;
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          g_assert (this_error != NULL);
Packit ae235b
          if (last_error != NULL)
Packit ae235b
            g_error_free (last_error);
Packit ae235b
          last_error = this_error;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
 out:
Packit ae235b
  if (ret != NULL)
Packit ae235b
    {
Packit ae235b
      if (last_error != NULL)
Packit ae235b
        g_error_free (last_error);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      g_assert (last_error != NULL);
Packit ae235b
      g_propagate_error (error, last_error);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_strfreev (addr_array);
Packit ae235b
  return ret;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * Return the address of XDG_RUNTIME_DIR/bus if it exists, belongs to
Packit ae235b
 * us, and is a socket, and we are on Unix.
Packit ae235b
 */
Packit ae235b
static gchar *
Packit ae235b
get_session_address_xdg (void)
Packit ae235b
{
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
  gchar *ret = NULL;
Packit ae235b
  gchar *bus;
Packit ae235b
  gchar *tmp;
Packit ae235b
  GStatBuf buf;
Packit ae235b
Packit ae235b
  bus = g_build_filename (g_get_user_runtime_dir (), "bus", NULL);
Packit ae235b
Packit ae235b
  /* if ENOENT, EPERM, etc., quietly don't use it */
Packit ae235b
  if (g_stat (bus, &buf) < 0)
Packit ae235b
    goto out;
Packit ae235b
Packit ae235b
  /* if it isn't ours, we have incorrectly inherited someone else's
Packit ae235b
   * XDG_RUNTIME_DIR; silently don't use it
Packit ae235b
   */
Packit ae235b
  if (buf.st_uid != geteuid ())
Packit ae235b
    goto out;
Packit ae235b
Packit ae235b
  /* if it isn't a socket, silently don't use it */
Packit ae235b
  if ((buf.st_mode & S_IFMT) != S_IFSOCK)
Packit ae235b
    goto out;
Packit ae235b
Packit ae235b
  tmp = g_dbus_address_escape_value (bus);
Packit ae235b
  ret = g_strconcat ("unix:path=", tmp, NULL);
Packit ae235b
  g_free (tmp);
Packit ae235b
Packit ae235b
out:
Packit ae235b
  g_free (bus);
Packit ae235b
  return ret;
Packit ae235b
#else
Packit ae235b
  return NULL;
Packit ae235b
#endif
Packit ae235b
}
Packit ae235b
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
static gchar *
Packit ae235b
get_session_address_dbus_launch (GError **error)
Packit ae235b
{
Packit ae235b
  gchar *ret;
Packit ae235b
  gchar *machine_id;
Packit ae235b
  gchar *command_line;
Packit ae235b
  gchar *launch_stdout;
Packit ae235b
  gchar *launch_stderr;
Packit ae235b
  gint exit_status;
Packit ae235b
  gchar *old_dbus_verbose;
Packit ae235b
  gboolean restore_dbus_verbose;
Packit ae235b
Packit ae235b
  ret = NULL;
Packit ae235b
  machine_id = NULL;
Packit ae235b
  command_line = NULL;
Packit ae235b
  launch_stdout = NULL;
Packit ae235b
  launch_stderr = NULL;
Packit ae235b
  restore_dbus_verbose = FALSE;
Packit ae235b
  old_dbus_verbose = NULL;
Packit ae235b
Packit ae235b
  /* Don't run binaries as root if we're setuid. */
Packit ae235b
  if (GLIB_PRIVATE_CALL (g_check_setuid) ())
Packit ae235b
    {
Packit ae235b
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
Packit ae235b
		   _("Cannot spawn a message bus when setuid"));
Packit ae235b
      goto out;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  machine_id = _g_dbus_get_machine_id (error);
Packit ae235b
  if (machine_id == NULL)
Packit ae235b
    {
Packit ae235b
      g_prefix_error (error, _("Cannot spawn a message bus without a machine-id: "));
Packit ae235b
      goto out;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (g_getenv ("DISPLAY") == NULL)
Packit ae235b
    {
Packit ae235b
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
Packit ae235b
                   _("Cannot autolaunch D-Bus without X11 $DISPLAY"));
Packit ae235b
      goto out;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* We're using private libdbus facilities here. When everything
Packit ae235b
   * (X11, Mac OS X, Windows) is spec'ed out correctly (not even the
Packit ae235b
   * X11 property is correctly documented right now) we should
Packit ae235b
   * consider using the spec instead of dbus-launch.
Packit ae235b
   *
Packit ae235b
   *   --autolaunch=MACHINEID
Packit ae235b
   *          This option implies that dbus-launch should scan  for  a  previ‐
Packit ae235b
   *          ously-started  session  and  reuse the values found there. If no
Packit ae235b
   *          session is found, it will start a new session. The  --exit-with-
Packit ae235b
   *          session option is implied if --autolaunch is given.  This option
Packit ae235b
   *          is for the exclusive use of libdbus, you do not want to  use  it
Packit ae235b
   *          manually. It may change in the future.
Packit ae235b
   */
Packit ae235b
Packit ae235b
  /* TODO: maybe provide a variable for where to look for the dbus-launch binary? */
Packit ae235b
  command_line = g_strdup_printf ("dbus-launch --autolaunch=%s --binary-syntax --close-stderr", machine_id);
Packit ae235b
Packit ae235b
  if (G_UNLIKELY (_g_dbus_debug_address ()))
Packit ae235b
    {
Packit ae235b
      _g_dbus_debug_print_lock ();
Packit ae235b
      g_print ("GDBus-debug:Address: Running '%s' to get bus address (possibly autolaunching)\n", command_line);
Packit ae235b
      old_dbus_verbose = g_strdup (g_getenv ("DBUS_VERBOSE"));
Packit ae235b
      restore_dbus_verbose = TRUE;
Packit ae235b
      g_setenv ("DBUS_VERBOSE", "1", TRUE);
Packit ae235b
      _g_dbus_debug_print_unlock ();
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (!g_spawn_command_line_sync (command_line,
Packit ae235b
                                  &launch_stdout,
Packit ae235b
                                  &launch_stderr,
Packit ae235b
                                  &exit_status,
Packit ae235b
                                  error))
Packit ae235b
    {
Packit ae235b
      goto out;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (!g_spawn_check_exit_status (exit_status, error))
Packit ae235b
    {
Packit ae235b
      g_prefix_error (error, _("Error spawning command line “%s”: "), command_line);
Packit ae235b
      goto out;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* From the dbus-launch(1) man page:
Packit ae235b
   *
Packit ae235b
   *   --binary-syntax Write to stdout a nul-terminated bus address,
Packit ae235b
   *   then the bus PID as a binary integer of size sizeof(pid_t),
Packit ae235b
   *   then the bus X window ID as a binary integer of size
Packit ae235b
   *   sizeof(long).  Integers are in the machine's byte order, not
Packit ae235b
   *   network byte order or any other canonical byte order.
Packit ae235b
   */
Packit ae235b
  ret = g_strdup (launch_stdout);
Packit ae235b
Packit ae235b
 out:
Packit ae235b
  if (G_UNLIKELY (_g_dbus_debug_address ()))
Packit ae235b
    {
Packit ae235b
      gchar *s;
Packit ae235b
      _g_dbus_debug_print_lock ();
Packit ae235b
      g_print ("GDBus-debug:Address: dbus-launch output:");
Packit ae235b
      if (launch_stdout != NULL)
Packit ae235b
        {
Packit ae235b
          s = _g_dbus_hexdump (launch_stdout, strlen (launch_stdout) + 1 + sizeof (pid_t) + sizeof (long), 2);
Packit ae235b
          g_print ("\n%s", s);
Packit ae235b
          g_free (s);
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          g_print (" (none)\n");
Packit ae235b
        }
Packit ae235b
      g_print ("GDBus-debug:Address: dbus-launch stderr output:");
Packit ae235b
      if (launch_stderr != NULL)
Packit ae235b
        g_print ("\n%s", launch_stderr);
Packit ae235b
      else
Packit ae235b
        g_print (" (none)\n");
Packit ae235b
      _g_dbus_debug_print_unlock ();
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_free (machine_id);
Packit ae235b
  g_free (command_line);
Packit ae235b
  g_free (launch_stdout);
Packit ae235b
  g_free (launch_stderr);
Packit ae235b
  if (G_UNLIKELY (restore_dbus_verbose))
Packit ae235b
    {
Packit ae235b
      if (old_dbus_verbose != NULL)
Packit ae235b
        g_setenv ("DBUS_VERBOSE", old_dbus_verbose, TRUE);
Packit ae235b
      else
Packit ae235b
        g_unsetenv ("DBUS_VERBOSE");
Packit ae235b
    }
Packit ae235b
  g_free (old_dbus_verbose);
Packit ae235b
  return ret;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* end of G_OS_UNIX case */
Packit ae235b
#elif defined(G_OS_WIN32)
Packit ae235b
Packit ae235b
#define DBUS_DAEMON_ADDRESS_INFO "DBusDaemonAddressInfo"
Packit ae235b
#define DBUS_DAEMON_MUTEX "DBusDaemonMutex"
Packit ae235b
#define UNIQUE_DBUS_INIT_MUTEX "UniqueDBusInitMutex"
Packit ae235b
#define DBUS_AUTOLAUNCH_MUTEX "DBusAutolaunchMutex"
Packit ae235b
Packit ae235b
static void
Packit ae235b
release_mutex (HANDLE mutex)
Packit ae235b
{
Packit ae235b
  ReleaseMutex (mutex);
Packit ae235b
  CloseHandle (mutex);
Packit ae235b
}
Packit ae235b
Packit ae235b
static HANDLE
Packit ae235b
acquire_mutex (const char *mutexname)
Packit ae235b
{
Packit ae235b
  HANDLE mutex;
Packit ae235b
  DWORD res;
Packit ae235b
Packit ae235b
  mutex = CreateMutexA (NULL, FALSE, mutexname);
Packit ae235b
  if (!mutex)
Packit ae235b
    return 0;
Packit ae235b
Packit ae235b
  res = WaitForSingleObject (mutex, INFINITE);
Packit ae235b
  switch (res)
Packit ae235b
    {
Packit ae235b
    case WAIT_ABANDONED:
Packit ae235b
      release_mutex (mutex);
Packit ae235b
      return 0;
Packit ae235b
    case WAIT_FAILED:
Packit ae235b
    case WAIT_TIMEOUT:
Packit ae235b
      return 0;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return mutex;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
is_mutex_owned (const char *mutexname)
Packit ae235b
{
Packit ae235b
  HANDLE mutex;
Packit ae235b
  gboolean res = FALSE;
Packit ae235b
Packit ae235b
  mutex = CreateMutexA (NULL, FALSE, mutexname);
Packit ae235b
  if (WaitForSingleObject (mutex, 10) == WAIT_TIMEOUT)
Packit ae235b
    res = TRUE;
Packit ae235b
  else
Packit ae235b
    ReleaseMutex (mutex);
Packit ae235b
  CloseHandle (mutex);
Packit ae235b
Packit ae235b
  return res;
Packit ae235b
}
Packit ae235b
Packit ae235b
static char *
Packit ae235b
read_shm (const char *shm_name)
Packit ae235b
{
Packit ae235b
  HANDLE shared_mem;
Packit ae235b
  char *shared_data;
Packit ae235b
  char *res;
Packit ae235b
  int i;
Packit ae235b
Packit ae235b
  res = NULL;
Packit ae235b
Packit ae235b
  for (i = 0; i < 20; i++)
Packit ae235b
    {
Packit ae235b
      shared_mem = OpenFileMappingA (FILE_MAP_READ, FALSE, shm_name);
Packit ae235b
      if (shared_mem != 0)
Packit ae235b
	break;
Packit ae235b
      Sleep (100);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (shared_mem != 0)
Packit ae235b
    {
Packit ae235b
      shared_data = MapViewOfFile (shared_mem, FILE_MAP_READ, 0, 0, 0);
Packit ae235b
      if (shared_data != NULL)
Packit ae235b
	{
Packit ae235b
	  res = g_strdup (shared_data);
Packit ae235b
	  UnmapViewOfFile (shared_data);
Packit ae235b
	}
Packit ae235b
      CloseHandle (shared_mem);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return res;
Packit ae235b
}
Packit ae235b
Packit ae235b
static HANDLE
Packit ae235b
set_shm (const char *shm_name, const char *value)
Packit ae235b
{
Packit ae235b
  HANDLE shared_mem;
Packit ae235b
  char *shared_data;
Packit ae235b
Packit ae235b
  shared_mem = CreateFileMappingA (INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE,
Packit ae235b
				   0, strlen (value) + 1, shm_name);
Packit ae235b
  if (shared_mem == 0)
Packit ae235b
    return 0;
Packit ae235b
Packit ae235b
  shared_data = MapViewOfFile (shared_mem, FILE_MAP_WRITE, 0, 0, 0 );
Packit ae235b
  if (shared_data == NULL)
Packit ae235b
    return 0;
Packit ae235b
Packit ae235b
  strcpy (shared_data, value);
Packit ae235b
Packit ae235b
  UnmapViewOfFile (shared_data);
Packit ae235b
Packit ae235b
  return shared_mem;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* These keep state between publish_session_bus and unpublish_session_bus */
Packit ae235b
static HANDLE published_daemon_mutex;
Packit ae235b
static HANDLE published_shared_mem;
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
publish_session_bus (const char *address)
Packit ae235b
{
Packit ae235b
  HANDLE init_mutex;
Packit ae235b
Packit ae235b
  init_mutex = acquire_mutex (UNIQUE_DBUS_INIT_MUTEX);
Packit ae235b
Packit ae235b
  published_daemon_mutex = CreateMutexA (NULL, FALSE, DBUS_DAEMON_MUTEX);
Packit ae235b
  if (WaitForSingleObject (published_daemon_mutex, 10 ) != WAIT_OBJECT_0)
Packit ae235b
    {
Packit ae235b
      release_mutex (init_mutex);
Packit ae235b
      CloseHandle (published_daemon_mutex);
Packit ae235b
      published_daemon_mutex = NULL;
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  published_shared_mem = set_shm (DBUS_DAEMON_ADDRESS_INFO, address);
Packit ae235b
  if (!published_shared_mem)
Packit ae235b
    {
Packit ae235b
      release_mutex (init_mutex);
Packit ae235b
      CloseHandle (published_daemon_mutex);
Packit ae235b
      published_daemon_mutex = NULL;
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  release_mutex (init_mutex);
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
unpublish_session_bus (void)
Packit ae235b
{
Packit ae235b
  HANDLE init_mutex;
Packit ae235b
Packit ae235b
  init_mutex = acquire_mutex (UNIQUE_DBUS_INIT_MUTEX);
Packit ae235b
Packit ae235b
  CloseHandle (published_shared_mem);
Packit ae235b
  published_shared_mem = NULL;
Packit ae235b
Packit ae235b
  release_mutex (published_daemon_mutex);
Packit ae235b
  published_daemon_mutex = NULL;
Packit ae235b
Packit ae235b
  release_mutex (init_mutex);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
wait_console_window (void)
Packit ae235b
{
Packit ae235b
  FILE *console = fopen ("CONOUT$", "w");
Packit ae235b
Packit ae235b
  SetConsoleTitleW (L"gdbus-daemon output. Type any character to close this window.");
Packit ae235b
  fprintf (console, _("(Type any character to close this window)\n"));
Packit ae235b
  fflush (console);
Packit ae235b
  _getch ();
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
open_console_window (void)
Packit ae235b
{
Packit ae235b
  if (((HANDLE) _get_osfhandle (fileno (stdout)) == INVALID_HANDLE_VALUE ||
Packit ae235b
       (HANDLE) _get_osfhandle (fileno (stderr)) == INVALID_HANDLE_VALUE) && AllocConsole ())
Packit ae235b
    {
Packit ae235b
      if ((HANDLE) _get_osfhandle (fileno (stdout)) == INVALID_HANDLE_VALUE)
Packit ae235b
        freopen ("CONOUT$", "w", stdout);
Packit ae235b
Packit ae235b
      if ((HANDLE) _get_osfhandle (fileno (stderr)) == INVALID_HANDLE_VALUE)
Packit ae235b
        freopen ("CONOUT$", "w", stderr);
Packit ae235b
Packit ae235b
      SetConsoleTitleW (L"gdbus-daemon debug output.");
Packit ae235b
Packit ae235b
      atexit (wait_console_window);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
idle_timeout_cb (GDBusDaemon *daemon, gpointer user_data)
Packit ae235b
{
Packit ae235b
  GMainLoop *loop = user_data;
Packit ae235b
  g_main_loop_quit (loop);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* Satisfies STARTF_FORCEONFEEDBACK */
Packit ae235b
static void
Packit ae235b
turn_off_the_starting_cursor (void)
Packit ae235b
{
Packit ae235b
  MSG msg;
Packit ae235b
  BOOL bRet;
Packit ae235b
Packit ae235b
  PostQuitMessage (0);
Packit ae235b
Packit ae235b
  while ((bRet = GetMessage (&msg, 0, 0, 0)) != 0)
Packit ae235b
    {
Packit ae235b
      if (bRet == -1)
Packit ae235b
        continue;
Packit ae235b
Packit ae235b
      TranslateMessage (&msg;;
Packit ae235b
      DispatchMessage (&msg;;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
__declspec(dllexport) void CALLBACK g_win32_run_session_bus (HWND hwnd, HINSTANCE hinst, char *cmdline, int nCmdShow);
Packit ae235b
Packit ae235b
__declspec(dllexport) void CALLBACK
Packit ae235b
g_win32_run_session_bus (HWND hwnd, HINSTANCE hinst, char *cmdline, int nCmdShow)
Packit ae235b
{
Packit ae235b
  GDBusDaemon *daemon;
Packit ae235b
  GMainLoop *loop;
Packit ae235b
  const char *address;
Packit ae235b
  GError *error = NULL;
Packit ae235b
Packit ae235b
  turn_off_the_starting_cursor ();
Packit ae235b
Packit ae235b
  if (g_getenv ("GDBUS_DAEMON_DEBUG") != NULL)
Packit ae235b
    open_console_window ();
Packit ae235b
Packit ae235b
  loop = g_main_loop_new (NULL, FALSE);
Packit ae235b
Packit ae235b
  address = "nonce-tcp:";
Packit ae235b
  daemon = _g_dbus_daemon_new (address, NULL, &error);
Packit ae235b
  if (daemon == NULL)
Packit ae235b
    {
Packit ae235b
      g_printerr ("Can't init bus: %s\n", error->message);
Packit ae235b
      g_error_free (error);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_signal_connect (daemon, "idle-timeout", G_CALLBACK (idle_timeout_cb), loop);
Packit ae235b
Packit ae235b
  if (publish_session_bus (_g_dbus_daemon_get_address (daemon)))
Packit ae235b
    {
Packit ae235b
      g_main_loop_run (loop);
Packit ae235b
Packit ae235b
      unpublish_session_bus ();
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_main_loop_unref (loop);
Packit ae235b
  g_object_unref (daemon);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gchar *
Packit ae235b
get_session_address_dbus_launch (GError **error)
Packit ae235b
{
Packit ae235b
  HANDLE autolaunch_mutex, init_mutex;
Packit ae235b
  char *address = NULL;
Packit ae235b
  wchar_t gio_path[MAX_PATH+1+200];
Packit ae235b
Packit ae235b
  autolaunch_mutex = acquire_mutex (DBUS_AUTOLAUNCH_MUTEX);
Packit ae235b
Packit ae235b
  init_mutex = acquire_mutex (UNIQUE_DBUS_INIT_MUTEX);
Packit ae235b
Packit ae235b
  if (is_mutex_owned (DBUS_DAEMON_MUTEX))
Packit ae235b
    address = read_shm (DBUS_DAEMON_ADDRESS_INFO);
Packit ae235b
Packit ae235b
  release_mutex (init_mutex);
Packit ae235b
Packit ae235b
  if (address == NULL)
Packit ae235b
    {
Packit ae235b
      gio_path[MAX_PATH] = 0;
Packit ae235b
      if (GetModuleFileNameW (_g_io_win32_get_module (), gio_path, MAX_PATH))
Packit ae235b
	{
Packit ae235b
	  PROCESS_INFORMATION pi = { 0 };
Packit ae235b
	  STARTUPINFOW si = { 0 };
Packit ae235b
	  BOOL res;
Packit ae235b
	  wchar_t gio_path_short[MAX_PATH];
Packit ae235b
	  wchar_t rundll_path[MAX_PATH*2];
Packit ae235b
	  wchar_t args[MAX_PATH*4];
Packit ae235b
Packit ae235b
	  GetShortPathNameW (gio_path, gio_path_short, MAX_PATH);
Packit ae235b
Packit ae235b
	  GetWindowsDirectoryW (rundll_path, MAX_PATH);
Packit ae235b
	  wcscat (rundll_path, L"\\rundll32.exe");
Packit ae235b
	  if (GetFileAttributesW (rundll_path) == INVALID_FILE_ATTRIBUTES)
Packit ae235b
	    {
Packit ae235b
	      GetSystemDirectoryW (rundll_path, MAX_PATH);
Packit ae235b
	      wcscat (rundll_path, L"\\rundll32.exe");
Packit ae235b
	    }
Packit ae235b
Packit ae235b
	  wcscpy (args, L"\"");
Packit ae235b
	  wcscat (args, rundll_path);
Packit ae235b
	  wcscat (args, L"\" ");
Packit ae235b
	  wcscat (args, gio_path_short);
Packit ae235b
#if defined(_WIN64) || defined(_M_X64) || defined(_M_AMD64)
Packit ae235b
	  wcscat (args, L",g_win32_run_session_bus");
Packit ae235b
#elif defined (_MSC_VER)
Packit ae235b
	  wcscat (args, L",_g_win32_run_session_bus@16");
Packit ae235b
#else
Packit ae235b
	  wcscat (args, L",g_win32_run_session_bus@16");
Packit ae235b
#endif
Packit ae235b
Packit ae235b
	  res = CreateProcessW (rundll_path, args,
Packit ae235b
				0, 0, FALSE,
Packit ae235b
				NORMAL_PRIORITY_CLASS | CREATE_NO_WINDOW | DETACHED_PROCESS,
Packit ae235b
				0, NULL /* TODO: Should be root */,
Packit ae235b
				&si, &pi);
Packit ae235b
	  if (res)
Packit ae235b
	    address = read_shm (DBUS_DAEMON_ADDRESS_INFO);
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
Packit ae235b
  release_mutex (autolaunch_mutex);
Packit ae235b
Packit ae235b
  if (address == NULL)
Packit ae235b
    g_set_error (error,
Packit ae235b
		 G_IO_ERROR,
Packit ae235b
		 G_IO_ERROR_FAILED,
Packit ae235b
		 _("Session dbus not running, and autolaunch failed"));
Packit ae235b
Packit ae235b
  return address;
Packit ae235b
}
Packit ae235b
#else /* neither G_OS_UNIX nor G_OS_WIN32 */
Packit ae235b
static gchar *
Packit ae235b
get_session_address_dbus_launch (GError **error)
Packit ae235b
{
Packit ae235b
  g_set_error (error,
Packit ae235b
               G_IO_ERROR,
Packit ae235b
               G_IO_ERROR_FAILED,
Packit ae235b
               _("Cannot determine session bus address (not implemented for this OS)"));
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
#endif /* neither G_OS_UNIX nor G_OS_WIN32 */
Packit ae235b
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
Packit ae235b
static gchar *
Packit ae235b
get_session_address_platform_specific (GError **error)
Packit ae235b
{
Packit ae235b
  gchar *ret;
Packit ae235b
Packit ae235b
  /* Use XDG_RUNTIME_DIR/bus if it exists and is suitable. This is appropriate
Packit ae235b
   * for systems using the "a session is a user-session" model described in
Packit ae235b
   * <http://lists.freedesktop.org/archives/dbus/2015-January/016522.html>,
Packit ae235b
   * and implemented in dbus >= 1.9.14 and sd-bus.
Packit ae235b
   *
Packit ae235b
   * On systems following the more traditional "a session is a login-session"
Packit ae235b
   * model, this will fail and we'll fall through to X11 autolaunching
Packit ae235b
   * (dbus-launch) below.
Packit ae235b
   */
Packit ae235b
  ret = get_session_address_xdg ();
Packit ae235b
Packit ae235b
  if (ret != NULL)
Packit ae235b
    return ret;
Packit ae235b
Packit ae235b
  /* TODO (#694472): try launchd on OS X, like
Packit ae235b
   * _dbus_lookup_session_address_launchd() does, since
Packit ae235b
   * 'dbus-launch --autolaunch' probably won't work there
Packit ae235b
   */
Packit ae235b
Packit ae235b
  /* As a last resort, try the "autolaunch:" transport. On Unix this means
Packit ae235b
   * X11 autolaunching; on Windows this means a different autolaunching
Packit ae235b
   * mechanism based on shared memory.
Packit ae235b
   */
Packit ae235b
  return get_session_address_dbus_launch (error);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_dbus_address_get_for_bus_sync:
Packit ae235b
 * @bus_type: a #GBusType
Packit ae235b
 * @cancellable: (nullable): a #GCancellable or %NULL
Packit ae235b
 * @error: return location for error or %NULL
Packit ae235b
 *
Packit ae235b
 * Synchronously looks up the D-Bus address for the well-known message
Packit ae235b
 * bus instance specified by @bus_type. This may involve using various
Packit ae235b
 * platform specific mechanisms.
Packit ae235b
 *
Packit ae235b
 * The returned address will be in the
Packit ae235b
 * [D-Bus address format](https://dbus.freedesktop.org/doc/dbus-specification.html#addresses).
Packit ae235b
 *
Packit ae235b
 * Returns: a valid D-Bus address string for @bus_type or %NULL if
Packit ae235b
 *     @error is set
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 */
Packit ae235b
gchar *
Packit ae235b
g_dbus_address_get_for_bus_sync (GBusType       bus_type,
Packit ae235b
                                 GCancellable  *cancellable,
Packit ae235b
                                 GError       **error)
Packit ae235b
{
Packit ae235b
  gchar *ret, *s = NULL;
Packit ae235b
  const gchar *starter_bus;
Packit ae235b
  GError *local_error;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
Packit ae235b
Packit ae235b
  ret = NULL;
Packit ae235b
  local_error = NULL;
Packit ae235b
Packit ae235b
  if (G_UNLIKELY (_g_dbus_debug_address ()))
Packit ae235b
    {
Packit ae235b
      guint n;
Packit ae235b
      _g_dbus_debug_print_lock ();
Packit ae235b
      s = _g_dbus_enum_to_string (G_TYPE_BUS_TYPE, bus_type);
Packit ae235b
      g_print ("GDBus-debug:Address: In g_dbus_address_get_for_bus_sync() for bus type '%s'\n",
Packit ae235b
               s);
Packit ae235b
      g_free (s);
Packit ae235b
      for (n = 0; n < 3; n++)
Packit ae235b
        {
Packit ae235b
          const gchar *k;
Packit ae235b
          const gchar *v;
Packit ae235b
          switch (n)
Packit ae235b
            {
Packit ae235b
            case 0: k = "DBUS_SESSION_BUS_ADDRESS"; break;
Packit ae235b
            case 1: k = "DBUS_SYSTEM_BUS_ADDRESS"; break;
Packit ae235b
            case 2: k = "DBUS_STARTER_BUS_TYPE"; break;
Packit ae235b
            default: g_assert_not_reached ();
Packit ae235b
            }
Packit ae235b
          v = g_getenv (k);
Packit ae235b
          g_print ("GDBus-debug:Address: env var %s", k);
Packit ae235b
          if (v != NULL)
Packit ae235b
            g_print ("='%s'\n", v);
Packit ae235b
          else
Packit ae235b
            g_print (" is not set\n");
Packit ae235b
        }
Packit ae235b
      _g_dbus_debug_print_unlock ();
Packit ae235b
    }
Packit ae235b
Packit ae235b
  switch (bus_type)
Packit ae235b
    {
Packit ae235b
    case G_BUS_TYPE_SYSTEM:
Packit ae235b
      ret = g_strdup (g_getenv ("DBUS_SYSTEM_BUS_ADDRESS"));
Packit ae235b
      if (ret == NULL)
Packit ae235b
        {
Packit ae235b
          ret = g_strdup ("unix:path=/var/run/dbus/system_bus_socket");
Packit ae235b
        }
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case G_BUS_TYPE_SESSION:
Packit ae235b
      ret = g_strdup (g_getenv ("DBUS_SESSION_BUS_ADDRESS"));
Packit ae235b
      if (ret == NULL)
Packit ae235b
        {
Packit ae235b
          ret = get_session_address_platform_specific (&local_error);
Packit ae235b
        }
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    case G_BUS_TYPE_STARTER:
Packit ae235b
      starter_bus = g_getenv ("DBUS_STARTER_BUS_TYPE");
Packit ae235b
      if (g_strcmp0 (starter_bus, "session") == 0)
Packit ae235b
        {
Packit ae235b
          ret = g_dbus_address_get_for_bus_sync (G_BUS_TYPE_SESSION, cancellable, &local_error);
Packit ae235b
          goto out;
Packit ae235b
        }
Packit ae235b
      else if (g_strcmp0 (starter_bus, "system") == 0)
Packit ae235b
        {
Packit ae235b
          ret = g_dbus_address_get_for_bus_sync (G_BUS_TYPE_SYSTEM, cancellable, &local_error);
Packit ae235b
          goto out;
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          if (starter_bus != NULL)
Packit ae235b
            {
Packit ae235b
              g_set_error (&local_error,
Packit ae235b
                           G_IO_ERROR,
Packit ae235b
                           G_IO_ERROR_FAILED,
Packit ae235b
                           _("Cannot determine bus address from DBUS_STARTER_BUS_TYPE environment variable"
Packit ae235b
                             " — unknown value “%s”"),
Packit ae235b
                           starter_bus);
Packit ae235b
            }
Packit ae235b
          else
Packit ae235b
            {
Packit ae235b
              g_set_error_literal (&local_error,
Packit ae235b
                                   G_IO_ERROR,
Packit ae235b
                                   G_IO_ERROR_FAILED,
Packit ae235b
                                   _("Cannot determine bus address because the DBUS_STARTER_BUS_TYPE environment "
Packit ae235b
                                     "variable is not set"));
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
      break;
Packit ae235b
Packit ae235b
    default:
Packit ae235b
      g_set_error (&local_error,
Packit ae235b
                   G_IO_ERROR,
Packit ae235b
                   G_IO_ERROR_FAILED,
Packit ae235b
                   _("Unknown bus type %d"),
Packit ae235b
                   bus_type);
Packit ae235b
      break;
Packit ae235b
    }
Packit ae235b
Packit ae235b
 out:
Packit ae235b
  if (G_UNLIKELY (_g_dbus_debug_address ()))
Packit ae235b
    {
Packit ae235b
      _g_dbus_debug_print_lock ();
Packit ae235b
      s = _g_dbus_enum_to_string (G_TYPE_BUS_TYPE, bus_type);
Packit ae235b
      if (ret != NULL)
Packit ae235b
        {
Packit ae235b
          g_print ("GDBus-debug:Address: Returning address '%s' for bus type '%s'\n",
Packit ae235b
                   ret, s);
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          g_print ("GDBus-debug:Address: Cannot look-up address bus type '%s': %s\n",
Packit ae235b
                   s, local_error ? local_error->message : "");
Packit ae235b
        }
Packit ae235b
      g_free (s);
Packit ae235b
      _g_dbus_debug_print_unlock ();
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (local_error != NULL)
Packit ae235b
    g_propagate_error (error, local_error);
Packit ae235b
Packit ae235b
  return ret;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_dbus_address_escape_value:
Packit ae235b
 * @string: an unescaped string to be included in a D-Bus address
Packit ae235b
 *     as the value in a key-value pair
Packit ae235b
 *
Packit ae235b
 * Escape @string so it can appear in a D-Bus address as the value
Packit ae235b
 * part of a key-value pair.
Packit ae235b
 *
Packit ae235b
 * For instance, if @string is `/run/bus-for-:0`,
Packit ae235b
 * this function would return `/run/bus-for-%3A0`,
Packit ae235b
 * which could be used in a D-Bus address like
Packit ae235b
 * `unix:nonce-tcp:host=127.0.0.1,port=42,noncefile=/run/bus-for-%3A0`.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): a copy of @string with all
Packit ae235b
 *     non-optionally-escaped bytes escaped
Packit ae235b
 *
Packit ae235b
 * Since: 2.36
Packit ae235b
 */
Packit ae235b
gchar *
Packit ae235b
g_dbus_address_escape_value (const gchar *string)
Packit ae235b
{
Packit ae235b
  GString *s;
Packit ae235b
  gsize i;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (string != NULL, NULL);
Packit ae235b
Packit ae235b
  /* There will often not be anything needing escaping at all. */
Packit ae235b
  s = g_string_sized_new (strlen (string));
Packit ae235b
Packit ae235b
  /* D-Bus address escaping is mostly the same as URI escaping... */
Packit ae235b
  g_string_append_uri_escaped (s, string, "\\/", FALSE);
Packit ae235b
Packit ae235b
  /* ... but '~' is an unreserved character in URIs, but a
Packit ae235b
   * non-optionally-escaped character in D-Bus addresses. */
Packit ae235b
  for (i = 0; i < s->len; i++)
Packit ae235b
    {
Packit ae235b
      if (G_UNLIKELY (s->str[i] == '~'))
Packit ae235b
        {
Packit ae235b
          s->str[i] = '%';
Packit ae235b
          g_string_insert (s, i + 1, "7E");
Packit ae235b
          i += 2;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return g_string_free (s, FALSE);
Packit ae235b
}