Fix: handle shutdown on recv reply in relayd
[lttng-tools.git] / src / common / relayd / relayd.c
index 45cb097f616c1bf65b31d5d66ba45966e0877d8b..63a55c22756fd8737c00cb0158bdbb5a01b7c007 100644 (file)
@@ -90,8 +90,16 @@ static int recv_reply(struct lttcomm_sock *sock, void *data, size_t size)
        DBG3("Relayd waiting for reply of size %ld", size);
 
        ret = sock->ops->recvmsg(sock, data, size, 0);
-       if (ret < 0) {
-               ret = -errno;
+       if (ret <= 0 || ret != size) {
+               if (ret == 0) {
+                       /* Orderly shutdown. */
+                       DBG("Socket %d has performed an orderly shutdown", sock->fd);
+               } else {
+                       DBG("Receiving reply failed on sock %d for size %lu with ret %d",
+                                       sock->fd, size, ret);
+               }
+               /* Always return -1 here and the caller can use errno. */
+               ret = -1;
                goto error;
        }
 
@@ -103,8 +111,8 @@ error:
  * Send a RELAYD_CREATE_SESSION command to the relayd with the given socket and
  * set session_id of the relayd if we have a successful reply from the relayd.
  *
- * On success, return 0 else a negative value being a lttng_error_code returned
- * from the relayd.
+ * On success, return 0 else a negative value which is either an errno error or
+ * a lttng error code from the relayd.
  */
 int relayd_create_session(struct lttcomm_sock *sock, uint64_t *session_id)
 {
@@ -122,7 +130,7 @@ int relayd_create_session(struct lttcomm_sock *sock, uint64_t *session_id)
                goto error;
        }
 
-       /* Recevie response */
+       /* Receive response */
        ret = recv_reply(sock, (void *) &reply, sizeof(reply));
        if (ret < 0) {
                goto error;
@@ -133,8 +141,8 @@ int relayd_create_session(struct lttcomm_sock *sock, uint64_t *session_id)
 
        /* Return session id or negative ret code. */
        if (reply.ret_code != LTTNG_OK) {
-               ret = -reply.ret_code;
-               ERR("Relayd create session replied error %d", ret);
+               ret = -1;
+               ERR("Relayd create session replied error %d", reply.ret_code);
                goto error;
        } else {
                ret = 0;
@@ -187,8 +195,8 @@ int relayd_add_stream(struct lttcomm_sock *sock, const char *channel_name,
 
        /* Return session id or negative ret code. */
        if (reply.ret_code != LTTNG_OK) {
-               ret = -reply.ret_code;
-               ERR("Relayd add stream replied error %d", ret);
+               ret = -1;
+               ERR("Relayd add stream replied error %d", reply.ret_code);
        } else {
                /* Success */
                ret = 0;
@@ -228,7 +236,7 @@ int relayd_version_check(struct lttcomm_sock *sock, uint32_t major,
                goto error;
        }
 
-       /* Recevie response */
+       /* Receive response */
        ret = recv_reply(sock, (void *) &msg, sizeof(msg));
        if (ret < 0) {
                goto error;
@@ -389,7 +397,7 @@ int relayd_send_close_stream(struct lttcomm_sock *sock, uint64_t stream_id,
                goto error;
        }
 
-       /* Recevie response */
+       /* Receive response */
        ret = recv_reply(sock, (void *) &reply, sizeof(reply));
        if (ret < 0) {
                goto error;
@@ -399,8 +407,8 @@ int relayd_send_close_stream(struct lttcomm_sock *sock, uint64_t stream_id,
 
        /* Return session id or negative ret code. */
        if (reply.ret_code != LTTNG_OK) {
-               ret = -reply.ret_code;
-               ERR("Relayd close stream replied error %d", ret);
+               ret = -1;
+               ERR("Relayd close stream replied error %d", reply.ret_code);
        } else {
                /* Success */
                ret = 0;
@@ -439,7 +447,7 @@ int relayd_data_pending(struct lttcomm_sock *sock, uint64_t stream_id,
                goto error;
        }
 
-       /* Recevie response */
+       /* Receive response */
        ret = recv_reply(sock, (void *) &reply, sizeof(reply));
        if (ret < 0) {
                goto error;
@@ -449,8 +457,7 @@ int relayd_data_pending(struct lttcomm_sock *sock, uint64_t stream_id,
 
        /* Return session id or negative ret code. */
        if (reply.ret_code >= LTTNG_OK) {
-               ret = -reply.ret_code;
-               ERR("Relayd data pending replied error %d", ret);
+               ERR("Relayd data pending replied error %d", reply.ret_code);
        }
 
        /* At this point, the ret code is either 1 or 0 */
@@ -486,7 +493,7 @@ int relayd_quiescent_control(struct lttcomm_sock *sock,
                goto error;
        }
 
-       /* Recevie response */
+       /* Receive response */
        ret = recv_reply(sock, (void *) &reply, sizeof(reply));
        if (ret < 0) {
                goto error;
@@ -496,8 +503,8 @@ int relayd_quiescent_control(struct lttcomm_sock *sock,
 
        /* Return session id or negative ret code. */
        if (reply.ret_code != LTTNG_OK) {
-               ret = -reply.ret_code;
-               ERR("Relayd quiescent control replied error %d", ret);
+               ret = -1;
+               ERR("Relayd quiescent control replied error %d", reply.ret_code);
                goto error;
        }
 
@@ -530,7 +537,7 @@ int relayd_begin_data_pending(struct lttcomm_sock *sock, uint64_t id)
                goto error;
        }
 
-       /* Recevie response */
+       /* Receive response */
        ret = recv_reply(sock, (void *) &reply, sizeof(reply));
        if (ret < 0) {
                goto error;
@@ -540,8 +547,8 @@ int relayd_begin_data_pending(struct lttcomm_sock *sock, uint64_t id)
 
        /* Return session id or negative ret code. */
        if (reply.ret_code != LTTNG_OK) {
-               ret = -reply.ret_code;
-               ERR("Relayd begin data pending replied error %d", ret);
+               ret = -1;
+               ERR("Relayd begin data pending replied error %d", reply.ret_code);
                goto error;
        }
 
@@ -577,7 +584,7 @@ int relayd_end_data_pending(struct lttcomm_sock *sock, uint64_t id,
                goto error;
        }
 
-       /* Recevie response */
+       /* Receive response */
        ret = recv_reply(sock, (void *) &reply, sizeof(reply));
        if (ret < 0) {
                goto error;
This page took 0.026306 seconds and 4 git commands to generate.