9e3540426620f64f89cf69c8bbefa3cbe08f8962
[lttng-tools.git] / src / common / kernel-consumer / kernel-consumer.c
1 /*
2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #define _LGPL_SOURCE
20 #include <assert.h>
21 #include <poll.h>
22 #include <pthread.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/mman.h>
26 #include <sys/socket.h>
27 #include <sys/types.h>
28 #include <inttypes.h>
29 #include <unistd.h>
30 #include <sys/stat.h>
31
32 #include <bin/lttng-consumerd/health-consumerd.h>
33 #include <common/common.h>
34 #include <common/kernel-ctl/kernel-ctl.h>
35 #include <common/sessiond-comm/sessiond-comm.h>
36 #include <common/sessiond-comm/relayd.h>
37 #include <common/compat/fcntl.h>
38 #include <common/compat/endian.h>
39 #include <common/pipe.h>
40 #include <common/relayd/relayd.h>
41 #include <common/utils.h>
42 #include <common/consumer/consumer-stream.h>
43 #include <common/index/index.h>
44 #include <common/consumer/consumer-timer.h>
45
46 #include "kernel-consumer.h"
47
48 extern struct lttng_consumer_global_data consumer_data;
49 extern int consumer_poll_timeout;
50 extern volatile int consumer_quit;
51
52 /*
53 * Take a snapshot for a specific fd
54 *
55 * Returns 0 on success, < 0 on error
56 */
57 int lttng_kconsumer_take_snapshot(struct lttng_consumer_stream *stream)
58 {
59 int ret = 0;
60 int infd = stream->wait_fd;
61
62 ret = kernctl_snapshot(infd);
63 if (ret != 0) {
64 PERROR("Getting sub-buffer snapshot.");
65 ret = -errno;
66 }
67
68 return ret;
69 }
70
71 /*
72 * Get the produced position
73 *
74 * Returns 0 on success, < 0 on error
75 */
76 int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream *stream,
77 unsigned long *pos)
78 {
79 int ret;
80 int infd = stream->wait_fd;
81
82 ret = kernctl_snapshot_get_produced(infd, pos);
83 if (ret != 0) {
84 PERROR("kernctl_snapshot_get_produced");
85 ret = -errno;
86 }
87
88 return ret;
89 }
90
91 /*
92 * Get the consumerd position
93 *
94 * Returns 0 on success, < 0 on error
95 */
96 int lttng_kconsumer_get_consumed_snapshot(struct lttng_consumer_stream *stream,
97 unsigned long *pos)
98 {
99 int ret;
100 int infd = stream->wait_fd;
101
102 ret = kernctl_snapshot_get_consumed(infd, pos);
103 if (ret != 0) {
104 PERROR("kernctl_snapshot_get_consumed");
105 ret = -errno;
106 }
107
108 return ret;
109 }
110
111 /*
112 * Take a snapshot of all the stream of a channel
113 *
114 * Returns 0 on success, < 0 on error
115 */
116 int lttng_kconsumer_snapshot_channel(uint64_t key, char *path,
117 uint64_t relayd_id, uint64_t nb_packets_per_stream,
118 struct lttng_consumer_local_data *ctx)
119 {
120 int ret;
121 unsigned long consumed_pos, produced_pos;
122 struct lttng_consumer_channel *channel;
123 struct lttng_consumer_stream *stream;
124
125 DBG("Kernel consumer snapshot channel %" PRIu64, key);
126
127 rcu_read_lock();
128
129 channel = consumer_find_channel(key);
130 if (!channel) {
131 ERR("No channel found for key %" PRIu64, key);
132 ret = -1;
133 goto end;
134 }
135
136 /* Splice is not supported yet for channel snapshot. */
137 if (channel->output != CONSUMER_CHANNEL_MMAP) {
138 ERR("Unsupported output %d", channel->output);
139 ret = -1;
140 goto end;
141 }
142
143 cds_list_for_each_entry(stream, &channel->streams.head, send_node) {
144 /* Are we at a position _before_ the first available packet ? */
145 bool before_first_packet = true;
146
147 health_code_update();
148
149 /*
150 * Lock stream because we are about to change its state.
151 */
152 pthread_mutex_lock(&stream->lock);
153
154 /*
155 * Assign the received relayd ID so we can use it for streaming. The streams
156 * are not visible to anyone so this is OK to change it.
157 */
158 stream->net_seq_idx = relayd_id;
159 channel->relayd_id = relayd_id;
160 if (relayd_id != (uint64_t) -1ULL) {
161 ret = consumer_send_relayd_stream(stream, path);
162 if (ret < 0) {
163 ERR("sending stream to relayd");
164 goto end_unlock;
165 }
166 } else {
167 ret = utils_create_stream_file(path, stream->name,
168 stream->chan->tracefile_size,
169 stream->tracefile_count_current,
170 stream->uid, stream->gid, NULL);
171 if (ret < 0) {
172 ERR("utils_create_stream_file");
173 goto end_unlock;
174 }
175
176 stream->out_fd = ret;
177 stream->tracefile_size_current = 0;
178
179 DBG("Kernel consumer snapshot stream %s/%s (%" PRIu64 ")",
180 path, stream->name, stream->key);
181 }
182 if (relayd_id != -1ULL) {
183 ret = consumer_send_relayd_streams_sent(relayd_id);
184 if (ret < 0) {
185 ERR("sending streams sent to relayd");
186 goto end_unlock;
187 }
188 }
189
190 ret = kernctl_buffer_flush(stream->wait_fd);
191 if (ret < 0) {
192 ERR("Failed to flush kernel stream");
193 ret = -errno;
194 goto end_unlock;
195 }
196
197 ret = lttng_kconsumer_take_snapshot(stream);
198 if (ret < 0) {
199 ERR("Taking kernel snapshot");
200 goto end_unlock;
201 }
202
203 ret = lttng_kconsumer_get_produced_snapshot(stream, &produced_pos);
204 if (ret < 0) {
205 ERR("Produced kernel snapshot position");
206 goto end_unlock;
207 }
208
209 ret = lttng_kconsumer_get_consumed_snapshot(stream, &consumed_pos);
210 if (ret < 0) {
211 ERR("Consumerd kernel snapshot position");
212 goto end_unlock;
213 }
214
215 if (stream->max_sb_size == 0) {
216 ret = kernctl_get_max_subbuf_size(stream->wait_fd,
217 &stream->max_sb_size);
218 if (ret < 0) {
219 ERR("Getting kernel max_sb_size");
220 ret = -errno;
221 goto end_unlock;
222 }
223 }
224
225 consumed_pos = consumer_get_consume_start_pos(consumed_pos,
226 produced_pos, nb_packets_per_stream,
227 stream->max_sb_size);
228
229 while (consumed_pos < produced_pos) {
230 ssize_t read_len;
231 unsigned long len, padded_len;
232 int lost_packet = 0;
233
234 health_code_update();
235
236 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos);
237
238 ret = kernctl_get_subbuf(stream->wait_fd, &consumed_pos);
239 if (ret < 0) {
240 if (errno != EAGAIN) {
241 PERROR("kernctl_get_subbuf snapshot");
242 ret = -errno;
243 goto end_unlock;
244 }
245 DBG("Kernel consumer get subbuf failed. Skipping it.");
246 consumed_pos += stream->max_sb_size;
247
248 /*
249 * Start accounting lost packets only when we
250 * already have extracted packets (to match the
251 * content of the final snapshot).
252 */
253 if (!before_first_packet) {
254 lost_packet = 1;
255 }
256 continue;
257 }
258
259 ret = kernctl_get_subbuf_size(stream->wait_fd, &len);
260 if (ret < 0) {
261 ERR("Snapshot kernctl_get_subbuf_size");
262 ret = -errno;
263 goto error_put_subbuf;
264 }
265
266 ret = kernctl_get_padded_subbuf_size(stream->wait_fd, &padded_len);
267 if (ret < 0) {
268 ERR("Snapshot kernctl_get_padded_subbuf_size");
269 ret = -errno;
270 goto error_put_subbuf;
271 }
272
273 read_len = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, len,
274 padded_len - len, NULL);
275 /*
276 * We write the padded len in local tracefiles but the data len
277 * when using a relay. Display the error but continue processing
278 * to try to release the subbuffer.
279 */
280 if (relayd_id != (uint64_t) -1ULL) {
281 if (read_len != len) {
282 ERR("Error sending to the relay (ret: %zd != len: %lu)",
283 read_len, len);
284 }
285 } else {
286 if (read_len != padded_len) {
287 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
288 read_len, padded_len);
289 }
290 }
291
292 ret = kernctl_put_subbuf(stream->wait_fd);
293 if (ret < 0) {
294 ERR("Snapshot kernctl_put_subbuf");
295 ret = -errno;
296 goto end_unlock;
297 }
298 consumed_pos += stream->max_sb_size;
299
300 /*
301 * Only account lost packets located between
302 * succesfully extracted packets (do not account before
303 * and after since they are not visible in the
304 * resulting snapshot).
305 */
306 stream->chan->lost_packets += lost_packet;
307 lost_packet = 0;
308 before_first_packet = false;
309 }
310
311 if (relayd_id == (uint64_t) -1ULL) {
312 if (stream->out_fd >= 0) {
313 ret = close(stream->out_fd);
314 if (ret < 0) {
315 PERROR("Kernel consumer snapshot close out_fd");
316 goto end_unlock;
317 }
318 stream->out_fd = -1;
319 }
320 } else {
321 close_relayd_stream(stream);
322 stream->net_seq_idx = (uint64_t) -1ULL;
323 }
324 pthread_mutex_unlock(&stream->lock);
325 }
326
327 /* All good! */
328 ret = 0;
329 goto end;
330
331 error_put_subbuf:
332 ret = kernctl_put_subbuf(stream->wait_fd);
333 if (ret < 0) {
334 ret = -errno;
335 ERR("Snapshot kernctl_put_subbuf error path");
336 }
337 end_unlock:
338 pthread_mutex_unlock(&stream->lock);
339 end:
340 rcu_read_unlock();
341 return ret;
342 }
343
344 /*
345 * Read the whole metadata available for a snapshot.
346 *
347 * Returns 0 on success, < 0 on error
348 */
349 int lttng_kconsumer_snapshot_metadata(uint64_t key, char *path,
350 uint64_t relayd_id, struct lttng_consumer_local_data *ctx)
351 {
352 int ret, use_relayd = 0;
353 ssize_t ret_read;
354 struct lttng_consumer_channel *metadata_channel;
355 struct lttng_consumer_stream *metadata_stream;
356
357 assert(ctx);
358
359 DBG("Kernel consumer snapshot metadata with key %" PRIu64 " at path %s",
360 key, path);
361
362 rcu_read_lock();
363
364 metadata_channel = consumer_find_channel(key);
365 if (!metadata_channel) {
366 ERR("Kernel snapshot metadata not found for key %" PRIu64, key);
367 ret = -1;
368 goto error;
369 }
370
371 metadata_stream = metadata_channel->metadata_stream;
372 assert(metadata_stream);
373
374 /* Flag once that we have a valid relayd for the stream. */
375 if (relayd_id != (uint64_t) -1ULL) {
376 use_relayd = 1;
377 }
378
379 if (use_relayd) {
380 ret = consumer_send_relayd_stream(metadata_stream, path);
381 if (ret < 0) {
382 goto error;
383 }
384 } else {
385 ret = utils_create_stream_file(path, metadata_stream->name,
386 metadata_stream->chan->tracefile_size,
387 metadata_stream->tracefile_count_current,
388 metadata_stream->uid, metadata_stream->gid, NULL);
389 if (ret < 0) {
390 goto error;
391 }
392 metadata_stream->out_fd = ret;
393 }
394
395 do {
396 health_code_update();
397
398 ret_read = lttng_kconsumer_read_subbuffer(metadata_stream, ctx);
399 if (ret_read < 0) {
400 if (ret_read != -EAGAIN) {
401 ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
402 ret_read);
403 goto error;
404 }
405 /* ret_read is negative at this point so we will exit the loop. */
406 continue;
407 }
408 } while (ret_read >= 0);
409
410 if (use_relayd) {
411 close_relayd_stream(metadata_stream);
412 metadata_stream->net_seq_idx = (uint64_t) -1ULL;
413 } else {
414 if (metadata_stream->out_fd >= 0) {
415 ret = close(metadata_stream->out_fd);
416 if (ret < 0) {
417 PERROR("Kernel consumer snapshot metadata close out_fd");
418 /*
419 * Don't go on error here since the snapshot was successful at this
420 * point but somehow the close failed.
421 */
422 }
423 metadata_stream->out_fd = -1;
424 }
425 }
426
427 ret = 0;
428
429 cds_list_del(&metadata_stream->send_node);
430 consumer_stream_destroy(metadata_stream, NULL);
431 metadata_channel->metadata_stream = NULL;
432 error:
433 rcu_read_unlock();
434 return ret;
435 }
436
437 /*
438 * Receive command from session daemon and process it.
439 *
440 * Return 1 on success else a negative value or 0.
441 */
442 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
443 int sock, struct pollfd *consumer_sockpoll)
444 {
445 ssize_t ret;
446 enum lttcomm_return_code ret_code = LTTCOMM_CONSUMERD_SUCCESS;
447 struct lttcomm_consumer_msg msg;
448
449 health_code_update();
450
451 ret = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg));
452 if (ret != sizeof(msg)) {
453 if (ret > 0) {
454 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_CMD);
455 ret = -1;
456 }
457 return ret;
458 }
459
460 health_code_update();
461
462 /* Deprecated command */
463 assert(msg.cmd_type != LTTNG_CONSUMER_STOP);
464
465 health_code_update();
466
467 /* relayd needs RCU read-side protection */
468 rcu_read_lock();
469
470 switch (msg.cmd_type) {
471 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET:
472 {
473 /* Session daemon status message are handled in the following call. */
474 ret = consumer_add_relayd_socket(msg.u.relayd_sock.net_index,
475 msg.u.relayd_sock.type, ctx, sock, consumer_sockpoll,
476 &msg.u.relayd_sock.sock, msg.u.relayd_sock.session_id,
477 msg.u.relayd_sock.relayd_session_id);
478 goto end_nosignal;
479 }
480 case LTTNG_CONSUMER_ADD_CHANNEL:
481 {
482 struct lttng_consumer_channel *new_channel;
483 int ret_recv;
484
485 health_code_update();
486
487 /* First send a status message before receiving the fds. */
488 ret = consumer_send_status_msg(sock, ret_code);
489 if (ret < 0) {
490 /* Somehow, the session daemon is not responding anymore. */
491 goto error_fatal;
492 }
493
494 health_code_update();
495
496 DBG("consumer_add_channel %" PRIu64, msg.u.channel.channel_key);
497 new_channel = consumer_allocate_channel(msg.u.channel.channel_key,
498 msg.u.channel.session_id, msg.u.channel.pathname,
499 msg.u.channel.name, msg.u.channel.uid, msg.u.channel.gid,
500 msg.u.channel.relayd_id, msg.u.channel.output,
501 msg.u.channel.tracefile_size,
502 msg.u.channel.tracefile_count, 0,
503 msg.u.channel.monitor,
504 msg.u.channel.live_timer_interval,
505 NULL, NULL);
506 if (new_channel == NULL) {
507 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
508 goto end_nosignal;
509 }
510 new_channel->nb_init_stream_left = msg.u.channel.nb_init_streams;
511 switch (msg.u.channel.output) {
512 case LTTNG_EVENT_SPLICE:
513 new_channel->output = CONSUMER_CHANNEL_SPLICE;
514 break;
515 case LTTNG_EVENT_MMAP:
516 new_channel->output = CONSUMER_CHANNEL_MMAP;
517 break;
518 default:
519 ERR("Channel output unknown %d", msg.u.channel.output);
520 goto end_nosignal;
521 }
522
523 /* Translate and save channel type. */
524 switch (msg.u.channel.type) {
525 case CONSUMER_CHANNEL_TYPE_DATA:
526 case CONSUMER_CHANNEL_TYPE_METADATA:
527 new_channel->type = msg.u.channel.type;
528 break;
529 default:
530 assert(0);
531 goto end_nosignal;
532 };
533
534 health_code_update();
535
536 if (ctx->on_recv_channel != NULL) {
537 ret_recv = ctx->on_recv_channel(new_channel);
538 if (ret_recv == 0) {
539 ret = consumer_add_channel(new_channel, ctx);
540 } else if (ret_recv < 0) {
541 goto end_nosignal;
542 }
543 } else {
544 ret = consumer_add_channel(new_channel, ctx);
545 }
546 if (CONSUMER_CHANNEL_TYPE_DATA) {
547 consumer_timer_live_start(new_channel,
548 msg.u.channel.live_timer_interval);
549 }
550
551 health_code_update();
552
553 /* If we received an error in add_channel, we need to report it. */
554 if (ret < 0) {
555 ret = consumer_send_status_msg(sock, ret);
556 if (ret < 0) {
557 goto error_fatal;
558 }
559 goto end_nosignal;
560 }
561
562 goto end_nosignal;
563 }
564 case LTTNG_CONSUMER_ADD_STREAM:
565 {
566 int fd;
567 struct lttng_pipe *stream_pipe;
568 struct lttng_consumer_stream *new_stream;
569 struct lttng_consumer_channel *channel;
570 int alloc_ret = 0;
571
572 /*
573 * Get stream's channel reference. Needed when adding the stream to the
574 * global hash table.
575 */
576 channel = consumer_find_channel(msg.u.stream.channel_key);
577 if (!channel) {
578 /*
579 * We could not find the channel. Can happen if cpu hotplug
580 * happens while tearing down.
581 */
582 ERR("Unable to find channel key %" PRIu64, msg.u.stream.channel_key);
583 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
584 }
585
586 health_code_update();
587
588 /* First send a status message before receiving the fds. */
589 ret = consumer_send_status_msg(sock, ret_code);
590 if (ret < 0) {
591 /* Somehow, the session daemon is not responding anymore. */
592 goto error_fatal;
593 }
594
595 health_code_update();
596
597 if (ret_code != LTTCOMM_CONSUMERD_SUCCESS) {
598 /* Channel was not found. */
599 goto end_nosignal;
600 }
601
602 /* Blocking call */
603 health_poll_entry();
604 ret = lttng_consumer_poll_socket(consumer_sockpoll);
605 health_poll_exit();
606 if (ret) {
607 goto error_fatal;
608 }
609
610 health_code_update();
611
612 /* Get stream file descriptor from socket */
613 ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1);
614 if (ret != sizeof(fd)) {
615 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD);
616 rcu_read_unlock();
617 return ret;
618 }
619
620 health_code_update();
621
622 /*
623 * Send status code to session daemon only if the recv works. If the
624 * above recv() failed, the session daemon is notified through the
625 * error socket and the teardown is eventually done.
626 */
627 ret = consumer_send_status_msg(sock, ret_code);
628 if (ret < 0) {
629 /* Somehow, the session daemon is not responding anymore. */
630 goto end_nosignal;
631 }
632
633 health_code_update();
634
635 new_stream = consumer_allocate_stream(channel->key,
636 fd,
637 LTTNG_CONSUMER_ACTIVE_STREAM,
638 channel->name,
639 channel->uid,
640 channel->gid,
641 channel->relayd_id,
642 channel->session_id,
643 msg.u.stream.cpu,
644 &alloc_ret,
645 channel->type,
646 channel->monitor);
647 if (new_stream == NULL) {
648 switch (alloc_ret) {
649 case -ENOMEM:
650 case -EINVAL:
651 default:
652 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
653 break;
654 }
655 goto end_nosignal;
656 }
657
658 new_stream->chan = channel;
659 new_stream->wait_fd = fd;
660 switch (channel->output) {
661 case CONSUMER_CHANNEL_SPLICE:
662 new_stream->output = LTTNG_EVENT_SPLICE;
663 ret = utils_create_pipe(new_stream->splice_pipe);
664 if (ret < 0) {
665 goto end_nosignal;
666 }
667 break;
668 case CONSUMER_CHANNEL_MMAP:
669 new_stream->output = LTTNG_EVENT_MMAP;
670 break;
671 default:
672 ERR("Stream output unknown %d", channel->output);
673 goto end_nosignal;
674 }
675
676 /*
677 * We've just assigned the channel to the stream so increment the
678 * refcount right now. We don't need to increment the refcount for
679 * streams in no monitor because we handle manually the cleanup of
680 * those. It is very important to make sure there is NO prior
681 * consumer_del_stream() calls or else the refcount will be unbalanced.
682 */
683 if (channel->monitor) {
684 uatomic_inc(&new_stream->chan->refcount);
685 }
686
687 /*
688 * The buffer flush is done on the session daemon side for the kernel
689 * so no need for the stream "hangup_flush_done" variable to be
690 * tracked. This is important for a kernel stream since we don't rely
691 * on the flush state of the stream to read data. It's not the case for
692 * user space tracing.
693 */
694 new_stream->hangup_flush_done = 0;
695
696 health_code_update();
697
698 if (ctx->on_recv_stream) {
699 ret = ctx->on_recv_stream(new_stream);
700 if (ret < 0) {
701 consumer_stream_free(new_stream);
702 goto end_nosignal;
703 }
704 }
705
706 health_code_update();
707
708 if (new_stream->metadata_flag) {
709 channel->metadata_stream = new_stream;
710 }
711
712 /* Do not monitor this stream. */
713 if (!channel->monitor) {
714 DBG("Kernel consumer add stream %s in no monitor mode with "
715 "relayd id %" PRIu64, new_stream->name,
716 new_stream->net_seq_idx);
717 cds_list_add(&new_stream->send_node, &channel->streams.head);
718 break;
719 }
720
721 /* Send stream to relayd if the stream has an ID. */
722 if (new_stream->net_seq_idx != (uint64_t) -1ULL) {
723 ret = consumer_send_relayd_stream(new_stream,
724 new_stream->chan->pathname);
725 if (ret < 0) {
726 consumer_stream_free(new_stream);
727 goto end_nosignal;
728 }
729 }
730
731 /* Get the right pipe where the stream will be sent. */
732 if (new_stream->metadata_flag) {
733 ret = consumer_add_metadata_stream(new_stream);
734 if (ret) {
735 ERR("Consumer add metadata stream %" PRIu64 " failed. Continuing",
736 new_stream->key);
737 consumer_stream_free(new_stream);
738 goto end_nosignal;
739 }
740 stream_pipe = ctx->consumer_metadata_pipe;
741 } else {
742 ret = consumer_add_data_stream(new_stream);
743 if (ret) {
744 ERR("Consumer add stream %" PRIu64 " failed. Continuing",
745 new_stream->key);
746 consumer_stream_free(new_stream);
747 goto end_nosignal;
748 }
749 stream_pipe = ctx->consumer_data_pipe;
750 }
751
752 /* Vitible to other threads */
753 new_stream->globally_visible = 1;
754
755 health_code_update();
756
757 ret = lttng_pipe_write(stream_pipe, &new_stream, sizeof(new_stream));
758 if (ret < 0) {
759 ERR("Consumer write %s stream to pipe %d",
760 new_stream->metadata_flag ? "metadata" : "data",
761 lttng_pipe_get_writefd(stream_pipe));
762 if (new_stream->metadata_flag) {
763 consumer_del_stream_for_metadata(new_stream);
764 } else {
765 consumer_del_stream_for_data(new_stream);
766 }
767 goto end_nosignal;
768 }
769
770 DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64,
771 new_stream->name, fd, new_stream->relayd_stream_id);
772 break;
773 }
774 case LTTNG_CONSUMER_STREAMS_SENT:
775 {
776 struct lttng_consumer_channel *channel;
777
778 /*
779 * Get stream's channel reference. Needed when adding the stream to the
780 * global hash table.
781 */
782 channel = consumer_find_channel(msg.u.sent_streams.channel_key);
783 if (!channel) {
784 /*
785 * We could not find the channel. Can happen if cpu hotplug
786 * happens while tearing down.
787 */
788 ERR("Unable to find channel key %" PRIu64,
789 msg.u.sent_streams.channel_key);
790 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
791 }
792
793 health_code_update();
794
795 /*
796 * Send status code to session daemon.
797 */
798 ret = consumer_send_status_msg(sock, ret_code);
799 if (ret < 0 || ret_code != LTTCOMM_CONSUMERD_SUCCESS) {
800 /* Somehow, the session daemon is not responding anymore. */
801 goto end_nosignal;
802 }
803
804 health_code_update();
805
806 /*
807 * We should not send this message if we don't monitor the
808 * streams in this channel.
809 */
810 if (!channel->monitor) {
811 break;
812 }
813
814 health_code_update();
815 /* Send stream to relayd if the stream has an ID. */
816 if (msg.u.sent_streams.net_seq_idx != (uint64_t) -1ULL) {
817 ret = consumer_send_relayd_streams_sent(
818 msg.u.sent_streams.net_seq_idx);
819 if (ret < 0) {
820 goto end_nosignal;
821 }
822 }
823 break;
824 }
825 case LTTNG_CONSUMER_UPDATE_STREAM:
826 {
827 rcu_read_unlock();
828 return -ENOSYS;
829 }
830 case LTTNG_CONSUMER_DESTROY_RELAYD:
831 {
832 uint64_t index = msg.u.destroy_relayd.net_seq_idx;
833 struct consumer_relayd_sock_pair *relayd;
834
835 DBG("Kernel consumer destroying relayd %" PRIu64, index);
836
837 /* Get relayd reference if exists. */
838 relayd = consumer_find_relayd(index);
839 if (relayd == NULL) {
840 DBG("Unable to find relayd %" PRIu64, index);
841 ret_code = LTTCOMM_CONSUMERD_RELAYD_FAIL;
842 }
843
844 /*
845 * Each relayd socket pair has a refcount of stream attached to it
846 * which tells if the relayd is still active or not depending on the
847 * refcount value.
848 *
849 * This will set the destroy flag of the relayd object and destroy it
850 * if the refcount reaches zero when called.
851 *
852 * The destroy can happen either here or when a stream fd hangs up.
853 */
854 if (relayd) {
855 consumer_flag_relayd_for_destroy(relayd);
856 }
857
858 health_code_update();
859
860 ret = consumer_send_status_msg(sock, ret_code);
861 if (ret < 0) {
862 /* Somehow, the session daemon is not responding anymore. */
863 goto error_fatal;
864 }
865
866 goto end_nosignal;
867 }
868 case LTTNG_CONSUMER_DATA_PENDING:
869 {
870 int32_t ret;
871 uint64_t id = msg.u.data_pending.session_id;
872
873 DBG("Kernel consumer data pending command for id %" PRIu64, id);
874
875 ret = consumer_data_pending(id);
876
877 health_code_update();
878
879 /* Send back returned value to session daemon */
880 ret = lttcomm_send_unix_sock(sock, &ret, sizeof(ret));
881 if (ret < 0) {
882 PERROR("send data pending ret code");
883 goto error_fatal;
884 }
885
886 /*
887 * No need to send back a status message since the data pending
888 * returned value is the response.
889 */
890 break;
891 }
892 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL:
893 {
894 if (msg.u.snapshot_channel.metadata == 1) {
895 ret = lttng_kconsumer_snapshot_metadata(msg.u.snapshot_channel.key,
896 msg.u.snapshot_channel.pathname,
897 msg.u.snapshot_channel.relayd_id, ctx);
898 if (ret < 0) {
899 ERR("Snapshot metadata failed");
900 ret_code = LTTCOMM_CONSUMERD_ERROR_METADATA;
901 }
902 } else {
903 ret = lttng_kconsumer_snapshot_channel(msg.u.snapshot_channel.key,
904 msg.u.snapshot_channel.pathname,
905 msg.u.snapshot_channel.relayd_id,
906 msg.u.snapshot_channel.nb_packets_per_stream,
907 ctx);
908 if (ret < 0) {
909 ERR("Snapshot channel failed");
910 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
911 }
912 }
913
914 health_code_update();
915
916 ret = consumer_send_status_msg(sock, ret_code);
917 if (ret < 0) {
918 /* Somehow, the session daemon is not responding anymore. */
919 goto end_nosignal;
920 }
921 break;
922 }
923 case LTTNG_CONSUMER_DESTROY_CHANNEL:
924 {
925 uint64_t key = msg.u.destroy_channel.key;
926 struct lttng_consumer_channel *channel;
927
928 channel = consumer_find_channel(key);
929 if (!channel) {
930 ERR("Kernel consumer destroy channel %" PRIu64 " not found", key);
931 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
932 }
933
934 health_code_update();
935
936 ret = consumer_send_status_msg(sock, ret_code);
937 if (ret < 0) {
938 /* Somehow, the session daemon is not responding anymore. */
939 goto end_nosignal;
940 }
941
942 health_code_update();
943
944 /* Stop right now if no channel was found. */
945 if (!channel) {
946 goto end_nosignal;
947 }
948
949 /*
950 * This command should ONLY be issued for channel with streams set in
951 * no monitor mode.
952 */
953 assert(!channel->monitor);
954
955 /*
956 * The refcount should ALWAYS be 0 in the case of a channel in no
957 * monitor mode.
958 */
959 assert(!uatomic_sub_return(&channel->refcount, 1));
960
961 consumer_del_channel(channel);
962
963 goto end_nosignal;
964 }
965 case LTTNG_CONSUMER_DISCARDED_EVENTS:
966 {
967 uint64_t ret;
968 struct lttng_consumer_channel *channel;
969 uint64_t id = msg.u.discarded_events.session_id;
970 uint64_t key = msg.u.discarded_events.channel_key;
971
972 channel = consumer_find_channel(key);
973 if (!channel) {
974 ERR("Kernel consumer discarded events channel %"
975 PRIu64 " not found", key);
976 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
977 }
978
979 DBG("Kernel consumer discarded events command for session id %"
980 PRIu64 ", channel key %" PRIu64, id, key);
981
982 ret = channel->discarded_events;
983
984 health_code_update();
985
986 /* Send back returned value to session daemon */
987 ret = lttcomm_send_unix_sock(sock, &ret, sizeof(ret));
988 if (ret < 0) {
989 PERROR("send discarded events");
990 goto error_fatal;
991 }
992
993 break;
994 }
995 case LTTNG_CONSUMER_LOST_PACKETS:
996 {
997 uint64_t ret;
998 struct lttng_consumer_channel *channel;
999 uint64_t id = msg.u.lost_packets.session_id;
1000 uint64_t key = msg.u.lost_packets.channel_key;
1001
1002 channel = consumer_find_channel(key);
1003 if (!channel) {
1004 ERR("Kernel consumer lost packets channel %"
1005 PRIu64 " not found", key);
1006 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
1007 }
1008
1009 DBG("Kernel consumer lost packets command for session id %"
1010 PRIu64 ", channel key %" PRIu64, id, key);
1011
1012 ret = channel->lost_packets;
1013
1014 health_code_update();
1015
1016 /* Send back returned value to session daemon */
1017 ret = lttcomm_send_unix_sock(sock, &ret, sizeof(ret));
1018 if (ret < 0) {
1019 PERROR("send lost packets");
1020 goto error_fatal;
1021 }
1022
1023 break;
1024 }
1025 default:
1026 goto end_nosignal;
1027 }
1028
1029 end_nosignal:
1030 rcu_read_unlock();
1031
1032 /*
1033 * Return 1 to indicate success since the 0 value can be a socket
1034 * shutdown during the recv() or send() call.
1035 */
1036 health_code_update();
1037 return 1;
1038
1039 error_fatal:
1040 rcu_read_unlock();
1041 /* This will issue a consumer stop. */
1042 return -1;
1043 }
1044
1045 /*
1046 * Populate index values of a kernel stream. Values are set in big endian order.
1047 *
1048 * Return 0 on success or else a negative value.
1049 */
1050 static int get_index_values(struct ctf_packet_index *index, int infd)
1051 {
1052 int ret;
1053
1054 ret = kernctl_get_timestamp_begin(infd, &index->timestamp_begin);
1055 if (ret < 0) {
1056 PERROR("kernctl_get_timestamp_begin");
1057 goto error;
1058 }
1059 index->timestamp_begin = htobe64(index->timestamp_begin);
1060
1061 ret = kernctl_get_timestamp_end(infd, &index->timestamp_end);
1062 if (ret < 0) {
1063 PERROR("kernctl_get_timestamp_end");
1064 goto error;
1065 }
1066 index->timestamp_end = htobe64(index->timestamp_end);
1067
1068 ret = kernctl_get_events_discarded(infd, &index->events_discarded);
1069 if (ret < 0) {
1070 PERROR("kernctl_get_events_discarded");
1071 goto error;
1072 }
1073 index->events_discarded = htobe64(index->events_discarded);
1074
1075 ret = kernctl_get_content_size(infd, &index->content_size);
1076 if (ret < 0) {
1077 PERROR("kernctl_get_content_size");
1078 goto error;
1079 }
1080 index->content_size = htobe64(index->content_size);
1081
1082 ret = kernctl_get_packet_size(infd, &index->packet_size);
1083 if (ret < 0) {
1084 PERROR("kernctl_get_packet_size");
1085 goto error;
1086 }
1087 index->packet_size = htobe64(index->packet_size);
1088
1089 ret = kernctl_get_stream_id(infd, &index->stream_id);
1090 if (ret < 0) {
1091 PERROR("kernctl_get_stream_id");
1092 goto error;
1093 }
1094 index->stream_id = htobe64(index->stream_id);
1095
1096 ret = kernctl_get_instance_id(infd, &index->stream_instance_id);
1097 if (ret < 0) {
1098 PERROR("kernctl_get_instance_id");
1099 goto error;
1100 }
1101 index->stream_instance_id = htobe64(index->stream_instance_id);
1102
1103 ret = kernctl_get_sequence_number(infd, &index->packet_seq_num);
1104 if (ret < 0) {
1105 PERROR("kernctl_get_sequence_number");
1106 goto error;
1107 }
1108 index->packet_seq_num = htobe64(index->packet_seq_num);
1109
1110 error:
1111 return ret;
1112 }
1113 /*
1114 * Sync metadata meaning request them to the session daemon and snapshot to the
1115 * metadata thread can consumer them.
1116 *
1117 * Metadata stream lock MUST be acquired.
1118 *
1119 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
1120 * is empty or a negative value on error.
1121 */
1122 int lttng_kconsumer_sync_metadata(struct lttng_consumer_stream *metadata)
1123 {
1124 int ret;
1125
1126 assert(metadata);
1127
1128 ret = kernctl_buffer_flush(metadata->wait_fd);
1129 if (ret < 0) {
1130 ERR("Failed to flush kernel stream");
1131 goto end;
1132 }
1133
1134 ret = kernctl_snapshot(metadata->wait_fd);
1135 if (ret < 0) {
1136 if (errno != EAGAIN) {
1137 ERR("Sync metadata, taking kernel snapshot failed.");
1138 goto end;
1139 }
1140 DBG("Sync metadata, no new kernel metadata");
1141 /* No new metadata, exit. */
1142 ret = ENODATA;
1143 goto end;
1144 }
1145
1146 end:
1147 return ret;
1148 }
1149
1150 static
1151 int update_stream_stats(struct lttng_consumer_stream *stream)
1152 {
1153 int ret;
1154 uint64_t seq, discarded;
1155
1156 ret = kernctl_get_sequence_number(stream->wait_fd, &seq);
1157 if (ret < 0) {
1158 PERROR("kernctl_get_sequence_number");
1159 goto end;
1160 }
1161
1162 /*
1163 * Start the sequence when we extract the first packet in case we don't
1164 * start at 0 (for example if a consumer is not connected to the
1165 * session immediately after the beginning).
1166 */
1167 if (stream->last_sequence_number == -1ULL) {
1168 stream->last_sequence_number = seq;
1169 } else if (seq > stream->last_sequence_number) {
1170 stream->chan->lost_packets += seq -
1171 stream->last_sequence_number - 1;
1172 } else {
1173 /* seq <= last_sequence_number */
1174 ERR("Sequence number inconsistent : prev = %" PRIu64
1175 ", current = %" PRIu64,
1176 stream->last_sequence_number, seq);
1177 ret = -1;
1178 goto end;
1179 }
1180 stream->last_sequence_number = seq;
1181
1182 ret = kernctl_get_events_discarded(stream->wait_fd, &discarded);
1183 if (ret < 0) {
1184 PERROR("kernctl_get_events_discarded");
1185 goto end;
1186 }
1187 if (discarded < stream->last_discarded_events) {
1188 /*
1189 * Overflow has occured. We assume only one wrap-around
1190 * has occured.
1191 */
1192 stream->chan->discarded_events += (1ULL << (CAA_BITS_PER_LONG - 1)) -
1193 stream->last_discarded_events + discarded;
1194 } else {
1195 stream->chan->discarded_events += discarded -
1196 stream->last_discarded_events;
1197 }
1198 stream->last_discarded_events = discarded;
1199 ret = 0;
1200
1201 end:
1202 return ret;
1203 }
1204
1205 /*
1206 * Check if the local version of the metadata stream matches with the version
1207 * of the metadata stream in the kernel. If it was updated, set the reset flag
1208 * on the stream.
1209 */
1210 static
1211 int metadata_stream_check_version(int infd, struct lttng_consumer_stream *stream)
1212 {
1213 int ret;
1214 uint64_t cur_version;
1215
1216 ret = kernctl_get_metadata_version(infd, &cur_version);
1217 if (ret < 0) {
1218 ERR("Failed to get the metadata version");
1219 goto end;
1220 }
1221
1222 if (stream->metadata_version == cur_version) {
1223 ret = 0;
1224 goto end;
1225 }
1226
1227 DBG("New metadata version detected");
1228 stream->metadata_version = cur_version;
1229 stream->reset_metadata_flag = 1;
1230 ret = 0;
1231
1232 end:
1233 return ret;
1234 }
1235
1236 /*
1237 * Consume data on a file descriptor and write it on a trace file.
1238 */
1239 ssize_t lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream *stream,
1240 struct lttng_consumer_local_data *ctx)
1241 {
1242 unsigned long len, subbuf_size, padding;
1243 int err, write_index = 1;
1244 ssize_t ret = 0;
1245 int infd = stream->wait_fd;
1246 struct ctf_packet_index index;
1247
1248 DBG("In read_subbuffer (infd : %d)", infd);
1249
1250 /* Get the next subbuffer */
1251 err = kernctl_get_next_subbuf(infd);
1252 if (err != 0) {
1253 /*
1254 * This is a debug message even for single-threaded consumer,
1255 * because poll() have more relaxed criterions than get subbuf,
1256 * so get_subbuf may fail for short race windows where poll()
1257 * would issue wakeups.
1258 */
1259 DBG("Reserving sub buffer failed (everything is normal, "
1260 "it is due to concurrency)");
1261 ret = -errno;
1262 goto end;
1263 }
1264
1265 /* Get the full subbuffer size including padding */
1266 err = kernctl_get_padded_subbuf_size(infd, &len);
1267 if (err != 0) {
1268 PERROR("Getting sub-buffer len failed.");
1269 err = kernctl_put_subbuf(infd);
1270 if (err != 0) {
1271 if (errno == EFAULT) {
1272 PERROR("Error in unreserving sub buffer\n");
1273 } else if (errno == EIO) {
1274 /* Should never happen with newer LTTng versions */
1275 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1276 }
1277 ret = -errno;
1278 goto end;
1279 }
1280 ret = -errno;
1281 goto end;
1282 }
1283
1284 if (!stream->metadata_flag) {
1285 ret = get_index_values(&index, infd);
1286 if (ret < 0) {
1287 err = kernctl_put_subbuf(infd);
1288 if (err != 0) {
1289 if (errno == EFAULT) {
1290 PERROR("Error in unreserving sub buffer\n");
1291 } else if (errno == EIO) {
1292 /* Should never happen with newer LTTng versions */
1293 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1294 }
1295 ret = -errno;
1296 goto end;
1297 }
1298 goto end;
1299 }
1300 ret = update_stream_stats(stream);
1301 if (ret < 0) {
1302 goto end;
1303 }
1304 } else {
1305 write_index = 0;
1306 ret = metadata_stream_check_version(infd, stream);
1307 if (ret < 0) {
1308 goto end;
1309 }
1310 }
1311
1312 switch (stream->chan->output) {
1313 case CONSUMER_CHANNEL_SPLICE:
1314 /*
1315 * XXX: The lttng-modules splice "actor" does not handle copying
1316 * partial pages hence only using the subbuffer size without the
1317 * padding makes the splice fail.
1318 */
1319 subbuf_size = len;
1320 padding = 0;
1321
1322 /* splice the subbuffer to the tracefile */
1323 ret = lttng_consumer_on_read_subbuffer_splice(ctx, stream, subbuf_size,
1324 padding, &index);
1325 /*
1326 * XXX: Splice does not support network streaming so the return value
1327 * is simply checked against subbuf_size and not like the mmap() op.
1328 */
1329 if (ret != subbuf_size) {
1330 /*
1331 * display the error but continue processing to try
1332 * to release the subbuffer
1333 */
1334 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
1335 ret, subbuf_size);
1336 write_index = 0;
1337 }
1338 break;
1339 case CONSUMER_CHANNEL_MMAP:
1340 /* Get subbuffer size without padding */
1341 err = kernctl_get_subbuf_size(infd, &subbuf_size);
1342 if (err != 0) {
1343 PERROR("Getting sub-buffer len failed.");
1344 err = kernctl_put_subbuf(infd);
1345 if (err != 0) {
1346 if (errno == EFAULT) {
1347 PERROR("Error in unreserving sub buffer\n");
1348 } else if (errno == EIO) {
1349 /* Should never happen with newer LTTng versions */
1350 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1351 }
1352 ret = -errno;
1353 goto end;
1354 }
1355 ret = -errno;
1356 goto end;
1357 }
1358
1359 /* Make sure the tracer is not gone mad on us! */
1360 assert(len >= subbuf_size);
1361
1362 padding = len - subbuf_size;
1363
1364 /* write the subbuffer to the tracefile */
1365 ret = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, subbuf_size,
1366 padding, &index);
1367 /*
1368 * The mmap operation should write subbuf_size amount of data when
1369 * network streaming or the full padding (len) size when we are _not_
1370 * streaming.
1371 */
1372 if ((ret != subbuf_size && stream->net_seq_idx != (uint64_t) -1ULL) ||
1373 (ret != len && stream->net_seq_idx == (uint64_t) -1ULL)) {
1374 /*
1375 * Display the error but continue processing to try to release the
1376 * subbuffer. This is a DBG statement since this is possible to
1377 * happen without being a critical error.
1378 */
1379 DBG("Error writing to tracefile "
1380 "(ret: %zd != len: %lu != subbuf_size: %lu)",
1381 ret, len, subbuf_size);
1382 write_index = 0;
1383 }
1384 break;
1385 default:
1386 ERR("Unknown output method");
1387 ret = -EPERM;
1388 }
1389
1390 err = kernctl_put_next_subbuf(infd);
1391 if (err != 0) {
1392 if (errno == EFAULT) {
1393 PERROR("Error in unreserving sub buffer\n");
1394 } else if (errno == EIO) {
1395 /* Should never happen with newer LTTng versions */
1396 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1397 }
1398 ret = -errno;
1399 goto end;
1400 }
1401
1402 /* Write index if needed. */
1403 if (!write_index) {
1404 goto end;
1405 }
1406
1407 if (stream->chan->live_timer_interval && !stream->metadata_flag) {
1408 /*
1409 * In live, block until all the metadata is sent.
1410 */
1411 pthread_mutex_lock(&stream->metadata_timer_lock);
1412 assert(!stream->missed_metadata_flush);
1413 stream->waiting_on_metadata = true;
1414 pthread_mutex_unlock(&stream->metadata_timer_lock);
1415
1416 err = consumer_stream_sync_metadata(ctx, stream->session_id);
1417
1418 pthread_mutex_lock(&stream->metadata_timer_lock);
1419 stream->waiting_on_metadata = false;
1420 if (stream->missed_metadata_flush) {
1421 stream->missed_metadata_flush = false;
1422 pthread_mutex_unlock(&stream->metadata_timer_lock);
1423 (void) consumer_flush_kernel_index(stream);
1424 } else {
1425 pthread_mutex_unlock(&stream->metadata_timer_lock);
1426 }
1427 if (err < 0) {
1428 goto end;
1429 }
1430 }
1431
1432 err = consumer_stream_write_index(stream, &index);
1433 if (err < 0) {
1434 goto end;
1435 }
1436
1437 end:
1438 return ret;
1439 }
1440
1441 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream *stream)
1442 {
1443 int ret;
1444
1445 assert(stream);
1446
1447 /*
1448 * Don't create anything if this is set for streaming or should not be
1449 * monitored.
1450 */
1451 if (stream->net_seq_idx == (uint64_t) -1ULL && stream->chan->monitor) {
1452 ret = utils_create_stream_file(stream->chan->pathname, stream->name,
1453 stream->chan->tracefile_size, stream->tracefile_count_current,
1454 stream->uid, stream->gid, NULL);
1455 if (ret < 0) {
1456 goto error;
1457 }
1458 stream->out_fd = ret;
1459 stream->tracefile_size_current = 0;
1460
1461 if (!stream->metadata_flag) {
1462 ret = index_create_file(stream->chan->pathname,
1463 stream->name, stream->uid, stream->gid,
1464 stream->chan->tracefile_size,
1465 stream->tracefile_count_current);
1466 if (ret < 0) {
1467 goto error;
1468 }
1469 stream->index_fd = ret;
1470 }
1471 }
1472
1473 if (stream->output == LTTNG_EVENT_MMAP) {
1474 /* get the len of the mmap region */
1475 unsigned long mmap_len;
1476
1477 ret = kernctl_get_mmap_len(stream->wait_fd, &mmap_len);
1478 if (ret != 0) {
1479 PERROR("kernctl_get_mmap_len");
1480 ret = -errno;
1481 goto error_close_fd;
1482 }
1483 stream->mmap_len = (size_t) mmap_len;
1484
1485 stream->mmap_base = mmap(NULL, stream->mmap_len, PROT_READ,
1486 MAP_PRIVATE, stream->wait_fd, 0);
1487 if (stream->mmap_base == MAP_FAILED) {
1488 PERROR("Error mmaping");
1489 ret = -1;
1490 goto error_close_fd;
1491 }
1492 }
1493
1494 /* we return 0 to let the library handle the FD internally */
1495 return 0;
1496
1497 error_close_fd:
1498 if (stream->out_fd >= 0) {
1499 int err;
1500
1501 err = close(stream->out_fd);
1502 assert(!err);
1503 stream->out_fd = -1;
1504 }
1505 error:
1506 return ret;
1507 }
1508
1509 /*
1510 * Check if data is still being extracted from the buffers for a specific
1511 * stream. Consumer data lock MUST be acquired before calling this function
1512 * and the stream lock.
1513 *
1514 * Return 1 if the traced data are still getting read else 0 meaning that the
1515 * data is available for trace viewer reading.
1516 */
1517 int lttng_kconsumer_data_pending(struct lttng_consumer_stream *stream)
1518 {
1519 int ret;
1520
1521 assert(stream);
1522
1523 if (stream->endpoint_status != CONSUMER_ENDPOINT_ACTIVE) {
1524 ret = 0;
1525 goto end;
1526 }
1527
1528 ret = kernctl_get_next_subbuf(stream->wait_fd);
1529 if (ret == 0) {
1530 /* There is still data so let's put back this subbuffer. */
1531 ret = kernctl_put_subbuf(stream->wait_fd);
1532 assert(ret == 0);
1533 ret = 1; /* Data is pending */
1534 goto end;
1535 }
1536
1537 /* Data is NOT pending and ready to be read. */
1538 ret = 0;
1539
1540 end:
1541 return ret;
1542 }
This page took 0.06112 seconds and 4 git commands to generate.