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