diff -Nur -x '*.orig' -x '*~' dbus-1.2.24/dbus/dbus-sysdeps-unix.c dbus-1.2.24.new/dbus/dbus-sysdeps-unix.c --- dbus-1.2.24/dbus/dbus-sysdeps-unix.c 2011-01-17 12:08:00.661714893 -0800 +++ dbus-1.2.24.new/dbus/dbus-sysdeps-unix.c 2011-01-17 12:08:02.109545036 -0800 @@ -765,14 +765,38 @@ _dbus_connect_tcp_socket (const char *host, const char *port, const char *family, + const char *timeout, DBusError *error) { - int fd = -1, res; + int fd = -1, res, timeout_milliseconds; + DBusPollFD fds[1]; struct addrinfo hints; struct addrinfo *ai, *tmp; _DBUS_ASSERT_ERROR_IS_CLEAR (error); + if (timeout == NULL) + timeout_milliseconds = 60000; + else + { + timeout_milliseconds = atoi(timeout); + if (errno == EINVAL) + { + dbus_set_error (error, + _dbus_error_from_errno (errno), + "timeout value is invalid: %s", timeout); + return -1; + } + + if (errno == ERANGE) + { + dbus_set_error (error, + _dbus_error_from_errno (errno), + "timeout value is out of range: %s", timeout); + return -1; + } + } + if (!_dbus_open_tcp_socket (&fd, error)) { _DBUS_ASSERT_ERROR_IS_SET(error); @@ -820,7 +844,19 @@ } _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 ((res = connect (fd, (struct sockaddr*) tmp->ai_addr, tmp->ai_addrlen)) < 0 && errno != EINPROGRESS) { _dbus_close(fd, NULL); fd = -1; @@ -828,6 +864,23 @@ continue; } + fds[0].fd = fd; + fds[0].events = _DBUS_POLLOUT; + fds[0].revents = 0; + + if ((res = _dbus_poll (fds, 1, timeout_milliseconds)) <= 0) + { + _dbus_close(fd, NULL); + fd = -1; + + if (res == 0) + { + errno = ETIMEDOUT; + } + + break; + } + break; } freeaddrinfo(ai); @@ -841,15 +894,6 @@ return -1; } - - if (!_dbus_set_fd_nonblocking (fd, error)) - { - _dbus_close (fd, NULL); - fd = -1; - - return -1; - } - return fd; } diff -Nur -x '*.orig' -x '*~' dbus-1.2.24/dbus/dbus-sysdeps.h dbus-1.2.24.new/dbus/dbus-sysdeps.h --- dbus-1.2.24/dbus/dbus-sysdeps.h 2011-01-17 12:08:00.662151358 -0800 +++ dbus-1.2.24.new/dbus/dbus-sysdeps.h 2011-01-17 12:09:32.594968625 -0800 @@ -162,6 +162,7 @@ int _dbus_connect_tcp_socket (const char *host, const char *port, const char *family, + const char *timeout, DBusError *error); int _dbus_listen_tcp_socket (const char *host, const char *port, diff -Nur -x '*.orig' -x '*~' dbus-1.2.24/dbus/dbus-transport-socket.c dbus-1.2.24.new/dbus/dbus-transport-socket.c --- dbus-1.2.24/dbus/dbus-transport-socket.c 2011-01-17 12:07:54.660740733 -0800 +++ dbus-1.2.24.new/dbus/dbus-transport-socket.c 2011-01-17 12:08:02.109984246 -0800 @@ -1241,6 +1241,7 @@ _dbus_transport_new_for_tcp_socket (const char *host, const char *port, const char *family, + const char *timeout, DBusError *error) { int fd; @@ -1274,7 +1275,12 @@ !_dbus_string_append (&address, family))) goto error; - fd = _dbus_connect_tcp_socket (host, port, family, error); + if (timeout != NULL && + (!_dbus_string_append (&address, "timeout=") || + !_dbus_string_append (&address, timeout))) + goto error; + + fd = _dbus_connect_tcp_socket (host, port, family, timeout, error); if (fd < 0) { _DBUS_ASSERT_ERROR_IS_SET (error); @@ -1327,6 +1333,7 @@ const char *host = dbus_address_entry_get_value (entry, "host"); const char *port = dbus_address_entry_get_value (entry, "port"); const char *family = dbus_address_entry_get_value (entry, "family"); + const char *timeout = dbus_address_entry_get_value (entry, "timeout"); if (port == NULL) { @@ -1334,7 +1341,7 @@ return DBUS_TRANSPORT_OPEN_BAD_ADDRESS; } - *transport_p = _dbus_transport_new_for_tcp_socket (host, port, family, error); + *transport_p = _dbus_transport_new_for_tcp_socket (host, port, family, timeout, error); if (*transport_p == NULL) { _DBUS_ASSERT_ERROR_IS_SET (error); diff -Nur -x '*.orig' -x '*~' dbus-1.2.24/dbus/dbus-transport-socket.h dbus-1.2.24.new/dbus/dbus-transport-socket.h --- dbus-1.2.24/dbus/dbus-transport-socket.h 2010-03-23 12:01:27.000000000 -0700 +++ dbus-1.2.24.new/dbus/dbus-transport-socket.h 2011-01-17 12:08:38.891882479 -0800 @@ -33,6 +33,7 @@ DBusTransport* _dbus_transport_new_for_tcp_socket (const char *host, const char *port, const char *family, + const char *timeout, DBusError *error); DBusTransportOpenResult _dbus_transport_open_socket (DBusAddressEntry *entry, DBusTransport **transport_p,