Minor fix for the license of tests package
[platform/upstream/dbus.git] / dbus / dbus-bus.c
index f05ddea..a7f31cd 100644 (file)
@@ -28,6 +28,7 @@
 #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"
@@ -95,19 +96,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)
 {
@@ -117,7 +105,7 @@ addresses_shutdown_func (void *data)
   while (i < N_BUS_TYPES)
     {
       if (bus_connections[i] != NULL)
-        _dbus_warn_check_failed ("dbus_shutdown() called but connections were still live. This probably means the application did not drop all its references to bus connections.\n");
+        _dbus_warn_check_failed ("dbus_shutdown() called but connections were still live. This probably means the application did not drop all its references to bus connections.");
       
       dbus_free (bus_connection_addresses[i]);
       bus_connection_addresses[i] = NULL;
@@ -180,9 +168,9 @@ init_session_address (void)
       else if (supported && !retval)
         {
           if (dbus_error_is_set(&error))
-            _dbus_warn ("Dynamic session lookup supported but failed: %s\n", error.message);
+            _dbus_warn ("Dynamic session lookup supported but failed: %s", error.message);
           else
-            _dbus_warn ("Dynamic session lookup supported but failed silently\n");
+            _dbus_warn ("Dynamic session lookup supported but failed silently");
         }
       _dbus_string_free (&addr);
     }
@@ -192,12 +180,12 @@ init_session_address (void)
   if (!retval)
     return FALSE;
 
-  /* The DBUS_SESSION_BUS_DEFAULT_ADDRESS should have really been named
-   * DBUS_SESSION_BUS_FALLBACK_ADDRESS. 
-   */
+  /* We have a hard-coded (but compile-time-configurable) fallback address for
+   * the session bus. */
   if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
     bus_connection_addresses[DBUS_BUS_SESSION] =
-      _dbus_strdup (DBUS_SESSION_BUS_DEFAULT_ADDRESS);
+      _dbus_strdup (DBUS_SESSION_BUS_CONNECT_ADDRESS);
+
   if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
     return FALSE;
 
@@ -301,17 +289,18 @@ init_connections_unlocked (void)
                 return FALSE;
             }
         }
+
+      s = _dbus_getenv ("DBUS_DEFAULT_PROTOCOL_STRATEGY");
+
+      if (s != NULL)
+        {
+          dbus_set_default_protocol_strategy (s);
+        }
       
       /* If we return FALSE we have to be sure that restarting
        * the above code will work right
        */
       
-      if (!_dbus_setenv ("DBUS_ACTIVATION_ADDRESS", NULL))
-        return FALSE;
-
-      if (!_dbus_setenv ("DBUS_ACTIVATION_BUS_TYPE", NULL))
-        return FALSE;
-      
       if (!_dbus_register_shutdown_func (addresses_shutdown_func,
                                          NULL))
         return FALSE;
@@ -330,7 +319,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 +394,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
@@ -434,15 +432,21 @@ internal_bus_get (DBusBusType  type,
   _dbus_return_val_if_fail (type >= 0 && type < N_BUS_TYPES, NULL);
   _dbus_return_val_if_error_is_set (error, NULL);
 
-  _DBUS_LOCK (bus);
+  connection = NULL;
 
-  if (!init_connections_unlocked ())
+  if (!_DBUS_LOCK (bus))
     {
-      _DBUS_UNLOCK (bus);
       _DBUS_SET_OOM (error);
+      /* do not "goto out", that would try to unlock */
       return NULL;
     }
 
+  if (!init_connections_unlocked ())
+    {
+      _DBUS_SET_OOM (error);
+      goto out;
+    }
+
   /* We want to use the activation address even if the
    * activating bus is the session or system bus,
    * per the spec.
@@ -462,9 +466,7 @@ internal_bus_get (DBusBusType  type,
     {
       connection = bus_connections[type];
       dbus_connection_ref (connection);
-      
-      _DBUS_UNLOCK (bus);
-      return connection;
+      goto out;
     }
 
   address = bus_connection_addresses[address_type];
@@ -472,8 +474,7 @@ internal_bus_get (DBusBusType  type,
     {
       dbus_set_error (error, DBUS_ERROR_FAILED,
                       "Unable to determine the address of the message bus (try 'man dbus-launch' and 'man dbus-daemon' for help)");
-      _DBUS_UNLOCK (bus);
-      return NULL;
+      goto out;
     }
 
   if (private)
@@ -483,19 +484,15 @@ internal_bus_get (DBusBusType  type,
   
   if (!connection)
     {
-      _DBUS_ASSERT_ERROR_IS_SET (error);
-      _DBUS_UNLOCK (bus);
-      return NULL;
+      goto out;
     }
 
   if (!dbus_bus_register (connection, error))
     {
-      _DBUS_ASSERT_ERROR_IS_SET (error);
       _dbus_connection_close_possibly_shared (connection);
       dbus_connection_unref (connection);
-
-      _DBUS_UNLOCK (bus);
-      return NULL;
+      connection = NULL;
+      goto out;
     }
 
   if (!private)
@@ -512,18 +509,22 @@ 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 */
   bd->is_well_known = TRUE;
   _DBUS_UNLOCK (bus_datas);
 
-  
-  _DBUS_UNLOCK (bus);
+out:
+  /* Return a reference to the caller, or NULL with error set. */
+  if (connection == NULL)
+    _DBUS_ASSERT_ERROR_IS_SET (error);
 
-  /* Return a reference to the caller */
+  _DBUS_UNLOCK (bus);
   return connection;
 }
 
@@ -558,7 +559,7 @@ internal_bus_get (DBusBusType  type,
  * 
  * @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,
@@ -664,7 +665,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)
@@ -773,8 +779,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;
@@ -816,8 +826,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;
@@ -925,7 +940,7 @@ dbus_bus_get_unix_user (DBusConnection *connection,
  * probably the bus ID is not useful; instead, use the machine ID
  * since it's accessible without necessarily connecting to the bus and
  * may be persistent beyond a single bus instance (across reboots for
- * example). See dbus_get_local_machine_id().
+ * example). See dbus_try_get_local_machine_id().
  *
  * In addition to an ID for each bus and an ID for each machine, there is
  * an ID for each address that the bus is listening on; that can