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