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>
36 * @defgroup DBusInternals D-Bus internal implementation details
37 * @brief Documentation useful when developing or debugging D-Bus itself.
42 * @defgroup DBusInternalsUtils Utilities and portability
43 * @ingroup DBusInternals
44 * @brief Utility functions (_dbus_assert(), _dbus_warn(), etc.)
51 * Aborts with an error message if the condition is false.
53 * @param condition condition which must be true.
57 * @def _dbus_assert_not_reached
59 * Aborts with an error message if called.
60 * The given explanation will be printed.
62 * @param explanation explanation of what happened if the code was reached.
66 * @def _DBUS_N_ELEMENTS
68 * Computes the number of elements in a fixed-size array using
71 * @param array the array to count elements in.
75 * @def _DBUS_POINTER_TO_INT
77 * Safely casts a void* to an integer; should only be used on void*
78 * that actually contain integers, for example one created with
79 * _DBUS_INT_TO_POINTER. Only guaranteed to preserve 32 bits.
80 * (i.e. it's used to store 32-bit ints in pointers, but
81 * can't be used to store 64-bit pointers in ints.)
83 * @param pointer pointer to extract an integer from.
86 * @def _DBUS_INT_TO_POINTER
88 * Safely stuffs an integer into a pointer, to be extracted later with
89 * _DBUS_POINTER_TO_INT. Only guaranteed to preserve 32 bits.
91 * @param integer the integer to stuff into a pointer.
96 * Sets all bits in an object to zero.
98 * @param object the object to be zeroed.
101 * @def _DBUS_INT16_MIN
103 * Minimum value of type "int16"
106 * @def _DBUS_INT16_MAX
108 * Maximum value of type "int16"
111 * @def _DBUS_UINT16_MAX
113 * Maximum value of type "uint16"
117 * @def _DBUS_INT32_MIN
119 * Minimum value of type "int32"
122 * @def _DBUS_INT32_MAX
124 * Maximum value of type "int32"
127 * @def _DBUS_UINT32_MAX
129 * Maximum value of type "uint32"
135 * Minimum value of type "int"
140 * Maximum value of type "int"
143 * @def _DBUS_UINT_MAX
145 * Maximum value of type "uint"
149 * @typedef DBusForeachFunction
151 * Used to iterate over each item in a collection, such as
156 * @def _DBUS_LOCK_NAME
158 * Expands to name of a global lock variable.
162 * @def _DBUS_DEFINE_GLOBAL_LOCK
164 * Defines a global lock variable with the given name.
165 * The lock must be added to the list to initialize
166 * in dbus_threads_init().
170 * @def _DBUS_DECLARE_GLOBAL_LOCK
172 * Expands to declaration of a global lock defined
173 * with _DBUS_DEFINE_GLOBAL_LOCK.
174 * The lock must be added to the list to initialize
175 * in dbus_threads_init().
181 * Locks a global lock
187 * Unlocks a global lock
191 * Fixed "out of memory" error message, just to avoid
192 * making up a different string every time and wasting
195 const char _dbus_no_memory_message[] = "Not enough memory";
198 * Prints a warning message to stderr.
200 * @param format printf-style format string.
203 _dbus_warn (const char *format,
206 /* FIXME not portable enough? */
209 va_start (args, format);
210 vfprintf (stderr, format, args);
214 #ifdef DBUS_ENABLE_VERBOSE_MODE
216 static dbus_bool_t verbose_initted = FALSE;
218 #define PTHREAD_IN_VERBOSE 0
219 #if PTHREAD_IN_VERBOSE
224 * Prints a warning message to stderr
225 * if the user has enabled verbose mode.
226 * This is the real function implementation,
227 * use _dbus_verbose() macro in code.
229 * @param format printf-style format string.
232 _dbus_verbose_real (const char *format,
236 static dbus_bool_t verbose = TRUE;
237 static dbus_bool_t need_pid = TRUE;
240 /* things are written a bit oddly here so that
241 * in the non-verbose case we just have the one
242 * conditional and return immediately.
247 if (!verbose_initted)
249 verbose = _dbus_getenv ("DBUS_VERBOSE") != NULL;
250 verbose_initted = TRUE;
255 /* Print out pid before the line */
258 #if PTHREAD_IN_VERBOSE
259 fprintf (stderr, "%lu: 0x%lx: ", _dbus_getpid (), pthread_self ());
261 fprintf (stderr, "%lu: ", _dbus_getpid ());
266 /* Only print pid again if the next line is a new line */
267 len = strlen (format);
268 if (format[len-1] == '\n')
273 va_start (args, format);
274 vfprintf (stderr, format, args);
281 * Reinitializes the verbose logging code, used
282 * as a hack in dbus-spawn.c so that a child
283 * process re-reads its pid
287 _dbus_verbose_reset_real (void)
289 verbose_initted = FALSE;
292 #endif /* DBUS_ENABLE_VERBOSE_MODE */
295 * Duplicates a string. Result must be freed with
296 * dbus_free(). Returns #NULL if memory allocation fails.
297 * If the string to be duplicated is #NULL, returns #NULL.
299 * @param str string to duplicate.
300 * @returns newly-allocated copy.
303 _dbus_strdup (const char *str)
313 copy = dbus_malloc (len + 1);
317 memcpy (copy, str, len + 1);
323 * Duplicates a block of memory. Returns
326 * @param mem memory to copy
327 * @param n_bytes number of bytes to copy
331 _dbus_memdup (const void *mem,
336 copy = dbus_malloc (n_bytes);
340 memcpy (copy, mem, n_bytes);
346 * Duplicates a string array. Result may be freed with
347 * dbus_free_string_array(). Returns #NULL if memory allocation fails.
348 * If the array to be duplicated is #NULL, returns #NULL.
350 * @param array array to duplicate.
351 * @returns newly-allocated copy.
354 _dbus_dup_string_array (const char **array)
363 for (len = 0; array[len] != NULL; ++len)
366 copy = dbus_new0 (char*, len + 1);
373 copy[i] = _dbus_strdup (array[i]);
376 dbus_free_string_array (copy);
387 * Checks whether a string array contains the given string.
389 * @param array array to search.
390 * @param str string to look for
391 * @returns #TRUE if array contains string
394 _dbus_string_array_contains (const char **array,
400 while (array[i] != NULL)
402 if (strcmp (array[i], str) == 0)
410 #ifdef DBUS_BUILD_TESTS
412 * Returns a string describing the given name.
414 * @param header_field the field to describe
415 * @returns a constant string describing the field
418 _dbus_header_field_to_string (int header_field)
420 switch (header_field)
422 case DBUS_HEADER_FIELD_INVALID:
424 case DBUS_HEADER_FIELD_PATH:
426 case DBUS_HEADER_FIELD_INTERFACE:
428 case DBUS_HEADER_FIELD_MEMBER:
430 case DBUS_HEADER_FIELD_ERROR_NAME:
432 case DBUS_HEADER_FIELD_REPLY_SERIAL:
433 return "reply-serial";
434 case DBUS_HEADER_FIELD_DESTINATION:
435 return "destination";
436 case DBUS_HEADER_FIELD_SENDER:
438 case DBUS_HEADER_FIELD_SIGNATURE:
444 #endif /* DBUS_BUILD_TESTS */
446 #ifndef DBUS_DISABLE_CHECKS
447 /** String used in _dbus_return_if_fail macro */
448 const char _dbus_return_if_fail_warning_format[] =
449 "%lu: arguments to %s() were incorrect, assertion \"%s\" failed in file %s line %d.\n"
450 "This is normally a bug in some application using the D-Bus library.\n";
453 #ifndef DBUS_DISABLE_ASSERT
455 * Internals of _dbus_assert(); it's a function
456 * rather than a macro with the inline code so
457 * that the assertion failure blocks don't show up
458 * in test suite coverage, and to shrink code size.
460 * @param condition TRUE if assertion succeeded
461 * @param condition_text condition as a string
462 * @param file file the assertion is in
463 * @param line line the assertion is in
464 * @param func function the assertion is in
467 _dbus_real_assert (dbus_bool_t condition,
468 const char *condition_text,
473 if (_DBUS_UNLIKELY (!condition))
475 _dbus_warn ("%lu: assertion failed \"%s\" file \"%s\" line %d function %s\n",
476 _dbus_getpid (), condition_text, file, line, func);
482 * Internals of _dbus_assert_not_reached(); it's a function
483 * rather than a macro with the inline code so
484 * that the assertion failure blocks don't show up
485 * in test suite coverage, and to shrink code size.
487 * @param explanation what was reached that shouldn't have been
488 * @param file file the assertion is in
489 * @param line line the assertion is in
492 _dbus_real_assert_not_reached (const char *explanation,
496 _dbus_warn ("File \"%s\" line %d process %lu should not have been reached: %s\n",
497 file, line, _dbus_getpid (), explanation);
500 #endif /* DBUS_DISABLE_ASSERT */
502 #ifdef DBUS_BUILD_TESTS
504 run_failing_each_malloc (int n_mallocs,
505 const char *description,
506 DBusTestMemoryFunction func,
509 n_mallocs += 10; /* fudge factor to ensure reallocs etc. are covered */
511 while (n_mallocs >= 0)
513 _dbus_set_fail_alloc_counter (n_mallocs);
515 _dbus_verbose ("\n===\n%s: (will fail malloc %d with %d failures)\n===\n",
516 description, n_mallocs,
517 _dbus_get_fail_alloc_failures ());
519 if (!(* func) (data))
525 _dbus_set_fail_alloc_counter (_DBUS_INT_MAX);
531 * Tests how well the given function responds to out-of-memory
532 * situations. Calls the function repeatedly, failing a different
533 * call to malloc() each time. If the function ever returns #FALSE,
534 * the test fails. The function should return #TRUE whenever something
535 * valid (such as returning an error, or succeeding) occurs, and #FALSE
536 * if it gets confused in some way.
538 * @param description description of the test used in verbose output
539 * @param func function to call
540 * @param data data to pass to function
541 * @returns #TRUE if the function never returns FALSE
544 _dbus_test_oom_handling (const char *description,
545 DBusTestMemoryFunction func,
550 int max_failures_to_try;
553 /* Run once to see about how many mallocs are involved */
555 _dbus_set_fail_alloc_counter (_DBUS_INT_MAX);
557 _dbus_verbose ("Running once to count mallocs\n");
559 if (!(* func) (data))
562 approx_mallocs = _DBUS_INT_MAX - _dbus_get_fail_alloc_counter ();
564 _dbus_verbose ("\n=================\n%s: about %d mallocs total\n=================\n",
565 description, approx_mallocs);
567 setting = _dbus_getenv ("DBUS_TEST_MALLOC_FAILURES");
572 _dbus_string_init_const (&str, setting);
574 if (!_dbus_string_parse_int (&str, 0, &v, NULL))
575 _dbus_warn ("couldn't parse '%s' as integer\n", setting);
576 max_failures_to_try = v;
580 max_failures_to_try = 4;
583 i = setting ? max_failures_to_try - 1 : 1;
584 while (i < max_failures_to_try)
586 _dbus_set_fail_alloc_failures (i);
587 if (!run_failing_each_malloc (approx_mallocs, description, func, data))
592 _dbus_verbose ("\n=================\n%s: all iterations passed\n=================\n",
597 #endif /* DBUS_BUILD_TESTS */