Merge "Optional autogen.sh flag --enable-kdbus-transport added allowing to compile...
[platform/upstream/dbus.git] / dbus / dbus-bus.c
index 6f81c74..3824b3f 100644 (file)
@@ -3,6 +3,7 @@
  *
  * Copyright (C) 2003  CodeFactory AB
  * Copyright (C) 2003  Red Hat, Inc.
+ * Copyright (C) 2013  Samsung Electronics
  *
  * Licensed under the Academic Free License version 2.1
  * 
 #include "dbus-internals.h"
 #include "dbus-message.h"
 #include "dbus-marshal-validate.h"
+#include "dbus-misc.h"
 #include "dbus-threads-internal.h"
 #include "dbus-connection-internal.h"
 #include "dbus-string.h"
+#ifdef ENABLE_KDBUS_TRANSPORT
+#include "dbus-transport-kdbus.h"
+#include <stdlib.h>
+#include <limits.h>
+#include <stdio.h>
+#endif
 
 /**
  * @defgroup DBusBus Message bus APIs
@@ -95,19 +103,6 @@ static DBusBusType activation_bus_type = DBUS_BUS_STARTER;
 
 static dbus_bool_t initialized = FALSE;
 
-/**
- * Lock for globals in this file
- */
-_DBUS_DEFINE_GLOBAL_LOCK (bus);
-
-/**
- * Global lock covering all BusData on any connection. The bet is
- * that some lock contention is better than more memory
- * for a per-connection lock, but it's tough to imagine it mattering
- * either way.
- */
-_DBUS_DEFINE_GLOBAL_LOCK (bus_datas);
-
 static void
 addresses_shutdown_func (void *data)
 {
@@ -147,6 +142,15 @@ get_from_env (char           **connection_p,
     }
 }
 
