Fix: consumerd: add missing put_subbuf for ust and kernel errors
[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 struct lttng_consumer_channel *channel;
122 struct lttng_consumer_stream *stream;
123
124 DBG("Kernel consumer snapshot channel %" PRIu64, key);
125
126 rcu_read_lock();
127
128 channel = consumer_find_channel(key);
129 if (!channel) {
130 ERR("No channel found for key %" PRIu64, key);
131 ret = -1;
132 goto end;
133 }
134
135 /* Splice is not supported yet for channel snapshot. */
136 if (channel->output != CONSUMER_CHANNEL_MMAP) {
137 ERR("Unsupported output %d", channel->output);
138 ret = -1;
139 goto end;
140 }
141
142 cds_list_for_each_entry(stream, &channel->streams.head, send_node) {
143 /* Are we at a position _before_ the first available packet ? */
144 bool before_first_packet = true;
145 unsigned long consumed_pos, produced_pos;
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 DBG("Kernel consumer discarded events command for session id %"
973 PRIu64 ", channel key %" PRIu64, id, key);
974
975 channel = consumer_find_channel(key);
976 if (!channel) {
977 ERR("Kernel consumer discarded events channel %"
978 PRIu64 " not found", key);
979 ret = 0;
980 } else {
981 ret = channel->discarded_events;
982 }
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 DBG("Kernel consumer lost packets command for session id %"
1003 PRIu64 ", channel key %" PRIu64, id, key);
1004
1005 channel = consumer_find_channel(key);
1006 if (!channel) {
1007 ERR("Kernel consumer lost packets channel %"
1008 PRIu64 " not found", key);
1009 ret = 0;
1010 } else {
1011 ret = channel->lost_packets;
1012 }
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 if (ret == -ENOTTY) {
1099 /* Command not implemented by lttng-modules. */
1100 index->stream_instance_id = -1ULL;
1101 ret = 0;
1102 } else {
1103 PERROR("kernctl_get_instance_id");
1104 goto error;
1105 }
1106 }
1107 index->stream_instance_id = htobe64(index->stream_instance_id);
1108
1109 ret = kernctl_get_sequence_number(infd, &index->packet_seq_num);
1110 if (ret < 0) {
1111 if (ret == -ENOTTY) {
1112 /* Command not implemented by lttng-modules. */
1113 index->packet_seq_num = -1ULL;
1114 ret = 0;
1115 } else {
1116 PERROR("kernctl_get_sequence_number");
1117 goto error;
1118 }
1119 }
1120 index->packet_seq_num = htobe64(index->packet_seq_num);
1121
1122 error:
1123 return ret;
1124 }
1125 /*
1126 * Sync metadata meaning request them to the session daemon and snapshot to the
1127 * metadata thread can consumer them.
1128 *
1129 * Metadata stream lock MUST be acquired.
1130 *
1131 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
1132 * is empty or a negative value on error.
1133 */
1134 int lttng_kconsumer_sync_metadata(struct lttng_consumer_stream *metadata)
1135 {
1136 int ret;
1137
1138 assert(metadata);
1139
1140 ret = kernctl_buffer_flush(metadata->wait_fd);
1141 if (ret < 0) {
1142 ERR("Failed to flush kernel stream");
1143 goto end;
1144 }
1145
1146 ret = kernctl_snapshot(metadata->wait_fd);
1147 if (ret < 0) {
1148 if (errno != EAGAIN) {
1149 ERR("Sync metadata, taking kernel snapshot failed.");
1150 goto end;
1151 }
1152 DBG("Sync metadata, no new kernel metadata");
1153 /* No new metadata, exit. */
1154 ret = ENODATA;
1155 goto end;
1156 }
1157
1158 end:
1159 return ret;
1160 }
1161
1162 static
1163 int update_stream_stats(struct lttng_consumer_stream *stream)
1164 {
1165 int ret;
1166 uint64_t seq, discarded;
1167
1168 ret = kernctl_get_sequence_number(stream->wait_fd, &seq);
1169 if (ret < 0) {
1170 if (ret == -ENOTTY) {
1171 /* Command not implemented by lttng-modules. */
1172 seq = -1ULL;
1173 ret = 0;
1174 } else {
1175 PERROR("kernctl_get_sequence_number");
1176 goto end;
1177 }
1178 }
1179
1180 /*
1181 * Start the sequence when we extract the first packet in case we don't
1182 * start at 0 (for example if a consumer is not connected to the
1183 * session immediately after the beginning).
1184 */
1185 if (stream->last_sequence_number == -1ULL) {
1186 stream->last_sequence_number = seq;
1187 } else if (seq > stream->last_sequence_number) {
1188 stream->chan->lost_packets += seq -
1189 stream->last_sequence_number - 1;
1190 } else {
1191 /* seq <= last_sequence_number */
1192 ERR("Sequence number inconsistent : prev = %" PRIu64
1193 ", current = %" PRIu64,
1194 stream->last_sequence_number, seq);
1195 ret = -1;
1196 goto end;
1197 }
1198 stream->last_sequence_number = seq;
1199
1200 ret = kernctl_get_events_discarded(stream->wait_fd, &discarded);
1201 if (ret < 0) {
1202 PERROR("kernctl_get_events_discarded");
1203 goto end;
1204 }
1205 if (discarded < stream->last_discarded_events) {
1206 /*
1207 * Overflow has occurred. We assume only one wrap-around
1208 * has occurred.
1209 */
1210 stream->chan->discarded_events += (1ULL << (CAA_BITS_PER_LONG - 1)) -
1211 stream->last_discarded_events + discarded;
1212 } else {
1213 stream->chan->discarded_events += discarded -
1214 stream->last_discarded_events;
1215 }
1216 stream->last_discarded_events = discarded;
1217 ret = 0;
1218
1219 end:
1220 return ret;
1221 }
1222
1223 /*
1224 * Check if the local version of the metadata stream matches with the version
1225 * of the metadata stream in the kernel. If it was updated, set the reset flag
1226 * on the stream.
1227 */
1228 static
1229 int metadata_stream_check_version(int infd, struct lttng_consumer_stream *stream)
1230 {
1231 int ret;
1232 uint64_t cur_version;
1233
1234 ret = kernctl_get_metadata_version(infd, &cur_version);
1235 if (ret < 0) {
1236 if (ret == -ENOTTY) {
1237 /*
1238 * LTTng-modules does not implement this
1239 * command.
1240 */
1241 ret = 0;
1242 goto end;
1243 }
1244 ERR("Failed to get the metadata version");
1245 goto end;
1246 }
1247
1248 if (stream->metadata_version == cur_version) {
1249 ret = 0;
1250 goto end;
1251 }
1252
1253 DBG("New metadata version detected");
1254 stream->metadata_version = cur_version;
1255 stream->reset_metadata_flag = 1;
1256 ret = 0;
1257
1258 end:
1259 return ret;
1260 }
1261
1262 /*
1263 * Consume data on a file descriptor and write it on a trace file.
1264 */
1265 ssize_t lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream *stream,
1266 struct lttng_consumer_local_data *ctx)
1267 {
1268 unsigned long len, subbuf_size, padding;
1269 int err, write_index = 1;
1270 ssize_t ret = 0;
1271 int infd = stream->wait_fd;
1272 struct ctf_packet_index index;
1273
1274 DBG("In read_subbuffer (infd : %d)", infd);
1275
1276 /* Get the next subbuffer */
1277 err = kernctl_get_next_subbuf(infd);
1278 if (err != 0) {
1279 /*
1280 * This is a debug message even for single-threaded consumer,
1281 * because poll() have more relaxed criterions than get subbuf,
1282 * so get_subbuf may fail for short race windows where poll()
1283 * would issue wakeups.
1284 */
1285 DBG("Reserving sub buffer failed (everything is normal, "
1286 "it is due to concurrency)");
1287 ret = -errno;
1288 goto end;
1289 }
1290
1291 /* Get the full subbuffer size including padding */
1292 err = kernctl_get_padded_subbuf_size(infd, &len);
1293 if (err != 0) {
1294 PERROR("Getting sub-buffer len failed.");
1295 err = kernctl_put_subbuf(infd);
1296 if (err != 0) {
1297 if (errno == EFAULT) {
1298 PERROR("Error in unreserving sub buffer\n");
1299 } else if (errno == EIO) {
1300 /* Should never happen with newer LTTng versions */
1301 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1302 }
1303 ret = -errno;
1304 goto end;
1305 }
1306 ret = -errno;
1307 goto end;
1308 }
1309
1310 if (!stream->metadata_flag) {
1311 ret = get_index_values(&index, infd);
1312 if (ret < 0) {
1313 err = kernctl_put_subbuf(infd);
1314 if (err != 0) {
1315 if (errno == EFAULT) {
1316 PERROR("Error in unreserving sub buffer\n");
1317 } else if (errno == EIO) {
1318 /* Should never happen with newer LTTng versions */
1319 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1320 }
1321 ret = -errno;
1322 goto end;
1323 }
1324 goto end;
1325 }
1326 ret = update_stream_stats(stream);
1327 if (ret < 0) {
1328 err = kernctl_put_subbuf(infd);
1329 if (err != 0) {
1330 if (err == -EFAULT) {
1331 PERROR("Error in unreserving sub buffer\n");
1332 } else if (err == -EIO) {
1333 /* Should never happen with newer LTTng versions */
1334 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1335 }
1336 ret = err;
1337 goto end;
1338 }
1339 goto end;
1340 }
1341 } else {
1342 write_index = 0;
1343 ret = metadata_stream_check_version(infd, stream);
1344 if (ret < 0) {
1345 err = kernctl_put_subbuf(infd);
1346 if (err != 0) {
1347 if (err == -EFAULT) {
1348 PERROR("Error in unreserving sub buffer\n");
1349 } else if (err == -EIO) {
1350 /* Should never happen with newer LTTng versions */
1351 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1352 }
1353 ret = err;
1354 goto end;
1355 }
1356 goto end;
1357 }
1358 }
1359
1360 switch (stream->chan->output) {
1361 case CONSUMER_CHANNEL_SPLICE:
1362 /*
1363 * XXX: The lttng-modules splice "actor" does not handle copying
1364 * partial pages hence only using the subbuffer size without the
1365 * padding makes the splice fail.
1366 */
1367 subbuf_size = len;
1368 padding = 0;
1369
1370 /* splice the subbuffer to the tracefile */
1371 ret = lttng_consumer_on_read_subbuffer_splice(ctx, stream, subbuf_size,
1372 padding, &index);
1373 /*
1374 * XXX: Splice does not support network streaming so the return value
1375 * is simply checked against subbuf_size and not like the mmap() op.
1376 */
1377 if (ret != subbuf_size) {
1378 /*
1379 * display the error but continue processing to try
1380 * to release the subbuffer
1381 */
1382 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
1383 ret, subbuf_size);
1384 write_index = 0;
1385 }
1386 break;
1387 case CONSUMER_CHANNEL_MMAP:
1388 /* Get subbuffer size without padding */
1389 err = kernctl_get_subbuf_size(infd, &subbuf_size);
1390 if (err != 0) {
1391 PERROR("Getting sub-buffer len failed.");
1392 err = kernctl_put_subbuf(infd);
1393 if (err != 0) {
1394 if (errno == EFAULT) {
1395 PERROR("Error in unreserving sub buffer\n");
1396 } else if (errno == EIO) {
1397 /* Should never happen with newer LTTng versions */
1398 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1399 }
1400 ret = -errno;
1401 goto end;
1402 }
1403 ret = -errno;
1404 goto end;
1405 }
1406
1407 /* Make sure the tracer is not gone mad on us! */
1408 assert(len >= subbuf_size);
1409
1410 padding = len - subbuf_size;
1411
1412 /* write the subbuffer to the tracefile */
1413 ret = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, subbuf_size,
1414 padding, &index);
1415 /*
1416 * The mmap operation should write subbuf_size amount of data when
1417 * network streaming or the full padding (len) size when we are _not_
1418 * streaming.
1419 */
1420 if ((ret != subbuf_size && stream->net_seq_idx != (uint64_t) -1ULL) ||
1421 (ret != len && stream->net_seq_idx == (uint64_t) -1ULL)) {
1422 /*
1423 * Display the error but continue processing to try to release the
1424 * subbuffer. This is a DBG statement since this is possible to
1425 * happen without being a critical error.
1426 */
1427 DBG("Error writing to tracefile "
1428 "(ret: %zd != len: %lu != subbuf_size: %lu)",
1429 ret, len, subbuf_size);
1430 write_index = 0;
1431 }
1432 break;
1433 default:
1434 ERR("Unknown output method");
1435 ret = -EPERM;
1436 }
1437
1438 err = kernctl_put_next_subbuf(infd);
1439 if (err != 0) {
1440 if (errno == EFAULT) {
1441 PERROR("Error in unreserving sub buffer\n");
1442 } else if (errno == EIO) {
1443 /* Should never happen with newer LTTng versions */
1444 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1445 }
1446 ret = -errno;
1447 goto end;
1448 }
1449
1450 /* Write index if needed. */
1451 if (!write_index) {
1452 goto end;
1453 }
1454
1455 if (stream->chan->live_timer_interval && !stream->metadata_flag) {
1456 /*
1457 * In live, block until all the metadata is sent.
1458 */
1459 pthread_mutex_lock(&stream->metadata_timer_lock);
1460 assert(!stream->missed_metadata_flush);
1461 stream->waiting_on_metadata = true;
1462 pthread_mutex_unlock(&stream->metadata_timer_lock);
1463
1464 err = consumer_stream_sync_metadata(ctx, stream->session_id);
1465
1466 pthread_mutex_lock(&stream->metadata_timer_lock);
1467 stream->waiting_on_metadata = false;
1468 if (stream->missed_metadata_flush) {
1469 stream->missed_metadata_flush = false;
1470 pthread_mutex_unlock(&stream->metadata_timer_lock);
1471 (void) consumer_flush_kernel_index(stream);
1472 } else {
1473 pthread_mutex_unlock(&stream->metadata_timer_lock);
1474 }
1475 if (err < 0) {
1476 goto end;
1477 }
1478 }
1479
1480 err = consumer_stream_write_index(stream, &index);
1481 if (err < 0) {
1482 goto end;
1483 }
1484
1485 end:
1486 return ret;
1487 }
1488
1489 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream *stream)
1490 {
1491 int ret;
1492
1493 assert(stream);
1494
1495 /*
1496 * Don't create anything if this is set for streaming or should not be
1497 * monitored.
1498 */
1499 if (stream->net_seq_idx == (uint64_t) -1ULL && stream->chan->monitor) {
1500 ret = utils_create_stream_file(stream->chan->pathname, stream->name,
1501 stream->chan->tracefile_size, stream->tracefile_count_current,
1502 stream->uid, stream->gid, NULL);
1503 if (ret < 0) {
1504 goto error;
1505 }
1506 stream->out_fd = ret;
1507 stream->tracefile_size_current = 0;
1508
1509 if (!stream->metadata_flag) {
1510 struct lttng_index_file *index_file;
1511
1512 index_file = lttng_index_file_create(stream->chan->pathname,
1513 stream->name, stream->uid, stream->gid,
1514 stream->chan->tracefile_size,
1515 stream->tracefile_count_current,
1516 CTF_INDEX_MAJOR, CTF_INDEX_MINOR);
1517 if (!index_file) {
1518 goto error;
1519 }
1520 stream->index_file = index_file;
1521 }
1522 }
1523
1524 if (stream->output == LTTNG_EVENT_MMAP) {
1525 /* get the len of the mmap region */
1526 unsigned long mmap_len;
1527
1528 ret = kernctl_get_mmap_len(stream->wait_fd, &mmap_len);
1529 if (ret != 0) {
1530 PERROR("kernctl_get_mmap_len");
1531 ret = -errno;
1532 goto error_close_fd;
1533 }
1534 stream->mmap_len = (size_t) mmap_len;
1535
1536 stream->mmap_base = mmap(NULL, stream->mmap_len, PROT_READ,
1537 MAP_PRIVATE, stream->wait_fd, 0);
1538 if (stream->mmap_base == MAP_FAILED) {
1539 PERROR("Error mmaping");
1540 ret = -1;
1541 goto error_close_fd;
1542 }
1543 }
1544
1545 /* we return 0 to let the library handle the FD internally */
1546 return 0;
1547
1548 error_close_fd:
1549 if (stream->out_fd >= 0) {
1550 int err;
1551
1552 err = close(stream->out_fd);
1553 assert(!err);
1554 stream->out_fd = -1;
1555 }
1556 error:
1557 return ret;
1558 }
1559
1560 /*
1561 * Check if data is still being extracted from the buffers for a specific
1562 * stream. Consumer data lock MUST be acquired before calling this function
1563 * and the stream lock.
1564 *
1565 * Return 1 if the traced data are still getting read else 0 meaning that the
1566 * data is available for trace viewer reading.
1567 */
1568 int lttng_kconsumer_data_pending(struct lttng_consumer_stream *stream)
1569 {
1570 int ret;
1571
1572 assert(stream);
1573
1574 if (stream->endpoint_status != CONSUMER_ENDPOINT_ACTIVE) {
1575 ret = 0;
1576 goto end;
1577 }
1578
1579 ret = kernctl_get_next_subbuf(stream->wait_fd);
1580 if (ret == 0) {
1581 /* There is still data so let's put back this subbuffer. */
1582 ret = kernctl_put_subbuf(stream->wait_fd);
1583 assert(ret == 0);
1584 ret = 1; /* Data is pending */
1585 goto end;
1586 }
1587
1588 /* Data is NOT pending and ready to be read. */
1589 ret = 0;
1590
1591 end:
1592 return ret;
1593 }
This page took 0.099595 seconds and 4 git commands to generate.