Fix: relayd: ressource leaks on viewer_stream_create error
[lttng-tools.git] / src / bin / lttng-relayd / viewer-stream.c
CommitLineData
2f8f53af 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>
2f8f53af 5 *
ab5be9fa 6 * SPDX-License-Identifier: GPL-2.0-only
2f8f53af 7 *
2f8f53af
DG
8 */
9
6c1c0768 10#define _LGPL_SOURCE
2f8f53af
DG
11#include <common/common.h>
12#include <common/index/index.h>
f5436bfc 13#include <common/compat/string.h>
b0d240a2
MD
14#include <common/utils.h>
15#include <sys/types.h>
16#include <sys/stat.h>
17#include <fcntl.h>
2f8f53af
DG
18
19#include "lttng-relayd.h"
20#include "viewer-stream.h"
21
0f31e182 22static void viewer_stream_release_composite_objects(struct relay_viewer_stream *vstream)
2f8f53af 23{
0f31e182
MD
24 if (vstream->stream_file.handle) {
25 fs_handle_close(vstream->stream_file.handle);
26 vstream->stream_file.handle = NULL;
27 }
28 if (vstream->index_file) {
29 lttng_index_file_put(vstream->index_file);
30 vstream->index_file = NULL;
31 }
32 if (vstream->stream) {
33 stream_put(vstream->stream);
34 vstream->stream = NULL;
35 }
eca8e6d9 36 lttng_trace_chunk_put(vstream->stream_file.trace_chunk);
0f31e182
MD
37 vstream->stream_file.trace_chunk = NULL;
38}
39
40static void viewer_stream_destroy(struct relay_viewer_stream *vstream)
41{
7591bab1
MD
42 free(vstream->path_name);
43 free(vstream->channel_name);
44 free(vstream);
2f8f53af
DG
45}
46
7591bab1 47static void viewer_stream_destroy_rcu(struct rcu_head *head)
2f8f53af 48{
7591bab1 49 struct relay_viewer_stream *vstream =
2f8f53af
DG
50 caa_container_of(head, struct relay_viewer_stream, rcu_node);
51
7591bab1 52 viewer_stream_destroy(vstream);
2f8f53af
DG
53}
54
e69067f2 55/* Relay stream's lock must be held by the caller. */
2f8f53af 56struct relay_viewer_stream *viewer_stream_create(struct relay_stream *stream,
e69067f2 57 struct lttng_trace_chunk *trace_chunk,
7591bab1 58 enum lttng_viewer_seek seek_t)
2f8f53af 59{
ebb29c10 60 struct relay_viewer_stream *vstream = NULL;
ebb29c10 61
e69067f2 62 ASSERT_LOCKED(stream->lock);
2f8f53af 63
2f8f53af
DG
64 vstream = zmalloc(sizeof(*vstream));
65 if (!vstream) {
66 PERROR("relay viewer stream zmalloc");
67 goto error;
68 }
69
eca8e6d9
JG
70 if (trace_chunk) {
71 const bool acquired_reference = lttng_trace_chunk_get(
72 trace_chunk);
73
74 assert(acquired_reference);
75 }
76
e69067f2 77 vstream->stream_file.trace_chunk = trace_chunk;
f5436bfc 78 vstream->path_name = lttng_strndup(stream->path_name, LTTNG_VIEWER_PATH_MAX);
b272577e
MD
79 if (vstream->path_name == NULL) {
80 PERROR("relay viewer path_name alloc");
81 goto error;
82 }
f5436bfc 83 vstream->channel_name = lttng_strndup(stream->channel_name,
2f8f53af 84 LTTNG_VIEWER_NAME_MAX);
b272577e
MD
85 if (vstream->channel_name == NULL) {
86 PERROR("relay viewer channel_name alloc");
87 goto error;
88 }
2f8f53af 89
a44ca2ca
MD
90 if (!stream_get(stream)) {
91 ERR("Cannot get stream");
92 goto error;
93 }
94 vstream->stream = stream;
95
a44ca2ca
MD
96 if (stream->is_metadata && stream->trace->viewer_metadata_stream) {
97 ERR("Cannot attach viewer metadata stream to trace (busy).");
a1c672ba 98 goto error;
a44ca2ca
MD
99 }
100
2f8f53af 101 switch (seek_t) {
c4e361a4 102 case LTTNG_VIEWER_SEEK_BEGINNING:
a44ca2ca
MD
103 {
104 uint64_t seq_tail = tracefile_array_get_seq_tail(stream->tfa);
105
106 if (seq_tail == -1ULL) {
107 /*
108 * Tail may not be initialized yet. Nonetheless, we know
109 * we want to send the first index once it becomes
110 * available.
111 */
112 seq_tail = 0;
113 }
114 vstream->current_tracefile_id =
115 tracefile_array_get_file_index_tail(stream->tfa);
116 vstream->index_sent_seqcount = seq_tail;
2f8f53af 117 break;
a44ca2ca 118 }
c4e361a4 119 case LTTNG_VIEWER_SEEK_LAST:
a44ca2ca 120 vstream->current_tracefile_id =
78118e3b 121 tracefile_array_get_read_file_index_head(stream->tfa);
a44ca2ca
MD
122 /*
123 * We seek at the very end of each stream, awaiting for
124 * a future packet to eventually come in.
125 *
126 * We don't need to check the head position for -1ULL since the
127 * increment will set it to 0.
128 */
129 vstream->index_sent_seqcount =
130 tracefile_array_get_seq_head(stream->tfa) + 1;
2f8f53af
DG
131 break;
132 default:
a1c672ba 133 goto error;
2f8f53af 134 }
2f8f53af 135
2f8f53af 136 /*
a44ca2ca
MD
137 * If we never received an index for the current stream, delay
138 * the opening of the index, otherwise open it right now.
2f8f53af 139 */
b0d240a2 140 if (stream->index_file == NULL) {
f8f3885c 141 vstream->index_file = NULL;
2f8f53af 142 } else {
ebb29c10
JG
143 const uint32_t connection_major = stream->trace->session->major;
144 const uint32_t connection_minor = stream->trace->session->minor;
3ff5c5db 145 enum lttng_trace_chunk_status chunk_status;
ebb29c10 146
3ff5c5db 147 chunk_status = lttng_index_file_create_from_trace_chunk_read_only(
b66a15d1
JG
148 vstream->stream_file.trace_chunk,
149 stream->path_name,
ebb29c10
JG
150 stream->channel_name, stream->tracefile_size,
151 vstream->current_tracefile_id,
152 lttng_to_index_major(connection_major,
153 connection_minor),
154 lttng_to_index_minor(connection_major,
3ff5c5db
MD
155 connection_minor),
156 true, &vstream->index_file);
157 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
158 if (chunk_status == LTTNG_TRACE_CHUNK_STATUS_NO_FILE) {
159 vstream->index_file = NULL;
160 } else {
a1c672ba 161 goto error;
3ff5c5db 162 }
2f8f53af 163 }
2f8f53af
DG
164 }
165
b0d240a2
MD
166 /*
167 * If we never received a data file for the current stream, delay the
168 * opening, otherwise open it right now.
169 */
8bb66c3c
JG
170 if (stream->file) {
171 int ret;
b0d240a2
MD
172 char file_path[LTTNG_PATH_MAX];
173 enum lttng_trace_chunk_status status;
174
175 ret = utils_stream_file_path(stream->path_name,
176 stream->channel_name, stream->tracefile_size,
177 vstream->current_tracefile_id, NULL, file_path,
178 sizeof(file_path));
179 if (ret < 0) {
a1c672ba 180 goto error;
b0d240a2
MD
181 }
182
8bb66c3c
JG
183 status = lttng_trace_chunk_open_fs_handle(
184 vstream->stream_file.trace_chunk, file_path,
185 O_RDONLY, 0, &vstream->stream_file.handle,
186 true);
b0d240a2 187 if (status != LTTNG_TRACE_CHUNK_STATUS_OK) {
a1c672ba 188 goto error;
b0d240a2 189 }
b0d240a2
MD
190 }
191
f8f3885c 192 if (seek_t == LTTNG_VIEWER_SEEK_LAST && vstream->index_file) {
2f8f53af
DG
193 off_t lseek_ret;
194
8bb66c3c
JG
195 lseek_ret = fs_handle_seek(
196 vstream->index_file->file, 0, SEEK_END);
2f8f53af 197 if (lseek_ret < 0) {
a1c672ba 198 goto error;
2f8f53af 199 }
7591bab1 200 }
7591bab1
MD
201 if (stream->is_metadata) {
202 rcu_assign_pointer(stream->trace->viewer_metadata_stream,
203 vstream);
2f8f53af
DG
204 }
205
eca8e6d9
JG
206 vstream->last_seen_rotation_count = stream->completed_rotation_count;
207
7591bab1
MD
208 /* Globally visible after the add unique. */
209 lttng_ht_node_init_u64(&vstream->stream_n, stream->stream_handle);
7591bab1 210 urcu_ref_init(&vstream->ref);
c51b2177 211 lttng_ht_add_unique_u64(viewer_streams_ht, &vstream->stream_n);
7591bab1 212
2f8f53af
DG
213 return vstream;
214
215error:
216 if (vstream) {
0f31e182
MD
217 /* Not using `put` since vstream is assumed to be published. */
218 viewer_stream_release_composite_objects(vstream);
7591bab1 219 viewer_stream_destroy(vstream);
2f8f53af
DG
220 }
221 return NULL;
222}
223
7591bab1 224static void viewer_stream_unpublish(struct relay_viewer_stream *vstream)
2f8f53af
DG
225{
226 int ret;
227 struct lttng_ht_iter iter;
228
7591bab1 229 iter.iter.node = &vstream->stream_n.node;
2f8f53af
DG
230 ret = lttng_ht_del(viewer_streams_ht, &iter);
231 assert(!ret);
232}
233
7591bab1 234static void viewer_stream_release(struct urcu_ref *ref)
2f8f53af 235{
7591bab1
MD
236 struct relay_viewer_stream *vstream = caa_container_of(ref,
237 struct relay_viewer_stream, ref);
2a174661 238
7591bab1
MD
239 if (vstream->stream->is_metadata) {
240 rcu_assign_pointer(vstream->stream->trace->viewer_metadata_stream, NULL);
2a174661 241 }
7591bab1 242 viewer_stream_unpublish(vstream);
0f31e182 243 viewer_stream_release_composite_objects(vstream);
7591bab1
MD
244 call_rcu(&vstream->rcu_node, viewer_stream_destroy_rcu);
245}
246
247/* Must be called with RCU read-side lock held. */
248bool viewer_stream_get(struct relay_viewer_stream *vstream)
249{
ce4d4083 250 return urcu_ref_get_unless_zero(&vstream->ref);
2f8f53af
DG
251}
252
253/*
7591bab1 254 * Get viewer stream by id.
2f8f53af 255 *
7591bab1 256 * Return viewer stream if found else NULL.
2f8f53af 257 */
7591bab1 258struct relay_viewer_stream *viewer_stream_get_by_id(uint64_t id)
2f8f53af
DG
259{
260 struct lttng_ht_node_u64 *node;
261 struct lttng_ht_iter iter;
7591bab1 262 struct relay_viewer_stream *vstream = NULL;
2f8f53af 263
7591bab1 264 rcu_read_lock();
2f8f53af
DG
265 lttng_ht_lookup(viewer_streams_ht, &id, &iter);
266 node = lttng_ht_iter_get_node_u64(&iter);
267 if (!node) {
268 DBG("Relay viewer stream %" PRIu64 " not found", id);
269 goto end;
270 }
7591bab1
MD
271 vstream = caa_container_of(node, struct relay_viewer_stream, stream_n);
272 if (!viewer_stream_get(vstream)) {
273 vstream = NULL;
274 }
2f8f53af 275end:
7591bab1
MD
276 rcu_read_unlock();
277 return vstream;
278}
279
280void viewer_stream_put(struct relay_viewer_stream *vstream)
281{
282 rcu_read_lock();
7591bab1 283 urcu_ref_put(&vstream->ref, viewer_stream_release);
7591bab1
MD
284 rcu_read_unlock();
285}
286
3087b021
MD
287void viewer_stream_close_files(struct relay_viewer_stream *vstream)
288{
289 if (vstream->index_file) {
290 lttng_index_file_put(vstream->index_file);
291 vstream->index_file = NULL;
292 }
8bb66c3c
JG
293 if (vstream->stream_file.handle) {
294 fs_handle_close(vstream->stream_file.handle);
295 vstream->stream_file.handle = NULL;
3087b021
MD
296 }
297}
298
299void viewer_stream_sync_tracefile_array_tail(struct relay_viewer_stream *vstream)
300{
301 const struct relay_stream *stream = vstream->stream;
302 uint64_t seq_tail;
303
304 vstream->current_tracefile_id = tracefile_array_get_file_index_tail(stream->tfa);
305 seq_tail = tracefile_array_get_seq_tail(stream->tfa);
306 if (seq_tail == -1ULL) {
307 seq_tail = 0;
308 }
90faeb9e
MD
309
310 /*
311 * Move the index sent seqcount forward if it was lagging behind
312 * the new tail of the tracefile array. If the current
313 * index_sent_seqcount is already further than the tracefile
314 * array tail position, keep its current position.
315 */
316 vstream->index_sent_seqcount = seq_tail > vstream->index_sent_seqcount ?
317 seq_tail : vstream->index_sent_seqcount;
3087b021
MD
318}
319
2f8f53af
DG
320/*
321 * Rotate a stream to the next tracefile.
322 *
7591bab1 323 * Must be called with the rstream lock held.
b0d240a2 324 * Returns 0 on success, 1 on EOF.
2f8f53af 325 */
7591bab1 326int viewer_stream_rotate(struct relay_viewer_stream *vstream)
2f8f53af
DG
327{
328 int ret;
a44ca2ca 329 uint64_t new_id;
ebb29c10 330 const struct relay_stream *stream = vstream->stream;
2f8f53af 331
7591bab1 332 /* Detect the last tracefile to open. */
a44ca2ca
MD
333 if (stream->index_received_seqcount
334 == vstream->index_sent_seqcount
7591bab1
MD
335 && stream->trace->session->connection_closed) {
336 ret = 1;
2a174661
DG
337 goto end;
338 }
2f8f53af 339
7591bab1
MD
340 if (stream->tracefile_count == 0) {
341 /* Ignore rotation, there is none to do. */
342 ret = 0;
2f8f53af
DG
343 goto end;
344 }
345
a44ca2ca
MD
346 /*
347 * Try to move to the next file.
348 */
349 new_id = (vstream->current_tracefile_id + 1)
350 % stream->tracefile_count;
351 if (tracefile_array_seq_in_file(stream->tfa, new_id,
352 vstream->index_sent_seqcount)) {
353 vstream->current_tracefile_id = new_id;
2f8f53af 354 } else {
a44ca2ca
MD
355 uint64_t seq_tail = tracefile_array_get_seq_tail(stream->tfa);
356
357 /*
358 * This can only be reached on overwrite, which implies there
359 * has been data written at some point, which will have set the
360 * tail.
361 */
362 assert(seq_tail != -1ULL);
363 /*
364 * We need to resync because we lag behind tail.
365 */
7591bab1 366 vstream->current_tracefile_id =
a44ca2ca
MD
367 tracefile_array_get_file_index_tail(stream->tfa);
368 vstream->index_sent_seqcount = seq_tail;
2f8f53af 369 }
b0d240a2
MD
370 viewer_stream_close_files(vstream);
371 ret = 0;
2f8f53af 372end:
2f8f53af
DG
373 return ret;
374}
7591bab1
MD
375
376void print_viewer_streams(void)
377{
378 struct lttng_ht_iter iter;
379 struct relay_viewer_stream *vstream;
380
ce3f3ba3
JG
381 if (!viewer_streams_ht) {
382 return;
383 }
384
7591bab1
MD
385 rcu_read_lock();
386 cds_lfht_for_each_entry(viewer_streams_ht->ht, &iter.iter, vstream,
387 stream_n.node) {
388 if (!viewer_stream_get(vstream)) {
389 continue;
390 }
391 DBG("vstream %p refcount %ld stream %" PRIu64 " trace %" PRIu64
392 " session %" PRIu64,
393 vstream,
394 vstream->ref.refcount,
395 vstream->stream->stream_handle,
396 vstream->stream->trace->id,
397 vstream->stream->trace->session->id);
398 viewer_stream_put(vstream);
399 }
400 rcu_read_unlock();
401}
This page took 0.079883 seconds and 5 git commands to generate.