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