Adjust the relayd protocol on version check
[lttng-tools.git] / src / common / relayd / relayd.c
index 2ec6ef2db39aebac9451f3b560884bfb0bde5f66..da54939f9a28153e4ae343d281ec1352e07b8ef9 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;
@@ -204,11 +212,13 @@ error:
 
 /*
  * Check version numbers on the relayd.
+ * If major versions are compatible, we assign minor_to_use to the
+ * minor version of the procotol we are going to use for this session.
  *
  * Return 0 if compatible else negative value.
  */
 int relayd_version_check(struct lttcomm_sock *sock, uint32_t major,
-               uint32_t minor)
+               uint32_t minor, uint32_t *minor_to_use)
 {
        int ret;
        struct lttcomm_relayd_version msg;
@@ -228,7 +238,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;
@@ -243,15 +253,12 @@ int relayd_version_check(struct lttcomm_sock *sock, uint32_t major,
         * communication is not possible. Only major version equal can talk to each
         * other. If the minor version differs, the lowest version is used by both
         * sides.
-        *
-        * For now, before 2.1.0 stable release, we don't have to check the minor
-        * because this new mechanism with the relayd will only be available with
-        * 2.1 and NOT 2.0.x.
         */
-       if (msg.major == major) {
-               /* Compatible */
-               ret = 0;
-               DBG2("Relayd version is compatible");
+       if (msg.major != major) {
+               /* Not compatible */
+               ret = -1;
+               DBG2("Relayd version is NOT compatible. Relayd version %u != %u (us)",
+                               msg.major, major);
                goto error;
        }
 
@@ -262,11 +269,16 @@ int relayd_version_check(struct lttcomm_sock *sock, uint32_t major,
         * version is higher, it will adapt to our version so we can continue to
         * use the latest relayd communication data structure.
         */
+       if (minor <= msg.minor) {
+               *minor_to_use = minor;
+       } else {
+               *minor_to_use = msg.minor;
+       }
 
-       /* Version number not compatible */
-       DBG2("Relayd version is NOT compatible. Relayd version %u != %u (us)",
-                       msg.major, major);
-       ret = -1;
+       /* Version number compatible */
+       DBG2("Relayd version is compatible, using protocol version %u.%u",
+                       major, *minor_to_use);
+       ret = 0;
 
 error:
        return ret;
@@ -319,15 +331,44 @@ int relayd_connect(struct lttcomm_sock *sock)
 
 /*
  * Close relayd socket with an allocated lttcomm_sock.
+ *
+ * If no socket operations are found, simply return 0 meaning that everything
+ * is fine. Without operations, the socket can not possibly be opened or used.
+ * This is possible if the socket was allocated but not created. However, the
+ * caller could simply use it to store a valid file descriptor for instance
+ * passed over a Unix socket and call this to cleanup but still without a valid
+ * ops pointer.
+ *
+ * Return the close returned value. On error, a negative value is usually
+ * returned back from close(2).
  */
 int relayd_close(struct lttcomm_sock *sock)
 {
+       int ret;
+
        /* Code flow error. Safety net. */
        assert(sock);
 
+       /* An invalid fd is fine, return success. */
+       if (sock->fd < 0) {
+               ret = 0;
+               goto end;
+       }
+
        DBG3("Relayd closing socket %d", sock->fd);
 
-       return sock->ops->close(sock);
+       if (sock->ops) {
+               ret = sock->ops->close(sock);
+       } else {
+               /* Default call if no specific ops found. */
+               ret = close(sock->fd);
+               if (ret < 0) {
+                       PERROR("relayd_close default close");
+               }
+       }
+
+end:
+       return ret;
 }
 
 /*
@@ -389,7 +430,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 +440,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 +480,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,15 +490,14 @@ 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 */
        ret = reply.ret_code;
 
        DBG("Relayd data is %s pending for stream id %" PRIu64,
-                       ret == 1 ? "NOT" : "", stream_id);
+                       ret == 1 ? "" : "NOT", stream_id);
 
 error:
        return ret;
@@ -466,9 +506,11 @@ error:
 /*
  * Check on the relayd side for a quiescent state on the control socket.
  */
-int relayd_quiescent_control(struct lttcomm_sock *sock)
+int relayd_quiescent_control(struct lttcomm_sock *sock,
+               uint64_t metadata_stream_id)
 {
        int ret;
+       struct lttcomm_relayd_quiescent_control msg;
        struct lttcomm_relayd_generic_reply reply;
 
        /* Code flow error. Safety net. */
@@ -476,13 +518,15 @@ int relayd_quiescent_control(struct lttcomm_sock *sock)
 
        DBG("Relayd checking quiescent control state");
 
+       msg.stream_id = htobe64(metadata_stream_id);
+
        /* Send command */
-       ret = send_command(sock, RELAYD_QUIESCENT_CONTROL, NULL, 0, 0);
+       ret = send_command(sock, RELAYD_QUIESCENT_CONTROL, &msg, sizeof(msg), 0);
        if (ret < 0) {
                goto error;
        }
 
-       /* Recevie response */
+       /* Receive response */
        ret = recv_reply(sock, (void *) &reply, sizeof(reply));
        if (ret < 0) {
                goto error;
@@ -492,8 +536,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;
        }
 
@@ -526,7 +570,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;
@@ -536,8 +580,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;
        }
 
@@ -573,7 +617,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.02923 seconds and 4 git commands to generate.