diff options
39 files changed, 649 insertions, 579 deletions
@@ -1,3 +1,19 @@ +2003-03-24 Havoc Pennington <hp@redhat.com> + + * dbus/dbus-sysdeps.c (_dbus_set_fd_nonblocking): move to this + file + + * dbus/dbus-errors.c (dbus_set_error, dbus_set_error_const): allow + NULL argument for "message" if the error is a well-known one, + fill in a generic message in this case. + + * dbus/dbus-errors.h (DBusResultCode): Kill DBusResultCode in + favor of DBusError + + * bus/test.c (bus_test_flush_bus): add + + * bus/policy.c (bus_policy_test): test code stub + 2003-03-24 Havoc Pennington <hp@pobox.com> * bus/connection.c (bus_connections_setup_connection): set up @@ -143,7 +143,6 @@ bus_context_new (const char *address, DBusError *error) { BusContext *context; - DBusResultCode result; context = dbus_new0 (BusContext, 1); if (context == NULL) @@ -161,14 +160,9 @@ bus_context_new (const char *address, goto failed; } - context->server = dbus_server_listen (address, &result); + context->server = dbus_server_listen (address, error); if (context->server == NULL) - { - dbus_set_error (error, DBUS_ERROR_FAILED, - "Failed to start server on %s: %s\n", - address, dbus_result_to_string (result)); - goto failed; - } + goto failed; context->activation = bus_activation_new (context, address, service_dirs, error); diff --git a/bus/dispatch.c b/bus/dispatch.c index 33289e1f..490ecf24 100644 --- a/bus/dispatch.c +++ b/bus/dispatch.c @@ -441,22 +441,6 @@ typedef dbus_bool_t (* Check2Func) (BusContext *context, static dbus_bool_t check_no_leftovers (BusContext *context); -static void -flush_bus (BusContext *context) -{ - /* This is race condition city, obviously. since we're all in one - * process we can't block, we just have to wait for data we put in - * one end of the debug pipe to come out the other end... - * a more robust setup would be good. - */ - - while (bus_loop_iterate (FALSE)) - ; - _dbus_sleep_milliseconds (15); - while (bus_loop_iterate (FALSE)) - ; -} - typedef struct { const char *expected_service_name; @@ -549,7 +533,7 @@ kill_client_connection (BusContext *context, /* kick in the disconnect handler that unrefs the connection */ dbus_connection_disconnect (connection); - flush_bus (context); + bus_test_flush_bus (context); _dbus_assert (bus_test_client_listed (connection)); @@ -742,7 +726,7 @@ check_hello_message (BusContext *context, dbus_message_unref (message); message = NULL; - flush_bus (context); + bus_test_flush_bus (context); if (!dbus_connection_get_is_connected (connection)) { @@ -891,13 +875,15 @@ static dbus_bool_t check_hello_connection (BusContext *context) { DBusConnection *connection; - DBusResultCode result; + DBusError error; - result = DBUS_RESULT_SUCCESS; - connection = dbus_connection_open ("debug-pipe:name=test-server", &result); + dbus_error_init (&error); + + connection = dbus_connection_open ("debug-pipe:name=test-server", &error); if (connection == NULL) { - _dbus_assert (result != DBUS_RESULT_SUCCESS); + _DBUS_ASSERT_ERROR_IS_SET (&error); + dbus_error_free (&error); return TRUE; } @@ -980,7 +966,6 @@ bus_dispatch_test (const DBusString *test_data_dir) DBusConnection *foo; DBusConnection *bar; DBusConnection *baz; - DBusResultCode result; dbus_error_init (&error); context = bus_context_new ("debug-pipe:name=test-server", @@ -989,7 +974,7 @@ bus_dispatch_test (const DBusString *test_data_dir) if (context == NULL) _dbus_assert_not_reached ("could not alloc context"); - foo = dbus_connection_open ("debug-pipe:name=test-server", &result); + foo = dbus_connection_open ("debug-pipe:name=test-server", &error); if (foo == NULL) _dbus_assert_not_reached ("could not alloc connection"); @@ -999,7 +984,7 @@ bus_dispatch_test (const DBusString *test_data_dir) if (!check_hello_message (context, foo)) _dbus_assert_not_reached ("hello message failed"); - bar = dbus_connection_open ("debug-pipe:name=test-server", &result); + bar = dbus_connection_open ("debug-pipe:name=test-server", &error); if (bar == NULL) _dbus_assert_not_reached ("could not alloc connection"); @@ -1009,7 +994,7 @@ bus_dispatch_test (const DBusString *test_data_dir) if (!check_hello_message (context, bar)) _dbus_assert_not_reached ("hello message failed"); - baz = dbus_connection_open ("debug-pipe:name=test-server", &result); + baz = dbus_connection_open ("debug-pipe:name=test-server", &error); if (baz == NULL) _dbus_assert_not_reached ("could not alloc connection"); diff --git a/bus/policy.c b/bus/policy.c index 75013c85..b5187715 100644 --- a/bus/policy.c +++ b/bus/policy.c @@ -23,6 +23,7 @@ #include "policy.h" #include "services.h" +#include "test.h" #include <dbus/dbus-list.h> #include <dbus/dbus-internals.h> @@ -421,3 +422,18 @@ bus_policy_check_can_own (BusPolicy *policy, return allowed; } + +#ifdef DBUS_BUILD_TESTS + +dbus_bool_t +bus_policy_test (const DBusString *test_data_dir) +{ + /* This doesn't do anything for now because I decided to do it in + * dispatch.c instead by having some of the clients in dispatch.c + * have particular policies applied to them. + */ + + return TRUE; +} + +#endif /* DBUS_BUILD_TESTS */ @@ -267,4 +267,22 @@ bus_test_client_listed (DBusConnection *connection) return FALSE; } + +void +bus_test_flush_bus (BusContext *context) +{ + /* This is race condition city, obviously. since we're all in one + * process we can't block, we just have to wait for data we put in + * one end of the debug pipe to come out the other end... + * a more robust setup would be good. Blocking on the other + * end of pipes we've pushed data into or something. + */ + + while (bus_loop_iterate (FALSE)) + ; + _dbus_sleep_milliseconds (15); + while (bus_loop_iterate (FALSE)) + ; +} + #endif @@ -33,10 +33,12 @@ #include "connection.h" dbus_bool_t bus_dispatch_test (const DBusString *test_data_dir); +dbus_bool_t bus_policy_test (const DBusString *test_data_dir); dbus_bool_t bus_setup_debug_client (DBusConnection *connection); void bus_test_clients_foreach (BusConnectionForeachFunction function, void *data); dbus_bool_t bus_test_client_listed (DBusConnection *connection); +void bus_test_flush_bus (BusContext *context); #endif diff --git a/dbus/dbus-address.c b/dbus/dbus-address.c index ef8a51da..ff0ba3b5 100644 --- a/dbus/dbus-address.c +++ b/dbus/dbus-address.c @@ -177,13 +177,15 @@ dbus_bool_t dbus_parse_address (const char *address, DBusAddressEntry ***entry, int *array_len, - DBusResultCode *result) + DBusError *error) { DBusString str; int pos, end_pos, len, i; DBusList *entries, *link; DBusAddressEntry **entry_array; + _DBUS_ASSERT_ERROR_IS_CLEAR (error); + _dbus_string_init_const (&str, address); entries = NULL; @@ -199,7 +201,7 @@ dbus_parse_address (const char *address, entry = create_entry (); if (!entry) { - dbus_set_result (result, DBUS_RESULT_NO_MEMORY); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); goto error; } @@ -207,7 +209,7 @@ dbus_parse_address (const char *address, /* Append the entry */ if (!_dbus_list_append (&entries, entry)) { - dbus_set_result (result, DBUS_RESULT_NO_MEMORY); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); dbus_address_entry_free (entry); goto error; } @@ -219,13 +221,13 @@ dbus_parse_address (const char *address, /* Look for the colon : */ if (!_dbus_string_find_to (&str, pos, end_pos, ":", &found_pos)) { - dbus_set_result (result, DBUS_RESULT_BAD_ADDRESS); + dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS, "Address does not contain a colon"); goto error; } if (!_dbus_string_copy_len (&str, pos, found_pos - pos, &entry->method, 0)) { - dbus_set_result (result, DBUS_RESULT_NO_MEMORY); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); goto error; } @@ -241,8 +243,9 @@ dbus_parse_address (const char *address, if (!_dbus_string_find (&str, pos, "=", &equals_pos) || equals_pos == pos || equals_pos + 1 == end_pos) { - dbus_set_result (result, DBUS_RESULT_BAD_ADDRESS); - goto error; + dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS, + "'=' character not found or has no value following it"); + goto error; } else { @@ -253,21 +256,21 @@ dbus_parse_address (const char *address, if (!key) { - dbus_set_result (result, DBUS_RESULT_NO_MEMORY); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); goto error; } value = dbus_new0 (DBusString, 1); if (!value) { - dbus_set_result (result, DBUS_RESULT_NO_MEMORY); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); dbus_free (key); goto error; } if (!_dbus_string_init (key, _DBUS_INT_MAX)) { - dbus_set_result (result, DBUS_RESULT_NO_MEMORY); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); dbus_free (key); dbus_free (value); @@ -276,7 +279,7 @@ dbus_parse_address (const char *address, if (!_dbus_string_init (value, _DBUS_INT_MAX)) { - dbus_set_result (result, DBUS_RESULT_NO_MEMORY); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); _dbus_string_free (key); dbus_free (key); @@ -286,7 +289,7 @@ dbus_parse_address (const char *address, if (!_dbus_string_copy_len (&str, pos, equals_pos - pos, key, 0)) { - dbus_set_result (result, DBUS_RESULT_NO_MEMORY); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); _dbus_string_free (key); _dbus_string_free (value); @@ -297,7 +300,7 @@ dbus_parse_address (const char *address, if (!_dbus_string_copy_len (&str, equals_pos + 1, comma_pos - equals_pos - 1, value, 0)) { - dbus_set_result (result, DBUS_RESULT_NO_MEMORY); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); _dbus_string_free (key); _dbus_string_free (value); @@ -308,7 +311,7 @@ dbus_parse_address (const char *address, if (!_dbus_list_append (&entry->keys, key)) { - dbus_set_result (result, DBUS_RESULT_NO_MEMORY); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); _dbus_string_free (key); _dbus_string_free (value); @@ -319,7 +322,7 @@ dbus_parse_address (const char *address, if (!_dbus_list_append (&entry->values, value)) { - dbus_set_result (result, DBUS_RESULT_NO_MEMORY); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); _dbus_string_free (value); dbus_free (value); @@ -339,7 +342,7 @@ dbus_parse_address (const char *address, if (!entry_array) { - dbus_set_result (result, DBUS_RESULT_NO_MEMORY); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); goto error; } @@ -358,7 +361,6 @@ dbus_parse_address (const char *address, _dbus_list_clear (&entries); *entry = entry_array; - dbus_set_result (result, DBUS_RESULT_SUCCESS); return TRUE; error: @@ -387,10 +389,12 @@ _dbus_address_test (void) { DBusAddressEntry **entries; int len; - DBusResultCode result; + DBusError error; + dbus_error_init (&error); + if (!dbus_parse_address ("unix:path=/tmp/foo;debug:name=test,sliff=sloff;", - &entries, &len, &result)) + &entries, &len, &error)) _dbus_assert_not_reached ("could not parse address"); _dbus_assert (len == 2); _dbus_assert (strcmp (dbus_address_entry_get_value (entries[0], "path"), "/tmp/foo") == 0); @@ -400,26 +404,40 @@ _dbus_address_test (void) dbus_address_entries_free (entries); /* Different possible errors */ - if (dbus_parse_address ("foo", &entries, &len, &result)) + if (dbus_parse_address ("foo", &entries, &len, &error)) _dbus_assert_not_reached ("Parsed incorrect address."); - - if (dbus_parse_address ("foo:bar", &entries, &len, &result)) + else + dbus_error_free (&error); + + if (dbus_parse_address ("foo:bar", &entries, &len, &error)) _dbus_assert_not_reached ("Parsed incorrect address."); - - if (dbus_parse_address ("foo:bar,baz", &entries, &len, &result)) + else + dbus_error_free (&error); + + if (dbus_parse_address ("foo:bar,baz", &entries, &len, &error)) _dbus_assert_not_reached ("Parsed incorrect address."); - - if (dbus_parse_address ("foo:bar=foo,baz", &entries, &len, &result)) + else + dbus_error_free (&error); + + if (dbus_parse_address ("foo:bar=foo,baz", &entries, &len, &error)) _dbus_assert_not_reached ("Parsed incorrect address."); - - if (dbus_parse_address ("foo:bar=foo;baz", &entries, &len, &result)) + else + dbus_error_free (&error); + + if (dbus_parse_address ("foo:bar=foo;baz", &entries, &len, &error)) _dbus_assert_not_reached ("Parsed incorrect address."); - - if (dbus_parse_address ("foo:=foo", &entries, &len, &result)) + else + dbus_error_free (&error); + + if (dbus_parse_address ("foo:=foo", &entries, &len, &error)) _dbus_assert_not_reached ("Parsed incorrect address."); - - if (dbus_parse_address ("foo:foo=", &entries, &len, &result)) + else + dbus_error_free (&error); + + if (dbus_parse_address ("foo:foo=", &entries, &len, &error)) _dbus_assert_not_reached ("Parsed incorrect address."); + else + dbus_error_free (&error); return TRUE; } diff --git a/dbus/dbus-address.h b/dbus/dbus-address.h index 294eb2bc..eb5b66e3 100644 --- a/dbus/dbus-address.h +++ b/dbus/dbus-address.h @@ -35,7 +35,7 @@ typedef struct DBusAddressEntry DBusAddressEntry; dbus_bool_t dbus_parse_address (const char *address, DBusAddressEntry ***entry, int *array_len, - DBusResultCode *result); + DBusError *error); const char *dbus_address_entry_get_value (DBusAddressEntry *entry, const char *key); const char *dbus_address_entry_get_method (DBusAddressEntry *entry); diff --git a/dbus/dbus-bus.c b/dbus/dbus-bus.c index 7a539382..1b63d608 100644 --- a/dbus/dbus-bus.c +++ b/dbus/dbus-bus.c @@ -184,6 +184,8 @@ dbus_bus_register (DBusConnection *connection, char *name; BusData *bd; + _DBUS_ASSERT_ERROR_IS_CLEAR (error); + bd = ensure_bus_data (connection); if (bd == NULL) { @@ -363,6 +365,8 @@ dbus_bus_service_exists (DBusConnection *connection, { DBusMessage *message, *reply; unsigned int exists; + + _DBUS_ASSERT_ERROR_IS_CLEAR (error); message = dbus_message_new (DBUS_SERVICE_DBUS, DBUS_MESSAGE_SERVICE_EXISTS); diff --git a/dbus/dbus-connection.c b/dbus/dbus-connection.c index 76bbfe41..1a00b702 100644 --- a/dbus/dbus-connection.c +++ b/dbus/dbus-connection.c @@ -800,19 +800,24 @@ _dbus_connection_set_connection_counter (DBusConnection *connection, * in the reason for failure. * * @param address the address. - * @param result address where a result code can be returned. + * @param error address where an error can be returned. * @returns new connection, or #NULL on failure. */ DBusConnection* dbus_connection_open (const char *address, - DBusResultCode *result) + DBusError *error) { DBusConnection *connection; DBusTransport *transport; + + _DBUS_ASSERT_ERROR_IS_CLEAR (error); - transport = _dbus_transport_open (address, result); + transport = _dbus_transport_open (address, error); if (transport == NULL) - return NULL; + { + _DBUS_ASSERT_ERROR_IS_SET (error); + return NULL; + } connection = _dbus_connection_new_for_transport (transport); @@ -820,7 +825,7 @@ dbus_connection_open (const char *address, if (connection == NULL) { - dbus_set_result (result, DBUS_RESULT_NO_MEMORY); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); return NULL; } @@ -1424,6 +1429,8 @@ dbus_connection_send_with_reply_and_block (DBusConnection *connection, long end_tv_sec, end_tv_usec; long tv_sec, tv_usec; DBusDispatchStatus status; + + _DBUS_ASSERT_ERROR_IS_CLEAR (error); if (timeout_milliseconds == -1) timeout_milliseconds = DEFAULT_TIMEOUT_VALUE; diff --git a/dbus/dbus-connection.h b/dbus/dbus-connection.h index 21a4a15a..1acbb1ad 100644 --- a/dbus/dbus-connection.h +++ b/dbus/dbus-connection.h @@ -81,7 +81,7 @@ typedef dbus_bool_t (* DBusAllowUnixUserFunction) (DBusConnection *connection, void *data); DBusConnection* dbus_connection_open (const char *address, - DBusResultCode *result); + DBusError *error); void dbus_connection_ref (DBusConnection *connection); void dbus_connection_unref (DBusConnection *connection); void dbus_connection_disconnect (DBusConnection *connection); diff --git a/dbus/dbus-errors.c b/dbus/dbus-errors.c index da49e2ea..7e3aa69b 100644 --- a/dbus/dbus-errors.c +++ b/dbus/dbus-errors.c @@ -25,6 +25,7 @@ #include "dbus-internals.h" #include <stdarg.h> #include <stdio.h> +#include <string.h> /** * @defgroup DBusErrors Error reporting @@ -65,73 +66,50 @@ typedef struct } DBusRealError; /** - * Set a result code at a result code location, - * if code_address is not #NULL. + * Returns a longer message describing an error name. + * If the error name is unknown, returns the name + * itself. * - * @param code_address place to store the result code. - * @param code the result code itself. + * @param error the error to describe + * @returns a constant string describing the error. */ -void -dbus_set_result (DBusResultCode *code_address, - DBusResultCode code) -{ - if (code_address) - *code_address = code; -} - -/** - * Returns a string describing the given result code. - * - * @param code the result code to describe. - * @returns a constant string describing the code. - */ -const char* -dbus_result_to_string (DBusResultCode code) +static const char* +message_from_error (const char *error) { - /* This is a switch to the compiler will complain if we - * aren't handling some codes - */ - switch (code) - { - case DBUS_RESULT_SUCCESS: - return "Success"; - case DBUS_RESULT_FAILED: - return "Unknown error"; - case DBUS_RESULT_NO_MEMORY: - return "Not enough memory available"; - case DBUS_RESULT_IO_ERROR: - return "Error reading or writing data"; - case DBUS_RESULT_BAD_ADDRESS: - return "Could not parse address"; - case DBUS_RESULT_NOT_SUPPORTED: - return "Feature not supported"; - case DBUS_RESULT_LIMITS_EXCEEDED: - return "Resource limits exceeded"; - case DBUS_RESULT_ACCESS_DENIED: - return "Permission denied"; - case DBUS_RESULT_AUTH_FAILED: - return "Could not authenticate to server"; - case DBUS_RESULT_NO_SERVER: - return "No server"; - case DBUS_RESULT_TIMEOUT: - return "Connection timed out"; - case DBUS_RESULT_NO_NETWORK: - return "Network unavailable"; - case DBUS_RESULT_ADDRESS_IN_USE: - return "Address already in use"; - case DBUS_RESULT_DISCONNECTED: - return "Disconnected."; - case DBUS_RESULT_INVALID_ARGS: - return "Invalid argumemts."; - case DBUS_RESULT_NO_REPLY: - return "Did not get a reply message."; - case DBUS_RESULT_FILE_NOT_FOUND: - return "File doesn't exist."; - - /* no default, it would break our compiler warnings */ - } - - return "Invalid error code"; + if (strcmp (error, DBUS_ERROR_FAILED) == 0) + return "Unknown error"; + else if (strcmp (error, DBUS_ERROR_NO_MEMORY) == 0) + return "Not enough memory available"; + else if (strcmp (error, DBUS_ERROR_IO_ERROR) == 0) + return "Error reading or writing data"; + else if (strcmp (error, DBUS_ERROR_BAD_ADDRESS) == 0) + return "Could not parse address"; + else if (strcmp (error, DBUS_ERROR_NOT_SUPPORTED) == 0) + return "Feature not supported"; + else if (strcmp (error, DBUS_ERROR_LIMITS_EXCEEDED) == 0) + return "Resource limits exceeded"; + else if (strcmp (error, DBUS_ERROR_ACCESS_DENIED) == 0) + return "Permission denied"; + else if (strcmp (error, DBUS_ERROR_AUTH_FAILED) == 0) + return "Could not authenticate to server"; + else if (strcmp (error, DBUS_ERROR_NO_SERVER) == 0) + return "No server"; + else if (strcmp (error, DBUS_ERROR_TIMEOUT) == 0) + return "Connection timed out"; + else if (strcmp (error, DBUS_ERROR_NO_NETWORK) == 0) + return "Network unavailable"; + else if (strcmp (error, DBUS_ERROR_ADDRESS_IN_USE) == 0) + return "Address already in use"; + else if (strcmp (error, DBUS_ERROR_DISCONNECTED) == 0) + return "Disconnected."; + else if (strcmp (error, DBUS_ERROR_INVALID_ARGS) == 0) + return "Invalid argumemts."; + else if (strcmp (error, DBUS_ERROR_NO_REPLY) == 0) + return "Did not get a reply message."; + else if (strcmp (error, DBUS_ERROR_FILE_NOT_FOUND) == 0) + return "File doesn't exist."; + else + return error; } /** @@ -179,7 +157,8 @@ dbus_error_free (DBusError *error) /** * Assigns an error name and message to a DBusError. - * Does nothing if error is #NULL. + * Does nothing if error is #NULL. The message may + * be NULL only if the error is DBUS_ERROR_NO_MEMORY. * * @param error the error. * @param name the error name (not copied!!!) @@ -199,7 +178,9 @@ dbus_set_error_const (DBusError *error, _dbus_assert (error->name == NULL); _dbus_assert (error->message == NULL); _dbus_assert (name != NULL); - _dbus_assert (message != NULL); + + if (message == NULL) + message = message_from_error (name); real = (DBusRealError *)error; @@ -280,6 +261,8 @@ dbus_error_is_set (const DBusError *error) * Assigns an error name and message to a DBusError. * Does nothing if error is #NULL. * + * The format may be NULL only if the error is DBUS_ERROR_NO_MEMORY. + * * If no memory can be allocated for the error message, * an out-of-memory error message will be set instead. * @@ -309,7 +292,9 @@ dbus_set_error (DBusError *error, _dbus_assert (error->name == NULL); _dbus_assert (error->message == NULL); _dbus_assert (name != NULL); - _dbus_assert (format != NULL); + + if (format == NULL) + format = message_from_error (name); va_start (args, format); /* Measure the message length */ @@ -320,8 +305,7 @@ dbus_set_error (DBusError *error, if (!message) { - dbus_set_error_const (error, DBUS_ERROR_NO_MEMORY, - "Failed to allocate memory for error message."); + dbus_set_error_const (error, DBUS_ERROR_NO_MEMORY, NULL); return; } diff --git a/dbus/dbus-errors.h b/dbus/dbus-errors.h index 63edbdbf..e6e24a85 100644 --- a/dbus/dbus-errors.h +++ b/dbus/dbus-errors.h @@ -71,30 +71,6 @@ struct DBusError #define DBUS_ERROR_FILE_NOT_FOUND "org.freedesktop.DBus.Error.FileNotFound" #define DBUS_ERROR_UNKNOWN_MESSAGE "org.freedesktop.DBus.Error.UnknownMessage" -typedef enum -{ - DBUS_RESULT_SUCCESS, /**< Operation was successful. */ - DBUS_RESULT_FAILED, /**< Operation failed for unspecified reason. */ - DBUS_RESULT_NO_MEMORY, /**< Operation failed for lack of memory. */ - DBUS_RESULT_IO_ERROR, /**< Operation failed because of an IO error, - * typically the other end closed the - * connection. - */ - DBUS_RESULT_BAD_ADDRESS, /**< Address was bad, could not be parsed. */ - DBUS_RESULT_NOT_SUPPORTED, /**< Feature is not supported. */ - DBUS_RESULT_LIMITS_EXCEEDED, /**< Some kernel resource limit exceeded. */ - DBUS_RESULT_ACCESS_DENIED, /**< Some sort of permissions/security problem. */ - DBUS_RESULT_AUTH_FAILED, /**< Could not authenticate. */ - DBUS_RESULT_NO_SERVER, /**< No one listening on the other end. */ - DBUS_RESULT_TIMEOUT, /**< Timed out trying to connect. */ - DBUS_RESULT_NO_NETWORK, /**< Can't find the network */ - DBUS_RESULT_ADDRESS_IN_USE, /**< Someone's already using the address */ - DBUS_RESULT_DISCONNECTED, /**< No more connection. */ - DBUS_RESULT_INVALID_ARGS, /**< One or more invalid arguments encountered. */ - DBUS_RESULT_NO_REPLY, /**< Did not get a reply message. */ - DBUS_RESULT_FILE_NOT_FOUND /**< File doesn't exist */ -} DBusResultCode; - void dbus_error_init (DBusError *error); void dbus_error_free (DBusError *error); void dbus_set_error (DBusError *error, @@ -110,11 +86,6 @@ dbus_bool_t dbus_error_has_name (const DBusError *error, const char *name); dbus_bool_t dbus_error_is_set (const DBusError *error); - -void dbus_set_result (DBusResultCode *code_address, - DBusResultCode code); -const char* dbus_result_to_string (DBusResultCode code); - DBUS_END_DECLS; #endif /* DBUS_ERROR_H */ diff --git a/dbus/dbus-internals.c b/dbus/dbus-internals.c index f9d632b4..9588e72b 100644 --- a/dbus/dbus-internals.c +++ b/dbus/dbus-internals.c @@ -251,40 +251,6 @@ _dbus_strdup (const char *str) } /** - * Sets a file descriptor to be nonblocking. - * - * @param fd the file descriptor. - * @param result address of result code. - * @returns #TRUE on success. - */ -dbus_bool_t -_dbus_set_fd_nonblocking (int fd, - DBusResultCode *result) -{ - int val; - - val = fcntl (fd, F_GETFL, 0); - if (val < 0) - { - dbus_set_result (result, _dbus_result_from_errno (errno)); - _dbus_verbose ("Failed to get flags for fd %d: %s\n", fd, - _dbus_strerror (errno)); - return FALSE; - } - - if (fcntl (fd, F_SETFL, val | O_NONBLOCK) < 0) - { - dbus_set_result (result, _dbus_result_from_errno (errno)); - _dbus_verbose ("Failed to set fd %d nonblocking: %s\n", - fd, _dbus_strerror (errno)); - - return FALSE; - } - - return TRUE; -} - -/** * Returns a string describing the given type. * * @param type the type to describe diff --git a/dbus/dbus-internals.h b/dbus/dbus-internals.h index 72658b65..d95e58d6 100644 --- a/dbus/dbus-internals.h +++ b/dbus/dbus-internals.h @@ -73,8 +73,6 @@ void _dbus_verbose_real (const char *format, const char* _dbus_strerror (int error_number); -DBusResultCode _dbus_result_from_errno (int error_number); - #ifdef DBUS_DISABLE_ASSERT #define _dbus_assert(condition) #else @@ -110,7 +108,8 @@ do { #define _DBUS_STRUCT_OFFSET(struct_type, member) \ ((long) ((unsigned char*) &((struct_type*) 0)->member)) -#define _DBUS_ASSERT_ERROR_IS_SET(error) _dbus_assert ((error) == NULL || dbus_error_is_set ((error))) +#define _DBUS_ASSERT_ERROR_IS_SET(error) _dbus_assert ((error) == NULL || dbus_error_is_set ((error))) +#define _DBUS_ASSERT_ERROR_IS_CLEAR(error) _dbus_assert ((error) == NULL || !dbus_error_is_set ((error))) /* This alignment thing is from ORBit2 */ /* Align a value upward to a boundary, expressed as a number of bytes. @@ -153,7 +152,7 @@ typedef void (* DBusForeachFunction) (void *element, void *data); dbus_bool_t _dbus_set_fd_nonblocking (int fd, - DBusResultCode *result); + DBusError *error); void _dbus_verbose_bytes (const unsigned char *data, int len); diff --git a/dbus/dbus-keyring.c b/dbus/dbus-keyring.c index db432be8..c5760a1d 100644 --- a/dbus/dbus-keyring.c +++ b/dbus/dbus-keyring.c @@ -277,11 +277,12 @@ add_new_key (DBusKey **keys_p, dbus_bool_t retval; DBusKey *keys; int n_keys; - + + _DBUS_ASSERT_ERROR_IS_CLEAR (error); + if (!_dbus_string_init (&bytes, _DBUS_INT_MAX)) { - dbus_set_error (error, DBUS_ERROR_NO_MEMORY, - "No memory to generate new secret key"); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); return FALSE; } @@ -294,8 +295,7 @@ add_new_key (DBusKey **keys_p, if (!_dbus_generate_random_bytes (&bytes, 4)) { - dbus_set_error (error, DBUS_ERROR_NO_MEMORY, - "No memory to generate new secret key"); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); goto out; } @@ -320,16 +320,14 @@ add_new_key (DBusKey **keys_p, _dbus_string_set_length (&bytes, 0); if (!_dbus_generate_random_bytes (&bytes, KEY_LENGTH_BYTES)) { - dbus_set_error (error, DBUS_ERROR_NO_MEMORY, - "No memory to generate new secret key"); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); goto out; } new = dbus_realloc (keys, sizeof (DBusKey) * (n_keys + 1)); if (new == NULL) { - dbus_set_error (error, DBUS_ERROR_NO_MEMORY, - "No memory to reallocate secret key list"); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); goto out; } @@ -340,8 +338,7 @@ add_new_key (DBusKey **keys_p, _DBUS_INT_MAX)) { n_keys -= 1; /* we don't want to free the one we didn't init */ - dbus_set_error (error, DBUS_ERROR_NO_MEMORY, - "No memory to store secret key"); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); goto out; } @@ -353,8 +350,7 @@ add_new_key (DBusKey **keys_p, &keys[n_keys-1].secret, 0)) { - dbus_set_error (error, DBUS_ERROR_NO_MEMORY, - "No memory to store secret key"); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); goto out; } @@ -392,7 +388,6 @@ _dbus_keyring_reload (DBusKeyring *keyring, { DBusString contents; DBusString line; - DBusResultCode result; dbus_bool_t retval; dbus_bool_t have_lock; DBusKey *keys; @@ -400,18 +395,18 @@ _dbus_keyring_reload (DBusKeyring *keyring, int i; long now; DBusError tmp_error; + + _DBUS_ASSERT_ERROR_IS_CLEAR (error); if (!_dbus_string_init (&contents, _DBUS_INT_MAX)) { - dbus_set_error (error, DBUS_ERROR_NO_MEMORY, - "No memory to reload keyring"); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); return FALSE; } if (!_dbus_string_init (&line, _DBUS_INT_MAX)) { - dbus_set_error (error, DBUS_ERROR_NO_MEMORY, - "No memory to reload keyring"); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); _dbus_string_free (&contents); return FALSE; } @@ -508,8 +503,7 @@ _dbus_keyring_reload (DBusKeyring *keyring, new = dbus_realloc (keys, sizeof (DBusKey) * (n_keys + 1)); if (new == NULL) { - dbus_set_error (error, DBUS_ERROR_NO_MEMORY, - "No memory to reallocate secret key list"); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); goto out; } @@ -520,8 +514,7 @@ _dbus_keyring_reload (DBusKeyring *keyring, _DBUS_INT_MAX)) { n_keys -= 1; /* we don't want to free the one we didn't init */ - dbus_set_error (error, DBUS_ERROR_NO_MEMORY, - "No memory to store secret key"); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); goto out; } @@ -531,8 +524,7 @@ _dbus_keyring_reload (DBusKeyring *keyring, &keys[n_keys-1].secret, 0)) { - dbus_set_error (error, DBUS_ERROR_NO_MEMORY, - "No memory to store secret key or invalid hex encoding"); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); goto out; } } @@ -580,19 +572,13 @@ _dbus_keyring_reload (DBusKeyring *keyring, continue; nomem: - dbus_set_error (error, DBUS_ERROR_NO_MEMORY, - "No memory to save secret keyring"); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); goto out; } - result = _dbus_string_save_to_file (&contents, &keyring->filename); - if (result != DBUS_RESULT_SUCCESS) - { - dbus_set_error (error, DBUS_ERROR_FAILED, - "Failed to save keyring file: %s", - dbus_result_to_string (result)); - goto out; - } + if (!_dbus_string_save_to_file (&contents, &keyring->filename, + error)) + goto out; } dbus_free (keyring->keys); @@ -697,6 +683,8 @@ _dbus_keyring_new_homedir (const DBusString *username, dbus_bool_t error_set; DBusString dotdir; DBusError tmp_error; + + _DBUS_ASSERT_ERROR_IS_CLEAR (error); keyring = NULL; error_set = FALSE; @@ -919,6 +907,8 @@ _dbus_keyring_get_best_key (DBusKeyring *keyring, { DBusKey *key; + _DBUS_ASSERT_ERROR_IS_CLEAR (error); + key = find_recent_key (keyring); if (key) return key->id; diff --git a/dbus/dbus-message.c b/dbus/dbus-message.c index 6366c54e..524a7a3f 100644 --- a/dbus/dbus-message.c +++ b/dbus/dbus-message.c @@ -1555,9 +1555,11 @@ dbus_message_get_args (DBusMessage *message, int first_arg_type, ...) { - DBusResultCode retval; + dbus_bool_t retval; va_list var_args; + _DBUS_ASSERT_ERROR_IS_CLEAR (error); + va_start (var_args, first_arg_type); retval = dbus_message_get_args_valist (message, error, first_arg_type, var_args); va_end (var_args); @@ -1593,13 +1595,14 @@ dbus_message_get_args_valist (DBusMessage *message, int spec_type, msg_type, i; DBusMessageIter *iter; dbus_bool_t retval; + + _DBUS_ASSERT_ERROR_IS_CLEAR (error); iter = dbus_message_get_args_iter (message); if (iter == NULL) { - dbus_set_error (error, DBUS_ERROR_NO_MEMORY, - "No memory to get message arguments"); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); return FALSE; } @@ -1675,8 +1678,7 @@ dbus_message_get_args_valist (DBusMessage *message, if (!*ptr) { - dbus_set_error (error, DBUS_ERROR_NO_MEMORY, - "No memory for argument %d", i); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); goto out; } @@ -1693,8 +1695,7 @@ dbus_message_get_args_valist (DBusMessage *message, if (!dbus_message_iter_get_boolean_array (iter, ptr, len)) { - dbus_set_error (error, DBUS_ERROR_NO_MEMORY, - "No memory for argument %d", i); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); goto out; } break; @@ -1710,8 +1711,7 @@ dbus_message_get_args_valist (DBusMessage *message, if (!dbus_message_iter_get_int32_array (iter, ptr, len)) { - dbus_set_error (error, DBUS_ERROR_NO_MEMORY, - "No memory for argument %d", i); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); goto out; } @@ -1728,8 +1728,7 @@ dbus_message_get_args_valist (DBusMessage *message, if (!dbus_message_iter_get_uint32_array (iter, ptr, len)) { - dbus_set_error (error, DBUS_ERROR_NO_MEMORY, - "No memory for argument %d", i); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); goto out; } @@ -1746,8 +1745,7 @@ dbus_message_get_args_valist (DBusMessage *message, if (!dbus_message_iter_get_double_array (iter, ptr, len)) { - dbus_set_error (error, DBUS_ERROR_NO_MEMORY, - "No memory for argument %d", i); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); goto out; } break; @@ -1763,8 +1761,7 @@ dbus_message_get_args_valist (DBusMessage *message, if (!dbus_message_iter_get_byte_array (iter, ptr, len)) { - dbus_set_error (error, DBUS_ERROR_NO_MEMORY, - "No memory for argument %d", i); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); goto out; } break; @@ -1779,8 +1776,7 @@ dbus_message_get_args_valist (DBusMessage *message, if (!dbus_message_iter_get_string_array (iter, ptr, len)) { - dbus_set_error (error, DBUS_ERROR_NO_MEMORY, - "No memory for argument %d", i); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); goto out; } break; @@ -1793,8 +1789,7 @@ dbus_message_get_args_valist (DBusMessage *message, if (!dbus_message_iter_get_dict (iter, dict)) { - dbus_set_error (error, DBUS_ERROR_NO_MEMORY, - "No memory for argument %d", i); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); goto out; } break; diff --git a/dbus/dbus-server-debug-pipe.c b/dbus/dbus-server-debug-pipe.c index c9a25020..261ee609 100644 --- a/dbus/dbus-server-debug-pipe.c +++ b/dbus/dbus-server-debug-pipe.c @@ -133,21 +133,23 @@ static DBusServerVTable debug_vtable = { * Creates a new debug server using an in-process pipe * * @param server_name the name of the server. - * @param result address where a result code can be returned. + * @param error address where an error can be returned. * @returns a new server, or #NULL on failure. */ DBusServer* _dbus_server_debug_pipe_new (const char *server_name, - DBusResultCode *result) + DBusError *error) { DBusServerDebugPipe *debug_server; + _DBUS_ASSERT_ERROR_IS_CLEAR (error); + if (!pipe_hash_ref ()) return NULL; if (_dbus_hash_table_lookup_string (server_pipe_hash, server_name) != NULL) { - dbus_set_result (result, DBUS_RESULT_ADDRESS_IN_USE); + dbus_set_error (error, DBUS_ERROR_ADDRESS_IN_USE, NULL); pipe_hash_unref (); return NULL; } @@ -166,7 +168,7 @@ _dbus_server_debug_pipe_new (const char *server_name, dbus_free (debug_server->name); dbus_free (debug_server); - dbus_set_result (result, DBUS_RESULT_NO_MEMORY); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); pipe_hash_unref (); return NULL; @@ -178,7 +180,7 @@ _dbus_server_debug_pipe_new (const char *server_name, dbus_free (debug_server->name); dbus_free (debug_server); - dbus_set_result (result, DBUS_RESULT_NO_MEMORY); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); pipe_hash_unref (); return NULL; @@ -192,14 +194,12 @@ _dbus_server_debug_pipe_new (const char *server_name, dbus_free (debug_server->name); dbus_free (debug_server); - dbus_set_result (result, DBUS_RESULT_NO_MEMORY); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); pipe_hash_unref (); return NULL; } - dbus_set_result (result, DBUS_RESULT_SUCCESS); - return (DBusServer *)debug_server; } @@ -214,7 +214,7 @@ _dbus_server_debug_pipe_new (const char *server_name, */ DBusTransport* _dbus_transport_debug_pipe_new (const char *server_name, - DBusResultCode *result) + DBusError *error) { DBusTransport *client_transport; DBusTransport *server_transport; @@ -222,12 +222,14 @@ _dbus_transport_debug_pipe_new (const char *server_name, int client_fd, server_fd; DBusServer *server; + _DBUS_ASSERT_ERROR_IS_CLEAR (error); + server = _dbus_hash_table_lookup_string (server_pipe_hash, server_name); if (server == NULL || ((DBusServerDebugPipe*)server)->disconnected) { - dbus_set_result (result, DBUS_RESULT_BAD_ADDRESS); + dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS, NULL); return NULL; } @@ -235,7 +237,7 @@ _dbus_transport_debug_pipe_new (const char *server_name, NULL)) { _dbus_verbose ("failed to create full duplex pipe\n"); - dbus_set_result (result, DBUS_RESULT_FAILED); + dbus_set_error (error, DBUS_ERROR_FAILED, "Could not create full-duplex pipe"); return NULL; } @@ -248,7 +250,7 @@ _dbus_transport_debug_pipe_new (const char *server_name, { _dbus_close (client_fd, NULL); _dbus_close (server_fd, NULL); - dbus_set_result (result, DBUS_RESULT_NO_MEMORY); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); return NULL; } @@ -260,7 +262,7 @@ _dbus_transport_debug_pipe_new (const char *server_name, { _dbus_transport_unref (client_transport); _dbus_close (server_fd, NULL); - dbus_set_result (result, DBUS_RESULT_NO_MEMORY); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); return NULL; } @@ -273,7 +275,7 @@ _dbus_transport_debug_pipe_new (const char *server_name, if (connection == NULL) { _dbus_transport_unref (client_transport); - dbus_set_result (result, DBUS_RESULT_NO_MEMORY); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); return NULL; } diff --git a/dbus/dbus-server-debug-pipe.h b/dbus/dbus-server-debug-pipe.h index 7de6957d..76cb5d60 100644 --- a/dbus/dbus-server-debug-pipe.h +++ b/dbus/dbus-server-debug-pipe.h @@ -30,10 +30,10 @@ DBUS_BEGIN_DECLS; -DBusServer* _dbus_server_debug_pipe_new (const char *server_name, - DBusResultCode *result); -DBusTransport* _dbus_transport_debug_pipe_new (const char *server_name, - DBusResultCode *result); +DBusServer* _dbus_server_debug_pipe_new (const char *server_name, + DBusError *error); +DBusTransport* _dbus_transport_debug_pipe_new (const char *server_name, + DBusError *error); DBUS_END_DECLS; diff --git a/dbus/dbus-server-debug.c b/dbus/dbus-server-debug.c index 3b7e78dd..7af2336b 100644 --- a/dbus/dbus-server-debug.c +++ b/dbus/dbus-server-debug.c @@ -109,29 +109,32 @@ _dbus_server_debug_lookup (const char *server_name) * Creates a new debug server. * * @param server_name the name of the server. - * @param result address where a result code can be returned. + * @param error address where an error can be returned. * @returns a new server, or #NULL on failure. */ DBusServer* _dbus_server_debug_new (const char *server_name, - DBusResultCode *result) + DBusError *error) { DBusServerDebug *debug_server; + _DBUS_ASSERT_ERROR_IS_CLEAR (error); + if (!server_hash) { server_hash = _dbus_hash_table_new (DBUS_HASH_STRING, NULL, NULL); if (!server_hash) { - dbus_set_result (result, DBUS_RESULT_NO_MEMORY); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); return NULL; } } if (_dbus_hash_table_lookup_string (server_hash, server_name) != NULL) { - dbus_set_result (result, DBUS_RESULT_ADDRESS_IN_USE); + dbus_set_error (error, DBUS_ERROR_ADDRESS_IN_USE, + NULL); return NULL; } @@ -146,7 +149,7 @@ _dbus_server_debug_new (const char *server_name, dbus_free (debug_server->name); dbus_free (debug_server); - dbus_set_result (result, DBUS_RESULT_NO_MEMORY); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); } if (!_dbus_server_init_base (&debug_server->base, @@ -155,7 +158,7 @@ _dbus_server_debug_new (const char *server_name, dbus_free (debug_server->name); dbus_free (debug_server); - dbus_set_result (result, DBUS_RESULT_NO_MEMORY); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); return NULL; } @@ -168,13 +171,11 @@ _dbus_server_debug_new (const char *server_name, dbus_free (debug_server->name); dbus_free (debug_server); - dbus_set_result (result, DBUS_RESULT_NO_MEMORY); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); return NULL; } - dbus_set_result (result, DBUS_RESULT_SUCCESS); - return (DBusServer *)debug_server; } diff --git a/dbus/dbus-server-debug.h b/dbus/dbus-server-debug.h index 6fff20c0..788267df 100644 --- a/dbus/dbus-server-debug.h +++ b/dbus/dbus-server-debug.h @@ -30,7 +30,7 @@ DBUS_BEGIN_DECLS; DBusServer* _dbus_server_debug_new (const char *server_name, - DBusResultCode *result); + DBusError *error); DBusServer* _dbus_server_debug_lookup (const char *server_name); dbus_bool_t _dbus_server_debug_accept_transport (DBusServer *server, DBusTransport *transport); diff --git a/dbus/dbus-server-unix.c b/dbus/dbus-server-unix.c index 88cc9ed7..41553fe2 100644 --- a/dbus/dbus-server-unix.c +++ b/dbus/dbus-server-unix.c @@ -250,17 +250,19 @@ _dbus_server_new_for_fd (int fd) * Creates a new server listening on the given Unix domain socket. * * @param path the path for the domain socket. - * @param result location to store reason for failure. + * @param error location to store reason for failure. * @returns the new server, or #NULL on failure. */ DBusServer* _dbus_server_new_for_domain_socket (const char *path, - DBusResultCode *result) + DBusError *error) { DBusServer *server; int listen_fd; - listen_fd = _dbus_listen_unix_socket (path, result); + _DBUS_ASSERT_ERROR_IS_CLEAR (error); + + listen_fd = _dbus_listen_unix_socket (path, error); _dbus_fd_set_close_on_exec (listen_fd); if (listen_fd < 0) @@ -269,7 +271,7 @@ _dbus_server_new_for_domain_socket (const char *path, server = _dbus_server_new_for_fd (listen_fd); if (server == NULL) { - dbus_set_result (result, DBUS_RESULT_NO_MEMORY); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); close (listen_fd); return NULL; } @@ -278,22 +280,25 @@ _dbus_server_new_for_domain_socket (const char *path, } /** - * Creates a new server listening on the given hostname and port + * Creates a new server listening on the given hostname and port. + * If the hostname is NULL, listens on localhost. * * @param host the hostname to listen on. * @param port the port to listen on. - * @param result location to store reason for failure. + * @param error location to store reason for failure. * @returns the new server, or #NULL on failure. */ DBusServer* _dbus_server_new_for_tcp_socket (const char *host, dbus_uint32_t port, - DBusResultCode *result) + DBusError *error) { DBusServer *server; int listen_fd; + + _DBUS_ASSERT_ERROR_IS_CLEAR (error); - listen_fd = _dbus_listen_tcp_socket (host, port, result); + listen_fd = _dbus_listen_tcp_socket (host, port, error); _dbus_fd_set_close_on_exec (listen_fd); if (listen_fd < 0) @@ -302,7 +307,7 @@ _dbus_server_new_for_tcp_socket (const char *host, server = _dbus_server_new_for_fd (listen_fd); if (server == NULL) { - dbus_set_result (result, DBUS_RESULT_NO_MEMORY); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); close (listen_fd); return NULL; } diff --git a/dbus/dbus-server-unix.h b/dbus/dbus-server-unix.h index ebd017d0..947425b1 100644 --- a/dbus/dbus-server-unix.h +++ b/dbus/dbus-server-unix.h @@ -30,10 +30,10 @@ DBUS_BEGIN_DECLS; DBusServer* _dbus_server_new_for_fd (int fd); DBusServer* _dbus_server_new_for_domain_socket (const char *path, - DBusResultCode *result); + DBusError *error); DBusServer* _dbus_server_new_for_tcp_socket (const char *host, dbus_uint32_t port, - DBusResultCode *result); + DBusError *error); DBUS_END_DECLS; diff --git a/dbus/dbus-server.c b/dbus/dbus-server.c index e717096e..6b9f17e8 100644 --- a/dbus/dbus-server.c +++ b/dbus/dbus-server.c @@ -246,22 +246,30 @@ _dbus_server_toggle_timeout (DBusServer *server, * DBusResultCode is a bit limiting here. * * @param address the address of this server. - * @param result location to store rationale for failure. + * @param error location to store rationale for failure. * @returns a new DBusServer, or #NULL on failure. * */ DBusServer* dbus_server_listen (const char *address, - DBusResultCode *result) + DBusError *error) { DBusServer *server; DBusAddressEntry **entries; int len, i; + const char *address_problem_type; + const char *address_problem_field; + const char *address_problem_other; + + _DBUS_ASSERT_ERROR_IS_CLEAR (error); - if (!dbus_parse_address (address, &entries, &len, result)) + if (!dbus_parse_address (address, &entries, &len, error)) return NULL; server = NULL; + address_problem_type = NULL; + address_problem_field = NULL; + address_problem_other = NULL; for (i = 0; i < len; i++) { @@ -272,9 +280,13 @@ dbus_server_listen (const char *address, const char *path = dbus_address_entry_get_value (entries[i], "path"); if (path == NULL) - goto bad_address; + { + address_problem_type = "unix"; + address_problem_field = "path"; + goto bad_address; + } - server = _dbus_server_new_for_domain_socket (path, result); + server = _dbus_server_new_for_domain_socket (path, error); if (server) break; @@ -288,16 +300,23 @@ dbus_server_listen (const char *address, dbus_bool_t sresult; if (port == NULL) - goto bad_address; + { + address_problem_type = "tcp"; + address_problem_field = "port"; + goto bad_address; + } _dbus_string_init_const (&str, port); sresult = _dbus_string_parse_int (&str, 0, &lport, NULL); _dbus_string_free (&str); if (sresult == FALSE || lport <= 0 || lport > 65535) - goto bad_address; + { + address_problem_other = "Port is not an integer between 0 and 65535"; + goto bad_address; + } - server = _dbus_server_new_for_tcp_socket (host, lport, result); + server = _dbus_server_new_for_tcp_socket (host, lport, error); if (server) break; @@ -308,9 +327,13 @@ dbus_server_listen (const char *address, const char *name = dbus_address_entry_get_value (entries[i], "name"); if (name == NULL) - goto bad_address; + { + address_problem_type = "debug"; + address_problem_field = "name"; + goto bad_address; + } - server = _dbus_server_debug_new (name, result); + server = _dbus_server_debug_new (name, error); if (server) break; @@ -320,16 +343,23 @@ dbus_server_listen (const char *address, const char *name = dbus_address_entry_get_value (entries[i], "name"); if (name == NULL) - goto bad_address; + { + address_problem_type = "debug-pipe"; + address_problem_field = "name"; + goto bad_address; + } - server = _dbus_server_debug_pipe_new (name, result); + server = _dbus_server_debug_pipe_new (name, error); if (server) break; } #endif else - goto bad_address; + { + address_problem_other = "Unknown address type (examples of valid types are \"unix\" and \"tcp\")"; + goto bad_address; + } } dbus_address_entries_free (entries); @@ -337,7 +367,14 @@ dbus_server_listen (const char *address, bad_address: dbus_address_entries_free (entries); - dbus_set_result (result, DBUS_RESULT_BAD_ADDRESS); + if (address_problem_type != NULL) + dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS, + "Server address of type %s was missing argument %s", + address_problem_type, address_problem_field); + else + dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS, + "Could not parse server address: %s", + address_problem_other); return NULL; } diff --git a/dbus/dbus-server.h b/dbus/dbus-server.h index 75632440..fcfc6e70 100644 --- a/dbus/dbus-server.h +++ b/dbus/dbus-server.h @@ -40,7 +40,7 @@ typedef void (* DBusNewConnectionFunction) (DBusServer *server, void *data); DBusServer* dbus_server_listen (const char *address, - DBusResultCode *result); + DBusError *error); void dbus_server_ref (DBusServer *server); void dbus_server_unref (DBusServer *server); void dbus_server_disconnect (DBusServer *server); diff --git a/dbus/dbus-sysdeps.c b/dbus/dbus-sysdeps.c index 948b0836..c23d8481 100644 --- a/dbus/dbus-sysdeps.c +++ b/dbus/dbus-sysdeps.c @@ -319,25 +319,26 @@ _dbus_write_two (int fd, * nonblocking. * * @param path the path to UNIX domain socket - * @param result return location for error code + * @param error return location for error code * @returns connection file descriptor or -1 on error */ int _dbus_connect_unix_socket (const char *path, - DBusResultCode *result) + DBusError *error) { int fd; struct sockaddr_un addr; + + _DBUS_ASSERT_ERROR_IS_CLEAR (error); fd = socket (PF_UNIX, SOCK_STREAM, 0); if (fd < 0) { - dbus_set_result (result, - _dbus_result_from_errno (errno)); - - _dbus_verbose ("Failed to create socket: %s\n", - _dbus_strerror (errno)); + dbus_set_error (error, + _dbus_error_from_errno (errno), + "Failed to create socket: %s", + _dbus_strerror (errno)); return -1; } @@ -349,11 +350,10 @@ _dbus_connect_unix_socket (const char *path, if (connect (fd, (struct sockaddr*) &addr, sizeof (addr)) < 0) { - dbus_set_result (result, - _dbus_result_from_errno (errno)); - - _dbus_verbose ("Failed to connect to socket %s: %s\n", - path, _dbus_strerror (errno)); + dbus_set_error (error, + _dbus_error_from_errno (errno), + "Failed to connect to socket %s: %s", + path, _dbus_strerror (errno)); close (fd); fd = -1; @@ -361,8 +361,10 @@ _dbus_connect_unix_socket (const char *path, return -1; } - if (!_dbus_set_fd_nonblocking (fd, result)) + if (!_dbus_set_fd_nonblocking (fd, error)) { + _DBUS_ASSERT_ERROR_IS_SET (error); + close (fd); fd = -1; @@ -378,23 +380,25 @@ _dbus_connect_unix_socket (const char *path, * set to be nonblocking. * * @param path the socket name - * @param result return location for errors + * @param error return location for errors * @returns the listening file descriptor or -1 on error */ int _dbus_listen_unix_socket (const char *path, - DBusResultCode *result) + DBusError *error) { int listen_fd; struct sockaddr_un addr; + _DBUS_ASSERT_ERROR_IS_CLEAR (error); + listen_fd = socket (PF_UNIX, SOCK_STREAM, 0); if (listen_fd < 0) { - dbus_set_result (result, _dbus_result_from_errno (errno)); - _dbus_verbose ("Failed to create socket \"%s\": %s\n", - path, _dbus_strerror (errno)); + dbus_set_error (error, _dbus_error_from_errno (errno), + "Failed to create socket \"%s\": %s", + path, _dbus_strerror (errno)); return -1; } @@ -405,24 +409,25 @@ _dbus_listen_unix_socket (const char *path, if (bind (listen_fd, (struct sockaddr*) &addr, SUN_LEN (&addr)) < 0) { - dbus_set_result (result, _dbus_result_from_errno (errno)); - _dbus_verbose ("Failed to bind socket \"%s\": %s\n", - path, _dbus_strerror (errno)); + dbus_set_error (error, _dbus_error_from_errno (errno), + "Failed to bind socket \"%s\": %s", + path, _dbus_strerror (errno)); close (listen_fd); return -1; } if (listen (listen_fd, 30 /* backlog */) < 0) { - dbus_set_result (result, _dbus_result_from_errno (errno)); - _dbus_verbose ("Failed to listen on socket \"%s\": %s\n", - path, _dbus_strerror (errno)); + dbus_set_error (error, _dbus_error_from_errno (errno), + "Failed to listen on socket \"%s\": %s", + path, _dbus_strerror (errno)); close (listen_fd); return -1; } - if (!_dbus_set_fd_nonblocking (listen_fd, result)) + if (!_dbus_set_fd_nonblocking (listen_fd, error)) { + _DBUS_ASSERT_ERROR_IS_SET (error); close (listen_fd); return -1; } @@ -437,28 +442,29 @@ _dbus_listen_unix_socket (const char *path, * * @param host the host name to connect to * @param port the prot to connect to - * @param result return location for error code + * @param error return location for error code * @returns connection file descriptor or -1 on error */ int _dbus_connect_tcp_socket (const char *host, dbus_uint32_t port, - DBusResultCode *result) + DBusError *error) { int fd; struct sockaddr_in addr; struct hostent *he; struct in_addr *haddr; + + _DBUS_ASSERT_ERROR_IS_CLEAR (error); fd = socket (AF_INET, SOCK_STREAM, 0); if (fd < 0) { - dbus_set_result (result, - _dbus_result_from_errno (errno)); - - _dbus_verbose ("Failed to create socket: %s\n", - _dbus_strerror (errno)); + dbus_set_error (error, + _dbus_error_from_errno (errno), + "Failed to create socket: %s", + _dbus_strerror (errno)); return -1; } @@ -469,10 +475,10 @@ _dbus_connect_tcp_socket (const char *host, he = gethostbyname (host); if (he == NULL) { - dbus_set_result (result, - _dbus_result_from_errno (errno)); - _dbus_verbose ("Failed to lookup hostname: %s\n", - host); + dbus_set_error (error, + _dbus_error_from_errno (errno), + "Failed to lookup hostname: %s", + host); return -1; } @@ -485,11 +491,10 @@ _dbus_connect_tcp_socket (const char *host, if (connect (fd, (struct sockaddr*) &addr, sizeof (addr)) < 0) { - dbus_set_result (result, - _dbus_result_from_errno (errno)); - - _dbus_verbose ("Failed to connect to socket %s: %s:%d\n", - host, _dbus_strerror (errno), port); + dbus_set_error (error, + _dbus_error_from_errno (errno), + "Failed to connect to socket %s: %s:%d", + host, _dbus_strerror (errno), port); close (fd); fd = -1; @@ -497,7 +502,7 @@ _dbus_connect_tcp_socket (const char *host, return -1; } - if (!_dbus_set_fd_nonblocking (fd, result)) + if (!_dbus_set_fd_nonblocking (fd, error)) { close (fd); fd = -1; @@ -515,26 +520,28 @@ _dbus_connect_tcp_socket (const char *host, * * @param host the host name to listen on * @param port the prot to listen on - * @param result return location for errors + * @param error return location for errors * @returns the listening file descriptor or -1 on error */ int _dbus_listen_tcp_socket (const char *host, dbus_uint32_t port, - DBusResultCode *result) + DBusError *error) { int listen_fd; struct sockaddr_in addr; struct hostent *he; struct in_addr *haddr; + + _DBUS_ASSERT_ERROR_IS_CLEAR (error); listen_fd = socket (AF_INET, SOCK_STREAM, 0); if (listen_fd < 0) { - dbus_set_result (result, _dbus_result_from_errno (errno)); - _dbus_verbose ("Failed to create socket \"%s:%d\": %s\n", - host, port, _dbus_strerror (errno)); + dbus_set_error (error, _dbus_error_from_errno (errno), + "Failed to create socket \"%s:%d\": %s", + host, port, _dbus_strerror (errno)); return -1; } @@ -544,10 +551,10 @@ _dbus_listen_tcp_socket (const char *host, he = gethostbyname (host); if (he == NULL) { - dbus_set_result (result, - _dbus_result_from_errno (errno)); - _dbus_verbose ("Failed to lookup hostname: %s\n", - host); + dbus_set_error (error, + _dbus_error_from_errno (errno), + "Failed to lookup hostname: %s", + host); return -1; } @@ -560,23 +567,23 @@ _dbus_listen_tcp_socket (const char *host, if (bind (listen_fd, (struct sockaddr*) &addr, sizeof (struct sockaddr))) { - dbus_set_result (result, _dbus_result_from_errno (errno)); - _dbus_verbose ("Failed to bind socket \"%s:%d\": %s\n", - host, port, _dbus_strerror (errno)); + dbus_set_error (error, _dbus_error_from_errno (errno), + "Failed to bind socket \"%s:%d\": %s", + host, port, _dbus_strerror (errno)); close (listen_fd); return -1; } if (listen (listen_fd, 30 /* backlog */) < 0) { - dbus_set_result (result, _dbus_result_from_errno (errno)); - _dbus_verbose ("Failed to listen on socket \"%s:%d\": %s\n", - host, port, _dbus_strerror (errno)); + dbus_set_error (error, _dbus_error_from_errno (errno), + "Failed to listen on socket \"%s:%d\": %s", + host, port, _dbus_strerror (errno)); close (listen_fd); return -1; } - if (!_dbus_set_fd_nonblocking (listen_fd, result)) + if (!_dbus_set_fd_nonblocking (listen_fd, error)) { close (listen_fd); return -1; @@ -587,10 +594,12 @@ _dbus_listen_tcp_socket (const char *host, static dbus_bool_t write_credentials_byte (int server_fd, - DBusResultCode *result) + DBusError *error) { int bytes_written; char buf[1] = { '\0' }; + + _DBUS_ASSERT_ERROR_IS_CLEAR (error); again: @@ -601,15 +610,15 @@ write_credentials_byte (int server_fd, if (bytes_written < 0) { - dbus_set_result (result, _dbus_result_from_errno (errno)); - _dbus_verbose ("Failed to write credentials byte: %s\n", + dbus_set_error (error, _dbus_error_from_errno (errno), + "Failed to write credentials byte: %s", _dbus_strerror (errno)); return FALSE; } else if (bytes_written == 0) { - dbus_set_result (result, DBUS_RESULT_IO_ERROR); - _dbus_verbose ("wrote zero bytes writing credentials byte\n"); + dbus_set_error (error, DBUS_ERROR_IO_ERROR, + "wrote zero bytes writing credentials byte"); return FALSE; } else @@ -635,13 +644,13 @@ write_credentials_byte (int server_fd, * * @param client_fd the client file descriptor * @param credentials struct to fill with credentials of client - * @param result location to store result code + * @param error location to store error code * @returns #TRUE on success */ dbus_bool_t _dbus_read_credentials_unix_socket (int client_fd, DBusCredentials *credentials, - DBusResultCode *result) + DBusError *error) { struct msghdr msg; struct iovec iov; @@ -652,6 +661,8 @@ _dbus_read_credentials_unix_socket (int client_fd, struct cmsghdr *cmsg = (struct cmsghdr *) cmsgmem; #endif + _DBUS_ASSERT_ERROR_IS_CLEAR (error); + /* The POSIX spec certainly doesn't promise this, but * we need these assertions to fail as soon as we're wrong about * it so we can do the porting fixups @@ -695,23 +706,23 @@ _dbus_read_credentials_unix_socket (int client_fd, if (errno == EINTR) goto again; - dbus_set_result (result, _dbus_result_from_errno (errno)); - _dbus_verbose ("Failed to read credentials byte: %s\n", - _dbus_strerror (errno)); + dbus_set_error (error, _dbus_error_from_errno (errno), + "Failed to read credentials byte: %s", + _dbus_strerror (errno)); return FALSE; } if (buf != '\0') { - dbus_set_result (result, DBUS_RESULT_FAILED); - _dbus_verbose ("Credentials byte was not nul\n"); + dbus_set_error (error, DBUS_ERROR_FAILED, + "Credentials byte was not nul"); return FALSE; } #ifdef HAVE_CMSGCRED if (cmsg->cmsg_len < sizeof (cmsgmem) || cmsg->cmsg_type != SCM_CREDS) { - dbus_set_result (result, DBUS_RESULT_FAILED); + dbus_set_error (error, DBUS_ERROR_FAILED); _dbus_verbose ("Message from recvmsg() was not SCM_CREDS\n"); return FALSE; } @@ -771,14 +782,16 @@ _dbus_read_credentials_unix_socket (int client_fd, * use sendmsg()/recvmsg() to transmit credentials. * * @param server_fd file descriptor for connection to server - * @param result return location for error code + * @param error return location for error code * @returns #TRUE if the byte was sent */ dbus_bool_t _dbus_send_credentials_unix_socket (int server_fd, - DBusResultCode *result) + DBusError *error) { - if (write_credentials_byte (server_fd, result)) + _DBUS_ASSERT_ERROR_IS_CLEAR (error); + + if (write_credentials_byte (server_fd, error)) return TRUE; else return FALSE; @@ -1805,7 +1818,7 @@ _dbus_get_current_time (long *tv_sec, /** * Appends the contents of the given file to the string, - * returning result code. At the moment, won't open a file + * returning error code. At the moment, won't open a file * more than a megabyte in size. * * @param str the string to append to @@ -1824,6 +1837,8 @@ _dbus_file_get_contents (DBusString *str, int total; const char *filename_c; + _DBUS_ASSERT_ERROR_IS_CLEAR (error); + _dbus_string_get_const_data (filename, &filename_c); /* O_BINARY useful on Cygwin */ @@ -1937,11 +1952,13 @@ append_unique_chars (DBusString *str) * * @param str the string to write out * @param filename the file to save string to - * @returns result code + * @param error error to be filled in on failure + * @returns #FALSE on failure */ -DBusResultCode +dbus_bool_t _dbus_string_save_to_file (const DBusString *str, - const DBusString *filename) + const DBusString *filename, + DBusError *error) { int fd; int bytes_to_write; @@ -1949,24 +1966,38 @@ _dbus_string_save_to_file (const DBusString *str, DBusString tmp_filename; const char *tmp_filename_c; int total; - DBusResultCode result; dbus_bool_t need_unlink; + dbus_bool_t retval; + + _DBUS_ASSERT_ERROR_IS_CLEAR (error); fd = -1; - result = DBUS_RESULT_FAILED; + retval = FALSE; need_unlink = FALSE; if (!_dbus_string_init (&tmp_filename, _DBUS_INT_MAX)) - return DBUS_RESULT_NO_MEMORY; + { + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); + return FALSE; + } if (!_dbus_string_copy (filename, 0, &tmp_filename, 0)) - return DBUS_RESULT_NO_MEMORY; + { + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); + return FALSE; + } if (!_dbus_string_append (&tmp_filename, ".")) - return DBUS_RESULT_NO_MEMORY; + { + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); + return FALSE; + } if (!append_unique_chars (&tmp_filename)) - return DBUS_RESULT_NO_MEMORY; + { + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); + return FALSE; + } _dbus_string_get_const_data (filename, &filename_c); _dbus_string_get_const_data (&tmp_filename, &tmp_filename_c); @@ -1975,7 +2006,9 @@ _dbus_string_save_to_file (const DBusString *str, 0600); if (fd < 0) { - result = _dbus_result_from_errno (errno); + dbus_set_error (error, _dbus_error_from_errno (errno), + "Could not create %s: %s", tmp_filename_c, + _dbus_strerror (errno)); goto out; } @@ -1993,12 +2026,9 @@ _dbus_string_save_to_file (const DBusString *str, if (bytes_written <= 0) { - DBusResultCode result; - - result = _dbus_result_from_errno (errno); /* prior to close() */ - - _dbus_verbose ("write() failed: %s", - _dbus_strerror (errno)); + dbus_set_error (error, _dbus_error_from_errno (errno), + "Could not write to %s: %s", tmp_filename_c, + _dbus_strerror (errno)); goto out; } @@ -2008,7 +2038,10 @@ _dbus_string_save_to_file (const DBusString *str, if (close (fd) < 0) { - _dbus_verbose ("close() failed: %s\n", _dbus_strerror (errno)); + dbus_set_error (error, _dbus_error_from_errno (errno), + "Could not close file %s: %s", + tmp_filename_c, _dbus_strerror (errno)); + goto out; } @@ -2016,13 +2049,17 @@ _dbus_string_save_to_file (const DBusString *str, if (rename (tmp_filename_c, filename_c) < 0) { - _dbus_verbose ("rename() failed: %s\n", _dbus_strerror (errno)); + dbus_set_error (error, _dbus_error_from_errno (errno), + "Could not rename %s to %s: %s", + tmp_filename_c, filename_c, + _dbus_strerror (errno)); + goto out; } need_unlink = FALSE; - result = DBUS_RESULT_SUCCESS; + retval = TRUE; out: /* close first, then unlink, to prevent ".nfs34234235" garbage @@ -2037,8 +2074,11 @@ _dbus_string_save_to_file (const DBusString *str, tmp_filename_c, _dbus_strerror (errno)); _dbus_string_free (&tmp_filename); + + if (!retval) + _DBUS_ASSERT_ERROR_IS_SET (error); - return result; + return retval; } /** Creates the given file, failing if the file already exists. @@ -2054,6 +2094,8 @@ _dbus_create_file_exclusively (const DBusString *filename, int fd; const char *filename_c; + _DBUS_ASSERT_ERROR_IS_CLEAR (error); + _dbus_string_get_const_data (filename, &filename_c); fd = open (filename_c, O_WRONLY | O_BINARY | O_EXCL | O_CREAT, @@ -2095,6 +2137,8 @@ _dbus_delete_file (const DBusString *filename, { const char *filename_c; + _DBUS_ASSERT_ERROR_IS_CLEAR (error); + _dbus_string_get_const_data (filename, &filename_c); if (unlink (filename_c) < 0) @@ -2122,6 +2166,8 @@ _dbus_create_directory (const DBusString *filename, { const char *filename_c; + _DBUS_ASSERT_ERROR_IS_CLEAR (error); + _dbus_string_get_const_data (filename, &filename_c); if (mkdir (filename_c, 0700) < 0) @@ -2196,6 +2242,8 @@ _dbus_directory_open (const DBusString *filename, DBusDirIter *iter; const char *filename_c; + _DBUS_ASSERT_ERROR_IS_CLEAR (error); + _dbus_string_get_const_data (filename, &filename_c); d = opendir (filename_c); @@ -2237,12 +2285,10 @@ _dbus_directory_get_next_file (DBusDirIter *iter, DBusString *filename, DBusError *error) { - /* we always have to put something in result, since return - * value means whether there's a filename and doesn't - * reliably indicate whether an error was set. - */ struct dirent *ent; + _DBUS_ASSERT_ERROR_IS_CLEAR (error); + again: errno = 0; ent = readdir (iter->d); @@ -2415,6 +2461,8 @@ static dbus_bool_t make_pipe (int p[2], DBusError *error) { + _DBUS_ASSERT_ERROR_IS_CLEAR (error); + if (pipe (p) < 0) { dbus_set_error (error, @@ -2458,6 +2506,8 @@ read_ints (int fd, DBusError *error) { size_t bytes = 0; + + _DBUS_ASSERT_ERROR_IS_CLEAR (error); while (TRUE) { @@ -2556,6 +2606,8 @@ _dbus_spawn_async (char **argv, int pid = -1, grandchild_pid; int child_err_report_pipe[2] = { -1, -1 }; int status; + + _DBUS_ASSERT_ERROR_IS_CLEAR (error); if (!make_pipe (child_err_report_pipe, error)) return FALSE; @@ -2715,105 +2767,6 @@ _dbus_fd_set_close_on_exec (int fd) fcntl (fd, F_SETFD, val); } - -/** - * Converts a UNIX errno into a DBusResultCode. - * - * @todo should cover more errnos, specifically those - * from open(). - * - * @param error_number the errno. - * @returns the result code. - */ -DBusResultCode -_dbus_result_from_errno (int error_number) -{ - switch (error_number) - { - case 0: - return DBUS_RESULT_SUCCESS; - -#ifdef EPROTONOSUPPORT - case EPROTONOSUPPORT: - return DBUS_RESULT_NOT_SUPPORTED; -#endif -#ifdef EAFNOSUPPORT - case EAFNOSUPPORT: - return DBUS_RESULT_NOT_SUPPORTED; -#endif -#ifdef ENFILE - case ENFILE: - return DBUS_RESULT_LIMITS_EXCEEDED; /* kernel out of memory */ -#endif -#ifdef EMFILE - case EMFILE: - return DBUS_RESULT_LIMITS_EXCEEDED; -#endif -#ifdef EACCES - case EACCES: - return DBUS_RESULT_ACCESS_DENIED; -#endif -#ifdef EPERM - case EPERM: - return DBUS_RESULT_ACCESS_DENIED; -#endif -#ifdef ENOBUFS - case ENOBUFS: - return DBUS_RESULT_NO_MEMORY; -#endif -#ifdef ENOMEM - case ENOMEM: - return DBUS_RESULT_NO_MEMORY; -#endif -#ifdef EINVAL - case EINVAL: - return DBUS_RESULT_FAILED; -#endif -#ifdef EBADF - case EBADF: - return DBUS_RESULT_FAILED; -#endif -#ifdef EFAULT - case EFAULT: - return DBUS_RESULT_FAILED; -#endif -#ifdef ENOTSOCK - case ENOTSOCK: - return DBUS_RESULT_FAILED; -#endif -#ifdef EISCONN - case EISCONN: - return DBUS_RESULT_FAILED; -#endif -#ifdef ECONNREFUSED - case ECONNREFUSED: - return DBUS_RESULT_NO_SERVER; -#endif -#ifdef ETIMEDOUT - case ETIMEDOUT: - return DBUS_RESULT_TIMEOUT; -#endif -#ifdef ENETUNREACH - case ENETUNREACH: - return DBUS_RESULT_NO_NETWORK; -#endif -#ifdef EADDRINUSE - case EADDRINUSE: - return DBUS_RESULT_ADDRESS_IN_USE; -#endif -#ifdef EEXIST - case EEXIST: - return DBUS_RESULT_FILE_NOT_FOUND; -#endif -#ifdef ENOENT - case ENOENT: - return DBUS_RESULT_FILE_NOT_FOUND; -#endif - } - - return DBUS_RESULT_FAILED; -} - /** * Converts a UNIX errno into a #DBusError name. * @@ -2938,6 +2891,8 @@ _dbus_stat (const DBusString *filename, { const char *filename_c; struct stat sb; + + _DBUS_ASSERT_ERROR_IS_CLEAR (error); _dbus_string_get_const_data (filename, &filename_c); @@ -2977,6 +2932,8 @@ _dbus_full_duplex_pipe (int *fd1, #ifdef HAVE_SOCKETPAIR int fds[2]; + _DBUS_ASSERT_ERROR_IS_CLEAR (error); + if (socketpair (AF_UNIX, SOCK_STREAM, 0, fds) < 0) { dbus_set_error (error, _dbus_error_from_errno (errno), @@ -3019,6 +2976,8 @@ dbus_bool_t _dbus_close (int fd, DBusError *error) { + _DBUS_ASSERT_ERROR_IS_CLEAR (error); + again: if (close (fd) < 0) { @@ -3034,6 +2993,46 @@ _dbus_close (int fd, } /** + * Sets a file descriptor to be nonblocking. + * + * @param fd the file descriptor. + * @param error address of error location. + * @returns #TRUE on success. + */ +dbus_bool_t +_dbus_set_fd_nonblocking (int fd, + DBusError *error) +{ + int val; + + _DBUS_ASSERT_ERROR_IS_CLEAR (error); + + val = fcntl (fd, F_GETFL, 0); + if (val < 0) + { + dbus_set_error (error, _dbus_error_from_errno (errno), + "Failed to get flags from file descriptor %d: %s", + fd, _dbus_strerror (errno)); + _dbus_verbose ("Failed to get flags for fd %d: %s\n", fd, + _dbus_strerror (errno)); + return FALSE; + } + + if (fcntl (fd, F_SETFL, val | O_NONBLOCK) < 0) + { + dbus_set_error (error, _dbus_error_from_errno (errno), + "Failed to set nonblocking flag of file descriptor %d: %s", + fd, _dbus_strerror (errno)); + _dbus_verbose ("Failed to set fd %d nonblocking: %s\n", + fd, _dbus_strerror (errno)); + + return FALSE; + } + + return TRUE; +} + +/** * On GNU libc systems, print a crude backtrace to the verbose log. * On other systems, print "no backtrace support" * diff --git a/dbus/dbus-sysdeps.h b/dbus/dbus-sysdeps.h index 672d23a8..ac4e828a 100644 --- a/dbus/dbus-sysdeps.h +++ b/dbus/dbus-sysdeps.h @@ -76,22 +76,22 @@ typedef struct } DBusCredentials; int _dbus_connect_unix_socket (const char *path, - DBusResultCode *result); + DBusError *error); int _dbus_listen_unix_socket (const char *path, - DBusResultCode *result); + DBusError *error); int _dbus_connect_tcp_socket (const char *host, dbus_uint32_t port, - DBusResultCode *result); + DBusError *error); int _dbus_listen_tcp_socket (const char *host, dbus_uint32_t port, - DBusResultCode *result); + DBusError *error); int _dbus_accept (int listen_fd); dbus_bool_t _dbus_read_credentials_unix_socket (int client_fd, DBusCredentials *credentials, - DBusResultCode *result); + DBusError *error); dbus_bool_t _dbus_send_credentials_unix_socket (int server_fd, - DBusResultCode *result); + DBusError *error); dbus_bool_t _dbus_credentials_from_username (const DBusString *username, @@ -146,11 +146,13 @@ void _dbus_sleep_milliseconds (int milliseconds); void _dbus_get_current_time (long *tv_sec, long *tv_usec); + dbus_bool_t _dbus_file_get_contents (DBusString *str, const DBusString *filename, DBusError *error); -DBusResultCode _dbus_string_save_to_file (const DBusString *str, - const DBusString *filename); +dbus_bool_t _dbus_string_save_to_file (const DBusString *str, + const DBusString *filename, + DBusError *error); dbus_bool_t _dbus_create_file_exclusively (const DBusString *filename, DBusError *error); diff --git a/dbus/dbus-transport-debug.c b/dbus/dbus-transport-debug.c index d477339a..03b40593 100644 --- a/dbus/dbus-transport-debug.c +++ b/dbus/dbus-transport-debug.c @@ -307,28 +307,30 @@ _dbus_transport_debug_server_new (DBusTransport *client) * * @param server_name name of the server transport that * the client should try to connect to. - * @param result address where a result code can be returned. + * @param error address where an error can be returned. * @returns a new transport, or #NULL on failure. */ DBusTransport* _dbus_transport_debug_client_new (const char *server_name, - DBusResultCode *result) + DBusError *error) { DBusServer *debug_server; DBusTransportDebug *debug_transport; + + _DBUS_ASSERT_ERROR_IS_CLEAR (error); debug_server = _dbus_server_debug_lookup (server_name); if (!debug_server) { - dbus_set_result (result, DBUS_RESULT_NO_SERVER); + dbus_set_error (error, DBUS_ERROR_NO_SERVER, NULL); return NULL; } debug_transport = dbus_new0 (DBusTransportDebug, 1); if (debug_transport == NULL) { - dbus_set_result (result, DBUS_RESULT_NO_MEMORY); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); return NULL; } @@ -337,7 +339,7 @@ _dbus_transport_debug_client_new (const char *server_name, FALSE)) { dbus_free (debug_transport); - dbus_set_result (result, DBUS_RESULT_NO_MEMORY); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); return NULL; } @@ -345,7 +347,7 @@ _dbus_transport_debug_client_new (const char *server_name, { _dbus_transport_finalize_base (&debug_transport->base); dbus_free (debug_transport); - dbus_set_result (result, DBUS_RESULT_NO_MEMORY); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); return NULL; } @@ -355,7 +357,7 @@ _dbus_transport_debug_client_new (const char *server_name, _dbus_timeout_unref (debug_transport->timeout); _dbus_transport_finalize_base (&debug_transport->base); dbus_free (debug_transport); - dbus_set_result (result, DBUS_RESULT_NO_MEMORY); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); return NULL; } diff --git a/dbus/dbus-transport-debug.h b/dbus/dbus-transport-debug.h index fd4a945c..ced95090 100644 --- a/dbus/dbus-transport-debug.h +++ b/dbus/dbus-transport-debug.h @@ -29,7 +29,7 @@ DBUS_BEGIN_DECLS; DBusTransport* _dbus_transport_debug_server_new (DBusTransport *client); DBusTransport* _dbus_transport_debug_client_new (const char *server_name, - DBusResultCode *result); + DBusError *error); DBUS_END_DECLS; diff --git a/dbus/dbus-transport-unix.c b/dbus/dbus-transport-unix.c index 5cbbb1f4..35eccb84 100644 --- a/dbus/dbus-transport-unix.c +++ b/dbus/dbus-transport-unix.c @@ -1028,20 +1028,25 @@ _dbus_transport_new_for_fd (int fd, * * @param path the path to the domain socket. * @param server #TRUE if this transport is on the server side of a connection - * @param result location to store reason for failure. + * @param error location to store reason for failure. * @returns a new transport, or #NULL on failure. */ DBusTransport* _dbus_transport_new_for_domain_socket (const char *path, dbus_bool_t server, - DBusResultCode *result) + DBusError *error) { int fd; DBusTransport *transport; - fd = _dbus_connect_unix_socket (path, result); + _DBUS_ASSERT_ERROR_IS_CLEAR (error); + + fd = _dbus_connect_unix_socket (path, error); if (fd < 0) - return NULL; + { + _DBUS_ASSERT_ERROR_IS_SET (error); + return NULL; + } _dbus_fd_set_close_on_exec (fd); @@ -1051,7 +1056,7 @@ _dbus_transport_new_for_domain_socket (const char *path, transport = _dbus_transport_new_for_fd (fd, server); if (transport == NULL) { - dbus_set_result (result, DBUS_RESULT_NO_MEMORY); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); _dbus_close (fd, NULL); fd = -1; } @@ -1072,14 +1077,19 @@ DBusTransport* _dbus_transport_new_for_tcp_socket (const char *host, dbus_int32_t port, dbus_bool_t server, - DBusResultCode *result) + DBusError *error) { int fd; DBusTransport *transport; + + _DBUS_ASSERT_ERROR_IS_CLEAR (error); - fd = _dbus_connect_tcp_socket (host, port, result); + fd = _dbus_connect_tcp_socket (host, port, error); if (fd < 0) - return NULL; + { + _DBUS_ASSERT_ERROR_IS_SET (error); + return NULL; + } _dbus_fd_set_close_on_exec (fd); @@ -1089,7 +1099,7 @@ _dbus_transport_new_for_tcp_socket (const char *host, transport = _dbus_transport_new_for_fd (fd, server); if (transport == NULL) { - dbus_set_result (result, DBUS_RESULT_NO_MEMORY); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); _dbus_close (fd, NULL); fd = -1; } diff --git a/dbus/dbus-transport-unix.h b/dbus/dbus-transport-unix.h index 1fd0134d..595fbc18 100644 --- a/dbus/dbus-transport-unix.h +++ b/dbus/dbus-transport-unix.h @@ -31,11 +31,11 @@ DBusTransport* _dbus_transport_new_for_fd (int fd, dbus_bool_t server); DBusTransport* _dbus_transport_new_for_domain_socket (const char *path, dbus_bool_t server, - DBusResultCode *result); + DBusError *error); DBusTransport* _dbus_transport_new_for_tcp_socket (const char *host, dbus_int32_t port, dbus_bool_t server, - DBusResultCode *result); + DBusError *error); DBUS_END_DECLS; diff --git a/dbus/dbus-transport.c b/dbus/dbus-transport.c index b6ad8f9a..41466ec9 100644 --- a/dbus/dbus-transport.c +++ b/dbus/dbus-transport.c @@ -178,21 +178,29 @@ _dbus_transport_finalize_base (DBusTransport *transport) * DBusResultCode is a bit limiting here. * * @param address the address. - * @param result location to store reason for failure. + * @param error location to store reason for failure. * @returns new transport of #NULL on failure. */ DBusTransport* _dbus_transport_open (const char *address, - DBusResultCode *result) + DBusError *error) { DBusTransport *transport; DBusAddressEntry **entries; int len, i; + const char *address_problem_type; + const char *address_problem_field; + const char *address_problem_other; + + _DBUS_ASSERT_ERROR_IS_CLEAR (error); - if (!dbus_parse_address (address, &entries, &len, result)) + if (!dbus_parse_address (address, &entries, &len, error)) return NULL; transport = NULL; + address_problem_type = NULL; + address_problem_field = NULL; + address_problem_other = NULL; for (i = 0; i < len; i++) { @@ -203,9 +211,13 @@ _dbus_transport_open (const char *address, const char *path = dbus_address_entry_get_value (entries[i], "path"); if (path == NULL) - goto bad_address; + { + address_problem_type = "unix"; + address_problem_field = "path"; + goto bad_address; + } - transport = _dbus_transport_new_for_domain_socket (path, FALSE, result); + transport = _dbus_transport_new_for_domain_socket (path, FALSE, error); } else if (strcmp (method, "tcp") == 0) { @@ -215,17 +227,24 @@ _dbus_transport_open (const char *address, long lport; dbus_bool_t sresult; - if (port == NULL) - goto bad_address; + if (port == NULL) + { + address_problem_type = "tcp"; + address_problem_field = "port"; + goto bad_address; + } _dbus_string_init_const (&str, port); sresult = _dbus_string_parse_int (&str, 0, &lport, NULL); _dbus_string_free (&str); if (sresult == FALSE || lport <= 0 || lport > 65535) - goto bad_address; + { + address_problem_other = "Port is not an integer between 0 and 65535"; + goto bad_address; + } - transport = _dbus_transport_new_for_tcp_socket (host, lport, FALSE, result); + transport = _dbus_transport_new_for_tcp_socket (host, lport, FALSE, error); } #ifdef DBUS_BUILD_TESTS else if (strcmp (method, "debug") == 0) @@ -233,22 +252,33 @@ _dbus_transport_open (const char *address, const char *name = dbus_address_entry_get_value (entries[i], "name"); if (name == NULL) - goto bad_address; + { + address_problem_type = "debug"; + address_problem_field = "name"; + goto bad_address; + } - transport = _dbus_transport_debug_client_new (name, result); + transport = _dbus_transport_debug_client_new (name, error); } else if (strcmp (method, "debug-pipe") == 0) { const char *name = dbus_address_entry_get_value (entries[i], "name"); - if (name == NULL) - goto bad_address; + if (name == NULL) + { + address_problem_type = "debug-pipe"; + address_problem_field = "name"; + goto bad_address; + } - transport = _dbus_transport_debug_pipe_new (name, result); + transport = _dbus_transport_debug_pipe_new (name, error); } #endif else - goto bad_address; + { + address_problem_other = "Unknown address type (examples of valid types are \"unix\" and \"tcp\")"; + goto bad_address; + } if (transport) break; @@ -259,7 +289,15 @@ _dbus_transport_open (const char *address, bad_address: dbus_address_entries_free (entries); - dbus_set_result (result, DBUS_RESULT_BAD_ADDRESS); + + if (address_problem_type != NULL) + dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS, + "Address of type %s was missing argument %s", + address_problem_type, address_problem_field); + else + dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS, + "Could not parse address: %s", + address_problem_other); return NULL; } diff --git a/dbus/dbus-transport.h b/dbus/dbus-transport.h index c016412f..d129ccef 100644 --- a/dbus/dbus-transport.h +++ b/dbus/dbus-transport.h @@ -31,7 +31,7 @@ DBUS_BEGIN_DECLS; typedef struct DBusTransport DBusTransport; DBusTransport* _dbus_transport_open (const char *address, - DBusResultCode *result); + DBusError *error); void _dbus_transport_ref (DBusTransport *transport); void _dbus_transport_unref (DBusTransport *transport); void _dbus_transport_disconnect (DBusTransport *transport); diff --git a/glib/test-dbus-glib.c b/glib/test-dbus-glib.c index b5c44024..c838b688 100644 --- a/glib/test-dbus-glib.c +++ b/glib/test-dbus-glib.c @@ -6,7 +6,6 @@ int main (int argc, char **argv) { DBusConnection *connection; - DBusResultCode result; DBusMessage *message, *reply; GMainLoop *loop; DBusError error; @@ -19,11 +18,13 @@ main (int argc, char **argv) loop = g_main_loop_new (NULL, FALSE); - connection = dbus_connection_open (argv[1], &result); + dbus_error_init (&error); + connection = dbus_connection_open (argv[1], &error); if (connection == NULL) { g_printerr ("Failed to open connection to %s: %s\n", argv[1], - dbus_result_to_string (result)); + error.message); + dbus_error_free (&error); return 1; } diff --git a/glib/test-thread-client.c b/glib/test-thread-client.c index 23ec3f33..0232b860 100644 --- a/glib/test-thread-client.c +++ b/glib/test-thread-client.c @@ -56,7 +56,7 @@ int main (int argc, char *argv[]) { GMainLoop *loop; - DBusResultCode result; + DBusError error; int i; g_thread_init (NULL); @@ -68,10 +68,12 @@ main (int argc, char *argv[]) return 1; } - connection = dbus_connection_open (argv[1], &result); + dbus_error_init (&error); + connection = dbus_connection_open (argv[1], &error); if (connection == NULL) { - g_printerr ("could not open connection\n"); + g_printerr ("could not open connection: %s\n", error.message); + dbus_error_free (&error); return 1; } diff --git a/glib/test-thread-server.c b/glib/test-thread-server.c index 066c393b..06f597df 100644 --- a/glib/test-thread-server.c +++ b/glib/test-thread-server.c @@ -202,7 +202,7 @@ main (int argc, char *argv[]) { GMainLoop *loop; DBusServer *server; - DBusResultCode result; + DBusError error; g_thread_init (NULL); dbus_gthread_init (); @@ -213,11 +213,13 @@ main (int argc, char *argv[]) return 1; } - server = dbus_server_listen (argv[1], &result); + dbus_error_init (&error); + server = dbus_server_listen (argv[1], &error); if (server == NULL) { fprintf (stderr, "Failed to start server on %s: %s\n", - argv[1], dbus_result_to_string (result)); + argv[1], error.message); + dbus_error_free (&error); return 1; } diff --git a/test/break-loader.c b/test/break-loader.c index f08f43d9..8aa15a80 100644 --- a/test/break-loader.c +++ b/test/break-loader.c @@ -154,7 +154,7 @@ try_mutated_data (const DBusString *data) if (failed) { const char *filename_c; - DBusResultCode result; + DBusError error; _dbus_string_append (&filename, ".message-raw"); @@ -162,12 +162,12 @@ try_mutated_data (const DBusString *data) printf ("Child failed, writing %s\n", filename_c); - result = _dbus_string_save_to_file (data, &filename); - - if (result != DBUS_RESULT_SUCCESS) + dbus_error_init (&error); + if (!_dbus_string_save_to_file (data, &filename, &error)) { fprintf (stderr, "Failed to save failed message data: %s\n", - dbus_result_to_string (result)); + error.message); + dbus_error_free (&error); exit (1); /* so we can see the seed that was printed out */ } diff --git a/test/echo-client.c b/test/echo-client.c index de32957f..b6f68fd7 100644 --- a/test/echo-client.c +++ b/test/echo-client.c @@ -7,7 +7,7 @@ main (int argc, char **argv) { DBusConnection *connection; - DBusResultCode result; + DBusError error; DBusMessage *message; if (argc < 2) @@ -15,12 +15,14 @@ main (int argc, fprintf (stderr, "Give the server address as an argument\n"); return 1; } - - connection = dbus_connection_open (argv[1], &result); + + dbus_error_init (&error); + connection = dbus_connection_open (argv[1], &error); if (connection == NULL) { fprintf (stderr, "Failed to open connection to %s: %s\n", - argv[1], dbus_result_to_string (result)); + argv[1], error.message); + dbus_error_free (&error); return 1; } diff --git a/test/echo-server.c b/test/echo-server.c index 27e10e72..9cfcd307 100644 --- a/test/echo-server.c +++ b/test/echo-server.c @@ -20,7 +20,7 @@ main (int argc, char **argv) { DBusServer *server; - DBusResultCode result; + DBusError error; if (argc < 2) { @@ -28,11 +28,13 @@ main (int argc, return 1; } - server = dbus_server_listen (argv[1], &result); + dbus_error_init (&error); + server = dbus_server_listen (argv[1], &error); if (server == NULL) { fprintf (stderr, "Failed to start server on %s: %s\n", - argv[1], dbus_result_to_string (result)); + argv[1], error.message); + dbus_error_free (&error); return 1; } |