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