+ rcu_read_lock();
+
+ metadata_channel = consumer_find_channel(key);
+ if (!metadata_channel) {
+ ERR("Snapshot kernel metadata channel not found for key %lu", key);
+ ret = -1;
+ goto end;
+ }
+
+ metadata_stream = metadata_channel->metadata_stream;
+ assert(metadata_stream);
+
+ if (relayd_id != (uint64_t) -1ULL) {
+ ret = send_relayd_stream(metadata_stream, path);
+ if (ret < 0) {
+ ERR("sending stream to relayd");
+ }
+ DBG("Stream %s sent to the relayd", metadata_stream->name);
+ } else {
+ ret = utils_create_stream_file(path, metadata_stream->name,
+ metadata_stream->chan->tracefile_size,
+ metadata_stream->tracefile_count_current,
+ metadata_stream->uid, metadata_stream->gid);
+ if (ret < 0) {
+ goto end;
+ }
+ metadata_stream->out_fd = ret;
+ }
+
+ ret = 0;
+ while (ret >= 0) {
+ ret = lttng_kconsumer_read_subbuffer(metadata_stream, ctx);
+ if (ret < 0) {
+ if (ret != -EPERM) {
+ ERR("Kernel snapshot reading subbuffer");
+ goto end;
+ }
+ /* "ret" is negative at this point so we will exit the loop. */
+ continue;
+ }
+ }
+
+ if (relayd_id == (uint64_t) -1ULL) {
+ ret = close(metadata_stream->out_fd);
+ if (ret < 0) {
+ PERROR("Kernel consumer snapshot close out_fd");
+ goto end;
+ }
+ metadata_stream->out_fd = -1;
+ } else {
+ close_relayd_stream(metadata_stream);
+ metadata_stream->net_seq_idx = (uint64_t) -1ULL;
+ }
+
+ ret = 0;
+end:
+ rcu_read_unlock();
+ return ret;
+}
+
+/*
+ * Receive command from session daemon and process it.
+ *
+ * Return 1 on success else a negative value or 0.
+ */
+int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
+ int sock, struct pollfd *consumer_sockpoll)
+{
+ ssize_t ret;
+ enum lttng_error_code ret_code = LTTNG_OK;
+ struct lttcomm_consumer_msg msg;
+
+ ret = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg));
+ if (ret != sizeof(msg)) {
+ lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_CMD);
+ if (ret > 0) {
+ ret = -1;
+ }
+ return ret;
+ }
+ if (msg.cmd_type == LTTNG_CONSUMER_STOP) {