63d0d65ee3157b05ef1113336185535ea23a9dbe
[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
31 #include <common/common.h>
32 #include <common/kernel-ctl/kernel-ctl.h>
33 #include <common/sessiond-comm/relayd.h>
34 #include <common/sessiond-comm/sessiond-comm.h>
35 #include <common/kernel-consumer/kernel-consumer.h>
36 #include <common/relayd/relayd.h>
37 #include <common/ust-consumer/ust-consumer.h>
38
39 #include "consumer.h"
40
41 struct lttng_consumer_global_data consumer_data = {
42 .stream_count = 0,
43 .need_update = 1,
44 .type = LTTNG_CONSUMER_UNKNOWN,
45 };
46
47 /* timeout parameter, to control the polling thread grace period. */
48 int consumer_poll_timeout = -1;
49
50 /*
51 * Flag to inform the polling thread to quit when all fd hung up. Updated by
52 * the consumer_thread_receive_fds when it notices that all fds has hung up.
53 * Also updated by the signal handler (consumer_should_exit()). Read by the
54 * polling threads.
55 */
56 volatile int consumer_quit = 0;
57
58 /*
59 * Find a stream. The consumer_data.lock must be locked during this
60 * call.
61 */
62 static struct lttng_consumer_stream *consumer_find_stream(int key)
63 {
64 struct lttng_ht_iter iter;
65 struct lttng_ht_node_ulong *node;
66 struct lttng_consumer_stream *stream = NULL;
67
68 /* Negative keys are lookup failures */
69 if (key < 0)
70 return NULL;
71
72 rcu_read_lock();
73
74 lttng_ht_lookup(consumer_data.stream_ht, (void *)((unsigned long) key),
75 &iter);
76 node = lttng_ht_iter_get_node_ulong(&iter);
77 if (node != NULL) {
78 stream = caa_container_of(node, struct lttng_consumer_stream, node);
79 }
80
81 rcu_read_unlock();
82
83 return stream;
84 }
85
86 static void consumer_steal_stream_key(int key)
87 {
88 struct lttng_consumer_stream *stream;
89
90 rcu_read_lock();
91 stream = consumer_find_stream(key);
92 if (stream) {
93 stream->key = -1;
94 /*
95 * We don't want the lookup to match, but we still need
96 * to iterate on this stream when iterating over the hash table. Just
97 * change the node key.
98 */
99 stream->node.key = -1;
100 }
101 rcu_read_unlock();
102 }
103
104 static struct lttng_consumer_channel *consumer_find_channel(int key)
105 {
106 struct lttng_ht_iter iter;
107 struct lttng_ht_node_ulong *node;
108 struct lttng_consumer_channel *channel = NULL;
109
110 /* Negative keys are lookup failures */
111 if (key < 0)
112 return NULL;
113
114 rcu_read_lock();
115
116 lttng_ht_lookup(consumer_data.channel_ht, (void *)((unsigned long) key),
117 &iter);
118 node = lttng_ht_iter_get_node_ulong(&iter);
119 if (node != NULL) {
120 channel = caa_container_of(node, struct lttng_consumer_channel, node);
121 }
122
123 rcu_read_unlock();
124
125 return channel;
126 }
127
128 static void consumer_steal_channel_key(int key)
129 {
130 struct lttng_consumer_channel *channel;
131
132 rcu_read_lock();
133 channel = consumer_find_channel(key);
134 if (channel) {
135 channel->key = -1;
136 /*
137 * We don't want the lookup to match, but we still need
138 * to iterate on this channel when iterating over the hash table. Just
139 * change the node key.
140 */
141 channel->node.key = -1;
142 }
143 rcu_read_unlock();
144 }
145
146 static
147 void consumer_free_stream(struct rcu_head *head)
148 {
149 struct lttng_ht_node_ulong *node =
150 caa_container_of(head, struct lttng_ht_node_ulong, head);
151 struct lttng_consumer_stream *stream =
152 caa_container_of(node, struct lttng_consumer_stream, node);
153
154 free(stream);
155 }
156
157 /*
158 * RCU protected relayd socket pair free.
159 */
160 static void consumer_rcu_free_relayd(struct rcu_head *head)
161 {
162 struct lttng_ht_node_ulong *node =
163 caa_container_of(head, struct lttng_ht_node_ulong, head);
164 struct consumer_relayd_sock_pair *relayd =
165 caa_container_of(node, struct consumer_relayd_sock_pair, node);
166
167 free(relayd);
168 }
169
170 /*
171 * Destroy and free relayd socket pair object.
172 *
173 * This function MUST be called with the consumer_data lock acquired.
174 */
175 void consumer_destroy_relayd(struct consumer_relayd_sock_pair *relayd)
176 {
177 int ret;
178 struct lttng_ht_iter iter;
179
180 if (relayd == NULL) {
181 return;
182 }
183
184 DBG("Consumer destroy and close relayd socket pair");
185
186 iter.iter.node = &relayd->node.node;
187 ret = lttng_ht_del(consumer_data.relayd_ht, &iter);
188 if (ret != 0) {
189 /* We assume the relayd was already destroyed */
190 return;
191 }
192
193 /* Close all sockets */
194 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
195 (void) relayd_close(&relayd->control_sock);
196 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
197 (void) relayd_close(&relayd->data_sock);
198
199 /* RCU free() call */
200 call_rcu(&relayd->node.head, consumer_rcu_free_relayd);
201 }
202
203 /*
204 * Remove a stream from the global list protected by a mutex. This
205 * function is also responsible for freeing its data structures.
206 */
207 void consumer_del_stream(struct lttng_consumer_stream *stream)
208 {
209 int ret;
210 struct lttng_ht_iter iter;
211 struct lttng_consumer_channel *free_chan = NULL;
212 struct consumer_relayd_sock_pair *relayd;
213
214 assert(stream);
215
216 pthread_mutex_lock(&consumer_data.lock);
217
218 switch (consumer_data.type) {
219 case LTTNG_CONSUMER_KERNEL:
220 if (stream->mmap_base != NULL) {
221 ret = munmap(stream->mmap_base, stream->mmap_len);
222 if (ret != 0) {
223 perror("munmap");
224 }
225 }
226 break;
227 case LTTNG_CONSUMER32_UST:
228 case LTTNG_CONSUMER64_UST:
229 lttng_ustconsumer_del_stream(stream);
230 break;
231 default:
232 ERR("Unknown consumer_data type");
233 assert(0);
234 goto end;
235 }
236
237 rcu_read_lock();
238 iter.iter.node = &stream->node.node;
239 ret = lttng_ht_del(consumer_data.stream_ht, &iter);
240 assert(!ret);
241
242 rcu_read_unlock();
243
244 if (consumer_data.stream_count <= 0) {
245 goto end;
246 }
247 consumer_data.stream_count--;
248 if (!stream) {
249 goto end;
250 }
251 if (stream->out_fd >= 0) {
252 ret = close(stream->out_fd);
253 if (ret) {
254 PERROR("close");
255 }
256 }
257 if (stream->wait_fd >= 0 && !stream->wait_fd_is_copy) {
258 ret = close(stream->wait_fd);
259 if (ret) {
260 PERROR("close");
261 }
262 }
263 if (stream->shm_fd >= 0 && stream->wait_fd != stream->shm_fd) {
264 ret = close(stream->shm_fd);
265 if (ret) {
266 PERROR("close");
267 }
268 }
269
270 /* Check and cleanup relayd */
271 rcu_read_lock();
272 relayd = consumer_find_relayd(stream->net_seq_idx);
273 if (relayd != NULL) {
274 uatomic_dec(&relayd->refcount);
275 assert(uatomic_read(&relayd->refcount) >= 0);
276
277 ret = relayd_send_close_stream(&relayd->control_sock,
278 stream->relayd_stream_id,
279 stream->next_net_seq_num - 1);
280 if (ret < 0) {
281 ERR("Unable to close stream on the relayd. Continuing");
282 /* Continue here. There is nothing we can do for the relayd.*/
283 }
284
285 /* Both conditions are met, we destroy the relayd. */
286 if (uatomic_read(&relayd->refcount) == 0 &&
287 uatomic_read(&relayd->destroy_flag)) {
288 consumer_destroy_relayd(relayd);
289 }
290 }
291 rcu_read_unlock();
292
293 if (!--stream->chan->refcount) {
294 free_chan = stream->chan;
295 }
296
297
298 call_rcu(&stream->node.head, consumer_free_stream);
299 end:
300 consumer_data.need_update = 1;
301 pthread_mutex_unlock(&consumer_data.lock);
302
303 if (free_chan)
304 consumer_del_channel(free_chan);
305 }
306
307 struct lttng_consumer_stream *consumer_allocate_stream(
308 int channel_key, int stream_key,
309 int shm_fd, int wait_fd,
310 enum lttng_consumer_stream_state state,
311 uint64_t mmap_len,
312 enum lttng_event_output output,
313 const char *path_name,
314 uid_t uid,
315 gid_t gid,
316 int net_index,
317 int metadata_flag)
318 {
319 struct lttng_consumer_stream *stream;
320 int ret;
321
322 stream = zmalloc(sizeof(*stream));
323 if (stream == NULL) {
324 perror("malloc struct lttng_consumer_stream");
325 goto end;
326 }
327 stream->chan = consumer_find_channel(channel_key);
328 if (!stream->chan) {
329 perror("Unable to find channel key");
330 goto end;
331 }
332 stream->chan->refcount++;
333 stream->key = stream_key;
334 stream->shm_fd = shm_fd;
335 stream->wait_fd = wait_fd;
336 stream->out_fd = -1;
337 stream->out_fd_offset = 0;
338 stream->state = state;
339 stream->mmap_len = mmap_len;
340 stream->mmap_base = NULL;
341 stream->output = output;
342 stream->uid = uid;
343 stream->gid = gid;
344 stream->net_seq_idx = net_index;
345 stream->metadata_flag = metadata_flag;
346 strncpy(stream->path_name, path_name, sizeof(stream->path_name));
347 stream->path_name[sizeof(stream->path_name) - 1] = '\0';
348 lttng_ht_node_init_ulong(&stream->node, stream->key);
349 lttng_ht_node_init_ulong(&stream->waitfd_node, stream->wait_fd);
350
351 switch (consumer_data.type) {
352 case LTTNG_CONSUMER_KERNEL:
353 break;
354 case LTTNG_CONSUMER32_UST:
355 case LTTNG_CONSUMER64_UST:
356 stream->cpu = stream->chan->cpucount++;
357 ret = lttng_ustconsumer_allocate_stream(stream);
358 if (ret) {
359 free(stream);
360 return NULL;
361 }
362 break;
363 default:
364 ERR("Unknown consumer_data type");
365 assert(0);
366 goto end;
367 }
368 DBG("Allocated stream %s (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, out_fd %d, net_seq_idx %d)",
369 stream->path_name, stream->key,
370 stream->shm_fd,
371 stream->wait_fd,
372 (unsigned long long) stream->mmap_len,
373 stream->out_fd,
374 stream->net_seq_idx);
375 end:
376 return stream;
377 }
378
379 /*
380 * Add a stream to the global list protected by a mutex.
381 */
382 int consumer_add_stream(struct lttng_consumer_stream *stream)
383 {
384 int ret = 0;
385 struct lttng_ht_node_ulong *node;
386 struct lttng_ht_iter iter;
387 struct consumer_relayd_sock_pair *relayd;
388
389 pthread_mutex_lock(&consumer_data.lock);
390 /* Steal stream identifier, for UST */
391 consumer_steal_stream_key(stream->key);
392
393 rcu_read_lock();
394 lttng_ht_lookup(consumer_data.stream_ht,
395 (void *)((unsigned long) stream->key), &iter);
396 node = lttng_ht_iter_get_node_ulong(&iter);
397 if (node != NULL) {
398 rcu_read_unlock();
399 /* Stream already exist. Ignore the insertion */
400 goto end;
401 }
402
403 lttng_ht_add_unique_ulong(consumer_data.stream_ht, &stream->node);
404
405 /* Check and cleanup relayd */
406 relayd = consumer_find_relayd(stream->net_seq_idx);
407 if (relayd != NULL) {
408 uatomic_inc(&relayd->refcount);
409 }
410 rcu_read_unlock();
411
412 /* Update consumer data */
413 consumer_data.stream_count++;
414 consumer_data.need_update = 1;
415
416 switch (consumer_data.type) {
417 case LTTNG_CONSUMER_KERNEL:
418 break;
419 case LTTNG_CONSUMER32_UST:
420 case LTTNG_CONSUMER64_UST:
421 /* Streams are in CPU number order (we rely on this) */
422 stream->cpu = stream->chan->nr_streams++;
423 break;
424 default:
425 ERR("Unknown consumer_data type");
426 assert(0);
427 goto end;
428 }
429
430 end:
431 pthread_mutex_unlock(&consumer_data.lock);
432
433 return ret;
434 }
435
436 /*
437 * Add relayd socket to global consumer data hashtable.
438 */
439 int consumer_add_relayd(struct consumer_relayd_sock_pair *relayd)
440 {
441 int ret = 0;
442 struct lttng_ht_node_ulong *node;
443 struct lttng_ht_iter iter;
444
445 if (relayd == NULL) {
446 ret = -1;
447 goto end;
448 }
449
450 rcu_read_lock();
451
452 lttng_ht_lookup(consumer_data.relayd_ht,
453 (void *)((unsigned long) relayd->net_seq_idx), &iter);
454 node = lttng_ht_iter_get_node_ulong(&iter);
455 if (node != NULL) {
456 rcu_read_unlock();
457 /* Relayd already exist. Ignore the insertion */
458 goto end;
459 }
460 lttng_ht_add_unique_ulong(consumer_data.relayd_ht, &relayd->node);
461
462 rcu_read_unlock();
463
464 end:
465 return ret;
466 }
467
468 /*
469 * Allocate and return a consumer relayd socket.
470 */
471 struct consumer_relayd_sock_pair *consumer_allocate_relayd_sock_pair(
472 int net_seq_idx)
473 {
474 struct consumer_relayd_sock_pair *obj = NULL;
475
476 /* Negative net sequence index is a failure */
477 if (net_seq_idx < 0) {
478 goto error;
479 }
480
481 obj = zmalloc(sizeof(struct consumer_relayd_sock_pair));
482 if (obj == NULL) {
483 PERROR("zmalloc relayd sock");
484 goto error;
485 }
486
487 obj->net_seq_idx = net_seq_idx;
488 obj->refcount = 0;
489 obj->destroy_flag = 0;
490 lttng_ht_node_init_ulong(&obj->node, obj->net_seq_idx);
491 pthread_mutex_init(&obj->ctrl_sock_mutex, NULL);
492
493 error:
494 return obj;
495 }
496
497 /*
498 * Find a relayd socket pair in the global consumer data.
499 *
500 * Return the object if found else NULL.
501 * RCU read-side lock must be held across this call and while using the
502 * returned object.
503 */
504 struct consumer_relayd_sock_pair *consumer_find_relayd(int key)
505 {
506 struct lttng_ht_iter iter;
507 struct lttng_ht_node_ulong *node;
508 struct consumer_relayd_sock_pair *relayd = NULL;
509
510 /* Negative keys are lookup failures */
511 if (key < 0) {
512 goto error;
513 }
514
515 lttng_ht_lookup(consumer_data.relayd_ht, (void *)((unsigned long) key),
516 &iter);
517 node = lttng_ht_iter_get_node_ulong(&iter);
518 if (node != NULL) {
519 relayd = caa_container_of(node, struct consumer_relayd_sock_pair, node);
520 }
521
522 error:
523 return relayd;
524 }
525
526 /*
527 * Handle stream for relayd transmission if the stream applies for network
528 * streaming where the net sequence index is set.
529 *
530 * Return destination file descriptor or negative value on error.
531 */
532 int consumer_handle_stream_before_relayd(struct lttng_consumer_stream *stream,
533 size_t data_size)
534 {
535 int outfd = -1, ret;
536 struct consumer_relayd_sock_pair *relayd;
537 struct lttcomm_relayd_data_hdr data_hdr;
538
539 /* Safety net */
540 assert(stream);
541
542 /* Reset data header */
543 memset(&data_hdr, 0, sizeof(data_hdr));
544
545 rcu_read_lock();
546 /* Get relayd reference of the stream. */
547 relayd = consumer_find_relayd(stream->net_seq_idx);
548 if (relayd == NULL) {
549 /* Stream is either local or corrupted */
550 goto error;
551 }
552
553 DBG("Consumer found relayd socks with index %d", stream->net_seq_idx);
554 if (stream->metadata_flag) {
555 /* Caller MUST acquire the relayd control socket lock */
556 ret = relayd_send_metadata(&relayd->control_sock, data_size);
557 if (ret < 0) {
558 goto error;
559 }
560
561 /* Metadata are always sent on the control socket. */
562 outfd = relayd->control_sock.fd;
563 } else {
564 /* Set header with stream information */
565 data_hdr.stream_id = htobe64(stream->relayd_stream_id);
566 data_hdr.data_size = htobe32(data_size);
567 data_hdr.net_seq_num = htobe64(stream->next_net_seq_num++);
568 /* Other fields are zeroed previously */
569
570 ret = relayd_send_data_hdr(&relayd->data_sock, &data_hdr,
571 sizeof(data_hdr));
572 if (ret < 0) {
573 goto error;
574 }
575
576 /* Set to go on data socket */
577 outfd = relayd->data_sock.fd;
578 }
579
580 error:
581 rcu_read_unlock();
582 return outfd;
583 }
584
585 /*
586 * Update a stream according to what we just received.
587 */
588 void consumer_change_stream_state(int stream_key,
589 enum lttng_consumer_stream_state state)
590 {
591 struct lttng_consumer_stream *stream;
592
593 pthread_mutex_lock(&consumer_data.lock);
594 stream = consumer_find_stream(stream_key);
595 if (stream) {
596 stream->state = state;
597 }
598 consumer_data.need_update = 1;
599 pthread_mutex_unlock(&consumer_data.lock);
600 }
601
602 static
603 void consumer_free_channel(struct rcu_head *head)
604 {
605 struct lttng_ht_node_ulong *node =
606 caa_container_of(head, struct lttng_ht_node_ulong, head);
607 struct lttng_consumer_channel *channel =
608 caa_container_of(node, struct lttng_consumer_channel, node);
609
610 free(channel);
611 }
612
613 /*
614 * Remove a channel from the global list protected by a mutex. This
615 * function is also responsible for freeing its data structures.
616 */
617 void consumer_del_channel(struct lttng_consumer_channel *channel)
618 {
619 int ret;
620 struct lttng_ht_iter iter;
621
622 pthread_mutex_lock(&consumer_data.lock);
623
624 switch (consumer_data.type) {
625 case LTTNG_CONSUMER_KERNEL:
626 break;
627 case LTTNG_CONSUMER32_UST:
628 case LTTNG_CONSUMER64_UST:
629 lttng_ustconsumer_del_channel(channel);
630 break;
631 default:
632 ERR("Unknown consumer_data type");
633 assert(0);
634 goto end;
635 }
636
637 rcu_read_lock();
638 iter.iter.node = &channel->node.node;
639 ret = lttng_ht_del(consumer_data.channel_ht, &iter);
640 assert(!ret);
641 rcu_read_unlock();
642
643 if (channel->mmap_base != NULL) {
644 ret = munmap(channel->mmap_base, channel->mmap_len);
645 if (ret != 0) {
646 perror("munmap");
647 }
648 }
649 if (channel->wait_fd >= 0 && !channel->wait_fd_is_copy) {
650 ret = close(channel->wait_fd);
651 if (ret) {
652 PERROR("close");
653 }
654 }
655 if (channel->shm_fd >= 0 && channel->wait_fd != channel->shm_fd) {
656 ret = close(channel->shm_fd);
657 if (ret) {
658 PERROR("close");
659 }
660 }
661
662 call_rcu(&channel->node.head, consumer_free_channel);
663 end:
664 pthread_mutex_unlock(&consumer_data.lock);
665 }
666
667 struct lttng_consumer_channel *consumer_allocate_channel(
668 int channel_key,
669 int shm_fd, int wait_fd,
670 uint64_t mmap_len,
671 uint64_t max_sb_size)
672 {
673 struct lttng_consumer_channel *channel;
674 int ret;
675
676 channel = zmalloc(sizeof(*channel));
677 if (channel == NULL) {
678 perror("malloc struct lttng_consumer_channel");
679 goto end;
680 }
681 channel->key = channel_key;
682 channel->shm_fd = shm_fd;
683 channel->wait_fd = wait_fd;
684 channel->mmap_len = mmap_len;
685 channel->max_sb_size = max_sb_size;
686 channel->refcount = 0;
687 channel->nr_streams = 0;
688 lttng_ht_node_init_ulong(&channel->node, channel->key);
689
690 switch (consumer_data.type) {
691 case LTTNG_CONSUMER_KERNEL:
692 channel->mmap_base = NULL;
693 channel->mmap_len = 0;
694 break;
695 case LTTNG_CONSUMER32_UST:
696 case LTTNG_CONSUMER64_UST:
697 ret = lttng_ustconsumer_allocate_channel(channel);
698 if (ret) {
699 free(channel);
700 return NULL;
701 }
702 break;
703 default:
704 ERR("Unknown consumer_data type");
705 assert(0);
706 goto end;
707 }
708 DBG("Allocated channel (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, max_sb_size %llu)",
709 channel->key, channel->shm_fd, channel->wait_fd,
710 (unsigned long long) channel->mmap_len,
711 (unsigned long long) channel->max_sb_size);
712 end:
713 return channel;
714 }
715
716 /*
717 * Add a channel to the global list protected by a mutex.
718 */
719 int consumer_add_channel(struct lttng_consumer_channel *channel)
720 {
721 struct lttng_ht_node_ulong *node;
722 struct lttng_ht_iter iter;
723
724 pthread_mutex_lock(&consumer_data.lock);
725 /* Steal channel identifier, for UST */
726 consumer_steal_channel_key(channel->key);
727 rcu_read_lock();
728
729 lttng_ht_lookup(consumer_data.channel_ht,
730 (void *)((unsigned long) channel->key), &iter);
731 node = lttng_ht_iter_get_node_ulong(&iter);
732 if (node != NULL) {
733 /* Channel already exist. Ignore the insertion */
734 goto end;
735 }
736
737 lttng_ht_add_unique_ulong(consumer_data.channel_ht, &channel->node);
738
739 end:
740 rcu_read_unlock();
741 pthread_mutex_unlock(&consumer_data.lock);
742
743 return 0;
744 }
745
746 /*
747 * Allocate the pollfd structure and the local view of the out fds to avoid
748 * doing a lookup in the linked list and concurrency issues when writing is
749 * needed. Called with consumer_data.lock held.
750 *
751 * Returns the number of fds in the structures.
752 */
753 int consumer_update_poll_array(
754 struct lttng_consumer_local_data *ctx, struct pollfd **pollfd,
755 struct lttng_consumer_stream **local_stream,
756 struct lttng_ht *metadata_ht)
757 {
758 int i = 0;
759 struct lttng_ht_iter iter;
760 struct lttng_consumer_stream *stream;
761
762 DBG("Updating poll fd array");
763 rcu_read_lock();
764 cds_lfht_for_each_entry(consumer_data.stream_ht->ht, &iter.iter, stream,
765 node.node) {
766 if (stream->state != LTTNG_CONSUMER_ACTIVE_STREAM) {
767 continue;
768 }
769 DBG("Active FD %d", stream->wait_fd);
770 (*pollfd)[i].fd = stream->wait_fd;
771 (*pollfd)[i].events = POLLIN | POLLPRI;
772 if (stream->metadata_flag && metadata_ht) {
773 lttng_ht_add_unique_ulong(metadata_ht, &stream->waitfd_node);
774 DBG("Active FD added to metadata hash table");
775 }
776 local_stream[i] = stream;
777 i++;
778 }
779 rcu_read_unlock();
780
781 /*
782 * Insert the consumer_poll_pipe at the end of the array and don't
783 * increment i so nb_fd is the number of real FD.
784 */
785 (*pollfd)[i].fd = ctx->consumer_poll_pipe[0];
786 (*pollfd)[i].events = POLLIN | POLLPRI;
787 return i;
788 }
789
790 /*
791 * Poll on the should_quit pipe and the command socket return -1 on error and
792 * should exit, 0 if data is available on the command socket
793 */
794 int lttng_consumer_poll_socket(struct pollfd *consumer_sockpoll)
795 {
796 int num_rdy;
797
798 restart:
799 num_rdy = poll(consumer_sockpoll, 2, -1);
800 if (num_rdy == -1) {
801 /*
802 * Restart interrupted system call.
803 */
804 if (errno == EINTR) {
805 goto restart;
806 }
807 perror("Poll error");
808 goto exit;
809 }
810 if (consumer_sockpoll[0].revents & (POLLIN | POLLPRI)) {
811 DBG("consumer_should_quit wake up");
812 goto exit;
813 }
814 return 0;
815
816 exit:
817 return -1;
818 }
819
820 /*
821 * Set the error socket.
822 */
823 void lttng_consumer_set_error_sock(
824 struct lttng_consumer_local_data *ctx, int sock)
825 {
826 ctx->consumer_error_socket = sock;
827 }
828
829 /*
830 * Set the command socket path.
831 */
832 void lttng_consumer_set_command_sock_path(
833 struct lttng_consumer_local_data *ctx, char *sock)
834 {
835 ctx->consumer_command_sock_path = sock;
836 }
837
838 /*
839 * Send return code to the session daemon.
840 * If the socket is not defined, we return 0, it is not a fatal error
841 */
842 int lttng_consumer_send_error(
843 struct lttng_consumer_local_data *ctx, int cmd)
844 {
845 if (ctx->consumer_error_socket > 0) {
846 return lttcomm_send_unix_sock(ctx->consumer_error_socket, &cmd,
847 sizeof(enum lttcomm_sessiond_command));
848 }
849
850 return 0;
851 }
852
853 /*
854 * Close all the tracefiles and stream fds, should be called when all instances
855 * are destroyed.
856 */
857 void lttng_consumer_cleanup(void)
858 {
859 struct lttng_ht_iter iter;
860 struct lttng_ht_node_ulong *node;
861
862 rcu_read_lock();
863
864 /*
865 * close all outfd. Called when there are no more threads running (after
866 * joining on the threads), no need to protect list iteration with mutex.
867 */
868 cds_lfht_for_each_entry(consumer_data.stream_ht->ht, &iter.iter, node,
869 node) {
870 struct lttng_consumer_stream *stream =
871 caa_container_of(node, struct lttng_consumer_stream, node);
872 consumer_del_stream(stream);
873 }
874
875 cds_lfht_for_each_entry(consumer_data.channel_ht->ht, &iter.iter, node,
876 node) {
877 struct lttng_consumer_channel *channel =
878 caa_container_of(node, struct lttng_consumer_channel, node);
879 consumer_del_channel(channel);
880 }
881
882 rcu_read_unlock();
883
884 lttng_ht_destroy(consumer_data.stream_ht);
885 lttng_ht_destroy(consumer_data.channel_ht);
886 }
887
888 /*
889 * Called from signal handler.
890 */
891 void lttng_consumer_should_exit(struct lttng_consumer_local_data *ctx)
892 {
893 int ret;
894 consumer_quit = 1;
895 do {
896 ret = write(ctx->consumer_should_quit[1], "4", 1);
897 } while (ret < 0 && errno == EINTR);
898 if (ret < 0) {
899 perror("write consumer quit");
900 }
901 }
902
903 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream *stream,
904 off_t orig_offset)
905 {
906 int outfd = stream->out_fd;
907
908 /*
909 * This does a blocking write-and-wait on any page that belongs to the
910 * subbuffer prior to the one we just wrote.
911 * Don't care about error values, as these are just hints and ways to
912 * limit the amount of page cache used.
913 */
914 if (orig_offset < stream->chan->max_sb_size) {
915 return;
916 }
917 lttng_sync_file_range(outfd, orig_offset - stream->chan->max_sb_size,
918 stream->chan->max_sb_size,
919 SYNC_FILE_RANGE_WAIT_BEFORE
920 | SYNC_FILE_RANGE_WRITE
921 | SYNC_FILE_RANGE_WAIT_AFTER);
922 /*
923 * Give hints to the kernel about how we access the file:
924 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
925 * we write it.
926 *
927 * We need to call fadvise again after the file grows because the
928 * kernel does not seem to apply fadvise to non-existing parts of the
929 * file.
930 *
931 * Call fadvise _after_ having waited for the page writeback to
932 * complete because the dirty page writeback semantic is not well
933 * defined. So it can be expected to lead to lower throughput in
934 * streaming.
935 */
936 posix_fadvise(outfd, orig_offset - stream->chan->max_sb_size,
937 stream->chan->max_sb_size, POSIX_FADV_DONTNEED);
938 }
939
940 /*
941 * Initialise the necessary environnement :
942 * - create a new context
943 * - create the poll_pipe
944 * - create the should_quit pipe (for signal handler)
945 * - create the thread pipe (for splice)
946 *
947 * Takes a function pointer as argument, this function is called when data is
948 * available on a buffer. This function is responsible to do the
949 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
950 * buffer configuration and then kernctl_put_next_subbuf at the end.
951 *
952 * Returns a pointer to the new context or NULL on error.
953 */
954 struct lttng_consumer_local_data *lttng_consumer_create(
955 enum lttng_consumer_type type,
956 ssize_t (*buffer_ready)(struct lttng_consumer_stream *stream,
957 struct lttng_consumer_local_data *ctx),
958 int (*recv_channel)(struct lttng_consumer_channel *channel),
959 int (*recv_stream)(struct lttng_consumer_stream *stream),
960 int (*update_stream)(int stream_key, uint32_t state))
961 {
962 int ret, i;
963 struct lttng_consumer_local_data *ctx;
964
965 assert(consumer_data.type == LTTNG_CONSUMER_UNKNOWN ||
966 consumer_data.type == type);
967 consumer_data.type = type;
968
969 ctx = zmalloc(sizeof(struct lttng_consumer_local_data));
970 if (ctx == NULL) {
971 perror("allocating context");
972 goto error;
973 }
974
975 ctx->consumer_error_socket = -1;
976 /* assign the callbacks */
977 ctx->on_buffer_ready = buffer_ready;
978 ctx->on_recv_channel = recv_channel;
979 ctx->on_recv_stream = recv_stream;
980 ctx->on_update_stream = update_stream;
981
982 ret = pipe(ctx->consumer_poll_pipe);
983 if (ret < 0) {
984 perror("Error creating poll pipe");
985 goto error_poll_pipe;
986 }
987
988 /* set read end of the pipe to non-blocking */
989 ret = fcntl(ctx->consumer_poll_pipe[0], F_SETFL, O_NONBLOCK);
990 if (ret < 0) {
991 perror("fcntl O_NONBLOCK");
992 goto error_poll_fcntl;
993 }
994
995 /* set write end of the pipe to non-blocking */
996 ret = fcntl(ctx->consumer_poll_pipe[1], F_SETFL, O_NONBLOCK);
997 if (ret < 0) {
998 perror("fcntl O_NONBLOCK");
999 goto error_poll_fcntl;
1000 }
1001
1002 ret = pipe(ctx->consumer_should_quit);
1003 if (ret < 0) {
1004 perror("Error creating recv pipe");
1005 goto error_quit_pipe;
1006 }
1007
1008 ret = pipe(ctx->consumer_thread_pipe);
1009 if (ret < 0) {
1010 perror("Error creating thread pipe");
1011 goto error_thread_pipe;
1012 }
1013
1014 return ctx;
1015
1016
1017 error_thread_pipe:
1018 for (i = 0; i < 2; i++) {
1019 int err;
1020
1021 err = close(ctx->consumer_should_quit[i]);
1022 if (err) {
1023 PERROR("close");
1024 }
1025 }
1026 error_poll_fcntl:
1027 error_quit_pipe:
1028 for (i = 0; i < 2; i++) {
1029 int err;
1030
1031 err = close(ctx->consumer_poll_pipe[i]);
1032 if (err) {
1033 PERROR("close");
1034 }
1035 }
1036 error_poll_pipe:
1037 free(ctx);
1038 error:
1039 return NULL;
1040 }
1041
1042 /*
1043 * Close all fds associated with the instance and free the context.
1044 */
1045 void lttng_consumer_destroy(struct lttng_consumer_local_data *ctx)
1046 {
1047 int ret;
1048
1049 ret = close(ctx->consumer_error_socket);
1050 if (ret) {
1051 PERROR("close");
1052 }
1053 ret = close(ctx->consumer_thread_pipe[0]);
1054 if (ret) {
1055 PERROR("close");
1056 }
1057 ret = close(ctx->consumer_thread_pipe[1]);
1058 if (ret) {
1059 PERROR("close");
1060 }
1061 ret = close(ctx->consumer_poll_pipe[0]);
1062 if (ret) {
1063 PERROR("close");
1064 }
1065 ret = close(ctx->consumer_poll_pipe[1]);
1066 if (ret) {
1067 PERROR("close");
1068 }
1069 ret = close(ctx->consumer_should_quit[0]);
1070 if (ret) {
1071 PERROR("close");
1072 }
1073 ret = close(ctx->consumer_should_quit[1]);
1074 if (ret) {
1075 PERROR("close");
1076 }
1077 unlink(ctx->consumer_command_sock_path);
1078 free(ctx);
1079 }
1080
1081 /*
1082 * Mmap the ring buffer, read it and write the data to the tracefile.
1083 *
1084 * Returns the number of bytes written
1085 */
1086 ssize_t lttng_consumer_on_read_subbuffer_mmap(
1087 struct lttng_consumer_local_data *ctx,
1088 struct lttng_consumer_stream *stream, unsigned long len)
1089 {
1090 switch (consumer_data.type) {
1091 case LTTNG_CONSUMER_KERNEL:
1092 return lttng_kconsumer_on_read_subbuffer_mmap(ctx, stream, len);
1093 case LTTNG_CONSUMER32_UST:
1094 case LTTNG_CONSUMER64_UST:
1095 return lttng_ustconsumer_on_read_subbuffer_mmap(ctx, stream, len);
1096 default:
1097 ERR("Unknown consumer_data type");
1098 assert(0);
1099 }
1100
1101 return 0;
1102 }
1103
1104 /*
1105 * Splice the data from the ring buffer to the tracefile.
1106 *
1107 * Returns the number of bytes spliced.
1108 */
1109 ssize_t lttng_consumer_on_read_subbuffer_splice(
1110 struct lttng_consumer_local_data *ctx,
1111 struct lttng_consumer_stream *stream, unsigned long len)
1112 {
1113 switch (consumer_data.type) {
1114 case LTTNG_CONSUMER_KERNEL:
1115 return lttng_kconsumer_on_read_subbuffer_splice(ctx, stream, len);
1116 case LTTNG_CONSUMER32_UST:
1117 case LTTNG_CONSUMER64_UST:
1118 return -ENOSYS;
1119 default:
1120 ERR("Unknown consumer_data type");
1121 assert(0);
1122 return -ENOSYS;
1123 }
1124
1125 }
1126
1127 /*
1128 * Take a snapshot for a specific fd
1129 *
1130 * Returns 0 on success, < 0 on error
1131 */
1132 int lttng_consumer_take_snapshot(struct lttng_consumer_local_data *ctx,
1133 struct lttng_consumer_stream *stream)
1134 {
1135 switch (consumer_data.type) {
1136 case LTTNG_CONSUMER_KERNEL:
1137 return lttng_kconsumer_take_snapshot(ctx, stream);
1138 case LTTNG_CONSUMER32_UST:
1139 case LTTNG_CONSUMER64_UST:
1140 return lttng_ustconsumer_take_snapshot(ctx, stream);
1141 default:
1142 ERR("Unknown consumer_data type");
1143 assert(0);
1144 return -ENOSYS;
1145 }
1146
1147 }
1148
1149 /*
1150 * Get the produced position
1151 *
1152 * Returns 0 on success, < 0 on error
1153 */
1154 int lttng_consumer_get_produced_snapshot(
1155 struct lttng_consumer_local_data *ctx,
1156 struct lttng_consumer_stream *stream,
1157 unsigned long *pos)
1158 {
1159 switch (consumer_data.type) {
1160 case LTTNG_CONSUMER_KERNEL:
1161 return lttng_kconsumer_get_produced_snapshot(ctx, stream, pos);
1162 case LTTNG_CONSUMER32_UST:
1163 case LTTNG_CONSUMER64_UST:
1164 return lttng_ustconsumer_get_produced_snapshot(ctx, stream, pos);
1165 default:
1166 ERR("Unknown consumer_data type");
1167 assert(0);
1168 return -ENOSYS;
1169 }
1170 }
1171
1172 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data *ctx,
1173 int sock, struct pollfd *consumer_sockpoll)
1174 {
1175 switch (consumer_data.type) {
1176 case LTTNG_CONSUMER_KERNEL:
1177 return lttng_kconsumer_recv_cmd(ctx, sock, consumer_sockpoll);
1178 case LTTNG_CONSUMER32_UST:
1179 case LTTNG_CONSUMER64_UST:
1180 return lttng_ustconsumer_recv_cmd(ctx, sock, consumer_sockpoll);
1181 default:
1182 ERR("Unknown consumer_data type");
1183 assert(0);
1184 return -ENOSYS;
1185 }
1186 }
1187
1188 /*
1189 * This thread polls the fds in the set to consume the data and write
1190 * it to tracefile if necessary.
1191 */
1192 void *lttng_consumer_thread_poll_fds(void *data)
1193 {
1194 int num_rdy, num_hup, high_prio, ret, i;
1195 struct pollfd *pollfd = NULL;
1196 /* local view of the streams */
1197 struct lttng_consumer_stream **local_stream = NULL;
1198 /* local view of consumer_data.fds_count */
1199 int nb_fd = 0;
1200 struct lttng_consumer_local_data *ctx = data;
1201 struct lttng_ht *metadata_ht;
1202 struct lttng_ht_iter iter;
1203 struct lttng_ht_node_ulong *node;
1204 struct lttng_consumer_stream *metadata_stream;
1205 ssize_t len;
1206
1207 metadata_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1208
1209 rcu_register_thread();
1210
1211 local_stream = zmalloc(sizeof(struct lttng_consumer_stream));
1212
1213 while (1) {
1214 high_prio = 0;
1215 num_hup = 0;
1216
1217 /*
1218 * the fds set has been updated, we need to update our
1219 * local array as well
1220 */
1221 pthread_mutex_lock(&consumer_data.lock);
1222 if (consumer_data.need_update) {
1223 if (pollfd != NULL) {
1224 free(pollfd);
1225 pollfd = NULL;
1226 }
1227 if (local_stream != NULL) {
1228 free(local_stream);
1229 local_stream = NULL;
1230 }
1231
1232 /* allocate for all fds + 1 for the consumer_poll_pipe */
1233 pollfd = zmalloc((consumer_data.stream_count + 1) * sizeof(struct pollfd));
1234 if (pollfd == NULL) {
1235 perror("pollfd malloc");
1236 pthread_mutex_unlock(&consumer_data.lock);
1237 goto end;
1238 }
1239
1240 /* allocate for all fds + 1 for the consumer_poll_pipe */
1241 local_stream = zmalloc((consumer_data.stream_count + 1) *
1242 sizeof(struct lttng_consumer_stream));
1243 if (local_stream == NULL) {
1244 perror("local_stream malloc");
1245 pthread_mutex_unlock(&consumer_data.lock);
1246 goto end;
1247 }
1248 ret = consumer_update_poll_array(ctx, &pollfd, local_stream,
1249 metadata_ht);
1250 if (ret < 0) {
1251 ERR("Error in allocating pollfd or local_outfds");
1252 lttng_consumer_send_error(ctx, CONSUMERD_POLL_ERROR);
1253 pthread_mutex_unlock(&consumer_data.lock);
1254 goto end;
1255 }
1256 nb_fd = ret;
1257 consumer_data.need_update = 0;
1258 }
1259 pthread_mutex_unlock(&consumer_data.lock);
1260
1261 /* No FDs and consumer_quit, consumer_cleanup the thread */
1262 if (nb_fd == 0 && consumer_quit == 1) {
1263 goto end;
1264 }
1265 /* poll on the array of fds */
1266 restart:
1267 DBG("polling on %d fd", nb_fd + 1);
1268 num_rdy = poll(pollfd, nb_fd + 1, consumer_poll_timeout);
1269 DBG("poll num_rdy : %d", num_rdy);
1270 if (num_rdy == -1) {
1271 /*
1272 * Restart interrupted system call.
1273 */
1274 if (errno == EINTR) {
1275 goto restart;
1276 }
1277 perror("Poll error");
1278 lttng_consumer_send_error(ctx, CONSUMERD_POLL_ERROR);
1279 goto end;
1280 } else if (num_rdy == 0) {
1281 DBG("Polling thread timed out");
1282 goto end;
1283 }
1284
1285 /*
1286 * If the consumer_poll_pipe triggered poll go directly to the
1287 * beginning of the loop to update the array. We want to prioritize
1288 * array update over low-priority reads.
1289 */
1290 if (pollfd[nb_fd].revents & (POLLIN | POLLPRI)) {
1291 size_t pipe_readlen;
1292 char tmp;
1293
1294 DBG("consumer_poll_pipe wake up");
1295 /* Consume 1 byte of pipe data */
1296 do {
1297 pipe_readlen = read(ctx->consumer_poll_pipe[0], &tmp, 1);
1298 } while (pipe_readlen == -1 && errno == EINTR);
1299 continue;
1300 }
1301
1302 /* Take care of high priority channels first. */
1303 for (i = 0; i < nb_fd; i++) {
1304 /* Lookup for metadata which is the highest priority */
1305 lttng_ht_lookup(metadata_ht,
1306 (void *)((unsigned long) pollfd[i].fd), &iter);
1307 node = lttng_ht_iter_get_node_ulong(&iter);
1308 if (node != NULL &&
1309 (pollfd[i].revents & (POLLIN | POLLPRI))) {
1310 DBG("Urgent metadata read on fd %d", pollfd[i].fd);
1311 metadata_stream = caa_container_of(node,
1312 struct lttng_consumer_stream, waitfd_node);
1313 high_prio = 1;
1314 len = ctx->on_buffer_ready(metadata_stream, ctx);
1315 /* it's ok to have an unavailable sub-buffer */
1316 if (len < 0 && len != -EAGAIN) {
1317 goto end;
1318 } else if (len > 0) {
1319 metadata_stream->data_read = 1;
1320 }
1321 } else if (pollfd[i].revents & POLLPRI) {
1322 DBG("Urgent read on fd %d", pollfd[i].fd);
1323 high_prio = 1;
1324 len = ctx->on_buffer_ready(local_stream[i], ctx);
1325 /* it's ok to have an unavailable sub-buffer */
1326 if (len < 0 && len != -EAGAIN) {
1327 goto end;
1328 } else if (len > 0) {
1329 local_stream[i]->data_read = 1;
1330 }
1331 }
1332 }
1333
1334 /*
1335 * If we read high prio channel in this loop, try again
1336 * for more high prio data.
1337 */
1338 if (high_prio) {
1339 continue;
1340 }
1341
1342 /* Take care of low priority channels. */
1343 for (i = 0; i < nb_fd; i++) {
1344 if ((pollfd[i].revents & POLLIN) ||
1345 local_stream[i]->hangup_flush_done) {
1346 DBG("Normal read on fd %d", pollfd[i].fd);
1347 len = ctx->on_buffer_ready(local_stream[i], ctx);
1348 /* it's ok to have an unavailable sub-buffer */
1349 if (len < 0 && len != -EAGAIN) {
1350 goto end;
1351 } else if (len > 0) {
1352 local_stream[i]->data_read = 1;
1353 }
1354 }
1355 }
1356
1357 /* Handle hangup and errors */
1358 for (i = 0; i < nb_fd; i++) {
1359 if (!local_stream[i]->hangup_flush_done
1360 && (pollfd[i].revents & (POLLHUP | POLLERR | POLLNVAL))
1361 && (consumer_data.type == LTTNG_CONSUMER32_UST
1362 || consumer_data.type == LTTNG_CONSUMER64_UST)) {
1363 DBG("fd %d is hup|err|nval. Attempting flush and read.",
1364 pollfd[i].fd);
1365 lttng_ustconsumer_on_stream_hangup(local_stream[i]);
1366 /* Attempt read again, for the data we just flushed. */
1367 local_stream[i]->data_read = 1;
1368 }
1369 /*
1370 * If the poll flag is HUP/ERR/NVAL and we have
1371 * read no data in this pass, we can remove the
1372 * stream from its hash table.
1373 */
1374 if ((pollfd[i].revents & POLLHUP)) {
1375 DBG("Polling fd %d tells it has hung up.", pollfd[i].fd);
1376 if (!local_stream[i]->data_read) {
1377 if (local_stream[i]->metadata_flag) {
1378 iter.iter.node = &local_stream[i]->waitfd_node.node;
1379 ret = lttng_ht_del(metadata_ht, &iter);
1380 assert(!ret);
1381 }
1382 consumer_del_stream(local_stream[i]);
1383 num_hup++;
1384 }
1385 } else if (pollfd[i].revents & POLLERR) {
1386 ERR("Error returned in polling fd %d.", pollfd[i].fd);
1387 if (!local_stream[i]->data_read) {
1388 if (local_stream[i]->metadata_flag) {
1389 iter.iter.node = &local_stream[i]->waitfd_node.node;
1390 ret = lttng_ht_del(metadata_ht, &iter);
1391 assert(!ret);
1392 }
1393 consumer_del_stream(local_stream[i]);
1394 num_hup++;
1395 }
1396 } else if (pollfd[i].revents & POLLNVAL) {
1397 ERR("Polling fd %d tells fd is not open.", pollfd[i].fd);
1398 if (!local_stream[i]->data_read) {
1399 if (local_stream[i]->metadata_flag) {
1400 iter.iter.node = &local_stream[i]->waitfd_node.node;
1401 ret = lttng_ht_del(metadata_ht, &iter);
1402 assert(!ret);
1403 }
1404 consumer_del_stream(local_stream[i]);
1405 num_hup++;
1406 }
1407 }
1408 local_stream[i]->data_read = 0;
1409 }
1410 }
1411 end:
1412 DBG("polling thread exiting");
1413 if (pollfd != NULL) {
1414 free(pollfd);
1415 pollfd = NULL;
1416 }
1417 if (local_stream != NULL) {
1418 free(local_stream);
1419 local_stream = NULL;
1420 }
1421 rcu_unregister_thread();
1422 return NULL;
1423 }
1424
1425 /*
1426 * This thread listens on the consumerd socket and receives the file
1427 * descriptors from the session daemon.
1428 */
1429 void *lttng_consumer_thread_receive_fds(void *data)
1430 {
1431 int sock, client_socket, ret;
1432 /*
1433 * structure to poll for incoming data on communication socket avoids
1434 * making blocking sockets.
1435 */
1436 struct pollfd consumer_sockpoll[2];
1437 struct lttng_consumer_local_data *ctx = data;
1438
1439 rcu_register_thread();
1440
1441 DBG("Creating command socket %s", ctx->consumer_command_sock_path);
1442 unlink(ctx->consumer_command_sock_path);
1443 client_socket = lttcomm_create_unix_sock(ctx->consumer_command_sock_path);
1444 if (client_socket < 0) {
1445 ERR("Cannot create command socket");
1446 goto end;
1447 }
1448
1449 ret = lttcomm_listen_unix_sock(client_socket);
1450 if (ret < 0) {
1451 goto end;
1452 }
1453
1454 DBG("Sending ready command to lttng-sessiond");
1455 ret = lttng_consumer_send_error(ctx, CONSUMERD_COMMAND_SOCK_READY);
1456 /* return < 0 on error, but == 0 is not fatal */
1457 if (ret < 0) {
1458 ERR("Error sending ready command to lttng-sessiond");
1459 goto end;
1460 }
1461
1462 ret = fcntl(client_socket, F_SETFL, O_NONBLOCK);
1463 if (ret < 0) {
1464 perror("fcntl O_NONBLOCK");
1465 goto end;
1466 }
1467
1468 /* prepare the FDs to poll : to client socket and the should_quit pipe */
1469 consumer_sockpoll[0].fd = ctx->consumer_should_quit[0];
1470 consumer_sockpoll[0].events = POLLIN | POLLPRI;
1471 consumer_sockpoll[1].fd = client_socket;
1472 consumer_sockpoll[1].events = POLLIN | POLLPRI;
1473
1474 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
1475 goto end;
1476 }
1477 DBG("Connection on client_socket");
1478
1479 /* Blocking call, waiting for transmission */
1480 sock = lttcomm_accept_unix_sock(client_socket);
1481 if (sock <= 0) {
1482 WARN("On accept");
1483 goto end;
1484 }
1485 ret = fcntl(sock, F_SETFL, O_NONBLOCK);
1486 if (ret < 0) {
1487 perror("fcntl O_NONBLOCK");
1488 goto end;
1489 }
1490
1491 /* update the polling structure to poll on the established socket */
1492 consumer_sockpoll[1].fd = sock;
1493 consumer_sockpoll[1].events = POLLIN | POLLPRI;
1494
1495 while (1) {
1496 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
1497 goto end;
1498 }
1499 DBG("Incoming command on sock");
1500 ret = lttng_consumer_recv_cmd(ctx, sock, consumer_sockpoll);
1501 if (ret == -ENOENT) {
1502 DBG("Received STOP command");
1503 goto end;
1504 }
1505 if (ret < 0) {
1506 ERR("Communication interrupted on command socket");
1507 goto end;
1508 }
1509 if (consumer_quit) {
1510 DBG("consumer_thread_receive_fds received quit from signal");
1511 goto end;
1512 }
1513 DBG("received fds on sock");
1514 }
1515 end:
1516 DBG("consumer_thread_receive_fds exiting");
1517
1518 /*
1519 * when all fds have hung up, the polling thread
1520 * can exit cleanly
1521 */
1522 consumer_quit = 1;
1523
1524 /*
1525 * 2s of grace period, if no polling events occur during
1526 * this period, the polling thread will exit even if there
1527 * are still open FDs (should not happen, but safety mechanism).
1528 */
1529 consumer_poll_timeout = LTTNG_CONSUMER_POLL_TIMEOUT;
1530
1531 /*
1532 * Wake-up the other end by writing a null byte in the pipe
1533 * (non-blocking). Important note: Because writing into the
1534 * pipe is non-blocking (and therefore we allow dropping wakeup
1535 * data, as long as there is wakeup data present in the pipe
1536 * buffer to wake up the other end), the other end should
1537 * perform the following sequence for waiting:
1538 * 1) empty the pipe (reads).
1539 * 2) perform update operation.
1540 * 3) wait on the pipe (poll).
1541 */
1542 do {
1543 ret = write(ctx->consumer_poll_pipe[1], "", 1);
1544 } while (ret < 0 && errno == EINTR);
1545 rcu_unregister_thread();
1546 return NULL;
1547 }
1548
1549 ssize_t lttng_consumer_read_subbuffer(struct lttng_consumer_stream *stream,
1550 struct lttng_consumer_local_data *ctx)
1551 {
1552 switch (consumer_data.type) {
1553 case LTTNG_CONSUMER_KERNEL:
1554 return lttng_kconsumer_read_subbuffer(stream, ctx);
1555 case LTTNG_CONSUMER32_UST:
1556 case LTTNG_CONSUMER64_UST:
1557 return lttng_ustconsumer_read_subbuffer(stream, ctx);
1558 default:
1559 ERR("Unknown consumer_data type");
1560 assert(0);
1561 return -ENOSYS;
1562 }
1563 }
1564
1565 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream *stream)
1566 {
1567 switch (consumer_data.type) {
1568 case LTTNG_CONSUMER_KERNEL:
1569 return lttng_kconsumer_on_recv_stream(stream);
1570 case LTTNG_CONSUMER32_UST:
1571 case LTTNG_CONSUMER64_UST:
1572 return lttng_ustconsumer_on_recv_stream(stream);
1573 default:
1574 ERR("Unknown consumer_data type");
1575 assert(0);
1576 return -ENOSYS;
1577 }
1578 }
1579
1580 /*
1581 * Allocate and set consumer data hash tables.
1582 */
1583 void lttng_consumer_init(void)
1584 {
1585 consumer_data.stream_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1586 consumer_data.channel_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1587 consumer_data.relayd_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1588 }
This page took 0.06554 seconds and 3 git commands to generate.