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