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