bus: add ConnectionOverflow signal API
[platform/upstream/dbus.git] / dbus / dbus-internals.c
index 63559be..267aef9 100644 (file)
@@ -230,13 +230,17 @@ void
 _dbus_warn (const char *format,
             ...)
 {
+  DBusSystemLogSeverity severity = DBUS_SYSTEM_LOG_WARNING;
   va_list args;
 
   if (!warn_initted)
     init_warnings ();
-  
+
+  if (fatal_warnings)
+    severity = DBUS_SYSTEM_LOG_ERROR;
+
   va_start (args, format);
-  vfprintf (stderr, format, args);
+  _dbus_logv (severity, format, args);
   va_end (args);
 
   if (fatal_warnings)
@@ -248,7 +252,7 @@ _dbus_warn (const char *format,
 
 /**
  * Prints a "critical" warning to stderr when an assertion fails;
- * differs from _dbus_warn primarily in that it prefixes the pid and
+ * differs from _dbus_warn primarily in that it
  * defaults to fatal. This should be used only when a programming
  * error has been detected. (NOT for unavoidable errors that an app
  * might handle - those should be returned as DBusError.) Calling this
@@ -258,15 +262,17 @@ void
 _dbus_warn_check_failed(const char *format,
                         ...)
 {
+  DBusSystemLogSeverity severity = DBUS_SYSTEM_LOG_WARNING;
   va_list args;
   
   if (!warn_initted)
     init_warnings ();
 
-  fprintf (stderr, "process %lu: ", _dbus_pid_for_log ());
-  
+  if (fatal_warnings_on_check_failed)
+    severity = DBUS_SYSTEM_LOG_ERROR;
+
   va_start (args, format);
-  vfprintf (stderr, format, args);
+  _dbus_logv (severity, format, args);
   va_end (args);
 
   if (fatal_warnings_on_check_failed)
@@ -281,12 +287,6 @@ _dbus_warn_check_failed(const char *format,
 static dbus_bool_t verbose_initted = FALSE;
 static dbus_bool_t verbose = TRUE;
 
-/** Whether to show the current thread in verbose messages */
-#define PTHREAD_IN_VERBOSE 0
-#if PTHREAD_IN_VERBOSE
-#include <pthread.h>
-#endif
-
 #ifdef DBUS_USE_OUTPUT_DEBUG_STRING
 static char module_name[1024];
 #endif
@@ -332,25 +332,22 @@ _dbus_verbose_init (void)
 */ 
 static char *_dbus_file_path_extract_elements_from_tail(const char *file,int level)
 {
-  static int prefix = -1;
+  int prefix = 0;
+  char *p = (char *)file + strlen(file);
+  int i = 0;
 
-  if (prefix == -1) 
+  for (;p >= file;p--)
     {
-      char *p = (char *)file + strlen(file);
-      int i = 0;
-      prefix = 0;
-      for (;p >= file;p--)
+      if (DBUS_IS_DIR_SEPARATOR(*p))
         {
-          if (DBUS_IS_DIR_SEPARATOR(*p))
+          if (++i >= level)
             {
-              if (++i >= level) 
-                {
-                  prefix = p-file+1;
-                  break;
-                }
-           }
-        }
+              prefix = p-file+1;
+              break;
+            }
+       }
     }
+
   return (char *)file+prefix;
 }
 
@@ -366,6 +363,16 @@ _dbus_is_verbose_real (void)
   return verbose;
 }
 
+void _dbus_set_verbose (dbus_bool_t state)
+{
+    verbose = state;
+}
+
+dbus_bool_t _dbus_get_verbose (void)
+{
+    return verbose;
+}
+
 /**
  * Prints a warning message to stderr
  * if the user has enabled verbose mode.
@@ -387,6 +394,7 @@ _dbus_verbose_real (
   va_list args;
   static dbus_bool_t need_pid = TRUE;
   int len;
+  long sec, usec;
   
   /* things are written a bit oddly here so that
    * in the non-verbose case we just have the one
@@ -399,12 +407,10 @@ _dbus_verbose_real (
   /* Print out pid before the line */
   if (need_pid)
     {
-#if PTHREAD_IN_VERBOSE
-      fprintf (stderr, "%lu: 0x%lx: ", _dbus_pid_for_log (), pthread_self ());
-#else
-      fprintf (stderr, "%lu: ", _dbus_pid_for_log ());
-#endif
+      _dbus_print_thread ();
     }
+  _dbus_get_real_time (&sec, &usec);
+  fprintf (stderr, "%ld.%06ld ", sec, usec);
 #endif
 
   /* Only print pid again if the next line is a new line */
