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