Fix: kernel ctl error codes are based on errno
[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 #include <signal.h>
32
33 #include <common/common.h>
34 #include <common/utils.h>
35 #include <common/compat/poll.h>
36 #include <common/kernel-ctl/kernel-ctl.h>
37 #include <common/sessiond-comm/relayd.h>
38 #include <common/sessiond-comm/sessiond-comm.h>
39 #include <common/kernel-consumer/kernel-consumer.h>
40 #include <common/relayd/relayd.h>
41 #include <common/ust-consumer/ust-consumer.h>
42
43 #include "consumer.h"
44
45 struct lttng_consumer_global_data consumer_data = {
46 .stream_count = 0,
47 .need_update = 1,
48 .type = LTTNG_CONSUMER_UNKNOWN,
49 };
50
51 enum consumer_channel_action {
52 CONSUMER_CHANNEL_ADD,
53 CONSUMER_CHANNEL_DEL,
54 CONSUMER_CHANNEL_QUIT,
55 };
56
57 struct consumer_channel_msg {
58 enum consumer_channel_action action;
59 struct lttng_consumer_channel *chan; /* add */
60 uint64_t key; /* del */
61 };
62
63 /*
64 * Flag to inform the polling thread to quit when all fd hung up. Updated by
65 * the consumer_thread_receive_fds when it notices that all fds has hung up.
66 * Also updated by the signal handler (consumer_should_exit()). Read by the
67 * polling threads.
68 */
69 volatile int consumer_quit;
70
71 /*
72 * Global hash table containing respectively metadata and data streams. The
73 * stream element in this ht should only be updated by the metadata poll thread
74 * for the metadata and the data poll thread for the data.
75 */
76 static struct lttng_ht *metadata_ht;
77 static struct lttng_ht *data_ht;
78
79 /*
80 * Notify a thread lttng pipe to poll back again. This usually means that some
81 * global state has changed so we just send back the thread in a poll wait
82 * call.
83 */
84 static void notify_thread_lttng_pipe(struct lttng_pipe *pipe)
85 {
86 struct lttng_consumer_stream *null_stream = NULL;
87
88 assert(pipe);
89
90 (void) lttng_pipe_write(pipe, &null_stream, sizeof(null_stream));
91 }
92
93 static void notify_channel_pipe(struct lttng_consumer_local_data *ctx,
94 struct lttng_consumer_channel *chan,
95 uint64_t key,
96 enum consumer_channel_action action)
97 {
98 struct consumer_channel_msg msg;
99 int ret;
100
101 memset(&msg, 0, sizeof(msg));
102
103 msg.action = action;
104 msg.chan = chan;
105 msg.key = key;
106 do {
107 ret = write(ctx->consumer_channel_pipe[1], &msg, sizeof(msg));
108 } while (ret < 0 && errno == EINTR);
109 }
110
111 void notify_thread_del_channel(struct lttng_consumer_local_data *ctx,
112 uint64_t key)
113 {
114 notify_channel_pipe(ctx, NULL, key, CONSUMER_CHANNEL_DEL);
115 }
116
117 static int read_channel_pipe(struct lttng_consumer_local_data *ctx,
118 struct lttng_consumer_channel **chan,
119 uint64_t *key,
120 enum consumer_channel_action *action)
121 {
122 struct consumer_channel_msg msg;
123 int ret;
124
125 do {
126 ret = read(ctx->consumer_channel_pipe[0], &msg, sizeof(msg));
127 } while (ret < 0 && errno == EINTR);
128 if (ret > 0) {
129 *action = msg.action;
130 *chan = msg.chan;
131 *key = msg.key;
132 }
133 return ret;
134 }
135
136 /*
137 * Find a stream. The consumer_data.lock must be locked during this
138 * call.
139 */
140 static struct lttng_consumer_stream *find_stream(uint64_t key,
141 struct lttng_ht *ht)
142 {
143 struct lttng_ht_iter iter;
144 struct lttng_ht_node_u64 *node;
145 struct lttng_consumer_stream *stream = NULL;
146
147 assert(ht);
148
149 /* -1ULL keys are lookup failures */
150 if (key == (uint64_t) -1ULL) {
151 return NULL;
152 }
153
154 rcu_read_lock();
155
156 lttng_ht_lookup(ht, &key, &iter);
157 node = lttng_ht_iter_get_node_u64(&iter);
158 if (node != NULL) {
159 stream = caa_container_of(node, struct lttng_consumer_stream, node);
160 }
161
162 rcu_read_unlock();
163
164 return stream;
165 }
166
167 static void steal_stream_key(uint64_t key, struct lttng_ht *ht)
168 {
169 struct lttng_consumer_stream *stream;
170
171 rcu_read_lock();
172 stream = find_stream(key, ht);
173 if (stream) {
174 stream->key = (uint64_t) -1ULL;
175 /*
176 * We don't want the lookup to match, but we still need
177 * to iterate on this stream when iterating over the hash table. Just
178 * change the node key.
179 */
180 stream->node.key = (uint64_t) -1ULL;
181 }
182 rcu_read_unlock();
183 }
184
185 /*
186 * Return a channel object for the given key.
187 *
188 * RCU read side lock MUST be acquired before calling this function and
189 * protects the channel ptr.
190 */
191 struct lttng_consumer_channel *consumer_find_channel(uint64_t key)
192 {
193 struct lttng_ht_iter iter;
194 struct lttng_ht_node_u64 *node;
195 struct lttng_consumer_channel *channel = NULL;
196
197 /* -1ULL keys are lookup failures */
198 if (key == (uint64_t) -1ULL) {
199 return NULL;
200 }
201
202 lttng_ht_lookup(consumer_data.channel_ht, &key, &iter);
203 node = lttng_ht_iter_get_node_u64(&iter);
204 if (node != NULL) {
205 channel = caa_container_of(node, struct lttng_consumer_channel, node);
206 }
207
208 return channel;
209 }
210
211 static void free_stream_rcu(struct rcu_head *head)
212 {
213 struct lttng_ht_node_u64 *node =
214 caa_container_of(head, struct lttng_ht_node_u64, head);
215 struct lttng_consumer_stream *stream =
216 caa_container_of(node, struct lttng_consumer_stream, node);
217
218 free(stream);
219 }
220
221 static void free_channel_rcu(struct rcu_head *head)
222 {
223 struct lttng_ht_node_u64 *node =
224 caa_container_of(head, struct lttng_ht_node_u64, head);
225 struct lttng_consumer_channel *channel =
226 caa_container_of(node, struct lttng_consumer_channel, node);
227
228 free(channel);
229 }
230
231 /*
232 * RCU protected relayd socket pair free.
233 */
234 static void free_relayd_rcu(struct rcu_head *head)
235 {
236 struct lttng_ht_node_u64 *node =
237 caa_container_of(head, struct lttng_ht_node_u64, head);
238 struct consumer_relayd_sock_pair *relayd =
239 caa_container_of(node, struct consumer_relayd_sock_pair, node);
240
241 /*
242 * Close all sockets. This is done in the call RCU since we don't want the
243 * socket fds to be reassigned thus potentially creating bad state of the
244 * relayd object.
245 *
246 * We do not have to lock the control socket mutex here since at this stage
247 * there is no one referencing to this relayd object.
248 */
249 (void) relayd_close(&relayd->control_sock);
250 (void) relayd_close(&relayd->data_sock);
251
252 free(relayd);
253 }
254
255 /*
256 * Destroy and free relayd socket pair object.
257 *
258 * This function MUST be called with the consumer_data lock acquired.
259 */
260 static void destroy_relayd(struct consumer_relayd_sock_pair *relayd)
261 {
262 int ret;
263 struct lttng_ht_iter iter;
264
265 if (relayd == NULL) {
266 return;
267 }
268
269 DBG("Consumer destroy and close relayd socket pair");
270
271 iter.iter.node = &relayd->node.node;
272 ret = lttng_ht_del(consumer_data.relayd_ht, &iter);
273 if (ret != 0) {
274 /* We assume the relayd is being or is destroyed */
275 return;
276 }
277
278 /* RCU free() call */
279 call_rcu(&relayd->node.head, free_relayd_rcu);
280 }
281
282 /*
283 * Remove a channel from the global list protected by a mutex. This function is
284 * also responsible for freeing its data structures.
285 */
286 void consumer_del_channel(struct lttng_consumer_channel *channel)
287 {
288 int ret;
289 struct lttng_ht_iter iter;
290 struct lttng_consumer_stream *stream, *stmp;
291
292 DBG("Consumer delete channel key %" PRIu64, channel->key);
293
294 pthread_mutex_lock(&consumer_data.lock);
295 pthread_mutex_lock(&channel->lock);
296
297 switch (consumer_data.type) {
298 case LTTNG_CONSUMER_KERNEL:
299 break;
300 case LTTNG_CONSUMER32_UST:
301 case LTTNG_CONSUMER64_UST:
302 /* Delete streams that might have been left in the stream list. */
303 cds_list_for_each_entry_safe(stream, stmp, &channel->streams.head,
304 send_node) {
305 cds_list_del(&stream->send_node);
306 lttng_ustconsumer_del_stream(stream);
307 free(stream);
308 }
309 lttng_ustconsumer_del_channel(channel);
310 break;
311 default:
312 ERR("Unknown consumer_data type");
313 assert(0);
314 goto end;
315 }
316
317 rcu_read_lock();
318 iter.iter.node = &channel->node.node;
319 ret = lttng_ht_del(consumer_data.channel_ht, &iter);
320 assert(!ret);
321 rcu_read_unlock();
322
323 call_rcu(&channel->node.head, free_channel_rcu);
324 end:
325 pthread_mutex_unlock(&channel->lock);
326 pthread_mutex_unlock(&consumer_data.lock);
327 }
328
329 /*
330 * Iterate over the relayd hash table and destroy each element. Finally,
331 * destroy the whole hash table.
332 */
333 static void cleanup_relayd_ht(void)
334 {
335 struct lttng_ht_iter iter;
336 struct consumer_relayd_sock_pair *relayd;
337
338 rcu_read_lock();
339
340 cds_lfht_for_each_entry(consumer_data.relayd_ht->ht, &iter.iter, relayd,
341 node.node) {
342 destroy_relayd(relayd);
343 }
344
345 rcu_read_unlock();
346
347 lttng_ht_destroy(consumer_data.relayd_ht);
348 }
349
350 /*
351 * Update the end point status of all streams having the given network sequence
352 * index (relayd index).
353 *
354 * It's atomically set without having the stream mutex locked which is fine
355 * because we handle the write/read race with a pipe wakeup for each thread.
356 */
357 static void update_endpoint_status_by_netidx(uint64_t net_seq_idx,
358 enum consumer_endpoint_status status)
359 {
360 struct lttng_ht_iter iter;
361 struct lttng_consumer_stream *stream;
362
363 DBG("Consumer set delete flag on stream by idx %" PRIu64, net_seq_idx);
364
365 rcu_read_lock();
366
367 /* Let's begin with metadata */
368 cds_lfht_for_each_entry(metadata_ht->ht, &iter.iter, stream, node.node) {
369 if (stream->net_seq_idx == net_seq_idx) {
370 uatomic_set(&stream->endpoint_status, status);
371 DBG("Delete flag set to metadata stream %d", stream->wait_fd);
372 }
373 }
374
375 /* Follow up by the data streams */
376 cds_lfht_for_each_entry(data_ht->ht, &iter.iter, stream, node.node) {
377 if (stream->net_seq_idx == net_seq_idx) {
378 uatomic_set(&stream->endpoint_status, status);
379 DBG("Delete flag set to data stream %d", stream->wait_fd);
380 }
381 }
382 rcu_read_unlock();
383 }
384
385 /*
386 * Cleanup a relayd object by flagging every associated streams for deletion,
387 * destroying the object meaning removing it from the relayd hash table,
388 * closing the sockets and freeing the memory in a RCU call.
389 *
390 * If a local data context is available, notify the threads that the streams'
391 * state have changed.
392 */
393 static void cleanup_relayd(struct consumer_relayd_sock_pair *relayd,
394 struct lttng_consumer_local_data *ctx)
395 {
396 uint64_t netidx;
397
398 assert(relayd);
399
400 DBG("Cleaning up relayd sockets");
401
402 /* Save the net sequence index before destroying the object */
403 netidx = relayd->net_seq_idx;
404
405 /*
406 * Delete the relayd from the relayd hash table, close the sockets and free
407 * the object in a RCU call.
408 */
409 destroy_relayd(relayd);
410
411 /* Set inactive endpoint to all streams */
412 update_endpoint_status_by_netidx(netidx, CONSUMER_ENDPOINT_INACTIVE);
413
414 /*
415 * With a local data context, notify the threads that the streams' state
416 * have changed. The write() action on the pipe acts as an "implicit"
417 * memory barrier ordering the updates of the end point status from the
418 * read of this status which happens AFTER receiving this notify.
419 */
420 if (ctx) {
421 notify_thread_lttng_pipe(ctx->consumer_data_pipe);
422 notify_thread_lttng_pipe(ctx->consumer_metadata_pipe);
423 }
424 }
425
426 /*
427 * Flag a relayd socket pair for destruction. Destroy it if the refcount
428 * reaches zero.
429 *
430 * RCU read side lock MUST be aquired before calling this function.
431 */
432 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair *relayd)
433 {
434 assert(relayd);
435
436 /* Set destroy flag for this object */
437 uatomic_set(&relayd->destroy_flag, 1);
438
439 /* Destroy the relayd if refcount is 0 */
440 if (uatomic_read(&relayd->refcount) == 0) {
441 destroy_relayd(relayd);
442 }
443 }
444
445 /*
446 * Remove a stream from the global list protected by a mutex. This
447 * function is also responsible for freeing its data structures.
448 */
449 void consumer_del_stream(struct lttng_consumer_stream *stream,
450 struct lttng_ht *ht)
451 {
452 int ret;
453 struct lttng_ht_iter iter;
454 struct lttng_consumer_channel *free_chan = NULL;
455 struct consumer_relayd_sock_pair *relayd;
456
457 assert(stream);
458
459 DBG("Consumer del stream %d", stream->wait_fd);
460
461 if (ht == NULL) {
462 /* Means the stream was allocated but not successfully added */
463 goto free_stream_rcu;
464 }
465
466 pthread_mutex_lock(&consumer_data.lock);
467 pthread_mutex_lock(&stream->lock);
468
469 switch (consumer_data.type) {
470 case LTTNG_CONSUMER_KERNEL:
471 if (stream->mmap_base != NULL) {
472 ret = munmap(stream->mmap_base, stream->mmap_len);
473 if (ret != 0) {
474 PERROR("munmap");
475 }
476 }
477
478 if (stream->wait_fd >= 0) {
479 ret = close(stream->wait_fd);
480 if (ret) {
481 PERROR("close");
482 }
483 }
484 break;
485 case LTTNG_CONSUMER32_UST:
486 case LTTNG_CONSUMER64_UST:
487 lttng_ustconsumer_del_stream(stream);
488 break;
489 default:
490 ERR("Unknown consumer_data type");
491 assert(0);
492 goto end;
493 }
494
495 rcu_read_lock();
496 iter.iter.node = &stream->node.node;
497 ret = lttng_ht_del(ht, &iter);
498 assert(!ret);
499
500 iter.iter.node = &stream->node_channel_id.node;
501 ret = lttng_ht_del(consumer_data.stream_per_chan_id_ht, &iter);
502 assert(!ret);
503
504 iter.iter.node = &stream->node_session_id.node;
505 ret = lttng_ht_del(consumer_data.stream_list_ht, &iter);
506 assert(!ret);
507 rcu_read_unlock();
508
509 assert(consumer_data.stream_count > 0);
510 consumer_data.stream_count--;
511
512 if (stream->out_fd >= 0) {
513 ret = close(stream->out_fd);
514 if (ret) {
515 PERROR("close");
516 }
517 }
518
519 /* Check and cleanup relayd */
520 rcu_read_lock();
521 relayd = consumer_find_relayd(stream->net_seq_idx);
522 if (relayd != NULL) {
523 uatomic_dec(&relayd->refcount);
524 assert(uatomic_read(&relayd->refcount) >= 0);
525
526 /* Closing streams requires to lock the control socket. */
527 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
528 ret = relayd_send_close_stream(&relayd->control_sock,
529 stream->relayd_stream_id,
530 stream->next_net_seq_num - 1);
531 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
532 if (ret < 0) {
533 DBG("Unable to close stream on the relayd. Continuing");
534 /*
535 * Continue here. There is nothing we can do for the relayd.
536 * Chances are that the relayd has closed the socket so we just
537 * continue cleaning up.
538 */
539 }
540
541 /* Both conditions are met, we destroy the relayd. */
542 if (uatomic_read(&relayd->refcount) == 0 &&
543 uatomic_read(&relayd->destroy_flag)) {
544 destroy_relayd(relayd);
545 }
546 }
547 rcu_read_unlock();
548
549 if (!uatomic_sub_return(&stream->chan->refcount, 1)
550 && !uatomic_read(&stream->chan->nb_init_stream_left)) {
551 free_chan = stream->chan;
552 }
553
554 end:
555 consumer_data.need_update = 1;
556 pthread_mutex_unlock(&stream->lock);
557 pthread_mutex_unlock(&consumer_data.lock);
558
559 if (free_chan) {
560 consumer_del_channel(free_chan);
561 }
562
563 free_stream_rcu:
564 call_rcu(&stream->node.head, free_stream_rcu);
565 }
566
567 /*
568 * XXX naming of del vs destroy is all mixed up.
569 */
570 void consumer_del_stream_for_data(struct lttng_consumer_stream *stream)
571 {
572 consumer_del_stream(stream, data_ht);
573 }
574
575 void consumer_del_stream_for_metadata(struct lttng_consumer_stream *stream)
576 {
577 consumer_del_stream(stream, metadata_ht);
578 }
579
580 struct lttng_consumer_stream *consumer_allocate_stream(uint64_t channel_key,
581 uint64_t stream_key,
582 enum lttng_consumer_stream_state state,
583 const char *channel_name,
584 uid_t uid,
585 gid_t gid,
586 uint64_t relayd_id,
587 uint64_t session_id,
588 int cpu,
589 int *alloc_ret,
590 enum consumer_channel_type type)
591 {
592 int ret;
593 struct lttng_consumer_stream *stream;
594
595 stream = zmalloc(sizeof(*stream));
596 if (stream == NULL) {
597 PERROR("malloc struct lttng_consumer_stream");
598 ret = -ENOMEM;
599 goto end;
600 }
601
602 rcu_read_lock();
603
604 stream->key = stream_key;
605 stream->out_fd = -1;
606 stream->out_fd_offset = 0;
607 stream->state = state;
608 stream->uid = uid;
609 stream->gid = gid;
610 stream->net_seq_idx = relayd_id;
611 stream->session_id = session_id;
612 stream->endpoint_status = CONSUMER_ENDPOINT_ACTIVE;
613 pthread_mutex_init(&stream->lock, NULL);
614
615 /* If channel is the metadata, flag this stream as metadata. */
616 if (type == CONSUMER_CHANNEL_TYPE_METADATA) {
617 stream->metadata_flag = 1;
618 /* Metadata is flat out. */
619 strncpy(stream->name, DEFAULT_METADATA_NAME, sizeof(stream->name));
620 } else {
621 /* Format stream name to <channel_name>_<cpu_number> */
622 ret = snprintf(stream->name, sizeof(stream->name), "%s_%d",
623 channel_name, cpu);
624 if (ret < 0) {
625 PERROR("snprintf stream name");
626 goto error;
627 }
628 }
629
630 /* Key is always the wait_fd for streams. */
631 lttng_ht_node_init_u64(&stream->node, stream->key);
632
633 /* Init node per channel id key */
634 lttng_ht_node_init_u64(&stream->node_channel_id, channel_key);
635
636 /* Init session id node with the stream session id */
637 lttng_ht_node_init_u64(&stream->node_session_id, stream->session_id);
638
639 DBG3("Allocated stream %s (key %" PRIu64 ", chan_key %" PRIu64 " relayd_id %" PRIu64 ", session_id %" PRIu64,
640 stream->name, stream->key, channel_key, stream->net_seq_idx, stream->session_id);
641
642 rcu_read_unlock();
643 return stream;
644
645 error:
646 rcu_read_unlock();
647 free(stream);
648 end:
649 if (alloc_ret) {
650 *alloc_ret = ret;
651 }
652 return NULL;
653 }
654
655 /*
656 * Add a stream to the global list protected by a mutex.
657 */
658 int consumer_add_data_stream(struct lttng_consumer_stream *stream)
659 {
660 struct lttng_ht *ht = data_ht;
661 int ret = 0;
662 struct consumer_relayd_sock_pair *relayd;
663
664 assert(stream);
665 assert(ht);
666
667 DBG3("Adding consumer stream %" PRIu64, stream->key);
668
669 pthread_mutex_lock(&consumer_data.lock);
670 pthread_mutex_lock(&stream->chan->lock);
671 pthread_mutex_lock(&stream->chan->timer_lock);
672 pthread_mutex_lock(&stream->lock);
673 rcu_read_lock();
674
675 /* Steal stream identifier to avoid having streams with the same key */
676 steal_stream_key(stream->key, ht);
677
678 lttng_ht_add_unique_u64(ht, &stream->node);
679
680 lttng_ht_add_u64(consumer_data.stream_per_chan_id_ht,
681 &stream->node_channel_id);
682
683 /*
684 * Add stream to the stream_list_ht of the consumer data. No need to steal
685 * the key since the HT does not use it and we allow to add redundant keys
686 * into this table.
687 */
688 lttng_ht_add_u64(consumer_data.stream_list_ht, &stream->node_session_id);
689
690 /* Check and cleanup relayd */
691 relayd = consumer_find_relayd(stream->net_seq_idx);
692 if (relayd != NULL) {
693 uatomic_inc(&relayd->refcount);
694 }
695
696 /*
697 * When nb_init_stream_left reaches 0, we don't need to trigger any action
698 * in terms of destroying the associated channel, because the action that
699 * causes the count to become 0 also causes a stream to be added. The
700 * channel deletion will thus be triggered by the following removal of this
701 * stream.
702 */
703 if (uatomic_read(&stream->chan->nb_init_stream_left) > 0) {
704 /* Increment refcount before decrementing nb_init_stream_left */
705 cmm_smp_wmb();
706 uatomic_dec(&stream->chan->nb_init_stream_left);
707 }
708
709 /* Update consumer data once the node is inserted. */
710 consumer_data.stream_count++;
711 consumer_data.need_update = 1;
712
713 rcu_read_unlock();
714 pthread_mutex_unlock(&stream->lock);
715 pthread_mutex_unlock(&stream->chan->timer_lock);
716 pthread_mutex_unlock(&stream->chan->lock);
717 pthread_mutex_unlock(&consumer_data.lock);
718
719 return ret;
720 }
721
722 void consumer_del_data_stream(struct lttng_consumer_stream *stream)
723 {
724 consumer_del_stream(stream, data_ht);
725 }
726
727 /*
728 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
729 * be acquired before calling this.
730 */
731 static int add_relayd(struct consumer_relayd_sock_pair *relayd)
732 {
733 int ret = 0;
734 struct lttng_ht_node_u64 *node;
735 struct lttng_ht_iter iter;
736
737 assert(relayd);
738
739 lttng_ht_lookup(consumer_data.relayd_ht,
740 &relayd->net_seq_idx, &iter);
741 node = lttng_ht_iter_get_node_u64(&iter);
742 if (node != NULL) {
743 goto end;
744 }
745 lttng_ht_add_unique_u64(consumer_data.relayd_ht, &relayd->node);
746
747 end:
748 return ret;
749 }
750
751 /*
752 * Allocate and return a consumer relayd socket.
753 */
754 struct consumer_relayd_sock_pair *consumer_allocate_relayd_sock_pair(
755 uint64_t net_seq_idx)
756 {
757 struct consumer_relayd_sock_pair *obj = NULL;
758
759 /* net sequence index of -1 is a failure */
760 if (net_seq_idx == (uint64_t) -1ULL) {
761 goto error;
762 }
763
764 obj = zmalloc(sizeof(struct consumer_relayd_sock_pair));
765 if (obj == NULL) {
766 PERROR("zmalloc relayd sock");
767 goto error;
768 }
769
770 obj->net_seq_idx = net_seq_idx;
771 obj->refcount = 0;
772 obj->destroy_flag = 0;
773 obj->control_sock.sock.fd = -1;
774 obj->data_sock.sock.fd = -1;
775 lttng_ht_node_init_u64(&obj->node, obj->net_seq_idx);
776 pthread_mutex_init(&obj->ctrl_sock_mutex, NULL);
777
778 error:
779 return obj;
780 }
781
782 /*
783 * Find a relayd socket pair in the global consumer data.
784 *
785 * Return the object if found else NULL.
786 * RCU read-side lock must be held across this call and while using the
787 * returned object.
788 */
789 struct consumer_relayd_sock_pair *consumer_find_relayd(uint64_t key)
790 {
791 struct lttng_ht_iter iter;
792 struct lttng_ht_node_u64 *node;
793 struct consumer_relayd_sock_pair *relayd = NULL;
794
795 /* Negative keys are lookup failures */
796 if (key == (uint64_t) -1ULL) {
797 goto error;
798 }
799
800 lttng_ht_lookup(consumer_data.relayd_ht, &key,
801 &iter);
802 node = lttng_ht_iter_get_node_u64(&iter);
803 if (node != NULL) {
804 relayd = caa_container_of(node, struct consumer_relayd_sock_pair, node);
805 }
806
807 error:
808 return relayd;
809 }
810
811 /*
812 * Handle stream for relayd transmission if the stream applies for network
813 * streaming where the net sequence index is set.
814 *
815 * Return destination file descriptor or negative value on error.
816 */
817 static int write_relayd_stream_header(struct lttng_consumer_stream *stream,
818 size_t data_size, unsigned long padding,
819 struct consumer_relayd_sock_pair *relayd)
820 {
821 int outfd = -1, ret;
822 struct lttcomm_relayd_data_hdr data_hdr;
823
824 /* Safety net */
825 assert(stream);
826 assert(relayd);
827
828 /* Reset data header */
829 memset(&data_hdr, 0, sizeof(data_hdr));
830
831 if (stream->metadata_flag) {
832 /* Caller MUST acquire the relayd control socket lock */
833 ret = relayd_send_metadata(&relayd->control_sock, data_size);
834 if (ret < 0) {
835 goto error;
836 }
837
838 /* Metadata are always sent on the control socket. */
839 outfd = relayd->control_sock.sock.fd;
840 } else {
841 /* Set header with stream information */
842 data_hdr.stream_id = htobe64(stream->relayd_stream_id);
843 data_hdr.data_size = htobe32(data_size);
844 data_hdr.padding_size = htobe32(padding);
845 /*
846 * Note that net_seq_num below is assigned with the *current* value of
847 * next_net_seq_num and only after that the next_net_seq_num will be
848 * increment. This is why when issuing a command on the relayd using
849 * this next value, 1 should always be substracted in order to compare
850 * the last seen sequence number on the relayd side to the last sent.
851 */
852 data_hdr.net_seq_num = htobe64(stream->next_net_seq_num);
853 /* Other fields are zeroed previously */
854
855 ret = relayd_send_data_hdr(&relayd->data_sock, &data_hdr,
856 sizeof(data_hdr));
857 if (ret < 0) {
858 goto error;
859 }
860
861 ++stream->next_net_seq_num;
862
863 /* Set to go on data socket */
864 outfd = relayd->data_sock.sock.fd;
865 }
866
867 error:
868 return outfd;
869 }
870
871 /*
872 * Allocate and return a new lttng_consumer_channel object using the given key
873 * to initialize the hash table node.
874 *
875 * On error, return NULL.
876 */
877 struct lttng_consumer_channel *consumer_allocate_channel(uint64_t key,
878 uint64_t session_id,
879 const char *pathname,
880 const char *name,
881 uid_t uid,
882 gid_t gid,
883 uint64_t relayd_id,
884 enum lttng_event_output output,
885 uint64_t tracefile_size,
886 uint64_t tracefile_count,
887 uint64_t session_id_per_pid)
888 {
889 struct lttng_consumer_channel *channel;
890
891 channel = zmalloc(sizeof(*channel));
892 if (channel == NULL) {
893 PERROR("malloc struct lttng_consumer_channel");
894 goto end;
895 }
896
897 channel->key = key;
898 channel->refcount = 0;
899 channel->session_id = session_id;
900 channel->session_id_per_pid = session_id_per_pid;
901 channel->uid = uid;
902 channel->gid = gid;
903 channel->relayd_id = relayd_id;
904 channel->output = output;
905 channel->tracefile_size = tracefile_size;
906 channel->tracefile_count = tracefile_count;
907 pthread_mutex_init(&channel->lock, NULL);
908 pthread_mutex_init(&channel->timer_lock, NULL);
909
910 strncpy(channel->pathname, pathname, sizeof(channel->pathname));
911 channel->pathname[sizeof(channel->pathname) - 1] = '\0';
912
913 strncpy(channel->name, name, sizeof(channel->name));
914 channel->name[sizeof(channel->name) - 1] = '\0';
915
916 lttng_ht_node_init_u64(&channel->node, channel->key);
917
918 channel->wait_fd = -1;
919
920 CDS_INIT_LIST_HEAD(&channel->streams.head);
921
922 DBG("Allocated channel (key %" PRIu64 ")", channel->key)
923
924 end:
925 return channel;
926 }
927
928 /*
929 * Add a channel to the global list protected by a mutex.
930 *
931 * On success 0 is returned else a negative value.
932 */
933 int consumer_add_channel(struct lttng_consumer_channel *channel,
934 struct lttng_consumer_local_data *ctx)
935 {
936 int ret = 0;
937 struct lttng_ht_node_u64 *node;
938 struct lttng_ht_iter iter;
939
940 pthread_mutex_lock(&consumer_data.lock);
941 pthread_mutex_lock(&channel->lock);
942 pthread_mutex_lock(&channel->timer_lock);
943 rcu_read_lock();
944
945 lttng_ht_lookup(consumer_data.channel_ht, &channel->key, &iter);
946 node = lttng_ht_iter_get_node_u64(&iter);
947 if (node != NULL) {
948 /* Channel already exist. Ignore the insertion */
949 ERR("Consumer add channel key %" PRIu64 " already exists!",
950 channel->key);
951 ret = -EEXIST;
952 goto end;
953 }
954
955 lttng_ht_add_unique_u64(consumer_data.channel_ht, &channel->node);
956
957 end:
958 rcu_read_unlock();
959 pthread_mutex_unlock(&channel->timer_lock);
960 pthread_mutex_unlock(&channel->lock);
961 pthread_mutex_unlock(&consumer_data.lock);
962
963 if (!ret && channel->wait_fd != -1 &&
964 channel->metadata_stream == NULL) {
965 notify_channel_pipe(ctx, channel, -1, CONSUMER_CHANNEL_ADD);
966 }
967 return ret;
968 }
969
970 /*
971 * Allocate the pollfd structure and the local view of the out fds to avoid
972 * doing a lookup in the linked list and concurrency issues when writing is
973 * needed. Called with consumer_data.lock held.
974 *
975 * Returns the number of fds in the structures.
976 */
977 static int update_poll_array(struct lttng_consumer_local_data *ctx,
978 struct pollfd **pollfd, struct lttng_consumer_stream **local_stream,
979 struct lttng_ht *ht)
980 {
981 int i = 0;
982 struct lttng_ht_iter iter;
983 struct lttng_consumer_stream *stream;
984
985 assert(ctx);
986 assert(ht);
987 assert(pollfd);
988 assert(local_stream);
989
990 DBG("Updating poll fd array");
991 rcu_read_lock();
992 cds_lfht_for_each_entry(ht->ht, &iter.iter, stream, node.node) {
993 /*
994 * Only active streams with an active end point can be added to the
995 * poll set and local stream storage of the thread.
996 *
997 * There is a potential race here for endpoint_status to be updated
998 * just after the check. However, this is OK since the stream(s) will
999 * be deleted once the thread is notified that the end point state has
1000 * changed where this function will be called back again.
1001 */
1002 if (stream->state != LTTNG_CONSUMER_ACTIVE_STREAM ||
1003 stream->endpoint_status == CONSUMER_ENDPOINT_INACTIVE) {
1004 continue;
1005 }
1006 /*
1007 * This clobbers way too much the debug output. Uncomment that if you
1008 * need it for debugging purposes.
1009 *
1010 * DBG("Active FD %d", stream->wait_fd);
1011 */
1012 (*pollfd)[i].fd = stream->wait_fd;
1013 (*pollfd)[i].events = POLLIN | POLLPRI;
1014 local_stream[i] = stream;
1015 i++;
1016 }
1017 rcu_read_unlock();
1018
1019 /*
1020 * Insert the consumer_data_pipe at the end of the array and don't
1021 * increment i so nb_fd is the number of real FD.
1022 */
1023 (*pollfd)[i].fd = lttng_pipe_get_readfd(ctx->consumer_data_pipe);
1024 (*pollfd)[i].events = POLLIN | POLLPRI;
1025 return i;
1026 }
1027
1028 /*
1029 * Poll on the should_quit pipe and the command socket return -1 on error and
1030 * should exit, 0 if data is available on the command socket
1031 */
1032 int lttng_consumer_poll_socket(struct pollfd *consumer_sockpoll)
1033 {
1034 int num_rdy;
1035
1036 restart:
1037 num_rdy = poll(consumer_sockpoll, 2, -1);
1038 if (num_rdy == -1) {
1039 /*
1040 * Restart interrupted system call.
1041 */
1042 if (errno == EINTR) {
1043 goto restart;
1044 }
1045 PERROR("Poll error");
1046 goto exit;
1047 }
1048 if (consumer_sockpoll[0].revents & (POLLIN | POLLPRI)) {
1049 DBG("consumer_should_quit wake up");
1050 goto exit;
1051 }
1052 return 0;
1053
1054 exit:
1055 return -1;
1056 }
1057
1058 /*
1059 * Set the error socket.
1060 */
1061 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data *ctx,
1062 int sock)
1063 {
1064 ctx->consumer_error_socket = sock;
1065 }
1066
1067 /*
1068 * Set the command socket path.
1069 */
1070 void lttng_consumer_set_command_sock_path(
1071 struct lttng_consumer_local_data *ctx, char *sock)
1072 {
1073 ctx->consumer_command_sock_path = sock;
1074 }
1075
1076 /*
1077 * Send return code to the session daemon.
1078 * If the socket is not defined, we return 0, it is not a fatal error
1079 */
1080 int lttng_consumer_send_error(struct lttng_consumer_local_data *ctx, int cmd)
1081 {
1082 if (ctx->consumer_error_socket > 0) {
1083 return lttcomm_send_unix_sock(ctx->consumer_error_socket, &cmd,
1084 sizeof(enum lttcomm_sessiond_command));
1085 }
1086
1087 return 0;
1088 }
1089
1090 /*
1091 * Close all the tracefiles and stream fds and MUST be called when all
1092 * instances are destroyed i.e. when all threads were joined and are ended.
1093 */
1094 void lttng_consumer_cleanup(void)
1095 {
1096 struct lttng_ht_iter iter;
1097 struct lttng_consumer_channel *channel;
1098
1099 rcu_read_lock();
1100
1101 cds_lfht_for_each_entry(consumer_data.channel_ht->ht, &iter.iter, channel,
1102 node.node) {
1103 consumer_del_channel(channel);
1104 }
1105
1106 rcu_read_unlock();
1107
1108 lttng_ht_destroy(consumer_data.channel_ht);
1109
1110 cleanup_relayd_ht();
1111
1112 lttng_ht_destroy(consumer_data.stream_per_chan_id_ht);
1113
1114 /*
1115 * This HT contains streams that are freed by either the metadata thread or
1116 * the data thread so we do *nothing* on the hash table and simply destroy
1117 * it.
1118 */
1119 lttng_ht_destroy(consumer_data.stream_list_ht);
1120 }
1121
1122 /*
1123 * Called from signal handler.
1124 */
1125 void lttng_consumer_should_exit(struct lttng_consumer_local_data *ctx)
1126 {
1127 int ret;
1128 consumer_quit = 1;
1129 do {
1130 ret = write(ctx->consumer_should_quit[1], "4", 1);
1131 } while (ret < 0 && errno == EINTR);
1132 if (ret < 0 || ret != 1) {
1133 PERROR("write consumer quit");
1134 }
1135
1136 DBG("Consumer flag that it should quit");
1137 }
1138
1139 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream *stream,
1140 off_t orig_offset)
1141 {
1142 int outfd = stream->out_fd;
1143
1144 /*
1145 * This does a blocking write-and-wait on any page that belongs to the
1146 * subbuffer prior to the one we just wrote.
1147 * Don't care about error values, as these are just hints and ways to
1148 * limit the amount of page cache used.
1149 */
1150 if (orig_offset < stream->max_sb_size) {
1151 return;
1152 }
1153 lttng_sync_file_range(outfd, orig_offset - stream->max_sb_size,
1154 stream->max_sb_size,
1155 SYNC_FILE_RANGE_WAIT_BEFORE
1156 | SYNC_FILE_RANGE_WRITE
1157 | SYNC_FILE_RANGE_WAIT_AFTER);
1158 /*
1159 * Give hints to the kernel about how we access the file:
1160 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1161 * we write it.
1162 *
1163 * We need to call fadvise again after the file grows because the
1164 * kernel does not seem to apply fadvise to non-existing parts of the
1165 * file.
1166 *
1167 * Call fadvise _after_ having waited for the page writeback to
1168 * complete because the dirty page writeback semantic is not well
1169 * defined. So it can be expected to lead to lower throughput in
1170 * streaming.
1171 */
1172 posix_fadvise(outfd, orig_offset - stream->max_sb_size,
1173 stream->max_sb_size, POSIX_FADV_DONTNEED);
1174 }
1175
1176 /*
1177 * Initialise the necessary environnement :
1178 * - create a new context
1179 * - create the poll_pipe
1180 * - create the should_quit pipe (for signal handler)
1181 * - create the thread pipe (for splice)
1182 *
1183 * Takes a function pointer as argument, this function is called when data is
1184 * available on a buffer. This function is responsible to do the
1185 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1186 * buffer configuration and then kernctl_put_next_subbuf at the end.
1187 *
1188 * Returns a pointer to the new context or NULL on error.
1189 */
1190 struct lttng_consumer_local_data *lttng_consumer_create(
1191 enum lttng_consumer_type type,
1192 ssize_t (*buffer_ready)(struct lttng_consumer_stream *stream,
1193 struct lttng_consumer_local_data *ctx),
1194 int (*recv_channel)(struct lttng_consumer_channel *channel),
1195 int (*recv_stream)(struct lttng_consumer_stream *stream),
1196 int (*update_stream)(uint64_t stream_key, uint32_t state))
1197 {
1198 int ret;
1199 struct lttng_consumer_local_data *ctx;
1200
1201 assert(consumer_data.type == LTTNG_CONSUMER_UNKNOWN ||
1202 consumer_data.type == type);
1203 consumer_data.type = type;
1204
1205 ctx = zmalloc(sizeof(struct lttng_consumer_local_data));
1206 if (ctx == NULL) {
1207 PERROR("allocating context");
1208 goto error;
1209 }
1210
1211 ctx->consumer_error_socket = -1;
1212 ctx->consumer_metadata_socket = -1;
1213 /* assign the callbacks */
1214 ctx->on_buffer_ready = buffer_ready;
1215 ctx->on_recv_channel = recv_channel;
1216 ctx->on_recv_stream = recv_stream;
1217 ctx->on_update_stream = update_stream;
1218
1219 ctx->consumer_data_pipe = lttng_pipe_open(0);
1220 if (!ctx->consumer_data_pipe) {
1221 goto error_poll_pipe;
1222 }
1223
1224 ret = pipe(ctx->consumer_should_quit);
1225 if (ret < 0) {
1226 PERROR("Error creating recv pipe");
1227 goto error_quit_pipe;
1228 }
1229
1230 ret = pipe(ctx->consumer_thread_pipe);
1231 if (ret < 0) {
1232 PERROR("Error creating thread pipe");
1233 goto error_thread_pipe;
1234 }
1235
1236 ret = pipe(ctx->consumer_channel_pipe);
1237 if (ret < 0) {
1238 PERROR("Error creating channel pipe");
1239 goto error_channel_pipe;
1240 }
1241
1242 ctx->consumer_metadata_pipe = lttng_pipe_open(0);
1243 if (!ctx->consumer_metadata_pipe) {
1244 goto error_metadata_pipe;
1245 }
1246
1247 ret = utils_create_pipe(ctx->consumer_splice_metadata_pipe);
1248 if (ret < 0) {
1249 goto error_splice_pipe;
1250 }
1251
1252 return ctx;
1253
1254 error_splice_pipe:
1255 lttng_pipe_destroy(ctx->consumer_metadata_pipe);
1256 error_metadata_pipe:
1257 utils_close_pipe(ctx->consumer_channel_pipe);
1258 error_channel_pipe:
1259 utils_close_pipe(ctx->consumer_thread_pipe);
1260 error_thread_pipe:
1261 utils_close_pipe(ctx->consumer_should_quit);
1262 error_quit_pipe:
1263 lttng_pipe_destroy(ctx->consumer_data_pipe);
1264 error_poll_pipe:
1265 free(ctx);
1266 error:
1267 return NULL;
1268 }
1269
1270 /*
1271 * Close all fds associated with the instance and free the context.
1272 */
1273 void lttng_consumer_destroy(struct lttng_consumer_local_data *ctx)
1274 {
1275 int ret;
1276
1277 DBG("Consumer destroying it. Closing everything.");
1278
1279 ret = close(ctx->consumer_error_socket);
1280 if (ret) {
1281 PERROR("close");
1282 }
1283 ret = close(ctx->consumer_metadata_socket);
1284 if (ret) {
1285 PERROR("close");
1286 }
1287 utils_close_pipe(ctx->consumer_thread_pipe);
1288 utils_close_pipe(ctx->consumer_channel_pipe);
1289 lttng_pipe_destroy(ctx->consumer_data_pipe);
1290 lttng_pipe_destroy(ctx->consumer_metadata_pipe);
1291 utils_close_pipe(ctx->consumer_should_quit);
1292 utils_close_pipe(ctx->consumer_splice_metadata_pipe);
1293
1294 unlink(ctx->consumer_command_sock_path);
1295 free(ctx);
1296 }
1297
1298 /*
1299 * Write the metadata stream id on the specified file descriptor.
1300 */
1301 static int write_relayd_metadata_id(int fd,
1302 struct lttng_consumer_stream *stream,
1303 struct consumer_relayd_sock_pair *relayd, unsigned long padding)
1304 {
1305 int ret;
1306 struct lttcomm_relayd_metadata_payload hdr;
1307
1308 hdr.stream_id = htobe64(stream->relayd_stream_id);
1309 hdr.padding_size = htobe32(padding);
1310 do {
1311 ret = write(fd, (void *) &hdr, sizeof(hdr));
1312 } while (ret < 0 && errno == EINTR);
1313 if (ret < 0 || ret != sizeof(hdr)) {
1314 /*
1315 * This error means that the fd's end is closed so ignore the perror
1316 * not to clubber the error output since this can happen in a normal
1317 * code path.
1318 */
1319 if (errno != EPIPE) {
1320 PERROR("write metadata stream id");
1321 }
1322 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno);
1323 /*
1324 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1325 * handle writting the missing part so report that as an error and
1326 * don't lie to the caller.
1327 */
1328 ret = -1;
1329 goto end;
1330 }
1331 DBG("Metadata stream id %" PRIu64 " with padding %lu written before data",
1332 stream->relayd_stream_id, padding);
1333
1334 end:
1335 return ret;
1336 }
1337
1338 /*
1339 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1340 * core function for writing trace buffers to either the local filesystem or
1341 * the network.
1342 *
1343 * It must be called with the stream lock held.
1344 *
1345 * Careful review MUST be put if any changes occur!
1346 *
1347 * Returns the number of bytes written
1348 */
1349 ssize_t lttng_consumer_on_read_subbuffer_mmap(
1350 struct lttng_consumer_local_data *ctx,
1351 struct lttng_consumer_stream *stream, unsigned long len,
1352 unsigned long padding)
1353 {
1354 unsigned long mmap_offset;
1355 void *mmap_base;
1356 ssize_t ret = 0, written = 0;
1357 off_t orig_offset = stream->out_fd_offset;
1358 /* Default is on the disk */
1359 int outfd = stream->out_fd;
1360 struct consumer_relayd_sock_pair *relayd = NULL;
1361 unsigned int relayd_hang_up = 0;
1362
1363 /* RCU lock for the relayd pointer */
1364 rcu_read_lock();
1365
1366 /* Flag that the current stream if set for network streaming. */
1367 if (stream->net_seq_idx != (uint64_t) -1ULL) {
1368 relayd = consumer_find_relayd(stream->net_seq_idx);
1369 if (relayd == NULL) {
1370 ret = -EPIPE;
1371 goto end;
1372 }
1373 }
1374
1375 /* get the offset inside the fd to mmap */
1376 switch (consumer_data.type) {
1377 case LTTNG_CONSUMER_KERNEL:
1378 mmap_base = stream->mmap_base;
1379 ret = kernctl_get_mmap_read_offset(stream->wait_fd, &mmap_offset);
1380 if (ret != 0) {
1381 PERROR("tracer ctl get_mmap_read_offset");
1382 written = -errno;
1383 goto end;
1384 }
1385 break;
1386 case LTTNG_CONSUMER32_UST:
1387 case LTTNG_CONSUMER64_UST:
1388 mmap_base = lttng_ustctl_get_mmap_base(stream);
1389 if (!mmap_base) {
1390 ERR("read mmap get mmap base for stream %s", stream->name);
1391 written = -EPERM;
1392 goto end;
1393 }
1394 ret = lttng_ustctl_get_mmap_read_offset(stream, &mmap_offset);
1395 if (ret != 0) {
1396 PERROR("tracer ctl get_mmap_read_offset");
1397 written = ret;
1398 goto end;
1399 }
1400 break;
1401 default:
1402 ERR("Unknown consumer_data type");
1403 assert(0);
1404 }
1405
1406 /* Handle stream on the relayd if the output is on the network */
1407 if (relayd) {
1408 unsigned long netlen = len;
1409
1410 /*
1411 * Lock the control socket for the complete duration of the function
1412 * since from this point on we will use the socket.
1413 */
1414 if (stream->metadata_flag) {
1415 /* Metadata requires the control socket. */
1416 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
1417 netlen += sizeof(struct lttcomm_relayd_metadata_payload);
1418 }
1419
1420 ret = write_relayd_stream_header(stream, netlen, padding, relayd);
1421 if (ret >= 0) {
1422 /* Use the returned socket. */
1423 outfd = ret;
1424
1425 /* Write metadata stream id before payload */
1426 if (stream->metadata_flag) {
1427 ret = write_relayd_metadata_id(outfd, stream, relayd, padding);
1428 if (ret < 0) {
1429 written = ret;
1430 /* Socket operation failed. We consider the relayd dead */
1431 if (ret == -EPIPE || ret == -EINVAL) {
1432 relayd_hang_up = 1;
1433 goto write_error;
1434 }
1435 goto end;
1436 }
1437 }
1438 } else {
1439 /* Socket operation failed. We consider the relayd dead */
1440 if (ret == -EPIPE || ret == -EINVAL) {
1441 relayd_hang_up = 1;
1442 goto write_error;
1443 }
1444 /* Else, use the default set before which is the filesystem. */
1445 }
1446 } else {
1447 /* No streaming, we have to set the len with the full padding */
1448 len += padding;
1449
1450 /*
1451 * Check if we need to change the tracefile before writing the packet.
1452 */
1453 if (stream->chan->tracefile_size > 0 &&
1454 (stream->tracefile_size_current + len) >
1455 stream->chan->tracefile_size) {
1456 ret = utils_rotate_stream_file(stream->chan->pathname,
1457 stream->name, stream->chan->tracefile_size,
1458 stream->chan->tracefile_count, stream->uid, stream->gid,
1459 stream->out_fd, &(stream->tracefile_count_current));
1460 if (ret < 0) {
1461 ERR("Rotating output file");
1462 goto end;
1463 }
1464 outfd = stream->out_fd = ret;
1465 /* Reset current size because we just perform a rotation. */
1466 stream->tracefile_size_current = 0;
1467 }
1468 stream->tracefile_size_current += len;
1469 }
1470
1471 while (len > 0) {
1472 do {
1473 ret = write(outfd, mmap_base + mmap_offset, len);
1474 } while (ret < 0 && errno == EINTR);
1475 DBG("Consumer mmap write() ret %zd (len %lu)", ret, len);
1476 if (ret < 0) {
1477 /*
1478 * This is possible if the fd is closed on the other side (outfd)
1479 * or any write problem. It can be verbose a bit for a normal
1480 * execution if for instance the relayd is stopped abruptly. This
1481 * can happen so set this to a DBG statement.
1482 */
1483 DBG("Error in file write mmap");
1484 if (written == 0) {
1485 written = -errno;
1486 }
1487 /* Socket operation failed. We consider the relayd dead */
1488 if (errno == EPIPE || errno == EINVAL) {
1489 relayd_hang_up = 1;
1490 goto write_error;
1491 }
1492 goto end;
1493 } else if (ret > len) {
1494 PERROR("Error in file write (ret %zd > len %lu)", ret, len);
1495 written += ret;
1496 goto end;
1497 } else {
1498 len -= ret;
1499 mmap_offset += ret;
1500 }
1501
1502 /* This call is useless on a socket so better save a syscall. */
1503 if (!relayd) {
1504 /* This won't block, but will start writeout asynchronously */
1505 lttng_sync_file_range(outfd, stream->out_fd_offset, ret,
1506 SYNC_FILE_RANGE_WRITE);
1507 stream->out_fd_offset += ret;
1508 }
1509 written += ret;
1510 }
1511 lttng_consumer_sync_trace_file(stream, orig_offset);
1512
1513 write_error:
1514 /*
1515 * This is a special case that the relayd has closed its socket. Let's
1516 * cleanup the relayd object and all associated streams.
1517 */
1518 if (relayd && relayd_hang_up) {
1519 cleanup_relayd(relayd, ctx);
1520 }
1521
1522 end:
1523 /* Unlock only if ctrl socket used */
1524 if (relayd && stream->metadata_flag) {
1525 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
1526 }
1527
1528 rcu_read_unlock();
1529 return written;
1530 }
1531
1532 /*
1533 * Splice the data from the ring buffer to the tracefile.
1534 *
1535 * It must be called with the stream lock held.
1536 *
1537 * Returns the number of bytes spliced.
1538 */
1539 ssize_t lttng_consumer_on_read_subbuffer_splice(
1540 struct lttng_consumer_local_data *ctx,
1541 struct lttng_consumer_stream *stream, unsigned long len,
1542 unsigned long padding)
1543 {
1544 ssize_t ret = 0, written = 0, ret_splice = 0;
1545 loff_t offset = 0;
1546 off_t orig_offset = stream->out_fd_offset;
1547 int fd = stream->wait_fd;
1548 /* Default is on the disk */
1549 int outfd = stream->out_fd;
1550 struct consumer_relayd_sock_pair *relayd = NULL;
1551 int *splice_pipe;
1552 unsigned int relayd_hang_up = 0;
1553
1554 switch (consumer_data.type) {
1555 case LTTNG_CONSUMER_KERNEL:
1556 break;
1557 case LTTNG_CONSUMER32_UST:
1558 case LTTNG_CONSUMER64_UST:
1559 /* Not supported for user space tracing */
1560 return -ENOSYS;
1561 default:
1562 ERR("Unknown consumer_data type");
1563 assert(0);
1564 }
1565
1566 /* RCU lock for the relayd pointer */
1567 rcu_read_lock();
1568
1569 /* Flag that the current stream if set for network streaming. */
1570 if (stream->net_seq_idx != (uint64_t) -1ULL) {
1571 relayd = consumer_find_relayd(stream->net_seq_idx);
1572 if (relayd == NULL) {
1573 ret = -EPIPE;
1574 goto end;
1575 }
1576 }
1577
1578 /*
1579 * Choose right pipe for splice. Metadata and trace data are handled by
1580 * different threads hence the use of two pipes in order not to race or
1581 * corrupt the written data.
1582 */
1583 if (stream->metadata_flag) {
1584 splice_pipe = ctx->consumer_splice_metadata_pipe;
1585 } else {
1586 splice_pipe = ctx->consumer_thread_pipe;
1587 }
1588
1589 /* Write metadata stream id before payload */
1590 if (relayd) {
1591 int total_len = len;
1592
1593 if (stream->metadata_flag) {
1594 /*
1595 * Lock the control socket for the complete duration of the function
1596 * since from this point on we will use the socket.
1597 */
1598 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
1599
1600 ret = write_relayd_metadata_id(splice_pipe[1], stream, relayd,
1601 padding);
1602 if (ret < 0) {
1603 written = ret;
1604 /* Socket operation failed. We consider the relayd dead */
1605 if (ret == -EBADF) {
1606 WARN("Remote relayd disconnected. Stopping");
1607 relayd_hang_up = 1;
1608 goto write_error;
1609 }
1610 goto end;
1611 }
1612
1613 total_len += sizeof(struct lttcomm_relayd_metadata_payload);
1614 }
1615
1616 ret = write_relayd_stream_header(stream, total_len, padding, relayd);
1617 if (ret >= 0) {
1618 /* Use the returned socket. */
1619 outfd = ret;
1620 } else {
1621 /* Socket operation failed. We consider the relayd dead */
1622 if (ret == -EBADF) {
1623 WARN("Remote relayd disconnected. Stopping");
1624 relayd_hang_up = 1;
1625 goto write_error;
1626 }
1627 goto end;
1628 }
1629 } else {
1630 /* No streaming, we have to set the len with the full padding */
1631 len += padding;
1632
1633 /*
1634 * Check if we need to change the tracefile before writing the packet.
1635 */
1636 if (stream->chan->tracefile_size > 0 &&
1637 (stream->tracefile_size_current + len) >
1638 stream->chan->tracefile_size) {
1639 ret = utils_rotate_stream_file(stream->chan->pathname,
1640 stream->name, stream->chan->tracefile_size,
1641 stream->chan->tracefile_count, stream->uid, stream->gid,
1642 stream->out_fd, &(stream->tracefile_count_current));
1643 if (ret < 0) {
1644 ERR("Rotating output file");
1645 goto end;
1646 }
1647 outfd = stream->out_fd = ret;
1648 /* Reset current size because we just perform a rotation. */
1649 stream->tracefile_size_current = 0;
1650 }
1651 stream->tracefile_size_current += len;
1652 }
1653
1654 while (len > 0) {
1655 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1656 (unsigned long)offset, len, fd, splice_pipe[1]);
1657 ret_splice = splice(fd, &offset, splice_pipe[1], NULL, len,
1658 SPLICE_F_MOVE | SPLICE_F_MORE);
1659 DBG("splice chan to pipe, ret %zd", ret_splice);
1660 if (ret_splice < 0) {
1661 PERROR("Error in relay splice");
1662 if (written == 0) {
1663 written = ret_splice;
1664 }
1665 ret = errno;
1666 goto splice_error;
1667 }
1668
1669 /* Handle stream on the relayd if the output is on the network */
1670 if (relayd) {
1671 if (stream->metadata_flag) {
1672 size_t metadata_payload_size =
1673 sizeof(struct lttcomm_relayd_metadata_payload);
1674
1675 /* Update counter to fit the spliced data */
1676 ret_splice += metadata_payload_size;
1677 len += metadata_payload_size;
1678 /*
1679 * We do this so the return value can match the len passed as
1680 * argument to this function.
1681 */
1682 written -= metadata_payload_size;
1683 }
1684 }
1685
1686 /* Splice data out */
1687 ret_splice = splice(splice_pipe[0], NULL, outfd, NULL,
1688 ret_splice, SPLICE_F_MOVE | SPLICE_F_MORE);
1689 DBG("Consumer splice pipe to file, ret %zd", ret_splice);
1690 if (ret_splice < 0) {
1691 PERROR("Error in file splice");
1692 if (written == 0) {
1693 written = ret_splice;
1694 }
1695 /* Socket operation failed. We consider the relayd dead */
1696 if (errno == EBADF || errno == EPIPE) {
1697 WARN("Remote relayd disconnected. Stopping");
1698 relayd_hang_up = 1;
1699 goto write_error;
1700 }
1701 ret = errno;
1702 goto splice_error;
1703 } else if (ret_splice > len) {
1704 errno = EINVAL;
1705 PERROR("Wrote more data than requested %zd (len: %lu)",
1706 ret_splice, len);
1707 written += ret_splice;
1708 ret = errno;
1709 goto splice_error;
1710 }
1711 len -= ret_splice;
1712
1713 /* This call is useless on a socket so better save a syscall. */
1714 if (!relayd) {
1715 /* This won't block, but will start writeout asynchronously */
1716 lttng_sync_file_range(outfd, stream->out_fd_offset, ret_splice,
1717 SYNC_FILE_RANGE_WRITE);
1718 stream->out_fd_offset += ret_splice;
1719 }
1720 written += ret_splice;
1721 }
1722 lttng_consumer_sync_trace_file(stream, orig_offset);
1723
1724 ret = ret_splice;
1725
1726 goto end;
1727
1728 write_error:
1729 /*
1730 * This is a special case that the relayd has closed its socket. Let's
1731 * cleanup the relayd object and all associated streams.
1732 */
1733 if (relayd && relayd_hang_up) {
1734 cleanup_relayd(relayd, ctx);
1735 /* Skip splice error so the consumer does not fail */
1736 goto end;
1737 }
1738
1739 splice_error:
1740 /* send the appropriate error description to sessiond */
1741 switch (ret) {
1742 case EINVAL:
1743 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_EINVAL);
1744 break;
1745 case ENOMEM:
1746 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_ENOMEM);
1747 break;
1748 case ESPIPE:
1749 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_SPLICE_ESPIPE);
1750 break;
1751 }
1752
1753 end:
1754 if (relayd && stream->metadata_flag) {
1755 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
1756 }
1757
1758 rcu_read_unlock();
1759 return written;
1760 }
1761
1762 /*
1763 * Take a snapshot for a specific fd
1764 *
1765 * Returns 0 on success, < 0 on error
1766 */
1767 int lttng_consumer_take_snapshot(struct lttng_consumer_stream *stream)
1768 {
1769 switch (consumer_data.type) {
1770 case LTTNG_CONSUMER_KERNEL:
1771 return lttng_kconsumer_take_snapshot(stream);
1772 case LTTNG_CONSUMER32_UST:
1773 case LTTNG_CONSUMER64_UST:
1774 return lttng_ustconsumer_take_snapshot(stream);
1775 default:
1776 ERR("Unknown consumer_data type");
1777 assert(0);
1778 return -ENOSYS;
1779 }
1780 }
1781
1782 /*
1783 * Get the produced position
1784 *
1785 * Returns 0 on success, < 0 on error
1786 */
1787 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream *stream,
1788 unsigned long *pos)
1789 {
1790 switch (consumer_data.type) {
1791 case LTTNG_CONSUMER_KERNEL:
1792 return lttng_kconsumer_get_produced_snapshot(stream, pos);
1793 case LTTNG_CONSUMER32_UST:
1794 case LTTNG_CONSUMER64_UST:
1795 return lttng_ustconsumer_get_produced_snapshot(stream, pos);
1796 default:
1797 ERR("Unknown consumer_data type");
1798 assert(0);
1799 return -ENOSYS;
1800 }
1801 }
1802
1803 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data *ctx,
1804 int sock, struct pollfd *consumer_sockpoll)
1805 {
1806 switch (consumer_data.type) {
1807 case LTTNG_CONSUMER_KERNEL:
1808 return lttng_kconsumer_recv_cmd(ctx, sock, consumer_sockpoll);
1809 case LTTNG_CONSUMER32_UST:
1810 case LTTNG_CONSUMER64_UST:
1811 return lttng_ustconsumer_recv_cmd(ctx, sock, consumer_sockpoll);
1812 default:
1813 ERR("Unknown consumer_data type");
1814 assert(0);
1815 return -ENOSYS;
1816 }
1817 }
1818
1819 /*
1820 * Iterate over all streams of the hashtable and free them properly.
1821 *
1822 * WARNING: *MUST* be used with data stream only.
1823 */
1824 static void destroy_data_stream_ht(struct lttng_ht *ht)
1825 {
1826 struct lttng_ht_iter iter;
1827 struct lttng_consumer_stream *stream;
1828
1829 if (ht == NULL) {
1830 return;
1831 }
1832
1833 rcu_read_lock();
1834 cds_lfht_for_each_entry(ht->ht, &iter.iter, stream, node.node) {
1835 /*
1836 * Ignore return value since we are currently cleaning up so any error
1837 * can't be handled.
1838 */
1839 (void) consumer_del_stream(stream, ht);
1840 }
1841 rcu_read_unlock();
1842
1843 lttng_ht_destroy(ht);
1844 }
1845
1846 /*
1847 * Iterate over all streams of the hashtable and free them properly.
1848 *
1849 * XXX: Should not be only for metadata stream or else use an other name.
1850 */
1851 static void destroy_stream_ht(struct lttng_ht *ht)
1852 {
1853 struct lttng_ht_iter iter;
1854 struct lttng_consumer_stream *stream;
1855
1856 if (ht == NULL) {
1857 return;
1858 }
1859
1860 rcu_read_lock();
1861 cds_lfht_for_each_entry(ht->ht, &iter.iter, stream, node.node) {
1862 /*
1863 * Ignore return value since we are currently cleaning up so any error
1864 * can't be handled.
1865 */
1866 (void) consumer_del_metadata_stream(stream, ht);
1867 }
1868 rcu_read_unlock();
1869
1870 lttng_ht_destroy(ht);
1871 }
1872
1873 void lttng_consumer_close_metadata(void)
1874 {
1875 switch (consumer_data.type) {
1876 case LTTNG_CONSUMER_KERNEL:
1877 /*
1878 * The Kernel consumer has a different metadata scheme so we don't
1879 * close anything because the stream will be closed by the session
1880 * daemon.
1881 */
1882 break;
1883 case LTTNG_CONSUMER32_UST:
1884 case LTTNG_CONSUMER64_UST:
1885 /*
1886 * Close all metadata streams. The metadata hash table is passed and
1887 * this call iterates over it by closing all wakeup fd. This is safe
1888 * because at this point we are sure that the metadata producer is
1889 * either dead or blocked.
1890 */
1891 lttng_ustconsumer_close_metadata(metadata_ht);
1892 break;
1893 default:
1894 ERR("Unknown consumer_data type");
1895 assert(0);
1896 }
1897 }
1898
1899 /*
1900 * Clean up a metadata stream and free its memory.
1901 */
1902 void consumer_del_metadata_stream(struct lttng_consumer_stream *stream,
1903 struct lttng_ht *ht)
1904 {
1905 int ret;
1906 struct lttng_ht_iter iter;
1907 struct lttng_consumer_channel *free_chan = NULL;
1908 struct consumer_relayd_sock_pair *relayd;
1909
1910 assert(stream);
1911 /*
1912 * This call should NEVER receive regular stream. It must always be
1913 * metadata stream and this is crucial for data structure synchronization.
1914 */
1915 assert(stream->metadata_flag);
1916
1917 DBG3("Consumer delete metadata stream %d", stream->wait_fd);
1918
1919 if (ht == NULL) {
1920 /* Means the stream was allocated but not successfully added */
1921 goto free_stream_rcu;
1922 }
1923
1924 pthread_mutex_lock(&consumer_data.lock);
1925 pthread_mutex_lock(&stream->chan->lock);
1926 pthread_mutex_lock(&stream->lock);
1927
1928 switch (consumer_data.type) {
1929 case LTTNG_CONSUMER_KERNEL:
1930 if (stream->mmap_base != NULL) {
1931 ret = munmap(stream->mmap_base, stream->mmap_len);
1932 if (ret != 0) {
1933 PERROR("munmap metadata stream");
1934 }
1935 }
1936
1937 if (stream->wait_fd >= 0) {
1938 ret = close(stream->wait_fd);
1939 if (ret < 0) {
1940 PERROR("close kernel metadata wait_fd");
1941 }
1942 }
1943 break;
1944 case LTTNG_CONSUMER32_UST:
1945 case LTTNG_CONSUMER64_UST:
1946 lttng_ustconsumer_del_stream(stream);
1947 break;
1948 default:
1949 ERR("Unknown consumer_data type");
1950 assert(0);
1951 goto end;
1952 }
1953
1954 rcu_read_lock();
1955 iter.iter.node = &stream->node.node;
1956 ret = lttng_ht_del(ht, &iter);
1957 assert(!ret);
1958
1959 iter.iter.node = &stream->node_channel_id.node;
1960 ret = lttng_ht_del(consumer_data.stream_per_chan_id_ht, &iter);
1961 assert(!ret);
1962
1963 iter.iter.node = &stream->node_session_id.node;
1964 ret = lttng_ht_del(consumer_data.stream_list_ht, &iter);
1965 assert(!ret);
1966 rcu_read_unlock();
1967
1968 if (stream->out_fd >= 0) {
1969 ret = close(stream->out_fd);
1970 if (ret) {
1971 PERROR("close");
1972 }
1973 }
1974
1975 /* Check and cleanup relayd */
1976 rcu_read_lock();
1977 relayd = consumer_find_relayd(stream->net_seq_idx);
1978 if (relayd != NULL) {
1979 uatomic_dec(&relayd->refcount);
1980 assert(uatomic_read(&relayd->refcount) >= 0);
1981
1982 /* Closing streams requires to lock the control socket. */
1983 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
1984 ret = relayd_send_close_stream(&relayd->control_sock,
1985 stream->relayd_stream_id, stream->next_net_seq_num - 1);
1986 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
1987 if (ret < 0) {
1988 DBG("Unable to close stream on the relayd. Continuing");
1989 /*
1990 * Continue here. There is nothing we can do for the relayd.
1991 * Chances are that the relayd has closed the socket so we just
1992 * continue cleaning up.
1993 */
1994 }
1995
1996 /* Both conditions are met, we destroy the relayd. */
1997 if (uatomic_read(&relayd->refcount) == 0 &&
1998 uatomic_read(&relayd->destroy_flag)) {
1999 destroy_relayd(relayd);
2000 }
2001 }
2002 rcu_read_unlock();
2003
2004 /* Atomically decrement channel refcount since other threads can use it. */
2005 if (!uatomic_sub_return(&stream->chan->refcount, 1)
2006 && !uatomic_read(&stream->chan->nb_init_stream_left)) {
2007 /* Go for channel deletion! */
2008 free_chan = stream->chan;
2009 }
2010
2011 end:
2012 /*
2013 * Nullify the stream reference so it is not used after deletion. The
2014 * channel lock MUST be acquired before being able to check for
2015 * a NULL pointer value.
2016 */
2017 stream->chan->metadata_stream = NULL;
2018
2019 pthread_mutex_unlock(&stream->lock);
2020 pthread_mutex_unlock(&stream->chan->lock);
2021 pthread_mutex_unlock(&consumer_data.lock);
2022
2023 if (free_chan) {
2024 consumer_del_channel(free_chan);
2025 }
2026
2027 free_stream_rcu:
2028 call_rcu(&stream->node.head, free_stream_rcu);
2029 }
2030
2031 /*
2032 * Action done with the metadata stream when adding it to the consumer internal
2033 * data structures to handle it.
2034 */
2035 int consumer_add_metadata_stream(struct lttng_consumer_stream *stream)
2036 {
2037 struct lttng_ht *ht = metadata_ht;
2038 int ret = 0;
2039 struct consumer_relayd_sock_pair *relayd;
2040 struct lttng_ht_iter iter;
2041 struct lttng_ht_node_u64 *node;
2042
2043 assert(stream);
2044 assert(ht);
2045
2046 DBG3("Adding metadata stream %" PRIu64 " to hash table", stream->key);
2047
2048 pthread_mutex_lock(&consumer_data.lock);
2049 pthread_mutex_lock(&stream->chan->lock);
2050 pthread_mutex_lock(&stream->chan->timer_lock);
2051 pthread_mutex_lock(&stream->lock);
2052
2053 /*
2054 * From here, refcounts are updated so be _careful_ when returning an error
2055 * after this point.
2056 */
2057
2058 rcu_read_lock();
2059
2060 /*
2061 * Lookup the stream just to make sure it does not exist in our internal
2062 * state. This should NEVER happen.
2063 */
2064 lttng_ht_lookup(ht, &stream->key, &iter);
2065 node = lttng_ht_iter_get_node_u64(&iter);
2066 assert(!node);
2067
2068 /* Find relayd and, if one is found, increment refcount. */
2069 relayd = consumer_find_relayd(stream->net_seq_idx);
2070 if (relayd != NULL) {
2071 uatomic_inc(&relayd->refcount);
2072 }
2073
2074 /*
2075 * When nb_init_stream_left reaches 0, we don't need to trigger any action
2076 * in terms of destroying the associated channel, because the action that
2077 * causes the count to become 0 also causes a stream to be added. The
2078 * channel deletion will thus be triggered by the following removal of this
2079 * stream.
2080 */
2081 if (uatomic_read(&stream->chan->nb_init_stream_left) > 0) {
2082 /* Increment refcount before decrementing nb_init_stream_left */
2083 cmm_smp_wmb();
2084 uatomic_dec(&stream->chan->nb_init_stream_left);
2085 }
2086
2087 lttng_ht_add_unique_u64(ht, &stream->node);
2088
2089 lttng_ht_add_unique_u64(consumer_data.stream_per_chan_id_ht,
2090 &stream->node_channel_id);
2091
2092 /*
2093 * Add stream to the stream_list_ht of the consumer data. No need to steal
2094 * the key since the HT does not use it and we allow to add redundant keys
2095 * into this table.
2096 */
2097 lttng_ht_add_u64(consumer_data.stream_list_ht, &stream->node_session_id);
2098
2099 rcu_read_unlock();
2100
2101 pthread_mutex_unlock(&stream->lock);
2102 pthread_mutex_unlock(&stream->chan->lock);
2103 pthread_mutex_unlock(&stream->chan->timer_lock);
2104 pthread_mutex_unlock(&consumer_data.lock);
2105 return ret;
2106 }
2107
2108 /*
2109 * Delete data stream that are flagged for deletion (endpoint_status).
2110 */
2111 static void validate_endpoint_status_data_stream(void)
2112 {
2113 struct lttng_ht_iter iter;
2114 struct lttng_consumer_stream *stream;
2115
2116 DBG("Consumer delete flagged data stream");
2117
2118 rcu_read_lock();
2119 cds_lfht_for_each_entry(data_ht->ht, &iter.iter, stream, node.node) {
2120 /* Validate delete flag of the stream */
2121 if (stream->endpoint_status == CONSUMER_ENDPOINT_ACTIVE) {
2122 continue;
2123 }
2124 /* Delete it right now */
2125 consumer_del_stream(stream, data_ht);
2126 }
2127 rcu_read_unlock();
2128 }
2129
2130 /*
2131 * Delete metadata stream that are flagged for deletion (endpoint_status).
2132 */
2133 static void validate_endpoint_status_metadata_stream(
2134 struct lttng_poll_event *pollset)
2135 {
2136 struct lttng_ht_iter iter;
2137 struct lttng_consumer_stream *stream;
2138
2139 DBG("Consumer delete flagged metadata stream");
2140
2141 assert(pollset);
2142
2143 rcu_read_lock();
2144 cds_lfht_for_each_entry(metadata_ht->ht, &iter.iter, stream, node.node) {
2145 /* Validate delete flag of the stream */
2146 if (stream->endpoint_status == CONSUMER_ENDPOINT_ACTIVE) {
2147 continue;
2148 }
2149 /*
2150 * Remove from pollset so the metadata thread can continue without
2151 * blocking on a deleted stream.
2152 */
2153 lttng_poll_del(pollset, stream->wait_fd);
2154
2155 /* Delete it right now */
2156 consumer_del_metadata_stream(stream, metadata_ht);
2157 }
2158 rcu_read_unlock();
2159 }
2160
2161 /*
2162 * Thread polls on metadata file descriptor and write them on disk or on the
2163 * network.
2164 */
2165 void *consumer_thread_metadata_poll(void *data)
2166 {
2167 int ret, i, pollfd;
2168 uint32_t revents, nb_fd;
2169 struct lttng_consumer_stream *stream = NULL;
2170 struct lttng_ht_iter iter;
2171 struct lttng_ht_node_u64 *node;
2172 struct lttng_poll_event events;
2173 struct lttng_consumer_local_data *ctx = data;
2174 ssize_t len;
2175
2176 rcu_register_thread();
2177
2178 metadata_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
2179 if (!metadata_ht) {
2180 /* ENOMEM at this point. Better to bail out. */
2181 goto end_ht;
2182 }
2183
2184 DBG("Thread metadata poll started");
2185
2186 /* Size is set to 1 for the consumer_metadata pipe */
2187 ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC);
2188 if (ret < 0) {
2189 ERR("Poll set creation failed");
2190 goto end_poll;
2191 }
2192
2193 ret = lttng_poll_add(&events,
2194 lttng_pipe_get_readfd(ctx->consumer_metadata_pipe), LPOLLIN);
2195 if (ret < 0) {
2196 goto end;
2197 }
2198
2199 /* Main loop */
2200 DBG("Metadata main loop started");
2201
2202 while (1) {
2203 /* Only the metadata pipe is set */
2204 if (LTTNG_POLL_GETNB(&events) == 0 && consumer_quit == 1) {
2205 goto end;
2206 }
2207
2208 restart:
2209 DBG("Metadata poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events));
2210 ret = lttng_poll_wait(&events, -1);
2211 DBG("Metadata event catched in thread");
2212 if (ret < 0) {
2213 if (errno == EINTR) {
2214 ERR("Poll EINTR catched");
2215 goto restart;
2216 }
2217 goto error;
2218 }
2219
2220 nb_fd = ret;
2221
2222 /* From here, the event is a metadata wait fd */
2223 for (i = 0; i < nb_fd; i++) {
2224 revents = LTTNG_POLL_GETEV(&events, i);
2225 pollfd = LTTNG_POLL_GETFD(&events, i);
2226
2227 /* Just don't waste time if no returned events for the fd */
2228 if (!revents) {
2229 continue;
2230 }
2231
2232 if (pollfd == lttng_pipe_get_readfd(ctx->consumer_metadata_pipe)) {
2233 if (revents & (LPOLLERR | LPOLLHUP )) {
2234 DBG("Metadata thread pipe hung up");
2235 /*
2236 * Remove the pipe from the poll set and continue the loop
2237 * since their might be data to consume.
2238 */
2239 lttng_poll_del(&events,
2240 lttng_pipe_get_readfd(ctx->consumer_metadata_pipe));
2241 lttng_pipe_read_close(ctx->consumer_metadata_pipe);
2242 continue;
2243 } else if (revents & LPOLLIN) {
2244 ssize_t pipe_len;
2245
2246 pipe_len = lttng_pipe_read(ctx->consumer_metadata_pipe,
2247 &stream, sizeof(stream));
2248 if (pipe_len < 0) {
2249 ERR("read metadata stream, ret: %zd", pipe_len);
2250 /*
2251 * Continue here to handle the rest of the streams.
2252 */
2253 continue;
2254 }
2255
2256 /* A NULL stream means that the state has changed. */
2257 if (stream == NULL) {
2258 /* Check for deleted streams. */
2259 validate_endpoint_status_metadata_stream(&events);
2260 goto restart;
2261 }
2262
2263 DBG("Adding metadata stream %d to poll set",
2264 stream->wait_fd);
2265
2266 /* Add metadata stream to the global poll events list */
2267 lttng_poll_add(&events, stream->wait_fd,
2268 LPOLLIN | LPOLLPRI);
2269 }
2270
2271 /* Handle other stream */
2272 continue;
2273 }
2274
2275 rcu_read_lock();
2276 {
2277 uint64_t tmp_id = (uint64_t) pollfd;
2278
2279 lttng_ht_lookup(metadata_ht, &tmp_id, &iter);
2280 }
2281 node = lttng_ht_iter_get_node_u64(&iter);
2282 assert(node);
2283
2284 stream = caa_container_of(node, struct lttng_consumer_stream,
2285 node);
2286
2287 /* Check for error event */
2288 if (revents & (LPOLLERR | LPOLLHUP)) {
2289 DBG("Metadata fd %d is hup|err.", pollfd);
2290 if (!stream->hangup_flush_done
2291 && (consumer_data.type == LTTNG_CONSUMER32_UST
2292 || consumer_data.type == LTTNG_CONSUMER64_UST)) {
2293 DBG("Attempting to flush and consume the UST buffers");
2294 lttng_ustconsumer_on_stream_hangup(stream);
2295
2296 /* We just flushed the stream now read it. */
2297 do {
2298 len = ctx->on_buffer_ready(stream, ctx);
2299 /*
2300 * We don't check the return value here since if we get
2301 * a negative len, it means an error occured thus we
2302 * simply remove it from the poll set and free the
2303 * stream.
2304 */
2305 } while (len > 0);
2306 }
2307
2308 lttng_poll_del(&events, stream->wait_fd);
2309 /*
2310 * This call update the channel states, closes file descriptors
2311 * and securely free the stream.
2312 */
2313 consumer_del_metadata_stream(stream, metadata_ht);
2314 } else if (revents & (LPOLLIN | LPOLLPRI)) {
2315 /* Get the data out of the metadata file descriptor */
2316 DBG("Metadata available on fd %d", pollfd);
2317 assert(stream->wait_fd == pollfd);
2318
2319 len = ctx->on_buffer_ready(stream, ctx);
2320 /* It's ok to have an unavailable sub-buffer */
2321 if (len < 0 && len != -EAGAIN && len != -ENODATA) {
2322 /* Clean up stream from consumer and free it. */
2323 lttng_poll_del(&events, stream->wait_fd);
2324 consumer_del_metadata_stream(stream, metadata_ht);
2325 } else if (len > 0) {
2326 stream->data_read = 1;
2327 }
2328 }
2329
2330 /* Release RCU lock for the stream looked up */
2331 rcu_read_unlock();
2332 }
2333 }
2334
2335 error:
2336 end:
2337 DBG("Metadata poll thread exiting");
2338
2339 lttng_poll_clean(&events);
2340 end_poll:
2341 destroy_stream_ht(metadata_ht);
2342 end_ht:
2343 rcu_unregister_thread();
2344 return NULL;
2345 }
2346
2347 /*
2348 * This thread polls the fds in the set to consume the data and write
2349 * it to tracefile if necessary.
2350 */
2351 void *consumer_thread_data_poll(void *data)
2352 {
2353 int num_rdy, num_hup, high_prio, ret, i;
2354 struct pollfd *pollfd = NULL;
2355 /* local view of the streams */
2356 struct lttng_consumer_stream **local_stream = NULL, *new_stream = NULL;
2357 /* local view of consumer_data.fds_count */
2358 int nb_fd = 0;
2359 struct lttng_consumer_local_data *ctx = data;
2360 ssize_t len;
2361
2362 rcu_register_thread();
2363
2364 data_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
2365 if (data_ht == NULL) {
2366 /* ENOMEM at this point. Better to bail out. */
2367 goto end;
2368 }
2369
2370 local_stream = zmalloc(sizeof(struct lttng_consumer_stream *));
2371 if (local_stream == NULL) {
2372 PERROR("local_stream malloc");
2373 goto end;
2374 }
2375
2376 while (1) {
2377 high_prio = 0;
2378 num_hup = 0;
2379
2380 /*
2381 * the fds set has been updated, we need to update our
2382 * local array as well
2383 */
2384 pthread_mutex_lock(&consumer_data.lock);
2385 if (consumer_data.need_update) {
2386 free(pollfd);
2387 pollfd = NULL;
2388
2389 free(local_stream);
2390 local_stream = NULL;
2391
2392 /* allocate for all fds + 1 for the consumer_data_pipe */
2393 pollfd = zmalloc((consumer_data.stream_count + 1) * sizeof(struct pollfd));
2394 if (pollfd == NULL) {
2395 PERROR("pollfd malloc");
2396 pthread_mutex_unlock(&consumer_data.lock);
2397 goto end;
2398 }
2399
2400 /* allocate for all fds + 1 for the consumer_data_pipe */
2401 local_stream = zmalloc((consumer_data.stream_count + 1) *
2402 sizeof(struct lttng_consumer_stream *));
2403 if (local_stream == NULL) {
2404 PERROR("local_stream malloc");
2405 pthread_mutex_unlock(&consumer_data.lock);
2406 goto end;
2407 }
2408 ret = update_poll_array(ctx, &pollfd, local_stream,
2409 data_ht);
2410 if (ret < 0) {
2411 ERR("Error in allocating pollfd or local_outfds");
2412 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_POLL_ERROR);
2413 pthread_mutex_unlock(&consumer_data.lock);
2414 goto end;
2415 }
2416 nb_fd = ret;
2417 consumer_data.need_update = 0;
2418 }
2419 pthread_mutex_unlock(&consumer_data.lock);
2420
2421 /* No FDs and consumer_quit, consumer_cleanup the thread */
2422 if (nb_fd == 0 && consumer_quit == 1) {
2423 goto end;
2424 }
2425 /* poll on the array of fds */
2426 restart:
2427 DBG("polling on %d fd", nb_fd + 1);
2428 num_rdy = poll(pollfd, nb_fd + 1, -1);
2429 DBG("poll num_rdy : %d", num_rdy);
2430 if (num_rdy == -1) {
2431 /*
2432 * Restart interrupted system call.
2433 */
2434 if (errno == EINTR) {
2435 goto restart;
2436 }
2437 PERROR("Poll error");
2438 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_POLL_ERROR);
2439 goto end;
2440 } else if (num_rdy == 0) {
2441 DBG("Polling thread timed out");
2442 goto end;
2443 }
2444
2445 /*
2446 * If the consumer_data_pipe triggered poll go directly to the
2447 * beginning of the loop to update the array. We want to prioritize
2448 * array update over low-priority reads.
2449 */
2450 if (pollfd[nb_fd].revents & (POLLIN | POLLPRI)) {
2451 ssize_t pipe_readlen;
2452
2453 DBG("consumer_data_pipe wake up");
2454 pipe_readlen = lttng_pipe_read(ctx->consumer_data_pipe,
2455 &new_stream, sizeof(new_stream));
2456 if (pipe_readlen < 0) {
2457 ERR("Consumer data pipe ret %zd", pipe_readlen);
2458 /* Continue so we can at least handle the current stream(s). */
2459 continue;
2460 }
2461
2462 /*
2463 * If the stream is NULL, just ignore it. It's also possible that
2464 * the sessiond poll thread changed the consumer_quit state and is
2465 * waking us up to test it.
2466 */
2467 if (new_stream == NULL) {
2468 validate_endpoint_status_data_stream();
2469 continue;
2470 }
2471
2472 /* Continue to update the local streams and handle prio ones */
2473 continue;
2474 }
2475
2476 /* Take care of high priority channels first. */
2477 for (i = 0; i < nb_fd; i++) {
2478 if (local_stream[i] == NULL) {
2479 continue;
2480 }
2481 if (pollfd[i].revents & POLLPRI) {
2482 DBG("Urgent read on fd %d", pollfd[i].fd);
2483 high_prio = 1;
2484 len = ctx->on_buffer_ready(local_stream[i], ctx);
2485 /* it's ok to have an unavailable sub-buffer */
2486 if (len < 0 && len != -EAGAIN && len != -ENODATA) {
2487 /* Clean the stream and free it. */
2488 consumer_del_stream(local_stream[i], data_ht);
2489 local_stream[i] = NULL;
2490 } else if (len > 0) {
2491 local_stream[i]->data_read = 1;
2492 }
2493 }
2494 }
2495
2496 /*
2497 * If we read high prio channel in this loop, try again
2498 * for more high prio data.
2499 */
2500 if (high_prio) {
2501 continue;
2502 }
2503
2504 /* Take care of low priority channels. */
2505 for (i = 0; i < nb_fd; i++) {
2506 if (local_stream[i] == NULL) {
2507 continue;
2508 }
2509 if ((pollfd[i].revents & POLLIN) ||
2510 local_stream[i]->hangup_flush_done) {
2511 DBG("Normal read on fd %d", pollfd[i].fd);
2512 len = ctx->on_buffer_ready(local_stream[i], ctx);
2513 /* it's ok to have an unavailable sub-buffer */
2514 if (len < 0 && len != -EAGAIN && len != -ENODATA) {
2515 /* Clean the stream and free it. */
2516 consumer_del_stream(local_stream[i], data_ht);
2517 local_stream[i] = NULL;
2518 } else if (len > 0) {
2519 local_stream[i]->data_read = 1;
2520 }
2521 }
2522 }
2523
2524 /* Handle hangup and errors */
2525 for (i = 0; i < nb_fd; i++) {
2526 if (local_stream[i] == NULL) {
2527 continue;
2528 }
2529 if (!local_stream[i]->hangup_flush_done
2530 && (pollfd[i].revents & (POLLHUP | POLLERR | POLLNVAL))
2531 && (consumer_data.type == LTTNG_CONSUMER32_UST
2532 || consumer_data.type == LTTNG_CONSUMER64_UST)) {
2533 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2534 pollfd[i].fd);
2535 lttng_ustconsumer_on_stream_hangup(local_stream[i]);
2536 /* Attempt read again, for the data we just flushed. */
2537 local_stream[i]->data_read = 1;
2538 }
2539 /*
2540 * If the poll flag is HUP/ERR/NVAL and we have
2541 * read no data in this pass, we can remove the
2542 * stream from its hash table.
2543 */
2544 if ((pollfd[i].revents & POLLHUP)) {
2545 DBG("Polling fd %d tells it has hung up.", pollfd[i].fd);
2546 if (!local_stream[i]->data_read) {
2547 consumer_del_stream(local_stream[i], data_ht);
2548 local_stream[i] = NULL;
2549 num_hup++;
2550 }
2551 } else if (pollfd[i].revents & POLLERR) {
2552 ERR("Error returned in polling fd %d.", pollfd[i].fd);
2553 if (!local_stream[i]->data_read) {
2554 consumer_del_stream(local_stream[i], data_ht);
2555 local_stream[i] = NULL;
2556 num_hup++;
2557 }
2558 } else if (pollfd[i].revents & POLLNVAL) {
2559 ERR("Polling fd %d tells fd is not open.", pollfd[i].fd);
2560 if (!local_stream[i]->data_read) {
2561 consumer_del_stream(local_stream[i], data_ht);
2562 local_stream[i] = NULL;
2563 num_hup++;
2564 }
2565 }
2566 if (local_stream[i] != NULL) {
2567 local_stream[i]->data_read = 0;
2568 }
2569 }
2570 }
2571 end:
2572 DBG("polling thread exiting");
2573 free(pollfd);
2574 free(local_stream);
2575
2576 /*
2577 * Close the write side of the pipe so epoll_wait() in
2578 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2579 * read side of the pipe. If we close them both, epoll_wait strangely does
2580 * not return and could create a endless wait period if the pipe is the
2581 * only tracked fd in the poll set. The thread will take care of closing
2582 * the read side.
2583 */
2584 (void) lttng_pipe_write_close(ctx->consumer_metadata_pipe);
2585
2586 destroy_data_stream_ht(data_ht);
2587
2588 rcu_unregister_thread();
2589 return NULL;
2590 }
2591
2592 /*
2593 * Close wake-up end of each stream belonging to the channel. This will
2594 * allow the poll() on the stream read-side to detect when the
2595 * write-side (application) finally closes them.
2596 */
2597 static
2598 void consumer_close_channel_streams(struct lttng_consumer_channel *channel)
2599 {
2600 struct lttng_ht *ht;
2601 struct lttng_consumer_stream *stream;
2602 struct lttng_ht_iter iter;
2603
2604 ht = consumer_data.stream_per_chan_id_ht;
2605
2606 rcu_read_lock();
2607 cds_lfht_for_each_entry_duplicate(ht->ht,
2608 ht->hash_fct(&channel->key, lttng_ht_seed),
2609 ht->match_fct, &channel->key,
2610 &iter.iter, stream, node_channel_id.node) {
2611 /*
2612 * Protect against teardown with mutex.
2613 */
2614 pthread_mutex_lock(&stream->lock);
2615 if (cds_lfht_is_node_deleted(&stream->node.node)) {
2616 goto next;
2617 }
2618 switch (consumer_data.type) {
2619 case LTTNG_CONSUMER_KERNEL:
2620 break;
2621 case LTTNG_CONSUMER32_UST:
2622 case LTTNG_CONSUMER64_UST:
2623 /*
2624 * Note: a mutex is taken internally within
2625 * liblttng-ust-ctl to protect timer wakeup_fd
2626 * use from concurrent close.
2627 */
2628 lttng_ustconsumer_close_stream_wakeup(stream);
2629 break;
2630 default:
2631 ERR("Unknown consumer_data type");
2632 assert(0);
2633 }
2634 next:
2635 pthread_mutex_unlock(&stream->lock);
2636 }
2637 rcu_read_unlock();
2638 }
2639
2640 static void destroy_channel_ht(struct lttng_ht *ht)
2641 {
2642 struct lttng_ht_iter iter;
2643 struct lttng_consumer_channel *channel;
2644 int ret;
2645
2646 if (ht == NULL) {
2647 return;
2648 }
2649
2650 rcu_read_lock();
2651 cds_lfht_for_each_entry(ht->ht, &iter.iter, channel, wait_fd_node.node) {
2652 ret = lttng_ht_del(ht, &iter);
2653 assert(ret != 0);
2654 }
2655 rcu_read_unlock();
2656
2657 lttng_ht_destroy(ht);
2658 }
2659
2660 /*
2661 * This thread polls the channel fds to detect when they are being
2662 * closed. It closes all related streams if the channel is detected as
2663 * closed. It is currently only used as a shim layer for UST because the
2664 * consumerd needs to keep the per-stream wakeup end of pipes open for
2665 * periodical flush.
2666 */
2667 void *consumer_thread_channel_poll(void *data)
2668 {
2669 int ret, i, pollfd;
2670 uint32_t revents, nb_fd;
2671 struct lttng_consumer_channel *chan = NULL;
2672 struct lttng_ht_iter iter;
2673 struct lttng_ht_node_u64 *node;
2674 struct lttng_poll_event events;
2675 struct lttng_consumer_local_data *ctx = data;
2676 struct lttng_ht *channel_ht;
2677
2678 rcu_register_thread();
2679
2680 channel_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
2681 if (!channel_ht) {
2682 /* ENOMEM at this point. Better to bail out. */
2683 goto end_ht;
2684 }
2685
2686 DBG("Thread channel poll started");
2687
2688 /* Size is set to 1 for the consumer_channel pipe */
2689 ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC);
2690 if (ret < 0) {
2691 ERR("Poll set creation failed");
2692 goto end_poll;
2693 }
2694
2695 ret = lttng_poll_add(&events, ctx->consumer_channel_pipe[0], LPOLLIN);
2696 if (ret < 0) {
2697 goto end;
2698 }
2699
2700 /* Main loop */
2701 DBG("Channel main loop started");
2702
2703 while (1) {
2704 /* Only the channel pipe is set */
2705 if (LTTNG_POLL_GETNB(&events) == 0 && consumer_quit == 1) {
2706 goto end;
2707 }
2708
2709 restart:
2710 DBG("Channel poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events));
2711 ret = lttng_poll_wait(&events, -1);
2712 DBG("Channel event catched in thread");
2713 if (ret < 0) {
2714 if (errno == EINTR) {
2715 ERR("Poll EINTR catched");
2716 goto restart;
2717 }
2718 goto end;
2719 }
2720
2721 nb_fd = ret;
2722
2723 /* From here, the event is a channel wait fd */
2724 for (i = 0; i < nb_fd; i++) {
2725 revents = LTTNG_POLL_GETEV(&events, i);
2726 pollfd = LTTNG_POLL_GETFD(&events, i);
2727
2728 /* Just don't waste time if no returned events for the fd */
2729 if (!revents) {
2730 continue;
2731 }
2732 if (pollfd == ctx->consumer_channel_pipe[0]) {
2733 if (revents & (LPOLLERR | LPOLLHUP)) {
2734 DBG("Channel thread pipe hung up");
2735 /*
2736 * Remove the pipe from the poll set and continue the loop
2737 * since their might be data to consume.
2738 */
2739 lttng_poll_del(&events, ctx->consumer_channel_pipe[0]);
2740 continue;
2741 } else if (revents & LPOLLIN) {
2742 enum consumer_channel_action action;
2743 uint64_t key;
2744
2745 ret = read_channel_pipe(ctx, &chan, &key, &action);
2746 if (ret <= 0) {
2747 ERR("Error reading channel pipe");
2748 continue;
2749 }
2750
2751 switch (action) {
2752 case CONSUMER_CHANNEL_ADD:
2753 DBG("Adding channel %d to poll set",
2754 chan->wait_fd);
2755
2756 lttng_ht_node_init_u64(&chan->wait_fd_node,
2757 chan->wait_fd);
2758 rcu_read_lock();
2759 lttng_ht_add_unique_u64(channel_ht,
2760 &chan->wait_fd_node);
2761 rcu_read_unlock();
2762 /* Add channel to the global poll events list */
2763 lttng_poll_add(&events, chan->wait_fd,
2764 LPOLLIN | LPOLLPRI);
2765 break;
2766 case CONSUMER_CHANNEL_DEL:
2767 {
2768 struct lttng_consumer_stream *stream, *stmp;
2769
2770 rcu_read_lock();
2771 chan = consumer_find_channel(key);
2772 if (!chan) {
2773 rcu_read_unlock();
2774 ERR("UST consumer get channel key %" PRIu64 " not found for del channel", key);
2775 break;
2776 }
2777 lttng_poll_del(&events, chan->wait_fd);
2778 iter.iter.node = &chan->wait_fd_node.node;
2779 ret = lttng_ht_del(channel_ht, &iter);
2780 assert(ret == 0);
2781 consumer_close_channel_streams(chan);
2782
2783 switch (consumer_data.type) {
2784 case LTTNG_CONSUMER_KERNEL:
2785 break;
2786 case LTTNG_CONSUMER32_UST:
2787 case LTTNG_CONSUMER64_UST:
2788 /* Delete streams that might have been left in the stream list. */
2789 cds_list_for_each_entry_safe(stream, stmp, &chan->streams.head,
2790 send_node) {
2791 cds_list_del(&stream->send_node);
2792 lttng_ustconsumer_del_stream(stream);
2793 uatomic_sub(&stream->chan->refcount, 1);
2794 assert(&chan->refcount);
2795 free(stream);
2796 }
2797 break;
2798 default:
2799 ERR("Unknown consumer_data type");
2800 assert(0);
2801 }
2802
2803 /*
2804 * Release our own refcount. Force channel deletion even if
2805 * streams were not initialized.
2806 */
2807 if (!uatomic_sub_return(&chan->refcount, 1)) {
2808 consumer_del_channel(chan);
2809 }
2810 rcu_read_unlock();
2811 goto restart;
2812 }
2813 case CONSUMER_CHANNEL_QUIT:
2814 /*
2815 * Remove the pipe from the poll set and continue the loop
2816 * since their might be data to consume.
2817 */
2818 lttng_poll_del(&events, ctx->consumer_channel_pipe[0]);
2819 continue;
2820 default:
2821 ERR("Unknown action");
2822 break;
2823 }
2824 }
2825
2826 /* Handle other stream */
2827 continue;
2828 }
2829
2830 rcu_read_lock();
2831 {
2832 uint64_t tmp_id = (uint64_t) pollfd;
2833
2834 lttng_ht_lookup(channel_ht, &tmp_id, &iter);
2835 }
2836 node = lttng_ht_iter_get_node_u64(&iter);
2837 assert(node);
2838
2839 chan = caa_container_of(node, struct lttng_consumer_channel,
2840 wait_fd_node);
2841
2842 /* Check for error event */
2843 if (revents & (LPOLLERR | LPOLLHUP)) {
2844 DBG("Channel fd %d is hup|err.", pollfd);
2845
2846 lttng_poll_del(&events, chan->wait_fd);
2847 ret = lttng_ht_del(channel_ht, &iter);
2848 assert(ret == 0);
2849 assert(cds_list_empty(&chan->streams.head));
2850 consumer_close_channel_streams(chan);
2851
2852 /* Release our own refcount */
2853 if (!uatomic_sub_return(&chan->refcount, 1)
2854 && !uatomic_read(&chan->nb_init_stream_left)) {
2855 consumer_del_channel(chan);
2856 }
2857 }
2858
2859 /* Release RCU lock for the channel looked up */
2860 rcu_read_unlock();
2861 }
2862 }
2863
2864 end:
2865 lttng_poll_clean(&events);
2866 end_poll:
2867 destroy_channel_ht(channel_ht);
2868 end_ht:
2869 DBG("Channel poll thread exiting");
2870 rcu_unregister_thread();
2871 return NULL;
2872 }
2873
2874 static int set_metadata_socket(struct lttng_consumer_local_data *ctx,
2875 struct pollfd *sockpoll, int client_socket)
2876 {
2877 int ret;
2878
2879 assert(ctx);
2880 assert(sockpoll);
2881
2882 if (lttng_consumer_poll_socket(sockpoll) < 0) {
2883 ret = -1;
2884 goto error;
2885 }
2886 DBG("Metadata connection on client_socket");
2887
2888 /* Blocking call, waiting for transmission */
2889 ctx->consumer_metadata_socket = lttcomm_accept_unix_sock(client_socket);
2890 if (ctx->consumer_metadata_socket < 0) {
2891 WARN("On accept metadata");
2892 ret = -1;
2893 goto error;
2894 }
2895 ret = 0;
2896
2897 error:
2898 return ret;
2899 }
2900
2901 /*
2902 * This thread listens on the consumerd socket and receives the file
2903 * descriptors from the session daemon.
2904 */
2905 void *consumer_thread_sessiond_poll(void *data)
2906 {
2907 int sock = -1, client_socket, ret;
2908 /*
2909 * structure to poll for incoming data on communication socket avoids
2910 * making blocking sockets.
2911 */
2912 struct pollfd consumer_sockpoll[2];
2913 struct lttng_consumer_local_data *ctx = data;
2914
2915 rcu_register_thread();
2916
2917 DBG("Creating command socket %s", ctx->consumer_command_sock_path);
2918 unlink(ctx->consumer_command_sock_path);
2919 client_socket = lttcomm_create_unix_sock(ctx->consumer_command_sock_path);
2920 if (client_socket < 0) {
2921 ERR("Cannot create command socket");
2922 goto end;
2923 }
2924
2925 ret = lttcomm_listen_unix_sock(client_socket);
2926 if (ret < 0) {
2927 goto end;
2928 }
2929
2930 DBG("Sending ready command to lttng-sessiond");
2931 ret = lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY);
2932 /* return < 0 on error, but == 0 is not fatal */
2933 if (ret < 0) {
2934 ERR("Error sending ready command to lttng-sessiond");
2935 goto end;
2936 }
2937
2938 /* prepare the FDs to poll : to client socket and the should_quit pipe */
2939 consumer_sockpoll[0].fd = ctx->consumer_should_quit[0];
2940 consumer_sockpoll[0].events = POLLIN | POLLPRI;
2941 consumer_sockpoll[1].fd = client_socket;
2942 consumer_sockpoll[1].events = POLLIN | POLLPRI;
2943
2944 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
2945 goto end;
2946 }
2947 DBG("Connection on client_socket");
2948
2949 /* Blocking call, waiting for transmission */
2950 sock = lttcomm_accept_unix_sock(client_socket);
2951 if (sock < 0) {
2952 WARN("On accept");
2953 goto end;
2954 }
2955
2956 /*
2957 * Setup metadata socket which is the second socket connection on the
2958 * command unix socket.
2959 */
2960 ret = set_metadata_socket(ctx, consumer_sockpoll, client_socket);
2961 if (ret < 0) {
2962 goto end;
2963 }
2964
2965 /* This socket is not useful anymore. */
2966 ret = close(client_socket);
2967 if (ret < 0) {
2968 PERROR("close client_socket");
2969 }
2970 client_socket = -1;
2971
2972 /* update the polling structure to poll on the established socket */
2973 consumer_sockpoll[1].fd = sock;
2974 consumer_sockpoll[1].events = POLLIN | POLLPRI;
2975
2976 while (1) {
2977 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
2978 goto end;
2979 }
2980 DBG("Incoming command on sock");
2981 ret = lttng_consumer_recv_cmd(ctx, sock, consumer_sockpoll);
2982 if (ret == -ENOENT) {
2983 DBG("Received STOP command");
2984 goto end;
2985 }
2986 if (ret <= 0) {
2987 /*
2988 * This could simply be a session daemon quitting. Don't output
2989 * ERR() here.
2990 */
2991 DBG("Communication interrupted on command socket");
2992 goto end;
2993 }
2994 if (consumer_quit) {
2995 DBG("consumer_thread_receive_fds received quit from signal");
2996 goto end;
2997 }
2998 DBG("received command on sock");
2999 }
3000 end:
3001 DBG("Consumer thread sessiond poll exiting");
3002
3003 /*
3004 * Close metadata streams since the producer is the session daemon which
3005 * just died.
3006 *
3007 * NOTE: for now, this only applies to the UST tracer.
3008 */
3009 lttng_consumer_close_metadata();
3010
3011 /*
3012 * when all fds have hung up, the polling thread
3013 * can exit cleanly
3014 */
3015 consumer_quit = 1;
3016
3017 /*
3018 * Notify the data poll thread to poll back again and test the
3019 * consumer_quit state that we just set so to quit gracefully.
3020 */
3021 notify_thread_lttng_pipe(ctx->consumer_data_pipe);
3022
3023 notify_channel_pipe(ctx, NULL, -1, CONSUMER_CHANNEL_QUIT);
3024
3025 /* Cleaning up possibly open sockets. */
3026 if (sock >= 0) {
3027 ret = close(sock);
3028 if (ret < 0) {
3029 PERROR("close sock sessiond poll");
3030 }
3031 }
3032 if (client_socket >= 0) {
3033 ret = close(client_socket);
3034 if (ret < 0) {
3035 PERROR("close client_socket sessiond poll");
3036 }
3037 }
3038
3039 rcu_unregister_thread();
3040 return NULL;
3041 }
3042
3043 ssize_t lttng_consumer_read_subbuffer(struct lttng_consumer_stream *stream,
3044 struct lttng_consumer_local_data *ctx)
3045 {
3046 ssize_t ret;
3047
3048 pthread_mutex_lock(&stream->lock);
3049
3050 switch (consumer_data.type) {
3051 case LTTNG_CONSUMER_KERNEL:
3052 ret = lttng_kconsumer_read_subbuffer(stream, ctx);
3053 break;
3054 case LTTNG_CONSUMER32_UST:
3055 case LTTNG_CONSUMER64_UST:
3056 ret = lttng_ustconsumer_read_subbuffer(stream, ctx);
3057 break;
3058 default:
3059 ERR("Unknown consumer_data type");
3060 assert(0);
3061 ret = -ENOSYS;
3062 break;
3063 }
3064
3065 pthread_mutex_unlock(&stream->lock);
3066 return ret;
3067 }
3068
3069 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream *stream)
3070 {
3071 switch (consumer_data.type) {
3072 case LTTNG_CONSUMER_KERNEL:
3073 return lttng_kconsumer_on_recv_stream(stream);
3074 case LTTNG_CONSUMER32_UST:
3075 case LTTNG_CONSUMER64_UST:
3076 return lttng_ustconsumer_on_recv_stream(stream);
3077 default:
3078 ERR("Unknown consumer_data type");
3079 assert(0);
3080 return -ENOSYS;
3081 }
3082 }
3083
3084 /*
3085 * Allocate and set consumer data hash tables.
3086 */
3087 void lttng_consumer_init(void)
3088 {
3089 consumer_data.channel_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
3090 consumer_data.relayd_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
3091 consumer_data.stream_list_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
3092 consumer_data.stream_per_chan_id_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
3093 }
3094
3095 /*
3096 * Process the ADD_RELAYD command receive by a consumer.
3097 *
3098 * This will create a relayd socket pair and add it to the relayd hash table.
3099 * The caller MUST acquire a RCU read side lock before calling it.
3100 */
3101 int consumer_add_relayd_socket(uint64_t net_seq_idx, int sock_type,
3102 struct lttng_consumer_local_data *ctx, int sock,
3103 struct pollfd *consumer_sockpoll,
3104 struct lttcomm_relayd_sock *relayd_sock, uint64_t sessiond_id)
3105 {
3106 int fd = -1, ret = -1, relayd_created = 0;
3107 enum lttng_error_code ret_code = LTTNG_OK;
3108 struct consumer_relayd_sock_pair *relayd = NULL;
3109
3110 assert(ctx);
3111 assert(relayd_sock);
3112
3113 DBG("Consumer adding relayd socket (idx: %" PRIu64 ")", net_seq_idx);
3114
3115 /* Get relayd reference if exists. */
3116 relayd = consumer_find_relayd(net_seq_idx);
3117 if (relayd == NULL) {
3118 assert(sock_type == LTTNG_STREAM_CONTROL);
3119 /* Not found. Allocate one. */
3120 relayd = consumer_allocate_relayd_sock_pair(net_seq_idx);
3121 if (relayd == NULL) {
3122 ret = -ENOMEM;
3123 ret_code = LTTCOMM_CONSUMERD_ENOMEM;
3124 goto error;
3125 } else {
3126 relayd->sessiond_session_id = sessiond_id;
3127 relayd_created = 1;
3128 }
3129
3130 /*
3131 * This code path MUST continue to the consumer send status message to
3132 * we can notify the session daemon and continue our work without
3133 * killing everything.
3134 */
3135 } else {
3136 /*
3137 * relayd key should never be found for control socket.
3138 */
3139 assert(sock_type != LTTNG_STREAM_CONTROL);
3140 }
3141
3142 /* First send a status message before receiving the fds. */
3143 ret = consumer_send_status_msg(sock, LTTNG_OK);
3144 if (ret < 0) {
3145 /* Somehow, the session daemon is not responding anymore. */
3146 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_FATAL);
3147 goto error_nosignal;
3148 }
3149
3150 /* Poll on consumer socket. */
3151 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
3152 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_POLL_ERROR);
3153 ret = -EINTR;
3154 goto error_nosignal;
3155 }
3156
3157 /* Get relayd socket from session daemon */
3158 ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1);
3159 if (ret != sizeof(fd)) {
3160 ret = -1;
3161 fd = -1; /* Just in case it gets set with an invalid value. */
3162
3163 /*
3164 * Failing to receive FDs might indicate a major problem such as
3165 * reaching a fd limit during the receive where the kernel returns a
3166 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3167 * don't take any chances and stop everything.
3168 *
3169 * XXX: Feature request #558 will fix that and avoid this possible
3170 * issue when reaching the fd limit.
3171 */
3172 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD);
3173 ret_code = LTTCOMM_CONSUMERD_ERROR_RECV_FD;
3174 goto error;
3175 }
3176
3177 /* Copy socket information and received FD */
3178 switch (sock_type) {
3179 case LTTNG_STREAM_CONTROL:
3180 /* Copy received lttcomm socket */
3181 lttcomm_copy_sock(&relayd->control_sock.sock, &relayd_sock->sock);
3182 ret = lttcomm_create_sock(&relayd->control_sock.sock);
3183 /* Handle create_sock error. */
3184 if (ret < 0) {
3185 ret_code = LTTCOMM_CONSUMERD_ENOMEM;
3186 goto error;
3187 }
3188 /*
3189 * Close the socket created internally by
3190 * lttcomm_create_sock, so we can replace it by the one
3191 * received from sessiond.
3192 */
3193 if (close(relayd->control_sock.sock.fd)) {
3194 PERROR("close");
3195 }
3196
3197 /* Assign new file descriptor */
3198 relayd->control_sock.sock.fd = fd;
3199 fd = -1; /* For error path */
3200 /* Assign version values. */
3201 relayd->control_sock.major = relayd_sock->major;
3202 relayd->control_sock.minor = relayd_sock->minor;
3203
3204 /*
3205 * Create a session on the relayd and store the returned id. Lock the
3206 * control socket mutex if the relayd was NOT created before.
3207 */
3208 if (!relayd_created) {
3209 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
3210 }
3211 ret = relayd_create_session(&relayd->control_sock,
3212 &relayd->relayd_session_id);
3213 if (!relayd_created) {
3214 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
3215 }
3216 if (ret < 0) {
3217 /*
3218 * Close all sockets of a relayd object. It will be freed if it was
3219 * created at the error code path or else it will be garbage
3220 * collect.
3221 */
3222 (void) relayd_close(&relayd->control_sock);
3223 (void) relayd_close(&relayd->data_sock);
3224 ret_code = LTTCOMM_CONSUMERD_RELAYD_FAIL;
3225 goto error;
3226 }
3227
3228 break;
3229 case LTTNG_STREAM_DATA:
3230 /* Copy received lttcomm socket */
3231 lttcomm_copy_sock(&relayd->data_sock.sock, &relayd_sock->sock);
3232 ret = lttcomm_create_sock(&relayd->data_sock.sock);
3233 /* Handle create_sock error. */
3234 if (ret < 0) {
3235 ret_code = LTTCOMM_CONSUMERD_ENOMEM;
3236 goto error;
3237 }
3238 /*
3239 * Close the socket created internally by
3240 * lttcomm_create_sock, so we can replace it by the one
3241 * received from sessiond.
3242 */
3243 if (close(relayd->data_sock.sock.fd)) {
3244 PERROR("close");
3245 }
3246
3247 /* Assign new file descriptor */
3248 relayd->data_sock.sock.fd = fd;
3249 fd = -1; /* for eventual error paths */
3250 /* Assign version values. */
3251 relayd->data_sock.major = relayd_sock->major;
3252 relayd->data_sock.minor = relayd_sock->minor;
3253 break;
3254 default:
3255 ERR("Unknown relayd socket type (%d)", sock_type);
3256 ret = -1;
3257 ret_code = LTTCOMM_CONSUMERD_FATAL;
3258 goto error;
3259 }
3260
3261 DBG("Consumer %s socket created successfully with net idx %" PRIu64 " (fd: %d)",
3262 sock_type == LTTNG_STREAM_CONTROL ? "control" : "data",
3263 relayd->net_seq_idx, fd);
3264
3265 /* We successfully added the socket. Send status back. */
3266 ret = consumer_send_status_msg(sock, ret_code);
3267 if (ret < 0) {
3268 /* Somehow, the session daemon is not responding anymore. */
3269 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_FATAL);
3270 goto error_nosignal;
3271 }
3272
3273 /*
3274 * Add relayd socket pair to consumer data hashtable. If object already
3275 * exists or on error, the function gracefully returns.
3276 */
3277 add_relayd(relayd);
3278
3279 /* All good! */
3280 return 0;
3281
3282 error:
3283 if (consumer_send_status_msg(sock, ret_code) < 0) {
3284 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_FATAL);
3285 }
3286
3287 error_nosignal:
3288 /* Close received socket if valid. */
3289 if (fd >= 0) {
3290 if (close(fd)) {
3291 PERROR("close received socket");
3292 }
3293 }
3294
3295 if (relayd_created) {
3296 free(relayd);
3297 }
3298
3299 return ret;
3300 }
3301
3302 /*
3303 * Try to lock the stream mutex.
3304 *
3305 * On success, 1 is returned else 0 indicating that the mutex is NOT lock.
3306 */
3307 static int stream_try_lock(struct lttng_consumer_stream *stream)
3308 {
3309 int ret;
3310
3311 assert(stream);
3312
3313 /*
3314 * Try to lock the stream mutex. On failure, we know that the stream is
3315 * being used else where hence there is data still being extracted.
3316 */
3317 ret = pthread_mutex_trylock(&stream->lock);
3318 if (ret) {
3319 /* For both EBUSY and EINVAL error, the mutex is NOT locked. */
3320 ret = 0;
3321 goto end;
3322 }
3323
3324 ret = 1;
3325
3326 end:
3327 return ret;
3328 }
3329
3330 /*
3331 * Search for a relayd associated to the session id and return the reference.
3332 *
3333 * A rcu read side lock MUST be acquire before calling this function and locked
3334 * until the relayd object is no longer necessary.
3335 */
3336 static struct consumer_relayd_sock_pair *find_relayd_by_session_id(uint64_t id)
3337 {
3338 struct lttng_ht_iter iter;
3339 struct consumer_relayd_sock_pair *relayd = NULL;
3340
3341 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3342 cds_lfht_for_each_entry(consumer_data.relayd_ht->ht, &iter.iter, relayd,
3343 node.node) {
3344 /*
3345 * Check by sessiond id which is unique here where the relayd session
3346 * id might not be when having multiple relayd.
3347 */
3348 if (relayd->sessiond_session_id == id) {
3349 /* Found the relayd. There can be only one per id. */
3350 goto found;
3351 }
3352 }
3353
3354 return NULL;
3355
3356 found:
3357 return relayd;
3358 }
3359
3360 /*
3361 * Check if for a given session id there is still data needed to be extract
3362 * from the buffers.
3363 *
3364 * Return 1 if data is pending or else 0 meaning ready to be read.
3365 */
3366 int consumer_data_pending(uint64_t id)
3367 {
3368 int ret;
3369 struct lttng_ht_iter iter;
3370 struct lttng_ht *ht;
3371 struct lttng_consumer_stream *stream;
3372 struct consumer_relayd_sock_pair *relayd = NULL;
3373 int (*data_pending)(struct lttng_consumer_stream *);
3374
3375 DBG("Consumer data pending command on session id %" PRIu64, id);
3376
3377 rcu_read_lock();
3378 pthread_mutex_lock(&consumer_data.lock);
3379
3380 switch (consumer_data.type) {
3381 case LTTNG_CONSUMER_KERNEL:
3382 data_pending = lttng_kconsumer_data_pending;
3383 break;
3384 case LTTNG_CONSUMER32_UST:
3385 case LTTNG_CONSUMER64_UST:
3386 data_pending = lttng_ustconsumer_data_pending;
3387 break;
3388 default:
3389 ERR("Unknown consumer data type");
3390 assert(0);
3391 }
3392
3393 /* Ease our life a bit */
3394 ht = consumer_data.stream_list_ht;
3395
3396 relayd = find_relayd_by_session_id(id);
3397 if (relayd) {
3398 /* Send init command for data pending. */
3399 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
3400 ret = relayd_begin_data_pending(&relayd->control_sock,
3401 relayd->relayd_session_id);
3402 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
3403 if (ret < 0) {
3404 /* Communication error thus the relayd so no data pending. */
3405 goto data_not_pending;
3406 }
3407 }
3408
3409 cds_lfht_for_each_entry_duplicate(ht->ht,
3410 ht->hash_fct(&id, lttng_ht_seed),
3411 ht->match_fct, &id,
3412 &iter.iter, stream, node_session_id.node) {
3413 /* If this call fails, the stream is being used hence data pending. */
3414 ret = stream_try_lock(stream);
3415 if (!ret) {
3416 goto data_pending;
3417 }
3418
3419 /*
3420 * A removed node from the hash table indicates that the stream has
3421 * been deleted thus having a guarantee that the buffers are closed
3422 * on the consumer side. However, data can still be transmitted
3423 * over the network so don't skip the relayd check.
3424 */
3425 ret = cds_lfht_is_node_deleted(&stream->node.node);
3426 if (!ret) {
3427 /* Check the stream if there is data in the buffers. */
3428 ret = data_pending(stream);
3429 if (ret == 1) {
3430 pthread_mutex_unlock(&stream->lock);
3431 goto data_pending;
3432 }
3433 }
3434
3435 /* Relayd check */
3436 if (relayd) {
3437 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
3438 if (stream->metadata_flag) {
3439 ret = relayd_quiescent_control(&relayd->control_sock,
3440 stream->relayd_stream_id);
3441 } else {
3442 ret = relayd_data_pending(&relayd->control_sock,
3443 stream->relayd_stream_id,
3444 stream->next_net_seq_num - 1);
3445 }
3446 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
3447 if (ret == 1) {
3448 pthread_mutex_unlock(&stream->lock);
3449 goto data_pending;
3450 }
3451 }
3452 pthread_mutex_unlock(&stream->lock);
3453 }
3454
3455 if (relayd) {
3456 unsigned int is_data_inflight = 0;
3457
3458 /* Send init command for data pending. */
3459 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
3460 ret = relayd_end_data_pending(&relayd->control_sock,
3461 relayd->relayd_session_id, &is_data_inflight);
3462 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
3463 if (ret < 0) {
3464 goto data_not_pending;
3465 }
3466 if (is_data_inflight) {
3467 goto data_pending;
3468 }
3469 }
3470
3471 /*
3472 * Finding _no_ node in the hash table and no inflight data means that the
3473 * stream(s) have been removed thus data is guaranteed to be available for
3474 * analysis from the trace files.
3475 */
3476
3477 data_not_pending:
3478 /* Data is available to be read by a viewer. */
3479 pthread_mutex_unlock(&consumer_data.lock);
3480 rcu_read_unlock();
3481 return 0;
3482
3483 data_pending:
3484 /* Data is still being extracted from buffers. */
3485 pthread_mutex_unlock(&consumer_data.lock);
3486 rcu_read_unlock();
3487 return 1;
3488 }
3489
3490 /*
3491 * Send a ret code status message to the sessiond daemon.
3492 *
3493 * Return the sendmsg() return value.
3494 */
3495 int consumer_send_status_msg(int sock, int ret_code)
3496 {
3497 struct lttcomm_consumer_status_msg msg;
3498
3499 msg.ret_code = ret_code;
3500
3501 return lttcomm_send_unix_sock(sock, &msg, sizeof(msg));
3502 }
3503
3504 /*
3505 * Send a channel status message to the sessiond daemon.
3506 *
3507 * Return the sendmsg() return value.
3508 */
3509 int consumer_send_status_channel(int sock,
3510 struct lttng_consumer_channel *channel)
3511 {
3512 struct lttcomm_consumer_status_channel msg;
3513
3514 assert(sock >= 0);
3515
3516 if (!channel) {
3517 msg.ret_code = -LTTNG_ERR_UST_CHAN_FAIL;
3518 } else {
3519 msg.ret_code = LTTNG_OK;
3520 msg.key = channel->key;
3521 msg.stream_count = channel->streams.count;
3522 }
3523
3524 return lttcomm_send_unix_sock(sock, &msg, sizeof(msg));
3525 }
This page took 0.112872 seconds and 4 git commands to generate.