Fix: wrong check on session started on stop command
[lttng-tools.git] / src / common / consumer.c
CommitLineData
3bd1e081
MD
1/*
2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
00e2e675 4 * 2012 - David Goulet <dgoulet@efficios.com>
3bd1e081 5 *
d14d33bf
AM
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
3bd1e081 9 *
d14d33bf
AM
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.
3bd1e081 14 *
d14d33bf
AM
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
3bd1e081
MD
18 */
19
20#define _GNU_SOURCE
21#include <assert.h>
3bd1e081
MD
22#include <poll.h>
23#include <pthread.h>
24#include <stdlib.h>
25#include <string.h>
26#include <sys/mman.h>
27#include <sys/socket.h>
28#include <sys/types.h>
29#include <unistd.h>
77c7c900 30#include <inttypes.h>
3bd1e081 31
990570ed 32#include <common/common.h>
fb3a43a9
DG
33#include <common/utils.h>
34#include <common/compat/poll.h>
10a8a223 35#include <common/kernel-ctl/kernel-ctl.h>
00e2e675 36#include <common/sessiond-comm/relayd.h>
10a8a223
DG
37#include <common/sessiond-comm/sessiond-comm.h>
38#include <common/kernel-consumer/kernel-consumer.h>
00e2e675 39#include <common/relayd/relayd.h>
10a8a223
DG
40#include <common/ust-consumer/ust-consumer.h>
41
42#include "consumer.h"
3bd1e081
MD
43
44struct lttng_consumer_global_data consumer_data = {
3bd1e081
MD
45 .stream_count = 0,
46 .need_update = 1,
47 .type = LTTNG_CONSUMER_UNKNOWN,
48};
49
3bd1e081
MD
50/*
51 * Flag to inform the polling thread to quit when all fd hung up. Updated by
52 * the consumer_thread_receive_fds when it notices that all fds has hung up.
53 * Also updated by the signal handler (consumer_should_exit()). Read by the
54 * polling threads.
55 */
a98dae5f 56volatile int consumer_quit;
3bd1e081 57
43c34bc3 58/*
43c34bc3
DG
59 * Global hash table containing respectively metadata and data streams. The
60 * stream element in this ht should only be updated by the metadata poll thread
61 * for the metadata and the data poll thread for the data.
62 */
40dc48e0
DG
63static struct lttng_ht *metadata_ht;
64static struct lttng_ht *data_ht;
43c34bc3 65
46e6455f
DG
66/*
67 * This hash table contains the mapping between the session id of the sessiond
f7079f67
DG
68 * and the relayd session id. Element of the ht are indexed by sessiond session
69 * id.
46e6455f
DG
70 *
71 * Node can be added when a relayd communication is opened in the sessiond
72 * thread.
73 *
74 * Note that a session id of the session daemon is unique to a tracing session
75 * and not to a domain session. However, a domain session has one consumer
76 * which forces the 1-1 mapping between a consumer and a domain session (ex:
77 * UST). This means that we can't have duplicate in this ht.
78 */
79static struct lttng_ht *relayd_session_id_ht;
80
8994307f
DG
81/*
82 * Notify a thread pipe to poll back again. This usually means that some global
83 * state has changed so we just send back the thread in a poll wait call.
84 */
85static void notify_thread_pipe(int wpipe)
86{
87 int ret;
88
89 do {
90 struct lttng_consumer_stream *null_stream = NULL;
91
92 ret = write(wpipe, &null_stream, sizeof(null_stream));
93 } while (ret < 0 && errno == EINTR);
94}
95
3bd1e081
MD
96/*
97 * Find a stream. The consumer_data.lock must be locked during this
98 * call.
99 */
8389e4f8
DG
100static struct lttng_consumer_stream *consumer_find_stream(int key,
101 struct lttng_ht *ht)
3bd1e081 102{
e4421fec
DG
103 struct lttng_ht_iter iter;
104 struct lttng_ht_node_ulong *node;
105 struct lttng_consumer_stream *stream = NULL;
3bd1e081 106
8389e4f8
DG
107 assert(ht);
108
7ad0a0cb 109 /* Negative keys are lookup failures */
7a57cf92 110 if (key < 0) {
7ad0a0cb 111 return NULL;
7a57cf92 112 }
e4421fec 113
6065ceec
DG
114 rcu_read_lock();
115
8389e4f8 116 lttng_ht_lookup(ht, (void *)((unsigned long) key), &iter);
e4421fec
DG
117 node = lttng_ht_iter_get_node_ulong(&iter);
118 if (node != NULL) {
119 stream = caa_container_of(node, struct lttng_consumer_stream, node);
3bd1e081 120 }
e4421fec 121
6065ceec
DG
122 rcu_read_unlock();
123
e4421fec 124 return stream;
3bd1e081
MD
125}
126
c869f647 127void consumer_steal_stream_key(int key, struct lttng_ht *ht)
7ad0a0cb
MD
128{
129 struct lttng_consumer_stream *stream;
130
04253271 131 rcu_read_lock();
8389e4f8 132 stream = consumer_find_stream(key, ht);
04253271 133 if (stream) {
7ad0a0cb 134 stream->key = -1;
04253271
MD
135 /*
136 * We don't want the lookup to match, but we still need
137 * to iterate on this stream when iterating over the hash table. Just
138 * change the node key.
139 */
140 stream->node.key = -1;
141 }
142 rcu_read_unlock();
7ad0a0cb
MD
143}
144
d56db448
DG
145/*
146 * Return a channel object for the given key.
147 *
148 * RCU read side lock MUST be acquired before calling this function and
149 * protects the channel ptr.
150 */
3bd1e081
MD
151static struct lttng_consumer_channel *consumer_find_channel(int key)
152{
e4421fec
DG
153 struct lttng_ht_iter iter;
154 struct lttng_ht_node_ulong *node;
155 struct lttng_consumer_channel *channel = NULL;
3bd1e081 156
7ad0a0cb 157 /* Negative keys are lookup failures */
7a57cf92 158 if (key < 0) {
7ad0a0cb 159 return NULL;
7a57cf92 160 }
e4421fec
DG
161
162 lttng_ht_lookup(consumer_data.channel_ht, (void *)((unsigned long) key),
163 &iter);
164 node = lttng_ht_iter_get_node_ulong(&iter);
165 if (node != NULL) {
166 channel = caa_container_of(node, struct lttng_consumer_channel, node);
3bd1e081 167 }
e4421fec
DG
168
169 return channel;
3bd1e081
MD
170}
171
7ad0a0cb
MD
172static void consumer_steal_channel_key(int key)
173{
174 struct lttng_consumer_channel *channel;
175
04253271 176 rcu_read_lock();
7ad0a0cb 177 channel = consumer_find_channel(key);
04253271 178 if (channel) {
7ad0a0cb 179 channel->key = -1;
04253271
MD
180 /*
181 * We don't want the lookup to match, but we still need
182 * to iterate on this channel when iterating over the hash table. Just
183 * change the node key.
184 */
185 channel->node.key = -1;
186 }
187 rcu_read_unlock();
7ad0a0cb
MD
188}
189
702b1ea4
MD
190static
191void consumer_free_stream(struct rcu_head *head)
192{
193 struct lttng_ht_node_ulong *node =
194 caa_container_of(head, struct lttng_ht_node_ulong, head);
195 struct lttng_consumer_stream *stream =
196 caa_container_of(node, struct lttng_consumer_stream, node);
197
198 free(stream);
199}
200
00e2e675
DG
201/*
202 * RCU protected relayd socket pair free.
203 */
204static void consumer_rcu_free_relayd(struct rcu_head *head)
205{
206 struct lttng_ht_node_ulong *node =
207 caa_container_of(head, struct lttng_ht_node_ulong, head);
208 struct consumer_relayd_sock_pair *relayd =
209 caa_container_of(node, struct consumer_relayd_sock_pair, node);
210
8994307f
DG
211 /*
212 * Close all sockets. This is done in the call RCU since we don't want the
213 * socket fds to be reassigned thus potentially creating bad state of the
214 * relayd object.
215 *
216 * We do not have to lock the control socket mutex here since at this stage
217 * there is no one referencing to this relayd object.
218 */
219 (void) relayd_close(&relayd->control_sock);
220 (void) relayd_close(&relayd->data_sock);
221
00e2e675
DG
222 free(relayd);
223}
224
225/*
226 * Destroy and free relayd socket pair object.
227 *
228 * This function MUST be called with the consumer_data lock acquired.
229 */
d09e1200 230static void destroy_relayd(struct consumer_relayd_sock_pair *relayd)
00e2e675
DG
231{
232 int ret;
233 struct lttng_ht_iter iter;
46e6455f 234 struct lttng_ht_node_ulong *node;
00e2e675 235
173af62f
DG
236 if (relayd == NULL) {
237 return;
238 }
239
00e2e675
DG
240 DBG("Consumer destroy and close relayd socket pair");
241
f7079f67 242 /* Loockup for a relayd node in the session id map hash table. */
46e6455f 243 lttng_ht_lookup(relayd_session_id_ht,
f7079f67 244 (void *)((unsigned long) relayd->sessiond_session_id), &iter);
46e6455f 245 node = lttng_ht_iter_get_node_ulong(&iter);
8fa0b55b 246 if (node == NULL) {
46e6455f
DG
247 /* We assume the relayd is being or is destroyed */
248 return;
249 }
250
f7079f67
DG
251 /*
252 * Try to delete it from the relayd session id ht. The return value is of
253 * no importance since either way we are going to try to delete the relayd
254 * from the global relayd_ht.
255 */
256 lttng_ht_del(relayd_session_id_ht, &iter);
46e6455f 257
00e2e675
DG
258 iter.iter.node = &relayd->node.node;
259 ret = lttng_ht_del(consumer_data.relayd_ht, &iter);
173af62f 260 if (ret != 0) {
8994307f 261 /* We assume the relayd is being or is destroyed */
173af62f
DG
262 return;
263 }
00e2e675 264
00e2e675
DG
265 /* RCU free() call */
266 call_rcu(&relayd->node.head, consumer_rcu_free_relayd);
267}
268
228b5bf7
DG
269/*
270 * Iterate over the relayd hash table and destroy each element. Finally,
271 * destroy the whole hash table.
272 */
273static void cleanup_relayd_ht(void)
274{
275 struct lttng_ht_iter iter;
276 struct consumer_relayd_sock_pair *relayd;
277
278 rcu_read_lock();
279
280 cds_lfht_for_each_entry(consumer_data.relayd_ht->ht, &iter.iter, relayd,
281 node.node) {
282 destroy_relayd(relayd);
283 }
284
285 lttng_ht_destroy(consumer_data.relayd_ht);
f7079f67
DG
286 /* The destroy_relayd call makes sure that this ht is empty here. */
287 lttng_ht_destroy(relayd_session_id_ht);
228b5bf7
DG
288
289 rcu_read_unlock();
290}
291
8994307f
DG
292/*
293 * Update the end point status of all streams having the given network sequence
294 * index (relayd index).
295 *
296 * It's atomically set without having the stream mutex locked which is fine
297 * because we handle the write/read race with a pipe wakeup for each thread.
298 */
299static void update_endpoint_status_by_netidx(int net_seq_idx,
300 enum consumer_endpoint_status status)
301{
302 struct lttng_ht_iter iter;
303 struct lttng_consumer_stream *stream;
304
305 DBG("Consumer set delete flag on stream by idx %d", net_seq_idx);
306
307 rcu_read_lock();
308
309 /* Let's begin with metadata */
310 cds_lfht_for_each_entry(metadata_ht->ht, &iter.iter, stream, node.node) {
311 if (stream->net_seq_idx == net_seq_idx) {
312 uatomic_set(&stream->endpoint_status, status);
313 DBG("Delete flag set to metadata stream %d", stream->wait_fd);
314 }
315 }
316
317 /* Follow up by the data streams */
318 cds_lfht_for_each_entry(data_ht->ht, &iter.iter, stream, node.node) {
319 if (stream->net_seq_idx == net_seq_idx) {
320 uatomic_set(&stream->endpoint_status, status);
321 DBG("Delete flag set to data stream %d", stream->wait_fd);
322 }
323 }
324 rcu_read_unlock();
325}
326
327/*
328 * Cleanup a relayd object by flagging every associated streams for deletion,
329 * destroying the object meaning removing it from the relayd hash table,
330 * closing the sockets and freeing the memory in a RCU call.
331 *
332 * If a local data context is available, notify the threads that the streams'
333 * state have changed.
334 */
335static void cleanup_relayd(struct consumer_relayd_sock_pair *relayd,
336 struct lttng_consumer_local_data *ctx)
337{
338 int netidx;
339
340 assert(relayd);
341
9617607b
DG
342 DBG("Cleaning up relayd sockets");
343
8994307f
DG
344 /* Save the net sequence index before destroying the object */
345 netidx = relayd->net_seq_idx;
346
347 /*
348 * Delete the relayd from the relayd hash table, close the sockets and free
349 * the object in a RCU call.
350 */
351 destroy_relayd(relayd);
352
353 /* Set inactive endpoint to all streams */
354 update_endpoint_status_by_netidx(netidx, CONSUMER_ENDPOINT_INACTIVE);
355
356 /*
357 * With a local data context, notify the threads that the streams' state
358 * have changed. The write() action on the pipe acts as an "implicit"
359 * memory barrier ordering the updates of the end point status from the
360 * read of this status which happens AFTER receiving this notify.
361 */
362 if (ctx) {
363 notify_thread_pipe(ctx->consumer_data_pipe[1]);
364 notify_thread_pipe(ctx->consumer_metadata_pipe[1]);
365 }
366}
367
a6ba4fe1
DG
368/*
369 * Flag a relayd socket pair for destruction. Destroy it if the refcount
370 * reaches zero.
371 *
372 * RCU read side lock MUST be aquired before calling this function.
373 */
374void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair *relayd)
375{
376 assert(relayd);
377
378 /* Set destroy flag for this object */
379 uatomic_set(&relayd->destroy_flag, 1);
380
381 /* Destroy the relayd if refcount is 0 */
382 if (uatomic_read(&relayd->refcount) == 0) {
d09e1200 383 destroy_relayd(relayd);
a6ba4fe1
DG
384 }
385}
386
3bd1e081
MD
387/*
388 * Remove a stream from the global list protected by a mutex. This
389 * function is also responsible for freeing its data structures.
390 */
e316aad5
DG
391void consumer_del_stream(struct lttng_consumer_stream *stream,
392 struct lttng_ht *ht)
3bd1e081
MD
393{
394 int ret;
e4421fec 395 struct lttng_ht_iter iter;
3bd1e081 396 struct lttng_consumer_channel *free_chan = NULL;
00e2e675
DG
397 struct consumer_relayd_sock_pair *relayd;
398
399 assert(stream);
3bd1e081 400
8994307f
DG
401 DBG("Consumer del stream %d", stream->wait_fd);
402
e316aad5
DG
403 if (ht == NULL) {
404 /* Means the stream was allocated but not successfully added */
405 goto free_stream;
406 }
407
3bd1e081 408 pthread_mutex_lock(&consumer_data.lock);
74251bb8 409 pthread_mutex_lock(&stream->lock);
3bd1e081
MD
410
411 switch (consumer_data.type) {
412 case LTTNG_CONSUMER_KERNEL:
413 if (stream->mmap_base != NULL) {
414 ret = munmap(stream->mmap_base, stream->mmap_len);
415 if (ret != 0) {
7a57cf92 416 PERROR("munmap");
3bd1e081
MD
417 }
418 }
419 break;
7753dea8
MD
420 case LTTNG_CONSUMER32_UST:
421 case LTTNG_CONSUMER64_UST:
3bd1e081
MD
422 lttng_ustconsumer_del_stream(stream);
423 break;
424 default:
425 ERR("Unknown consumer_data type");
426 assert(0);
427 goto end;
428 }
429
6065ceec 430 rcu_read_lock();
04253271 431 iter.iter.node = &stream->node.node;
e316aad5 432 ret = lttng_ht_del(ht, &iter);
04253271 433 assert(!ret);
ca22feea
DG
434
435 /* Remove node session id from the consumer_data stream ht */
436 iter.iter.node = &stream->node_session_id.node;
437 ret = lttng_ht_del(consumer_data.stream_list_ht, &iter);
438 assert(!ret);
6065ceec
DG
439 rcu_read_unlock();
440
50f8ae69 441 assert(consumer_data.stream_count > 0);
3bd1e081 442 consumer_data.stream_count--;
50f8ae69 443
3bd1e081 444 if (stream->out_fd >= 0) {
4c462e79
MD
445 ret = close(stream->out_fd);
446 if (ret) {
447 PERROR("close");
448 }
3bd1e081 449 }
b5c5fc29 450 if (stream->wait_fd >= 0 && !stream->wait_fd_is_copy) {
4c462e79
MD
451 ret = close(stream->wait_fd);
452 if (ret) {
453 PERROR("close");
454 }
3bd1e081 455 }
2c1dd183 456 if (stream->shm_fd >= 0 && stream->wait_fd != stream->shm_fd) {
4c462e79
MD
457 ret = close(stream->shm_fd);
458 if (ret) {
459 PERROR("close");
460 }
3bd1e081 461 }
00e2e675
DG
462
463 /* Check and cleanup relayd */
b0b335c8 464 rcu_read_lock();
00e2e675
DG
465 relayd = consumer_find_relayd(stream->net_seq_idx);
466 if (relayd != NULL) {
b0b335c8
MD
467 uatomic_dec(&relayd->refcount);
468 assert(uatomic_read(&relayd->refcount) >= 0);
173af62f 469
3f8e211f
DG
470 /* Closing streams requires to lock the control socket. */
471 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
173af62f
DG
472 ret = relayd_send_close_stream(&relayd->control_sock,
473 stream->relayd_stream_id,
474 stream->next_net_seq_num - 1);
3f8e211f 475 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
173af62f 476 if (ret < 0) {
a4b92340
DG
477 DBG("Unable to close stream on the relayd. Continuing");
478 /*
479 * Continue here. There is nothing we can do for the relayd.
480 * Chances are that the relayd has closed the socket so we just
481 * continue cleaning up.
482 */
173af62f
DG
483 }
484
485 /* Both conditions are met, we destroy the relayd. */
486 if (uatomic_read(&relayd->refcount) == 0 &&
487 uatomic_read(&relayd->destroy_flag)) {
d09e1200 488 destroy_relayd(relayd);
00e2e675 489 }
00e2e675 490 }
b0b335c8 491 rcu_read_unlock();
00e2e675 492
c30aaa51
MD
493 uatomic_dec(&stream->chan->refcount);
494 if (!uatomic_read(&stream->chan->refcount)
495 && !uatomic_read(&stream->chan->nb_init_streams)) {
3bd1e081 496 free_chan = stream->chan;
00e2e675
DG
497 }
498
3bd1e081
MD
499end:
500 consumer_data.need_update = 1;
8994307f 501 pthread_mutex_unlock(&stream->lock);
74251bb8 502 pthread_mutex_unlock(&consumer_data.lock);
3bd1e081 503
c30aaa51 504 if (free_chan) {
3bd1e081 505 consumer_del_channel(free_chan);
c30aaa51 506 }
e316aad5
DG
507
508free_stream:
509 call_rcu(&stream->node.head, consumer_free_stream);
3bd1e081
MD
510}
511
512struct lttng_consumer_stream *consumer_allocate_stream(
513 int channel_key, int stream_key,
514 int shm_fd, int wait_fd,
515 enum lttng_consumer_stream_state state,
516 uint64_t mmap_len,
517 enum lttng_event_output output,
6df2e2c9
MD
518 const char *path_name,
519 uid_t uid,
00e2e675
DG
520 gid_t gid,
521 int net_index,
c80048c6 522 int metadata_flag,
53632229 523 uint64_t session_id,
c80048c6 524 int *alloc_ret)
3bd1e081
MD
525{
526 struct lttng_consumer_stream *stream;
3bd1e081 527
effcf122 528 stream = zmalloc(sizeof(*stream));
3bd1e081 529 if (stream == NULL) {
7a57cf92 530 PERROR("malloc struct lttng_consumer_stream");
c80048c6 531 *alloc_ret = -ENOMEM;
7a57cf92 532 goto end;
3bd1e081 533 }
7a57cf92 534
d56db448
DG
535 rcu_read_lock();
536
7a57cf92
DG
537 /*
538 * Get stream's channel reference. Needed when adding the stream to the
539 * global hash table.
540 */
3bd1e081
MD
541 stream->chan = consumer_find_channel(channel_key);
542 if (!stream->chan) {
c80048c6 543 *alloc_ret = -ENOENT;
7a57cf92 544 ERR("Unable to find channel for stream %d", stream_key);
c80048c6 545 goto error;
3bd1e081 546 }
e316aad5 547
3bd1e081
MD
548 stream->key = stream_key;
549 stream->shm_fd = shm_fd;
550 stream->wait_fd = wait_fd;
551 stream->out_fd = -1;
552 stream->out_fd_offset = 0;
553 stream->state = state;
554 stream->mmap_len = mmap_len;
555 stream->mmap_base = NULL;
556 stream->output = output;
6df2e2c9
MD
557 stream->uid = uid;
558 stream->gid = gid;
00e2e675
DG
559 stream->net_seq_idx = net_index;
560 stream->metadata_flag = metadata_flag;
53632229 561 stream->session_id = session_id;
00e2e675
DG
562 strncpy(stream->path_name, path_name, sizeof(stream->path_name));
563 stream->path_name[sizeof(stream->path_name) - 1] = '\0';
53632229 564 pthread_mutex_init(&stream->lock, NULL);
58b1f425
DG
565
566 /*
567 * Index differently the metadata node because the thread is using an
568 * internal hash table to match streams in the metadata_ht to the epoll set
569 * file descriptor.
570 */
571 if (metadata_flag) {
572 lttng_ht_node_init_ulong(&stream->node, stream->wait_fd);
573 } else {
574 lttng_ht_node_init_ulong(&stream->node, stream->key);
575 }
c30aaa51 576
53632229
DG
577 /* Init session id node with the stream session id */
578 lttng_ht_node_init_ulong(&stream->node_session_id, stream->session_id);
579
c869f647
DG
580 /*
581 * The cpu number is needed before using any ustctl_* actions. Ignored for
582 * the kernel so the value does not matter.
583 */
584 pthread_mutex_lock(&consumer_data.lock);
585 stream->cpu = stream->chan->cpucount++;
586 pthread_mutex_unlock(&consumer_data.lock);
587
c30aaa51 588 DBG3("Allocated stream %s (key %d, shm_fd %d, wait_fd %d, mmap_len %llu,"
53632229
DG
589 " out_fd %d, net_seq_idx %d, session_id %" PRIu64,
590 stream->path_name, stream->key, stream->shm_fd, stream->wait_fd,
c30aaa51 591 (unsigned long long) stream->mmap_len, stream->out_fd,
53632229 592 stream->net_seq_idx, stream->session_id);
d56db448
DG
593
594 rcu_read_unlock();
3bd1e081 595 return stream;
c80048c6
MD
596
597error:
d56db448 598 rcu_read_unlock();
c80048c6 599 free(stream);
7a57cf92 600end:
c80048c6 601 return NULL;
3bd1e081
MD
602}
603
604/*
605 * Add a stream to the global list protected by a mutex.
606 */
43c34bc3
DG
607static int consumer_add_stream(struct lttng_consumer_stream *stream,
608 struct lttng_ht *ht)
3bd1e081
MD
609{
610 int ret = 0;
00e2e675 611 struct consumer_relayd_sock_pair *relayd;
3bd1e081 612
e316aad5 613 assert(stream);
43c34bc3 614 assert(ht);
c77fc10a 615
e316aad5
DG
616 DBG3("Adding consumer stream %d", stream->key);
617
618 pthread_mutex_lock(&consumer_data.lock);
2e818a6a 619 pthread_mutex_lock(&stream->lock);
b0b335c8 620 rcu_read_lock();
e316aad5 621
43c34bc3
DG
622 /* Steal stream identifier to avoid having streams with the same key */
623 consumer_steal_stream_key(stream->key, ht);
624
625 lttng_ht_add_unique_ulong(ht, &stream->node);
00e2e675 626
ca22feea
DG
627 /*
628 * Add stream to the stream_list_ht of the consumer data. No need to steal
629 * the key since the HT does not use it and we allow to add redundant keys
630 * into this table.
631 */
632 lttng_ht_add_ulong(consumer_data.stream_list_ht, &stream->node_session_id);
633
00e2e675
DG
634 /* Check and cleanup relayd */
635 relayd = consumer_find_relayd(stream->net_seq_idx);
636 if (relayd != NULL) {
b0b335c8 637 uatomic_inc(&relayd->refcount);
00e2e675
DG
638 }
639
e316aad5
DG
640 /* Update channel refcount once added without error(s). */
641 uatomic_inc(&stream->chan->refcount);
642
643 /*
644 * When nb_init_streams reaches 0, we don't need to trigger any action in
645 * terms of destroying the associated channel, because the action that
646 * causes the count to become 0 also causes a stream to be added. The
647 * channel deletion will thus be triggered by the following removal of this
648 * stream.
649 */
650 if (uatomic_read(&stream->chan->nb_init_streams) > 0) {
651 uatomic_dec(&stream->chan->nb_init_streams);
652 }
653
654 /* Update consumer data once the node is inserted. */
3bd1e081
MD
655 consumer_data.stream_count++;
656 consumer_data.need_update = 1;
657
e316aad5 658 rcu_read_unlock();
2e818a6a 659 pthread_mutex_unlock(&stream->lock);
3bd1e081 660 pthread_mutex_unlock(&consumer_data.lock);
702b1ea4 661
3bd1e081
MD
662 return ret;
663}
664
00e2e675 665/*
3f8e211f
DG
666 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
667 * be acquired before calling this.
00e2e675 668 */
d09e1200 669static int add_relayd(struct consumer_relayd_sock_pair *relayd)
00e2e675
DG
670{
671 int ret = 0;
672 struct lttng_ht_node_ulong *node;
673 struct lttng_ht_iter iter;
674
675 if (relayd == NULL) {
676 ret = -1;
677 goto end;
678 }
679
00e2e675
DG
680 lttng_ht_lookup(consumer_data.relayd_ht,
681 (void *)((unsigned long) relayd->net_seq_idx), &iter);
682 node = lttng_ht_iter_get_node_ulong(&iter);
683 if (node != NULL) {
00e2e675
DG
684 /* Relayd already exist. Ignore the insertion */
685 goto end;
686 }
687 lttng_ht_add_unique_ulong(consumer_data.relayd_ht, &relayd->node);
688
00e2e675
DG
689end:
690 return ret;
691}
692
693/*
694 * Allocate and return a consumer relayd socket.
695 */
696struct consumer_relayd_sock_pair *consumer_allocate_relayd_sock_pair(
697 int net_seq_idx)
698{
699 struct consumer_relayd_sock_pair *obj = NULL;
700
701 /* Negative net sequence index is a failure */
702 if (net_seq_idx < 0) {
703 goto error;
704 }
705
706 obj = zmalloc(sizeof(struct consumer_relayd_sock_pair));
707 if (obj == NULL) {
708 PERROR("zmalloc relayd sock");
709 goto error;
710 }
711
712 obj->net_seq_idx = net_seq_idx;
713 obj->refcount = 0;
173af62f 714 obj->destroy_flag = 0;
00e2e675
DG
715 lttng_ht_node_init_ulong(&obj->node, obj->net_seq_idx);
716 pthread_mutex_init(&obj->ctrl_sock_mutex, NULL);
717
718error:
719 return obj;
720}
721
722/*
723 * Find a relayd socket pair in the global consumer data.
724 *
725 * Return the object if found else NULL.
b0b335c8
MD
726 * RCU read-side lock must be held across this call and while using the
727 * returned object.
00e2e675
DG
728 */
729struct consumer_relayd_sock_pair *consumer_find_relayd(int key)
730{
731 struct lttng_ht_iter iter;
732 struct lttng_ht_node_ulong *node;
733 struct consumer_relayd_sock_pair *relayd = NULL;
734
735 /* Negative keys are lookup failures */
736 if (key < 0) {
737 goto error;
738 }
739
00e2e675
DG
740 lttng_ht_lookup(consumer_data.relayd_ht, (void *)((unsigned long) key),
741 &iter);
742 node = lttng_ht_iter_get_node_ulong(&iter);
743 if (node != NULL) {
744 relayd = caa_container_of(node, struct consumer_relayd_sock_pair, node);
745 }
746
00e2e675
DG
747error:
748 return relayd;
749}
750
751/*
752 * Handle stream for relayd transmission if the stream applies for network
753 * streaming where the net sequence index is set.
754 *
755 * Return destination file descriptor or negative value on error.
756 */
6197aea7 757static int write_relayd_stream_header(struct lttng_consumer_stream *stream,
1d4dfdef
DG
758 size_t data_size, unsigned long padding,
759 struct consumer_relayd_sock_pair *relayd)
00e2e675
DG
760{
761 int outfd = -1, ret;
00e2e675
DG
762 struct lttcomm_relayd_data_hdr data_hdr;
763
764 /* Safety net */
765 assert(stream);
6197aea7 766 assert(relayd);
00e2e675
DG
767
768 /* Reset data header */
769 memset(&data_hdr, 0, sizeof(data_hdr));
770
00e2e675
DG
771 if (stream->metadata_flag) {
772 /* Caller MUST acquire the relayd control socket lock */
773 ret = relayd_send_metadata(&relayd->control_sock, data_size);
774 if (ret < 0) {
775 goto error;
776 }
777
778 /* Metadata are always sent on the control socket. */
779 outfd = relayd->control_sock.fd;
780 } else {
781 /* Set header with stream information */
782 data_hdr.stream_id = htobe64(stream->relayd_stream_id);
783 data_hdr.data_size = htobe32(data_size);
1d4dfdef 784 data_hdr.padding_size = htobe32(padding);
173af62f 785 data_hdr.net_seq_num = htobe64(stream->next_net_seq_num++);
00e2e675
DG
786 /* Other fields are zeroed previously */
787
788 ret = relayd_send_data_hdr(&relayd->data_sock, &data_hdr,
789 sizeof(data_hdr));
790 if (ret < 0) {
791 goto error;
792 }
793
794 /* Set to go on data socket */
795 outfd = relayd->data_sock.fd;
796 }
797
798error:
799 return outfd;
800}
801
702b1ea4
MD
802static
803void consumer_free_channel(struct rcu_head *head)
804{
805 struct lttng_ht_node_ulong *node =
806 caa_container_of(head, struct lttng_ht_node_ulong, head);
807 struct lttng_consumer_channel *channel =
808 caa_container_of(node, struct lttng_consumer_channel, node);
809
810 free(channel);
811}
812
3bd1e081
MD
813/*
814 * Remove a channel from the global list protected by a mutex. This
815 * function is also responsible for freeing its data structures.
816 */
817void consumer_del_channel(struct lttng_consumer_channel *channel)
818{
819 int ret;
e4421fec 820 struct lttng_ht_iter iter;
3bd1e081 821
5c540210
DG
822 DBG("Consumer delete channel key %d", channel->key);
823
3bd1e081
MD
824 pthread_mutex_lock(&consumer_data.lock);
825
826 switch (consumer_data.type) {
827 case LTTNG_CONSUMER_KERNEL:
828 break;
7753dea8
MD
829 case LTTNG_CONSUMER32_UST:
830 case LTTNG_CONSUMER64_UST:
3bd1e081
MD
831 lttng_ustconsumer_del_channel(channel);
832 break;
833 default:
834 ERR("Unknown consumer_data type");
835 assert(0);
836 goto end;
837 }
838
6065ceec 839 rcu_read_lock();
04253271
MD
840 iter.iter.node = &channel->node.node;
841 ret = lttng_ht_del(consumer_data.channel_ht, &iter);
842 assert(!ret);
6065ceec
DG
843 rcu_read_unlock();
844
3bd1e081
MD
845 if (channel->mmap_base != NULL) {
846 ret = munmap(channel->mmap_base, channel->mmap_len);
847 if (ret != 0) {
7a57cf92 848 PERROR("munmap");
3bd1e081
MD
849 }
850 }
b5c5fc29 851 if (channel->wait_fd >= 0 && !channel->wait_fd_is_copy) {
4c462e79
MD
852 ret = close(channel->wait_fd);
853 if (ret) {
854 PERROR("close");
855 }
3bd1e081 856 }
2c1dd183 857 if (channel->shm_fd >= 0 && channel->wait_fd != channel->shm_fd) {
4c462e79
MD
858 ret = close(channel->shm_fd);
859 if (ret) {
860 PERROR("close");
861 }
3bd1e081 862 }
702b1ea4
MD
863
864 call_rcu(&channel->node.head, consumer_free_channel);
3bd1e081
MD
865end:
866 pthread_mutex_unlock(&consumer_data.lock);
867}
868
869struct lttng_consumer_channel *consumer_allocate_channel(
870 int channel_key,
871 int shm_fd, int wait_fd,
872 uint64_t mmap_len,
c30aaa51
MD
873 uint64_t max_sb_size,
874 unsigned int nb_init_streams)
3bd1e081
MD
875{
876 struct lttng_consumer_channel *channel;
877 int ret;
878
276b26d1 879 channel = zmalloc(sizeof(*channel));
3bd1e081 880 if (channel == NULL) {
7a57cf92 881 PERROR("malloc struct lttng_consumer_channel");
3bd1e081
MD
882 goto end;
883 }
884 channel->key = channel_key;
885 channel->shm_fd = shm_fd;
886 channel->wait_fd = wait_fd;
887 channel->mmap_len = mmap_len;
888 channel->max_sb_size = max_sb_size;
889 channel->refcount = 0;
c30aaa51 890 channel->nb_init_streams = nb_init_streams;
e4421fec 891 lttng_ht_node_init_ulong(&channel->node, channel->key);
3bd1e081
MD
892
893 switch (consumer_data.type) {
894 case LTTNG_CONSUMER_KERNEL:
895 channel->mmap_base = NULL;
896 channel->mmap_len = 0;
897 break;
7753dea8
MD
898 case LTTNG_CONSUMER32_UST:
899 case LTTNG_CONSUMER64_UST:
3bd1e081
MD
900 ret = lttng_ustconsumer_allocate_channel(channel);
901 if (ret) {
902 free(channel);
903 return NULL;
904 }
905 break;
906 default:
907 ERR("Unknown consumer_data type");
908 assert(0);
909 goto end;
910 }
911 DBG("Allocated channel (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, max_sb_size %llu)",
00e2e675 912 channel->key, channel->shm_fd, channel->wait_fd,
3bd1e081
MD
913 (unsigned long long) channel->mmap_len,
914 (unsigned long long) channel->max_sb_size);
915end:
916 return channel;
917}
918
919/*
920 * Add a channel to the global list protected by a mutex.
921 */
922int consumer_add_channel(struct lttng_consumer_channel *channel)
923{
c77fc10a
DG
924 struct lttng_ht_node_ulong *node;
925 struct lttng_ht_iter iter;
926
3bd1e081 927 pthread_mutex_lock(&consumer_data.lock);
7ad0a0cb
MD
928 /* Steal channel identifier, for UST */
929 consumer_steal_channel_key(channel->key);
6065ceec 930 rcu_read_lock();
c77fc10a
DG
931
932 lttng_ht_lookup(consumer_data.channel_ht,
933 (void *)((unsigned long) channel->key), &iter);
934 node = lttng_ht_iter_get_node_ulong(&iter);
935 if (node != NULL) {
936 /* Channel already exist. Ignore the insertion */
937 goto end;
938 }
939
04253271 940 lttng_ht_add_unique_ulong(consumer_data.channel_ht, &channel->node);
c77fc10a
DG
941
942end:
6065ceec 943 rcu_read_unlock();
3bd1e081 944 pthread_mutex_unlock(&consumer_data.lock);
702b1ea4 945
7ad0a0cb 946 return 0;
3bd1e081
MD
947}
948
949/*
950 * Allocate the pollfd structure and the local view of the out fds to avoid
951 * doing a lookup in the linked list and concurrency issues when writing is
952 * needed. Called with consumer_data.lock held.
953 *
954 * Returns the number of fds in the structures.
955 */
43c34bc3 956static int consumer_update_poll_array(
3bd1e081 957 struct lttng_consumer_local_data *ctx, struct pollfd **pollfd,
43c34bc3 958 struct lttng_consumer_stream **local_stream, struct lttng_ht *ht)
3bd1e081 959{
3bd1e081 960 int i = 0;
e4421fec
DG
961 struct lttng_ht_iter iter;
962 struct lttng_consumer_stream *stream;
3bd1e081
MD
963
964 DBG("Updating poll fd array");
481d6c57 965 rcu_read_lock();
43c34bc3 966 cds_lfht_for_each_entry(ht->ht, &iter.iter, stream, node.node) {
8994307f
DG
967 /*
968 * Only active streams with an active end point can be added to the
969 * poll set and local stream storage of the thread.
970 *
971 * There is a potential race here for endpoint_status to be updated
972 * just after the check. However, this is OK since the stream(s) will
973 * be deleted once the thread is notified that the end point state has
974 * changed where this function will be called back again.
975 */
976 if (stream->state != LTTNG_CONSUMER_ACTIVE_STREAM ||
79d4ffb7 977 stream->endpoint_status == CONSUMER_ENDPOINT_INACTIVE) {
3bd1e081
MD
978 continue;
979 }
e4421fec
DG
980 DBG("Active FD %d", stream->wait_fd);
981 (*pollfd)[i].fd = stream->wait_fd;
3bd1e081 982 (*pollfd)[i].events = POLLIN | POLLPRI;
e4421fec 983 local_stream[i] = stream;
3bd1e081
MD
984 i++;
985 }
481d6c57 986 rcu_read_unlock();
3bd1e081
MD
987
988 /*
50f8ae69 989 * Insert the consumer_data_pipe at the end of the array and don't
3bd1e081
MD
990 * increment i so nb_fd is the number of real FD.
991 */
50f8ae69 992 (*pollfd)[i].fd = ctx->consumer_data_pipe[0];
509bb1cf 993 (*pollfd)[i].events = POLLIN | POLLPRI;
3bd1e081
MD
994 return i;
995}
996
997/*
998 * Poll on the should_quit pipe and the command socket return -1 on error and
999 * should exit, 0 if data is available on the command socket
1000 */
1001int lttng_consumer_poll_socket(struct pollfd *consumer_sockpoll)
1002{
1003 int num_rdy;
1004
88f2b785 1005restart:
3bd1e081
MD
1006 num_rdy = poll(consumer_sockpoll, 2, -1);
1007 if (num_rdy == -1) {
88f2b785
MD
1008 /*
1009 * Restart interrupted system call.
1010 */
1011 if (errno == EINTR) {
1012 goto restart;
1013 }
7a57cf92 1014 PERROR("Poll error");
3bd1e081
MD
1015 goto exit;
1016 }
509bb1cf 1017 if (consumer_sockpoll[0].revents & (POLLIN | POLLPRI)) {
3bd1e081
MD
1018 DBG("consumer_should_quit wake up");
1019 goto exit;
1020 }
1021 return 0;
1022
1023exit:
1024 return -1;
1025}
1026
1027/*
1028 * Set the error socket.
1029 */
1030void lttng_consumer_set_error_sock(
1031 struct lttng_consumer_local_data *ctx, int sock)
1032{
1033 ctx->consumer_error_socket = sock;
1034}
1035
1036/*
1037 * Set the command socket path.
1038 */
3bd1e081
MD
1039void lttng_consumer_set_command_sock_path(
1040 struct lttng_consumer_local_data *ctx, char *sock)
1041{
1042 ctx->consumer_command_sock_path = sock;
1043}
1044
1045/*
1046 * Send return code to the session daemon.
1047 * If the socket is not defined, we return 0, it is not a fatal error
1048 */
1049int lttng_consumer_send_error(
1050 struct lttng_consumer_local_data *ctx, int cmd)
1051{
1052 if (ctx->consumer_error_socket > 0) {
1053 return lttcomm_send_unix_sock(ctx->consumer_error_socket, &cmd,
1054 sizeof(enum lttcomm_sessiond_command));
1055 }
1056
1057 return 0;
1058}
1059
1060/*
228b5bf7
DG
1061 * Close all the tracefiles and stream fds and MUST be called when all
1062 * instances are destroyed i.e. when all threads were joined and are ended.
3bd1e081
MD
1063 */
1064void lttng_consumer_cleanup(void)
1065{
e4421fec 1066 struct lttng_ht_iter iter;
6065ceec
DG
1067 struct lttng_ht_node_ulong *node;
1068
1069 rcu_read_lock();
3bd1e081 1070
6065ceec
DG
1071 cds_lfht_for_each_entry(consumer_data.channel_ht->ht, &iter.iter, node,
1072 node) {
702b1ea4
MD
1073 struct lttng_consumer_channel *channel =
1074 caa_container_of(node, struct lttng_consumer_channel, node);
1075 consumer_del_channel(channel);
3bd1e081 1076 }
6065ceec
DG
1077
1078 rcu_read_unlock();
d6ce1df2 1079
d6ce1df2 1080 lttng_ht_destroy(consumer_data.channel_ht);
228b5bf7
DG
1081
1082 cleanup_relayd_ht();
1083
1084 /*
1085 * This HT contains streams that are freed by either the metadata thread or
1086 * the data thread so we do *nothing* on the hash table and simply destroy
1087 * it.
1088 */
1089 lttng_ht_destroy(consumer_data.stream_list_ht);
3bd1e081
MD
1090}
1091
1092/*
1093 * Called from signal handler.
1094 */
1095void lttng_consumer_should_exit(struct lttng_consumer_local_data *ctx)
1096{
1097 int ret;
1098 consumer_quit = 1;
6f94560a
MD
1099 do {
1100 ret = write(ctx->consumer_should_quit[1], "4", 1);
1101 } while (ret < 0 && errno == EINTR);
3bd1e081 1102 if (ret < 0) {
7a57cf92 1103 PERROR("write consumer quit");
3bd1e081 1104 }
ab1027f4
DG
1105
1106 DBG("Consumer flag that it should quit");
3bd1e081
MD
1107}
1108
00e2e675
DG
1109void lttng_consumer_sync_trace_file(struct lttng_consumer_stream *stream,
1110 off_t orig_offset)
3bd1e081
MD
1111{
1112 int outfd = stream->out_fd;
1113
1114 /*
1115 * This does a blocking write-and-wait on any page that belongs to the
1116 * subbuffer prior to the one we just wrote.
1117 * Don't care about error values, as these are just hints and ways to
1118 * limit the amount of page cache used.
1119 */
1120 if (orig_offset < stream->chan->max_sb_size) {
1121 return;
1122 }
b9182dd9 1123 lttng_sync_file_range(outfd, orig_offset - stream->chan->max_sb_size,
3bd1e081
MD
1124 stream->chan->max_sb_size,
1125 SYNC_FILE_RANGE_WAIT_BEFORE
1126 | SYNC_FILE_RANGE_WRITE
1127 | SYNC_FILE_RANGE_WAIT_AFTER);
1128 /*
1129 * Give hints to the kernel about how we access the file:
1130 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1131 * we write it.
1132 *
1133 * We need to call fadvise again after the file grows because the
1134 * kernel does not seem to apply fadvise to non-existing parts of the
1135 * file.
1136 *
1137 * Call fadvise _after_ having waited for the page writeback to
1138 * complete because the dirty page writeback semantic is not well
1139 * defined. So it can be expected to lead to lower throughput in
1140 * streaming.
1141 */
1142 posix_fadvise(outfd, orig_offset - stream->chan->max_sb_size,
1143 stream->chan->max_sb_size, POSIX_FADV_DONTNEED);
1144}
1145
1146/*
1147 * Initialise the necessary environnement :
1148 * - create a new context
1149 * - create the poll_pipe
1150 * - create the should_quit pipe (for signal handler)
1151 * - create the thread pipe (for splice)
1152 *
1153 * Takes a function pointer as argument, this function is called when data is
1154 * available on a buffer. This function is responsible to do the
1155 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1156 * buffer configuration and then kernctl_put_next_subbuf at the end.
1157 *
1158 * Returns a pointer to the new context or NULL on error.
1159 */
1160struct lttng_consumer_local_data *lttng_consumer_create(
1161 enum lttng_consumer_type type,
4078b776 1162 ssize_t (*buffer_ready)(struct lttng_consumer_stream *stream,
d41f73b7 1163 struct lttng_consumer_local_data *ctx),
3bd1e081
MD
1164 int (*recv_channel)(struct lttng_consumer_channel *channel),
1165 int (*recv_stream)(struct lttng_consumer_stream *stream),
1166 int (*update_stream)(int stream_key, uint32_t state))
1167{
1168 int ret, i;
1169 struct lttng_consumer_local_data *ctx;
1170
1171 assert(consumer_data.type == LTTNG_CONSUMER_UNKNOWN ||
1172 consumer_data.type == type);
1173 consumer_data.type = type;
1174
effcf122 1175 ctx = zmalloc(sizeof(struct lttng_consumer_local_data));
3bd1e081 1176 if (ctx == NULL) {
7a57cf92 1177 PERROR("allocating context");
3bd1e081
MD
1178 goto error;
1179 }
1180
1181 ctx->consumer_error_socket = -1;
1182 /* assign the callbacks */
1183 ctx->on_buffer_ready = buffer_ready;
1184 ctx->on_recv_channel = recv_channel;
1185 ctx->on_recv_stream = recv_stream;
1186 ctx->on_update_stream = update_stream;
1187
50f8ae69 1188 ret = pipe(ctx->consumer_data_pipe);
3bd1e081 1189 if (ret < 0) {
7a57cf92 1190 PERROR("Error creating poll pipe");
3bd1e081
MD
1191 goto error_poll_pipe;
1192 }
1193
04fdd819 1194 /* set read end of the pipe to non-blocking */
50f8ae69 1195 ret = fcntl(ctx->consumer_data_pipe[0], F_SETFL, O_NONBLOCK);
04fdd819 1196 if (ret < 0) {
7a57cf92 1197 PERROR("fcntl O_NONBLOCK");
04fdd819
MD
1198 goto error_poll_fcntl;
1199 }
1200
1201 /* set write end of the pipe to non-blocking */
50f8ae69 1202 ret = fcntl(ctx->consumer_data_pipe[1], F_SETFL, O_NONBLOCK);
04fdd819 1203 if (ret < 0) {
7a57cf92 1204 PERROR("fcntl O_NONBLOCK");
04fdd819
MD
1205 goto error_poll_fcntl;
1206 }
1207
3bd1e081
MD
1208 ret = pipe(ctx->consumer_should_quit);
1209 if (ret < 0) {
7a57cf92 1210 PERROR("Error creating recv pipe");
3bd1e081
MD
1211 goto error_quit_pipe;
1212 }
1213
1214 ret = pipe(ctx->consumer_thread_pipe);
1215 if (ret < 0) {
7a57cf92 1216 PERROR("Error creating thread pipe");
3bd1e081
MD
1217 goto error_thread_pipe;
1218 }
1219
fb3a43a9
DG
1220 ret = utils_create_pipe(ctx->consumer_metadata_pipe);
1221 if (ret < 0) {
1222 goto error_metadata_pipe;
1223 }
3bd1e081 1224
fb3a43a9
DG
1225 ret = utils_create_pipe(ctx->consumer_splice_metadata_pipe);
1226 if (ret < 0) {
1227 goto error_splice_pipe;
1228 }
1229
1230 return ctx;
3bd1e081 1231
fb3a43a9
DG
1232error_splice_pipe:
1233 utils_close_pipe(ctx->consumer_metadata_pipe);
1234error_metadata_pipe:
1235 utils_close_pipe(ctx->consumer_thread_pipe);
3bd1e081
MD
1236error_thread_pipe:
1237 for (i = 0; i < 2; i++) {
1238 int err;
1239
1240 err = close(ctx->consumer_should_quit[i]);
4c462e79
MD
1241 if (err) {
1242 PERROR("close");
1243 }
3bd1e081 1244 }
04fdd819 1245error_poll_fcntl:
3bd1e081
MD
1246error_quit_pipe:
1247 for (i = 0; i < 2; i++) {
1248 int err;
1249
50f8ae69 1250 err = close(ctx->consumer_data_pipe[i]);
4c462e79
MD
1251 if (err) {
1252 PERROR("close");
1253 }
3bd1e081
MD
1254 }
1255error_poll_pipe:
1256 free(ctx);
1257error:
1258 return NULL;
1259}
1260
1261/*
1262 * Close all fds associated with the instance and free the context.
1263 */
1264void lttng_consumer_destroy(struct lttng_consumer_local_data *ctx)
1265{
4c462e79
MD
1266 int ret;
1267
ab1027f4
DG
1268 DBG("Consumer destroying it. Closing everything.");
1269
4c462e79
MD
1270 ret = close(ctx->consumer_error_socket);
1271 if (ret) {
1272 PERROR("close");
1273 }
1274 ret = close(ctx->consumer_thread_pipe[0]);
1275 if (ret) {
1276 PERROR("close");
1277 }
1278 ret = close(ctx->consumer_thread_pipe[1]);
1279 if (ret) {
1280 PERROR("close");
1281 }
50f8ae69 1282 ret = close(ctx->consumer_data_pipe[0]);
4c462e79
MD
1283 if (ret) {
1284 PERROR("close");
1285 }
50f8ae69 1286 ret = close(ctx->consumer_data_pipe[1]);
4c462e79
MD
1287 if (ret) {
1288 PERROR("close");
1289 }
1290 ret = close(ctx->consumer_should_quit[0]);
1291 if (ret) {
1292 PERROR("close");
1293 }
1294 ret = close(ctx->consumer_should_quit[1]);
1295 if (ret) {
1296 PERROR("close");
1297 }
fb3a43a9
DG
1298 utils_close_pipe(ctx->consumer_splice_metadata_pipe);
1299
3bd1e081
MD
1300 unlink(ctx->consumer_command_sock_path);
1301 free(ctx);
1302}
1303
6197aea7
DG
1304/*
1305 * Write the metadata stream id on the specified file descriptor.
1306 */
1307static int write_relayd_metadata_id(int fd,
1308 struct lttng_consumer_stream *stream,
1d4dfdef
DG
1309 struct consumer_relayd_sock_pair *relayd,
1310 unsigned long padding)
6197aea7
DG
1311{
1312 int ret;
1d4dfdef 1313 struct lttcomm_relayd_metadata_payload hdr;
6197aea7 1314
1d4dfdef
DG
1315 hdr.stream_id = htobe64(stream->relayd_stream_id);
1316 hdr.padding_size = htobe32(padding);
6197aea7 1317 do {
1d4dfdef 1318 ret = write(fd, (void *) &hdr, sizeof(hdr));
6197aea7
DG
1319 } while (ret < 0 && errno == EINTR);
1320 if (ret < 0) {
1321 PERROR("write metadata stream id");
1322 goto end;
1323 }
1d4dfdef
DG
1324 DBG("Metadata stream id %" PRIu64 " with padding %lu written before data",
1325 stream->relayd_stream_id, padding);
6197aea7
DG
1326
1327end:
1328 return ret;
1329}
1330
3bd1e081 1331/*
09e26845
DG
1332 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1333 * core function for writing trace buffers to either the local filesystem or
1334 * the network.
1335 *
79d4ffb7
DG
1336 * It must be called with the stream lock held.
1337 *
09e26845 1338 * Careful review MUST be put if any changes occur!
3bd1e081
MD
1339 *
1340 * Returns the number of bytes written
1341 */
4078b776 1342ssize_t lttng_consumer_on_read_subbuffer_mmap(
3bd1e081 1343 struct lttng_consumer_local_data *ctx,
1d4dfdef
DG
1344 struct lttng_consumer_stream *stream, unsigned long len,
1345 unsigned long padding)
3bd1e081 1346{
f02e1e8a
DG
1347 unsigned long mmap_offset;
1348 ssize_t ret = 0, written = 0;
1349 off_t orig_offset = stream->out_fd_offset;
1350 /* Default is on the disk */
1351 int outfd = stream->out_fd;
f02e1e8a 1352 struct consumer_relayd_sock_pair *relayd = NULL;
8994307f 1353 unsigned int relayd_hang_up = 0;
f02e1e8a
DG
1354
1355 /* RCU lock for the relayd pointer */
1356 rcu_read_lock();
1357
1358 /* Flag that the current stream if set for network streaming. */
1359 if (stream->net_seq_idx != -1) {
1360 relayd = consumer_find_relayd(stream->net_seq_idx);
1361 if (relayd == NULL) {
1362 goto end;
1363 }
1364 }
1365
1366 /* get the offset inside the fd to mmap */
3bd1e081
MD
1367 switch (consumer_data.type) {
1368 case LTTNG_CONSUMER_KERNEL:
f02e1e8a
DG
1369 ret = kernctl_get_mmap_read_offset(stream->wait_fd, &mmap_offset);
1370 break;
7753dea8
MD
1371 case LTTNG_CONSUMER32_UST:
1372 case LTTNG_CONSUMER64_UST:
f02e1e8a
DG
1373 ret = lttng_ustctl_get_mmap_read_offset(stream->chan->handle,
1374 stream->buf, &mmap_offset);
1375 break;
3bd1e081
MD
1376 default:
1377 ERR("Unknown consumer_data type");
1378 assert(0);
1379 }
f02e1e8a
DG
1380 if (ret != 0) {
1381 errno = -ret;
1382 PERROR("tracer ctl get_mmap_read_offset");
1383 written = ret;
1384 goto end;
1385 }
b9182dd9 1386
f02e1e8a
DG
1387 /* Handle stream on the relayd if the output is on the network */
1388 if (relayd) {
1389 unsigned long netlen = len;
1390
1391 /*
1392 * Lock the control socket for the complete duration of the function
1393 * since from this point on we will use the socket.
1394 */
1395 if (stream->metadata_flag) {
1396 /* Metadata requires the control socket. */
1397 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
1d4dfdef 1398 netlen += sizeof(struct lttcomm_relayd_metadata_payload);
f02e1e8a
DG
1399 }
1400
1d4dfdef 1401 ret = write_relayd_stream_header(stream, netlen, padding, relayd);
f02e1e8a
DG
1402 if (ret >= 0) {
1403 /* Use the returned socket. */
1404 outfd = ret;
1405
1406 /* Write metadata stream id before payload */
1407 if (stream->metadata_flag) {
1d4dfdef 1408 ret = write_relayd_metadata_id(outfd, stream, relayd, padding);
f02e1e8a 1409 if (ret < 0) {
f02e1e8a 1410 written = ret;
8994307f
DG
1411 /* Socket operation failed. We consider the relayd dead */
1412 if (ret == -EPIPE || ret == -EINVAL) {
1413 relayd_hang_up = 1;
1414 goto write_error;
1415 }
f02e1e8a
DG
1416 goto end;
1417 }
f02e1e8a 1418 }
8994307f
DG
1419 } else {
1420 /* Socket operation failed. We consider the relayd dead */
1421 if (ret == -EPIPE || ret == -EINVAL) {
1422 relayd_hang_up = 1;
1423 goto write_error;
1424 }
1425 /* Else, use the default set before which is the filesystem. */
f02e1e8a 1426 }
1d4dfdef
DG
1427 } else {
1428 /* No streaming, we have to set the len with the full padding */
1429 len += padding;
f02e1e8a
DG
1430 }
1431
1432 while (len > 0) {
1433 do {
1434 ret = write(outfd, stream->mmap_base + mmap_offset, len);
1435 } while (ret < 0 && errno == EINTR);
1d4dfdef 1436 DBG("Consumer mmap write() ret %zd (len %lu)", ret, len);
f02e1e8a 1437 if (ret < 0) {
c5c45efa
DG
1438 /*
1439 * This is possible if the fd is closed on the other side (outfd)
1440 * or any write problem. It can be verbose a bit for a normal
1441 * execution if for instance the relayd is stopped abruptly. This
1442 * can happen so set this to a DBG statement.
1443 */
1444 DBG("Error in file write mmap");
f02e1e8a
DG
1445 if (written == 0) {
1446 written = ret;
1447 }
8994307f
DG
1448 /* Socket operation failed. We consider the relayd dead */
1449 if (errno == EPIPE || errno == EINVAL) {
1450 relayd_hang_up = 1;
1451 goto write_error;
1452 }
f02e1e8a
DG
1453 goto end;
1454 } else if (ret > len) {
77c7c900 1455 PERROR("Error in file write (ret %zd > len %lu)", ret, len);
f02e1e8a
DG
1456 written += ret;
1457 goto end;
1458 } else {
1459 len -= ret;
1460 mmap_offset += ret;
1461 }
f02e1e8a
DG
1462
1463 /* This call is useless on a socket so better save a syscall. */
1464 if (!relayd) {
1465 /* This won't block, but will start writeout asynchronously */
1466 lttng_sync_file_range(outfd, stream->out_fd_offset, ret,
1467 SYNC_FILE_RANGE_WRITE);
1468 stream->out_fd_offset += ret;
1469 }
1470 written += ret;
1471 }
1472 lttng_consumer_sync_trace_file(stream, orig_offset);
1473
8994307f
DG
1474write_error:
1475 /*
1476 * This is a special case that the relayd has closed its socket. Let's
1477 * cleanup the relayd object and all associated streams.
1478 */
1479 if (relayd && relayd_hang_up) {
1480 cleanup_relayd(relayd, ctx);
1481 }
1482
f02e1e8a
DG
1483end:
1484 /* Unlock only if ctrl socket used */
1485 if (relayd && stream->metadata_flag) {
1486 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
1487 }
1488
1489 rcu_read_unlock();
1490 return written;
3bd1e081
MD
1491}
1492
1493/*
1494 * Splice the data from the ring buffer to the tracefile.
1495 *
79d4ffb7
DG
1496 * It must be called with the stream lock held.
1497 *
3bd1e081
MD
1498 * Returns the number of bytes spliced.
1499 */
4078b776 1500ssize_t lttng_consumer_on_read_subbuffer_splice(
3bd1e081 1501 struct lttng_consumer_local_data *ctx,
1d4dfdef
DG
1502 struct lttng_consumer_stream *stream, unsigned long len,
1503 unsigned long padding)
3bd1e081 1504{
f02e1e8a
DG
1505 ssize_t ret = 0, written = 0, ret_splice = 0;
1506 loff_t offset = 0;
1507 off_t orig_offset = stream->out_fd_offset;
1508 int fd = stream->wait_fd;
1509 /* Default is on the disk */
1510 int outfd = stream->out_fd;
f02e1e8a 1511 struct consumer_relayd_sock_pair *relayd = NULL;
fb3a43a9 1512 int *splice_pipe;
8994307f 1513 unsigned int relayd_hang_up = 0;
f02e1e8a 1514
3bd1e081
MD
1515 switch (consumer_data.type) {
1516 case LTTNG_CONSUMER_KERNEL:
f02e1e8a 1517 break;
7753dea8
MD
1518 case LTTNG_CONSUMER32_UST:
1519 case LTTNG_CONSUMER64_UST:
f02e1e8a 1520 /* Not supported for user space tracing */
3bd1e081
MD
1521 return -ENOSYS;
1522 default:
1523 ERR("Unknown consumer_data type");
1524 assert(0);
3bd1e081
MD
1525 }
1526
f02e1e8a
DG
1527 /* RCU lock for the relayd pointer */
1528 rcu_read_lock();
1529
1530 /* Flag that the current stream if set for network streaming. */
1531 if (stream->net_seq_idx != -1) {
1532 relayd = consumer_find_relayd(stream->net_seq_idx);
1533 if (relayd == NULL) {
1534 goto end;
1535 }
1536 }
1537
fb3a43a9
DG
1538 /*
1539 * Choose right pipe for splice. Metadata and trace data are handled by
1540 * different threads hence the use of two pipes in order not to race or
1541 * corrupt the written data.
1542 */
1543 if (stream->metadata_flag) {
1544 splice_pipe = ctx->consumer_splice_metadata_pipe;
1545 } else {
1546 splice_pipe = ctx->consumer_thread_pipe;
1547 }
1548
f02e1e8a 1549 /* Write metadata stream id before payload */
1d4dfdef
DG
1550 if (relayd) {
1551 int total_len = len;
f02e1e8a 1552
1d4dfdef
DG
1553 if (stream->metadata_flag) {
1554 /*
1555 * Lock the control socket for the complete duration of the function
1556 * since from this point on we will use the socket.
1557 */
1558 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
1559
1560 ret = write_relayd_metadata_id(splice_pipe[1], stream, relayd,
1561 padding);
1562 if (ret < 0) {
1563 written = ret;
8994307f
DG
1564 /* Socket operation failed. We consider the relayd dead */
1565 if (ret == -EBADF) {
1566 WARN("Remote relayd disconnected. Stopping");
1567 relayd_hang_up = 1;
1568 goto write_error;
1569 }
1d4dfdef
DG
1570 goto end;
1571 }
1572
1573 total_len += sizeof(struct lttcomm_relayd_metadata_payload);
1574 }
1575
1576 ret = write_relayd_stream_header(stream, total_len, padding, relayd);
1577 if (ret >= 0) {
1578 /* Use the returned socket. */
1579 outfd = ret;
1580 } else {
8994307f
DG
1581 /* Socket operation failed. We consider the relayd dead */
1582 if (ret == -EBADF) {
1583 WARN("Remote relayd disconnected. Stopping");
1584 relayd_hang_up = 1;
1585 goto write_error;
1586 }
f02e1e8a
DG
1587 goto end;
1588 }
1d4dfdef
DG
1589 } else {
1590 /* No streaming, we have to set the len with the full padding */
1591 len += padding;
f02e1e8a
DG
1592 }
1593
1594 while (len > 0) {
1d4dfdef
DG
1595 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1596 (unsigned long)offset, len, fd, splice_pipe[1]);
fb3a43a9 1597 ret_splice = splice(fd, &offset, splice_pipe[1], NULL, len,
f02e1e8a
DG
1598 SPLICE_F_MOVE | SPLICE_F_MORE);
1599 DBG("splice chan to pipe, ret %zd", ret_splice);
1600 if (ret_splice < 0) {
1601 PERROR("Error in relay splice");
1602 if (written == 0) {
1603 written = ret_splice;
1604 }
1605 ret = errno;
1606 goto splice_error;
1607 }
1608
1609 /* Handle stream on the relayd if the output is on the network */
1610 if (relayd) {
1611 if (stream->metadata_flag) {
1d4dfdef
DG
1612 size_t metadata_payload_size =
1613 sizeof(struct lttcomm_relayd_metadata_payload);
1614
f02e1e8a 1615 /* Update counter to fit the spliced data */
1d4dfdef
DG
1616 ret_splice += metadata_payload_size;
1617 len += metadata_payload_size;
f02e1e8a
DG
1618 /*
1619 * We do this so the return value can match the len passed as
1620 * argument to this function.
1621 */
1d4dfdef 1622 written -= metadata_payload_size;
f02e1e8a
DG
1623 }
1624 }
1625
1626 /* Splice data out */
fb3a43a9 1627 ret_splice = splice(splice_pipe[0], NULL, outfd, NULL,
f02e1e8a 1628 ret_splice, SPLICE_F_MOVE | SPLICE_F_MORE);
1d4dfdef 1629 DBG("Consumer splice pipe to file, ret %zd", ret_splice);
f02e1e8a
DG
1630 if (ret_splice < 0) {
1631 PERROR("Error in file splice");
1632 if (written == 0) {
1633 written = ret_splice;
1634 }
8994307f 1635 /* Socket operation failed. We consider the relayd dead */
00c8752b 1636 if (errno == EBADF || errno == EPIPE) {
8994307f
DG
1637 WARN("Remote relayd disconnected. Stopping");
1638 relayd_hang_up = 1;
1639 goto write_error;
1640 }
f02e1e8a
DG
1641 ret = errno;
1642 goto splice_error;
1643 } else if (ret_splice > len) {
1644 errno = EINVAL;
1645 PERROR("Wrote more data than requested %zd (len: %lu)",
1646 ret_splice, len);
1647 written += ret_splice;
1648 ret = errno;
1649 goto splice_error;
1650 }
1651 len -= ret_splice;
1652
1653 /* This call is useless on a socket so better save a syscall. */
1654 if (!relayd) {
1655 /* This won't block, but will start writeout asynchronously */
1656 lttng_sync_file_range(outfd, stream->out_fd_offset, ret_splice,
1657 SYNC_FILE_RANGE_WRITE);
1658 stream->out_fd_offset += ret_splice;
1659 }
1660 written += ret_splice;
1661 }
1662 lttng_consumer_sync_trace_file(stream, orig_offset);
1663
1664 ret = ret_splice;
1665
1666 goto end;
1667
8994307f
DG
1668write_error:
1669 /*
1670 * This is a special case that the relayd has closed its socket. Let's
1671 * cleanup the relayd object and all associated streams.
1672 */
1673 if (relayd && relayd_hang_up) {
1674 cleanup_relayd(relayd, ctx);
1675 /* Skip splice error so the consumer does not fail */
1676 goto end;
1677 }
1678
f02e1e8a
DG
1679splice_error:
1680 /* send the appropriate error description to sessiond */
1681 switch (ret) {
f02e1e8a 1682 case EINVAL:
f73fabfd 1683 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_EINVAL);
f02e1e8a
DG
1684 break;
1685 case ENOMEM:
f73fabfd 1686 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_ENOMEM);
f02e1e8a
DG
1687 break;
1688 case ESPIPE:
f73fabfd 1689 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_ESPIPE);
f02e1e8a
DG
1690 break;
1691 }
1692
1693end:
1694 if (relayd && stream->metadata_flag) {
1695 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
1696 }
1697
1698 rcu_read_unlock();
1699 return written;
3bd1e081
MD
1700}
1701
1702/*
1703 * Take a snapshot for a specific fd
1704 *
1705 * Returns 0 on success, < 0 on error
1706 */
1707int lttng_consumer_take_snapshot(struct lttng_consumer_local_data *ctx,
1708 struct lttng_consumer_stream *stream)
1709{
1710 switch (consumer_data.type) {
1711 case LTTNG_CONSUMER_KERNEL:
1712 return lttng_kconsumer_take_snapshot(ctx, stream);
7753dea8
MD
1713 case LTTNG_CONSUMER32_UST:
1714 case LTTNG_CONSUMER64_UST:
3bd1e081
MD
1715 return lttng_ustconsumer_take_snapshot(ctx, stream);
1716 default:
1717 ERR("Unknown consumer_data type");
1718 assert(0);
1719 return -ENOSYS;
1720 }
1721
1722}
1723
1724/*
1725 * Get the produced position
1726 *
1727 * Returns 0 on success, < 0 on error
1728 */
1729int lttng_consumer_get_produced_snapshot(
1730 struct lttng_consumer_local_data *ctx,
1731 struct lttng_consumer_stream *stream,
1732 unsigned long *pos)
1733{
1734 switch (consumer_data.type) {
1735 case LTTNG_CONSUMER_KERNEL:
1736 return lttng_kconsumer_get_produced_snapshot(ctx, stream, pos);
7753dea8
MD
1737 case LTTNG_CONSUMER32_UST:
1738 case LTTNG_CONSUMER64_UST:
3bd1e081
MD
1739 return lttng_ustconsumer_get_produced_snapshot(ctx, stream, pos);
1740 default:
1741 ERR("Unknown consumer_data type");
1742 assert(0);
1743 return -ENOSYS;
1744 }
1745}
1746
1747int lttng_consumer_recv_cmd(struct lttng_consumer_local_data *ctx,
1748 int sock, struct pollfd *consumer_sockpoll)
1749{
1750 switch (consumer_data.type) {
1751 case LTTNG_CONSUMER_KERNEL:
1752 return lttng_kconsumer_recv_cmd(ctx, sock, consumer_sockpoll);
7753dea8
MD
1753 case LTTNG_CONSUMER32_UST:
1754 case LTTNG_CONSUMER64_UST:
3bd1e081
MD
1755 return lttng_ustconsumer_recv_cmd(ctx, sock, consumer_sockpoll);
1756 default:
1757 ERR("Unknown consumer_data type");
1758 assert(0);
1759 return -ENOSYS;
1760 }
1761}
1762
43c34bc3
DG
1763/*
1764 * Iterate over all streams of the hashtable and free them properly.
1765 *
1766 * WARNING: *MUST* be used with data stream only.
1767 */
1768static void destroy_data_stream_ht(struct lttng_ht *ht)
1769{
43c34bc3
DG
1770 struct lttng_ht_iter iter;
1771 struct lttng_consumer_stream *stream;
1772
1773 if (ht == NULL) {
1774 return;
1775 }
1776
1777 rcu_read_lock();
1778 cds_lfht_for_each_entry(ht->ht, &iter.iter, stream, node.node) {
5c540210
DG
1779 /*
1780 * Ignore return value since we are currently cleaning up so any error
1781 * can't be handled.
1782 */
1783 (void) consumer_del_stream(stream, ht);
43c34bc3
DG
1784 }
1785 rcu_read_unlock();
1786
1787 lttng_ht_destroy(ht);
1788}
1789
fb3a43a9 1790/*
f724d81e 1791 * Iterate over all streams of the hashtable and free them properly.
e316aad5
DG
1792 *
1793 * XXX: Should not be only for metadata stream or else use an other name.
fb3a43a9
DG
1794 */
1795static void destroy_stream_ht(struct lttng_ht *ht)
1796{
fb3a43a9
DG
1797 struct lttng_ht_iter iter;
1798 struct lttng_consumer_stream *stream;
1799
1800 if (ht == NULL) {
1801 return;
1802 }
1803
d09e1200 1804 rcu_read_lock();
58b1f425 1805 cds_lfht_for_each_entry(ht->ht, &iter.iter, stream, node.node) {
5c540210
DG
1806 /*
1807 * Ignore return value since we are currently cleaning up so any error
1808 * can't be handled.
1809 */
1810 (void) consumer_del_metadata_stream(stream, ht);
fb3a43a9 1811 }
d09e1200 1812 rcu_read_unlock();
fb3a43a9
DG
1813
1814 lttng_ht_destroy(ht);
1815}
1816
1817/*
1818 * Clean up a metadata stream and free its memory.
1819 */
e316aad5
DG
1820void consumer_del_metadata_stream(struct lttng_consumer_stream *stream,
1821 struct lttng_ht *ht)
fb3a43a9
DG
1822{
1823 int ret;
e316aad5
DG
1824 struct lttng_ht_iter iter;
1825 struct lttng_consumer_channel *free_chan = NULL;
fb3a43a9
DG
1826 struct consumer_relayd_sock_pair *relayd;
1827
1828 assert(stream);
1829 /*
1830 * This call should NEVER receive regular stream. It must always be
1831 * metadata stream and this is crucial for data structure synchronization.
1832 */
1833 assert(stream->metadata_flag);
1834
e316aad5
DG
1835 DBG3("Consumer delete metadata stream %d", stream->wait_fd);
1836
1837 if (ht == NULL) {
1838 /* Means the stream was allocated but not successfully added */
1839 goto free_stream;
1840 }
1841
74251bb8 1842 pthread_mutex_lock(&consumer_data.lock);
8994307f
DG
1843 pthread_mutex_lock(&stream->lock);
1844
fb3a43a9
DG
1845 switch (consumer_data.type) {
1846 case LTTNG_CONSUMER_KERNEL:
1847 if (stream->mmap_base != NULL) {
1848 ret = munmap(stream->mmap_base, stream->mmap_len);
1849 if (ret != 0) {
1850 PERROR("munmap metadata stream");
1851 }
1852 }
1853 break;
1854 case LTTNG_CONSUMER32_UST:
1855 case LTTNG_CONSUMER64_UST:
1856 lttng_ustconsumer_del_stream(stream);
1857 break;
1858 default:
1859 ERR("Unknown consumer_data type");
1860 assert(0);
e316aad5 1861 goto end;
fb3a43a9 1862 }
fb3a43a9 1863
c869f647 1864 rcu_read_lock();
58b1f425 1865 iter.iter.node = &stream->node.node;
c869f647
DG
1866 ret = lttng_ht_del(ht, &iter);
1867 assert(!ret);
ca22feea
DG
1868
1869 /* Remove node session id from the consumer_data stream ht */
1870 iter.iter.node = &stream->node_session_id.node;
1871 ret = lttng_ht_del(consumer_data.stream_list_ht, &iter);
1872 assert(!ret);
c869f647
DG
1873 rcu_read_unlock();
1874
fb3a43a9
DG
1875 if (stream->out_fd >= 0) {
1876 ret = close(stream->out_fd);
1877 if (ret) {
1878 PERROR("close");
1879 }
1880 }
1881
1882 if (stream->wait_fd >= 0 && !stream->wait_fd_is_copy) {
1883 ret = close(stream->wait_fd);
1884 if (ret) {
1885 PERROR("close");
1886 }
1887 }
1888
1889 if (stream->shm_fd >= 0 && stream->wait_fd != stream->shm_fd) {
1890 ret = close(stream->shm_fd);
1891 if (ret) {
1892 PERROR("close");
1893 }
1894 }
1895
1896 /* Check and cleanup relayd */
1897 rcu_read_lock();
1898 relayd = consumer_find_relayd(stream->net_seq_idx);
1899 if (relayd != NULL) {
1900 uatomic_dec(&relayd->refcount);
1901 assert(uatomic_read(&relayd->refcount) >= 0);
1902
1903 /* Closing streams requires to lock the control socket. */
1904 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
1905 ret = relayd_send_close_stream(&relayd->control_sock,
1906 stream->relayd_stream_id, stream->next_net_seq_num - 1);
1907 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
1908 if (ret < 0) {
1909 DBG("Unable to close stream on the relayd. Continuing");
1910 /*
1911 * Continue here. There is nothing we can do for the relayd.
1912 * Chances are that the relayd has closed the socket so we just
1913 * continue cleaning up.
1914 */
1915 }
1916
1917 /* Both conditions are met, we destroy the relayd. */
1918 if (uatomic_read(&relayd->refcount) == 0 &&
1919 uatomic_read(&relayd->destroy_flag)) {
d09e1200 1920 destroy_relayd(relayd);
fb3a43a9
DG
1921 }
1922 }
1923 rcu_read_unlock();
1924
1925 /* Atomically decrement channel refcount since other threads can use it. */
1926 uatomic_dec(&stream->chan->refcount);
c30aaa51
MD
1927 if (!uatomic_read(&stream->chan->refcount)
1928 && !uatomic_read(&stream->chan->nb_init_streams)) {
1929 /* Go for channel deletion! */
e316aad5 1930 free_chan = stream->chan;
fb3a43a9
DG
1931 }
1932
e316aad5 1933end:
8994307f 1934 pthread_mutex_unlock(&stream->lock);
74251bb8 1935 pthread_mutex_unlock(&consumer_data.lock);
e316aad5
DG
1936
1937 if (free_chan) {
1938 consumer_del_channel(free_chan);
1939 }
1940
1941free_stream:
58b1f425 1942 call_rcu(&stream->node.head, consumer_free_stream);
fb3a43a9
DG
1943}
1944
1945/*
1946 * Action done with the metadata stream when adding it to the consumer internal
1947 * data structures to handle it.
1948 */
e316aad5
DG
1949static int consumer_add_metadata_stream(struct lttng_consumer_stream *stream,
1950 struct lttng_ht *ht)
fb3a43a9 1951{
e316aad5 1952 int ret = 0;
fb3a43a9 1953 struct consumer_relayd_sock_pair *relayd;
76082088
DG
1954 struct lttng_ht_iter iter;
1955 struct lttng_ht_node_ulong *node;
fb3a43a9 1956
e316aad5
DG
1957 assert(stream);
1958 assert(ht);
1959
1960 DBG3("Adding metadata stream %d to hash table", stream->wait_fd);
1961
1962 pthread_mutex_lock(&consumer_data.lock);
2e818a6a 1963 pthread_mutex_lock(&stream->lock);
e316aad5 1964
e316aad5
DG
1965 /*
1966 * From here, refcounts are updated so be _careful_ when returning an error
1967 * after this point.
1968 */
1969
fb3a43a9 1970 rcu_read_lock();
76082088
DG
1971
1972 /*
1973 * Lookup the stream just to make sure it does not exist in our internal
1974 * state. This should NEVER happen.
1975 */
1976 lttng_ht_lookup(ht, (void *)((unsigned long) stream->wait_fd), &iter);
1977 node = lttng_ht_iter_get_node_ulong(&iter);
1978 assert(!node);
1979
e316aad5 1980 /* Find relayd and, if one is found, increment refcount. */
fb3a43a9
DG
1981 relayd = consumer_find_relayd(stream->net_seq_idx);
1982 if (relayd != NULL) {
1983 uatomic_inc(&relayd->refcount);
1984 }
e316aad5
DG
1985
1986 /* Update channel refcount once added without error(s). */
1987 uatomic_inc(&stream->chan->refcount);
1988
1989 /*
1990 * When nb_init_streams reaches 0, we don't need to trigger any action in
1991 * terms of destroying the associated channel, because the action that
1992 * causes the count to become 0 also causes a stream to be added. The
1993 * channel deletion will thus be triggered by the following removal of this
1994 * stream.
1995 */
1996 if (uatomic_read(&stream->chan->nb_init_streams) > 0) {
1997 uatomic_dec(&stream->chan->nb_init_streams);
1998 }
1999
58b1f425 2000 lttng_ht_add_unique_ulong(ht, &stream->node);
ca22feea
DG
2001
2002 /*
2003 * Add stream to the stream_list_ht of the consumer data. No need to steal
2004 * the key since the HT does not use it and we allow to add redundant keys
2005 * into this table.
2006 */
2007 lttng_ht_add_ulong(consumer_data.stream_list_ht, &stream->node_session_id);
2008
fb3a43a9 2009 rcu_read_unlock();
e316aad5 2010
2e818a6a 2011 pthread_mutex_unlock(&stream->lock);
e316aad5
DG
2012 pthread_mutex_unlock(&consumer_data.lock);
2013 return ret;
fb3a43a9
DG
2014}
2015
8994307f
DG
2016/*
2017 * Delete data stream that are flagged for deletion (endpoint_status).
2018 */
2019static void validate_endpoint_status_data_stream(void)
2020{
2021 struct lttng_ht_iter iter;
2022 struct lttng_consumer_stream *stream;
2023
2024 DBG("Consumer delete flagged data stream");
2025
2026 rcu_read_lock();
2027 cds_lfht_for_each_entry(data_ht->ht, &iter.iter, stream, node.node) {
2028 /* Validate delete flag of the stream */
79d4ffb7 2029 if (stream->endpoint_status == CONSUMER_ENDPOINT_ACTIVE) {
8994307f
DG
2030 continue;
2031 }
2032 /* Delete it right now */
2033 consumer_del_stream(stream, data_ht);
2034 }
2035 rcu_read_unlock();
2036}
2037
2038/*
2039 * Delete metadata stream that are flagged for deletion (endpoint_status).
2040 */
2041static void validate_endpoint_status_metadata_stream(
2042 struct lttng_poll_event *pollset)
2043{
2044 struct lttng_ht_iter iter;
2045 struct lttng_consumer_stream *stream;
2046
2047 DBG("Consumer delete flagged metadata stream");
2048
2049 assert(pollset);
2050
2051 rcu_read_lock();
2052 cds_lfht_for_each_entry(metadata_ht->ht, &iter.iter, stream, node.node) {
2053 /* Validate delete flag of the stream */
79d4ffb7 2054 if (stream->endpoint_status == CONSUMER_ENDPOINT_ACTIVE) {
8994307f
DG
2055 continue;
2056 }
2057 /*
2058 * Remove from pollset so the metadata thread can continue without
2059 * blocking on a deleted stream.
2060 */
2061 lttng_poll_del(pollset, stream->wait_fd);
2062
2063 /* Delete it right now */
2064 consumer_del_metadata_stream(stream, metadata_ht);
2065 }
2066 rcu_read_unlock();
2067}
2068
fb3a43a9
DG
2069/*
2070 * Thread polls on metadata file descriptor and write them on disk or on the
2071 * network.
2072 */
7d980def 2073void *consumer_thread_metadata_poll(void *data)
fb3a43a9
DG
2074{
2075 int ret, i, pollfd;
2076 uint32_t revents, nb_fd;
e316aad5 2077 struct lttng_consumer_stream *stream = NULL;
fb3a43a9
DG
2078 struct lttng_ht_iter iter;
2079 struct lttng_ht_node_ulong *node;
fb3a43a9
DG
2080 struct lttng_poll_event events;
2081 struct lttng_consumer_local_data *ctx = data;
2082 ssize_t len;
2083
2084 rcu_register_thread();
2085
04bb2b64
DG
2086 metadata_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
2087 if (!metadata_ht) {
2088 /* ENOMEM at this point. Better to bail out. */
2089 goto error;
2090 }
2091
fb3a43a9
DG
2092 DBG("Thread metadata poll started");
2093
fb3a43a9
DG
2094 /* Size is set to 1 for the consumer_metadata pipe */
2095 ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC);
2096 if (ret < 0) {
2097 ERR("Poll set creation failed");
2098 goto end;
2099 }
2100
2101 ret = lttng_poll_add(&events, ctx->consumer_metadata_pipe[0], LPOLLIN);
2102 if (ret < 0) {
2103 goto end;
2104 }
2105
2106 /* Main loop */
2107 DBG("Metadata main loop started");
2108
2109 while (1) {
fb3a43a9 2110 /* Only the metadata pipe is set */
d21b0d71 2111 if (LTTNG_POLL_GETNB(&events) == 0 && consumer_quit == 1) {
fb3a43a9
DG
2112 goto end;
2113 }
2114
2115restart:
d21b0d71 2116 DBG("Metadata poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events));
fb3a43a9
DG
2117 ret = lttng_poll_wait(&events, -1);
2118 DBG("Metadata event catched in thread");
2119 if (ret < 0) {
2120 if (errno == EINTR) {
e316aad5 2121 ERR("Poll EINTR catched");
fb3a43a9
DG
2122 goto restart;
2123 }
2124 goto error;
2125 }
2126
0d9c5d77
DG
2127 nb_fd = ret;
2128
e316aad5 2129 /* From here, the event is a metadata wait fd */
fb3a43a9
DG
2130 for (i = 0; i < nb_fd; i++) {
2131 revents = LTTNG_POLL_GETEV(&events, i);
2132 pollfd = LTTNG_POLL_GETFD(&events, i);
2133
e316aad5
DG
2134 /* Just don't waste time if no returned events for the fd */
2135 if (!revents) {
2136 continue;
2137 }
2138
fb3a43a9 2139 if (pollfd == ctx->consumer_metadata_pipe[0]) {
4adabd61 2140 if (revents & (LPOLLERR | LPOLLHUP )) {
fb3a43a9
DG
2141 DBG("Metadata thread pipe hung up");
2142 /*
2143 * Remove the pipe from the poll set and continue the loop
2144 * since their might be data to consume.
2145 */
2146 lttng_poll_del(&events, ctx->consumer_metadata_pipe[0]);
f66c074c
DG
2147 ret = close(ctx->consumer_metadata_pipe[0]);
2148 if (ret < 0) {
2149 PERROR("close metadata pipe");
2150 }
fb3a43a9
DG
2151 continue;
2152 } else if (revents & LPOLLIN) {
fb3a43a9 2153 do {
633d0084
DG
2154 /* Get the stream pointer received */
2155 ret = read(pollfd, &stream, sizeof(stream));
fb3a43a9 2156 } while (ret < 0 && errno == EINTR);
633d0084
DG
2157 if (ret < 0 ||
2158 ret < sizeof(struct lttng_consumer_stream *)) {
fb3a43a9 2159 PERROR("read metadata stream");
fb3a43a9
DG
2160 /*
2161 * Let's continue here and hope we can still work
2162 * without stopping the consumer. XXX: Should we?
2163 */
2164 continue;
2165 }
2166
8994307f
DG
2167 /* A NULL stream means that the state has changed. */
2168 if (stream == NULL) {
2169 /* Check for deleted streams. */
2170 validate_endpoint_status_metadata_stream(&events);
2171 continue;
2172 }
2173
fb3a43a9
DG
2174 DBG("Adding metadata stream %d to poll set",
2175 stream->wait_fd);
2176
e316aad5
DG
2177 ret = consumer_add_metadata_stream(stream, metadata_ht);
2178 if (ret) {
2179 ERR("Unable to add metadata stream");
2180 /* Stream was not setup properly. Continuing. */
2181 consumer_del_metadata_stream(stream, NULL);
2182 continue;
2183 }
fb3a43a9
DG
2184
2185 /* Add metadata stream to the global poll events list */
2186 lttng_poll_add(&events, stream->wait_fd,
2187 LPOLLIN | LPOLLPRI);
fb3a43a9
DG
2188 }
2189
e316aad5 2190 /* Handle other stream */
fb3a43a9
DG
2191 continue;
2192 }
2193
d09e1200 2194 rcu_read_lock();
fb3a43a9
DG
2195 lttng_ht_lookup(metadata_ht, (void *)((unsigned long) pollfd),
2196 &iter);
2197 node = lttng_ht_iter_get_node_ulong(&iter);
e316aad5 2198 assert(node);
fb3a43a9
DG
2199
2200 stream = caa_container_of(node, struct lttng_consumer_stream,
58b1f425 2201 node);
fb3a43a9 2202
e316aad5 2203 /* Check for error event */
4adabd61 2204 if (revents & (LPOLLERR | LPOLLHUP)) {
e316aad5 2205 DBG("Metadata fd %d is hup|err.", pollfd);
fb3a43a9
DG
2206 if (!stream->hangup_flush_done
2207 && (consumer_data.type == LTTNG_CONSUMER32_UST
2208 || consumer_data.type == LTTNG_CONSUMER64_UST)) {
2209 DBG("Attempting to flush and consume the UST buffers");
2210 lttng_ustconsumer_on_stream_hangup(stream);
2211
2212 /* We just flushed the stream now read it. */
4bb94b75
DG
2213 do {
2214 len = ctx->on_buffer_ready(stream, ctx);
2215 /*
2216 * We don't check the return value here since if we get
2217 * a negative len, it means an error occured thus we
2218 * simply remove it from the poll set and free the
2219 * stream.
2220 */
2221 } while (len > 0);
fb3a43a9
DG
2222 }
2223
fb3a43a9 2224 lttng_poll_del(&events, stream->wait_fd);
e316aad5
DG
2225 /*
2226 * This call update the channel states, closes file descriptors
2227 * and securely free the stream.
2228 */
2229 consumer_del_metadata_stream(stream, metadata_ht);
2230 } else if (revents & (LPOLLIN | LPOLLPRI)) {
2231 /* Get the data out of the metadata file descriptor */
2232 DBG("Metadata available on fd %d", pollfd);
2233 assert(stream->wait_fd == pollfd);
2234
2235 len = ctx->on_buffer_ready(stream, ctx);
2236 /* It's ok to have an unavailable sub-buffer */
b64403e3 2237 if (len < 0 && len != -EAGAIN && len != -ENODATA) {
ab1027f4
DG
2238 /* Clean up stream from consumer and free it. */
2239 lttng_poll_del(&events, stream->wait_fd);
2240 consumer_del_metadata_stream(stream, metadata_ht);
e316aad5
DG
2241 } else if (len > 0) {
2242 stream->data_read = 1;
2243 }
fb3a43a9 2244 }
e316aad5
DG
2245
2246 /* Release RCU lock for the stream looked up */
d09e1200 2247 rcu_read_unlock();
fb3a43a9
DG
2248 }
2249 }
2250
2251error:
2252end:
2253 DBG("Metadata poll thread exiting");
2254 lttng_poll_clean(&events);
2255
04bb2b64 2256 destroy_stream_ht(metadata_ht);
fb3a43a9
DG
2257
2258 rcu_unregister_thread();
2259 return NULL;
2260}
2261
3bd1e081 2262/*
e4421fec 2263 * This thread polls the fds in the set to consume the data and write
3bd1e081
MD
2264 * it to tracefile if necessary.
2265 */
7d980def 2266void *consumer_thread_data_poll(void *data)
3bd1e081
MD
2267{
2268 int num_rdy, num_hup, high_prio, ret, i;
2269 struct pollfd *pollfd = NULL;
2270 /* local view of the streams */
c869f647 2271 struct lttng_consumer_stream **local_stream = NULL, *new_stream = NULL;
3bd1e081
MD
2272 /* local view of consumer_data.fds_count */
2273 int nb_fd = 0;
3bd1e081 2274 struct lttng_consumer_local_data *ctx = data;
00e2e675 2275 ssize_t len;
3bd1e081 2276
e7b994a3
DG
2277 rcu_register_thread();
2278
43c34bc3
DG
2279 data_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
2280 if (data_ht == NULL) {
04bb2b64 2281 /* ENOMEM at this point. Better to bail out. */
43c34bc3
DG
2282 goto end;
2283 }
2284
effcf122 2285 local_stream = zmalloc(sizeof(struct lttng_consumer_stream));
3bd1e081
MD
2286
2287 while (1) {
2288 high_prio = 0;
2289 num_hup = 0;
2290
2291 /*
e4421fec 2292 * the fds set has been updated, we need to update our
3bd1e081
MD
2293 * local array as well
2294 */
2295 pthread_mutex_lock(&consumer_data.lock);
2296 if (consumer_data.need_update) {
2297 if (pollfd != NULL) {
2298 free(pollfd);
2299 pollfd = NULL;
2300 }
2301 if (local_stream != NULL) {
2302 free(local_stream);
2303 local_stream = NULL;
2304 }
2305
50f8ae69 2306 /* allocate for all fds + 1 for the consumer_data_pipe */
effcf122 2307 pollfd = zmalloc((consumer_data.stream_count + 1) * sizeof(struct pollfd));
3bd1e081 2308 if (pollfd == NULL) {
7a57cf92 2309 PERROR("pollfd malloc");
3bd1e081
MD
2310 pthread_mutex_unlock(&consumer_data.lock);
2311 goto end;
2312 }
2313
50f8ae69 2314 /* allocate for all fds + 1 for the consumer_data_pipe */
effcf122 2315 local_stream = zmalloc((consumer_data.stream_count + 1) *
3bd1e081
MD
2316 sizeof(struct lttng_consumer_stream));
2317 if (local_stream == NULL) {
7a57cf92 2318 PERROR("local_stream malloc");
3bd1e081
MD
2319 pthread_mutex_unlock(&consumer_data.lock);
2320 goto end;
2321 }
43c34bc3
DG
2322 ret = consumer_update_poll_array(ctx, &pollfd, local_stream,
2323 data_ht);
3bd1e081
MD
2324 if (ret < 0) {
2325 ERR("Error in allocating pollfd or local_outfds");
f73fabfd 2326 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_POLL_ERROR);
3bd1e081
MD
2327 pthread_mutex_unlock(&consumer_data.lock);
2328 goto end;
2329 }
2330 nb_fd = ret;
2331 consumer_data.need_update = 0;
2332 }
2333 pthread_mutex_unlock(&consumer_data.lock);
2334
4078b776
MD
2335 /* No FDs and consumer_quit, consumer_cleanup the thread */
2336 if (nb_fd == 0 && consumer_quit == 1) {
2337 goto end;
2338 }
3bd1e081 2339 /* poll on the array of fds */
88f2b785 2340 restart:
3bd1e081 2341 DBG("polling on %d fd", nb_fd + 1);
cb365c03 2342 num_rdy = poll(pollfd, nb_fd + 1, -1);
3bd1e081
MD
2343 DBG("poll num_rdy : %d", num_rdy);
2344 if (num_rdy == -1) {
88f2b785
MD
2345 /*
2346 * Restart interrupted system call.
2347 */
2348 if (errno == EINTR) {
2349 goto restart;
2350 }
7a57cf92 2351 PERROR("Poll error");
f73fabfd 2352 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_POLL_ERROR);
3bd1e081
MD
2353 goto end;
2354 } else if (num_rdy == 0) {
2355 DBG("Polling thread timed out");
2356 goto end;
2357 }
2358
3bd1e081 2359 /*
50f8ae69 2360 * If the consumer_data_pipe triggered poll go directly to the
00e2e675
DG
2361 * beginning of the loop to update the array. We want to prioritize
2362 * array update over low-priority reads.
3bd1e081 2363 */
509bb1cf 2364 if (pollfd[nb_fd].revents & (POLLIN | POLLPRI)) {
04fdd819 2365 size_t pipe_readlen;
04fdd819 2366
50f8ae69 2367 DBG("consumer_data_pipe wake up");
04fdd819
MD
2368 /* Consume 1 byte of pipe data */
2369 do {
50f8ae69 2370 pipe_readlen = read(ctx->consumer_data_pipe[0], &new_stream,
c869f647 2371 sizeof(new_stream));
04fdd819 2372 } while (pipe_readlen == -1 && errno == EINTR);
23f5f35d
DG
2373 if (pipe_readlen < 0) {
2374 PERROR("read consumer data pipe");
2375 /* Continue so we can at least handle the current stream(s). */
2376 continue;
2377 }
c869f647
DG
2378
2379 /*
2380 * If the stream is NULL, just ignore it. It's also possible that
2381 * the sessiond poll thread changed the consumer_quit state and is
2382 * waking us up to test it.
2383 */
2384 if (new_stream == NULL) {
8994307f 2385 validate_endpoint_status_data_stream();
c869f647
DG
2386 continue;
2387 }
2388
43c34bc3 2389 ret = consumer_add_stream(new_stream, data_ht);
c869f647
DG
2390 if (ret) {
2391 ERR("Consumer add stream %d failed. Continuing",
2392 new_stream->key);
2393 /*
2394 * At this point, if the add_stream fails, it is not in the
2395 * hash table thus passing the NULL value here.
2396 */
2397 consumer_del_stream(new_stream, NULL);
2398 }
2399
2400 /* Continue to update the local streams and handle prio ones */
3bd1e081
MD
2401 continue;
2402 }
2403
2404 /* Take care of high priority channels first. */
2405 for (i = 0; i < nb_fd; i++) {
9617607b
DG
2406 if (local_stream[i] == NULL) {
2407 continue;
2408 }
fb3a43a9 2409 if (pollfd[i].revents & POLLPRI) {
d41f73b7
MD
2410 DBG("Urgent read on fd %d", pollfd[i].fd);
2411 high_prio = 1;
4078b776 2412 len = ctx->on_buffer_ready(local_stream[i], ctx);
d41f73b7 2413 /* it's ok to have an unavailable sub-buffer */
b64403e3 2414 if (len < 0 && len != -EAGAIN && len != -ENODATA) {
ab1027f4
DG
2415 /* Clean the stream and free it. */
2416 consumer_del_stream(local_stream[i], data_ht);
9617607b 2417 local_stream[i] = NULL;
4078b776
MD
2418 } else if (len > 0) {
2419 local_stream[i]->data_read = 1;
d41f73b7 2420 }
3bd1e081
MD
2421 }
2422 }
2423
4078b776
MD
2424 /*
2425 * If we read high prio channel in this loop, try again
2426 * for more high prio data.
2427 */
2428 if (high_prio) {
3bd1e081
MD
2429 continue;
2430 }
2431
2432 /* Take care of low priority channels. */
4078b776 2433 for (i = 0; i < nb_fd; i++) {
9617607b
DG
2434 if (local_stream[i] == NULL) {
2435 continue;
2436 }
4078b776
MD
2437 if ((pollfd[i].revents & POLLIN) ||
2438 local_stream[i]->hangup_flush_done) {
4078b776
MD
2439 DBG("Normal read on fd %d", pollfd[i].fd);
2440 len = ctx->on_buffer_ready(local_stream[i], ctx);
2441 /* it's ok to have an unavailable sub-buffer */
b64403e3 2442 if (len < 0 && len != -EAGAIN && len != -ENODATA) {
ab1027f4
DG
2443 /* Clean the stream and free it. */
2444 consumer_del_stream(local_stream[i], data_ht);
9617607b 2445 local_stream[i] = NULL;
4078b776
MD
2446 } else if (len > 0) {
2447 local_stream[i]->data_read = 1;
2448 }
2449 }
2450 }
2451
2452 /* Handle hangup and errors */
2453 for (i = 0; i < nb_fd; i++) {
9617607b
DG
2454 if (local_stream[i] == NULL) {
2455 continue;
2456 }
4078b776
MD
2457 if (!local_stream[i]->hangup_flush_done
2458 && (pollfd[i].revents & (POLLHUP | POLLERR | POLLNVAL))
2459 && (consumer_data.type == LTTNG_CONSUMER32_UST
2460 || consumer_data.type == LTTNG_CONSUMER64_UST)) {
2461 DBG("fd %d is hup|err|nval. Attempting flush and read.",
9617607b 2462 pollfd[i].fd);
4078b776
MD
2463 lttng_ustconsumer_on_stream_hangup(local_stream[i]);
2464 /* Attempt read again, for the data we just flushed. */
2465 local_stream[i]->data_read = 1;
2466 }
2467 /*
2468 * If the poll flag is HUP/ERR/NVAL and we have
2469 * read no data in this pass, we can remove the
2470 * stream from its hash table.
2471 */
2472 if ((pollfd[i].revents & POLLHUP)) {
2473 DBG("Polling fd %d tells it has hung up.", pollfd[i].fd);
2474 if (!local_stream[i]->data_read) {
43c34bc3 2475 consumer_del_stream(local_stream[i], data_ht);
9617607b 2476 local_stream[i] = NULL;
4078b776
MD
2477 num_hup++;
2478 }
2479 } else if (pollfd[i].revents & POLLERR) {
2480 ERR("Error returned in polling fd %d.", pollfd[i].fd);
2481 if (!local_stream[i]->data_read) {
43c34bc3 2482 consumer_del_stream(local_stream[i], data_ht);
9617607b 2483 local_stream[i] = NULL;
4078b776
MD
2484 num_hup++;
2485 }
2486 } else if (pollfd[i].revents & POLLNVAL) {
2487 ERR("Polling fd %d tells fd is not open.", pollfd[i].fd);
2488 if (!local_stream[i]->data_read) {
43c34bc3 2489 consumer_del_stream(local_stream[i], data_ht);
9617607b 2490 local_stream[i] = NULL;
4078b776 2491 num_hup++;
3bd1e081
MD
2492 }
2493 }
9617607b
DG
2494 if (local_stream[i] != NULL) {
2495 local_stream[i]->data_read = 0;
2496 }
3bd1e081
MD
2497 }
2498 }
2499end:
2500 DBG("polling thread exiting");
2501 if (pollfd != NULL) {
2502 free(pollfd);
2503 pollfd = NULL;
2504 }
2505 if (local_stream != NULL) {
2506 free(local_stream);
2507 local_stream = NULL;
2508 }
fb3a43a9
DG
2509
2510 /*
2511 * Close the write side of the pipe so epoll_wait() in
7d980def
DG
2512 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2513 * read side of the pipe. If we close them both, epoll_wait strangely does
2514 * not return and could create a endless wait period if the pipe is the
2515 * only tracked fd in the poll set. The thread will take care of closing
2516 * the read side.
fb3a43a9 2517 */
f66c074c
DG
2518 ret = close(ctx->consumer_metadata_pipe[1]);
2519 if (ret < 0) {
2520 PERROR("close data pipe");
2521 }
fb3a43a9 2522
04bb2b64 2523 destroy_data_stream_ht(data_ht);
43c34bc3 2524
e7b994a3 2525 rcu_unregister_thread();
3bd1e081
MD
2526 return NULL;
2527}
2528
2529/*
2530 * This thread listens on the consumerd socket and receives the file
2531 * descriptors from the session daemon.
2532 */
7d980def 2533void *consumer_thread_sessiond_poll(void *data)
3bd1e081 2534{
d96f09c6 2535 int sock = -1, client_socket, ret;
3bd1e081
MD
2536 /*
2537 * structure to poll for incoming data on communication socket avoids
2538 * making blocking sockets.
2539 */
2540 struct pollfd consumer_sockpoll[2];
2541 struct lttng_consumer_local_data *ctx = data;
2542
e7b994a3
DG
2543 rcu_register_thread();
2544
3bd1e081
MD
2545 DBG("Creating command socket %s", ctx->consumer_command_sock_path);
2546 unlink(ctx->consumer_command_sock_path);
2547 client_socket = lttcomm_create_unix_sock(ctx->consumer_command_sock_path);
2548 if (client_socket < 0) {
2549 ERR("Cannot create command socket");
2550 goto end;
2551 }
2552
2553 ret = lttcomm_listen_unix_sock(client_socket);
2554 if (ret < 0) {
2555 goto end;
2556 }
2557
32258573 2558 DBG("Sending ready command to lttng-sessiond");
f73fabfd 2559 ret = lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY);
3bd1e081
MD
2560 /* return < 0 on error, but == 0 is not fatal */
2561 if (ret < 0) {
32258573 2562 ERR("Error sending ready command to lttng-sessiond");
3bd1e081
MD
2563 goto end;
2564 }
2565
2566 ret = fcntl(client_socket, F_SETFL, O_NONBLOCK);
2567 if (ret < 0) {
7a57cf92 2568 PERROR("fcntl O_NONBLOCK");
3bd1e081
MD
2569 goto end;
2570 }
2571
2572 /* prepare the FDs to poll : to client socket and the should_quit pipe */
2573 consumer_sockpoll[0].fd = ctx->consumer_should_quit[0];
2574 consumer_sockpoll[0].events = POLLIN | POLLPRI;
2575 consumer_sockpoll[1].fd = client_socket;
2576 consumer_sockpoll[1].events = POLLIN | POLLPRI;
2577
2578 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
2579 goto end;
2580 }
2581 DBG("Connection on client_socket");
2582
2583 /* Blocking call, waiting for transmission */
2584 sock = lttcomm_accept_unix_sock(client_socket);
2585 if (sock <= 0) {
2586 WARN("On accept");
2587 goto end;
2588 }
2589 ret = fcntl(sock, F_SETFL, O_NONBLOCK);
2590 if (ret < 0) {
7a57cf92 2591 PERROR("fcntl O_NONBLOCK");
3bd1e081
MD
2592 goto end;
2593 }
2594
d96f09c6
DG
2595 /* This socket is not useful anymore. */
2596 ret = close(client_socket);
2597 if (ret < 0) {
2598 PERROR("close client_socket");
2599 }
2600 client_socket = -1;
2601
3bd1e081
MD
2602 /* update the polling structure to poll on the established socket */
2603 consumer_sockpoll[1].fd = sock;
2604 consumer_sockpoll[1].events = POLLIN | POLLPRI;
2605
2606 while (1) {
2607 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
2608 goto end;
2609 }
2610 DBG("Incoming command on sock");
2611 ret = lttng_consumer_recv_cmd(ctx, sock, consumer_sockpoll);
2612 if (ret == -ENOENT) {
2613 DBG("Received STOP command");
2614 goto end;
2615 }
4cbc1a04
DG
2616 if (ret <= 0) {
2617 /*
2618 * This could simply be a session daemon quitting. Don't output
2619 * ERR() here.
2620 */
2621 DBG("Communication interrupted on command socket");
3bd1e081
MD
2622 goto end;
2623 }
2624 if (consumer_quit) {
2625 DBG("consumer_thread_receive_fds received quit from signal");
2626 goto end;
2627 }
2628 DBG("received fds on sock");
2629 }
2630end:
2631 DBG("consumer_thread_receive_fds exiting");
2632
2633 /*
2634 * when all fds have hung up, the polling thread
2635 * can exit cleanly
2636 */
2637 consumer_quit = 1;
2638
04fdd819 2639 /*
c869f647 2640 * Notify the data poll thread to poll back again and test the
8994307f 2641 * consumer_quit state that we just set so to quit gracefully.
04fdd819 2642 */
8994307f 2643 notify_thread_pipe(ctx->consumer_data_pipe[1]);
c869f647 2644
d96f09c6
DG
2645 /* Cleaning up possibly open sockets. */
2646 if (sock >= 0) {
2647 ret = close(sock);
2648 if (ret < 0) {
2649 PERROR("close sock sessiond poll");
2650 }
2651 }
2652 if (client_socket >= 0) {
2653 ret = close(sock);
2654 if (ret < 0) {
2655 PERROR("close client_socket sessiond poll");
2656 }
2657 }
2658
e7b994a3 2659 rcu_unregister_thread();
3bd1e081
MD
2660 return NULL;
2661}
d41f73b7 2662
4078b776 2663ssize_t lttng_consumer_read_subbuffer(struct lttng_consumer_stream *stream,
d41f73b7
MD
2664 struct lttng_consumer_local_data *ctx)
2665{
74251bb8
DG
2666 ssize_t ret;
2667
2668 pthread_mutex_lock(&stream->lock);
2669
d41f73b7
MD
2670 switch (consumer_data.type) {
2671 case LTTNG_CONSUMER_KERNEL:
74251bb8
DG
2672 ret = lttng_kconsumer_read_subbuffer(stream, ctx);
2673 break;
7753dea8
MD
2674 case LTTNG_CONSUMER32_UST:
2675 case LTTNG_CONSUMER64_UST:
74251bb8
DG
2676 ret = lttng_ustconsumer_read_subbuffer(stream, ctx);
2677 break;
d41f73b7
MD
2678 default:
2679 ERR("Unknown consumer_data type");
2680 assert(0);
74251bb8
DG
2681 ret = -ENOSYS;
2682 break;
d41f73b7 2683 }
74251bb8
DG
2684
2685 pthread_mutex_unlock(&stream->lock);
2686 return ret;
d41f73b7
MD
2687}
2688
2689int lttng_consumer_on_recv_stream(struct lttng_consumer_stream *stream)
2690{
2691 switch (consumer_data.type) {
2692 case LTTNG_CONSUMER_KERNEL:
2693 return lttng_kconsumer_on_recv_stream(stream);
7753dea8
MD
2694 case LTTNG_CONSUMER32_UST:
2695 case LTTNG_CONSUMER64_UST:
d41f73b7
MD
2696 return lttng_ustconsumer_on_recv_stream(stream);
2697 default:
2698 ERR("Unknown consumer_data type");
2699 assert(0);
2700 return -ENOSYS;
2701 }
2702}
e4421fec
DG
2703
2704/*
2705 * Allocate and set consumer data hash tables.
2706 */
2707void lttng_consumer_init(void)
2708{
e4421fec 2709 consumer_data.channel_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
00e2e675 2710 consumer_data.relayd_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
53632229 2711 consumer_data.stream_list_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
46e6455f 2712 relayd_session_id_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
e4421fec 2713}
7735ef9e
DG
2714
2715/*
2716 * Process the ADD_RELAYD command receive by a consumer.
2717 *
2718 * This will create a relayd socket pair and add it to the relayd hash table.
2719 * The caller MUST acquire a RCU read side lock before calling it.
2720 */
2721int consumer_add_relayd_socket(int net_seq_idx, int sock_type,
2722 struct lttng_consumer_local_data *ctx, int sock,
46e6455f
DG
2723 struct pollfd *consumer_sockpoll, struct lttcomm_sock *relayd_sock,
2724 unsigned int sessiond_id)
7735ef9e 2725{
cd2b09ed 2726 int fd = -1, ret = -1, relayd_created = 0;
f50f23d9 2727 enum lttng_error_code ret_code = LTTNG_OK;
7735ef9e 2728 struct consumer_relayd_sock_pair *relayd;
46e6455f 2729 struct consumer_relayd_session_id *relayd_id_node;
7735ef9e
DG
2730
2731 DBG("Consumer adding relayd socket (idx: %d)", net_seq_idx);
2732
f50f23d9
DG
2733 /* First send a status message before receiving the fds. */
2734 ret = consumer_send_status_msg(sock, ret_code);
2735 if (ret < 0) {
2736 /* Somehow, the session daemon is not responding anymore. */
2737 goto error;
2738 }
2739
7735ef9e
DG
2740 /* Get relayd reference if exists. */
2741 relayd = consumer_find_relayd(net_seq_idx);
2742 if (relayd == NULL) {
2743 /* Not found. Allocate one. */
2744 relayd = consumer_allocate_relayd_sock_pair(net_seq_idx);
2745 if (relayd == NULL) {
f73fabfd 2746 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
7735ef9e
DG
2747 goto error;
2748 }
f7079f67 2749 relayd->sessiond_session_id = (uint64_t) sessiond_id;
cd2b09ed 2750 relayd_created = 1;
7735ef9e
DG
2751 }
2752
2753 /* Poll on consumer socket. */
2754 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
2755 ret = -EINTR;
2756 goto error;
2757 }
2758
2759 /* Get relayd socket from session daemon */
2760 ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1);
2761 if (ret != sizeof(fd)) {
f73fabfd 2762 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD);
7735ef9e 2763 ret = -1;
4028eeb9 2764 fd = -1; /* Just in case it gets set with an invalid value. */
7735ef9e
DG
2765 goto error;
2766 }
2767
f50f23d9
DG
2768 /* We have the fds without error. Send status back. */
2769 ret = consumer_send_status_msg(sock, ret_code);
2770 if (ret < 0) {
2771 /* Somehow, the session daemon is not responding anymore. */
2772 goto error;
2773 }
2774
7735ef9e
DG
2775 /* Copy socket information and received FD */
2776 switch (sock_type) {
2777 case LTTNG_STREAM_CONTROL:
2778 /* Copy received lttcomm socket */
2779 lttcomm_copy_sock(&relayd->control_sock, relayd_sock);
2780 ret = lttcomm_create_sock(&relayd->control_sock);
4028eeb9
DG
2781 /* Immediately try to close the created socket if valid. */
2782 if (relayd->control_sock.fd >= 0) {
2783 if (close(relayd->control_sock.fd)) {
2784 PERROR("close relayd control socket");
2785 }
7735ef9e 2786 }
4028eeb9 2787 /* Handle create_sock error. */
f66c074c 2788 if (ret < 0) {
4028eeb9 2789 goto error;
f66c074c 2790 }
7735ef9e
DG
2791
2792 /* Assign new file descriptor */
2793 relayd->control_sock.fd = fd;
c5b6f4f0
DG
2794
2795 /*
2796 * Create a session on the relayd and store the returned id. No need to
2797 * grab the socket lock since the relayd object is not yet visible.
2798 */
2799 ret = relayd_create_session(&relayd->control_sock,
f7079f67 2800 &relayd->relayd_session_id);
c5b6f4f0
DG
2801 if (ret < 0) {
2802 goto error;
2803 }
2804
46e6455f
DG
2805 /* Set up a relayd session id node. */
2806 relayd_id_node = zmalloc(sizeof(struct consumer_relayd_session_id));
2807 if (!relayd_id_node) {
2808 PERROR("zmalloc relayd id node");
2809 goto error;
2810 }
2811
f7079f67 2812 relayd_id_node->relayd_id = relayd->relayd_session_id;
46e6455f
DG
2813 relayd_id_node->sessiond_id = (uint64_t) sessiond_id;
2814
f7079f67 2815 /* Indexed by session id of the sessiond. */
46e6455f
DG
2816 lttng_ht_node_init_ulong(&relayd_id_node->node,
2817 relayd_id_node->sessiond_id);
2818 rcu_read_lock();
2819 lttng_ht_add_unique_ulong(relayd_session_id_ht, &relayd_id_node->node);
2820 rcu_read_unlock();
2821
7735ef9e
DG
2822 break;
2823 case LTTNG_STREAM_DATA:
2824 /* Copy received lttcomm socket */
2825 lttcomm_copy_sock(&relayd->data_sock, relayd_sock);
2826 ret = lttcomm_create_sock(&relayd->data_sock);
4028eeb9
DG
2827 /* Immediately try to close the created socket if valid. */
2828 if (relayd->data_sock.fd >= 0) {
2829 if (close(relayd->data_sock.fd)) {
2830 PERROR("close relayd data socket");
2831 }
7735ef9e 2832 }
4028eeb9 2833 /* Handle create_sock error. */
f66c074c 2834 if (ret < 0) {
4028eeb9 2835 goto error;
f66c074c 2836 }
7735ef9e
DG
2837
2838 /* Assign new file descriptor */
2839 relayd->data_sock.fd = fd;
2840 break;
2841 default:
2842 ERR("Unknown relayd socket type (%d)", sock_type);
2843 goto error;
2844 }
2845
2846 DBG("Consumer %s socket created successfully with net idx %d (fd: %d)",
2847 sock_type == LTTNG_STREAM_CONTROL ? "control" : "data",
2848 relayd->net_seq_idx, fd);
2849
2850 /*
2851 * Add relayd socket pair to consumer data hashtable. If object already
2852 * exists or on error, the function gracefully returns.
2853 */
d09e1200 2854 add_relayd(relayd);
7735ef9e
DG
2855
2856 /* All good! */
4028eeb9 2857 return 0;
7735ef9e
DG
2858
2859error:
4028eeb9
DG
2860 /* Close received socket if valid. */
2861 if (fd >= 0) {
2862 if (close(fd)) {
2863 PERROR("close received socket");
2864 }
2865 }
cd2b09ed
DG
2866
2867 if (relayd_created) {
2868 /* We just want to cleanup. Ignore ret value. */
2869 (void) relayd_close(&relayd->control_sock);
2870 (void) relayd_close(&relayd->data_sock);
2871 free(relayd);
2872 }
2873
7735ef9e
DG
2874 return ret;
2875}
ca22feea 2876
4e9a4686
DG
2877/*
2878 * Try to lock the stream mutex.
2879 *
2880 * On success, 1 is returned else 0 indicating that the mutex is NOT lock.
2881 */
2882static int stream_try_lock(struct lttng_consumer_stream *stream)
2883{
2884 int ret;
2885
2886 assert(stream);
2887
2888 /*
2889 * Try to lock the stream mutex. On failure, we know that the stream is
2890 * being used else where hence there is data still being extracted.
2891 */
2892 ret = pthread_mutex_trylock(&stream->lock);
2893 if (ret) {
2894 /* For both EBUSY and EINVAL error, the mutex is NOT locked. */
2895 ret = 0;
2896 goto end;
2897 }
2898
2899 ret = 1;
2900
2901end:
2902 return ret;
2903}
2904
f7079f67
DG
2905/*
2906 * Search for a relayd associated to the session id and return the reference.
2907 *
2908 * A rcu read side lock MUST be acquire before calling this function and locked
2909 * until the relayd object is no longer necessary.
2910 */
2911static struct consumer_relayd_sock_pair *find_relayd_by_session_id(uint64_t id)
2912{
2913 struct lttng_ht_iter iter;
2914 struct lttng_ht_node_ulong *node;
2915 struct consumer_relayd_sock_pair *relayd = NULL;
2916 struct consumer_relayd_session_id *session_id_map;
2917
2918 /* Get the session id map. */
2919 lttng_ht_lookup(relayd_session_id_ht, (void *)((unsigned long) id), &iter);
2920 node = lttng_ht_iter_get_node_ulong(&iter);
2921 if (node == NULL) {
2922 goto end;
2923 }
2924
2925 session_id_map = caa_container_of(node, struct consumer_relayd_session_id,
2926 node);
2927
2928 /* Iterate over all relayd since they are indexed by net_seq_idx. */
2929 cds_lfht_for_each_entry(consumer_data.relayd_ht->ht, &iter.iter, relayd,
2930 node.node) {
2931 if (relayd->relayd_session_id == session_id_map->relayd_id) {
2932 /* Found the relayd. There can be only one per id. */
2933 break;
2934 }
2935 }
2936
2937end:
2938 return relayd;
2939}
2940
ca22feea
DG
2941/*
2942 * Check if for a given session id there is still data needed to be extract
2943 * from the buffers.
2944 *
6d805429 2945 * Return 1 if data is pending or else 0 meaning ready to be read.
ca22feea 2946 */
6d805429 2947int consumer_data_pending(uint64_t id)
ca22feea
DG
2948{
2949 int ret;
2950 struct lttng_ht_iter iter;
2951 struct lttng_ht *ht;
2952 struct lttng_consumer_stream *stream;
f7079f67 2953 struct consumer_relayd_sock_pair *relayd = NULL;
6d805429 2954 int (*data_pending)(struct lttng_consumer_stream *);
ca22feea 2955
6d805429 2956 DBG("Consumer data pending command on session id %" PRIu64, id);
ca22feea 2957
6f6eda74 2958 rcu_read_lock();
ca22feea
DG
2959 pthread_mutex_lock(&consumer_data.lock);
2960
2961 switch (consumer_data.type) {
2962 case LTTNG_CONSUMER_KERNEL:
6d805429 2963 data_pending = lttng_kconsumer_data_pending;
ca22feea
DG
2964 break;
2965 case LTTNG_CONSUMER32_UST:
2966 case LTTNG_CONSUMER64_UST:
6d805429 2967 data_pending = lttng_ustconsumer_data_pending;
ca22feea
DG
2968 break;
2969 default:
2970 ERR("Unknown consumer data type");
2971 assert(0);
2972 }
2973
2974 /* Ease our life a bit */
2975 ht = consumer_data.stream_list_ht;
2976
f7079f67
DG
2977 relayd = find_relayd_by_session_id(id);
2978 if (relayd) {
2979 /* Send init command for data pending. */
2980 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
2981 ret = relayd_begin_data_pending(&relayd->control_sock,
2982 relayd->relayd_session_id);
2983 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
2984 if (ret < 0) {
2985 /* Communication error thus the relayd so no data pending. */
2986 goto data_not_pending;
2987 }
2988 }
2989
c8f59ee5 2990 cds_lfht_for_each_entry_duplicate(ht->ht,
b6314938 2991 ht->hash_fct((void *)((unsigned long) id), lttng_ht_seed),
ca22feea
DG
2992 ht->match_fct, (void *)((unsigned long) id),
2993 &iter.iter, stream, node_session_id.node) {
4e9a4686
DG
2994 /* If this call fails, the stream is being used hence data pending. */
2995 ret = stream_try_lock(stream);
2996 if (!ret) {
f7079f67 2997 goto data_pending;
ca22feea 2998 }
ca22feea 2999
4e9a4686
DG
3000 /*
3001 * A removed node from the hash table indicates that the stream has
3002 * been deleted thus having a guarantee that the buffers are closed
3003 * on the consumer side. However, data can still be transmitted
3004 * over the network so don't skip the relayd check.
3005 */
3006 ret = cds_lfht_is_node_deleted(&stream->node.node);
3007 if (!ret) {
3008 /* Check the stream if there is data in the buffers. */
6d805429
DG
3009 ret = data_pending(stream);
3010 if (ret == 1) {
4e9a4686 3011 pthread_mutex_unlock(&stream->lock);
f7079f67 3012 goto data_pending;
4e9a4686
DG
3013 }
3014 }
3015
3016 /* Relayd check */
f7079f67 3017 if (relayd) {
c8f59ee5
DG
3018 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
3019 if (stream->metadata_flag) {
ad7051c0
DG
3020 ret = relayd_quiescent_control(&relayd->control_sock,
3021 stream->relayd_stream_id);
c8f59ee5 3022 } else {
6d805429 3023 ret = relayd_data_pending(&relayd->control_sock,
c8f59ee5
DG
3024 stream->relayd_stream_id, stream->next_net_seq_num);
3025 }
3026 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
6d805429 3027 if (ret == 1) {
4e9a4686 3028 pthread_mutex_unlock(&stream->lock);
f7079f67 3029 goto data_pending;
c8f59ee5
DG
3030 }
3031 }
4e9a4686 3032 pthread_mutex_unlock(&stream->lock);
c8f59ee5 3033 }
ca22feea 3034
f7079f67
DG
3035 if (relayd) {
3036 unsigned int is_data_inflight = 0;
3037
3038 /* Send init command for data pending. */
3039 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
3040 ret = relayd_end_data_pending(&relayd->control_sock,
3041 relayd->relayd_session_id, &is_data_inflight);
3042 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
bdd88757 3043 if (ret < 0) {
f7079f67
DG
3044 goto data_not_pending;
3045 }
bdd88757
DG
3046 if (is_data_inflight) {
3047 goto data_pending;
3048 }
f7079f67
DG
3049 }
3050
ca22feea 3051 /*
f7079f67
DG
3052 * Finding _no_ node in the hash table and no inflight data means that the
3053 * stream(s) have been removed thus data is guaranteed to be available for
3054 * analysis from the trace files.
ca22feea
DG
3055 */
3056
f7079f67 3057data_not_pending:
ca22feea
DG
3058 /* Data is available to be read by a viewer. */
3059 pthread_mutex_unlock(&consumer_data.lock);
c8f59ee5 3060 rcu_read_unlock();
6d805429 3061 return 0;
ca22feea 3062
f7079f67 3063data_pending:
ca22feea
DG
3064 /* Data is still being extracted from buffers. */
3065 pthread_mutex_unlock(&consumer_data.lock);
c8f59ee5 3066 rcu_read_unlock();
6d805429 3067 return 1;
ca22feea 3068}
f50f23d9
DG
3069
3070/*
3071 * Send a ret code status message to the sessiond daemon.
3072 *
3073 * Return the sendmsg() return value.
3074 */
3075int consumer_send_status_msg(int sock, int ret_code)
3076{
3077 struct lttcomm_consumer_status_msg msg;
3078
3079 msg.ret_code = ret_code;
3080
3081 return lttcomm_send_unix_sock(sock, &msg, sizeof(msg));
3082}
This page took 0.19259 seconds and 5 git commands to generate.