+ assert(ctx);
+
+ DBG("Kernel consumer snapshot metadata with key %" PRIu64 " at path %s",
+ key, path);
+
+ rcu_read_lock();
+
+ metadata_channel = consumer_find_channel(key);
+ if (!metadata_channel) {
+ ERR("Kernel snapshot metadata not found for key %" PRIu64, key);
+ ret = -1;
+ goto error;
+ }
+
+ metadata_stream = metadata_channel->metadata_stream;
+ assert(metadata_stream);
+
+ /* Flag once that we have a valid relayd for the stream. */
+ if (relayd_id != (uint64_t) -1ULL) {
+ use_relayd = 1;
+ }
+
+ if (use_relayd) {
+ ret = consumer_send_relayd_stream(metadata_stream, path);
+ if (ret < 0) {
+ goto error;
+ }
+ } 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 error;
+ }
+ metadata_stream->out_fd = ret;
+ }
+
+ do {
+ ret_read = lttng_kconsumer_read_subbuffer(metadata_stream, ctx);
+ if (ret_read < 0) {
+ if (ret_read != -EPERM) {
+ ERR("Kernel snapshot reading metadata subbuffer (ret: %ld)",
+ ret_read);
+ goto error;
+ }
+ /* ret_read is negative at this point so we will exit the loop. */
+ continue;
+ }
+ } while (ret_read >= 0);
+
+ if (use_relayd) {
+ close_relayd_stream(metadata_stream);
+ metadata_stream->net_seq_idx = (uint64_t) -1ULL;
+ } else {
+ ret = close(metadata_stream->out_fd);
+ if (ret < 0) {
+ PERROR("Kernel consumer snapshot metadata close out_fd");
+ /*
+ * Don't go on error here since the snapshot was successful at this
+ * point but somehow the close failed.
+ */
+ }
+ metadata_stream->out_fd = -1;
+ }
+
+ ret = 0;
+
+error:
+ 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) {