| 1 | /* |
| 2 | * Copyright (C) 2013 Julien Desfossez <jdesfossez@efficios.com> |
| 3 | * Copyright (C) 2013 David Goulet <dgoulet@efficios.com> |
| 4 | * Copyright (C) 2015 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 5 | * |
| 6 | * SPDX-License-Identifier: GPL-2.0-only |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | #define _LGPL_SOURCE |
| 11 | #include "ctf-trace.hpp" |
| 12 | #include "lttng-relayd.hpp" |
| 13 | #include "session.hpp" |
| 14 | #include "stream.hpp" |
| 15 | #include "viewer-session.hpp" |
| 16 | #include "viewer-stream.hpp" |
| 17 | |
| 18 | #include <common/common.hpp> |
| 19 | #include <common/urcu.hpp> |
| 20 | |
| 21 | #include <urcu/rculist.h> |
| 22 | |
| 23 | struct relay_viewer_session *viewer_session_create() |
| 24 | { |
| 25 | struct relay_viewer_session *vsession; |
| 26 | |
| 27 | vsession = zmalloc<relay_viewer_session>(); |
| 28 | if (!vsession) { |
| 29 | goto end; |
| 30 | } |
| 31 | CDS_INIT_LIST_HEAD(&vsession->session_list); |
| 32 | end: |
| 33 | return vsession; |
| 34 | } |
| 35 | |
| 36 | int viewer_session_set_trace_chunk_copy(struct relay_viewer_session *vsession, |
| 37 | struct lttng_trace_chunk *relay_session_trace_chunk) |
| 38 | { |
| 39 | int ret = 0; |
| 40 | struct lttng_trace_chunk *viewer_chunk; |
| 41 | |
| 42 | lttng_trace_chunk_put(vsession->current_trace_chunk); |
| 43 | vsession->current_trace_chunk = nullptr; |
| 44 | |
| 45 | DBG("Copying relay session's current trace chunk to the viewer session"); |
| 46 | if (!relay_session_trace_chunk) { |
| 47 | goto end; |
| 48 | } |
| 49 | |
| 50 | viewer_chunk = lttng_trace_chunk_copy(relay_session_trace_chunk); |
| 51 | if (!viewer_chunk) { |
| 52 | ERR("Failed to create a viewer trace chunk from the relay session's current chunk"); |
| 53 | ret = -1; |
| 54 | goto end; |
| 55 | } |
| 56 | |
| 57 | vsession->current_trace_chunk = viewer_chunk; |
| 58 | end: |
| 59 | return ret; |
| 60 | } |
| 61 | |
| 62 | /* The existence of session must be guaranteed by the caller. */ |
| 63 | enum lttng_viewer_attach_return_code viewer_session_attach(struct relay_viewer_session *vsession, |
| 64 | struct relay_session *session) |
| 65 | { |
| 66 | enum lttng_viewer_attach_return_code viewer_attach_status = LTTNG_VIEWER_ATTACH_OK; |
| 67 | |
| 68 | ASSERT_LOCKED(session->lock); |
| 69 | |
| 70 | /* Will not fail, as per the ownership guarantee. */ |
| 71 | if (!session_get(session)) { |
| 72 | viewer_attach_status = LTTNG_VIEWER_ATTACH_UNK; |
| 73 | goto end; |
| 74 | } |
| 75 | if (session->viewer_attached) { |
| 76 | viewer_attach_status = LTTNG_VIEWER_ATTACH_ALREADY; |
| 77 | } else { |
| 78 | int ret; |
| 79 | |
| 80 | session->viewer_attached = true; |
| 81 | |
| 82 | ret = viewer_session_set_trace_chunk_copy(vsession, session->current_trace_chunk); |
| 83 | if (ret) { |
| 84 | /* |
| 85 | * The live protocol does not define a generic error |
| 86 | * value for the "attach" command. The "unknown" |
| 87 | * status is used so that the viewer may handle this |
| 88 | * failure as if the session didn't exist anymore. |
| 89 | */ |
| 90 | DBG("Failed to create a viewer trace chunk from the current trace chunk of session \"%s\", returning LTTNG_VIEWER_ATTACH_UNK", |
| 91 | session->session_name); |
| 92 | viewer_attach_status = LTTNG_VIEWER_ATTACH_UNK; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | if (viewer_attach_status == LTTNG_VIEWER_ATTACH_OK) { |
| 97 | pthread_mutex_lock(&vsession->session_list_lock); |
| 98 | /* Ownership is transfered to the list. */ |
| 99 | cds_list_add_rcu(&session->viewer_session_node, &vsession->session_list); |
| 100 | pthread_mutex_unlock(&vsession->session_list_lock); |
| 101 | } else { |
| 102 | /* Put our local ref. */ |
| 103 | session_put(session); |
| 104 | } |
| 105 | end: |
| 106 | return viewer_attach_status; |
| 107 | } |
| 108 | |
| 109 | /* The existence of session must be guaranteed by the caller. */ |
| 110 | static int viewer_session_detach(struct relay_viewer_session *vsession, |
| 111 | struct relay_session *session) |
| 112 | { |
| 113 | int ret = 0; |
| 114 | |
| 115 | pthread_mutex_lock(&session->lock); |
| 116 | if (!session->viewer_attached) { |
| 117 | ret = -1; |
| 118 | } else { |
| 119 | session->viewer_attached = false; |
| 120 | } |
| 121 | |
| 122 | if (!ret) { |
| 123 | pthread_mutex_lock(&vsession->session_list_lock); |
| 124 | cds_list_del_rcu(&session->viewer_session_node); |
| 125 | pthread_mutex_unlock(&vsession->session_list_lock); |
| 126 | /* Release reference held by the list. */ |
| 127 | session_put(session); |
| 128 | } |
| 129 | /* Safe since we know the session exists. */ |
| 130 | pthread_mutex_unlock(&session->lock); |
| 131 | return ret; |
| 132 | } |
| 133 | |
| 134 | void viewer_session_destroy(struct relay_viewer_session *vsession) |
| 135 | { |
| 136 | lttng_trace_chunk_put(vsession->current_trace_chunk); |
| 137 | free(vsession); |
| 138 | } |
| 139 | |
| 140 | /* |
| 141 | * Release ownership of all the streams of one session and detach the viewer. |
| 142 | */ |
| 143 | void viewer_session_close_one_session(struct relay_viewer_session *vsession, |
| 144 | struct relay_session *session) |
| 145 | { |
| 146 | struct lttng_ht_iter iter; |
| 147 | struct relay_viewer_stream *vstream; |
| 148 | |
| 149 | /* |
| 150 | * TODO: improvement: create more efficient list of |
| 151 | * vstream per session. |
| 152 | */ |
| 153 | { |
| 154 | lttng::urcu::read_lock_guard read_guard; |
| 155 | |
| 156 | cds_lfht_for_each_entry ( |
| 157 | viewer_streams_ht->ht, &iter.iter, vstream, stream_n.node) { |
| 158 | if (!viewer_stream_get(vstream)) { |
| 159 | continue; |
| 160 | } |
| 161 | if (vstream->stream->trace->session != session) { |
| 162 | viewer_stream_put(vstream); |
| 163 | continue; |
| 164 | } |
| 165 | /* Put local reference. */ |
| 166 | viewer_stream_put(vstream); |
| 167 | /* |
| 168 | * We have reached one of the viewer stream's lifetime |
| 169 | * end condition. This "put" will cause the proper |
| 170 | * teardown of the viewer stream. |
| 171 | */ |
| 172 | viewer_stream_put(vstream); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | lttng_trace_chunk_put(vsession->current_trace_chunk); |
| 177 | vsession->current_trace_chunk = nullptr; |
| 178 | viewer_session_detach(vsession, session); |
| 179 | } |
| 180 | |
| 181 | void viewer_session_close(struct relay_viewer_session *vsession) |
| 182 | { |
| 183 | struct relay_session *session; |
| 184 | |
| 185 | { |
| 186 | lttng::urcu::read_lock_guard read_lock; |
| 187 | |
| 188 | cds_list_for_each_entry_rcu(session, &vsession->session_list, viewer_session_node) |
| 189 | { |
| 190 | viewer_session_close_one_session(vsession, session); |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | /* |
| 196 | * Check if a connection is attached to a session. |
| 197 | * Return 1 if attached, 0 if not attached, a negative value on error. |
| 198 | */ |
| 199 | int viewer_session_is_attached(struct relay_viewer_session *vsession, struct relay_session *session) |
| 200 | { |
| 201 | struct relay_session *iter; |
| 202 | int found = 0; |
| 203 | |
| 204 | pthread_mutex_lock(&session->lock); |
| 205 | if (!vsession) { |
| 206 | goto end; |
| 207 | } |
| 208 | if (!session->viewer_attached) { |
| 209 | goto end; |
| 210 | } |
| 211 | |
| 212 | { |
| 213 | lttng::urcu::read_lock_guard read_lock; |
| 214 | cds_list_for_each_entry_rcu(iter, &vsession->session_list, viewer_session_node) |
| 215 | { |
| 216 | if (session == iter) { |
| 217 | found = 1; |
| 218 | break; |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | end: |
| 224 | pthread_mutex_unlock(&session->lock); |
| 225 | return found; |
| 226 | } |