+#ifdef ENABLE_KDBUS_TRANSPORT
+void dbus_bus_set_bus_connection_address(DBusBusType address_type, char* address)
+{
+       if(bus_connection_addresses[address_type] != NULL)
+               free(bus_connection_addresses[address_type]);
+       bus_connection_addresses[address_type] = address;
+}
+#endif
+
 static dbus_bool_t
 init_session_address (void)
 {
@@ -306,10 +310,10 @@ init_connections_unlocked (void)
        * the above code will work right
        */
       
-      if (!_dbus_setenv ("DBUS_ACTIVATION_ADDRESS", NULL))
+      if (!dbus_setenv ("DBUS_ACTIVATION_ADDRESS", NULL))
         return FALSE;
 
-      if (!_dbus_setenv ("DBUS_ACTIVATION_BUS_TYPE", NULL))
+      if (!dbus_setenv ("DBUS_ACTIVATION_BUS_TYPE", NULL))
         return FALSE;
       
       if (!_dbus_register_shutdown_func (addresses_shutdown_func,
@@ -330,7 +334,11 @@ bus_data_free (void *data)
   if (bd->is_well_known)
     {
       int i;
-      _DBUS_LOCK (bus);
+
+      if (!_DBUS_LOCK (bus))
+        _dbus_assert_not_reached ("global locks should have been initialized "
+            "when we attached bus data");
+
       /* We may be stored in more than one slot */
       /* This should now be impossible - these slots are supposed to
        * be cleared on disconnect, so should not need to be cleared on
@@ -401,8 +409,13 @@ void
 _dbus_bus_notify_shared_connection_disconnected_unlocked (DBusConnection *connection)
 {
   int i;
-  
-  _DBUS_LOCK (bus);
+
+  if (!_DBUS_LOCK (bus))
+    {
+      /* If it was in bus_connections, we would have initialized global locks
+       * when we added it. So, it can't be. */
+      return;
+    }
 
   /* We are expecting to have the connection saved in only one of these
    * slots, but someone could in a pathological case set system and session
@@ -436,7 +449,12 @@ internal_bus_get (DBusBusType  type,
 
   connection = NULL;
 
-  _DBUS_LOCK (bus);
+  if (!_DBUS_LOCK (bus))
+    {
+      _DBUS_SET_OOM (error);
+      /* do not "goto out", that would try to unlock */
+      return NULL;
+    }
 
   if (!init_connections_unlocked ())
     {
@@ -506,8 +524,10 @@ internal_bus_get (DBusBusType  type,
    */
   dbus_connection_set_exit_on_disconnect (connection,
                                           TRUE);
-  _DBUS_LOCK (bus_datas);
+
+  if (!_DBUS_LOCK (bus_datas))
+    _dbus_assert_not_reached ("global locks were initialized already");
+
   bd = ensure_bus_data (connection);
   _dbus_assert (bd != NULL); /* it should have been created on
                                 register, so OOM not possible */
@@ -554,7 +574,7 @@ out:
  * 
  * @param type bus type
  * @param error address where an error can be returned.
- * @returns a #DBusConnection with new ref
+ * @returns a #DBusConnection with new ref or #NULL on error
  */
 DBusConnection *
 dbus_bus_get (DBusBusType  type,
@@ -649,7 +669,7 @@ dbus_bus_register (DBusConnection *connection,
                    DBusError      *error)
 {
   DBusMessage *message, *reply;
-  char *name;
+  char *name = NULL;
   BusData *bd;
   dbus_bool_t retval;
 
@@ -660,7 +680,12 @@ dbus_bus_register (DBusConnection *connection,
   message = NULL;
   reply = NULL;
 
-  _DBUS_LOCK (bus_datas);
+  if (!_DBUS_LOCK (bus_datas))
+    {
+      _DBUS_SET_OOM (error);
+      /* do not "goto out", that would try to unlock */
+      return FALSE;
+    }
 
   bd = ensure_bus_data (connection);
   if (bd == NULL)
@@ -677,36 +702,35 @@ dbus_bus_register (DBusConnection *connection,
       retval = TRUE;
       goto out;
     }
-  
   message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
-                                          DBUS_PATH_DBUS,
-                                          DBUS_INTERFACE_DBUS,
-                                          "Hello"); 
-
+                                                                                         DBUS_PATH_DBUS,
+                                                                                         DBUS_INTERFACE_DBUS,
+                                                                                         "Hello");
   if (!message)
-    {
-      _DBUS_SET_OOM (error);
-      goto out;
-    }
-  
+       {
+         _DBUS_SET_OOM (error);
+         goto out;
+       }
+
   reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
 
   if (reply == NULL)
-    goto out;
+       goto out;
   else if (dbus_set_error_from_message (error, reply))
-    goto out;
+       goto out;
   else if (!dbus_message_get_args (reply, error,
-                                   DBUS_TYPE_STRING, &name,
-                                   DBUS_TYPE_INVALID))
-    goto out;
-  
+                                                                  DBUS_TYPE_STRING, &name,
+                                                                  DBUS_TYPE_INVALID))
+       goto out;
+
   bd->unique_name = _dbus_strdup (name);
+
   if (bd->unique_name == NULL)
     {
       _DBUS_SET_OOM (error);
       goto out;
     }
-  
+
   retval = TRUE;
   
  out:
@@ -769,8 +793,12 @@ dbus_bus_set_unique_name (DBusConnection *connection,
   _dbus_return_val_if_fail (connection != NULL, FALSE);
   _dbus_return_val_if_fail (unique_name != NULL, FALSE);
 
-  _DBUS_LOCK (bus_datas);
-  
+  if (!_DBUS_LOCK (bus_datas))
+    {
+      /* do not "goto out", that would try to unlock */
+      return FALSE;
+    }
+
   bd = ensure_bus_data (connection);
   if (bd == NULL)
     goto out;
@@ -812,8 +840,13 @@ dbus_bus_get_unique_name (DBusConnection *connection)
 
   _dbus_return_val_if_fail (connection != NULL, NULL);
 
-  _DBUS_LOCK (bus_datas);
-  
+  if (!_DBUS_LOCK (bus_datas))
+    {
+      /* We'd have initialized locks when we gave it its unique name, if it
+       * had one. Don't "goto out", that would try to unlock. */
+      return NULL;
+    }
+
   bd = ensure_bus_data (connection);
   if (bd == NULL)
     goto out;
@@ -1102,65 +1135,63 @@ dbus_bus_request_name (DBusConnection *connection,
                        unsigned int    flags,
                        DBusError      *error)
 {
-  DBusMessage *message, *reply;
-  dbus_uint32_t result;
-
-  _dbus_return_val_if_fail (connection != NULL, 0);
-  _dbus_return_val_if_fail (name != NULL, 0);
-  _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), 0);
-  _dbus_return_val_if_error_is_set (error, 0);
-  
-  message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
-                                          DBUS_PATH_DBUS,
-                                          DBUS_INTERFACE_DBUS,
-                                          "RequestName");
-
-  if (message == NULL)
-    {
-      _DBUS_SET_OOM (error);
-      return -1;
-    }
-  if (!dbus_message_append_args (message,
-                                DBUS_TYPE_STRING, &name,
-                                DBUS_TYPE_UINT32, &flags,
-                                DBUS_TYPE_INVALID))
-    {
-      dbus_message_unref (message);
-      _DBUS_SET_OOM (error);
-      return -1;
-    }
-  
-  reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
-                                                     error);
-  
-  dbus_message_unref (message);
-  
-  if (reply == NULL)
-    {
-      _DBUS_ASSERT_ERROR_IS_SET (error);
-      return -1;
-    }  
-
-  if (dbus_set_error_from_message (error, reply))
-    {
-      _DBUS_ASSERT_ERROR_IS_SET (error);
-      dbus_message_unref (reply);
-      return -1;
-    }
-  
-  if (!dbus_message_get_args (reply, error,
-                              DBUS_TYPE_UINT32, &result,
-                              DBUS_TYPE_INVALID))
-    {
-      _DBUS_ASSERT_ERROR_IS_SET (error);
-      dbus_message_unref (reply);
-      return -1;
-    }
-
-  dbus_message_unref (reply);
-  
-  return result;
+       dbus_uint32_t result;
+       DBusMessage *message, *reply;
+
+       _dbus_return_val_if_fail (connection != NULL, 0);
+       _dbus_return_val_if_fail (name != NULL, 0);
+       _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), 0);
+       _dbus_return_val_if_error_is_set (error, 0);
+
+       message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
+                                                                                         DBUS_PATH_DBUS,
+                                                                                         DBUS_INTERFACE_DBUS,
+                                                                                         "RequestName");
+       if (message == NULL)
+       {
+         _DBUS_SET_OOM (error);
+         return -1;
+       }
+
+       if (!dbus_message_append_args (message,
+                                        DBUS_TYPE_STRING, &name,
+                                        DBUS_TYPE_UINT32, &flags,
+                                        DBUS_TYPE_INVALID))
+       {
+               dbus_message_unref (message);
+               _DBUS_SET_OOM (error);
+               return -1;
+       }
+
+       reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
+                                                                                                                error);
+
+       dbus_message_unref (message);
+
+       if (reply == NULL)
+       {
+               _DBUS_ASSERT_ERROR_IS_SET (error);
+               return -1;
+       }
+
+       if (dbus_set_error_from_message (error, reply))
+       {
+               _DBUS_ASSERT_ERROR_IS_SET (error);
+               dbus_message_unref (reply);
+               return -1;
+       }
+
+       if (!dbus_message_get_args (reply, error,
+                                                                 DBUS_TYPE_UINT32, &result,
+                                                                 DBUS_TYPE_INVALID))
+       {
+               _DBUS_ASSERT_ERROR_IS_SET (error);
+               dbus_message_unref (reply);
+               return -1;
+       }
+
+       dbus_message_unref (reply);
+       return result;
 }
 
 
