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