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