Fix: relayd: failure to read index entry or stream packet after clear
[lttng-tools.git] / src / bin / lttng-relayd / viewer-session.c
1 /*
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>
5 *
6 * SPDX-License-Identifier: GPL-2.0-only
7 *
8 */
9
10 #define _LGPL_SOURCE
11 #include <common/common.h>
12 #include <urcu/rculist.h>
13
14 #include "lttng-relayd.h"
15 #include "ctf-trace.h"
16 #include "session.h"
17 #include "viewer-session.h"
18 #include "viewer-stream.h"
19 #include "stream.h"
20
21 struct relay_viewer_session *viewer_session_create(void)
22 {
23 struct relay_viewer_session *vsession;
24
25 vsession = zmalloc(sizeof(*vsession));
26 if (!vsession) {
27 goto end;
28 }
29 CDS_INIT_LIST_HEAD(&vsession->session_list);
30 end:
31 return vsession;
32 }
33
34 int viewer_session_set_trace_chunk_copy(struct relay_viewer_session *vsession,
35 struct lttng_trace_chunk *relay_session_trace_chunk)
36 {
37 int ret = 0;
38 struct lttng_trace_chunk *viewer_chunk;
39
40 lttng_trace_chunk_put(vsession->current_trace_chunk);
41 vsession->current_trace_chunk = NULL;
42
43 DBG("Copying relay session's current trace chunk to the viewer session");
44 if (!relay_session_trace_chunk) {
45 goto end;
46 }
47
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;
56 end:
57 return ret;
58 }
59
60 /* The existence of session must be guaranteed by the caller. */
61 enum lttng_viewer_attach_return_code viewer_session_attach(
62 struct relay_viewer_session *vsession,
63 struct relay_session *session)
64 {
65 enum lttng_viewer_attach_return_code viewer_attach_status =
66 LTTNG_VIEWER_ATTACH_OK;
67
68 ASSERT_LOCKED(session->lock);
69
70 /* Will not fail, as per the ownership guarantee. */
71 if (!session_get(session)) {
72 viewer_attach_status = LTTNG_VIEWER_ATTACH_UNK;
73 goto end;
74 }
75 if (session->viewer_attached) {
76 viewer_attach_status = LTTNG_VIEWER_ATTACH_ALREADY;
77 } else {
78 int ret;
79
80 assert(!vsession->current_trace_chunk);
81 session->viewer_attached = true;
82
83 ret = viewer_session_set_trace_chunk_copy(vsession,
84 session->current_trace_chunk);
85 if (ret) {
86 /*
87 * The live protocol does not define a generic error
88 * value for the "attach" command. The "unknown"
89 * status is used so that the viewer may handle this
90 * failure as if the session didn't exist anymore.
91 */
92 DBG("Failed to create a viewer trace chunk from the current trace chunk of session \"%s\", returning LTTNG_VIEWER_ATTACH_UNK",
93 session->session_name);
94 viewer_attach_status = LTTNG_VIEWER_ATTACH_UNK;
95 }
96 }
97
98 if (viewer_attach_status == LTTNG_VIEWER_ATTACH_OK) {
99 pthread_mutex_lock(&vsession->session_list_lock);
100 /* Ownership is transfered to the list. */
101 cds_list_add_rcu(&session->viewer_session_node,
102 &vsession->session_list);
103 pthread_mutex_unlock(&vsession->session_list_lock);
104 } else {
105 /* Put our local ref. */
106 session_put(session);
107 }
108 end:
109 return viewer_attach_status;
110 }
111
112 /* The existence of session must be guaranteed by the caller. */
113 static int viewer_session_detach(struct relay_viewer_session *vsession,
114 struct relay_session *session)
115 {
116 int ret = 0;
117
118 pthread_mutex_lock(&session->lock);
119 if (!session->viewer_attached) {
120 ret = -1;
121 } else {
122 session->viewer_attached = false;
123 }
124
125 if (!ret) {
126 pthread_mutex_lock(&vsession->session_list_lock);
127 cds_list_del_rcu(&session->viewer_session_node);
128 pthread_mutex_unlock(&vsession->session_list_lock);
129 /* Release reference held by the list. */
130 session_put(session);
131 }
132 /* Safe since we know the session exists. */
133 pthread_mutex_unlock(&session->lock);
134 return ret;
135 }
136
137 void viewer_session_destroy(struct relay_viewer_session *vsession)
138 {
139 lttng_trace_chunk_put(vsession->current_trace_chunk);
140 free(vsession);
141 }
142
143 /*
144 * Release ownership of all the streams of one session and detach the viewer.
145 */
146 void viewer_session_close_one_session(struct relay_viewer_session *vsession,
147 struct relay_session *session)
148 {
149 struct lttng_ht_iter iter;
150 struct relay_viewer_stream *vstream;
151
152 /*
153 * TODO: improvement: create more efficient list of
154 * vstream per session.
155 */
156 cds_lfht_for_each_entry(viewer_streams_ht->ht, &iter.iter,
157 vstream, stream_n.node) {
158 if (!viewer_stream_get(vstream)) {
159 continue;
160 }
161 if (vstream->stream->trace->session != session) {
162 viewer_stream_put(vstream);
163 continue;
164 }
165 /* Put local reference. */
166 viewer_stream_put(vstream);
167 /*
168 * We have reached one of the viewer stream's lifetime
169 * end condition. This "put" will cause the proper
170 * teardown of the viewer stream.
171 */
172 viewer_stream_put(vstream);
173 }
174 lttng_trace_chunk_put(vsession->current_trace_chunk);
175 vsession->current_trace_chunk = NULL;
176 viewer_session_detach(vsession, session);
177 }
178
179 void viewer_session_close(struct relay_viewer_session *vsession)
180 {
181 struct relay_session *session;
182
183 rcu_read_lock();
184 cds_list_for_each_entry_rcu(session,
185 &vsession->session_list, viewer_session_node) {
186 viewer_session_close_one_session(vsession, session);
187 }
188 rcu_read_unlock();
189 }
190
191 /*
192 * Check if a connection is attached to a session.
193 * Return 1 if attached, 0 if not attached, a negative value on error.
194 */
195 int viewer_session_is_attached(struct relay_viewer_session *vsession,
196 struct relay_session *session)
197 {
198 struct relay_session *iter;
199 int found = 0;
200
201 pthread_mutex_lock(&session->lock);
202 if (!vsession) {
203 goto end;
204 }
205 if (!session->viewer_attached) {
206 goto end;
207 }
208 rcu_read_lock();
209 cds_list_for_each_entry_rcu(iter,
210 &vsession->session_list,
211 viewer_session_node) {
212 if (session == iter) {
213 found = 1;
214 goto end_rcu_unlock;
215 }
216 }
217 end_rcu_unlock:
218 rcu_read_unlock();
219 end:
220 pthread_mutex_unlock(&session->lock);
221 return found;
222 }
This page took 0.033604 seconds and 4 git commands to generate.