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