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