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