2003-04-10 Havoc Pennington <hp@pobox.com>
[platform/upstream/dbus.git] / dbus / dbus-internals.c
index acd6d72..230c8ef 100644 (file)
  *
  * Maximum value of type "int"
  */
-/**
- * @def _DBUS_MAX_SUN_PATH_LENGTH
- *
- * Maximum length of the path to a UNIX domain socket,
- * sockaddr_un::sun_path member. POSIX requires that all systems
- * support at least 100 bytes here, including the nul termination.
- * We use 99 for the max value to allow for the nul.
- *
- * We could probably also do sizeof (addr.sun_path)
- * but this way we are the same on all platforms
- * which is probably a good idea.
- */
 
 /**
  * @typedef DBusForeachFunction
  */
 
 /**
+ * @def _DBUS_LOCK_NAME
+ *
+ * Expands to name of a global lock variable.
+ */
+
+/**
+ * @def _DBUS_DEFINE_GLOBAL_LOCK
+ *
+ * Defines a global lock variable with the given name.
+ * The lock must be added to the list to initialize
+ * in dbus_threads_init().
+ */
+
+/**
+ * @def _DBUS_DECLARE_GLOBAL_LOCK
+ *
+ * Expands to declaration of a global lock defined
+ * with _DBUS_DEFINE_GLOBAL_LOCK.
+ * The lock must be added to the list to initialize
+ * in dbus_threads_init().
+ */
+
+/**
+ * @def _DBUS_LOCK
+ *
+ * Locks a global lock
+ */
+
+/**
+ * @def _DBUS_UNLOCK
+ *
+ * Unlocks a global lock
+ */
+
+/**
  * Fixed "out of memory" error message, just to avoid
  * making up a different string every time and wasting
  * space.
@@ -166,7 +189,8 @@ _dbus_verbose_real (const char *format,
   va_list args;
   static dbus_bool_t verbose = TRUE;
   static dbus_bool_t initted = FALSE;
-
+  static unsigned long pid;
+  
   /* things are written a bit oddly here so that
    * in the non-verbose case we just have the one
    * conditional and return immediately.
@@ -177,10 +201,13 @@ _dbus_verbose_real (const char *format,
   if (!initted)
     {
       verbose = _dbus_getenv ("DBUS_VERBOSE") != NULL;
+      pid = _dbus_getpid ();
       initted = TRUE;
       if (!verbose)
         return;
     }
+
+  fprintf (stderr, "%lu: ", pid);
   
   va_start (args, format);
   vfprintf (stderr, format, args);
@@ -216,37 +243,68 @@ _dbus_strdup (const char *str)
 }
 
 /**
- * Sets a file descriptor to be nonblocking.
- *
- * @param fd the file descriptor.
- * @param result address of result code.
- * @returns #TRUE on success.
+ * Duplicates a string array. Result may be freed with
+ * dbus_free_string_array(). Returns #NULL if memory allocation fails.
+ * If the array to be duplicated is #NULL, returns #NULL.
+ * 
+ * @param array array to duplicate.
+ * @returns newly-allocated copy.
  */
