Fix: create/destroy a splice_pipe per stream
[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
51a9e1c7 32#include <bin/lttng-consumerd/health-consumerd.h>
990570ed 33#include <common/common.h>
10a8a223 34#include <common/kernel-ctl/kernel-ctl.h>
10a8a223 35#include <common/sessiond-comm/sessiond-comm.h>
00e2e675 36#include <common/sessiond-comm/relayd.h>
dbb5dfe6 37#include <common/compat/fcntl.h>
acdb9057 38#include <common/pipe.h>
00e2e675 39#include <common/relayd/relayd.h>
fe4477ee 40#include <common/utils.h>
07b86b52 41#include <common/consumer-stream.h>
309167d2 42#include <common/index/index.h>
d3e2ba59 43#include <common/consumer-timer.h>
0857097f 44
10a8a223 45#include "kernel-consumer.h"
3bd1e081
MD
46
47extern struct lttng_consumer_global_data consumer_data;
48extern int consumer_poll_timeout;
49extern volatile int consumer_quit;
50
3bd1e081
MD
51/*
52 * Take a snapshot for a specific fd
53 *
54 * Returns 0 on success, < 0 on error
55 */
ffe60014 56int lttng_kconsumer_take_snapshot(struct lttng_consumer_stream *stream)
3bd1e081
MD
57{
58 int ret = 0;
59 int infd = stream->wait_fd;
60
61 ret = kernctl_snapshot(infd);
62 if (ret != 0) {
3bd1e081 63 perror("Getting sub-buffer snapshot.");
56591bac 64 ret = -errno;
3bd1e081
MD
65 }
66
67 return ret;
68}
69
70/*
71 * Get the produced position
72 *
73 * Returns 0 on success, < 0 on error
74 */
ffe60014 75int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream *stream,
3bd1e081
MD
76 unsigned long *pos)
77{
78 int ret;
79 int infd = stream->wait_fd;
80
81 ret = kernctl_snapshot_get_produced(infd, pos);
82 if (ret != 0) {
3bd1e081 83 perror("kernctl_snapshot_get_produced");
56591bac 84 ret = -errno;
3bd1e081
MD
85 }
86
87 return ret;
88}
89
07b86b52
JD
90/*
91 * Get the consumerd position
92 *
93 * Returns 0 on success, < 0 on error
94 */
95int lttng_kconsumer_get_consumed_snapshot(struct lttng_consumer_stream *stream,
96 unsigned long *pos)
97{
98 int ret;
99 int infd = stream->wait_fd;
100
101 ret = kernctl_snapshot_get_consumed(infd, pos);
102 if (ret != 0) {
07b86b52 103 perror("kernctl_snapshot_get_consumed");
56591bac 104 ret = -errno;
07b86b52
JD
105 }
106
107 return ret;
108}
109
07b86b52
JD
110/*
111 * Take a snapshot of all the stream of a channel
112 *
113 * Returns 0 on success, < 0 on error
114 */
115int lttng_kconsumer_snapshot_channel(uint64_t key, char *path,
5c786ded
JD
116 uint64_t relayd_id, uint64_t max_stream_size,
117 struct lttng_consumer_local_data *ctx)
07b86b52
JD
118{
119 int ret;
120 unsigned long consumed_pos, produced_pos;
121 struct lttng_consumer_channel *channel;
122 struct lttng_consumer_stream *stream;
123
6a00837f 124 DBG("Kernel consumer snapshot channel %" PRIu64, key);
07b86b52
JD
125
126 rcu_read_lock();
127
128 channel = consumer_find_channel(key);
129 if (!channel) {
6a00837f 130 ERR("No channel found for key %" PRIu64, key);
07b86b52
JD
131 ret = -1;
132 goto end;
133 }
134
135 /* Splice is not supported yet for channel snapshot. */
136 if (channel->output != CONSUMER_CHANNEL_MMAP) {
137 ERR("Unsupported output %d", channel->output);
138 ret = -1;
139 goto end;
140 }
141
10a50311 142 cds_list_for_each_entry(stream, &channel->streams.head, send_node) {
9ce5646a
MD
143
144 health_code_update();
145
07b86b52
JD
146 /*
147 * Lock stream because we are about to change its state.
148 */
149 pthread_mutex_lock(&stream->lock);
150
29decac3
DG
151 /*
152 * Assign the received relayd ID so we can use it for streaming. The streams
153 * are not visible to anyone so this is OK to change it.
154 */
07b86b52
JD
155 stream->net_seq_idx = relayd_id;
156 channel->relayd_id = relayd_id;
157 if (relayd_id != (uint64_t) -1ULL) {
10a50311 158 ret = consumer_send_relayd_stream(stream, path);
07b86b52
JD
159 if (ret < 0) {
160 ERR("sending stream to relayd");
161 goto end_unlock;
162 }
07b86b52
JD
163 } else {
164 ret = utils_create_stream_file(path, stream->name,
10a50311
JD
165 stream->chan->tracefile_size,
166 stream->tracefile_count_current,
309167d2 167 stream->uid, stream->gid, NULL);
07b86b52
JD
168 if (ret < 0) {
169 ERR("utils_create_stream_file");
170 goto end_unlock;
171 }
172
173 stream->out_fd = ret;
174 stream->tracefile_size_current = 0;
175
81ea21bf
MD
176 DBG("Kernel consumer snapshot stream %s/%s (%" PRIu64 ")",
177 path, stream->name, stream->key);
07b86b52 178 }
e37dd78f
JD
179 if (relayd_id != -1ULL) {
180 ret = consumer_send_relayd_streams_sent(relayd_id);
181 if (ret < 0) {
182 ERR("sending streams sent to relayd");
183 goto end_unlock;
184 }
814fcae4 185 }
07b86b52
JD
186
187 ret = kernctl_buffer_flush(stream->wait_fd);
188 if (ret < 0) {
10a50311 189 ERR("Failed to flush kernel stream");
56591bac 190 ret = -errno;
07b86b52
JD
191 goto end_unlock;
192 }
193
194 ret = lttng_kconsumer_take_snapshot(stream);
195 if (ret < 0) {
196 ERR("Taking kernel snapshot");
197 goto end_unlock;
198 }
199
200 ret = lttng_kconsumer_get_produced_snapshot(stream, &produced_pos);
201 if (ret < 0) {
202 ERR("Produced kernel snapshot position");
203 goto end_unlock;
204 }
205
206 ret = lttng_kconsumer_get_consumed_snapshot(stream, &consumed_pos);
207 if (ret < 0) {
208 ERR("Consumerd kernel snapshot position");
209 goto end_unlock;
210 }
211
212 if (stream->max_sb_size == 0) {
213 ret = kernctl_get_max_subbuf_size(stream->wait_fd,
214 &stream->max_sb_size);
215 if (ret < 0) {
216 ERR("Getting kernel max_sb_size");
56591bac 217 ret = -errno;
07b86b52
JD
218 goto end_unlock;
219 }
220 }
221
5c786ded
JD
222 /*
223 * The original value is sent back if max stream size is larger than
224 * the possible size of the snapshot. Also, we asume that the session
225 * daemon should never send a maximum stream size that is lower than
226 * subbuffer size.
227 */
228 consumed_pos = consumer_get_consumed_maxsize(consumed_pos,
229 produced_pos, max_stream_size);
230
07b86b52
JD
231 while (consumed_pos < produced_pos) {
232 ssize_t read_len;
233 unsigned long len, padded_len;
234
9ce5646a
MD
235 health_code_update();
236
07b86b52
JD
237 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos);
238
239 ret = kernctl_get_subbuf(stream->wait_fd, &consumed_pos);
240 if (ret < 0) {
241 if (errno != EAGAIN) {
242 PERROR("kernctl_get_subbuf snapshot");
56591bac 243 ret = -errno;
07b86b52
JD
244 goto end_unlock;
245 }
246 DBG("Kernel consumer get subbuf failed. Skipping it.");
247 consumed_pos += stream->max_sb_size;
248 continue;
249 }
250
251 ret = kernctl_get_subbuf_size(stream->wait_fd, &len);
252 if (ret < 0) {
253 ERR("Snapshot kernctl_get_subbuf_size");
56591bac 254 ret = -errno;
29decac3 255 goto error_put_subbuf;
07b86b52
JD
256 }
257
258 ret = kernctl_get_padded_subbuf_size(stream->wait_fd, &padded_len);
259 if (ret < 0) {
260 ERR("Snapshot kernctl_get_padded_subbuf_size");
56591bac 261 ret = -errno;
29decac3 262 goto error_put_subbuf;
07b86b52
JD
263 }
264
265 read_len = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, len,
309167d2 266 padded_len - len, NULL);
07b86b52 267 /*
29decac3
DG
268 * We write the padded len in local tracefiles but the data len
269 * when using a relay. Display the error but continue processing
270 * to try to release the subbuffer.
07b86b52
JD
271 */
272 if (relayd_id != (uint64_t) -1ULL) {
273 if (read_len != len) {
274 ERR("Error sending to the relay (ret: %zd != len: %lu)",
275 read_len, len);
276 }
277 } else {
278 if (read_len != padded_len) {
279 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
280 read_len, padded_len);
281 }
282 }
283
284 ret = kernctl_put_subbuf(stream->wait_fd);
285 if (ret < 0) {
286 ERR("Snapshot kernctl_put_subbuf");
56591bac 287 ret = -errno;
07b86b52
JD
288 goto end_unlock;
289 }
290 consumed_pos += stream->max_sb_size;
291 }
292
293 if (relayd_id == (uint64_t) -1ULL) {
fdf9986c
MD
294 if (stream->out_fd >= 0) {
295 ret = close(stream->out_fd);
296 if (ret < 0) {
297 PERROR("Kernel consumer snapshot close out_fd");
298 goto end_unlock;
299 }
300 stream->out_fd = -1;
07b86b52 301 }
07b86b52
JD
302 } else {
303 close_relayd_stream(stream);
304 stream->net_seq_idx = (uint64_t) -1ULL;
305 }
306 pthread_mutex_unlock(&stream->lock);
307 }
308
309 /* All good! */
310 ret = 0;
311 goto end;
312
29decac3
DG
313error_put_subbuf:
314 ret = kernctl_put_subbuf(stream->wait_fd);
315 if (ret < 0) {
56591bac 316 ret = -errno;
29decac3
DG
317 ERR("Snapshot kernctl_put_subbuf error path");
318 }
07b86b52
JD
319end_unlock:
320 pthread_mutex_unlock(&stream->lock);
321end:
322 rcu_read_unlock();
323 return ret;
324}
325
326/*
327 * Read the whole metadata available for a snapshot.
328 *
329 * Returns 0 on success, < 0 on error
330 */
331int lttng_kconsumer_snapshot_metadata(uint64_t key, char *path,
e2039c7a 332 uint64_t relayd_id, struct lttng_consumer_local_data *ctx)
07b86b52 333{
d771f832
DG
334 int ret, use_relayd = 0;
335 ssize_t ret_read;
07b86b52
JD
336 struct lttng_consumer_channel *metadata_channel;
337 struct lttng_consumer_stream *metadata_stream;
d771f832
DG
338
339 assert(ctx);
07b86b52
JD
340
341 DBG("Kernel consumer snapshot metadata with key %" PRIu64 " at path %s",
342 key, path);
343
344 rcu_read_lock();
345
346 metadata_channel = consumer_find_channel(key);
347 if (!metadata_channel) {
d771f832 348 ERR("Kernel snapshot metadata not found for key %" PRIu64, key);
07b86b52 349 ret = -1;
d771f832 350 goto error;
07b86b52
JD
351 }
352
353 metadata_stream = metadata_channel->metadata_stream;
354 assert(metadata_stream);
355
d771f832 356 /* Flag once that we have a valid relayd for the stream. */
e2039c7a 357 if (relayd_id != (uint64_t) -1ULL) {
d771f832
DG
358 use_relayd = 1;
359 }
360
361 if (use_relayd) {
10a50311 362 ret = consumer_send_relayd_stream(metadata_stream, path);
e2039c7a 363 if (ret < 0) {
d771f832 364 goto error;
e2039c7a 365 }
e2039c7a
JD
366 } else {
367 ret = utils_create_stream_file(path, metadata_stream->name,
368 metadata_stream->chan->tracefile_size,
369 metadata_stream->tracefile_count_current,
309167d2 370 metadata_stream->uid, metadata_stream->gid, NULL);
e2039c7a 371 if (ret < 0) {
d771f832 372 goto error;
e2039c7a
JD
373 }
374 metadata_stream->out_fd = ret;
07b86b52 375 }
07b86b52 376
d771f832 377 do {
9ce5646a
MD
378 health_code_update();
379
d771f832
DG
380 ret_read = lttng_kconsumer_read_subbuffer(metadata_stream, ctx);
381 if (ret_read < 0) {
56591bac 382 if (ret_read != -EAGAIN) {
6a00837f 383 ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
d771f832
DG
384 ret_read);
385 goto error;
07b86b52 386 }
d771f832 387 /* ret_read is negative at this point so we will exit the loop. */
07b86b52
JD
388 continue;
389 }
d771f832 390 } while (ret_read >= 0);
07b86b52 391
d771f832
DG
392 if (use_relayd) {
393 close_relayd_stream(metadata_stream);
394 metadata_stream->net_seq_idx = (uint64_t) -1ULL;
395 } else {
fdf9986c
MD
396 if (metadata_stream->out_fd >= 0) {
397 ret = close(metadata_stream->out_fd);
398 if (ret < 0) {
399 PERROR("Kernel consumer snapshot metadata close out_fd");
400 /*
401 * Don't go on error here since the snapshot was successful at this
402 * point but somehow the close failed.
403 */
404 }
405 metadata_stream->out_fd = -1;
e2039c7a 406 }
e2039c7a
JD
407 }
408
07b86b52 409 ret = 0;
d771f832 410
cf53a8a6
JD
411 cds_list_del(&metadata_stream->send_node);
412 consumer_stream_destroy(metadata_stream, NULL);
413 metadata_channel->metadata_stream = NULL;
d771f832 414error:
07b86b52
JD
415 rcu_read_unlock();
416 return ret;
417}
418
1803a064
MD
419/*
420 * Receive command from session daemon and process it.
421 *
422 * Return 1 on success else a negative value or 0.
423 */
3bd1e081
MD
424int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
425 int sock, struct pollfd *consumer_sockpoll)
426{
427 ssize_t ret;
0c759fc9 428 enum lttcomm_return_code ret_code = LTTCOMM_CONSUMERD_SUCCESS;
3bd1e081
MD
429 struct lttcomm_consumer_msg msg;
430
9ce5646a
MD
431 health_code_update();
432
3bd1e081
MD
433 ret = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg));
434 if (ret != sizeof(msg)) {
1803a064 435 if (ret > 0) {
c6857fcf 436 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_CMD);
1803a064
MD
437 ret = -1;
438 }
3bd1e081
MD
439 return ret;
440 }
9ce5646a
MD
441
442 health_code_update();
443
1c9d6f43
MD
444 /* Deprecated command */
445 assert(msg.cmd_type != LTTNG_CONSUMER_STOP);
3bd1e081 446
9ce5646a
MD
447 health_code_update();
448
b0b335c8
MD
449 /* relayd needs RCU read-side protection */
450 rcu_read_lock();
451
3bd1e081 452 switch (msg.cmd_type) {
00e2e675
DG
453 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET:
454 {
f50f23d9 455 /* Session daemon status message are handled in the following call. */
7735ef9e
DG
456 ret = consumer_add_relayd_socket(msg.u.relayd_sock.net_index,
457 msg.u.relayd_sock.type, ctx, sock, consumer_sockpoll,
d3e2ba59
JD
458 &msg.u.relayd_sock.sock, msg.u.relayd_sock.session_id,
459 msg.u.relayd_sock.relayd_session_id);
00e2e675
DG
460 goto end_nosignal;
461 }
3bd1e081
MD
462 case LTTNG_CONSUMER_ADD_CHANNEL:
463 {
464 struct lttng_consumer_channel *new_channel;
e43c41c5 465 int ret_recv;
3bd1e081 466
9ce5646a
MD
467 health_code_update();
468
f50f23d9
DG
469 /* First send a status message before receiving the fds. */
470 ret = consumer_send_status_msg(sock, ret_code);
471 if (ret < 0) {
472 /* Somehow, the session daemon is not responding anymore. */
1803a064 473 goto error_fatal;
f50f23d9 474 }
9ce5646a
MD
475
476 health_code_update();
477
d88aee68 478 DBG("consumer_add_channel %" PRIu64, msg.u.channel.channel_key);
3bd1e081 479 new_channel = consumer_allocate_channel(msg.u.channel.channel_key,
ffe60014
DG
480 msg.u.channel.session_id, msg.u.channel.pathname,
481 msg.u.channel.name, msg.u.channel.uid, msg.u.channel.gid,
1624d5b7
JD
482 msg.u.channel.relayd_id, msg.u.channel.output,
483 msg.u.channel.tracefile_size,
1950109e 484 msg.u.channel.tracefile_count, 0,
ecc48a90
JD
485 msg.u.channel.monitor,
486 msg.u.channel.live_timer_interval);
3bd1e081 487 if (new_channel == NULL) {
f73fabfd 488 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
3bd1e081
MD
489 goto end_nosignal;
490 }
ffe60014 491 new_channel->nb_init_stream_left = msg.u.channel.nb_init_streams;
95a1109b
JD
492 switch (msg.u.channel.output) {
493 case LTTNG_EVENT_SPLICE:
494 new_channel->output = CONSUMER_CHANNEL_SPLICE;
495 break;
496 case LTTNG_EVENT_MMAP:
497 new_channel->output = CONSUMER_CHANNEL_MMAP;
498 break;
499 default:
500 ERR("Channel output unknown %d", msg.u.channel.output);
501 goto end_nosignal;
502 }
ffe60014
DG
503
504 /* Translate and save channel type. */
505 switch (msg.u.channel.type) {
506 case CONSUMER_CHANNEL_TYPE_DATA:
507 case CONSUMER_CHANNEL_TYPE_METADATA:
508 new_channel->type = msg.u.channel.type;
509 break;
510 default:
511 assert(0);
512 goto end_nosignal;
513 };
514
9ce5646a
MD
515 health_code_update();
516
3bd1e081 517 if (ctx->on_recv_channel != NULL) {
e43c41c5
JD
518 ret_recv = ctx->on_recv_channel(new_channel);
519 if (ret_recv == 0) {
520 ret = consumer_add_channel(new_channel, ctx);
521 } else if (ret_recv < 0) {
3bd1e081
MD
522 goto end_nosignal;
523 }
524 } else {
e43c41c5 525 ret = consumer_add_channel(new_channel, ctx);
3bd1e081 526 }
94d49140
JD
527 if (CONSUMER_CHANNEL_TYPE_DATA) {
528 consumer_timer_live_start(new_channel,
529 msg.u.channel.live_timer_interval);
530 }
e43c41c5 531
9ce5646a
MD
532 health_code_update();
533
e43c41c5 534 /* If we received an error in add_channel, we need to report it. */
821fffb2 535 if (ret < 0) {
1803a064
MD
536 ret = consumer_send_status_msg(sock, ret);
537 if (ret < 0) {
538 goto error_fatal;
539 }
e43c41c5
JD
540 goto end_nosignal;
541 }
542
3bd1e081
MD
543 goto end_nosignal;
544 }
545 case LTTNG_CONSUMER_ADD_STREAM:
546 {
dae10966
DG
547 int fd;
548 struct lttng_pipe *stream_pipe;
00e2e675 549 struct lttng_consumer_stream *new_stream;
ffe60014 550 struct lttng_consumer_channel *channel;
c80048c6 551 int alloc_ret = 0;
3bd1e081 552
ffe60014
DG
553 /*
554 * Get stream's channel reference. Needed when adding the stream to the
555 * global hash table.
556 */
557 channel = consumer_find_channel(msg.u.stream.channel_key);
558 if (!channel) {
559 /*
560 * We could not find the channel. Can happen if cpu hotplug
561 * happens while tearing down.
562 */
d88aee68 563 ERR("Unable to find channel key %" PRIu64, msg.u.stream.channel_key);
aaf33532 564 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
ffe60014
DG
565 }
566
9ce5646a
MD
567 health_code_update();
568
f50f23d9
DG
569 /* First send a status message before receiving the fds. */
570 ret = consumer_send_status_msg(sock, ret_code);
1803a064 571 if (ret < 0) {
d771f832 572 /* Somehow, the session daemon is not responding anymore. */
1803a064
MD
573 goto error_fatal;
574 }
9ce5646a
MD
575
576 health_code_update();
577
0c759fc9 578 if (ret_code != LTTCOMM_CONSUMERD_SUCCESS) {
d771f832 579 /* Channel was not found. */
f50f23d9
DG
580 goto end_nosignal;
581 }
582
d771f832 583 /* Blocking call */
9ce5646a
MD
584 health_poll_entry();
585 ret = lttng_consumer_poll_socket(consumer_sockpoll);
586 health_poll_exit();
1c9d6f43
MD
587 if (ret) {
588 goto error_fatal;
3bd1e081 589 }
00e2e675 590
9ce5646a
MD
591 health_code_update();
592
00e2e675 593 /* Get stream file descriptor from socket */
f2fc6720
MD
594 ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1);
595 if (ret != sizeof(fd)) {
f73fabfd 596 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD);
3f8e211f 597 rcu_read_unlock();
3bd1e081
MD
598 return ret;
599 }
3bd1e081 600
9ce5646a
MD
601 health_code_update();
602
f50f23d9
DG
603 /*
604 * Send status code to session daemon only if the recv works. If the
605 * above recv() failed, the session daemon is notified through the
606 * error socket and the teardown is eventually done.
607 */
608 ret = consumer_send_status_msg(sock, ret_code);
609 if (ret < 0) {
610 /* Somehow, the session daemon is not responding anymore. */
611 goto end_nosignal;
612 }
613
9ce5646a
MD
614 health_code_update();
615
ffe60014
DG
616 new_stream = consumer_allocate_stream(channel->key,
617 fd,
618 LTTNG_CONSUMER_ACTIVE_STREAM,
619 channel->name,
620 channel->uid,
621 channel->gid,
622 channel->relayd_id,
623 channel->session_id,
624 msg.u.stream.cpu,
625 &alloc_ret,
4891ece8
DG
626 channel->type,
627 channel->monitor);
3bd1e081 628 if (new_stream == NULL) {
c80048c6
MD
629 switch (alloc_ret) {
630 case -ENOMEM:
631 case -EINVAL:
632 default:
633 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
634 break;
c80048c6 635 }
3f8e211f 636 goto end_nosignal;
3bd1e081 637 }
d771f832 638
ffe60014
DG
639 new_stream->chan = channel;
640 new_stream->wait_fd = fd;
07b86b52
JD
641 switch (channel->output) {
642 case CONSUMER_CHANNEL_SPLICE:
643 new_stream->output = LTTNG_EVENT_SPLICE;
babec7b7
JD
644 ret = utils_create_pipe(new_stream->splice_pipe);
645 if (ret < 0) {
646 goto end_nosignal;
647 }
07b86b52
JD
648 break;
649 case CONSUMER_CHANNEL_MMAP:
650 new_stream->output = LTTNG_EVENT_MMAP;
651 break;
652 default:
653 ERR("Stream output unknown %d", channel->output);
654 goto end_nosignal;
655 }
00e2e675 656
a0c83db9
DG
657 /*
658 * We've just assigned the channel to the stream so increment the
07b86b52
JD
659 * refcount right now. We don't need to increment the refcount for
660 * streams in no monitor because we handle manually the cleanup of
661 * those. It is very important to make sure there is NO prior
662 * consumer_del_stream() calls or else the refcount will be unbalanced.
a0c83db9 663 */
07b86b52
JD
664 if (channel->monitor) {
665 uatomic_inc(&new_stream->chan->refcount);
666 }
9d9353f9 667
fb3a43a9
DG
668 /*
669 * The buffer flush is done on the session daemon side for the kernel
670 * so no need for the stream "hangup_flush_done" variable to be
671 * tracked. This is important for a kernel stream since we don't rely
672 * on the flush state of the stream to read data. It's not the case for
673 * user space tracing.
674 */
675 new_stream->hangup_flush_done = 0;
676
9ce5646a
MD
677 health_code_update();
678
633d0084
DG
679 if (ctx->on_recv_stream) {
680 ret = ctx->on_recv_stream(new_stream);
681 if (ret < 0) {
d771f832 682 consumer_stream_free(new_stream);
633d0084 683 goto end_nosignal;
fb3a43a9 684 }
633d0084 685 }
fb3a43a9 686
9ce5646a
MD
687 health_code_update();
688
07b86b52
JD
689 if (new_stream->metadata_flag) {
690 channel->metadata_stream = new_stream;
691 }
692
2bba9e53
DG
693 /* Do not monitor this stream. */
694 if (!channel->monitor) {
5eecee74 695 DBG("Kernel consumer add stream %s in no monitor mode with "
6dc3064a 696 "relayd id %" PRIu64, new_stream->name,
5eecee74 697 new_stream->net_seq_idx);
10a50311 698 cds_list_add(&new_stream->send_node, &channel->streams.head);
6dc3064a
DG
699 break;
700 }
701
e1b71bdc
DG
702 /* Send stream to relayd if the stream has an ID. */
703 if (new_stream->net_seq_idx != (uint64_t) -1ULL) {
194ee077
DG
704 ret = consumer_send_relayd_stream(new_stream,
705 new_stream->chan->pathname);
e1b71bdc
DG
706 if (ret < 0) {
707 consumer_stream_free(new_stream);
708 goto end_nosignal;
709 }
e2039c7a
JD
710 }
711
50f8ae69 712 /* Get the right pipe where the stream will be sent. */
633d0084 713 if (new_stream->metadata_flag) {
5ab66908
MD
714 ret = consumer_add_metadata_stream(new_stream);
715 if (ret) {
716 ERR("Consumer add metadata stream %" PRIu64 " failed. Continuing",
717 new_stream->key);
718 consumer_stream_free(new_stream);
719 goto end_nosignal;
720 }
dae10966 721 stream_pipe = ctx->consumer_metadata_pipe;
3bd1e081 722 } else {
5ab66908
MD
723 ret = consumer_add_data_stream(new_stream);
724 if (ret) {
725 ERR("Consumer add stream %" PRIu64 " failed. Continuing",
726 new_stream->key);
727 consumer_stream_free(new_stream);
728 goto end_nosignal;
729 }
dae10966 730 stream_pipe = ctx->consumer_data_pipe;
50f8ae69
DG
731 }
732
5ab66908
MD
733 /* Vitible to other threads */
734 new_stream->globally_visible = 1;
735
9ce5646a
MD
736 health_code_update();
737
dae10966 738 ret = lttng_pipe_write(stream_pipe, &new_stream, sizeof(new_stream));
50f8ae69 739 if (ret < 0) {
dae10966 740 ERR("Consumer write %s stream to pipe %d",
50f8ae69 741 new_stream->metadata_flag ? "metadata" : "data",
dae10966 742 lttng_pipe_get_writefd(stream_pipe));
5ab66908
MD
743 if (new_stream->metadata_flag) {
744 consumer_del_stream_for_metadata(new_stream);
745 } else {
746 consumer_del_stream_for_data(new_stream);
747 }
50f8ae69 748 goto end_nosignal;
3bd1e081 749 }
00e2e675 750
50f8ae69 751 DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64,
ffe60014 752 new_stream->name, fd, new_stream->relayd_stream_id);
3bd1e081
MD
753 break;
754 }
814fcae4
JD
755 case LTTNG_CONSUMER_STREAMS_SENT:
756 {
757 struct lttng_consumer_channel *channel;
758
759 /*
760 * Get stream's channel reference. Needed when adding the stream to the
761 * global hash table.
762 */
763 channel = consumer_find_channel(msg.u.sent_streams.channel_key);
764 if (!channel) {
765 /*
766 * We could not find the channel. Can happen if cpu hotplug
767 * happens while tearing down.
768 */
769 ERR("Unable to find channel key %" PRIu64,
770 msg.u.sent_streams.channel_key);
aaf33532 771 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
814fcae4
JD
772 }
773
774 health_code_update();
775
776 /*
777 * Send status code to session daemon.
778 */
779 ret = consumer_send_status_msg(sock, ret_code);
c9284ac8 780 if (ret < 0 || ret_code != LTTCOMM_CONSUMERD_SUCCESS) {
814fcae4
JD
781 /* Somehow, the session daemon is not responding anymore. */
782 goto end_nosignal;
783 }
784
785 health_code_update();
786
787 /*
788 * We should not send this message if we don't monitor the
789 * streams in this channel.
790 */
791 if (!channel->monitor) {
792 break;
793 }
794
795 health_code_update();
796 /* Send stream to relayd if the stream has an ID. */
797 if (msg.u.sent_streams.net_seq_idx != (uint64_t) -1ULL) {
798 ret = consumer_send_relayd_streams_sent(
799 msg.u.sent_streams.net_seq_idx);
800 if (ret < 0) {
801 goto end_nosignal;
802 }
803 }
804 break;
805 }
3bd1e081
MD
806 case LTTNG_CONSUMER_UPDATE_STREAM:
807 {
3f8e211f
DG
808 rcu_read_unlock();
809 return -ENOSYS;
810 }
811 case LTTNG_CONSUMER_DESTROY_RELAYD:
812 {
a6ba4fe1 813 uint64_t index = msg.u.destroy_relayd.net_seq_idx;
3f8e211f
DG
814 struct consumer_relayd_sock_pair *relayd;
815
a6ba4fe1 816 DBG("Kernel consumer destroying relayd %" PRIu64, index);
3f8e211f
DG
817
818 /* Get relayd reference if exists. */
a6ba4fe1 819 relayd = consumer_find_relayd(index);
3f8e211f 820 if (relayd == NULL) {
3448e266 821 DBG("Unable to find relayd %" PRIu64, index);
aaf33532 822 ret_code = LTTCOMM_CONSUMERD_RELAYD_FAIL;
3bd1e081 823 }
3f8e211f 824
a6ba4fe1
DG
825 /*
826 * Each relayd socket pair has a refcount of stream attached to it
827 * which tells if the relayd is still active or not depending on the
828 * refcount value.
829 *
830 * This will set the destroy flag of the relayd object and destroy it
831 * if the refcount reaches zero when called.
832 *
833 * The destroy can happen either here or when a stream fd hangs up.
834 */
f50f23d9
DG
835 if (relayd) {
836 consumer_flag_relayd_for_destroy(relayd);
837 }
838
9ce5646a
MD
839 health_code_update();
840
f50f23d9
DG
841 ret = consumer_send_status_msg(sock, ret_code);
842 if (ret < 0) {
843 /* Somehow, the session daemon is not responding anymore. */
1803a064 844 goto error_fatal;
f50f23d9 845 }
3f8e211f 846
3f8e211f 847 goto end_nosignal;
3bd1e081 848 }
6d805429 849 case LTTNG_CONSUMER_DATA_PENDING:
53632229 850 {
c8f59ee5 851 int32_t ret;
6d805429 852 uint64_t id = msg.u.data_pending.session_id;
c8f59ee5 853
6d805429 854 DBG("Kernel consumer data pending command for id %" PRIu64, id);
c8f59ee5 855
6d805429 856 ret = consumer_data_pending(id);
c8f59ee5 857
9ce5646a
MD
858 health_code_update();
859
c8f59ee5
DG
860 /* Send back returned value to session daemon */
861 ret = lttcomm_send_unix_sock(sock, &ret, sizeof(ret));
862 if (ret < 0) {
6d805429 863 PERROR("send data pending ret code");
1803a064 864 goto error_fatal;
c8f59ee5 865 }
f50f23d9
DG
866
867 /*
868 * No need to send back a status message since the data pending
869 * returned value is the response.
870 */
c8f59ee5 871 break;
53632229 872 }
6dc3064a
DG
873 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL:
874 {
07b86b52
JD
875 if (msg.u.snapshot_channel.metadata == 1) {
876 ret = lttng_kconsumer_snapshot_metadata(msg.u.snapshot_channel.key,
e2039c7a
JD
877 msg.u.snapshot_channel.pathname,
878 msg.u.snapshot_channel.relayd_id, ctx);
07b86b52
JD
879 if (ret < 0) {
880 ERR("Snapshot metadata failed");
aaf33532 881 ret_code = LTTCOMM_CONSUMERD_ERROR_METADATA;
07b86b52
JD
882 }
883 } else {
884 ret = lttng_kconsumer_snapshot_channel(msg.u.snapshot_channel.key,
885 msg.u.snapshot_channel.pathname,
5c786ded
JD
886 msg.u.snapshot_channel.relayd_id,
887 msg.u.snapshot_channel.max_stream_size,
888 ctx);
07b86b52
JD
889 if (ret < 0) {
890 ERR("Snapshot channel failed");
aaf33532 891 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
07b86b52
JD
892 }
893 }
894
9ce5646a
MD
895 health_code_update();
896
6dc3064a
DG
897 ret = consumer_send_status_msg(sock, ret_code);
898 if (ret < 0) {
899 /* Somehow, the session daemon is not responding anymore. */
900 goto end_nosignal;
901 }
902 break;
903 }
07b86b52
JD
904 case LTTNG_CONSUMER_DESTROY_CHANNEL:
905 {
906 uint64_t key = msg.u.destroy_channel.key;
907 struct lttng_consumer_channel *channel;
908
909 channel = consumer_find_channel(key);
910 if (!channel) {
911 ERR("Kernel consumer destroy channel %" PRIu64 " not found", key);
aaf33532 912 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
07b86b52
JD
913 }
914
9ce5646a
MD
915 health_code_update();
916
07b86b52
JD
917 ret = consumer_send_status_msg(sock, ret_code);
918 if (ret < 0) {
919 /* Somehow, the session daemon is not responding anymore. */
920 goto end_nosignal;
921 }
922
9ce5646a
MD
923 health_code_update();
924
9000e568
DG
925 /* Stop right now if no channel was found. */
926 if (!channel) {
927 goto end_nosignal;
928 }
929
07b86b52
JD
930 /*
931 * This command should ONLY be issued for channel with streams set in
932 * no monitor mode.
933 */
934 assert(!channel->monitor);
935
936 /*
937 * The refcount should ALWAYS be 0 in the case of a channel in no
938 * monitor mode.
939 */
940 assert(!uatomic_sub_return(&channel->refcount, 1));
941
942 consumer_del_channel(channel);
943
944 goto end_nosignal;
945 }
3bd1e081 946 default:
3f8e211f 947 goto end_nosignal;
3bd1e081 948 }
3f8e211f 949
3bd1e081 950end_nosignal:
b0b335c8 951 rcu_read_unlock();
4cbc1a04
DG
952
953 /*
954 * Return 1 to indicate success since the 0 value can be a socket
955 * shutdown during the recv() or send() call.
956 */
9ce5646a 957 health_code_update();
4cbc1a04 958 return 1;
1803a064
MD
959
960error_fatal:
961 rcu_read_unlock();
962 /* This will issue a consumer stop. */
963 return -1;
3bd1e081 964}
d41f73b7 965
309167d2
JD
966/*
967 * Populate index values of a kernel stream. Values are set in big endian order.
968 *
969 * Return 0 on success or else a negative value.
970 */
50adc264 971static int get_index_values(struct ctf_packet_index *index, int infd)
309167d2
JD
972{
973 int ret;
974
975 ret = kernctl_get_timestamp_begin(infd, &index->timestamp_begin);
976 if (ret < 0) {
977 PERROR("kernctl_get_timestamp_begin");
978 goto error;
979 }
980 index->timestamp_begin = htobe64(index->timestamp_begin);
981
982 ret = kernctl_get_timestamp_end(infd, &index->timestamp_end);
983 if (ret < 0) {
984 PERROR("kernctl_get_timestamp_end");
985 goto error;
986 }
987 index->timestamp_end = htobe64(index->timestamp_end);
988
989 ret = kernctl_get_events_discarded(infd, &index->events_discarded);
990 if (ret < 0) {
991 PERROR("kernctl_get_events_discarded");
992 goto error;
993 }
994 index->events_discarded = htobe64(index->events_discarded);
995
996 ret = kernctl_get_content_size(infd, &index->content_size);
997 if (ret < 0) {
998 PERROR("kernctl_get_content_size");
999 goto error;
1000 }
1001 index->content_size = htobe64(index->content_size);
1002
1003 ret = kernctl_get_packet_size(infd, &index->packet_size);
1004 if (ret < 0) {
1005 PERROR("kernctl_get_packet_size");
1006 goto error;
1007 }
1008 index->packet_size = htobe64(index->packet_size);
1009
1010 ret = kernctl_get_stream_id(infd, &index->stream_id);
1011 if (ret < 0) {
1012 PERROR("kernctl_get_stream_id");
1013 goto error;
1014 }
1015 index->stream_id = htobe64(index->stream_id);
1016
1017error:
1018 return ret;
1019}
94d49140
JD
1020/*
1021 * Sync metadata meaning request them to the session daemon and snapshot to the
1022 * metadata thread can consumer them.
1023 *
1024 * Metadata stream lock MUST be acquired.
1025 *
1026 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
1027 * is empty or a negative value on error.
1028 */
1029int lttng_kconsumer_sync_metadata(struct lttng_consumer_stream *metadata)
1030{
1031 int ret;
1032
1033 assert(metadata);
1034
1035 ret = kernctl_buffer_flush(metadata->wait_fd);
1036 if (ret < 0) {
1037 ERR("Failed to flush kernel stream");
1038 goto end;
1039 }
1040
1041 ret = kernctl_snapshot(metadata->wait_fd);
1042 if (ret < 0) {
1043 if (errno != EAGAIN) {
1044 ERR("Sync metadata, taking kernel snapshot failed.");
1045 goto end;
1046 }
1047 DBG("Sync metadata, no new kernel metadata");
1048 /* No new metadata, exit. */
1049 ret = ENODATA;
1050 goto end;
1051 }
1052
1053end:
1054 return ret;
1055}
309167d2 1056
d41f73b7
MD
1057/*
1058 * Consume data on a file descriptor and write it on a trace file.
1059 */
4078b776 1060ssize_t lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream *stream,
d41f73b7
MD
1061 struct lttng_consumer_local_data *ctx)
1062{
1d4dfdef 1063 unsigned long len, subbuf_size, padding;
1c20f0e2 1064 int err, write_index = 1;
4078b776 1065 ssize_t ret = 0;
d41f73b7 1066 int infd = stream->wait_fd;
50adc264 1067 struct ctf_packet_index index;
d41f73b7
MD
1068
1069 DBG("In read_subbuffer (infd : %d)", infd);
309167d2 1070
d41f73b7
MD
1071 /* Get the next subbuffer */
1072 err = kernctl_get_next_subbuf(infd);
1073 if (err != 0) {
d41f73b7
MD
1074 /*
1075 * This is a debug message even for single-threaded consumer,
1076 * because poll() have more relaxed criterions than get subbuf,
1077 * so get_subbuf may fail for short race windows where poll()
1078 * would issue wakeups.
1079 */
1080 DBG("Reserving sub buffer failed (everything is normal, "
1081 "it is due to concurrency)");
56591bac 1082 ret = -errno;
d41f73b7
MD
1083 goto end;
1084 }
1085
1d4dfdef
DG
1086 /* Get the full subbuffer size including padding */
1087 err = kernctl_get_padded_subbuf_size(infd, &len);
1088 if (err != 0) {
1d4dfdef 1089 perror("Getting sub-buffer len failed.");
56591bac 1090 ret = -errno;
1d4dfdef
DG
1091 goto end;
1092 }
1093
1c20f0e2 1094 if (!stream->metadata_flag) {
309167d2
JD
1095 ret = get_index_values(&index, infd);
1096 if (ret < 0) {
1097 goto end;
1098 }
1c20f0e2
JD
1099 } else {
1100 write_index = 0;
309167d2
JD
1101 }
1102
ffe60014 1103 switch (stream->chan->output) {
07b86b52 1104 case CONSUMER_CHANNEL_SPLICE:
1d4dfdef
DG
1105 /*
1106 * XXX: The lttng-modules splice "actor" does not handle copying
1107 * partial pages hence only using the subbuffer size without the
1108 * padding makes the splice fail.
1109 */
1110 subbuf_size = len;
1111 padding = 0;
1112
1113 /* splice the subbuffer to the tracefile */
91dfef6e 1114 ret = lttng_consumer_on_read_subbuffer_splice(ctx, stream, subbuf_size,
309167d2 1115 padding, &index);
91dfef6e
DG
1116 /*
1117 * XXX: Splice does not support network streaming so the return value
1118 * is simply checked against subbuf_size and not like the mmap() op.
1119 */
1d4dfdef
DG
1120 if (ret != subbuf_size) {
1121 /*
1122 * display the error but continue processing to try
1123 * to release the subbuffer
1124 */
1125 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
1126 ret, subbuf_size);
309167d2 1127 write_index = 0;
1d4dfdef
DG
1128 }
1129 break;
07b86b52 1130 case CONSUMER_CHANNEL_MMAP:
1d4dfdef
DG
1131 /* Get subbuffer size without padding */
1132 err = kernctl_get_subbuf_size(infd, &subbuf_size);
1133 if (err != 0) {
1d4dfdef 1134 perror("Getting sub-buffer len failed.");
56591bac 1135 ret = -errno;
1d4dfdef
DG
1136 goto end;
1137 }
47e81c02 1138
1d4dfdef
DG
1139 /* Make sure the tracer is not gone mad on us! */
1140 assert(len >= subbuf_size);
1141
1142 padding = len - subbuf_size;
1143
1144 /* write the subbuffer to the tracefile */
91dfef6e 1145 ret = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, subbuf_size,
309167d2 1146 padding, &index);
91dfef6e
DG
1147 /*
1148 * The mmap operation should write subbuf_size amount of data when
1149 * network streaming or the full padding (len) size when we are _not_
1150 * streaming.
1151 */
d88aee68
DG
1152 if ((ret != subbuf_size && stream->net_seq_idx != (uint64_t) -1ULL) ||
1153 (ret != len && stream->net_seq_idx == (uint64_t) -1ULL)) {
1d4dfdef 1154 /*
91dfef6e 1155 * Display the error but continue processing to try to release the
a0424645
DG
1156 * subbuffer. This is a DBG statement since this is possible to
1157 * happen without being a critical error.
1d4dfdef 1158 */
a0424645 1159 DBG("Error writing to tracefile "
91dfef6e
DG
1160 "(ret: %zd != len: %lu != subbuf_size: %lu)",
1161 ret, len, subbuf_size);
309167d2 1162 write_index = 0;
1d4dfdef
DG
1163 }
1164 break;
1165 default:
1166 ERR("Unknown output method");
56591bac 1167 ret = -EPERM;
d41f73b7
MD
1168 }
1169
1170 err = kernctl_put_next_subbuf(infd);
1171 if (err != 0) {
d41f73b7
MD
1172 if (errno == EFAULT) {
1173 perror("Error in unreserving sub buffer\n");
1174 } else if (errno == EIO) {
1175 /* Should never happen with newer LTTng versions */
1176 perror("Reader has been pushed by the writer, last sub-buffer corrupted.");
1177 }
56591bac 1178 ret = -errno;
d41f73b7
MD
1179 goto end;
1180 }
1181
309167d2 1182 /* Write index if needed. */
1c20f0e2
JD
1183 if (!write_index) {
1184 goto end;
1185 }
1186
94d49140
JD
1187 if (stream->chan->live_timer_interval && !stream->metadata_flag) {
1188 /*
1189 * In live, block until all the metadata is sent.
1190 */
1191 err = consumer_stream_sync_metadata(ctx, stream->session_id);
1192 if (err < 0) {
1193 goto end;
1194 }
1195 }
1196
1c20f0e2
JD
1197 err = consumer_stream_write_index(stream, &index);
1198 if (err < 0) {
1199 goto end;
309167d2
JD
1200 }
1201
d41f73b7
MD
1202end:
1203 return ret;
1204}
1205
1206int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream *stream)
1207{
1208 int ret;
ffe60014
DG
1209
1210 assert(stream);
1211
2bba9e53
DG
1212 /*
1213 * Don't create anything if this is set for streaming or should not be
1214 * monitored.
1215 */
1216 if (stream->net_seq_idx == (uint64_t) -1ULL && stream->chan->monitor) {
fe4477ee
JD
1217 ret = utils_create_stream_file(stream->chan->pathname, stream->name,
1218 stream->chan->tracefile_size, stream->tracefile_count_current,
309167d2 1219 stream->uid, stream->gid, NULL);
fe4477ee
JD
1220 if (ret < 0) {
1221 goto error;
1222 }
1223 stream->out_fd = ret;
1224 stream->tracefile_size_current = 0;
309167d2
JD
1225
1226 if (!stream->metadata_flag) {
1227 ret = index_create_file(stream->chan->pathname,
1228 stream->name, stream->uid, stream->gid,
1229 stream->chan->tracefile_size,
1230 stream->tracefile_count_current);
1231 if (ret < 0) {
1232 goto error;
1233 }
1234 stream->index_fd = ret;
1235 }
ffe60014 1236 }
d41f73b7 1237
d41f73b7
MD
1238 if (stream->output == LTTNG_EVENT_MMAP) {
1239 /* get the len of the mmap region */
1240 unsigned long mmap_len;
1241
1242 ret = kernctl_get_mmap_len(stream->wait_fd, &mmap_len);
1243 if (ret != 0) {
ffe60014 1244 PERROR("kernctl_get_mmap_len");
56591bac 1245 ret = -errno;
d41f73b7
MD
1246 goto error_close_fd;
1247 }
1248 stream->mmap_len = (size_t) mmap_len;
1249
ffe60014
DG
1250 stream->mmap_base = mmap(NULL, stream->mmap_len, PROT_READ,
1251 MAP_PRIVATE, stream->wait_fd, 0);
d41f73b7 1252 if (stream->mmap_base == MAP_FAILED) {
ffe60014 1253 PERROR("Error mmaping");
d41f73b7
MD
1254 ret = -1;
1255 goto error_close_fd;
1256 }
1257 }
1258
1259 /* we return 0 to let the library handle the FD internally */
1260 return 0;
1261
1262error_close_fd:
2f225ce2 1263 if (stream->out_fd >= 0) {
d41f73b7
MD
1264 int err;
1265
1266 err = close(stream->out_fd);
1267 assert(!err);
2f225ce2 1268 stream->out_fd = -1;
d41f73b7
MD
1269 }
1270error:
1271 return ret;
1272}
1273
ca22feea
DG
1274/*
1275 * Check if data is still being extracted from the buffers for a specific
4e9a4686
DG
1276 * stream. Consumer data lock MUST be acquired before calling this function
1277 * and the stream lock.
ca22feea 1278 *
6d805429 1279 * Return 1 if the traced data are still getting read else 0 meaning that the
ca22feea
DG
1280 * data is available for trace viewer reading.
1281 */
6d805429 1282int lttng_kconsumer_data_pending(struct lttng_consumer_stream *stream)
ca22feea
DG
1283{
1284 int ret;
1285
1286 assert(stream);
1287
873b9e9a
MD
1288 if (stream->endpoint_status != CONSUMER_ENDPOINT_ACTIVE) {
1289 ret = 0;
1290 goto end;
1291 }
1292
ca22feea
DG
1293 ret = kernctl_get_next_subbuf(stream->wait_fd);
1294 if (ret == 0) {
1295 /* There is still data so let's put back this subbuffer. */
1296 ret = kernctl_put_subbuf(stream->wait_fd);
1297 assert(ret == 0);
6d805429 1298 ret = 1; /* Data is pending */
4e9a4686 1299 goto end;
ca22feea
DG
1300 }
1301
6d805429
DG
1302 /* Data is NOT pending and ready to be read. */
1303 ret = 0;
ca22feea 1304
6efae65e
DG
1305end:
1306 return ret;
ca22feea 1307}
This page took 0.132722 seconds and 4 git commands to generate.