relayd: track the health thread's poll fd with fd-tracker
[lttng-tools.git] / src / bin / lttng-relayd / viewer-session.c
CommitLineData
7591bab1
MD
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
7591bab1
MD
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
31struct 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);
40end:
41 return vsession;
42}
43
b3ab5004 44int viewer_session_set_trace_chunk_copy(struct relay_viewer_session *vsession,
664eef54
JG
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;
62end:
63 return ret;
64}
65
7591bab1 66/* The existence of session must be guaranteed by the caller. */
dbd6665b
JG
67enum lttng_viewer_attach_return_code viewer_session_attach(
68 struct relay_viewer_session *vsession,
7591bab1
MD
69 struct relay_session *session)
70{
dbd6665b
JG
71 enum lttng_viewer_attach_return_code viewer_attach_status =
72 LTTNG_VIEWER_ATTACH_OK;
7591bab1 73
c06fdd95
JG
74 ASSERT_LOCKED(session->lock);
75
7591bab1
MD
76 /* Will not fail, as per the ownership guarantee. */
77 if (!session_get(session)) {
dbd6665b 78 viewer_attach_status = LTTNG_VIEWER_ATTACH_UNK;
7591bab1
MD
79 goto end;
80 }
7591bab1 81 if (session->viewer_attached) {
dbd6665b 82 viewer_attach_status = LTTNG_VIEWER_ATTACH_ALREADY;
7591bab1 83 } else {
dbd6665b
JG
84 int ret;
85
86 assert(session->current_trace_chunk);
87 assert(!vsession->current_trace_chunk);
7591bab1 88 session->viewer_attached = true;
dbd6665b 89
664eef54 90 ret = viewer_session_set_trace_chunk_copy(vsession,
dbd6665b
JG
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 }
7591bab1
MD
103 }
104
dbd6665b 105 if (viewer_attach_status == LTTNG_VIEWER_ATTACH_OK) {
7591bab1
MD
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 }
7591bab1 115end:
dbd6665b 116 return viewer_attach_status;
7591bab1
MD
117}
118
119/* The existence of session must be guaranteed by the caller. */
120static 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
144void viewer_session_destroy(struct relay_viewer_session *vsession)
145{
b66a15d1 146 lttng_trace_chunk_put(vsession->current_trace_chunk);
7591bab1
MD
147 free(vsession);
148}
149
d62023be
JD
150/*
151 * Release ownership of all the streams of one session and detach the viewer.
152 */
153void 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 }
b3feb91b
JG
181 lttng_trace_chunk_put(vsession->current_trace_chunk);
182 vsession->current_trace_chunk = NULL;
d62023be
JD
183 viewer_session_detach(vsession, session);
184}
185
7591bab1
MD
186void viewer_session_close(struct relay_viewer_session *vsession)
187{
188 struct relay_session *session;
189
190 rcu_read_lock();
191 cds_list_for_each_entry_rcu(session,
192 &vsession->session_list, viewer_session_node) {
d62023be 193 viewer_session_close_one_session(vsession, session);
7591bab1
MD
194 }
195 rcu_read_unlock();
196}
197
198/*
199 * Check if a connection is attached to a session.
200 * Return 1 if attached, 0 if not attached, a negative value on error.
201 */
202int viewer_session_is_attached(struct relay_viewer_session *vsession,
203 struct relay_session *session)
204{
205 struct relay_session *iter;
206 int found = 0;
207
208 pthread_mutex_lock(&session->lock);
209 if (!vsession) {
210 goto end;
211 }
212 if (!session->viewer_attached) {
213 goto end;
214 }
215 rcu_read_lock();
216 cds_list_for_each_entry_rcu(iter,
217 &vsession->session_list,
218 viewer_session_node) {
219 if (session == iter) {
220 found = 1;
221 goto end_rcu_unlock;
222 }
223 }
224end_rcu_unlock:
225 rcu_read_unlock();
226end:
227 pthread_mutex_unlock(&session->lock);
228 return found;
229}
This page took 0.043723 seconds and 4 git commands to generate.