2003-03-31 Havoc Pennington <hp@redhat.com>
[platform/upstream/dbus.git] / dbus / dbus-bus.c
index 8f7fb90..d65a3f0 100644 (file)
@@ -51,6 +51,7 @@ typedef struct
 {
   char *base_service; /**< Base service name of this connection */
 
+  DBusConnection **connection; /**< Pointer to bus_connections entry */
 } BusData;
 
 /** The slot we have reserved to store BusData
@@ -63,37 +64,30 @@ static int bus_data_slot_refcount = 0;
 /**
  * Lock for bus_data_slot and bus_data_slot_refcount
  */
-static DBusMutex *slot_lock;
+_DBUS_DEFINE_GLOBAL_LOCK (bus);
 
-/**
- * Initialize the mutex used for bus_data_slot
- *
- * @returns the mutex
- */
-DBusMutex *
-_dbus_bus_init_lock (void)
-{
-  slot_lock = dbus_mutex_new ();
-  return slot_lock;
-}
 
 static dbus_bool_t
 data_slot_ref (void)
 {
-  dbus_mutex_lock (slot_lock);
-
-  if (bus_data_slot < 0)
-    bus_data_slot = dbus_connection_allocate_data_slot ();
+  _DBUS_LOCK (bus);
 
   if (bus_data_slot < 0)
     {
-      dbus_mutex_unlock (slot_lock);
-      return FALSE;
+      bus_data_slot = dbus_connection_allocate_data_slot ();
+      
+      if (bus_data_slot < 0)
+        {
+          _DBUS_UNLOCK (bus);
+          return FALSE;
+        }
+
+      _dbus_assert (bus_data_slot_refcount == 0);
     }
 
   bus_data_slot_refcount += 1;
 
-  dbus_mutex_unlock (slot_lock);
+  _DBUS_UNLOCK (bus);
 
   return TRUE;
 }
@@ -101,10 +95,10 @@ data_slot_ref (void)
 static void
 data_slot_unref (void)
 {
-  dbus_mutex_lock (slot_lock);
+  _DBUS_LOCK (bus);
 
-  _dbus_assert (bus_data_slot >= 0);
   _dbus_assert (bus_data_slot_refcount > 0);
+  _dbus_assert (bus_data_slot >= 0);
 
   bus_data_slot_refcount -= 1;
 
@@ -114,7 +108,7 @@ data_slot_unref (void)
       bus_data_slot = -1;
     }
 
-  dbus_mutex_unlock (slot_lock);
+  _DBUS_UNLOCK (bus);
 }
 
 static void
@@ -122,6 +116,13 @@ bus_data_free (void *data)
 {
   BusData *bd = data;
 
+  if (bd->connection)
+    {
+      _DBUS_LOCK (bus);
+      *bd->connection = NULL;
+      _DBUS_UNLOCK (bus);
+    }
+  
   dbus_free (bd->base_service);
   dbus_free (bd);
 
@@ -149,7 +150,7 @@ ensure_bus_data (DBusConnection *connection)
       if (!dbus_connection_set_data (connection, bus_data_slot, bd,
                                      bus_data_free))
         {
-          bus_data_free (bd);
+          dbus_free (bd);
           data_slot_unref ();
           return NULL;
         }
@@ -171,6 +172,104 @@ ensure_bus_data (DBusConnection *connection)
  * @{
  */
 
+/** Number of bus types */
+#define BUS_TYPES 2
+
+static DBusConnection *bus_connections[BUS_TYPES];
+
+/**
+ * Connects to a bus daemon and registers the client with it.
+ * If a connection to the bus already exists, then that connection is returned.
+ *
+ * @todo alex thinks we should nullify the connection when we get a disconnect-message.
+ *
+ * @param type bus type
+ * @param error address where an error can be returned.
+ * @returns a DBusConnection
+ */
+DBusConnection *
+dbus_bus_get (DBusBusType  type,
+             DBusError   *error)
+{
+  const char *name, *value;
+  DBusConnection *connection;
+  BusData *bd;
+
+  if (type <= 0 || type >= BUS_TYPES)
+    {
+      _dbus_assert_not_reached ("Invalid bus type specified.");
+
+      return NULL;
+    }
+
+  _DBUS_LOCK (bus);
+  
+  if (bus_connections[type] != NULL)
+    {
+      connection = bus_connections[type];
+      dbus_connection_ref (connection);
+      
+      _DBUS_UNLOCK (bus);
+      return connection;
+    }
+
+  switch (type)
+    {
+    case DBUS_BUS_SESSION:
+      name = "DBUS_SESSION_BUS_ADDRESS";
+      break;
+    case DBUS_BUS_SYSTEM:
+      name = "DBUS_SYSTEM_BUS_ADDRESS";
+      break;
+    }
+
+  value = _dbus_getenv (name);
+
+  if (type == DBUS_BUS_SYSTEM &&
+      (value == NULL || *value == '\0'))
+    {
+      /* Use default system bus address if none set */
+      value = "unix:path=" DBUS_SYSTEM_BUS_PATH;
+    }
+  
+  if (value == NULL || *value == '\0')
+    {
+      dbus_set_error (error, DBUS_ERROR_FAILED,
+                     "Environment variable %s not set, address of message bus unknown",
+                      name);
+      _DBUS_UNLOCK (bus);
+      
+      return NULL;
+    }
+
+  connection = dbus_connection_open (value, error);
+  
+  if (!connection)
+    {
+      _DBUS_UNLOCK (bus);
+      return NULL;
+    }
+  
+  if (!dbus_bus_register (connection, error))
+    {
+      dbus_connection_disconnect (connection);
+      dbus_connection_unref (connection);
+
+      _DBUS_UNLOCK (bus);
+      return NULL;
+    }
+
+  bus_connections[type] = connection;
+  bd = ensure_bus_data (connection);
+  _dbus_assert (bd != NULL);
+
+  bd->connection = &bus_connections[type];
+
+  _DBUS_UNLOCK (bus);  
+  return connection;
+}
+
+
 /**
  * Registers a connection with the bus. This must be the first
  * thing an application does when connecting to the message bus.
@@ -192,6 +291,8 @@ dbus_bus_register (DBusConnection *connection,
   char *name;
   BusData *bd;
 
+  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
+  
   bd = ensure_bus_data (connection);
   if (bd == NULL)
     {
@@ -371,6 +472,8 @@ dbus_bus_service_exists (DBusConnection *connection,
 {
   DBusMessage *message, *reply;
   unsigned int exists;
+
+  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
   
   message = dbus_message_new (DBUS_SERVICE_DBUS,
                               DBUS_MESSAGE_SERVICE_EXISTS);