Use compiler-agnostic defines to silence warning
[lttng-tools.git] / src / common / ust-consumer / ust-consumer.cpp
... / ...
CommitLineData
1/*
2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 *
6 * SPDX-License-Identifier: GPL-2.0-only
7 *
8 */
9
10#define _LGPL_SOURCE
11#include "ust-consumer.hpp"
12
13#include <common/common.hpp>
14#include <common/compat/endian.hpp>
15#include <common/consumer/consumer-metadata-cache.hpp>
16#include <common/consumer/consumer-stream.hpp>
17#include <common/consumer/consumer-timer.hpp>
18#include <common/consumer/consumer.hpp>
19#include <common/index/index.hpp>
20#include <common/optional.hpp>
21#include <common/pthread-lock.hpp>
22#include <common/relayd/relayd.hpp>
23#include <common/scope-exit.hpp>
24#include <common/sessiond-comm/sessiond-comm.hpp>
25#include <common/shm.hpp>
26#include <common/urcu.hpp>
27#include <common/utils.hpp>
28
29#include <lttng/ust-ctl.h>
30#include <lttng/ust-sigbus.h>
31
32#include <bin/lttng-consumerd/health-consumerd.hpp>
33#include <fcntl.h>
34#include <inttypes.h>
35#include <poll.h>
36#include <pthread.h>
37#include <signal.h>
38#include <stdbool.h>
39#include <stdint.h>
40#include <stdlib.h>
41#include <string.h>
42#include <sys/mman.h>
43#include <sys/socket.h>
44#include <sys/stat.h>
45#include <sys/types.h>
46#include <unistd.h>
47#include <urcu/list.h>
48
49#define INT_MAX_STR_LEN 12 /* includes \0 */
50
51extern struct lttng_consumer_global_data the_consumer_data;
52extern int consumer_poll_timeout;
53
54LTTNG_EXPORT DEFINE_LTTNG_UST_SIGBUS_STATE();
55
56/*
57 * Add channel to internal consumer state.
58 *
59 * Returns 0 on success or else a negative value.
60 */
61static int add_channel(struct lttng_consumer_channel *channel,
62 struct lttng_consumer_local_data *ctx)
63{
64 int ret = 0;
65
66 LTTNG_ASSERT(channel);
67 LTTNG_ASSERT(!channel->is_deleted);
68 LTTNG_ASSERT(ctx);
69
70 if (ctx->on_recv_channel != nullptr) {
71 ret = ctx->on_recv_channel(channel);
72 if (ret == 0) {
73 ret = consumer_add_channel(channel, ctx);
74 } else if (ret < 0) {
75 /* Most likely an ENOMEM. */
76 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
77 goto error;
78 }
79 } else {
80 ret = consumer_add_channel(channel, ctx);
81 }
82
83 DBG("UST consumer channel added (key: %" PRIu64 ")", channel->key);
84
85error:
86 return ret;
87}
88
89/*
90 * Allocate and return a consumer stream object. If _alloc_ret is not NULL, the
91 * error value if applicable is set in it else it is kept untouched.
92 *
93 * Return NULL on error else the newly allocated stream object.
94 */
95static struct lttng_consumer_stream *allocate_stream(int cpu,
96 int key,
97 struct lttng_consumer_channel *channel,
98 struct lttng_consumer_local_data *ctx,
99 int *_alloc_ret)
100{
101 int alloc_ret;
102 struct lttng_consumer_stream *stream = nullptr;
103
104 LTTNG_ASSERT(channel);
105 LTTNG_ASSERT(!channel->is_deleted);
106 LTTNG_ASSERT(ctx);
107
108 stream = consumer_stream_create(channel,
109 channel->key,
110 key,
111 channel->name,
112 channel->relayd_id,
113 channel->session_id,
114 channel->trace_chunk,
115 cpu,
116 &alloc_ret,
117 channel->type,
118 channel->monitor);
119 if (stream == nullptr) {
120 switch (alloc_ret) {
121 case -ENOENT:
122 /*
123 * We could not find the channel. Can happen if cpu hotplug
124 * happens while tearing down.
125 */
126 DBG3("Could not find channel");
127 break;
128 case -ENOMEM:
129 case -EINVAL:
130 default:
131 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
132 break;
133 }
134 goto error;
135 }
136
137 consumer_stream_update_channel_attributes(stream, channel);
138
139error:
140 if (_alloc_ret) {
141 *_alloc_ret = alloc_ret;
142 }
143 return stream;
144}
145
146/*
147 * Send the given stream pointer to the corresponding thread.
148 *
149 * Returns 0 on success else a negative value.
150 */
151static int send_stream_to_thread(struct lttng_consumer_stream *stream,
152 struct lttng_consumer_local_data *ctx)
153{
154 int ret;
155 struct lttng_pipe *stream_pipe;
156
157 /* Get the right pipe where the stream will be sent. */
158 if (stream->metadata_flag) {
159 consumer_add_metadata_stream(stream);
160 stream_pipe = ctx->consumer_metadata_pipe;
161 } else {
162 consumer_add_data_stream(stream);
163 stream_pipe = ctx->consumer_data_pipe;
164 }
165
166 /*
167 * From this point on, the stream's ownership has been moved away from
168 * the channel and it becomes globally visible. Hence, remove it from
169 * the local stream list to prevent the stream from being both local and
170 * global.
171 */
172 stream->globally_visible = 1;
173 cds_list_del_init(&stream->send_node);
174
175 ret = lttng_pipe_write(stream_pipe, &stream, sizeof(stream)); /* NOLINT sizeof used on a
176 pointer. */
177 if (ret < 0) {
178 ERR("Consumer write %s stream to pipe %d",
179 stream->metadata_flag ? "metadata" : "data",
180 lttng_pipe_get_writefd(stream_pipe));
181 if (stream->metadata_flag) {
182 consumer_del_stream_for_metadata(stream);
183 } else {
184 consumer_del_stream_for_data(stream);
185 }
186 goto error;
187 }
188
189error:
190 return ret;
191}
192
193static int get_stream_shm_path(char *stream_shm_path, const char *shm_path, int cpu)
194{
195 char cpu_nr[INT_MAX_STR_LEN]; /* int max len */
196 int ret;
197
198 strncpy(stream_shm_path, shm_path, PATH_MAX);
199 stream_shm_path[PATH_MAX - 1] = '\0';
200 ret = snprintf(cpu_nr, INT_MAX_STR_LEN, "%i", cpu);
201 if (ret < 0) {
202 PERROR("snprintf");
203 goto end;
204 }
205 strncat(stream_shm_path, cpu_nr, PATH_MAX - strlen(stream_shm_path) - 1);
206 ret = 0;
207end:
208 return ret;
209}
210
211/*
212 * Create streams for the given channel using liblttng-ust-ctl.
213 * The channel lock must be acquired by the caller.
214 *
215 * Return 0 on success else a negative value.
216 */
217static int create_ust_streams(struct lttng_consumer_channel *channel,
218 struct lttng_consumer_local_data *ctx)
219{
220 int ret, cpu = 0;
221 struct lttng_ust_ctl_consumer_stream *ustream;
222 struct lttng_consumer_stream *stream;
223 pthread_mutex_t *current_stream_lock = nullptr;
224
225 LTTNG_ASSERT(channel);
226 LTTNG_ASSERT(!channel->is_deleted);
227 LTTNG_ASSERT(ctx);
228
229 /*
230 * While a stream is available from ustctl. When NULL is returned, we've
231 * reached the end of the possible stream for the channel.
232 */
233 while ((ustream = lttng_ust_ctl_create_stream(channel->uchan, cpu))) {
234 int wait_fd;
235 int ust_metadata_pipe[2];
236
237 health_code_update();
238
239 if (channel->type == CONSUMER_CHANNEL_TYPE_METADATA && channel->monitor) {
240 ret = utils_create_pipe_cloexec_nonblock(ust_metadata_pipe);
241 if (ret < 0) {
242 ERR("Create ust metadata poll pipe");
243 goto error;
244 }
245 wait_fd = ust_metadata_pipe[0];
246 } else {
247 wait_fd = lttng_ust_ctl_stream_get_wait_fd(ustream);
248 }
249
250 /* Allocate consumer stream object. */
251 stream = allocate_stream(cpu, wait_fd, channel, ctx, &ret);
252 if (!stream) {
253 goto error_alloc;
254 }
255 stream->ustream = ustream;
256 /*
257 * Store it so we can save multiple function calls afterwards since
258 * this value is used heavily in the stream threads. This is UST
259 * specific so this is why it's done after allocation.
260 */
261 stream->wait_fd = wait_fd;
262
263 /*
264 * Increment channel refcount since the channel reference has now been
265 * assigned in the allocation process above.
266 */
267 if (stream->chan->monitor) {
268 uatomic_inc(&stream->chan->refcount);
269 }
270
271 pthread_mutex_lock(&stream->lock);
272 current_stream_lock = &stream->lock;
273 /*
274 * Order is important this is why a list is used. On error, the caller
275 * should clean this list.
276 */
277 cds_list_add_tail(&stream->send_node, &channel->streams.head);
278
279 ret = lttng_ust_ctl_get_max_subbuf_size(stream->ustream, &stream->max_sb_size);
280 if (ret < 0) {
281 ERR("lttng_ust_ctl_get_max_subbuf_size failed for stream %s", stream->name);
282 goto error;
283 }
284
285 /* Do actions once stream has been received. */
286 if (ctx->on_recv_stream) {
287 ret = ctx->on_recv_stream(stream);
288 if (ret < 0) {
289 goto error;
290 }
291 }
292
293 DBG("UST consumer add stream %s (key: %" PRIu64 ") with relayd id %" PRIu64,
294 stream->name,
295 stream->key,
296 stream->relayd_stream_id);
297
298 /* Set next CPU stream. */
299 channel->streams.count = ++cpu;
300
301 /* Keep stream reference when creating metadata. */
302 if (channel->type == CONSUMER_CHANNEL_TYPE_METADATA) {
303 channel->metadata_stream = stream;
304 if (channel->monitor) {
305 /* Set metadata poll pipe if we created one */
306 memcpy(stream->ust_metadata_poll_pipe,
307 ust_metadata_pipe,
308 sizeof(ust_metadata_pipe));
309 }
310 }
311 pthread_mutex_unlock(&stream->lock);
312 current_stream_lock = nullptr;
313 }
314
315 return 0;
316
317error:
318error_alloc:
319 if (current_stream_lock) {
320 pthread_mutex_unlock(current_stream_lock);
321 }
322 return ret;
323}
324
325static int open_ust_stream_fd(struct lttng_consumer_channel *channel,
326 int cpu,
327 const struct lttng_credentials *session_credentials)
328{
329 char shm_path[PATH_MAX];
330 int ret;
331
332 if (!channel->shm_path[0]) {
333 return shm_create_anonymous("ust-consumer");
334 }
335 ret = get_stream_shm_path(shm_path, channel->shm_path, cpu);
336 if (ret) {
337 goto error_shm_path;
338 }
339 return run_as_open(shm_path,
340 O_RDWR | O_CREAT | O_EXCL,
341 S_IRUSR | S_IWUSR,
342 lttng_credentials_get_uid(session_credentials),
343 lttng_credentials_get_gid(session_credentials));
344
345error_shm_path:
346 return -1;
347}
348
349/*
350 * Create an UST channel with the given attributes and send it to the session
351 * daemon using the ust ctl API.
352 *
353 * Return 0 on success or else a negative value.
354 */
355static int create_ust_channel(struct lttng_consumer_channel *channel,
356 struct lttng_ust_ctl_consumer_channel_attr *attr,
357 struct lttng_ust_ctl_consumer_channel **ust_chanp)
358{
359 int ret, nr_stream_fds, i, j;
360 int *stream_fds;
361 struct lttng_ust_ctl_consumer_channel *ust_channel;
362
363 LTTNG_ASSERT(channel);
364 LTTNG_ASSERT(!channel->is_deleted);
365 LTTNG_ASSERT(attr);
366 LTTNG_ASSERT(ust_chanp);
367 LTTNG_ASSERT(channel->buffer_credentials.is_set);
368
369 DBG3("Creating channel to ustctl with attr: [overwrite: %d, "
370 "subbuf_size: %" PRIu64 ", num_subbuf: %" PRIu64 ", "
371 "switch_timer_interval: %u, read_timer_interval: %u, "
372 "output: %d, type: %d",
373 attr->overwrite,
374 attr->subbuf_size,
375 attr->num_subbuf,
376 attr->switch_timer_interval,
377 attr->read_timer_interval,
378 attr->output,
379 attr->type);
380
381 if (channel->type == CONSUMER_CHANNEL_TYPE_METADATA)
382 nr_stream_fds = 1;
383 else
384 nr_stream_fds = lttng_ust_ctl_get_nr_stream_per_channel();
385 stream_fds = calloc<int>(nr_stream_fds);
386 if (!stream_fds) {
387 ret = -1;
388 goto error_alloc;
389 }
390 for (i = 0; i < nr_stream_fds; i++) {
391 stream_fds[i] = open_ust_stream_fd(channel, i, &channel->buffer_credentials.value);
392 if (stream_fds[i] < 0) {
393 ret = -1;
394 goto error_open;
395 }
396 }
397 ust_channel = lttng_ust_ctl_create_channel(attr, stream_fds, nr_stream_fds);
398 if (!ust_channel) {
399 ret = -1;
400 goto error_create;
401 }
402 channel->nr_stream_fds = nr_stream_fds;
403 channel->stream_fds = stream_fds;
404 *ust_chanp = ust_channel;
405
406 return 0;
407
408error_create:
409error_open:
410 for (j = i - 1; j >= 0; j--) {
411 int closeret;
412
413 closeret = close(stream_fds[j]);
414 if (closeret) {
415 PERROR("close");
416 }
417 if (channel->shm_path[0]) {
418 char shm_path[PATH_MAX];
419
420 closeret = get_stream_shm_path(shm_path, channel->shm_path, j);
421 if (closeret) {
422 ERR("Cannot get stream shm path");
423 }
424 closeret = run_as_unlink(shm_path,
425 lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(
426 channel->buffer_credentials)),
427 lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(
428 channel->buffer_credentials)));
429 if (closeret) {
430 PERROR("unlink %s", shm_path);
431 }
432 }
433 }
434 /* Try to rmdir all directories under shm_path root. */
435 if (channel->root_shm_path[0]) {
436 (void) run_as_rmdir_recursive(channel->root_shm_path,
437 lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(
438 channel->buffer_credentials)),
439 lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(
440 channel->buffer_credentials)),
441 LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG);
442 }
443 free(stream_fds);
444error_alloc:
445 return ret;
446}
447
448/*
449 * Send a single given stream to the session daemon using the sock.
450 *
451 * Return 0 on success else a negative value.
452 */
453static int send_sessiond_stream(int sock, struct lttng_consumer_stream *stream)
454{
455 int ret;
456
457 LTTNG_ASSERT(stream);
458 LTTNG_ASSERT(sock >= 0);
459
460 DBG("UST consumer sending stream %" PRIu64 " to sessiond", stream->key);
461
462 /* Send stream to session daemon. */
463 ret = lttng_ust_ctl_send_stream_to_sessiond(sock, stream->ustream);
464 if (ret < 0) {
465 goto error;
466 }
467
468error:
469 return ret;
470}
471
472/*
473 * Send channel to sessiond and relayd if applicable.
474 *
475 * Return 0 on success or else a negative value.
476 */
477static int send_channel_to_sessiond_and_relayd(int sock,
478 struct lttng_consumer_channel *channel,
479 struct lttng_consumer_local_data *ctx,
480 int *relayd_error)
481{
482 int ret, ret_code = LTTCOMM_CONSUMERD_SUCCESS;
483 uint64_t net_seq_idx = -1ULL;
484
485 LTTNG_ASSERT(channel);
486 LTTNG_ASSERT(!channel->is_deleted);
487 LTTNG_ASSERT(ctx);
488 LTTNG_ASSERT(sock >= 0);
489
490 DBG("UST consumer sending channel %s to sessiond", channel->name);
491
492 if (channel->relayd_id != (uint64_t) -1ULL) {
493 for (auto stream :
494 lttng::urcu::list_iteration_adapter<lttng_consumer_stream,
495 &lttng_consumer_stream::send_node>(
496 channel->streams.head)) {
497 health_code_update();
498
499 /* Try to send the stream to the relayd if one is available. */
500 DBG("Sending stream %" PRIu64 " of channel \"%s\" to relayd",
501 stream->key,
502 channel->name);
503 ret = consumer_send_relayd_stream(stream, stream->chan->pathname);
504 if (ret < 0) {
505 /*
506 * Flag that the relayd was the problem here probably due to a
507 * communicaton error on the socket.
508 */
509 if (relayd_error) {
510 *relayd_error = 1;
511 }
512 ret_code = LTTCOMM_CONSUMERD_RELAYD_FAIL;
513 }
514 if (net_seq_idx == -1ULL) {
515 net_seq_idx = stream->net_seq_idx;
516 }
517 }
518 }
519
520 /* Inform sessiond that we are about to send channel and streams. */
521 ret = consumer_send_status_msg(sock, ret_code);
522 if (ret < 0 || ret_code != LTTCOMM_CONSUMERD_SUCCESS) {
523 /*
524 * Either the session daemon is not responding or the relayd died so we
525 * stop now.
526 */
527 goto error;
528 }
529
530 /* Send channel to sessiond. */
531 ret = lttng_ust_ctl_send_channel_to_sessiond(sock, channel->uchan);
532 if (ret < 0) {
533 goto error;
534 }
535
536 ret = lttng_ust_ctl_channel_close_wakeup_fd(channel->uchan);
537 if (ret < 0) {
538 goto error;
539 }
540
541 /* The channel was sent successfully to the sessiond at this point. */
542 for (auto stream : lttng::urcu::list_iteration_adapter<lttng_consumer_stream,
543 &lttng_consumer_stream::send_node>(
544 channel->streams.head)) {
545 health_code_update();
546
547 /* Send stream to session daemon. */
548 ret = send_sessiond_stream(sock, stream);
549 if (ret < 0) {
550 goto error;
551 }
552 }
553
554 /* Tell sessiond there is no more stream. */
555 ret = lttng_ust_ctl_send_stream_to_sessiond(sock, nullptr);
556 if (ret < 0) {
557 goto error;
558 }
559
560 DBG("UST consumer NULL stream sent to sessiond");
561
562 return 0;
563
564error:
565 if (ret_code != LTTCOMM_CONSUMERD_SUCCESS) {
566 ret = -1;
567 }
568 return ret;
569}
570
571/*
572 * Creates a channel and streams and add the channel it to the channel internal
573 * state. The created stream must ONLY be sent once the GET_CHANNEL command is
574 * received.
575 *
576 * Return 0 on success or else, a negative value is returned and the channel
577 * MUST be destroyed by consumer_del_channel().
578 */
579static int ask_channel(struct lttng_consumer_local_data *ctx,
580 struct lttng_consumer_channel *channel,
581 struct lttng_ust_ctl_consumer_channel_attr *attr)
582{
583 int ret;
584
585 LTTNG_ASSERT(ctx);
586 LTTNG_ASSERT(channel);
587 LTTNG_ASSERT(!channel->is_deleted);
588 LTTNG_ASSERT(attr);
589
590 /*
591 * This value is still used by the kernel consumer since for the kernel,
592 * the stream ownership is not IN the consumer so we need to have the
593 * number of left stream that needs to be initialized so we can know when
594 * to delete the channel (see consumer.c).
595 *
596 * As for the user space tracer now, the consumer creates and sends the
597 * stream to the session daemon which only sends them to the application
598 * once every stream of a channel is received making this value useless
599 * because we they will be added to the poll thread before the application
600 * receives them. This ensures that a stream can not hang up during
601 * initilization of a channel.
602 */
603 channel->nb_init_stream_left = 0;
604
605 /* The reply msg status is handled in the following call. */
606 ret = create_ust_channel(channel, attr, &channel->uchan);
607 if (ret < 0) {
608 goto end;
609 }
610
611 channel->wait_fd = lttng_ust_ctl_channel_get_wait_fd(channel->uchan);
612
613 /*
614 * For the snapshots (no monitor), we create the metadata streams
615 * on demand, not during the channel creation.
616 */
617 if (channel->type == CONSUMER_CHANNEL_TYPE_METADATA && !channel->monitor) {
618 ret = 0;
619 goto end;
620 }
621
622 /* Open all streams for this channel. */
623 pthread_mutex_lock(&channel->lock);
624 ret = create_ust_streams(channel, ctx);
625 pthread_mutex_unlock(&channel->lock);
626 if (ret < 0) {
627 goto end;
628 }
629
630end:
631 return ret;
632}
633
634/*
635 * Send all stream of a channel to the right thread handling it.
636 *
637 * On error, return a negative value else 0 on success.
638 */
639static int send_streams_to_thread(struct lttng_consumer_channel *channel,
640 struct lttng_consumer_local_data *ctx)
641{
642 int ret = 0;
643
644 LTTNG_ASSERT(channel);
645 LTTNG_ASSERT(!channel->is_deleted);
646 LTTNG_ASSERT(ctx);
647
648 /* Send streams to the corresponding thread. */
649 for (auto stream : lttng::urcu::list_iteration_adapter<lttng_consumer_stream,
650 &lttng_consumer_stream::send_node>(
651 channel->streams.head)) {
652 health_code_update();
653
654 /* Sending the stream to the thread. */
655 ret = send_stream_to_thread(stream, ctx);
656 if (ret < 0) {
657 /*
658 * If we are unable to send the stream to the thread, there is
659 * a big problem so just stop everything.
660 */
661 goto error;
662 }
663 }
664
665error:
666 return ret;
667}
668
669/*
670 * Flush channel's streams using the given key to retrieve the channel.
671 *
672 * Return 0 on success else an LTTng error code.
673 */
674static int flush_channel(uint64_t chan_key)
675{
676 int ret = 0;
677 struct lttng_consumer_channel *channel;
678 const auto ht = the_consumer_data.stream_per_chan_id_ht;
679
680 DBG("UST consumer flush channel key %" PRIu64, chan_key);
681
682 const lttng::urcu::read_lock_guard read_lock;
683 channel = consumer_find_channel(chan_key);
684 if (!channel) {
685 ERR("UST consumer flush channel %" PRIu64 " not found", chan_key);
686 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
687 goto error;
688 }
689
690 /* For each stream of the channel id, flush it. */
691 for (auto *stream : lttng::urcu::lfht_filtered_iteration_adapter<
692 lttng_consumer_stream,
693 decltype(lttng_consumer_stream::node_channel_id),
694 &lttng_consumer_stream::node_channel_id,
695 std::uint64_t>(*ht->ht,
696 &channel->key,
697 ht->hash_fct(&channel->key, lttng_ht_seed),
698 ht->match_fct)) {
699 health_code_update();
700
701 pthread_mutex_lock(&stream->lock);
702
703 /*
704 * Protect against concurrent teardown of a stream.
705 */
706 if (cds_lfht_is_node_deleted(&stream->node.node)) {
707 goto next;
708 }
709
710 if (!stream->quiescent) {
711 ret = lttng_ust_ctl_flush_buffer(stream->ustream, 0);
712 if (ret) {
713 ERR("Failed to flush buffer while flushing channel: channel key = %" PRIu64
714 ", channel name = '%s'",
715 chan_key,
716 channel->name);
717 ret = LTTNG_ERR_BUFFER_FLUSH_FAILED;
718 pthread_mutex_unlock(&stream->lock);
719 goto error;
720 }
721 stream->quiescent = true;
722 }
723 next:
724 pthread_mutex_unlock(&stream->lock);
725 }
726
727 /*
728 * Send one last buffer statistics update to the session daemon. This
729 * ensures that the session daemon gets at least one statistics update
730 * per channel even in the case of short-lived channels, such as when a
731 * short-lived app is traced in per-pid mode.
732 */
733 sample_and_send_channel_buffer_stats(channel);
734error:
735 return ret;
736}
737
738/*
739 * Clear quiescent state from channel's streams using the given key to
740 * retrieve the channel.
741 *
742 * Return 0 on success else an LTTng error code.
743 */
744static int clear_quiescent_channel(uint64_t chan_key)
745{
746 DBG("UST consumer clear quiescent channel key %" PRIu64, chan_key);
747
748 const lttng::urcu::read_lock_guard read_lock;
749 auto channel = consumer_find_channel(chan_key);
750 if (!channel) {
751 ERR("UST consumer clear quiescent channel %" PRIu64 " not found", chan_key);
752 return LTTNG_ERR_UST_CHAN_NOT_FOUND;
753 }
754
755 const auto ht = the_consumer_data.stream_per_chan_id_ht;
756
757 /* For each stream of the channel id, clear quiescent state. */
758 for (auto *stream : lttng::urcu::lfht_filtered_iteration_adapter<
759 lttng_consumer_stream,
760 decltype(lttng_consumer_stream::node_channel_id),
761 &lttng_consumer_stream::node_channel_id,
762 std::uint64_t>(*ht->ht,
763 &channel->key,
764 ht->hash_fct(&channel->key, lttng_ht_seed),
765 ht->match_fct)) {
766 health_code_update();
767
768 const lttng::pthread::lock_guard stream_lock(stream->lock);
769 stream->quiescent = false;
770 }
771
772 return 0;
773}
774
775/*
776 * Close metadata stream wakeup_fd using the given key to retrieve the channel.
777 *
778 * Return 0 on success else an LTTng error code.
779 */
780static int close_metadata(uint64_t chan_key)
781{
782 int ret = 0;
783 struct lttng_consumer_channel *channel;
784 unsigned int channel_monitor;
785
786 DBG("UST consumer close metadata key %" PRIu64, chan_key);
787
788 channel = consumer_find_channel(chan_key);
789 if (!channel) {
790 /*
791 * This is possible if the metadata thread has issue a delete because
792 * the endpoint point of the stream hung up. There is no way the
793 * session daemon can know about it thus use a DBG instead of an actual
794 * error.
795 */
796 DBG("UST consumer close metadata %" PRIu64 " not found", chan_key);
797 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
798 goto error;
799 }
800
801 pthread_mutex_lock(&the_consumer_data.lock);
802 pthread_mutex_lock(&channel->lock);
803 channel_monitor = channel->monitor;
804 if (cds_lfht_is_node_deleted(&channel->node.node)) {
805 goto error_unlock;
806 }
807
808 lttng_ustconsumer_close_metadata(channel);
809 pthread_mutex_unlock(&channel->lock);
810 pthread_mutex_unlock(&the_consumer_data.lock);
811
812 /*
813 * The ownership of a metadata channel depends on the type of
814 * session to which it belongs. In effect, the monitor flag is checked
815 * to determine if this metadata channel is in "snapshot" mode or not.
816 *
817 * In the non-snapshot case, the metadata channel is created along with
818 * a single stream which will remain present until the metadata channel
819 * is destroyed (on the destruction of its session). In this case, the
820 * metadata stream in "monitored" by the metadata poll thread and holds
821 * the ownership of its channel.
822 *
823 * Closing the metadata will cause the metadata stream's "metadata poll
824 * pipe" to be closed. Closing this pipe will wake-up the metadata poll
825 * thread which will teardown the metadata stream which, in return,
826 * deletes the metadata channel.
827 *
828 * In the snapshot case, the metadata stream is created and destroyed
829 * on every snapshot record. Since the channel doesn't have an owner
830 * other than the session daemon, it is safe to destroy it immediately
831 * on reception of the CLOSE_METADATA command.
832 */
833 if (!channel_monitor) {
834 /*
835 * The channel and consumer_data locks must be
836 * released before this call since consumer_del_channel
837 * re-acquires the channel and consumer_data locks to teardown
838 * the channel and queue its reclamation by the "call_rcu"
839 * worker thread.
840 */
841 consumer_del_channel(channel);
842 }
843
844 return ret;
845error_unlock:
846 pthread_mutex_unlock(&channel->lock);
847 pthread_mutex_unlock(&the_consumer_data.lock);
848error:
849 return ret;
850}
851
852/*
853 * RCU read side lock MUST be acquired before calling this function.
854 *
855 * Return 0 on success else an LTTng error code.
856 */
857static int setup_metadata(struct lttng_consumer_local_data *ctx, uint64_t key)
858{
859 int ret;
860 struct lttng_consumer_channel *metadata;
861
862 ASSERT_RCU_READ_LOCKED();
863
864 DBG("UST consumer setup metadata key %" PRIu64, key);
865
866 metadata = consumer_find_channel(key);
867 if (!metadata) {
868 ERR("UST consumer push metadata %" PRIu64 " not found", key);
869 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
870 goto end;
871 }
872
873 /*
874 * In no monitor mode, the metadata channel has no stream(s) so skip the
875 * ownership transfer to the metadata thread.
876 */
877 if (!metadata->monitor) {
878 DBG("Metadata channel in no monitor");
879 ret = 0;
880 goto end;
881 }
882
883 /*
884 * Send metadata stream to relayd if one available. Availability is
885 * known if the stream is still in the list of the channel.
886 */
887 if (cds_list_empty(&metadata->streams.head)) {
888 ERR("Metadata channel key %" PRIu64 ", no stream available.", key);
889 ret = LTTCOMM_CONSUMERD_ERROR_METADATA;
890 goto error_no_stream;
891 }
892
893 /* Send metadata stream to relayd if needed. */
894 if (metadata->metadata_stream->net_seq_idx != (uint64_t) -1ULL) {
895 ret = consumer_send_relayd_stream(metadata->metadata_stream, metadata->pathname);
896 if (ret < 0) {
897 ret = LTTCOMM_CONSUMERD_ERROR_METADATA;
898 goto error;
899 }
900 ret = consumer_send_relayd_streams_sent(metadata->metadata_stream->net_seq_idx);
901 if (ret < 0) {
902 ret = LTTCOMM_CONSUMERD_RELAYD_FAIL;
903 goto error;
904 }
905 }
906
907 /*
908 * Ownership of metadata stream is passed along. Freeing is handled by
909 * the callee.
910 */
911 ret = send_streams_to_thread(metadata, ctx);
912 if (ret < 0) {
913 /*
914 * If we are unable to send the stream to the thread, there is
915 * a big problem so just stop everything.
916 */
917 ret = LTTCOMM_CONSUMERD_FATAL;
918 goto send_streams_error;
919 }
920 /* List MUST be empty after or else it could be reused. */
921 LTTNG_ASSERT(cds_list_empty(&metadata->streams.head));
922
923 ret = 0;
924 goto end;
925
926error:
927 /*
928 * Delete metadata channel on error. At this point, the metadata stream can
929 * NOT be monitored by the metadata thread thus having the guarantee that
930 * the stream is still in the local stream list of the channel. This call
931 * will make sure to clean that list.
932 */
933 consumer_stream_destroy(metadata->metadata_stream, nullptr);
934 metadata->metadata_stream = nullptr;
935 metadata->metadata_pushed_wait_queue.wake_all();
936
937send_streams_error:
938error_no_stream:
939end:
940 return ret;
941}
942
943/*
944 * Snapshot the whole metadata.
945 * RCU read-side lock must be held by the caller.
946 *
947 * Returns 0 on success, < 0 on error
948 */
949static int snapshot_metadata(struct lttng_consumer_channel *metadata_channel,
950 uint64_t key,
951 char *path,
952 uint64_t relayd_id,
953 struct lttng_consumer_local_data *ctx)
954{
955 int ret = 0;
956 struct lttng_consumer_stream *metadata_stream;
957
958 LTTNG_ASSERT(path);
959 LTTNG_ASSERT(ctx);
960 ASSERT_RCU_READ_LOCKED();
961
962 DBG("UST consumer snapshot metadata with key %" PRIu64 " at path %s", key, path);
963
964 const lttng::urcu::read_lock_guard read_lock;
965
966 LTTNG_ASSERT(!metadata_channel->monitor);
967
968 health_code_update();
969
970 /*
971 * Ask the sessiond if we have new metadata waiting and update the
972 * consumer metadata cache.
973 */
974 ret = lttng_ustconsumer_request_metadata(ctx, metadata_channel, false, 1);
975 if (ret < 0) {
976 goto error;
977 }
978
979 health_code_update();
980
981 /*
982 * The metadata stream is NOT created in no monitor mode when the channel
983 * is created on a sessiond ask channel command.
984 */
985 ret = create_ust_streams(metadata_channel, ctx);
986 if (ret < 0) {
987 goto error;
988 }
989
990 metadata_stream = metadata_channel->metadata_stream;
991 LTTNG_ASSERT(metadata_stream);
992
993 metadata_stream->read_subbuffer_ops.lock(metadata_stream);
994 if (relayd_id != (uint64_t) -1ULL) {
995 metadata_stream->net_seq_idx = relayd_id;
996 ret = consumer_send_relayd_stream(metadata_stream, path);
997 } else {
998 ret = consumer_stream_create_output_files(metadata_stream, false);
999 }
1000 if (ret < 0) {
1001 goto error_stream;
1002 }
1003
1004 do {
1005 health_code_update();
1006 ret = lttng_consumer_read_subbuffer(metadata_stream, ctx, true);
1007 if (ret < 0) {
1008 goto error_stream;
1009 }
1010 } while (ret > 0);
1011
1012error_stream:
1013 metadata_stream->read_subbuffer_ops.unlock(metadata_stream);
1014 /*
1015 * Clean up the stream completely because the next snapshot will use a
1016 * new metadata stream.
1017 */
1018 consumer_stream_destroy(metadata_stream, nullptr);
1019 metadata_channel->metadata_stream = nullptr;
1020 metadata_channel->metadata_pushed_wait_queue.wake_all();
1021
1022error:
1023 return ret;
1024}
1025
1026static int get_current_subbuf_addr(struct lttng_consumer_stream *stream, const char **addr)
1027{
1028 int ret;
1029 unsigned long mmap_offset;
1030 const char *mmap_base;
1031
1032 mmap_base = (const char *) lttng_ust_ctl_get_mmap_base(stream->ustream);
1033 if (!mmap_base) {
1034 ERR("Failed to get mmap base for stream `%s`", stream->name);
1035 ret = -EPERM;
1036 goto error;
1037 }
1038
1039 ret = lttng_ust_ctl_get_mmap_read_offset(stream->ustream, &mmap_offset);
1040 if (ret != 0) {
1041 ERR("Failed to get mmap offset for stream `%s`", stream->name);
1042 ret = -EINVAL;
1043 goto error;
1044 }
1045
1046 *addr = mmap_base + mmap_offset;
1047error:
1048 return ret;
1049}
1050
1051/*
1052 * Take a snapshot of all the stream of a channel.
1053 * RCU read-side lock and the channel lock must be held by the caller.
1054 *
1055 * Returns 0 on success, < 0 on error
1056 */
1057static int snapshot_channel(struct lttng_consumer_channel *channel,
1058 uint64_t key,
1059 char *path,
1060 uint64_t relayd_id,
1061 uint64_t nb_packets_per_stream,
1062 struct lttng_consumer_local_data *ctx)
1063{
1064 int ret;
1065 unsigned use_relayd = 0;
1066 unsigned long consumed_pos, produced_pos;
1067
1068 LTTNG_ASSERT(path);
1069 LTTNG_ASSERT(ctx);
1070 ASSERT_RCU_READ_LOCKED();
1071
1072 const lttng::urcu::read_lock_guard read_lock;
1073
1074 if (relayd_id != (uint64_t) -1ULL) {
1075 use_relayd = 1;
1076 }
1077
1078 LTTNG_ASSERT(!channel->monitor);
1079 DBG("UST consumer snapshot channel %" PRIu64, key);
1080
1081 for (auto stream : lttng::urcu::list_iteration_adapter<lttng_consumer_stream,
1082 &lttng_consumer_stream::send_node>(
1083 channel->streams.head)) {
1084 health_code_update();
1085
1086 /* Lock stream because we are about to change its state. */
1087 const lttng::pthread::lock_guard stream_lock(stream->lock);
1088 LTTNG_ASSERT(channel->trace_chunk);
1089 if (!lttng_trace_chunk_get(channel->trace_chunk)) {
1090 /*
1091 * Can't happen barring an internal error as the channel
1092 * holds a reference to the trace chunk.
1093 */
1094 ERR("Failed to acquire reference to channel's trace chunk");
1095 return -1;
1096 }
1097 LTTNG_ASSERT(!stream->trace_chunk);
1098 stream->trace_chunk = channel->trace_chunk;
1099
1100 stream->net_seq_idx = relayd_id;
1101
1102 /* Close stream output when were are done. */
1103 const auto close_stream_output = lttng::make_scope_exit(
1104 [stream]() noexcept { consumer_stream_close_output(stream); });
1105
1106 if (use_relayd) {
1107 ret = consumer_send_relayd_stream(stream, path);
1108 if (ret < 0) {
1109 return ret;
1110 }
1111 } else {
1112 ret = consumer_stream_create_output_files(stream, false);
1113 if (ret < 0) {
1114 return ret;
1115 }
1116
1117 DBG("UST consumer snapshot stream (%" PRIu64 ")", stream->key);
1118 }
1119
1120 /*
1121 * If tracing is active, we want to perform a "full" buffer flush.
1122 * Else, if quiescent, it has already been done by the prior stop.
1123 */
1124 if (!stream->quiescent) {
1125 ret = lttng_ust_ctl_flush_buffer(stream->ustream, 0);
1126 if (ret < 0) {
1127 ERR("Failed to flush buffer during snapshot of channel: channel key = %" PRIu64
1128 ", channel name = '%s'",
1129 channel->key,
1130 channel->name);
1131 return ret;
1132 }
1133 }
1134
1135 ret = lttng_ustconsumer_take_snapshot(stream);
1136 if (ret < 0) {
1137 ERR("Taking UST snapshot");
1138 return ret;
1139 }
1140
1141 ret = lttng_ustconsumer_get_produced_snapshot(stream, &produced_pos);
1142 if (ret < 0) {
1143 ERR("Produced UST snapshot position");
1144 return ret;
1145 }
1146
1147 ret = lttng_ustconsumer_get_consumed_snapshot(stream, &consumed_pos);
1148 if (ret < 0) {
1149 ERR("Consumerd UST snapshot position");
1150 return ret;
1151 }
1152
1153 /*
1154 * The original value is sent back if max stream size is larger than
1155 * the possible size of the snapshot. Also, we assume that the session
1156 * daemon should never send a maximum stream size that is lower than
1157 * subbuffer size.
1158 */
1159 consumed_pos = consumer_get_consume_start_pos(
1160 consumed_pos, produced_pos, nb_packets_per_stream, stream->max_sb_size);
1161
1162 while ((long) (consumed_pos - produced_pos) < 0) {
1163 ssize_t read_len;
1164 unsigned long len, padded_len;
1165 const char *subbuf_addr;
1166 struct lttng_buffer_view subbuf_view;
1167
1168 health_code_update();
1169
1170 DBG("UST consumer taking snapshot at pos %lu", consumed_pos);
1171
1172 ret = lttng_ust_ctl_get_subbuf(stream->ustream, &consumed_pos);
1173 if (ret < 0) {
1174 if (ret != -EAGAIN) {
1175 PERROR("lttng_ust_ctl_get_subbuf snapshot");
1176 return ret;
1177 }
1178
1179 DBG("UST consumer get subbuf failed. Skipping it.");
1180 consumed_pos += stream->max_sb_size;
1181 stream->chan->lost_packets++;
1182 continue;
1183 }
1184
1185 /* Put the subbuffer once we are done. */
1186 const auto put_subbuf = lttng::make_scope_exit([stream]() noexcept {
1187 if (lttng_ust_ctl_put_subbuf(stream->ustream) < 0) {
1188 ERR("Snapshot lttng_ust_ctl_put_subbuf");
1189 }
1190 });
1191
1192 ret = lttng_ust_ctl_get_subbuf_size(stream->ustream, &len);
1193 if (ret < 0) {
1194 ERR("Snapshot lttng_ust_ctl_get_subbuf_size");
1195 return ret;
1196 }
1197
1198 ret = lttng_ust_ctl_get_padded_subbuf_size(stream->ustream, &padded_len);
1199 if (ret < 0) {
1200 ERR("Snapshot lttng_ust_ctl_get_padded_subbuf_size");
1201 return ret;
1202 }
1203
1204 ret = get_current_subbuf_addr(stream, &subbuf_addr);
1205 if (ret) {
1206 return ret;
1207 }
1208
1209 subbuf_view = lttng_buffer_view_init(subbuf_addr, 0, padded_len);
1210 read_len = lttng_consumer_on_read_subbuffer_mmap(
1211 stream, &subbuf_view, padded_len - len);
1212 if (use_relayd) {
1213 if (read_len != len) {
1214 return -EPERM;
1215 }
1216 } else {
1217 if (read_len != padded_len) {
1218 return -EPERM;
1219 }
1220 }
1221
1222 consumed_pos += stream->max_sb_size;
1223 }
1224
1225 /* Simply close the stream so we can use it on the next snapshot. */
1226 consumer_stream_close_output(stream);
1227 }
1228
1229 return 0;
1230}
1231
1232static void metadata_stream_reset_cache_consumed_position(struct lttng_consumer_stream *stream)
1233{
1234 ASSERT_LOCKED(stream->lock);
1235
1236 DBG("Reset metadata cache of session %" PRIu64, stream->chan->session_id);
1237 stream->ust_metadata_pushed = 0;
1238}
1239
1240/*
1241 * Receive the metadata updates from the sessiond. Supports receiving
1242 * overlapping metadata, but is needs to always belong to a contiguous
1243 * range starting from 0.
1244 * Be careful about the locks held when calling this function: it needs
1245 * the metadata cache flush to concurrently progress in order to
1246 * complete.
1247 */
1248int lttng_ustconsumer_recv_metadata(int sock,
1249 uint64_t key,
1250 uint64_t offset,
1251 uint64_t len,
1252 uint64_t version,
1253 struct lttng_consumer_channel *channel,
1254 bool invoked_by_timer,
1255 int wait)
1256{
1257 int ret, ret_code = LTTCOMM_CONSUMERD_SUCCESS;
1258 char *metadata_str;
1259 enum consumer_metadata_cache_write_status cache_write_status;
1260
1261 DBG("UST consumer push metadata key %" PRIu64 " of len %" PRIu64, key, len);
1262
1263 metadata_str = calloc<char>(len);
1264 if (!metadata_str) {
1265 PERROR("zmalloc metadata string");
1266 ret_code = LTTCOMM_CONSUMERD_ENOMEM;
1267 goto end;
1268 }
1269
1270 health_code_update();
1271
1272 /* Receive metadata string. */
1273 ret = lttcomm_recv_unix_sock(sock, metadata_str, len);
1274 if (ret < 0) {
1275 /* Session daemon is dead so return gracefully. */
1276 ret_code = ret;
1277 goto end_free;
1278 }
1279
1280 health_code_update();
1281
1282 pthread_mutex_lock(&channel->metadata_cache->lock);
1283 cache_write_status = consumer_metadata_cache_write(
1284 channel->metadata_cache, offset, len, version, metadata_str);
1285 pthread_mutex_unlock(&channel->metadata_cache->lock);
1286 switch (cache_write_status) {
1287 case CONSUMER_METADATA_CACHE_WRITE_STATUS_NO_CHANGE:
1288 /*
1289 * The write entirely overlapped with existing contents of the
1290 * same metadata version (same content); there is nothing to do.
1291 */
1292 break;
1293 case CONSUMER_METADATA_CACHE_WRITE_STATUS_INVALIDATED:
1294 /*
1295 * The metadata cache was invalidated (previously pushed
1296 * content has been overwritten). Reset the stream's consumed
1297 * metadata position to ensure the metadata poll thread consumes
1298 * the whole cache.
1299 */
1300
1301 /*
1302 * channel::metadata_stream can be null when the metadata
1303 * channel is under a snapshot session type. No need to update
1304 * the stream position in that scenario.
1305 */
1306 if (channel->metadata_stream != nullptr) {
1307 pthread_mutex_lock(&channel->metadata_stream->lock);
1308 metadata_stream_reset_cache_consumed_position(channel->metadata_stream);
1309 pthread_mutex_unlock(&channel->metadata_stream->lock);
1310 } else {
1311 /* Validate we are in snapshot mode. */
1312 LTTNG_ASSERT(!channel->monitor);
1313 }
1314 /* Fall-through. */
1315 case CONSUMER_METADATA_CACHE_WRITE_STATUS_APPENDED_CONTENT:
1316 /*
1317 * In both cases, the metadata poll thread has new data to
1318 * consume.
1319 */
1320 ret = consumer_metadata_wakeup_pipe(channel);
1321 if (ret) {
1322 ret_code = LTTCOMM_CONSUMERD_ERROR_METADATA;
1323 goto end_free;
1324 }
1325 break;
1326 case CONSUMER_METADATA_CACHE_WRITE_STATUS_ERROR:
1327 /* Unable to handle metadata. Notify session daemon. */
1328 ret_code = LTTCOMM_CONSUMERD_ERROR_METADATA;
1329 /*
1330 * Skip metadata flush on write error since the offset and len might
1331 * not have been updated which could create an infinite loop below when
1332 * waiting for the metadata cache to be flushed.
1333 */
1334 goto end_free;
1335 default:
1336 abort();
1337 }
1338
1339 if (!wait) {
1340 goto end_free;
1341 }
1342
1343 consumer_wait_metadata_cache_flushed(channel, offset + len, invoked_by_timer);
1344
1345end_free:
1346 free(metadata_str);
1347end:
1348 return ret_code;
1349}
1350
1351/*
1352 * Receive command from session daemon and process it.
1353 *
1354 * Return 1 on success else a negative value or 0.
1355 */
1356int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
1357 int sock,
1358 struct pollfd *consumer_sockpoll)
1359{
1360 int ret_func;
1361 enum lttcomm_return_code ret_code = LTTCOMM_CONSUMERD_SUCCESS;
1362 struct lttcomm_consumer_msg msg;
1363 struct lttng_consumer_channel *channel = nullptr;
1364
1365 health_code_update();
1366
1367 {
1368 ssize_t ret_recv;
1369
1370 ret_recv = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg));
1371 if (ret_recv != sizeof(msg)) {
1372 DBG("Consumer received unexpected message size %zd (expects %zu)",
1373 ret_recv,
1374 sizeof(msg));
1375 /*
1376 * The ret value might 0 meaning an orderly shutdown but this is ok
1377 * since the caller handles this.
1378 */
1379 if (ret_recv > 0) {
1380 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_CMD);
1381 ret_recv = -1;
1382 }
1383 return ret_recv;
1384 }
1385 }
1386
1387 health_code_update();
1388
1389 /* deprecated */
1390 LTTNG_ASSERT(msg.cmd_type != LTTNG_CONSUMER_STOP);
1391
1392 health_code_update();
1393
1394 /* relayd needs RCU read-side lock */
1395 const lttng::urcu::read_lock_guard read_lock;
1396
1397 switch (msg.cmd_type) {
1398 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET:
1399 {
1400 const uint32_t major = msg.u.relayd_sock.major;
1401 const uint32_t minor = msg.u.relayd_sock.minor;
1402 const lttcomm_sock_proto protocol =
1403 (enum lttcomm_sock_proto) msg.u.relayd_sock.relayd_socket_protocol;
1404
1405 /* Session daemon status message are handled in the following call. */
1406 consumer_add_relayd_socket(msg.u.relayd_sock.net_index,
1407 msg.u.relayd_sock.type,
1408 ctx,
1409 sock,
1410 consumer_sockpoll,
1411 msg.u.relayd_sock.session_id,
1412 msg.u.relayd_sock.relayd_session_id,
1413 major,
1414 minor,
1415 protocol);
1416 goto end_nosignal;
1417 }
1418 case LTTNG_CONSUMER_DESTROY_RELAYD:
1419 {
1420 const uint64_t index = msg.u.destroy_relayd.net_seq_idx;
1421 struct consumer_relayd_sock_pair *relayd;
1422
1423 DBG("UST consumer destroying relayd %" PRIu64, index);
1424
1425 /* Get relayd reference if exists. */
1426 relayd = consumer_find_relayd(index);
1427 if (relayd == nullptr) {
1428 DBG("Unable to find relayd %" PRIu64, index);
1429 ret_code = LTTCOMM_CONSUMERD_RELAYD_FAIL;
1430 }
1431
1432 /*
1433 * Each relayd socket pair has a refcount of stream attached to it
1434 * which tells if the relayd is still active or not depending on the
1435 * refcount value.
1436 *
1437 * This will set the destroy flag of the relayd object and destroy it
1438 * if the refcount reaches zero when called.
1439 *
1440 * The destroy can happen either here or when a stream fd hangs up.
1441 */
1442 if (relayd) {
1443 consumer_flag_relayd_for_destroy(relayd);
1444 }
1445
1446 goto end_msg_sessiond;
1447 }
1448 case LTTNG_CONSUMER_UPDATE_STREAM:
1449 {
1450 return -ENOSYS;
1451 }
1452 case LTTNG_CONSUMER_DATA_PENDING:
1453 {
1454 int is_data_pending;
1455 ssize_t ret_send;
1456 const uint64_t id = msg.u.data_pending.session_id;
1457
1458 DBG("UST consumer data pending command for id %" PRIu64, id);
1459
1460 is_data_pending = consumer_data_pending(id);
1461
1462 /* Send back returned value to session daemon */
1463 ret_send = lttcomm_send_unix_sock(sock, &is_data_pending, sizeof(is_data_pending));
1464 if (ret_send < 0) {
1465 DBG("Error when sending the data pending ret code: %zd", ret_send);
1466 goto error_fatal;
1467 }
1468
1469 /*
1470 * No need to send back a status message since the data pending
1471 * returned value is the response.
1472 */
1473 break;
1474 }
1475 case LTTNG_CONSUMER_ASK_CHANNEL_CREATION:
1476 {
1477 int ret_ask_channel, ret_add_channel, ret_send;
1478 struct lttng_ust_ctl_consumer_channel_attr attr;
1479 const uint64_t chunk_id = msg.u.ask_channel.chunk_id.value;
1480 const struct lttng_credentials buffer_credentials = {
1481 .uid = LTTNG_OPTIONAL_INIT_VALUE(msg.u.ask_channel.buffer_credentials.uid),
1482 .gid = LTTNG_OPTIONAL_INIT_VALUE(msg.u.ask_channel.buffer_credentials.gid),
1483 };
1484
1485 /* Create a plain object and reserve a channel key. */
1486 channel = consumer_allocate_channel(
1487 msg.u.ask_channel.key,
1488 msg.u.ask_channel.session_id,
1489 msg.u.ask_channel.chunk_id.is_set ? &chunk_id : nullptr,
1490 msg.u.ask_channel.pathname,
1491 msg.u.ask_channel.name,
1492 msg.u.ask_channel.relayd_id,
1493 (enum lttng_event_output) msg.u.ask_channel.output,
1494 msg.u.ask_channel.tracefile_size,
1495 msg.u.ask_channel.tracefile_count,
1496 msg.u.ask_channel.session_id_per_pid,
1497 msg.u.ask_channel.monitor,
1498 msg.u.ask_channel.live_timer_interval,
1499 msg.u.ask_channel.is_live,
1500 msg.u.ask_channel.root_shm_path,
1501 msg.u.ask_channel.shm_path);
1502 if (!channel) {
1503 goto end_channel_error;
1504 }
1505
1506 LTTNG_OPTIONAL_SET(&channel->buffer_credentials, buffer_credentials);
1507
1508 /*
1509 * Assign UST application UID to the channel. This value is ignored for
1510 * per PID buffers. This is specific to UST thus setting this after the
1511 * allocation.
1512 */
1513 channel->ust_app_uid = msg.u.ask_channel.ust_app_uid;
1514
1515 /* Build channel attributes from received message. */
1516 attr.subbuf_size = msg.u.ask_channel.subbuf_size;
1517 attr.num_subbuf = msg.u.ask_channel.num_subbuf;
1518 attr.overwrite = msg.u.ask_channel.overwrite;
1519 attr.switch_timer_interval = msg.u.ask_channel.switch_timer_interval;
1520 attr.read_timer_interval = msg.u.ask_channel.read_timer_interval;
1521 attr.chan_id = msg.u.ask_channel.chan_id;
1522 memcpy(attr.uuid, msg.u.ask_channel.uuid, sizeof(attr.uuid));
1523 attr.blocking_timeout = msg.u.ask_channel.blocking_timeout;
1524
1525 /* Match channel buffer type to the UST abi. */
1526 switch (msg.u.ask_channel.output) {
1527 case LTTNG_EVENT_MMAP:
1528 default:
1529 attr.output = LTTNG_UST_ABI_MMAP;
1530 break;
1531 }
1532
1533 /* Translate and save channel type. */
1534 switch (msg.u.ask_channel.type) {
1535 case LTTNG_UST_ABI_CHAN_PER_CPU:
1536 channel->type = CONSUMER_CHANNEL_TYPE_DATA;
1537 attr.type = LTTNG_UST_ABI_CHAN_PER_CPU;
1538 /*
1539 * Set refcount to 1 for owner. Below, we will
1540 * pass ownership to the
1541 * consumer_thread_channel_poll() thread.
1542 */
1543 channel->refcount = 1;
1544 break;
1545 case LTTNG_UST_ABI_CHAN_METADATA:
1546 channel->type = CONSUMER_CHANNEL_TYPE_METADATA;
1547 attr.type = LTTNG_UST_ABI_CHAN_METADATA;
1548 break;
1549 default:
1550 abort();
1551 goto error_fatal;
1552 };
1553
1554 health_code_update();
1555
1556 ret_ask_channel = ask_channel(ctx, channel, &attr);
1557 if (ret_ask_channel < 0) {
1558 goto end_channel_error;
1559 }
1560
1561 if (msg.u.ask_channel.type == LTTNG_UST_ABI_CHAN_METADATA) {
1562 int ret_allocate;
1563
1564 ret_allocate = consumer_metadata_cache_allocate(channel);
1565 if (ret_allocate < 0) {
1566 ERR("Allocating metadata cache");
1567 goto end_channel_error;
1568 }
1569 consumer_timer_switch_start(channel, attr.switch_timer_interval);
1570 attr.switch_timer_interval = 0;
1571 } else {
1572 int monitor_start_ret;
1573
1574 consumer_timer_live_start(channel, msg.u.ask_channel.live_timer_interval);
1575 monitor_start_ret = consumer_timer_monitor_start(
1576 channel, msg.u.ask_channel.monitor_timer_interval);
1577 if (monitor_start_ret < 0) {
1578 ERR("Starting channel monitoring timer failed");
1579 goto end_channel_error;
1580 }
1581 }
1582
1583 health_code_update();
1584
1585 /*
1586 * Add the channel to the internal state AFTER all streams were created
1587 * and successfully sent to session daemon. This way, all streams must
1588 * be ready before this channel is visible to the threads.
1589 * If add_channel succeeds, ownership of the channel is
1590 * passed to consumer_thread_channel_poll().
1591 */
1592 ret_add_channel = add_channel(channel, ctx);
1593 if (ret_add_channel < 0) {
1594 if (msg.u.ask_channel.type == LTTNG_UST_ABI_CHAN_METADATA) {
1595 if (channel->switch_timer_enabled == 1) {
1596 consumer_timer_switch_stop(channel);
1597 }
1598 consumer_metadata_cache_destroy(channel);
1599 }
1600 if (channel->live_timer_enabled == 1) {
1601 consumer_timer_live_stop(channel);
1602 }
1603 if (channel->monitor_timer_enabled == 1) {
1604 consumer_timer_monitor_stop(channel);
1605 }
1606 goto end_channel_error;
1607 }
1608
1609 health_code_update();
1610
1611 /*
1612 * Channel and streams are now created. Inform the session daemon that
1613 * everything went well and should wait to receive the channel and
1614 * streams with ustctl API.
1615 */
1616 ret_send = consumer_send_status_channel(sock, channel);
1617 if (ret_send < 0) {
1618 /*
1619 * There is probably a problem on the socket.
1620 */
1621 goto error_fatal;
1622 }
1623
1624 break;
1625 }
1626 case LTTNG_CONSUMER_GET_CHANNEL:
1627 {
1628 int ret, relayd_err = 0;
1629 const uint64_t key = msg.u.get_channel.key;
1630 struct lttng_consumer_channel *found_channel;
1631
1632 found_channel = consumer_find_channel(key);
1633 if (!found_channel) {
1634 ERR("UST consumer get channel key %" PRIu64 " not found", key);
1635 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
1636 goto end_get_channel;
1637 }
1638
1639 health_code_update();
1640
1641 /* Send the channel to sessiond (and relayd, if applicable). */
1642 ret = send_channel_to_sessiond_and_relayd(sock, found_channel, ctx, &relayd_err);
1643 if (ret < 0) {
1644 if (relayd_err) {
1645 /*
1646 * We were unable to send to the relayd the stream so avoid
1647 * sending back a fatal error to the thread since this is OK
1648 * and the consumer can continue its work. The above call
1649 * has sent the error status message to the sessiond.
1650 */
1651 goto end_get_channel_nosignal;
1652 }
1653 /*
1654 * The communicaton was broken hence there is a bad state between
1655 * the consumer and sessiond so stop everything.
1656 */
1657 goto error_get_channel_fatal;
1658 }
1659
1660 health_code_update();
1661
1662 /*
1663 * In no monitor mode, the streams ownership is kept inside the channel
1664 * so don't send them to the data thread.
1665 */
1666 if (!found_channel->monitor) {
1667 goto end_get_channel;
1668 }
1669
1670 ret = send_streams_to_thread(found_channel, ctx);
1671 if (ret < 0) {
1672 /*
1673 * If we are unable to send the stream to the thread, there is
1674 * a big problem so just stop everything.
1675 */
1676 goto error_get_channel_fatal;
1677 }
1678 /* List MUST be empty after or else it could be reused. */
1679 LTTNG_ASSERT(cds_list_empty(&found_channel->streams.head));
1680 end_get_channel:
1681 goto end_msg_sessiond;
1682 error_get_channel_fatal:
1683 goto error_fatal;
1684 end_get_channel_nosignal:
1685 goto end_nosignal;
1686 }
1687 case LTTNG_CONSUMER_DESTROY_CHANNEL:
1688 {
1689 const uint64_t key = msg.u.destroy_channel.key;
1690
1691 /*
1692 * Only called if streams have not been sent to stream
1693 * manager thread. However, channel has been sent to
1694 * channel manager thread.
1695 */
1696 notify_thread_del_channel(ctx, key);
1697 goto end_msg_sessiond;
1698 }
1699 case LTTNG_CONSUMER_CLOSE_METADATA:
1700 {
1701 int ret;
1702
1703 ret = close_metadata(msg.u.close_metadata.key);
1704 if (ret != 0) {
1705 ret_code = (lttcomm_return_code) ret;
1706 }
1707
1708 goto end_msg_sessiond;
1709 }
1710 case LTTNG_CONSUMER_FLUSH_CHANNEL:
1711 {
1712 int ret;
1713
1714 ret = flush_channel(msg.u.flush_channel.key);
1715 if (ret != 0) {
1716 ret_code = (lttcomm_return_code) ret;
1717 }
1718
1719 goto end_msg_sessiond;
1720 }
1721 case LTTNG_CONSUMER_CLEAR_QUIESCENT_CHANNEL:
1722 {
1723 int ret;
1724
1725 ret = clear_quiescent_channel(msg.u.clear_quiescent_channel.key);
1726 if (ret != 0) {
1727 ret_code = (lttcomm_return_code) ret;
1728 }
1729
1730 goto end_msg_sessiond;
1731 }
1732 case LTTNG_CONSUMER_PUSH_METADATA:
1733 {
1734 int ret;
1735 const uint64_t len = msg.u.push_metadata.len;
1736 const uint64_t key = msg.u.push_metadata.key;
1737 const uint64_t offset = msg.u.push_metadata.target_offset;
1738 const uint64_t version = msg.u.push_metadata.version;
1739 struct lttng_consumer_channel *found_channel;
1740
1741 DBG("UST consumer push metadata key %" PRIu64 " of len %" PRIu64, key, len);
1742
1743 found_channel = consumer_find_channel(key);
1744 if (!found_channel) {
1745 /*
1746 * This is possible if the metadata creation on the consumer side
1747 * is in flight vis-a-vis a concurrent push metadata from the
1748 * session daemon. Simply return that the channel failed and the
1749 * session daemon will handle that message correctly considering
1750 * that this race is acceptable thus the DBG() statement here.
1751 */
1752 DBG("UST consumer push metadata %" PRIu64 " not found", key);
1753 ret_code = LTTCOMM_CONSUMERD_CHANNEL_FAIL;
1754 goto end_push_metadata_msg_sessiond;
1755 }
1756
1757 health_code_update();
1758
1759 if (!len) {
1760 /*
1761 * There is nothing to receive. We have simply
1762 * checked whether the channel can be found.
1763 */
1764 ret_code = LTTCOMM_CONSUMERD_SUCCESS;
1765 goto end_push_metadata_msg_sessiond;
1766 }
1767
1768 /* Tell session daemon we are ready to receive the metadata. */
1769 ret = consumer_send_status_msg(sock, LTTCOMM_CONSUMERD_SUCCESS);
1770 if (ret < 0) {
1771 /* Somehow, the session daemon is not responding anymore. */
1772 goto error_push_metadata_fatal;
1773 }
1774
1775 health_code_update();
1776
1777 /* Wait for more data. */
1778 health_poll_entry();
1779 ret = lttng_consumer_poll_socket(consumer_sockpoll);
1780 health_poll_exit();
1781 if (ret) {
1782 goto error_push_metadata_fatal;
1783 }
1784
1785 health_code_update();
1786
1787 ret = lttng_ustconsumer_recv_metadata(
1788 sock, key, offset, len, version, found_channel, false, 1);
1789 if (ret < 0) {
1790 /* error receiving from sessiond */
1791 goto error_push_metadata_fatal;
1792 } else {
1793 ret_code = (lttcomm_return_code) ret;
1794 goto end_push_metadata_msg_sessiond;
1795 }
1796 end_push_metadata_msg_sessiond:
1797 goto end_msg_sessiond;
1798 error_push_metadata_fatal:
1799 goto error_fatal;
1800 }
1801 case LTTNG_CONSUMER_SETUP_METADATA:
1802 {
1803 int ret;
1804
1805 ret = setup_metadata(ctx, msg.u.setup_metadata.key);
1806 if (ret) {
1807 ret_code = (lttcomm_return_code) ret;
1808 }
1809 goto end_msg_sessiond;
1810 }
1811 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL:
1812 {
1813 struct lttng_consumer_channel *found_channel;
1814 const uint64_t key = msg.u.snapshot_channel.key;
1815 int ret_send;
1816
1817 found_channel = consumer_find_channel(key);
1818 if (!found_channel) {
1819 DBG("UST snapshot channel not found for key %" PRIu64, key);
1820 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
1821 } else {
1822 if (msg.u.snapshot_channel.metadata) {
1823 int ret_snapshot;
1824
1825 ret_snapshot = snapshot_metadata(found_channel,
1826 key,
1827 msg.u.snapshot_channel.pathname,
1828 msg.u.snapshot_channel.relayd_id,
1829 ctx);
1830 if (ret_snapshot < 0) {
1831 ERR("Snapshot metadata failed");
1832 ret_code = LTTCOMM_CONSUMERD_SNAPSHOT_FAILED;
1833 }
1834 } else {
1835 int ret_snapshot;
1836
1837 ret_snapshot = snapshot_channel(
1838 found_channel,
1839 key,
1840 msg.u.snapshot_channel.pathname,
1841 msg.u.snapshot_channel.relayd_id,
1842 msg.u.snapshot_channel.nb_packets_per_stream,
1843 ctx);
1844 if (ret_snapshot < 0) {
1845 ERR("Snapshot channel failed");
1846 ret_code = LTTCOMM_CONSUMERD_SNAPSHOT_FAILED;
1847 }
1848 }
1849 }
1850 health_code_update();
1851 ret_send = consumer_send_status_msg(sock, ret_code);
1852 if (ret_send < 0) {
1853 /* Somehow, the session daemon is not responding anymore. */
1854 goto end_nosignal;
1855 }
1856 health_code_update();
1857 break;
1858 }
1859 case LTTNG_CONSUMER_DISCARDED_EVENTS:
1860 {
1861 int ret = 0;
1862 uint64_t discarded_events;
1863 const auto id = msg.u.discarded_events.session_id;
1864 const auto key = msg.u.discarded_events.channel_key;
1865
1866 DBG("UST consumer discarded events command for session id %" PRIu64, id);
1867 {
1868 const lttng::pthread::lock_guard consumer_data_lock(the_consumer_data.lock);
1869 const auto ht = the_consumer_data.stream_list_ht;
1870
1871 /*
1872 * We only need a reference to the channel, but they are not
1873 * directly indexed, so we just use the first matching stream
1874 * to extract the information we need, we default to 0 if not
1875 * found (no events are dropped if the channel is not yet in
1876 * use).
1877 */
1878 discarded_events = 0;
1879 for (auto *stream : lttng::urcu::lfht_filtered_iteration_adapter<
1880 lttng_consumer_stream,
1881 decltype(lttng_consumer_stream::node_channel_id),
1882 &lttng_consumer_stream::node_session_id,
1883 std::uint64_t>(*ht->ht,
1884 &id,
1885 ht->hash_fct(&id, lttng_ht_seed),
1886 ht->match_fct)) {
1887 if (stream->chan->key == key) {
1888 discarded_events = stream->chan->discarded_events;
1889 break;
1890 }
1891 }
1892 }
1893
1894 DBG("UST consumer discarded events command for session id %" PRIu64
1895 ", channel key %" PRIu64,
1896 id,
1897 key);
1898
1899 health_code_update();
1900
1901 /* Send back returned value to session daemon */
1902 ret = lttcomm_send_unix_sock(sock, &discarded_events, sizeof(discarded_events));
1903 if (ret < 0) {
1904 PERROR("send discarded events");
1905 goto error_fatal;
1906 }
1907
1908 break;
1909 }
1910 case LTTNG_CONSUMER_LOST_PACKETS:
1911 {
1912 int ret;
1913 uint64_t lost_packets;
1914 const auto id = msg.u.lost_packets.session_id;
1915 const auto key = msg.u.lost_packets.channel_key;
1916
1917 DBG("UST consumer lost packets command for session id %" PRIu64, id);
1918 {
1919 const lttng::pthread::lock_guard consumer_data_lock(the_consumer_data.lock);
1920 const auto ht = the_consumer_data.stream_list_ht;
1921
1922 /*
1923 * We only need a reference to the channel, but they are not
1924 * directly indexed, so we just use the first matching stream
1925 * to extract the information we need, we default to 0 if not
1926 * found (no packets lost if the channel is not yet in use).
1927 */
1928 lost_packets = 0;
1929 for (auto *stream : lttng::urcu::lfht_filtered_iteration_adapter<
1930 lttng_consumer_stream,
1931 decltype(lttng_consumer_stream::node_session_id),
1932 &lttng_consumer_stream::node_session_id,
1933 std::uint64_t>(*ht->ht,
1934 &id,
1935 ht->hash_fct(&id, lttng_ht_seed),
1936 ht->match_fct)) {
1937 if (stream->chan->key == key) {
1938 lost_packets = stream->chan->lost_packets;
1939 break;
1940 }
1941 }
1942 }
1943
1944 DBG("UST consumer lost packets command for session id %" PRIu64
1945 ", channel key %" PRIu64,
1946 id,
1947 key);
1948
1949 health_code_update();
1950
1951 /* Send back returned value to session daemon */
1952 ret = lttcomm_send_unix_sock(sock, &lost_packets, sizeof(lost_packets));
1953 if (ret < 0) {
1954 PERROR("send lost packets");
1955 goto error_fatal;
1956 }
1957
1958 break;
1959 }
1960 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE:
1961 {
1962 int channel_monitor_pipe, ret_send, ret_set_channel_monitor_pipe;
1963 ssize_t ret_recv;
1964
1965 ret_code = LTTCOMM_CONSUMERD_SUCCESS;
1966 /* Successfully received the command's type. */
1967 ret_send = consumer_send_status_msg(sock, ret_code);
1968 if (ret_send < 0) {
1969 goto error_fatal;
1970 }
1971
1972 ret_recv = lttcomm_recv_fds_unix_sock(sock, &channel_monitor_pipe, 1);
1973 if (ret_recv != sizeof(channel_monitor_pipe)) {
1974 ERR("Failed to receive channel monitor pipe");
1975 goto error_fatal;
1976 }
1977
1978 DBG("Received channel monitor pipe (%d)", channel_monitor_pipe);
1979 ret_set_channel_monitor_pipe =
1980 consumer_timer_thread_set_channel_monitor_pipe(channel_monitor_pipe);
1981 if (!ret_set_channel_monitor_pipe) {
1982 int flags;
1983 int ret_fcntl;
1984
1985 ret_code = LTTCOMM_CONSUMERD_SUCCESS;
1986 /* Set the pipe as non-blocking. */
1987 ret_fcntl = fcntl(channel_monitor_pipe, F_GETFL, 0);
1988 if (ret_fcntl == -1) {
1989 PERROR("fcntl get flags of the channel monitoring pipe");
1990 goto error_fatal;
1991 }
1992 flags = ret_fcntl;
1993
1994 ret_fcntl = fcntl(channel_monitor_pipe, F_SETFL, flags | O_NONBLOCK);
1995 if (ret_fcntl == -1) {
1996 PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe");
1997 goto error_fatal;
1998 }
1999 DBG("Channel monitor pipe set as non-blocking");
2000 } else {
2001 ret_code = LTTCOMM_CONSUMERD_ALREADY_SET;
2002 }
2003 goto end_msg_sessiond;
2004 }
2005 case LTTNG_CONSUMER_ROTATE_CHANNEL:
2006 {
2007 struct lttng_consumer_channel *found_channel;
2008 const uint64_t key = msg.u.rotate_channel.key;
2009 int ret_send_status;
2010
2011 found_channel = consumer_find_channel(key);
2012 if (!found_channel) {
2013 DBG("Channel %" PRIu64 " not found", key);
2014 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
2015 } else {
2016 int rotate_channel;
2017
2018 /*
2019 * Sample the rotate position of all the streams in
2020 * this channel.
2021 */
2022 rotate_channel = lttng_consumer_rotate_channel(
2023 found_channel, key, msg.u.rotate_channel.relayd_id);
2024 if (rotate_channel < 0) {
2025 ERR("Rotate channel failed");
2026 ret_code = LTTCOMM_CONSUMERD_ROTATION_FAIL;
2027 }
2028
2029 health_code_update();
2030 }
2031
2032 ret_send_status = consumer_send_status_msg(sock, ret_code);
2033 if (ret_send_status < 0) {
2034 /* Somehow, the session daemon is not responding anymore. */
2035 goto end_rotate_channel_nosignal;
2036 }
2037
2038 /*
2039 * Rotate the streams that are ready right now.
2040 * FIXME: this is a second consecutive iteration over the
2041 * streams in a channel, there is probably a better way to
2042 * handle this, but it needs to be after the
2043 * consumer_send_status_msg() call.
2044 */
2045 if (found_channel) {
2046 int ret_rotate_read_streams;
2047
2048 ret_rotate_read_streams =
2049 lttng_consumer_rotate_ready_streams(found_channel, key);
2050 if (ret_rotate_read_streams < 0) {
2051 ERR("Rotate channel failed");
2052 }
2053 }
2054 break;
2055 end_rotate_channel_nosignal:
2056 goto end_nosignal;
2057 }
2058 case LTTNG_CONSUMER_CLEAR_CHANNEL:
2059 {
2060 struct lttng_consumer_channel *found_channel;
2061 const uint64_t key = msg.u.clear_channel.key;
2062 int ret_send_status;
2063
2064 found_channel = consumer_find_channel(key);
2065 if (!found_channel) {
2066 DBG("Channel %" PRIu64 " not found", key);
2067 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
2068 } else {
2069 int ret_clear_channel;
2070
2071 ret_clear_channel = lttng_consumer_clear_channel(found_channel);
2072 if (ret_clear_channel) {
2073 ERR("Clear channel failed key %" PRIu64, key);
2074 ret_code = (lttcomm_return_code) ret_clear_channel;
2075 }
2076
2077 health_code_update();
2078 }
2079 ret_send_status = consumer_send_status_msg(sock, ret_code);
2080 if (ret_send_status < 0) {
2081 /* Somehow, the session daemon is not responding anymore. */
2082 goto end_nosignal;
2083 }
2084 break;
2085 }
2086 case LTTNG_CONSUMER_INIT:
2087 {
2088 int ret_send_status;
2089 lttng_uuid sessiond_uuid;
2090
2091 std::copy(std::begin(msg.u.init.sessiond_uuid),
2092 std::end(msg.u.init.sessiond_uuid),
2093 sessiond_uuid.begin());
2094 ret_code = lttng_consumer_init_command(ctx, sessiond_uuid);
2095 health_code_update();
2096 ret_send_status = consumer_send_status_msg(sock, ret_code);
2097 if (ret_send_status < 0) {
2098 /* Somehow, the session daemon is not responding anymore. */
2099 goto end_nosignal;
2100 }
2101 break;
2102 }
2103 case LTTNG_CONSUMER_CREATE_TRACE_CHUNK:
2104 {
2105 const struct lttng_credentials credentials = {
2106 .uid = LTTNG_OPTIONAL_INIT_VALUE(
2107 msg.u.create_trace_chunk.credentials.value.uid),
2108 .gid = LTTNG_OPTIONAL_INIT_VALUE(
2109 msg.u.create_trace_chunk.credentials.value.gid),
2110 };
2111 const bool is_local_trace = !msg.u.create_trace_chunk.relayd_id.is_set;
2112 const uint64_t relayd_id = msg.u.create_trace_chunk.relayd_id.value;
2113 const char *chunk_override_name = *msg.u.create_trace_chunk.override_name ?
2114 msg.u.create_trace_chunk.override_name :
2115 nullptr;
2116 struct lttng_directory_handle *chunk_directory_handle = nullptr;
2117
2118 /*
2119 * The session daemon will only provide a chunk directory file
2120 * descriptor for local traces.
2121 */
2122 if (is_local_trace) {
2123 int chunk_dirfd;
2124 int ret_send_status;
2125 ssize_t ret_recv;
2126
2127 /* Acnowledge the reception of the command. */
2128 ret_send_status = consumer_send_status_msg(sock, LTTCOMM_CONSUMERD_SUCCESS);
2129 if (ret_send_status < 0) {
2130 /* Somehow, the session daemon is not responding anymore. */
2131 goto end_nosignal;
2132 }
2133
2134 /*
2135 * Receive trace chunk domain dirfd.
2136 */
2137 ret_recv = lttcomm_recv_fds_unix_sock(sock, &chunk_dirfd, 1);
2138 if (ret_recv != sizeof(chunk_dirfd)) {
2139 ERR("Failed to receive trace chunk domain directory file descriptor");
2140 goto error_fatal;
2141 }
2142
2143 DBG("Received trace chunk domain directory fd (%d)", chunk_dirfd);
2144 chunk_directory_handle =
2145 lttng_directory_handle_create_from_dirfd(chunk_dirfd);
2146 if (!chunk_directory_handle) {
2147 ERR("Failed to initialize chunk domain directory handle from directory file descriptor");
2148 if (close(chunk_dirfd)) {
2149 PERROR("Failed to close chunk directory file descriptor");
2150 }
2151 goto error_fatal;
2152 }
2153 }
2154
2155 ret_code = lttng_consumer_create_trace_chunk(
2156 !is_local_trace ? &relayd_id : nullptr,
2157 msg.u.create_trace_chunk.session_id,
2158 msg.u.create_trace_chunk.chunk_id,
2159 (time_t) msg.u.create_trace_chunk.creation_timestamp,
2160 chunk_override_name,
2161 msg.u.create_trace_chunk.credentials.is_set ? &credentials : nullptr,
2162 chunk_directory_handle);
2163 lttng_directory_handle_put(chunk_directory_handle);
2164 goto end_msg_sessiond;
2165 }
2166 case LTTNG_CONSUMER_CLOSE_TRACE_CHUNK:
2167 {
2168 enum lttng_trace_chunk_command_type close_command =
2169 (lttng_trace_chunk_command_type) msg.u.close_trace_chunk.close_command.value;
2170 const uint64_t relayd_id = msg.u.close_trace_chunk.relayd_id.value;
2171 struct lttcomm_consumer_close_trace_chunk_reply reply;
2172 char closed_trace_chunk_path[LTTNG_PATH_MAX] = {};
2173 int ret;
2174
2175 ret_code = lttng_consumer_close_trace_chunk(
2176 msg.u.close_trace_chunk.relayd_id.is_set ? &relayd_id : nullptr,
2177 msg.u.close_trace_chunk.session_id,
2178 msg.u.close_trace_chunk.chunk_id,
2179 (time_t) msg.u.close_trace_chunk.close_timestamp,
2180 msg.u.close_trace_chunk.close_command.is_set ? &close_command : nullptr,
2181 closed_trace_chunk_path);
2182 reply.ret_code = ret_code;
2183 reply.path_length = strlen(closed_trace_chunk_path) + 1;
2184 ret = lttcomm_send_unix_sock(sock, &reply, sizeof(reply));
2185 if (ret != sizeof(reply)) {
2186 goto error_fatal;
2187 }
2188 ret = lttcomm_send_unix_sock(sock, closed_trace_chunk_path, reply.path_length);
2189 if (ret != reply.path_length) {
2190 goto error_fatal;
2191 }
2192 goto end_nosignal;
2193 }
2194 case LTTNG_CONSUMER_TRACE_CHUNK_EXISTS:
2195 {
2196 const uint64_t relayd_id = msg.u.trace_chunk_exists.relayd_id.value;
2197
2198 ret_code = lttng_consumer_trace_chunk_exists(
2199 msg.u.trace_chunk_exists.relayd_id.is_set ? &relayd_id : nullptr,
2200 msg.u.trace_chunk_exists.session_id,
2201 msg.u.trace_chunk_exists.chunk_id);
2202 goto end_msg_sessiond;
2203 }
2204 case LTTNG_CONSUMER_OPEN_CHANNEL_PACKETS:
2205 {
2206 const uint64_t key = msg.u.open_channel_packets.key;
2207 struct lttng_consumer_channel *found_channel = consumer_find_channel(key);
2208
2209 if (found_channel) {
2210 pthread_mutex_lock(&found_channel->lock);
2211 ret_code = lttng_consumer_open_channel_packets(found_channel);
2212 pthread_mutex_unlock(&found_channel->lock);
2213 } else {
2214 /*
2215 * The channel could have disappeared in per-pid
2216 * buffering mode.
2217 */
2218 DBG("Channel %" PRIu64 " not found", key);
2219 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
2220 }
2221
2222 health_code_update();
2223 goto end_msg_sessiond;
2224 }
2225 default:
2226 break;
2227 }
2228
2229end_nosignal:
2230 /*
2231 * Return 1 to indicate success since the 0 value can be a socket
2232 * shutdown during the recv() or send() call.
2233 */
2234 ret_func = 1;
2235 goto end;
2236
2237end_msg_sessiond:
2238 /*
2239 * The returned value here is not useful since either way we'll return 1 to
2240 * the caller because the session daemon socket management is done
2241 * elsewhere. Returning a negative code or 0 will shutdown the consumer.
2242 */
2243 {
2244 int ret_send_status;
2245
2246 ret_send_status = consumer_send_status_msg(sock, ret_code);
2247 if (ret_send_status < 0) {
2248 goto error_fatal;
2249 }
2250 }
2251
2252 ret_func = 1;
2253 goto end;
2254
2255end_channel_error:
2256 if (channel) {
2257 consumer_del_channel(channel);
2258 }
2259 /* We have to send a status channel message indicating an error. */
2260 {
2261 int ret_send_status;
2262
2263 ret_send_status = consumer_send_status_channel(sock, nullptr);
2264 if (ret_send_status < 0) {
2265 /* Stop everything if session daemon can not be notified. */
2266 goto error_fatal;
2267 }
2268 }
2269
2270 ret_func = 1;
2271 goto end;
2272
2273error_fatal:
2274 /* This will issue a consumer stop. */
2275 ret_func = -1;
2276 goto end;
2277
2278end:
2279 health_code_update();
2280 return ret_func;
2281}
2282
2283int lttng_ust_flush_buffer(struct lttng_consumer_stream *stream, int producer_active)
2284{
2285 LTTNG_ASSERT(stream);
2286 LTTNG_ASSERT(stream->ustream);
2287
2288 return lttng_ust_ctl_flush_buffer(stream->ustream, producer_active);
2289}
2290
2291/*
2292 * Take a snapshot for a specific stream.
2293 *
2294 * Returns 0 on success, < 0 on error
2295 */
2296int lttng_ustconsumer_take_snapshot(struct lttng_consumer_stream *stream)
2297{
2298 LTTNG_ASSERT(stream);
2299 LTTNG_ASSERT(stream->ustream);
2300
2301 return lttng_ust_ctl_snapshot(stream->ustream);
2302}
2303
2304/*
2305 * Sample consumed and produced positions for a specific stream.
2306 *
2307 * Returns 0 on success, < 0 on error.
2308 */
2309int lttng_ustconsumer_sample_snapshot_positions(struct lttng_consumer_stream *stream)
2310{
2311 LTTNG_ASSERT(stream);
2312 LTTNG_ASSERT(stream->ustream);
2313
2314 return lttng_ust_ctl_snapshot_sample_positions(stream->ustream);
2315}
2316
2317/*
2318 * Get the produced position
2319 *
2320 * Returns 0 on success, < 0 on error
2321 */
2322int lttng_ustconsumer_get_produced_snapshot(struct lttng_consumer_stream *stream,
2323 unsigned long *pos)
2324{
2325 LTTNG_ASSERT(stream);
2326 LTTNG_ASSERT(stream->ustream);
2327 LTTNG_ASSERT(pos);
2328
2329 return lttng_ust_ctl_snapshot_get_produced(stream->ustream, pos);
2330}
2331
2332/*
2333 * Get the consumed position
2334 *
2335 * Returns 0 on success, < 0 on error
2336 */
2337int lttng_ustconsumer_get_consumed_snapshot(struct lttng_consumer_stream *stream,
2338 unsigned long *pos)
2339{
2340 LTTNG_ASSERT(stream);
2341 LTTNG_ASSERT(stream->ustream);
2342 LTTNG_ASSERT(pos);
2343
2344 return lttng_ust_ctl_snapshot_get_consumed(stream->ustream, pos);
2345}
2346
2347int lttng_ustconsumer_flush_buffer(struct lttng_consumer_stream *stream, int producer)
2348{
2349 LTTNG_ASSERT(stream);
2350 LTTNG_ASSERT(stream->ustream);
2351
2352 return lttng_ust_ctl_flush_buffer(stream->ustream, producer);
2353}
2354
2355int lttng_ustconsumer_clear_buffer(struct lttng_consumer_stream *stream)
2356{
2357 LTTNG_ASSERT(stream);
2358 LTTNG_ASSERT(stream->ustream);
2359
2360 return lttng_ust_ctl_clear_buffer(stream->ustream);
2361}
2362
2363int lttng_ustconsumer_get_current_timestamp(struct lttng_consumer_stream *stream, uint64_t *ts)
2364{
2365 LTTNG_ASSERT(stream);
2366 LTTNG_ASSERT(stream->ustream);
2367 LTTNG_ASSERT(ts);
2368
2369 return lttng_ust_ctl_get_current_timestamp(stream->ustream, ts);
2370}
2371
2372int lttng_ustconsumer_get_sequence_number(struct lttng_consumer_stream *stream, uint64_t *seq)
2373{
2374 LTTNG_ASSERT(stream);
2375 LTTNG_ASSERT(stream->ustream);
2376 LTTNG_ASSERT(seq);
2377
2378 return lttng_ust_ctl_get_sequence_number(stream->ustream, seq);
2379}
2380
2381/*
2382 * Called when the stream signals the consumer that it has hung up.
2383 */
2384void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream *stream)
2385{
2386 LTTNG_ASSERT(stream);
2387 LTTNG_ASSERT(stream->ustream);
2388
2389 pthread_mutex_lock(&stream->lock);
2390 if (!stream->quiescent) {
2391 if (lttng_ust_ctl_flush_buffer(stream->ustream, 0) < 0) {
2392 ERR("Failed to flush buffer on stream hang-up");
2393 } else {
2394 stream->quiescent = true;
2395 }
2396 }
2397
2398 stream->hangup_flush_done = 1;
2399 pthread_mutex_unlock(&stream->lock);
2400}
2401
2402void lttng_ustconsumer_del_channel(struct lttng_consumer_channel *chan)
2403{
2404 int i;
2405
2406 LTTNG_ASSERT(chan);
2407 LTTNG_ASSERT(chan->uchan);
2408 LTTNG_ASSERT(chan->buffer_credentials.is_set);
2409
2410 if (chan->switch_timer_enabled == 1) {
2411 consumer_timer_switch_stop(chan);
2412 }
2413 for (i = 0; i < chan->nr_stream_fds; i++) {
2414 int ret;
2415
2416 ret = close(chan->stream_fds[i]);
2417 if (ret) {
2418 PERROR("close");
2419 }
2420 if (chan->shm_path[0]) {
2421 char shm_path[PATH_MAX];
2422
2423 ret = get_stream_shm_path(shm_path, chan->shm_path, i);
2424 if (ret) {
2425 ERR("Cannot get stream shm path");
2426 }
2427 ret = run_as_unlink(shm_path,
2428 lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(
2429 chan->buffer_credentials)),
2430 lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(
2431 chan->buffer_credentials)));
2432 if (ret) {
2433 PERROR("unlink %s", shm_path);
2434 }
2435 }
2436 }
2437}
2438
2439void lttng_ustconsumer_free_channel(struct lttng_consumer_channel *chan)
2440{
2441 LTTNG_ASSERT(chan);
2442 LTTNG_ASSERT(chan->uchan);
2443 LTTNG_ASSERT(chan->buffer_credentials.is_set);
2444
2445 consumer_metadata_cache_destroy(chan);
2446 lttng_ust_ctl_destroy_channel(chan->uchan);
2447 /* Try to rmdir all directories under shm_path root. */
2448 if (chan->root_shm_path[0]) {
2449 (void) run_as_rmdir_recursive(
2450 chan->root_shm_path,
2451 lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(chan->buffer_credentials)),
2452 lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(chan->buffer_credentials)),
2453 LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG);
2454 }
2455 free(chan->stream_fds);
2456}
2457
2458void lttng_ustconsumer_del_stream(struct lttng_consumer_stream *stream)
2459{
2460 LTTNG_ASSERT(stream);
2461 LTTNG_ASSERT(stream->ustream);
2462
2463 if (stream->chan->switch_timer_enabled == 1) {
2464 consumer_timer_switch_stop(stream->chan);
2465 }
2466 lttng_ust_ctl_destroy_stream(stream->ustream);
2467}
2468
2469int lttng_ustconsumer_get_wakeup_fd(struct lttng_consumer_stream *stream)
2470{
2471 LTTNG_ASSERT(stream);
2472 LTTNG_ASSERT(stream->ustream);
2473
2474 return lttng_ust_ctl_stream_get_wakeup_fd(stream->ustream);
2475}
2476
2477int lttng_ustconsumer_close_wakeup_fd(struct lttng_consumer_stream *stream)
2478{
2479 LTTNG_ASSERT(stream);
2480 LTTNG_ASSERT(stream->ustream);
2481
2482 return lttng_ust_ctl_stream_close_wakeup_fd(stream->ustream);
2483}
2484
2485/*
2486 * Write up to one packet from the metadata cache to the channel.
2487 *
2488 * Returns the number of bytes pushed from the cache into the ring buffer, or a
2489 * negative value on error.
2490 */
2491static int commit_one_metadata_packet(struct lttng_consumer_stream *stream)
2492{
2493 ssize_t write_len;
2494 int ret;
2495
2496 pthread_mutex_lock(&stream->chan->metadata_cache->lock);
2497 if (stream->chan->metadata_cache->contents.size == stream->ust_metadata_pushed) {
2498 /*
2499 * In the context of a user space metadata channel, a
2500 * change in version can be detected in two ways:
2501 * 1) During the pre-consume of the `read_subbuffer` loop,
2502 * 2) When populating the metadata ring buffer (i.e. here).
2503 *
2504 * This function is invoked when there is no metadata
2505 * available in the ring-buffer. If all data was consumed
2506 * up to the size of the metadata cache, there is no metadata
2507 * to insert in the ring-buffer.
2508 *
2509 * However, the metadata version could still have changed (a
2510 * regeneration without any new data will yield the same cache
2511 * size).
2512 *
2513 * The cache's version is checked for a version change and the
2514 * consumed position is reset if one occurred.
2515 *
2516 * This check is only necessary for the user space domain as
2517 * it has to manage the cache explicitly. If this reset was not
2518 * performed, no metadata would be consumed (and no reset would
2519 * occur as part of the pre-consume) until the metadata size
2520 * exceeded the cache size.
2521 */
2522 if (stream->metadata_version != stream->chan->metadata_cache->version) {
2523 metadata_stream_reset_cache_consumed_position(stream);
2524 consumer_stream_metadata_set_version(stream,
2525 stream->chan->metadata_cache->version);
2526 } else {
2527 ret = 0;
2528 goto end;
2529 }
2530 }
2531
2532 write_len = lttng_ust_ctl_write_one_packet_to_channel(
2533 stream->chan->uchan,
2534 &stream->chan->metadata_cache->contents.data[stream->ust_metadata_pushed],
2535 stream->chan->metadata_cache->contents.size - stream->ust_metadata_pushed);
2536 LTTNG_ASSERT(write_len != 0);
2537 if (write_len < 0) {
2538 ERR("Writing one metadata packet");
2539 ret = write_len;
2540 goto end;
2541 }
2542
2543 stream->ust_metadata_pushed += write_len;
2544 stream->chan->metadata_pushed_wait_queue.wake_all();
2545
2546 LTTNG_ASSERT(stream->chan->metadata_cache->contents.size >= stream->ust_metadata_pushed);
2547 ret = write_len;
2548
2549 /*
2550 * Switch packet (but don't open the next one) on every commit of
2551 * a metadata packet. Since the subbuffer is fully filled (with padding,
2552 * if needed), the stream is "quiescent" after this commit.
2553 */
2554 if (lttng_ust_ctl_flush_buffer(stream->ustream, 1)) {
2555 ERR("Failed to flush buffer while committing one metadata packet");
2556 ret = -EIO;
2557 } else {
2558 stream->quiescent = true;
2559 }
2560end:
2561 pthread_mutex_unlock(&stream->chan->metadata_cache->lock);
2562 return ret;
2563}
2564
2565/*
2566 * Sync metadata meaning request them to the session daemon and snapshot to the
2567 * metadata thread can consumer them.
2568 *
2569 * Metadata stream lock is held here, but we need to release it when
2570 * interacting with sessiond, else we cause a deadlock with live
2571 * awaiting on metadata to be pushed out.
2572 *
2573 * The RCU read side lock must be held by the caller.
2574 */
2575enum sync_metadata_status
2576lttng_ustconsumer_sync_metadata(struct lttng_consumer_local_data *ctx,
2577 struct lttng_consumer_stream *metadata_stream)
2578{
2579 int ret;
2580 enum sync_metadata_status status;
2581 struct lttng_consumer_channel *metadata_channel;
2582
2583 LTTNG_ASSERT(ctx);
2584 LTTNG_ASSERT(metadata_stream);
2585 ASSERT_RCU_READ_LOCKED();
2586
2587 metadata_channel = metadata_stream->chan;
2588 pthread_mutex_unlock(&metadata_stream->lock);
2589 /*
2590 * Request metadata from the sessiond, but don't wait for the flush
2591 * because we locked the metadata thread.
2592 */
2593 ret = lttng_ustconsumer_request_metadata(ctx, metadata_channel, false, 0);
2594 pthread_mutex_lock(&metadata_stream->lock);
2595 if (ret < 0) {
2596 status = SYNC_METADATA_STATUS_ERROR;
2597 goto end;
2598 }
2599
2600 /*
2601 * The metadata stream and channel can be deleted while the
2602 * metadata stream lock was released. The streamed is checked
2603 * for deletion before we use it further.
2604 *
2605 * Note that it is safe to access a logically-deleted stream since its
2606 * existence is still guaranteed by the RCU read side lock. However,
2607 * it should no longer be used. The close/deletion of the metadata
2608 * channel and stream already guarantees that all metadata has been
2609 * consumed. Therefore, there is nothing left to do in this function.
2610 */
2611 if (consumer_stream_is_deleted(metadata_stream)) {
2612 DBG("Metadata stream %" PRIu64 " was deleted during the metadata synchronization",
2613 metadata_stream->key);
2614 status = SYNC_METADATA_STATUS_NO_DATA;
2615 goto end;
2616 }
2617
2618 ret = commit_one_metadata_packet(metadata_stream);
2619 if (ret < 0) {
2620 status = SYNC_METADATA_STATUS_ERROR;
2621 goto end;
2622 } else if (ret > 0) {
2623 status = SYNC_METADATA_STATUS_NEW_DATA;
2624 } else /* ret == 0 */ {
2625 status = SYNC_METADATA_STATUS_NO_DATA;
2626 goto end;
2627 }
2628
2629 ret = lttng_ust_ctl_snapshot(metadata_stream->ustream);
2630 if (ret < 0) {
2631 ERR("Failed to take a snapshot of the metadata ring-buffer positions, ret = %d",
2632 ret);
2633 status = SYNC_METADATA_STATUS_ERROR;
2634 goto end;
2635 }
2636
2637end:
2638 return status;
2639}
2640
2641/*
2642 * Return 0 on success else a negative value.
2643 */
2644static int notify_if_more_data(struct lttng_consumer_stream *stream,
2645 struct lttng_consumer_local_data *ctx)
2646{
2647 int ret;
2648 struct lttng_ust_ctl_consumer_stream *ustream;
2649
2650 LTTNG_ASSERT(stream);
2651 LTTNG_ASSERT(ctx);
2652
2653 ustream = stream->ustream;
2654
2655 /*
2656 * First, we are going to check if there is a new subbuffer available
2657 * before reading the stream wait_fd.
2658 */
2659 /* Get the next subbuffer */
2660 ret = lttng_ust_ctl_get_next_subbuf(ustream);
2661 if (ret) {
2662 /* No more data found, flag the stream. */
2663 stream->has_data = 0;
2664 ret = 0;
2665 goto end;
2666 }
2667
2668 ret = lttng_ust_ctl_put_subbuf(ustream);
2669 LTTNG_ASSERT(!ret);
2670
2671 /* This stream still has data. Flag it and wake up the data thread. */
2672 stream->has_data = 1;
2673
2674 if (stream->monitor && !stream->hangup_flush_done && !ctx->has_wakeup) {
2675 ssize_t writelen;
2676
2677 writelen = lttng_pipe_write(ctx->consumer_wakeup_pipe, "!", 1);
2678 if (writelen < 0 && errno != EAGAIN && errno != EWOULDBLOCK) {
2679 ret = writelen;
2680 goto end;
2681 }
2682
2683 /* The wake up pipe has been notified. */
2684 ctx->has_wakeup = 1;
2685 }
2686 ret = 0;
2687
2688end:
2689 return ret;
2690}
2691
2692static int consumer_stream_ust_on_wake_up(struct lttng_consumer_stream *stream)
2693{
2694 int ret = 0;
2695
2696 /*
2697 * We can consume the 1 byte written into the wait_fd by
2698 * UST. Don't trigger error if we cannot read this one byte
2699 * (read returns 0), or if the error is EAGAIN or EWOULDBLOCK.
2700 *
2701 * This is only done when the stream is monitored by a thread,
2702 * before the flush is done after a hangup and if the stream
2703 * is not flagged with data since there might be nothing to
2704 * consume in the wait fd but still have data available
2705 * flagged by the consumer wake up pipe.
2706 */
2707 if (stream->monitor && !stream->hangup_flush_done && !stream->has_data) {
2708 char dummy;
2709 ssize_t readlen;
2710
2711 readlen = lttng_read(stream->wait_fd, &dummy, 1);
2712 if (readlen < 0 && errno != EAGAIN && errno != EWOULDBLOCK) {
2713 ret = readlen;
2714 }
2715 }
2716
2717 return ret;
2718}
2719
2720static int extract_common_subbuffer_info(struct lttng_consumer_stream *stream,
2721 struct stream_subbuffer *subbuf)
2722{
2723 int ret;
2724
2725 ret = lttng_ust_ctl_get_subbuf_size(stream->ustream, &subbuf->info.data.subbuf_size);
2726 if (ret) {
2727 goto end;
2728 }
2729
2730 ret = lttng_ust_ctl_get_padded_subbuf_size(stream->ustream,
2731 &subbuf->info.data.padded_subbuf_size);
2732 if (ret) {
2733 goto end;
2734 }
2735
2736end:
2737 return ret;
2738}
2739
2740static int extract_metadata_subbuffer_info(struct lttng_consumer_stream *stream,
2741 struct stream_subbuffer *subbuf)
2742{
2743 int ret;
2744
2745 ret = extract_common_subbuffer_info(stream, subbuf);
2746 if (ret) {
2747 goto end;
2748 }
2749
2750 subbuf->info.metadata.version = stream->metadata_version;
2751
2752end:
2753 return ret;
2754}
2755
2756static int extract_data_subbuffer_info(struct lttng_consumer_stream *stream,
2757 struct stream_subbuffer *subbuf)
2758{
2759 int ret;
2760
2761 ret = extract_common_subbuffer_info(stream, subbuf);
2762 if (ret) {
2763 goto end;
2764 }
2765
2766 ret = lttng_ust_ctl_get_packet_size(stream->ustream, &subbuf->info.data.packet_size);
2767 if (ret < 0) {
2768 PERROR("Failed to get sub-buffer packet size");
2769 goto end;
2770 }
2771
2772 ret = lttng_ust_ctl_get_content_size(stream->ustream, &subbuf->info.data.content_size);
2773 if (ret < 0) {
2774 PERROR("Failed to get sub-buffer content size");
2775 goto end;
2776 }
2777
2778 ret = lttng_ust_ctl_get_timestamp_begin(stream->ustream,
2779 &subbuf->info.data.timestamp_begin);
2780 if (ret < 0) {
2781 PERROR("Failed to get sub-buffer begin timestamp");
2782 goto end;
2783 }
2784
2785 ret = lttng_ust_ctl_get_timestamp_end(stream->ustream, &subbuf->info.data.timestamp_end);
2786 if (ret < 0) {
2787 PERROR("Failed to get sub-buffer end timestamp");
2788 goto end;
2789 }
2790
2791 ret = lttng_ust_ctl_get_events_discarded(stream->ustream,
2792 &subbuf->info.data.events_discarded);
2793 if (ret) {
2794 PERROR("Failed to get sub-buffer events discarded count");
2795 goto end;
2796 }
2797
2798 ret = lttng_ust_ctl_get_sequence_number(stream->ustream,
2799 &subbuf->info.data.sequence_number.value);
2800 if (ret) {
2801 /* May not be supported by older LTTng-modules. */
2802 if (ret != -ENOTTY) {
2803 PERROR("Failed to get sub-buffer sequence number");
2804 goto end;
2805 }
2806 } else {
2807 subbuf->info.data.sequence_number.is_set = true;
2808 }
2809
2810 ret = lttng_ust_ctl_get_stream_id(stream->ustream, &subbuf->info.data.stream_id);
2811 if (ret < 0) {
2812 PERROR("Failed to get stream id");
2813 goto end;
2814 }
2815
2816 ret = lttng_ust_ctl_get_instance_id(stream->ustream,
2817 &subbuf->info.data.stream_instance_id.value);
2818 if (ret) {
2819 /* May not be supported by older LTTng-modules. */
2820 if (ret != -ENOTTY) {
2821 PERROR("Failed to get stream instance id");
2822 goto end;
2823 }
2824 } else {
2825 subbuf->info.data.stream_instance_id.is_set = true;
2826 }
2827end:
2828 return ret;
2829}
2830
2831static int get_next_subbuffer_common(struct lttng_consumer_stream *stream,
2832 struct stream_subbuffer *subbuffer)
2833{
2834 int ret;
2835 const char *addr;
2836
2837 ret = stream->read_subbuffer_ops.extract_subbuffer_info(stream, subbuffer);
2838 if (ret) {
2839 goto end;
2840 }
2841
2842 ret = get_current_subbuf_addr(stream, &addr);
2843 if (ret) {
2844 goto end;
2845 }
2846
2847 subbuffer->buffer.buffer =
2848 lttng_buffer_view_init(addr, 0, subbuffer->info.data.padded_subbuf_size);
2849 LTTNG_ASSERT(subbuffer->buffer.buffer.data != nullptr);
2850end:
2851 return ret;
2852}
2853
2854static enum get_next_subbuffer_status get_next_subbuffer(struct lttng_consumer_stream *stream,
2855 struct stream_subbuffer *subbuffer)
2856{
2857 int ret;
2858 enum get_next_subbuffer_status status;
2859
2860 ret = lttng_ust_ctl_get_next_subbuf(stream->ustream);
2861 switch (ret) {
2862 case 0:
2863 status = GET_NEXT_SUBBUFFER_STATUS_OK;
2864 break;
2865 case -ENODATA:
2866 case -EAGAIN:
2867 /*
2868 * The caller only expects -ENODATA when there is no data to
2869 * read, but the kernel tracer returns -EAGAIN when there is
2870 * currently no data for a non-finalized stream, and -ENODATA
2871 * when there is no data for a finalized stream. Those can be
2872 * combined into a -ENODATA return value.
2873 */
2874 status = GET_NEXT_SUBBUFFER_STATUS_NO_DATA;
2875 goto end;
2876 default:
2877 status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
2878 goto end;
2879 }
2880
2881 ret = get_next_subbuffer_common(stream, subbuffer);
2882 if (ret) {
2883 status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
2884 goto end;
2885 }
2886end:
2887 return status;
2888}
2889
2890static enum get_next_subbuffer_status
2891get_next_subbuffer_metadata(struct lttng_consumer_stream *stream,
2892 struct stream_subbuffer *subbuffer)
2893{
2894 int ret;
2895 bool cache_empty;
2896 bool got_subbuffer;
2897 bool coherent;
2898 bool buffer_empty;
2899 unsigned long consumed_pos, produced_pos;
2900 enum get_next_subbuffer_status status;
2901
2902 do {
2903 ret = lttng_ust_ctl_get_next_subbuf(stream->ustream);
2904 if (ret == 0) {
2905 got_subbuffer = true;
2906 } else {
2907 got_subbuffer = false;
2908 if (ret != -EAGAIN) {
2909 /* Fatal error. */
2910 status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
2911 goto end;
2912 }
2913 }
2914
2915 /*
2916 * Determine if the cache is empty and ensure that a sub-buffer
2917 * is made available if the cache is not empty.
2918 */
2919 if (!got_subbuffer) {
2920 ret = commit_one_metadata_packet(stream);
2921 if (ret < 0 && ret != -ENOBUFS) {
2922 status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
2923 goto end;
2924 } else if (ret == 0) {
2925 /* Not an error, the cache is empty. */
2926 cache_empty = true;
2927 status = GET_NEXT_SUBBUFFER_STATUS_NO_DATA;
2928 goto end;
2929 } else {
2930 cache_empty = false;
2931 }
2932 } else {
2933 pthread_mutex_lock(&stream->chan->metadata_cache->lock);
2934 cache_empty = stream->chan->metadata_cache->contents.size ==
2935 stream->ust_metadata_pushed;
2936 pthread_mutex_unlock(&stream->chan->metadata_cache->lock);
2937 }
2938 } while (!got_subbuffer);
2939
2940 /* Populate sub-buffer infos and view. */
2941 ret = get_next_subbuffer_common(stream, subbuffer);
2942 if (ret) {
2943 status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
2944 goto end;
2945 }
2946
2947 ret = lttng_ustconsumer_sample_snapshot_positions(stream);
2948 if (ret < 0) {
2949 /*
2950 * -EAGAIN is not expected since we got a sub-buffer and haven't
2951 * pushed the consumption position yet (on put_next).
2952 */
2953 PERROR("Failed to take a snapshot of metadata buffer positions");
2954 status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
2955 goto end;
2956 }
2957
2958 ret = lttng_ustconsumer_get_consumed_snapshot(stream, &consumed_pos);
2959 if (ret) {
2960 PERROR("Failed to get metadata consumed position");
2961 status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
2962 goto end;
2963 }
2964
2965 ret = lttng_ustconsumer_get_produced_snapshot(stream, &produced_pos);
2966 if (ret) {
2967 PERROR("Failed to get metadata produced position");
2968 status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
2969 goto end;
2970 }
2971
2972 /* Last sub-buffer of the ring buffer ? */
2973 buffer_empty = (consumed_pos + stream->max_sb_size) == produced_pos;
2974
2975 /*
2976 * The sessiond registry lock ensures that coherent units of metadata
2977 * are pushed to the consumer daemon at once. Hence, if a sub-buffer is
2978 * acquired, the cache is empty, and it is the only available sub-buffer
2979 * available, it is safe to assume that it is "coherent".
2980 */
2981 coherent = got_subbuffer && cache_empty && buffer_empty;
2982
2983 LTTNG_OPTIONAL_SET(&subbuffer->info.metadata.coherent, coherent);
2984 status = GET_NEXT_SUBBUFFER_STATUS_OK;
2985end:
2986 return status;
2987}
2988
2989static int put_next_subbuffer(struct lttng_consumer_stream *stream,
2990 struct stream_subbuffer *subbuffer __attribute__((unused)))
2991{
2992 const int ret = lttng_ust_ctl_put_next_subbuf(stream->ustream);
2993
2994 LTTNG_ASSERT(ret == 0);
2995 return ret;
2996}
2997
2998static int signal_metadata(struct lttng_consumer_stream *stream,
2999 struct lttng_consumer_local_data *ctx __attribute__((unused)))
3000{
3001 ASSERT_LOCKED(stream->metadata_rdv_lock);
3002 return pthread_cond_broadcast(&stream->metadata_rdv) ? -errno : 0;
3003}
3004
3005static int lttng_ustconsumer_set_stream_ops(struct lttng_consumer_stream *stream)
3006{
3007 int ret = 0;
3008
3009 stream->read_subbuffer_ops.on_wake_up = consumer_stream_ust_on_wake_up;
3010 if (stream->metadata_flag) {
3011 stream->read_subbuffer_ops.get_next_subbuffer = get_next_subbuffer_metadata;
3012 stream->read_subbuffer_ops.extract_subbuffer_info = extract_metadata_subbuffer_info;
3013 stream->read_subbuffer_ops.reset_metadata =
3014 metadata_stream_reset_cache_consumed_position;
3015 if (stream->chan->is_live) {
3016 stream->read_subbuffer_ops.on_sleep = signal_metadata;
3017 ret = consumer_stream_enable_metadata_bucketization(stream);
3018 if (ret) {
3019 goto end;
3020 }
3021 }
3022 } else {
3023 stream->read_subbuffer_ops.get_next_subbuffer = get_next_subbuffer;
3024 stream->read_subbuffer_ops.extract_subbuffer_info = extract_data_subbuffer_info;
3025 stream->read_subbuffer_ops.on_sleep = notify_if_more_data;
3026 if (stream->chan->is_live) {
3027 stream->read_subbuffer_ops.send_live_beacon = consumer_flush_ust_index;
3028 }
3029 }
3030
3031 stream->read_subbuffer_ops.put_next_subbuffer = put_next_subbuffer;
3032end:
3033 return ret;
3034}
3035
3036/*
3037 * Called when a stream is created.
3038 *
3039 * Return 0 on success or else a negative value.
3040 */
3041int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream *stream)
3042{
3043 int ret;
3044
3045 LTTNG_ASSERT(stream);
3046
3047 /*
3048 * Don't create anything if this is set for streaming or if there is
3049 * no current trace chunk on the parent channel.
3050 */
3051 if (stream->net_seq_idx == (uint64_t) -1ULL && stream->chan->monitor &&
3052 stream->chan->trace_chunk) {
3053 ret = consumer_stream_create_output_files(stream, true);
3054 if (ret) {
3055 goto error;
3056 }
3057 }
3058
3059 lttng_ustconsumer_set_stream_ops(stream);
3060 ret = 0;
3061
3062error:
3063 return ret;
3064}
3065
3066/*
3067 * Check if data is still being extracted from the buffers for a specific
3068 * stream. Consumer data lock MUST be acquired before calling this function
3069 * and the stream lock.
3070 *
3071 * Return 1 if the traced data are still getting read else 0 meaning that the
3072 * data is available for trace viewer reading.
3073 */
3074int lttng_ustconsumer_data_pending(struct lttng_consumer_stream *stream)
3075{
3076 int ret;
3077
3078 LTTNG_ASSERT(stream);
3079 LTTNG_ASSERT(stream->ustream);
3080 ASSERT_LOCKED(stream->lock);
3081
3082 DBG("UST consumer checking data pending");
3083
3084 if (stream->endpoint_status != CONSUMER_ENDPOINT_ACTIVE) {
3085 ret = 0;
3086 goto end;
3087 }
3088
3089 if (stream->chan->type == CONSUMER_CHANNEL_TYPE_METADATA) {
3090 uint64_t contiguous, pushed;
3091
3092 /* Ease our life a bit. */
3093 pthread_mutex_lock(&stream->chan->metadata_cache->lock);
3094 contiguous = stream->chan->metadata_cache->contents.size;
3095 pthread_mutex_unlock(&stream->chan->metadata_cache->lock);
3096 pushed = stream->ust_metadata_pushed;
3097
3098 /*
3099 * We can simply check whether all contiguously available data
3100 * has been pushed to the ring buffer, since the push operation
3101 * is performed within get_next_subbuf(), and because both
3102 * get_next_subbuf() and put_next_subbuf() are issued atomically
3103 * thanks to the stream lock within
3104 * lttng_ustconsumer_read_subbuffer(). This basically means that
3105 * whetnever ust_metadata_pushed is incremented, the associated
3106 * metadata has been consumed from the metadata stream.
3107 */
3108 DBG("UST consumer metadata pending check: contiguous %" PRIu64
3109 " vs pushed %" PRIu64,
3110 contiguous,
3111 pushed);
3112 LTTNG_ASSERT(((int64_t) (contiguous - pushed)) >= 0);
3113 if ((contiguous != pushed) ||
3114 (((int64_t) contiguous - pushed) > 0 || contiguous == 0)) {
3115 ret = 1; /* Data is pending */
3116 goto end;
3117 }
3118 } else {
3119 ret = lttng_ust_ctl_get_next_subbuf(stream->ustream);
3120 if (ret == 0) {
3121 /*
3122 * There is still data so let's put back this
3123 * subbuffer.
3124 */
3125 ret = lttng_ust_ctl_put_subbuf(stream->ustream);
3126 LTTNG_ASSERT(ret == 0);
3127 ret = 1; /* Data is pending */
3128 goto end;
3129 }
3130 }
3131
3132 /* Data is NOT pending so ready to be read. */
3133 ret = 0;
3134
3135end:
3136 return ret;
3137}
3138
3139/*
3140 * Stop a given metadata channel timer if enabled and close the wait fd which
3141 * is the poll pipe of the metadata stream.
3142 *
3143 * This MUST be called with the metadata channel lock acquired.
3144 */
3145void lttng_ustconsumer_close_metadata(struct lttng_consumer_channel *metadata)
3146{
3147 int ret;
3148
3149 LTTNG_ASSERT(metadata);
3150 LTTNG_ASSERT(metadata->type == CONSUMER_CHANNEL_TYPE_METADATA);
3151
3152 DBG("Closing metadata channel key %" PRIu64, metadata->key);
3153
3154 if (metadata->switch_timer_enabled == 1) {
3155 consumer_timer_switch_stop(metadata);
3156 }
3157
3158 if (!metadata->metadata_stream) {
3159 goto end;
3160 }
3161
3162 /*
3163 * Closing write side so the thread monitoring the stream wakes up if any
3164 * and clean the metadata stream.
3165 */
3166 if (metadata->metadata_stream->ust_metadata_poll_pipe[1] >= 0) {
3167 ret = close(metadata->metadata_stream->ust_metadata_poll_pipe[1]);
3168 if (ret < 0) {
3169 PERROR("closing metadata pipe write side");
3170 }
3171 metadata->metadata_stream->ust_metadata_poll_pipe[1] = -1;
3172 }
3173
3174end:
3175 return;
3176}
3177
3178/*
3179 * Close every metadata stream wait fd of the metadata hash table. This
3180 * function MUST be used very carefully so not to run into a race between the
3181 * metadata thread handling streams and this function closing their wait fd.
3182 *
3183 * For UST, this is used when the session daemon hangs up. Its the metadata
3184 * producer so calling this is safe because we are assured that no state change
3185 * can occur in the metadata thread for the streams in the hash table.
3186 */
3187void lttng_ustconsumer_close_all_metadata(struct lttng_ht *metadata_ht)
3188{
3189 LTTNG_ASSERT(metadata_ht);
3190 LTTNG_ASSERT(metadata_ht->ht);
3191
3192 DBG("UST consumer closing all metadata streams");
3193
3194 for (auto *stream :
3195 lttng::urcu::lfht_iteration_adapter<lttng_consumer_stream,
3196 decltype(lttng_consumer_stream::node),
3197 &lttng_consumer_stream::node>(*metadata_ht->ht)) {
3198 health_code_update();
3199
3200 pthread_mutex_lock(&stream->chan->lock);
3201 pthread_mutex_lock(&stream->lock);
3202 lttng_ustconsumer_close_metadata(stream->chan);
3203 pthread_mutex_unlock(&stream->lock);
3204 pthread_mutex_unlock(&stream->chan->lock);
3205 }
3206}
3207
3208void lttng_ustconsumer_close_stream_wakeup(struct lttng_consumer_stream *stream)
3209{
3210 int ret;
3211
3212 ret = lttng_ust_ctl_stream_close_wakeup_fd(stream->ustream);
3213 if (ret < 0) {
3214 ERR("Unable to close wakeup fd");
3215 }
3216}
3217
3218/*
3219 * Please refer to consumer-timer.c before adding any lock within this
3220 * function or any of its callees. Timers have a very strict locking
3221 * semantic with respect to teardown. Failure to respect this semantic
3222 * introduces deadlocks.
3223 *
3224 * DON'T hold the metadata lock when calling this function, else this
3225 * can cause deadlock involving consumer awaiting for metadata to be
3226 * pushed out due to concurrent interaction with the session daemon.
3227 */
3228int lttng_ustconsumer_request_metadata(struct lttng_consumer_local_data *ctx,
3229 struct lttng_consumer_channel *channel,
3230 bool invoked_by_timer,
3231 int wait)
3232{
3233 struct lttcomm_metadata_request_msg request;
3234 struct lttcomm_consumer_msg msg;
3235 const lttcomm_return_code ret_code = LTTCOMM_CONSUMERD_SUCCESS;
3236 uint64_t len, key, offset, version;
3237 int ret;
3238
3239 LTTNG_ASSERT(channel);
3240 LTTNG_ASSERT(!channel->is_deleted);
3241 LTTNG_ASSERT(channel->metadata_cache);
3242
3243 memset(&request, 0, sizeof(request));
3244
3245 /* send the metadata request to sessiond */
3246 switch (the_consumer_data.type) {
3247 case LTTNG_CONSUMER64_UST:
3248 request.bits_per_long = 64;
3249 break;
3250 case LTTNG_CONSUMER32_UST:
3251 request.bits_per_long = 32;
3252 break;
3253 default:
3254 request.bits_per_long = 0;
3255 break;
3256 }
3257
3258 request.session_id = channel->session_id;
3259 request.session_id_per_pid = channel->session_id_per_pid;
3260 /*
3261 * Request the application UID here so the metadata of that application can
3262 * be sent back. The channel UID corresponds to the user UID of the session
3263 * used for the rights on the stream file(s).
3264 */
3265 request.uid = channel->ust_app_uid;
3266 request.key = channel->key;
3267
3268 DBG("Sending metadata request to sessiond, session id %" PRIu64 ", per-pid %" PRIu64
3269 ", app UID %u and channel key %" PRIu64,
3270 request.session_id,
3271 request.session_id_per_pid,
3272 request.uid,
3273 request.key);
3274
3275 pthread_mutex_lock(&ctx->metadata_socket_lock);
3276
3277 health_code_update();
3278
3279 ret = lttcomm_send_unix_sock(ctx->consumer_metadata_socket, &request, sizeof(request));
3280 if (ret < 0) {
3281 ERR("Asking metadata to sessiond");
3282 goto end;
3283 }
3284
3285 health_code_update();
3286
3287 /* Receive the metadata from sessiond */
3288 ret = lttcomm_recv_unix_sock(ctx->consumer_metadata_socket, &msg, sizeof(msg));
3289 if (ret != sizeof(msg)) {
3290 DBG("Consumer received unexpected message size %d (expects %zu)", ret, sizeof(msg));
3291 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_CMD);
3292 /*
3293 * The ret value might 0 meaning an orderly shutdown but this is ok
3294 * since the caller handles this.
3295 */
3296 goto end;
3297 }
3298
3299 health_code_update();
3300
3301 if (msg.cmd_type == LTTNG_ERR_UND) {
3302 /* No registry found */
3303 (void) consumer_send_status_msg(ctx->consumer_metadata_socket, ret_code);
3304 ret = 0;
3305 goto end;
3306 } else if (msg.cmd_type != LTTNG_CONSUMER_PUSH_METADATA) {
3307 ERR("Unexpected cmd_type received %d", msg.cmd_type);
3308 ret = -1;
3309 goto end;
3310 }
3311
3312 len = msg.u.push_metadata.len;
3313 key = msg.u.push_metadata.key;
3314 offset = msg.u.push_metadata.target_offset;
3315 version = msg.u.push_metadata.version;
3316
3317 LTTNG_ASSERT(key == channel->key);
3318 if (len == 0) {
3319 DBG("No new metadata to receive for key %" PRIu64, key);
3320 }
3321
3322 health_code_update();
3323
3324 /* Tell session daemon we are ready to receive the metadata. */
3325 ret = consumer_send_status_msg(ctx->consumer_metadata_socket, LTTCOMM_CONSUMERD_SUCCESS);
3326 if (ret < 0 || len == 0) {
3327 /*
3328 * Somehow, the session daemon is not responding anymore or there is
3329 * nothing to receive.
3330 */
3331 goto end;
3332 }
3333
3334 health_code_update();
3335
3336 ret = lttng_ustconsumer_recv_metadata(ctx->consumer_metadata_socket,
3337 key,
3338 offset,
3339 len,
3340 version,
3341 channel,
3342 invoked_by_timer,
3343 wait);
3344 if (ret >= 0) {
3345 /*
3346 * Only send the status msg if the sessiond is alive meaning a positive
3347 * ret code.
3348 */
3349 (void) consumer_send_status_msg(ctx->consumer_metadata_socket, ret);
3350 }
3351 ret = 0;
3352
3353end:
3354 health_code_update();
3355
3356 pthread_mutex_unlock(&ctx->metadata_socket_lock);
3357 return ret;
3358}
3359
3360/*
3361 * Return the ustctl call for the get stream id.
3362 */
3363int lttng_ustconsumer_get_stream_id(struct lttng_consumer_stream *stream, uint64_t *stream_id)
3364{
3365 LTTNG_ASSERT(stream);
3366 LTTNG_ASSERT(stream_id);
3367
3368 return lttng_ust_ctl_get_stream_id(stream->ustream, stream_id);
3369}
3370
3371void lttng_ustconsumer_sigbus_handle(void *addr)
3372{
3373 lttng_ust_ctl_sigbus_handle(addr);
3374}
This page took 0.034992 seconds and 4 git commands to generate.