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