Add a basic .clang-tidy file and fix typedef warnings
[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 "ctf-trace.hpp"
28ab034a 12#include "lttng-relayd.hpp"
c9e313bc 13#include "session.hpp"
28ab034a 14#include "stream.hpp"
c9e313bc
SM
15#include "viewer-session.hpp"
16#include "viewer-stream.hpp"
28ab034a
JG
17
18#include <common/common.hpp>
19
20#include <urcu/rculist.h>
7591bab1
MD
21
22struct relay_viewer_session *viewer_session_create(void)
23{
24 struct relay_viewer_session *vsession;
25
64803277 26 vsession = zmalloc<relay_viewer_session>();
7591bab1
MD
27 if (!vsession) {
28 goto end;
29 }
30 CDS_INIT_LIST_HEAD(&vsession->session_list);
31end:
32 return vsession;
33}
34
b3ab5004 35int viewer_session_set_trace_chunk_copy(struct relay_viewer_session *vsession,
28ab034a 36 struct lttng_trace_chunk *relay_session_trace_chunk)
664eef54
JG
37{
38 int ret = 0;
39 struct lttng_trace_chunk *viewer_chunk;
40
ad8bec24
JG
41 lttng_trace_chunk_put(vsession->current_trace_chunk);
42 vsession->current_trace_chunk = NULL;
664eef54
JG
43
44 DBG("Copying relay session's current trace chunk to the viewer session");
80516611
JG
45 if (!relay_session_trace_chunk) {
46 goto end;
47 }
48
664eef54
JG
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;
57end:
58 return ret;
59}
60
7591bab1 61/* The existence of session must be guaranteed by the caller. */
28ab034a
JG
62enum lttng_viewer_attach_return_code viewer_session_attach(struct relay_viewer_session *vsession,
63 struct relay_session *session)
7591bab1 64{
28ab034a 65 enum lttng_viewer_attach_return_code viewer_attach_status = LTTNG_VIEWER_ATTACH_OK;
7591bab1 66
c06fdd95
JG
67 ASSERT_LOCKED(session->lock);
68
7591bab1
MD
69 /* Will not fail, as per the ownership guarantee. */
70 if (!session_get(session)) {
dbd6665b 71 viewer_attach_status = LTTNG_VIEWER_ATTACH_UNK;
7591bab1
MD
72 goto end;
73 }
7591bab1 74 if (session->viewer_attached) {
dbd6665b 75 viewer_attach_status = LTTNG_VIEWER_ATTACH_ALREADY;
7591bab1 76 } else {
dbd6665b
JG
77 int ret;
78
7591bab1 79 session->viewer_attached = true;
dbd6665b 80
28ab034a 81 ret = viewer_session_set_trace_chunk_copy(vsession, session->current_trace_chunk);
dbd6665b
JG
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",
28ab034a 90 session->session_name);
dbd6665b
JG
91 viewer_attach_status = LTTNG_VIEWER_ATTACH_UNK;
92 }
7591bab1
MD
93 }
94
dbd6665b 95 if (viewer_attach_status == LTTNG_VIEWER_ATTACH_OK) {
7591bab1
MD
96 pthread_mutex_lock(&vsession->session_list_lock);
97 /* Ownership is transfered to the list. */
28ab034a 98 cds_list_add_rcu(&session->viewer_session_node, &vsession->session_list);
7591bab1
MD
99 pthread_mutex_unlock(&vsession->session_list_lock);
100 } else {
101 /* Put our local ref. */
102 session_put(session);
103 }
7591bab1 104end:
dbd6665b 105 return viewer_attach_status;
7591bab1
MD
106}
107
108/* The existence of session must be guaranteed by the caller. */
109static int viewer_session_detach(struct relay_viewer_session *vsession,
28ab034a 110 struct relay_session *session)
7591bab1
MD
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
133void viewer_session_destroy(struct relay_viewer_session *vsession)
134{
b66a15d1 135 lttng_trace_chunk_put(vsession->current_trace_chunk);
7591bab1
MD
136 free(vsession);
137}
138
d62023be
JD
139/*
140 * Release ownership of all the streams of one session and detach the viewer.
141 */
142void viewer_session_close_one_session(struct relay_viewer_session *vsession,
28ab034a 143 struct relay_session *session)
d62023be
JD
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 */
28ab034a 152 cds_lfht_for_each_entry (viewer_streams_ht->ht, &iter.iter, vstream, stream_n.node) {
d62023be
JD
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 }
b3feb91b
JG
169 lttng_trace_chunk_put(vsession->current_trace_chunk);
170 vsession->current_trace_chunk = NULL;
d62023be
JD
171 viewer_session_detach(vsession, session);
172}
173
7591bab1
MD
174void viewer_session_close(struct relay_viewer_session *vsession)
175{
176 struct relay_session *session;
177
178 rcu_read_lock();
28ab034a
JG
179 cds_list_for_each_entry_rcu(session, &vsession->session_list, viewer_session_node)
180 {
d62023be 181 viewer_session_close_one_session(vsession, session);
7591bab1
MD
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 */
28ab034a 190int viewer_session_is_attached(struct relay_viewer_session *vsession, struct relay_session *session)
7591bab1
MD
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();
28ab034a
JG
203 cds_list_for_each_entry_rcu(iter, &vsession->session_list, viewer_session_node)
204 {
7591bab1
MD
205 if (session == iter) {
206 found = 1;
207 goto end_rcu_unlock;
208 }
209 }
210end_rcu_unlock:
211 rcu_read_unlock();
212end:
213 pthread_mutex_unlock(&session->lock);
214 return found;
215}
This page took 0.064897 seconds and 4 git commands to generate.