From f1ca9b89e92de4d876dc5e7e85710c4d2dc87638 Mon Sep 17 00:00:00 2001 From: Havoc Pennington Date: Tue, 17 Oct 2006 20:52:13 +0000 Subject: 2006-10-17 Havoc Pennington * dbus/dbus-internals.c (_dbus_warn_check_failed): new function to be used for return_if_fail type warnings; prefixes the pid, and fatal by default. --- dbus/dbus-bus.c | 9 +++-- dbus/dbus-connection.c | 23 ++++++------ dbus/dbus-dataslot.c | 2 +- dbus/dbus-internals.c | 83 ++++++++++++++++++++++++++++++++++++------- dbus/dbus-internals.h | 20 ++++++----- dbus/dbus-marshal-basic.c | 4 +-- dbus/dbus-marshal-recursive.c | 20 +++++------ dbus/dbus-server.c | 4 +-- dbus/dbus-sysdeps-unix.c | 2 +- dbus/dbus-transport.c | 2 +- dbus/dbus-watch.c | 4 +-- 11 files changed, 116 insertions(+), 57 deletions(-) (limited to 'dbus') diff --git a/dbus/dbus-bus.c b/dbus/dbus-bus.c index 9eb2c526..1661f246 100644 --- a/dbus/dbus-bus.c +++ b/dbus/dbus-bus.c @@ -93,7 +93,7 @@ addresses_shutdown_func (void *data) while (i < N_BUS_TYPES) { if (bus_connections[i] != NULL) - _dbus_warn ("dbus_shutdown() called but connections were still live!"); + _dbus_warn_check_failed ("dbus_shutdown() called but connections were still live. This probably means the application did not drop all its references to bus connections.\n"); dbus_free (bus_connection_addresses[i]); bus_connection_addresses[i] = NULL; @@ -547,10 +547,9 @@ dbus_bus_register (DBusConnection *connection, if (bd->unique_name != NULL) { - _dbus_warn ("Attempt to register the same DBusConnection with the message bus, but it is already registered\n"); - /* This isn't an error, it's a programming bug. We'll be nice - * and not _dbus_assert_not_reached() - */ + _dbus_warn_check_failed ("Attempt to register the same DBusConnection %s with the message bus a second time.\n", + bd->unique_name); + /* This isn't an error, it's a programming bug. so return TRUE */ return TRUE; } diff --git a/dbus/dbus-connection.c b/dbus/dbus-connection.c index 7a38a0ab..4c1bce91 100644 --- a/dbus/dbus-connection.c +++ b/dbus/dbus-connection.c @@ -2007,11 +2007,10 @@ dbus_connection_unref (DBusConnection *connection) #ifndef DBUS_DISABLE_CHECKS if (_dbus_transport_get_is_connected (connection->transport)) { - _dbus_warn ("The last reference on a connection was dropped without closing the connection. This is a bug. See dbus_connection_unref() documentation for details.\n"); - if (connection->shareable) - _dbus_warn ("Most likely, the application called unref() too many times and removed a reference belonging to libdbus, since this is a shared connection.\n"); - else - _dbus_warn ("Most likely, the application was supposed to call dbus_connection_close(), since this is a private connection.\n"); + _dbus_warn_check_failed ("The last reference on a connection was dropped without closing the connection. This is a bug in an application. See dbus_connection_unref() documentation for details.\n%s", + connection->shareable ? + "Most likely, the application called unref() too many times and removed a reference belonging to libdbus, since this is a shared connection.\n" : + "Most likely, the application was supposed to call dbus_connection_close(), since this is a private connection.\n"); return; } #endif @@ -2128,7 +2127,7 @@ dbus_connection_close (DBusConnection *connection) { CONNECTION_UNLOCK (connection); - _dbus_warn ("Applications must not close shared connections - see dbus_connection_close() docs. This is a bug in the application.\n"); + _dbus_warn_check_failed ("Applications must not close shared connections - see dbus_connection_close() docs. This is a bug in the application.\n"); return; } #endif @@ -4378,8 +4377,8 @@ dbus_connection_set_watch_functions (DBusConnection *connection, #ifndef DBUS_DISABLE_CHECKS if (connection->watches == NULL) { - _dbus_warn ("Re-entrant call to %s is not allowed\n", - _DBUS_FUNCTION_NAME); + _dbus_warn_check_failed ("Re-entrant call to %s is not allowed\n", + _DBUS_FUNCTION_NAME); return FALSE; } #endif @@ -4460,8 +4459,8 @@ dbus_connection_set_timeout_functions (DBusConnection *connection, #ifndef DBUS_DISABLE_CHECKS if (connection->timeouts == NULL) { - _dbus_warn ("Re-entrant call to %s is not allowed\n", - _DBUS_FUNCTION_NAME); + _dbus_warn_check_failed ("Re-entrant call to %s is not allowed\n", + _DBUS_FUNCTION_NAME); return FALSE; } #endif @@ -4900,8 +4899,8 @@ dbus_connection_remove_filter (DBusConnection *connection, #ifndef DBUS_DISABLE_CHECKS if (filter == NULL) { - _dbus_warn ("Attempt to remove filter function %p user data %p, but no such filter has been added\n", - function, user_data); + _dbus_warn_check_failed ("Attempt to remove filter function %p user data %p, but no such filter has been added\n", + function, user_data); return; } #endif diff --git a/dbus/dbus-dataslot.c b/dbus/dbus-dataslot.c index f9c12f84..3e9aabfc 100644 --- a/dbus/dbus-dataslot.c +++ b/dbus/dbus-dataslot.c @@ -79,7 +79,7 @@ _dbus_data_slot_allocator_alloc (DBusDataSlotAllocator *allocator, } else if (allocator->lock_loc != mutex_loc) { - _dbus_warn ("D-Bus threads were initialized after first using the D-Bus library. If your application does not directly initialize threads or use D-Bus, keep in mind that some library or plugin may have used D-Bus or initialized threads behind your back. You can often fix this problem by calling dbus_init_threads() or dbus_g_threads_init() early in your main() method, before D-Bus is used."); + _dbus_warn_check_failed ("D-Bus threads were initialized after first using the D-Bus library. If your application does not directly initialize threads or use D-Bus, keep in mind that some library or plugin may have used D-Bus or initialized threads behind your back. You can often fix this problem by calling dbus_init_threads() or dbus_g_threads_init() early in your main() method, before D-Bus is used.\n"); _dbus_assert_not_reached ("exiting"); } diff --git a/dbus/dbus-internals.c b/dbus/dbus-internals.c index ecddfb6f..2233fc68 100644 --- a/dbus/dbus-internals.c +++ b/dbus/dbus-internals.c @@ -192,9 +192,44 @@ const char _dbus_no_memory_message[] = "Not enough memory"; static dbus_bool_t warn_initted = FALSE; static dbus_bool_t fatal_warnings = FALSE; +static dbus_bool_t fatal_warnings_on_check_failed = TRUE; + +static void +init_warnings(void) +{ + if (!warn_initted) + { + const char *s; + s = _dbus_getenv ("DBUS_FATAL_WARNINGS"); + if (s && *s) + { + if (*s == '0') + { + fatal_warnings = FALSE; + fatal_warnings_on_check_failed = FALSE; + } + else if (*s == '1') + { + fatal_warnings = TRUE; + fatal_warnings_on_check_failed = TRUE; + } + else + { + fprintf(stderr, "DBUS_FATAL_WARNINGS should be set to 0 or 1 if set, not '%s'", + s); + } + } + + warn_initted = TRUE; + } +} /** - * Prints a warning message to stderr. + * Prints a warning message to stderr. Can optionally be made to exit + * fatally by setting DBUS_FATAL_WARNINGS, but this is rarely + * used. This function should be considered pretty much equivalent to + * fprintf(stderr). _dbus_warn_check_failed() on the other hand is + * suitable for use when a programming mistake has been made. * * @param format printf-style format string. */ @@ -205,20 +240,43 @@ _dbus_warn (const char *format, va_list args; if (!warn_initted) - { - const char *s; - s = _dbus_getenv ("DBUS_FATAL_WARNINGS"); - if (s && *s) - fatal_warnings = TRUE; + init_warnings (); + + va_start (args, format); + vfprintf (stderr, format, args); + va_end (args); - warn_initted = TRUE; + if (fatal_warnings) + { + fflush (stderr); + _dbus_abort (); } +} + +/** + * Prints a "critical" warning to stderr when an assertion fails; + * differs from _dbus_warn primarily in that it prefixes the pid and + * defaults to fatal. This should be used only when a programming + * error has been detected. (NOT for unavoidable errors that an app + * might handle - those should be returned as DBusError.) Calling this + * means "there is a bug" + */ +void +_dbus_warn_check_failed(const char *format, + ...) +{ + va_list args; + + if (!warn_initted) + init_warnings (); + + fprintf (stderr, "process %lu: ", _dbus_getpid ()); va_start (args, format); vfprintf (stderr, format, args); va_end (args); - if (fatal_warnings) + if (fatal_warnings_on_check_failed) { fflush (stderr); _dbus_abort (); @@ -664,10 +722,9 @@ _dbus_get_local_machine_uuid_encoded (DBusString *uuid_str) * here. But in a production build, we want to be nice and loud about * this. */ - _dbus_warn ("D-Bus library appears to be incorrectly set up; failed to read machine uuid: %s\n", - error.message); - _dbus_warn ("See the manual page for dbus-uuidgen to correct this issue.\n"); - _dbus_warn ("Continuing with a bogus made-up machine UUID, which may cause problems."); + _dbus_warn_check_failed ("D-Bus library appears to be incorrectly set up; failed to read machine uuid: %s\n" + "See the manual page for dbus-uuidgen to correct this issue.\n", + error.message); #endif dbus_error_free (&error); @@ -722,7 +779,7 @@ _dbus_header_field_to_string (int header_field) #ifndef DBUS_DISABLE_CHECKS /** String used in _dbus_return_if_fail macro */ const char _dbus_return_if_fail_warning_format[] = -"%lu: arguments to %s() were incorrect, assertion \"%s\" failed in file %s line %d.\n" +"arguments to %s() were incorrect, assertion \"%s\" failed in file %s line %d.\n" "This is normally a bug in some application using the D-Bus library.\n"; #endif diff --git a/dbus/dbus-internals.h b/dbus/dbus-internals.h index 204ac1f9..3c8750e9 100644 --- a/dbus/dbus-internals.h +++ b/dbus/dbus-internals.h @@ -42,6 +42,10 @@ DBUS_BEGIN_DECLS void _dbus_warn (const char *format, ...) _DBUS_GNUC_PRINTF (1, 2); +void _dbus_warn_check_failed (const char *format, + ...) _DBUS_GNUC_PRINTF (1, 2); + + #if defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) #define _DBUS_FUNCTION_NAME __func__ #elif defined(__GNUC__) @@ -129,19 +133,19 @@ void _dbus_real_assert_not_reached (const char *explanation, #else extern const char _dbus_return_if_fail_warning_format[]; -#define _dbus_return_if_fail(condition) do { \ - _dbus_assert ((*(const char*)_DBUS_FUNCTION_NAME) != '_'); \ - if (!(condition)) { \ - _dbus_warn (_dbus_return_if_fail_warning_format, \ - _dbus_getpid (), _DBUS_FUNCTION_NAME, #condition, __FILE__, __LINE__); \ - return; \ +#define _dbus_return_if_fail(condition) do { \ + _dbus_assert ((*(const char*)_DBUS_FUNCTION_NAME) != '_'); \ + if (!(condition)) { \ + _dbus_warn_check_failed (_dbus_return_if_fail_warning_format, \ + _DBUS_FUNCTION_NAME, #condition, __FILE__, __LINE__); \ + return; \ } } while (0) #define _dbus_return_val_if_fail(condition, val) do { \ _dbus_assert ((*(const char*)_DBUS_FUNCTION_NAME) != '_'); \ if (!(condition)) { \ - _dbus_warn (_dbus_return_if_fail_warning_format, \ - _dbus_getpid (), _DBUS_FUNCTION_NAME, #condition, __FILE__, __LINE__); \ + _dbus_warn_check_failed (_dbus_return_if_fail_warning_format, \ + _DBUS_FUNCTION_NAME, #condition, __FILE__, __LINE__); \ return (val); \ } } while (0) diff --git a/dbus/dbus-marshal-basic.c b/dbus/dbus-marshal-basic.c index 56f4b83d..944e2c78 100644 --- a/dbus/dbus-marshal-basic.c +++ b/dbus/dbus-marshal-basic.c @@ -578,8 +578,8 @@ _dbus_marshal_read_basic (const DBusString *str, } break; default: - _dbus_warn ("type %s %d not a basic type\n", - _dbus_type_to_string (type), type); + _dbus_warn_check_failed ("type %s %d not a basic type\n", + _dbus_type_to_string (type), type); _dbus_assert_not_reached ("not a basic type"); break; } diff --git a/dbus/dbus-marshal-recursive.c b/dbus/dbus-marshal-recursive.c index db71e863..536d4cbf 100644 --- a/dbus/dbus-marshal-recursive.c +++ b/dbus/dbus-marshal-recursive.c @@ -1007,7 +1007,7 @@ _dbus_type_reader_recurse (DBusTypeReader *reader, _dbus_verbose ("recursing into type %s\n", _dbus_type_to_string (t)); #ifndef DBUS_DISABLE_CHECKS if (t == DBUS_TYPE_INVALID) - _dbus_warn ("You can't recurse into an empty array or off the end of a message body\n"); + _dbus_warn_check_failed ("You can't recurse into an empty array or off the end of a message body\n"); #endif /* DBUS_DISABLE_CHECKS */ _dbus_assert_not_reached ("don't yet handle recursing into this type"); @@ -1645,9 +1645,9 @@ writer_recurse_init_and_check (DBusTypeWriter *writer, if (expected != sub->container_type) { - _dbus_warn ("Writing an element of type %s, but the expected type here is %s\n", - _dbus_type_to_string (sub->container_type), - _dbus_type_to_string (expected)); + _dbus_warn_check_failed ("Writing an element of type %s, but the expected type here is %s\n", + _dbus_type_to_string (sub->container_type), + _dbus_type_to_string (expected)); _dbus_assert_not_reached ("bad array element or variant content written"); } } @@ -1702,8 +1702,8 @@ write_or_verify_typecode (DBusTypeWriter *writer, if (expected != typecode) { - _dbus_warn ("Array or variant type requires that type %s be written, but %s was written\n", - _dbus_type_to_string (expected), _dbus_type_to_string (typecode)); + _dbus_warn_check_failed ("Array or variant type requires that type %s be written, but %s was written\n", + _dbus_type_to_string (expected), _dbus_type_to_string (typecode)); _dbus_assert_not_reached ("bad type inserted somewhere inside an array or variant"); } } @@ -1794,10 +1794,10 @@ writer_recurse_array (DBusTypeWriter *writer, writer->type_str, writer->u.array.element_type_pos + 1)) { - _dbus_warn ("Writing an array of '%s' but this is incompatible with the expected type of elements in the parent array\n", - _dbus_string_get_const_data_len (contained_type, - contained_type_start, - contained_type_len)); + _dbus_warn_check_failed ("Writing an array of '%s' but this is incompatible with the expected type of elements in the parent array\n", + _dbus_string_get_const_data_len (contained_type, + contained_type_start, + contained_type_len)); _dbus_assert_not_reached ("incompatible type for child array"); } } diff --git a/dbus/dbus-server.c b/dbus/dbus-server.c index e8bb8d11..059c8485 100644 --- a/dbus/dbus-server.c +++ b/dbus/dbus-server.c @@ -875,7 +875,7 @@ dbus_server_set_watch_functions (DBusServer *server, } else { - _dbus_warn ("Re-entrant call to %s\n", _DBUS_FUNCTION_NAME); + _dbus_warn_check_failed ("Re-entrant call to %s\n", _DBUS_FUNCTION_NAME); result = FALSE; } server->watches = watches; @@ -928,7 +928,7 @@ dbus_server_set_timeout_functions (DBusServer *server, } else { - _dbus_warn ("Re-entrant call to %s\n", _DBUS_FUNCTION_NAME); + _dbus_warn_check_failed ("Re-entrant call to %s\n", _DBUS_FUNCTION_NAME); result = FALSE; } server->timeouts = timeouts; diff --git a/dbus/dbus-sysdeps-unix.c b/dbus/dbus-sysdeps-unix.c index ce565884..aa0de381 100644 --- a/dbus/dbus-sysdeps-unix.c +++ b/dbus/dbus-sysdeps-unix.c @@ -2320,7 +2320,7 @@ _dbus_get_autolaunch_address (DBusString *address, ++i; argv[i] = "--autolaunch"; ++i; - argv[i] = _dbus_string_get_const_data (&uuid); + argv[i] = /* const cast */ (char*) _dbus_string_get_const_data (&uuid); ++i; argv[i] = "--binary-syntax"; ++i; diff --git a/dbus/dbus-transport.c b/dbus/dbus-transport.c index f43dd3d2..d8d3bb29 100644 --- a/dbus/dbus-transport.c +++ b/dbus/dbus-transport.c @@ -676,7 +676,7 @@ _dbus_transport_handle_watch (DBusTransport *transport, if (dbus_watch_get_fd (watch) < 0) { - _dbus_warn ("Tried to handle an invalidated watch; this watch should have been removed\n"); + _dbus_warn_check_failed ("Tried to handle an invalidated watch; this watch should have been removed\n"); return TRUE; } diff --git a/dbus/dbus-watch.c b/dbus/dbus-watch.c index 019b8b1c..a7a5adba 100644 --- a/dbus/dbus-watch.c +++ b/dbus/dbus-watch.c @@ -594,8 +594,8 @@ dbus_watch_handle (DBusWatch *watch, #ifndef DBUS_DISABLE_CHECKS if (watch->fd < 0 || watch->flags == 0) { - _dbus_warn ("%s: Watch is invalid, it should have been removed\n", - _DBUS_FUNCTION_NAME); + _dbus_warn_check_failed ("%s: Watch is invalid, it should have been removed\n", + _DBUS_FUNCTION_NAME); return TRUE; } #endif -- cgit