ca3909
From 2c99b7db00a6238fd43053dd672c8ce519d8fd27 Mon Sep 17 00:00:00 2001
ca3909
From: Xavi Hernandez <xhernandez@redhat.com>
ca3909
Date: Wed, 11 Dec 2019 18:21:14 +0100
ca3909
Subject: [PATCH 341/344] socket: fix error handling
ca3909
ca3909
When __socket_proto_state_machine() detected a problem in the size of
ca3909
the request or it couldn't allocate an iobuf of the requested size, it
ca3909
returned -ENOMEM (-12). However the caller was expecting only -1 in
ca3909
case of error. For this reason the error passes undetected initially,
ca3909
adding back the socket to the epoll object. On further processing,
ca3909
however, the error is finally detected and the connection terminated.
ca3909
Meanwhile, another thread could receive a poll_in event from the same
ca3909
connection, which could cause races with the connection destruction.
ca3909
When this happened, the process crashed.
ca3909
ca3909
To fix this, all error detection conditions have been hardened to be
ca3909
more strict on what is valid and what not. Also, we don't return
ca3909
-ENOMEM anymore. We always return -1 in case of error.
ca3909
ca3909
An additional change has been done to prevent destruction of the
ca3909
transport object while it may still be needed.
ca3909
ca3909
Upstream patch:
ca3909
> Change-Id: I6e59cd81cbf670f7adfdde942625d4e6c3fbc82d
ca3909
> Upstream patch link: https://review.gluster.org/c/glusterfs/+/23861
ca3909
> Fixes: bz#1782495
ca3909
> Signed-off-by: Xavi Hernandez <xhernandez@redhat.com>
ca3909
ca3909
Change-Id: I6e59cd81cbf670f7adfdde942625d4e6c3fbc82d
ca3909
BUG: 1779696
ca3909
Signed-off-by: Xavi Hernandez <xhernandez@redhat.com>
ca3909
Reviewed-on: https://code.engineering.redhat.com/gerrit/187689
ca3909
Tested-by: RHGS Build Bot <nigelb@redhat.com>
ca3909
Reviewed-by: Raghavendra Gowdappa <rgowdapp@redhat.com>
ca3909
---
ca3909
 rpc/rpc-transport/socket/src/socket.c | 173 ++++++++++++++++++----------------
ca3909
 1 file changed, 90 insertions(+), 83 deletions(-)
ca3909
ca3909
diff --git a/rpc/rpc-transport/socket/src/socket.c b/rpc/rpc-transport/socket/src/socket.c
ca3909
index bf2fa71..f54ca83 100644
ca3909
--- a/rpc/rpc-transport/socket/src/socket.c
ca3909
+++ b/rpc/rpc-transport/socket/src/socket.c
ca3909
@@ -173,7 +173,7 @@ ssl_setup_connection_params(rpc_transport_t *this);
ca3909
                                                                                \
ca3909
         ret = __socket_readv(this, in->pending_vector, 1, &in->pending_vector, \
ca3909
                              &in->pending_count, &bytes_read);                 \
ca3909
-        if (ret == -1)                                                         \
ca3909
+        if (ret < 0)                                                           \
ca3909
             break;                                                             \
ca3909
         __socket_proto_update_priv_after_read(priv, ret, bytes_read);          \
ca3909
     }
ca3909
@@ -739,7 +739,7 @@ __socket_rwv(rpc_transport_t *this, struct iovec *vector, int count,
ca3909
                 ret = sys_writev(sock, opvector, IOV_MIN(opcount));
ca3909
             }
ca3909
 
