+ 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;
+ }
+ cds_list_for_each_entry(session,
+ &conn->viewer_session->sessions_head,
+ viewer_session_list) {
+ current_val = uatomic_cmpxchg(&session->new_streams, 1, 0);
+ ret = current_val;
+ if (ret == 1) {
+ goto end;
+ }
+ }
+
+end:
+ 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,
+ struct relay_session *session, unsigned int ignore_sent_flag)
+{
+ ssize_t ret;
+ struct lttng_viewer_stream send_stream;
+ struct lttng_ht_iter iter;
+ struct relay_viewer_stream *vstream;
+
+ assert(session);
+
+ rcu_read_lock();
+
+ cds_lfht_for_each_entry(viewer_streams_ht->ht, &iter.iter, vstream,
+ stream_n.node) {
+ struct ctf_trace *ctf_trace;
+
+ health_code_update();
+
+ /* Ignore if not the same session. */
+ if (vstream->session_id != session->id ||
+ (!ignore_sent_flag && vstream->sent_flag)) {
+ continue;
+ }
+
+ ctf_trace = ctf_trace_find_by_path(session->ctf_traces_ht,
+ vstream->path_name);
+ assert(ctf_trace);
+
+ send_stream.id = htobe64(vstream->stream_handle);
+ send_stream.ctf_trace_id = htobe64(ctf_trace->id);
+ send_stream.metadata_flag = htobe32(vstream->metadata_flag);
+ strncpy(send_stream.path_name, vstream->path_name,
+ sizeof(send_stream.path_name));
+ strncpy(send_stream.channel_name, vstream->channel_name,
+ sizeof(send_stream.channel_name));
+
+ DBG("Sending stream %" PRIu64 " to viewer", vstream->stream_handle);
+ ret = send_response(sock, &send_stream, sizeof(send_stream));
+ if (ret < 0) {
+ goto end_unlock;
+ }
+ vstream->sent_flag = 1;
+ }
+
+ 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.
+ *
+ * Return 0 on success or else a negative value.
+ */
+static
+int make_viewer_streams(struct relay_session *session,
+ enum lttng_viewer_seek seek_t, uint32_t *nb_total, uint32_t *nb_unsent,
+ uint32_t *nb_created)
+{
+ int ret;
+ struct lttng_ht_iter iter;
+ struct ctf_trace *ctf_trace;
+
+ assert(session);
+
+ /*
+ * This is to make sure we create viewer streams for a full received
+ * channel. For instance, if we have 8 streams for a channel that are
+ * concurrently being flagged ready, we can end up creating just a subset
+ * of the 8 streams (the ones that are flagged). This lock avoids this
+ * limbo state.
+ */
+ pthread_mutex_lock(&session->viewer_ready_lock);
+
+ /*
+ * 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(session->ctf_traces_ht->ht, &iter.iter, ctf_trace,
+ node.node) {
+ struct relay_stream *stream;
+
+ health_code_update();
+
+ if (ctf_trace->invalid_flag) {
+ continue;
+ }
+
+ cds_list_for_each_entry(stream, &ctf_trace->stream_list, trace_list) {
+ struct relay_viewer_stream *vstream;
+
+ if (!stream->viewer_ready) {
+ continue;
+ }
+
+ vstream = viewer_stream_find_by_id(stream->stream_handle);
+ if (!vstream) {
+ vstream = viewer_stream_create(stream, seek_t, ctf_trace);
+ if (!vstream) {
+ ret = -1;
+ goto error_unlock;
+ }
+ /* Acquire reference to ctf_trace. */
+ ctf_trace_get_ref(ctf_trace);
+
+ if (nb_created) {
+ /* Update number of created stream counter. */
+ (*nb_created)++;
+ }
+ } else if (!vstream->sent_flag && nb_unsent) {
+ /* Update number of unsent stream counter. */
+ (*nb_unsent)++;
+ }
+ /* Update number of total stream counter. */
+ if (nb_total) {
+ (*nb_total)++;
+ }
+ }