Adjust the relayd protocol on version check
[lttng-tools.git] / src / bin / lttng-relayd / main.c
index cd17dcd6720717e54fa9897d96a587c97bd68450..995714faf40b6f6d34b6ed31319834baf99e2b6d 100644 (file)
@@ -734,6 +734,10 @@ char *create_output_path_noauto(char *path_name)
        char *full_path;
 
        full_path = utils_expand_path(opt_output_path);
+       if (!full_path) {
+               goto exit;
+       }
+
        ret = asprintf(&traces_path, "%s/%s", full_path, path_name);
        if (ret < 0) {
                PERROR("asprintf trace dir name");
@@ -941,6 +945,7 @@ int relay_add_stream(struct lttcomm_relayd_hdr *recv_hdr,
        ret = asprintf(&path, "%s/%s", root_path, stream_info.channel_name);
        if (ret < 0) {
                PERROR("asprintf stream path");
+               path = NULL;
                goto end;
        }
 
@@ -963,13 +968,17 @@ int relay_add_stream(struct lttcomm_relayd_hdr *recv_hdr,
 end:
        free(path);
        free(root_path);
+
+       reply.handle = htobe64(stream->stream_handle);
        /* send the session id to the client or a negative return code on error */
        if (ret < 0) {
                reply.ret_code = htobe32(LTTNG_ERR_UNK);
+               /* stream was not properly added to the ht, so free it */
+               free(stream);
        } else {
                reply.ret_code = htobe32(LTTNG_OK);
        }
-       reply.handle = htobe64(stream->stream_handle);
+
        send_ret = cmd->sock->ops->sendmsg(cmd->sock, &reply,
                        sizeof(struct lttcomm_relayd_status_stream), 0);
        if (send_ret < 0) {
@@ -1231,7 +1240,7 @@ end:
  */
 static
 int relay_send_version(struct lttcomm_relayd_hdr *recv_hdr,
-               struct relay_command *cmd)
+               struct relay_command *cmd, struct lttng_ht *streams_ht)
 {
        int ret;
        struct lttcomm_relayd_version reply, msg;
@@ -1253,13 +1262,6 @@ int relay_send_version(struct lttcomm_relayd_hdr *recv_hdr,
                goto end;
        }
 
-       /*
-        * For now, we just ignore the received version but after 2.1 stable
-        * release, a check must be done to see if we either adapt to the other
-        * side version (which MUST be lower than us) or keep the latest data
-        * structure considering that the other side will adapt.
-        */
-
        ret = sscanf(VERSION, "%10u.%10u", &reply.major, &reply.minor);
        if (ret < 2) {
                ERR("Error in scanning version");
@@ -1273,8 +1275,25 @@ int relay_send_version(struct lttcomm_relayd_hdr *recv_hdr,
        if (ret < 0) {
                ERR("Relay sending version");
        }
-       DBG("Version check done (%u.%u)", be32toh(reply.major),
-                       be32toh(reply.minor));
+
+       /* Major versions must be the same */
+       if (reply.major != be32toh(msg.major)) {
+               DBG("Incompatible major versions, deleting session");
+               relay_delete_session(cmd, streams_ht);
+               ret = 0;
+               goto end;
+       }
+
+       cmd->session->major = reply.major;
+       /* We adapt to the lowest compatible version */
+       if (reply.minor <= be32toh(msg.minor)) {
+               cmd->session->minor = reply.minor;
+       } else {
+               cmd->session->minor = be32toh(msg.minor);
+       }
+
+       DBG("Version check done using protocol %u.%u", cmd->session->major,
+                       cmd->session->minor);
 
 end:
        return ret;
@@ -1584,7 +1603,7 @@ int relay_process_control(struct lttcomm_relayd_hdr *recv_hdr,
                ret = relay_recv_metadata(recv_hdr, cmd, streams_ht);
                break;
        case RELAYD_VERSION:
-               ret = relay_send_version(recv_hdr, cmd);
+               ret = relay_send_version(recv_hdr, cmd, streams_ht);
                break;
        case RELAYD_CLOSE_STREAM:
                ret = relay_close_stream(recv_hdr, cmd, streams_ht);
@@ -2107,6 +2126,15 @@ int main(int argc, char **argv)
                goto exit;
        }
 
+       /* Try to create directory if -o, --output is specified. */
+       if (opt_output_path) {
+               ret = utils_mkdir_recursive(opt_output_path, S_IRWXU | S_IRWXG);
+               if (ret < 0) {
+                       ERR("Unable to create %s", opt_output_path);
+                       goto exit;
+               }
+       }
+
        /* Daemonize */
        if (opt_daemon) {
                ret = daemon(0, 0);
This page took 0.025357 seconds and 4 git commands to generate.