@@ -502,7 +508,7 @@ _dbus_trace_ref (const char *obj_name,
         {
           VALGRIND_PRINTF_BACKTRACE ("%s %p ref stolen (%s)",
                                      obj_name, obj, why);
-          _dbus_verbose ("%s %p ref stolen (%s)",
+          _dbus_verbose ("%s %p ref stolen (%s)\n",
                          obj_name, obj, why);
         }
       else
@@ -510,7 +516,7 @@ _dbus_trace_ref (const char *obj_name,
           VALGRIND_PRINTF_BACKTRACE ("%s %p %d -> %d refs (%s)",
                                      obj_name, obj,
                                      old_refcount, new_refcount, why);
-          _dbus_verbose ("%s %p %d -> %d refs (%s)",
+          _dbus_verbose ("%s %p %d -> %d refs (%s)\n",
                          obj_name, obj, old_refcount, new_refcount, why);
         }
     }
@@ -635,16 +641,37 @@ _dbus_string_array_contains (const char **array,
 }
 
 /**
+ * Returns the size of a string array
+ *
+ * @param array array to search.
+ * @returns size of array
+ */
+size_t
+_dbus_string_array_length (const char **array)
+{
+  size_t i;
+  for (i = 0; array[i]; i++) {}
+  return i;
+}
+
+
+/**
  * Generates a new UUID. If you change how this is done,
  * there's some text about it in the spec that should also change.
  *
  * @param uuid the uuid to initialize
+ * @param error location to store reason for failure
+ * @returns #TRUE on success
  */
-void
-_dbus_generate_uuid (DBusGUID *uuid)
+dbus_bool_t
+_dbus_generate_uuid (DBusGUID  *uuid,
+                     DBusError *error)
 {
+  DBusError rand_error;
   long now;
 
+  dbus_error_init (&rand_error);
+
   /* don't use monotonic time because the UUID may be saved to disk, e.g.
    * it may persist across reboots
    */
@@ -652,7 +679,17 @@ _dbus_generate_uuid (DBusGUID *uuid)
 
   uuid->as_uint32s[DBUS_UUID_LENGTH_WORDS - 1] = DBUS_UINT32_TO_BE (now);
   
-  _dbus_generate_random_bytes_buffer (uuid->as_bytes, DBUS_UUID_LENGTH_BYTES - 4);
+  if (!_dbus_generate_random_bytes_buffer (uuid->as_bytes,
+                                           DBUS_UUID_LENGTH_BYTES - 4,
+                                           &rand_error))
+    {
+      dbus_set_error (error, rand_error.name,
+                      "Failed to generate UUID: %s", rand_error.message);
+      dbus_error_free (&rand_error);
+      return FALSE;
+    }
+
+  return TRUE;
 }
 
 /**
@@ -748,10 +785,18 @@ _dbus_read_uuid_file_without_creating (const DBusString *filename,
   return FALSE;
 }
 
-static dbus_bool_t
-_dbus_create_uuid_file_exclusively (const DBusString *filename,
-                                    DBusGUID         *uuid,
-                                    DBusError        *error)
+/**
+ * Write the give UUID to a file.
+ *
+ * @param filename the file to write
+ * @param uuid the UUID to save
+ * @param error used to raise an error
+ * @returns #FALSE on error
+ */
+dbus_bool_t
+_dbus_write_uuid_file (const DBusString *filename,
+                       const DBusGUID   *uuid,
+                       DBusError        *error)
 {
   DBusString encoded;
 
@@ -760,8 +805,6 @@ _dbus_create_uuid_file_exclusively (const DBusString *filename,
       _DBUS_SET_OOM (error);
       return FALSE;
     }
-
-  _dbus_generate_uuid (uuid);
   
   if (!_dbus_uuid_encode (uuid, &encoded))
     {
@@ -828,7 +871,11 @@ _dbus_read_uuid_file (const DBusString *filename,
   else
     {
       dbus_error_free (&read_error);
-      return _dbus_create_uuid_file_exclusively (filename, uuid, error);
+
+      if (!_dbus_generate_uuid (uuid, error))
+        return FALSE;
+
+      return _dbus_write_uuid_file (filename, uuid, error);
     }
 }
 
@@ -844,40 +891,35 @@ static DBusGUID machine_uuid;
  * machine is reconfigured or its hardware is modified.
  * 
  * @param uuid_str string to append hex-encoded machine uuid to
- * @returns #FALSE if no memory
+ * @param error location to store reason for failure
+ * @returns #TRUE if successful
  */
 dbus_bool_t
-_dbus_get_local_machine_uuid_encoded (DBusString *uuid_str)
+_dbus_get_local_machine_uuid_encoded (DBusString *uuid_str,
+                                      DBusError  *error)
 {
-  dbus_bool_t ok;
+  dbus_bool_t ok = TRUE;
   
   if (!_DBUS_LOCK (machine_uuid))
-    return FALSE;
+    {
+      _DBUS_SET_OOM (error);
+      return FALSE;
+    }
 
   if (machine_uuid_initialized_generation != _dbus_current_generation)
     {
-      DBusError error = DBUS_ERROR_INIT;
-
-      if (!_dbus_read_local_machine_uuid (&machine_uuid, FALSE,
-                                          &error))
-        {          
-#ifndef DBUS_BUILD_TESTS
-          /* For the test suite, we may not be installed so just continue silently
-           * here. But in a production build, we want to be nice and loud about
-           * this.
-           */
-          _dbus_warn_check_failed ("D-Bus library appears to be incorrectly set up; failed to read machine uuid: %s\n"
-                                   "See the manual page for dbus-uuidgen to correct this issue.\n",
-                                   error.message);
-#endif
-          
-          dbus_error_free (&error);
-          
-          _dbus_generate_uuid (&machine_uuid);
-        }
+      if (!_dbus_read_local_machine_uuid (&machine_uuid, FALSE, error))
+        ok = FALSE;
     }
 
-  ok = _dbus_uuid_encode (&machine_uuid, uuid_str);
+  if (ok)
+    {
+      if (!_dbus_uuid_encode (&machine_uuid, uuid_str))
+        {
+          ok = FALSE;
+          _DBUS_SET_OOM (error);
+        }
+    }
 
   _DBUS_UNLOCK (machine_uuid);
 
@@ -885,10 +927,17 @@ _dbus_get_local_machine_uuid_encoded (DBusString *uuid_str)
 }
 
 #ifndef DBUS_DISABLE_CHECKS
-/** String used in _dbus_return_if_fail macro */
-const char *_dbus_return_if_fail_warning_format =
-"arguments to %s() were incorrect, assertion \"%s\" failed in file %s line %d.\n"
-"This is normally a bug in some application using the D-Bus library.\n";
+void
+_dbus_warn_return_if_fail (const char *function,
+                           const char *assertion,
+                           const char *file,
+                           int line)
+{
+  _dbus_warn_check_failed (
+      "arguments to %s() were incorrect, assertion \"%s\" failed in file %s line %d.\n"
+      "This is normally a bug in some application using the D-Bus library.\n",
+      function, assertion, file, line);
+}
 #endif
 
 #ifndef DBUS_DISABLE_ASSERT
@@ -913,8 +962,8 @@ _dbus_real_assert (dbus_bool_t  condition,
 {
   if (_DBUS_UNLIKELY (!condition))
     {
-      _dbus_warn ("%lu: assertion failed \"%s\" file \"%s\" line %d function %s\n",
-                  _dbus_pid_for_log (), condition_text, file, line, func);
+      _dbus_warn ("assertion failed \"%s\" file \"%s\" line %d function %s",
+                  condition_text, file, line, func);
       _dbus_abort ();
     }
 }
@@ -934,13 +983,13 @@ _dbus_real_assert_not_reached (const char *explanation,
                                const char *file,
                                int         line)
 {
-  _dbus_warn ("File \"%s\" line %d process %lu should not have been reached: %s\n",
-              file, line, _dbus_pid_for_log (), explanation);
+  _dbus_warn ("File \"%s\" line %d should not have been reached: %s",
+              file, line, explanation);
   _dbus_abort ();
 }
 #endif /* DBUS_DISABLE_ASSERT */
   
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
 static dbus_bool_t
 run_failing_each_malloc (int                    n_mallocs,
                          const char            *description,
@@ -1021,6 +1070,12 @@ _dbus_test_oom_handling (const char             *description,
       max_failures_to_try = 4;
     }
 
+  if (max_failures_to_try < 1)
+    {
+      _dbus_verbose ("not testing OOM handling\n");
+      return TRUE;
+    }
+
   i = setting ? max_failures_to_try - 1 : 1;
   while (i < max_failures_to_try)
     {
@@ -1035,6 +1090,6 @@ _dbus_test_oom_handling (const char             *description,
 
   return TRUE;
 }
-#endif /* DBUS_BUILD_TESTS */
+#endif /* DBUS_ENABLE_EMBEDDED_TESTS */
 
 /** @} */