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