Fix: session ID signess to uin64_t in sessiond
[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
990570ed 32#include <common/common.h>
10a8a223 33#include <common/kernel-ctl/kernel-ctl.h>
10a8a223 34#include <common/sessiond-comm/sessiond-comm.h>
00e2e675 35#include <common/sessiond-comm/relayd.h>
dbb5dfe6 36#include <common/compat/fcntl.h>
acdb9057 37#include <common/pipe.h>
00e2e675 38#include <common/relayd/relayd.h>
fe4477ee 39#include <common/utils.h>
07b86b52 40#include <common/consumer-stream.h>
0857097f 41
10a8a223 42#include "kernel-consumer.h"
3bd1e081
MD
43
44extern struct lttng_consumer_global_data consumer_data;
45extern int consumer_poll_timeout;
46extern volatile int consumer_quit;
47
3bd1e081
MD
48/*
49 * Take a snapshot for a specific fd
50 *
51 * Returns 0 on success, < 0 on error
52 */
ffe60014 53int lttng_kconsumer_take_snapshot(struct lttng_consumer_stream *stream)
3bd1e081
MD
54{
55 int ret = 0;
56 int infd = stream->wait_fd;
57
58 ret = kernctl_snapshot(infd);
59 if (ret != 0) {
87dc6a9c 60 errno = -ret;
3bd1e081
MD
61 perror("Getting sub-buffer snapshot.");
62 }
63
64 return ret;
65}
66
67/*
68 * Get the produced position
69 *
70 * Returns 0 on success, < 0 on error
71 */
ffe60014 72int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream *stream,
3bd1e081
MD
73 unsigned long *pos)
74{
75 int ret;
76 int infd = stream->wait_fd;
77
78 ret = kernctl_snapshot_get_produced(infd, pos);
79 if (ret != 0) {
87dc6a9c 80 errno = -ret;
3bd1e081
MD
81 perror("kernctl_snapshot_get_produced");
82 }
83
84 return ret;
85}
86
07b86b52
JD
87/*
88 * Get the consumerd position
89 *
90 * Returns 0 on success, < 0 on error
91 */
92int lttng_kconsumer_get_consumed_snapshot(struct lttng_consumer_stream *stream,
93 unsigned long *pos)
94{
95 int ret;
96 int infd = stream->wait_fd;
97
98 ret = kernctl_snapshot_get_consumed(infd, pos);
99 if (ret != 0) {
100 errno = -ret;
101 perror("kernctl_snapshot_get_consumed");
102 }
103
104 return ret;
105}
106
07b86b52
JD
107/*
108 * Take a snapshot of all the stream of a channel
109 *
110 * Returns 0 on success, < 0 on error
111 */
112int lttng_kconsumer_snapshot_channel(uint64_t key, char *path,
113 uint64_t relayd_id, struct lttng_consumer_local_data *ctx)
114{
115 int ret;
116 unsigned long consumed_pos, produced_pos;
117 struct lttng_consumer_channel *channel;
118 struct lttng_consumer_stream *stream;
119
120 DBG("Kernel consumer snapshot channel %lu", key);
121
122 rcu_read_lock();
123
124 channel = consumer_find_channel(key);
125 if (!channel) {
126 ERR("No channel found for key %lu", key);
127 ret = -1;
128 goto end;
129 }
130
131 /* Splice is not supported yet for channel snapshot. */
132 if (channel->output != CONSUMER_CHANNEL_MMAP) {
133 ERR("Unsupported output %d", channel->output);
134 ret = -1;
135 goto end;
136 }
137
10a50311 138 cds_list_for_each_entry(stream, &channel->streams.head, send_node) {
07b86b52
JD
139 /*
140 * Lock stream because we are about to change its state.
141 */
142 pthread_mutex_lock(&stream->lock);
143
29decac3
DG
144 /*
145 * Assign the received relayd ID so we can use it for streaming. The streams
146 * are not visible to anyone so this is OK to change it.
147 */
07b86b52
JD
148 stream->net_seq_idx = relayd_id;
149 channel->relayd_id = relayd_id;
150 if (relayd_id != (uint64_t) -1ULL) {
10a50311 151 ret = consumer_send_relayd_stream(stream, path);
07b86b52
JD
152 if (ret < 0) {
153 ERR("sending stream to relayd");
154 goto end_unlock;
155 }
07b86b52
JD
156 } else {
157 ret = utils_create_stream_file(path, stream->name,
10a50311
JD
158 stream->chan->tracefile_size,
159 stream->tracefile_count_current,
07b86b52
JD
160 stream->uid, stream->gid);
161 if (ret < 0) {
162 ERR("utils_create_stream_file");
163 goto end_unlock;
164 }
165
166 stream->out_fd = ret;
167 stream->tracefile_size_current = 0;
168
169 DBG("Kernel consumer snapshot stream %s/%s (%lu)", path,
170 stream->name, stream->key);
171 }
172
173 ret = kernctl_buffer_flush(stream->wait_fd);
174 if (ret < 0) {
10a50311 175 ERR("Failed to flush kernel stream");
07b86b52
JD
176 goto end_unlock;
177 }
178
179 ret = lttng_kconsumer_take_snapshot(stream);
180 if (ret < 0) {
181 ERR("Taking kernel snapshot");
182 goto end_unlock;
183 }
184
185 ret = lttng_kconsumer_get_produced_snapshot(stream, &produced_pos);
186 if (ret < 0) {
187 ERR("Produced kernel snapshot position");
188 goto end_unlock;
189 }
190
191 ret = lttng_kconsumer_get_consumed_snapshot(stream, &consumed_pos);
192 if (ret < 0) {
193 ERR("Consumerd kernel snapshot position");
194 goto end_unlock;
195 }
196
197 if (stream->max_sb_size == 0) {
198 ret = kernctl_get_max_subbuf_size(stream->wait_fd,
199 &stream->max_sb_size);
200 if (ret < 0) {
201 ERR("Getting kernel max_sb_size");
202 goto end_unlock;
203 }
204 }
205
206 while (consumed_pos < produced_pos) {
207 ssize_t read_len;
208 unsigned long len, padded_len;
209
210 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos);
211
212 ret = kernctl_get_subbuf(stream->wait_fd, &consumed_pos);
213 if (ret < 0) {
214 if (errno != EAGAIN) {
215 PERROR("kernctl_get_subbuf snapshot");
216 goto end_unlock;
217 }
218 DBG("Kernel consumer get subbuf failed. Skipping it.");
219 consumed_pos += stream->max_sb_size;
220 continue;
221 }
222
223 ret = kernctl_get_subbuf_size(stream->wait_fd, &len);
224 if (ret < 0) {
225 ERR("Snapshot kernctl_get_subbuf_size");
29decac3 226 goto error_put_subbuf;
07b86b52
JD
227 }
228
229 ret = kernctl_get_padded_subbuf_size(stream->wait_fd, &padded_len);
230 if (ret < 0) {
231 ERR("Snapshot kernctl_get_padded_subbuf_size");
29decac3 232 goto error_put_subbuf;
07b86b52
JD
233 }
234
235 read_len = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, len,
236 padded_len - len);
237 /*
29decac3
DG
238 * We write the padded len in local tracefiles but the data len
239 * when using a relay. Display the error but continue processing
240 * to try to release the subbuffer.
07b86b52
JD
241 */
242 if (relayd_id != (uint64_t) -1ULL) {
243 if (read_len != len) {
244 ERR("Error sending to the relay (ret: %zd != len: %lu)",
245 read_len, len);
246 }
247 } else {
248 if (read_len != padded_len) {
249 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
250 read_len, padded_len);
251 }
252 }
253
254 ret = kernctl_put_subbuf(stream->wait_fd);
255 if (ret < 0) {
256 ERR("Snapshot kernctl_put_subbuf");
257 goto end_unlock;
258 }
259 consumed_pos += stream->max_sb_size;
260 }
261
262 if (relayd_id == (uint64_t) -1ULL) {
263 ret = close(stream->out_fd);
264 if (ret < 0) {
265 PERROR("Kernel consumer snapshot close out_fd");
266 goto end_unlock;
267 }
268 stream->out_fd = -1;
269 } else {
270 close_relayd_stream(stream);
271 stream->net_seq_idx = (uint64_t) -1ULL;
272 }
273 pthread_mutex_unlock(&stream->lock);
274 }
275
276 /* All good! */
277 ret = 0;
278 goto end;
279
29decac3
DG
280error_put_subbuf:
281 ret = kernctl_put_subbuf(stream->wait_fd);
282 if (ret < 0) {
283 ERR("Snapshot kernctl_put_subbuf error path");
284 }
07b86b52
JD
285end_unlock:
286 pthread_mutex_unlock(&stream->lock);
287end:
288 rcu_read_unlock();
289 return ret;
290}
291
292/*
293 * Read the whole metadata available for a snapshot.
294 *
295 * Returns 0 on success, < 0 on error
296 */
297int lttng_kconsumer_snapshot_metadata(uint64_t key, char *path,
e2039c7a 298 uint64_t relayd_id, struct lttng_consumer_local_data *ctx)
07b86b52 299{
d771f832
DG
300 int ret, use_relayd = 0;
301 ssize_t ret_read;
07b86b52
JD
302 struct lttng_consumer_channel *metadata_channel;
303 struct lttng_consumer_stream *metadata_stream;
d771f832
DG
304
305 assert(ctx);
07b86b52
JD
306
307 DBG("Kernel consumer snapshot metadata with key %" PRIu64 " at path %s",
308 key, path);
309
310 rcu_read_lock();
311
312 metadata_channel = consumer_find_channel(key);
313 if (!metadata_channel) {
d771f832 314 ERR("Kernel snapshot metadata not found for key %" PRIu64, key);
07b86b52 315 ret = -1;
d771f832 316 goto error;
07b86b52
JD
317 }
318
319 metadata_stream = metadata_channel->metadata_stream;
320 assert(metadata_stream);
321
d771f832 322 /* Flag once that we have a valid relayd for the stream. */
e2039c7a 323 if (relayd_id != (uint64_t) -1ULL) {
d771f832
DG
324 use_relayd = 1;
325 }
326
327 if (use_relayd) {
10a50311 328 ret = consumer_send_relayd_stream(metadata_stream, path);
e2039c7a 329 if (ret < 0) {
d771f832 330 goto error;
e2039c7a 331 }
e2039c7a
JD
332 } else {
333 ret = utils_create_stream_file(path, metadata_stream->name,
334 metadata_stream->chan->tracefile_size,
335 metadata_stream->tracefile_count_current,
336 metadata_stream->uid, metadata_stream->gid);
337 if (ret < 0) {
d771f832 338 goto error;
e2039c7a
JD
339 }
340 metadata_stream->out_fd = ret;
07b86b52 341 }
07b86b52 342
d771f832
DG
343 do {
344 ret_read = lttng_kconsumer_read_subbuffer(metadata_stream, ctx);
345 if (ret_read < 0) {
346 if (ret_read != -EPERM) {
347 ERR("Kernel snapshot reading metadata subbuffer (ret: %ld)",
348 ret_read);
349 goto error;
07b86b52 350 }
d771f832 351 /* ret_read is negative at this point so we will exit the loop. */
07b86b52
JD
352 continue;
353 }
d771f832 354 } while (ret_read >= 0);
07b86b52 355
d771f832
DG
356 if (use_relayd) {
357 close_relayd_stream(metadata_stream);
358 metadata_stream->net_seq_idx = (uint64_t) -1ULL;
359 } else {
e2039c7a
JD
360 ret = close(metadata_stream->out_fd);
361 if (ret < 0) {
d771f832
DG
362 PERROR("Kernel consumer snapshot metadata close out_fd");
363 /*
364 * Don't go on error here since the snapshot was successful at this
365 * point but somehow the close failed.
366 */
e2039c7a
JD
367 }
368 metadata_stream->out_fd = -1;
e2039c7a
JD
369 }
370
07b86b52 371 ret = 0;
d771f832
DG
372
373error:
07b86b52
JD
374 rcu_read_unlock();
375 return ret;
376}
377
1803a064
MD
378/*
379 * Receive command from session daemon and process it.
380 *
381 * Return 1 on success else a negative value or 0.
382 */
3bd1e081
MD
383int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
384 int sock, struct pollfd *consumer_sockpoll)
385{
386 ssize_t ret;
f50f23d9 387 enum lttng_error_code ret_code = LTTNG_OK;
3bd1e081
MD
388 struct lttcomm_consumer_msg msg;
389
390 ret = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg));
391 if (ret != sizeof(msg)) {
f73fabfd 392 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_CMD);
1803a064
MD
393 if (ret > 0) {
394 ret = -1;
395 }
3bd1e081
MD
396 return ret;
397 }
398 if (msg.cmd_type == LTTNG_CONSUMER_STOP) {
f50f23d9
DG
399 /*
400 * Notify the session daemon that the command is completed.
401 *
402 * On transport layer error, the function call will print an error
403 * message so handling the returned code is a bit useless since we
404 * return an error code anyway.
405 */
406 (void) consumer_send_status_msg(sock, ret_code);
3bd1e081
MD
407 return -ENOENT;
408 }
409
b0b335c8
MD
410 /* relayd needs RCU read-side protection */
411 rcu_read_lock();
412
3bd1e081 413 switch (msg.cmd_type) {
00e2e675
DG
414 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET:
415 {
f50f23d9 416 /* Session daemon status message are handled in the following call. */
7735ef9e
DG
417 ret = consumer_add_relayd_socket(msg.u.relayd_sock.net_index,
418 msg.u.relayd_sock.type, ctx, sock, consumer_sockpoll,
46e6455f 419 &msg.u.relayd_sock.sock, msg.u.relayd_sock.session_id);
00e2e675
DG
420 goto end_nosignal;
421 }
3bd1e081
MD
422 case LTTNG_CONSUMER_ADD_CHANNEL:
423 {
424 struct lttng_consumer_channel *new_channel;
e43c41c5 425 int ret_recv;
3bd1e081 426
f50f23d9
DG
427 /* First send a status message before receiving the fds. */
428 ret = consumer_send_status_msg(sock, ret_code);
429 if (ret < 0) {
430 /* Somehow, the session daemon is not responding anymore. */
1803a064 431 goto error_fatal;
f50f23d9 432 }
d88aee68 433 DBG("consumer_add_channel %" PRIu64, msg.u.channel.channel_key);
3bd1e081 434 new_channel = consumer_allocate_channel(msg.u.channel.channel_key,
ffe60014
DG
435 msg.u.channel.session_id, msg.u.channel.pathname,
436 msg.u.channel.name, msg.u.channel.uid, msg.u.channel.gid,
1624d5b7
JD
437 msg.u.channel.relayd_id, msg.u.channel.output,
438 msg.u.channel.tracefile_size,
1950109e 439 msg.u.channel.tracefile_count, 0,
2bba9e53 440 msg.u.channel.monitor);
3bd1e081 441 if (new_channel == NULL) {
f73fabfd 442 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
3bd1e081
MD
443 goto end_nosignal;
444 }
ffe60014 445 new_channel->nb_init_stream_left = msg.u.channel.nb_init_streams;
95a1109b
JD
446 switch (msg.u.channel.output) {
447 case LTTNG_EVENT_SPLICE:
448 new_channel->output = CONSUMER_CHANNEL_SPLICE;
449 break;
450 case LTTNG_EVENT_MMAP:
451 new_channel->output = CONSUMER_CHANNEL_MMAP;
452 break;
453 default:
454 ERR("Channel output unknown %d", msg.u.channel.output);
455 goto end_nosignal;
456 }
ffe60014
DG
457
458 /* Translate and save channel type. */
459 switch (msg.u.channel.type) {
460 case CONSUMER_CHANNEL_TYPE_DATA:
461 case CONSUMER_CHANNEL_TYPE_METADATA:
462 new_channel->type = msg.u.channel.type;
463 break;
464 default:
465 assert(0);
466 goto end_nosignal;
467 };
468
3bd1e081 469 if (ctx->on_recv_channel != NULL) {
e43c41c5
JD
470 ret_recv = ctx->on_recv_channel(new_channel);
471 if (ret_recv == 0) {
472 ret = consumer_add_channel(new_channel, ctx);
473 } else if (ret_recv < 0) {
3bd1e081
MD
474 goto end_nosignal;
475 }
476 } else {
e43c41c5 477 ret = consumer_add_channel(new_channel, ctx);
3bd1e081 478 }
e43c41c5
JD
479
480 /* If we received an error in add_channel, we need to report it. */
821fffb2 481 if (ret < 0) {
1803a064
MD
482 ret = consumer_send_status_msg(sock, ret);
483 if (ret < 0) {
484 goto error_fatal;
485 }
e43c41c5
JD
486 goto end_nosignal;
487 }
488
3bd1e081
MD
489 goto end_nosignal;
490 }
491 case LTTNG_CONSUMER_ADD_STREAM:
492 {
dae10966
DG
493 int fd;
494 struct lttng_pipe *stream_pipe;
00e2e675 495 struct lttng_consumer_stream *new_stream;
ffe60014 496 struct lttng_consumer_channel *channel;
c80048c6 497 int alloc_ret = 0;
3bd1e081 498
ffe60014
DG
499 /*
500 * Get stream's channel reference. Needed when adding the stream to the
501 * global hash table.
502 */
503 channel = consumer_find_channel(msg.u.stream.channel_key);
504 if (!channel) {
505 /*
506 * We could not find the channel. Can happen if cpu hotplug
507 * happens while tearing down.
508 */
d88aee68 509 ERR("Unable to find channel key %" PRIu64, msg.u.stream.channel_key);
ffe60014
DG
510 ret_code = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
511 }
512
f50f23d9
DG
513 /* First send a status message before receiving the fds. */
514 ret = consumer_send_status_msg(sock, ret_code);
1803a064 515 if (ret < 0) {
d771f832 516 /* Somehow, the session daemon is not responding anymore. */
1803a064
MD
517 goto error_fatal;
518 }
519 if (ret_code != LTTNG_OK) {
d771f832 520 /* Channel was not found. */
f50f23d9
DG
521 goto end_nosignal;
522 }
523
d771f832 524 /* Blocking call */
3bd1e081 525 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
3f8e211f 526 rcu_read_unlock();
3bd1e081
MD
527 return -EINTR;
528 }
00e2e675
DG
529
530 /* Get stream file descriptor from socket */
f2fc6720
MD
531 ret = lttcomm_recv_fds_unix_sock(sock, &fd, 1);
532 if (ret != sizeof(fd)) {
f73fabfd 533 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_FD);
3f8e211f 534 rcu_read_unlock();
3bd1e081
MD
535 return ret;
536 }
3bd1e081 537
f50f23d9
DG
538 /*
539 * Send status code to session daemon only if the recv works. If the
540 * above recv() failed, the session daemon is notified through the
541 * error socket and the teardown is eventually done.
542 */
543 ret = consumer_send_status_msg(sock, ret_code);
544 if (ret < 0) {
545 /* Somehow, the session daemon is not responding anymore. */
546 goto end_nosignal;
547 }
548
ffe60014
DG
549 new_stream = consumer_allocate_stream(channel->key,
550 fd,
551 LTTNG_CONSUMER_ACTIVE_STREAM,
552 channel->name,
553 channel->uid,
554 channel->gid,
555 channel->relayd_id,
556 channel->session_id,
557 msg.u.stream.cpu,
558 &alloc_ret,
559 channel->type);
3bd1e081 560 if (new_stream == NULL) {
c80048c6
MD
561 switch (alloc_ret) {
562 case -ENOMEM:
563 case -EINVAL:
564 default:
565 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
566 break;
c80048c6 567 }
3f8e211f 568 goto end_nosignal;
3bd1e081 569 }
d771f832 570
ffe60014
DG
571 new_stream->chan = channel;
572 new_stream->wait_fd = fd;
07b86b52
JD
573 switch (channel->output) {
574 case CONSUMER_CHANNEL_SPLICE:
575 new_stream->output = LTTNG_EVENT_SPLICE;
576 break;
577 case CONSUMER_CHANNEL_MMAP:
578 new_stream->output = LTTNG_EVENT_MMAP;
579 break;
580 default:
581 ERR("Stream output unknown %d", channel->output);
582 goto end_nosignal;
583 }
00e2e675 584
a0c83db9
DG
585 /*
586 * We've just assigned the channel to the stream so increment the
07b86b52
JD
587 * refcount right now. We don't need to increment the refcount for
588 * streams in no monitor because we handle manually the cleanup of
589 * those. It is very important to make sure there is NO prior
590 * consumer_del_stream() calls or else the refcount will be unbalanced.
a0c83db9 591 */
07b86b52
JD
592 if (channel->monitor) {
593 uatomic_inc(&new_stream->chan->refcount);
594 }
9d9353f9 595
fb3a43a9
DG
596 /*
597 * The buffer flush is done on the session daemon side for the kernel
598 * so no need for the stream "hangup_flush_done" variable to be
599 * tracked. This is important for a kernel stream since we don't rely
600 * on the flush state of the stream to read data. It's not the case for
601 * user space tracing.
602 */
603 new_stream->hangup_flush_done = 0;
604
633d0084
DG
605 if (ctx->on_recv_stream) {
606 ret = ctx->on_recv_stream(new_stream);
607 if (ret < 0) {
d771f832 608 consumer_stream_free(new_stream);
633d0084 609 goto end_nosignal;
fb3a43a9 610 }
633d0084 611 }
fb3a43a9 612
07b86b52
JD
613 if (new_stream->metadata_flag) {
614 channel->metadata_stream = new_stream;
615 }
616
2bba9e53
DG
617 /* Do not monitor this stream. */
618 if (!channel->monitor) {
5eecee74 619 DBG("Kernel consumer add stream %s in no monitor mode with "
6dc3064a 620 "relayd id %" PRIu64, new_stream->name,
5eecee74 621 new_stream->net_seq_idx);
10a50311 622 cds_list_add(&new_stream->send_node, &channel->streams.head);
6dc3064a
DG
623 break;
624 }
625
10a50311 626 ret = consumer_send_relayd_stream(new_stream, NULL);
e2039c7a 627 if (ret < 0) {
d771f832 628 consumer_stream_free(new_stream);
e2039c7a
JD
629 goto end_nosignal;
630 }
631
50f8ae69 632 /* Get the right pipe where the stream will be sent. */
633d0084 633 if (new_stream->metadata_flag) {
dae10966 634 stream_pipe = ctx->consumer_metadata_pipe;
3bd1e081 635 } else {
dae10966 636 stream_pipe = ctx->consumer_data_pipe;
50f8ae69
DG
637 }
638
dae10966 639 ret = lttng_pipe_write(stream_pipe, &new_stream, sizeof(new_stream));
50f8ae69 640 if (ret < 0) {
dae10966 641 ERR("Consumer write %s stream to pipe %d",
50f8ae69 642 new_stream->metadata_flag ? "metadata" : "data",
dae10966 643 lttng_pipe_get_writefd(stream_pipe));
d771f832 644 consumer_stream_free(new_stream);
50f8ae69 645 goto end_nosignal;
3bd1e081 646 }
00e2e675 647
50f8ae69 648 DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64,
ffe60014 649 new_stream->name, fd, new_stream->relayd_stream_id);
3bd1e081
MD
650 break;
651 }
652 case LTTNG_CONSUMER_UPDATE_STREAM:
653 {
3f8e211f
DG
654 rcu_read_unlock();
655 return -ENOSYS;
656 }
657 case LTTNG_CONSUMER_DESTROY_RELAYD:
658 {
a6ba4fe1 659 uint64_t index = msg.u.destroy_relayd.net_seq_idx;
3f8e211f
DG
660 struct consumer_relayd_sock_pair *relayd;
661
a6ba4fe1 662 DBG("Kernel consumer destroying relayd %" PRIu64, index);
3f8e211f
DG
663
664 /* Get relayd reference if exists. */
a6ba4fe1 665 relayd = consumer_find_relayd(index);
3f8e211f 666 if (relayd == NULL) {
3448e266 667 DBG("Unable to find relayd %" PRIu64, index);
f50f23d9 668 ret_code = LTTNG_ERR_NO_CONSUMER;
3bd1e081 669 }
3f8e211f 670
a6ba4fe1
DG
671 /*
672 * Each relayd socket pair has a refcount of stream attached to it
673 * which tells if the relayd is still active or not depending on the
674 * refcount value.
675 *
676 * This will set the destroy flag of the relayd object and destroy it
677 * if the refcount reaches zero when called.
678 *
679 * The destroy can happen either here or when a stream fd hangs up.
680 */
f50f23d9
DG
681 if (relayd) {
682 consumer_flag_relayd_for_destroy(relayd);
683 }
684
685 ret = consumer_send_status_msg(sock, ret_code);
686 if (ret < 0) {
687 /* Somehow, the session daemon is not responding anymore. */
1803a064 688 goto error_fatal;
f50f23d9 689 }
3f8e211f 690
3f8e211f 691 goto end_nosignal;
3bd1e081 692 }
6d805429 693 case LTTNG_CONSUMER_DATA_PENDING:
53632229 694 {
c8f59ee5 695 int32_t ret;
6d805429 696 uint64_t id = msg.u.data_pending.session_id;
c8f59ee5 697
6d805429 698 DBG("Kernel consumer data pending command for id %" PRIu64, id);
c8f59ee5 699
6d805429 700 ret = consumer_data_pending(id);
c8f59ee5
DG
701
702 /* Send back returned value to session daemon */
703 ret = lttcomm_send_unix_sock(sock, &ret, sizeof(ret));
704 if (ret < 0) {
6d805429 705 PERROR("send data pending ret code");
1803a064 706 goto error_fatal;
c8f59ee5 707 }
f50f23d9
DG
708
709 /*
710 * No need to send back a status message since the data pending
711 * returned value is the response.
712 */
c8f59ee5 713 break;
53632229 714 }
6dc3064a
DG
715 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL:
716 {
07b86b52
JD
717 if (msg.u.snapshot_channel.metadata == 1) {
718 ret = lttng_kconsumer_snapshot_metadata(msg.u.snapshot_channel.key,
e2039c7a
JD
719 msg.u.snapshot_channel.pathname,
720 msg.u.snapshot_channel.relayd_id, ctx);
07b86b52
JD
721 if (ret < 0) {
722 ERR("Snapshot metadata failed");
723 ret_code = LTTNG_ERR_KERN_META_FAIL;
724 }
725 } else {
726 ret = lttng_kconsumer_snapshot_channel(msg.u.snapshot_channel.key,
727 msg.u.snapshot_channel.pathname,
728 msg.u.snapshot_channel.relayd_id, ctx);
729 if (ret < 0) {
730 ERR("Snapshot channel failed");
731 ret_code = LTTNG_ERR_KERN_CHAN_FAIL;
732 }
733 }
734
6dc3064a
DG
735 ret = consumer_send_status_msg(sock, ret_code);
736 if (ret < 0) {
737 /* Somehow, the session daemon is not responding anymore. */
738 goto end_nosignal;
739 }
740 break;
741 }
07b86b52
JD
742 case LTTNG_CONSUMER_DESTROY_CHANNEL:
743 {
744 uint64_t key = msg.u.destroy_channel.key;
745 struct lttng_consumer_channel *channel;
746
747 channel = consumer_find_channel(key);
748 if (!channel) {
749 ERR("Kernel consumer destroy channel %" PRIu64 " not found", key);
750 ret_code = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
751 }
752
753 ret = consumer_send_status_msg(sock, ret_code);
754 if (ret < 0) {
755 /* Somehow, the session daemon is not responding anymore. */
756 goto end_nosignal;
757 }
758
759 /*
760 * This command should ONLY be issued for channel with streams set in
761 * no monitor mode.
762 */
763 assert(!channel->monitor);
764
765 /*
766 * The refcount should ALWAYS be 0 in the case of a channel in no
767 * monitor mode.
768 */
769 assert(!uatomic_sub_return(&channel->refcount, 1));
770
771 consumer_del_channel(channel);
772
773 goto end_nosignal;
774 }
3bd1e081 775 default:
3f8e211f 776 goto end_nosignal;
3bd1e081 777 }
3f8e211f 778
3bd1e081 779end_nosignal:
b0b335c8 780 rcu_read_unlock();
4cbc1a04
DG
781
782 /*
783 * Return 1 to indicate success since the 0 value can be a socket
784 * shutdown during the recv() or send() call.
785 */
786 return 1;
1803a064
MD
787
788error_fatal:
789 rcu_read_unlock();
790 /* This will issue a consumer stop. */
791 return -1;
3bd1e081 792}
d41f73b7
MD
793
794/*
795 * Consume data on a file descriptor and write it on a trace file.
796 */
4078b776 797ssize_t lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream *stream,
d41f73b7
MD
798 struct lttng_consumer_local_data *ctx)
799{
1d4dfdef 800 unsigned long len, subbuf_size, padding;
d41f73b7 801 int err;
4078b776 802 ssize_t ret = 0;
d41f73b7
MD
803 int infd = stream->wait_fd;
804
805 DBG("In read_subbuffer (infd : %d)", infd);
806 /* Get the next subbuffer */
807 err = kernctl_get_next_subbuf(infd);
808 if (err != 0) {
1d4dfdef 809 ret = err;
d41f73b7
MD
810 /*
811 * This is a debug message even for single-threaded consumer,
812 * because poll() have more relaxed criterions than get subbuf,
813 * so get_subbuf may fail for short race windows where poll()
814 * would issue wakeups.
815 */
816 DBG("Reserving sub buffer failed (everything is normal, "
817 "it is due to concurrency)");
818 goto end;
819 }
820
1d4dfdef
DG
821 /* Get the full subbuffer size including padding */
822 err = kernctl_get_padded_subbuf_size(infd, &len);
823 if (err != 0) {
824 errno = -err;
825 perror("Getting sub-buffer len failed.");
826 ret = err;
827 goto end;
828 }
829
ffe60014 830 switch (stream->chan->output) {
07b86b52 831 case CONSUMER_CHANNEL_SPLICE:
1d4dfdef
DG
832 /*
833 * XXX: The lttng-modules splice "actor" does not handle copying
834 * partial pages hence only using the subbuffer size without the
835 * padding makes the splice fail.
836 */
837 subbuf_size = len;
838 padding = 0;
839
840 /* splice the subbuffer to the tracefile */
91dfef6e
DG
841 ret = lttng_consumer_on_read_subbuffer_splice(ctx, stream, subbuf_size,
842 padding);
843 /*
844 * XXX: Splice does not support network streaming so the return value
845 * is simply checked against subbuf_size and not like the mmap() op.
846 */
1d4dfdef
DG
847 if (ret != subbuf_size) {
848 /*
849 * display the error but continue processing to try
850 * to release the subbuffer
851 */
852 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
853 ret, subbuf_size);
854 }
855 break;
07b86b52 856 case CONSUMER_CHANNEL_MMAP:
1d4dfdef
DG
857 /* Get subbuffer size without padding */
858 err = kernctl_get_subbuf_size(infd, &subbuf_size);
859 if (err != 0) {
860 errno = -err;
861 perror("Getting sub-buffer len failed.");
862 ret = err;
863 goto end;
864 }
47e81c02 865
1d4dfdef
DG
866 /* Make sure the tracer is not gone mad on us! */
867 assert(len >= subbuf_size);
868
869 padding = len - subbuf_size;
870
871 /* write the subbuffer to the tracefile */
91dfef6e
DG
872 ret = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, subbuf_size,
873 padding);
874 /*
875 * The mmap operation should write subbuf_size amount of data when
876 * network streaming or the full padding (len) size when we are _not_
877 * streaming.
878 */
d88aee68
DG
879 if ((ret != subbuf_size && stream->net_seq_idx != (uint64_t) -1ULL) ||
880 (ret != len && stream->net_seq_idx == (uint64_t) -1ULL)) {
1d4dfdef 881 /*
91dfef6e
DG
882 * Display the error but continue processing to try to release the
883 * subbuffer
1d4dfdef 884 */
91dfef6e
DG
885 ERR("Error writing to tracefile "
886 "(ret: %zd != len: %lu != subbuf_size: %lu)",
887 ret, len, subbuf_size);
1d4dfdef
DG
888 }
889 break;
890 default:
891 ERR("Unknown output method");
892 ret = -1;
d41f73b7
MD
893 }
894
895 err = kernctl_put_next_subbuf(infd);
896 if (err != 0) {
21073eaa 897 errno = -err;
d41f73b7
MD
898 if (errno == EFAULT) {
899 perror("Error in unreserving sub buffer\n");
900 } else if (errno == EIO) {
901 /* Should never happen with newer LTTng versions */
902 perror("Reader has been pushed by the writer, last sub-buffer corrupted.");
903 }
21073eaa
DG
904
905 ret = -err;
d41f73b7
MD
906 goto end;
907 }
908
909end:
910 return ret;
911}
912
913int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream *stream)
914{
915 int ret;
ffe60014
DG
916
917 assert(stream);
918
2bba9e53
DG
919 /*
920 * Don't create anything if this is set for streaming or should not be
921 * monitored.
922 */
923 if (stream->net_seq_idx == (uint64_t) -1ULL && stream->chan->monitor) {
fe4477ee
JD
924 ret = utils_create_stream_file(stream->chan->pathname, stream->name,
925 stream->chan->tracefile_size, stream->tracefile_count_current,
926 stream->uid, stream->gid);
927 if (ret < 0) {
928 goto error;
929 }
930 stream->out_fd = ret;
931 stream->tracefile_size_current = 0;
ffe60014 932 }
d41f73b7 933
d41f73b7
MD
934 if (stream->output == LTTNG_EVENT_MMAP) {
935 /* get the len of the mmap region */
936 unsigned long mmap_len;
937
938 ret = kernctl_get_mmap_len(stream->wait_fd, &mmap_len);
939 if (ret != 0) {
87dc6a9c 940 errno = -ret;
ffe60014 941 PERROR("kernctl_get_mmap_len");
d41f73b7
MD
942 goto error_close_fd;
943 }
944 stream->mmap_len = (size_t) mmap_len;
945
ffe60014
DG
946 stream->mmap_base = mmap(NULL, stream->mmap_len, PROT_READ,
947 MAP_PRIVATE, stream->wait_fd, 0);
d41f73b7 948 if (stream->mmap_base == MAP_FAILED) {
ffe60014 949 PERROR("Error mmaping");
d41f73b7
MD
950 ret = -1;
951 goto error_close_fd;
952 }
953 }
954
955 /* we return 0 to let the library handle the FD internally */
956 return 0;
957
958error_close_fd:
959 {
960 int err;
961
962 err = close(stream->out_fd);
963 assert(!err);
964 }
965error:
966 return ret;
967}
968
ca22feea
DG
969/*
970 * Check if data is still being extracted from the buffers for a specific
4e9a4686
DG
971 * stream. Consumer data lock MUST be acquired before calling this function
972 * and the stream lock.
ca22feea 973 *
6d805429 974 * Return 1 if the traced data are still getting read else 0 meaning that the
ca22feea
DG
975 * data is available for trace viewer reading.
976 */
6d805429 977int lttng_kconsumer_data_pending(struct lttng_consumer_stream *stream)
ca22feea
DG
978{
979 int ret;
980
981 assert(stream);
982
ca22feea
DG
983 ret = kernctl_get_next_subbuf(stream->wait_fd);
984 if (ret == 0) {
985 /* There is still data so let's put back this subbuffer. */
986 ret = kernctl_put_subbuf(stream->wait_fd);
987 assert(ret == 0);
6d805429 988 ret = 1; /* Data is pending */
4e9a4686 989 goto end;
ca22feea
DG
990 }
991
6d805429
DG
992 /* Data is NOT pending and ready to be read. */
993 ret = 0;
ca22feea 994
6efae65e
DG
995end:
996 return ret;
ca22feea 997}
This page took 0.084635 seconds and 4 git commands to generate.