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"
34 #ifdef DBUS_USE_OUTPUT_DEBUG_STRING
201 fatal_warnings =
FALSE;
202 fatal_warnings_on_check_failed =
FALSE;
206 fatal_warnings =
TRUE;
207 fatal_warnings_on_check_failed =
TRUE;
211 fprintf(stderr,
"DBUS_FATAL_WARNINGS should be set to 0 or 1 if set, not '%s'",
238 va_start (args, format);
239 vfprintf (stderr, format, args);
268 va_start (args, format);
269 vfprintf (stderr, format, args);
272 if (fatal_warnings_on_check_failed)
279 #ifdef DBUS_ENABLE_VERBOSE_MODE
285 #define PTHREAD_IN_VERBOSE 0
286 #if PTHREAD_IN_VERBOSE
290 #ifdef DBUS_USE_OUTPUT_DEBUG_STRING
291 static char module_name[1024];
295 _dbus_verbose_init (
void)
297 if (!verbose_initted)
300 verbose = p !=
NULL && *p ==
'1';
301 verbose_initted =
TRUE;
302 #ifdef DBUS_USE_OUTPUT_DEBUG_STRING
304 char *last_period, *last_slash;
305 GetModuleFileName(0,module_name,
sizeof(module_name)-1);
306 last_period = _mbsrchr(module_name,
'.');
309 last_slash = _mbsrchr(module_name,
'\\');
311 strcpy(module_name,last_slash+1);
312 strcat(module_name,
": ");
324 #define DBUS_IS_DIR_SEPARATOR(c) (c == '\\' || c == '/')
326 #define DBUS_IS_DIR_SEPARATOR(c) (c == '/')
333 static char *_dbus_file_path_extract_elements_from_tail(
const char *file,
int level)
336 char *p = (
char *)file + strlen(file);
341 if (DBUS_IS_DIR_SEPARATOR(*p))
351 return (
char *)file+prefix;
360 _dbus_is_verbose_real (
void)
362 _dbus_verbose_init ();
386 #ifdef DBUS_CPP_SUPPORTS_VARIABLE_MACRO_ARGUMENTS
389 const char *
function,
402 if (!_dbus_is_verbose_real())
405 #ifndef DBUS_USE_OUTPUT_DEBUG_STRING
409 #if PTHREAD_IN_VERBOSE
418 len = strlen (format);
419 if (format[len-1] ==
'\n')
424 va_start (args, format);
425 #ifdef DBUS_USE_OUTPUT_DEBUG_STRING
428 strcpy(buf,module_name);
429 #ifdef DBUS_CPP_SUPPORTS_VARIABLE_MACRO_ARGUMENTS
430 sprintf (buf+strlen(buf),
"[%s(%d):%s] ",_dbus_file_path_extract_elements_from_tail(file,2),line,
function);
432 vsprintf (buf+strlen(buf),format, args);
434 OutputDebugStringA(buf);
437 #ifdef DBUS_CPP_SUPPORTS_VARIABLE_MACRO_ARGUMENTS
438 fprintf (stderr,
"[%s(%d):%s] ",_dbus_file_path_extract_elements_from_tail(file,2),line,
function);
441 vfprintf (stderr, format, args);
455 _dbus_verbose_reset_real (
void)
457 verbose_initted =
FALSE;
461 _dbus_trace_ref (
const char *obj_name,
474 if (old_refcount == -1)
502 _dbus_warn (
"%s should be 0 or 1 if set, not '%s'", env_var, s);
508 if (old_refcount == -1)
510 VALGRIND_PRINTF_BACKTRACE (
"%s %p ref stolen (%s)",
512 _dbus_verbose (
"%s %p ref stolen (%s)\n",
517 VALGRIND_PRINTF_BACKTRACE (
"%s %p %d -> %d refs (%s)",
519 old_refcount, new_refcount, why);
520 _dbus_verbose (
"%s %p %d -> %d refs (%s)\n",
521 obj_name, obj, old_refcount, new_refcount, why);
551 memcpy (copy, str, len + 1);
574 memcpy (copy, mem, n_bytes);
597 for (len = 0; array[len] !=
NULL; ++len)
634 while (array[i] !=
NULL)
636 if (strcmp (array[i], str) == 0)
666 uuid->
as_uint32s[DBUS_UUID_LENGTH_WORDS - 1] = DBUS_UINT32_TO_BE (now);
669 DBUS_UUID_LENGTH_BYTES - 4,
673 "Failed to generate UUID: %s", rand_error.
message);
698 _dbus_read_uuid_file_without_creating (
const DBusString *filename,
708 _DBUS_SET_OOM (error);
715 _DBUS_SET_OOM (error);
724 if (_dbus_string_get_length (&contents) != DBUS_UUID_LENGTH_HEX)
727 "UUID file '%s' should contain a hex string of length %d, not length %d, with no other text",
728 _dbus_string_get_const_data (filename),
729 DBUS_UUID_LENGTH_HEX,
730 _dbus_string_get_length (&contents));
736 _DBUS_SET_OOM (error);
743 "UUID file '%s' contains invalid hex data",
744 _dbus_string_get_const_data (filename));
748 if (_dbus_string_get_length (&decoded) != DBUS_UUID_LENGTH_BYTES)
751 "UUID file '%s' contains %d bytes of hex-encoded data instead of %d",
752 _dbus_string_get_const_data (filename),
753 _dbus_string_get_length (&decoded),
754 DBUS_UUID_LENGTH_BYTES);
763 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
768 _DBUS_ASSERT_ERROR_IS_SET (error);
791 _DBUS_SET_OOM (error);
797 _DBUS_SET_OOM (error);
803 _DBUS_SET_OOM (error);
812 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
816 _DBUS_ASSERT_ERROR_IS_SET (error);
839 if (_dbus_read_uuid_file_without_creating (filename, uuid, &read_error))
842 if (!create_if_not_found)
869 static int machine_uuid_initialized_generation = 0;
891 _DBUS_SET_OOM (error);
902 #ifndef DBUS_ENABLE_EMBEDDED_TESTS
908 "See the manual page for dbus-uuidgen to correct this issue.\n",
923 _DBUS_SET_OOM (error);
932 #ifndef DBUS_DISABLE_CHECKS
935 "arguments to %s() were incorrect, assertion \"%s\" failed in file %s line %d.\n"
936 "This is normally a bug in some application using the D-Bus library.\n";
939 #ifndef DBUS_DISABLE_ASSERT
954 const char *condition_text,
959 if (_DBUS_UNLIKELY (!condition))
961 _dbus_warn (
"%lu: assertion failed \"%s\" file \"%s\" line %d function %s\n",
982 _dbus_warn (
"File \"%s\" line %d process %lu should not have been reached: %s\n",
988 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
990 run_failing_each_malloc (
int n_mallocs,
991 const char *description,
992 DBusTestMemoryFunction func,
997 while (n_mallocs >= 0)
999 _dbus_set_fail_alloc_counter (n_mallocs);
1001 _dbus_verbose (
"\n===\n%s: (will fail malloc %d with %d failures)\n===\n",
1002 description, n_mallocs,
1003 _dbus_get_fail_alloc_failures ());
1005 if (!(* func) (data))
1030 _dbus_test_oom_handling (
const char *description,
1031 DBusTestMemoryFunction func,
1035 const char *setting;
1036 int max_failures_to_try;
1043 _dbus_verbose (
"Running once to count mallocs\n");
1045 if (!(* func) (data))
1048 approx_mallocs =
_DBUS_INT_MAX - _dbus_get_fail_alloc_counter ();
1050 _dbus_verbose (
"\n=================\n%s: about %d mallocs total\n=================\n",
1051 description, approx_mallocs);
1054 if (setting !=
NULL)
1061 _dbus_warn (
"couldn't parse '%s' as integer\n", setting);
1062 max_failures_to_try = v;
1066 max_failures_to_try = 4;
1069 if (max_failures_to_try < 1)
1071 _dbus_verbose (
"not testing OOM handling\n");
1075 i = setting ? max_failures_to_try - 1 : 1;
1076 while (i < max_failures_to_try)
1078 _dbus_set_fail_alloc_failures (i);
1079 if (!run_failing_each_malloc (approx_mallocs, description, func, data))
1084 _dbus_verbose (
"\n=================\n%s: all iterations passed\n=================\n",
dbus_bool_t dbus_error_has_name(const DBusError *error, const char *name)
Checks whether the error is set and has the given name.
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.
void _dbus_string_copy_to_buffer(const DBusString *str, char *buffer, int avail_len)
Copies the contents of a DBusString into a different buffer.
dbus_bool_t _dbus_uuid_encode(const DBusGUID *uuid, DBusString *encoded)
Hex-encode a UUID.
const char * message
public error message field
#define NULL
A null pointer, defined appropriately for C or C++.
#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.
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_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
const char * _dbus_return_if_fail_warning_format
String used in _dbus_return_if_fail macro.
void _dbus_string_chop_white(DBusString *str)
Deletes leading and trailing whitespace.
#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.
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's been set (or just initialized), then reinitializes the error as in dbus_error_i...
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.
#define _DBUS_INT_MAX
Maximum value of type "int".
void _dbus_abort(void)
Aborts the program with SIGABRT (dumping core).
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 ...
void * dbus_malloc(size_t bytes)
Allocates the given number of bytes, as with standard malloc().
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
void _dbus_string_init_const(DBusString *str, const char *value)
Initializes a constant string.
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's been called.
Object representing an exception.
void dbus_set_error(DBusError *error, const char *name, const char *format,...)
Assigns an error name and message to a DBusError.
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.
#define _DBUS_UNLOCK(name)
Unlocks a global lock.
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init().
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.
const char * name
public error name field
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.
unsigned long _dbus_pid_for_log(void)
The only reason this is separate from _dbus_getpid() is to allow it on Windows for logging but not fo...
void dbus_error_init(DBusError *error)
Initializes a DBusError structure.
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.
void dbus_free_string_array(char **str_array)
Frees a NULL-terminated array of strings.
dbus_bool_t _dbus_string_array_contains(const char **array, const char *str)
Checks whether a string array contains the given string.
#define FALSE
Expands to "0".
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.
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.
char * _dbus_strdup(const char *str)
Duplicates a string.
const char * _dbus_getenv(const char *varname)
Wrapper for getenv().
void _dbus_string_init_const_len(DBusString *str, const char *value, int len)
Initializes a constant string with a length.
void _dbus_get_real_time(long *tv_sec, long *tv_usec)
Get current time, as in gettimeofday().