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