From c370c70f77079339e2cfb7f284f3a2fb13fd2f97 Mon Sep 17 00:00:00 2001 From: Mohit Agrawal Date: Tue, 13 Aug 2019 18:45:43 +0530 Subject: rpc: glusterd start is failed and throwing an error Address already in use Problem: Some of the .t are failed due to bind is throwing an error EADDRINUSE Solution: After killing all gluster processes .t is trying to start glusterd but somehow if kernel has not cleaned up resources(socket) then glusterd startup is failed due to bind system call failure.To avoid the issue retries to call bind 10 times to execute system call succesfully Change-Id: Ia5fd6b788f7b211c1508c1b7304fc08a32266629 Fixes: bz#1743020 Signed-off-by: Mohit Agrawal --- rpc/rpc-transport/socket/src/socket.c | 44 +++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/rpc/rpc-transport/socket/src/socket.c b/rpc/rpc-transport/socket/src/socket.c index c2d7d507de8..2fbcbf4a882 100644 --- a/rpc/rpc-transport/socket/src/socket.c +++ b/rpc/rpc-transport/socket/src/socket.c @@ -925,6 +925,8 @@ __socket_server_bind(rpc_transport_t *this) int ret = -1; int opt = 1; int reuse_check_sock = -1; + uint16_t sin_port = 0; + int retries = 0; GF_VALIDATE_OR_GOTO("socket", this, out); GF_VALIDATE_OR_GOTO("socket", this->private, out); @@ -957,14 +959,42 @@ __socket_server_bind(rpc_transport_t *this) } } - ret = bind(priv->sock, (struct sockaddr *)&this->myinfo.sockaddr, - this->myinfo.sockaddr_len); + if (AF_UNIX != SA(&this->myinfo.sockaddr)->sa_family) { + sin_port = (int)ntohs( + ((struct sockaddr_in *)&this->myinfo.sockaddr)->sin_port); + if (!sin_port) { + sin_port = GF_DEFAULT_SOCKET_LISTEN_PORT; + ((struct sockaddr_in *)&this->myinfo.sockaddr)->sin_port = htons( + sin_port); + } + retries = 10; + while (retries) { + ret = bind(priv->sock, (struct sockaddr *)&this->myinfo.sockaddr, + this->myinfo.sockaddr_len); + if (ret == -1) { + gf_log(this->name, GF_LOG_ERROR, "binding to %s failed: %s", + this->myinfo.identifier, strerror(errno)); + if (errno == EADDRINUSE) { + gf_log(this->name, GF_LOG_ERROR, "Port is already in use"); + sleep(1); + retries--; + } else { + break; + } + } else { + break; + } + } + } else { + ret = bind(priv->sock, (struct sockaddr *)&this->myinfo.sockaddr, + this->myinfo.sockaddr_len); - if (ret == -1) { - gf_log(this->name, GF_LOG_ERROR, "binding to %s failed: %s", - this->myinfo.identifier, strerror(errno)); - if (errno == EADDRINUSE) { - gf_log(this->name, GF_LOG_ERROR, "Port is already in use"); + if (ret == -1) { + gf_log(this->name, GF_LOG_ERROR, "binding to %s failed: %s", + this->myinfo.identifier, strerror(errno)); + if (errno == EADDRINUSE) { + gf_log(this->name, GF_LOG_ERROR, "Port is already in use"); + } } } if (AF_UNIX != SA(&this->myinfo.sockaddr)->sa_family) { -- cgit