From a31f85d4ba9ca08af301d2822047b222692bbcfd Mon Sep 17 00:00:00 2001 From: Pavel Strashkin Date: Thu, 10 Nov 2011 12:35:30 -0800 Subject: [PATCH 1/1] add TCP/IP connect timeout support; change all timeout variables from int to long --- dbus/dbus-connection-internal.h | 4 +- dbus/dbus-connection.c | 28 ++++++------ dbus/dbus-connection.h | 10 ++-- dbus/dbus-pending-call-internal.h | 2 +- dbus/dbus-pending-call.c | 2 +- dbus/dbus-sysdeps-pthread.c | 2 +- dbus/dbus-sysdeps-thread-win.c | 2 +- dbus/dbus-sysdeps-unix.c | 66 +++++++++++++++++++++++---- dbus/dbus-sysdeps-win.c | 11 +++-- dbus/dbus-sysdeps.h | 4 +- dbus/dbus-threads-internal.h | 2 +- dbus/dbus-threads.c | 6 +- dbus/dbus-threads.h | 2 +- dbus/dbus-timeout.c | 2 +- dbus/dbus-transport-protected.h | 5 +- dbus/dbus-transport-socket.c | 29 ++++++++++-- dbus/dbus-transport-socket.h | 1 + dbus/dbus-transport-win.c | 14 +++++- dbus/dbus-transport.c | 8 +++- dbus/dbus-transport.h | 2 +- test/name-test/test-pending-call-timeout.c | 2 +- 21 files changed, 146 insertions(+), 58 deletions(-) diff --git a/dbus/dbus-connection-internal.h b/dbus/dbus-connection-internal.h index 4cc2761..c1281cb 100644 --- a/dbus/dbus-connection-internal.h +++ b/dbus/dbus-connection-internal.h @@ -77,12 +77,12 @@ DBusConnection* _dbus_connection_new_for_transport (DBusTransport void _dbus_connection_do_iteration_unlocked (DBusConnection *connection, DBusPendingCall *pending, unsigned int flags, - int timeout_milliseconds); + long timeout_milliseconds); void _dbus_connection_close_possibly_shared (DBusConnection *connection); void _dbus_connection_close_if_only_one_ref (DBusConnection *connection); DBusPendingCall* _dbus_pending_call_new (DBusConnection *connection, - int timeout_milliseconds, + long timeout_milliseconds, DBusTimeoutHandler timeout_handler); void _dbus_pending_call_notify (DBusPendingCall *pending); void _dbus_connection_remove_pending_call (DBusConnection *connection, diff --git a/dbus/dbus-connection.c b/dbus/dbus-connection.c index 9128ffc..8b4ff19 100644 --- a/dbus/dbus-connection.c +++ b/dbus/dbus-connection.c @@ -1064,7 +1064,7 @@ _dbus_connection_remove_pending_call (DBusConnection *connection, */ static dbus_bool_t _dbus_connection_acquire_io_path (DBusConnection *connection, - int timeout_milliseconds) + long timeout_milliseconds) { dbus_bool_t we_acquired; @@ -1079,7 +1079,7 @@ _dbus_connection_acquire_io_path (DBusConnection *connection, _dbus_verbose ("locking io_path_mutex\n"); _dbus_mutex_lock (connection->io_path_mutex); - _dbus_verbose ("start connection->io_path_acquired = %d timeout = %d\n", + _dbus_verbose ("start connection->io_path_acquired = %d timeout = %ld\n", connection->io_path_acquired, timeout_milliseconds); we_acquired = FALSE; @@ -1088,7 +1088,7 @@ _dbus_connection_acquire_io_path (DBusConnection *connection, { if (timeout_milliseconds != -1) { - _dbus_verbose ("waiting %d for IO path to be acquirable\n", + _dbus_verbose ("waiting %ld for IO path to be acquirable\n", timeout_milliseconds); if (!_dbus_condvar_wait_timeout (connection->io_path_cond, @@ -1203,7 +1203,7 @@ void _dbus_connection_do_iteration_unlocked (DBusConnection *connection, DBusPendingCall *pending, unsigned int flags, - int timeout_milliseconds) + long timeout_milliseconds) { _dbus_verbose ("start\n"); @@ -2143,7 +2143,7 @@ _dbus_connection_close_if_only_one_ref (DBusConnection *connection) * @timeout_milliseconds the timeout requested for blocking */ static void -_dbus_memory_pause_based_on_timeout (int timeout_milliseconds) +_dbus_memory_pause_based_on_timeout (long timeout_milliseconds) { if (timeout_milliseconds == -1) _dbus_sleep_milliseconds (1000); @@ -2357,7 +2357,7 @@ _dbus_connection_block_pending_call (DBusPendingCall *pending) DBusConnection *connection; dbus_uint32_t client_serial; DBusTimeout *timeout; - int timeout_milliseconds, elapsed_milliseconds; + long timeout_milliseconds, elapsed_milliseconds; _dbus_assert (pending != NULL); @@ -2383,7 +2383,7 @@ _dbus_connection_block_pending_call (DBusPendingCall *pending) { timeout_milliseconds = dbus_timeout_get_interval (timeout); - _dbus_verbose ("dbus_connection_send_with_reply_and_block(): will block %d milliseconds for reply serial %u from %ld sec %ld usec\n", + _dbus_verbose ("dbus_connection_send_with_reply_and_block(): will block %ld milliseconds for reply serial %u from %ld sec %ld usec\n", timeout_milliseconds, client_serial, start_tv_sec, start_tv_usec); @@ -2482,7 +2482,7 @@ _dbus_connection_block_pending_call (DBusPendingCall *pending) _dbus_verbose ("dbus_connection_send_with_reply_and_block(): clock set backward\n"); else if (elapsed_milliseconds < timeout_milliseconds) { - _dbus_verbose ("dbus_connection_send_with_reply_and_block(): %d milliseconds remain\n", timeout_milliseconds - elapsed_milliseconds); + _dbus_verbose ("dbus_connection_send_with_reply_and_block(): %ld milliseconds remain\n", timeout_milliseconds - elapsed_milliseconds); if (status == DBUS_DISPATCH_NEED_MEMORY) { @@ -2507,7 +2507,7 @@ _dbus_connection_block_pending_call (DBusPendingCall *pending) goto recheck_status; } - _dbus_verbose ("dbus_connection_send_with_reply_and_block(): Waited %d milliseconds and got no reply\n", + _dbus_verbose ("dbus_connection_send_with_reply_and_block(): Waited %ld milliseconds and got no reply\n", elapsed_milliseconds); _dbus_assert (!_dbus_pending_call_get_completed_unlocked (pending)); @@ -3338,7 +3338,7 @@ dbus_bool_t dbus_connection_send_with_reply (DBusConnection *connection, DBusMessage *message, DBusPendingCall **pending_return, - int timeout_milliseconds) + long timeout_milliseconds) { DBusPendingCall *pending; dbus_int32_t serial = -1; @@ -3473,7 +3473,7 @@ dbus_connection_send_with_reply (DBusConnection *connection, DBusMessage* dbus_connection_send_with_reply_and_block (DBusConnection *connection, DBusMessage *message, - int timeout_milliseconds, + long timeout_milliseconds, DBusError *error) { DBusMessage *reply; @@ -3611,7 +3611,7 @@ dbus_connection_flush (DBusConnection *connection) */ static dbus_bool_t _dbus_connection_read_write_dispatch (DBusConnection *connection, - int timeout_milliseconds, + long timeout_milliseconds, dbus_bool_t dispatch) { DBusDispatchStatus dstatus; @@ -3706,7 +3706,7 @@ _dbus_connection_read_write_dispatch (DBusConnection *connection, */ dbus_bool_t dbus_connection_read_write_dispatch (DBusConnection *connection, - int timeout_milliseconds) + long timeout_milliseconds) { _dbus_return_val_if_fail (connection != NULL, FALSE); _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE); @@ -3738,7 +3738,7 @@ dbus_connection_read_write_dispatch (DBusConnection *connection, */ dbus_bool_t dbus_connection_read_write (DBusConnection *connection, - int timeout_milliseconds) + long timeout_milliseconds) { _dbus_return_val_if_fail (connection != NULL, FALSE); _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE); diff --git a/dbus/dbus-connection.h b/dbus/dbus-connection.h index 3e2a7d8..8a4bc38 100644 --- a/dbus/dbus-connection.h +++ b/dbus/dbus-connection.h @@ -199,10 +199,10 @@ DBUS_EXPORT void dbus_connection_flush (DBusConnection *connection); DBUS_EXPORT dbus_bool_t dbus_connection_read_write_dispatch (DBusConnection *connection, - int timeout_milliseconds); + long timeout_milliseconds); DBUS_EXPORT dbus_bool_t dbus_connection_read_write (DBusConnection *connection, - int timeout_milliseconds); + long timeout_milliseconds); DBUS_EXPORT DBusMessage* dbus_connection_borrow_message (DBusConnection *connection); DBUS_EXPORT @@ -227,11 +227,11 @@ DBUS_EXPORT dbus_bool_t dbus_connection_send_with_reply (DBusConnection *connection, DBusMessage *message, DBusPendingCall **pending_return, - int timeout_milliseconds); + long timeout_milliseconds); DBUS_EXPORT DBusMessage * dbus_connection_send_with_reply_and_block (DBusConnection *connection, DBusMessage *message, - int timeout_milliseconds, + long timeout_milliseconds, DBusError *error); DBUS_EXPORT dbus_bool_t dbus_connection_set_watch_functions (DBusConnection *connection, @@ -476,7 +476,7 @@ dbus_bool_t dbus_watch_get_enabled (DBusWatch *watch); */ DBUS_EXPORT -int dbus_timeout_get_interval (DBusTimeout *timeout); +long dbus_timeout_get_interval (DBusTimeout *timeout); DBUS_EXPORT void* dbus_timeout_get_data (DBusTimeout *timeout); DBUS_EXPORT diff --git a/dbus/dbus-pending-call-internal.h b/dbus/dbus-pending-call-internal.h index 1875eea..065fe4f 100644 --- a/dbus/dbus-pending-call-internal.h +++ b/dbus/dbus-pending-call-internal.h @@ -52,7 +52,7 @@ dbus_bool_t _dbus_pending_call_set_timeout_error_unlocked (DBusPendingCal DBusMessage *message, dbus_uint32_t serial); DBusPendingCall* _dbus_pending_call_new_unlocked (DBusConnection *connection, - int timeout_milliseconds, + long timeout_milliseconds, DBusTimeoutHandler timeout_handler); DBusPendingCall* _dbus_pending_call_ref_unlocked (DBusPendingCall *pending); void _dbus_pending_call_unref_and_unlock (DBusPendingCall *pending); diff --git a/dbus/dbus-pending-call.c b/dbus/dbus-pending-call.c index e7f5cd9..9214271 100644 --- a/dbus/dbus-pending-call.c +++ b/dbus/dbus-pending-call.c @@ -92,7 +92,7 @@ static dbus_int32_t notify_user_data_slot = -1; */ DBusPendingCall* _dbus_pending_call_new_unlocked (DBusConnection *connection, - int timeout_milliseconds, + long timeout_milliseconds, DBusTimeoutHandler timeout_handler) { DBusPendingCall *pending; diff --git a/dbus/dbus-sysdeps-pthread.c b/dbus/dbus-sysdeps-pthread.c index 7073751..d13b632 100644 --- a/dbus/dbus-sysdeps-pthread.c +++ b/dbus/dbus-sysdeps-pthread.c @@ -255,7 +255,7 @@ _dbus_pthread_condvar_wait (DBusCondVar *cond, static dbus_bool_t _dbus_pthread_condvar_wait_timeout (DBusCondVar *cond, DBusMutex *mutex, - int timeout_milliseconds) + long timeout_milliseconds) { DBusMutexPThread *pmutex = DBUS_MUTEX_PTHREAD (mutex); DBusCondVarPThread *pcond = DBUS_COND_VAR_PTHREAD (cond); diff --git a/dbus/dbus-sysdeps-thread-win.c b/dbus/dbus-sysdeps-thread-win.c index e2972a3..056fc9a 100644 --- a/dbus/dbus-sysdeps-thread-win.c +++ b/dbus/dbus-sysdeps-thread-win.c @@ -208,7 +208,7 @@ _dbus_windows_condvar_wait (DBusCondVar *cond, static dbus_bool_t _dbus_windows_condvar_wait_timeout (DBusCondVar *cond, DBusMutex *mutex, - int timeout_milliseconds) + long timeout_milliseconds) { return _dbus_condvar_wait_win32 (cond, mutex, timeout_milliseconds); } diff --git a/dbus/dbus-sysdeps-unix.c b/dbus/dbus-sysdeps-unix.c index e99f67a..4a2890a 100644 --- a/dbus/dbus-sysdeps-unix.c +++ b/dbus/dbus-sysdeps-unix.c @@ -1158,6 +1158,7 @@ _dbus_listen_systemd_sockets (int **fds, * @param host the host name to connect to * @param port the port to connect to * @param family the address family to listen on, NULL for all + * @param timeout_milliseconds the connect timeout * @param error return location for error code * @returns connection file descriptor or -1 on error */ @@ -1165,20 +1166,23 @@ int _dbus_connect_tcp_socket (const char *host, const char *port, const char *family, + long timeout_milliseconds, DBusError *error) { - return _dbus_connect_tcp_socket_with_nonce (host, port, family, (const char*)NULL, error); + return _dbus_connect_tcp_socket_with_nonce (host, port, family, timeout_milliseconds, (const char*)NULL, error); } int _dbus_connect_tcp_socket_with_nonce (const char *host, const char *port, const char *family, + long timeout_milliseconds, const char *noncefile, DBusError *error) { int saved_errno = 0; int fd = -1, res; + DBusPollFD fds[1]; struct addrinfo hints; struct addrinfo *ai, *tmp; @@ -1224,15 +1228,63 @@ _dbus_connect_tcp_socket_with_nonce (const char *host, } _DBUS_ASSERT_ERROR_IS_CLEAR(error); - if (connect (fd, (struct sockaddr*) tmp->ai_addr, tmp->ai_addrlen) < 0) - { + if (!_dbus_set_fd_nonblocking (fd, error)) { + freeaddrinfo(ai); + _DBUS_ASSERT_ERROR_IS_SET(error); + + _dbus_close (fd, NULL); + fd = -1; + + return -1; + } + _DBUS_ASSERT_ERROR_IS_CLEAR(error); + + if (connect (fd, (struct sockaddr*) tmp->ai_addr, tmp->ai_addrlen) < 0 && errno != EINPROGRESS) { saved_errno = errno; + _dbus_close(fd, NULL); fd = -1; + tmp = tmp->ai_next; continue; } + fds[0].fd = fd; + fds[0].events = _DBUS_POLLOUT; + fds[0].revents = 0; + + if ((res = _dbus_poll (fds, 1, timeout_milliseconds)) == 1) { + int error = 0; + socklen_t error_len = sizeof(error); + + if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &error_len) < 0) { + saved_errno = error; + + _dbus_close(fd, NULL); + fd = -1; + + break; + } + + if (error) { + saved_errno = error; + + _dbus_close(fd, NULL); + fd = -1; + } + + break; + } + + saved_errno = errno; + + _dbus_close(fd, NULL); + fd = -1; + + if (res == 0) { + saved_errno = ETIMEDOUT; + } + break; } freeaddrinfo(ai); @@ -1261,12 +1313,6 @@ _dbus_connect_tcp_socket_with_nonce (const char *host, } } - if (!_dbus_set_fd_nonblocking (fd, error)) - { - _dbus_close (fd, NULL); - return -1; - } - return fd; } @@ -2426,7 +2472,7 @@ _dbus_getgid (void) int _dbus_poll (DBusPollFD *fds, int n_fds, - int timeout_milliseconds) + long timeout_milliseconds) { #if defined(HAVE_POLL) && !defined(BROKEN_POLL) /* This big thing is a constant expression and should get optimized diff --git a/dbus/dbus-sysdeps-win.c b/dbus/dbus-sysdeps-win.c index e30e92f..ff58731 100644 --- a/dbus/dbus-sysdeps-win.c +++ b/dbus/dbus-sysdeps-win.c @@ -921,7 +921,7 @@ out0: int _dbus_poll (DBusPollFD *fds, int n_fds, - int timeout_milliseconds) + long timeout_milliseconds) { #define USE_CHRIS_IMPL 0 @@ -947,7 +947,7 @@ _dbus_poll (DBusPollFD *fds, #ifdef DBUS_ENABLE_VERBOSE_MODE msgp = msg; - msgp += sprintf (msgp, "WSAEventSelect: to=%d\n\t", timeout_milliseconds); + msgp += sprintf (msgp, "WSAEventSelect: to=%ld\n\t", timeout_milliseconds); for (i = 0; i < n_fds; i++) { DBusPollFD *fdp = &fds[i]; @@ -1084,7 +1084,7 @@ _dbus_poll (DBusPollFD *fds, #ifdef DBUS_ENABLE_VERBOSE_MODE msgp = msg; - msgp += sprintf (msgp, "select: to=%d\n\t", timeout_milliseconds); + msgp += sprintf (msgp, "select: to=%ld\n\t", timeout_milliseconds); for (i = 0; i < n_fds; i++) { DBusPollFD *fdp = &fds[i]; @@ -1235,6 +1235,7 @@ _dbus_exit (int code) * @param host the host name to connect to * @param port the port to connect to * @param family the address family to listen on, NULL for all + * @param timeout_milliseconds the connect timeout * @param error return location for error code * @returns connection file descriptor or -1 on error */ @@ -1242,15 +1243,17 @@ int _dbus_connect_tcp_socket (const char *host, const char *port, const char *family, + long timeout_milliseconds, DBusError *error) { - return _dbus_connect_tcp_socket_with_nonce (host, port, family, (const char*)NULL, error); + return _dbus_connect_tcp_socket_with_nonce (host, port, family, timeout_milliseconds, (const char*)NULL, error); } int _dbus_connect_tcp_socket_with_nonce (const char *host, const char *port, const char *family, + long timeout_milliseconds, const char *noncefile, DBusError *error) { diff --git a/dbus/dbus-sysdeps.h b/dbus/dbus-sysdeps.h index f8438f4..42b7543 100644 --- a/dbus/dbus-sysdeps.h +++ b/dbus/dbus-sysdeps.h @@ -168,10 +168,12 @@ dbus_bool_t _dbus_socket_is_invalid (int fd); int _dbus_connect_tcp_socket (const char *host, const char *port, const char *family, + long timeout_milliseconds, DBusError *error); int _dbus_connect_tcp_socket_with_nonce (const char *host, const char *port, const char *family, + long timeout_milliseconds, const char *noncefile, DBusError *error); int _dbus_listen_tcp_socket (const char *host, @@ -304,7 +306,7 @@ typedef struct int _dbus_poll (DBusPollFD *fds, int n_fds, - int timeout_milliseconds); + long timeout_milliseconds); void _dbus_sleep_milliseconds (int milliseconds); diff --git a/dbus/dbus-threads-internal.h b/dbus/dbus-threads-internal.h index 11f9ce2..f36638a 100644 --- a/dbus/dbus-threads-internal.h +++ b/dbus/dbus-threads-internal.h @@ -42,7 +42,7 @@ void _dbus_condvar_wait (DBusCondVar *cond, DBusMutex *mutex); dbus_bool_t _dbus_condvar_wait_timeout (DBusCondVar *cond, DBusMutex *mutex, - int timeout_milliseconds); + long timeout_milliseconds); void _dbus_condvar_wake_one (DBusCondVar *cond); void _dbus_condvar_wake_all (DBusCondVar *cond); void _dbus_condvar_new_at_location (DBusCondVar **location_p); diff --git a/dbus/dbus-threads.c b/dbus/dbus-threads.c index 37b68ba..f644c1b 100644 --- a/dbus/dbus-threads.c +++ b/dbus/dbus-threads.c @@ -269,7 +269,7 @@ _dbus_condvar_wait (DBusCondVar *cond, dbus_bool_t _dbus_condvar_wait_timeout (DBusCondVar *cond, DBusMutex *mutex, - int timeout_milliseconds) + long timeout_milliseconds) { if (cond && mutex && thread_functions.condvar_wait) return (* thread_functions.condvar_wait_timeout) (cond, mutex, timeout_milliseconds); @@ -698,7 +698,7 @@ static void dbus_fake_condvar_wait (DBusCondVar *cond, DBusMutex *mutex); static dbus_bool_t dbus_fake_condvar_wait_timeout (DBusCondVar *cond, DBusMutex *mutex, - int timeout_msec); + long timeout_msec); static void dbus_fake_condvar_wake_one (DBusCondVar *cond); static void dbus_fake_condvar_wake_all (DBusCondVar *cond); @@ -793,7 +793,7 @@ dbus_fake_condvar_wait (DBusCondVar *cond, static dbus_bool_t dbus_fake_condvar_wait_timeout (DBusCondVar *cond, DBusMutex *mutex, - int timeout_msec) + long timeout_msec) { return TRUE; } diff --git a/dbus/dbus-threads.h b/dbus/dbus-threads.h index ba07ca5..2dd8510 100644 --- a/dbus/dbus-threads.h +++ b/dbus/dbus-threads.h @@ -100,7 +100,7 @@ typedef void (* DBusCondVarWaitFunction) (DBusCondVar *cond, */ typedef dbus_bool_t (* DBusCondVarWaitTimeoutFunction) (DBusCondVar *cond, DBusMutex *mutex, - int timeout_milliseconds); + long timeout_milliseconds); /** Wakes one waiting thread on a condition variable. Found in #DBusThreadFunctions. * * Has no error conditions. Must succeed if it returns. diff --git a/dbus/dbus-timeout.c b/dbus/dbus-timeout.c index a1d6ce5..15741d3 100644 --- a/dbus/dbus-timeout.c +++ b/dbus/dbus-timeout.c @@ -412,7 +412,7 @@ _dbus_timeout_list_toggle_timeout (DBusTimeoutList *timeout_list, * @param timeout the DBusTimeout object. * @returns the interval in milliseconds. */ -int +long dbus_timeout_get_interval (DBusTimeout *timeout) { return timeout->interval; diff --git a/dbus/dbus-transport-protected.h b/dbus/dbus-transport-protected.h index 44b9d78..7ed4402 100644 --- a/dbus/dbus-transport-protected.h +++ b/dbus/dbus-transport-protected.h @@ -58,7 +58,7 @@ struct DBusTransportVTable void (* do_iteration) (DBusTransport *transport, unsigned int flags, - int timeout_milliseconds); + long timeout_milliseconds); /**< Called to do a single "iteration" (block on select/poll * followed by reading or writing data). */ @@ -131,7 +131,8 @@ typedef enum DBUS_TRANSPORT_OPEN_NOT_HANDLED, /**< we aren't in charge of this address type */ DBUS_TRANSPORT_OPEN_OK, /**< we set up the listen */ DBUS_TRANSPORT_OPEN_BAD_ADDRESS, /**< malformed address */ - DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT /**< well-formed address but failed to set it up */ + DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT, /**< well-formed address but failed to set it up */ + DBUS_TRANSPORT_OPEN_BAD_TIMEOUT /**< malformed timeout (for TCP/IP) */ } DBusTransportOpenResult; DBusTransportOpenResult _dbus_transport_open_platform_specific (DBusAddressEntry *entry, diff --git a/dbus/dbus-transport-socket.c b/dbus/dbus-transport-socket.c index 0673a8c..aa4dcb4 100644 --- a/dbus/dbus-transport-socket.c +++ b/dbus/dbus-transport-socket.c @@ -1031,14 +1031,14 @@ socket_connection_set (DBusTransport *transport) static void socket_do_iteration (DBusTransport *transport, unsigned int flags, - int timeout_milliseconds) + long timeout_milliseconds) { DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport; DBusPollFD poll_fd; int poll_res; - int poll_timeout; + long poll_timeout; - _dbus_verbose (" iteration flags = %s%s timeout = %d read_watch = %p write_watch = %p fd = %d\n", + _dbus_verbose (" iteration flags = %s%s timeout = %ld read_watch = %p write_watch = %p fd = %d\n", flags & DBUS_ITERATION_DO_READING ? "read" : "", flags & DBUS_ITERATION_DO_WRITING ? "write" : "", timeout_milliseconds, @@ -1299,6 +1299,7 @@ _dbus_transport_new_for_socket (int fd, * @param host the host to connect to * @param port the port to connect to * @param family the address family to connect to + * @param timeout_milliseconds the connect timeout * @param path to nonce file * @param error location to store reason for failure. * @returns a new transport, or #NULL on failure. @@ -1307,6 +1308,7 @@ DBusTransport* _dbus_transport_new_for_tcp_socket (const char *host, const char *port, const char *family, + long timeout_milliseconds, const char *noncefile, DBusError *error) { @@ -1341,12 +1343,17 @@ _dbus_transport_new_for_tcp_socket (const char *host, !_dbus_string_append (&address, family))) goto error; + /* don't append "invalid" timeout */ + if (timeout_milliseconds > 0 && + (!_dbus_string_append_printf(&address, "timeout=%ld", timeout_milliseconds))) + goto error; + if (noncefile != NULL && (!_dbus_string_append (&address, "noncefile=") || !_dbus_string_append (&address, noncefile))) goto error; - fd = _dbus_connect_tcp_socket_with_nonce (host, port, family, noncefile, error); + fd = _dbus_connect_tcp_socket_with_nonce (host, port, family, timeout_milliseconds, noncefile, error); if (fd < 0) { _DBUS_ASSERT_ERROR_IS_SET (error); @@ -1404,6 +1411,9 @@ _dbus_transport_open_socket(DBusAddressEntry *entry, const char *family = dbus_address_entry_get_value (entry, "family"); const char *noncefile = dbus_address_entry_get_value (entry, "noncefile"); + const char *timeout = dbus_address_entry_get_value (entry, "timeout"); + long timeout_milliseconds = -1; + if ((isNonceTcp == TRUE) != (noncefile != NULL)) { _dbus_set_bad_address (error, method, "noncefile", NULL); return DBUS_TRANSPORT_OPEN_BAD_ADDRESS; @@ -1415,7 +1425,16 @@ _dbus_transport_open_socket(DBusAddressEntry *entry, return DBUS_TRANSPORT_OPEN_BAD_ADDRESS; } - *transport_p = _dbus_transport_new_for_tcp_socket (host, port, family, noncefile, error); + if (timeout) { + DBusString timeout_str; + _dbus_string_init_const (&timeout_str, timeout); + + if (!_dbus_string_parse_int (&timeout_str, 0, &timeout_milliseconds, NULL)) { + return DBUS_TRANSPORT_OPEN_BAD_TIMEOUT; + } + } + + *transport_p = _dbus_transport_new_for_tcp_socket (host, port, family, timeout_milliseconds, noncefile, error); if (*transport_p == NULL) { _DBUS_ASSERT_ERROR_IS_SET (error); diff --git a/dbus/dbus-transport-socket.h b/dbus/dbus-transport-socket.h index 8aefae3..57fff2a 100644 --- a/dbus/dbus-transport-socket.h +++ b/dbus/dbus-transport-socket.h @@ -33,6 +33,7 @@ DBusTransport* _dbus_transport_new_for_socket (int f DBusTransport* _dbus_transport_new_for_tcp_socket (const char *host, const char *port, const char *family, + long timeout_milliseconds, const char *noncefile, DBusError *error); DBusTransportOpenResult _dbus_transport_open_socket (DBusAddressEntry *entry, diff --git a/dbus/dbus-transport-win.c b/dbus/dbus-transport-win.c index faaf1bd..2fb8ef0 100644 --- a/dbus/dbus-transport-win.c +++ b/dbus/dbus-transport-win.c @@ -58,6 +58,9 @@ _dbus_transport_open_platform_specific (DBusAddressEntry *entry, const char *family = dbus_address_entry_get_value (entry, "family"); const char *noncefile = dbus_address_entry_get_value (entry, "noncefile"); + const char *timeout = dbus_address_entry_get_value (entry, "timeout"); + long timeout_milliseconds = -1; + method = dbus_address_entry_get_method (entry); _dbus_assert (method != NULL); @@ -73,7 +76,16 @@ _dbus_transport_open_platform_specific (DBusAddressEntry *entry, return DBUS_TRANSPORT_OPEN_BAD_ADDRESS; } - *transport_p = _dbus_transport_new_for_tcp_socket (host, port, family, noncefile, error); + if (timeout) { + DBusString timeout_str; + _dbus_string_init_const (&timeout_str, timeout); + + if (!_dbus_string_parse_int (&timeout_str, 0, &timeout_milliseconds, NULL)) { + return DBUS_TRANSPORT_OPEN_BAD_TIMEOUT; + } + } + + *transport_p = _dbus_transport_new_for_tcp_socket (host, port, family, timeout_milliseconds, noncefile, error); if (*transport_p == NULL) { _DBUS_ASSERT_ERROR_IS_SET (error); diff --git a/dbus/dbus-transport.c b/dbus/dbus-transport.c index f743d01..d3d447a 100644 --- a/dbus/dbus-transport.c +++ b/dbus/dbus-transport.c @@ -410,6 +410,10 @@ _dbus_transport_open (DBusAddressEntry *entry, _DBUS_ASSERT_ERROR_IS_SET (&tmp_error); goto out; break; + case DBUS_TRANSPORT_OPEN_BAD_TIMEOUT: + _DBUS_ASSERT_ERROR_IS_SET (&tmp_error); + goto out; + break; } } @@ -960,11 +964,11 @@ _dbus_transport_get_socket_fd (DBusTransport *transport, void _dbus_transport_do_iteration (DBusTransport *transport, unsigned int flags, - int timeout_milliseconds) + long timeout_milliseconds) { _dbus_assert (transport->vtable->do_iteration != NULL); - _dbus_verbose ("Transport iteration flags 0x%x timeout %d connected = %d\n", + _dbus_verbose ("Transport iteration flags 0x%x timeout %ld connected = %d\n", flags, timeout_milliseconds, !transport->disconnected); if ((flags & (DBUS_ITERATION_DO_WRITING | diff --git a/dbus/dbus-transport.h b/dbus/dbus-transport.h index 4b82151..23a950b 100644 --- a/dbus/dbus-transport.h +++ b/dbus/dbus-transport.h @@ -51,7 +51,7 @@ dbus_bool_t _dbus_transport_set_connection (DBusTransport DBusConnection *connection); void _dbus_transport_do_iteration (DBusTransport *transport, unsigned int flags, - int timeout_milliseconds); + long timeout_milliseconds); DBusDispatchStatus _dbus_transport_get_dispatch_status (DBusTransport *transport); dbus_bool_t _dbus_transport_queue_messages (DBusTransport *transport); diff --git a/test/name-test/test-pending-call-timeout.c b/test/name-test/test-pending-call-timeout.c index 381113b..0a4bf6e 100644 --- a/test/name-test/test-pending-call-timeout.c +++ b/test/name-test/test-pending-call-timeout.c @@ -12,7 +12,7 @@ static void _method_call (DBusConnection *conn, - int timeout_milliseconds) + long timeout_milliseconds) { DBusPendingCall *pending; DBusMessage *method; -- 1.7.4.1