sessiond: document RCU locking assumption during channel metadata statedump
[lttng-tools.git] / src / bin / lttng-relayd / viewer-session.cpp
CommitLineData
7591bab1 1/*
ab5be9fa
MJ
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>
7591bab1 5 *
ab5be9fa 6 * SPDX-License-Identifier: GPL-2.0-only
7591bab1 7 *
7591bab1
MD
8 */
9
7591bab1 10#define _LGPL_SOURCE
c9e313bc 11#include <common/common.hpp>
7591bab1
MD
12#include <urcu/rculist.h>
13
c9e313bc
SM
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"
7591bab1
MD
20
21struct relay_viewer_session *viewer_session_create(void)
22{
23 struct relay_viewer_session *vsession;
24
ac497a37 25 vsession = (relay_viewer_session *) zmalloc(sizeof(*vsession));
7591bab1
MD
26 if (!vsession) {
27 goto end;
28 }
29 CDS_INIT_LIST_HEAD(&vsession->session_list);
30end:
31 return vsession;
32}
33
b3ab5004 34int viewer_session_set_trace_chunk_copy(struct relay_viewer_session *vsession,
664eef54
JG
35 struct lttng_trace_chunk *relay_session_trace_chunk)
36{
37 int ret = 0;
38 struct lttng_trace_chunk *viewer_chunk;
39
ad8bec24
JG
40 lttng_trace_chunk_put(vsession->current_trace_chunk);
41 vsession->current_trace_chunk = NULL;
664eef54
JG
42
43 DBG("Copying relay session's current trace chunk to the viewer session");
80516611
JG
44 if (!relay_session_trace_chunk) {
45 goto end;
46 }
47
664eef54
JG
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;
56end:
57 return ret;
58}
59
7591bab1 60/* The existence of session must be guaranteed by the caller. */
dbd6665b
JG
61enum lttng_viewer_attach_return_code viewer_session_attach(
62 struct relay_viewer_session *vsession,
7591bab1
MD
63 struct relay_session *session)
64{
dbd6665b
JG
65 enum lttng_viewer_attach_return_code viewer_attach_status =
66 LTTNG_VIEWER_ATTACH_OK;
7591bab1 67
c06fdd95
JG
68 ASSERT_LOCKED(session->lock);
69
7591bab1
MD
70 /* Will not fail, as per the ownership guarantee. */
71 if (!session_get(session)) {
dbd6665b 72 viewer_attach_status = LTTNG_VIEWER_ATTACH_UNK;
7591bab1
MD
73 goto end;
74 }
7591bab1 75 if (session->viewer_attached) {
dbd6665b 76 viewer_attach_status = LTTNG_VIEWER_ATTACH_ALREADY;
7591bab1 77 } else {
dbd6665b
JG
78 int ret;
79
7591bab1 80 session->viewer_attached = true;
dbd6665b 81
664eef54 82 ret = viewer_session_set_trace_chunk_copy(vsession,
dbd6665b
JG
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 }
7591bab1
MD
95 }
96
dbd6665b 97 if (viewer_attach_status == LTTNG_VIEWER_ATTACH_OK) {
7591bab1
MD
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 }
7591bab1 107end:
dbd6665b 108 return viewer_attach_status;
7591bab1
MD
109}
110
111/* The existence of session must be guaranteed by the caller. */
112static 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
136void viewer_session_destroy(struct relay_viewer_session *vsession)
137{
b66a15d1 138 lttng_trace_chunk_put(vsession->current_trace_chunk);
7591bab1
MD
139 free(vsession);
140}
141
d62023be
JD
142/*
143 * Release ownership of all the streams of one session and detach the viewer.
144 */
145void 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 }
b3feb91b
JG
173 lttng_trace_chunk_put(vsession->current_trace_chunk);
174 vsession->current_trace_chunk = NULL;
d62023be
JD
175 viewer_session_detach(vsession, session);
176}
177
7591bab1
MD
178void 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) {
d62023be 185 viewer_session_close_one_session(vsession, session);
7591bab1
MD
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 */
194int 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 }
216end_rcu_unlock:
217 rcu_read_unlock();
218end:
219 pthread_mutex_unlock(&session->lock);
220 return found;
221}
This page took 0.057556 seconds and 4 git commands to generate.