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