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