-dbus_bool_t
-_dbus_set_fd_nonblocking (int             fd,
-                          DBusResultCode *result)
+char**
+_dbus_dup_string_array (const char **array)
 {
-  int val;
+  int len;
+  int i;
+  char **copy;
+  
+  if (array == NULL)
+    return NULL;
+
+  for (len = 0; array[len] != NULL; ++len)
+    ;
+
+  copy = dbus_new0 (char*, len + 1);
+  if (copy == NULL)
+    return NULL;
 
-  val = fcntl (fd, F_GETFL, 0);
-  if (val < 0)
+  i = 0;
+  while (i < len)
     {
-      dbus_set_result (result, _dbus_result_from_errno (errno));
-      _dbus_verbose ("Failed to get flags for fd %d: %s\n", fd,
-                     _dbus_strerror (errno));
-      return FALSE;
+      copy[i] = _dbus_strdup (array[i]);
+      if (copy[i] == NULL)
+        {
+          dbus_free_string_array (copy);
+          return NULL;
+        }
+
+      ++i;
     }
 
-  if (fcntl (fd, F_SETFL, val | O_NONBLOCK) < 0)
-    {
-      dbus_set_result (result, _dbus_result_from_errno (errno));      
-      _dbus_verbose ("Failed to set fd %d nonblocking: %s\n",
-                     fd, _dbus_strerror (errno));
+  return copy;
+}
+
+/**
+ * Checks whether a string array contains the given string.
+ * 
+ * @param array array to search.
+ * @param str string to look for
+ * @returns #TRUE if array contains string
+ */
+dbus_bool_t
+_dbus_string_array_contains (const char **array,
+                             const char  *str)
+{
+  int i;
 
-      return FALSE;
+  i = 0;
+  while (array[i] != NULL)
+    {
+      if (strcmp (array[i], str) == 0)
+        return TRUE;
+      ++i;
     }
 
-  return TRUE;
+  return FALSE;
 }
 
 /**
@@ -274,72 +332,146 @@ _dbus_type_to_string (int type)
       return "double";
     case DBUS_TYPE_STRING:
       return "string";
-    case DBUS_TYPE_BOOLEAN_ARRAY:
-      return "boolean array";
-    case DBUS_TYPE_INT32_ARRAY:
-      return "int32 array";
-    case DBUS_TYPE_UINT32_ARRAY:
-      return "uint32 array";
-    case DBUS_TYPE_DOUBLE_ARRAY:
-      return "double array";
-    case DBUS_TYPE_BYTE_ARRAY:
-      return "byte array";
-    case DBUS_TYPE_STRING_ARRAY:
-      return "string array";
+    case DBUS_TYPE_NAMED:
+      return "named";
+    case DBUS_TYPE_ARRAY:
+      return "array";
+    case DBUS_TYPE_DICT:
+      return "dict";
     default:
       return "unknown";
     }
 }
 
-#ifdef DBUS_BUILD_TESTS
-static int fail_alloc_counter = _DBUS_INT_MAX;
+#ifndef DBUS_DISABLE_ASSERT
 /**
- * Sets the number of allocations until we simulate a failed
- * allocation. If set to 0, the next allocation to run
- * fails; if set to 1, one succeeds then the next fails; etc.
- * Set to _DBUS_INT_MAX to not fail anything. 
+ * Internals of _dbus_assert(); it's a function
+ * rather than a macro with the inline code so
+ * that the assertion failure blocks don't show up
+ * in test suite coverage, and to shrink code size.
  *
- * @param until_next_fail number of successful allocs before one fails
+ * @param condition TRUE if assertion succeeded
+ * @param condition_text condition as a string
+ * @param file file the assertion is in
+ * @param line line the assertion is in
  */
 void
-_dbus_set_fail_alloc_counter (int until_next_fail)
+_dbus_real_assert (dbus_bool_t  condition,
+                   const char  *condition_text,
+                   const char  *file,
+                   int          line)
 {
-  fail_alloc_counter = until_next_fail;
+  if (!condition)
+    {
+      _dbus_warn ("Assertion failed \"%s\" file \"%s\" line %d process %lu\n",
+                  condition_text, file, line, _dbus_getpid ());
+      _dbus_abort ();
+    }
 }
 
 /**
- * Gets the number of successful allocs until we'll simulate
- * a failed alloc.
+ * Internals of _dbus_assert_not_reached(); it's a function
+ * rather than a macro with the inline code so
+ * that the assertion failure blocks don't show up
+ * in test suite coverage, and to shrink code size.
  *
- * @returns current counter value
+ * @param explanation what was reached that shouldn't have been
+ * @param file file the assertion is in
+ * @param line line the assertion is in
  */
-int
-_dbus_get_fail_alloc_counter (void)
+void
+_dbus_real_assert_not_reached (const char *explanation,
+                               const char *file,
+                               int         line)
 {
-  return fail_alloc_counter;
+  _dbus_warn ("File \"%s\" line %d process %lu should not have been reached: %s\n",
+              file, line, _dbus_getpid (), explanation);
+  _dbus_abort ();
 }
+#endif /* DBUS_DISABLE_ASSERT */
+  
+#ifdef DBUS_BUILD_TESTS
+static dbus_bool_t
+run_failing_each_malloc (int                    n_mallocs,
+                         const char            *description,
+                         DBusTestMemoryFunction func,
+                         void                  *data)
+{
+  n_mallocs += 10; /* fudge factor to ensure reallocs etc. are covered */
+  
+  while (n_mallocs >= 0)
+    {      
+      _dbus_set_fail_alloc_counter (n_mallocs);
+
+      _dbus_verbose ("\n===\n%s: (will fail malloc %d with %d failures)\n===\n",
+                     description, n_mallocs,
+                     _dbus_get_fail_alloc_failures ());
+
+      if (!(* func) (data))
+        return FALSE;
+      
+      n_mallocs -= 1;
+    }
+
+  _dbus_set_fail_alloc_counter (_DBUS_INT_MAX);
+
+  return TRUE;
+}                        
 
 /**
- * Called when about to alloc some memory; if
- * it returns #TRUE, then the allocation should
- * fail. If it returns #FALSE, then the allocation
- * should not fail.
+ * Tests how well the given function responds to out-of-memory
+ * situations. Calls the function repeatedly, failing a different
+ * call to malloc() each time. If the function ever returns #FALSE,
+ * the test fails. The function should return #TRUE whenever something
+ * valid (such as returning an error, or succeeding) occurs, and #FALSE
+ * if it gets confused in some way.
  *
- * @returns #TRUE if this alloc should fail
+ * @param description description of the test used in verbose output
+ * @param func function to call
+ * @param data data to pass to function
+ * @returns #TRUE if the function never returns FALSE
  */
 dbus_bool_t
-_dbus_decrement_fail_alloc_counter (void)
+_dbus_test_oom_handling (const char             *description,
+                         DBusTestMemoryFunction  func,
+                         void                   *data)
 {
-  if (fail_alloc_counter <= 0)
-    {
-      fail_alloc_counter = _DBUS_INT_MAX;
-      return TRUE;
-    }
-  else
-    {
-      fail_alloc_counter -= 1;
-      return FALSE;
-    }
+  int approx_mallocs;
+
+  /* Run once to see about how many mallocs are involved */
+  
+  _dbus_set_fail_alloc_counter (_DBUS_INT_MAX);
+
+  _dbus_verbose ("Running once to count mallocs\n");
+  
+  if (!(* func) (data))
+    return FALSE;
+  
+  approx_mallocs = _DBUS_INT_MAX - _dbus_get_fail_alloc_counter ();
+
+  _dbus_verbose ("\n=================\n%s: about %d mallocs total\n=================\n",
+                 description, approx_mallocs);
+
+  _dbus_set_fail_alloc_failures (1);
+  if (!run_failing_each_malloc (approx_mallocs, description, func, data))
+    return FALSE;
+
+  _dbus_set_fail_alloc_failures (2);
+  if (!run_failing_each_malloc (approx_mallocs, description, func, data))
+    return FALSE;
+  
+  _dbus_set_fail_alloc_failures (3);
+  if (!run_failing_each_malloc (approx_mallocs, description, func, data))
+    return FALSE;
+
+  _dbus_set_fail_alloc_failures (4);
+  if (!run_failing_each_malloc (approx_mallocs, description, func, data))
+    return FALSE;
+  
+  _dbus_verbose ("\n=================\n%s: all iterations passed\n=================\n",
+                 description);
+
+  return TRUE;
 }
 #endif /* DBUS_BUILD_TESTS */