ca3909
-            if (ret == 0 || (ret == -1 && errno == EAGAIN)) {
ca3909
+            if ((ret == 0) || ((ret < 0) && (errno == EAGAIN))) {
ca3909
                 /* done for now */
ca3909
                 break;
ca3909
             } else if (ret > 0)
ca3909
@@ -754,7 +754,7 @@ __socket_rwv(rpc_transport_t *this, struct iovec *vector, int count,
ca3909
                 errno = ENODATA;
ca3909
                 ret = -1;
ca3909
             }
ca3909
-            if (ret == -1 && errno == EAGAIN) {
ca3909
+            if ((ret < 0) && (errno == EAGAIN)) {
ca3909
                 /* done for now */
ca3909
                 break;
ca3909
             } else if (ret > 0)
ca3909
@@ -770,7 +770,7 @@ __socket_rwv(rpc_transport_t *this, struct iovec *vector, int count,
ca3909
             errno = ENOTCONN;
ca3909
             break;
ca3909
         }
ca3909
-        if (ret == -1) {
ca3909
+        if (ret < 0) {
ca3909
             if (errno == EINTR)
ca3909
                 continue;
ca3909
 
ca3909
@@ -907,7 +907,7 @@ __socket_disconnect(rpc_transport_t *this)
ca3909
     gf_log(this->name, GF_LOG_TRACE, "disconnecting %p, sock=%d", this,
ca3909
            priv->sock);
ca3909
 
ca3909
-    if (priv->sock != -1) {
ca3909
+    if (priv->sock >= 0) {
ca3909
         gf_log_callingfn(this->name, GF_LOG_TRACE,
ca3909
                          "tearing down socket connection");
ca3909
         ret = __socket_teardown_connection(this);
ca3909
@@ -942,7 +942,7 @@ __socket_server_bind(rpc_transport_t *this)
ca3909
 
ca3909
     ret = setsockopt(priv->sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
ca3909
 
ca3909
-    if (ret == -1) {
ca3909
+    if (ret != 0) {
ca3909
         gf_log(this->name, GF_LOG_ERROR,
ca3909
                "setsockopt() for SO_REUSEADDR failed (%s)", strerror(errno));
ca3909
     }
ca3909
@@ -955,7 +955,7 @@ __socket_server_bind(rpc_transport_t *this)
ca3909
         if (reuse_check_sock >= 0) {
ca3909
             ret = connect(reuse_check_sock, SA(&unix_addr),
ca3909
                           this->myinfo.sockaddr_len);
ca3909
-            if ((ret == -1) && (ECONNREFUSED == errno)) {
ca3909
+            if ((ret != 0) && (ECONNREFUSED == errno)) {
ca3909
                 sys_unlink(((struct sockaddr_un *)&unix_addr)->sun_path);
ca3909
             }
ca3909
             gf_log(this->name, GF_LOG_INFO,
ca3909
@@ -967,7 +967,7 @@ __socket_server_bind(rpc_transport_t *this)
ca3909
     ret = bind(priv->sock, (struct sockaddr *)&this->myinfo.sockaddr,
ca3909
                this->myinfo.sockaddr_len);
ca3909
 
ca3909
-    if (ret == -1) {
ca3909
+    if (ret != 0) {
ca3909
         gf_log(this->name, GF_LOG_ERROR, "binding to %s failed: %s",
ca3909
                this->myinfo.identifier, strerror(errno));
ca3909
         if (errno == EADDRINUSE) {
ca3909
@@ -976,7 +976,7 @@ __socket_server_bind(rpc_transport_t *this)
ca3909
     }
ca3909
     if (AF_UNIX != SA(&this->myinfo.sockaddr)->sa_family) {
ca3909
         if (getsockname(priv->sock, SA(&this->myinfo.sockaddr),
ca3909
-                        &this->myinfo.sockaddr_len) == -1) {
ca3909
+                        &this->myinfo.sockaddr_len) != 0) {
ca3909
             gf_log(this->name, GF_LOG_WARNING,
ca3909
                    "getsockname on (%d) failed (%s)", priv->sock,
ca3909
                    strerror(errno));
ca3909
@@ -1004,7 +1004,7 @@ __socket_nonblock(int fd)
ca3909
 
ca3909
     flags = fcntl(fd, F_GETFL);
ca3909
 
ca3909
-    if (flags != -1)
ca3909
+    if (flags >= 0)
ca3909
         ret = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
ca3909
 
ca3909
     return ret;
ca3909
@@ -1034,7 +1034,7 @@ __socket_keepalive(int fd, int family, int keepaliveintvl, int keepaliveidle,
ca3909
 #endif
ca3909
 
ca3909
     ret = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on));
ca3909
-    if (ret == -1) {
ca3909
+    if (ret != 0) {
ca3909
         gf_log("socket", GF_LOG_WARNING,
ca3909
                "failed to set keep alive option on socket %d", fd);
ca3909
         goto err;
ca3909
@@ -1051,7 +1051,7 @@ __socket_keepalive(int fd, int family, int keepaliveintvl, int keepaliveidle,
ca3909
     ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPALIVE, &keepaliveintvl,
ca3909
                      sizeof(keepaliveintvl));
ca3909
 #endif
ca3909
-    if (ret == -1) {
ca3909
+    if (ret != 0) {
ca3909
         gf_log("socket", GF_LOG_WARNING,
ca3909
                "failed to set keep alive interval on socket %d", fd);
ca3909
         goto err;
ca3909
@@ -1062,7 +1062,7 @@ __socket_keepalive(int fd, int family, int keepaliveintvl, int keepaliveidle,
ca3909
 
ca3909
     ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &keepaliveidle,
ca3909
                      sizeof(keepaliveidle));
ca3909
-    if (ret == -1) {
ca3909
+    if (ret != 0) {
ca3909
         gf_log("socket", GF_LOG_WARNING,
ca3909
                "failed to set keep idle %d on socket %d, %s", keepaliveidle, fd,
ca3909
                strerror(errno));
ca3909
@@ -1070,7 +1070,7 @@ __socket_keepalive(int fd, int family, int keepaliveintvl, int keepaliveidle,
ca3909
     }
ca3909
     ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &keepaliveintvl,
ca3909
                      sizeof(keepaliveintvl));
ca3909
-    if (ret == -1) {
ca3909
+    if (ret != 0) {
ca3909
         gf_log("socket", GF_LOG_WARNING,
ca3909
                "failed to set keep interval %d on socket %d, %s",
ca3909
                keepaliveintvl, fd, strerror(errno));
ca3909
@@ -1082,7 +1082,7 @@ __socket_keepalive(int fd, int family, int keepaliveintvl, int keepaliveidle,
ca3909
         goto done;
ca3909
     ret = setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &timeout_ms,
ca3909
                      sizeof(timeout_ms));
ca3909
-    if (ret == -1) {
ca3909
+    if (ret != 0) {
ca3909
         gf_log("socket", GF_LOG_WARNING,
ca3909
                "failed to set "
ca3909
                "TCP_USER_TIMEOUT %d on socket %d, %s",
ca3909
@@ -1093,7 +1093,7 @@ __socket_keepalive(int fd, int family, int keepaliveintvl, int keepaliveidle,
ca3909
 #if defined(TCP_KEEPCNT)
ca3909
     ret = setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &keepalivecnt,
ca3909
                      sizeof(keepalivecnt));
ca3909
-    if (ret == -1) {
ca3909
+    if (ret != 0) {
ca3909
         gf_log("socket", GF_LOG_WARNING,
ca3909
                "failed to set "
ca3909
                "TCP_KEEPCNT %d on socket %d, %s",
ca3909
@@ -1366,7 +1366,7 @@ socket_event_poll_err(rpc_transport_t *this, int gen, int idx)
ca3909
 
ca3909
     pthread_mutex_lock(&priv->out_lock);
ca3909
     {
ca3909
-        if ((priv->gen == gen) && (priv->idx == idx) && (priv->sock != -1)) {
ca3909
+        if ((priv->gen == gen) && (priv->idx == idx) && (priv->sock >= 0)) {
ca3909
             __socket_ioq_flush(this);
ca3909
             __socket_reset(this);
ca3909
             socket_closed = _gf_true;
ca3909
@@ -1405,7 +1405,7 @@ socket_event_poll_out(rpc_transport_t *this)
ca3909
         if (priv->connected == 1) {
ca3909
             ret = __socket_ioq_churn(this);
ca3909
 
ca3909
-            if (ret == -1) {
ca3909
+            if (ret < 0) {
ca3909
                 gf_log(this->name, GF_LOG_TRACE,
ca3909
                        "__socket_ioq_churn returned -1; "
ca3909
                        "disconnecting socket");
ca3909
@@ -1463,7 +1463,7 @@ __socket_read_simple_msg(rpc_transport_t *this)
ca3909
                                      &bytes_read);
ca3909
             }
ca3909
 
ca3909
-            if (ret == -1) {
ca3909
+            if (ret < 0) {
ca3909
                 gf_log(this->name, GF_LOG_WARNING,
ca3909
                        "reading from socket failed. Error (%s), "
ca3909
                        "peer (%s)",
ca3909
@@ -1661,8 +1661,8 @@ __socket_read_vectored_request(rpc_transport_t *this,
ca3909
 
ca3909
             remaining_size = RPC_FRAGSIZE(in->fraghdr) - frag->bytes_read;
ca3909
 
ca3909
-            if ((ret == -1) || ((ret == 0) && (remaining_size == 0) &&
ca3909
-                                RPC_LASTFRAG(in->fraghdr))) {
ca3909
+            if ((ret < 0) || ((ret == 0) && (remaining_size == 0) &&
ca3909
+                              RPC_LASTFRAG(in->fraghdr))) {
ca3909
                 request->vector_state = SP_STATE_VECTORED_REQUEST_INIT;
ca3909
                 in->payload_vector.iov_len = ((unsigned long)frag->fragcurrent -
ca3909
                                               (unsigned long)
ca3909
@@ -1739,8 +1739,8 @@ __socket_read_request(rpc_transport_t *this)
ca3909
 
ca3909
             remaining_size = RPC_FRAGSIZE(in->fraghdr) - frag->bytes_read;
ca3909
 
ca3909
-            if ((ret == -1) || ((ret == 0) && (remaining_size == 0) &&
ca3909
-                                (RPC_LASTFRAG(in->fraghdr)))) {
ca3909
+            if ((ret < 0) || ((ret == 0) && (remaining_size == 0) &&
ca3909
+                              (RPC_LASTFRAG(in->fraghdr)))) {
ca3909
                 request->header_state = SP_STATE_REQUEST_HEADER_INIT;
ca3909
             }
ca3909
 
ca3909
@@ -1870,8 +1870,8 @@ __socket_read_accepted_successful_reply(rpc_transport_t *this)
ca3909
             /* now read the entire remaining msg into new iobuf */
ca3909
             ret = __socket_read_simple_msg(this);
ca3909
             remaining_size = RPC_FRAGSIZE(in->fraghdr) - frag->bytes_read;
ca3909
-            if ((ret == -1) || ((ret == 0) && (remaining_size == 0) &&
ca3909
-                                RPC_LASTFRAG(in->fraghdr))) {
ca3909
+            if ((ret < 0) || ((ret == 0) && (remaining_size == 0) &&
ca3909
+                              RPC_LASTFRAG(in->fraghdr))) {
ca3909
                 frag->call_body.reply.accepted_success_state =
ca3909
                     SP_STATE_ACCEPTED_SUCCESS_REPLY_INIT;
ca3909
             }
ca3909
@@ -2003,8 +2003,8 @@ __socket_read_accepted_successful_reply_v2(rpc_transport_t *this)
ca3909
             /* now read the entire remaining msg into new iobuf */
ca3909
             ret = __socket_read_simple_msg(this);
ca3909
             remaining_size = RPC_FRAGSIZE(in->fraghdr) - frag->bytes_read;
ca3909
-            if ((ret == -1) || ((ret == 0) && (remaining_size == 0) &&
ca3909
-                                RPC_LASTFRAG(in->fraghdr))) {
ca3909
+            if ((ret < 0) || ((ret == 0) && (remaining_size == 0) &&
ca3909
+                              RPC_LASTFRAG(in->fraghdr))) {
ca3909
                 frag->call_body.reply.accepted_success_state =
ca3909
                     SP_STATE_ACCEPTED_SUCCESS_REPLY_INIT;
ca3909
             }
ca3909
@@ -2103,8 +2103,8 @@ __socket_read_accepted_reply(rpc_transport_t *this)
ca3909
 
ca3909
             remaining_size = RPC_FRAGSIZE(in->fraghdr) - frag->bytes_read;
ca3909
 
ca3909
-            if ((ret == -1) || ((ret == 0) && (remaining_size == 0) &&
ca3909
-                                (RPC_LASTFRAG(in->fraghdr)))) {
ca3909
+            if ((ret < 0) || ((ret == 0) && (remaining_size == 0) &&
ca3909
+                              (RPC_LASTFRAG(in->fraghdr)))) {
ca3909
                 frag->call_body.reply
ca3909
                     .accepted_state = SP_STATE_ACCEPTED_REPLY_INIT;
ca3909
             }
ca3909
@@ -2169,8 +2169,8 @@ __socket_read_vectored_reply(rpc_transport_t *this)
ca3909
 
ca3909
             remaining_size = RPC_FRAGSIZE(in->fraghdr) - frag->bytes_read;
ca3909
 
ca3909
-            if ((ret == -1) || ((ret == 0) && (remaining_size == 0) &&
ca3909
-                                (RPC_LASTFRAG(in->fraghdr)))) {
ca3909
+            if ((ret < 0) || ((ret == 0) && (remaining_size == 0) &&
ca3909
+                              (RPC_LASTFRAG(in->fraghdr)))) {
ca3909
                 frag->call_body.reply
ca3909
                     .status_state = SP_STATE_VECTORED_REPLY_STATUS_INIT;
ca3909
                 in->payload_vector.iov_len = (unsigned long)frag->fragcurrent -
ca3909
@@ -2237,7 +2237,7 @@ __socket_read_reply(rpc_transport_t *this)
ca3909
         /* Transition back to externally visible state. */
ca3909
         frag->state = SP_STATE_READ_MSGTYPE;
ca3909
 
ca3909
-        if (ret == -1) {
ca3909
+        if (ret < 0) {
ca3909
             gf_log(this->name, GF_LOG_WARNING,
ca3909
                    "notify for event MAP_XID failed for %s",
ca3909
                    this->peerinfo.identifier);
ca3909
@@ -2315,8 +2315,8 @@ __socket_read_frag(rpc_transport_t *this)
ca3909
 
ca3909
             remaining_size = RPC_FRAGSIZE(in->fraghdr) - frag->bytes_read;
ca3909
 
ca3909
-            if ((ret == -1) || ((ret == 0) && (remaining_size == 0) &&
ca3909
-                                (RPC_LASTFRAG(in->fraghdr)))) {
ca3909
+            if ((ret < 0) || ((ret == 0) && (remaining_size == 0) &&
ca3909
+                              (RPC_LASTFRAG(in->fraghdr)))) {
ca3909
                 /* frag->state = SP_STATE_NADA; */
ca3909
                 frag->state = SP_STATE_RPCFRAG_INIT;
ca3909
             }
ca3909
@@ -2400,7 +2400,7 @@ __socket_proto_state_machine(rpc_transport_t *this,
ca3909
                 ret = __socket_readv(this, in->pending_vector, 1,
ca3909
                                      &in->pending_vector, &in->pending_count,
ca3909
                                      NULL);
ca3909
-                if (ret == -1)
ca3909
+                if (ret < 0)
ca3909
                     goto out;
ca3909
 
ca3909
                 if (ret > 0) {
ca3909
@@ -2422,7 +2422,7 @@ __socket_proto_state_machine(rpc_transport_t *this,
ca3909
                 in->total_bytes_read += RPC_FRAGSIZE(in->fraghdr);
ca3909
 
ca3909
                 if (in->total_bytes_read >= GF_UNIT_GB) {
ca3909
-                    ret = -ENOMEM;
ca3909
+                    ret = -1;
ca3909
                     goto out;
ca3909
                 }
ca3909
 
ca3909
@@ -2430,7 +2430,7 @@ __socket_proto_state_machine(rpc_transport_t *this,
ca3909
                     this->ctx->iobuf_pool,
ca3909
                     (in->total_bytes_read + sizeof(in->fraghdr)));
ca3909
                 if (!iobuf) {
ca3909
-                    ret = -ENOMEM;
ca3909
+                    ret = -1;
ca3909
                     goto out;
ca3909
                 }
ca3909
 
ca3909
@@ -2457,7 +2457,7 @@ __socket_proto_state_machine(rpc_transport_t *this,
ca3909
             case SP_STATE_READING_FRAG:
ca3909
                 ret = __socket_read_frag(this);
ca3909
 
ca3909
-                if ((ret == -1) ||
ca3909
+                if ((ret < 0) ||
ca3909
                     (frag->bytes_read != RPC_FRAGSIZE(in->fraghdr))) {
ca3909
                     goto out;
ca3909
                 }
ca3909
@@ -2575,7 +2575,7 @@ socket_event_poll_in(rpc_transport_t *this, gf_boolean_t notify_handled)
ca3909
         pthread_mutex_unlock(&priv->notify.lock);
ca3909
     }
ca3909
 
ca3909
-    if (notify_handled && (ret != -1))
ca3909
+    if (notify_handled && (ret >= 0))
ca3909
         event_handled(ctx->event_pool, priv->sock, priv->idx, priv->gen);
ca3909
 
ca3909
     if (pollin) {
ca3909
@@ -2618,10 +2618,10 @@ socket_connect_finish(rpc_transport_t *this)
ca3909
 
ca3909
         ret = __socket_connect_finish(priv->sock);
ca3909
 
ca3909
-        if (ret == -1 && errno == EINPROGRESS)
ca3909
+        if ((ret < 0) && (errno == EINPROGRESS))
ca3909
             ret = 1;
ca3909
 
ca3909
-        if (ret == -1 && errno != EINPROGRESS) {
ca3909
+        if ((ret < 0) && (errno != EINPROGRESS)) {
ca3909
             if (!priv->connect_finish_log) {
ca3909
                 gf_log(this->name, GF_LOG_ERROR,
ca3909
                        "connection to %s failed (%s); "
ca3909
@@ -2640,7 +2640,7 @@ socket_connect_finish(rpc_transport_t *this)
ca3909
 
ca3909
             ret = getsockname(priv->sock, SA(&this->myinfo.sockaddr),
ca3909
                               &this->myinfo.sockaddr_len);
ca3909
-            if (ret == -1) {
ca3909
+            if (ret != 0) {
ca3909
                 gf_log(this->name, GF_LOG_WARNING,
ca3909
                        "getsockname on (%d) failed (%s) - "
ca3909
                        "disconnecting socket",
ca3909
@@ -2924,6 +2924,13 @@ socket_event_handler(int fd, int idx, int gen, void *data, int poll_in,
ca3909
         return;
ca3909
     }
ca3909
 
ca3909
+    /* At this point we are sure no other thread is using the transport because
ca3909
+     * we cannot receive more events until we call gf_event_handled(). However
ca3909
+     * this function may call gf_event_handled() in some cases. When this is
ca3909
+     * done, the transport may be destroyed at any moment if another thread
ca3909
+     * handled an error event. To prevent that we take a reference here. */
ca3909
+    rpc_transport_ref(this);
ca3909
+
ca3909
     GF_VALIDATE_OR_GOTO("socket", this, out);
ca3909
     GF_VALIDATE_OR_GOTO("socket", this->private, out);
ca3909
     GF_VALIDATE_OR_GOTO("socket", this->xl, out);
ca3909
@@ -2960,7 +2967,7 @@ socket_event_handler(int fd, int idx, int gen, void *data, int poll_in,
ca3909
             if (ret > 0) {
ca3909
                 gf_log(this->name, GF_LOG_TRACE,
ca3909
                        "(sock:%d) returning to wait on socket", priv->sock);
ca3909
-                return;
ca3909
+                goto out;
ca3909
             }
ca3909
         } else {
ca3909
             char *sock_type = (priv->is_server ? "Server" : "Client");
ca3909
@@ -3015,7 +3022,7 @@ socket_event_handler(int fd, int idx, int gen, void *data, int poll_in,
ca3909
     }
ca3909
 
ca3909
 out:
ca3909
-    return;
ca3909
+    rpc_transport_unref(this);
ca3909
 }
ca3909
 
ca3909
 static void
ca3909
@@ -3074,7 +3081,7 @@ socket_server_event_handler(int fd, int idx, int gen, void *data, int poll_in,
ca3909
 
ca3909
         event_handled(ctx->event_pool, fd, idx, gen);
ca3909
 
ca3909
-        if (new_sock == -1) {
ca3909
+        if (new_sock < 0) {
ca3909
             gf_log(this->name, GF_LOG_WARNING, "accept on %d failed (%s)",
ca3909
                    priv->sock, strerror(errno));
ca3909
             goto out;
ca3909
@@ -3082,7 +3089,7 @@ socket_server_event_handler(int fd, int idx, int gen, void *data, int poll_in,
ca3909
 
ca3909
         if (priv->nodelay && (new_sockaddr.ss_family != AF_UNIX)) {
ca3909
             ret = __socket_nodelay(new_sock);
ca3909
-            if (ret == -1) {
ca3909
+            if (ret != 0) {
ca3909
                 gf_log(this->name, GF_LOG_WARNING,
ca3909
                        "setsockopt() failed for "
ca3909
                        "NODELAY (%s)",
ca3909
@@ -3094,7 +3101,7 @@ socket_server_event_handler(int fd, int idx, int gen, void *data, int poll_in,
ca3909
             ret = __socket_keepalive(new_sock, new_sockaddr.ss_family,
ca3909
                                      priv->keepaliveintvl, priv->keepaliveidle,
ca3909
                                      priv->keepalivecnt, priv->timeout);
ca3909
-            if (ret == -1)
ca3909
+            if (ret != 0)
ca3909
                 gf_log(this->name, GF_LOG_WARNING,
ca3909
                        "Failed to set keep-alive: %s", strerror(errno));
ca3909
         }
ca3909
@@ -3110,7 +3117,7 @@ socket_server_event_handler(int fd, int idx, int gen, void *data, int poll_in,
ca3909
         }
ca3909
 
ca3909
         ret = pthread_mutex_init(&new_trans->lock, NULL);
ca3909
-        if (ret == -1) {
ca3909
+        if (ret != 0) {
ca3909
             gf_log(this->name, GF_LOG_WARNING,
ca3909
                    "pthread_mutex_init() failed: %s; closing newly accepted "
ca3909
                    "socket %d",
ca3909
@@ -3130,7 +3137,7 @@ socket_server_event_handler(int fd, int idx, int gen, void *data, int poll_in,
ca3909
 
ca3909
         ret = getsockname(new_sock, SA(&new_trans->myinfo.sockaddr),
ca3909
                           &new_trans->myinfo.sockaddr_len);
ca3909
-        if (ret == -1) {
ca3909
+        if (ret != 0) {
ca3909
             gf_log(this->name, GF_LOG_WARNING,
ca3909
                    "getsockname on socket %d "
ca3909
                    "failed (errno:%s); closing newly accepted socket",
ca3909
@@ -3237,7 +3244,7 @@ socket_server_event_handler(int fd, int idx, int gen, void *data, int poll_in,
ca3909
              */
ca3909
             ret = rpc_transport_notify(this, RPC_TRANSPORT_ACCEPT, new_trans);
ca3909
 
ca3909
-            if (ret != -1) {
ca3909
+            if (ret >= 0) {
ca3909
                 new_priv->idx = event_register(
ca3909
                     ctx->event_pool, new_sock, socket_event_handler, new_trans,
ca3909
                     1, 0, new_trans->notify_poller_death);
ca3909
@@ -3275,7 +3282,7 @@ socket_server_event_handler(int fd, int idx, int gen, void *data, int poll_in,
ca3909
             rpc_transport_unref(new_trans);
ca3909
         }
ca3909
 
ca3909
-        if (ret == -1) {
ca3909
+        if (ret < 0) {
ca3909
             gf_log(this->name, GF_LOG_WARNING, "closing newly accepted socket");
ca3909
             sys_close(new_sock);
ca3909
             /* this unref is to actually cause the destruction of
ca3909
@@ -3406,7 +3413,7 @@ socket_connect(rpc_transport_t *this, int port)
ca3909
 
ca3909
     pthread_mutex_lock(&priv->out_lock);
ca3909
     {
ca3909
-        if (priv->sock != -1) {
ca3909
+        if (priv->sock >= 0) {
ca3909
             gf_log_callingfn(this->name, GF_LOG_TRACE,
ca3909
                              "connect () called on transport "
ca3909
                              "already connected");
ca3909
@@ -3420,7 +3427,7 @@ socket_connect(rpc_transport_t *this, int port)
ca3909
 
ca3909
         ret = socket_client_get_remote_sockaddr(this, &sock_union.sa,
ca3909
                                                 &sockaddr_len, &sa_family);
ca3909
-        if (ret == -1) {
ca3909
+        if (ret < 0) {
ca3909
             /* logged inside client_get_remote_sockaddr */
ca3909
             goto unlock;
ca3909
         }
ca3909
@@ -3439,7 +3446,7 @@ socket_connect(rpc_transport_t *this, int port)
ca3909
         this->peerinfo.sockaddr_len = sockaddr_len;
ca3909
 
ca3909
         priv->sock = sys_socket(sa_family, SOCK_STREAM, 0);
ca3909
-        if (priv->sock == -1) {
ca3909
+        if (priv->sock < 0) {
ca3909
             gf_log(this->name, GF_LOG_ERROR, "socket creation failed (%s)",
ca3909
                    strerror(errno));
ca3909
             ret = -1;
ca3909
@@ -3451,7 +3458,7 @@ socket_connect(rpc_transport_t *this, int port)
ca3909
          */
ca3909
         if (priv->windowsize != 0) {
ca3909
             if (setsockopt(priv->sock, SOL_SOCKET, SO_RCVBUF, &priv->windowsize,
ca3909
-                           sizeof(priv->windowsize)) < 0) {
ca3909
+                           sizeof(priv->windowsize)) != 0) {
ca3909
                 gf_log(this->name, GF_LOG_ERROR,
ca3909
                        "setting receive window "
ca3909
                        "size failed: %d: %d: %s",
ca3909
@@ -3459,7 +3466,7 @@ socket_connect(rpc_transport_t *this, int port)
ca3909
             }
ca3909
 
ca3909
             if (setsockopt(priv->sock, SOL_SOCKET, SO_SNDBUF, &priv->windowsize,
ca3909
-                           sizeof(priv->windowsize)) < 0) {
ca3909
+                           sizeof(priv->windowsize)) != 0) {
ca3909
                 gf_log(this->name, GF_LOG_ERROR,
ca3909
                        "setting send window size "
ca3909
                        "failed: %d: %d: %s",
ca3909
@@ -3484,7 +3491,7 @@ socket_connect(rpc_transport_t *this, int port)
ca3909
         if (priv->nodelay && (sa_family != AF_UNIX)) {
ca3909
             ret = __socket_nodelay(priv->sock);
ca3909
 
ca3909
-            if (ret == -1) {
ca3909
+            if (ret != 0) {
ca3909
                 gf_log(this->name, GF_LOG_ERROR, "NODELAY on %d failed (%s)",
ca3909
                        priv->sock, strerror(errno));
ca3909
             }
ca3909
@@ -3494,7 +3501,7 @@ socket_connect(rpc_transport_t *this, int port)
ca3909
             ret = __socket_keepalive(priv->sock, sa_family,
ca3909
                                      priv->keepaliveintvl, priv->keepaliveidle,
ca3909
                                      priv->keepalivecnt, priv->timeout);
ca3909
-            if (ret == -1)
ca3909
+            if (ret != 0)
ca3909
                 gf_log(this->name, GF_LOG_ERROR, "Failed to set keep-alive: %s",
ca3909
                        strerror(errno));
ca3909
         }
ca3909
@@ -3516,7 +3523,7 @@ socket_connect(rpc_transport_t *this, int port)
ca3909
 
ca3909
         ret = client_bind(this, SA(&this->myinfo.sockaddr),
ca3909
                           &this->myinfo.sockaddr_len, priv->sock);
ca3909
-        if (ret == -1) {
ca3909
+        if (ret < 0) {
ca3909
             gf_log(this->name, GF_LOG_WARNING, "client bind failed: %s",
ca3909
                    strerror(errno));
ca3909
             goto handler;
ca3909
@@ -3525,7 +3532,7 @@ socket_connect(rpc_transport_t *this, int port)
ca3909
         /* make socket non-blocking for all types of sockets */
ca3909
         if (!priv->bio) {
ca3909
             ret = __socket_nonblock(priv->sock);
ca3909
-            if (ret == -1) {
ca3909
+            if (ret != 0) {
ca3909
                 gf_log(this->name, GF_LOG_ERROR, "NBIO on %d failed (%s)",
ca3909
                        priv->sock, strerror(errno));
ca3909
                 goto handler;
ca3909
@@ -3552,7 +3559,7 @@ socket_connect(rpc_transport_t *this, int port)
ca3909
 
ca3909
         connect_attempted = _gf_true;
ca3909
 
ca3909
-        if (ret == -1 && errno == ENOENT && ign_enoent) {
ca3909
+        if ((ret != 0) && (errno == ENOENT) && ign_enoent) {
ca3909
             gf_log(this->name, GF_LOG_WARNING,
ca3909
                    "Ignore failed connection attempt on %s, (%s) ",
ca3909
                    this->peerinfo.identifier, strerror(errno));
ca3909
@@ -3570,7 +3577,7 @@ socket_connect(rpc_transport_t *this, int port)
ca3909
             goto handler;
ca3909
         }
ca3909
 
ca3909
-        if (ret == -1 && ((errno != EINPROGRESS) && (errno != ENOENT))) {
ca3909
+        if ((ret != 0) && (errno != EINPROGRESS) && (errno != ENOENT)) {
ca3909
             /* For unix path based sockets, the socket path is
ca3909
              * cryptic (md5sum of path) and may not be useful for
ca3909
              * the user in debugging so log it in DEBUG
ca3909
@@ -3634,8 +3641,8 @@ socket_connect(rpc_transport_t *this, int port)
ca3909
     pthread_mutex_unlock(&priv->out_lock);
ca3909
 
ca3909
 err:
ca3909
-    /* if sock != -1, then cleanup is done from the event handler */
ca3909
-    if (ret == -1 && sock == -1) {
ca3909
+    /* if sock >= 0, then cleanup is done from the event handler */
ca3909
+    if ((ret < 0) && (sock < 0)) {
ca3909
         /* Cleaup requires to send notification to upper layer which
ca3909
            intern holds the big_lock. There can be dead-lock situation
ca3909
            if big_lock is already held by the current thread.
ca3909
@@ -3689,20 +3696,20 @@ socket_listen(rpc_transport_t *this)
ca3909
     }
ca3909
     pthread_mutex_unlock(&priv->out_lock);
ca3909
 
ca3909
-    if (sock != -1) {
ca3909
+    if (sock >= 0) {
ca3909
         gf_log_callingfn(this->name, GF_LOG_DEBUG, "already listening");
ca3909
         return ret;
ca3909
     }
ca3909
 
ca3909
     ret = socket_server_get_local_sockaddr(this, SA(&sockaddr), &sockaddr_len,
ca3909
                                            &sa_family);
ca3909
-    if (ret == -1) {
ca3909
+    if (ret < 0) {
ca3909
         return ret;
ca3909
     }
ca3909
 
ca3909
     pthread_mutex_lock(&priv->out_lock);
ca3909
     {
ca3909
-        if (priv->sock != -1) {
ca3909
+        if (priv->sock >= 0) {
ca3909
             gf_log(this->name, GF_LOG_DEBUG, "already listening");
ca3909
             goto unlock;
ca3909
         }
ca3909
@@ -3712,7 +3719,7 @@ socket_listen(rpc_transport_t *this)
ca3909
 
ca3909
         priv->sock = sys_socket(sa_family, SOCK_STREAM, 0);
ca3909
 
ca3909
-        if (priv->sock == -1) {
ca3909
+        if (priv->sock < 0) {
ca3909
             gf_log(this->name, GF_LOG_ERROR, "socket creation failed (%s)",
ca3909
                    strerror(errno));
ca3909
             goto unlock;
ca3909
@@ -3723,7 +3730,7 @@ socket_listen(rpc_transport_t *this)
ca3909
          */
ca3909
         if (priv->windowsize != 0) {
ca3909
             if (setsockopt(priv->sock, SOL_SOCKET, SO_RCVBUF, &priv->windowsize,
ca3909
-                           sizeof(priv->windowsize)) < 0) {
ca3909
+                           sizeof(priv->windowsize)) != 0) {
ca3909
                 gf_log(this->name, GF_LOG_ERROR,
ca3909
                        "setting receive window size "
ca3909
                        "failed: %d: %d: %s",
ca3909
@@ -3731,7 +3738,7 @@ socket_listen(rpc_transport_t *this)
ca3909
             }
ca3909
 
ca3909
             if (setsockopt(priv->sock, SOL_SOCKET, SO_SNDBUF, &priv->windowsize,
ca3909
-                           sizeof(priv->windowsize)) < 0) {
ca3909
+                           sizeof(priv->windowsize)) != 0) {
ca3909
                 gf_log(this->name, GF_LOG_ERROR,
ca3909
                        "setting send window size failed:"
ca3909
                        " %d: %d: %s",
ca3909
@@ -3741,7 +3748,7 @@ socket_listen(rpc_transport_t *this)
ca3909
 
ca3909
         if (priv->nodelay && (sa_family != AF_UNIX)) {
ca3909
             ret = __socket_nodelay(priv->sock);
ca3909
-            if (ret == -1) {
ca3909
+            if (ret != 0) {
ca3909
                 gf_log(this->name, GF_LOG_ERROR,
ca3909
                        "setsockopt() failed for NODELAY (%s)", strerror(errno));
ca3909
             }
ca3909
@@ -3750,7 +3757,7 @@ socket_listen(rpc_transport_t *this)
ca3909
         if (!priv->bio) {
ca3909
             ret = __socket_nonblock(priv->sock);
ca3909
 
ca3909
-            if (ret == -1) {
ca3909
+            if (ret != 0) {
ca3909
                 gf_log(this->name, GF_LOG_ERROR,
ca3909
                        "NBIO on socket %d failed "
ca3909
                        "(errno:%s); closing socket",
ca3909
@@ -3763,7 +3770,7 @@ socket_listen(rpc_transport_t *this)
ca3909
 
ca3909
         ret = __socket_server_bind(this);
ca3909
 
ca3909
-        if ((ret == -EADDRINUSE) || (ret == -1)) {
ca3909
+        if (ret < 0) {
ca3909
             /* logged inside __socket_server_bind() */
ca3909
             gf_log(this->name, GF_LOG_ERROR,
ca3909
                    "__socket_server_bind failed;"
ca3909
@@ -3779,7 +3786,7 @@ socket_listen(rpc_transport_t *this)
ca3909
 
ca3909
         ret = listen(priv->sock, priv->backlog);
ca3909
 
ca3909
-        if (ret == -1) {
ca3909
+        if (ret != 0) {
ca3909
             gf_log(this->name, GF_LOG_ERROR,
ca3909
                    "could not set socket %d to listen mode (errno:%s); "
ca3909
                    "closing socket",
ca3909
@@ -4025,7 +4032,7 @@ reconfigure(rpc_transport_t *this, dict_t *options)
ca3909
     priv = this->private;
ca3909
 
ca3909
     if (dict_get_str(options, "transport.socket.keepalive", &optstr) == 0) {
ca3909
-        if (gf_string2boolean(optstr, &tmp_bool) == -1) {
ca3909
+        if (gf_string2boolean(optstr, &tmp_bool) != 0) {
ca3909
             gf_log(this->name, GF_LOG_ERROR,
ca3909
                    "'transport.socket.keepalive' takes only "
ca3909
                    "boolean options, not taking any action");
ca3909
@@ -4094,7 +4101,7 @@ reconfigure(rpc_transport_t *this, dict_t *options)
ca3909
     if (dict_get(options, "non-blocking-io")) {
ca3909
         optstr = data_to_str(dict_get(options, "non-blocking-io"));
ca3909
 
ca3909
-        if (gf_string2boolean(optstr, &tmp_bool) == -1) {
ca3909
+        if (gf_string2boolean(optstr, &tmp_bool) != 0) {
ca3909
             gf_log(this->name, GF_LOG_ERROR,
ca3909
                    "'non-blocking-io' takes only boolean options,"
ca3909
                    " not taking any action");
ca3909
@@ -4109,7 +4116,7 @@ reconfigure(rpc_transport_t *this, dict_t *options)
ca3909
 
ca3909
     if (!priv->bio) {
ca3909
         ret = __socket_nonblock(priv->sock);
ca3909
-        if (ret == -1) {
ca3909
+        if (ret != 0) {
ca3909
             gf_log(this->name, GF_LOG_WARNING, "NBIO on %d failed (%s)",
ca3909
                    priv->sock, strerror(errno));
ca3909
             goto out;
ca3909
@@ -4508,7 +4515,7 @@ socket_init(rpc_transport_t *this)
ca3909
     if (dict_get(this->options, "non-blocking-io")) {
ca3909
         optstr = data_to_str(dict_get(this->options, "non-blocking-io"));
ca3909
 
ca3909
-        if (gf_string2boolean(optstr, &tmp_bool) == -1) {
ca3909
+        if (gf_string2boolean(optstr, &tmp_bool) != 0) {
ca3909
             gf_log(this->name, GF_LOG_ERROR,
ca3909
                    "'non-blocking-io' takes only boolean options,"
ca3909
                    " not taking any action");
ca3909
@@ -4528,7 +4535,7 @@ socket_init(rpc_transport_t *this)
ca3909
         optstr = data_to_str(
ca3909
             dict_get(this->options, "transport.socket.nodelay"));
ca3909
 
ca3909
-        if (gf_string2boolean(optstr, &tmp_bool) == -1) {
ca3909
+        if (gf_string2boolean(optstr, &tmp_bool) != 0) {
ca3909
             gf_log(this->name, GF_LOG_ERROR,
ca3909
                    "'transport.socket.nodelay' takes only "
ca3909
                    "boolean options, not taking any action");
ca3909
@@ -4559,7 +4566,7 @@ socket_init(rpc_transport_t *this)
ca3909
     priv->keepalivecnt = GF_KEEPALIVE_COUNT;
ca3909
     if (dict_get_str(this->options, "transport.socket.keepalive", &optstr) ==
ca3909
         0) {
ca3909
-        if (gf_string2boolean(optstr, &tmp_bool) == -1) {
ca3909
+        if (gf_string2boolean(optstr, &tmp_bool) != 0) {
ca3909
             gf_log(this->name, GF_LOG_ERROR,
ca3909
                    "'transport.socket.keepalive' takes only "
ca3909
                    "boolean options, not taking any action");
ca3909
@@ -4609,7 +4616,7 @@ socket_init(rpc_transport_t *this)
ca3909
     if (dict_get(this->options, "transport.socket.read-fail-log")) {
ca3909
         optstr = data_to_str(
ca3909
             dict_get(this->options, "transport.socket.read-fail-log"));
ca3909
-        if (gf_string2boolean(optstr, &tmp_bool) == -1) {
ca3909
+        if (gf_string2boolean(optstr, &tmp_bool) != 0) {
ca3909
             gf_log(this->name, GF_LOG_WARNING,
ca3909
                    "'transport.socket.read-fail-log' takes only "
ca3909
                    "boolean options; logging socket read fails");
ca3909
@@ -4646,7 +4653,7 @@ fini(rpc_transport_t *this)
ca3909
 
ca3909
     priv = this->private;
ca3909
     if (priv) {
ca3909
-        if (priv->sock != -1) {
ca3909
+        if (priv->sock >= 0) {
ca3909
             pthread_mutex_lock(&priv->out_lock);
ca3909
             {
ca3909
                 __socket_ioq_flush(this);
ca3909
@@ -4683,7 +4690,7 @@ init(rpc_transport_t *this)
ca3909
 
ca3909
     ret = socket_init(this);
ca3909
 
ca3909
-    if (ret == -1) {
ca3909
+    if (ret < 0) {
ca3909
         gf_log(this->name, GF_LOG_DEBUG, "socket_init() failed");
ca3909
     }
ca3909
 
ca3909
-- 
ca3909
1.8.3.1
ca3909