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