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