Fix: kernel-consumer: double-close
[lttng-tools.git] / src / common / kernel-consumer / kernel-consumer.c
CommitLineData
3bd1e081
MD
1/*
2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
d14d33bf
AM
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.
3bd1e081
MD
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 *
d14d33bf
AM
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.
3bd1e081
MD
17 */
18
19#define _GNU_SOURCE
20#include <assert.h>
3bd1e081
MD
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>
77c7c900 28#include <inttypes.h>
3bd1e081 29#include <unistd.h>
dbb5dfe6 30#include <sys/stat.h>
3bd1e081 31
990570ed 32#include <common/common.h>
10a8a223 33#include <common/kernel-ctl/kernel-ctl.h>
10a8a223 34#include <common/sessiond-comm/sessiond-comm.h>
00e2e675 35#include <common/sessiond-comm/relayd.h>
dbb5dfe6 36#include <common/compat/fcntl.h>
acdb9057 37#include <common/pipe.h>
00e2e675 38#include <common/relayd/relayd.h>
fe4477ee 39#include <common/utils.h>
07b86b52 40#include <common/consumer-stream.h>
0857097f 41
10a8a223 42#include "kernel-consumer.h"
3bd1e081
MD
43
44extern struct lttng_consumer_global_data consumer_data;
45extern int consumer_poll_timeout;
46extern volatile int consumer_quit;
47
3bd1e081
MD
48/*
49 * Take a snapshot for a specific fd
50 *
51 * Returns 0 on success, < 0 on error
52 */
ffe60014 53int lttng_kconsumer_take_snapshot(struct lttng_consumer_stream *stream)
3bd1e081
MD
54{
55 int ret = 0;
56 int infd = stream->wait_fd;
57
58 ret = kernctl_snapshot(infd);
59 if (ret != 0) {
87dc6a9c 60 errno = -ret;
3bd1e081
MD
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 */
ffe60014 72int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream *stream,
3bd1e081
MD
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) {
87dc6a9c 80 errno = -ret;
3bd1e081
MD
81 perror("kernctl_snapshot_get_produced");
82 }
83
84 return ret;
85}
86
07b86b52
JD
87/*
88 * Get the consumerd position
89 *
90 * Returns 0 on success, < 0 on error
91 */
92int 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
07b86b52
JD
107/*
108 * Take a snapshot of all the stream of a channel
109 *
110 * Returns 0 on success, < 0 on error
111 */
112int 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
10a50311 138 cds_list_for_each_entry(stream, &channel->streams.head, send_node) {
07b86b52
JD
139 /*
140 * Lock stream because we are about to change its state.
141 */
142 pthread_mutex_lock(&stream->lock);
143
29decac3
DG
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 */
07b86b52
JD
148 stream->net_seq_idx = relayd_id;
149 channel->relayd_id = relayd_id;
150 if (relayd_id != (uint64_t) -1ULL) {
10a50311 151 ret = consumer_send_relayd_stream(stream, path);
07b86b52
JD
152 if (ret < 0) {
153 ERR("sending stream to relayd");
154 goto end_unlock;
155 }
07b86b52
JD
156 } else {
157 ret = utils_create_stream_file(path, stream->name,
10a50311
JD
158 stream->chan->tracefile_size,
159 stream->tracefile_count_current,
07b86b52
JD
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) {
10a50311 175 ERR("Failed to flush kernel stream");
07b86b52
JD
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");
29decac3 226 goto error_put_subbuf;
07b86b52
JD
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");
29decac3 232 goto error_put_subbuf;
07b86b52
JD
233 }
234
235 read_len = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, len,
236 padded_len - len);
237 /*
29decac3
DG
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.
07b86b52
JD
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) {
fdf9986c
MD
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;
07b86b52 270 }
07b86b52
JD
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
29decac3
DG
282error_put_subbuf:
283 ret = kernctl_put_subbuf(stream->wait_fd);
284 if (ret < 0) {
285 ERR("Snapshot kernctl_put_subbuf error path");
286 }
07b86b52
JD
287end_unlock:
288 pthread_mutex_unlock(&stream->lock);
289end:
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 */
299int lttng_kconsumer_snapshot_metadata(uint64_t key, char *path,
e2039c7a 300 uint64_t relayd_id, struct lttng_consumer_local_data *ctx)
07b86b52 301{
d771f832
DG
302 int ret, use_relayd = 0;
303 ssize_t ret_read;
07b86b52
JD
304 struct lttng_consumer_channel *metadata_channel;
305 struct lttng_consumer_stream *metadata_stream;
d771f832
DG
306
307 assert(ctx);
07b86b52
JD
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) {
d771f832 316 ERR("Kernel snapshot metadata not found for key %" PRIu64, key);
07b86b52 317 ret = -1;
d771f832 318 goto error;
07b86b52
JD
319 }
320
321 metadata_stream = metadata_channel->metadata_stream;
322 assert(metadata_stream);
323
d771f832 324 /* Flag once that we have a valid relayd for the stream. */
e2039c7a 325 if (relayd_id != (uint64_t) -1ULL) {
d771f832
DG
326 use_relayd = 1;
327 }
328
329 if (use_relayd) {
10a50311 330 ret = consumer_send_relayd_stream(metadata_stream, path);
e2039c7a 331 if (ret < 0) {
d771f832 332 goto error;
e2039c7a 333 }
e2039c7a
JD
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) {
d771f832 340 goto error;
e2039c7a
JD
341 }
342 metadata_stream->out_fd = ret;
07b86b52 343 }
07b86b52 344
d771f832
DG
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;
07b86b52 352 }
d771f832 353 /* ret_read is negative at this point so we will exit the loop. */
07b86b52
JD
354 continue;
355 }
d771f832 356 } while (ret_read >= 0);
07b86b52 357
d771f832
DG
358 if (use_relayd) {
359 close_relayd_stream(metadata_stream);
360 metadata_stream->net_seq_idx = (uint64_t) -1ULL;
361 } else {
fdf9986c
MD
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;
e2039c7a 372 }
e2039c7a
JD
373 }
374
07b86b52 375 ret = 0;
d771f832 376
cf53a8a6
JD
377 cds_list_del(&metadata_stream->send_node);
378 consumer_stream_destroy(metadata_stream, NULL);
379 metadata_channel->metadata_stream = NULL;
d771f832 380error:
07b86b52
JD
381 rcu_read_unlock();
382 return ret;
383}
384
1803a064
MD
385/*
386 * Receive command from session daemon and process it.
387 *
388 * Return 1 on success else a negative value or 0.
389 */
3bd1e081
MD
390int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
391 int sock, struct pollfd *consumer_sockpoll)
392{
393 ssize_t ret;
f50f23d9 394 enum lttng_error_code ret_code = LTTNG_OK;
3bd1e081
MD
395 struct lttcomm_consumer_msg msg;
396
397 ret = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg));
398 if (ret != sizeof(msg)) {
1803a064 399 if (ret > 0) {
c6857fcf 400 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_CMD);
1803a064
MD
401 ret = -1;
402 }
3bd1e081
MD
403 return ret;
404 }
405 if (msg.cmd_type == LTTNG_CONSUMER_STOP) {
f50f23d9
DG
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);
3bd1e081
MD
414 return -ENOENT;
415 }
416
b0b335c8
MD
417 /* relayd needs RCU read-side protection */
418 rcu_read_lock();
419
3bd1e081 420 switch (msg.cmd_type) {
00e2e675
DG
421 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET:
422 {
f50f23d9 423 /* Session daemon status message are handled in the following call. */
7735ef9e
DG
424 ret = consumer_add_relayd_socket(msg.u.relayd_sock.net_index,
425 msg.u.relayd_sock.type, ctx, sock, consumer_sockpoll,
46e6455f 426 &msg.u.relayd_sock.sock, msg.u.relayd_sock.session_id);
00e2e675
DG
427 goto end_nosignal;
428 }
3bd1e081
MD
429 case LTTNG_CONSUMER_ADD_CHANNEL:
430 {
431 struct lttng_consumer_channel *new_channel;
e43c41c5 432 int ret_recv;
3bd1e081 433
f50f23d9
DG
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. */
1803a064 438 goto error_fatal;
f50f23d9 439 }
d88aee68 440 DBG("consumer_add_channel %" PRIu64, msg.u.channel.channel_key);
3bd1e081 441 new_channel = consumer_allocate_channel(msg.u.channel.channel_key,
ffe60014
DG
442 msg.u.channel.session_id, msg.u.channel.pathname,
443 msg.u.channel.name, msg.u.channel.uid, msg.u.channel.gid,
1624d5b7
JD
444 msg.u.channel.relayd_id, msg.u.channel.output,
445 msg.u.channel.tracefile_size,
1950109e 446 msg.u.channel.tracefile_count, 0,
2bba9e53 447 msg.u.channel.monitor);
3bd1e081 448 if (new_channel == NULL) {
f73fabfd 449 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
3bd1e081
MD
450 goto end_nosignal;
451 }
ffe60014 452 new_channel->nb_init_stream_left = msg.u.channel.nb_init_streams;
95a1109b
JD
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 }
ffe60014
DG
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
3bd1e081 476 if (ctx->on_recv_channel != NULL) {
e43c41c5
JD
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) {
3bd1e081
MD
481 goto end_nosignal;
482 }
483 } else {
e43c41c5 484 ret = consumer_add_channel(new_channel, ctx);
3bd1e081 485 }
e43c41c5
JD
486
487 /* If we received an error in add_channel, we need to report it. */
821fffb2 488 if (ret < 0) {
1803a064
MD
489 ret = consumer_send_status_msg(sock, ret);
490 if (ret < 0) {
491 goto error_fatal;
492 }
e43c41c5
JD
493 goto end_nosignal;
494 }
495
3bd1e081
MD
496 goto end_nosignal;
497 }
498 case LTTNG_CONSUMER_ADD_STREAM:
499 {
dae10966
DG
500 int fd;
501 struct lttng_pipe *stream_pipe;
00e2e675 502 struct lttng_consumer_stream *new_stream;
ffe60014 503 struct lttng_consumer_channel *channel;
c80048c6 504 int alloc_ret = 0;
3bd1e081 505
ffe60014
DG
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 */
d88aee68 516 ERR("Unable to find channel key %" PRIu64, msg.u.stream.channel_key);
ffe60014
DG
517 ret_code = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
518 }
519
f50f23d9
DG
520 /* First send a status message before receiving the fds. */
521 ret = consumer_send_status_msg(sock, ret_code);
1803a064 522 if (ret < 0) {
d771f832 523 /* Somehow, the session daemon is not responding anymore. */
1803a064
MD
524 goto error_fatal;
525 }
526 if (ret_code != LTTNG_OK) {
d771f832 527 /* Channel was not found. */
f50f23d9
DG
528 goto end_nosignal;
529 }
530
d771f832 531 /* Blocking call */
3bd1e081 532 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
3f8e211f 533 rcu_read_unlock();
3bd1e081
MD
534 return -EINTR;
535 }
00e2e675
DG
536
537 /* Get stream file descriptor from socket */
f2fc6720
MD
538 ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1);
539 if (ret != sizeof(fd)) {
f73fabfd 540 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD);
3f8e211f 541 rcu_read_unlock();
3bd1e081
MD
542 return ret;
543 }
3bd1e081 544
f50f23d9
DG
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
ffe60014
DG
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,
4891ece8
DG
566 channel->type,
567 channel->monitor);
3bd1e081 568 if (new_stream == NULL) {
c80048c6
MD
569 switch (alloc_ret) {
570 case -ENOMEM:
571 case -EINVAL:
572 default:
573 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
574 break;
c80048c6 575 }
3f8e211f 576 goto end_nosignal;
3bd1e081 577 }
d771f832 578
ffe60014
DG
579 new_stream->chan = channel;
580 new_stream->wait_fd = fd;
07b86b52
JD
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 }
00e2e675 592
a0c83db9
DG
593 /*
594 * We've just assigned the channel to the stream so increment the
07b86b52
JD
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.
a0c83db9 599 */
07b86b52
JD
600 if (channel->monitor) {
601 uatomic_inc(&new_stream->chan->refcount);
602 }
9d9353f9 603
fb3a43a9
DG
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
633d0084
DG
613 if (ctx->on_recv_stream) {
614 ret = ctx->on_recv_stream(new_stream);
615 if (ret < 0) {
d771f832 616 consumer_stream_free(new_stream);
633d0084 617 goto end_nosignal;
fb3a43a9 618 }
633d0084 619 }
fb3a43a9 620
07b86b52
JD
621 if (new_stream->metadata_flag) {
622 channel->metadata_stream = new_stream;
623 }
624
2bba9e53
DG
625 /* Do not monitor this stream. */
626 if (!channel->monitor) {
5eecee74 627 DBG("Kernel consumer add stream %s in no monitor mode with "
6dc3064a 628 "relayd id %" PRIu64, new_stream->name,
5eecee74 629 new_stream->net_seq_idx);
10a50311 630 cds_list_add(&new_stream->send_node, &channel->streams.head);
6dc3064a
DG
631 break;
632 }
633
e1b71bdc
DG
634 /* Send stream to relayd if the stream has an ID. */
635 if (new_stream->net_seq_idx != (uint64_t) -1ULL) {
194ee077
DG
636 ret = consumer_send_relayd_stream(new_stream,
637 new_stream->chan->pathname);
e1b71bdc
DG
638 if (ret < 0) {
639 consumer_stream_free(new_stream);
640 goto end_nosignal;
641 }
e2039c7a
JD
642 }
643
50f8ae69 644 /* Get the right pipe where the stream will be sent. */
633d0084 645 if (new_stream->metadata_flag) {
dae10966 646 stream_pipe = ctx->consumer_metadata_pipe;
3bd1e081 647 } else {
dae10966 648 stream_pipe = ctx->consumer_data_pipe;
50f8ae69
DG
649 }
650
dae10966 651 ret = lttng_pipe_write(stream_pipe, &new_stream, sizeof(new_stream));
50f8ae69 652 if (ret < 0) {
dae10966 653 ERR("Consumer write %s stream to pipe %d",
50f8ae69 654 new_stream->metadata_flag ? "metadata" : "data",
dae10966 655 lttng_pipe_get_writefd(stream_pipe));
d771f832 656 consumer_stream_free(new_stream);
50f8ae69 657 goto end_nosignal;
3bd1e081 658 }
00e2e675 659
50f8ae69 660 DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64,
ffe60014 661 new_stream->name, fd, new_stream->relayd_stream_id);
3bd1e081
MD
662 break;
663 }
664 case LTTNG_CONSUMER_UPDATE_STREAM:
665 {
3f8e211f
DG
666 rcu_read_unlock();
667 return -ENOSYS;
668 }
669 case LTTNG_CONSUMER_DESTROY_RELAYD:
670 {
a6ba4fe1 671 uint64_t index = msg.u.destroy_relayd.net_seq_idx;
3f8e211f
DG
672 struct consumer_relayd_sock_pair *relayd;
673
a6ba4fe1 674 DBG("Kernel consumer destroying relayd %" PRIu64, index);
3f8e211f
DG
675
676 /* Get relayd reference if exists. */
a6ba4fe1 677 relayd = consumer_find_relayd(index);
3f8e211f 678 if (relayd == NULL) {
3448e266 679 DBG("Unable to find relayd %" PRIu64, index);
f50f23d9 680 ret_code = LTTNG_ERR_NO_CONSUMER;
3bd1e081 681 }
3f8e211f 682
a6ba4fe1
DG
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 */
f50f23d9
DG
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. */
1803a064 700 goto error_fatal;
f50f23d9 701 }
3f8e211f 702
3f8e211f 703 goto end_nosignal;
3bd1e081 704 }
6d805429 705 case LTTNG_CONSUMER_DATA_PENDING:
53632229 706 {
c8f59ee5 707 int32_t ret;
6d805429 708 uint64_t id = msg.u.data_pending.session_id;
c8f59ee5 709
6d805429 710 DBG("Kernel consumer data pending command for id %" PRIu64, id);
c8f59ee5 711
6d805429 712 ret = consumer_data_pending(id);
c8f59ee5
DG
713
714 /* Send back returned value to session daemon */
715 ret = lttcomm_send_unix_sock(sock, &ret, sizeof(ret));
716 if (ret < 0) {
6d805429 717 PERROR("send data pending ret code");
1803a064 718 goto error_fatal;
c8f59ee5 719 }
f50f23d9
DG
720
721 /*
722 * No need to send back a status message since the data pending
723 * returned value is the response.
724 */
c8f59ee5 725 break;
53632229 726 }
6dc3064a
DG
727 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL:
728 {
07b86b52
JD
729 if (msg.u.snapshot_channel.metadata == 1) {
730 ret = lttng_kconsumer_snapshot_metadata(msg.u.snapshot_channel.key,
e2039c7a
JD
731 msg.u.snapshot_channel.pathname,
732 msg.u.snapshot_channel.relayd_id, ctx);
07b86b52
JD
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
6dc3064a
DG
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 }
07b86b52
JD
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 }
3bd1e081 787 default:
3f8e211f 788 goto end_nosignal;
3bd1e081 789 }
3f8e211f 790
3bd1e081 791end_nosignal:
b0b335c8 792 rcu_read_unlock();
4cbc1a04
DG
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;
1803a064
MD
799
800error_fatal:
801 rcu_read_unlock();
802 /* This will issue a consumer stop. */
803 return -1;
3bd1e081 804}
d41f73b7
MD
805
806/*
807 * Consume data on a file descriptor and write it on a trace file.
808 */
4078b776 809ssize_t lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream *stream,
d41f73b7
MD
810 struct lttng_consumer_local_data *ctx)
811{
1d4dfdef 812 unsigned long len, subbuf_size, padding;
d41f73b7 813 int err;
4078b776 814 ssize_t ret = 0;
d41f73b7
MD
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) {
1d4dfdef 821 ret = err;
d41f73b7
MD
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
1d4dfdef
DG
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
ffe60014 842 switch (stream->chan->output) {
07b86b52 843 case CONSUMER_CHANNEL_SPLICE:
1d4dfdef
DG
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 */
91dfef6e
DG
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 */
1d4dfdef
DG
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;
07b86b52 868 case CONSUMER_CHANNEL_MMAP:
1d4dfdef
DG
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 }
47e81c02 877
1d4dfdef
DG
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 */
91dfef6e
DG
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 */
d88aee68
DG
891 if ((ret != subbuf_size && stream->net_seq_idx != (uint64_t) -1ULL) ||
892 (ret != len && stream->net_seq_idx == (uint64_t) -1ULL)) {
1d4dfdef 893 /*
91dfef6e
DG
894 * Display the error but continue processing to try to release the
895 * subbuffer
1d4dfdef 896 */
91dfef6e
DG
897 ERR("Error writing to tracefile "
898 "(ret: %zd != len: %lu != subbuf_size: %lu)",
899 ret, len, subbuf_size);
1d4dfdef
DG
900 }
901 break;
902 default:
903 ERR("Unknown output method");
904 ret = -1;
d41f73b7
MD
905 }
906
907 err = kernctl_put_next_subbuf(infd);
908 if (err != 0) {
21073eaa 909 errno = -err;
d41f73b7
MD
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 }
21073eaa
DG
916
917 ret = -err;
d41f73b7
MD
918 goto end;
919 }
920
921end:
922 return ret;
923}
924
925int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream *stream)
926{
927 int ret;
ffe60014
DG
928
929 assert(stream);
930
2bba9e53
DG
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) {
fe4477ee
JD
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;
ffe60014 944 }
d41f73b7 945
d41f73b7
MD
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) {
87dc6a9c 952 errno = -ret;
ffe60014 953 PERROR("kernctl_get_mmap_len");
d41f73b7
MD
954 goto error_close_fd;
955 }
956 stream->mmap_len = (size_t) mmap_len;
957
ffe60014
DG
958 stream->mmap_base = mmap(NULL, stream->mmap_len, PROT_READ,
959 MAP_PRIVATE, stream->wait_fd, 0);
d41f73b7 960 if (stream->mmap_base == MAP_FAILED) {
ffe60014 961 PERROR("Error mmaping");
d41f73b7
MD
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
970error_close_fd:
2f225ce2 971 if (stream->out_fd >= 0) {
d41f73b7
MD
972 int err;
973
974 err = close(stream->out_fd);
975 assert(!err);
2f225ce2 976 stream->out_fd = -1;
d41f73b7
MD
977 }
978error:
979 return ret;
980}
981
ca22feea
DG
982/*
983 * Check if data is still being extracted from the buffers for a specific
4e9a4686
DG
984 * stream. Consumer data lock MUST be acquired before calling this function
985 * and the stream lock.
ca22feea 986 *
6d805429 987 * Return 1 if the traced data are still getting read else 0 meaning that the
ca22feea
DG
988 * data is available for trace viewer reading.
989 */
6d805429 990int lttng_kconsumer_data_pending(struct lttng_consumer_stream *stream)
ca22feea
DG
991{
992 int ret;
993
994 assert(stream);
995
ca22feea
DG
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);
6d805429 1001 ret = 1; /* Data is pending */
4e9a4686 1002 goto end;
ca22feea
DG
1003 }
1004
6d805429
DG
1005 /* Data is NOT pending and ready to be read. */
1006 ret = 0;
ca22feea 1007
6efae65e
DG
1008end:
1009 return ret;
ca22feea 1010}
This page took 0.086871 seconds and 4 git commands to generate.