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