Fix: sessiond vs consumerd push/get metadata deadlock
[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>
4 *
d14d33bf
AM
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
3bd1e081
MD
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
d14d33bf
AM
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
3bd1e081
MD
17 */
18
19#define _GNU_SOURCE
20#include <assert.h>
3bd1e081
MD
21#include <poll.h>
22#include <pthread.h>
23#include <stdlib.h>
24#include <string.h>
25#include <sys/mman.h>
26#include <sys/socket.h>
27#include <sys/types.h>
77c7c900 28#include <inttypes.h>
3bd1e081 29#include <unistd.h>
dbb5dfe6 30#include <sys/stat.h>
3bd1e081 31
51a9e1c7 32#include <bin/lttng-consumerd/health-consumerd.h>
990570ed 33#include <common/common.h>
10a8a223 34#include <common/kernel-ctl/kernel-ctl.h>
10a8a223 35#include <common/sessiond-comm/sessiond-comm.h>
00e2e675 36#include <common/sessiond-comm/relayd.h>
dbb5dfe6 37#include <common/compat/fcntl.h>
f263b7fd 38#include <common/compat/endian.h>
acdb9057 39#include <common/pipe.h>
00e2e675 40#include <common/relayd/relayd.h>
fe4477ee 41#include <common/utils.h>
07b86b52 42#include <common/consumer-stream.h>
309167d2 43#include <common/index/index.h>
d3e2ba59 44#include <common/consumer-timer.h>
0857097f 45
10a8a223 46#include "kernel-consumer.h"
3bd1e081
MD
47
48extern struct lttng_consumer_global_data consumer_data;
49extern int consumer_poll_timeout;
50extern volatile int consumer_quit;
51
3bd1e081
MD
52/*
53 * Take a snapshot for a specific fd
54 *
55 * Returns 0 on success, < 0 on error
56 */
ffe60014 57int lttng_kconsumer_take_snapshot(struct lttng_consumer_stream *stream)
3bd1e081
MD
58{
59 int ret = 0;
60 int infd = stream->wait_fd;
61
62 ret = kernctl_snapshot(infd);
63 if (ret != 0) {
5a510c9f 64 PERROR("Getting sub-buffer snapshot.");
56591bac 65 ret = -errno;
3bd1e081
MD
66 }
67
68 return ret;
69}
70
71/*
72 * Get the produced position
73 *
74 * Returns 0 on success, < 0 on error
75 */
ffe60014 76int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream *stream,
3bd1e081
MD
77 unsigned long *pos)
78{
79 int ret;
80 int infd = stream->wait_fd;
81
82 ret = kernctl_snapshot_get_produced(infd, pos);
83 if (ret != 0) {
5a510c9f 84 PERROR("kernctl_snapshot_get_produced");
56591bac 85 ret = -errno;
3bd1e081
MD
86 }
87
88 return ret;
89}
90
07b86b52
JD
91/*
92 * Get the consumerd position
93 *
94 * Returns 0 on success, < 0 on error
95 */
96int lttng_kconsumer_get_consumed_snapshot(struct lttng_consumer_stream *stream,
97 unsigned long *pos)
98{
99 int ret;
100 int infd = stream->wait_fd;
101
102 ret = kernctl_snapshot_get_consumed(infd, pos);
103 if (ret != 0) {
5a510c9f 104 PERROR("kernctl_snapshot_get_consumed");
56591bac 105 ret = -errno;
07b86b52
JD
106 }
107
108 return ret;
109}
110
07b86b52
JD
111/*
112 * Take a snapshot of all the stream of a channel
113 *
114 * Returns 0 on success, < 0 on error
115 */
116int lttng_kconsumer_snapshot_channel(uint64_t key, char *path,
135347bd 117 uint64_t relayd_id, uint64_t nb_packets_per_stream,
5c786ded 118 struct lttng_consumer_local_data *ctx)
07b86b52
JD
119{
120 int ret;
121 unsigned long consumed_pos, produced_pos;
122 struct lttng_consumer_channel *channel;
123 struct lttng_consumer_stream *stream;
124
6a00837f 125 DBG("Kernel consumer snapshot channel %" PRIu64, key);
07b86b52
JD
126
127 rcu_read_lock();
128
129 channel = consumer_find_channel(key);
130 if (!channel) {
6a00837f 131 ERR("No channel found for key %" PRIu64, key);
07b86b52
JD
132 ret = -1;
133 goto end;
134 }
135
136 /* Splice is not supported yet for channel snapshot. */
137 if (channel->output != CONSUMER_CHANNEL_MMAP) {
138 ERR("Unsupported output %d", channel->output);
139 ret = -1;
140 goto end;
141 }
142
10a50311 143 cds_list_for_each_entry(stream, &channel->streams.head, send_node) {
9ce5646a
MD
144
145 health_code_update();
146
07b86b52
JD
147 /*
148 * Lock stream because we are about to change its state.
149 */
150 pthread_mutex_lock(&stream->lock);
151
29decac3
DG
152 /*
153 * Assign the received relayd ID so we can use it for streaming. The streams
154 * are not visible to anyone so this is OK to change it.
155 */
07b86b52
JD
156 stream->net_seq_idx = relayd_id;
157 channel->relayd_id = relayd_id;
158 if (relayd_id != (uint64_t) -1ULL) {
10a50311 159 ret = consumer_send_relayd_stream(stream, path);
07b86b52
JD
160 if (ret < 0) {
161 ERR("sending stream to relayd");
162 goto end_unlock;
163 }
07b86b52
JD
164 } else {
165 ret = utils_create_stream_file(path, stream->name,
10a50311
JD
166 stream->chan->tracefile_size,
167 stream->tracefile_count_current,
309167d2 168 stream->uid, stream->gid, NULL);
07b86b52
JD
169 if (ret < 0) {
170 ERR("utils_create_stream_file");
171 goto end_unlock;
172 }
173
174 stream->out_fd = ret;
175 stream->tracefile_size_current = 0;
176
81ea21bf
MD
177 DBG("Kernel consumer snapshot stream %s/%s (%" PRIu64 ")",
178 path, stream->name, stream->key);
07b86b52 179 }
601262d6
JD
180 if (relayd_id != -1ULL) {
181 ret = consumer_send_relayd_streams_sent(relayd_id);
182 if (ret < 0) {
183 ERR("sending streams sent to relayd");
184 goto end_unlock;
185 }
a4baae1b 186 }
07b86b52
JD
187
188 ret = kernctl_buffer_flush(stream->wait_fd);
189 if (ret < 0) {
10a50311 190 ERR("Failed to flush kernel stream");
56591bac 191 ret = -errno;
07b86b52
JD
192 goto end_unlock;
193 }
194
195 ret = lttng_kconsumer_take_snapshot(stream);
196 if (ret < 0) {
197 ERR("Taking kernel snapshot");
198 goto end_unlock;
199 }
200
201 ret = lttng_kconsumer_get_produced_snapshot(stream, &produced_pos);
202 if (ret < 0) {
203 ERR("Produced kernel snapshot position");
204 goto end_unlock;
205 }
206
207 ret = lttng_kconsumer_get_consumed_snapshot(stream, &consumed_pos);
208 if (ret < 0) {
209 ERR("Consumerd kernel snapshot position");
210 goto end_unlock;
211 }
212
213 if (stream->max_sb_size == 0) {
214 ret = kernctl_get_max_subbuf_size(stream->wait_fd,
215 &stream->max_sb_size);
216 if (ret < 0) {
217 ERR("Getting kernel max_sb_size");
56591bac 218 ret = -errno;
07b86b52
JD
219 goto end_unlock;
220 }
221 }
222
135347bd
MD
223 consumed_pos = consumer_get_consume_start_pos(consumed_pos,
224 produced_pos, nb_packets_per_stream,
225 stream->max_sb_size);
5c786ded 226
07b86b52
JD
227 while (consumed_pos < produced_pos) {
228 ssize_t read_len;
229 unsigned long len, padded_len;
230
9ce5646a
MD
231 health_code_update();
232
07b86b52
JD
233 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos);
234
235 ret = kernctl_get_subbuf(stream->wait_fd, &consumed_pos);
236 if (ret < 0) {
237 if (errno != EAGAIN) {
238 PERROR("kernctl_get_subbuf snapshot");
56591bac 239 ret = -errno;
07b86b52
JD
240 goto end_unlock;
241 }
242 DBG("Kernel consumer get subbuf failed. Skipping it.");
243 consumed_pos += stream->max_sb_size;
244 continue;
245 }
246
247 ret = kernctl_get_subbuf_size(stream->wait_fd, &len);
248 if (ret < 0) {
249 ERR("Snapshot kernctl_get_subbuf_size");
56591bac 250 ret = -errno;
29decac3 251 goto error_put_subbuf;
07b86b52
JD
252 }
253
254 ret = kernctl_get_padded_subbuf_size(stream->wait_fd, &padded_len);
255 if (ret < 0) {
256 ERR("Snapshot kernctl_get_padded_subbuf_size");
56591bac 257 ret = -errno;
29decac3 258 goto error_put_subbuf;
07b86b52
JD
259 }
260
261 read_len = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, len,
309167d2 262 padded_len - len, NULL);
07b86b52 263 /*
29decac3
DG
264 * We write the padded len in local tracefiles but the data len
265 * when using a relay. Display the error but continue processing
266 * to try to release the subbuffer.
07b86b52
JD
267 */
268 if (relayd_id != (uint64_t) -1ULL) {
269 if (read_len != len) {
270 ERR("Error sending to the relay (ret: %zd != len: %lu)",
271 read_len, len);
272 }
273 } else {
274 if (read_len != padded_len) {
275 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
276 read_len, padded_len);
277 }
278 }
279
280 ret = kernctl_put_subbuf(stream->wait_fd);
281 if (ret < 0) {
282 ERR("Snapshot kernctl_put_subbuf");
56591bac 283 ret = -errno;
07b86b52
JD
284 goto end_unlock;
285 }
286 consumed_pos += stream->max_sb_size;
287 }
288
289 if (relayd_id == (uint64_t) -1ULL) {
fdf9986c
MD
290 if (stream->out_fd >= 0) {
291 ret = close(stream->out_fd);
292 if (ret < 0) {
293 PERROR("Kernel consumer snapshot close out_fd");
294 goto end_unlock;
295 }
296 stream->out_fd = -1;
07b86b52 297 }
07b86b52
JD
298 } else {
299 close_relayd_stream(stream);
300 stream->net_seq_idx = (uint64_t) -1ULL;
301 }
302 pthread_mutex_unlock(&stream->lock);
303 }
304
305 /* All good! */
306 ret = 0;
307 goto end;
308
29decac3
DG
309error_put_subbuf:
310 ret = kernctl_put_subbuf(stream->wait_fd);
311 if (ret < 0) {
56591bac 312 ret = -errno;
29decac3
DG
313 ERR("Snapshot kernctl_put_subbuf error path");
314 }
07b86b52
JD
315end_unlock:
316 pthread_mutex_unlock(&stream->lock);
317end:
318 rcu_read_unlock();
319 return ret;
320}
321
322/*
323 * Read the whole metadata available for a snapshot.
324 *
325 * Returns 0 on success, < 0 on error
326 */
327int lttng_kconsumer_snapshot_metadata(uint64_t key, char *path,
e2039c7a 328 uint64_t relayd_id, struct lttng_consumer_local_data *ctx)
07b86b52 329{
d771f832
DG
330 int ret, use_relayd = 0;
331 ssize_t ret_read;
07b86b52
JD
332 struct lttng_consumer_channel *metadata_channel;
333 struct lttng_consumer_stream *metadata_stream;
d771f832
DG
334
335 assert(ctx);
07b86b52
JD
336
337 DBG("Kernel consumer snapshot metadata with key %" PRIu64 " at path %s",
338 key, path);
339
340 rcu_read_lock();
341
342 metadata_channel = consumer_find_channel(key);
343 if (!metadata_channel) {
d771f832 344 ERR("Kernel snapshot metadata not found for key %" PRIu64, key);
07b86b52 345 ret = -1;
d771f832 346 goto error;
07b86b52
JD
347 }
348
349 metadata_stream = metadata_channel->metadata_stream;
350 assert(metadata_stream);
351
d771f832 352 /* Flag once that we have a valid relayd for the stream. */
e2039c7a 353 if (relayd_id != (uint64_t) -1ULL) {
d771f832
DG
354 use_relayd = 1;
355 }
356
357 if (use_relayd) {
10a50311 358 ret = consumer_send_relayd_stream(metadata_stream, path);
e2039c7a 359 if (ret < 0) {
d771f832 360 goto error;
e2039c7a 361 }
e2039c7a
JD
362 } else {
363 ret = utils_create_stream_file(path, metadata_stream->name,
364 metadata_stream->chan->tracefile_size,
365 metadata_stream->tracefile_count_current,
309167d2 366 metadata_stream->uid, metadata_stream->gid, NULL);
e2039c7a 367 if (ret < 0) {
d771f832 368 goto error;
e2039c7a
JD
369 }
370 metadata_stream->out_fd = ret;
07b86b52 371 }
07b86b52 372
d771f832 373 do {
9ce5646a
MD
374 health_code_update();
375
d771f832
DG
376 ret_read = lttng_kconsumer_read_subbuffer(metadata_stream, ctx);
377 if (ret_read < 0) {
56591bac 378 if (ret_read != -EAGAIN) {
6a00837f 379 ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
d771f832
DG
380 ret_read);
381 goto error;
07b86b52 382 }
d771f832 383 /* ret_read is negative at this point so we will exit the loop. */
07b86b52
JD
384 continue;
385 }
d771f832 386 } while (ret_read >= 0);
07b86b52 387
d771f832
DG
388 if (use_relayd) {
389 close_relayd_stream(metadata_stream);
390 metadata_stream->net_seq_idx = (uint64_t) -1ULL;
391 } else {
fdf9986c
MD
392 if (metadata_stream->out_fd >= 0) {
393 ret = close(metadata_stream->out_fd);
394 if (ret < 0) {
395 PERROR("Kernel consumer snapshot metadata close out_fd");
396 /*
397 * Don't go on error here since the snapshot was successful at this
398 * point but somehow the close failed.
399 */
400 }
401 metadata_stream->out_fd = -1;
e2039c7a 402 }
e2039c7a
JD
403 }
404
07b86b52 405 ret = 0;
d771f832 406
cf53a8a6
JD
407 cds_list_del(&metadata_stream->send_node);
408 consumer_stream_destroy(metadata_stream, NULL);
409 metadata_channel->metadata_stream = NULL;
d771f832 410error:
07b86b52
JD
411 rcu_read_unlock();
412 return ret;
413}
414
1803a064
MD
415/*
416 * Receive command from session daemon and process it.
417 *
418 * Return 1 on success else a negative value or 0.
419 */
3bd1e081
MD
420int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
421 int sock, struct pollfd *consumer_sockpoll)
422{
423 ssize_t ret;
0c759fc9 424 enum lttcomm_return_code ret_code = LTTCOMM_CONSUMERD_SUCCESS;
3bd1e081
MD
425 struct lttcomm_consumer_msg msg;
426
9ce5646a
MD
427 health_code_update();
428
3bd1e081
MD
429 ret = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg));
430 if (ret != sizeof(msg)) {
1803a064 431 if (ret > 0) {
c6857fcf 432 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_CMD);
1803a064
MD
433 ret = -1;
434 }
3bd1e081
MD
435 return ret;
436 }
9ce5646a
MD
437
438 health_code_update();
439
84382d49
MD
440 /* Deprecated command */
441 assert(msg.cmd_type != LTTNG_CONSUMER_STOP);
3bd1e081 442
9ce5646a
MD
443 health_code_update();
444
b0b335c8
MD
445 /* relayd needs RCU read-side protection */
446 rcu_read_lock();
447
3bd1e081 448 switch (msg.cmd_type) {
00e2e675
DG
449 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET:
450 {
f50f23d9 451 /* Session daemon status message are handled in the following call. */
7735ef9e
DG
452 ret = consumer_add_relayd_socket(msg.u.relayd_sock.net_index,
453 msg.u.relayd_sock.type, ctx, sock, consumer_sockpoll,
d3e2ba59
JD
454 &msg.u.relayd_sock.sock, msg.u.relayd_sock.session_id,
455 msg.u.relayd_sock.relayd_session_id);
00e2e675
DG
456 goto end_nosignal;
457 }
3bd1e081
MD
458 case LTTNG_CONSUMER_ADD_CHANNEL:
459 {
460 struct lttng_consumer_channel *new_channel;
e43c41c5 461 int ret_recv;
3bd1e081 462
9ce5646a
MD
463 health_code_update();
464
f50f23d9
DG
465 /* First send a status message before receiving the fds. */
466 ret = consumer_send_status_msg(sock, ret_code);
467 if (ret < 0) {
468 /* Somehow, the session daemon is not responding anymore. */
1803a064 469 goto error_fatal;
f50f23d9 470 }
9ce5646a
MD
471
472 health_code_update();
473
d88aee68 474 DBG("consumer_add_channel %" PRIu64, msg.u.channel.channel_key);
3bd1e081 475 new_channel = consumer_allocate_channel(msg.u.channel.channel_key,
ffe60014
DG
476 msg.u.channel.session_id, msg.u.channel.pathname,
477 msg.u.channel.name, msg.u.channel.uid, msg.u.channel.gid,
1624d5b7
JD
478 msg.u.channel.relayd_id, msg.u.channel.output,
479 msg.u.channel.tracefile_size,
1950109e 480 msg.u.channel.tracefile_count, 0,
ecc48a90
JD
481 msg.u.channel.monitor,
482 msg.u.channel.live_timer_interval);
3bd1e081 483 if (new_channel == NULL) {
f73fabfd 484 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
3bd1e081
MD
485 goto end_nosignal;
486 }
ffe60014 487 new_channel->nb_init_stream_left = msg.u.channel.nb_init_streams;
95a1109b
JD
488 switch (msg.u.channel.output) {
489 case LTTNG_EVENT_SPLICE:
490 new_channel->output = CONSUMER_CHANNEL_SPLICE;
491 break;
492 case LTTNG_EVENT_MMAP:
493 new_channel->output = CONSUMER_CHANNEL_MMAP;
494 break;
495 default:
496 ERR("Channel output unknown %d", msg.u.channel.output);
497 goto end_nosignal;
498 }
ffe60014
DG
499
500 /* Translate and save channel type. */
501 switch (msg.u.channel.type) {
502 case CONSUMER_CHANNEL_TYPE_DATA:
503 case CONSUMER_CHANNEL_TYPE_METADATA:
504 new_channel->type = msg.u.channel.type;
505 break;
506 default:
507 assert(0);
508 goto end_nosignal;
509 };
510
9ce5646a
MD
511 health_code_update();
512
3bd1e081 513 if (ctx->on_recv_channel != NULL) {
e43c41c5
JD
514 ret_recv = ctx->on_recv_channel(new_channel);
515 if (ret_recv == 0) {
516 ret = consumer_add_channel(new_channel, ctx);
517 } else if (ret_recv < 0) {
3bd1e081
MD
518 goto end_nosignal;
519 }
520 } else {
e43c41c5 521 ret = consumer_add_channel(new_channel, ctx);
3bd1e081 522 }
94d49140
JD
523 if (CONSUMER_CHANNEL_TYPE_DATA) {
524 consumer_timer_live_start(new_channel,
525 msg.u.channel.live_timer_interval);
526 }
e43c41c5 527
9ce5646a
MD
528 health_code_update();
529
e43c41c5 530 /* If we received an error in add_channel, we need to report it. */
821fffb2 531 if (ret < 0) {
1803a064
MD
532 ret = consumer_send_status_msg(sock, ret);
533 if (ret < 0) {
534 goto error_fatal;
535 }
e43c41c5
JD
536 goto end_nosignal;
537 }
538
3bd1e081
MD
539 goto end_nosignal;
540 }
541 case LTTNG_CONSUMER_ADD_STREAM:
542 {
dae10966
DG
543 int fd;
544 struct lttng_pipe *stream_pipe;
00e2e675 545 struct lttng_consumer_stream *new_stream;
ffe60014 546 struct lttng_consumer_channel *channel;
c80048c6 547 int alloc_ret = 0;
3bd1e081 548
ffe60014
DG
549 /*
550 * Get stream's channel reference. Needed when adding the stream to the
551 * global hash table.
552 */
553 channel = consumer_find_channel(msg.u.stream.channel_key);
554 if (!channel) {
555 /*
556 * We could not find the channel. Can happen if cpu hotplug
557 * happens while tearing down.
558 */
d88aee68 559 ERR("Unable to find channel key %" PRIu64, msg.u.stream.channel_key);
e462382a 560 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
ffe60014
DG
561 }
562
9ce5646a
MD
563 health_code_update();
564
f50f23d9
DG
565 /* First send a status message before receiving the fds. */
566 ret = consumer_send_status_msg(sock, ret_code);
1803a064 567 if (ret < 0) {
d771f832 568 /* Somehow, the session daemon is not responding anymore. */
1803a064
MD
569 goto error_fatal;
570 }
9ce5646a
MD
571
572 health_code_update();
573
0c759fc9 574 if (ret_code != LTTCOMM_CONSUMERD_SUCCESS) {
d771f832 575 /* Channel was not found. */
f50f23d9
DG
576 goto end_nosignal;
577 }
578
d771f832 579 /* Blocking call */
9ce5646a
MD
580 health_poll_entry();
581 ret = lttng_consumer_poll_socket(consumer_sockpoll);
582 health_poll_exit();
84382d49
MD
583 if (ret) {
584 goto error_fatal;
3bd1e081 585 }
00e2e675 586
9ce5646a
MD
587 health_code_update();
588
00e2e675 589 /* Get stream file descriptor from socket */
f2fc6720
MD
590 ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1);
591 if (ret != sizeof(fd)) {
f73fabfd 592 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD);
3f8e211f 593 rcu_read_unlock();
3bd1e081
MD
594 return ret;
595 }
3bd1e081 596
9ce5646a
MD
597 health_code_update();
598
f50f23d9
DG
599 /*
600 * Send status code to session daemon only if the recv works. If the
601 * above recv() failed, the session daemon is notified through the
602 * error socket and the teardown is eventually done.
603 */
604 ret = consumer_send_status_msg(sock, ret_code);
605 if (ret < 0) {
606 /* Somehow, the session daemon is not responding anymore. */
607 goto end_nosignal;
608 }
609
9ce5646a
MD
610 health_code_update();
611
ffe60014
DG
612 new_stream = consumer_allocate_stream(channel->key,
613 fd,
614 LTTNG_CONSUMER_ACTIVE_STREAM,
615 channel->name,
616 channel->uid,
617 channel->gid,
618 channel->relayd_id,
619 channel->session_id,
620 msg.u.stream.cpu,
621 &alloc_ret,
4891ece8
DG
622 channel->type,
623 channel->monitor);
3bd1e081 624 if (new_stream == NULL) {
c80048c6
MD
625 switch (alloc_ret) {
626 case -ENOMEM:
627 case -EINVAL:
628 default:
629 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
630 break;
c80048c6 631 }
3f8e211f 632 goto end_nosignal;
3bd1e081 633 }
d771f832 634
ffe60014
DG
635 new_stream->chan = channel;
636 new_stream->wait_fd = fd;
07b86b52
JD
637 switch (channel->output) {
638 case CONSUMER_CHANNEL_SPLICE:
639 new_stream->output = LTTNG_EVENT_SPLICE;
6f2b2a45
JD
640 ret = utils_create_pipe(new_stream->splice_pipe);
641 if (ret < 0) {
642 goto end_nosignal;
643 }
07b86b52
JD
644 break;
645 case CONSUMER_CHANNEL_MMAP:
646 new_stream->output = LTTNG_EVENT_MMAP;
647 break;
648 default:
649 ERR("Stream output unknown %d", channel->output);
650 goto end_nosignal;
651 }
00e2e675 652
a0c83db9
DG
653 /*
654 * We've just assigned the channel to the stream so increment the
07b86b52
JD
655 * refcount right now. We don't need to increment the refcount for
656 * streams in no monitor because we handle manually the cleanup of
657 * those. It is very important to make sure there is NO prior
658 * consumer_del_stream() calls or else the refcount will be unbalanced.
a0c83db9 659 */
07b86b52
JD
660 if (channel->monitor) {
661 uatomic_inc(&new_stream->chan->refcount);
662 }
9d9353f9 663
fb3a43a9
DG
664 /*
665 * The buffer flush is done on the session daemon side for the kernel
666 * so no need for the stream "hangup_flush_done" variable to be
667 * tracked. This is important for a kernel stream since we don't rely
668 * on the flush state of the stream to read data. It's not the case for
669 * user space tracing.
670 */
671 new_stream->hangup_flush_done = 0;
672
9ce5646a
MD
673 health_code_update();
674
633d0084
DG
675 if (ctx->on_recv_stream) {
676 ret = ctx->on_recv_stream(new_stream);
677 if (ret < 0) {
d771f832 678 consumer_stream_free(new_stream);
633d0084 679 goto end_nosignal;
fb3a43a9 680 }
633d0084 681 }
fb3a43a9 682
9ce5646a
MD
683 health_code_update();
684
07b86b52
JD
685 if (new_stream->metadata_flag) {
686 channel->metadata_stream = new_stream;
687 }
688
2bba9e53
DG
689 /* Do not monitor this stream. */
690 if (!channel->monitor) {
5eecee74 691 DBG("Kernel consumer add stream %s in no monitor mode with "
6dc3064a 692 "relayd id %" PRIu64, new_stream->name,
5eecee74 693 new_stream->net_seq_idx);
10a50311 694 cds_list_add(&new_stream->send_node, &channel->streams.head);
6dc3064a
DG
695 break;
696 }
697
e1b71bdc
DG
698 /* Send stream to relayd if the stream has an ID. */
699 if (new_stream->net_seq_idx != (uint64_t) -1ULL) {
194ee077
DG
700 ret = consumer_send_relayd_stream(new_stream,
701 new_stream->chan->pathname);
e1b71bdc
DG
702 if (ret < 0) {
703 consumer_stream_free(new_stream);
704 goto end_nosignal;
705 }
e2039c7a
JD
706 }
707
50f8ae69 708 /* Get the right pipe where the stream will be sent. */
633d0084 709 if (new_stream->metadata_flag) {
5ab66908
MD
710 ret = consumer_add_metadata_stream(new_stream);
711 if (ret) {
712 ERR("Consumer add metadata stream %" PRIu64 " failed. Continuing",
713 new_stream->key);
714 consumer_stream_free(new_stream);
715 goto end_nosignal;
716 }
dae10966 717 stream_pipe = ctx->consumer_metadata_pipe;
3bd1e081 718 } else {
5ab66908
MD
719 ret = consumer_add_data_stream(new_stream);
720 if (ret) {
721 ERR("Consumer add stream %" PRIu64 " failed. Continuing",
722 new_stream->key);
723 consumer_stream_free(new_stream);
724 goto end_nosignal;
725 }
dae10966 726 stream_pipe = ctx->consumer_data_pipe;
50f8ae69
DG
727 }
728
5ab66908
MD
729 /* Vitible to other threads */
730 new_stream->globally_visible = 1;
731
9ce5646a
MD
732 health_code_update();
733
dae10966 734 ret = lttng_pipe_write(stream_pipe, &new_stream, sizeof(new_stream));
50f8ae69 735 if (ret < 0) {
dae10966 736 ERR("Consumer write %s stream to pipe %d",
50f8ae69 737 new_stream->metadata_flag ? "metadata" : "data",
dae10966 738 lttng_pipe_get_writefd(stream_pipe));
5ab66908
MD
739 if (new_stream->metadata_flag) {
740 consumer_del_stream_for_metadata(new_stream);
741 } else {
742 consumer_del_stream_for_data(new_stream);
743 }
50f8ae69 744 goto end_nosignal;
3bd1e081 745 }
00e2e675 746
50f8ae69 747 DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64,
ffe60014 748 new_stream->name, fd, new_stream->relayd_stream_id);
3bd1e081
MD
749 break;
750 }
a4baae1b
JD
751 case LTTNG_CONSUMER_STREAMS_SENT:
752 {
753 struct lttng_consumer_channel *channel;
754
755 /*
756 * Get stream's channel reference. Needed when adding the stream to the
757 * global hash table.
758 */
759 channel = consumer_find_channel(msg.u.sent_streams.channel_key);
760 if (!channel) {
761 /*
762 * We could not find the channel. Can happen if cpu hotplug
763 * happens while tearing down.
764 */
765 ERR("Unable to find channel key %" PRIu64,
766 msg.u.sent_streams.channel_key);
e462382a 767 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
a4baae1b
JD
768 }
769
770 health_code_update();
771
772 /*
773 * Send status code to session daemon.
774 */
775 ret = consumer_send_status_msg(sock, ret_code);
f261ad0a 776 if (ret < 0 || ret_code != LTTCOMM_CONSUMERD_SUCCESS) {
a4baae1b
JD
777 /* Somehow, the session daemon is not responding anymore. */
778 goto end_nosignal;
779 }
780
781 health_code_update();
782
783 /*
784 * We should not send this message if we don't monitor the
785 * streams in this channel.
786 */
787 if (!channel->monitor) {
788 break;
789 }
790
791 health_code_update();
792 /* Send stream to relayd if the stream has an ID. */
793 if (msg.u.sent_streams.net_seq_idx != (uint64_t) -1ULL) {
794 ret = consumer_send_relayd_streams_sent(
795 msg.u.sent_streams.net_seq_idx);
796 if (ret < 0) {
797 goto end_nosignal;
798 }
799 }
800 break;
801 }
3bd1e081
MD
802 case LTTNG_CONSUMER_UPDATE_STREAM:
803 {
3f8e211f
DG
804 rcu_read_unlock();
805 return -ENOSYS;
806 }
807 case LTTNG_CONSUMER_DESTROY_RELAYD:
808 {
a6ba4fe1 809 uint64_t index = msg.u.destroy_relayd.net_seq_idx;
3f8e211f
DG
810 struct consumer_relayd_sock_pair *relayd;
811
a6ba4fe1 812 DBG("Kernel consumer destroying relayd %" PRIu64, index);
3f8e211f
DG
813
814 /* Get relayd reference if exists. */
a6ba4fe1 815 relayd = consumer_find_relayd(index);
3f8e211f 816 if (relayd == NULL) {
3448e266 817 DBG("Unable to find relayd %" PRIu64, index);
e462382a 818 ret_code = LTTCOMM_CONSUMERD_RELAYD_FAIL;
3bd1e081 819 }
3f8e211f 820
a6ba4fe1
DG
821 /*
822 * Each relayd socket pair has a refcount of stream attached to it
823 * which tells if the relayd is still active or not depending on the
824 * refcount value.
825 *
826 * This will set the destroy flag of the relayd object and destroy it
827 * if the refcount reaches zero when called.
828 *
829 * The destroy can happen either here or when a stream fd hangs up.
830 */
f50f23d9
DG
831 if (relayd) {
832 consumer_flag_relayd_for_destroy(relayd);
833 }
834
9ce5646a
MD
835 health_code_update();
836
f50f23d9
DG
837 ret = consumer_send_status_msg(sock, ret_code);
838 if (ret < 0) {
839 /* Somehow, the session daemon is not responding anymore. */
1803a064 840 goto error_fatal;
f50f23d9 841 }
3f8e211f 842
3f8e211f 843 goto end_nosignal;
3bd1e081 844 }
6d805429 845 case LTTNG_CONSUMER_DATA_PENDING:
53632229 846 {
c8f59ee5 847 int32_t ret;
6d805429 848 uint64_t id = msg.u.data_pending.session_id;
c8f59ee5 849
6d805429 850 DBG("Kernel consumer data pending command for id %" PRIu64, id);
c8f59ee5 851
6d805429 852 ret = consumer_data_pending(id);
c8f59ee5 853
9ce5646a
MD
854 health_code_update();
855
c8f59ee5
DG
856 /* Send back returned value to session daemon */
857 ret = lttcomm_send_unix_sock(sock, &ret, sizeof(ret));
858 if (ret < 0) {
6d805429 859 PERROR("send data pending ret code");
1803a064 860 goto error_fatal;
c8f59ee5 861 }
f50f23d9
DG
862
863 /*
864 * No need to send back a status message since the data pending
865 * returned value is the response.
866 */
c8f59ee5 867 break;
53632229 868 }
6dc3064a
DG
869 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL:
870 {
07b86b52
JD
871 if (msg.u.snapshot_channel.metadata == 1) {
872 ret = lttng_kconsumer_snapshot_metadata(msg.u.snapshot_channel.key,
e2039c7a
JD
873 msg.u.snapshot_channel.pathname,
874 msg.u.snapshot_channel.relayd_id, ctx);
07b86b52
JD
875 if (ret < 0) {
876 ERR("Snapshot metadata failed");
e462382a 877 ret_code = LTTCOMM_CONSUMERD_ERROR_METADATA;
07b86b52
JD
878 }
879 } else {
880 ret = lttng_kconsumer_snapshot_channel(msg.u.snapshot_channel.key,
881 msg.u.snapshot_channel.pathname,
5c786ded 882 msg.u.snapshot_channel.relayd_id,
135347bd 883 msg.u.snapshot_channel.nb_packets_per_stream,
5c786ded 884 ctx);
07b86b52
JD
885 if (ret < 0) {
886 ERR("Snapshot channel failed");
e462382a 887 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
07b86b52
JD
888 }
889 }
890
9ce5646a
MD
891 health_code_update();
892
6dc3064a
DG
893 ret = consumer_send_status_msg(sock, ret_code);
894 if (ret < 0) {
895 /* Somehow, the session daemon is not responding anymore. */
896 goto end_nosignal;
897 }
898 break;
899 }
07b86b52
JD
900 case LTTNG_CONSUMER_DESTROY_CHANNEL:
901 {
902 uint64_t key = msg.u.destroy_channel.key;
903 struct lttng_consumer_channel *channel;
904
905 channel = consumer_find_channel(key);
906 if (!channel) {
907 ERR("Kernel consumer destroy channel %" PRIu64 " not found", key);
e462382a 908 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
07b86b52
JD
909 }
910
9ce5646a
MD
911 health_code_update();
912
07b86b52
JD
913 ret = consumer_send_status_msg(sock, ret_code);
914 if (ret < 0) {
915 /* Somehow, the session daemon is not responding anymore. */
916 goto end_nosignal;
917 }
918
9ce5646a
MD
919 health_code_update();
920
15dc512a
DG
921 /* Stop right now if no channel was found. */
922 if (!channel) {
923 goto end_nosignal;
924 }
925
07b86b52
JD
926 /*
927 * This command should ONLY be issued for channel with streams set in
928 * no monitor mode.
929 */
930 assert(!channel->monitor);
931
932 /*
933 * The refcount should ALWAYS be 0 in the case of a channel in no
934 * monitor mode.
935 */
936 assert(!uatomic_sub_return(&channel->refcount, 1));
937
938 consumer_del_channel(channel);
939
940 goto end_nosignal;
941 }
3bd1e081 942 default:
3f8e211f 943 goto end_nosignal;
3bd1e081 944 }
3f8e211f 945
3bd1e081 946end_nosignal:
b0b335c8 947 rcu_read_unlock();
4cbc1a04
DG
948
949 /*
950 * Return 1 to indicate success since the 0 value can be a socket
951 * shutdown during the recv() or send() call.
952 */
9ce5646a 953 health_code_update();
4cbc1a04 954 return 1;
1803a064
MD
955
956error_fatal:
957 rcu_read_unlock();
958 /* This will issue a consumer stop. */
959 return -1;
3bd1e081 960}
d41f73b7 961
309167d2
JD
962/*
963 * Populate index values of a kernel stream. Values are set in big endian order.
964 *
965 * Return 0 on success or else a negative value.
966 */
50adc264 967static int get_index_values(struct ctf_packet_index *index, int infd)
309167d2
JD
968{
969 int ret;
970
971 ret = kernctl_get_timestamp_begin(infd, &index->timestamp_begin);
972 if (ret < 0) {
973 PERROR("kernctl_get_timestamp_begin");
974 goto error;
975 }
976 index->timestamp_begin = htobe64(index->timestamp_begin);
977
978 ret = kernctl_get_timestamp_end(infd, &index->timestamp_end);
979 if (ret < 0) {
980 PERROR("kernctl_get_timestamp_end");
981 goto error;
982 }
983 index->timestamp_end = htobe64(index->timestamp_end);
984
985 ret = kernctl_get_events_discarded(infd, &index->events_discarded);
986 if (ret < 0) {
987 PERROR("kernctl_get_events_discarded");
988 goto error;
989 }
990 index->events_discarded = htobe64(index->events_discarded);
991
992 ret = kernctl_get_content_size(infd, &index->content_size);
993 if (ret < 0) {
994 PERROR("kernctl_get_content_size");
995 goto error;
996 }
997 index->content_size = htobe64(index->content_size);
998
999 ret = kernctl_get_packet_size(infd, &index->packet_size);
1000 if (ret < 0) {
1001 PERROR("kernctl_get_packet_size");
1002 goto error;
1003 }
1004 index->packet_size = htobe64(index->packet_size);
1005
1006 ret = kernctl_get_stream_id(infd, &index->stream_id);
1007 if (ret < 0) {
1008 PERROR("kernctl_get_stream_id");
1009 goto error;
1010 }
1011 index->stream_id = htobe64(index->stream_id);
1012
1013error:
1014 return ret;
1015}
94d49140
JD
1016/*
1017 * Sync metadata meaning request them to the session daemon and snapshot to the
1018 * metadata thread can consumer them.
1019 *
1020 * Metadata stream lock MUST be acquired.
1021 *
1022 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
1023 * is empty or a negative value on error.
1024 */
1025int lttng_kconsumer_sync_metadata(struct lttng_consumer_stream *metadata)
1026{
1027 int ret;
1028
1029 assert(metadata);
1030
1031 ret = kernctl_buffer_flush(metadata->wait_fd);
1032 if (ret < 0) {
1033 ERR("Failed to flush kernel stream");
1034 goto end;
1035 }
1036
1037 ret = kernctl_snapshot(metadata->wait_fd);
1038 if (ret < 0) {
1039 if (errno != EAGAIN) {
1040 ERR("Sync metadata, taking kernel snapshot failed.");
1041 goto end;
1042 }
1043 DBG("Sync metadata, no new kernel metadata");
1044 /* No new metadata, exit. */
1045 ret = ENODATA;
1046 goto end;
1047 }
1048
1049end:
1050 return ret;
1051}
309167d2 1052
d41f73b7
MD
1053/*
1054 * Consume data on a file descriptor and write it on a trace file.
1055 */
4078b776 1056ssize_t lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream *stream,
d41f73b7
MD
1057 struct lttng_consumer_local_data *ctx)
1058{
1d4dfdef 1059 unsigned long len, subbuf_size, padding;
1c20f0e2 1060 int err, write_index = 1;
4078b776 1061 ssize_t ret = 0;
d41f73b7 1062 int infd = stream->wait_fd;
50adc264 1063 struct ctf_packet_index index;
d41f73b7
MD
1064
1065 DBG("In read_subbuffer (infd : %d)", infd);
309167d2 1066
d41f73b7
MD
1067 /* Get the next subbuffer */
1068 err = kernctl_get_next_subbuf(infd);
1069 if (err != 0) {
d41f73b7
MD
1070 /*
1071 * This is a debug message even for single-threaded consumer,
1072 * because poll() have more relaxed criterions than get subbuf,
1073 * so get_subbuf may fail for short race windows where poll()
1074 * would issue wakeups.
1075 */
1076 DBG("Reserving sub buffer failed (everything is normal, "
1077 "it is due to concurrency)");
56591bac 1078 ret = -errno;
d41f73b7
MD
1079 goto end;
1080 }
1081
1d4dfdef
DG
1082 /* Get the full subbuffer size including padding */
1083 err = kernctl_get_padded_subbuf_size(infd, &len);
1084 if (err != 0) {
5a510c9f 1085 PERROR("Getting sub-buffer len failed.");
8265f19e
MD
1086 err = kernctl_put_subbuf(infd);
1087 if (err != 0) {
1088 if (errno == EFAULT) {
5a510c9f 1089 PERROR("Error in unreserving sub buffer\n");
8265f19e
MD
1090 } else if (errno == EIO) {
1091 /* Should never happen with newer LTTng versions */
5a510c9f 1092 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
8265f19e
MD
1093 }
1094 ret = -errno;
1095 goto end;
1096 }
56591bac 1097 ret = -errno;
1d4dfdef
DG
1098 goto end;
1099 }
1100
1c20f0e2 1101 if (!stream->metadata_flag) {
309167d2
JD
1102 ret = get_index_values(&index, infd);
1103 if (ret < 0) {
8265f19e
MD
1104 err = kernctl_put_subbuf(infd);
1105 if (err != 0) {
1106 if (errno == EFAULT) {
5a510c9f 1107 PERROR("Error in unreserving sub buffer\n");
8265f19e
MD
1108 } else if (errno == EIO) {
1109 /* Should never happen with newer LTTng versions */
5a510c9f 1110 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
8265f19e
MD
1111 }
1112 ret = -errno;
1113 goto end;
1114 }
309167d2
JD
1115 goto end;
1116 }
1c20f0e2
JD
1117 } else {
1118 write_index = 0;
309167d2
JD
1119 }
1120
ffe60014 1121 switch (stream->chan->output) {
07b86b52 1122 case CONSUMER_CHANNEL_SPLICE:
1d4dfdef
DG
1123 /*
1124 * XXX: The lttng-modules splice "actor" does not handle copying
1125 * partial pages hence only using the subbuffer size without the
1126 * padding makes the splice fail.
1127 */
1128 subbuf_size = len;
1129 padding = 0;
1130
1131 /* splice the subbuffer to the tracefile */
91dfef6e 1132 ret = lttng_consumer_on_read_subbuffer_splice(ctx, stream, subbuf_size,
309167d2 1133 padding, &index);
91dfef6e
DG
1134 /*
1135 * XXX: Splice does not support network streaming so the return value
1136 * is simply checked against subbuf_size and not like the mmap() op.
1137 */
1d4dfdef
DG
1138 if (ret != subbuf_size) {
1139 /*
1140 * display the error but continue processing to try
1141 * to release the subbuffer
1142 */
1143 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
1144 ret, subbuf_size);
309167d2 1145 write_index = 0;
1d4dfdef
DG
1146 }
1147 break;
07b86b52 1148 case CONSUMER_CHANNEL_MMAP:
1d4dfdef
DG
1149 /* Get subbuffer size without padding */
1150 err = kernctl_get_subbuf_size(infd, &subbuf_size);
1151 if (err != 0) {
5a510c9f 1152 PERROR("Getting sub-buffer len failed.");
8265f19e
MD
1153 err = kernctl_put_subbuf(infd);
1154 if (err != 0) {
1155 if (errno == EFAULT) {
5a510c9f 1156 PERROR("Error in unreserving sub buffer\n");
8265f19e
MD
1157 } else if (errno == EIO) {
1158 /* Should never happen with newer LTTng versions */
5a510c9f 1159 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
8265f19e
MD
1160 }
1161 ret = -errno;
1162 goto end;
1163 }
56591bac 1164 ret = -errno;
1d4dfdef
DG
1165 goto end;
1166 }
47e81c02 1167
1d4dfdef
DG
1168 /* Make sure the tracer is not gone mad on us! */
1169 assert(len >= subbuf_size);
1170
1171 padding = len - subbuf_size;
1172
1173 /* write the subbuffer to the tracefile */
91dfef6e 1174 ret = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, subbuf_size,
309167d2 1175 padding, &index);
91dfef6e
DG
1176 /*
1177 * The mmap operation should write subbuf_size amount of data when
1178 * network streaming or the full padding (len) size when we are _not_
1179 * streaming.
1180 */
d88aee68
DG
1181 if ((ret != subbuf_size && stream->net_seq_idx != (uint64_t) -1ULL) ||
1182 (ret != len && stream->net_seq_idx == (uint64_t) -1ULL)) {
1d4dfdef 1183 /*
91dfef6e 1184 * Display the error but continue processing to try to release the
2336629e
DG
1185 * subbuffer. This is a DBG statement since this is possible to
1186 * happen without being a critical error.
1d4dfdef 1187 */
2336629e 1188 DBG("Error writing to tracefile "
91dfef6e
DG
1189 "(ret: %zd != len: %lu != subbuf_size: %lu)",
1190 ret, len, subbuf_size);
309167d2 1191 write_index = 0;
1d4dfdef
DG
1192 }
1193 break;
1194 default:
1195 ERR("Unknown output method");
56591bac 1196 ret = -EPERM;
d41f73b7
MD
1197 }
1198
1199 err = kernctl_put_next_subbuf(infd);
1200 if (err != 0) {
d41f73b7 1201 if (errno == EFAULT) {
5a510c9f 1202 PERROR("Error in unreserving sub buffer\n");
d41f73b7
MD
1203 } else if (errno == EIO) {
1204 /* Should never happen with newer LTTng versions */
5a510c9f 1205 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
d41f73b7 1206 }
56591bac 1207 ret = -errno;
d41f73b7
MD
1208 goto end;
1209 }
1210
309167d2 1211 /* Write index if needed. */
1c20f0e2
JD
1212 if (!write_index) {
1213 goto end;
1214 }
1215
94d49140
JD
1216 if (stream->chan->live_timer_interval && !stream->metadata_flag) {
1217 /*
1218 * In live, block until all the metadata is sent.
1219 */
5cc6b3e4
MD
1220 pthread_mutex_lock(&stream->metadata_timer_lock);
1221 assert(!stream->missed_metadata_flush);
1222 stream->waiting_on_metadata = true;
1223 pthread_mutex_unlock(&stream->metadata_timer_lock);
1224
94d49140 1225 err = consumer_stream_sync_metadata(ctx, stream->session_id);
5cc6b3e4
MD
1226
1227 pthread_mutex_lock(&stream->metadata_timer_lock);
1228 stream->waiting_on_metadata = false;
1229 if (stream->missed_metadata_flush) {
1230 stream->missed_metadata_flush = false;
1231 pthread_mutex_unlock(&stream->metadata_timer_lock);
1232 (void) consumer_flush_kernel_index(stream);
1233 } else {
1234 pthread_mutex_unlock(&stream->metadata_timer_lock);
1235 }
94d49140
JD
1236 if (err < 0) {
1237 goto end;
1238 }
1239 }
1240
1c20f0e2
JD
1241 err = consumer_stream_write_index(stream, &index);
1242 if (err < 0) {
1243 goto end;
309167d2
JD
1244 }
1245
d41f73b7
MD
1246end:
1247 return ret;
1248}
1249
1250int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream *stream)
1251{
1252 int ret;
ffe60014
DG
1253
1254 assert(stream);
1255
2bba9e53
DG
1256 /*
1257 * Don't create anything if this is set for streaming or should not be
1258 * monitored.
1259 */
1260 if (stream->net_seq_idx == (uint64_t) -1ULL && stream->chan->monitor) {
fe4477ee
JD
1261 ret = utils_create_stream_file(stream->chan->pathname, stream->name,
1262 stream->chan->tracefile_size, stream->tracefile_count_current,
309167d2 1263 stream->uid, stream->gid, NULL);
fe4477ee
JD
1264 if (ret < 0) {
1265 goto error;
1266 }
1267 stream->out_fd = ret;
1268 stream->tracefile_size_current = 0;
309167d2
JD
1269
1270 if (!stream->metadata_flag) {
1271 ret = index_create_file(stream->chan->pathname,
1272 stream->name, stream->uid, stream->gid,
1273 stream->chan->tracefile_size,
1274 stream->tracefile_count_current);
1275 if (ret < 0) {
1276 goto error;
1277 }
1278 stream->index_fd = ret;
1279 }
ffe60014 1280 }
d41f73b7 1281
d41f73b7
MD
1282 if (stream->output == LTTNG_EVENT_MMAP) {
1283 /* get the len of the mmap region */
1284 unsigned long mmap_len;
1285
1286 ret = kernctl_get_mmap_len(stream->wait_fd, &mmap_len);
1287 if (ret != 0) {
ffe60014 1288 PERROR("kernctl_get_mmap_len");
56591bac 1289 ret = -errno;
d41f73b7
MD
1290 goto error_close_fd;
1291 }
1292 stream->mmap_len = (size_t) mmap_len;
1293
ffe60014
DG
1294 stream->mmap_base = mmap(NULL, stream->mmap_len, PROT_READ,
1295 MAP_PRIVATE, stream->wait_fd, 0);
d41f73b7 1296 if (stream->mmap_base == MAP_FAILED) {
ffe60014 1297 PERROR("Error mmaping");
d41f73b7
MD
1298 ret = -1;
1299 goto error_close_fd;
1300 }
1301 }
1302
1303 /* we return 0 to let the library handle the FD internally */
1304 return 0;
1305
1306error_close_fd:
2f225ce2 1307 if (stream->out_fd >= 0) {
d41f73b7
MD
1308 int err;
1309
1310 err = close(stream->out_fd);
1311 assert(!err);
2f225ce2 1312 stream->out_fd = -1;
d41f73b7
MD
1313 }
1314error:
1315 return ret;
1316}
1317
ca22feea
DG
1318/*
1319 * Check if data is still being extracted from the buffers for a specific
4e9a4686
DG
1320 * stream. Consumer data lock MUST be acquired before calling this function
1321 * and the stream lock.
ca22feea 1322 *
6d805429 1323 * Return 1 if the traced data are still getting read else 0 meaning that the
ca22feea
DG
1324 * data is available for trace viewer reading.
1325 */
6d805429 1326int lttng_kconsumer_data_pending(struct lttng_consumer_stream *stream)
ca22feea
DG
1327{
1328 int ret;
1329
1330 assert(stream);
1331
873b9e9a
MD
1332 if (stream->endpoint_status != CONSUMER_ENDPOINT_ACTIVE) {
1333 ret = 0;
1334 goto end;
1335 }
1336
ca22feea
DG
1337 ret = kernctl_get_next_subbuf(stream->wait_fd);
1338 if (ret == 0) {
1339 /* There is still data so let's put back this subbuffer. */
1340 ret = kernctl_put_subbuf(stream->wait_fd);
1341 assert(ret == 0);
6d805429 1342 ret = 1; /* Data is pending */
4e9a4686 1343 goto end;
ca22feea
DG
1344 }
1345
6d805429
DG
1346 /* Data is NOT pending and ready to be read. */
1347 ret = 0;
ca22feea 1348
6efae65e
DG
1349end:
1350 return ret;
ca22feea 1351}
This page took 0.117682 seconds and 4 git commands to generate.