Fix: Unexpected payload size in cmd_recv_stream_2_11
[lttng-tools.git] / src / common / kernel-consumer / kernel-consumer.c
index 4ace64920109dd38e58e75dad6452a78518b47b8..423b3f7bb2905196d78f9265edf98677dc9cf12d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2011 Julien Desfossez <julien.desfossez@polymtl.ca>
+ * Copyright (C) 2011 EfficiOS Inc.
  * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
  * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
  *
@@ -139,7 +139,7 @@ error:
 /*
  * Take a snapshot of all the stream of a channel
  * RCU read-side lock must be held across this function to ensure existence of
- * channel. The channel lock must be held by the caller.
+ * channel.
  *
  * Returns 0 on success, < 0 on error
  */
@@ -154,6 +154,9 @@ static int lttng_kconsumer_snapshot_channel(
 
        DBG("Kernel consumer snapshot channel %" PRIu64, key);
 
+       /* Prevent channel modifications while we perform the snapshot.*/
+       pthread_mutex_lock(&channel->lock);
+
        rcu_read_lock();
 
        /* Splice is not supported yet for channel snapshot. */
@@ -347,13 +350,14 @@ end_unlock:
        pthread_mutex_unlock(&stream->lock);
 end:
        rcu_read_unlock();
+       pthread_mutex_unlock(&channel->lock);
        return ret;
 }
 
 /*
  * Read the whole metadata available for a snapshot.
  * RCU read-side lock must be held across this function to ensure existence of
- * metadata_channel. The channel lock must be held by the caller.
+ * metadata_channel.
  *
  * Returns 0 on success, < 0 on error
  */
@@ -376,7 +380,7 @@ static int lttng_kconsumer_snapshot_metadata(
        metadata_stream = metadata_channel->metadata_stream;
        assert(metadata_stream);
 
-       pthread_mutex_lock(&metadata_stream->lock);
+       metadata_stream->read_subbuffer_ops.lock(metadata_stream);
        assert(metadata_channel->trace_chunk);
        assert(metadata_stream->trace_chunk);
 
@@ -403,16 +407,12 @@ static int lttng_kconsumer_snapshot_metadata(
 
                ret_read = lttng_consumer_read_subbuffer(metadata_stream, ctx, true);
                if (ret_read < 0) {
-                       if (ret_read != -EAGAIN) {
-                               ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
-                                               ret_read);
-                               ret = ret_read;
-                               goto error_snapshot;
-                       }
-                       /* ret_read is negative at this point so we will exit the loop. */
-                       continue;
+                       ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
+                                       ret_read);
+                       ret = ret_read;
+                       goto error_snapshot;
                }
-       } while (ret_read >= 0);
+       } while (ret_read > 0);
 
        if (use_relayd) {
                close_relayd_stream(metadata_stream);
@@ -435,7 +435,7 @@ static int lttng_kconsumer_snapshot_metadata(
 
        ret = 0;
 error_snapshot:
-       pthread_mutex_unlock(&metadata_stream->lock);
+       metadata_stream->read_subbuffer_ops.unlock(metadata_stream);
        cds_list_del(&metadata_stream->send_node);
        consumer_stream_destroy(metadata_stream, NULL);
        metadata_channel->metadata_stream = NULL;
@@ -484,17 +484,23 @@ int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
        switch (msg.cmd_type) {
        case LTTNG_CONSUMER_ADD_RELAYD_SOCKET:
        {
+               uint32_t major = msg.u.relayd_sock.major;
+               uint32_t minor = msg.u.relayd_sock.minor;
+               enum lttcomm_sock_proto protocol = (enum lttcomm_sock_proto)
+                               msg.u.relayd_sock.relayd_socket_protocol;
+
                /* Session daemon status message are handled in the following call. */
                consumer_add_relayd_socket(msg.u.relayd_sock.net_index,
-                               msg.u.relayd_sock.type, ctx, sock, consumer_sockpoll,
-                               &msg.u.relayd_sock.sock, msg.u.relayd_sock.session_id,
-                               msg.u.relayd_sock.relayd_session_id);
+                               msg.u.relayd_sock.type, ctx, sock,
+                               consumer_sockpoll, msg.u.relayd_sock.session_id,
+                               msg.u.relayd_sock.relayd_session_id, major,
+                               minor, protocol);
                goto end_nosignal;
        }
        case LTTNG_CONSUMER_ADD_CHANNEL:
        {
                struct lttng_consumer_channel *new_channel;
-               int ret_send_status, ret_add_channel;
+               int ret_send_status, ret_add_channel = 0;
                const uint64_t chunk_id = msg.u.channel.chunk_id.value;
 
                health_code_update();
@@ -965,7 +971,6 @@ error_streams_sent_nosignal:
                        ERR("Channel %" PRIu64 " not found", key);
                        ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
                } else {
-                       pthread_mutex_lock(&channel->lock);
                        if (msg.u.snapshot_channel.metadata == 1) {
                                int ret_snapshot;
 
@@ -993,7 +998,6 @@ error_streams_sent_nosignal:
                                        ret_code = LTTCOMM_CONSUMERD_SNAPSHOT_FAILED;
                                }
                        }
-                       pthread_mutex_unlock(&channel->lock);
                }
                health_code_update();
 
@@ -1587,68 +1591,94 @@ end:
 }
 
 static
