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