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