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