42a097b955e96e73a330f38b634099d34c1f0c62
[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.h>
29 #include <common/common.h>
30 #include <common/sessiond-comm/sessiond-comm.h>
31 #include <common/relayd/relayd.h>
32 #include <common/compat/fcntl.h>
33 #include <common/compat/endian.h>
34 #include <common/consumer/consumer-metadata-cache.h>
35 #include <common/consumer/consumer-stream.h>
36 #include <common/consumer/consumer-timer.h>
37 #include <common/utils.h>
38 #include <common/index/index.h>
39 #include <common/consumer/consumer.h>
40 #include <common/shm.h>
41 #include <common/optional.h>
42
43 #include "ust-consumer.h"
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(&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(&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 cds_list_del(&metadata->metadata_stream->send_node);
954 metadata->metadata_stream = NULL;
955 send_streams_error:
956 error_no_stream:
957 end:
958 return ret;
959 }
960
961 /*
962 * Snapshot the whole metadata.
963 * RCU read-side lock must be held by the caller.
964 *
965 * Returns 0 on success, < 0 on error
966 */
967 static int snapshot_metadata(struct lttng_consumer_channel *metadata_channel,
968 uint64_t key, char *path, uint64_t relayd_id,
969 struct lttng_consumer_local_data *ctx)
970 {
971 int ret = 0;
972 struct lttng_consumer_stream *metadata_stream;
973
974 LTTNG_ASSERT(path);
975 LTTNG_ASSERT(ctx);
976 ASSERT_RCU_READ_LOCKED();
977
978 DBG("UST consumer snapshot metadata with key %" PRIu64 " at path %s",
979 key, path);
980
981 rcu_read_lock();
982
983 LTTNG_ASSERT(!metadata_channel->monitor);
984
985 health_code_update();
986
987 /*
988 * Ask the sessiond if we have new metadata waiting and update the
989 * consumer metadata cache.
990 */
991 ret = lttng_ustconsumer_request_metadata(ctx, metadata_channel, 0, 1);
992 if (ret < 0) {
993 goto error;
994 }
995
996 health_code_update();
997
998 /*
999 * The metadata stream is NOT created in no monitor mode when the channel
1000 * is created on a sessiond ask channel command.
1001 */
1002 ret = create_ust_streams(metadata_channel, ctx);
1003 if (ret < 0) {
1004 goto error;
1005 }
1006
1007 metadata_stream = metadata_channel->metadata_stream;
1008 LTTNG_ASSERT(metadata_stream);
1009
1010 metadata_stream->read_subbuffer_ops.lock(metadata_stream);
1011 if (relayd_id != (uint64_t) -1ULL) {
1012 metadata_stream->net_seq_idx = relayd_id;
1013 ret = consumer_send_relayd_stream(metadata_stream, path);
1014 } else {
1015 ret = consumer_stream_create_output_files(metadata_stream,
1016 false);
1017 }
1018 if (ret < 0) {
1019 goto error_stream;
1020 }
1021
1022 do {
1023 health_code_update();
1024 ret = lttng_consumer_read_subbuffer(metadata_stream, ctx, true);
1025 if (ret < 0) {
1026 goto error_stream;
1027 }
1028 } while (ret > 0);
1029
1030 error_stream:
1031 metadata_stream->read_subbuffer_ops.unlock(metadata_stream);
1032 /*
1033 * Clean up the stream completely because the next snapshot will use a
1034 * new metadata stream.
1035 */
1036 consumer_stream_destroy(metadata_stream, NULL);
1037 cds_list_del(&metadata_stream->send_node);
1038 metadata_channel->metadata_stream = NULL;
1039
1040 error:
1041 rcu_read_unlock();
1042 return ret;
1043 }
1044
1045 static
1046 int get_current_subbuf_addr(struct lttng_consumer_stream *stream,
1047 const char **addr)
1048 {
1049 int ret;
1050 unsigned long mmap_offset;
1051 const char *mmap_base;
1052
1053 mmap_base = (const char *) lttng_ust_ctl_get_mmap_base(stream->ustream);
1054 if (!mmap_base) {
1055 ERR("Failed to get mmap base for stream `%s`",
1056 stream->name);
1057 ret = -EPERM;
1058 goto error;
1059 }
1060
1061 ret = lttng_ust_ctl_get_mmap_read_offset(stream->ustream, &mmap_offset);
1062 if (ret != 0) {
1063 ERR("Failed to get mmap offset for stream `%s`", stream->name);
1064 ret = -EINVAL;
1065 goto error;
1066 }
1067
1068 *addr = mmap_base + mmap_offset;
1069 error:
1070 return ret;
1071
1072 }
1073
1074 /*
1075 * Take a snapshot of all the stream of a channel.
1076 * RCU read-side lock and the channel lock must be held by the caller.
1077 *
1078 * Returns 0 on success, < 0 on error
1079 */
1080 static int snapshot_channel(struct lttng_consumer_channel *channel,
1081 uint64_t key, char *path, uint64_t relayd_id,
1082 uint64_t nb_packets_per_stream,
1083 struct lttng_consumer_local_data *ctx)
1084 {
1085 int ret;
1086 unsigned use_relayd = 0;
1087 unsigned long consumed_pos, produced_pos;
1088 struct lttng_consumer_stream *stream;
1089
1090 LTTNG_ASSERT(path);
1091 LTTNG_ASSERT(ctx);
1092 ASSERT_RCU_READ_LOCKED();
1093
1094 rcu_read_lock();
1095
1096 if (relayd_id != (uint64_t) -1ULL) {
1097 use_relayd = 1;
1098 }
1099
1100 LTTNG_ASSERT(!channel->monitor);
1101 DBG("UST consumer snapshot channel %" PRIu64, key);
1102
1103 cds_list_for_each_entry(stream, &channel->streams.head, send_node) {
1104 health_code_update();
1105
1106 /* Lock stream because we are about to change its state. */
1107 pthread_mutex_lock(&stream->lock);
1108 LTTNG_ASSERT(channel->trace_chunk);
1109 if (!lttng_trace_chunk_get(channel->trace_chunk)) {
1110 /*
1111 * Can't happen barring an internal error as the channel
1112 * holds a reference to the trace chunk.
1113 */
1114 ERR("Failed to acquire reference to channel's trace chunk");
1115 ret = -1;
1116 goto error_unlock;
1117 }
1118 LTTNG_ASSERT(!stream->trace_chunk);
1119 stream->trace_chunk = channel->trace_chunk;
1120
1121 stream->net_seq_idx = relayd_id;
1122
1123 if (use_relayd) {
1124 ret = consumer_send_relayd_stream(stream, path);
1125 if (ret < 0) {
1126 goto error_unlock;
1127 }
1128 } else {
1129 ret = consumer_stream_create_output_files(stream,
1130 false);
1131 if (ret < 0) {
1132 goto error_unlock;
1133 }
1134 DBG("UST consumer snapshot stream (%" PRIu64 ")",
1135 stream->key);
1136 }
1137
1138 /*
1139 * If tracing is active, we want to perform a "full" buffer flush.
1140 * Else, if quiescent, it has already been done by the prior stop.
1141 */
1142 if (!stream->quiescent) {
1143 ret = lttng_ust_ctl_flush_buffer(stream->ustream, 0);
1144 if (ret < 0) {
1145 ERR("Failed to flush buffer during snapshot of channel: channel key = %" PRIu64 ", channel name = '%s'",
1146 channel->key, channel->name);
1147 goto error_unlock;
1148 }
1149 }
1150
1151 ret = lttng_ustconsumer_take_snapshot(stream);
1152 if (ret < 0) {
1153 ERR("Taking UST snapshot");
1154 goto error_unlock;
1155 }
1156
1157 ret = lttng_ustconsumer_get_produced_snapshot(stream, &produced_pos);
1158 if (ret < 0) {
1159 ERR("Produced UST snapshot position");
1160 goto error_unlock;
1161 }
1162
1163 ret = lttng_ustconsumer_get_consumed_snapshot(stream, &consumed_pos);
1164 if (ret < 0) {
1165 ERR("Consumerd UST snapshot position");
1166 goto error_unlock;
1167 }
1168
1169 /*
1170 * The original value is sent back if max stream size is larger than
1171 * the possible size of the snapshot. Also, we assume that the session
1172 * daemon should never send a maximum stream size that is lower than
1173 * subbuffer size.
1174 */
1175 consumed_pos = consumer_get_consume_start_pos(consumed_pos,
1176 produced_pos, nb_packets_per_stream,
1177 stream->max_sb_size);
1178
1179 while ((long) (consumed_pos - produced_pos) < 0) {
1180 ssize_t read_len;
1181 unsigned long len, padded_len;
1182 const char *subbuf_addr;
1183 struct lttng_buffer_view subbuf_view;
1184
1185 health_code_update();
1186
1187 DBG("UST consumer taking snapshot at pos %lu", consumed_pos);
1188
1189 ret = lttng_ust_ctl_get_subbuf(stream->ustream, &consumed_pos);
1190 if (ret < 0) {
1191 if (ret != -EAGAIN) {
1192 PERROR("lttng_ust_ctl_get_subbuf snapshot");
1193 goto error_close_stream;
1194 }
1195 DBG("UST consumer get subbuf failed. Skipping it.");
1196 consumed_pos += stream->max_sb_size;
1197 stream->chan->lost_packets++;
1198 continue;
1199 }
1200
1201 ret = lttng_ust_ctl_get_subbuf_size(stream->ustream, &len);
1202 if (ret < 0) {
1203 ERR("Snapshot lttng_ust_ctl_get_subbuf_size");
1204 goto error_put_subbuf;
1205 }
1206
1207 ret = lttng_ust_ctl_get_padded_subbuf_size(stream->ustream, &padded_len);
1208 if (ret < 0) {
1209 ERR("Snapshot lttng_ust_ctl_get_padded_subbuf_size");
1210 goto error_put_subbuf;
1211 }
1212
1213 ret = get_current_subbuf_addr(stream, &subbuf_addr);
1214 if (ret) {
1215 goto error_put_subbuf;
1216 }
1217
1218 subbuf_view = lttng_buffer_view_init(
1219 subbuf_addr, 0, padded_len);
1220 read_len = lttng_consumer_on_read_subbuffer_mmap(
1221 stream, &subbuf_view, padded_len - len);
1222 if (use_relayd) {
1223 if (read_len != len) {
1224 ret = -EPERM;
1225 goto error_put_subbuf;
1226 }
1227 } else {
1228 if (read_len != padded_len) {
1229 ret = -EPERM;
1230 goto error_put_subbuf;
1231 }
1232 }
1233
1234 ret = lttng_ust_ctl_put_subbuf(stream->ustream);
1235 if (ret < 0) {
1236 ERR("Snapshot lttng_ust_ctl_put_subbuf");
1237 goto error_close_stream;
1238 }
1239 consumed_pos += stream->max_sb_size;
1240 }
1241
1242 /* Simply close the stream so we can use it on the next snapshot. */
1243 consumer_stream_close(stream);
1244 pthread_mutex_unlock(&stream->lock);
1245 }
1246
1247 rcu_read_unlock();
1248 return 0;
1249
1250 error_put_subbuf:
1251 if (lttng_ust_ctl_put_subbuf(stream->ustream) < 0) {
1252 ERR("Snapshot lttng_ust_ctl_put_subbuf");
1253 }
1254 error_close_stream:
1255 consumer_stream_close(stream);
1256 error_unlock:
1257 pthread_mutex_unlock(&stream->lock);
1258 rcu_read_unlock();
1259 return ret;
1260 }
1261
1262 static
1263 void metadata_stream_reset_cache_consumed_position(
1264 struct lttng_consumer_stream *stream)
1265 {
1266 ASSERT_LOCKED(stream->lock);
1267
1268 DBG("Reset metadata cache of session %" PRIu64,
1269 stream->chan->session_id);
1270 stream->ust_metadata_pushed = 0;
1271 }
1272
1273 /*
1274 * Receive the metadata updates from the sessiond. Supports receiving
1275 * overlapping metadata, but is needs to always belong to a contiguous
1276 * range starting from 0.
1277 * Be careful about the locks held when calling this function: it needs
1278 * the metadata cache flush to concurrently progress in order to
1279 * complete.
1280 */
1281 int lttng_ustconsumer_recv_metadata(int sock, uint64_t key, uint64_t offset,
1282 uint64_t len, uint64_t version,
1283 struct lttng_consumer_channel *channel, int timer, int wait)
1284 {
1285 int ret, ret_code = LTTCOMM_CONSUMERD_SUCCESS;
1286 char *metadata_str;
1287 enum consumer_metadata_cache_write_status cache_write_status;
1288
1289 DBG("UST consumer push metadata key %" PRIu64 " of len %" PRIu64, key, len);
1290
1291 metadata_str = (char *) zmalloc(len * sizeof(char));
1292 if (!metadata_str) {
1293 PERROR("zmalloc metadata string");
1294 ret_code = LTTCOMM_CONSUMERD_ENOMEM;
1295 goto end;
1296 }
1297
1298 health_code_update();
1299
1300 /* Receive metadata string. */
1301 ret = lttcomm_recv_unix_sock(sock, metadata_str, len);
1302 if (ret < 0) {
1303 /* Session daemon is dead so return gracefully. */
1304 ret_code = ret;
1305 goto end_free;
1306 }
1307
1308 health_code_update();
1309
1310 pthread_mutex_lock(&channel->metadata_cache->lock);
1311 cache_write_status = consumer_metadata_cache_write(
1312 channel->metadata_cache, offset, len, version,
1313 metadata_str);
1314 pthread_mutex_unlock(&channel->metadata_cache->lock);
1315 switch (cache_write_status) {
1316 case CONSUMER_METADATA_CACHE_WRITE_STATUS_NO_CHANGE:
1317 /*
1318 * The write entirely overlapped with existing contents of the
1319 * same metadata version (same content); there is nothing to do.
1320 */
1321 break;
1322 case CONSUMER_METADATA_CACHE_WRITE_STATUS_INVALIDATED:
1323 /*
1324 * The metadata cache was invalidated (previously pushed
1325 * content has been overwritten). Reset the stream's consumed
1326 * metadata position to ensure the metadata poll thread consumes
1327 * the whole cache.
1328 */
1329
1330 /*
1331 * channel::metadata_stream can be null when the metadata
1332 * channel is under a snapshot session type. No need to update
1333 * the stream position in that scenario.
1334 */
1335 if (channel->metadata_stream != NULL) {
1336 pthread_mutex_lock(&channel->metadata_stream->lock);
1337 metadata_stream_reset_cache_consumed_position(
1338 channel->metadata_stream);
1339 pthread_mutex_unlock(&channel->metadata_stream->lock);
1340 } else {
1341 /* Validate we are in snapshot mode. */
1342 LTTNG_ASSERT(!channel->monitor);
1343 }
1344 /* Fall-through. */
1345 case CONSUMER_METADATA_CACHE_WRITE_STATUS_APPENDED_CONTENT:
1346 /*
1347 * In both cases, the metadata poll thread has new data to
1348 * consume.
1349 */
1350 ret = consumer_metadata_wakeup_pipe(channel);
1351 if (ret) {
1352 ret_code = LTTCOMM_CONSUMERD_ERROR_METADATA;
1353 goto end_free;
1354 }
1355 break;
1356 case CONSUMER_METADATA_CACHE_WRITE_STATUS_ERROR:
1357 /* Unable to handle metadata. Notify session daemon. */
1358 ret_code = LTTCOMM_CONSUMERD_ERROR_METADATA;
1359 /*
1360 * Skip metadata flush on write error since the offset and len might
1361 * not have been updated which could create an infinite loop below when
1362 * waiting for the metadata cache to be flushed.
1363 */
1364 goto end_free;
1365 default:
1366 abort();
1367 }
1368
1369 if (!wait) {
1370 goto end_free;
1371 }
1372 while (consumer_metadata_cache_flushed(channel, offset + len, timer)) {
1373 DBG("Waiting for metadata to be flushed");
1374
1375 health_code_update();
1376
1377 usleep(DEFAULT_METADATA_AVAILABILITY_WAIT_TIME);
1378 }
1379
1380 end_free:
1381 free(metadata_str);
1382 end:
1383 return ret_code;
1384 }
1385
1386 /*
1387 * Receive command from session daemon and process it.
1388 *
1389 * Return 1 on success else a negative value or 0.
1390 */
1391 int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
1392 int sock, struct pollfd *consumer_sockpoll)
1393 {
1394 int ret_func;
1395 enum lttcomm_return_code ret_code = LTTCOMM_CONSUMERD_SUCCESS;
1396 struct lttcomm_consumer_msg msg;
1397 struct lttng_consumer_channel *channel = NULL;
1398
1399 health_code_update();
1400
1401 {
1402 ssize_t ret_recv;
1403
1404 ret_recv = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg));
1405 if (ret_recv != sizeof(msg)) {
1406 DBG("Consumer received unexpected message size %zd (expects %zu)",
1407 ret_recv, sizeof(msg));
1408 /*
1409 * The ret value might 0 meaning an orderly shutdown but this is ok
1410 * since the caller handles this.
1411 */
1412 if (ret_recv > 0) {
1413 lttng_consumer_send_error(ctx,
1414 LTTCOMM_CONSUMERD_ERROR_RECV_CMD);
1415 ret_recv = -1;
1416 }
1417 return ret_recv;
1418 }
1419 }
1420
1421 health_code_update();
1422
1423 /* deprecated */
1424 LTTNG_ASSERT(msg.cmd_type != LTTNG_CONSUMER_STOP);
1425
1426 health_code_update();
1427
1428 /* relayd needs RCU read-side lock */
1429 rcu_read_lock();
1430
1431 switch (msg.cmd_type) {
1432 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET:
1433 {
1434 uint32_t major = msg.u.relayd_sock.major;
1435 uint32_t minor = msg.u.relayd_sock.minor;
1436 enum lttcomm_sock_proto protocol =
1437 (enum lttcomm_sock_proto) msg.u.relayd_sock
1438 .relayd_socket_protocol;
1439
1440 /* Session daemon status message are handled in the following call. */
1441 consumer_add_relayd_socket(msg.u.relayd_sock.net_index,
1442 msg.u.relayd_sock.type, ctx, sock,
1443 consumer_sockpoll, msg.u.relayd_sock.session_id,
1444 msg.u.relayd_sock.relayd_session_id, major,
1445 minor, protocol);
1446 goto end_nosignal;
1447 }
1448 case LTTNG_CONSUMER_DESTROY_RELAYD:
1449 {
1450 uint64_t index = msg.u.destroy_relayd.net_seq_idx;
1451 struct consumer_relayd_sock_pair *relayd;
1452
1453 DBG("UST consumer destroying relayd %" PRIu64, index);
1454
1455 /* Get relayd reference if exists. */
1456 relayd = consumer_find_relayd(index);
1457 if (relayd == NULL) {
1458 DBG("Unable to find relayd %" PRIu64, index);
1459 ret_code = LTTCOMM_CONSUMERD_RELAYD_FAIL;
1460 }
1461
1462 /*
1463 * Each relayd socket pair has a refcount of stream attached to it
1464 * which tells if the relayd is still active or not depending on the
1465 * refcount value.
1466 *
1467 * This will set the destroy flag of the relayd object and destroy it
1468 * if the refcount reaches zero when called.
1469 *
1470 * The destroy can happen either here or when a stream fd hangs up.
1471 */
1472 if (relayd) {
1473 consumer_flag_relayd_for_destroy(relayd);
1474 }
1475
1476 goto end_msg_sessiond;
1477 }
1478 case LTTNG_CONSUMER_UPDATE_STREAM:
1479 {
1480 rcu_read_unlock();
1481 return -ENOSYS;
1482 }
1483 case LTTNG_CONSUMER_DATA_PENDING:
1484 {
1485 int is_data_pending;
1486 ssize_t ret_send;
1487 uint64_t id = msg.u.data_pending.session_id;
1488
1489 DBG("UST consumer data pending command for id %" PRIu64, id);
1490
1491 is_data_pending = consumer_data_pending(id);
1492
1493 /* Send back returned value to session daemon */
1494 ret_send = lttcomm_send_unix_sock(sock, &is_data_pending,
1495 sizeof(is_data_pending));
1496 if (ret_send < 0) {
1497 DBG("Error when sending the data pending ret code: %zd",
1498 ret_send);
1499 goto error_fatal;
1500 }
1501
1502 /*
1503 * No need to send back a status message since the data pending
1504 * returned value is the response.
1505 */
1506 break;
1507 }
1508 case LTTNG_CONSUMER_ASK_CHANNEL_CREATION:
1509 {
1510 int ret_ask_channel, ret_add_channel, ret_send;
1511 struct lttng_ust_ctl_consumer_channel_attr attr;
1512 const uint64_t chunk_id = msg.u.ask_channel.chunk_id.value;
1513 const struct lttng_credentials buffer_credentials = {
1514 .uid = LTTNG_OPTIONAL_INIT_VALUE(msg.u.ask_channel.buffer_credentials.uid),
1515 .gid = LTTNG_OPTIONAL_INIT_VALUE(msg.u.ask_channel.buffer_credentials.gid),
1516 };
1517
1518 /* Create a plain object and reserve a channel key. */
1519 channel = consumer_allocate_channel(
1520 msg.u.ask_channel.key,
1521 msg.u.ask_channel.session_id,
1522 msg.u.ask_channel.chunk_id.is_set ?
1523 &chunk_id : NULL,
1524 msg.u.ask_channel.pathname,
1525 msg.u.ask_channel.name,
1526 msg.u.ask_channel.relayd_id,
1527 (enum lttng_event_output) msg.u.ask_channel.output,
1528 msg.u.ask_channel.tracefile_size,
1529 msg.u.ask_channel.tracefile_count,
1530 msg.u.ask_channel.session_id_per_pid,
1531 msg.u.ask_channel.monitor,
1532 msg.u.ask_channel.live_timer_interval,
1533 msg.u.ask_channel.is_live,
1534 msg.u.ask_channel.root_shm_path,
1535 msg.u.ask_channel.shm_path);
1536 if (!channel) {
1537 goto end_channel_error;
1538 }
1539
1540 LTTNG_OPTIONAL_SET(&channel->buffer_credentials,
1541 buffer_credentials);
1542
1543 /*
1544 * Assign UST application UID to the channel. This value is ignored for
1545 * per PID buffers. This is specific to UST thus setting this after the
1546 * allocation.
1547 */
1548 channel->ust_app_uid = msg.u.ask_channel.ust_app_uid;
1549
1550 /* Build channel attributes from received message. */
1551 attr.subbuf_size = msg.u.ask_channel.subbuf_size;
1552 attr.num_subbuf = msg.u.ask_channel.num_subbuf;
1553 attr.overwrite = msg.u.ask_channel.overwrite;
1554 attr.switch_timer_interval = msg.u.ask_channel.switch_timer_interval;
1555 attr.read_timer_interval = msg.u.ask_channel.read_timer_interval;
1556 attr.chan_id = msg.u.ask_channel.chan_id;
1557 memcpy(attr.uuid, msg.u.ask_channel.uuid, sizeof(attr.uuid));
1558 attr.blocking_timeout= msg.u.ask_channel.blocking_timeout;
1559
1560 /* Match channel buffer type to the UST abi. */
1561 switch (msg.u.ask_channel.output) {
1562 case LTTNG_EVENT_MMAP:
1563 default:
1564 attr.output = LTTNG_UST_ABI_MMAP;
1565 break;
1566 }
1567
1568 /* Translate and save channel type. */
1569 switch (msg.u.ask_channel.type) {
1570 case LTTNG_UST_ABI_CHAN_PER_CPU:
1571 channel->type = CONSUMER_CHANNEL_TYPE_DATA;
1572 attr.type = LTTNG_UST_ABI_CHAN_PER_CPU;
1573 /*
1574 * Set refcount to 1 for owner. Below, we will
1575 * pass ownership to the
1576 * consumer_thread_channel_poll() thread.
1577 */
1578 channel->refcount = 1;
1579 break;
1580 case LTTNG_UST_ABI_CHAN_METADATA:
1581 channel->type = CONSUMER_CHANNEL_TYPE_METADATA;
1582 attr.type = LTTNG_UST_ABI_CHAN_METADATA;
1583 break;
1584 default:
1585 abort();
1586 goto error_fatal;
1587 };
1588
1589 health_code_update();
1590
1591 ret_ask_channel = ask_channel(ctx, channel, &attr);
1592 if (ret_ask_channel < 0) {
1593 goto end_channel_error;
1594 }
1595
1596 if (msg.u.ask_channel.type == LTTNG_UST_ABI_CHAN_METADATA) {
1597 int ret_allocate;
1598
1599 ret_allocate = consumer_metadata_cache_allocate(
1600 channel);
1601 if (ret_allocate < 0) {
1602 ERR("Allocating metadata cache");
1603 goto end_channel_error;
1604 }
1605 consumer_timer_switch_start(channel, attr.switch_timer_interval);
1606 attr.switch_timer_interval = 0;
1607 } else {
1608 int monitor_start_ret;
1609
1610 consumer_timer_live_start(channel,
1611 msg.u.ask_channel.live_timer_interval);
1612 monitor_start_ret = consumer_timer_monitor_start(
1613 channel,
1614 msg.u.ask_channel.monitor_timer_interval);
1615 if (monitor_start_ret < 0) {
1616 ERR("Starting channel monitoring timer failed");
1617 goto end_channel_error;
1618 }
1619 }
1620
1621 health_code_update();
1622
1623 /*
1624 * Add the channel to the internal state AFTER all streams were created
1625 * and successfully sent to session daemon. This way, all streams must
1626 * be ready before this channel is visible to the threads.
1627 * If add_channel succeeds, ownership of the channel is
1628 * passed to consumer_thread_channel_poll().
1629 */
1630 ret_add_channel = add_channel(channel, ctx);
1631 if (ret_add_channel < 0) {
1632 if (msg.u.ask_channel.type == LTTNG_UST_ABI_CHAN_METADATA) {
1633 if (channel->switch_timer_enabled == 1) {
1634 consumer_timer_switch_stop(channel);
1635 }
1636 consumer_metadata_cache_destroy(channel);
1637 }
1638 if (channel->live_timer_enabled == 1) {
1639 consumer_timer_live_stop(channel);
1640 }
1641 if (channel->monitor_timer_enabled == 1) {
1642 consumer_timer_monitor_stop(channel);
1643 }
1644 goto end_channel_error;
1645 }
1646
1647 health_code_update();
1648
1649 /*
1650 * Channel and streams are now created. Inform the session daemon that
1651 * everything went well and should wait to receive the channel and
1652 * streams with ustctl API.
1653 */
1654 ret_send = consumer_send_status_channel(sock, channel);
1655 if (ret_send < 0) {
1656 /*
1657 * There is probably a problem on the socket.
1658 */
1659 goto error_fatal;
1660 }
1661
1662 break;
1663 }
1664 case LTTNG_CONSUMER_GET_CHANNEL:
1665 {
1666 int ret, relayd_err = 0;
1667 uint64_t key = msg.u.get_channel.key;
1668 struct lttng_consumer_channel *found_channel;
1669
1670 found_channel = consumer_find_channel(key);
1671 if (!found_channel) {
1672 ERR("UST consumer get channel key %" PRIu64 " not found", key);
1673 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
1674 goto end_get_channel;
1675 }
1676
1677 health_code_update();
1678
1679 /* Send the channel to sessiond (and relayd, if applicable). */
1680 ret = send_channel_to_sessiond_and_relayd(
1681 sock, found_channel, ctx, &relayd_err);
1682 if (ret < 0) {
1683 if (relayd_err) {
1684 /*
1685 * We were unable to send to the relayd the stream so avoid
1686 * sending back a fatal error to the thread since this is OK
1687 * and the consumer can continue its work. The above call
1688 * has sent the error status message to the sessiond.
1689 */
1690 goto end_get_channel_nosignal;
1691 }
1692 /*
1693 * The communicaton was broken hence there is a bad state between
1694 * the consumer and sessiond so stop everything.
1695 */
1696 goto error_get_channel_fatal;
1697 }
1698
1699 health_code_update();
1700
1701 /*
1702 * In no monitor mode, the streams ownership is kept inside the channel
1703 * so don't send them to the data thread.
1704 */
1705 if (!found_channel->monitor) {
1706 goto end_get_channel;
1707 }
1708
1709 ret = send_streams_to_thread(found_channel, ctx);
1710 if (ret < 0) {
1711 /*
1712 * If we are unable to send the stream to the thread, there is
1713 * a big problem so just stop everything.
1714 */
1715 goto error_get_channel_fatal;
1716 }
1717 /* List MUST be empty after or else it could be reused. */
1718 LTTNG_ASSERT(cds_list_empty(&found_channel->streams.head));
1719 end_get_channel:
1720 goto end_msg_sessiond;
1721 error_get_channel_fatal:
1722 goto error_fatal;
1723 end_get_channel_nosignal:
1724 goto end_nosignal;
1725 }
1726 case LTTNG_CONSUMER_DESTROY_CHANNEL:
1727 {
1728 uint64_t key = msg.u.destroy_channel.key;
1729
1730 /*
1731 * Only called if streams have not been sent to stream
1732 * manager thread. However, channel has been sent to
1733 * channel manager thread.
1734 */
1735 notify_thread_del_channel(ctx, key);
1736 goto end_msg_sessiond;
1737 }
1738 case LTTNG_CONSUMER_CLOSE_METADATA:
1739 {
1740 int ret;
1741
1742 ret = close_metadata(msg.u.close_metadata.key);
1743 if (ret != 0) {
1744 ret_code = (lttcomm_return_code) ret;
1745 }
1746
1747 goto end_msg_sessiond;
1748 }
1749 case LTTNG_CONSUMER_FLUSH_CHANNEL:
1750 {
1751 int ret;
1752
1753 ret = flush_channel(msg.u.flush_channel.key);
1754 if (ret != 0) {
1755 ret_code = (lttcomm_return_code) ret;
1756 }
1757
1758 goto end_msg_sessiond;
1759 }
1760 case LTTNG_CONSUMER_CLEAR_QUIESCENT_CHANNEL:
1761 {
1762 int ret;
1763
1764 ret = clear_quiescent_channel(
1765 msg.u.clear_quiescent_channel.key);
1766 if (ret != 0) {
1767 ret_code = (lttcomm_return_code) ret;
1768 }
1769
1770 goto end_msg_sessiond;
1771 }
1772 case LTTNG_CONSUMER_PUSH_METADATA:
1773 {
1774 int ret;
1775 uint64_t len = msg.u.push_metadata.len;
1776 uint64_t key = msg.u.push_metadata.key;
1777 uint64_t offset = msg.u.push_metadata.target_offset;
1778 uint64_t version = msg.u.push_metadata.version;
1779 struct lttng_consumer_channel *found_channel;
1780
1781 DBG("UST consumer push metadata key %" PRIu64 " of len %" PRIu64, key,
1782 len);
1783
1784 found_channel = consumer_find_channel(key);
1785 if (!found_channel) {
1786 /*
1787 * This is possible if the metadata creation on the consumer side
1788 * is in flight vis-a-vis a concurrent push metadata from the
1789 * session daemon. Simply return that the channel failed and the
1790 * session daemon will handle that message correctly considering
1791 * that this race is acceptable thus the DBG() statement here.
1792 */
1793 DBG("UST consumer push metadata %" PRIu64 " not found", key);
1794 ret_code = LTTCOMM_CONSUMERD_CHANNEL_FAIL;
1795 goto end_push_metadata_msg_sessiond;
1796 }
1797
1798 health_code_update();
1799
1800 if (!len) {
1801 /*
1802 * There is nothing to receive. We have simply
1803 * checked whether the channel can be found.
1804 */
1805 ret_code = LTTCOMM_CONSUMERD_SUCCESS;
1806 goto end_push_metadata_msg_sessiond;
1807 }
1808
1809 /* Tell session daemon we are ready to receive the metadata. */
1810 ret = consumer_send_status_msg(sock, LTTCOMM_CONSUMERD_SUCCESS);
1811 if (ret < 0) {
1812 /* Somehow, the session daemon is not responding anymore. */
1813 goto error_push_metadata_fatal;
1814 }
1815
1816 health_code_update();
1817
1818 /* Wait for more data. */
1819 health_poll_entry();
1820 ret = lttng_consumer_poll_socket(consumer_sockpoll);
1821 health_poll_exit();
1822 if (ret) {
1823 goto error_push_metadata_fatal;
1824 }
1825
1826 health_code_update();
1827
1828 ret = lttng_ustconsumer_recv_metadata(sock, key, offset, len,
1829 version, found_channel, 0, 1);
1830 if (ret < 0) {
1831 /* error receiving from sessiond */
1832 goto error_push_metadata_fatal;
1833 } else {
1834 ret_code = (lttcomm_return_code) ret;
1835 goto end_push_metadata_msg_sessiond;
1836 }
1837 end_push_metadata_msg_sessiond:
1838 goto end_msg_sessiond;
1839 error_push_metadata_fatal:
1840 goto error_fatal;
1841 }
1842 case LTTNG_CONSUMER_SETUP_METADATA:
1843 {
1844 int ret;
1845
1846 ret = setup_metadata(ctx, msg.u.setup_metadata.key);
1847 if (ret) {
1848 ret_code = (lttcomm_return_code) ret;
1849 }
1850 goto end_msg_sessiond;
1851 }
1852 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL:
1853 {
1854 struct lttng_consumer_channel *found_channel;
1855 uint64_t key = msg.u.snapshot_channel.key;
1856 int ret_send;
1857
1858 found_channel = consumer_find_channel(key);
1859 if (!found_channel) {
1860 DBG("UST snapshot channel not found for key %" PRIu64, key);
1861 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
1862 } else {
1863 if (msg.u.snapshot_channel.metadata) {
1864 int ret_snapshot;
1865
1866 ret_snapshot = snapshot_metadata(found_channel,
1867 key,
1868 msg.u.snapshot_channel.pathname,
1869 msg.u.snapshot_channel.relayd_id,
1870 ctx);
1871 if (ret_snapshot < 0) {
1872 ERR("Snapshot metadata failed");
1873 ret_code = LTTCOMM_CONSUMERD_SNAPSHOT_FAILED;
1874 }
1875 } else {
1876 int ret_snapshot;
1877
1878 ret_snapshot = snapshot_channel(found_channel,
1879 key,
1880 msg.u.snapshot_channel.pathname,
1881 msg.u.snapshot_channel.relayd_id,
1882 msg.u.snapshot_channel
1883 .nb_packets_per_stream,
1884 ctx);
1885 if (ret_snapshot < 0) {
1886 ERR("Snapshot channel failed");
1887 ret_code = LTTCOMM_CONSUMERD_SNAPSHOT_FAILED;
1888 }
1889 }
1890 }
1891 health_code_update();
1892 ret_send = consumer_send_status_msg(sock, ret_code);
1893 if (ret_send < 0) {
1894 /* Somehow, the session daemon is not responding anymore. */
1895 goto end_nosignal;
1896 }
1897 health_code_update();
1898 break;
1899 }
1900 case LTTNG_CONSUMER_DISCARDED_EVENTS:
1901 {
1902 int ret = 0;
1903 uint64_t discarded_events;
1904 struct lttng_ht_iter iter;
1905 struct lttng_ht *ht;
1906 struct lttng_consumer_stream *stream;
1907 uint64_t id = msg.u.discarded_events.session_id;
1908 uint64_t key = msg.u.discarded_events.channel_key;
1909
1910 DBG("UST consumer discarded events command for session id %"
1911 PRIu64, id);
1912 rcu_read_lock();
1913 pthread_mutex_lock(&the_consumer_data.lock);
1914
1915 ht = the_consumer_data.stream_list_ht;
1916
1917 /*
1918 * We only need a reference to the channel, but they are not
1919 * directly indexed, so we just use the first matching stream
1920 * to extract the information we need, we default to 0 if not
1921 * found (no events are dropped if the channel is not yet in
1922 * use).
1923 */
1924 discarded_events = 0;
1925 cds_lfht_for_each_entry_duplicate(ht->ht,
1926 ht->hash_fct(&id, lttng_ht_seed),
1927 ht->match_fct, &id,
1928 &iter.iter, stream, node_session_id.node) {
1929 if (stream->chan->key == key) {
1930 discarded_events = stream->chan->discarded_events;
1931 break;
1932 }
1933 }
1934 pthread_mutex_unlock(&the_consumer_data.lock);
1935 rcu_read_unlock();
1936
1937 DBG("UST consumer discarded events command for session id %"
1938 PRIu64 ", channel key %" PRIu64, id, key);
1939
1940 health_code_update();
1941
1942 /* Send back returned value to session daemon */
1943 ret = lttcomm_send_unix_sock(sock, &discarded_events, sizeof(discarded_events));
1944 if (ret < 0) {
1945 PERROR("send discarded events");
1946 goto error_fatal;
1947 }
1948
1949 break;
1950 }
1951 case LTTNG_CONSUMER_LOST_PACKETS:
1952 {
1953 int ret;
1954 uint64_t lost_packets;
1955 struct lttng_ht_iter iter;
1956 struct lttng_ht *ht;
1957 struct lttng_consumer_stream *stream;
1958 uint64_t id = msg.u.lost_packets.session_id;
1959 uint64_t key = msg.u.lost_packets.channel_key;
1960
1961 DBG("UST consumer lost packets command for session id %"
1962 PRIu64, id);
1963 rcu_read_lock();
1964 pthread_mutex_lock(&the_consumer_data.lock);
1965
1966 ht = the_consumer_data.stream_list_ht;
1967
1968 /*
1969 * We only need a reference to the channel, but they are not
1970 * directly indexed, so we just use the first matching stream
1971 * to extract the information we need, we default to 0 if not
1972 * found (no packets lost if the channel is not yet in use).
1973 */
1974 lost_packets = 0;
1975 cds_lfht_for_each_entry_duplicate(ht->ht,
1976 ht->hash_fct(&id, lttng_ht_seed),
1977 ht->match_fct, &id,
1978 &iter.iter, stream, node_session_id.node) {
1979 if (stream->chan->key == key) {
1980 lost_packets = stream->chan->lost_packets;
1981 break;
1982 }
1983 }
1984 pthread_mutex_unlock(&the_consumer_data.lock);
1985 rcu_read_unlock();
1986
1987 DBG("UST consumer lost packets command for session id %"
1988 PRIu64 ", channel key %" PRIu64, id, key);
1989
1990 health_code_update();
1991
1992 /* Send back returned value to session daemon */
1993 ret = lttcomm_send_unix_sock(sock, &lost_packets,
1994 sizeof(lost_packets));
1995 if (ret < 0) {
1996 PERROR("send lost packets");
1997 goto error_fatal;
1998 }
1999
2000 break;
2001 }
2002 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE:
2003 {
2004 int channel_monitor_pipe, ret_send,
2005 ret_set_channel_monitor_pipe;
2006 ssize_t ret_recv;
2007
2008 ret_code = LTTCOMM_CONSUMERD_SUCCESS;
2009 /* Successfully received the command's type. */
2010 ret_send = consumer_send_status_msg(sock, ret_code);
2011 if (ret_send < 0) {
2012 goto error_fatal;
2013 }
2014
2015 ret_recv = lttcomm_recv_fds_unix_sock(
2016 sock, &channel_monitor_pipe, 1);
2017 if (ret_recv != sizeof(channel_monitor_pipe)) {
2018 ERR("Failed to receive channel monitor pipe");
2019 goto error_fatal;
2020 }
2021
2022 DBG("Received channel monitor pipe (%d)", channel_monitor_pipe);
2023 ret_set_channel_monitor_pipe =
2024 consumer_timer_thread_set_channel_monitor_pipe(
2025 channel_monitor_pipe);
2026 if (!ret_set_channel_monitor_pipe) {
2027 int flags;
2028 int ret_fcntl;
2029
2030 ret_code = LTTCOMM_CONSUMERD_SUCCESS;
2031 /* Set the pipe as non-blocking. */
2032 ret_fcntl = fcntl(channel_monitor_pipe, F_GETFL, 0);
2033 if (ret_fcntl == -1) {
2034 PERROR("fcntl get flags of the channel monitoring pipe");
2035 goto error_fatal;
2036 }
2037 flags = ret_fcntl;
2038
2039 ret_fcntl = fcntl(channel_monitor_pipe, F_SETFL,
2040 flags | O_NONBLOCK);
2041 if (ret_fcntl == -1) {
2042 PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe");
2043 goto error_fatal;
2044 }
2045 DBG("Channel monitor pipe set as non-blocking");
2046 } else {
2047 ret_code = LTTCOMM_CONSUMERD_ALREADY_SET;
2048 }
2049 goto end_msg_sessiond;
2050 }
2051 case LTTNG_CONSUMER_ROTATE_CHANNEL:
2052 {
2053 struct lttng_consumer_channel *found_channel;
2054 uint64_t key = msg.u.rotate_channel.key;
2055 int ret_send_status;
2056
2057 found_channel = consumer_find_channel(key);
2058 if (!found_channel) {
2059 DBG("Channel %" PRIu64 " not found", key);
2060 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
2061 } else {
2062 int rotate_channel;
2063
2064 /*
2065 * Sample the rotate position of all the streams in
2066 * this channel.
2067 */
2068 rotate_channel = lttng_consumer_rotate_channel(
2069 found_channel, key,
2070 msg.u.rotate_channel.relayd_id,
2071 msg.u.rotate_channel.metadata, ctx);
2072 if (rotate_channel < 0) {
2073 ERR("Rotate channel failed");
2074 ret_code = LTTCOMM_CONSUMERD_ROTATION_FAIL;
2075 }
2076
2077 health_code_update();
2078 }
2079
2080 ret_send_status = consumer_send_status_msg(sock, ret_code);
2081 if (ret_send_status < 0) {
2082 /* Somehow, the session daemon is not responding anymore. */
2083 goto end_rotate_channel_nosignal;
2084 }
2085
2086 /*
2087 * Rotate the streams that are ready right now.
2088 * FIXME: this is a second consecutive iteration over the
2089 * streams in a channel, there is probably a better way to
2090 * handle this, but it needs to be after the
2091 * consumer_send_status_msg() call.
2092 */
2093 if (found_channel) {
2094 int ret_rotate_read_streams;
2095
2096 ret_rotate_read_streams =
2097 lttng_consumer_rotate_ready_streams(
2098 found_channel, key,
2099 ctx);
2100 if (ret_rotate_read_streams < 0) {
2101 ERR("Rotate channel failed");
2102 }
2103 }
2104 break;
2105 end_rotate_channel_nosignal:
2106 goto end_nosignal;
2107 }
2108 case LTTNG_CONSUMER_CLEAR_CHANNEL:
2109 {
2110 struct lttng_consumer_channel *found_channel;
2111 uint64_t key = msg.u.clear_channel.key;
2112 int ret_send_status;
2113
2114 found_channel = consumer_find_channel(key);
2115 if (!found_channel) {
2116 DBG("Channel %" PRIu64 " not found", key);
2117 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
2118 } else {
2119 int ret_clear_channel;
2120
2121 ret_clear_channel = lttng_consumer_clear_channel(
2122 found_channel);
2123 if (ret_clear_channel) {
2124 ERR("Clear channel failed key %" PRIu64, key);
2125 ret_code = (lttcomm_return_code) ret_clear_channel;
2126 }
2127
2128 health_code_update();
2129 }
2130 ret_send_status = consumer_send_status_msg(sock, ret_code);
2131 if (ret_send_status < 0) {
2132 /* Somehow, the session daemon is not responding anymore. */
2133 goto end_nosignal;
2134 }
2135 break;
2136 }
2137 case LTTNG_CONSUMER_INIT:
2138 {
2139 int ret_send_status;
2140
2141 ret_code = lttng_consumer_init_command(ctx,
2142 msg.u.init.sessiond_uuid);
2143 health_code_update();
2144 ret_send_status = consumer_send_status_msg(sock, ret_code);
2145 if (ret_send_status < 0) {
2146 /* Somehow, the session daemon is not responding anymore. */
2147 goto end_nosignal;
2148 }
2149 break;
2150 }
2151 case LTTNG_CONSUMER_CREATE_TRACE_CHUNK:
2152 {
2153 const struct lttng_credentials credentials = {
2154 .uid = LTTNG_OPTIONAL_INIT_VALUE(msg.u.create_trace_chunk.credentials.value.uid),
2155 .gid = LTTNG_OPTIONAL_INIT_VALUE(msg.u.create_trace_chunk.credentials.value.gid),
2156 };
2157 const bool is_local_trace =
2158 !msg.u.create_trace_chunk.relayd_id.is_set;
2159 const uint64_t relayd_id =
2160 msg.u.create_trace_chunk.relayd_id.value;
2161 const char *chunk_override_name =
2162 *msg.u.create_trace_chunk.override_name ?
2163 msg.u.create_trace_chunk.override_name :
2164 NULL;
2165 struct lttng_directory_handle *chunk_directory_handle = NULL;
2166
2167 /*
2168 * The session daemon will only provide a chunk directory file
2169 * descriptor for local traces.
2170 */
2171 if (is_local_trace) {
2172 int chunk_dirfd;
2173 int ret_send_status;
2174 ssize_t ret_recv;
2175
2176 /* Acnowledge the reception of the command. */
2177 ret_send_status = consumer_send_status_msg(
2178 sock, LTTCOMM_CONSUMERD_SUCCESS);
2179 if (ret_send_status < 0) {
2180 /* Somehow, the session daemon is not responding anymore. */
2181 goto end_nosignal;
2182 }
2183
2184 /*
2185 * Receive trace chunk domain dirfd.
2186 */
2187 ret_recv = lttcomm_recv_fds_unix_sock(
2188 sock, &chunk_dirfd, 1);
2189 if (ret_recv != sizeof(chunk_dirfd)) {
2190 ERR("Failed to receive trace chunk domain directory file descriptor");
2191 goto error_fatal;
2192 }
2193
2194 DBG("Received trace chunk domain directory fd (%d)",
2195 chunk_dirfd);
2196 chunk_directory_handle = lttng_directory_handle_create_from_dirfd(
2197 chunk_dirfd);
2198 if (!chunk_directory_handle) {
2199 ERR("Failed to initialize chunk domain directory handle from directory file descriptor");
2200 if (close(chunk_dirfd)) {
2201 PERROR("Failed to close chunk directory file descriptor");
2202 }
2203 goto error_fatal;
2204 }
2205 }
2206
2207 ret_code = lttng_consumer_create_trace_chunk(
2208 !is_local_trace ? &relayd_id : NULL,
2209 msg.u.create_trace_chunk.session_id,
2210 msg.u.create_trace_chunk.chunk_id,
2211 (time_t) msg.u.create_trace_chunk
2212 .creation_timestamp,
2213 chunk_override_name,
2214 msg.u.create_trace_chunk.credentials.is_set ?
2215 &credentials :
2216 NULL,
2217 chunk_directory_handle);
2218 lttng_directory_handle_put(chunk_directory_handle);
2219 goto end_msg_sessiond;
2220 }
2221 case LTTNG_CONSUMER_CLOSE_TRACE_CHUNK:
2222 {
2223 enum lttng_trace_chunk_command_type close_command =
2224 (lttng_trace_chunk_command_type)
2225 msg.u.close_trace_chunk.close_command.value;
2226 const uint64_t relayd_id =
2227 msg.u.close_trace_chunk.relayd_id.value;
2228 struct lttcomm_consumer_close_trace_chunk_reply reply;
2229 char closed_trace_chunk_path[LTTNG_PATH_MAX] = {};
2230 int ret;
2231
2232 ret_code = lttng_consumer_close_trace_chunk(
2233 msg.u.close_trace_chunk.relayd_id.is_set ?
2234 &relayd_id :
2235 NULL,
2236 msg.u.close_trace_chunk.session_id,
2237 msg.u.close_trace_chunk.chunk_id,
2238 (time_t) msg.u.close_trace_chunk.close_timestamp,
2239 msg.u.close_trace_chunk.close_command.is_set ?
2240 &close_command :
2241 NULL, closed_trace_chunk_path);
2242 reply.ret_code = ret_code;
2243 reply.path_length = strlen(closed_trace_chunk_path) + 1;
2244 ret = lttcomm_send_unix_sock(sock, &reply, sizeof(reply));
2245 if (ret != sizeof(reply)) {
2246 goto error_fatal;
2247 }
2248 ret = lttcomm_send_unix_sock(sock, closed_trace_chunk_path,
2249 reply.path_length);
2250 if (ret != reply.path_length) {
2251 goto error_fatal;
2252 }
2253 goto end_nosignal;
2254 }
2255 case LTTNG_CONSUMER_TRACE_CHUNK_EXISTS:
2256 {
2257 const uint64_t relayd_id =
2258 msg.u.trace_chunk_exists.relayd_id.value;
2259
2260 ret_code = lttng_consumer_trace_chunk_exists(
2261 msg.u.trace_chunk_exists.relayd_id.is_set ?
2262 &relayd_id : NULL,
2263 msg.u.trace_chunk_exists.session_id,
2264 msg.u.trace_chunk_exists.chunk_id);
2265 goto end_msg_sessiond;
2266 }
2267 case LTTNG_CONSUMER_OPEN_CHANNEL_PACKETS:
2268 {
2269 const uint64_t key = msg.u.open_channel_packets.key;
2270 struct lttng_consumer_channel *found_channel =
2271 consumer_find_channel(key);
2272
2273 if (found_channel) {
2274 pthread_mutex_lock(&found_channel->lock);
2275 ret_code = lttng_consumer_open_channel_packets(
2276 found_channel);
2277 pthread_mutex_unlock(&found_channel->lock);
2278 } else {
2279 /*
2280 * The channel could have disappeared in per-pid
2281 * buffering mode.
2282 */
2283 DBG("Channel %" PRIu64 " not found", key);
2284 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
2285 }
2286
2287 health_code_update();
2288 goto end_msg_sessiond;
2289 }
2290 default:
2291 break;
2292 }
2293
2294 end_nosignal:
2295 /*
2296 * Return 1 to indicate success since the 0 value can be a socket
2297 * shutdown during the recv() or send() call.
2298 */
2299 ret_func = 1;
2300 goto end;
2301
2302 end_msg_sessiond:
2303 /*
2304 * The returned value here is not useful since either way we'll return 1 to
2305 * the caller because the session daemon socket management is done
2306 * elsewhere. Returning a negative code or 0 will shutdown the consumer.
2307 */
2308 {
2309 int ret_send_status;
2310
2311 ret_send_status = consumer_send_status_msg(sock, ret_code);
2312 if (ret_send_status < 0) {
2313 goto error_fatal;
2314 }
2315 }
2316
2317 ret_func = 1;
2318 goto end;
2319
2320 end_channel_error:
2321 if (channel) {
2322 /*
2323 * Free channel here since no one has a reference to it. We don't
2324 * free after that because a stream can store this pointer.
2325 */
2326 destroy_channel(channel);
2327 }
2328 /* We have to send a status channel message indicating an error. */
2329 {
2330 int ret_send_status;
2331
2332 ret_send_status = consumer_send_status_channel(sock, NULL);
2333 if (ret_send_status < 0) {
2334 /* Stop everything if session daemon can not be notified. */
2335 goto error_fatal;
2336 }
2337 }
2338
2339 ret_func = 1;
2340 goto end;
2341
2342 error_fatal:
2343 /* This will issue a consumer stop. */
2344 ret_func = -1;
2345 goto end;
2346
2347 end:
2348 rcu_read_unlock();
2349 health_code_update();
2350 return ret_func;
2351 }
2352
2353 int lttng_ust_flush_buffer(struct lttng_consumer_stream *stream,
2354 int producer_active)
2355 {
2356 LTTNG_ASSERT(stream);
2357 LTTNG_ASSERT(stream->ustream);
2358
2359 return lttng_ust_ctl_flush_buffer(stream->ustream, producer_active);
2360 }
2361
2362 /*
2363 * Take a snapshot for a specific stream.
2364 *
2365 * Returns 0 on success, < 0 on error
2366 */
2367 int lttng_ustconsumer_take_snapshot(struct lttng_consumer_stream *stream)
2368 {
2369 LTTNG_ASSERT(stream);
2370 LTTNG_ASSERT(stream->ustream);
2371
2372 return lttng_ust_ctl_snapshot(stream->ustream);
2373 }
2374
2375 /*
2376 * Sample consumed and produced positions for a specific stream.
2377 *
2378 * Returns 0 on success, < 0 on error.
2379 */
2380 int lttng_ustconsumer_sample_snapshot_positions(
2381 struct lttng_consumer_stream *stream)
2382 {
2383 LTTNG_ASSERT(stream);
2384 LTTNG_ASSERT(stream->ustream);
2385
2386 return lttng_ust_ctl_snapshot_sample_positions(stream->ustream);
2387 }
2388
2389 /*
2390 * Get the produced position
2391 *
2392 * Returns 0 on success, < 0 on error
2393 */
2394 int lttng_ustconsumer_get_produced_snapshot(
2395 struct lttng_consumer_stream *stream, unsigned long *pos)
2396 {
2397 LTTNG_ASSERT(stream);
2398 LTTNG_ASSERT(stream->ustream);
2399 LTTNG_ASSERT(pos);
2400
2401 return lttng_ust_ctl_snapshot_get_produced(stream->ustream, pos);
2402 }
2403
2404 /*
2405 * Get the consumed position
2406 *
2407 * Returns 0 on success, < 0 on error
2408 */
2409 int lttng_ustconsumer_get_consumed_snapshot(
2410 struct lttng_consumer_stream *stream, unsigned long *pos)
2411 {
2412 LTTNG_ASSERT(stream);
2413 LTTNG_ASSERT(stream->ustream);
2414 LTTNG_ASSERT(pos);
2415
2416 return lttng_ust_ctl_snapshot_get_consumed(stream->ustream, pos);
2417 }
2418
2419 int lttng_ustconsumer_flush_buffer(struct lttng_consumer_stream *stream,
2420 int producer)
2421 {
2422 LTTNG_ASSERT(stream);
2423 LTTNG_ASSERT(stream->ustream);
2424
2425 return lttng_ust_ctl_flush_buffer(stream->ustream, producer);
2426 }
2427
2428 int lttng_ustconsumer_clear_buffer(struct lttng_consumer_stream *stream)
2429 {
2430 LTTNG_ASSERT(stream);
2431 LTTNG_ASSERT(stream->ustream);
2432
2433 return lttng_ust_ctl_clear_buffer(stream->ustream);
2434 }
2435
2436 int lttng_ustconsumer_get_current_timestamp(
2437 struct lttng_consumer_stream *stream, uint64_t *ts)
2438 {
2439 LTTNG_ASSERT(stream);
2440 LTTNG_ASSERT(stream->ustream);
2441 LTTNG_ASSERT(ts);
2442
2443 return lttng_ust_ctl_get_current_timestamp(stream->ustream, ts);
2444 }
2445
2446 int lttng_ustconsumer_get_sequence_number(
2447 struct lttng_consumer_stream *stream, uint64_t *seq)
2448 {
2449 LTTNG_ASSERT(stream);
2450 LTTNG_ASSERT(stream->ustream);
2451 LTTNG_ASSERT(seq);
2452
2453 return lttng_ust_ctl_get_sequence_number(stream->ustream, seq);
2454 }
2455
2456 /*
2457 * Called when the stream signals the consumer that it has hung up.
2458 */
2459 void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream *stream)
2460 {
2461 LTTNG_ASSERT(stream);
2462 LTTNG_ASSERT(stream->ustream);
2463
2464 pthread_mutex_lock(&stream->lock);
2465 if (!stream->quiescent) {
2466 if (lttng_ust_ctl_flush_buffer(stream->ustream, 0) < 0) {
2467 ERR("Failed to flush buffer on stream hang-up");
2468 } else {
2469 stream->quiescent = true;
2470 }
2471 }
2472 pthread_mutex_unlock(&stream->lock);
2473 stream->hangup_flush_done = 1;
2474 }
2475
2476 void lttng_ustconsumer_del_channel(struct lttng_consumer_channel *chan)
2477 {
2478 int i;
2479
2480 LTTNG_ASSERT(chan);
2481 LTTNG_ASSERT(chan->uchan);
2482 LTTNG_ASSERT(chan->buffer_credentials.is_set);
2483
2484 if (chan->switch_timer_enabled == 1) {
2485 consumer_timer_switch_stop(chan);
2486 }
2487 for (i = 0; i < chan->nr_stream_fds; i++) {
2488 int ret;
2489
2490 ret = close(chan->stream_fds[i]);
2491 if (ret) {
2492 PERROR("close");
2493 }
2494 if (chan->shm_path[0]) {
2495 char shm_path[PATH_MAX];
2496
2497 ret = get_stream_shm_path(shm_path, chan->shm_path, i);
2498 if (ret) {
2499 ERR("Cannot get stream shm path");
2500 }
2501 ret = run_as_unlink(shm_path,
2502 lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(
2503 chan->buffer_credentials)),
2504 lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(
2505 chan->buffer_credentials)));
2506 if (ret) {
2507 PERROR("unlink %s", shm_path);
2508 }
2509 }
2510 }
2511 }
2512
2513 void lttng_ustconsumer_free_channel(struct lttng_consumer_channel *chan)
2514 {
2515 LTTNG_ASSERT(chan);
2516 LTTNG_ASSERT(chan->uchan);
2517 LTTNG_ASSERT(chan->buffer_credentials.is_set);
2518
2519 consumer_metadata_cache_destroy(chan);
2520 lttng_ust_ctl_destroy_channel(chan->uchan);
2521 /* Try to rmdir all directories under shm_path root. */
2522 if (chan->root_shm_path[0]) {
2523 (void) run_as_rmdir_recursive(chan->root_shm_path,
2524 lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(
2525 chan->buffer_credentials)),
2526 lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(
2527 chan->buffer_credentials)),
2528 LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG);
2529 }
2530 free(chan->stream_fds);
2531 }
2532
2533 void lttng_ustconsumer_del_stream(struct lttng_consumer_stream *stream)
2534 {
2535 LTTNG_ASSERT(stream);
2536 LTTNG_ASSERT(stream->ustream);
2537
2538 if (stream->chan->switch_timer_enabled == 1) {
2539 consumer_timer_switch_stop(stream->chan);
2540 }
2541 lttng_ust_ctl_destroy_stream(stream->ustream);
2542 }
2543
2544 int lttng_ustconsumer_get_wakeup_fd(struct lttng_consumer_stream *stream)
2545 {
2546 LTTNG_ASSERT(stream);
2547 LTTNG_ASSERT(stream->ustream);
2548
2549 return lttng_ust_ctl_stream_get_wakeup_fd(stream->ustream);
2550 }
2551
2552 int lttng_ustconsumer_close_wakeup_fd(struct lttng_consumer_stream *stream)
2553 {
2554 LTTNG_ASSERT(stream);
2555 LTTNG_ASSERT(stream->ustream);
2556
2557 return lttng_ust_ctl_stream_close_wakeup_fd(stream->ustream);
2558 }
2559
2560 /*
2561 * Write up to one packet from the metadata cache to the channel.
2562 *
2563 * Returns the number of bytes pushed from the cache into the ring buffer, or a
2564 * negative value on error.
2565 */
2566 static
2567 int commit_one_metadata_packet(struct lttng_consumer_stream *stream)
2568 {
2569 ssize_t write_len;
2570 int ret;
2571
2572 pthread_mutex_lock(&stream->chan->metadata_cache->lock);
2573 if (stream->chan->metadata_cache->contents.size ==
2574 stream->ust_metadata_pushed) {
2575 /*
2576 * In the context of a user space metadata channel, a
2577 * change in version can be detected in two ways:
2578 * 1) During the pre-consume of the `read_subbuffer` loop,
2579 * 2) When populating the metadata ring buffer (i.e. here).
2580 *
2581 * This function is invoked when there is no metadata
2582 * available in the ring-buffer. If all data was consumed
2583 * up to the size of the metadata cache, there is no metadata
2584 * to insert in the ring-buffer.
2585 *
2586 * However, the metadata version could still have changed (a
2587 * regeneration without any new data will yield the same cache
2588 * size).
2589 *
2590 * The cache's version is checked for a version change and the
2591 * consumed position is reset if one occurred.
2592 *
2593 * This check is only necessary for the user space domain as
2594 * it has to manage the cache explicitly. If this reset was not
2595 * performed, no metadata would be consumed (and no reset would
2596 * occur as part of the pre-consume) until the metadata size
2597 * exceeded the cache size.
2598 */
2599 if (stream->metadata_version !=
2600 stream->chan->metadata_cache->version) {
2601 metadata_stream_reset_cache_consumed_position(stream);
2602 consumer_stream_metadata_set_version(stream,
2603 stream->chan->metadata_cache->version);
2604 } else {
2605 ret = 0;
2606 goto end;
2607 }
2608 }
2609
2610 write_len = lttng_ust_ctl_write_one_packet_to_channel(stream->chan->uchan,
2611 &stream->chan->metadata_cache->contents.data[stream->ust_metadata_pushed],
2612 stream->chan->metadata_cache->contents.size -
2613 stream->ust_metadata_pushed);
2614 LTTNG_ASSERT(write_len != 0);
2615 if (write_len < 0) {
2616 ERR("Writing one metadata packet");
2617 ret = write_len;
2618 goto end;
2619 }
2620 stream->ust_metadata_pushed += write_len;
2621
2622 LTTNG_ASSERT(stream->chan->metadata_cache->contents.size >=
2623 stream->ust_metadata_pushed);
2624 ret = write_len;
2625
2626 /*
2627 * Switch packet (but don't open the next one) on every commit of
2628 * a metadata packet. Since the subbuffer is fully filled (with padding,
2629 * if needed), the stream is "quiescent" after this commit.
2630 */
2631 if (lttng_ust_ctl_flush_buffer(stream->ustream, 1)) {
2632 ERR("Failed to flush buffer while committing one metadata packet");
2633 ret = -EIO;
2634 } else {
2635 stream->quiescent = true;
2636 }
2637 end:
2638 pthread_mutex_unlock(&stream->chan->metadata_cache->lock);
2639 return ret;
2640 }
2641
2642
2643 /*
2644 * Sync metadata meaning request them to the session daemon and snapshot to the
2645 * metadata thread can consumer them.
2646 *
2647 * Metadata stream lock is held here, but we need to release it when
2648 * interacting with sessiond, else we cause a deadlock with live
2649 * awaiting on metadata to be pushed out.
2650 *
2651 * The RCU read side lock must be held by the caller.
2652 */
2653 enum sync_metadata_status lttng_ustconsumer_sync_metadata(
2654 struct lttng_consumer_local_data *ctx,
2655 struct lttng_consumer_stream *metadata_stream)
2656 {
2657 int ret;
2658 enum sync_metadata_status status;
2659 struct lttng_consumer_channel *metadata_channel;
2660
2661 LTTNG_ASSERT(ctx);
2662 LTTNG_ASSERT(metadata_stream);
2663 ASSERT_RCU_READ_LOCKED();
2664
2665 metadata_channel = metadata_stream->chan;
2666 pthread_mutex_unlock(&metadata_stream->lock);
2667 /*
2668 * Request metadata from the sessiond, but don't wait for the flush
2669 * because we locked the metadata thread.
2670 */
2671 ret = lttng_ustconsumer_request_metadata(ctx, metadata_channel, 0, 0);
2672 pthread_mutex_lock(&metadata_stream->lock);
2673 if (ret < 0) {
2674 status = SYNC_METADATA_STATUS_ERROR;
2675 goto end;
2676 }
2677
2678 /*
2679 * The metadata stream and channel can be deleted while the
2680 * metadata stream lock was released. The streamed is checked
2681 * for deletion before we use it further.
2682 *
2683 * Note that it is safe to access a logically-deleted stream since its
2684 * existence is still guaranteed by the RCU read side lock. However,
2685 * it should no longer be used. The close/deletion of the metadata
2686 * channel and stream already guarantees that all metadata has been
2687 * consumed. Therefore, there is nothing left to do in this function.
2688 */
2689 if (consumer_stream_is_deleted(metadata_stream)) {
2690 DBG("Metadata stream %" PRIu64 " was deleted during the metadata synchronization",
2691 metadata_stream->key);
2692 status = SYNC_METADATA_STATUS_NO_DATA;
2693 goto end;
2694 }
2695
2696 ret = commit_one_metadata_packet(metadata_stream);
2697 if (ret < 0) {
2698 status = SYNC_METADATA_STATUS_ERROR;
2699 goto end;
2700 } else if (ret > 0) {
2701 status = SYNC_METADATA_STATUS_NEW_DATA;
2702 } else /* ret == 0 */ {
2703 status = SYNC_METADATA_STATUS_NO_DATA;
2704 goto end;
2705 }
2706
2707 ret = lttng_ust_ctl_snapshot(metadata_stream->ustream);
2708 if (ret < 0) {
2709 ERR("Failed to take a snapshot of the metadata ring-buffer positions, ret = %d", ret);
2710 status = SYNC_METADATA_STATUS_ERROR;
2711 goto end;
2712 }
2713
2714 end:
2715 return status;
2716 }
2717
2718 /*
2719 * Return 0 on success else a negative value.
2720 */
2721 static int notify_if_more_data(struct lttng_consumer_stream *stream,
2722 struct lttng_consumer_local_data *ctx)
2723 {
2724 int ret;
2725 struct lttng_ust_ctl_consumer_stream *ustream;
2726
2727 LTTNG_ASSERT(stream);
2728 LTTNG_ASSERT(ctx);
2729
2730 ustream = stream->ustream;
2731
2732 /*
2733 * First, we are going to check if there is a new subbuffer available
2734 * before reading the stream wait_fd.
2735 */
2736 /* Get the next subbuffer */
2737 ret = lttng_ust_ctl_get_next_subbuf(ustream);
2738 if (ret) {
2739 /* No more data found, flag the stream. */
2740 stream->has_data = 0;
2741 ret = 0;
2742 goto end;
2743 }
2744
2745 ret = lttng_ust_ctl_put_subbuf(ustream);
2746 LTTNG_ASSERT(!ret);
2747
2748 /* This stream still has data. Flag it and wake up the data thread. */
2749 stream->has_data = 1;
2750
2751 if (stream->monitor && !stream->hangup_flush_done && !ctx->has_wakeup) {
2752 ssize_t writelen;
2753
2754 writelen = lttng_pipe_write(ctx->consumer_wakeup_pipe, "!", 1);
2755 if (writelen < 0 && errno != EAGAIN && errno != EWOULDBLOCK) {
2756 ret = writelen;
2757 goto end;
2758 }
2759
2760 /* The wake up pipe has been notified. */
2761 ctx->has_wakeup = 1;
2762 }
2763 ret = 0;
2764
2765 end:
2766 return ret;
2767 }
2768
2769 static int consumer_stream_ust_on_wake_up(struct lttng_consumer_stream *stream)
2770 {
2771 int ret = 0;
2772
2773 /*
2774 * We can consume the 1 byte written into the wait_fd by
2775 * UST. Don't trigger error if we cannot read this one byte
2776 * (read returns 0), or if the error is EAGAIN or EWOULDBLOCK.
2777 *
2778 * This is only done when the stream is monitored by a thread,
2779 * before the flush is done after a hangup and if the stream
2780 * is not flagged with data since there might be nothing to
2781 * consume in the wait fd but still have data available
2782 * flagged by the consumer wake up pipe.
2783 */
2784 if (stream->monitor && !stream->hangup_flush_done && !stream->has_data) {
2785 char dummy;
2786 ssize_t readlen;
2787
2788 readlen = lttng_read(stream->wait_fd, &dummy, 1);
2789 if (readlen < 0 && errno != EAGAIN && errno != EWOULDBLOCK) {
2790 ret = readlen;
2791 }
2792 }
2793
2794 return ret;
2795 }
2796
2797 static int extract_common_subbuffer_info(struct lttng_consumer_stream *stream,
2798 struct stream_subbuffer *subbuf)
2799 {
2800 int ret;
2801
2802 ret = lttng_ust_ctl_get_subbuf_size(
2803 stream->ustream, &subbuf->info.data.subbuf_size);
2804 if (ret) {
2805 goto end;
2806 }
2807
2808 ret = lttng_ust_ctl_get_padded_subbuf_size(
2809 stream->ustream, &subbuf->info.data.padded_subbuf_size);
2810 if (ret) {
2811 goto end;
2812 }
2813
2814 end:
2815 return ret;
2816 }
2817
2818 static int extract_metadata_subbuffer_info(struct lttng_consumer_stream *stream,
2819 struct stream_subbuffer *subbuf)
2820 {
2821 int ret;
2822
2823 ret = extract_common_subbuffer_info(stream, subbuf);
2824 if (ret) {
2825 goto end;
2826 }
2827
2828 subbuf->info.metadata.version = stream->metadata_version;
2829
2830 end:
2831 return ret;
2832 }
2833
2834 static int extract_data_subbuffer_info(struct lttng_consumer_stream *stream,
2835 struct stream_subbuffer *subbuf)
2836 {
2837 int ret;
2838
2839 ret = extract_common_subbuffer_info(stream, subbuf);
2840 if (ret) {
2841 goto end;
2842 }
2843
2844 ret = lttng_ust_ctl_get_packet_size(
2845 stream->ustream, &subbuf->info.data.packet_size);
2846 if (ret < 0) {
2847 PERROR("Failed to get sub-buffer packet size");
2848 goto end;
2849 }
2850
2851 ret = lttng_ust_ctl_get_content_size(
2852 stream->ustream, &subbuf->info.data.content_size);
2853 if (ret < 0) {
2854 PERROR("Failed to get sub-buffer content size");
2855 goto end;
2856 }
2857
2858 ret = lttng_ust_ctl_get_timestamp_begin(
2859 stream->ustream, &subbuf->info.data.timestamp_begin);
2860 if (ret < 0) {
2861 PERROR("Failed to get sub-buffer begin timestamp");
2862 goto end;
2863 }
2864
2865 ret = lttng_ust_ctl_get_timestamp_end(
2866 stream->ustream, &subbuf->info.data.timestamp_end);
2867 if (ret < 0) {
2868 PERROR("Failed to get sub-buffer end timestamp");
2869 goto end;
2870 }
2871
2872 ret = lttng_ust_ctl_get_events_discarded(
2873 stream->ustream, &subbuf->info.data.events_discarded);
2874 if (ret) {
2875 PERROR("Failed to get sub-buffer events discarded count");
2876 goto end;
2877 }
2878
2879 ret = lttng_ust_ctl_get_sequence_number(stream->ustream,
2880 &subbuf->info.data.sequence_number.value);
2881 if (ret) {
2882 /* May not be supported by older LTTng-modules. */
2883 if (ret != -ENOTTY) {
2884 PERROR("Failed to get sub-buffer sequence number");
2885 goto end;
2886 }
2887 } else {
2888 subbuf->info.data.sequence_number.is_set = true;
2889 }
2890
2891 ret = lttng_ust_ctl_get_stream_id(
2892 stream->ustream, &subbuf->info.data.stream_id);
2893 if (ret < 0) {
2894 PERROR("Failed to get stream id");
2895 goto end;
2896 }
2897
2898 ret = lttng_ust_ctl_get_instance_id(stream->ustream,
2899 &subbuf->info.data.stream_instance_id.value);
2900 if (ret) {
2901 /* May not be supported by older LTTng-modules. */
2902 if (ret != -ENOTTY) {
2903 PERROR("Failed to get stream instance id");
2904 goto end;
2905 }
2906 } else {
2907 subbuf->info.data.stream_instance_id.is_set = true;
2908 }
2909 end:
2910 return ret;
2911 }
2912
2913 static int get_next_subbuffer_common(struct lttng_consumer_stream *stream,
2914 struct stream_subbuffer *subbuffer)
2915 {
2916 int ret;
2917 const char *addr;
2918
2919 ret = stream->read_subbuffer_ops.extract_subbuffer_info(
2920 stream, subbuffer);
2921 if (ret) {
2922 goto end;
2923 }
2924
2925 ret = get_current_subbuf_addr(stream, &addr);
2926 if (ret) {
2927 goto end;
2928 }
2929
2930 subbuffer->buffer.buffer = lttng_buffer_view_init(
2931 addr, 0, subbuffer->info.data.padded_subbuf_size);
2932 LTTNG_ASSERT(subbuffer->buffer.buffer.data != NULL);
2933 end:
2934 return ret;
2935 }
2936
2937 static enum get_next_subbuffer_status get_next_subbuffer(
2938 struct lttng_consumer_stream *stream,
2939 struct stream_subbuffer *subbuffer)
2940 {
2941 int ret;
2942 enum get_next_subbuffer_status status;
2943
2944 ret = lttng_ust_ctl_get_next_subbuf(stream->ustream);
2945 switch (ret) {
2946 case 0:
2947 status = GET_NEXT_SUBBUFFER_STATUS_OK;
2948 break;
2949 case -ENODATA:
2950 case -EAGAIN:
2951 /*
2952 * The caller only expects -ENODATA when there is no data to
2953 * read, but the kernel tracer returns -EAGAIN when there is
2954 * currently no data for a non-finalized stream, and -ENODATA
2955 * when there is no data for a finalized stream. Those can be
2956 * combined into a -ENODATA return value.
2957 */
2958 status = GET_NEXT_SUBBUFFER_STATUS_NO_DATA;
2959 goto end;
2960 default:
2961 status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
2962 goto end;
2963 }
2964
2965 ret = get_next_subbuffer_common(stream, subbuffer);
2966 if (ret) {
2967 status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
2968 goto end;
2969 }
2970 end:
2971 return status;
2972 }
2973
2974 static enum get_next_subbuffer_status get_next_subbuffer_metadata(
2975 struct lttng_consumer_stream *stream,
2976 struct stream_subbuffer *subbuffer)
2977 {
2978 int ret;
2979 bool cache_empty;
2980 bool got_subbuffer;
2981 bool coherent;
2982 bool buffer_empty;
2983 unsigned long consumed_pos, produced_pos;
2984 enum get_next_subbuffer_status status;
2985
2986 do {
2987 ret = lttng_ust_ctl_get_next_subbuf(stream->ustream);
2988 if (ret == 0) {
2989 got_subbuffer = true;
2990 } else {
2991 got_subbuffer = false;
2992 if (ret != -EAGAIN) {
2993 /* Fatal error. */
2994 status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
2995 goto end;
2996 }
2997 }
2998
2999 /*
3000 * Determine if the cache is empty and ensure that a sub-buffer
3001 * is made available if the cache is not empty.
3002 */
3003 if (!got_subbuffer) {
3004 ret = commit_one_metadata_packet(stream);
3005 if (ret < 0 && ret != -ENOBUFS) {
3006 status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
3007 goto end;
3008 } else if (ret == 0) {
3009 /* Not an error, the cache is empty. */
3010 cache_empty = true;
3011 status = GET_NEXT_SUBBUFFER_STATUS_NO_DATA;
3012 goto end;
3013 } else {
3014 cache_empty = false;
3015 }
3016 } else {
3017 pthread_mutex_lock(&stream->chan->metadata_cache->lock);
3018 cache_empty = stream->chan->metadata_cache->contents.size ==
3019 stream->ust_metadata_pushed;
3020 pthread_mutex_unlock(&stream->chan->metadata_cache->lock);
3021 }
3022 } while (!got_subbuffer);
3023
3024 /* Populate sub-buffer infos and view. */
3025 ret = get_next_subbuffer_common(stream, subbuffer);
3026 if (ret) {
3027 status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
3028 goto end;
3029 }
3030
3031 ret = lttng_ustconsumer_sample_snapshot_positions(stream);
3032 if (ret < 0) {
3033 /*
3034 * -EAGAIN is not expected since we got a sub-buffer and haven't
3035 * pushed the consumption position yet (on put_next).
3036 */
3037 PERROR("Failed to take a snapshot of metadata buffer positions");
3038 status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
3039 goto end;
3040 }
3041
3042 ret = lttng_ustconsumer_get_consumed_snapshot(stream, &consumed_pos);
3043 if (ret) {
3044 PERROR("Failed to get metadata consumed position");
3045 status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
3046 goto end;
3047 }
3048
3049 ret = lttng_ustconsumer_get_produced_snapshot(stream, &produced_pos);
3050 if (ret) {
3051 PERROR("Failed to get metadata produced position");
3052 status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
3053 goto end;
3054 }
3055
3056 /* Last sub-buffer of the ring buffer ? */
3057 buffer_empty = (consumed_pos + stream->max_sb_size) == produced_pos;
3058
3059 /*
3060 * The sessiond registry lock ensures that coherent units of metadata
3061 * are pushed to the consumer daemon at once. Hence, if a sub-buffer is
3062 * acquired, the cache is empty, and it is the only available sub-buffer
3063 * available, it is safe to assume that it is "coherent".
3064 */
3065 coherent = got_subbuffer && cache_empty && buffer_empty;
3066
3067 LTTNG_OPTIONAL_SET(&subbuffer->info.metadata.coherent, coherent);
3068 status = GET_NEXT_SUBBUFFER_STATUS_OK;
3069 end:
3070 return status;
3071 }
3072
3073 static int put_next_subbuffer(struct lttng_consumer_stream *stream,
3074 struct stream_subbuffer *subbuffer)
3075 {
3076 const int ret = lttng_ust_ctl_put_next_subbuf(stream->ustream);
3077
3078 LTTNG_ASSERT(ret == 0);
3079 return ret;
3080 }
3081
3082 static int signal_metadata(struct lttng_consumer_stream *stream,
3083 struct lttng_consumer_local_data *ctx)
3084 {
3085 ASSERT_LOCKED(stream->metadata_rdv_lock);
3086 return pthread_cond_broadcast(&stream->metadata_rdv) ? -errno : 0;
3087 }
3088
3089 static int lttng_ustconsumer_set_stream_ops(
3090 struct lttng_consumer_stream *stream)
3091 {
3092 int ret = 0;
3093
3094 stream->read_subbuffer_ops.on_wake_up = consumer_stream_ust_on_wake_up;
3095 if (stream->metadata_flag) {
3096 stream->read_subbuffer_ops.get_next_subbuffer =
3097 get_next_subbuffer_metadata;
3098 stream->read_subbuffer_ops.extract_subbuffer_info =
3099 extract_metadata_subbuffer_info;
3100 stream->read_subbuffer_ops.reset_metadata =
3101 metadata_stream_reset_cache_consumed_position;
3102 if (stream->chan->is_live) {
3103 stream->read_subbuffer_ops.on_sleep = signal_metadata;
3104 ret = consumer_stream_enable_metadata_bucketization(
3105 stream);
3106 if (ret) {
3107 goto end;
3108 }
3109 }
3110 } else {
3111 stream->read_subbuffer_ops.get_next_subbuffer =
3112 get_next_subbuffer;
3113 stream->read_subbuffer_ops.extract_subbuffer_info =
3114 extract_data_subbuffer_info;
3115 stream->read_subbuffer_ops.on_sleep = notify_if_more_data;
3116 if (stream->chan->is_live) {
3117 stream->read_subbuffer_ops.send_live_beacon =
3118 consumer_flush_ust_index;
3119 }
3120 }
3121
3122 stream->read_subbuffer_ops.put_next_subbuffer = put_next_subbuffer;
3123 end:
3124 return ret;
3125 }
3126
3127 /*
3128 * Called when a stream is created.
3129 *
3130 * Return 0 on success or else a negative value.
3131 */
3132 int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream *stream)
3133 {
3134 int ret;
3135
3136 LTTNG_ASSERT(stream);
3137
3138 /*
3139 * Don't create anything if this is set for streaming or if there is
3140 * no current trace chunk on the parent channel.
3141 */
3142 if (stream->net_seq_idx == (uint64_t) -1ULL && stream->chan->monitor &&
3143 stream->chan->trace_chunk) {
3144 ret = consumer_stream_create_output_files(stream, true);
3145 if (ret) {
3146 goto error;
3147 }
3148 }
3149
3150 lttng_ustconsumer_set_stream_ops(stream);
3151 ret = 0;
3152
3153 error:
3154 return ret;
3155 }
3156
3157 /*
3158 * Check if data is still being extracted from the buffers for a specific
3159 * stream. Consumer data lock MUST be acquired before calling this function
3160 * and the stream lock.
3161 *
3162 * Return 1 if the traced data are still getting read else 0 meaning that the
3163 * data is available for trace viewer reading.
3164 */
3165 int lttng_ustconsumer_data_pending(struct lttng_consumer_stream *stream)
3166 {
3167 int ret;
3168
3169 LTTNG_ASSERT(stream);
3170 LTTNG_ASSERT(stream->ustream);
3171 ASSERT_LOCKED(stream->lock);
3172
3173 DBG("UST consumer checking data pending");
3174
3175 if (stream->endpoint_status != CONSUMER_ENDPOINT_ACTIVE) {
3176 ret = 0;
3177 goto end;
3178 }
3179
3180 if (stream->chan->type == CONSUMER_CHANNEL_TYPE_METADATA) {
3181 uint64_t contiguous, pushed;
3182
3183 /* Ease our life a bit. */
3184 pthread_mutex_lock(&stream->chan->metadata_cache->lock);
3185 contiguous = stream->chan->metadata_cache->contents.size;
3186 pthread_mutex_unlock(&stream->chan->metadata_cache->lock);
3187 pushed = stream->ust_metadata_pushed;
3188
3189 /*
3190 * We can simply check whether all contiguously available data
3191 * has been pushed to the ring buffer, since the push operation
3192 * is performed within get_next_subbuf(), and because both
3193 * get_next_subbuf() and put_next_subbuf() are issued atomically
3194 * thanks to the stream lock within
3195 * lttng_ustconsumer_read_subbuffer(). This basically means that
3196 * whetnever ust_metadata_pushed is incremented, the associated
3197 * metadata has been consumed from the metadata stream.
3198 */
3199 DBG("UST consumer metadata pending check: contiguous %" PRIu64 " vs pushed %" PRIu64,
3200 contiguous, pushed);
3201 LTTNG_ASSERT(((int64_t) (contiguous - pushed)) >= 0);
3202 if ((contiguous != pushed) ||
3203 (((int64_t) contiguous - pushed) > 0 || contiguous == 0)) {
3204 ret = 1; /* Data is pending */
3205 goto end;
3206 }
3207 } else {
3208 ret = lttng_ust_ctl_get_next_subbuf(stream->ustream);
3209 if (ret == 0) {
3210 /*
3211 * There is still data so let's put back this
3212 * subbuffer.
3213 */
3214 ret = lttng_ust_ctl_put_subbuf(stream->ustream);
3215 LTTNG_ASSERT(ret == 0);
3216 ret = 1; /* Data is pending */
3217 goto end;
3218 }
3219 }
3220
3221 /* Data is NOT pending so ready to be read. */
3222 ret = 0;
3223
3224 end:
3225 return ret;
3226 }
3227
3228 /*
3229 * Stop a given metadata channel timer if enabled and close the wait fd which
3230 * is the poll pipe of the metadata stream.
3231 *
3232 * This MUST be called with the metadata channel lock acquired.
3233 */
3234 void lttng_ustconsumer_close_metadata(struct lttng_consumer_channel *metadata)
3235 {
3236 int ret;
3237
3238 LTTNG_ASSERT(metadata);
3239 LTTNG_ASSERT(metadata->type == CONSUMER_CHANNEL_TYPE_METADATA);
3240
3241 DBG("Closing metadata channel key %" PRIu64, metadata->key);
3242
3243 if (metadata->switch_timer_enabled == 1) {
3244 consumer_timer_switch_stop(metadata);
3245 }
3246
3247 if (!metadata->metadata_stream) {
3248 goto end;
3249 }
3250
3251 /*
3252 * Closing write side so the thread monitoring the stream wakes up if any
3253 * and clean the metadata stream.
3254 */
3255 if (metadata->metadata_stream->ust_metadata_poll_pipe[1] >= 0) {
3256 ret = close(metadata->metadata_stream->ust_metadata_poll_pipe[1]);
3257 if (ret < 0) {
3258 PERROR("closing metadata pipe write side");
3259 }
3260 metadata->metadata_stream->ust_metadata_poll_pipe[1] = -1;
3261 }
3262
3263 end:
3264 return;
3265 }
3266
3267 /*
3268 * Close every metadata stream wait fd of the metadata hash table. This
3269 * function MUST be used very carefully so not to run into a race between the
3270 * metadata thread handling streams and this function closing their wait fd.
3271 *
3272 * For UST, this is used when the session daemon hangs up. Its the metadata
3273 * producer so calling this is safe because we are assured that no state change
3274 * can occur in the metadata thread for the streams in the hash table.
3275 */
3276 void lttng_ustconsumer_close_all_metadata(struct lttng_ht *metadata_ht)
3277 {
3278 struct lttng_ht_iter iter;
3279 struct lttng_consumer_stream *stream;
3280
3281 LTTNG_ASSERT(metadata_ht);
3282 LTTNG_ASSERT(metadata_ht->ht);
3283
3284 DBG("UST consumer closing all metadata streams");
3285
3286 rcu_read_lock();
3287 cds_lfht_for_each_entry(metadata_ht->ht, &iter.iter, stream,
3288 node.node) {
3289
3290 health_code_update();
3291
3292 pthread_mutex_lock(&stream->chan->lock);
3293 lttng_ustconsumer_close_metadata(stream->chan);
3294 pthread_mutex_unlock(&stream->chan->lock);
3295
3296 }
3297 rcu_read_unlock();
3298 }
3299
3300 void lttng_ustconsumer_close_stream_wakeup(struct lttng_consumer_stream *stream)
3301 {
3302 int ret;
3303
3304 ret = lttng_ust_ctl_stream_close_wakeup_fd(stream->ustream);
3305 if (ret < 0) {
3306 ERR("Unable to close wakeup fd");
3307 }
3308 }
3309
3310 /*
3311 * Please refer to consumer-timer.c before adding any lock within this
3312 * function or any of its callees. Timers have a very strict locking
3313 * semantic with respect to teardown. Failure to respect this semantic
3314 * introduces deadlocks.
3315 *
3316 * DON'T hold the metadata lock when calling this function, else this
3317 * can cause deadlock involving consumer awaiting for metadata to be
3318 * pushed out due to concurrent interaction with the session daemon.
3319 */
3320 int lttng_ustconsumer_request_metadata(struct lttng_consumer_local_data *ctx,
3321 struct lttng_consumer_channel *channel, int timer, int wait)
3322 {
3323 struct lttcomm_metadata_request_msg request;
3324 struct lttcomm_consumer_msg msg;
3325 enum lttcomm_return_code ret_code = LTTCOMM_CONSUMERD_SUCCESS;
3326 uint64_t len, key, offset, version;
3327 int ret;
3328
3329 LTTNG_ASSERT(channel);
3330 LTTNG_ASSERT(channel->metadata_cache);
3331
3332 memset(&request, 0, sizeof(request));
3333
3334 /* send the metadata request to sessiond */
3335 switch (the_consumer_data.type) {
3336 case LTTNG_CONSUMER64_UST:
3337 request.bits_per_long = 64;
3338 break;
3339 case LTTNG_CONSUMER32_UST:
3340 request.bits_per_long = 32;
3341 break;
3342 default:
3343 request.bits_per_long = 0;
3344 break;
3345 }
3346
3347 request.session_id = channel->session_id;
3348 request.session_id_per_pid = channel->session_id_per_pid;
3349 /*
3350 * Request the application UID here so the metadata of that application can
3351 * be sent back. The channel UID corresponds to the user UID of the session
3352 * used for the rights on the stream file(s).
3353 */
3354 request.uid = channel->ust_app_uid;
3355 request.key = channel->key;
3356
3357 DBG("Sending metadata request to sessiond, session id %" PRIu64
3358 ", per-pid %" PRIu64 ", app UID %u and channel key %" PRIu64,
3359 request.session_id, request.session_id_per_pid, request.uid,
3360 request.key);
3361
3362 pthread_mutex_lock(&ctx->metadata_socket_lock);
3363
3364 health_code_update();
3365
3366 ret = lttcomm_send_unix_sock(ctx->consumer_metadata_socket, &request,
3367 sizeof(request));
3368 if (ret < 0) {
3369 ERR("Asking metadata to sessiond");
3370 goto end;
3371 }
3372
3373 health_code_update();
3374
3375 /* Receive the metadata from sessiond */
3376 ret = lttcomm_recv_unix_sock(ctx->consumer_metadata_socket, &msg,
3377 sizeof(msg));
3378 if (ret != sizeof(msg)) {
3379 DBG("Consumer received unexpected message size %d (expects %zu)",
3380 ret, sizeof(msg));
3381 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_CMD);
3382 /*
3383 * The ret value might 0 meaning an orderly shutdown but this is ok
3384 * since the caller handles this.
3385 */
3386 goto end;
3387 }
3388
3389 health_code_update();
3390
3391 if (msg.cmd_type == LTTNG_ERR_UND) {
3392 /* No registry found */
3393 (void) consumer_send_status_msg(ctx->consumer_metadata_socket,
3394 ret_code);
3395 ret = 0;
3396 goto end;
3397 } else if (msg.cmd_type != LTTNG_CONSUMER_PUSH_METADATA) {
3398 ERR("Unexpected cmd_type received %d", msg.cmd_type);
3399 ret = -1;
3400 goto end;
3401 }
3402
3403 len = msg.u.push_metadata.len;
3404 key = msg.u.push_metadata.key;
3405 offset = msg.u.push_metadata.target_offset;
3406 version = msg.u.push_metadata.version;
3407
3408 LTTNG_ASSERT(key == channel->key);
3409 if (len == 0) {
3410 DBG("No new metadata to receive for key %" PRIu64, key);
3411 }
3412
3413 health_code_update();
3414
3415 /* Tell session daemon we are ready to receive the metadata. */
3416 ret = consumer_send_status_msg(ctx->consumer_metadata_socket,
3417 LTTCOMM_CONSUMERD_SUCCESS);
3418 if (ret < 0 || len == 0) {
3419 /*
3420 * Somehow, the session daemon is not responding anymore or there is
3421 * nothing to receive.
3422 */
3423 goto end;
3424 }
3425
3426 health_code_update();
3427
3428 ret = lttng_ustconsumer_recv_metadata(ctx->consumer_metadata_socket,
3429 key, offset, len, version, channel, timer, wait);
3430 if (ret >= 0) {
3431 /*
3432 * Only send the status msg if the sessiond is alive meaning a positive
3433 * ret code.
3434 */
3435 (void) consumer_send_status_msg(ctx->consumer_metadata_socket, ret);
3436 }
3437 ret = 0;
3438
3439 end:
3440 health_code_update();
3441
3442 pthread_mutex_unlock(&ctx->metadata_socket_lock);
3443 return ret;
3444 }
3445
3446 /*
3447 * Return the ustctl call for the get stream id.
3448 */
3449 int lttng_ustconsumer_get_stream_id(struct lttng_consumer_stream *stream,
3450 uint64_t *stream_id)
3451 {
3452 LTTNG_ASSERT(stream);
3453 LTTNG_ASSERT(stream_id);
3454
3455 return lttng_ust_ctl_get_stream_id(stream->ustream, stream_id);
3456 }
3457
3458 void lttng_ustconsumer_sigbus_handle(void *addr)
3459 {
3460 lttng_ust_ctl_sigbus_handle(addr);
3461 }
This page took 0.105075 seconds and 4 git commands to generate.