-int get_subbuffer_common(struct lttng_consumer_stream *stream,
+enum get_next_subbuffer_status get_subbuffer_common(
+               struct lttng_consumer_stream *stream,
                struct stream_subbuffer *subbuffer)
 {
        int ret;
+       enum get_next_subbuffer_status status;
 
        ret = kernctl_get_next_subbuf(stream->wait_fd);
-       if (ret) {
+       switch (ret) {
+       case 0:
+               status = GET_NEXT_SUBBUFFER_STATUS_OK;
+               break;
+       case -ENODATA:
+               case -EAGAIN:
+               /*
+                * The caller only expects -ENODATA when there is no data to
+                * read, but the kernel tracer returns -EAGAIN when there is
+                * currently no data for a non-finalized stream, and -ENODATA
+                * when there is no data for a finalized stream. Those can be
+                * combined into a -ENODATA return value.
+                */
+               status = GET_NEXT_SUBBUFFER_STATUS_NO_DATA;
+               goto end;
+       default:
+               status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
                goto end;
        }
 
        ret = stream->read_subbuffer_ops.extract_subbuffer_info(
-                       stream, subbuffer);
+               stream, subbuffer);
+       if (ret) {
+               status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
+       }
 end:
-       return ret;
+       return status;
 }
 
 static
-int get_next_subbuffer_splice(struct lttng_consumer_stream *stream,
+enum get_next_subbuffer_status get_next_subbuffer_splice(
+               struct lttng_consumer_stream *stream,
                struct stream_subbuffer *subbuffer)
 {
-       int ret;
+       const enum get_next_subbuffer_status status =
+                       get_subbuffer_common(stream, subbuffer);
 
-       ret = get_subbuffer_common(stream, subbuffer);
-       if (ret) {
+       if (status != GET_NEXT_SUBBUFFER_STATUS_OK) {
                goto end;
        }
 
        subbuffer->buffer.fd = stream->wait_fd;
 end:
-       return ret;
+       return status;
 }
 
 static
-int get_next_subbuffer_mmap(struct lttng_consumer_stream *stream,
+enum get_next_subbuffer_status get_next_subbuffer_mmap(
+               struct lttng_consumer_stream *stream,
                struct stream_subbuffer *subbuffer)
 {
        int ret;
+       enum get_next_subbuffer_status status;
        const char *addr;
 
-       ret = get_subbuffer_common(stream, subbuffer);
-       if (ret) {
+       status = get_subbuffer_common(stream, subbuffer);
+       if (status != GET_NEXT_SUBBUFFER_STATUS_OK) {
                goto end;
        }
 
        ret = get_current_subbuf_addr(stream, &addr);
        if (ret) {
+               status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
                goto end;
        }
 
        subbuffer->buffer.buffer = lttng_buffer_view_init(
                        addr, 0, subbuffer->info.data.padded_subbuf_size);
 end:
-       return ret;
+       return status;
 }
 
 static
-int get_next_subbuffer_metadata_check(struct lttng_consumer_stream *stream,
+enum get_next_subbuffer_status get_next_subbuffer_metadata_check(struct lttng_consumer_stream *stream,
                struct stream_subbuffer *subbuffer)
 {
        int ret;
        const char *addr;
        bool coherent;
+       enum get_next_subbuffer_status status;
 
        ret = kernctl_get_next_subbuf_metadata_check(stream->wait_fd,
                        &coherent);
@@ -1675,7 +1705,33 @@ int get_next_subbuffer_metadata_check(struct lttng_consumer_stream *stream,
                        subbuffer->info.metadata.padded_subbuf_size,
                        coherent ? "true" : "false");
 end:
-       return ret;
+       /*
+        * The caller only expects -ENODATA when there is no data to read, but
+        * the kernel tracer returns -EAGAIN when there is currently no data
+        * for a non-finalized stream, and -ENODATA when there is no data for a
+        * finalized stream. Those can be combined into a -ENODATA return value.
+        */
+       switch (ret) {
+       case 0:
+               status = GET_NEXT_SUBBUFFER_STATUS_OK;
+               break;
+       case -ENODATA:
+       case -EAGAIN:
+               /*
+                * The caller only expects -ENODATA when there is no data to
+                * read, but the kernel tracer returns -EAGAIN when there is
+                * currently no data for a non-finalized stream, and -ENODATA
+                * when there is no data for a finalized stream. Those can be
+                * combined into a -ENODATA return value.
+                */
+               status = GET_NEXT_SUBBUFFER_STATUS_NO_DATA;
+               break;
+       default:
+               status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
+               break;
+       }
+
+       return status;
 }
 
 static
@@ -1699,8 +1755,23 @@ int put_next_subbuffer(struct lttng_consumer_stream *stream,
 static
 bool is_get_next_check_metadata_available(int tracer_fd)
 {
-       return kernctl_get_next_subbuf_metadata_check(tracer_fd, NULL) !=
-                       -ENOTTY;
+       const int ret = kernctl_get_next_subbuf_metadata_check(tracer_fd, NULL);
+       const bool available = ret != -ENOTTY;
+
+       if (ret == 0) {
+               /* get succeeded, make sure to put the subbuffer. */
+               kernctl_put_subbuf(tracer_fd);
+       }
+
+       return available;
+}
+
+static
+int signal_metadata(struct lttng_consumer_stream *stream,
+               struct lttng_consumer_local_data *ctx)
+{
+       ASSERT_LOCKED(stream->metadata_rdv_lock);
+       return pthread_cond_broadcast(&stream->metadata_rdv) ? -errno : 0;
 }
 
 static
@@ -1733,6 +1804,8 @@ int lttng_kconsumer_set_stream_ops(
                        metadata_bucket_destroy(stream->metadata_bucket);
                        stream->metadata_bucket = NULL;
                }
+
+               stream->read_subbuffer_ops.on_sleep = signal_metadata;
        }
 
        if (!stream->read_subbuffer_ops.get_next_subbuffer) {
This page took 0.027139 seconds and 4 git commands to generate.