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