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