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