@@ -1515,32 +1546,32 @@ dbus_bus_add_match (DBusConnection *connection,
                     const char     *rule,
                     DBusError      *error)
 {
-  DBusMessage *msg;
+       DBusMessage *msg;
 
-  _dbus_return_if_fail (rule != NULL);
+       _dbus_return_if_fail (rule != NULL);
 
-  msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
+       msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
                                       DBUS_PATH_DBUS,
                                       DBUS_INTERFACE_DBUS,
                                       "AddMatch");
 
-  if (msg == NULL)
-    {
-      _DBUS_SET_OOM (error);
-      return;
-    }
+       if (msg == NULL)
+       {
+               _DBUS_SET_OOM (error);
+               return;
+       }
 
-  if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &rule,
-                                 DBUS_TYPE_INVALID))
-    {
-      dbus_message_unref (msg);
-      _DBUS_SET_OOM (error);
-      return;
-    }
+       if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &rule,
+                                                                        DBUS_TYPE_INVALID))
+       {
+               dbus_message_unref (msg);
+               _DBUS_SET_OOM (error);
+               return;
+       }
 
-  send_no_return_values (connection, msg, error);
+       send_no_return_values (connection, msg, error);
 
-  dbus_message_unref (msg);
+       dbus_message_unref (msg);
 }
 
 /**
@@ -1565,26 +1596,26 @@ dbus_bus_remove_match (DBusConnection *connection,
                        const char     *rule,
                        DBusError      *error)
 {
-  DBusMessage *msg;
+       DBusMessage *msg;
 
-  _dbus_return_if_fail (rule != NULL);
-  
-  msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
-                                      DBUS_PATH_DBUS,
-                                      DBUS_INTERFACE_DBUS,
-                                      "RemoveMatch");
+       _dbus_return_if_fail (rule != NULL);
 
-  if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &rule,
-                                 DBUS_TYPE_INVALID))
-    {
-      dbus_message_unref (msg);
-      _DBUS_SET_OOM (error);
-      return;
-    }
+       msg = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
+                                                                                 DBUS_PATH_DBUS,
+                                                                                 DBUS_INTERFACE_DBUS,
+                                                                                 "RemoveMatch");
 
-  send_no_return_values (connection, msg, error);
+       if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &rule,
+                                                                        DBUS_TYPE_INVALID))
+       {
+               dbus_message_unref (msg);
+               _DBUS_SET_OOM (error);
+               return;
+       }
 
-  dbus_message_unref (msg);
+       send_no_return_values (connection, msg, error);
+
+       dbus_message_unref (msg);
 }
 
 /** @} */