+ try_stream_close(stream);
+ if (stream->is_metadata) {
+ struct relay_viewer_stream *vstream;
+
+ vstream = viewer_stream_get_by_id(stream->stream_handle);
+ if (vstream) {
+ if (vstream->metadata_sent == stream->metadata_received) {
+ /*
+ * Since all the metadata has been sent to the
+ * viewer and that we have a request to close
+ * its stream, we can safely teardown the
+ * corresponding metadata viewer stream.
+ */
+ viewer_stream_put(vstream);
+ }
+ /* Put local reference. */
+ viewer_stream_put(vstream);
+ }
+ }
+ stream_put(stream);
+ ret = 0;
+
+end:
+ memset(&reply, 0, sizeof(reply));
+ if (ret < 0) {
+ reply.ret_code = htobe32(LTTNG_ERR_UNK);
+ } else {
+ reply.ret_code = htobe32(LTTNG_OK);
+ }
+ send_ret = conn->sock->ops->sendmsg(conn->sock, &reply,
+ sizeof(struct lttcomm_relayd_generic_reply), 0);
+ if (send_ret < (ssize_t) sizeof(reply)) {
+ ERR("Failed to send \"close stream\" command reply (ret = %zd)",
+ send_ret);
+ ret = -1;
+ }
+
+end_no_session:
+ return ret;
+}
+
+/*
+ * relay_reset_metadata: reset a metadata stream
+ */
+static
+int relay_reset_metadata(const struct lttcomm_relayd_hdr *recv_hdr,
+ struct relay_connection *conn,
+ const struct lttng_buffer_view *payload)
+{
+ int ret;
+ ssize_t send_ret;
+ struct relay_session *session = conn->session;
+ struct lttcomm_relayd_reset_metadata stream_info;
+ struct lttcomm_relayd_generic_reply reply;
+ struct relay_stream *stream;
+
+ DBG("Reset metadata received");
+
+ if (!session || !conn->version_check_done) {
+ ERR("Trying to reset a metadata stream before version check");
+ ret = -1;
+ goto end_no_session;
+ }
+
+ if (payload->size < sizeof(stream_info)) {
+ ERR("Unexpected payload size in \"relay_reset_metadata\": expected >= %zu bytes, got %zu bytes",
+ sizeof(stream_info), payload->size);
+ ret = -1;
+ goto end_no_session;
+ }
+ memcpy(&stream_info, payload->data, sizeof(stream_info));
+ stream_info.stream_id = be64toh(stream_info.stream_id);
+ stream_info.version = be64toh(stream_info.version);
+
+ DBG("Update metadata to version %" PRIu64, stream_info.version);
+
+ /* Unsupported for live sessions for now. */
+ if (session->live_timer != 0) {
+ ret = -1;
+ goto end;
+ }
+
+ stream = stream_get_by_id(stream_info.stream_id);
+ if (!stream) {
+ ret = -1;
+ goto end;
+ }
+ pthread_mutex_lock(&stream->lock);
+ if (!stream->is_metadata) {
+ ret = -1;
+ goto end_unlock;
+ }