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