2003-01-21 Havoc Pennington <hp@pobox.com>
authorHavoc Pennington <hp@redhat.com>
Wed, 22 Jan 2003 03:36:05 +0000 (03:36 +0000)
committerHavoc Pennington <hp@redhat.com>
Wed, 22 Jan 2003 03:36:05 +0000 (03:36 +0000)
        (patch untested because can't compile)

* bus/driver.c (create_unique_client_name): make this function
never recycle client names. Also, caller should initialize
the DBusString.

* dbus/dbus-sysdeps.c (_dbus_get_current_time): new function

ChangeLog
bus/driver.c
dbus/dbus-sysdeps.c
dbus/dbus-sysdeps.h

index 54168e9..adbd7bb 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2003-01-21  Havoc Pennington  <hp@pobox.com>
+
+        (patch untested because can't compile)
+       
+       * bus/driver.c (create_unique_client_name): make this function
+       never recycle client names. Also, caller should initialize 
+       the DBusString.
+
+       * dbus/dbus-sysdeps.c (_dbus_get_current_time): new function
+
 2003-01-21  Anders Carlsson  <andersca@codefactory.se>
 
        * dbus/dbus-marshal.c: (_dbus_marshal_double),
index d24cb77..eda09af 100644 (file)
@@ -36,34 +36,62 @@ static dbus_bool_t  bus_driver_send_welcome_message (DBusConnection *connection,
                                                     DBusMessage    *hello_message);
 
 static dbus_bool_t
-create_unique_client_name (const char *name, DBusString *str)
+create_unique_client_name (const char *name,
+                           DBusString *str)
 {
-  int i, len;
-
-  if (!_dbus_string_init (str, _DBUS_INT_MAX))
-    return FALSE;
+  /* We never want to use the same unique client name twice, because
+   * we want to guarantee that if you send a message to a given unique
+   * name, you always get the same application. So we use two numbers
+   * for INT_MAX * INT_MAX combinations, should be pretty safe against
+   * wraparound.
+   */
+  static int next_major_number = 0;
+  static int next_minor_number = 0;
+  int len;
 
   if (!_dbus_string_append (str, name))
     return FALSE;
   
   len = _dbus_string_get_length (str);
   
-  i = 0;  
-  while (1)
+  while (TRUE)
     {
-      if (!_dbus_string_append_int (str, i))
-       {
-         _dbus_string_free (str);
-         return FALSE;
-       }
+      /* start out with 1-0, go to 1-1, 1-2, 1-3,
+       * up to 1-MAXINT, then 2-0, 2-1, etc.
+       */
+      if (next_minor_number <= 0)
+        {
+          next_major_number += 1;
+          next_minor_number = 0;
+          if (next_major_number <= 0)
+            _dbus_assert_not_reached ("INT_MAX * INT_MAX clients were added");
+        }
+
+      _dbus_assert (next_major_number > 0);
+      _dbus_assert (next_minor_number >= 0);
+
+      /* appname:MAJOR-MINOR */
+      
+      if (!_dbus_string_append (str, ":"))
+        return FALSE;
+      
+      if (!_dbus_string_append_int (str, next_major_number))
+        return FALSE;
+
+      if (!_dbus_string_append (str, "-"))
+        return FALSE;
+      
+      if (!_dbus_string_append_int (str, next_minor_number))
+        return FALSE;
+
+      next_minor_number += 1;
       
       /* Check if a client with the name exists */
       if (bus_service_lookup (str, FALSE) == NULL)
        break;
 
+      /* drop the number again, try the next one. */
       _dbus_string_set_length (str, len);
-      
-      i++;
     }
 
   return TRUE;
@@ -87,8 +115,14 @@ bus_driver_handle_hello_message (DBusConnection *connection,
   if (result != DBUS_RESULT_SUCCESS)
     return FALSE;
 
-  if (!create_unique_client_name (name, &unique_name))
+  if (!_dbus_string_init (&unique_name, _DBUS_INT_MAX))
     return FALSE;
+  
+  if (!create_unique_client_name (name, &unique_name))
+    {
+      _dbus_string_free (&unique_name);
+      return FALSE;
+    }
 
   /* Create the service */
   service = bus_service_lookup (&unique_name, TRUE);
index a407482..c91a06f 100644 (file)
@@ -983,4 +983,24 @@ _dbus_sleep_milliseconds (int milliseconds)
 #endif
 }
 
+/**
+ * Get current time, as in gettimeofday().
+ *
+ * @param tv_sec return location for number of seconds
+ * @param tv_usec return location for number of microseconds (thousandths)
+ */
+void
+_dbus_get_current_time (long *tv_sec,
+                        long *tv_usec)
+{
+  struct timeval t;
+
+  gettimeofday (&t, NULL);
+
+  if (tv_sec)
+    *tv_sec = t.tv_sec;
+  if (tv_usec)
+    *tv_usec = t.tv_usec;
+}
+
 /** @} end of sysdeps */
index c76c691..5b9a0c0 100644 (file)
@@ -118,6 +118,9 @@ int _dbus_poll (DBusPollFD *fds,
 
 void _dbus_sleep_milliseconds (int milliseconds);
 
+void _dbus_get_current_time (long *tv_sec,
+                             long *tv_usec);
+
 DBUS_END_DECLS;
 
 #endif /* DBUS_SYSDEPS_H */