[kdbus] Integrate kdbus core with new API.
[platform/upstream/glib.git] / gio / gdbusaddress.c
index 349e1b7..0f59a85 100644 (file)
@@ -42,6 +42,7 @@
 
 #ifdef G_OS_UNIX
 #include <gio/gunixsocketaddress.h>
+#include <gio/gkdbusconnection.h>
 #endif
 
 #ifdef G_OS_WIN32
@@ -60,7 +61,7 @@
  *
  * Routines for working with D-Bus addresses. A D-Bus address is a string
  * like "unix:tmpdir=/tmp/my-app-name". The exact format of addresses
- * is explained in detail in the <link linkend="http://dbus.freedesktop.org/doc/dbus-specification.html&num;addresses">D-Bus specification</link>.
+ * is explained in detail in the [D-Bus specification](http://dbus.freedesktop.org/doc/dbus-specification.html\#addresses).
  */
 
 static gchar *get_session_address_platform_specific (GError **error);
@@ -358,6 +359,16 @@ is_valid_tcp (const gchar  *address_entry,
   return ret;
 }
 
+static int
+g_dbus_is_supported_address_kdbus (const gchar  *transport_name)
+{
+  int supported = 0;
+
+  supported = g_strcmp0 (transport_name, "kernel") == 0;
+
+  return supported;
+}
+
 /**
  * g_dbus_is_supported_address:
  * @string: A string.
@@ -399,7 +410,8 @@ g_dbus_is_supported_address (const gchar  *string,
         goto out;
 
       supported = FALSE;
-      if (g_strcmp0 (transport_name, "unix") == 0)
+      if ((g_strcmp0 (transport_name, "unix") == 0)
+          || g_dbus_is_supported_address_kdbus (transport_name))
         supported = is_valid_unix (a[n], key_value_pairs, error);
       else if (g_strcmp0 (transport_name, "tcp") == 0)
         supported = is_valid_tcp (a[n], key_value_pairs, error);
@@ -551,7 +563,8 @@ g_dbus_address_connect (const gchar   *address_entry,
     {
     }
 #ifdef G_OS_UNIX
-  else if (g_strcmp0 (transport_name, "unix") == 0)
+  if ((g_strcmp0 (transport_name, "unix") == 0)
+      || g_dbus_is_supported_address_kdbus (transport_name))
     {
       const gchar *path;
       const gchar *abstract;
@@ -662,21 +675,46 @@ g_dbus_address_connect (const gchar   *address_entry,
 
   if (connectable != NULL)
     {
-      GSocketClient *client;
-      GSocketConnection *connection;
 
-      g_assert (ret == NULL);
-      client = g_socket_client_new ();
-      connection = g_socket_client_connect (client,
-                                            connectable,
-                                            cancellable,
-                                            error);
-      g_object_unref (connectable);
-      g_object_unref (client);
-      if (connection == NULL)
-        goto out;
+      if (g_dbus_is_supported_address_kdbus (transport_name))
+        {
+          GKdbusConnection *connection;
+          gboolean status;
+
+          const gchar *path;
+          path = g_hash_table_lookup (key_value_pairs, "path");
 
-      ret = G_IO_STREAM (connection);
+          g_assert (ret == NULL);
+          connection = _g_kdbus_connection_new ();
+          status = _g_kdbus_connection_connect (connection,
+                                                path,
+                                                cancellable,
+                                                error);
+          g_object_unref (connectable);
+
+          if (!status)
+            goto out;
+
+          ret = G_IO_STREAM (connection);
+        }
+      else
+        {
+          GSocketClient *client;
+          GSocketConnection *connection;
+
+          g_assert (ret == NULL);
+          client = g_socket_client_new ();
+          connection = g_socket_client_connect (client,
+                                                connectable,
+                                                cancellable,
+                                                error);
+          g_object_unref (connectable);
+          g_object_unref (client);
+          if (connection == NULL)
+            goto out;
+
+          ret = G_IO_STREAM (connection);
+        }
 
       if (nonce_file != NULL)
         {
@@ -1471,6 +1509,8 @@ g_dbus_address_get_for_bus_sync (GBusType       bus_type,
                                  GError       **error)
 {
   gchar *ret;
+  const gchar *system_bus;
+  const gchar *session_bus;
   const gchar *starter_bus;
   GError *local_error;
 
@@ -1524,6 +1564,23 @@ g_dbus_address_get_for_bus_sync (GBusType       bus_type,
         }
       break;
 
+    case G_BUS_TYPE_MACHINE:
+      system_bus = g_getenv ("DBUS_SYSTEM_BUS_ADDRESS");
+      if (system_bus == NULL)
+        ret = g_strdup ("kernel:path=/dev/kdbus/0-system/bus;unix:path=/var/run/dbus/system_bus_socket");
+      else
+        ret = g_strdup_printf ("kernel:path=/dev/kdbus/0-system/bus;%s", system_bus);
+      break;
+
+    case G_BUS_TYPE_USER:
+      session_bus = g_getenv ("DBUS_SESSION_BUS_ADDRESS");
+      if (session_bus == NULL)
+        ret = g_strdup_printf ("kernel:path=/dev/kdbus/%d-user/bus;%s", getuid(),
+                                   get_session_address_platform_specific (&local_error));
+      else
+        ret = g_strdup_printf ("kernel:path=/dev/kdbus/%d-user/bus;%s", getuid(), session_bus);
+      break;
+
     case G_BUS_TYPE_STARTER:
       starter_bus = g_getenv ("DBUS_STARTER_BUS_TYPE");
       if (g_strcmp0 (starter_bus, "session") == 0)