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