D-Bus  1.12.20
dbus-internals.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  */
23 
24 #include <config.h>
25 #include "dbus-internals.h"
26 #include "dbus-protocol.h"
27 #include "dbus-marshal-basic.h"
28 #include "dbus-test.h"
29 #include "dbus-valgrind-internal.h"
30 #include <stdio.h>
31 #include <stdarg.h>
32 #include <string.h>
33 #include <stdlib.h>
34 #ifdef DBUS_USE_OUTPUT_DEBUG_STRING
35 #include <windows.h>
36 #include <mbstring.h>
37 #endif
38 
39 
40 #ifdef HAVE_PDPLINUX
41 
42 #include <setjmp.h>
43 /*
44 #define TRY do{ jmp_buf ex_buf__; if( !setjmp(ex_buf__) ){
45 #define CATCH } else {
46 #define ETRY } }while(0)
47 #define THROW longjmp(ex_buf__, 1)
48 */
49 
50  #ifdef DBUS_ENABLE_VERBOSE_MODE
51 
52 // Option used at debug and release modes with DBUS_ENABLE_VERBOSE_MODE ONLY!
53 // TO force variable DBUS_VERBOSE=1 (if DBUS_ENABLE_VERBOSE_MODE => _dbus_verbose is only define)
54 
55 // This option send to config: HAVE_PDPLINUX_FORCE_ENABLE_DBUS_VERBOSE
56 // NB Look at option --enable-pdplinux-force-verbose | on
57 
58 
59 //option used only when DBUS_ENABLE_VERBOSE_MODE mode to use FILE LOGGING (also loves HAVE_PDPLINUX_FORCE_ENABLE_DBUS_VERBOSE)
60 //#define PDPLINUX_USE_FILELOG
61 
62  #ifdef PDPLINUX_USE_FILELOG
63 
64 //use special verbose command to close file log search => "!PDPLINUX_LOGCLOSE!"
65 #define PDPLINUX_LOGCLOSE "!PDPLINUX_LOGCLOSE!"
66 
67  #endif
68  #endif
69 
70 #endif
71 
217 const char *_dbus_no_memory_message = "Not enough memory";
218 
219 static dbus_bool_t warn_initted = FALSE;
220 static dbus_bool_t fatal_warnings = FALSE;
221 static dbus_bool_t fatal_warnings_on_check_failed = TRUE;
222 
223 static void
224 init_warnings(void)
225 {
226  if (!warn_initted)
227  {
228  const char *s;
229  s = _dbus_getenv ("DBUS_FATAL_WARNINGS");
230  if (s && *s)
231  {
232  if (*s == '0')
233  {
234  fatal_warnings = FALSE;
235  fatal_warnings_on_check_failed = FALSE;
236  }
237  else if (*s == '1')
238  {
239  fatal_warnings = TRUE;
240  fatal_warnings_on_check_failed = TRUE;
241  }
242  else
243  {
244  fprintf(stderr, "DBUS_FATAL_WARNINGS should be set to 0 or 1 if set, not '%s'",
245  s);
246  }
247  }
248 
249  warn_initted = TRUE;
250  }
251 }
252 
262 void
263 _dbus_warn (const char *format,
264  ...)
265 {
266  DBusSystemLogSeverity severity = DBUS_SYSTEM_LOG_WARNING;
267  va_list args;
268 
269  if (!warn_initted)
270  init_warnings ();
271 
272  if (fatal_warnings)
273  severity = DBUS_SYSTEM_LOG_ERROR;
274 
275  va_start (args, format);
276  _dbus_logv (severity, format, args);
277  va_end (args);
278 
279  if (fatal_warnings)
280  {
281  fflush (stderr);
282  _dbus_abort ();
283  }
284 }
285 
294 void
295 _dbus_warn_check_failed(const char *format,
296  ...)
297 {
298  DBusSystemLogSeverity severity = DBUS_SYSTEM_LOG_WARNING;
299  va_list args;
300 
301  if (!warn_initted)
302  init_warnings ();
303 
304  if (fatal_warnings_on_check_failed)
305  severity = DBUS_SYSTEM_LOG_ERROR;
306 
307  va_start (args, format);
308  _dbus_logv (severity, format, args);
309  va_end (args);
310 
311  if (fatal_warnings_on_check_failed)
312  {
313  fflush (stderr);
314  _dbus_abort ();
315  }
316 }
317 
318 #ifdef DBUS_ENABLE_VERBOSE_MODE
319 
320 static dbus_bool_t verbose_initted = FALSE;
321 static dbus_bool_t verbose = TRUE;
322 
323 #ifdef DBUS_USE_OUTPUT_DEBUG_STRING
324 static char module_name[1024];
325 #endif
326 
327 #ifdef HAVE_PDPLINUX
328 #include <time.h>
329 #include <unistd.h>
330 
331 #ifdef PDPLINUX_USE_FILELOG
332 static FILE* fdlog=0;
333 static char namelog[1024];
334 #endif
335 #endif
336 
337 static inline void
338 _dbus_verbose_init (void)
339 {
340  if (!verbose_initted)
341  {
342  const char *p = _dbus_getenv ("DBUS_VERBOSE");
343 
344  verbose = p != NULL && *p == '1';
345  verbose_initted = TRUE;
346 
347 #ifdef HAVE_PDPLINUX
348 #ifdef HAVE_PDPLINUX_FORCE_ENABLE_DBUS_VERBOSE
349  verbose=TRUE;
350 #endif
351 
352 #ifdef PDPLINUX_USE_FILELOG
353  if (!fdlog && verbose){
354  time_t rawtime;
355  PDPL_T* pdpl=NULL;
356  char* textlabel=NULL;
357  struct tm* timeinfo=NULL;
358  char sttime[80];
359 
360  time(&rawtime);
361  timeinfo=localtime(&rawtime);
362 
363  snprintf(sttime,sizeof(sttime),"notime");
364  if (timeinfo)
365  strftime (sttime,sizeof(sttime),"%F-%H%M%S",timeinfo);
366 
367  pdpl=pdp_get_current();
368  if (pdpl){
369  textlabel=pdpl_get_text(pdpl,PDPL_FMT_NUM);
370  }
371  if (snprintf(namelog,sizeof(namelog),"/dbuslog/dbus-%s-(%s)-%ld",
372  sttime,
373  textlabel ? textlabel : "-",
374  (long)getpid())>0){
375  char* pos=namelog;
376  while ('\0' != *pos && pos <= namelog+sizeof(namelog)/sizeof(*namelog)){
377  if (*pos == ':') *pos='-';
378  pos++;
379  }
380 
381  fdlog=fopen(namelog,"wt+");
382  }
383  if (textlabel) free(textlabel);
384  if (pdpl) pdpl_put(pdpl);
385  }
386 #endif
387 #endif
388 
389 #ifdef DBUS_USE_OUTPUT_DEBUG_STRING
390  {
391  char *last_period, *last_slash;
392  GetModuleFileName(0,module_name,sizeof(module_name)-1);
393  last_period = _mbsrchr(module_name,'.');
394  if (last_period)
395  *last_period ='\0';
396  last_slash = _mbsrchr(module_name,'\\');
397  if (last_slash)
398  strcpy(module_name,last_slash+1);
399  strcat(module_name,": ");
400  }
401 #endif
402  }
403 }
404 
410 #ifdef DBUS_WIN
411 #define DBUS_IS_DIR_SEPARATOR(c) (c == '\\' || c == '/')
412 #else
413 #define DBUS_IS_DIR_SEPARATOR(c) (c == '/')
414 #endif
415 
420 static char *_dbus_file_path_extract_elements_from_tail(const char *file,int level)
421 {
422  int prefix = 0;
423  char *p = (char *)file + strlen(file);
424  int i = 0;
425 
426  for (;p >= file;p--)
427  {
428  if (DBUS_IS_DIR_SEPARATOR(*p))
429  {
430  if (++i >= level)
431  {
432  prefix = p-file+1;
433  break;
434  }
435  }
436  }
437 
438  return (char *)file+prefix;
439 }
440 
447 _dbus_is_verbose_real (void)
448 {
449  _dbus_verbose_init ();
450  return verbose;
451 }
452 
453 void _dbus_set_verbose (dbus_bool_t state)
454 {
455  verbose = state;
456 }
457 
458 dbus_bool_t _dbus_get_verbose (void)
459 {
460  return verbose;
461 }
462 
471 void
472 _dbus_verbose_real (
473 #ifdef DBUS_CPP_SUPPORTS_VARIABLE_MACRO_ARGUMENTS
474  const char *file,
475  const int line,
476  const char *function,
477 #endif
478  const char *format,
479  ...)
480 {
481  va_list args;
482  static dbus_bool_t need_pid = TRUE;
483  int len;
484  long sec, usec;
485  FILE* outfile=stderr;
486 
487 #ifdef HAVE_PDPLINUX
488 #ifdef PDPLINUX_USE_FILELOG
489  if (fdlog) outfile=fdlog;
490  if (format){
491  if (strlen(format) == sizeof(PDPLINUX_LOGCLOSE)-1 ){
492  if (0 == memcmp(format,PDPLINUX_LOGCLOSE,sizeof(PDPLINUX_LOGCLOSE))){
493  if (fdlog) fclose(fdlog);
494  fdlog = NULL;
495  outfile = stderr;
496  return;
497  }
498  }
499  }
500 #endif
501 #endif
502 
503  /* things are written a bit oddly here so that
504  * in the non-verbose case we just have the one
505  * conditional and return immediately.
506  */
507  if (!_dbus_is_verbose_real())
508  return;
509 
510 #ifndef DBUS_USE_OUTPUT_DEBUG_STRING
511  /* Print out pid before the line */
512  if (need_pid)
513  {
514  _dbus_print_thread ();
515  }
516  _dbus_get_real_time (&sec, &usec);
517  fprintf (outfile, "%ld.%06ld ", sec, usec);
518 #endif
519 
520  /* Only print pid again if the next line is a new line */
521  if (format){
522  len = strlen (format);
523  if (len > 0){
524  if (format[len-1] == '\n')
525  need_pid = TRUE;
526  else
527  need_pid = FALSE;
528  }
529 
530  va_start (args, format);
531 #ifdef DBUS_USE_OUTPUT_DEBUG_STRING
532  {
533  char buf[1024];
534  strcpy(buf,module_name);
535 #ifdef DBUS_CPP_SUPPORTS_VARIABLE_MACRO_ARGUMENTS
536  sprintf (buf+strlen(buf), "[%s(%d):%s] ",_dbus_file_path_extract_elements_from_tail(file,2),line,function);
537 #endif
538  vsprintf (buf+strlen(buf),format, args);
539  va_end (args);
540  OutputDebugStringA(buf);
541  }
542 #else
543 #ifdef DBUS_CPP_SUPPORTS_VARIABLE_MACRO_ARGUMENTS
544  fprintf (outfile, "[%s(%d):%s] ",_dbus_file_path_extract_elements_from_tail(file,2),line,function);
545 #endif
546 
547  vfprintf (outfile, format, args);
548  va_end (args);
549 #endif
550  }
551 
552  fflush (outfile);
553 }
554 
561 void
562 _dbus_verbose_reset_real (void)
563 {
564  verbose_initted = FALSE;
565 }
566 
567 void
568 _dbus_trace_ref (const char *obj_name,
569  void *obj,
570  int old_refcount,
571  int new_refcount,
572  const char *why,
573  const char *env_var,
574  int *enabled)
575 {
576  _dbus_assert (obj_name != NULL);
577  _dbus_assert (obj != NULL);
578  _dbus_assert (old_refcount >= -1);
579  _dbus_assert (new_refcount >= -1);
580 
581  if (old_refcount == -1)
582  {
583  _dbus_assert (new_refcount == -1);
584  }
585  else
586  {
587  _dbus_assert (new_refcount >= 0);
588  _dbus_assert (old_refcount >= 0);
589  _dbus_assert (old_refcount > 0 || new_refcount > 0);
590  }
591 
592  _dbus_assert (why != NULL);
593  _dbus_assert (env_var != NULL);
594  _dbus_assert (enabled != NULL);
595 
596  if (*enabled < 0)
597  {
598  const char *s = _dbus_getenv (env_var);
599 
600  *enabled = FALSE;
601 
602  if (s && *s)
603  {
604  if (*s == '0')
605  *enabled = FALSE;
606  else if (*s == '1')
607  *enabled = TRUE;
608  else
609  _dbus_warn ("%s should be 0 or 1 if set, not '%s'", env_var, s);
610  }
611  }
612 
613  if (*enabled)
614  {
615  if (old_refcount == -1)
616  {
617  VALGRIND_PRINTF_BACKTRACE ("%s %p ref stolen (%s)",
618  obj_name, obj, why);
619  _dbus_verbose ("%s %p ref stolen (%s)\n",
620  obj_name, obj, why);
621  }
622  else
623  {
624  VALGRIND_PRINTF_BACKTRACE ("%s %p %d -> %d refs (%s)",
625  obj_name, obj,
626  old_refcount, new_refcount, why);
627  _dbus_verbose ("%s %p %d -> %d refs (%s)\n",
628  obj_name, obj, old_refcount, new_refcount, why);
629  }
630  }
631 }
632 
633 #endif /* DBUS_ENABLE_VERBOSE_MODE */
634 
643 char*
644 _dbus_strdup (const char *str)
645 {
646  size_t len;
647  char *copy;
648 
649  if (str == NULL)
650  return NULL;
651 
652  len = strlen (str);
653 
654  copy = dbus_malloc (len + 1);
655  if (copy == NULL)
656  return NULL;
657 
658  memcpy (copy, str, len + 1);
659 
660  return copy;
661 }
662 
671 void*
672 _dbus_memdup (const void *mem,
673  size_t n_bytes)
674 {
675  void *copy;
676 
677  copy = dbus_malloc (n_bytes);
678  if (copy == NULL)
679  return NULL;
680 
681  memcpy (copy, mem, n_bytes);
682 
683  return copy;
684 }
685 
694 char**
695 _dbus_dup_string_array (const char **array)
696 {
697  int len;
698  int i;
699  char **copy;
700 
701  if (array == NULL)
702  return NULL;
703 
704  for (len = 0; array[len] != NULL; ++len)
705  ;
706 
707  copy = dbus_new0 (char*, len + 1);
708  if (copy == NULL)
709  return NULL;
710 
711  i = 0;
712  while (i < len)
713  {
714  copy[i] = _dbus_strdup (array[i]);
715  if (copy[i] == NULL)
716  {
717  dbus_free_string_array (copy);
718  return NULL;
719  }
720 
721  ++i;
722  }
723 
724  return copy;
725 }
726 
735 _dbus_string_array_contains (const char **array,
736  const char *str)
737 {
738  int i;
739 
740  i = 0;
741  while (array[i] != NULL)
742  {
743  if (strcmp (array[i], str) == 0)
744  return TRUE;
745  ++i;
746  }
747 
748  return FALSE;
749 }
750 
757 size_t
758 _dbus_string_array_length (const char **array)
759 {
760  size_t i;
761  for (i = 0; array[i]; i++) {}
762  return i;
763 }
764 
765 
776  DBusError *error)
777 {
778  DBusError rand_error;
779  long now;
780 
781  dbus_error_init (&rand_error);
782 
783  /* don't use monotonic time because the UUID may be saved to disk, e.g.
784  * it may persist across reboots
785  */
786  _dbus_get_real_time (&now, NULL);
787 
788  uuid->as_uint32s[DBUS_UUID_LENGTH_WORDS - 1] = DBUS_UINT32_TO_BE (now);
789 
791  DBUS_UUID_LENGTH_BYTES - 4,
792  &rand_error))
793  {
794  dbus_set_error (error, rand_error.name,
795  "Failed to generate UUID: %s", rand_error.message);
796  dbus_error_free (&rand_error);
797  return FALSE;
798  }
799 
800  return TRUE;
801 }
802 
812  DBusString *encoded)
813 {
814  DBusString binary;
815  _dbus_string_init_const_len (&binary, uuid->as_bytes, DBUS_UUID_LENGTH_BYTES);
816  return _dbus_string_hex_encode (&binary, 0, encoded, _dbus_string_get_length (encoded));
817 }
818 
819 static dbus_bool_t
820 _dbus_read_uuid_file_without_creating (const DBusString *filename,
821  DBusGUID *uuid,
822  DBusError *error)
823 {
824  DBusString contents;
825  DBusString decoded;
826  int end;
827 
828  if (!_dbus_string_init (&contents))
829  {
830  _DBUS_SET_OOM (error);
831  return FALSE;
832  }
833 
834  if (!_dbus_string_init (&decoded))
835  {
836  _dbus_string_free (&contents);
837  _DBUS_SET_OOM (error);
838  return FALSE;
839  }
840 
841  if (!_dbus_file_get_contents (&contents, filename, error))
842  goto error;
843 
844  _dbus_string_chop_white (&contents);
845 
846  if (_dbus_string_get_length (&contents) != DBUS_UUID_LENGTH_HEX)
847  {
849  "UUID file '%s' should contain a hex string of length %d, not length %d, with no other text",
850  _dbus_string_get_const_data (filename),
851  DBUS_UUID_LENGTH_HEX,
852  _dbus_string_get_length (&contents));
853  goto error;
854  }
855 
856  if (!_dbus_string_hex_decode (&contents, 0, &end, &decoded, 0))
857  {
858  _DBUS_SET_OOM (error);
859  goto error;
860  }
861 
862  if (end == 0)
863  {
865  "UUID file '%s' contains invalid hex data",
866  _dbus_string_get_const_data (filename));
867  goto error;
868  }
869 
870  if (_dbus_string_get_length (&decoded) != DBUS_UUID_LENGTH_BYTES)
871  {
873  "UUID file '%s' contains %d bytes of hex-encoded data instead of %d",
874  _dbus_string_get_const_data (filename),
875  _dbus_string_get_length (&decoded),
876  DBUS_UUID_LENGTH_BYTES);
877  goto error;
878  }
879 
880  _dbus_string_copy_to_buffer (&decoded, uuid->as_bytes, DBUS_UUID_LENGTH_BYTES);
881 
882  _dbus_string_free (&decoded);
883  _dbus_string_free (&contents);
884 
885  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
886 
887  return TRUE;
888 
889  error:
890  _DBUS_ASSERT_ERROR_IS_SET (error);
891  _dbus_string_free (&contents);
892  _dbus_string_free (&decoded);
893  return FALSE;
894 }
895 
906  const DBusGUID *uuid,
907  DBusError *error)
908 {
909  DBusString encoded;
910 
911  if (!_dbus_string_init (&encoded))
912  {
913  _DBUS_SET_OOM (error);
914  return FALSE;
915  }
916 
917  if (!_dbus_uuid_encode (uuid, &encoded))
918  {
919  _DBUS_SET_OOM (error);
920  goto error;
921  }
922 
923  if (!_dbus_string_append_byte (&encoded, '\n'))
924  {
925  _DBUS_SET_OOM (error);
926  goto error;
927  }
928 
929  if (!_dbus_string_save_to_file (&encoded, filename, TRUE, error))
930  goto error;
931 
932  _dbus_string_free (&encoded);
933 
934  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
935  return TRUE;
936 
937  error:
938  _DBUS_ASSERT_ERROR_IS_SET (error);
939  _dbus_string_free (&encoded);
940  return FALSE;
941 }
942 
955  DBusGUID *uuid,
956  dbus_bool_t create_if_not_found,
957  DBusError *error)
958 {
959  DBusError read_error = DBUS_ERROR_INIT;
960 
961  if (_dbus_read_uuid_file_without_creating (filename, uuid, &read_error))
962  return TRUE;
963 
964  if (!create_if_not_found)
965  {
966  dbus_move_error (&read_error, error);
967  return FALSE;
968  }
969 
970  /* If the file exists and contains junk, we want to keep that error
971  * message instead of overwriting it with a "file exists" error
972  * message when we try to write
973  */
975  {
976  dbus_move_error (&read_error, error);
977  return FALSE;
978  }
979  else
980  {
981  dbus_error_free (&read_error);
982 
983  if (!_dbus_generate_uuid (uuid, error))
984  return FALSE;
985 
986  return _dbus_write_uuid_file (filename, uuid, error);
987  }
988 }
989 
990 /* Protected by _DBUS_LOCK (machine_uuid) */
991 static int machine_uuid_initialized_generation = 0;
992 static DBusGUID machine_uuid;
993 
1007  DBusError *error)
1008 {
1009  dbus_bool_t ok = TRUE;
1010 
1011  if (!_DBUS_LOCK (machine_uuid))
1012  {
1013  _DBUS_SET_OOM (error);
1014  return FALSE;
1015  }
1016 
1017  if (machine_uuid_initialized_generation != _dbus_current_generation)
1018  {
1019  if (!_dbus_read_local_machine_uuid (&machine_uuid, FALSE, error))
1020  ok = FALSE;
1021  }
1022 
1023  if (ok)
1024  {
1025  if (!_dbus_uuid_encode (&machine_uuid, uuid_str))
1026  {
1027  ok = FALSE;
1028  _DBUS_SET_OOM (error);
1029  }
1030  }
1031 
1032  _DBUS_UNLOCK (machine_uuid);
1033 
1034  return ok;
1035 }
1036 
1037 #ifndef DBUS_DISABLE_CHECKS
1038 void
1039 _dbus_warn_return_if_fail (const char *function,
1040  const char *assertion,
1041  const char *file,
1042  int line)
1043 {
1045  "arguments to %s() were incorrect, assertion \"%s\" failed in file %s line %d.\n"
1046  "This is normally a bug in some application using the D-Bus library.\n",
1047  function, assertion, file, line);
1048 }
1049 #endif
1050 
1051 #ifndef DBUS_DISABLE_ASSERT
1052 
1064 void
1066  const char *condition_text,
1067  const char *file,
1068  int line,
1069  const char *func)
1070 {
1071  if (_DBUS_UNLIKELY (!condition))
1072  {
1073  _dbus_warn ("assertion failed \"%s\" file \"%s\" line %d function %s",
1074  condition_text, file, line, func);
1075  _dbus_abort ();
1076  }
1077 }
1078 
1089 void
1090 _dbus_real_assert_not_reached (const char *explanation,
1091  const char *file,
1092  int line)
1093 {
1094  _dbus_warn ("File \"%s\" line %d should not have been reached: %s",
1095  file, line, explanation);
1096  _dbus_abort ();
1097 }
1098 #endif /* DBUS_DISABLE_ASSERT */
1099 
1100 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
1101 static dbus_bool_t
1102 run_failing_each_malloc (int n_mallocs,
1103  const char *description,
1104  DBusTestMemoryFunction func,
1105  void *data)
1106 {
1107  n_mallocs += 10; /* fudge factor to ensure reallocs etc. are covered */
1108 
1109  while (n_mallocs >= 0)
1110  {
1111  _dbus_set_fail_alloc_counter (n_mallocs);
1112 
1113  _dbus_verbose ("\n===\n%s: (will fail malloc %d with %d failures)\n===\n",
1114  description, n_mallocs,
1115  _dbus_get_fail_alloc_failures ());
1116 
1117  if (!(* func) (data))
1118  return FALSE;
1119 
1120  n_mallocs -= 1;
1121  }
1122 
1123  _dbus_set_fail_alloc_counter (_DBUS_INT_MAX);
1124 
1125  return TRUE;
1126 }
1127 
1142 _dbus_test_oom_handling (const char *description,
1143  DBusTestMemoryFunction func,
1144  void *data)
1145 {
1146  int approx_mallocs;
1147  const char *setting;
1148  int max_failures_to_try;
1149  int i;
1150 
1151  /* Run once to see about how many mallocs are involved */
1152 
1153  _dbus_set_fail_alloc_counter (_DBUS_INT_MAX);
1154 
1155  _dbus_verbose ("Running once to count mallocs\n");
1156 
1157  if (!(* func) (data))
1158  return FALSE;
1159 
1160  approx_mallocs = _DBUS_INT_MAX - _dbus_get_fail_alloc_counter ();
1161 
1162  _dbus_verbose ("\n=================\n%s: about %d mallocs total\n=================\n",
1163  description, approx_mallocs);
1164 
1165  setting = _dbus_getenv ("DBUS_TEST_MALLOC_FAILURES");
1166  if (setting != NULL)
1167  {
1168  DBusString str;
1169  long v;
1170  _dbus_string_init_const (&str, setting);
1171  v = 4;
1172  if (!_dbus_string_parse_int (&str, 0, &v, NULL))
1173  _dbus_warn ("couldn't parse '%s' as integer\n", setting);
1174  max_failures_to_try = v;
1175  }
1176  else
1177  {
1178  max_failures_to_try = 4;
1179  }
1180 
1181  if (max_failures_to_try < 1)
1182  {
1183  _dbus_verbose ("not testing OOM handling\n");
1184  return TRUE;
1185  }
1186 
1187  i = setting ? max_failures_to_try - 1 : 1;
1188  while (i < max_failures_to_try)
1189  {
1190  _dbus_set_fail_alloc_failures (i);
1191  if (!run_failing_each_malloc (approx_mallocs, description, func, data))
1192  return FALSE;
1193  ++i;
1194  }
1195 
1196  _dbus_verbose ("\n=================\n%s: all iterations passed\n=================\n",
1197  description);
1198 
1199  return TRUE;
1200 }
1201 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */
1202 
dbus_bool_t dbus_error_has_name(const DBusError *error, const char *name)
Checks whether the error is set and has the given name.
Definition: dbus-errors.c:302
DBUS_PRIVATE_EXPORT dbus_bool_t _dbus_string_parse_int(const DBusString *str, int start, long *value_return, int *end_return)
Parses an integer contained in a DBusString.
Definition: dbus-sysdeps.c:437
void _dbus_string_copy_to_buffer(const DBusString *str, char *buffer, int avail_len)
Copies the contents of a DBusString into a different buffer.
Definition: dbus-string.c:697
dbus_bool_t _dbus_uuid_encode(const DBusGUID *uuid, DBusString *encoded)
Hex-encode a UUID.
const char * message
public error message field
Definition: dbus-errors.h:51
#define NULL
A null pointer, defined appropriately for C or C++.
dbus_bool_t _dbus_read_local_machine_uuid(DBusGUID *machine_id, dbus_bool_t create_if_not_found, DBusError *error)
Reads the uuid of the machine we&#39;re running on from the dbus configuration.
void _dbus_abort(void)
Aborts the program with SIGABRT (dumping core).
Definition: dbus-sysdeps.c:79
#define DBUS_ERROR_INVALID_FILE_CONTENT
A file contains invalid syntax or is otherwise broken.
dbus_bool_t _dbus_string_save_to_file(const DBusString *str, const DBusString *filename, dbus_bool_t world_readable, DBusError *error)
Writes a string out to a file.
dbus_bool_t _dbus_string_hex_encode(const DBusString *source, int start, DBusString *dest, int insert_at)
Encodes a string in hex, the way MD5 and SHA-1 are usually encoded.
Definition: dbus-string.c:2259
void _dbus_real_assert(dbus_bool_t condition, const char *condition_text, const char *file, int line, const char *func)
Internals of _dbus_assert(); it&#39;s a function rather than a macro with the inline code so that the ass...
dbus_bool_t _dbus_generate_uuid(DBusGUID *uuid, DBusError *error)
Generates a new UUID.
dbus_bool_t _dbus_get_local_machine_uuid_encoded(DBusString *uuid_str, DBusError *error)
Gets the hex-encoded UUID of the machine this function is executed on.
dbus_uint32_t as_uint32s[DBUS_UUID_LENGTH_WORDS]
guid as four uint32 values
void _dbus_string_chop_white(DBusString *str)
Deletes leading and trailing whitespace.
Definition: dbus-string.c:1989
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
#define DBUS_ERROR_INIT
Expands to a suitable initializer for a DBusError on the stack.
Definition: dbus-errors.h:62
char as_bytes[DBUS_UUID_LENGTH_BYTES]
guid as 16 single-byte values
void _dbus_warn_check_failed(const char *format,...)
Prints a "critical" warning to stderr when an assertion fails; differs from _dbus_warn primarily in t...
void dbus_error_free(DBusError *error)
Frees an error that&#39;s been set (or just initialized), then reinitializes the error as in dbus_error_i...
Definition: dbus-errors.c:211
dbus_bool_t _dbus_file_get_contents(DBusString *str, const DBusString *filename, DBusError *error)
Appends the contents of the given file to the string, returning error code.
A globally unique ID ; we have one for each DBusServer, and also one for each machine with libdbus in...
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition: dbus-string.c:175
#define _DBUS_INT_MAX
Maximum value of type "int".
void _dbus_real_assert_not_reached(const char *explanation, const char *file, int line)
Internals of _dbus_assert_not_reached(); it&#39;s a function rather than a macro with the inline code so ...
void * dbus_malloc(size_t bytes)
Allocates the given number of bytes, as with standard malloc().
Definition: dbus-memory.c:462
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
Definition: dbus-memory.h:58
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
void _dbus_string_init_const(DBusString *str, const char *value)
Initializes a constant string.
Definition: dbus-string.c:190
const char * _dbus_no_memory_message
Fixed "out of memory" error message, just to avoid making up a different string every time and wastin...
void _dbus_warn(const char *format,...)
Prints a warning message to stderr.
void * _dbus_memdup(const void *mem, size_t n_bytes)
Duplicates a block of memory.
int _dbus_current_generation
_dbus_current_generation is used to track each time that dbus_shutdown() is called, so we can reinit things after it&#39;s been called.
Definition: dbus-memory.c:782
Object representing an exception.
Definition: dbus-errors.h:48
size_t _dbus_string_array_length(const char **array)
Returns the size of a string array.
void dbus_set_error(DBusError *error, const char *name, const char *format,...)
Assigns an error name and message to a DBusError.
Definition: dbus-errors.c:354
dbus_bool_t _dbus_string_append_byte(DBusString *str, unsigned char byte)
Appends a single byte to the string, returning FALSE if not enough memory.
Definition: dbus-string.c:1157
#define _DBUS_UNLOCK(name)
Unlocks a global lock.
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init().
Definition: dbus-string.c:259
char ** _dbus_dup_string_array(const char **array)
Duplicates a string array.
#define TRUE
Expands to "1".
void dbus_move_error(DBusError *src, DBusError *dest)
Moves an error src into dest, freeing src and overwriting dest.
Definition: dbus-errors.c:279
const char * name
public error name field
Definition: dbus-errors.h:50
dbus_bool_t _dbus_read_uuid_file(const DBusString *filename, DBusGUID *uuid, dbus_bool_t create_if_not_found, DBusError *error)
Reads (and optionally writes) a uuid to a file.
void dbus_error_init(DBusError *error)
Initializes a DBusError structure.
Definition: dbus-errors.c:188
dbus_bool_t _dbus_string_hex_decode(const DBusString *source, int start, int *end_return, DBusString *dest, int insert_at)
Decodes a string from hex encoding.
Definition: dbus-string.c:2309
void dbus_free_string_array(char **str_array)
Frees a NULL-terminated array of strings.
Definition: dbus-memory.c:750
dbus_bool_t _dbus_string_array_contains(const char **array, const char *str)
Checks whether a string array contains the given string.
void _dbus_logv(DBusSystemLogSeverity severity, const char *msg, va_list args)
Log a message to the system log file (e.g.
#define FALSE
Expands to "0".
dbus_bool_t _dbus_write_uuid_file(const DBusString *filename, const DBusGUID *uuid, DBusError *error)
Write the give UUID to a file.
#define _DBUS_LOCK(name)
Locks a global lock, initializing it first if necessary.
dbus_bool_t _dbus_generate_random_bytes_buffer(char *buffer, int n_bytes, DBusError *error)
Fills n_bytes of the given buffer with random bytes.
Definition: dbus-sysdeps.c:518
char * _dbus_strdup(const char *str)
Duplicates a string.
const char * _dbus_getenv(const char *varname)
Wrapper for getenv().
Definition: dbus-sysdeps.c:187
void _dbus_string_init_const_len(DBusString *str, const char *value, int len)
Initializes a constant string with a length.
Definition: dbus-string.c:210
void _dbus_get_real_time(long *tv_sec, long *tv_usec)
Get current time, as in gettimeofday().