Fix: kernel-consumer: double-close
[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 _GNU_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 <common/common.h>
33 #include <common/kernel-ctl/kernel-ctl.h>
34 #include <common/sessiond-comm/sessiond-comm.h>
35 #include <common/sessiond-comm/relayd.h>
36 #include <common/compat/fcntl.h>
37 #include <common/pipe.h>
38 #include <common/relayd/relayd.h>
39 #include <common/utils.h>
40 #include <common/consumer-stream.h>
41
42 #include "kernel-consumer.h"
43
44 extern struct lttng_consumer_global_data consumer_data;
45 extern int consumer_poll_timeout;
46 extern volatile int consumer_quit;
47
48 /*
49 * Take a snapshot for a specific fd
50 *
51 * Returns 0 on success, < 0 on error
52 */
53 int lttng_kconsumer_take_snapshot(struct lttng_consumer_stream *stream)
54 {
55 int ret = 0;
56 int infd = stream->wait_fd;
57
58 ret = kernctl_snapshot(infd);
59 if (ret != 0) {
60 errno = -ret;
61 perror("Getting sub-buffer snapshot.");
62 }
63
64 return ret;
65 }
66
67 /*
68 * Get the produced position
69 *
70 * Returns 0 on success, < 0 on error
71 */
72 int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream *stream,
73 unsigned long *pos)
74 {
75 int ret;
76 int infd = stream->wait_fd;
77
78 ret = kernctl_snapshot_get_produced(infd, pos);
79 if (ret != 0) {
80 errno = -ret;
81 perror("kernctl_snapshot_get_produced");
82 }
83
84 return ret;
85 }
86
87 /*
88 * Get the consumerd position
89 *
90 * Returns 0 on success, < 0 on error
91 */
92 int lttng_kconsumer_get_consumed_snapshot(struct lttng_consumer_stream *stream,
93 unsigned long *pos)
94 {
95 int ret;
96 int infd = stream->wait_fd;
97
98 ret = kernctl_snapshot_get_consumed(infd, pos);
99 if (ret != 0) {
100 errno = -ret;
101 perror("kernctl_snapshot_get_consumed");
102 }
103
104 return ret;
105 }
106
107 /*
108 * Take a snapshot of all the stream of a channel
109 *
110 * Returns 0 on success, < 0 on error
111 */
112 int lttng_kconsumer_snapshot_channel(uint64_t key, char *path,
113 uint64_t relayd_id, struct lttng_consumer_local_data *ctx)
114 {
115 int ret;
116 unsigned long consumed_pos, produced_pos;
117 struct lttng_consumer_channel *channel;
118 struct lttng_consumer_stream *stream;
119
120 DBG("Kernel consumer snapshot channel %lu", key);
121
122 rcu_read_lock();
123
124 channel = consumer_find_channel(key);
125 if (!channel) {
126 ERR("No channel found for key %lu", key);
127 ret = -1;
128 goto end;
129 }
130
131 /* Splice is not supported yet for channel snapshot. */
132 if (channel->output != CONSUMER_CHANNEL_MMAP) {
133 ERR("Unsupported output %d", channel->output);
134 ret = -1;
135 goto end;
136 }
137
138 cds_list_for_each_entry(stream, &channel->streams.head, send_node) {
139 /*
140 * Lock stream because we are about to change its state.
141 */
142 pthread_mutex_lock(&stream->lock);
143
144 /*
145 * Assign the received relayd ID so we can use it for streaming. The streams
146 * are not visible to anyone so this is OK to change it.
147 */
148 stream->net_seq_idx = relayd_id;
149 channel->relayd_id = relayd_id;
150 if (relayd_id != (uint64_t) -1ULL) {
151 ret = consumer_send_relayd_stream(stream, path);
152 if (ret < 0) {
153 ERR("sending stream to relayd");
154 goto end_unlock;
155 }
156 } else {
157 ret = utils_create_stream_file(path, stream->name,
158 stream->chan->tracefile_size,
159 stream->tracefile_count_current,
160 stream->uid, stream->gid);
161 if (ret < 0) {
162 ERR("utils_create_stream_file");
163 goto end_unlock;
164 }
165
166 stream->out_fd = ret;
167 stream->tracefile_size_current = 0;
168
169 DBG("Kernel consumer snapshot stream %s/%s (%lu)", path,
170 stream->name, stream->key);
171 }
172
173 ret = kernctl_buffer_flush(stream->wait_fd);
174 if (ret < 0) {
175 ERR("Failed to flush kernel stream");
176 goto end_unlock;
177 }
178
179 ret = lttng_kconsumer_take_snapshot(stream);
180 if (ret < 0) {
181 ERR("Taking kernel snapshot");
182 goto end_unlock;
183 }
184
185 ret = lttng_kconsumer_get_produced_snapshot(stream, &produced_pos);
186 if (ret < 0) {
187 ERR("Produced kernel snapshot position");
188 goto end_unlock;
189 }
190
191 ret = lttng_kconsumer_get_consumed_snapshot(stream, &consumed_pos);
192 if (ret < 0) {
193 ERR("Consumerd kernel snapshot position");
194 goto end_unlock;
195 }
196
197 if (stream->max_sb_size == 0) {
198 ret = kernctl_get_max_subbuf_size(stream->wait_fd,
199 &stream->max_sb_size);
200 if (ret < 0) {
201 ERR("Getting kernel max_sb_size");
202 goto end_unlock;
203 }
204 }
205
206 while (consumed_pos < produced_pos) {
207 ssize_t read_len;
208 unsigned long len, padded_len;
209
210 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos);
211
212 ret = kernctl_get_subbuf(stream->wait_fd, &consumed_pos);
213 if (ret < 0) {
214 if (errno != EAGAIN) {
215 PERROR("kernctl_get_subbuf snapshot");
216 goto end_unlock;
217 }
218 DBG("Kernel consumer get subbuf failed. Skipping it.");
219 consumed_pos += stream->max_sb_size;
220 continue;
221 }
222
223 ret = kernctl_get_subbuf_size(stream->wait_fd, &len);
224 if (ret < 0) {
225 ERR("Snapshot kernctl_get_subbuf_size");
226 goto error_put_subbuf;
227 }
228
229 ret = kernctl_get_padded_subbuf_size(stream->wait_fd, &padded_len);
230 if (ret < 0) {
231 ERR("Snapshot kernctl_get_padded_subbuf_size");
232 goto error_put_subbuf;
233 }
234
235 read_len = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, len,
236 padded_len - len);
237 /*
238 * We write the padded len in local tracefiles but the data len
239 * when using a relay. Display the error but continue processing
240 * to try to release the subbuffer.
241 */
242 if (relayd_id != (uint64_t) -1ULL) {
243 if (read_len != len) {
244 ERR("Error sending to the relay (ret: %zd != len: %lu)",
245 read_len, len);
246 }
247 } else {
248 if (read_len != padded_len) {
249 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
250 read_len, padded_len);
251 }
252 }
253
254 ret = kernctl_put_subbuf(stream->wait_fd);
255 if (ret < 0) {
256 ERR("Snapshot kernctl_put_subbuf");
257 goto end_unlock;
258 }
259 consumed_pos += stream->max_sb_size;
260 }
261
262 if (relayd_id == (uint64_t) -1ULL) {
263 if (stream->out_fd >= 0) {
264 ret = close(stream->out_fd);
265 if (ret < 0) {
266 PERROR("Kernel consumer snapshot close out_fd");
267 goto end_unlock;
268 }
269 stream->out_fd = -1;
270 }
271 } else {
272 close_relayd_stream(stream);
273 stream->net_seq_idx = (uint64_t) -1ULL;
274 }
275 pthread_mutex_unlock(&stream->lock);
276 }
277
278 /* All good! */
279 ret = 0;
280 goto end;
281
282 error_put_subbuf:
283 ret = kernctl_put_subbuf(stream->wait_fd);
284 if (ret < 0) {
285 ERR("Snapshot kernctl_put_subbuf error path");
286 }
287 end_unlock:
288 pthread_mutex_unlock(&stream->lock);
289 end:
290 rcu_read_unlock();
291 return ret;
292 }
293
294 /*
295 * Read the whole metadata available for a snapshot.
296 *
297 * Returns 0 on success, < 0 on error
298 */
299 int lttng_kconsumer_snapshot_metadata(uint64_t key, char *path,
300 uint64_t relayd_id, struct lttng_consumer_local_data *ctx)
301 {
302 int ret, use_relayd = 0;
303 ssize_t ret_read;
304 struct lttng_consumer_channel *metadata_channel;
305 struct lttng_consumer_stream *metadata_stream;
306
307 assert(ctx);
308
309 DBG("Kernel consumer snapshot metadata with key %" PRIu64 " at path %s",
310 key, path);
311
312 rcu_read_lock();
313
314 metadata_channel = consumer_find_channel(key);
315 if (!metadata_channel) {
316 ERR("Kernel snapshot metadata not found for key %" PRIu64, key);
317 ret = -1;
318 goto error;
319 }
320
321 metadata_stream = metadata_channel->metadata_stream;
322 assert(metadata_stream);
323
324 /* Flag once that we have a valid relayd for the stream. */
325 if (relayd_id != (uint64_t) -1ULL) {
326 use_relayd = 1;
327 }
328
329 if (use_relayd) {
330 ret = consumer_send_relayd_stream(metadata_stream, path);
331 if (ret < 0) {
332 goto error;
333 }
334 } else {
335 ret = utils_create_stream_file(path, metadata_stream->name,
336 metadata_stream->chan->tracefile_size,
337 metadata_stream->tracefile_count_current,
338 metadata_stream->uid, metadata_stream->gid);
339 if (ret < 0) {
340 goto error;
341 }
342 metadata_stream->out_fd = ret;
343 }
344
345 do {
346 ret_read = lttng_kconsumer_read_subbuffer(metadata_stream, ctx);
347 if (ret_read < 0) {
348 if (ret_read != -EPERM) {
349 ERR("Kernel snapshot reading metadata subbuffer (ret: %ld)",
350 ret_read);
351 goto error;
352 }
353 /* ret_read is negative at this point so we will exit the loop. */
354 continue;
355 }
356 } while (ret_read >= 0);
357
358 if (use_relayd) {
359 close_relayd_stream(metadata_stream);
360 metadata_stream->net_seq_idx = (uint64_t) -1ULL;
361 } else {
362 if (metadata_stream->out_fd >= 0) {
363 ret = close(metadata_stream->out_fd);
364 if (ret < 0) {
365 PERROR("Kernel consumer snapshot metadata close out_fd");
366 /*
367 * Don't go on error here since the snapshot was successful at this
368 * point but somehow the close failed.
369 */
370 }
371 metadata_stream->out_fd = -1;
372 }
373 }
374
375 ret = 0;
376
377 cds_list_del(&metadata_stream->send_node);
378 consumer_stream_destroy(metadata_stream, NULL);
379 metadata_channel->metadata_stream = NULL;
380 error:
381 rcu_read_unlock();
382 return ret;
383 }
384
385 /*
386 * Receive command from session daemon and process it.
387 *
388 * Return 1 on success else a negative value or 0.
389 */
390 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
391 int sock, struct pollfd *consumer_sockpoll)
392 {
393 ssize_t ret;
394 enum lttng_error_code ret_code = LTTNG_OK;
395 struct lttcomm_consumer_msg msg;
396
397 ret = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg));
398 if (ret != sizeof(msg)) {
399 if (ret > 0) {
400 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_CMD);
401 ret = -1;
402 }
403 return ret;
404 }
405 if (msg.cmd_type == LTTNG_CONSUMER_STOP) {
406 /*
407 * Notify the session daemon that the command is completed.
408 *
409 * On transport layer error, the function call will print an error
410 * message so handling the returned code is a bit useless since we
411 * return an error code anyway.
412 */
413 (void) consumer_send_status_msg(sock, ret_code);
414 return -ENOENT;
415 }
416
417 /* relayd needs RCU read-side protection */
418 rcu_read_lock();
419
420 switch (msg.cmd_type) {
421 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET:
422 {
423 /* Session daemon status message are handled in the following call. */
424 ret = consumer_add_relayd_socket(msg.u.relayd_sock.net_index,
425 msg.u.relayd_sock.type, ctx, sock, consumer_sockpoll,
426 &msg.u.relayd_sock.sock, msg.u.relayd_sock.session_id);
427 goto end_nosignal;
428 }
429 case LTTNG_CONSUMER_ADD_CHANNEL:
430 {
431 struct lttng_consumer_channel *new_channel;
432 int ret_recv;
433
434 /* First send a status message before receiving the fds. */
435 ret = consumer_send_status_msg(sock, ret_code);
436 if (ret < 0) {
437 /* Somehow, the session daemon is not responding anymore. */
438 goto error_fatal;
439 }
440 DBG("consumer_add_channel %" PRIu64, msg.u.channel.channel_key);
441 new_channel = consumer_allocate_channel(msg.u.channel.channel_key,
442 msg.u.channel.session_id, msg.u.channel.pathname,
443 msg.u.channel.name, msg.u.channel.uid, msg.u.channel.gid,
444 msg.u.channel.relayd_id, msg.u.channel.output,
445 msg.u.channel.tracefile_size,
446 msg.u.channel.tracefile_count, 0,
447 msg.u.channel.monitor);
448 if (new_channel == NULL) {
449 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
450 goto end_nosignal;
451 }
452 new_channel->nb_init_stream_left = msg.u.channel.nb_init_streams;
453 switch (msg.u.channel.output) {
454 case LTTNG_EVENT_SPLICE:
455 new_channel->output = CONSUMER_CHANNEL_SPLICE;
456 break;
457 case LTTNG_EVENT_MMAP:
458 new_channel->output = CONSUMER_CHANNEL_MMAP;
459 break;
460 default:
461 ERR("Channel output unknown %d", msg.u.channel.output);
462 goto end_nosignal;
463 }
464
465 /* Translate and save channel type. */
466 switch (msg.u.channel.type) {
467 case CONSUMER_CHANNEL_TYPE_DATA:
468 case CONSUMER_CHANNEL_TYPE_METADATA:
469 new_channel->type = msg.u.channel.type;
470 break;
471 default:
472 assert(0);
473 goto end_nosignal;
474 };
475
476 if (ctx->on_recv_channel != NULL) {
477 ret_recv = ctx->on_recv_channel(new_channel);
478 if (ret_recv == 0) {
479 ret = consumer_add_channel(new_channel, ctx);
480 } else if (ret_recv < 0) {
481 goto end_nosignal;
482 }
483 } else {
484 ret = consumer_add_channel(new_channel, ctx);
485 }
486
487 /* If we received an error in add_channel, we need to report it. */
488 if (ret < 0) {
489 ret = consumer_send_status_msg(sock, ret);
490 if (ret < 0) {
491 goto error_fatal;
492 }
493 goto end_nosignal;
494 }
495
496 goto end_nosignal;
497 }
498 case LTTNG_CONSUMER_ADD_STREAM:
499 {
500 int fd;
501 struct lttng_pipe *stream_pipe;
502 struct lttng_consumer_stream *new_stream;
503 struct lttng_consumer_channel *channel;
504 int alloc_ret = 0;
505
506 /*
507 * Get stream's channel reference. Needed when adding the stream to the
508 * global hash table.
509 */
510 channel = consumer_find_channel(msg.u.stream.channel_key);
511 if (!channel) {
512 /*
513 * We could not find the channel. Can happen if cpu hotplug
514 * happens while tearing down.
515 */
516 ERR("Unable to find channel key %" PRIu64, msg.u.stream.channel_key);
517 ret_code = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
518 }
519
520 /* First send a status message before receiving the fds. */
521 ret = consumer_send_status_msg(sock, ret_code);
522 if (ret < 0) {
523 /* Somehow, the session daemon is not responding anymore. */
524 goto error_fatal;
525 }
526 if (ret_code != LTTNG_OK) {
527 /* Channel was not found. */
528 goto end_nosignal;
529 }
530
531 /* Blocking call */
532 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
533 rcu_read_unlock();
534 return -EINTR;
535 }
536
537 /* Get stream file descriptor from socket */
538 ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1);
539 if (ret != sizeof(fd)) {
540 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD);
541 rcu_read_unlock();
542 return ret;
543 }
544
545 /*
546 * Send status code to session daemon only if the recv works. If the
547 * above recv() failed, the session daemon is notified through the
548 * error socket and the teardown is eventually done.
549 */
550 ret = consumer_send_status_msg(sock, ret_code);
551 if (ret < 0) {
552 /* Somehow, the session daemon is not responding anymore. */
553 goto end_nosignal;
554 }
555
556 new_stream = consumer_allocate_stream(channel->key,
557 fd,
558 LTTNG_CONSUMER_ACTIVE_STREAM,
559 channel->name,
560 channel->uid,
561 channel->gid,
562 channel->relayd_id,
563 channel->session_id,
564 msg.u.stream.cpu,
565 &alloc_ret,
566 channel->type,
567 channel->monitor);
568 if (new_stream == NULL) {
569 switch (alloc_ret) {
570 case -ENOMEM:
571 case -EINVAL:
572 default:
573 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
574 break;
575 }
576 goto end_nosignal;
577 }
578
579 new_stream->chan = channel;
580 new_stream->wait_fd = fd;
581 switch (channel->output) {
582 case CONSUMER_CHANNEL_SPLICE:
583 new_stream->output = LTTNG_EVENT_SPLICE;
584 break;
585 case CONSUMER_CHANNEL_MMAP:
586 new_stream->output = LTTNG_EVENT_MMAP;
587 break;
588 default:
589 ERR("Stream output unknown %d", channel->output);
590 goto end_nosignal;
591 }
592
593 /*
594 * We've just assigned the channel to the stream so increment the
595 * refcount right now. We don't need to increment the refcount for
596 * streams in no monitor because we handle manually the cleanup of
597 * those. It is very important to make sure there is NO prior
598 * consumer_del_stream() calls or else the refcount will be unbalanced.
599 */
600 if (channel->monitor) {
601 uatomic_inc(&new_stream->chan->refcount);
602 }
603
604 /*
605 * The buffer flush is done on the session daemon side for the kernel
606 * so no need for the stream "hangup_flush_done" variable to be
607 * tracked. This is important for a kernel stream since we don't rely
608 * on the flush state of the stream to read data. It's not the case for
609 * user space tracing.
610 */
611 new_stream->hangup_flush_done = 0;
612
613 if (ctx->on_recv_stream) {
614 ret = ctx->on_recv_stream(new_stream);
615 if (ret < 0) {
616 consumer_stream_free(new_stream);
617 goto end_nosignal;
618 }
619 }
620
621 if (new_stream->metadata_flag) {
622 channel->metadata_stream = new_stream;
623 }
624
625 /* Do not monitor this stream. */
626 if (!channel->monitor) {
627 DBG("Kernel consumer add stream %s in no monitor mode with "
628 "relayd id %" PRIu64, new_stream->name,
629 new_stream->net_seq_idx);
630 cds_list_add(&new_stream->send_node, &channel->streams.head);
631 break;
632 }
633
634 /* Send stream to relayd if the stream has an ID. */
635 if (new_stream->net_seq_idx != (uint64_t) -1ULL) {
636 ret = consumer_send_relayd_stream(new_stream,
637 new_stream->chan->pathname);
638 if (ret < 0) {
639 consumer_stream_free(new_stream);
640 goto end_nosignal;
641 }
642 }
643
644 /* Get the right pipe where the stream will be sent. */
645 if (new_stream->metadata_flag) {
646 stream_pipe = ctx->consumer_metadata_pipe;
647 } else {
648 stream_pipe = ctx->consumer_data_pipe;
649 }
650
651 ret = lttng_pipe_write(stream_pipe, &new_stream, sizeof(new_stream));
652 if (ret < 0) {
653 ERR("Consumer write %s stream to pipe %d",
654 new_stream->metadata_flag ? "metadata" : "data",
655 lttng_pipe_get_writefd(stream_pipe));
656 consumer_stream_free(new_stream);
657 goto end_nosignal;
658 }
659
660 DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64,
661 new_stream->name, fd, new_stream->relayd_stream_id);
662 break;
663 }
664 case LTTNG_CONSUMER_UPDATE_STREAM:
665 {
666 rcu_read_unlock();
667 return -ENOSYS;
668 }
669 case LTTNG_CONSUMER_DESTROY_RELAYD:
670 {
671 uint64_t index = msg.u.destroy_relayd.net_seq_idx;
672 struct consumer_relayd_sock_pair *relayd;
673
674 DBG("Kernel consumer destroying relayd %" PRIu64, index);
675
676 /* Get relayd reference if exists. */
677 relayd = consumer_find_relayd(index);
678 if (relayd == NULL) {
679 DBG("Unable to find relayd %" PRIu64, index);
680 ret_code = LTTNG_ERR_NO_CONSUMER;
681 }
682
683 /*
684 * Each relayd socket pair has a refcount of stream attached to it
685 * which tells if the relayd is still active or not depending on the
686 * refcount value.
687 *
688 * This will set the destroy flag of the relayd object and destroy it
689 * if the refcount reaches zero when called.
690 *
691 * The destroy can happen either here or when a stream fd hangs up.
692 */
693 if (relayd) {
694 consumer_flag_relayd_for_destroy(relayd);
695 }
696
697 ret = consumer_send_status_msg(sock, ret_code);
698 if (ret < 0) {
699 /* Somehow, the session daemon is not responding anymore. */
700 goto error_fatal;
701 }
702
703 goto end_nosignal;
704 }
705 case LTTNG_CONSUMER_DATA_PENDING:
706 {
707 int32_t ret;
708 uint64_t id = msg.u.data_pending.session_id;
709
710 DBG("Kernel consumer data pending command for id %" PRIu64, id);
711
712 ret = consumer_data_pending(id);
713
714 /* Send back returned value to session daemon */
715 ret = lttcomm_send_unix_sock(sock, &ret, sizeof(ret));
716 if (ret < 0) {
717 PERROR("send data pending ret code");
718 goto error_fatal;
719 }
720
721 /*
722 * No need to send back a status message since the data pending
723 * returned value is the response.
724 */
725 break;
726 }
727 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL:
728 {
729 if (msg.u.snapshot_channel.metadata == 1) {
730 ret = lttng_kconsumer_snapshot_metadata(msg.u.snapshot_channel.key,
731 msg.u.snapshot_channel.pathname,
732 msg.u.snapshot_channel.relayd_id, ctx);
733 if (ret < 0) {
734 ERR("Snapshot metadata failed");
735 ret_code = LTTNG_ERR_KERN_META_FAIL;
736 }
737 } else {
738 ret = lttng_kconsumer_snapshot_channel(msg.u.snapshot_channel.key,
739 msg.u.snapshot_channel.pathname,
740 msg.u.snapshot_channel.relayd_id, ctx);
741 if (ret < 0) {
742 ERR("Snapshot channel failed");
743 ret_code = LTTNG_ERR_KERN_CHAN_FAIL;
744 }
745 }
746
747 ret = consumer_send_status_msg(sock, ret_code);
748 if (ret < 0) {
749 /* Somehow, the session daemon is not responding anymore. */
750 goto end_nosignal;
751 }
752 break;
753 }
754 case LTTNG_CONSUMER_DESTROY_CHANNEL:
755 {
756 uint64_t key = msg.u.destroy_channel.key;
757 struct lttng_consumer_channel *channel;
758
759 channel = consumer_find_channel(key);
760 if (!channel) {
761 ERR("Kernel consumer destroy channel %" PRIu64 " not found", key);
762 ret_code = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
763 }
764
765 ret = consumer_send_status_msg(sock, ret_code);
766 if (ret < 0) {
767 /* Somehow, the session daemon is not responding anymore. */
768 goto end_nosignal;
769 }
770
771 /*
772 * This command should ONLY be issued for channel with streams set in
773 * no monitor mode.
774 */
775 assert(!channel->monitor);
776
777 /*
778 * The refcount should ALWAYS be 0 in the case of a channel in no
779 * monitor mode.
780 */
781 assert(!uatomic_sub_return(&channel->refcount, 1));
782
783 consumer_del_channel(channel);
784
785 goto end_nosignal;
786 }
787 default:
788 goto end_nosignal;
789 }
790
791 end_nosignal:
792 rcu_read_unlock();
793
794 /*
795 * Return 1 to indicate success since the 0 value can be a socket
796 * shutdown during the recv() or send() call.
797 */
798 return 1;
799
800 error_fatal:
801 rcu_read_unlock();
802 /* This will issue a consumer stop. */
803 return -1;
804 }
805
806 /*
807 * Consume data on a file descriptor and write it on a trace file.
808 */
809 ssize_t lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream *stream,
810 struct lttng_consumer_local_data *ctx)
811 {
812 unsigned long len, subbuf_size, padding;
813 int err;
814 ssize_t ret = 0;
815 int infd = stream->wait_fd;
816
817 DBG("In read_subbuffer (infd : %d)", infd);
818 /* Get the next subbuffer */
819 err = kernctl_get_next_subbuf(infd);
820 if (err != 0) {
821 ret = err;
822 /*
823 * This is a debug message even for single-threaded consumer,
824 * because poll() have more relaxed criterions than get subbuf,
825 * so get_subbuf may fail for short race windows where poll()
826 * would issue wakeups.
827 */
828 DBG("Reserving sub buffer failed (everything is normal, "
829 "it is due to concurrency)");
830 goto end;
831 }
832
833 /* Get the full subbuffer size including padding */
834 err = kernctl_get_padded_subbuf_size(infd, &len);
835 if (err != 0) {
836 errno = -err;
837 perror("Getting sub-buffer len failed.");
838 ret = err;
839 goto end;
840 }
841
842 switch (stream->chan->output) {
843 case CONSUMER_CHANNEL_SPLICE:
844 /*
845 * XXX: The lttng-modules splice "actor" does not handle copying
846 * partial pages hence only using the subbuffer size without the
847 * padding makes the splice fail.
848 */
849 subbuf_size = len;
850 padding = 0;
851
852 /* splice the subbuffer to the tracefile */
853 ret = lttng_consumer_on_read_subbuffer_splice(ctx, stream, subbuf_size,
854 padding);
855 /*
856 * XXX: Splice does not support network streaming so the return value
857 * is simply checked against subbuf_size and not like the mmap() op.
858 */
859 if (ret != subbuf_size) {
860 /*
861 * display the error but continue processing to try
862 * to release the subbuffer
863 */
864 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
865 ret, subbuf_size);
866 }
867 break;
868 case CONSUMER_CHANNEL_MMAP:
869 /* Get subbuffer size without padding */
870 err = kernctl_get_subbuf_size(infd, &subbuf_size);
871 if (err != 0) {
872 errno = -err;
873 perror("Getting sub-buffer len failed.");
874 ret = err;
875 goto end;
876 }
877
878 /* Make sure the tracer is not gone mad on us! */
879 assert(len >= subbuf_size);
880
881 padding = len - subbuf_size;
882
883 /* write the subbuffer to the tracefile */
884 ret = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, subbuf_size,
885 padding);
886 /*
887 * The mmap operation should write subbuf_size amount of data when
888 * network streaming or the full padding (len) size when we are _not_
889 * streaming.
890 */
891 if ((ret != subbuf_size && stream->net_seq_idx != (uint64_t) -1ULL) ||
892 (ret != len && stream->net_seq_idx == (uint64_t) -1ULL)) {
893 /*
894 * Display the error but continue processing to try to release the
895 * subbuffer
896 */
897 ERR("Error writing to tracefile "
898 "(ret: %zd != len: %lu != subbuf_size: %lu)",
899 ret, len, subbuf_size);
900 }
901 break;
902 default:
903 ERR("Unknown output method");
904 ret = -1;
905 }
906
907 err = kernctl_put_next_subbuf(infd);
908 if (err != 0) {
909 errno = -err;
910 if (errno == EFAULT) {
911 perror("Error in unreserving sub buffer\n");
912 } else if (errno == EIO) {
913 /* Should never happen with newer LTTng versions */
914 perror("Reader has been pushed by the writer, last sub-buffer corrupted.");
915 }
916
917 ret = -err;
918 goto end;
919 }
920
921 end:
922 return ret;
923 }
924
925 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream *stream)
926 {
927 int ret;
928
929 assert(stream);
930
931 /*
932 * Don't create anything if this is set for streaming or should not be
933 * monitored.
934 */
935 if (stream->net_seq_idx == (uint64_t) -1ULL && stream->chan->monitor) {
936 ret = utils_create_stream_file(stream->chan->pathname, stream->name,
937 stream->chan->tracefile_size, stream->tracefile_count_current,
938 stream->uid, stream->gid);
939 if (ret < 0) {
940 goto error;
941 }
942 stream->out_fd = ret;
943 stream->tracefile_size_current = 0;
944 }
945
946 if (stream->output == LTTNG_EVENT_MMAP) {
947 /* get the len of the mmap region */
948 unsigned long mmap_len;
949
950 ret = kernctl_get_mmap_len(stream->wait_fd, &mmap_len);
951 if (ret != 0) {
952 errno = -ret;
953 PERROR("kernctl_get_mmap_len");
954 goto error_close_fd;
955 }
956 stream->mmap_len = (size_t) mmap_len;
957
958 stream->mmap_base = mmap(NULL, stream->mmap_len, PROT_READ,
959 MAP_PRIVATE, stream->wait_fd, 0);
960 if (stream->mmap_base == MAP_FAILED) {
961 PERROR("Error mmaping");
962 ret = -1;
963 goto error_close_fd;
964 }
965 }
966
967 /* we return 0 to let the library handle the FD internally */
968 return 0;
969
970 error_close_fd:
971 if (stream->out_fd >= 0) {
972 int err;
973
974 err = close(stream->out_fd);
975 assert(!err);
976 stream->out_fd = -1;
977 }
978 error:
979 return ret;
980 }
981
982 /*
983 * Check if data is still being extracted from the buffers for a specific
984 * stream. Consumer data lock MUST be acquired before calling this function
985 * and the stream lock.
986 *
987 * Return 1 if the traced data are still getting read else 0 meaning that the
988 * data is available for trace viewer reading.
989 */
990 int lttng_kconsumer_data_pending(struct lttng_consumer_stream *stream)
991 {
992 int ret;
993
994 assert(stream);
995
996 ret = kernctl_get_next_subbuf(stream->wait_fd);
997 if (ret == 0) {
998 /* There is still data so let's put back this subbuffer. */
999 ret = kernctl_put_subbuf(stream->wait_fd);
1000 assert(ret == 0);
1001 ret = 1; /* Data is pending */
1002 goto end;
1003 }
1004
1005 /* Data is NOT pending and ready to be read. */
1006 ret = 0;
1007
1008 end:
1009 return ret;
1010 }
This page took 0.077294 seconds and 5 git commands to generate.