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