From b8155545b3bd597c89860a6bd4b005045cf17855 Mon Sep 17 00:00:00 2001 From: Sledz Date: Tue, 29 Jun 2010 23:04:15 +0200 Subject: [PATCH] sysdeps-unix: enable address reuse on TCP sockets This helps to avoid 'Address already in use' problems if server has been shut down, and then restarted while sockets are still active on its port. Signed-off-by: Steffen Sledz --- dbus/dbus-sysdeps-unix.c | 20 +++++++++++++++++++- 1 files changed, 19 insertions(+), 1 deletions(-) diff --git a/dbus/dbus-sysdeps-unix.c b/dbus/dbus-sysdeps-unix.c index ebe9bdd..48b1fce 100644 --- a/dbus/dbus-sysdeps-unix.c +++ b/dbus/dbus-sysdeps-unix.c @@ -900,6 +900,7 @@ _dbus_listen_unix_socket (const char *path, int listen_fd; struct sockaddr_un addr; size_t path_len; + unsigned int reuseaddr; _DBUS_ASSERT_ERROR_IS_CLEAR (error); @@ -973,7 +974,15 @@ _dbus_listen_unix_socket (const char *path, strncpy (addr.sun_path, path, path_len); } - + + reuseaddr = 1; + if (setsockopt (listen_fd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(reuseaddr))==-1) + { + dbus_set_error (error, _dbus_error_from_errno (errno), + "Failed to set socket option\"%s\": %s", + path, _dbus_strerror (errno)); + } + if (bind (listen_fd, (struct sockaddr*) &addr, _DBUS_STRUCT_OFFSET (struct sockaddr_un, sun_path) + path_len) < 0) { dbus_set_error (error, _dbus_error_from_errno (errno), @@ -1176,6 +1185,7 @@ _dbus_listen_tcp_socket (const char *host, int nlisten_fd = 0, *listen_fd = NULL, res, i; struct addrinfo hints; struct addrinfo *ai, *tmp; + unsigned int reuseaddr; *fds_p = NULL; _DBUS_ASSERT_ERROR_IS_CLEAR (error); @@ -1220,6 +1230,14 @@ _dbus_listen_tcp_socket (const char *host, goto failed; } _DBUS_ASSERT_ERROR_IS_CLEAR(error); + + reuseaddr = 1; + if (setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(reuseaddr))==-1) + { + dbus_set_error (error, _dbus_error_from_errno (errno), + "Failed to set socket option \"%s:%s\": %s", + host ? host : "*", port, _dbus_strerror (errno)); + } if (bind (fd, (struct sockaddr*) tmp->ai_addr, tmp->ai_addrlen) < 0) { -- 1.6.4.2