1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-internals.c random utility stuff (internal to D-Bus implementation)
4 * Copyright (C) 2002, 2003 Red Hat, Inc.
6 * Licensed under the Academic Free License version 2.1
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "dbus-internals.h"
24 #include "dbus-protocol.h"
25 #include "dbus-test.h"
29 #include <sys/types.h>
35 * @defgroup DBusInternals D-Bus internal implementation details
36 * @brief Documentation useful when developing or debugging D-Bus itself.
41 * @defgroup DBusInternalsUtils Utilities and portability
42 * @ingroup DBusInternals
43 * @brief Utility functions (_dbus_assert(), _dbus_warn(), etc.)
50 * Aborts with an error message if the condition is false.
52 * @param condition condition which must be true.
56 * @def _dbus_assert_not_reached
58 * Aborts with an error message if called.
59 * The given explanation will be printed.
61 * @param explanation explanation of what happened if the code was reached.
65 * @def _DBUS_N_ELEMENTS
67 * Computes the number of elements in a fixed-size array using
70 * @param array the array to count elements in.
74 * @def _DBUS_POINTER_TO_INT
76 * Safely casts a void* to an integer; should only be used on void*
77 * that actually contain integers, for example one created with
78 * _DBUS_INT_TO_POINTER. Only guaranteed to preserve 32 bits.
79 * (i.e. it's used to store 32-bit ints in pointers, but
80 * can't be used to store 64-bit pointers in ints.)
82 * @param pointer pointer to extract an integer from.
85 * @def _DBUS_INT_TO_POINTER
87 * Safely stuffs an integer into a pointer, to be extracted later with
88 * _DBUS_POINTER_TO_INT. Only guaranteed to preserve 32 bits.
90 * @param integer the integer to stuff into a pointer.
95 * Sets all bits in an object to zero.
97 * @param object the object to be zeroed.
100 * @def _DBUS_INT16_MIN
102 * Minimum value of type "int16"
105 * @def _DBUS_INT16_MAX
107 * Maximum value of type "int16"
110 * @def _DBUS_UINT16_MAX
112 * Maximum value of type "uint16"
116 * @def _DBUS_INT32_MIN
118 * Minimum value of type "int32"
121 * @def _DBUS_INT32_MAX
123 * Maximum value of type "int32"
126 * @def _DBUS_UINT32_MAX
128 * Maximum value of type "uint32"
134 * Minimum value of type "int"
139 * Maximum value of type "int"
142 * @def _DBUS_UINT_MAX
144 * Maximum value of type "uint"
148 * @typedef DBusForeachFunction
150 * Used to iterate over each item in a collection, such as
155 * @def _DBUS_LOCK_NAME
157 * Expands to name of a global lock variable.
161 * @def _DBUS_DEFINE_GLOBAL_LOCK
163 * Defines a global lock variable with the given name.
164 * The lock must be added to the list to initialize
165 * in dbus_threads_init().
169 * @def _DBUS_DECLARE_GLOBAL_LOCK
171 * Expands to declaration of a global lock defined
172 * with _DBUS_DEFINE_GLOBAL_LOCK.
173 * The lock must be added to the list to initialize
174 * in dbus_threads_init().
180 * Locks a global lock
186 * Unlocks a global lock
190 * Fixed "out of memory" error message, just to avoid
191 * making up a different string every time and wasting
194 const char _dbus_no_memory_message[] = "Not enough memory";
197 * Prints a warning message to stderr.
199 * @param format printf-style format string.
202 _dbus_warn (const char *format,
205 /* FIXME not portable enough? */
208 va_start (args, format);
209 vfprintf (stderr, format, args);
213 #ifdef DBUS_ENABLE_VERBOSE_MODE
215 static dbus_bool_t verbose_initted = FALSE;
216 static dbus_bool_t verbose = TRUE;
218 #define PTHREAD_IN_VERBOSE 0
219 #if PTHREAD_IN_VERBOSE
224 _dbus_verbose_init (void)
226 if (!verbose_initted)
228 const char *p = _dbus_getenv ("DBUS_VERBOSE");
229 verbose = p != NULL && *p == '1';
230 verbose_initted = TRUE;
235 _dbus_is_verbose_real (void)
237 _dbus_verbose_init ();
242 * Prints a warning message to stderr
243 * if the user has enabled verbose mode.
244 * This is the real function implementation,
245 * use _dbus_verbose() macro in code.
247 * @param format printf-style format string.
250 _dbus_verbose_real (const char *format,
254 static dbus_bool_t need_pid = TRUE;
257 /* things are written a bit oddly here so that
258 * in the non-verbose case we just have the one
259 * conditional and return immediately.
261 if (!_dbus_is_verbose_real())
264 /* Print out pid before the line */
267 #if PTHREAD_IN_VERBOSE
268 fprintf (stderr, "%lu: 0x%lx: ", _dbus_getpid (), pthread_self ());
270 fprintf (stderr, "%lu: ", _dbus_getpid ());
275 /* Only print pid again if the next line is a new line */
276 len = strlen (format);
277 if (format[len-1] == '\n')
282 va_start (args, format);
283 vfprintf (stderr, format, args);
290 * Reinitializes the verbose logging code, used
291 * as a hack in dbus-spawn.c so that a child
292 * process re-reads its pid
296 _dbus_verbose_reset_real (void)
298 verbose_initted = FALSE;
301 #endif /* DBUS_ENABLE_VERBOSE_MODE */
304 * Duplicates a string. Result must be freed with
305 * dbus_free(). Returns #NULL if memory allocation fails.
306 * If the string to be duplicated is #NULL, returns #NULL.
308 * @param str string to duplicate.
309 * @returns newly-allocated copy.
312 _dbus_strdup (const char *str)
322 copy = dbus_malloc (len + 1);
326 memcpy (copy, str, len + 1);
332 * Duplicates a block of memory. Returns
335 * @param mem memory to copy
336 * @param n_bytes number of bytes to copy
340 _dbus_memdup (const void *mem,
345 copy = dbus_malloc (n_bytes);
349 memcpy (copy, mem, n_bytes);
355 * Duplicates a string array. Result may be freed with
356 * dbus_free_string_array(). Returns #NULL if memory allocation fails.
357 * If the array to be duplicated is #NULL, returns #NULL.
359 * @param array array to duplicate.
360 * @returns newly-allocated copy.
363 _dbus_dup_string_array (const char **array)
372 for (len = 0; array[len] != NULL; ++len)
375 copy = dbus_new0 (char*, len + 1);
382 copy[i] = _dbus_strdup (array[i]);
385 dbus_free_string_array (copy);
396 * Checks whether a string array contains the given string.
398 * @param array array to search.
399 * @param str string to look for
400 * @returns #TRUE if array contains string
403 _dbus_string_array_contains (const char **array,
409 while (array[i] != NULL)
411 if (strcmp (array[i], str) == 0)
419 #ifdef DBUS_BUILD_TESTS
421 * Returns a string describing the given name.
423 * @param header_field the field to describe
424 * @returns a constant string describing the field
427 _dbus_header_field_to_string (int header_field)
429 switch (header_field)
431 case DBUS_HEADER_FIELD_INVALID:
433 case DBUS_HEADER_FIELD_PATH:
435 case DBUS_HEADER_FIELD_INTERFACE:
437 case DBUS_HEADER_FIELD_MEMBER:
439 case DBUS_HEADER_FIELD_ERROR_NAME:
441 case DBUS_HEADER_FIELD_REPLY_SERIAL:
442 return "reply-serial";
443 case DBUS_HEADER_FIELD_DESTINATION:
444 return "destination";
445 case DBUS_HEADER_FIELD_SENDER:
447 case DBUS_HEADER_FIELD_SIGNATURE:
453 #endif /* DBUS_BUILD_TESTS */
455 #ifndef DBUS_DISABLE_CHECKS
456 /** String used in _dbus_return_if_fail macro */
457 const char _dbus_return_if_fail_warning_format[] =
458 "%lu: arguments to %s() were incorrect, assertion \"%s\" failed in file %s line %d.\n"
459 "This is normally a bug in some application using the D-Bus library.\n";
462 #ifndef DBUS_DISABLE_ASSERT
464 * Internals of _dbus_assert(); it's a function
465 * rather than a macro with the inline code so
466 * that the assertion failure blocks don't show up
467 * in test suite coverage, and to shrink code size.
469 * @param condition TRUE if assertion succeeded
470 * @param condition_text condition as a string
471 * @param file file the assertion is in
472 * @param line line the assertion is in
473 * @param func function the assertion is in
476 _dbus_real_assert (dbus_bool_t condition,
477 const char *condition_text,
482 if (_DBUS_UNLIKELY (!condition))
484 _dbus_warn ("%lu: assertion failed \"%s\" file \"%s\" line %d function %s\n",
485 _dbus_getpid (), condition_text, file, line, func);
491 * Internals of _dbus_assert_not_reached(); it's a function
492 * rather than a macro with the inline code so
493 * that the assertion failure blocks don't show up
494 * in test suite coverage, and to shrink code size.
496 * @param explanation what was reached that shouldn't have been
497 * @param file file the assertion is in
498 * @param line line the assertion is in
501 _dbus_real_assert_not_reached (const char *explanation,
505 _dbus_warn ("File \"%s\" line %d process %lu should not have been reached: %s\n",
506 file, line, _dbus_getpid (), explanation);
509 #endif /* DBUS_DISABLE_ASSERT */
511 #ifdef DBUS_BUILD_TESTS
513 run_failing_each_malloc (int n_mallocs,
514 const char *description,
515 DBusTestMemoryFunction func,
518 n_mallocs += 10; /* fudge factor to ensure reallocs etc. are covered */
520 while (n_mallocs >= 0)
522 _dbus_set_fail_alloc_counter (n_mallocs);
524 _dbus_verbose ("\n===\n%s: (will fail malloc %d with %d failures)\n===\n",
525 description, n_mallocs,
526 _dbus_get_fail_alloc_failures ());
528 if (!(* func) (data))
534 _dbus_set_fail_alloc_counter (_DBUS_INT_MAX);
540 * Tests how well the given function responds to out-of-memory
541 * situations. Calls the function repeatedly, failing a different
542 * call to malloc() each time. If the function ever returns #FALSE,
543 * the test fails. The function should return #TRUE whenever something
544 * valid (such as returning an error, or succeeding) occurs, and #FALSE
545 * if it gets confused in some way.
547 * @param description description of the test used in verbose output
548 * @param func function to call
549 * @param data data to pass to function
550 * @returns #TRUE if the function never returns FALSE
553 _dbus_test_oom_handling (const char *description,
554 DBusTestMemoryFunction func,
559 int max_failures_to_try;
562 /* Run once to see about how many mallocs are involved */
564 _dbus_set_fail_alloc_counter (_DBUS_INT_MAX);
566 _dbus_verbose ("Running once to count mallocs\n");
568 if (!(* func) (data))
571 approx_mallocs = _DBUS_INT_MAX - _dbus_get_fail_alloc_counter ();
573 _dbus_verbose ("\n=================\n%s: about %d mallocs total\n=================\n",
574 description, approx_mallocs);
576 setting = _dbus_getenv ("DBUS_TEST_MALLOC_FAILURES");
581 _dbus_string_init_const (&str, setting);
583 if (!_dbus_string_parse_int (&str, 0, &v, NULL))
584 _dbus_warn ("couldn't parse '%s' as integer\n", setting);
585 max_failures_to_try = v;
589 max_failures_to_try = 4;
592 i = setting ? max_failures_to_try - 1 : 1;
593 while (i < max_failures_to_try)
595 _dbus_set_fail_alloc_failures (i);
596 if (!run_failing_each_malloc (approx_mallocs, description, func, data))
601 _dbus_verbose ("\n=================\n%s: all iterations passed\n=================\n",
606 #endif /* DBUS_BUILD_TESTS */