Backport: Fix: relayd streams can be leaked on connection error
[lttng-tools.git] / src / bin / lttng-relayd / stream.c
CommitLineData
2a174661
DG
1/*
2 * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
7591bab1 4 * 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
2a174661
DG
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License, version 2 only, as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
6c1c0768 20#define _LGPL_SOURCE
2a174661 21#include <common/common.h>
7591bab1
MD
22#include <common/utils.h>
23#include <common/defaults.h>
24#include <urcu/rculist.h>
25#include <sys/stat.h>
2a174661 26
7591bab1 27#include "lttng-relayd.h"
2a174661
DG
28#include "index.h"
29#include "stream.h"
30#include "viewer-stream.h"
31
7591bab1
MD
32/* Should be called with RCU read-side lock held. */
33bool stream_get(struct relay_stream *stream)
34{
35 bool has_ref = false;
36
37 pthread_mutex_lock(&stream->reflock);
38 if (stream->ref.refcount != 0) {
39 has_ref = true;
40 urcu_ref_get(&stream->ref);
41 }
42 pthread_mutex_unlock(&stream->reflock);
43
44 return has_ref;
45}
46
2a174661 47/*
7591bab1
MD
48 * Get stream from stream id from the streams hash table. Return stream
49 * if found else NULL. A stream reference is taken when a stream is
50 * returned. stream_put() must be called on that stream.
2a174661 51 */
7591bab1 52struct relay_stream *stream_get_by_id(uint64_t stream_id)
2a174661
DG
53{
54 struct lttng_ht_node_u64 *node;
55 struct lttng_ht_iter iter;
56 struct relay_stream *stream = NULL;
57
7591bab1
MD
58 rcu_read_lock();
59 lttng_ht_lookup(relay_streams_ht, &stream_id, &iter);
2a174661 60 node = lttng_ht_iter_get_node_u64(&iter);
7591bab1 61 if (!node) {
2a174661
DG
62 DBG("Relay stream %" PRIu64 " not found", stream_id);
63 goto end;
64 }
65 stream = caa_container_of(node, struct relay_stream, node);
7591bab1
MD
66 if (!stream_get(stream)) {
67 stream = NULL;
68 }
2a174661 69end:
7591bab1 70 rcu_read_unlock();
2a174661
DG
71 return stream;
72}
73
74/*
7591bab1 75 * We keep ownership of path_name and channel_name.
2a174661 76 */
7591bab1
MD
77struct relay_stream *stream_create(struct ctf_trace *trace,
78 uint64_t stream_handle, char *path_name,
79 char *channel_name, uint64_t tracefile_size,
80 uint64_t tracefile_count)
2a174661 81{
7591bab1
MD
82 int ret;
83 struct relay_stream *stream = NULL;
84 struct relay_session *session = trace->session;
2a174661 85
7591bab1
MD
86 stream = zmalloc(sizeof(struct relay_stream));
87 if (stream == NULL) {
88 PERROR("relay stream zmalloc");
89 ret = -1;
90 goto error_no_alloc;
91 }
2a174661 92
7591bab1
MD
93 stream->stream_handle = stream_handle;
94 stream->prev_seq = -1ULL;
bda7c7b9 95 stream->last_net_seq_num = -1ULL;
7591bab1
MD
96 stream->ctf_stream_id = -1ULL;
97 stream->tracefile_size = tracefile_size;
98 stream->tracefile_count = tracefile_count;
99 stream->path_name = path_name;
100 stream->channel_name = channel_name;
101 lttng_ht_node_init_u64(&stream->node, stream->stream_handle);
102 pthread_mutex_init(&stream->lock, NULL);
103 pthread_mutex_init(&stream->reflock, NULL);
104 urcu_ref_init(&stream->ref);
105 ctf_trace_get(trace);
106 stream->trace = trace;
2a174661 107
7591bab1
MD
108 stream->indexes_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
109 if (!stream->indexes_ht) {
110 ERR("Cannot created indexes_ht");
111 ret = -1;
112 goto end;
2a174661
DG
113 }
114
7591bab1
MD
115 ret = utils_mkdir_recursive(stream->path_name, S_IRWXU | S_IRWXG,
116 -1, -1);
117 if (ret < 0) {
118 ERR("relay creating output directory");
119 goto end;
120 }
2a174661 121
7591bab1
MD
122 /*
123 * No need to use run_as API here because whatever we receive,
124 * the relayd uses its own credentials for the stream files.
125 */
126 ret = utils_create_stream_file(stream->path_name, stream->channel_name,
127 stream->tracefile_size, 0, -1, -1, NULL);
128 if (ret < 0) {
129 ERR("Create output file");
130 goto end;
131 }
132 stream->stream_fd = stream_fd_create(ret);
133 if (!stream->stream_fd) {
134 if (close(ret)) {
135 PERROR("Error closing file %d", ret);
2a174661 136 }
7591bab1
MD
137 ret = -1;
138 goto end;
2a174661 139 }
a44ca2ca
MD
140 stream->tfa = tracefile_array_create(stream->tracefile_count);
141 if (!stream->tfa) {
142 ret = -1;
143 goto end;
144 }
7591bab1
MD
145 if (stream->tracefile_size) {
146 DBG("Tracefile %s/%s_0 created", stream->path_name, stream->channel_name);
147 } else {
148 DBG("Tracefile %s/%s created", stream->path_name, stream->channel_name);
149 }
150
36d2e35d 151 if (!strncmp(stream->channel_name, DEFAULT_METADATA_NAME, LTTNG_NAME_MAX)) {
7591bab1
MD
152 stream->is_metadata = 1;
153 }
154
155 stream->in_recv_list = true;
156
157 /*
158 * Add the stream in the recv list of the session. Once the end stream
159 * message is received, all session streams are published.
160 */
161 pthread_mutex_lock(&session->recv_list_lock);
162 cds_list_add_rcu(&stream->recv_node, &session->recv_list);
163 session->stream_count++;
164 pthread_mutex_unlock(&session->recv_list_lock);
165
166 /*
167 * Both in the ctf_trace object and the global stream ht since the data
168 * side of the relayd does not have the concept of session.
169 */
170 lttng_ht_add_unique_u64(relay_streams_ht, &stream->node);
77f7bd85 171 stream->in_stream_ht = true;
2a174661 172
7591bab1
MD
173 DBG("Relay new stream added %s with ID %" PRIu64, stream->channel_name,
174 stream->stream_handle);
175 ret = 0;
176
177end:
178 if (ret) {
179 if (stream->stream_fd) {
180 stream_fd_put(stream->stream_fd);
181 stream->stream_fd = NULL;
2a174661 182 }
7591bab1
MD
183 stream_put(stream);
184 stream = NULL;
2a174661 185 }
7591bab1 186 return stream;
2a174661 187
7591bab1
MD
188error_no_alloc:
189 /*
190 * path_name and channel_name need to be freed explicitly here
191 * because we cannot rely on stream_put().
192 */
193 free(path_name);
194 free(channel_name);
195 return NULL;
196}
197
198/*
199 * Called with the session lock held.
200 */
201void stream_publish(struct relay_stream *stream)
202{
203 struct relay_session *session;
204
205 pthread_mutex_lock(&stream->lock);
206 if (stream->published) {
207 goto unlock;
2a174661
DG
208 }
209
7591bab1 210 session = stream->trace->session;
2a174661 211
7591bab1
MD
212 pthread_mutex_lock(&session->recv_list_lock);
213 if (stream->in_recv_list) {
214 cds_list_del_rcu(&stream->recv_node);
215 stream->in_recv_list = false;
216 }
217 pthread_mutex_unlock(&session->recv_list_lock);
2a174661 218
7591bab1
MD
219 pthread_mutex_lock(&stream->trace->stream_list_lock);
220 cds_list_add_rcu(&stream->stream_node, &stream->trace->stream_list);
221 pthread_mutex_unlock(&stream->trace->stream_list_lock);
2a174661 222
7591bab1
MD
223 stream->published = true;
224unlock:
2a174661 225 pthread_mutex_unlock(&stream->lock);
2a174661
DG
226}
227
7591bab1 228/*
77f7bd85
MD
229 * Stream must be protected by holding the stream lock or by virtue of being
230 * called from stream_destroy, in which case it is guaranteed to be accessed
231 * from a single thread by the reflock.
7591bab1
MD
232 */
233static void stream_unpublish(struct relay_stream *stream)
2a174661 234{
77f7bd85
MD
235 if (stream->in_stream_ht) {
236 struct lttng_ht_iter iter;
237 int ret;
238
239 iter.iter.node = &stream->node.node;
240 ret = lttng_ht_del(relay_streams_ht, &iter);
241 assert(!ret);
242 stream->in_stream_ht = false;
243 }
244 if (stream->published) {
245 pthread_mutex_lock(&stream->trace->stream_list_lock);
246 cds_list_del_rcu(&stream->stream_node);
247 pthread_mutex_unlock(&stream->trace->stream_list_lock);
248 stream->published = false;
7591bab1 249 }
7591bab1
MD
250}
251
252static void stream_destroy(struct relay_stream *stream)
253{
254 if (stream->indexes_ht) {
49e614cb
MD
255 /*
256 * Calling lttng_ht_destroy in call_rcu worker thread so
257 * we don't hold the RCU read-side lock while calling
258 * it.
259 */
7591bab1
MD
260 lttng_ht_destroy(stream->indexes_ht);
261 }
a44ca2ca
MD
262 if (stream->tfa) {
263 tracefile_array_destroy(stream->tfa);
264 }
7591bab1
MD
265 free(stream->path_name);
266 free(stream->channel_name);
267 free(stream);
268}
269
270static void stream_destroy_rcu(struct rcu_head *rcu_head)
271{
272 struct relay_stream *stream =
273 caa_container_of(rcu_head, struct relay_stream, rcu_node);
274
275 stream_destroy(stream);
276}
277
278/*
279 * No need to take stream->lock since this is only called on the final
280 * stream_put which ensures that a single thread may act on the stream.
281 *
282 * At that point, the object is also protected by the reflock which
283 * guarantees that no other thread may share ownership of this stream.
284 */
285static void stream_release(struct urcu_ref *ref)
286{
287 struct relay_stream *stream =
288 caa_container_of(ref, struct relay_stream, ref);
289 struct relay_session *session;
2a174661 290
7591bab1
MD
291 session = stream->trace->session;
292
293 DBG("Releasing stream id %" PRIu64, stream->stream_handle);
294
295 pthread_mutex_lock(&session->recv_list_lock);
296 session->stream_count--;
297 if (stream->in_recv_list) {
298 cds_list_del_rcu(&stream->recv_node);
299 stream->in_recv_list = false;
300 }
301 pthread_mutex_unlock(&session->recv_list_lock);
2a174661 302
7591bab1
MD
303 stream_unpublish(stream);
304
305 if (stream->stream_fd) {
306 stream_fd_put(stream->stream_fd);
307 stream->stream_fd = NULL;
308 }
e0547b83
MD
309 if (stream->index_file) {
310 lttng_index_file_put(stream->index_file);
311 stream->index_file = NULL;
7591bab1
MD
312 }
313 if (stream->trace) {
314 ctf_trace_put(stream->trace);
315 stream->trace = NULL;
316 }
317
318 call_rcu(&stream->rcu_node, stream_destroy_rcu);
2a174661
DG
319}
320
7591bab1 321void stream_put(struct relay_stream *stream)
2a174661 322{
7591bab1
MD
323 DBG("stream put for stream id %" PRIu64, stream->stream_handle);
324 /*
325 * Ensure existence of stream->reflock for stream unlock.
326 */
327 rcu_read_lock();
328 /*
329 * Stream reflock ensures that concurrent test and update of
330 * stream ref is atomic.
331 */
332 pthread_mutex_lock(&stream->reflock);
333 assert(stream->ref.refcount != 0);
334 /*
335 * Wait until we have processed all the stream packets before
336 * actually putting our last stream reference.
337 */
338 DBG("stream put stream id %" PRIu64 " refcount %d",
339 stream->stream_handle,
340 (int) stream->ref.refcount);
341 urcu_ref_put(&stream->ref, stream_release);
342 pthread_mutex_unlock(&stream->reflock);
343 rcu_read_unlock();
344}
345
bda7c7b9 346void try_stream_close(struct relay_stream *stream)
7591bab1 347{
06a68401
JR
348 bool session_aborted;
349 struct relay_session *session = stream->trace->session;
350
bda7c7b9 351 DBG("Trying to close stream %" PRIu64, stream->stream_handle);
06a68401
JR
352
353 pthread_mutex_lock(&session->lock);
354 session_aborted = session->aborted;
355 pthread_mutex_unlock(&session->lock);
356
7591bab1 357 pthread_mutex_lock(&stream->lock);
bda7c7b9
JG
358 /*
359 * Can be called concurently by connection close and reception of last
360 * pending data.
361 */
362 if (stream->closed) {
363 pthread_mutex_unlock(&stream->lock);
364 DBG("closing stream %" PRIu64 " aborted since it is already marked as closed", stream->stream_handle);
365 return;
366 }
367
368 stream->close_requested = true;
3d07a857
MD
369
370 if (stream->last_net_seq_num == -1ULL) {
371 /*
372 * Handle connection close without explicit stream close
373 * command.
374 *
375 * We can be clever about indexes partially received in
376 * cases where we received the data socket part, but not
377 * the control socket part: since we're currently closing
378 * the stream on behalf of the control socket, we *know*
379 * there won't be any more control information for this
380 * socket. Therefore, we can destroy all indexes for
381 * which we have received only the file descriptor (from
382 * data socket). This takes care of consumerd crashes
383 * between sending the data and control information for
384 * a packet. Since those are sent in that order, we take
385 * care of consumerd crashes.
386 */
44a2fbf1 387 DBG("relay_index_close_partial_fd");
3d07a857
MD
388 relay_index_close_partial_fd(stream);
389 /*
390 * Use the highest net_seq_num we currently have pending
391 * As end of stream indicator. Leave last_net_seq_num
392 * at -1ULL if we cannot find any index.
393 */
394 stream->last_net_seq_num = relay_index_find_last(stream);
44a2fbf1 395 DBG("Updating stream->last_net_seq_num to %" PRIu64, stream->last_net_seq_num);
3d07a857
MD
396 /* Fall-through into the next check. */
397 }
398
bda7c7b9 399 if (stream->last_net_seq_num != -1ULL &&
06a68401
JR
400 ((int64_t) (stream->prev_seq - stream->last_net_seq_num)) < 0
401 && !session_aborted) {
3d07a857
MD
402 /*
403 * Don't close since we still have data pending. This
404 * handles cases where an explicit close command has
405 * been received for this stream, and cases where the
406 * connection has been closed, and we are awaiting for
407 * index information from the data socket. It is
408 * therefore expected that all the index fd information
409 * we need has already been received on the control
410 * socket. Matching index information from data socket
411 * should be Expected Soon(TM).
412 *
413 * TODO: We should implement a timer to garbage collect
414 * streams after a timeout to be resilient against a
415 * consumerd implementation that would not match this
416 * expected behavior.
417 */
bda7c7b9
JG
418 pthread_mutex_unlock(&stream->lock);
419 DBG("closing stream %" PRIu64 " aborted since it still has data pending", stream->stream_handle);
420 return;
421 }
3d07a857
MD
422 /*
423 * We received all the indexes we can expect.
424 */
77f7bd85 425 stream_unpublish(stream);
2229a09c 426 stream->closed = true;
bda7c7b9 427 /* Relay indexes are only used by the "consumer/sessiond" end. */
7591bab1
MD
428 relay_index_close_all(stream);
429 pthread_mutex_unlock(&stream->lock);
bda7c7b9 430 DBG("Succeeded in closing stream %" PRIu64, stream->stream_handle);
7591bab1
MD
431 stream_put(stream);
432}
433
da412cde
MD
434static void print_stream_indexes(struct relay_stream *stream)
435{
436 struct lttng_ht_iter iter;
437 struct relay_index *index;
438
439 rcu_read_lock();
440 cds_lfht_for_each_entry(stream->indexes_ht->ht, &iter.iter, index,
441 index_n.node) {
442 DBG("index %p net_seq_num %" PRIu64 " refcount %ld"
443 " stream %" PRIu64 " trace %" PRIu64
444 " session %" PRIu64,
445 index,
446 index->index_n.key,
447 stream->ref.refcount,
448 index->stream->stream_handle,
449 index->stream->trace->id,
450 index->stream->trace->session->id);
451 }
452 rcu_read_unlock();
453}
454
7591bab1
MD
455void print_relay_streams(void)
456{
457 struct lttng_ht_iter iter;
458 struct relay_stream *stream;
459
ce3f3ba3
JG
460 if (!relay_streams_ht) {
461 return;
462 }
463
7591bab1
MD
464 rcu_read_lock();
465 cds_lfht_for_each_entry(relay_streams_ht->ht, &iter.iter, stream,
466 node.node) {
467 if (!stream_get(stream)) {
468 continue;
469 }
470 DBG("stream %p refcount %ld stream %" PRIu64 " trace %" PRIu64
471 " session %" PRIu64,
472 stream,
473 stream->ref.refcount,
474 stream->stream_handle,
475 stream->trace->id,
476 stream->trace->session->id);
da412cde 477 print_stream_indexes(stream);
7591bab1
MD
478 stream_put(stream);
479 }
480 rcu_read_unlock();
2a174661 481}
This page took 0.068213 seconds and 5 git commands to generate.