summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Wu <peter@lekensteyn.nl>2014-11-27 23:59:22 +0100
committerDaniel Stenberg <daniel@haxx.se>2014-12-04 02:52:18 +0100
commit99fb36797a3f0b64ad20fcb8b83026875640f8e0 (patch)
treed133f8a404e1e7b83daeaf2dc1a0675d5bd89c86
parente9c7a86220ddf4e67b8bff56cddfc7388afcc9ef (diff)
downloadcurl-99fb36797a3f0b64ad20fcb8b83026875640f8e0.tar.gz
sws: try to remove socket and retry bind
If sws is killed it might leave a stale socket file on the filesystem which would cause an EADDRINUSE error. After this patch, it is checked whether the socket is really stale and if so, the socket file gets removed and another bind is executed. Signed-off-by: Peter Wu <peter@lekensteyn.nl>
-rw-r--r--tests/server/sws.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/tests/server/sws.c b/tests/server/sws.c
index 766c46a43..03e0d0c3d 100644
--- a/tests/server/sws.c
+++ b/tests/server/sws.c
@@ -2143,6 +2143,48 @@ int main(int argc, char *argv[])
me.sau.sun_family = AF_UNIX;
strncpy(me.sau.sun_path, unix_socket, sizeof(me.sau.sun_path));
rc = bind(sock, &me.sa, sizeof(me.sau));
+ if(0 != rc && errno == EADDRINUSE) {
+ struct stat statbuf;
+ /* socket already exists. Perhaps it is stale? */
+ int unixfd = socket(AF_UNIX, SOCK_STREAM, 0);
+ if(CURL_SOCKET_BAD == unixfd) {
+ error = SOCKERRNO;
+ logmsg("Error binding socket, failed to create socket at %s: (%d) %s",
+ unix_socket, error, strerror(error));
+ goto sws_cleanup;
+ }
+ /* check whether the server is alive */
+ rc = connect(unixfd, &me.sa, sizeof(me.sau));
+ error = errno;
+ close(unixfd);
+ if(ECONNREFUSED != error) {
+ logmsg("Error binding socket, failed to connect to %s: (%d) %s",
+ unix_socket, error, strerror(error));
+ goto sws_cleanup;
+ }
+ /* socket server is not alive, now check if it was actually a socket.
+ * Systems which have UNIX sockets will also have lstat */
+ rc = lstat(unix_socket, &statbuf);
+ if (0 != rc) {
+ logmsg("Error binding socket, failed to stat %s: (%d) %s",
+ unix_socket, errno, strerror(errno));
+ goto sws_cleanup;
+ }
+ if((statbuf.st_mode & S_IFSOCK) != S_IFSOCK) {
+ logmsg("Error binding socket, failed to stat %s: (%d) %s",
+ unix_socket, error, strerror(error));
+ goto sws_cleanup;
+ }
+ /* dead socket, cleanup and retry bind */
+ rc = unlink(unix_socket);
+ if(0 != rc) {
+ logmsg("Error binding socket, failed to unlink %s: (%d) %s",
+ unix_socket, errno, strerror(errno));
+ goto sws_cleanup;
+ }
+ /* stale socket is gone, retry bind */
+ rc = bind(sock, &me.sa, sizeof(me.sau));
+ }
break;
#endif /* USE_UNIX_SOCKETS */
}