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