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 1.2
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.
103 * Minimum value of type "int"
108 * Maximum value of type "int"
112 * @typedef DBusForeachFunction
114 * Used to iterate over each item in a collection, such as
119 * @def _DBUS_LOCK_NAME
121 * Expands to name of a global lock variable.
125 * @def _DBUS_DEFINE_GLOBAL_LOCK
127 * Defines a global lock variable with the given name.
128 * The lock must be added to the list to initialize
129 * in dbus_threads_init().
133 * @def _DBUS_DECLARE_GLOBAL_LOCK
135 * Expands to declaration of a global lock defined
136 * with _DBUS_DEFINE_GLOBAL_LOCK.
137 * The lock must be added to the list to initialize
138 * in dbus_threads_init().
144 * Locks a global lock
150 * Unlocks a global lock
154 * Fixed "out of memory" error message, just to avoid
155 * making up a different string every time and wasting
158 const char _dbus_no_memory_message[] = "Not enough memory";
161 * Prints a warning message to stderr.
163 * @param format printf-style format string.
166 _dbus_warn (const char *format,
169 /* FIXME not portable enough? */
172 va_start (args, format);
173 vfprintf (stderr, format, args);
177 static dbus_bool_t verbose_initted = FALSE;
180 * Prints a warning message to stderr
181 * if the user has enabled verbose mode.
182 * This is the real function implementation,
183 * use _dbus_verbose() macro in code.
185 * @param format printf-style format string.
188 _dbus_verbose_real (const char *format,
192 static dbus_bool_t verbose = TRUE;
193 static dbus_bool_t need_pid = TRUE;
195 /* things are written a bit oddly here so that
196 * in the non-verbose case we just have the one
197 * conditional and return immediately.
202 if (!verbose_initted)
204 verbose = _dbus_getenv ("DBUS_VERBOSE") != NULL;
205 verbose_initted = TRUE;
214 fprintf (stderr, "%lu: ", _dbus_getpid ());
216 len = strlen (format);
217 if (format[len-1] == '\n')
223 va_start (args, format);
224 vfprintf (stderr, format, args);
229 * Reinitializes the verbose logging code, used
230 * as a hack in dbus-spawn.c so that a child
231 * process re-reads its pid
235 _dbus_verbose_reset_real (void)
237 verbose_initted = FALSE;
241 * Duplicates a string. Result must be freed with
242 * dbus_free(). Returns #NULL if memory allocation fails.
243 * If the string to be duplicated is #NULL, returns #NULL.
245 * @param str string to duplicate.
246 * @returns newly-allocated copy.
249 _dbus_strdup (const char *str)
259 copy = dbus_malloc (len + 1);
263 memcpy (copy, str, len + 1);
269 * Duplicates a block of memory. Returns
272 * @param mem memory to copy
273 * @param n_bytes number of bytes to copy
277 _dbus_memdup (const void *mem,
282 copy = dbus_malloc (n_bytes);
286 memcpy (copy, mem, n_bytes);
292 * Duplicates a string array. Result may be freed with
293 * dbus_free_string_array(). Returns #NULL if memory allocation fails.
294 * If the array to be duplicated is #NULL, returns #NULL.
296 * @param array array to duplicate.
297 * @returns newly-allocated copy.
300 _dbus_dup_string_array (const char **array)
309 for (len = 0; array[len] != NULL; ++len)
312 copy = dbus_new0 (char*, len + 1);
319 copy[i] = _dbus_strdup (array[i]);
322 dbus_free_string_array (copy);
333 * Checks whether a string array contains the given string.
335 * @param array array to search.
336 * @param str string to look for
337 * @returns #TRUE if array contains string
340 _dbus_string_array_contains (const char **array,
346 while (array[i] != NULL)
348 if (strcmp (array[i], str) == 0)
357 * Returns a string describing the given type.
359 * @param type the type to describe
360 * @returns a constant string describing the type
363 _dbus_type_to_string (int type)
367 case DBUS_TYPE_INVALID:
371 case DBUS_TYPE_BOOLEAN:
373 case DBUS_TYPE_INT32:
375 case DBUS_TYPE_UINT32:
377 case DBUS_TYPE_DOUBLE:
379 case DBUS_TYPE_STRING:
381 case DBUS_TYPE_NAMED:
383 case DBUS_TYPE_ARRAY:
392 #ifndef DBUS_DISABLE_CHECKS
393 /** String used in _dbus_return_if_fail macro */
394 const char _dbus_return_if_fail_warning_format[] =
395 "Arguments to %s were incorrect, assertion \"%s\" failed in file %s line %d.\n"
396 "This is normally a bug in some application using the D-BUS library.\n";
399 #ifndef DBUS_DISABLE_ASSERT
401 * Internals of _dbus_assert(); it's a function
402 * rather than a macro with the inline code so
403 * that the assertion failure blocks don't show up
404 * in test suite coverage, and to shrink code size.
406 * @param condition TRUE if assertion succeeded
407 * @param condition_text condition as a string
408 * @param file file the assertion is in
409 * @param line line the assertion is in
412 _dbus_real_assert (dbus_bool_t condition,
413 const char *condition_text,
419 _dbus_warn ("Assertion failed \"%s\" file \"%s\" line %d process %lu\n",
420 condition_text, file, line, _dbus_getpid ());
426 * Internals of _dbus_assert_not_reached(); it's a function
427 * rather than a macro with the inline code so
428 * that the assertion failure blocks don't show up
429 * in test suite coverage, and to shrink code size.
431 * @param explanation what was reached that shouldn't have been
432 * @param file file the assertion is in
433 * @param line line the assertion is in
436 _dbus_real_assert_not_reached (const char *explanation,
440 _dbus_warn ("File \"%s\" line %d process %lu should not have been reached: %s\n",
441 file, line, _dbus_getpid (), explanation);
444 #endif /* DBUS_DISABLE_ASSERT */
446 #ifdef DBUS_BUILD_TESTS
448 run_failing_each_malloc (int n_mallocs,
449 const char *description,
450 DBusTestMemoryFunction func,
453 n_mallocs += 10; /* fudge factor to ensure reallocs etc. are covered */
455 while (n_mallocs >= 0)
457 _dbus_set_fail_alloc_counter (n_mallocs);
459 _dbus_verbose ("\n===\n%s: (will fail malloc %d with %d failures)\n===\n",
460 description, n_mallocs,
461 _dbus_get_fail_alloc_failures ());
463 if (!(* func) (data))
469 _dbus_set_fail_alloc_counter (_DBUS_INT_MAX);
475 * Tests how well the given function responds to out-of-memory
476 * situations. Calls the function repeatedly, failing a different
477 * call to malloc() each time. If the function ever returns #FALSE,
478 * the test fails. The function should return #TRUE whenever something
479 * valid (such as returning an error, or succeeding) occurs, and #FALSE
480 * if it gets confused in some way.
482 * @param description description of the test used in verbose output
483 * @param func function to call
484 * @param data data to pass to function
485 * @returns #TRUE if the function never returns FALSE
488 _dbus_test_oom_handling (const char *description,
489 DBusTestMemoryFunction func,
494 /* Run once to see about how many mallocs are involved */
496 _dbus_set_fail_alloc_counter (_DBUS_INT_MAX);
498 _dbus_verbose ("Running once to count mallocs\n");
500 if (!(* func) (data))
503 approx_mallocs = _DBUS_INT_MAX - _dbus_get_fail_alloc_counter ();
505 _dbus_verbose ("\n=================\n%s: about %d mallocs total\n=================\n",
506 description, approx_mallocs);
508 _dbus_set_fail_alloc_failures (1);
509 if (!run_failing_each_malloc (approx_mallocs, description, func, data))
512 _dbus_set_fail_alloc_failures (2);
513 if (!run_failing_each_malloc (approx_mallocs, description, func, data))
516 _dbus_set_fail_alloc_failures (3);
517 if (!run_failing_each_malloc (approx_mallocs, description, func, data))
520 _dbus_set_fail_alloc_failures (4);
521 if (!run_failing_each_malloc (approx_mallocs, description, func, data))
524 _dbus_verbose ("\n=================\n%s: all iterations passed\n=================\n",
529 #endif /* DBUS_BUILD_TESTS */