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