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