Fix: Remove LPOLLNVAL from consumer metadata revents
[lttng-tools.git] / src / common / consumer.c
CommitLineData
3bd1e081
MD
1/*
2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
00e2e675 4 * 2012 - David Goulet <dgoulet@efficios.com>
3bd1e081 5 *
d14d33bf
AM
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
3bd1e081 9 *
d14d33bf
AM
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
3bd1e081 14 *
d14d33bf
AM
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
3bd1e081
MD
18 */
19
20#define _GNU_SOURCE
21#include <assert.h>
3bd1e081
MD
22#include <poll.h>
23#include <pthread.h>
24#include <stdlib.h>
25#include <string.h>
26#include <sys/mman.h>
27#include <sys/socket.h>
28#include <sys/types.h>
29#include <unistd.h>
77c7c900 30#include <inttypes.h>
3bd1e081 31
990570ed 32#include <common/common.h>
fb3a43a9
DG
33#include <common/utils.h>
34#include <common/compat/poll.h>
10a8a223 35#include <common/kernel-ctl/kernel-ctl.h>
00e2e675 36#include <common/sessiond-comm/relayd.h>
10a8a223
DG
37#include <common/sessiond-comm/sessiond-comm.h>
38#include <common/kernel-consumer/kernel-consumer.h>
00e2e675 39#include <common/relayd/relayd.h>
10a8a223
DG
40#include <common/ust-consumer/ust-consumer.h>
41
42#include "consumer.h"
3bd1e081
MD
43
44struct lttng_consumer_global_data consumer_data = {
3bd1e081
MD
45 .stream_count = 0,
46 .need_update = 1,
47 .type = LTTNG_CONSUMER_UNKNOWN,
48};
49
50/* timeout parameter, to control the polling thread grace period. */
51int consumer_poll_timeout = -1;
52
53/*
54 * Flag to inform the polling thread to quit when all fd hung up. Updated by
55 * the consumer_thread_receive_fds when it notices that all fds has hung up.
56 * Also updated by the signal handler (consumer_should_exit()). Read by the
57 * polling threads.
58 */
59volatile int consumer_quit = 0;
60
61/*
62 * Find a stream. The consumer_data.lock must be locked during this
63 * call.
64 */
65static struct lttng_consumer_stream *consumer_find_stream(int key)
66{
e4421fec
DG
67 struct lttng_ht_iter iter;
68 struct lttng_ht_node_ulong *node;
69 struct lttng_consumer_stream *stream = NULL;
3bd1e081 70
7ad0a0cb
MD
71 /* Negative keys are lookup failures */
72 if (key < 0)
73 return NULL;
e4421fec 74
6065ceec
DG
75 rcu_read_lock();
76
e4421fec
DG
77 lttng_ht_lookup(consumer_data.stream_ht, (void *)((unsigned long) key),
78 &iter);
79 node = lttng_ht_iter_get_node_ulong(&iter);
80 if (node != NULL) {
81 stream = caa_container_of(node, struct lttng_consumer_stream, node);
3bd1e081 82 }
e4421fec 83
6065ceec
DG
84 rcu_read_unlock();
85
e4421fec 86 return stream;
3bd1e081
MD
87}
88
7ad0a0cb
MD
89static void consumer_steal_stream_key(int key)
90{
91 struct lttng_consumer_stream *stream;
92
04253271 93 rcu_read_lock();
7ad0a0cb 94 stream = consumer_find_stream(key);
04253271 95 if (stream) {
7ad0a0cb 96 stream->key = -1;
04253271
MD
97 /*
98 * We don't want the lookup to match, but we still need
99 * to iterate on this stream when iterating over the hash table. Just
100 * change the node key.
101 */
102 stream->node.key = -1;
103 }
104 rcu_read_unlock();
7ad0a0cb
MD
105}
106
3bd1e081
MD
107static struct lttng_consumer_channel *consumer_find_channel(int key)
108{
e4421fec
DG
109 struct lttng_ht_iter iter;
110 struct lttng_ht_node_ulong *node;
111 struct lttng_consumer_channel *channel = NULL;
3bd1e081 112
7ad0a0cb
MD
113 /* Negative keys are lookup failures */
114 if (key < 0)
115 return NULL;
e4421fec 116
6065ceec
DG
117 rcu_read_lock();
118
e4421fec
DG
119 lttng_ht_lookup(consumer_data.channel_ht, (void *)((unsigned long) key),
120 &iter);
121 node = lttng_ht_iter_get_node_ulong(&iter);
122 if (node != NULL) {
123 channel = caa_container_of(node, struct lttng_consumer_channel, node);
3bd1e081 124 }
e4421fec 125
6065ceec
DG
126 rcu_read_unlock();
127
e4421fec 128 return channel;
3bd1e081
MD
129}
130
7ad0a0cb
MD
131static void consumer_steal_channel_key(int key)
132{
133 struct lttng_consumer_channel *channel;
134
04253271 135 rcu_read_lock();
7ad0a0cb 136 channel = consumer_find_channel(key);
04253271 137 if (channel) {
7ad0a0cb 138 channel->key = -1;
04253271
MD
139 /*
140 * We don't want the lookup to match, but we still need
141 * to iterate on this channel when iterating over the hash table. Just
142 * change the node key.
143 */
144 channel->node.key = -1;
145 }
146 rcu_read_unlock();
7ad0a0cb
MD
147}
148
702b1ea4
MD
149static
150void consumer_free_stream(struct rcu_head *head)
151{
152 struct lttng_ht_node_ulong *node =
153 caa_container_of(head, struct lttng_ht_node_ulong, head);
154 struct lttng_consumer_stream *stream =
155 caa_container_of(node, struct lttng_consumer_stream, node);
156
157 free(stream);
158}
159
00e2e675
DG
160/*
161 * RCU protected relayd socket pair free.
162 */
163static void consumer_rcu_free_relayd(struct rcu_head *head)
164{
165 struct lttng_ht_node_ulong *node =
166 caa_container_of(head, struct lttng_ht_node_ulong, head);
167 struct consumer_relayd_sock_pair *relayd =
168 caa_container_of(node, struct consumer_relayd_sock_pair, node);
169
170 free(relayd);
171}
172
173/*
174 * Destroy and free relayd socket pair object.
175 *
176 * This function MUST be called with the consumer_data lock acquired.
177 */
178void consumer_destroy_relayd(struct consumer_relayd_sock_pair *relayd)
179{
180 int ret;
181 struct lttng_ht_iter iter;
182
173af62f
DG
183 if (relayd == NULL) {
184 return;
185 }
186
00e2e675
DG
187 DBG("Consumer destroy and close relayd socket pair");
188
189 iter.iter.node = &relayd->node.node;
190 ret = lttng_ht_del(consumer_data.relayd_ht, &iter);
173af62f
DG
191 if (ret != 0) {
192 /* We assume the relayd was already destroyed */
193 return;
194 }
00e2e675
DG
195
196 /* Close all sockets */
197 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
198 (void) relayd_close(&relayd->control_sock);
199 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
200 (void) relayd_close(&relayd->data_sock);
201
202 /* RCU free() call */
203 call_rcu(&relayd->node.head, consumer_rcu_free_relayd);
204}
205
a6ba4fe1
DG
206/*
207 * Flag a relayd socket pair for destruction. Destroy it if the refcount
208 * reaches zero.
209 *
210 * RCU read side lock MUST be aquired before calling this function.
211 */
212void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair *relayd)
213{
214 assert(relayd);
215
216 /* Set destroy flag for this object */
217 uatomic_set(&relayd->destroy_flag, 1);
218
219 /* Destroy the relayd if refcount is 0 */
220 if (uatomic_read(&relayd->refcount) == 0) {
221 consumer_destroy_relayd(relayd);
222 }
223}
224
3bd1e081
MD
225/*
226 * Remove a stream from the global list protected by a mutex. This
227 * function is also responsible for freeing its data structures.
228 */
229void consumer_del_stream(struct lttng_consumer_stream *stream)
230{
231 int ret;
e4421fec 232 struct lttng_ht_iter iter;
3bd1e081 233 struct lttng_consumer_channel *free_chan = NULL;
00e2e675
DG
234 struct consumer_relayd_sock_pair *relayd;
235
236 assert(stream);
3bd1e081
MD
237
238 pthread_mutex_lock(&consumer_data.lock);
239
240 switch (consumer_data.type) {
241 case LTTNG_CONSUMER_KERNEL:
242 if (stream->mmap_base != NULL) {
243 ret = munmap(stream->mmap_base, stream->mmap_len);
244 if (ret != 0) {
245 perror("munmap");
246 }
247 }
248 break;
7753dea8
MD
249 case LTTNG_CONSUMER32_UST:
250 case LTTNG_CONSUMER64_UST:
3bd1e081
MD
251 lttng_ustconsumer_del_stream(stream);
252 break;
253 default:
254 ERR("Unknown consumer_data type");
255 assert(0);
256 goto end;
257 }
258
6065ceec 259 rcu_read_lock();
04253271
MD
260 iter.iter.node = &stream->node.node;
261 ret = lttng_ht_del(consumer_data.stream_ht, &iter);
262 assert(!ret);
e4421fec 263
6065ceec
DG
264 rcu_read_unlock();
265
3bd1e081
MD
266 if (consumer_data.stream_count <= 0) {
267 goto end;
268 }
269 consumer_data.stream_count--;
270 if (!stream) {
271 goto end;
272 }
273 if (stream->out_fd >= 0) {
4c462e79
MD
274 ret = close(stream->out_fd);
275 if (ret) {
276 PERROR("close");
277 }
3bd1e081 278 }
b5c5fc29 279 if (stream->wait_fd >= 0 && !stream->wait_fd_is_copy) {
4c462e79
MD
280 ret = close(stream->wait_fd);
281 if (ret) {
282 PERROR("close");
283 }
3bd1e081 284 }
2c1dd183 285 if (stream->shm_fd >= 0 && stream->wait_fd != stream->shm_fd) {
4c462e79
MD
286 ret = close(stream->shm_fd);
287 if (ret) {
288 PERROR("close");
289 }
3bd1e081 290 }
00e2e675
DG
291
292 /* Check and cleanup relayd */
b0b335c8 293 rcu_read_lock();
00e2e675
DG
294 relayd = consumer_find_relayd(stream->net_seq_idx);
295 if (relayd != NULL) {
b0b335c8
MD
296 uatomic_dec(&relayd->refcount);
297 assert(uatomic_read(&relayd->refcount) >= 0);
173af62f 298
3f8e211f
DG
299 /* Closing streams requires to lock the control socket. */
300 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
173af62f
DG
301 ret = relayd_send_close_stream(&relayd->control_sock,
302 stream->relayd_stream_id,
303 stream->next_net_seq_num - 1);
3f8e211f 304 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
173af62f 305 if (ret < 0) {
a4b92340
DG
306 DBG("Unable to close stream on the relayd. Continuing");
307 /*
308 * Continue here. There is nothing we can do for the relayd.
309 * Chances are that the relayd has closed the socket so we just
310 * continue cleaning up.
311 */
173af62f
DG
312 }
313
314 /* Both conditions are met, we destroy the relayd. */
315 if (uatomic_read(&relayd->refcount) == 0 &&
316 uatomic_read(&relayd->destroy_flag)) {
00e2e675
DG
317 consumer_destroy_relayd(relayd);
318 }
00e2e675 319 }
b0b335c8 320 rcu_read_unlock();
00e2e675
DG
321
322 if (!--stream->chan->refcount) {
3bd1e081 323 free_chan = stream->chan;
00e2e675
DG
324 }
325
702b1ea4
MD
326
327 call_rcu(&stream->node.head, consumer_free_stream);
3bd1e081
MD
328end:
329 consumer_data.need_update = 1;
330 pthread_mutex_unlock(&consumer_data.lock);
331
332 if (free_chan)
333 consumer_del_channel(free_chan);
334}
335
336struct lttng_consumer_stream *consumer_allocate_stream(
337 int channel_key, int stream_key,
338 int shm_fd, int wait_fd,
339 enum lttng_consumer_stream_state state,
340 uint64_t mmap_len,
341 enum lttng_event_output output,
6df2e2c9
MD
342 const char *path_name,
343 uid_t uid,
00e2e675
DG
344 gid_t gid,
345 int net_index,
346 int metadata_flag)
3bd1e081
MD
347{
348 struct lttng_consumer_stream *stream;
349 int ret;
350
effcf122 351 stream = zmalloc(sizeof(*stream));
3bd1e081
MD
352 if (stream == NULL) {
353 perror("malloc struct lttng_consumer_stream");
354 goto end;
355 }
356 stream->chan = consumer_find_channel(channel_key);
357 if (!stream->chan) {
358 perror("Unable to find channel key");
359 goto end;
360 }
361 stream->chan->refcount++;
362 stream->key = stream_key;
363 stream->shm_fd = shm_fd;
364 stream->wait_fd = wait_fd;
365 stream->out_fd = -1;
366 stream->out_fd_offset = 0;
367 stream->state = state;
368 stream->mmap_len = mmap_len;
369 stream->mmap_base = NULL;
370 stream->output = output;
6df2e2c9
MD
371 stream->uid = uid;
372 stream->gid = gid;
00e2e675
DG
373 stream->net_seq_idx = net_index;
374 stream->metadata_flag = metadata_flag;
375 strncpy(stream->path_name, path_name, sizeof(stream->path_name));
376 stream->path_name[sizeof(stream->path_name) - 1] = '\0';
e4421fec 377 lttng_ht_node_init_ulong(&stream->node, stream->key);
00e2e675 378 lttng_ht_node_init_ulong(&stream->waitfd_node, stream->wait_fd);
3bd1e081
MD
379
380 switch (consumer_data.type) {
381 case LTTNG_CONSUMER_KERNEL:
382 break;
7753dea8
MD
383 case LTTNG_CONSUMER32_UST:
384 case LTTNG_CONSUMER64_UST:
5af2f756 385 stream->cpu = stream->chan->cpucount++;
3bd1e081
MD
386 ret = lttng_ustconsumer_allocate_stream(stream);
387 if (ret) {
388 free(stream);
389 return NULL;
390 }
391 break;
392 default:
393 ERR("Unknown consumer_data type");
394 assert(0);
395 goto end;
396 }
00e2e675 397 DBG("Allocated stream %s (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, out_fd %d, net_seq_idx %d)",
3bd1e081
MD
398 stream->path_name, stream->key,
399 stream->shm_fd,
400 stream->wait_fd,
401 (unsigned long long) stream->mmap_len,
00e2e675
DG
402 stream->out_fd,
403 stream->net_seq_idx);
3bd1e081
MD
404end:
405 return stream;
406}
407
408/*
409 * Add a stream to the global list protected by a mutex.
410 */
411int consumer_add_stream(struct lttng_consumer_stream *stream)
412{
413 int ret = 0;
c77fc10a
DG
414 struct lttng_ht_node_ulong *node;
415 struct lttng_ht_iter iter;
00e2e675 416 struct consumer_relayd_sock_pair *relayd;
3bd1e081
MD
417
418 pthread_mutex_lock(&consumer_data.lock);
7ad0a0cb
MD
419 /* Steal stream identifier, for UST */
420 consumer_steal_stream_key(stream->key);
c77fc10a 421
b0b335c8 422 rcu_read_lock();
c77fc10a
DG
423 lttng_ht_lookup(consumer_data.stream_ht,
424 (void *)((unsigned long) stream->key), &iter);
425 node = lttng_ht_iter_get_node_ulong(&iter);
426 if (node != NULL) {
427 rcu_read_unlock();
428 /* Stream already exist. Ignore the insertion */
429 goto end;
430 }
431
04253271 432 lttng_ht_add_unique_ulong(consumer_data.stream_ht, &stream->node);
00e2e675
DG
433
434 /* Check and cleanup relayd */
435 relayd = consumer_find_relayd(stream->net_seq_idx);
436 if (relayd != NULL) {
b0b335c8 437 uatomic_inc(&relayd->refcount);
00e2e675 438 }
b0b335c8 439 rcu_read_unlock();
00e2e675
DG
440
441 /* Update consumer data */
3bd1e081
MD
442 consumer_data.stream_count++;
443 consumer_data.need_update = 1;
444
3bd1e081
MD
445end:
446 pthread_mutex_unlock(&consumer_data.lock);
702b1ea4 447
3bd1e081
MD
448 return ret;
449}
450
00e2e675 451/*
3f8e211f
DG
452 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
453 * be acquired before calling this.
00e2e675 454 */
3f8e211f 455
00e2e675
DG
456int consumer_add_relayd(struct consumer_relayd_sock_pair *relayd)
457{
458 int ret = 0;
459 struct lttng_ht_node_ulong *node;
460 struct lttng_ht_iter iter;
461
462 if (relayd == NULL) {
463 ret = -1;
464 goto end;
465 }
466
00e2e675
DG
467 lttng_ht_lookup(consumer_data.relayd_ht,
468 (void *)((unsigned long) relayd->net_seq_idx), &iter);
469 node = lttng_ht_iter_get_node_ulong(&iter);
470 if (node != NULL) {
00e2e675
DG
471 /* Relayd already exist. Ignore the insertion */
472 goto end;
473 }
474 lttng_ht_add_unique_ulong(consumer_data.relayd_ht, &relayd->node);
475
00e2e675
DG
476end:
477 return ret;
478}
479
480/*
481 * Allocate and return a consumer relayd socket.
482 */
483struct consumer_relayd_sock_pair *consumer_allocate_relayd_sock_pair(
484 int net_seq_idx)
485{
486 struct consumer_relayd_sock_pair *obj = NULL;
487
488 /* Negative net sequence index is a failure */
489 if (net_seq_idx < 0) {
490 goto error;
491 }
492
493 obj = zmalloc(sizeof(struct consumer_relayd_sock_pair));
494 if (obj == NULL) {
495 PERROR("zmalloc relayd sock");
496 goto error;
497 }
498
499 obj->net_seq_idx = net_seq_idx;
500 obj->refcount = 0;
173af62f 501 obj->destroy_flag = 0;
00e2e675
DG
502 lttng_ht_node_init_ulong(&obj->node, obj->net_seq_idx);
503 pthread_mutex_init(&obj->ctrl_sock_mutex, NULL);
504
505error:
506 return obj;
507}
508
509/*
510 * Find a relayd socket pair in the global consumer data.
511 *
512 * Return the object if found else NULL.
b0b335c8
MD
513 * RCU read-side lock must be held across this call and while using the
514 * returned object.
00e2e675
DG
515 */
516struct consumer_relayd_sock_pair *consumer_find_relayd(int key)
517{
518 struct lttng_ht_iter iter;
519 struct lttng_ht_node_ulong *node;
520 struct consumer_relayd_sock_pair *relayd = NULL;
521
522 /* Negative keys are lookup failures */
523 if (key < 0) {
524 goto error;
525 }
526
00e2e675
DG
527 lttng_ht_lookup(consumer_data.relayd_ht, (void *)((unsigned long) key),
528 &iter);
529 node = lttng_ht_iter_get_node_ulong(&iter);
530 if (node != NULL) {
531 relayd = caa_container_of(node, struct consumer_relayd_sock_pair, node);
532 }
533
00e2e675
DG
534error:
535 return relayd;
536}
537
538/*
539 * Handle stream for relayd transmission if the stream applies for network
540 * streaming where the net sequence index is set.
541 *
542 * Return destination file descriptor or negative value on error.
543 */
6197aea7 544static int write_relayd_stream_header(struct lttng_consumer_stream *stream,
1d4dfdef
DG
545 size_t data_size, unsigned long padding,
546 struct consumer_relayd_sock_pair *relayd)
00e2e675
DG
547{
548 int outfd = -1, ret;
00e2e675
DG
549 struct lttcomm_relayd_data_hdr data_hdr;
550
551 /* Safety net */
552 assert(stream);
6197aea7 553 assert(relayd);
00e2e675
DG
554
555 /* Reset data header */
556 memset(&data_hdr, 0, sizeof(data_hdr));
557
00e2e675
DG
558 if (stream->metadata_flag) {
559 /* Caller MUST acquire the relayd control socket lock */
560 ret = relayd_send_metadata(&relayd->control_sock, data_size);
561 if (ret < 0) {
562 goto error;
563 }
564
565 /* Metadata are always sent on the control socket. */
566 outfd = relayd->control_sock.fd;
567 } else {
568 /* Set header with stream information */
569 data_hdr.stream_id = htobe64(stream->relayd_stream_id);
570 data_hdr.data_size = htobe32(data_size);
1d4dfdef 571 data_hdr.padding_size = htobe32(padding);
173af62f 572 data_hdr.net_seq_num = htobe64(stream->next_net_seq_num++);
00e2e675
DG
573 /* Other fields are zeroed previously */
574
575 ret = relayd_send_data_hdr(&relayd->data_sock, &data_hdr,
576 sizeof(data_hdr));
577 if (ret < 0) {
578 goto error;
579 }
580
581 /* Set to go on data socket */
582 outfd = relayd->data_sock.fd;
583 }
584
585error:
586 return outfd;
587}
588
3bd1e081
MD
589/*
590 * Update a stream according to what we just received.
591 */
592void consumer_change_stream_state(int stream_key,
593 enum lttng_consumer_stream_state state)
594{
595 struct lttng_consumer_stream *stream;
596
597 pthread_mutex_lock(&consumer_data.lock);
598 stream = consumer_find_stream(stream_key);
599 if (stream) {
600 stream->state = state;
601 }
602 consumer_data.need_update = 1;
603 pthread_mutex_unlock(&consumer_data.lock);
604}
605
702b1ea4
MD
606static
607void consumer_free_channel(struct rcu_head *head)
608{
609 struct lttng_ht_node_ulong *node =
610 caa_container_of(head, struct lttng_ht_node_ulong, head);
611 struct lttng_consumer_channel *channel =
612 caa_container_of(node, struct lttng_consumer_channel, node);
613
614 free(channel);
615}
616
3bd1e081
MD
617/*
618 * Remove a channel from the global list protected by a mutex. This
619 * function is also responsible for freeing its data structures.
620 */
621void consumer_del_channel(struct lttng_consumer_channel *channel)
622{
623 int ret;
e4421fec 624 struct lttng_ht_iter iter;
3bd1e081
MD
625
626 pthread_mutex_lock(&consumer_data.lock);
627
628 switch (consumer_data.type) {
629 case LTTNG_CONSUMER_KERNEL:
630 break;
7753dea8
MD
631 case LTTNG_CONSUMER32_UST:
632 case LTTNG_CONSUMER64_UST:
3bd1e081
MD
633 lttng_ustconsumer_del_channel(channel);
634 break;
635 default:
636 ERR("Unknown consumer_data type");
637 assert(0);
638 goto end;
639 }
640
6065ceec 641 rcu_read_lock();
04253271
MD
642 iter.iter.node = &channel->node.node;
643 ret = lttng_ht_del(consumer_data.channel_ht, &iter);
644 assert(!ret);
6065ceec
DG
645 rcu_read_unlock();
646
3bd1e081
MD
647 if (channel->mmap_base != NULL) {
648 ret = munmap(channel->mmap_base, channel->mmap_len);
649 if (ret != 0) {
650 perror("munmap");
651 }
652 }
b5c5fc29 653 if (channel->wait_fd >= 0 && !channel->wait_fd_is_copy) {
4c462e79
MD
654 ret = close(channel->wait_fd);
655 if (ret) {
656 PERROR("close");
657 }
3bd1e081 658 }
2c1dd183 659 if (channel->shm_fd >= 0 && channel->wait_fd != channel->shm_fd) {
4c462e79
MD
660 ret = close(channel->shm_fd);
661 if (ret) {
662 PERROR("close");
663 }
3bd1e081 664 }
702b1ea4
MD
665
666 call_rcu(&channel->node.head, consumer_free_channel);
3bd1e081
MD
667end:
668 pthread_mutex_unlock(&consumer_data.lock);
669}
670
671struct lttng_consumer_channel *consumer_allocate_channel(
672 int channel_key,
673 int shm_fd, int wait_fd,
674 uint64_t mmap_len,
675 uint64_t max_sb_size)
676{
677 struct lttng_consumer_channel *channel;
678 int ret;
679
276b26d1 680 channel = zmalloc(sizeof(*channel));
3bd1e081
MD
681 if (channel == NULL) {
682 perror("malloc struct lttng_consumer_channel");
683 goto end;
684 }
685 channel->key = channel_key;
686 channel->shm_fd = shm_fd;
687 channel->wait_fd = wait_fd;
688 channel->mmap_len = mmap_len;
689 channel->max_sb_size = max_sb_size;
690 channel->refcount = 0;
e4421fec 691 lttng_ht_node_init_ulong(&channel->node, channel->key);
3bd1e081
MD
692
693 switch (consumer_data.type) {
694 case LTTNG_CONSUMER_KERNEL:
695 channel->mmap_base = NULL;
696 channel->mmap_len = 0;
697 break;
7753dea8
MD
698 case LTTNG_CONSUMER32_UST:
699 case LTTNG_CONSUMER64_UST:
3bd1e081
MD
700 ret = lttng_ustconsumer_allocate_channel(channel);
701 if (ret) {
702 free(channel);
703 return NULL;
704 }
705 break;
706 default:
707 ERR("Unknown consumer_data type");
708 assert(0);
709 goto end;
710 }
711 DBG("Allocated channel (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, max_sb_size %llu)",
00e2e675 712 channel->key, channel->shm_fd, channel->wait_fd,
3bd1e081
MD
713 (unsigned long long) channel->mmap_len,
714 (unsigned long long) channel->max_sb_size);
715end:
716 return channel;
717}
718
719/*
720 * Add a channel to the global list protected by a mutex.
721 */
722int consumer_add_channel(struct lttng_consumer_channel *channel)
723{
c77fc10a
DG
724 struct lttng_ht_node_ulong *node;
725 struct lttng_ht_iter iter;
726
3bd1e081 727 pthread_mutex_lock(&consumer_data.lock);
7ad0a0cb
MD
728 /* Steal channel identifier, for UST */
729 consumer_steal_channel_key(channel->key);
6065ceec 730 rcu_read_lock();
c77fc10a
DG
731
732 lttng_ht_lookup(consumer_data.channel_ht,
733 (void *)((unsigned long) channel->key), &iter);
734 node = lttng_ht_iter_get_node_ulong(&iter);
735 if (node != NULL) {
736 /* Channel already exist. Ignore the insertion */
737 goto end;
738 }
739
04253271 740 lttng_ht_add_unique_ulong(consumer_data.channel_ht, &channel->node);
c77fc10a
DG
741
742end:
6065ceec 743 rcu_read_unlock();
3bd1e081 744 pthread_mutex_unlock(&consumer_data.lock);
702b1ea4 745
7ad0a0cb 746 return 0;
3bd1e081
MD
747}
748
749/*
750 * Allocate the pollfd structure and the local view of the out fds to avoid
751 * doing a lookup in the linked list and concurrency issues when writing is
752 * needed. Called with consumer_data.lock held.
753 *
754 * Returns the number of fds in the structures.
755 */
756int consumer_update_poll_array(
757 struct lttng_consumer_local_data *ctx, struct pollfd **pollfd,
fb3a43a9 758 struct lttng_consumer_stream **local_stream)
3bd1e081 759{
3bd1e081 760 int i = 0;
e4421fec
DG
761 struct lttng_ht_iter iter;
762 struct lttng_consumer_stream *stream;
3bd1e081
MD
763
764 DBG("Updating poll fd array");
481d6c57 765 rcu_read_lock();
e4421fec
DG
766 cds_lfht_for_each_entry(consumer_data.stream_ht->ht, &iter.iter, stream,
767 node.node) {
768 if (stream->state != LTTNG_CONSUMER_ACTIVE_STREAM) {
3bd1e081
MD
769 continue;
770 }
e4421fec
DG
771 DBG("Active FD %d", stream->wait_fd);
772 (*pollfd)[i].fd = stream->wait_fd;
3bd1e081 773 (*pollfd)[i].events = POLLIN | POLLPRI;
e4421fec 774 local_stream[i] = stream;
3bd1e081
MD
775 i++;
776 }
481d6c57 777 rcu_read_unlock();
3bd1e081
MD
778
779 /*
780 * Insert the consumer_poll_pipe at the end of the array and don't
781 * increment i so nb_fd is the number of real FD.
782 */
783 (*pollfd)[i].fd = ctx->consumer_poll_pipe[0];
509bb1cf 784 (*pollfd)[i].events = POLLIN | POLLPRI;
3bd1e081
MD
785 return i;
786}
787
788/*
789 * Poll on the should_quit pipe and the command socket return -1 on error and
790 * should exit, 0 if data is available on the command socket
791 */
792int lttng_consumer_poll_socket(struct pollfd *consumer_sockpoll)
793{
794 int num_rdy;
795
88f2b785 796restart:
3bd1e081
MD
797 num_rdy = poll(consumer_sockpoll, 2, -1);
798 if (num_rdy == -1) {
88f2b785
MD
799 /*
800 * Restart interrupted system call.
801 */
802 if (errno == EINTR) {
803 goto restart;
804 }
3bd1e081
MD
805 perror("Poll error");
806 goto exit;
807 }
509bb1cf 808 if (consumer_sockpoll[0].revents & (POLLIN | POLLPRI)) {
3bd1e081
MD
809 DBG("consumer_should_quit wake up");
810 goto exit;
811 }
812 return 0;
813
814exit:
815 return -1;
816}
817
818/*
819 * Set the error socket.
820 */
821void lttng_consumer_set_error_sock(
822 struct lttng_consumer_local_data *ctx, int sock)
823{
824 ctx->consumer_error_socket = sock;
825}
826
827/*
828 * Set the command socket path.
829 */
3bd1e081
MD
830void lttng_consumer_set_command_sock_path(
831 struct lttng_consumer_local_data *ctx, char *sock)
832{
833 ctx->consumer_command_sock_path = sock;
834}
835
836/*
837 * Send return code to the session daemon.
838 * If the socket is not defined, we return 0, it is not a fatal error
839 */
840int lttng_consumer_send_error(
841 struct lttng_consumer_local_data *ctx, int cmd)
842{
843 if (ctx->consumer_error_socket > 0) {
844 return lttcomm_send_unix_sock(ctx->consumer_error_socket, &cmd,
845 sizeof(enum lttcomm_sessiond_command));
846 }
847
848 return 0;
849}
850
851/*
852 * Close all the tracefiles and stream fds, should be called when all instances
853 * are destroyed.
854 */
855void lttng_consumer_cleanup(void)
856{
e4421fec 857 struct lttng_ht_iter iter;
6065ceec
DG
858 struct lttng_ht_node_ulong *node;
859
860 rcu_read_lock();
3bd1e081
MD
861
862 /*
6065ceec
DG
863 * close all outfd. Called when there are no more threads running (after
864 * joining on the threads), no need to protect list iteration with mutex.
3bd1e081 865 */
6065ceec
DG
866 cds_lfht_for_each_entry(consumer_data.stream_ht->ht, &iter.iter, node,
867 node) {
702b1ea4
MD
868 struct lttng_consumer_stream *stream =
869 caa_container_of(node, struct lttng_consumer_stream, node);
870 consumer_del_stream(stream);
3bd1e081 871 }
e4421fec 872
6065ceec
DG
873 cds_lfht_for_each_entry(consumer_data.channel_ht->ht, &iter.iter, node,
874 node) {
702b1ea4
MD
875 struct lttng_consumer_channel *channel =
876 caa_container_of(node, struct lttng_consumer_channel, node);
877 consumer_del_channel(channel);
3bd1e081 878 }
6065ceec
DG
879
880 rcu_read_unlock();
d6ce1df2
MD
881
882 lttng_ht_destroy(consumer_data.stream_ht);
883 lttng_ht_destroy(consumer_data.channel_ht);
3bd1e081
MD
884}
885
886/*
887 * Called from signal handler.
888 */
889void lttng_consumer_should_exit(struct lttng_consumer_local_data *ctx)
890{
891 int ret;
892 consumer_quit = 1;
6f94560a
MD
893 do {
894 ret = write(ctx->consumer_should_quit[1], "4", 1);
895 } while (ret < 0 && errno == EINTR);
3bd1e081
MD
896 if (ret < 0) {
897 perror("write consumer quit");
898 }
899}
900
00e2e675
DG
901void lttng_consumer_sync_trace_file(struct lttng_consumer_stream *stream,
902 off_t orig_offset)
3bd1e081
MD
903{
904 int outfd = stream->out_fd;
905
906 /*
907 * This does a blocking write-and-wait on any page that belongs to the
908 * subbuffer prior to the one we just wrote.
909 * Don't care about error values, as these are just hints and ways to
910 * limit the amount of page cache used.
911 */
912 if (orig_offset < stream->chan->max_sb_size) {
913 return;
914 }
b9182dd9 915 lttng_sync_file_range(outfd, orig_offset - stream->chan->max_sb_size,
3bd1e081
MD
916 stream->chan->max_sb_size,
917 SYNC_FILE_RANGE_WAIT_BEFORE
918 | SYNC_FILE_RANGE_WRITE
919 | SYNC_FILE_RANGE_WAIT_AFTER);
920 /*
921 * Give hints to the kernel about how we access the file:
922 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
923 * we write it.
924 *
925 * We need to call fadvise again after the file grows because the
926 * kernel does not seem to apply fadvise to non-existing parts of the
927 * file.
928 *
929 * Call fadvise _after_ having waited for the page writeback to
930 * complete because the dirty page writeback semantic is not well
931 * defined. So it can be expected to lead to lower throughput in
932 * streaming.
933 */
934 posix_fadvise(outfd, orig_offset - stream->chan->max_sb_size,
935 stream->chan->max_sb_size, POSIX_FADV_DONTNEED);
936}
937
938/*
939 * Initialise the necessary environnement :
940 * - create a new context
941 * - create the poll_pipe
942 * - create the should_quit pipe (for signal handler)
943 * - create the thread pipe (for splice)
944 *
945 * Takes a function pointer as argument, this function is called when data is
946 * available on a buffer. This function is responsible to do the
947 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
948 * buffer configuration and then kernctl_put_next_subbuf at the end.
949 *
950 * Returns a pointer to the new context or NULL on error.
951 */
952struct lttng_consumer_local_data *lttng_consumer_create(
953 enum lttng_consumer_type type,
4078b776 954 ssize_t (*buffer_ready)(struct lttng_consumer_stream *stream,
d41f73b7 955 struct lttng_consumer_local_data *ctx),
3bd1e081
MD
956 int (*recv_channel)(struct lttng_consumer_channel *channel),
957 int (*recv_stream)(struct lttng_consumer_stream *stream),
958 int (*update_stream)(int stream_key, uint32_t state))
959{
960 int ret, i;
961 struct lttng_consumer_local_data *ctx;
962
963 assert(consumer_data.type == LTTNG_CONSUMER_UNKNOWN ||
964 consumer_data.type == type);
965 consumer_data.type = type;
966
effcf122 967 ctx = zmalloc(sizeof(struct lttng_consumer_local_data));
3bd1e081
MD
968 if (ctx == NULL) {
969 perror("allocating context");
970 goto error;
971 }
972
973 ctx->consumer_error_socket = -1;
974 /* assign the callbacks */
975 ctx->on_buffer_ready = buffer_ready;
976 ctx->on_recv_channel = recv_channel;
977 ctx->on_recv_stream = recv_stream;
978 ctx->on_update_stream = update_stream;
979
980 ret = pipe(ctx->consumer_poll_pipe);
981 if (ret < 0) {
982 perror("Error creating poll pipe");
983 goto error_poll_pipe;
984 }
985
04fdd819
MD
986 /* set read end of the pipe to non-blocking */
987 ret = fcntl(ctx->consumer_poll_pipe[0], F_SETFL, O_NONBLOCK);
988 if (ret < 0) {
989 perror("fcntl O_NONBLOCK");
990 goto error_poll_fcntl;
991 }
992
993 /* set write end of the pipe to non-blocking */
994 ret = fcntl(ctx->consumer_poll_pipe[1], F_SETFL, O_NONBLOCK);
995 if (ret < 0) {
996 perror("fcntl O_NONBLOCK");
997 goto error_poll_fcntl;
998 }
999
3bd1e081
MD
1000 ret = pipe(ctx->consumer_should_quit);
1001 if (ret < 0) {
1002 perror("Error creating recv pipe");
1003 goto error_quit_pipe;
1004 }
1005
1006 ret = pipe(ctx->consumer_thread_pipe);
1007 if (ret < 0) {
1008 perror("Error creating thread pipe");
1009 goto error_thread_pipe;
1010 }
1011
fb3a43a9
DG
1012 ret = utils_create_pipe(ctx->consumer_metadata_pipe);
1013 if (ret < 0) {
1014 goto error_metadata_pipe;
1015 }
3bd1e081 1016
fb3a43a9
DG
1017 ret = utils_create_pipe(ctx->consumer_splice_metadata_pipe);
1018 if (ret < 0) {
1019 goto error_splice_pipe;
1020 }
1021
1022 return ctx;
3bd1e081 1023
fb3a43a9
DG
1024error_splice_pipe:
1025 utils_close_pipe(ctx->consumer_metadata_pipe);
1026error_metadata_pipe:
1027 utils_close_pipe(ctx->consumer_thread_pipe);
3bd1e081
MD
1028error_thread_pipe:
1029 for (i = 0; i < 2; i++) {
1030 int err;
1031
1032 err = close(ctx->consumer_should_quit[i]);
4c462e79
MD
1033 if (err) {
1034 PERROR("close");
1035 }
3bd1e081 1036 }
04fdd819 1037error_poll_fcntl:
3bd1e081
MD
1038error_quit_pipe:
1039 for (i = 0; i < 2; i++) {
1040 int err;
1041
1042 err = close(ctx->consumer_poll_pipe[i]);
4c462e79
MD
1043 if (err) {
1044 PERROR("close");
1045 }
3bd1e081
MD
1046 }
1047error_poll_pipe:
1048 free(ctx);
1049error:
1050 return NULL;
1051}
1052
1053/*
1054 * Close all fds associated with the instance and free the context.
1055 */
1056void lttng_consumer_destroy(struct lttng_consumer_local_data *ctx)
1057{
4c462e79
MD
1058 int ret;
1059
1060 ret = close(ctx->consumer_error_socket);
1061 if (ret) {
1062 PERROR("close");
1063 }
1064 ret = close(ctx->consumer_thread_pipe[0]);
1065 if (ret) {
1066 PERROR("close");
1067 }
1068 ret = close(ctx->consumer_thread_pipe[1]);
1069 if (ret) {
1070 PERROR("close");
1071 }
1072 ret = close(ctx->consumer_poll_pipe[0]);
1073 if (ret) {
1074 PERROR("close");
1075 }
1076 ret = close(ctx->consumer_poll_pipe[1]);
1077 if (ret) {
1078 PERROR("close");
1079 }
1080 ret = close(ctx->consumer_should_quit[0]);
1081 if (ret) {
1082 PERROR("close");
1083 }
1084 ret = close(ctx->consumer_should_quit[1]);
1085 if (ret) {
1086 PERROR("close");
1087 }
fb3a43a9
DG
1088 utils_close_pipe(ctx->consumer_splice_metadata_pipe);
1089
3bd1e081
MD
1090 unlink(ctx->consumer_command_sock_path);
1091 free(ctx);
1092}
1093
6197aea7
DG
1094/*
1095 * Write the metadata stream id on the specified file descriptor.
1096 */
1097static int write_relayd_metadata_id(int fd,
1098 struct lttng_consumer_stream *stream,
1d4dfdef
DG
1099 struct consumer_relayd_sock_pair *relayd,
1100 unsigned long padding)
6197aea7
DG
1101{
1102 int ret;
1d4dfdef 1103 struct lttcomm_relayd_metadata_payload hdr;
6197aea7 1104
1d4dfdef
DG
1105 hdr.stream_id = htobe64(stream->relayd_stream_id);
1106 hdr.padding_size = htobe32(padding);
6197aea7 1107 do {
1d4dfdef 1108 ret = write(fd, (void *) &hdr, sizeof(hdr));
6197aea7
DG
1109 } while (ret < 0 && errno == EINTR);
1110 if (ret < 0) {
1111 PERROR("write metadata stream id");
1112 goto end;
1113 }
1d4dfdef
DG
1114 DBG("Metadata stream id %" PRIu64 " with padding %lu written before data",
1115 stream->relayd_stream_id, padding);
6197aea7
DG
1116
1117end:
1118 return ret;
1119}
1120
3bd1e081 1121/*
09e26845
DG
1122 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1123 * core function for writing trace buffers to either the local filesystem or
1124 * the network.
1125 *
1126 * Careful review MUST be put if any changes occur!
3bd1e081
MD
1127 *
1128 * Returns the number of bytes written
1129 */
4078b776 1130ssize_t lttng_consumer_on_read_subbuffer_mmap(
3bd1e081 1131 struct lttng_consumer_local_data *ctx,
1d4dfdef
DG
1132 struct lttng_consumer_stream *stream, unsigned long len,
1133 unsigned long padding)
3bd1e081 1134{
f02e1e8a
DG
1135 unsigned long mmap_offset;
1136 ssize_t ret = 0, written = 0;
1137 off_t orig_offset = stream->out_fd_offset;
1138 /* Default is on the disk */
1139 int outfd = stream->out_fd;
f02e1e8a
DG
1140 struct consumer_relayd_sock_pair *relayd = NULL;
1141
1142 /* RCU lock for the relayd pointer */
1143 rcu_read_lock();
1144
1145 /* Flag that the current stream if set for network streaming. */
1146 if (stream->net_seq_idx != -1) {
1147 relayd = consumer_find_relayd(stream->net_seq_idx);
1148 if (relayd == NULL) {
1149 goto end;
1150 }
1151 }
1152
1153 /* get the offset inside the fd to mmap */
3bd1e081
MD
1154 switch (consumer_data.type) {
1155 case LTTNG_CONSUMER_KERNEL:
f02e1e8a
DG
1156 ret = kernctl_get_mmap_read_offset(stream->wait_fd, &mmap_offset);
1157 break;
7753dea8
MD
1158 case LTTNG_CONSUMER32_UST:
1159 case LTTNG_CONSUMER64_UST:
f02e1e8a
DG
1160 ret = lttng_ustctl_get_mmap_read_offset(stream->chan->handle,
1161 stream->buf, &mmap_offset);
1162 break;
3bd1e081
MD
1163 default:
1164 ERR("Unknown consumer_data type");
1165 assert(0);
1166 }
f02e1e8a
DG
1167 if (ret != 0) {
1168 errno = -ret;
1169 PERROR("tracer ctl get_mmap_read_offset");
1170 written = ret;
1171 goto end;
1172 }
b9182dd9 1173
f02e1e8a
DG
1174 /* Handle stream on the relayd if the output is on the network */
1175 if (relayd) {
1176 unsigned long netlen = len;
1177
1178 /*
1179 * Lock the control socket for the complete duration of the function
1180 * since from this point on we will use the socket.
1181 */
1182 if (stream->metadata_flag) {
1183 /* Metadata requires the control socket. */
1184 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
1d4dfdef 1185 netlen += sizeof(struct lttcomm_relayd_metadata_payload);
f02e1e8a
DG
1186 }
1187
1d4dfdef 1188 ret = write_relayd_stream_header(stream, netlen, padding, relayd);
f02e1e8a
DG
1189 if (ret >= 0) {
1190 /* Use the returned socket. */
1191 outfd = ret;
1192
1193 /* Write metadata stream id before payload */
1194 if (stream->metadata_flag) {
1d4dfdef 1195 ret = write_relayd_metadata_id(outfd, stream, relayd, padding);
f02e1e8a 1196 if (ret < 0) {
f02e1e8a
DG
1197 written = ret;
1198 goto end;
1199 }
f02e1e8a
DG
1200 }
1201 }
1202 /* Else, use the default set before which is the filesystem. */
1d4dfdef
DG
1203 } else {
1204 /* No streaming, we have to set the len with the full padding */
1205 len += padding;
f02e1e8a
DG
1206 }
1207
1208 while (len > 0) {
1209 do {
1210 ret = write(outfd, stream->mmap_base + mmap_offset, len);
1211 } while (ret < 0 && errno == EINTR);
1d4dfdef 1212 DBG("Consumer mmap write() ret %zd (len %lu)", ret, len);
f02e1e8a
DG
1213 if (ret < 0) {
1214 PERROR("Error in file write");
1215 if (written == 0) {
1216 written = ret;
1217 }
1218 goto end;
1219 } else if (ret > len) {
77c7c900 1220 PERROR("Error in file write (ret %zd > len %lu)", ret, len);
f02e1e8a
DG
1221 written += ret;
1222 goto end;
1223 } else {
1224 len -= ret;
1225 mmap_offset += ret;
1226 }
f02e1e8a
DG
1227
1228 /* This call is useless on a socket so better save a syscall. */
1229 if (!relayd) {
1230 /* This won't block, but will start writeout asynchronously */
1231 lttng_sync_file_range(outfd, stream->out_fd_offset, ret,
1232 SYNC_FILE_RANGE_WRITE);
1233 stream->out_fd_offset += ret;
1234 }
1235 written += ret;
1236 }
1237 lttng_consumer_sync_trace_file(stream, orig_offset);
1238
1239end:
1240 /* Unlock only if ctrl socket used */
1241 if (relayd && stream->metadata_flag) {
1242 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
1243 }
1244
1245 rcu_read_unlock();
1246 return written;
3bd1e081
MD
1247}
1248
1249/*
1250 * Splice the data from the ring buffer to the tracefile.
1251 *
1252 * Returns the number of bytes spliced.
1253 */
4078b776 1254ssize_t lttng_consumer_on_read_subbuffer_splice(
3bd1e081 1255 struct lttng_consumer_local_data *ctx,
1d4dfdef
DG
1256 struct lttng_consumer_stream *stream, unsigned long len,
1257 unsigned long padding)
3bd1e081 1258{
f02e1e8a
DG
1259 ssize_t ret = 0, written = 0, ret_splice = 0;
1260 loff_t offset = 0;
1261 off_t orig_offset = stream->out_fd_offset;
1262 int fd = stream->wait_fd;
1263 /* Default is on the disk */
1264 int outfd = stream->out_fd;
f02e1e8a 1265 struct consumer_relayd_sock_pair *relayd = NULL;
fb3a43a9 1266 int *splice_pipe;
f02e1e8a 1267
3bd1e081
MD
1268 switch (consumer_data.type) {
1269 case LTTNG_CONSUMER_KERNEL:
f02e1e8a 1270 break;
7753dea8
MD
1271 case LTTNG_CONSUMER32_UST:
1272 case LTTNG_CONSUMER64_UST:
f02e1e8a 1273 /* Not supported for user space tracing */
3bd1e081
MD
1274 return -ENOSYS;
1275 default:
1276 ERR("Unknown consumer_data type");
1277 assert(0);
3bd1e081
MD
1278 }
1279
f02e1e8a
DG
1280 /* RCU lock for the relayd pointer */
1281 rcu_read_lock();
1282
1283 /* Flag that the current stream if set for network streaming. */
1284 if (stream->net_seq_idx != -1) {
1285 relayd = consumer_find_relayd(stream->net_seq_idx);
1286 if (relayd == NULL) {
1287 goto end;
1288 }
1289 }
1290
fb3a43a9
DG
1291 /*
1292 * Choose right pipe for splice. Metadata and trace data are handled by
1293 * different threads hence the use of two pipes in order not to race or
1294 * corrupt the written data.
1295 */
1296 if (stream->metadata_flag) {
1297 splice_pipe = ctx->consumer_splice_metadata_pipe;
1298 } else {
1299 splice_pipe = ctx->consumer_thread_pipe;
1300 }
1301
f02e1e8a 1302 /* Write metadata stream id before payload */
1d4dfdef
DG
1303 if (relayd) {
1304 int total_len = len;
f02e1e8a 1305
1d4dfdef
DG
1306 if (stream->metadata_flag) {
1307 /*
1308 * Lock the control socket for the complete duration of the function
1309 * since from this point on we will use the socket.
1310 */
1311 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
1312
1313 ret = write_relayd_metadata_id(splice_pipe[1], stream, relayd,
1314 padding);
1315 if (ret < 0) {
1316 written = ret;
1317 goto end;
1318 }
1319
1320 total_len += sizeof(struct lttcomm_relayd_metadata_payload);
1321 }
1322
1323 ret = write_relayd_stream_header(stream, total_len, padding, relayd);
1324 if (ret >= 0) {
1325 /* Use the returned socket. */
1326 outfd = ret;
1327 } else {
1328 ERR("Remote relayd disconnected. Stopping");
f02e1e8a
DG
1329 goto end;
1330 }
1d4dfdef
DG
1331 } else {
1332 /* No streaming, we have to set the len with the full padding */
1333 len += padding;
f02e1e8a
DG
1334 }
1335
1336 while (len > 0) {
1d4dfdef
DG
1337 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1338 (unsigned long)offset, len, fd, splice_pipe[1]);
fb3a43a9 1339 ret_splice = splice(fd, &offset, splice_pipe[1], NULL, len,
f02e1e8a
DG
1340 SPLICE_F_MOVE | SPLICE_F_MORE);
1341 DBG("splice chan to pipe, ret %zd", ret_splice);
1342 if (ret_splice < 0) {
1343 PERROR("Error in relay splice");
1344 if (written == 0) {
1345 written = ret_splice;
1346 }
1347 ret = errno;
1348 goto splice_error;
1349 }
1350
1351 /* Handle stream on the relayd if the output is on the network */
1352 if (relayd) {
1353 if (stream->metadata_flag) {
1d4dfdef
DG
1354 size_t metadata_payload_size =
1355 sizeof(struct lttcomm_relayd_metadata_payload);
1356
f02e1e8a 1357 /* Update counter to fit the spliced data */
1d4dfdef
DG
1358 ret_splice += metadata_payload_size;
1359 len += metadata_payload_size;
f02e1e8a
DG
1360 /*
1361 * We do this so the return value can match the len passed as
1362 * argument to this function.
1363 */
1d4dfdef 1364 written -= metadata_payload_size;
f02e1e8a
DG
1365 }
1366 }
1367
1368 /* Splice data out */
fb3a43a9 1369 ret_splice = splice(splice_pipe[0], NULL, outfd, NULL,
f02e1e8a 1370 ret_splice, SPLICE_F_MOVE | SPLICE_F_MORE);
1d4dfdef 1371 DBG("Consumer splice pipe to file, ret %zd", ret_splice);
f02e1e8a
DG
1372 if (ret_splice < 0) {
1373 PERROR("Error in file splice");
1374 if (written == 0) {
1375 written = ret_splice;
1376 }
1377 ret = errno;
1378 goto splice_error;
1379 } else if (ret_splice > len) {
1380 errno = EINVAL;
1381 PERROR("Wrote more data than requested %zd (len: %lu)",
1382 ret_splice, len);
1383 written += ret_splice;
1384 ret = errno;
1385 goto splice_error;
1386 }
1387 len -= ret_splice;
1388
1389 /* This call is useless on a socket so better save a syscall. */
1390 if (!relayd) {
1391 /* This won't block, but will start writeout asynchronously */
1392 lttng_sync_file_range(outfd, stream->out_fd_offset, ret_splice,
1393 SYNC_FILE_RANGE_WRITE);
1394 stream->out_fd_offset += ret_splice;
1395 }
1396 written += ret_splice;
1397 }
1398 lttng_consumer_sync_trace_file(stream, orig_offset);
1399
1400 ret = ret_splice;
1401
1402 goto end;
1403
1404splice_error:
1405 /* send the appropriate error description to sessiond */
1406 switch (ret) {
1407 case EBADF:
f73fabfd 1408 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_EBADF);
f02e1e8a
DG
1409 break;
1410 case EINVAL:
f73fabfd 1411 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_EINVAL);
f02e1e8a
DG
1412 break;
1413 case ENOMEM:
f73fabfd 1414 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_ENOMEM);
f02e1e8a
DG
1415 break;
1416 case ESPIPE:
f73fabfd 1417 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_ESPIPE);
f02e1e8a
DG
1418 break;
1419 }
1420
1421end:
1422 if (relayd && stream->metadata_flag) {
1423 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
1424 }
1425
1426 rcu_read_unlock();
1427 return written;
3bd1e081
MD
1428}
1429
1430/*
1431 * Take a snapshot for a specific fd
1432 *
1433 * Returns 0 on success, < 0 on error
1434 */
1435int lttng_consumer_take_snapshot(struct lttng_consumer_local_data *ctx,
1436 struct lttng_consumer_stream *stream)
1437{
1438 switch (consumer_data.type) {
1439 case LTTNG_CONSUMER_KERNEL:
1440 return lttng_kconsumer_take_snapshot(ctx, stream);
7753dea8
MD
1441 case LTTNG_CONSUMER32_UST:
1442 case LTTNG_CONSUMER64_UST:
3bd1e081
MD
1443 return lttng_ustconsumer_take_snapshot(ctx, stream);
1444 default:
1445 ERR("Unknown consumer_data type");
1446 assert(0);
1447 return -ENOSYS;
1448 }
1449
1450}
1451
1452/*
1453 * Get the produced position
1454 *
1455 * Returns 0 on success, < 0 on error
1456 */
1457int lttng_consumer_get_produced_snapshot(
1458 struct lttng_consumer_local_data *ctx,
1459 struct lttng_consumer_stream *stream,
1460 unsigned long *pos)
1461{
1462 switch (consumer_data.type) {
1463 case LTTNG_CONSUMER_KERNEL:
1464 return lttng_kconsumer_get_produced_snapshot(ctx, stream, pos);
7753dea8
MD
1465 case LTTNG_CONSUMER32_UST:
1466 case LTTNG_CONSUMER64_UST:
3bd1e081
MD
1467 return lttng_ustconsumer_get_produced_snapshot(ctx, stream, pos);
1468 default:
1469 ERR("Unknown consumer_data type");
1470 assert(0);
1471 return -ENOSYS;
1472 }
1473}
1474
1475int lttng_consumer_recv_cmd(struct lttng_consumer_local_data *ctx,
1476 int sock, struct pollfd *consumer_sockpoll)
1477{
1478 switch (consumer_data.type) {
1479 case LTTNG_CONSUMER_KERNEL:
1480 return lttng_kconsumer_recv_cmd(ctx, sock, consumer_sockpoll);
7753dea8
MD
1481 case LTTNG_CONSUMER32_UST:
1482 case LTTNG_CONSUMER64_UST:
3bd1e081
MD
1483 return lttng_ustconsumer_recv_cmd(ctx, sock, consumer_sockpoll);
1484 default:
1485 ERR("Unknown consumer_data type");
1486 assert(0);
1487 return -ENOSYS;
1488 }
1489}
1490
fb3a43a9
DG
1491/*
1492 * Iterate over all stream element of the hashtable and free them. This is race
1493 * free since the hashtable received MUST be in a race free synchronization
1494 * state. It's the caller responsability to make sure of that.
1495 */
1496static void destroy_stream_ht(struct lttng_ht *ht)
1497{
1498 int ret;
1499 struct lttng_ht_iter iter;
1500 struct lttng_consumer_stream *stream;
1501
1502 if (ht == NULL) {
1503 return;
1504 }
1505
1506 cds_lfht_for_each_entry(ht->ht, &iter.iter, stream, node.node) {
1507 ret = lttng_ht_del(ht, &iter);
1508 assert(!ret);
1509
1510 free(stream);
1511 }
1512
1513 lttng_ht_destroy(ht);
1514}
1515
1516/*
1517 * Clean up a metadata stream and free its memory.
1518 */
1519static void consumer_del_metadata_stream(struct lttng_consumer_stream *stream)
1520{
1521 int ret;
1522 struct lttng_consumer_channel *free_chan = NULL;
1523 struct consumer_relayd_sock_pair *relayd;
1524
1525 assert(stream);
1526 /*
1527 * This call should NEVER receive regular stream. It must always be
1528 * metadata stream and this is crucial for data structure synchronization.
1529 */
1530 assert(stream->metadata_flag);
1531
1532 pthread_mutex_lock(&consumer_data.lock);
1533 switch (consumer_data.type) {
1534 case LTTNG_CONSUMER_KERNEL:
1535 if (stream->mmap_base != NULL) {
1536 ret = munmap(stream->mmap_base, stream->mmap_len);
1537 if (ret != 0) {
1538 PERROR("munmap metadata stream");
1539 }
1540 }
1541 break;
1542 case LTTNG_CONSUMER32_UST:
1543 case LTTNG_CONSUMER64_UST:
1544 lttng_ustconsumer_del_stream(stream);
1545 break;
1546 default:
1547 ERR("Unknown consumer_data type");
1548 assert(0);
1549 }
1550 pthread_mutex_unlock(&consumer_data.lock);
1551
1552 if (stream->out_fd >= 0) {
1553 ret = close(stream->out_fd);
1554 if (ret) {
1555 PERROR("close");
1556 }
1557 }
1558
1559 if (stream->wait_fd >= 0 && !stream->wait_fd_is_copy) {
1560 ret = close(stream->wait_fd);
1561 if (ret) {
1562 PERROR("close");
1563 }
1564 }
1565
1566 if (stream->shm_fd >= 0 && stream->wait_fd != stream->shm_fd) {
1567 ret = close(stream->shm_fd);
1568 if (ret) {
1569 PERROR("close");
1570 }
1571 }
1572
1573 /* Check and cleanup relayd */
1574 rcu_read_lock();
1575 relayd = consumer_find_relayd(stream->net_seq_idx);
1576 if (relayd != NULL) {
1577 uatomic_dec(&relayd->refcount);
1578 assert(uatomic_read(&relayd->refcount) >= 0);
1579
1580 /* Closing streams requires to lock the control socket. */
1581 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
1582 ret = relayd_send_close_stream(&relayd->control_sock,
1583 stream->relayd_stream_id, stream->next_net_seq_num - 1);
1584 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
1585 if (ret < 0) {
1586 DBG("Unable to close stream on the relayd. Continuing");
1587 /*
1588 * Continue here. There is nothing we can do for the relayd.
1589 * Chances are that the relayd has closed the socket so we just
1590 * continue cleaning up.
1591 */
1592 }
1593
1594 /* Both conditions are met, we destroy the relayd. */
1595 if (uatomic_read(&relayd->refcount) == 0 &&
1596 uatomic_read(&relayd->destroy_flag)) {
1597 consumer_destroy_relayd(relayd);
1598 }
1599 }
1600 rcu_read_unlock();
1601
1602 /* Atomically decrement channel refcount since other threads can use it. */
1603 uatomic_dec(&stream->chan->refcount);
1604 if (!uatomic_read(&stream->chan->refcount)) {
1605 free_chan = stream->chan;
1606 }
1607
1608 if (free_chan) {
1609 consumer_del_channel(free_chan);
1610 }
1611
1612 free(stream);
1613}
1614
1615/*
1616 * Action done with the metadata stream when adding it to the consumer internal
1617 * data structures to handle it.
1618 */
1619static void consumer_add_metadata_stream(struct lttng_consumer_stream *stream)
1620{
1621 struct consumer_relayd_sock_pair *relayd;
1622
1623 /* Find relayd and, if one is found, increment refcount. */
1624 rcu_read_lock();
1625 relayd = consumer_find_relayd(stream->net_seq_idx);
1626 if (relayd != NULL) {
1627 uatomic_inc(&relayd->refcount);
1628 }
1629 rcu_read_unlock();
1630}
1631
1632/*
1633 * Thread polls on metadata file descriptor and write them on disk or on the
1634 * network.
1635 */
1636void *lttng_consumer_thread_poll_metadata(void *data)
1637{
1638 int ret, i, pollfd;
1639 uint32_t revents, nb_fd;
1640 struct lttng_consumer_stream *stream;
1641 struct lttng_ht_iter iter;
1642 struct lttng_ht_node_ulong *node;
1643 struct lttng_ht *metadata_ht = NULL;
1644 struct lttng_poll_event events;
1645 struct lttng_consumer_local_data *ctx = data;
1646 ssize_t len;
1647
1648 rcu_register_thread();
1649
1650 DBG("Thread metadata poll started");
1651
1652 metadata_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1653 if (metadata_ht == NULL) {
1654 goto end;
1655 }
1656
1657 /* Size is set to 1 for the consumer_metadata pipe */
1658 ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC);
1659 if (ret < 0) {
1660 ERR("Poll set creation failed");
1661 goto end;
1662 }
1663
1664 ret = lttng_poll_add(&events, ctx->consumer_metadata_pipe[0], LPOLLIN);
1665 if (ret < 0) {
1666 goto end;
1667 }
1668
1669 /* Main loop */
1670 DBG("Metadata main loop started");
1671
1672 while (1) {
1673 lttng_poll_reset(&events);
1674
1675 nb_fd = LTTNG_POLL_GETNB(&events);
1676
1677 /* Only the metadata pipe is set */
1678 if (nb_fd == 0 && consumer_quit == 1) {
1679 goto end;
1680 }
1681
1682restart:
1683 DBG("Metadata poll wait with %d fd(s)", nb_fd);
1684 ret = lttng_poll_wait(&events, -1);
1685 DBG("Metadata event catched in thread");
1686 if (ret < 0) {
1687 if (errno == EINTR) {
1688 goto restart;
1689 }
1690 goto error;
1691 }
1692
1693 for (i = 0; i < nb_fd; i++) {
1694 revents = LTTNG_POLL_GETEV(&events, i);
1695 pollfd = LTTNG_POLL_GETFD(&events, i);
1696
1697 /* Check the metadata pipe for incoming metadata. */
1698 if (pollfd == ctx->consumer_metadata_pipe[0]) {
4adabd61 1699 if (revents & (LPOLLERR | LPOLLHUP )) {
fb3a43a9
DG
1700 DBG("Metadata thread pipe hung up");
1701 /*
1702 * Remove the pipe from the poll set and continue the loop
1703 * since their might be data to consume.
1704 */
1705 lttng_poll_del(&events, ctx->consumer_metadata_pipe[0]);
1706 close(ctx->consumer_metadata_pipe[0]);
1707 continue;
1708 } else if (revents & LPOLLIN) {
1709 stream = zmalloc(sizeof(struct lttng_consumer_stream));
1710 if (stream == NULL) {
1711 PERROR("zmalloc metadata consumer stream");
1712 goto error;
1713 }
1714
1715 do {
1716 /* Get the stream and add it to the local hash table */
1717 ret = read(pollfd, stream,
1718 sizeof(struct lttng_consumer_stream));
1719 } while (ret < 0 && errno == EINTR);
1720 if (ret < 0 || ret < sizeof(struct lttng_consumer_stream)) {
1721 PERROR("read metadata stream");
1722 free(stream);
1723 /*
1724 * Let's continue here and hope we can still work
1725 * without stopping the consumer. XXX: Should we?
1726 */
1727 continue;
1728 }
1729
1730 DBG("Adding metadata stream %d to poll set",
1731 stream->wait_fd);
1732
1733 /* The node should be init at this point */
1734 lttng_ht_add_unique_ulong(metadata_ht,
1735 &stream->waitfd_node);
1736
1737 /* Add metadata stream to the global poll events list */
1738 lttng_poll_add(&events, stream->wait_fd,
1739 LPOLLIN | LPOLLPRI);
1740
1741 consumer_add_metadata_stream(stream);
1742 }
1743
1744 /* Metadata pipe handled. Continue handling the others */
1745 continue;
1746 }
1747
1748 /* From here, the event is a metadata wait fd */
1749
1750 lttng_ht_lookup(metadata_ht, (void *)((unsigned long) pollfd),
1751 &iter);
1752 node = lttng_ht_iter_get_node_ulong(&iter);
1753 if (node == NULL) {
1754 /* FD not found, continue loop */
1755 continue;
1756 }
1757
1758 stream = caa_container_of(node, struct lttng_consumer_stream,
1759 waitfd_node);
1760
1761 /* Get the data out of the metadata file descriptor */
1762 if (revents & (LPOLLIN | LPOLLPRI)) {
1763 DBG("Metadata available on fd %d", pollfd);
1764 assert(stream->wait_fd == pollfd);
1765
1766 len = ctx->on_buffer_ready(stream, ctx);
1767 /* It's ok to have an unavailable sub-buffer */
1768 if (len < 0 && len != -EAGAIN) {
1769 goto end;
1770 } else if (len > 0) {
1771 stream->data_read = 1;
1772 }
1773 }
1774
1775 /*
1776 * Remove the stream from the hash table since there is no data
1777 * left on the fd because we previously did a read on the buffer.
1778 */
4adabd61 1779 if (revents & (LPOLLERR | LPOLLHUP)) {
fb3a43a9
DG
1780 DBG("Metadata fd %d is hup|err|nval.", pollfd);
1781 if (!stream->hangup_flush_done
1782 && (consumer_data.type == LTTNG_CONSUMER32_UST
1783 || consumer_data.type == LTTNG_CONSUMER64_UST)) {
1784 DBG("Attempting to flush and consume the UST buffers");
1785 lttng_ustconsumer_on_stream_hangup(stream);
1786
1787 /* We just flushed the stream now read it. */
1788 len = ctx->on_buffer_ready(stream, ctx);
1789 /* It's ok to have an unavailable sub-buffer */
1790 if (len < 0 && len != -EAGAIN) {
1791 goto end;
1792 }
1793 }
1794
1795 /* Removing it from hash table, poll set and free memory */
1796 lttng_ht_del(metadata_ht, &iter);
1797 lttng_poll_del(&events, stream->wait_fd);
1798 consumer_del_metadata_stream(stream);
1799 }
1800 }
1801 }
1802
1803error:
1804end:
1805 DBG("Metadata poll thread exiting");
1806 lttng_poll_clean(&events);
1807
1808 if (metadata_ht) {
1809 destroy_stream_ht(metadata_ht);
1810 }
1811
1812 rcu_unregister_thread();
1813 return NULL;
1814}
1815
3bd1e081 1816/*
e4421fec 1817 * This thread polls the fds in the set to consume the data and write
3bd1e081
MD
1818 * it to tracefile if necessary.
1819 */
1820void *lttng_consumer_thread_poll_fds(void *data)
1821{
1822 int num_rdy, num_hup, high_prio, ret, i;
1823 struct pollfd *pollfd = NULL;
1824 /* local view of the streams */
1825 struct lttng_consumer_stream **local_stream = NULL;
1826 /* local view of consumer_data.fds_count */
1827 int nb_fd = 0;
3bd1e081 1828 struct lttng_consumer_local_data *ctx = data;
00e2e675 1829 ssize_t len;
fb3a43a9
DG
1830 pthread_t metadata_thread;
1831 void *status;
3bd1e081 1832
e7b994a3
DG
1833 rcu_register_thread();
1834
fb3a43a9
DG
1835 /* Start metadata polling thread */
1836 ret = pthread_create(&metadata_thread, NULL,
1837 lttng_consumer_thread_poll_metadata, (void *) ctx);
1838 if (ret < 0) {
1839 PERROR("pthread_create metadata thread");
1840 goto end;
1841 }
1842
effcf122 1843 local_stream = zmalloc(sizeof(struct lttng_consumer_stream));
3bd1e081
MD
1844
1845 while (1) {
1846 high_prio = 0;
1847 num_hup = 0;
1848
1849 /*
e4421fec 1850 * the fds set has been updated, we need to update our
3bd1e081
MD
1851 * local array as well
1852 */
1853 pthread_mutex_lock(&consumer_data.lock);
1854 if (consumer_data.need_update) {
1855 if (pollfd != NULL) {
1856 free(pollfd);
1857 pollfd = NULL;
1858 }
1859 if (local_stream != NULL) {
1860 free(local_stream);
1861 local_stream = NULL;
1862 }
1863
1864 /* allocate for all fds + 1 for the consumer_poll_pipe */
effcf122 1865 pollfd = zmalloc((consumer_data.stream_count + 1) * sizeof(struct pollfd));
3bd1e081
MD
1866 if (pollfd == NULL) {
1867 perror("pollfd malloc");
1868 pthread_mutex_unlock(&consumer_data.lock);
1869 goto end;
1870 }
1871
1872 /* allocate for all fds + 1 for the consumer_poll_pipe */
effcf122 1873 local_stream = zmalloc((consumer_data.stream_count + 1) *
3bd1e081
MD
1874 sizeof(struct lttng_consumer_stream));
1875 if (local_stream == NULL) {
1876 perror("local_stream malloc");
1877 pthread_mutex_unlock(&consumer_data.lock);
1878 goto end;
1879 }
fb3a43a9 1880 ret = consumer_update_poll_array(ctx, &pollfd, local_stream);
3bd1e081
MD
1881 if (ret < 0) {
1882 ERR("Error in allocating pollfd or local_outfds");
f73fabfd 1883 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_POLL_ERROR);
3bd1e081
MD
1884 pthread_mutex_unlock(&consumer_data.lock);
1885 goto end;
1886 }
1887 nb_fd = ret;
1888 consumer_data.need_update = 0;
1889 }
1890 pthread_mutex_unlock(&consumer_data.lock);
1891
4078b776
MD
1892 /* No FDs and consumer_quit, consumer_cleanup the thread */
1893 if (nb_fd == 0 && consumer_quit == 1) {
1894 goto end;
1895 }
3bd1e081 1896 /* poll on the array of fds */
88f2b785 1897 restart:
3bd1e081
MD
1898 DBG("polling on %d fd", nb_fd + 1);
1899 num_rdy = poll(pollfd, nb_fd + 1, consumer_poll_timeout);
1900 DBG("poll num_rdy : %d", num_rdy);
1901 if (num_rdy == -1) {
88f2b785
MD
1902 /*
1903 * Restart interrupted system call.
1904 */
1905 if (errno == EINTR) {
1906 goto restart;
1907 }
3bd1e081 1908 perror("Poll error");
f73fabfd 1909 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_POLL_ERROR);
3bd1e081
MD
1910 goto end;
1911 } else if (num_rdy == 0) {
1912 DBG("Polling thread timed out");
1913 goto end;
1914 }
1915
3bd1e081 1916 /*
00e2e675
DG
1917 * If the consumer_poll_pipe triggered poll go directly to the
1918 * beginning of the loop to update the array. We want to prioritize
1919 * array update over low-priority reads.
3bd1e081 1920 */
509bb1cf 1921 if (pollfd[nb_fd].revents & (POLLIN | POLLPRI)) {
04fdd819
MD
1922 size_t pipe_readlen;
1923 char tmp;
1924
3bd1e081 1925 DBG("consumer_poll_pipe wake up");
04fdd819
MD
1926 /* Consume 1 byte of pipe data */
1927 do {
1928 pipe_readlen = read(ctx->consumer_poll_pipe[0], &tmp, 1);
1929 } while (pipe_readlen == -1 && errno == EINTR);
3bd1e081
MD
1930 continue;
1931 }
1932
1933 /* Take care of high priority channels first. */
1934 for (i = 0; i < nb_fd; i++) {
fb3a43a9 1935 if (pollfd[i].revents & POLLPRI) {
d41f73b7
MD
1936 DBG("Urgent read on fd %d", pollfd[i].fd);
1937 high_prio = 1;
4078b776 1938 len = ctx->on_buffer_ready(local_stream[i], ctx);
d41f73b7 1939 /* it's ok to have an unavailable sub-buffer */
4078b776
MD
1940 if (len < 0 && len != -EAGAIN) {
1941 goto end;
1942 } else if (len > 0) {
1943 local_stream[i]->data_read = 1;
d41f73b7 1944 }
3bd1e081
MD
1945 }
1946 }
1947
4078b776
MD
1948 /*
1949 * If we read high prio channel in this loop, try again
1950 * for more high prio data.
1951 */
1952 if (high_prio) {
3bd1e081
MD
1953 continue;
1954 }
1955
1956 /* Take care of low priority channels. */
4078b776
MD
1957 for (i = 0; i < nb_fd; i++) {
1958 if ((pollfd[i].revents & POLLIN) ||
1959 local_stream[i]->hangup_flush_done) {
4078b776
MD
1960 DBG("Normal read on fd %d", pollfd[i].fd);
1961 len = ctx->on_buffer_ready(local_stream[i], ctx);
1962 /* it's ok to have an unavailable sub-buffer */
1963 if (len < 0 && len != -EAGAIN) {
1964 goto end;
1965 } else if (len > 0) {
1966 local_stream[i]->data_read = 1;
1967 }
1968 }
1969 }
1970
1971 /* Handle hangup and errors */
1972 for (i = 0; i < nb_fd; i++) {
1973 if (!local_stream[i]->hangup_flush_done
1974 && (pollfd[i].revents & (POLLHUP | POLLERR | POLLNVAL))
1975 && (consumer_data.type == LTTNG_CONSUMER32_UST
1976 || consumer_data.type == LTTNG_CONSUMER64_UST)) {
1977 DBG("fd %d is hup|err|nval. Attempting flush and read.",
1978 pollfd[i].fd);
1979 lttng_ustconsumer_on_stream_hangup(local_stream[i]);
1980 /* Attempt read again, for the data we just flushed. */
1981 local_stream[i]->data_read = 1;
1982 }
1983 /*
1984 * If the poll flag is HUP/ERR/NVAL and we have
1985 * read no data in this pass, we can remove the
1986 * stream from its hash table.
1987 */
1988 if ((pollfd[i].revents & POLLHUP)) {
1989 DBG("Polling fd %d tells it has hung up.", pollfd[i].fd);
1990 if (!local_stream[i]->data_read) {
702b1ea4 1991 consumer_del_stream(local_stream[i]);
4078b776
MD
1992 num_hup++;
1993 }
1994 } else if (pollfd[i].revents & POLLERR) {
1995 ERR("Error returned in polling fd %d.", pollfd[i].fd);
1996 if (!local_stream[i]->data_read) {
702b1ea4 1997 consumer_del_stream(local_stream[i]);
4078b776
MD
1998 num_hup++;
1999 }
2000 } else if (pollfd[i].revents & POLLNVAL) {
2001 ERR("Polling fd %d tells fd is not open.", pollfd[i].fd);
2002 if (!local_stream[i]->data_read) {
702b1ea4 2003 consumer_del_stream(local_stream[i]);
4078b776 2004 num_hup++;
3bd1e081
MD
2005 }
2006 }
4078b776 2007 local_stream[i]->data_read = 0;
3bd1e081
MD
2008 }
2009 }
2010end:
2011 DBG("polling thread exiting");
2012 if (pollfd != NULL) {
2013 free(pollfd);
2014 pollfd = NULL;
2015 }
2016 if (local_stream != NULL) {
2017 free(local_stream);
2018 local_stream = NULL;
2019 }
fb3a43a9
DG
2020
2021 /*
2022 * Close the write side of the pipe so epoll_wait() in
2023 * lttng_consumer_thread_poll_metadata can catch it. The thread is
2024 * monitoring the read side of the pipe. If we close them both, epoll_wait
2025 * strangely does not return and could create a endless wait period if the
2026 * pipe is the only tracked fd in the poll set. The thread will take care
2027 * of closing the read side.
2028 */
2029 close(ctx->consumer_metadata_pipe[1]);
2030 if (ret) {
2031 ret = pthread_join(metadata_thread, &status);
2032 if (ret < 0) {
2033 PERROR("pthread_join metadata thread");
2034 }
2035 }
2036
e7b994a3 2037 rcu_unregister_thread();
3bd1e081
MD
2038 return NULL;
2039}
2040
2041/*
2042 * This thread listens on the consumerd socket and receives the file
2043 * descriptors from the session daemon.
2044 */
2045void *lttng_consumer_thread_receive_fds(void *data)
2046{
2047 int sock, client_socket, ret;
2048 /*
2049 * structure to poll for incoming data on communication socket avoids
2050 * making blocking sockets.
2051 */
2052 struct pollfd consumer_sockpoll[2];
2053 struct lttng_consumer_local_data *ctx = data;
2054
e7b994a3
DG
2055 rcu_register_thread();
2056
3bd1e081
MD
2057 DBG("Creating command socket %s", ctx->consumer_command_sock_path);
2058 unlink(ctx->consumer_command_sock_path);
2059 client_socket = lttcomm_create_unix_sock(ctx->consumer_command_sock_path);
2060 if (client_socket < 0) {
2061 ERR("Cannot create command socket");
2062 goto end;
2063 }
2064
2065 ret = lttcomm_listen_unix_sock(client_socket);
2066 if (ret < 0) {
2067 goto end;
2068 }
2069
32258573 2070 DBG("Sending ready command to lttng-sessiond");
f73fabfd 2071 ret = lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY);
3bd1e081
MD
2072 /* return < 0 on error, but == 0 is not fatal */
2073 if (ret < 0) {
32258573 2074 ERR("Error sending ready command to lttng-sessiond");
3bd1e081
MD
2075 goto end;
2076 }
2077
2078 ret = fcntl(client_socket, F_SETFL, O_NONBLOCK);
2079 if (ret < 0) {
2080 perror("fcntl O_NONBLOCK");
2081 goto end;
2082 }
2083
2084 /* prepare the FDs to poll : to client socket and the should_quit pipe */
2085 consumer_sockpoll[0].fd = ctx->consumer_should_quit[0];
2086 consumer_sockpoll[0].events = POLLIN | POLLPRI;
2087 consumer_sockpoll[1].fd = client_socket;
2088 consumer_sockpoll[1].events = POLLIN | POLLPRI;
2089
2090 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
2091 goto end;
2092 }
2093 DBG("Connection on client_socket");
2094
2095 /* Blocking call, waiting for transmission */
2096 sock = lttcomm_accept_unix_sock(client_socket);
2097 if (sock <= 0) {
2098 WARN("On accept");
2099 goto end;
2100 }
2101 ret = fcntl(sock, F_SETFL, O_NONBLOCK);
2102 if (ret < 0) {
2103 perror("fcntl O_NONBLOCK");
2104 goto end;
2105 }
2106
2107 /* update the polling structure to poll on the established socket */
2108 consumer_sockpoll[1].fd = sock;
2109 consumer_sockpoll[1].events = POLLIN | POLLPRI;
2110
2111 while (1) {
2112 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
2113 goto end;
2114 }
2115 DBG("Incoming command on sock");
2116 ret = lttng_consumer_recv_cmd(ctx, sock, consumer_sockpoll);
2117 if (ret == -ENOENT) {
2118 DBG("Received STOP command");
2119 goto end;
2120 }
4cbc1a04
DG
2121 if (ret <= 0) {
2122 /*
2123 * This could simply be a session daemon quitting. Don't output
2124 * ERR() here.
2125 */
2126 DBG("Communication interrupted on command socket");
3bd1e081
MD
2127 goto end;
2128 }
2129 if (consumer_quit) {
2130 DBG("consumer_thread_receive_fds received quit from signal");
2131 goto end;
2132 }
2133 DBG("received fds on sock");
2134 }
2135end:
2136 DBG("consumer_thread_receive_fds exiting");
2137
2138 /*
2139 * when all fds have hung up, the polling thread
2140 * can exit cleanly
2141 */
2142 consumer_quit = 1;
2143
2144 /*
2145 * 2s of grace period, if no polling events occur during
2146 * this period, the polling thread will exit even if there
2147 * are still open FDs (should not happen, but safety mechanism).
2148 */
2149 consumer_poll_timeout = LTTNG_CONSUMER_POLL_TIMEOUT;
2150
04fdd819
MD
2151 /*
2152 * Wake-up the other end by writing a null byte in the pipe
2153 * (non-blocking). Important note: Because writing into the
2154 * pipe is non-blocking (and therefore we allow dropping wakeup
2155 * data, as long as there is wakeup data present in the pipe
2156 * buffer to wake up the other end), the other end should
2157 * perform the following sequence for waiting:
2158 * 1) empty the pipe (reads).
2159 * 2) perform update operation.
2160 * 3) wait on the pipe (poll).
2161 */
2162 do {
2163 ret = write(ctx->consumer_poll_pipe[1], "", 1);
6f94560a 2164 } while (ret < 0 && errno == EINTR);
e7b994a3 2165 rcu_unregister_thread();
3bd1e081
MD
2166 return NULL;
2167}
d41f73b7 2168
4078b776 2169ssize_t lttng_consumer_read_subbuffer(struct lttng_consumer_stream *stream,
d41f73b7
MD
2170 struct lttng_consumer_local_data *ctx)
2171{
2172 switch (consumer_data.type) {
2173 case LTTNG_CONSUMER_KERNEL:
2174 return lttng_kconsumer_read_subbuffer(stream, ctx);
7753dea8
MD
2175 case LTTNG_CONSUMER32_UST:
2176 case LTTNG_CONSUMER64_UST:
d41f73b7
MD
2177 return lttng_ustconsumer_read_subbuffer(stream, ctx);
2178 default:
2179 ERR("Unknown consumer_data type");
2180 assert(0);
2181 return -ENOSYS;
2182 }
2183}
2184
2185int lttng_consumer_on_recv_stream(struct lttng_consumer_stream *stream)
2186{
2187 switch (consumer_data.type) {
2188 case LTTNG_CONSUMER_KERNEL:
2189 return lttng_kconsumer_on_recv_stream(stream);
7753dea8
MD
2190 case LTTNG_CONSUMER32_UST:
2191 case LTTNG_CONSUMER64_UST:
d41f73b7
MD
2192 return lttng_ustconsumer_on_recv_stream(stream);
2193 default:
2194 ERR("Unknown consumer_data type");
2195 assert(0);
2196 return -ENOSYS;
2197 }
2198}
e4421fec
DG
2199
2200/*
2201 * Allocate and set consumer data hash tables.
2202 */
2203void lttng_consumer_init(void)
2204{
2205 consumer_data.stream_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
2206 consumer_data.channel_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
00e2e675 2207 consumer_data.relayd_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
e4421fec 2208}
7735ef9e
DG
2209
2210/*
2211 * Process the ADD_RELAYD command receive by a consumer.
2212 *
2213 * This will create a relayd socket pair and add it to the relayd hash table.
2214 * The caller MUST acquire a RCU read side lock before calling it.
2215 */
2216int consumer_add_relayd_socket(int net_seq_idx, int sock_type,
2217 struct lttng_consumer_local_data *ctx, int sock,
2218 struct pollfd *consumer_sockpoll, struct lttcomm_sock *relayd_sock)
2219{
2220 int fd, ret = -1;
2221 struct consumer_relayd_sock_pair *relayd;
2222
2223 DBG("Consumer adding relayd socket (idx: %d)", net_seq_idx);
2224
2225 /* Get relayd reference if exists. */
2226 relayd = consumer_find_relayd(net_seq_idx);
2227 if (relayd == NULL) {
2228 /* Not found. Allocate one. */
2229 relayd = consumer_allocate_relayd_sock_pair(net_seq_idx);
2230 if (relayd == NULL) {
f73fabfd 2231 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
7735ef9e
DG
2232 goto error;
2233 }
2234 }
2235
2236 /* Poll on consumer socket. */
2237 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
2238 ret = -EINTR;
2239 goto error;
2240 }
2241
2242 /* Get relayd socket from session daemon */
2243 ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1);
2244 if (ret != sizeof(fd)) {
f73fabfd 2245 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD);
7735ef9e
DG
2246 ret = -1;
2247 goto error;
2248 }
2249
2250 /* Copy socket information and received FD */
2251 switch (sock_type) {
2252 case LTTNG_STREAM_CONTROL:
2253 /* Copy received lttcomm socket */
2254 lttcomm_copy_sock(&relayd->control_sock, relayd_sock);
2255 ret = lttcomm_create_sock(&relayd->control_sock);
2256 if (ret < 0) {
2257 goto error;
2258 }
2259
2260 /* Close the created socket fd which is useless */
2261 close(relayd->control_sock.fd);
2262
2263 /* Assign new file descriptor */
2264 relayd->control_sock.fd = fd;
2265 break;
2266 case LTTNG_STREAM_DATA:
2267 /* Copy received lttcomm socket */
2268 lttcomm_copy_sock(&relayd->data_sock, relayd_sock);
2269 ret = lttcomm_create_sock(&relayd->data_sock);
2270 if (ret < 0) {
2271 goto error;
2272 }
2273
2274 /* Close the created socket fd which is useless */
2275 close(relayd->data_sock.fd);
2276
2277 /* Assign new file descriptor */
2278 relayd->data_sock.fd = fd;
2279 break;
2280 default:
2281 ERR("Unknown relayd socket type (%d)", sock_type);
2282 goto error;
2283 }
2284
2285 DBG("Consumer %s socket created successfully with net idx %d (fd: %d)",
2286 sock_type == LTTNG_STREAM_CONTROL ? "control" : "data",
2287 relayd->net_seq_idx, fd);
2288
2289 /*
2290 * Add relayd socket pair to consumer data hashtable. If object already
2291 * exists or on error, the function gracefully returns.
2292 */
2293 consumer_add_relayd(relayd);
2294
2295 /* All good! */
2296 ret = 0;
2297
2298error:
2299 return ret;
2300}
This page took 0.225584 seconds and 4 git commands to generate.