Add .clang-format
[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>
b3530820 4 * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
3bd1e081 5 *
d14d33bf
AM
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
3bd1e081
MD
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
d14d33bf
AM
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
3bd1e081
MD
18 */
19
6c1c0768 20#define _LGPL_SOURCE
3bd1e081 21#include <assert.h>
3bd1e081
MD
22#include <poll.h>
23#include <pthread.h>
24#include <stdlib.h>
25#include <string.h>
26#include <sys/mman.h>
27#include <sys/socket.h>
28#include <sys/types.h>
77c7c900 29#include <inttypes.h>
3bd1e081 30#include <unistd.h>
dbb5dfe6 31#include <sys/stat.h>
3bd1e081 32
51a9e1c7 33#include <bin/lttng-consumerd/health-consumerd.h>
990570ed 34#include <common/common.h>
10a8a223 35#include <common/kernel-ctl/kernel-ctl.h>
10a8a223 36#include <common/sessiond-comm/sessiond-comm.h>
00e2e675 37#include <common/sessiond-comm/relayd.h>
dbb5dfe6 38#include <common/compat/fcntl.h>
f263b7fd 39#include <common/compat/endian.h>
acdb9057 40#include <common/pipe.h>
00e2e675 41#include <common/relayd/relayd.h>
fe4477ee 42#include <common/utils.h>
c8fea79c 43#include <common/consumer/consumer-stream.h>
309167d2 44#include <common/index/index.h>
c8fea79c 45#include <common/consumer/consumer-timer.h>
d2956687 46#include <common/optional.h>
0857097f 47
10a8a223 48#include "kernel-consumer.h"
3bd1e081
MD
49
50extern struct lttng_consumer_global_data consumer_data;
51extern int consumer_poll_timeout;
3bd1e081 52
3bd1e081
MD
53/*
54 * Take a snapshot for a specific fd
55 *
56 * Returns 0 on success, < 0 on error
57 */
ffe60014 58int lttng_kconsumer_take_snapshot(struct lttng_consumer_stream *stream)
3bd1e081
MD
59{
60 int ret = 0;
61 int infd = stream->wait_fd;
62
63 ret = kernctl_snapshot(infd);
d2d2f190
JD
64 /*
65 * -EAGAIN is not an error, it just means that there is no data to
66 * be read.
67 */
68 if (ret != 0 && ret != -EAGAIN) {
5a510c9f 69 PERROR("Getting sub-buffer snapshot.");
3bd1e081
MD
70 }
71
72 return ret;
73}
74
e9404c27
JG
75/*
76 * Sample consumed and produced positions for a specific fd.
77 *
78 * Returns 0 on success, < 0 on error.
79 */
80int lttng_kconsumer_sample_snapshot_positions(
81 struct lttng_consumer_stream *stream)
82{
83 assert(stream);
84
85 return kernctl_snapshot_sample_positions(stream->wait_fd);
86}
87
3bd1e081
MD
88/*
89 * Get the produced position
90 *
91 * Returns 0 on success, < 0 on error
92 */
ffe60014 93int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream *stream,
3bd1e081
MD
94 unsigned long *pos)
95{
96 int ret;
97 int infd = stream->wait_fd;
98
99 ret = kernctl_snapshot_get_produced(infd, pos);
100 if (ret != 0) {
5a510c9f 101 PERROR("kernctl_snapshot_get_produced");
3bd1e081
MD
102 }
103
104 return ret;
105}
106
07b86b52
JD
107/*
108 * Get the consumerd position
109 *
110 * Returns 0 on success, < 0 on error
111 */
112int lttng_kconsumer_get_consumed_snapshot(struct lttng_consumer_stream *stream,
113 unsigned long *pos)
114{
115 int ret;
116 int infd = stream->wait_fd;
117
118 ret = kernctl_snapshot_get_consumed(infd, pos);
119 if (ret != 0) {
5a510c9f 120 PERROR("kernctl_snapshot_get_consumed");
07b86b52
JD
121 }
122
123 return ret;
124}
125
07b86b52
JD
126/*
127 * Take a snapshot of all the stream of a channel
3eb928aa 128 * RCU read-side lock must be held across this function to ensure existence of
d2956687 129 * channel. The channel lock must be held by the caller.
07b86b52
JD
130 *
131 * Returns 0 on success, < 0 on error
132 */
f72bb42f
JG
133static int lttng_kconsumer_snapshot_channel(
134 struct lttng_consumer_channel *channel,
135 uint64_t key, char *path, uint64_t relayd_id,
136 uint64_t nb_packets_per_stream,
5c786ded 137 struct lttng_consumer_local_data *ctx)
07b86b52
JD
138{
139 int ret;
07b86b52
JD
140 struct lttng_consumer_stream *stream;
141
6a00837f 142 DBG("Kernel consumer snapshot channel %" PRIu64, key);
07b86b52
JD
143
144 rcu_read_lock();
145
07b86b52
JD
146 /* Splice is not supported yet for channel snapshot. */
147 if (channel->output != CONSUMER_CHANNEL_MMAP) {
9381314c
JG
148 ERR("Unsupported output type for channel \"%s\": mmap output is required to record a snapshot",
149 channel->name);
07b86b52
JD
150 ret = -1;
151 goto end;
152 }
153
10a50311 154 cds_list_for_each_entry(stream, &channel->streams.head, send_node) {
923333cd 155 unsigned long consumed_pos, produced_pos;
9ce5646a
MD
156
157 health_code_update();
158
07b86b52
JD
159 /*
160 * Lock stream because we are about to change its state.
161 */
162 pthread_mutex_lock(&stream->lock);
163
d2956687
JG
164 assert(channel->trace_chunk);
165 if (!lttng_trace_chunk_get(channel->trace_chunk)) {
166 /*
167 * Can't happen barring an internal error as the channel
168 * holds a reference to the trace chunk.
169 */
170 ERR("Failed to acquire reference to channel's trace chunk");
171 ret = -1;
172 goto end_unlock;
173 }
174 assert(!stream->trace_chunk);
175 stream->trace_chunk = channel->trace_chunk;
176
29decac3
DG
177 /*
178 * Assign the received relayd ID so we can use it for streaming. The streams
179 * are not visible to anyone so this is OK to change it.
180 */
07b86b52
JD
181 stream->net_seq_idx = relayd_id;
182 channel->relayd_id = relayd_id;
183 if (relayd_id != (uint64_t) -1ULL) {
10a50311 184 ret = consumer_send_relayd_stream(stream, path);
07b86b52
JD
185 if (ret < 0) {
186 ERR("sending stream to relayd");
187 goto end_unlock;
188 }
07b86b52 189 } else {
d2956687
JG
190 ret = consumer_stream_create_output_files(stream,
191 false);
07b86b52 192 if (ret < 0) {
07b86b52
JD
193 goto end_unlock;
194 }
d2956687
JG
195 DBG("Kernel consumer snapshot stream (%" PRIu64 ")",
196 stream->key);
07b86b52
JD
197 }
198
f22dd891 199 ret = kernctl_buffer_flush_empty(stream->wait_fd);
07b86b52 200 if (ret < 0) {
f22dd891
MD
201 /*
202 * Doing a buffer flush which does not take into
203 * account empty packets. This is not perfect
204 * for stream intersection, but required as a
205 * fall-back when "flush_empty" is not
206 * implemented by lttng-modules.
207 */
208 ret = kernctl_buffer_flush(stream->wait_fd);
209 if (ret < 0) {
210 ERR("Failed to flush kernel stream");
211 goto end_unlock;
212 }
07b86b52
JD
213 goto end_unlock;
214 }
215
216 ret = lttng_kconsumer_take_snapshot(stream);
217 if (ret < 0) {
218 ERR("Taking kernel snapshot");
219 goto end_unlock;
220 }
221
222 ret = lttng_kconsumer_get_produced_snapshot(stream, &produced_pos);
223 if (ret < 0) {
224 ERR("Produced kernel snapshot position");
225 goto end_unlock;
226 }
227
228 ret = lttng_kconsumer_get_consumed_snapshot(stream, &consumed_pos);
229 if (ret < 0) {
230 ERR("Consumerd kernel snapshot position");
231 goto end_unlock;
232 }
233
234 if (stream->max_sb_size == 0) {
235 ret = kernctl_get_max_subbuf_size(stream->wait_fd,
236 &stream->max_sb_size);
237 if (ret < 0) {
238 ERR("Getting kernel max_sb_size");
239 goto end_unlock;
240 }
241 }
242
d07ceecd
MD
243 consumed_pos = consumer_get_consume_start_pos(consumed_pos,
244 produced_pos, nb_packets_per_stream,
245 stream->max_sb_size);
5c786ded 246
9377d830 247 while ((long) (consumed_pos - produced_pos) < 0) {
07b86b52
JD
248 ssize_t read_len;
249 unsigned long len, padded_len;
250
9ce5646a
MD
251 health_code_update();
252
07b86b52
JD
253 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos);
254
255 ret = kernctl_get_subbuf(stream->wait_fd, &consumed_pos);
256 if (ret < 0) {
32af2c95 257 if (ret != -EAGAIN) {
07b86b52
JD
258 PERROR("kernctl_get_subbuf snapshot");
259 goto end_unlock;
260 }
261 DBG("Kernel consumer get subbuf failed. Skipping it.");
262 consumed_pos += stream->max_sb_size;
ddc93ee4 263 stream->chan->lost_packets++;
07b86b52
JD
264 continue;
265 }
266
267 ret = kernctl_get_subbuf_size(stream->wait_fd, &len);
268 if (ret < 0) {
269 ERR("Snapshot kernctl_get_subbuf_size");
29decac3 270 goto error_put_subbuf;
07b86b52
JD
271 }
272
273 ret = kernctl_get_padded_subbuf_size(stream->wait_fd, &padded_len);
274 if (ret < 0) {
275 ERR("Snapshot kernctl_get_padded_subbuf_size");
29decac3 276 goto error_put_subbuf;
07b86b52
JD
277 }
278
279 read_len = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, len,
309167d2 280 padded_len - len, NULL);
07b86b52 281 /*
29decac3
DG
282 * We write the padded len in local tracefiles but the data len
283 * when using a relay. Display the error but continue processing
284 * to try to release the subbuffer.
07b86b52
JD
285 */
286 if (relayd_id != (uint64_t) -1ULL) {
287 if (read_len != len) {
288 ERR("Error sending to the relay (ret: %zd != len: %lu)",
289 read_len, len);
290 }
291 } else {
292 if (read_len != padded_len) {
293 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
294 read_len, padded_len);
295 }
296 }
297
298 ret = kernctl_put_subbuf(stream->wait_fd);
299 if (ret < 0) {
300 ERR("Snapshot kernctl_put_subbuf");
301 goto end_unlock;
302 }
303 consumed_pos += stream->max_sb_size;
304 }
305
306 if (relayd_id == (uint64_t) -1ULL) {
fdf9986c
MD
307 if (stream->out_fd >= 0) {
308 ret = close(stream->out_fd);
309 if (ret < 0) {
310 PERROR("Kernel consumer snapshot close out_fd");
311 goto end_unlock;
312 }
313 stream->out_fd = -1;
07b86b52 314 }
07b86b52
JD
315 } else {
316 close_relayd_stream(stream);
317 stream->net_seq_idx = (uint64_t) -1ULL;
318 }
d2956687
JG
319 lttng_trace_chunk_put(stream->trace_chunk);
320 stream->trace_chunk = NULL;
07b86b52
JD
321 pthread_mutex_unlock(&stream->lock);
322 }
323
324 /* All good! */
325 ret = 0;
326 goto end;
327
29decac3
DG
328error_put_subbuf:
329 ret = kernctl_put_subbuf(stream->wait_fd);
330 if (ret < 0) {
331 ERR("Snapshot kernctl_put_subbuf error path");
332 }
07b86b52
JD
333end_unlock:
334 pthread_mutex_unlock(&stream->lock);
335end:
336 rcu_read_unlock();
337 return ret;
338}
339
340/*
341 * Read the whole metadata available for a snapshot.
3eb928aa 342 * RCU read-side lock must be held across this function to ensure existence of
d2956687 343 * metadata_channel. The channel lock must be held by the caller.
07b86b52
JD
344 *
345 * Returns 0 on success, < 0 on error
346 */
d2956687
JG
347static int lttng_kconsumer_snapshot_metadata(
348 struct lttng_consumer_channel *metadata_channel,
3eb928aa
MD
349 uint64_t key, char *path, uint64_t relayd_id,
350 struct lttng_consumer_local_data *ctx)
07b86b52 351{
d771f832
DG
352 int ret, use_relayd = 0;
353 ssize_t ret_read;
07b86b52 354 struct lttng_consumer_stream *metadata_stream;
d771f832
DG
355
356 assert(ctx);
07b86b52
JD
357
358 DBG("Kernel consumer snapshot metadata with key %" PRIu64 " at path %s",
359 key, path);
360
361 rcu_read_lock();
362
07b86b52
JD
363 metadata_stream = metadata_channel->metadata_stream;
364 assert(metadata_stream);
d2956687 365
fa27abe8 366 pthread_mutex_lock(&metadata_stream->lock);
d2956687
JG
367 assert(metadata_channel->trace_chunk);
368 assert(metadata_stream->trace_chunk);
07b86b52 369
d771f832 370 /* Flag once that we have a valid relayd for the stream. */
e2039c7a 371 if (relayd_id != (uint64_t) -1ULL) {
d771f832
DG
372 use_relayd = 1;
373 }
374
375 if (use_relayd) {
10a50311 376 ret = consumer_send_relayd_stream(metadata_stream, path);
e2039c7a 377 if (ret < 0) {
fa27abe8 378 goto error_snapshot;
e2039c7a 379 }
e2039c7a 380 } else {
d2956687
JG
381 ret = consumer_stream_create_output_files(metadata_stream,
382 false);
e2039c7a 383 if (ret < 0) {
fa27abe8 384 goto error_snapshot;
e2039c7a 385 }
07b86b52 386 }
07b86b52 387
d771f832 388 do {
9ce5646a
MD
389 health_code_update();
390
d2956687 391 ret_read = lttng_kconsumer_read_subbuffer(metadata_stream, ctx);
d771f832 392 if (ret_read < 0) {
56591bac 393 if (ret_read != -EAGAIN) {
6a00837f 394 ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
d771f832 395 ret_read);
fa27abe8
JG
396 ret = ret_read;
397 goto error_snapshot;
07b86b52 398 }
d771f832 399 /* ret_read is negative at this point so we will exit the loop. */
07b86b52
JD
400 continue;
401 }
d771f832 402 } while (ret_read >= 0);
07b86b52 403
d771f832
DG
404 if (use_relayd) {
405 close_relayd_stream(metadata_stream);
406 metadata_stream->net_seq_idx = (uint64_t) -1ULL;
407 } else {
fdf9986c
MD
408 if (metadata_stream->out_fd >= 0) {
409 ret = close(metadata_stream->out_fd);
410 if (ret < 0) {
411 PERROR("Kernel consumer snapshot metadata close out_fd");
412 /*
413 * Don't go on error here since the snapshot was successful at this
414 * point but somehow the close failed.
415 */
416 }
417 metadata_stream->out_fd = -1;
d2956687
JG
418 lttng_trace_chunk_put(metadata_stream->trace_chunk);
419 metadata_stream->trace_chunk = NULL;
e2039c7a 420 }
e2039c7a
JD
421 }
422
07b86b52 423 ret = 0;
fa27abe8
JG
424error_snapshot:
425 pthread_mutex_unlock(&metadata_stream->lock);
cf53a8a6
JD
426 cds_list_del(&metadata_stream->send_node);
427 consumer_stream_destroy(metadata_stream, NULL);
428 metadata_channel->metadata_stream = NULL;
07b86b52
JD
429 rcu_read_unlock();
430 return ret;
431}
432
1803a064
MD
433/*
434 * Receive command from session daemon and process it.
435 *
436 * Return 1 on success else a negative value or 0.
437 */
3bd1e081
MD
438int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
439 int sock, struct pollfd *consumer_sockpoll)
440{
441 ssize_t ret;
0c759fc9 442 enum lttcomm_return_code ret_code = LTTCOMM_CONSUMERD_SUCCESS;
3bd1e081
MD
443 struct lttcomm_consumer_msg msg;
444
9ce5646a
MD
445 health_code_update();
446
3bd1e081
MD
447 ret = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg));
448 if (ret != sizeof(msg)) {
1803a064 449 if (ret > 0) {
c6857fcf 450 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_CMD);
1803a064
MD
451 ret = -1;
452 }
3bd1e081
MD
453 return ret;
454 }
9ce5646a
MD
455
456 health_code_update();
457
84382d49
MD
458 /* Deprecated command */
459 assert(msg.cmd_type != LTTNG_CONSUMER_STOP);
3bd1e081 460
9ce5646a
MD
461 health_code_update();
462
b0b335c8
MD
463 /* relayd needs RCU read-side protection */
464 rcu_read_lock();
465
3bd1e081 466 switch (msg.cmd_type) {
00e2e675
DG
467 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET:
468 {
f50f23d9 469 /* Session daemon status message are handled in the following call. */
2527bf85 470 consumer_add_relayd_socket(msg.u.relayd_sock.net_index,
7735ef9e 471 msg.u.relayd_sock.type, ctx, sock, consumer_sockpoll,
d3e2ba59 472 &msg.u.relayd_sock.sock, msg.u.relayd_sock.session_id,
2527bf85 473 msg.u.relayd_sock.relayd_session_id);
00e2e675
DG
474 goto end_nosignal;
475 }
3bd1e081
MD
476 case LTTNG_CONSUMER_ADD_CHANNEL:
477 {
478 struct lttng_consumer_channel *new_channel;
e43c41c5 479 int ret_recv;
d2956687 480 const uint64_t chunk_id = msg.u.channel.chunk_id.value;
3bd1e081 481
9ce5646a
MD
482 health_code_update();
483
f50f23d9
DG
484 /* First send a status message before receiving the fds. */
485 ret = consumer_send_status_msg(sock, ret_code);
486 if (ret < 0) {
487 /* Somehow, the session daemon is not responding anymore. */
1803a064 488 goto error_fatal;
f50f23d9 489 }
9ce5646a
MD
490
491 health_code_update();
492
d88aee68 493 DBG("consumer_add_channel %" PRIu64, msg.u.channel.channel_key);
3bd1e081 494 new_channel = consumer_allocate_channel(msg.u.channel.channel_key,
d2956687
JG
495 msg.u.channel.session_id,
496 msg.u.channel.chunk_id.is_set ?
497 &chunk_id : NULL,
498 msg.u.channel.pathname,
499 msg.u.channel.name,
1624d5b7
JD
500 msg.u.channel.relayd_id, msg.u.channel.output,
501 msg.u.channel.tracefile_size,
1950109e 502 msg.u.channel.tracefile_count, 0,
ecc48a90 503 msg.u.channel.monitor,
d7ba1388 504 msg.u.channel.live_timer_interval,
3d071855 505 NULL, NULL);
3bd1e081 506 if (new_channel == NULL) {
f73fabfd 507 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
3bd1e081
MD
508 goto end_nosignal;
509 }
ffe60014 510 new_channel->nb_init_stream_left = msg.u.channel.nb_init_streams;
95a1109b
JD
511 switch (msg.u.channel.output) {
512 case LTTNG_EVENT_SPLICE:
513 new_channel->output = CONSUMER_CHANNEL_SPLICE;
514 break;
515 case LTTNG_EVENT_MMAP:
516 new_channel->output = CONSUMER_CHANNEL_MMAP;
517 break;
518 default:
519 ERR("Channel output unknown %d", msg.u.channel.output);
520 goto end_nosignal;
521 }
ffe60014
DG
522
523 /* Translate and save channel type. */
524 switch (msg.u.channel.type) {
525 case CONSUMER_CHANNEL_TYPE_DATA:
526 case CONSUMER_CHANNEL_TYPE_METADATA:
527 new_channel->type = msg.u.channel.type;
528 break;
529 default:
530 assert(0);
531 goto end_nosignal;
532 };
533
9ce5646a
MD
534 health_code_update();
535
3bd1e081 536 if (ctx->on_recv_channel != NULL) {
e43c41c5
JD
537 ret_recv = ctx->on_recv_channel(new_channel);
538 if (ret_recv == 0) {
539 ret = consumer_add_channel(new_channel, ctx);
540 } else if (ret_recv < 0) {
3bd1e081
MD
541 goto end_nosignal;
542 }
543 } else {
e43c41c5 544 ret = consumer_add_channel(new_channel, ctx);
3bd1e081 545 }
e9404c27
JG
546 if (msg.u.channel.type == CONSUMER_CHANNEL_TYPE_DATA && !ret) {
547 int monitor_start_ret;
548
549 DBG("Consumer starting monitor timer");
94d49140
JD
550 consumer_timer_live_start(new_channel,
551 msg.u.channel.live_timer_interval);
e9404c27
JG
552 monitor_start_ret = consumer_timer_monitor_start(
553 new_channel,
554 msg.u.channel.monitor_timer_interval);
555 if (monitor_start_ret < 0) {
556 ERR("Starting channel monitoring timer failed");
557 goto end_nosignal;
558 }
559
94d49140 560 }
e43c41c5 561
9ce5646a
MD
562 health_code_update();
563
e43c41c5 564 /* If we received an error in add_channel, we need to report it. */
821fffb2 565 if (ret < 0) {
1803a064
MD
566 ret = consumer_send_status_msg(sock, ret);
567 if (ret < 0) {
568 goto error_fatal;
569 }
e43c41c5
JD
570 goto end_nosignal;
571 }
572
3bd1e081
MD
573 goto end_nosignal;
574 }
575 case LTTNG_CONSUMER_ADD_STREAM:
576 {
dae10966
DG
577 int fd;
578 struct lttng_pipe *stream_pipe;
00e2e675 579 struct lttng_consumer_stream *new_stream;
ffe60014 580 struct lttng_consumer_channel *channel;
c80048c6 581 int alloc_ret = 0;
3bd1e081 582
ffe60014
DG
583 /*
584 * Get stream's channel reference. Needed when adding the stream to the
585 * global hash table.
586 */
587 channel = consumer_find_channel(msg.u.stream.channel_key);
588 if (!channel) {
589 /*
590 * We could not find the channel. Can happen if cpu hotplug
591 * happens while tearing down.
592 */
d88aee68 593 ERR("Unable to find channel key %" PRIu64, msg.u.stream.channel_key);
e462382a 594 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
ffe60014
DG
595 }
596
9ce5646a
MD
597 health_code_update();
598
f50f23d9
DG
599 /* First send a status message before receiving the fds. */
600 ret = consumer_send_status_msg(sock, ret_code);
1803a064 601 if (ret < 0) {
d771f832 602 /* Somehow, the session daemon is not responding anymore. */
1803a064
MD
603 goto error_fatal;
604 }
9ce5646a
MD
605
606 health_code_update();
607
0c759fc9 608 if (ret_code != LTTCOMM_CONSUMERD_SUCCESS) {
d771f832 609 /* Channel was not found. */
f50f23d9
DG
610 goto end_nosignal;
611 }
612
d771f832 613 /* Blocking call */
9ce5646a
MD
614 health_poll_entry();
615 ret = lttng_consumer_poll_socket(consumer_sockpoll);
616 health_poll_exit();
84382d49
MD
617 if (ret) {
618 goto error_fatal;
3bd1e081 619 }
00e2e675 620
9ce5646a
MD
621 health_code_update();
622
00e2e675 623 /* Get stream file descriptor from socket */
f2fc6720
MD
624 ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1);
625 if (ret != sizeof(fd)) {
f73fabfd 626 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD);
3f8e211f 627 rcu_read_unlock();
3bd1e081
MD
628 return ret;
629 }
3bd1e081 630
9ce5646a
MD
631 health_code_update();
632
f50f23d9
DG
633 /*
634 * Send status code to session daemon only if the recv works. If the
635 * above recv() failed, the session daemon is notified through the
636 * error socket and the teardown is eventually done.
637 */
638 ret = consumer_send_status_msg(sock, ret_code);
639 if (ret < 0) {
640 /* Somehow, the session daemon is not responding anymore. */
641 goto end_nosignal;
642 }
643
9ce5646a
MD
644 health_code_update();
645
d2956687 646 pthread_mutex_lock(&channel->lock);
ffe60014
DG
647 new_stream = consumer_allocate_stream(channel->key,
648 fd,
ffe60014 649 channel->name,
ffe60014
DG
650 channel->relayd_id,
651 channel->session_id,
d2956687 652 channel->trace_chunk,
ffe60014
DG
653 msg.u.stream.cpu,
654 &alloc_ret,
4891ece8 655 channel->type,
d2956687 656 channel->monitor);
3bd1e081 657 if (new_stream == NULL) {
c80048c6
MD
658 switch (alloc_ret) {
659 case -ENOMEM:
660 case -EINVAL:
661 default:
662 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
663 break;
c80048c6 664 }
d2956687 665 pthread_mutex_unlock(&channel->lock);
3f8e211f 666 goto end_nosignal;
3bd1e081 667 }
d771f832 668
ffe60014
DG
669 new_stream->chan = channel;
670 new_stream->wait_fd = fd;
d9a2e16e
JD
671 consumer_stream_update_channel_attributes(new_stream,
672 channel);
07b86b52
JD
673 switch (channel->output) {
674 case CONSUMER_CHANNEL_SPLICE:
675 new_stream->output = LTTNG_EVENT_SPLICE;
a2361a61
JD
676 ret = utils_create_pipe(new_stream->splice_pipe);
677 if (ret < 0) {
d2956687 678 pthread_mutex_unlock(&channel->lock);
a2361a61
JD
679 goto end_nosignal;
680 }
07b86b52
JD
681 break;
682 case CONSUMER_CHANNEL_MMAP:
683 new_stream->output = LTTNG_EVENT_MMAP;
684 break;
685 default:
686 ERR("Stream output unknown %d", channel->output);
d2956687 687 pthread_mutex_unlock(&channel->lock);
07b86b52
JD
688 goto end_nosignal;
689 }
00e2e675 690
a0c83db9
DG
691 /*
692 * We've just assigned the channel to the stream so increment the
07b86b52
JD
693 * refcount right now. We don't need to increment the refcount for
694 * streams in no monitor because we handle manually the cleanup of
695 * those. It is very important to make sure there is NO prior
696 * consumer_del_stream() calls or else the refcount will be unbalanced.
a0c83db9 697 */
07b86b52
JD
698 if (channel->monitor) {
699 uatomic_inc(&new_stream->chan->refcount);
700 }
9d9353f9 701
fb3a43a9
DG
702 /*
703 * The buffer flush is done on the session daemon side for the kernel
704 * so no need for the stream "hangup_flush_done" variable to be
705 * tracked. This is important for a kernel stream since we don't rely
706 * on the flush state of the stream to read data. It's not the case for
707 * user space tracing.
708 */
709 new_stream->hangup_flush_done = 0;
710
9ce5646a
MD
711 health_code_update();
712
d2956687 713 pthread_mutex_lock(&new_stream->lock);
633d0084
DG
714 if (ctx->on_recv_stream) {
715 ret = ctx->on_recv_stream(new_stream);
716 if (ret < 0) {
d2956687
JG
717 pthread_mutex_unlock(&new_stream->lock);
718 pthread_mutex_unlock(&channel->lock);
d771f832 719 consumer_stream_free(new_stream);
633d0084 720 goto end_nosignal;
fb3a43a9 721 }
633d0084 722 }
9ce5646a
MD
723 health_code_update();
724
07b86b52
JD
725 if (new_stream->metadata_flag) {
726 channel->metadata_stream = new_stream;
727 }
728
2bba9e53
DG
729 /* Do not monitor this stream. */
730 if (!channel->monitor) {
5eecee74 731 DBG("Kernel consumer add stream %s in no monitor mode with "
6dc3064a 732 "relayd id %" PRIu64, new_stream->name,
5eecee74 733 new_stream->net_seq_idx);
10a50311 734 cds_list_add(&new_stream->send_node, &channel->streams.head);
d2956687
JG
735 pthread_mutex_unlock(&new_stream->lock);
736 pthread_mutex_unlock(&channel->lock);
6dc3064a
DG
737 break;
738 }
739
e1b71bdc
DG
740 /* Send stream to relayd if the stream has an ID. */
741 if (new_stream->net_seq_idx != (uint64_t) -1ULL) {
194ee077
DG
742 ret = consumer_send_relayd_stream(new_stream,
743 new_stream->chan->pathname);
e1b71bdc 744 if (ret < 0) {
d2956687
JG
745 pthread_mutex_unlock(&new_stream->lock);
746 pthread_mutex_unlock(&channel->lock);
e1b71bdc
DG
747 consumer_stream_free(new_stream);
748 goto end_nosignal;
749 }
001b7e62
MD
750
751 /*
752 * If adding an extra stream to an already
753 * existing channel (e.g. cpu hotplug), we need
754 * to send the "streams_sent" command to relayd.
755 */
756 if (channel->streams_sent_to_relayd) {
757 ret = consumer_send_relayd_streams_sent(
758 new_stream->net_seq_idx);
759 if (ret < 0) {
d2956687
JG
760 pthread_mutex_unlock(&new_stream->lock);
761 pthread_mutex_unlock(&channel->lock);
001b7e62
MD
762 goto end_nosignal;
763 }
764 }
e2039c7a 765 }
d2956687
JG
766 pthread_mutex_unlock(&new_stream->lock);
767 pthread_mutex_unlock(&channel->lock);
e2039c7a 768
50f8ae69 769 /* Get the right pipe where the stream will be sent. */
633d0084 770 if (new_stream->metadata_flag) {
66d583dc 771 consumer_add_metadata_stream(new_stream);
dae10966 772 stream_pipe = ctx->consumer_metadata_pipe;
3bd1e081 773 } else {
66d583dc 774 consumer_add_data_stream(new_stream);
dae10966 775 stream_pipe = ctx->consumer_data_pipe;
50f8ae69
DG
776 }
777
66d583dc 778 /* Visible to other threads */
5ab66908
MD
779 new_stream->globally_visible = 1;
780
9ce5646a
MD
781 health_code_update();
782
dae10966 783 ret = lttng_pipe_write(stream_pipe, &new_stream, sizeof(new_stream));
50f8ae69 784 if (ret < 0) {
dae10966 785 ERR("Consumer write %s stream to pipe %d",
50f8ae69 786 new_stream->metadata_flag ? "metadata" : "data",
dae10966 787 lttng_pipe_get_writefd(stream_pipe));
5ab66908
MD
788 if (new_stream->metadata_flag) {
789 consumer_del_stream_for_metadata(new_stream);
790 } else {
791 consumer_del_stream_for_data(new_stream);
792 }
50f8ae69 793 goto end_nosignal;
3bd1e081 794 }
00e2e675 795
02d02e31
JD
796 DBG("Kernel consumer ADD_STREAM %s (fd: %d) %s with relayd id %" PRIu64,
797 new_stream->name, fd, new_stream->chan->pathname, new_stream->relayd_stream_id);
3bd1e081
MD
798 break;
799 }
a4baae1b
JD
800 case LTTNG_CONSUMER_STREAMS_SENT:
801 {
802 struct lttng_consumer_channel *channel;
803
804 /*
805 * Get stream's channel reference. Needed when adding the stream to the
806 * global hash table.
807 */
808 channel = consumer_find_channel(msg.u.sent_streams.channel_key);
809 if (!channel) {
810 /*
811 * We could not find the channel. Can happen if cpu hotplug
812 * happens while tearing down.
813 */
814 ERR("Unable to find channel key %" PRIu64,
815 msg.u.sent_streams.channel_key);
e462382a 816 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
a4baae1b
JD
817 }
818
819 health_code_update();
820
821 /*
822 * Send status code to session daemon.
823 */
824 ret = consumer_send_status_msg(sock, ret_code);
f261ad0a 825 if (ret < 0 || ret_code != LTTCOMM_CONSUMERD_SUCCESS) {
a4baae1b
JD
826 /* Somehow, the session daemon is not responding anymore. */
827 goto end_nosignal;
828 }
829
830 health_code_update();
831
832 /*
833 * We should not send this message if we don't monitor the
834 * streams in this channel.
835 */
836 if (!channel->monitor) {
837 break;
838 }
839
840 health_code_update();
841 /* Send stream to relayd if the stream has an ID. */
842 if (msg.u.sent_streams.net_seq_idx != (uint64_t) -1ULL) {
843 ret = consumer_send_relayd_streams_sent(
844 msg.u.sent_streams.net_seq_idx);
845 if (ret < 0) {
846 goto end_nosignal;
847 }
001b7e62 848 channel->streams_sent_to_relayd = true;
a4baae1b
JD
849 }
850 break;
851 }
3bd1e081
MD
852 case LTTNG_CONSUMER_UPDATE_STREAM:
853 {
3f8e211f
DG
854 rcu_read_unlock();
855 return -ENOSYS;
856 }
857 case LTTNG_CONSUMER_DESTROY_RELAYD:
858 {
a6ba4fe1 859 uint64_t index = msg.u.destroy_relayd.net_seq_idx;
3f8e211f
DG
860 struct consumer_relayd_sock_pair *relayd;
861
a6ba4fe1 862 DBG("Kernel consumer destroying relayd %" PRIu64, index);
3f8e211f
DG
863
864 /* Get relayd reference if exists. */
a6ba4fe1 865 relayd = consumer_find_relayd(index);
3f8e211f 866 if (relayd == NULL) {
3448e266 867 DBG("Unable to find relayd %" PRIu64, index);
e462382a 868 ret_code = LTTCOMM_CONSUMERD_RELAYD_FAIL;
3bd1e081 869 }
3f8e211f 870
a6ba4fe1
DG
871 /*
872 * Each relayd socket pair has a refcount of stream attached to it
873 * which tells if the relayd is still active or not depending on the
874 * refcount value.
875 *
876 * This will set the destroy flag of the relayd object and destroy it
877 * if the refcount reaches zero when called.
878 *
879 * The destroy can happen either here or when a stream fd hangs up.
880 */
f50f23d9
DG
881 if (relayd) {
882 consumer_flag_relayd_for_destroy(relayd);
883 }
884
9ce5646a
MD
885 health_code_update();
886
f50f23d9
DG
887 ret = consumer_send_status_msg(sock, ret_code);
888 if (ret < 0) {
889 /* Somehow, the session daemon is not responding anymore. */
1803a064 890 goto error_fatal;
f50f23d9 891 }
3f8e211f 892
3f8e211f 893 goto end_nosignal;
3bd1e081 894 }
6d805429 895 case LTTNG_CONSUMER_DATA_PENDING:
53632229 896 {
c8f59ee5 897 int32_t ret;
6d805429 898 uint64_t id = msg.u.data_pending.session_id;
c8f59ee5 899
6d805429 900 DBG("Kernel consumer data pending command for id %" PRIu64, id);
c8f59ee5 901
6d805429 902 ret = consumer_data_pending(id);
c8f59ee5 903
9ce5646a
MD
904 health_code_update();
905
c8f59ee5
DG
906 /* Send back returned value to session daemon */
907 ret = lttcomm_send_unix_sock(sock, &ret, sizeof(ret));
908 if (ret < 0) {
6d805429 909 PERROR("send data pending ret code");
1803a064 910 goto error_fatal;
c8f59ee5 911 }
f50f23d9
DG
912
913 /*
914 * No need to send back a status message since the data pending
915 * returned value is the response.
916 */
c8f59ee5 917 break;
53632229 918 }
6dc3064a
DG
919 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL:
920 {
3eb928aa
MD
921 struct lttng_consumer_channel *channel;
922 uint64_t key = msg.u.snapshot_channel.key;
923
924 channel = consumer_find_channel(key);
925 if (!channel) {
926 ERR("Channel %" PRIu64 " not found", key);
927 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
07b86b52 928 } else {
d2956687 929 pthread_mutex_lock(&channel->lock);
3eb928aa
MD
930 if (msg.u.snapshot_channel.metadata == 1) {
931 ret = lttng_kconsumer_snapshot_metadata(channel, key,
932 msg.u.snapshot_channel.pathname,
933 msg.u.snapshot_channel.relayd_id, ctx);
934 if (ret < 0) {
935 ERR("Snapshot metadata failed");
936 ret_code = LTTCOMM_CONSUMERD_SNAPSHOT_FAILED;
937 }
938 } else {
939 ret = lttng_kconsumer_snapshot_channel(channel, key,
940 msg.u.snapshot_channel.pathname,
941 msg.u.snapshot_channel.relayd_id,
942 msg.u.snapshot_channel.nb_packets_per_stream,
943 ctx);
944 if (ret < 0) {
945 ERR("Snapshot channel failed");
946 ret_code = LTTCOMM_CONSUMERD_SNAPSHOT_FAILED;
947 }
07b86b52 948 }
d2956687 949 pthread_mutex_unlock(&channel->lock);
07b86b52 950 }
9ce5646a
MD
951 health_code_update();
952
6dc3064a
DG
953 ret = consumer_send_status_msg(sock, ret_code);
954 if (ret < 0) {
955 /* Somehow, the session daemon is not responding anymore. */
956 goto end_nosignal;
957 }
958 break;
959 }
07b86b52
JD
960 case LTTNG_CONSUMER_DESTROY_CHANNEL:
961 {
962 uint64_t key = msg.u.destroy_channel.key;
963 struct lttng_consumer_channel *channel;
964
965 channel = consumer_find_channel(key);
966 if (!channel) {
967 ERR("Kernel consumer destroy channel %" PRIu64 " not found", key);
e462382a 968 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
07b86b52
JD
969 }
970
9ce5646a
MD
971 health_code_update();
972
07b86b52
JD
973 ret = consumer_send_status_msg(sock, ret_code);
974 if (ret < 0) {
975 /* Somehow, the session daemon is not responding anymore. */
976 goto end_nosignal;
977 }
978
9ce5646a
MD
979 health_code_update();
980
15dc512a
DG
981 /* Stop right now if no channel was found. */
982 if (!channel) {
983 goto end_nosignal;
984 }
985
07b86b52
JD
986 /*
987 * This command should ONLY be issued for channel with streams set in
988 * no monitor mode.
989 */
990 assert(!channel->monitor);
991
992 /*
993 * The refcount should ALWAYS be 0 in the case of a channel in no
994 * monitor mode.
995 */
996 assert(!uatomic_sub_return(&channel->refcount, 1));
997
998 consumer_del_channel(channel);
999
1000 goto end_nosignal;
1001 }
fb83fe64
JD
1002 case LTTNG_CONSUMER_DISCARDED_EVENTS:
1003 {
66ab32be
JD
1004 ssize_t ret;
1005 uint64_t count;
fb83fe64
JD
1006 struct lttng_consumer_channel *channel;
1007 uint64_t id = msg.u.discarded_events.session_id;
1008 uint64_t key = msg.u.discarded_events.channel_key;
1009
e5742757
MD
1010 DBG("Kernel consumer discarded events command for session id %"
1011 PRIu64 ", channel key %" PRIu64, id, key);
1012
fb83fe64
JD
1013 channel = consumer_find_channel(key);
1014 if (!channel) {
1015 ERR("Kernel consumer discarded events channel %"
1016 PRIu64 " not found", key);
66ab32be 1017 count = 0;
e5742757 1018 } else {
66ab32be 1019 count = channel->discarded_events;
fb83fe64
JD
1020 }
1021
fb83fe64
JD
1022 health_code_update();
1023
1024 /* Send back returned value to session daemon */
66ab32be 1025 ret = lttcomm_send_unix_sock(sock, &count, sizeof(count));
fb83fe64
JD
1026 if (ret < 0) {
1027 PERROR("send discarded events");
1028 goto error_fatal;
1029 }
1030
1031 break;
1032 }
1033 case LTTNG_CONSUMER_LOST_PACKETS:
1034 {
66ab32be
JD
1035 ssize_t ret;
1036 uint64_t count;
fb83fe64
JD
1037 struct lttng_consumer_channel *channel;
1038 uint64_t id = msg.u.lost_packets.session_id;
1039 uint64_t key = msg.u.lost_packets.channel_key;
1040
e5742757
MD
1041 DBG("Kernel consumer lost packets command for session id %"
1042 PRIu64 ", channel key %" PRIu64, id, key);
1043
fb83fe64
JD
1044 channel = consumer_find_channel(key);
1045 if (!channel) {
1046 ERR("Kernel consumer lost packets channel %"
1047 PRIu64 " not found", key);
66ab32be 1048 count = 0;
e5742757 1049 } else {
66ab32be 1050 count = channel->lost_packets;
fb83fe64
JD
1051 }
1052
fb83fe64
JD
1053 health_code_update();
1054
1055 /* Send back returned value to session daemon */
66ab32be 1056 ret = lttcomm_send_unix_sock(sock, &count, sizeof(count));
fb83fe64
JD
1057 if (ret < 0) {
1058 PERROR("send lost packets");
1059 goto error_fatal;
1060 }
1061
1062 break;
1063 }
b3530820
JG
1064 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE:
1065 {
1066 int channel_monitor_pipe;
1067
1068 ret_code = LTTCOMM_CONSUMERD_SUCCESS;
1069 /* Successfully received the command's type. */
1070 ret = consumer_send_status_msg(sock, ret_code);
1071 if (ret < 0) {
1072 goto error_fatal;
1073 }
1074
1075 ret = lttcomm_recv_fds_unix_sock(sock, &channel_monitor_pipe,
1076 1);
1077 if (ret != sizeof(channel_monitor_pipe)) {
1078 ERR("Failed to receive channel monitor pipe");
1079 goto error_fatal;
1080 }
1081
1082 DBG("Received channel monitor pipe (%d)", channel_monitor_pipe);
1083 ret = consumer_timer_thread_set_channel_monitor_pipe(
1084 channel_monitor_pipe);
1085 if (!ret) {
1086 int flags;
1087
1088 ret_code = LTTCOMM_CONSUMERD_SUCCESS;
1089 /* Set the pipe as non-blocking. */
1090 ret = fcntl(channel_monitor_pipe, F_GETFL, 0);
1091 if (ret == -1) {
1092 PERROR("fcntl get flags of the channel monitoring pipe");
1093 goto error_fatal;
1094 }
1095 flags = ret;
1096
1097 ret = fcntl(channel_monitor_pipe, F_SETFL,
1098 flags | O_NONBLOCK);
1099 if (ret == -1) {
1100 PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe");
1101 goto error_fatal;
1102 }
1103 DBG("Channel monitor pipe set as non-blocking");
1104 } else {
1105 ret_code = LTTCOMM_CONSUMERD_ALREADY_SET;
1106 }
1107 ret = consumer_send_status_msg(sock, ret_code);
1108 if (ret < 0) {
1109 goto error_fatal;
1110 }
1111 break;
1112 }
b99a8d42
JD
1113 case LTTNG_CONSUMER_ROTATE_CHANNEL:
1114 {
92b7a7f8
MD
1115 struct lttng_consumer_channel *channel;
1116 uint64_t key = msg.u.rotate_channel.key;
b99a8d42 1117
92b7a7f8 1118 DBG("Consumer rotate channel %" PRIu64, key);
b99a8d42 1119
92b7a7f8
MD
1120 channel = consumer_find_channel(key);
1121 if (!channel) {
1122 ERR("Channel %" PRIu64 " not found", key);
1123 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
1124 } else {
1125 /*
1126 * Sample the rotate position of all the streams in this channel.
1127 */
1128 ret = lttng_consumer_rotate_channel(channel, key,
92b7a7f8
MD
1129 msg.u.rotate_channel.relayd_id,
1130 msg.u.rotate_channel.metadata,
92b7a7f8
MD
1131 ctx);
1132 if (ret < 0) {
1133 ERR("Rotate channel failed");
1134 ret_code = LTTCOMM_CONSUMERD_ROTATION_FAIL;
1135 }
b99a8d42 1136
92b7a7f8
MD
1137 health_code_update();
1138 }
b99a8d42
JD
1139 ret = consumer_send_status_msg(sock, ret_code);
1140 if (ret < 0) {
1141 /* Somehow, the session daemon is not responding anymore. */
1142 goto end_nosignal;
1143 }
92b7a7f8
MD
1144 if (channel) {
1145 /* Rotate the streams that are ready right now. */
1146 ret = lttng_consumer_rotate_ready_streams(
1147 channel, key, ctx);
1148 if (ret < 0) {
1149 ERR("Rotate ready streams failed");
1150 }
b99a8d42
JD
1151 }
1152
1153 break;
1154 }
d2956687 1155 case LTTNG_CONSUMER_INIT:
00fb02ac 1156 {
d2956687
JG
1157 ret_code = lttng_consumer_init_command(ctx,
1158 msg.u.init.sessiond_uuid);
00fb02ac 1159 health_code_update();
00fb02ac
JD
1160 ret = consumer_send_status_msg(sock, ret_code);
1161 if (ret < 0) {
1162 /* Somehow, the session daemon is not responding anymore. */
1163 goto end_nosignal;
1164 }
1165 break;
1166 }
d2956687 1167 case LTTNG_CONSUMER_CREATE_TRACE_CHUNK:
d88744a4 1168 {
d2956687
JG
1169 const struct lttng_credentials credentials = {
1170 .uid = msg.u.create_trace_chunk.credentials.uid,
1171 .gid = msg.u.create_trace_chunk.credentials.gid,
1172 };
1173 const bool is_local_trace =
1174 !msg.u.create_trace_chunk.relayd_id.is_set;
1175 const uint64_t relayd_id =
1176 msg.u.create_trace_chunk.relayd_id.value;
1177 const char *chunk_override_name =
1178 *msg.u.create_trace_chunk.override_name ?
1179 msg.u.create_trace_chunk.override_name :
1180 NULL;
1181 LTTNG_OPTIONAL(struct lttng_directory_handle) chunk_directory_handle =
1182 LTTNG_OPTIONAL_INIT;
d88744a4 1183
d2956687
JG
1184 /*
1185 * The session daemon will only provide a chunk directory file
1186 * descriptor for local traces.
1187 */
1188 if (is_local_trace) {
1189 int chunk_dirfd;
19990ed5 1190
d2956687
JG
1191 /* Acnowledge the reception of the command. */
1192 ret = consumer_send_status_msg(sock,
1193 LTTCOMM_CONSUMERD_SUCCESS);
1194 if (ret < 0) {
1195 /* Somehow, the session daemon is not responding anymore. */
1196 goto end_nosignal;
1197 }
92816cc3 1198
d2956687
JG
1199 ret = lttcomm_recv_fds_unix_sock(sock, &chunk_dirfd, 1);
1200 if (ret != sizeof(chunk_dirfd)) {
1201 ERR("Failed to receive trace chunk directory file descriptor");
1202 goto error_fatal;
1203 }
92816cc3 1204
d2956687
JG
1205 DBG("Received trace chunk directory fd (%d)",
1206 chunk_dirfd);
1207 ret = lttng_directory_handle_init_from_dirfd(
1208 &chunk_directory_handle.value,
1209 chunk_dirfd);
1210 if (ret) {
1211 ERR("Failed to initialize chunk directory handle from directory file descriptor");
1212 if (close(chunk_dirfd)) {
1213 PERROR("Failed to close chunk directory file descriptor");
1214 }
1215 goto error_fatal;
1216 }
1217 chunk_directory_handle.is_set = true;
92816cc3
JG
1218 }
1219
d2956687
JG
1220 ret_code = lttng_consumer_create_trace_chunk(
1221 !is_local_trace ? &relayd_id : NULL,
1222 msg.u.create_trace_chunk.session_id,
1223 msg.u.create_trace_chunk.chunk_id,
1224 (time_t) msg.u.create_trace_chunk.creation_timestamp,
1225 chunk_override_name,
1226 &credentials,
1227 chunk_directory_handle.is_set ?
1228 &chunk_directory_handle.value :
1229 NULL);
92816cc3 1230
d2956687
JG
1231 if (chunk_directory_handle.is_set) {
1232 lttng_directory_handle_fini(
1233 &chunk_directory_handle.value);
d88744a4 1234 }
d2956687 1235 goto end_msg_sessiond;
d88744a4 1236 }
d2956687 1237 case LTTNG_CONSUMER_CLOSE_TRACE_CHUNK:
a1ae2ea5 1238 {
d2956687
JG
1239 const uint64_t relayd_id =
1240 msg.u.close_trace_chunk.relayd_id.value;
1241
1242 ret_code = lttng_consumer_close_trace_chunk(
1243 msg.u.close_trace_chunk.relayd_id.is_set ?
1244 &relayd_id : NULL,
1245 msg.u.close_trace_chunk.session_id,
1246 msg.u.close_trace_chunk.chunk_id,
1247 (time_t) msg.u.close_trace_chunk.close_timestamp);
1248 goto end_msg_sessiond;
3654ed19 1249 }
d2956687 1250 case LTTNG_CONSUMER_TRACE_CHUNK_EXISTS:
3654ed19 1251 {
d2956687
JG
1252 const uint64_t relayd_id =
1253 msg.u.trace_chunk_exists.relayd_id.value;
1254
1255 ret_code = lttng_consumer_trace_chunk_exists(
1256 msg.u.trace_chunk_exists.relayd_id.is_set ?
1257 &relayd_id : NULL,
1258 msg.u.trace_chunk_exists.session_id,
1259 msg.u.trace_chunk_exists.chunk_id);
1260 goto end_msg_sessiond;
a1ae2ea5 1261 }
3bd1e081 1262 default:
3f8e211f 1263 goto end_nosignal;
3bd1e081 1264 }
3f8e211f 1265
3bd1e081 1266end_nosignal:
b0b335c8 1267 rcu_read_unlock();
4cbc1a04
DG
1268
1269 /*
1270 * Return 1 to indicate success since the 0 value can be a socket
1271 * shutdown during the recv() or send() call.
1272 */
9ce5646a 1273 health_code_update();
4cbc1a04 1274 return 1;
1803a064 1275
d2956687
JG
1276end_msg_sessiond:
1277 /*
1278 * The returned value here is not useful since either way we'll return 1 to
1279 * the caller because the session daemon socket management is done
1280 * elsewhere. Returning a negative code or 0 will shutdown the consumer.
1281 */
1282 ret = consumer_send_status_msg(sock, ret_code);
1283 if (ret < 0) {
1284 goto error_fatal;
1285 }
1286 rcu_read_unlock();
1287
1288 health_code_update();
1289
1290 return 1;
1291
1803a064
MD
1292error_fatal:
1293 rcu_read_unlock();
1294 /* This will issue a consumer stop. */
1295 return -1;
3bd1e081 1296}
d41f73b7 1297
309167d2
JD
1298/*
1299 * Populate index values of a kernel stream. Values are set in big endian order.
1300 *
1301 * Return 0 on success or else a negative value.
1302 */
50adc264 1303static int get_index_values(struct ctf_packet_index *index, int infd)
309167d2
JD
1304{
1305 int ret;
1306
1307 ret = kernctl_get_timestamp_begin(infd, &index->timestamp_begin);
1308 if (ret < 0) {
1309 PERROR("kernctl_get_timestamp_begin");
1310 goto error;
1311 }
1312 index->timestamp_begin = htobe64(index->timestamp_begin);
1313
1314 ret = kernctl_get_timestamp_end(infd, &index->timestamp_end);
1315 if (ret < 0) {
1316 PERROR("kernctl_get_timestamp_end");
1317 goto error;
1318 }
1319 index->timestamp_end = htobe64(index->timestamp_end);
1320
1321 ret = kernctl_get_events_discarded(infd, &index->events_discarded);
1322 if (ret < 0) {
1323 PERROR("kernctl_get_events_discarded");
1324 goto error;
1325 }
1326 index->events_discarded = htobe64(index->events_discarded);
1327
1328 ret = kernctl_get_content_size(infd, &index->content_size);
1329 if (ret < 0) {
1330 PERROR("kernctl_get_content_size");
1331 goto error;
1332 }
1333 index->content_size = htobe64(index->content_size);
1334
1335 ret = kernctl_get_packet_size(infd, &index->packet_size);
1336 if (ret < 0) {
1337 PERROR("kernctl_get_packet_size");
1338 goto error;
1339 }
1340 index->packet_size = htobe64(index->packet_size);
1341
1342 ret = kernctl_get_stream_id(infd, &index->stream_id);
1343 if (ret < 0) {
1344 PERROR("kernctl_get_stream_id");
1345 goto error;
1346 }
1347 index->stream_id = htobe64(index->stream_id);
1348
234cd636
JD
1349 ret = kernctl_get_instance_id(infd, &index->stream_instance_id);
1350 if (ret < 0) {
f0b03c22
MD
1351 if (ret == -ENOTTY) {
1352 /* Command not implemented by lttng-modules. */
1353 index->stream_instance_id = -1ULL;
f0b03c22
MD
1354 } else {
1355 PERROR("kernctl_get_instance_id");
1356 goto error;
1357 }
234cd636
JD
1358 }
1359 index->stream_instance_id = htobe64(index->stream_instance_id);
1360
1361 ret = kernctl_get_sequence_number(infd, &index->packet_seq_num);
1362 if (ret < 0) {
f0b03c22
MD
1363 if (ret == -ENOTTY) {
1364 /* Command not implemented by lttng-modules. */
1365 index->packet_seq_num = -1ULL;
1366 ret = 0;
1367 } else {
1368 PERROR("kernctl_get_sequence_number");
1369 goto error;
1370 }
234cd636
JD
1371 }
1372 index->packet_seq_num = htobe64(index->packet_seq_num);
1373
309167d2
JD
1374error:
1375 return ret;
1376}
94d49140
JD
1377/*
1378 * Sync metadata meaning request them to the session daemon and snapshot to the
1379 * metadata thread can consumer them.
1380 *
1381 * Metadata stream lock MUST be acquired.
1382 *
1383 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
1384 * is empty or a negative value on error.
1385 */
1386int lttng_kconsumer_sync_metadata(struct lttng_consumer_stream *metadata)
1387{
1388 int ret;
1389
1390 assert(metadata);
1391
1392 ret = kernctl_buffer_flush(metadata->wait_fd);
1393 if (ret < 0) {
1394 ERR("Failed to flush kernel stream");
1395 goto end;
1396 }
1397
1398 ret = kernctl_snapshot(metadata->wait_fd);
1399 if (ret < 0) {
32af2c95 1400 if (ret != -EAGAIN) {
94d49140
JD
1401 ERR("Sync metadata, taking kernel snapshot failed.");
1402 goto end;
1403 }
1404 DBG("Sync metadata, no new kernel metadata");
1405 /* No new metadata, exit. */
1406 ret = ENODATA;
1407 goto end;
1408 }
1409
1410end:
1411 return ret;
1412}
309167d2 1413
fb83fe64
JD
1414static
1415int update_stream_stats(struct lttng_consumer_stream *stream)
1416{
1417 int ret;
1418 uint64_t seq, discarded;
1419
1420 ret = kernctl_get_sequence_number(stream->wait_fd, &seq);
1421 if (ret < 0) {
f0b03c22
MD
1422 if (ret == -ENOTTY) {
1423 /* Command not implemented by lttng-modules. */
1424 seq = -1ULL;
f0b03c22
MD
1425 } else {
1426 PERROR("kernctl_get_sequence_number");
1427 goto end;
1428 }
fb83fe64
JD
1429 }
1430
1431 /*
1432 * Start the sequence when we extract the first packet in case we don't
1433 * start at 0 (for example if a consumer is not connected to the
1434 * session immediately after the beginning).
1435 */
1436 if (stream->last_sequence_number == -1ULL) {
1437 stream->last_sequence_number = seq;
1438 } else if (seq > stream->last_sequence_number) {
1439 stream->chan->lost_packets += seq -
1440 stream->last_sequence_number - 1;
1441 } else {
1442 /* seq <= last_sequence_number */
1443 ERR("Sequence number inconsistent : prev = %" PRIu64
1444 ", current = %" PRIu64,
1445 stream->last_sequence_number, seq);
1446 ret = -1;
1447 goto end;
1448 }
1449 stream->last_sequence_number = seq;
1450
1451 ret = kernctl_get_events_discarded(stream->wait_fd, &discarded);
1452 if (ret < 0) {
1453 PERROR("kernctl_get_events_discarded");
1454 goto end;
1455 }
1456 if (discarded < stream->last_discarded_events) {
1457 /*
83f4233d
MJ
1458 * Overflow has occurred. We assume only one wrap-around
1459 * has occurred.
fb83fe64
JD
1460 */
1461 stream->chan->discarded_events += (1ULL << (CAA_BITS_PER_LONG - 1)) -
1462 stream->last_discarded_events + discarded;
1463 } else {
1464 stream->chan->discarded_events += discarded -
1465 stream->last_discarded_events;
1466 }
1467 stream->last_discarded_events = discarded;
1468 ret = 0;
1469
1470end:
1471 return ret;
1472}
1473
93ec662e
JD
1474/*
1475 * Check if the local version of the metadata stream matches with the version
1476 * of the metadata stream in the kernel. If it was updated, set the reset flag
1477 * on the stream.
1478 */
1479static
1480int metadata_stream_check_version(int infd, struct lttng_consumer_stream *stream)
1481{
1482 int ret;
1483 uint64_t cur_version;
1484
1485 ret = kernctl_get_metadata_version(infd, &cur_version);
1486 if (ret < 0) {
f0b03c22
MD
1487 if (ret == -ENOTTY) {
1488 /*
1489 * LTTng-modules does not implement this
1490 * command.
1491 */
1492 ret = 0;
1493 goto end;
1494 }
93ec662e
JD
1495 ERR("Failed to get the metadata version");
1496 goto end;
1497 }
1498
1499 if (stream->metadata_version == cur_version) {
1500 ret = 0;
1501 goto end;
1502 }
1503
1504 DBG("New metadata version detected");
1505 stream->metadata_version = cur_version;
1506 stream->reset_metadata_flag = 1;
1507 ret = 0;
1508
1509end:
1510 return ret;
1511}
1512
d41f73b7
MD
1513/*
1514 * Consume data on a file descriptor and write it on a trace file.
d2956687 1515 * The stream and channel locks must be held by the caller.
d41f73b7 1516 */
4078b776 1517ssize_t lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream *stream,
d2956687 1518 struct lttng_consumer_local_data *ctx)
d41f73b7 1519{
1d4dfdef 1520 unsigned long len, subbuf_size, padding;
02d02e31 1521 int err, write_index = 1, rotation_ret;
4078b776 1522 ssize_t ret = 0;
d41f73b7 1523 int infd = stream->wait_fd;
50adc264 1524 struct ctf_packet_index index;
d41f73b7
MD
1525
1526 DBG("In read_subbuffer (infd : %d)", infd);
309167d2 1527
02d02e31
JD
1528 /*
1529 * If the stream was flagged to be ready for rotation before we extract the
1530 * next packet, rotate it now.
1531 */
1532 if (stream->rotate_ready) {
1533 DBG("Rotate stream before extracting data");
d2956687 1534 rotation_ret = lttng_consumer_rotate_stream(ctx, stream);
02d02e31
JD
1535 if (rotation_ret < 0) {
1536 ERR("Stream rotation error");
1537 ret = -1;
1538 goto error;
1539 }
1540 }
1541
d41f73b7
MD
1542 /* Get the next subbuffer */
1543 err = kernctl_get_next_subbuf(infd);
1544 if (err != 0) {
d41f73b7
MD
1545 /*
1546 * This is a debug message even for single-threaded consumer,
1547 * because poll() have more relaxed criterions than get subbuf,
1548 * so get_subbuf may fail for short race windows where poll()
1549 * would issue wakeups.
1550 */
1551 DBG("Reserving sub buffer failed (everything is normal, "
1552 "it is due to concurrency)");
32af2c95 1553 ret = err;
02d02e31 1554 goto error;
d41f73b7
MD
1555 }
1556
1d4dfdef
DG
1557 /* Get the full subbuffer size including padding */
1558 err = kernctl_get_padded_subbuf_size(infd, &len);
1559 if (err != 0) {
5a510c9f 1560 PERROR("Getting sub-buffer len failed.");
8265f19e
MD
1561 err = kernctl_put_subbuf(infd);
1562 if (err != 0) {
32af2c95 1563 if (err == -EFAULT) {
5a510c9f 1564 PERROR("Error in unreserving sub buffer\n");
32af2c95 1565 } else if (err == -EIO) {
8265f19e 1566 /* Should never happen with newer LTTng versions */
5a510c9f 1567 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
8265f19e 1568 }
32af2c95 1569 ret = err;
02d02e31 1570 goto error;
8265f19e 1571 }
32af2c95 1572 ret = err;
02d02e31 1573 goto error;
1d4dfdef
DG
1574 }
1575
1c20f0e2 1576 if (!stream->metadata_flag) {
309167d2
JD
1577 ret = get_index_values(&index, infd);
1578 if (ret < 0) {
8265f19e
MD
1579 err = kernctl_put_subbuf(infd);
1580 if (err != 0) {
32af2c95 1581 if (err == -EFAULT) {
5a510c9f 1582 PERROR("Error in unreserving sub buffer\n");
32af2c95 1583 } else if (err == -EIO) {
8265f19e 1584 /* Should never happen with newer LTTng versions */
5a510c9f 1585 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
8265f19e 1586 }
32af2c95 1587 ret = err;
02d02e31 1588 goto error;
8265f19e 1589 }
02d02e31 1590 goto error;
309167d2 1591 }
fb83fe64
JD
1592 ret = update_stream_stats(stream);
1593 if (ret < 0) {
7b87473d
MD
1594 err = kernctl_put_subbuf(infd);
1595 if (err != 0) {
1596 if (err == -EFAULT) {
1597 PERROR("Error in unreserving sub buffer\n");
1598 } else if (err == -EIO) {
1599 /* Should never happen with newer LTTng versions */
1600 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1601 }
1602 ret = err;
02d02e31 1603 goto error;
7b87473d 1604 }
02d02e31 1605 goto error;
fb83fe64 1606 }
1c20f0e2
JD
1607 } else {
1608 write_index = 0;
93ec662e
JD
1609 ret = metadata_stream_check_version(infd, stream);
1610 if (ret < 0) {
7b87473d
MD
1611 err = kernctl_put_subbuf(infd);
1612 if (err != 0) {
1613 if (err == -EFAULT) {
1614 PERROR("Error in unreserving sub buffer\n");
1615 } else if (err == -EIO) {
1616 /* Should never happen with newer LTTng versions */
1617 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1618 }
1619 ret = err;
02d02e31 1620 goto error;
7b87473d 1621 }
02d02e31 1622 goto error;
93ec662e 1623 }
309167d2
JD
1624 }
1625
ffe60014 1626 switch (stream->chan->output) {
07b86b52 1627 case CONSUMER_CHANNEL_SPLICE:
1d4dfdef
DG
1628 /*
1629 * XXX: The lttng-modules splice "actor" does not handle copying
1630 * partial pages hence only using the subbuffer size without the
1631 * padding makes the splice fail.
1632 */
1633 subbuf_size = len;
1634 padding = 0;
1635
1636 /* splice the subbuffer to the tracefile */
91dfef6e 1637 ret = lttng_consumer_on_read_subbuffer_splice(ctx, stream, subbuf_size,
309167d2 1638 padding, &index);
91dfef6e
DG
1639 /*
1640 * XXX: Splice does not support network streaming so the return value
1641 * is simply checked against subbuf_size and not like the mmap() op.
1642 */
1d4dfdef
DG
1643 if (ret != subbuf_size) {
1644 /*
1645 * display the error but continue processing to try
1646 * to release the subbuffer
1647 */
1648 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
1649 ret, subbuf_size);
309167d2 1650 write_index = 0;
1d4dfdef
DG
1651 }
1652 break;
07b86b52 1653 case CONSUMER_CHANNEL_MMAP:
1d4dfdef
DG
1654 /* Get subbuffer size without padding */
1655 err = kernctl_get_subbuf_size(infd, &subbuf_size);
1656 if (err != 0) {
5a510c9f 1657 PERROR("Getting sub-buffer len failed.");
8265f19e
MD
1658 err = kernctl_put_subbuf(infd);
1659 if (err != 0) {
32af2c95 1660 if (err == -EFAULT) {
5a510c9f 1661 PERROR("Error in unreserving sub buffer\n");
32af2c95 1662 } else if (err == -EIO) {
8265f19e 1663 /* Should never happen with newer LTTng versions */
5a510c9f 1664 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
8265f19e 1665 }
32af2c95 1666 ret = err;
02d02e31 1667 goto error;
8265f19e 1668 }
32af2c95 1669 ret = err;
02d02e31 1670 goto error;
1d4dfdef 1671 }
47e81c02 1672
1d4dfdef
DG
1673 /* Make sure the tracer is not gone mad on us! */
1674 assert(len >= subbuf_size);
1675
1676 padding = len - subbuf_size;
1677
1678 /* write the subbuffer to the tracefile */
91dfef6e 1679 ret = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, subbuf_size,
309167d2 1680 padding, &index);
91dfef6e
DG
1681 /*
1682 * The mmap operation should write subbuf_size amount of data when
1683 * network streaming or the full padding (len) size when we are _not_
1684 * streaming.
1685 */
d88aee68
DG
1686 if ((ret != subbuf_size && stream->net_seq_idx != (uint64_t) -1ULL) ||
1687 (ret != len && stream->net_seq_idx == (uint64_t) -1ULL)) {
1d4dfdef 1688 /*
91dfef6e 1689 * Display the error but continue processing to try to release the
2336629e
DG
1690 * subbuffer. This is a DBG statement since this is possible to
1691 * happen without being a critical error.
1d4dfdef 1692 */
2336629e 1693 DBG("Error writing to tracefile "
91dfef6e
DG
1694 "(ret: %zd != len: %lu != subbuf_size: %lu)",
1695 ret, len, subbuf_size);
309167d2 1696 write_index = 0;
1d4dfdef
DG
1697 }
1698 break;
1699 default:
1700 ERR("Unknown output method");
56591bac 1701 ret = -EPERM;
d41f73b7
MD
1702 }
1703
1704 err = kernctl_put_next_subbuf(infd);
1705 if (err != 0) {
32af2c95 1706 if (err == -EFAULT) {
5a510c9f 1707 PERROR("Error in unreserving sub buffer\n");
32af2c95 1708 } else if (err == -EIO) {
d41f73b7 1709 /* Should never happen with newer LTTng versions */
5a510c9f 1710 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
d41f73b7 1711 }
32af2c95 1712 ret = err;
02d02e31 1713 goto error;
d41f73b7
MD
1714 }
1715
309167d2 1716 /* Write index if needed. */
1c20f0e2 1717 if (!write_index) {
02d02e31 1718 goto rotate;
1c20f0e2
JD
1719 }
1720
94d49140
JD
1721 if (stream->chan->live_timer_interval && !stream->metadata_flag) {
1722 /*
1723 * In live, block until all the metadata is sent.
1724 */
c585821b
MD
1725 pthread_mutex_lock(&stream->metadata_timer_lock);
1726 assert(!stream->missed_metadata_flush);
1727 stream->waiting_on_metadata = true;
1728 pthread_mutex_unlock(&stream->metadata_timer_lock);
1729
94d49140 1730 err = consumer_stream_sync_metadata(ctx, stream->session_id);
c585821b
MD
1731
1732 pthread_mutex_lock(&stream->metadata_timer_lock);
1733 stream->waiting_on_metadata = false;
1734 if (stream->missed_metadata_flush) {
1735 stream->missed_metadata_flush = false;
1736 pthread_mutex_unlock(&stream->metadata_timer_lock);
1737 (void) consumer_flush_kernel_index(stream);
1738 } else {
1739 pthread_mutex_unlock(&stream->metadata_timer_lock);
1740 }
94d49140 1741 if (err < 0) {
02d02e31 1742 goto error;
94d49140
JD
1743 }
1744 }
1745
1c20f0e2
JD
1746 err = consumer_stream_write_index(stream, &index);
1747 if (err < 0) {
02d02e31 1748 goto error;
309167d2
JD
1749 }
1750
02d02e31
JD
1751rotate:
1752 /*
1753 * After extracting the packet, we check if the stream is now ready to be
1754 * rotated and perform the action immediately.
1755 */
1756 rotation_ret = lttng_consumer_stream_is_rotate_ready(stream);
1757 if (rotation_ret == 1) {
d2956687 1758 rotation_ret = lttng_consumer_rotate_stream(ctx, stream);
02d02e31
JD
1759 if (rotation_ret < 0) {
1760 ERR("Stream rotation error");
1761 ret = -1;
1762 goto error;
1763 }
1764 } else if (rotation_ret < 0) {
1765 ERR("Checking if stream is ready to rotate");
1766 ret = -1;
1767 goto error;
1768 }
1769
1770error:
d41f73b7
MD
1771 return ret;
1772}
1773
1774int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream *stream)
1775{
1776 int ret;
ffe60014
DG
1777
1778 assert(stream);
1779
2bba9e53 1780 /*
d2956687
JG
1781 * Don't create anything if this is set for streaming or if there is
1782 * no current trace chunk on the parent channel.
2bba9e53 1783 */
d2956687
JG
1784 if (stream->net_seq_idx == (uint64_t) -1ULL && stream->chan->monitor &&
1785 stream->chan->trace_chunk) {
1786 ret = consumer_stream_create_output_files(stream, true);
1787 if (ret) {
fe4477ee
JD
1788 goto error;
1789 }
ffe60014 1790 }
d41f73b7 1791
d41f73b7
MD
1792 if (stream->output == LTTNG_EVENT_MMAP) {
1793 /* get the len of the mmap region */
1794 unsigned long mmap_len;
1795
1796 ret = kernctl_get_mmap_len(stream->wait_fd, &mmap_len);
1797 if (ret != 0) {
ffe60014 1798 PERROR("kernctl_get_mmap_len");
d41f73b7
MD
1799 goto error_close_fd;
1800 }
1801 stream->mmap_len = (size_t) mmap_len;
1802
ffe60014
DG
1803 stream->mmap_base = mmap(NULL, stream->mmap_len, PROT_READ,
1804 MAP_PRIVATE, stream->wait_fd, 0);
d41f73b7 1805 if (stream->mmap_base == MAP_FAILED) {
ffe60014 1806 PERROR("Error mmaping");
d41f73b7
MD
1807 ret = -1;
1808 goto error_close_fd;
1809 }
1810 }
1811
1812 /* we return 0 to let the library handle the FD internally */
1813 return 0;
1814
1815error_close_fd:
2f225ce2 1816 if (stream->out_fd >= 0) {
d41f73b7
MD
1817 int err;
1818
1819 err = close(stream->out_fd);
1820 assert(!err);
2f225ce2 1821 stream->out_fd = -1;
d41f73b7
MD
1822 }
1823error:
1824 return ret;
1825}
1826
ca22feea
DG
1827/*
1828 * Check if data is still being extracted from the buffers for a specific
4e9a4686
DG
1829 * stream. Consumer data lock MUST be acquired before calling this function
1830 * and the stream lock.
ca22feea 1831 *
6d805429 1832 * Return 1 if the traced data are still getting read else 0 meaning that the
ca22feea
DG
1833 * data is available for trace viewer reading.
1834 */
6d805429 1835int lttng_kconsumer_data_pending(struct lttng_consumer_stream *stream)
ca22feea
DG
1836{
1837 int ret;
1838
1839 assert(stream);
1840
873b9e9a
MD
1841 if (stream->endpoint_status != CONSUMER_ENDPOINT_ACTIVE) {
1842 ret = 0;
1843 goto end;
1844 }
1845
ca22feea
DG
1846 ret = kernctl_get_next_subbuf(stream->wait_fd);
1847 if (ret == 0) {
1848 /* There is still data so let's put back this subbuffer. */
1849 ret = kernctl_put_subbuf(stream->wait_fd);
1850 assert(ret == 0);
6d805429 1851 ret = 1; /* Data is pending */
4e9a4686 1852 goto end;
ca22feea
DG
1853 }
1854
6d805429
DG
1855 /* Data is NOT pending and ready to be read. */
1856 ret = 0;
ca22feea 1857
6efae65e
DG
1858end:
1859 return ret;
ca22feea 1860}
This page took 0.172953 seconds and 5 git commands to generate.