25#include "dbus-internals.h"
26#include "dbus-protocol.h"
27#include "dbus-marshal-basic.h"
29#include "dbus-test-tap.h"
30#include "dbus-valgrind-internal.h"
35#ifdef DBUS_USE_OUTPUT_DEBUG_STRING
51 #ifdef DBUS_ENABLE_VERBOSE_MODE
63 #ifdef PDPLINUX_USE_FILELOG
66#define PDPLINUX_LOGCLOSE "!PDPLINUX_LOGCLOSE!"
242 fatal_warnings =
FALSE;
243 fatal_warnings_on_check_failed =
FALSE;
247 fatal_warnings =
TRUE;
248 fatal_warnings_on_check_failed =
TRUE;
252 fprintf(stderr,
"DBUS_FATAL_WARNINGS should be set to 0 or 1 if set, not '%s'",
274 DBusSystemLogSeverity severity = DBUS_SYSTEM_LOG_WARNING;
281 severity = DBUS_SYSTEM_LOG_ERROR;
283 va_start (args, format);
306 DBusSystemLogSeverity severity = DBUS_SYSTEM_LOG_WARNING;
312 if (fatal_warnings_on_check_failed)
313 severity = DBUS_SYSTEM_LOG_ERROR;
315 va_start (args, format);
319 if (fatal_warnings_on_check_failed)
326#ifdef DBUS_ENABLE_VERBOSE_MODE
331#ifdef DBUS_USE_OUTPUT_DEBUG_STRING
332static char module_name[1024];
339#ifdef PDPLINUX_USE_FILELOG
341static char namelog[1024];
346_dbus_verbose_init (
void)
348 if (!verbose_initted)
352 verbose = p !=
NULL && *p ==
'1';
353 verbose_initted =
TRUE;
356#ifdef HAVE_PDPLINUX_FORCE_ENABLE_DBUS_VERBOSE
360#ifdef PDPLINUX_USE_FILELOG
361 if (!fdlog && verbose){
364 char* textlabel=
NULL;
365 struct tm* timeinfo=
NULL;
369 timeinfo=localtime(&rawtime);
371 snprintf(sttime,
sizeof(sttime),
"notime");
373 strftime (sttime,
sizeof(sttime),
"%F-%H%M%S",timeinfo);
375 pdpl=pdp_get_current();
377 textlabel=pdpl_get_text(pdpl,PDPL_FMT_NUM);
379 if (snprintf(namelog,
sizeof(namelog),
"/dbuslog/dbus-%s-(%s)-%ld",
381 textlabel ? textlabel :
"-",
384 while (
'\0' != *pos && pos <= namelog+
sizeof(namelog)/
sizeof(*namelog)){
385 if (*pos ==
':') *pos=
'-';
389 fdlog=fopen(namelog,
"wt+");
391 if (textlabel) free(textlabel);
392 if (pdpl) pdpl_put(pdpl);
397#ifdef DBUS_USE_OUTPUT_DEBUG_STRING
399 char *last_period, *last_slash;
400 GetModuleFileName(0,module_name,
sizeof(module_name)-1);
401 last_period = _mbsrchr(module_name,
'.');
404 last_slash = _mbsrchr(module_name,
'\\');
406 strcpy(module_name,last_slash+1);
407 strcat(module_name,
": ");
419#define DBUS_IS_DIR_SEPARATOR(c) (c == '\\' || c == '/')
421#define DBUS_IS_DIR_SEPARATOR(c) (c == '/')
428static char *_dbus_file_path_extract_elements_from_tail(
const char *file,
int level)
431 char *p = (
char *)file + strlen(file);
436 if (DBUS_IS_DIR_SEPARATOR(*p))
446 return (
char *)file+prefix;
455_dbus_is_verbose_real (
void)
457 _dbus_verbose_init ();
483_dbus_verbose_raw (
const char *s)
485 if (!_dbus_is_verbose_real ())
487#ifdef DBUS_USE_OUTPUT_DEBUG_STRING
488 OutputDebugStringA (s);
505#ifdef DBUS_CPP_SUPPORTS_VARIABLE_MACRO_ARGUMENTS
508 const char *function,
517 FILE* outfile=stderr;
520#ifdef PDPLINUX_USE_FILELOG
521 if (fdlog) outfile=fdlog;
523 if (strlen(format) ==
sizeof(PDPLINUX_LOGCLOSE)-1 ){
524 if (0 == memcmp(format,PDPLINUX_LOGCLOSE,
sizeof(PDPLINUX_LOGCLOSE))){
525 if (fdlog) fclose(fdlog);
539 if (!_dbus_is_verbose_real())
542#ifndef DBUS_USE_OUTPUT_DEBUG_STRING
546 _dbus_print_thread ();
549 fprintf (outfile,
"%ld.%06ld ", sec, usec);
554 len = strlen (format);
556 if (format[len-1] ==
'\n')
562 va_start (args, format);
563#ifdef DBUS_USE_OUTPUT_DEBUG_STRING
566 const char *message =
NULL;
574#ifdef DBUS_CPP_SUPPORTS_VARIABLE_MACRO_ARGUMENTS
580 message = _dbus_string_get_const_data (&out);
584 OutputDebugStringA (
"Out of memory while formatting verbose message: '''");
585 OutputDebugStringA (format);
586 OutputDebugStringA (
"'''");
590 OutputDebugStringA (message);
595#ifdef DBUS_CPP_SUPPORTS_VARIABLE_MACRO_ARGUMENTS
596 fprintf (outfile,
"[%s(%d):%s] ",_dbus_file_path_extract_elements_from_tail(file,2),line,function);
599 vfprintf (outfile, format, args);
614_dbus_verbose_reset_real (
void)
616 verbose_initted =
FALSE;
620_dbus_trace_ref (
const char *obj_name,
633 if (old_refcount == -1)
661 _dbus_warn (
"%s should be 0 or 1 if set, not '%s'", env_var, s);
667 if (old_refcount == -1)
669 VALGRIND_PRINTF_BACKTRACE (
"%s %p ref stolen (%s)",
671 _dbus_verbose (
"%s %p ref stolen (%s)\n",
676 VALGRIND_PRINTF_BACKTRACE (
"%s %p %d -> %d refs (%s)",
678 old_refcount, new_refcount, why);
679 _dbus_verbose (
"%s %p %d -> %d refs (%s)\n",
680 obj_name, obj, old_refcount, new_refcount, why);
710 memcpy (copy, str, len + 1);
733 memcpy (copy, mem, n_bytes);
756 for (len = 0; array[len] !=
NULL; ++len)
793 while (array[i] !=
NULL)
795 if (strcmp (array[i], str) == 0)
813 for (i = 0; array[i]; i++) {}
840 uuid->
as_uint32s[DBUS_UUID_LENGTH_WORDS - 1] = DBUS_UINT32_TO_BE (now);
843 DBUS_UUID_LENGTH_BYTES - 4,
847 "Failed to generate UUID: %s", rand_error.
message);
872_dbus_read_uuid_file_without_creating (
const DBusString *filename,
882 _DBUS_SET_OOM (error);
889 _DBUS_SET_OOM (error);
898 if (_dbus_string_get_length (&contents) != DBUS_UUID_LENGTH_HEX)
901 "UUID file '%s' should contain a hex string of length %d, not length %d, with no other text",
902 _dbus_string_get_const_data (filename),
903 DBUS_UUID_LENGTH_HEX,
904 _dbus_string_get_length (&contents));
910 _DBUS_SET_OOM (error);
917 "UUID file '%s' contains invalid hex data",
918 _dbus_string_get_const_data (filename));
922 if (_dbus_string_get_length (&decoded) != DBUS_UUID_LENGTH_BYTES)
925 "UUID file '%s' contains %d bytes of hex-encoded data instead of %d",
926 _dbus_string_get_const_data (filename),
927 _dbus_string_get_length (&decoded),
928 DBUS_UUID_LENGTH_BYTES);
937 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
942 _DBUS_ASSERT_ERROR_IS_SET (error);
965 _DBUS_SET_OOM (error);
971 _DBUS_SET_OOM (error);
977 _DBUS_SET_OOM (error);
986 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
990 _DBUS_ASSERT_ERROR_IS_SET (error);
1013 if (_dbus_read_uuid_file_without_creating (filename, uuid, &read_error))
1016 if (!create_if_not_found)
1043static int machine_uuid_initialized_generation = 0;
1065 _DBUS_SET_OOM (error);
1080 _DBUS_SET_OOM (error);
1089#ifndef DBUS_DISABLE_CHECKS
1091_dbus_warn_return_if_fail (
const char *function,
1092 const char *assertion,
1097 "arguments to %s() were incorrect, assertion \"%s\" failed in file %s line %d.\n"
1098 "This is normally a bug in some application using the D-Bus library.\n",
1099 function, assertion, file, line);
1103#ifndef DBUS_DISABLE_ASSERT
1118 const char *condition_text,
1123 if (_DBUS_UNLIKELY (!condition))
1125 _dbus_warn (
"assertion failed \"%s\" file \"%s\" line %d function %s",
1126 condition_text, file, line, func);
1146 _dbus_warn (
"File \"%s\" line %d should not have been reached: %s",
1147 file, line, explanation);
1152#ifdef DBUS_ENABLE_EMBEDDED_TESTS
1154run_failing_each_malloc (
int n_mallocs,
1155 const char *description,
1156 DBusTestMemoryFunction func,
1161 while (n_mallocs >= 0)
1163 _dbus_set_fail_alloc_counter (n_mallocs);
1165 _dbus_test_diag (
"%s: will fail malloc %d and %d that follow",
1166 description, n_mallocs,
1167 _dbus_get_fail_alloc_failures () - 1);
1169 if (!(* func) (data,
FALSE))
1194_dbus_test_oom_handling (
const char *description,
1195 DBusTestMemoryFunction func,
1199 const char *setting;
1200 int max_failures_to_try;
1207 _dbus_test_diag (
"Running \"%s\" once to count mallocs", description);
1209 if (!(* func) (data,
TRUE))
1212 approx_mallocs =
_DBUS_INT_MAX - _dbus_get_fail_alloc_counter ();
1214 _dbus_test_diag (
"\"%s\" has about %d mallocs in total",
1215 description, approx_mallocs);
1219 if (setting !=
NULL)
1226 _dbus_warn (
"couldn't parse '%s' as integer\n", setting);
1227 max_failures_to_try = v;
1231 max_failures_to_try = 4;
1239 max_failures_to_try = 0;
1242 if (max_failures_to_try < 1)
1244 _dbus_test_diag (
"not testing OOM handling");
1248 _dbus_test_diag (
"testing \"%s\" with up to %d consecutive malloc failures",
1249 description, max_failures_to_try);
1251 i = setting ? max_failures_to_try - 1 : 1;
1252 while (i < max_failures_to_try)
1254 _dbus_test_diag (
"testing \"%s\" with %d consecutive malloc failures",
1255 description, i + 1);
1257 _dbus_set_fail_alloc_failures (i);
1258 if (!run_failing_each_malloc (approx_mallocs, description, func, data))
1263 _dbus_verbose (
"\"%s\" coped OK with malloc failures\n", description);
#define DBUS_ERROR_INIT
Expands to a suitable initializer for a DBusError on the stack.
void dbus_move_error(DBusError *src, DBusError *dest)
Moves an error src into dest, freeing src and overwriting dest.
dbus_bool_t dbus_error_has_name(const DBusError *error, const char *name)
Checks whether the error is set and has the given name.
void dbus_error_init(DBusError *error)
Initializes a DBusError structure.
void dbus_set_error(DBusError *error, const char *name, const char *format,...)
Assigns an error name and message to a DBusError.
void dbus_error_free(DBusError *error)
Frees an error that's been set (or just initialized), then reinitializes the error as in dbus_error_i...
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_file_get_contents(DBusString *str, const DBusString *filename, DBusError *error)
Appends the contents of the given file to the string, returning error code.
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
#define _DBUS_UNLOCK(name)
Unlocks a global lock.
#define _DBUS_LOCK(name)
Locks a global lock, initializing it first if necessary.
void _dbus_real_assert_not_reached(const char *explanation, const char *file, int line)
Internals of _dbus_assert_not_reached(); it's a function rather than a macro with the inline code so ...
dbus_bool_t _dbus_generate_uuid(DBusGUID *uuid, DBusError *error)
Generates a new UUID.
#define _DBUS_INT_MAX
Maximum value of type "int".
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...
char * _dbus_strdup(const char *str)
Duplicates a string.
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.
dbus_bool_t _dbus_string_array_contains(const char **array, const char *str)
Checks whether a string array contains the given string.
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.
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's a function rather than a macro with the inline code so that the ass...
dbus_bool_t _dbus_write_uuid_file(const DBusString *filename, const DBusGUID *uuid, DBusError *error)
Write the give UUID to a file.
void _dbus_warn(const char *format,...)
Prints a warning message to stderr.
size_t _dbus_string_array_length(const char **array)
Returns the size of a string array.
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_memdup(const void *mem, size_t n_bytes)
Duplicates a block of memory.
dbus_bool_t _dbus_uuid_encode(const DBusGUID *uuid, DBusString *encoded)
Hex-encode a UUID.
char ** _dbus_dup_string_array(const char **array)
Duplicates a string array.
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.
#define NULL
A null pointer, defined appropriately for C or C++.
#define TRUE
Expands to "1".
#define FALSE
Expands to "0".
int _dbus_current_generation
_dbus_current_generation is used to track each time that dbus_shutdown() is called,...
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
void dbus_free_string_array(char **str_array)
Frees a NULL-terminated array of strings.
void * dbus_malloc(size_t bytes)
Allocates the given number of bytes, as with standard malloc().
#define DBUS_ERROR_INVALID_FILE_CONTENT
A file contains invalid syntax or is otherwise broken.
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.
dbus_bool_t _dbus_string_append(DBusString *str, const char *buffer)
Appends a nul-terminated C-style string to a DBusString.
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
void _dbus_string_init_const(DBusString *str, const char *value)
Initializes a constant string.
void _dbus_string_init_const_len(DBusString *str, const char *value, int len)
Initializes a constant string with a length.
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init(), and fills it with the same contents as #_DBUS_STRING_I...
dbus_bool_t _dbus_string_append_printf_valist(DBusString *str, const char *format, va_list args)
Appends a printf-style formatted string to the DBusString.
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.
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.
void _dbus_string_chop_white(DBusString *str)
Deletes leading and trailing whitespace.
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.
dbus_bool_t _dbus_string_append_printf(DBusString *str, const char *format,...)
Appends a printf-style formatted string to the DBusString.
void _dbus_string_copy_to_buffer(const DBusString *str, char *buffer, int avail_len)
Copies the contents of a DBusString into a different buffer.
void _dbus_logv(DBusSystemLogSeverity severity, const char *msg, va_list args)
Log a message to the system log file (e.g.
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're running on from the dbus configuration.
const char * _dbus_getenv(const char *varname)
Wrapper for getenv().
void _dbus_get_real_time(long *tv_sec, long *tv_usec)
Get current time, as in gettimeofday().
void _dbus_abort(void)
Aborts the program with SIGABRT (dumping core).
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Object representing an exception.
const char * name
public error name field
const char * message
public error message field
A globally unique ID ; we have one for each DBusServer, and also one for each machine with libdbus in...
dbus_uint32_t as_uint32s[DBUS_UUID_LENGTH_WORDS]
guid as four uint32 values
char as_bytes[DBUS_UUID_LENGTH_BYTES]
guid as 16 single-byte values