2006-09-30 Havoc Pennington <hp@redhat.com>
[platform/upstream/dbus.git] / dbus / dbus-internals.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-internals.c  random utility stuff (internal to D-Bus implementation)
3  *
4  * Copyright (C) 2002, 2003  Red Hat, Inc.
5  *
6  * Licensed under the Academic Free License version 2.1
7  * 
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.
12  *
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.
17  * 
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
21  *
22  */
23 #include "dbus-internals.h"
24 #include "dbus-protocol.h"
25 #include "dbus-test.h"
26 #include <stdio.h>
27 #include <stdarg.h>
28 #include <string.h>
29 #include <stdlib.h>
30
31 /**
32  * @defgroup DBusInternals D-Bus internal implementation details
33  * @brief Documentation useful when developing or debugging D-Bus itself.
34  * 
35  */
36
37 /**
38  * @defgroup DBusInternalsUtils Utilities and portability
39  * @ingroup DBusInternals
40  * @brief Utility functions (_dbus_assert(), _dbus_warn(), etc.)
41  * @{
42  */
43
44 /**
45  * @def _dbus_assert
46  *
47  * Aborts with an error message if the condition is false.
48  * 
49  * @param condition condition which must be true.
50  */
51
52 /**
53  * @def _dbus_assert_not_reached
54  *
55  * Aborts with an error message if called.
56  * The given explanation will be printed.
57  * 
58  * @param explanation explanation of what happened if the code was reached.
59  */
60
61 /**
62  * @def _DBUS_N_ELEMENTS
63  *
64  * Computes the number of elements in a fixed-size array using
65  * sizeof().
66  *
67  * @param array the array to count elements in.
68  */
69
70 /**
71  * @def _DBUS_POINTER_TO_INT
72  *
73  * Safely casts a void* to an integer; should only be used on void*
74  * that actually contain integers, for example one created with
75  * _DBUS_INT_TO_POINTER.  Only guaranteed to preserve 32 bits.
76  * (i.e. it's used to store 32-bit ints in pointers, but
77  * can't be used to store 64-bit pointers in ints.)
78  *
79  * @param pointer pointer to extract an integer from.
80  */
81 /**
82  * @def _DBUS_INT_TO_POINTER
83  *
84  * Safely stuffs an integer into a pointer, to be extracted later with
85  * _DBUS_POINTER_TO_INT. Only guaranteed to preserve 32 bits.
86  *
87  * @param integer the integer to stuff into a pointer.
88  */
89 /**
90  * @def _DBUS_ZERO
91  *
92  * Sets all bits in an object to zero.
93  *
94  * @param object the object to be zeroed.
95  */
96 /**
97  * @def _DBUS_INT16_MIN
98  *
99  * Minimum value of type "int16"
100  */
101 /**
102  * @def _DBUS_INT16_MAX
103  *
104  * Maximum value of type "int16"
105  */
106 /**
107  * @def _DBUS_UINT16_MAX
108  *
109  * Maximum value of type "uint16"
110  */
111
112 /**
113  * @def _DBUS_INT32_MIN
114  *
115  * Minimum value of type "int32"
116  */
117 /**
118  * @def _DBUS_INT32_MAX
119  *
120  * Maximum value of type "int32"
121  */
122 /**
123  * @def _DBUS_UINT32_MAX
124  *
125  * Maximum value of type "uint32"
126  */
127
128 /**
129  * @def _DBUS_INT_MIN
130  *
131  * Minimum value of type "int"
132  */
133 /**
134  * @def _DBUS_INT_MAX
135  *
136  * Maximum value of type "int"
137  */
138 /**
139  * @def _DBUS_UINT_MAX
140  *
141  * Maximum value of type "uint"
142  */
143
144 /**
145  * @typedef DBusForeachFunction
146  * 
147  * Used to iterate over each item in a collection, such as
148  * a DBusList.
149  */
150
151 /**
152  * @def _DBUS_LOCK_NAME
153  *
154  * Expands to name of a global lock variable.
155  */
156
157 /**
158  * @def _DBUS_DEFINE_GLOBAL_LOCK
159  *
160  * Defines a global lock variable with the given name.
161  * The lock must be added to the list to initialize
162  * in dbus_threads_init().
163  */
164
165 /**
166  * @def _DBUS_DECLARE_GLOBAL_LOCK
167  *
168  * Expands to declaration of a global lock defined
169  * with _DBUS_DEFINE_GLOBAL_LOCK.
170  * The lock must be added to the list to initialize
171  * in dbus_threads_init().
172  */
173
174 /**
175  * @def _DBUS_LOCK
176  *
177  * Locks a global lock
178  */
179
180 /**
181  * @def _DBUS_UNLOCK
182  *
183  * Unlocks a global lock
184  */
185
186 /**
187  * Fixed "out of memory" error message, just to avoid
188  * making up a different string every time and wasting
189  * space.
190  */
191 const char _dbus_no_memory_message[] = "Not enough memory";
192
193 /**
194  * Prints a warning message to stderr.
195  *
196  * @param format printf-style format string.
197  */
198 void
199 _dbus_warn (const char *format,
200             ...)
201 {
202   /* FIXME not portable enough? */
203   va_list args;
204
205   va_start (args, format);
206   vfprintf (stderr, format, args);
207   va_end (args);
208 }
209
210 #ifdef DBUS_ENABLE_VERBOSE_MODE
211
212 static dbus_bool_t verbose_initted = FALSE;
213 static dbus_bool_t verbose = TRUE;
214
215 #define PTHREAD_IN_VERBOSE 0
216 #if PTHREAD_IN_VERBOSE
217 #include <pthread.h>
218 #endif
219
220 static inline void
221 _dbus_verbose_init (void)
222 {
223   if (!verbose_initted)
224     {
225       const char *p = _dbus_getenv ("DBUS_VERBOSE"); 
226       verbose = p != NULL && *p == '1';
227       verbose_initted = TRUE;
228     }
229 }
230
231 dbus_bool_t
232 _dbus_is_verbose_real (void)
233 {
234   _dbus_verbose_init ();
235   return verbose;
236 }
237
238 /**
239  * Prints a warning message to stderr
240  * if the user has enabled verbose mode.
241  * This is the real function implementation,
242  * use _dbus_verbose() macro in code.
243  *
244  * @param format printf-style format string.
245  */
246 void
247 _dbus_verbose_real (const char *format,
248                     ...)
249 {
250   va_list args;
251   static dbus_bool_t need_pid = TRUE;
252   int len;
253   
254   /* things are written a bit oddly here so that
255    * in the non-verbose case we just have the one
256    * conditional and return immediately.
257    */
258   if (!_dbus_is_verbose_real())
259     return;
260
261   /* Print out pid before the line */
262   if (need_pid)
263     {
264 #if PTHREAD_IN_VERBOSE
265       fprintf (stderr, "%lu: 0x%lx: ", _dbus_getpid (), pthread_self ());
266 #else
267       fprintf (stderr, "%lu: ", _dbus_getpid ());
268 #endif
269     }
270       
271
272   /* Only print pid again if the next line is a new line */
273   len = strlen (format);
274   if (format[len-1] == '\n')
275     need_pid = TRUE;
276   else
277     need_pid = FALSE;
278   
279   va_start (args, format);
280   vfprintf (stderr, format, args);
281   va_end (args);
282
283   fflush (stderr);
284 }
285
286 /**
287  * Reinitializes the verbose logging code, used
288  * as a hack in dbus-spawn.c so that a child
289  * process re-reads its pid
290  *
291  */
292 void
293 _dbus_verbose_reset_real (void)
294 {
295   verbose_initted = FALSE;
296 }
297
298 #endif /* DBUS_ENABLE_VERBOSE_MODE */
299
300 /**
301  * Duplicates a string. Result must be freed with
302  * dbus_free(). Returns #NULL if memory allocation fails.
303  * If the string to be duplicated is #NULL, returns #NULL.
304  * 
305  * @param str string to duplicate.
306  * @returns newly-allocated copy.
307  */
308 char*
309 _dbus_strdup (const char *str)
310 {
311   size_t len;
312   char *copy;
313   
314   if (str == NULL)
315     return NULL;
316   
317   len = strlen (str);
318
319   copy = dbus_malloc (len + 1);
320   if (copy == NULL)
321     return NULL;
322
323   memcpy (copy, str, len + 1);
324   
325   return copy;
326 }
327
328 /**
329  * Duplicates a block of memory. Returns
330  * #NULL on failure.
331  *
332  * @param mem memory to copy
333  * @param n_bytes number of bytes to copy
334  * @returns the copy
335  */
336 void*
337 _dbus_memdup (const void  *mem,
338               size_t       n_bytes)
339 {
340   void *copy;
341
342   copy = dbus_malloc (n_bytes);
343   if (copy == NULL)
344     return NULL;
345
346   memcpy (copy, mem, n_bytes);
347   
348   return copy;
349 }
350
351 /**
352  * Duplicates a string array. Result may be freed with
353  * dbus_free_string_array(). Returns #NULL if memory allocation fails.
354  * If the array to be duplicated is #NULL, returns #NULL.
355  * 
356  * @param array array to duplicate.
357  * @returns newly-allocated copy.
358  */
359 char**
360 _dbus_dup_string_array (const char **array)
361 {
362   int len;
363   int i;
364   char **copy;
365   
366   if (array == NULL)
367     return NULL;
368
369   for (len = 0; array[len] != NULL; ++len)
370     ;
371
372   copy = dbus_new0 (char*, len + 1);
373   if (copy == NULL)
374     return NULL;
375
376   i = 0;
377   while (i < len)
378     {
379       copy[i] = _dbus_strdup (array[i]);
380       if (copy[i] == NULL)
381         {
382           dbus_free_string_array (copy);
383           return NULL;
384         }
385
386       ++i;
387     }
388
389   return copy;
390 }
391
392 /**
393  * Checks whether a string array contains the given string.
394  * 
395  * @param array array to search.
396  * @param str string to look for
397  * @returns #TRUE if array contains string
398  */
399 dbus_bool_t
400 _dbus_string_array_contains (const char **array,
401                              const char  *str)
402 {
403   int i;
404
405   i = 0;
406   while (array[i] != NULL)
407     {
408       if (strcmp (array[i], str) == 0)
409         return TRUE;
410       ++i;
411     }
412
413   return FALSE;
414 }
415
416 /**
417  * Generates a new UUID. If you change how this is done,
418  * there's some text about it in the spec that should also change.
419  *
420  * @param uuid the uuid to initialize
421  */
422 void
423 _dbus_generate_uuid (DBusGUID *uuid)
424 {
425   long now;
426   char *p;
427   int ts_size;
428
429   _dbus_get_current_time (&now, NULL);
430
431   uuid->as_uint32s[0] = now;
432
433   ts_size = sizeof (uuid->as_uint32s[0]);
434   p = ((char*)uuid->as_bytes) + ts_size;
435   
436   _dbus_generate_random_bytes_buffer (p,
437                                       sizeof (uuid->as_bytes) - ts_size);
438 }
439
440 /**
441  * Hex-encode a UUID.
442  *
443  * @param uuid the uuid
444  * @param encoded string to append hex uuid to
445  * @returns #FALSE if no memory
446  */
447 dbus_bool_t
448 _dbus_uuid_encode (const DBusGUID *uuid,
449                    DBusString     *encoded)
450 {
451   DBusString binary;
452   _dbus_string_init_const_len (&binary, uuid->as_bytes, DBUS_UUID_LENGTH_BYTES);
453   return _dbus_string_hex_encode (&binary, 0, encoded, _dbus_string_get_length (encoded));
454 }
455
456 static dbus_bool_t
457 _dbus_read_uuid_file_without_creating (const DBusString *filename,
458                                        DBusGUID         *uuid,
459                                        DBusError        *error)
460 {
461   DBusString contents;
462   DBusString decoded;
463   int end;
464   
465   _dbus_string_init (&contents);
466   _dbus_string_init (&decoded);
467   
468   if (!_dbus_file_get_contents (&contents, filename, error))
469     goto error;
470
471   _dbus_string_chop_white (&contents);
472
473   if (_dbus_string_get_length (&contents) != DBUS_UUID_LENGTH_HEX)
474     {
475       dbus_set_error (error, DBUS_ERROR_INVALID_FILE_CONTENT,
476                       "UUID file '%s' should contain a hex string of length %d, not length %d, with no other text",
477                       _dbus_string_get_const_data (filename),
478                       DBUS_UUID_LENGTH_HEX,
479                       _dbus_string_get_length (&contents));
480       goto error;
481     }
482
483   if (!_dbus_string_hex_decode (&contents, 0, &end, &decoded, 0))
484     {
485       _DBUS_SET_OOM (error);
486       goto error;
487     }
488
489   if (end == 0)
490     {
491       dbus_set_error (error, DBUS_ERROR_INVALID_FILE_CONTENT,
492                       "UUID file '%s' contains invalid hex data",
493                       _dbus_string_get_const_data (filename));
494       goto error;
495     }
496
497   if (_dbus_string_get_length (&decoded) != DBUS_UUID_LENGTH_BYTES)
498     {
499       dbus_set_error (error, DBUS_ERROR_INVALID_FILE_CONTENT,
500                       "UUID file '%s' contains %d bytes of hex-encoded data instead of %d",
501                       _dbus_string_get_const_data (filename),
502                       _dbus_string_get_length (&decoded),
503                       DBUS_UUID_LENGTH_BYTES);
504       goto error;
505     }
506
507   _dbus_string_copy_to_buffer (&decoded, uuid->as_bytes, DBUS_UUID_LENGTH_BYTES);
508
509   _dbus_string_free (&decoded);
510   _dbus_string_free (&contents);
511
512   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
513
514   return TRUE;
515   
516  error:
517   _DBUS_ASSERT_ERROR_IS_SET (error);
518   _dbus_string_free (&contents);
519   _dbus_string_free (&decoded);
520   return FALSE;
521 }
522
523 static dbus_bool_t
524 _dbus_create_uuid_file_exclusively (const DBusString *filename,
525                                     DBusGUID         *uuid,
526                                     DBusError        *error)
527 {
528   DBusString encoded;
529
530   _dbus_string_init (&encoded);
531
532   _dbus_generate_uuid (uuid);
533   
534   if (!_dbus_uuid_encode (uuid, &encoded))
535     {
536       _DBUS_SET_OOM (error);
537       goto error;
538     }
539   
540   /* FIXME this is racy; we need a save_file_exclusively
541    * function. But in practice this should be fine for now.
542    *
543    * - first be sure we can create the file and it
544    *   doesn't exist by creating it empty with O_EXCL
545    * - then create it by creating a temporary file and
546    *   overwriting atomically with rename()
547    */
548   if (!_dbus_create_file_exclusively (filename, error))
549     goto error;
550
551   if (!_dbus_string_append_byte (&encoded, '\n'))
552     {
553       _DBUS_SET_OOM (error);
554       goto error;
555     }
556   
557   if (!_dbus_string_save_to_file (&encoded, filename, error))
558     goto error;
559
560
561   _dbus_string_free (&encoded);
562
563   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
564   return TRUE;
565   
566  error:
567   _DBUS_ASSERT_ERROR_IS_SET (error);
568   _dbus_string_free (&encoded);
569   return FALSE;        
570 }
571
572 /**
573  * Reads (and optionally writes) a uuid to a file. Initializes the uuid
574  * unless an error is returned.
575  *
576  * @param filename the name of the file
577  * @param uuid uuid to be initialized with the loaded uuid
578  * @param create_if_not_found #TRUE to create a new uuid and save it if the file doesn't exist
579  * @param error the error return
580  * @returns #FALSE if the error is set
581  */
582 dbus_bool_t
583 _dbus_read_uuid_file (const DBusString *filename,
584                       DBusGUID         *uuid,
585                       dbus_bool_t       create_if_not_found,
586                       DBusError        *error)
587 {
588   DBusError read_error;
589
590   dbus_error_init (&read_error);
591
592   if (_dbus_read_uuid_file_without_creating (filename, uuid, &read_error))
593     return TRUE;
594
595   if (!create_if_not_found)
596     {
597       dbus_move_error (&read_error, error);
598       return FALSE;
599     }
600
601   /* If the file exists and contains junk, we want to keep that error
602    * message instead of overwriting it with a "file exists" error
603    * message when we try to write
604    */
605   if (dbus_error_has_name (&read_error, DBUS_ERROR_INVALID_FILE_CONTENT))
606     {
607       dbus_move_error (&read_error, error);
608       return FALSE;
609     }
610   else
611     {
612       dbus_error_free (&read_error);
613       return _dbus_create_uuid_file_exclusively (filename, uuid, error);
614     }
615 }
616
617 _DBUS_DEFINE_GLOBAL_LOCK (machine_uuid);
618 static int machine_uuid_initialized_generation = 0;
619 static DBusGUID machine_uuid;
620
621 /**
622  * Gets the hex-encoded UUID of the machine this function is
623  * executed on. This UUID is guaranteed to be the same for a given
624  * machine at least until it next reboots, though it also
625  * makes some effort to be the same forever, it may change if the
626  * machine is reconfigured or its hardware is modified.
627  * 
628  * @param uuid_str string to append hex-encoded machine uuid to
629  * @returns #FALSE if no memory
630  */
631 dbus_bool_t
632 _dbus_get_local_machine_uuid_encoded (DBusString *uuid_str)
633 {
634   dbus_bool_t ok;
635   
636   _DBUS_LOCK (machine_uuid);
637   if (machine_uuid_initialized_generation != _dbus_current_generation)
638     {
639       DBusError error;
640       dbus_error_init (&error);
641       if (!_dbus_read_local_machine_uuid (&machine_uuid, FALSE,
642                                           &error))
643         {          
644 #ifndef DBUS_BUILD_TESTS
645           /* For the test suite, we may not be installed so just continue silently
646            * here. But in a production build, we want to be nice and loud about
647            * this.
648            */
649           _dbus_warn ("D-Bus library appears to be incorrectly set up; failed to read machine uuid: %s\n",
650                       error.message);
651           _dbus_warn ("See the manual page for dbus-uuidgen to correct this issue.\n");
652           _dbus_warn ("Continuing with a bogus made-up machine UUID, which may cause problems.");
653 #endif
654           
655           dbus_error_free (&error);
656           
657           _dbus_generate_uuid (&machine_uuid);
658         }
659     }
660
661   ok = _dbus_uuid_encode (&machine_uuid, uuid_str);
662
663   _DBUS_UNLOCK (machine_uuid);
664
665   return ok;
666 }
667
668 #ifdef DBUS_BUILD_TESTS
669 /**
670  * Returns a string describing the given name.
671  *
672  * @param header_field the field to describe
673  * @returns a constant string describing the field
674  */
675 const char *
676 _dbus_header_field_to_string (int header_field)
677 {
678   switch (header_field)
679     {
680     case DBUS_HEADER_FIELD_INVALID:
681       return "invalid";
682     case DBUS_HEADER_FIELD_PATH:
683       return "path";
684     case DBUS_HEADER_FIELD_INTERFACE:
685       return "interface";
686     case DBUS_HEADER_FIELD_MEMBER:
687       return "member";
688     case DBUS_HEADER_FIELD_ERROR_NAME:
689       return "error-name";
690     case DBUS_HEADER_FIELD_REPLY_SERIAL:
691       return "reply-serial";
692     case DBUS_HEADER_FIELD_DESTINATION:
693       return "destination";
694     case DBUS_HEADER_FIELD_SENDER:
695       return "sender";
696     case DBUS_HEADER_FIELD_SIGNATURE:
697       return "signature";
698     default:
699       return "unknown";
700     }
701 }
702 #endif /* DBUS_BUILD_TESTS */
703
704 #ifndef DBUS_DISABLE_CHECKS
705 /** String used in _dbus_return_if_fail macro */
706 const char _dbus_return_if_fail_warning_format[] =
707 "%lu: arguments to %s() were incorrect, assertion \"%s\" failed in file %s line %d.\n"
708 "This is normally a bug in some application using the D-Bus library.\n";
709 #endif
710
711 #ifndef DBUS_DISABLE_ASSERT
712 /**
713  * Internals of _dbus_assert(); it's a function
714  * rather than a macro with the inline code so
715  * that the assertion failure blocks don't show up
716  * in test suite coverage, and to shrink code size.
717  *
718  * @param condition TRUE if assertion succeeded
719  * @param condition_text condition as a string
720  * @param file file the assertion is in
721  * @param line line the assertion is in
722  * @param func function the assertion is in
723  */
724 void
725 _dbus_real_assert (dbus_bool_t  condition,
726                    const char  *condition_text,
727                    const char  *file,
728                    int          line,
729                    const char  *func)
730 {
731   if (_DBUS_UNLIKELY (!condition))
732     {
733       _dbus_warn ("%lu: assertion failed \"%s\" file \"%s\" line %d function %s\n",
734                   _dbus_getpid (), condition_text, file, line, func);
735       _dbus_abort ();
736     }
737 }
738
739 /**
740  * Internals of _dbus_assert_not_reached(); it's a function
741  * rather than a macro with the inline code so
742  * that the assertion failure blocks don't show up
743  * in test suite coverage, and to shrink code size.
744  *
745  * @param explanation what was reached that shouldn't have been
746  * @param file file the assertion is in
747  * @param line line the assertion is in
748  */
749 void
750 _dbus_real_assert_not_reached (const char *explanation,
751                                const char *file,
752                                int         line)
753 {
754   _dbus_warn ("File \"%s\" line %d process %lu should not have been reached: %s\n",
755               file, line, _dbus_getpid (), explanation);
756   _dbus_abort ();
757 }
758 #endif /* DBUS_DISABLE_ASSERT */
759   
760 #ifdef DBUS_BUILD_TESTS
761 static dbus_bool_t
762 run_failing_each_malloc (int                    n_mallocs,
763                          const char            *description,
764                          DBusTestMemoryFunction func,
765                          void                  *data)
766 {
767   n_mallocs += 10; /* fudge factor to ensure reallocs etc. are covered */
768   
769   while (n_mallocs >= 0)
770     {      
771       _dbus_set_fail_alloc_counter (n_mallocs);
772
773       _dbus_verbose ("\n===\n%s: (will fail malloc %d with %d failures)\n===\n",
774                      description, n_mallocs,
775                      _dbus_get_fail_alloc_failures ());
776
777       if (!(* func) (data))
778         return FALSE;
779       
780       n_mallocs -= 1;
781     }
782
783   _dbus_set_fail_alloc_counter (_DBUS_INT_MAX);
784
785   return TRUE;
786 }                        
787
788 /**
789  * Tests how well the given function responds to out-of-memory
790  * situations. Calls the function repeatedly, failing a different
791  * call to malloc() each time. If the function ever returns #FALSE,
792  * the test fails. The function should return #TRUE whenever something
793  * valid (such as returning an error, or succeeding) occurs, and #FALSE
794  * if it gets confused in some way.
795  *
796  * @param description description of the test used in verbose output
797  * @param func function to call
798  * @param data data to pass to function
799  * @returns #TRUE if the function never returns FALSE
800  */
801 dbus_bool_t
802 _dbus_test_oom_handling (const char             *description,
803                          DBusTestMemoryFunction  func,
804                          void                   *data)
805 {
806   int approx_mallocs;
807   const char *setting;
808   int max_failures_to_try;
809   int i;
810
811   /* Run once to see about how many mallocs are involved */
812   
813   _dbus_set_fail_alloc_counter (_DBUS_INT_MAX);
814
815   _dbus_verbose ("Running once to count mallocs\n");
816   
817   if (!(* func) (data))
818     return FALSE;
819   
820   approx_mallocs = _DBUS_INT_MAX - _dbus_get_fail_alloc_counter ();
821
822   _dbus_verbose ("\n=================\n%s: about %d mallocs total\n=================\n",
823                  description, approx_mallocs);
824
825   setting = _dbus_getenv ("DBUS_TEST_MALLOC_FAILURES");
826   if (setting != NULL)
827     {
828       DBusString str;
829       long v;
830       _dbus_string_init_const (&str, setting);
831       v = 4;
832       if (!_dbus_string_parse_int (&str, 0, &v, NULL))
833         _dbus_warn ("couldn't parse '%s' as integer\n", setting);
834       max_failures_to_try = v;
835     }
836   else
837     {
838       max_failures_to_try = 4;
839     }
840
841   i = setting ? max_failures_to_try - 1 : 1;
842   while (i < max_failures_to_try)
843     {
844       _dbus_set_fail_alloc_failures (i);
845       if (!run_failing_each_malloc (approx_mallocs, description, func, data))
846         return FALSE;
847       ++i;
848     }
849   
850   _dbus_verbose ("\n=================\n%s: all iterations passed\n=================\n",
851                  description);
852
853   return TRUE;
854 }
855 #endif /* DBUS_BUILD_TESTS */
856
857 /** @} */