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