+ ERR("Relayd failed to send response.");
+ }
+
+ return ret;
+}
+
+/*
+ * Atomically check if new streams got added in one of the sessions attached
+ * and reset the flag to 0.
+ *
+ * Returns 1 if new streams got added, 0 if nothing changed, a negative value
+ * on error.
+ */
+static
+int check_new_streams(struct relay_connection *conn)
+{
+ struct relay_session *session;
+ unsigned long current_val;
+ int ret = 0;
+
+ if (!conn->viewer_session) {
+ goto end;
+ }
+ rcu_read_lock();
+ cds_list_for_each_entry_rcu(session,
+ &conn->viewer_session->session_list,
+ viewer_session_node) {
+ if (!session_get(session)) {
+ continue;
+ }
+ current_val = uatomic_cmpxchg(&session->new_streams, 1, 0);
+ ret = current_val;
+ session_put(session);
+ if (ret == 1) {
+ goto end;
+ }
+ }
+end:
+ rcu_read_unlock();
+ return ret;
+}
+
+/*
+ * Send viewer streams to the given socket. The ignore_sent_flag indicates if
+ * this function should ignore the sent flag or not.
+ *
+ * Return 0 on success or else a negative value.
+ */
+static
+ssize_t send_viewer_streams(struct lttcomm_sock *sock,
+ uint64_t session_id, unsigned int ignore_sent_flag)
+{
+ ssize_t ret;
+ struct lttng_ht_iter iter;
+ struct relay_viewer_stream *vstream;
+
+ rcu_read_lock();
+
+ cds_lfht_for_each_entry(viewer_streams_ht->ht, &iter.iter, vstream,
+ stream_n.node) {
+ struct ctf_trace *ctf_trace;
+ struct lttng_viewer_stream send_stream = {};
+
+ health_code_update();
+
+ if (!viewer_stream_get(vstream)) {
+ continue;
+ }
+
+ pthread_mutex_lock(&vstream->stream->lock);
+ /* Ignore if not the same session. */
+ if (vstream->stream->trace->session->id != session_id ||
+ (!ignore_sent_flag && vstream->sent_flag)) {
+ pthread_mutex_unlock(&vstream->stream->lock);
+ viewer_stream_put(vstream);
+ continue;
+ }
+
+ ctf_trace = vstream->stream->trace;
+ send_stream.id = htobe64(vstream->stream->stream_handle);
+ send_stream.ctf_trace_id = htobe64(ctf_trace->id);
+ send_stream.metadata_flag = htobe32(
+ vstream->stream->is_metadata);
+ if (lttng_strncpy(send_stream.path_name, vstream->path_name,
+ sizeof(send_stream.path_name))) {
+ pthread_mutex_unlock(&vstream->stream->lock);
+ viewer_stream_put(vstream);
+ ret = -1; /* Error. */
+ goto end_unlock;
+ }
+ if (lttng_strncpy(send_stream.channel_name,
+ vstream->channel_name,
+ sizeof(send_stream.channel_name))) {
+ pthread_mutex_unlock(&vstream->stream->lock);
+ viewer_stream_put(vstream);
+ ret = -1; /* Error. */
+ goto end_unlock;
+ }
+
+ DBG("Sending stream %" PRIu64 " to viewer",
+ vstream->stream->stream_handle);
+ vstream->sent_flag = 1;
+ pthread_mutex_unlock(&vstream->stream->lock);
+
+ ret = send_response(sock, &send_stream, sizeof(send_stream));
+ viewer_stream_put(vstream);
+ if (ret < 0) {
+ goto end_unlock;
+ }
+ }
+
+ ret = 0;
+
+end_unlock:
+ rcu_read_unlock();
+ return ret;
+}
+
+/*
+ * Create every viewer stream possible for the given session with the seek
+ * type. Three counters *can* be return which are in order the total amount of
+ * viewer stream of the session, the number of unsent stream and the number of
+ * stream created. Those counters can be NULL and thus will be ignored.
+ *
+ * session must be locked to ensure that we see either none or all initial
+ * streams for a session, but no intermediate state..
+ *
+ * Return 0 on success or else a negative value.
+ */
+static int make_viewer_streams(struct relay_session *relay_session,
+ struct relay_viewer_session *viewer_session,
+ enum lttng_viewer_seek seek_t,
+ uint32_t *nb_total,
+ uint32_t *nb_unsent,
+ uint32_t *nb_created,
+ bool *closed)
+{
+ int ret;
+ struct lttng_ht_iter iter;
+ struct ctf_trace *ctf_trace;
+ struct relay_stream *relay_stream = NULL;
+
+ assert(relay_session);
+ ASSERT_LOCKED(relay_session->lock);
+
+ if (relay_session->connection_closed) {
+ *closed = true;
+ }
+
+ /*
+ * Create viewer streams for relay streams that are ready to be
+ * used for a the given session id only.
+ */
+ rcu_read_lock();
+ cds_lfht_for_each_entry (relay_session->ctf_traces_ht->ht, &iter.iter,
+ ctf_trace, node.node) {
+ bool trace_has_metadata_stream = false;
+
+ health_code_update();
+
+ if (!ctf_trace_get(ctf_trace)) {
+ continue;
+ }
+
+ /*
+ * Iterate over all the streams of the trace to see if we have a
+ * metadata stream.
+ */
+ cds_list_for_each_entry_rcu(relay_stream,
+ &ctf_trace->stream_list, stream_node)
+ {
+ bool is_metadata_stream;
+
+ pthread_mutex_lock(&relay_stream->lock);
+ is_metadata_stream = relay_stream->is_metadata;
+ pthread_mutex_unlock(&relay_stream->lock);
+
+ if (is_metadata_stream) {
+ trace_has_metadata_stream = true;
+ break;
+ }
+ }
+
+ relay_stream = NULL;
+
+ /*
+ * If there is no metadata stream in this trace at the moment
+ * and we never sent one to the viewer, skip the trace. We
+ * accept that the viewer will not see this trace at all.
+ */
+ if (!trace_has_metadata_stream &&
+ !ctf_trace->metadata_stream_sent_to_viewer) {
+ ctf_trace_put(ctf_trace);
+ continue;
+ }
+
+ cds_list_for_each_entry_rcu(relay_stream,
+ &ctf_trace->stream_list, stream_node)
+ {
+ struct relay_viewer_stream *viewer_stream;
+
+ if (!stream_get(relay_stream)) {
+ continue;
+ }
+
+ pthread_mutex_lock(&relay_stream->lock);
+ /*
+ * stream published is protected by the session lock.
+ */
+ if (!relay_stream->published) {
+ goto next;
+ }
+ viewer_stream = viewer_stream_get_by_id(
+ relay_stream->stream_handle);
+ if (!viewer_stream) {
+ struct lttng_trace_chunk *viewer_stream_trace_chunk = NULL;
+
+ /*
+ * Save that we sent the metadata stream to the
+ * viewer. So that we know what trace the viewer
+ * is aware of.
+ */
+ if (relay_stream->is_metadata) {
+ ctf_trace->metadata_stream_sent_to_viewer = true;
+ }
+
+ /*
+ * If a rotation is ongoing, use a copy of the
+ * relay stream's chunk to ensure the stream
+ * files exist.
+ *
+ * Otherwise, the viewer session's current trace
+ * chunk can be used safely.
+ */
+ if ((relay_stream->ongoing_rotation.is_set ||
+ relay_session->ongoing_rotation) &&
+ relay_stream->trace_chunk) {
+ viewer_stream_trace_chunk = lttng_trace_chunk_copy(
+ relay_stream->trace_chunk);
+ if (!viewer_stream_trace_chunk) {
+ ret = -1;
+ ctf_trace_put(ctf_trace);
+ goto error_unlock;
+ }
+ } else {
+ bool reference_acquired;
+
+ /*
+ * Transition the viewer session into the newest trace chunk available.
+ */
+ if (!lttng_trace_chunk_ids_equal(viewer_session->current_trace_chunk,
+ relay_stream->trace_chunk)) {
+
+ ret = viewer_session_set_trace_chunk_copy(
+ viewer_session,
+ relay_stream->trace_chunk);
+ if (ret) {
+ ret = -1;
+ ctf_trace_put(ctf_trace);
+ goto error_unlock;
+ }
+ }
+
+ reference_acquired = lttng_trace_chunk_get(
+ viewer_session->current_trace_chunk);
+ assert(reference_acquired);
+ viewer_stream_trace_chunk =
+ viewer_session->current_trace_chunk;
+ }
+
+ viewer_stream = viewer_stream_create(
+ relay_stream,
+ viewer_stream_trace_chunk,
+ seek_t);
+ lttng_trace_chunk_put(viewer_stream_trace_chunk);
+ viewer_stream_trace_chunk = NULL;
+ if (!viewer_stream) {
+ ret = -1;
+ ctf_trace_put(ctf_trace);
+ goto error_unlock;
+ }
+
+ if (nb_created) {
+ /* Update number of created stream counter. */
+ (*nb_created)++;
+ }
+ /*
+ * Ensure a self-reference is preserved even
+ * after we have put our local reference.
+ */
+ if (!viewer_stream_get(viewer_stream)) {
+ ERR("Unable to get self-reference on viewer stream, logic error.");
+ abort();
+ }
+ } else {
+ if (!viewer_stream->sent_flag && nb_unsent) {
+ /* Update number of unsent stream counter. */
+ (*nb_unsent)++;
+ }
+ }
+ /* Update number of total stream counter. */
+ if (nb_total) {
+ if (relay_stream->is_metadata) {
+ if (!relay_stream->closed ||
+ relay_stream->metadata_received >
+ viewer_stream->metadata_sent) {
+ (*nb_total)++;
+ }
+ } else {
+ if (!relay_stream->closed ||
+ !(((int64_t)(relay_stream->prev_data_seq -
+ relay_stream->last_net_seq_num)) >=
+ 0)) {
+ (*nb_total)++;
+ }
+ }
+ }
+ /* Put local reference. */
+ viewer_stream_put(viewer_stream);
+ next:
+ pthread_mutex_unlock(&relay_stream->lock);
+ stream_put(relay_stream);
+ }
+ relay_stream = NULL;
+ ctf_trace_put(ctf_trace);