consumerd: tag metadata channel as being part of a live session
[lttng-tools.git] / src / common / ust-consumer / ust-consumer.c
1 /*
2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #include <stdint.h>
20 #define _LGPL_SOURCE
21 #include <assert.h>
22 #include <lttng/ust-ctl.h>
23 #include <poll.h>
24 #include <pthread.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/mman.h>
28 #include <sys/socket.h>
29 #include <sys/stat.h>
30 #include <sys/types.h>
31 #include <inttypes.h>
32 #include <unistd.h>
33 #include <urcu/list.h>
34 #include <signal.h>
35
36 #include <bin/lttng-consumerd/health-consumerd.h>
37 #include <common/common.h>
38 #include <common/sessiond-comm/sessiond-comm.h>
39 #include <common/relayd/relayd.h>
40 #include <common/compat/fcntl.h>
41 #include <common/compat/endian.h>
42 #include <common/consumer/consumer-metadata-cache.h>
43 #include <common/consumer/consumer-stream.h>
44 #include <common/consumer/consumer-timer.h>
45 #include <common/utils.h>
46 #include <common/index/index.h>
47
48 #include "ust-consumer.h"
49
50 #define INT_MAX_STR_LEN 12 /* includes \0 */
51
52 extern struct lttng_consumer_global_data consumer_data;
53 extern int consumer_poll_timeout;
54 extern volatile int consumer_quit;
55
56 /*
57 * Free channel object and all streams associated with it. This MUST be used
58 * only and only if the channel has _NEVER_ been added to the global channel
59 * hash table.
60 */
61 static void destroy_channel(struct lttng_consumer_channel *channel)
62 {
63 struct lttng_consumer_stream *stream, *stmp;
64
65 assert(channel);
66
67 DBG("UST consumer cleaning stream list");
68
69 cds_list_for_each_entry_safe(stream, stmp, &channel->streams.head,
70 send_node) {
71
72 health_code_update();
73
74 cds_list_del(&stream->send_node);
75 ustctl_destroy_stream(stream->ustream);
76 free(stream);
77 }
78
79 /*
80 * If a channel is available meaning that was created before the streams
81 * were, delete it.
82 */
83 if (channel->uchan) {
84 lttng_ustconsumer_del_channel(channel);
85 lttng_ustconsumer_free_channel(channel);
86 }
87 free(channel);
88 }
89
90 /*
91 * Add channel to internal consumer state.
92 *
93 * Returns 0 on success or else a negative value.
94 */
95 static int add_channel(struct lttng_consumer_channel *channel,
96 struct lttng_consumer_local_data *ctx)
97 {
98 int ret = 0;
99
100 assert(channel);
101 assert(ctx);
102
103 if (ctx->on_recv_channel != NULL) {
104 ret = ctx->on_recv_channel(channel);
105 if (ret == 0) {
106 ret = consumer_add_channel(channel, ctx);
107 } else if (ret < 0) {
108 /* Most likely an ENOMEM. */
109 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
110 goto error;
111 }
112 } else {
113 ret = consumer_add_channel(channel, ctx);
114 }
115
116 DBG("UST consumer channel added (key: %" PRIu64 ")", channel->key);
117
118 error:
119 return ret;
120 }
121
122 /*
123 * Allocate and return a consumer stream object. If _alloc_ret is not NULL, the
124 * error value if applicable is set in it else it is kept untouched.
125 *
126 * Return NULL on error else the newly allocated stream object.
127 */
128 static struct lttng_consumer_stream *allocate_stream(int cpu, int key,
129 struct lttng_consumer_channel *channel,
130 struct lttng_consumer_local_data *ctx, int *_alloc_ret)
131 {
132 int alloc_ret;
133 struct lttng_consumer_stream *stream = NULL;
134
135 assert(channel);
136 assert(ctx);
137
138 stream = consumer_allocate_stream(
139 channel,
140 channel->key,
141 key,
142 LTTNG_CONSUMER_ACTIVE_STREAM,
143 channel->name,
144 channel->uid,
145 channel->gid,
146 channel->relayd_id,
147 channel->session_id,
148 cpu,
149 &alloc_ret,
150 channel->type,
151 channel->monitor);
152 if (stream == NULL) {
153 switch (alloc_ret) {
154 case -ENOENT:
155 /*
156 * We could not find the channel. Can happen if cpu hotplug
157 * happens while tearing down.
158 */
159 DBG3("Could not find channel");
160 break;
161 case -ENOMEM:
162 case -EINVAL:
163 default:
164 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
165 break;
166 }
167 goto error;
168 }
169
170 error:
171 if (_alloc_ret) {
172 *_alloc_ret = alloc_ret;
173 }
174 return stream;
175 }
176
177 /*
178 * Send the given stream pointer to the corresponding thread.
179 *
180 * Returns 0 on success else a negative value.
181 */
182 static int send_stream_to_thread(struct lttng_consumer_stream *stream,
183 struct lttng_consumer_local_data *ctx)
184 {
185 int ret;
186 struct lttng_pipe *stream_pipe;
187
188 /* Get the right pipe where the stream will be sent. */
189 if (stream->metadata_flag) {
190 ret = consumer_add_metadata_stream(stream);
191 if (ret) {
192 ERR("Consumer add metadata stream %" PRIu64 " failed.",
193 stream->key);
194 goto error;
195 }
196 stream_pipe = ctx->consumer_metadata_pipe;
197 } else {
198 ret = consumer_add_data_stream(stream);
199 if (ret) {
200 ERR("Consumer add stream %" PRIu64 " failed.",
201 stream->key);
202 goto error;
203 }
204 stream_pipe = ctx->consumer_data_pipe;
205 }
206
207 /*
208 * From this point on, the stream's ownership has been moved away from
209 * the channel and becomes globally visible.
210 */
211 stream->globally_visible = 1;
212
213 ret = lttng_pipe_write(stream_pipe, &stream, sizeof(stream));
214 if (ret < 0) {
215 ERR("Consumer write %s stream to pipe %d",
216 stream->metadata_flag ? "metadata" : "data",
217 lttng_pipe_get_writefd(stream_pipe));
218 if (stream->metadata_flag) {
219 consumer_del_stream_for_metadata(stream);
220 } else {
221 consumer_del_stream_for_data(stream);
222 }
223 }
224 error:
225 return ret;
226 }
227
228 static
229 int get_stream_shm_path(char *stream_shm_path, const char *shm_path, int cpu)
230 {
231 char cpu_nr[INT_MAX_STR_LEN]; /* int max len */
232 int ret;
233
234 strncpy(stream_shm_path, shm_path, PATH_MAX);
235 stream_shm_path[PATH_MAX - 1] = '\0';
236 ret = snprintf(cpu_nr, INT_MAX_STR_LEN, "%i", cpu);
237 if (ret < 0) {
238 PERROR("snprintf");
239 goto end;
240 }
241 strncat(stream_shm_path, cpu_nr,
242 PATH_MAX - strlen(stream_shm_path) - 1);
243 ret = 0;
244 end:
245 return ret;
246 }
247
248 /*
249 * Create streams for the given channel using liblttng-ust-ctl.
250 *
251 * Return 0 on success else a negative value.
252 */
253 static int create_ust_streams(struct lttng_consumer_channel *channel,
254 struct lttng_consumer_local_data *ctx)
255 {
256 int ret, cpu = 0;
257 struct ustctl_consumer_stream *ustream;
258 struct lttng_consumer_stream *stream;
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 = ustctl_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 = ustctl_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 /*
306 * Order is important this is why a list is used. On error, the caller
307 * should clean this list.
308 */
309 cds_list_add_tail(&stream->send_node, &channel->streams.head);
310
311 ret = ustctl_get_max_subbuf_size(stream->ustream,
312 &stream->max_sb_size);
313 if (ret < 0) {
314 ERR("ustctl_get_max_subbuf_size failed for stream %s",
315 stream->name);
316 goto error;
317 }
318
319 /* Do actions once stream has been received. */
320 if (ctx->on_recv_stream) {
321 ret = ctx->on_recv_stream(stream);
322 if (ret < 0) {
323 goto error;
324 }
325 }
326
327 DBG("UST consumer add stream %s (key: %" PRIu64 ") with relayd id %" PRIu64,
328 stream->name, stream->key, stream->relayd_stream_id);
329
330 /* Set next CPU stream. */
331 channel->streams.count = ++cpu;
332
333 /* Keep stream reference when creating metadata. */
334 if (channel->type == CONSUMER_CHANNEL_TYPE_METADATA) {
335 channel->metadata_stream = stream;
336 if (channel->monitor) {
337 /* Set metadata poll pipe if we created one */
338 memcpy(stream->ust_metadata_poll_pipe,
339 ust_metadata_pipe,
340 sizeof(ust_metadata_pipe));
341 }
342 }
343 }
344
345 return 0;
346
347 error:
348 error_alloc:
349 return ret;
350 }
351
352 /*
353 * create_posix_shm is never called concurrently within a process.
354 */
355 static
356 int create_posix_shm(void)
357 {
358 char tmp_name[NAME_MAX];
359 int shmfd, ret;
360
361 ret = snprintf(tmp_name, NAME_MAX, "/ust-shm-consumer-%d", getpid());
362 if (ret < 0) {
363 PERROR("snprintf");
364 return -1;
365 }
366 /*
367 * Allocate shm, and immediately unlink its shm oject, keeping
368 * only the file descriptor as a reference to the object.
369 * We specifically do _not_ use the / at the beginning of the
370 * pathname so that some OS implementations can keep it local to
371 * the process (POSIX leaves this implementation-defined).
372 */
373 shmfd = shm_open(tmp_name, O_CREAT | O_EXCL | O_RDWR, 0700);
374 if (shmfd < 0) {
375 PERROR("shm_open");
376 goto error_shm_open;
377 }
378 ret = shm_unlink(tmp_name);
379 if (ret < 0 && errno != ENOENT) {
380 PERROR("shm_unlink");
381 goto error_shm_release;
382 }
383 return shmfd;
384
385 error_shm_release:
386 ret = close(shmfd);
387 if (ret) {
388 PERROR("close");
389 }
390 error_shm_open:
391 return -1;
392 }
393
394 static int open_ust_stream_fd(struct lttng_consumer_channel *channel,
395 struct ustctl_consumer_channel_attr *attr,
396 int cpu)
397 {
398 char shm_path[PATH_MAX];
399 int ret;
400
401 if (!channel->shm_path[0]) {
402 return create_posix_shm();
403 }
404 ret = get_stream_shm_path(shm_path, channel->shm_path, cpu);
405 if (ret) {
406 goto error_shm_path;
407 }
408 return run_as_open(shm_path,
409 O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR,
410 channel->uid, channel->gid);
411
412 error_shm_path:
413 return -1;
414 }
415
416 /*
417 * Create an UST channel with the given attributes and send it to the session
418 * daemon using the ust ctl API.
419 *
420 * Return 0 on success or else a negative value.
421 */
422 static int create_ust_channel(struct lttng_consumer_channel *channel,
423 struct ustctl_consumer_channel_attr *attr,
424 struct ustctl_consumer_channel **ust_chanp)
425 {
426 int ret, nr_stream_fds, i, j;
427 int *stream_fds;
428 struct ustctl_consumer_channel *ust_channel;
429
430 assert(channel);
431 assert(attr);
432 assert(ust_chanp);
433
434 DBG3("Creating channel to ustctl with attr: [overwrite: %d, "
435 "subbuf_size: %" PRIu64 ", num_subbuf: %" PRIu64 ", "
436 "switch_timer_interval: %u, read_timer_interval: %u, "
437 "output: %d, type: %d", attr->overwrite, attr->subbuf_size,
438 attr->num_subbuf, attr->switch_timer_interval,
439 attr->read_timer_interval, attr->output, attr->type);
440
441 if (channel->type == CONSUMER_CHANNEL_TYPE_METADATA)
442 nr_stream_fds = 1;
443 else
444 nr_stream_fds = ustctl_get_nr_stream_per_channel();
445 stream_fds = zmalloc(nr_stream_fds * sizeof(*stream_fds));
446 if (!stream_fds) {
447 ret = -1;
448 goto error_alloc;
449 }
450 for (i = 0; i < nr_stream_fds; i++) {
451 stream_fds[i] = open_ust_stream_fd(channel, attr, i);
452 if (stream_fds[i] < 0) {
453 ret = -1;
454 goto error_open;
455 }
456 }
457 ust_channel = ustctl_create_channel(attr, stream_fds, nr_stream_fds);
458 if (!ust_channel) {
459 ret = -1;
460 goto error_create;
461 }
462 channel->nr_stream_fds = nr_stream_fds;
463 channel->stream_fds = stream_fds;
464 *ust_chanp = ust_channel;
465
466 return 0;
467
468 error_create:
469 error_open:
470 for (j = i - 1; j >= 0; j--) {
471 int closeret;
472
473 closeret = close(stream_fds[j]);
474 if (closeret) {
475 PERROR("close");
476 }
477 if (channel->shm_path[0]) {
478 char shm_path[PATH_MAX];
479
480 closeret = get_stream_shm_path(shm_path,
481 channel->shm_path, j);
482 if (closeret) {
483 ERR("Cannot get stream shm path");
484 }
485 closeret = run_as_unlink(shm_path,
486 channel->uid, channel->gid);
487 if (closeret) {
488 PERROR("unlink %s", shm_path);
489 }
490 }
491 }
492 /* Try to rmdir all directories under shm_path root. */
493 if (channel->root_shm_path[0]) {
494 (void) run_as_recursive_rmdir(channel->root_shm_path,
495 channel->uid, channel->gid);
496 }
497 free(stream_fds);
498 error_alloc:
499 return ret;
500 }
501
502 /*
503 * Send a single given stream to the session daemon using the sock.
504 *
505 * Return 0 on success else a negative value.
506 */
507 static int send_sessiond_stream(int sock, struct lttng_consumer_stream *stream)
508 {
509 int ret;
510
511 assert(stream);
512 assert(sock >= 0);
513
514 DBG("UST consumer sending stream %" PRIu64 " to sessiond", stream->key);
515
516 /* Send stream to session daemon. */
517 ret = ustctl_send_stream_to_sessiond(sock, stream->ustream);
518 if (ret < 0) {
519 goto error;
520 }
521
522 error:
523 return ret;
524 }
525
526 /*
527 * Send channel to sessiond.
528 *
529 * Return 0 on success or else a negative value.
530 */
531 static int send_sessiond_channel(int sock,
532 struct lttng_consumer_channel *channel,
533 struct lttng_consumer_local_data *ctx, int *relayd_error)
534 {
535 int ret, ret_code = LTTCOMM_CONSUMERD_SUCCESS;
536 struct lttng_consumer_stream *stream;
537 uint64_t relayd_id = -1ULL;
538
539 assert(channel);
540 assert(ctx);
541 assert(sock >= 0);
542
543 DBG("UST consumer sending channel %s to sessiond", channel->name);
544
545 if (channel->relayd_id != (uint64_t) -1ULL) {
546 cds_list_for_each_entry(stream, &channel->streams.head, send_node) {
547
548 health_code_update();
549
550 /* Try to send the stream to the relayd if one is available. */
551 ret = consumer_send_relayd_stream(stream, stream->chan->pathname);
552 if (ret < 0) {
553 /*
554 * Flag that the relayd was the problem here probably due to a
555 * communicaton error on the socket.
556 */
557 if (relayd_error) {
558 *relayd_error = 1;
559 }
560 ret_code = LTTCOMM_CONSUMERD_RELAYD_FAIL;
561 }
562 if (relayd_id == -1ULL) {
563 relayd_id = stream->relayd_id;
564 }
565 }
566 }
567
568 /* Inform sessiond that we are about to send channel and streams. */
569 ret = consumer_send_status_msg(sock, ret_code);
570 if (ret < 0 || ret_code != LTTCOMM_CONSUMERD_SUCCESS) {
571 /*
572 * Either the session daemon is not responding or the relayd died so we
573 * stop now.
574 */
575 goto error;
576 }
577
578 /* Send channel to sessiond. */
579 ret = ustctl_send_channel_to_sessiond(sock, channel->uchan);
580 if (ret < 0) {
581 goto error;
582 }
583
584 ret = ustctl_channel_close_wakeup_fd(channel->uchan);
585 if (ret < 0) {
586 goto error;
587 }
588
589 /* The channel was sent successfully to the sessiond at this point. */
590 cds_list_for_each_entry(stream, &channel->streams.head, send_node) {
591
592 health_code_update();
593
594 /* Send stream to session daemon. */
595 ret = send_sessiond_stream(sock, stream);
596 if (ret < 0) {
597 goto error;
598 }
599 }
600
601 /* Tell sessiond there is no more stream. */
602 ret = ustctl_send_stream_to_sessiond(sock, NULL);
603 if (ret < 0) {
604 goto error;
605 }
606
607 DBG("UST consumer NULL stream sent to sessiond");
608
609 return 0;
610
611 error:
612 if (ret_code != LTTCOMM_CONSUMERD_SUCCESS) {
613 ret = -1;
614 }
615 return ret;
616 }
617
618 /*
619 * Creates a channel and streams and add the channel it to the channel internal
620 * state. The created stream must ONLY be sent once the GET_CHANNEL command is
621 * received.
622 *
623 * Return 0 on success or else, a negative value is returned and the channel
624 * MUST be destroyed by consumer_del_channel().
625 */
626 static int ask_channel(struct lttng_consumer_local_data *ctx, int sock,
627 struct lttng_consumer_channel *channel,
628 struct ustctl_consumer_channel_attr *attr)
629 {
630 int ret;
631
632 assert(ctx);
633 assert(channel);
634 assert(attr);
635
636 /*
637 * This value is still used by the kernel consumer since for the kernel,
638 * the stream ownership is not IN the consumer so we need to have the
639 * number of left stream that needs to be initialized so we can know when
640 * to delete the channel (see consumer.c).
641 *
642 * As for the user space tracer now, the consumer creates and sends the
643 * stream to the session daemon which only sends them to the application
644 * once every stream of a channel is received making this value useless
645 * because we they will be added to the poll thread before the application
646 * receives them. This ensures that a stream can not hang up during
647 * initilization of a channel.
648 */
649 channel->nb_init_stream_left = 0;
650
651 /* The reply msg status is handled in the following call. */
652 ret = create_ust_channel(channel, attr, &channel->uchan);
653 if (ret < 0) {
654 goto end;
655 }
656
657 channel->wait_fd = ustctl_channel_get_wait_fd(channel->uchan);
658
659 /*
660 * For the snapshots (no monitor), we create the metadata streams
661 * on demand, not during the channel creation.
662 */
663 if (channel->type == CONSUMER_CHANNEL_TYPE_METADATA && !channel->monitor) {
664 ret = 0;
665 goto end;
666 }
667
668 /* Open all streams for this channel. */
669 ret = create_ust_streams(channel, ctx);
670 if (ret < 0) {
671 goto end;
672 }
673
674 end:
675 return ret;
676 }
677
678 /*
679 * Send all stream of a channel to the right thread handling it.
680 *
681 * On error, return a negative value else 0 on success.
682 */
683 static int send_streams_to_thread(struct lttng_consumer_channel *channel,
684 struct lttng_consumer_local_data *ctx)
685 {
686 int ret = 0;
687 struct lttng_consumer_stream *stream, *stmp;
688
689 assert(channel);
690 assert(ctx);
691
692 /* Send streams to the corresponding thread. */
693 cds_list_for_each_entry_safe(stream, stmp, &channel->streams.head,
694 send_node) {
695
696 health_code_update();
697
698 /* Sending the stream to the thread. */
699 ret = send_stream_to_thread(stream, ctx);
700 if (ret < 0) {
701 /*
702 * If we are unable to send the stream to the thread, there is
703 * a big problem so just stop everything.
704 */
705 /* Remove node from the channel stream list. */
706 cds_list_del(&stream->send_node);
707 goto error;
708 }
709
710 /* Remove node from the channel stream list. */
711 cds_list_del(&stream->send_node);
712
713 }
714
715 error:
716 return ret;
717 }
718
719 /*
720 * Flush channel's streams using the given key to retrieve the channel.
721 *
722 * Return 0 on success else an LTTng error code.
723 */
724 static int flush_channel(uint64_t chan_key)
725 {
726 int ret = 0;
727 struct lttng_consumer_channel *channel;
728 struct lttng_consumer_stream *stream;
729 struct lttng_ht *ht;
730 struct lttng_ht_iter iter;
731
732 DBG("UST consumer flush channel key %" PRIu64, chan_key);
733
734 rcu_read_lock();
735 channel = consumer_find_channel(chan_key);
736 if (!channel) {
737 ERR("UST consumer flush channel %" PRIu64 " not found", chan_key);
738 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
739 goto error;
740 }
741
742 ht = consumer_data.stream_per_chan_id_ht;
743
744 /* For each stream of the channel id, flush it. */
745 cds_lfht_for_each_entry_duplicate(ht->ht,
746 ht->hash_fct(&channel->key, lttng_ht_seed), ht->match_fct,
747 &channel->key, &iter.iter, stream, node_channel_id.node) {
748
749 health_code_update();
750
751 pthread_mutex_lock(&stream->lock);
752
753 /*
754 * Protect against concurrent teardown of a stream.
755 */
756 if (cds_lfht_is_node_deleted(&stream->node.node)) {
757 goto next;
758 }
759
760 if (!stream->quiescent) {
761 ustctl_flush_buffer(stream->ustream, 0);
762 stream->quiescent = true;
763 }
764 next:
765 pthread_mutex_unlock(&stream->lock);
766 }
767 error:
768 rcu_read_unlock();
769 return ret;
770 }
771
772 /*
773 * Clear quiescent state from channel's streams using the given key to
774 * retrieve the channel.
775 *
776 * Return 0 on success else an LTTng error code.
777 */
778 static int clear_quiescent_channel(uint64_t chan_key)
779 {
780 int ret = 0;
781 struct lttng_consumer_channel *channel;
782 struct lttng_consumer_stream *stream;
783 struct lttng_ht *ht;
784 struct lttng_ht_iter iter;
785
786 DBG("UST consumer clear quiescent channel key %" PRIu64, chan_key);
787
788 rcu_read_lock();
789 channel = consumer_find_channel(chan_key);
790 if (!channel) {
791 ERR("UST consumer clear quiescent channel %" PRIu64 " not found", chan_key);
792 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
793 goto error;
794 }
795
796 ht = consumer_data.stream_per_chan_id_ht;
797
798 /* For each stream of the channel id, clear quiescent state. */
799 cds_lfht_for_each_entry_duplicate(ht->ht,
800 ht->hash_fct(&channel->key, lttng_ht_seed), ht->match_fct,
801 &channel->key, &iter.iter, stream, node_channel_id.node) {
802
803 health_code_update();
804
805 pthread_mutex_lock(&stream->lock);
806 stream->quiescent = false;
807 pthread_mutex_unlock(&stream->lock);
808 }
809 error:
810 rcu_read_unlock();
811 return ret;
812 }
813
814 /*
815 * Close metadata stream wakeup_fd using the given key to retrieve the channel.
816 * RCU read side lock MUST be acquired before calling this function.
817 *
818 * Return 0 on success else an LTTng error code.
819 */
820 static int close_metadata(uint64_t chan_key)
821 {
822 int ret = 0;
823 struct lttng_consumer_channel *channel;
824 unsigned int channel_monitor;
825
826 DBG("UST consumer close metadata key %" PRIu64, chan_key);
827
828 channel = consumer_find_channel(chan_key);
829 if (!channel) {
830 /*
831 * This is possible if the metadata thread has issue a delete because
832 * the endpoint point of the stream hung up. There is no way the
833 * session daemon can know about it thus use a DBG instead of an actual
834 * error.
835 */
836 DBG("UST consumer close metadata %" PRIu64 " not found", chan_key);
837 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
838 goto error;
839 }
840
841 pthread_mutex_lock(&consumer_data.lock);
842 pthread_mutex_lock(&channel->lock);
843 channel_monitor = channel->monitor;
844 if (cds_lfht_is_node_deleted(&channel->node.node)) {
845 goto error_unlock;
846 }
847
848 lttng_ustconsumer_close_metadata(channel);
849 pthread_mutex_unlock(&channel->lock);
850 pthread_mutex_unlock(&consumer_data.lock);
851
852 /*
853 * The ownership of a metadata channel depends on the type of
854 * session to which it belongs. In effect, the monitor flag is checked
855 * to determine if this metadata channel is in "snapshot" mode or not.
856 *
857 * In the non-snapshot case, the metadata channel is created along with
858 * a single stream which will remain present until the metadata channel
859 * is destroyed (on the destruction of its session). In this case, the
860 * metadata stream in "monitored" by the metadata poll thread and holds
861 * the ownership of its channel.
862 *
863 * Closing the metadata will cause the metadata stream's "metadata poll
864 * pipe" to be closed. Closing this pipe will wake-up the metadata poll
865 * thread which will teardown the metadata stream which, in return,
866 * deletes the metadata channel.
867 *
868 * In the snapshot case, the metadata stream is created and destroyed
869 * on every snapshot record. Since the channel doesn't have an owner
870 * other than the session daemon, it is safe to destroy it immediately
871 * on reception of the CLOSE_METADATA command.
872 */
873 if (!channel_monitor) {
874 /*
875 * The channel and consumer_data locks must be
876 * released before this call since consumer_del_channel
877 * re-acquires the channel and consumer_data locks to teardown
878 * the channel and queue its reclamation by the "call_rcu"
879 * worker thread.
880 */
881 consumer_del_channel(channel);
882 }
883
884 return ret;
885 error_unlock:
886 pthread_mutex_unlock(&channel->lock);
887 pthread_mutex_unlock(&consumer_data.lock);
888 error:
889 return ret;
890 }
891
892 /*
893 * RCU read side lock MUST be acquired before calling this function.
894 *
895 * Return 0 on success else an LTTng error code.
896 */
897 static int setup_metadata(struct lttng_consumer_local_data *ctx, uint64_t key)
898 {
899 int ret;
900 struct lttng_consumer_channel *metadata;
901
902 DBG("UST consumer setup metadata key %" PRIu64, key);
903
904 metadata = consumer_find_channel(key);
905 if (!metadata) {
906 ERR("UST consumer push metadata %" PRIu64 " not found", key);
907 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
908 goto end;
909 }
910
911 /*
912 * In no monitor mode, the metadata channel has no stream(s) so skip the
913 * ownership transfer to the metadata thread.
914 */
915 if (!metadata->monitor) {
916 DBG("Metadata channel in no monitor");
917 ret = 0;
918 goto end;
919 }
920
921 /*
922 * Send metadata stream to relayd if one available. Availability is
923 * known if the stream is still in the list of the channel.
924 */
925 if (cds_list_empty(&metadata->streams.head)) {
926 ERR("Metadata channel key %" PRIu64 ", no stream available.", key);
927 ret = LTTCOMM_CONSUMERD_ERROR_METADATA;
928 goto error_no_stream;
929 }
930
931 /* Send metadata stream to relayd if needed. */
932 if (metadata->metadata_stream->relayd_id != (uint64_t) -1ULL) {
933 ret = consumer_send_relayd_stream(metadata->metadata_stream,
934 metadata->pathname);
935 if (ret < 0) {
936 ret = LTTCOMM_CONSUMERD_ERROR_METADATA;
937 goto error;
938 }
939 ret = consumer_send_relayd_streams_sent(
940 metadata->metadata_stream->relayd_id);
941 if (ret < 0) {
942 ret = LTTCOMM_CONSUMERD_RELAYD_FAIL;
943 goto error;
944 }
945 }
946
947 ret = send_streams_to_thread(metadata, ctx);
948 if (ret < 0) {
949 /*
950 * If we are unable to send the stream to the thread, there is
951 * a big problem so just stop everything.
952 */
953 ret = LTTCOMM_CONSUMERD_FATAL;
954 goto error;
955 }
956 /* List MUST be empty after or else it could be reused. */
957 assert(cds_list_empty(&metadata->streams.head));
958
959 ret = 0;
960 goto end;
961
962 error:
963 /*
964 * Delete metadata channel on error. At this point, the metadata stream can
965 * NOT be monitored by the metadata thread thus having the guarantee that
966 * the stream is still in the local stream list of the channel. This call
967 * will make sure to clean that list.
968 */
969 consumer_stream_destroy(metadata->metadata_stream, NULL);
970 cds_list_del(&metadata->metadata_stream->send_node);
971 metadata->metadata_stream = NULL;
972 error_no_stream:
973 end:
974 return ret;
975 }
976
977 /*
978 * Snapshot the whole metadata.
979 *
980 * Returns 0 on success, < 0 on error
981 */
982 static int snapshot_metadata(uint64_t key, char *path, uint64_t relayd_id,
983 struct lttng_consumer_local_data *ctx)
984 {
985 int ret = 0;
986 struct lttng_consumer_channel *metadata_channel;
987 struct lttng_consumer_stream *metadata_stream;
988
989 assert(path);
990 assert(ctx);
991
992 DBG("UST consumer snapshot metadata with key %" PRIu64 " at path %s",
993 key, path);
994
995 rcu_read_lock();
996
997 metadata_channel = consumer_find_channel(key);
998 if (!metadata_channel) {
999 ERR("UST snapshot metadata channel not found for key %" PRIu64,
1000 key);
1001 ret = -1;
1002 goto error;
1003 }
1004 assert(!metadata_channel->monitor);
1005
1006 health_code_update();
1007
1008 /*
1009 * Ask the sessiond if we have new metadata waiting and update the
1010 * consumer metadata cache.
1011 */
1012 ret = lttng_ustconsumer_request_metadata(ctx, metadata_channel, 0, 1);
1013 if (ret < 0) {
1014 goto error;
1015 }
1016
1017 health_code_update();
1018
1019 /*
1020 * The metadata stream is NOT created in no monitor mode when the channel
1021 * is created on a sessiond ask channel command.
1022 */
1023 ret = create_ust_streams(metadata_channel, ctx);
1024 if (ret < 0) {
1025 goto error;
1026 }
1027
1028 metadata_stream = metadata_channel->metadata_stream;
1029 assert(metadata_stream);
1030
1031 if (relayd_id != (uint64_t) -1ULL) {
1032 metadata_stream->relayd_id = relayd_id;
1033 ret = consumer_send_relayd_stream(metadata_stream, path);
1034 if (ret < 0) {
1035 goto error_stream;
1036 }
1037 } else {
1038 ret = utils_create_stream_file(path, metadata_stream->name,
1039 metadata_stream->chan->tracefile_size,
1040 metadata_stream->tracefile_count_current,
1041 metadata_stream->uid, metadata_stream->gid, NULL);
1042 if (ret < 0) {
1043 goto error_stream;
1044 }
1045 metadata_stream->out_fd = ret;
1046 metadata_stream->tracefile_size_current = 0;
1047 }
1048
1049 do {
1050 health_code_update();
1051
1052 ret = lttng_consumer_read_subbuffer(metadata_stream, ctx);
1053 if (ret < 0) {
1054 goto error_stream;
1055 }
1056 } while (ret > 0);
1057
1058 error_stream:
1059 /*
1060 * Clean up the stream completly because the next snapshot will use a new
1061 * metadata stream.
1062 */
1063 consumer_stream_destroy(metadata_stream, NULL);
1064 cds_list_del(&metadata_stream->send_node);
1065 metadata_channel->metadata_stream = NULL;
1066
1067 error:
1068 rcu_read_unlock();
1069 return ret;
1070 }
1071
1072 static
1073 int get_current_subbuf_addr(struct lttng_consumer_stream *stream,
1074 const char **addr)
1075 {
1076 int ret;
1077 unsigned long mmap_offset;
1078 const char *mmap_base;
1079
1080 mmap_base = ustctl_get_mmap_base(stream->ustream);
1081 if (!mmap_base) {
1082 ERR("Failed to get mmap base for stream `%s`",
1083 stream->name);
1084 ret = -EPERM;
1085 goto error;
1086 }
1087
1088 ret = ustctl_get_mmap_read_offset(stream->ustream, &mmap_offset);
1089 if (ret != 0) {
1090 ERR("Failed to get mmap offset for stream `%s`", stream->name);
1091 ret = -EINVAL;
1092 goto error;
1093 }
1094
1095 *addr = mmap_base + mmap_offset;
1096 error:
1097 return ret;
1098
1099 }
1100
1101 /*
1102 * Take a snapshot of all the stream of a channel.
1103 *
1104 * Returns 0 on success, < 0 on error
1105 */
1106 static int snapshot_channel(uint64_t key, char *path, uint64_t relayd_id,
1107 uint64_t nb_packets_per_stream, struct lttng_consumer_local_data *ctx)
1108 {
1109 int ret;
1110 unsigned use_relayd = 0;
1111 unsigned long consumed_pos, produced_pos;
1112 struct lttng_consumer_channel *channel;
1113 struct lttng_consumer_stream *stream;
1114
1115 assert(path);
1116 assert(ctx);
1117
1118 rcu_read_lock();
1119
1120 if (relayd_id != (uint64_t) -1ULL) {
1121 use_relayd = 1;
1122 }
1123
1124 channel = consumer_find_channel(key);
1125 if (!channel) {
1126 ERR("UST snapshot channel not found for key %" PRIu64, key);
1127 ret = -1;
1128 goto error;
1129 }
1130 assert(!channel->monitor);
1131 DBG("UST consumer snapshot channel %" PRIu64, key);
1132
1133 cds_list_for_each_entry(stream, &channel->streams.head, send_node) {
1134 health_code_update();
1135
1136 /* Lock stream because we are about to change its state. */
1137 pthread_mutex_lock(&stream->lock);
1138 stream->relayd_id = relayd_id;
1139
1140 if (use_relayd) {
1141 ret = consumer_send_relayd_stream(stream, path);
1142 if (ret < 0) {
1143 goto error_unlock;
1144 }
1145 } else {
1146 ret = utils_create_stream_file(path, stream->name,
1147 stream->chan->tracefile_size,
1148 stream->tracefile_count_current,
1149 stream->uid, stream->gid, NULL);
1150 if (ret < 0) {
1151 goto error_unlock;
1152 }
1153 stream->out_fd = ret;
1154 stream->tracefile_size_current = 0;
1155
1156 DBG("UST consumer snapshot stream %s/%s (%" PRIu64 ")", path,
1157 stream->name, stream->key);
1158 }
1159 if (relayd_id != -1ULL) {
1160 ret = consumer_send_relayd_streams_sent(relayd_id);
1161 if (ret < 0) {
1162 goto error_unlock;
1163 }
1164 }
1165
1166 /*
1167 * If tracing is active, we want to perform a "full" buffer flush.
1168 * Else, if quiescent, it has already been done by the prior stop.
1169 */
1170 if (!stream->quiescent) {
1171 ustctl_flush_buffer(stream->ustream, 0);
1172 }
1173
1174 ret = lttng_ustconsumer_take_snapshot(stream);
1175 if (ret < 0) {
1176 ERR("Taking UST snapshot");
1177 goto error_unlock;
1178 }
1179
1180 ret = lttng_ustconsumer_get_produced_snapshot(stream, &produced_pos);
1181 if (ret < 0) {
1182 ERR("Produced UST snapshot position");
1183 goto error_unlock;
1184 }
1185
1186 ret = lttng_ustconsumer_get_consumed_snapshot(stream, &consumed_pos);
1187 if (ret < 0) {
1188 ERR("Consumerd UST snapshot position");
1189 goto error_unlock;
1190 }
1191
1192 /*
1193 * The original value is sent back if max stream size is larger than
1194 * the possible size of the snapshot. Also, we assume that the session
1195 * daemon should never send a maximum stream size that is lower than
1196 * subbuffer size.
1197 */
1198 consumed_pos = consumer_get_consume_start_pos(consumed_pos,
1199 produced_pos, nb_packets_per_stream,
1200 stream->max_sb_size);
1201
1202 while (consumed_pos < produced_pos) {
1203 ssize_t read_len;
1204 unsigned long len, padded_len;
1205 const char *subbuf_addr;
1206 struct lttng_buffer_view subbuf_view;
1207
1208 health_code_update();
1209
1210 DBG("UST consumer taking snapshot at pos %lu", consumed_pos);
1211
1212 ret = ustctl_get_subbuf(stream->ustream, &consumed_pos);
1213 if (ret < 0) {
1214 if (ret != -EAGAIN) {
1215 PERROR("ustctl_get_subbuf snapshot");
1216 goto error_close_stream;
1217 }
1218 DBG("UST consumer get subbuf failed. Skipping it.");
1219 consumed_pos += stream->max_sb_size;
1220 stream->chan->lost_packets++;
1221 continue;
1222 }
1223
1224 ret = ustctl_get_subbuf_size(stream->ustream, &len);
1225 if (ret < 0) {
1226 ERR("Snapshot ustctl_get_subbuf_size");
1227 goto error_put_subbuf;
1228 }
1229
1230 ret = ustctl_get_padded_subbuf_size(stream->ustream, &padded_len);
1231 if (ret < 0) {
1232 ERR("Snapshot ustctl_get_padded_subbuf_size");
1233 goto error_put_subbuf;
1234 }
1235
1236 ret = get_current_subbuf_addr(stream, &subbuf_addr);
1237 if (ret) {
1238 goto error_put_subbuf;
1239 }
1240
1241 subbuf_view = lttng_buffer_view_init(
1242 subbuf_addr, 0, padded_len);
1243 read_len = lttng_consumer_on_read_subbuffer_mmap(ctx,
1244 stream, &subbuf_view, padded_len - len,
1245 NULL);
1246 if (use_relayd) {
1247 if (read_len != len) {
1248 ret = -EPERM;
1249 goto error_put_subbuf;
1250 }
1251 } else {
1252 if (read_len != padded_len) {
1253 ret = -EPERM;
1254 goto error_put_subbuf;
1255 }
1256 }
1257
1258 ret = ustctl_put_subbuf(stream->ustream);
1259 if (ret < 0) {
1260 ERR("Snapshot ustctl_put_subbuf");
1261 goto error_close_stream;
1262 }
1263 consumed_pos += stream->max_sb_size;
1264 }
1265
1266 /* Simply close the stream so we can use it on the next snapshot. */
1267 consumer_stream_close(stream);
1268 pthread_mutex_unlock(&stream->lock);
1269 }
1270
1271 rcu_read_unlock();
1272 return 0;
1273
1274 error_put_subbuf:
1275 if (ustctl_put_subbuf(stream->ustream) < 0) {
1276 ERR("Snapshot ustctl_put_subbuf");
1277 }
1278 error_close_stream:
1279 consumer_stream_close(stream);
1280 error_unlock:
1281 pthread_mutex_unlock(&stream->lock);
1282 error:
1283 rcu_read_unlock();
1284 return ret;
1285 }
1286
1287 /*
1288 * Receive the metadata updates from the sessiond. Supports receiving
1289 * overlapping metadata, but is needs to always belong to a contiguous
1290 * range starting from 0.
1291 * Be careful about the locks held when calling this function: it needs
1292 * the metadata cache flush to concurrently progress in order to
1293 * complete.
1294 */
1295 int lttng_ustconsumer_recv_metadata(int sock, uint64_t key, uint64_t offset,
1296 uint64_t len, uint64_t version,
1297 struct lttng_consumer_channel *channel, int timer, int wait)
1298 {
1299 int ret, ret_code = LTTCOMM_CONSUMERD_SUCCESS;
1300 char *metadata_str;
1301
1302 DBG("UST consumer push metadata key %" PRIu64 " of len %" PRIu64, key, len);
1303
1304 metadata_str = zmalloc(len * sizeof(char));
1305 if (!metadata_str) {
1306 PERROR("zmalloc metadata string");
1307 ret_code = LTTCOMM_CONSUMERD_ENOMEM;
1308 goto end;
1309 }
1310
1311 health_code_update();
1312
1313 /* Receive metadata string. */
1314 ret = lttcomm_recv_unix_sock(sock, metadata_str, len);
1315 if (ret < 0) {
1316 /* Session daemon is dead so return gracefully. */
1317 ret_code = ret;
1318 goto end_free;
1319 }
1320
1321 health_code_update();
1322
1323 pthread_mutex_lock(&channel->metadata_cache->lock);
1324 ret = consumer_metadata_cache_write(channel, offset, len, version,
1325 metadata_str);
1326 if (ret < 0) {
1327 /* Unable to handle metadata. Notify session daemon. */
1328 ret_code = LTTCOMM_CONSUMERD_ERROR_METADATA;
1329 /*
1330 * Skip metadata flush on write error since the offset and len might
1331 * not have been updated which could create an infinite loop below when
1332 * waiting for the metadata cache to be flushed.
1333 */
1334 pthread_mutex_unlock(&channel->metadata_cache->lock);
1335 goto end_free;
1336 }
1337 pthread_mutex_unlock(&channel->metadata_cache->lock);
1338
1339 if (!wait) {
1340 goto end_free;
1341 }
1342 while (consumer_metadata_cache_flushed(channel, offset + len, timer)) {
1343 DBG("Waiting for metadata to be flushed");
1344
1345 health_code_update();
1346
1347 usleep(DEFAULT_METADATA_AVAILABILITY_WAIT_TIME);
1348 }
1349
1350 end_free:
1351 free(metadata_str);
1352 end:
1353 return ret_code;
1354 }
1355
1356 /*
1357 * Receive command from session daemon and process it.
1358 *
1359 * Return 1 on success else a negative value or 0.
1360 */
1361 int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
1362 int sock, struct pollfd *consumer_sockpoll)
1363 {
1364 ssize_t ret;
1365 enum lttcomm_return_code ret_code = LTTCOMM_CONSUMERD_SUCCESS;
1366 struct lttcomm_consumer_msg msg;
1367 struct lttng_consumer_channel *channel = NULL;
1368
1369 health_code_update();
1370
1371 ret = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg));
1372 if (ret != sizeof(msg)) {
1373 DBG("Consumer received unexpected message size %zd (expects %zu)",
1374 ret, sizeof(msg));
1375 /*
1376 * The ret value might 0 meaning an orderly shutdown but this is ok
1377 * since the caller handles this.
1378 */
1379 if (ret > 0) {
1380 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_CMD);
1381 ret = -1;
1382 }
1383 return ret;
1384 }
1385
1386 health_code_update();
1387
1388 /* deprecated */
1389 assert(msg.cmd_type != LTTNG_CONSUMER_STOP);
1390
1391 health_code_update();
1392
1393 /* relayd needs RCU read-side lock */
1394 rcu_read_lock();
1395
1396 switch (msg.cmd_type) {
1397 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET:
1398 {
1399 /* Session daemon status message are handled in the following call. */
1400 consumer_add_relayd_socket(msg.u.relayd_sock.net_index,
1401 msg.u.relayd_sock.type, ctx, sock, consumer_sockpoll,
1402 &msg.u.relayd_sock.sock, msg.u.relayd_sock.session_id,
1403 msg.u.relayd_sock.relayd_session_id);
1404 goto end_nosignal;
1405 }
1406 case LTTNG_CONSUMER_DESTROY_RELAYD:
1407 {
1408 uint64_t index = msg.u.destroy_relayd.net_seq_idx;
1409 struct consumer_relayd_sock_pair *relayd;
1410
1411 DBG("UST consumer destroying relayd %" PRIu64, index);
1412
1413 /* Get relayd reference if exists. */
1414 relayd = consumer_find_relayd(index);
1415 if (relayd == NULL) {
1416 DBG("Unable to find relayd %" PRIu64, index);
1417 ret_code = LTTCOMM_CONSUMERD_RELAYD_FAIL;
1418 }
1419
1420 /*
1421 * Each relayd socket pair has a refcount of stream attached to it
1422 * which tells if the relayd is still active or not depending on the
1423 * refcount value.
1424 *
1425 * This will set the destroy flag of the relayd object and destroy it
1426 * if the refcount reaches zero when called.
1427 *
1428 * The destroy can happen either here or when a stream fd hangs up.
1429 */
1430 if (relayd) {
1431 consumer_flag_relayd_for_destroy(relayd);
1432 }
1433
1434 goto end_msg_sessiond;
1435 }
1436 case LTTNG_CONSUMER_UPDATE_STREAM:
1437 {
1438 rcu_read_unlock();
1439 return -ENOSYS;
1440 }
1441 case LTTNG_CONSUMER_DATA_PENDING:
1442 {
1443 int ret, is_data_pending;
1444 uint64_t id = msg.u.data_pending.session_id;
1445
1446 DBG("UST consumer data pending command for id %" PRIu64, id);
1447
1448 is_data_pending = consumer_data_pending(id);
1449
1450 /* Send back returned value to session daemon */
1451 ret = lttcomm_send_unix_sock(sock, &is_data_pending,
1452 sizeof(is_data_pending));
1453 if (ret < 0) {
1454 DBG("Error when sending the data pending ret code: %d", ret);
1455 goto error_fatal;
1456 }
1457
1458 /*
1459 * No need to send back a status message since the data pending
1460 * returned value is the response.
1461 */
1462 break;
1463 }
1464 case LTTNG_CONSUMER_ASK_CHANNEL_CREATION:
1465 {
1466 int ret;
1467 struct ustctl_consumer_channel_attr attr;
1468
1469 /* Create a plain object and reserve a channel key. */
1470 channel = consumer_allocate_channel(
1471 msg.u.ask_channel.key,
1472 msg.u.ask_channel.session_id,
1473 msg.u.ask_channel.pathname,
1474 msg.u.ask_channel.name,
1475 msg.u.ask_channel.uid,
1476 msg.u.ask_channel.gid,
1477 msg.u.ask_channel.relayd_id,
1478 (enum lttng_event_output) msg.u.ask_channel.output,
1479 msg.u.ask_channel.tracefile_size,
1480 msg.u.ask_channel.tracefile_count,
1481 msg.u.ask_channel.session_id_per_pid,
1482 msg.u.ask_channel.monitor,
1483 msg.u.ask_channel.live_timer_interval,
1484 msg.u.ask_channel.is_live,
1485 msg.u.ask_channel.root_shm_path,
1486 msg.u.ask_channel.shm_path);
1487 if (!channel) {
1488 goto end_channel_error;
1489 }
1490
1491 /*
1492 * Assign UST application UID to the channel. This value is ignored for
1493 * per PID buffers. This is specific to UST thus setting this after the
1494 * allocation.
1495 */
1496 channel->ust_app_uid = msg.u.ask_channel.ust_app_uid;
1497
1498 /* Build channel attributes from received message. */
1499 attr.subbuf_size = msg.u.ask_channel.subbuf_size;
1500 attr.num_subbuf = msg.u.ask_channel.num_subbuf;
1501 attr.overwrite = msg.u.ask_channel.overwrite;
1502 attr.switch_timer_interval = msg.u.ask_channel.switch_timer_interval;
1503 attr.read_timer_interval = msg.u.ask_channel.read_timer_interval;
1504 attr.chan_id = msg.u.ask_channel.chan_id;
1505 memcpy(attr.uuid, msg.u.ask_channel.uuid, sizeof(attr.uuid));
1506
1507 /* Match channel buffer type to the UST abi. */
1508 switch (msg.u.ask_channel.output) {
1509 case LTTNG_EVENT_MMAP:
1510 default:
1511 attr.output = LTTNG_UST_MMAP;
1512 break;
1513 }
1514
1515 /* Translate and save channel type. */
1516 switch (msg.u.ask_channel.type) {
1517 case LTTNG_UST_CHAN_PER_CPU:
1518 channel->type = CONSUMER_CHANNEL_TYPE_DATA;
1519 attr.type = LTTNG_UST_CHAN_PER_CPU;
1520 /*
1521 * Set refcount to 1 for owner. Below, we will
1522 * pass ownership to the
1523 * consumer_thread_channel_poll() thread.
1524 */
1525 channel->refcount = 1;
1526 break;
1527 case LTTNG_UST_CHAN_METADATA:
1528 channel->type = CONSUMER_CHANNEL_TYPE_METADATA;
1529 attr.type = LTTNG_UST_CHAN_METADATA;
1530 break;
1531 default:
1532 assert(0);
1533 goto error_fatal;
1534 };
1535
1536 health_code_update();
1537
1538 ret = ask_channel(ctx, sock, channel, &attr);
1539 if (ret < 0) {
1540 goto end_channel_error;
1541 }
1542
1543 if (msg.u.ask_channel.type == LTTNG_UST_CHAN_METADATA) {
1544 ret = consumer_metadata_cache_allocate(channel);
1545 if (ret < 0) {
1546 ERR("Allocating metadata cache");
1547 goto end_channel_error;
1548 }
1549 consumer_timer_switch_start(channel, attr.switch_timer_interval);
1550 attr.switch_timer_interval = 0;
1551 } else {
1552 consumer_timer_live_start(channel,
1553 msg.u.ask_channel.live_timer_interval);
1554 }
1555
1556 health_code_update();
1557
1558 /*
1559 * Add the channel to the internal state AFTER all streams were created
1560 * and successfully sent to session daemon. This way, all streams must
1561 * be ready before this channel is visible to the threads.
1562 * If add_channel succeeds, ownership of the channel is
1563 * passed to consumer_thread_channel_poll().
1564 */
1565 ret = add_channel(channel, ctx);
1566 if (ret < 0) {
1567 if (msg.u.ask_channel.type == LTTNG_UST_CHAN_METADATA) {
1568 if (channel->switch_timer_enabled == 1) {
1569 consumer_timer_switch_stop(channel);
1570 }
1571 consumer_metadata_cache_destroy(channel);
1572 }
1573 if (channel->live_timer_enabled == 1) {
1574 consumer_timer_live_stop(channel);
1575 }
1576 goto end_channel_error;
1577 }
1578
1579 health_code_update();
1580
1581 /*
1582 * Channel and streams are now created. Inform the session daemon that
1583 * everything went well and should wait to receive the channel and
1584 * streams with ustctl API.
1585 */
1586 ret = consumer_send_status_channel(sock, channel);
1587 if (ret < 0) {
1588 /*
1589 * There is probably a problem on the socket.
1590 */
1591 goto error_fatal;
1592 }
1593
1594 break;
1595 }
1596 case LTTNG_CONSUMER_GET_CHANNEL:
1597 {
1598 int ret, relayd_err = 0;
1599 uint64_t key = msg.u.get_channel.key;
1600 struct lttng_consumer_channel *channel;
1601
1602 channel = consumer_find_channel(key);
1603 if (!channel) {
1604 ERR("UST consumer get channel key %" PRIu64 " not found", key);
1605 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
1606 goto end_msg_sessiond;
1607 }
1608
1609 health_code_update();
1610
1611 /* Send everything to sessiond. */
1612 ret = send_sessiond_channel(sock, channel, ctx, &relayd_err);
1613 if (ret < 0) {
1614 if (relayd_err) {
1615 /*
1616 * We were unable to send to the relayd the stream so avoid
1617 * sending back a fatal error to the thread since this is OK
1618 * and the consumer can continue its work. The above call
1619 * has sent the error status message to the sessiond.
1620 */
1621 goto end_nosignal;
1622 }
1623 /*
1624 * The communicaton was broken hence there is a bad state between
1625 * the consumer and sessiond so stop everything.
1626 */
1627 goto error_fatal;
1628 }
1629
1630 health_code_update();
1631
1632 /*
1633 * In no monitor mode, the streams ownership is kept inside the channel
1634 * so don't send them to the data thread.
1635 */
1636 if (!channel->monitor) {
1637 goto end_msg_sessiond;
1638 }
1639
1640 ret = send_streams_to_thread(channel, ctx);
1641 if (ret < 0) {
1642 /*
1643 * If we are unable to send the stream to the thread, there is
1644 * a big problem so just stop everything.
1645 */
1646 goto error_fatal;
1647 }
1648 /* List MUST be empty after or else it could be reused. */
1649 assert(cds_list_empty(&channel->streams.head));
1650 goto end_msg_sessiond;
1651 }
1652 case LTTNG_CONSUMER_DESTROY_CHANNEL:
1653 {
1654 uint64_t key = msg.u.destroy_channel.key;
1655
1656 /*
1657 * Only called if streams have not been sent to stream
1658 * manager thread. However, channel has been sent to
1659 * channel manager thread.
1660 */
1661 notify_thread_del_channel(ctx, key);
1662 goto end_msg_sessiond;
1663 }
1664 case LTTNG_CONSUMER_CLOSE_METADATA:
1665 {
1666 int ret;
1667
1668 ret = close_metadata(msg.u.close_metadata.key);
1669 if (ret != 0) {
1670 ret_code = ret;
1671 }
1672
1673 goto end_msg_sessiond;
1674 }
1675 case LTTNG_CONSUMER_FLUSH_CHANNEL:
1676 {
1677 int ret;
1678
1679 ret = flush_channel(msg.u.flush_channel.key);
1680 if (ret != 0) {
1681 ret_code = ret;
1682 }
1683
1684 goto end_msg_sessiond;
1685 }
1686 case LTTNG_CONSUMER_CLEAR_QUIESCENT_CHANNEL:
1687 {
1688 int ret;
1689
1690 ret = clear_quiescent_channel(
1691 msg.u.clear_quiescent_channel.key);
1692 if (ret != 0) {
1693 ret_code = ret;
1694 }
1695
1696 goto end_msg_sessiond;
1697 }
1698 case LTTNG_CONSUMER_PUSH_METADATA:
1699 {
1700 int ret;
1701 uint64_t len = msg.u.push_metadata.len;
1702 uint64_t key = msg.u.push_metadata.key;
1703 uint64_t offset = msg.u.push_metadata.target_offset;
1704 uint64_t version = msg.u.push_metadata.version;
1705 struct lttng_consumer_channel *channel;
1706
1707 DBG("UST consumer push metadata key %" PRIu64 " of len %" PRIu64, key,
1708 len);
1709
1710 channel = consumer_find_channel(key);
1711 if (!channel) {
1712 /*
1713 * This is possible if the metadata creation on the consumer side
1714 * is in flight vis-a-vis a concurrent push metadata from the
1715 * session daemon. Simply return that the channel failed and the
1716 * session daemon will handle that message correctly considering
1717 * that this race is acceptable thus the DBG() statement here.
1718 */
1719 DBG("UST consumer push metadata %" PRIu64 " not found", key);
1720 ret_code = LTTCOMM_CONSUMERD_CHANNEL_FAIL;
1721 goto end_msg_sessiond;
1722 }
1723
1724 health_code_update();
1725
1726 if (!len) {
1727 /*
1728 * There is nothing to receive. We have simply
1729 * checked whether the channel can be found.
1730 */
1731 ret_code = LTTCOMM_CONSUMERD_SUCCESS;
1732 goto end_msg_sessiond;
1733 }
1734
1735 /* Tell session daemon we are ready to receive the metadata. */
1736 ret = consumer_send_status_msg(sock, LTTCOMM_CONSUMERD_SUCCESS);
1737 if (ret < 0) {
1738 /* Somehow, the session daemon is not responding anymore. */
1739 goto error_fatal;
1740 }
1741
1742 health_code_update();
1743
1744 /* Wait for more data. */
1745 health_poll_entry();
1746 ret = lttng_consumer_poll_socket(consumer_sockpoll);
1747 health_poll_exit();
1748 if (ret) {
1749 goto error_fatal;
1750 }
1751
1752 health_code_update();
1753
1754 ret = lttng_ustconsumer_recv_metadata(sock, key, offset,
1755 len, version, channel, 0, 1);
1756 if (ret < 0) {
1757 /* error receiving from sessiond */
1758 goto error_fatal;
1759 } else {
1760 ret_code = ret;
1761 goto end_msg_sessiond;
1762 }
1763 }
1764 case LTTNG_CONSUMER_SETUP_METADATA:
1765 {
1766 int ret;
1767
1768 ret = setup_metadata(ctx, msg.u.setup_metadata.key);
1769 if (ret) {
1770 ret_code = ret;
1771 }
1772 goto end_msg_sessiond;
1773 }
1774 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL:
1775 {
1776 if (msg.u.snapshot_channel.metadata) {
1777 ret = snapshot_metadata(msg.u.snapshot_channel.key,
1778 msg.u.snapshot_channel.pathname,
1779 msg.u.snapshot_channel.relayd_id,
1780 ctx);
1781 if (ret < 0) {
1782 ERR("Snapshot metadata failed");
1783 ret_code = LTTCOMM_CONSUMERD_ERROR_METADATA;
1784 }
1785 } else {
1786 ret = snapshot_channel(msg.u.snapshot_channel.key,
1787 msg.u.snapshot_channel.pathname,
1788 msg.u.snapshot_channel.relayd_id,
1789 msg.u.snapshot_channel.nb_packets_per_stream,
1790 ctx);
1791 if (ret < 0) {
1792 ERR("Snapshot channel failed");
1793 ret_code = LTTCOMM_CONSUMERD_CHANNEL_FAIL;
1794 }
1795 }
1796
1797 health_code_update();
1798 ret = consumer_send_status_msg(sock, ret_code);
1799 if (ret < 0) {
1800 /* Somehow, the session daemon is not responding anymore. */
1801 goto end_nosignal;
1802 }
1803 health_code_update();
1804 break;
1805 }
1806 case LTTNG_CONSUMER_DISCARDED_EVENTS:
1807 {
1808 int ret = 0;
1809 uint64_t discarded_events;
1810 struct lttng_ht_iter iter;
1811 struct lttng_ht *ht;
1812 struct lttng_consumer_stream *stream;
1813 uint64_t id = msg.u.discarded_events.session_id;
1814 uint64_t key = msg.u.discarded_events.channel_key;
1815
1816 DBG("UST consumer discarded events command for session id %"
1817 PRIu64, id);
1818 rcu_read_lock();
1819 pthread_mutex_lock(&consumer_data.lock);
1820
1821 ht = consumer_data.stream_list_ht;
1822
1823 /*
1824 * We only need a reference to the channel, but they are not
1825 * directly indexed, so we just use the first matching stream
1826 * to extract the information we need, we default to 0 if not
1827 * found (no events are dropped if the channel is not yet in
1828 * use).
1829 */
1830 discarded_events = 0;
1831 cds_lfht_for_each_entry_duplicate(ht->ht,
1832 ht->hash_fct(&id, lttng_ht_seed),
1833 ht->match_fct, &id,
1834 &iter.iter, stream, node_session_id.node) {
1835 if (stream->chan->key == key) {
1836 discarded_events = stream->chan->discarded_events;
1837 break;
1838 }
1839 }
1840 pthread_mutex_unlock(&consumer_data.lock);
1841 rcu_read_unlock();
1842
1843 DBG("UST consumer discarded events command for session id %"
1844 PRIu64 ", channel key %" PRIu64, id, key);
1845
1846 health_code_update();
1847
1848 /* Send back returned value to session daemon */
1849 ret = lttcomm_send_unix_sock(sock, &discarded_events, sizeof(discarded_events));
1850 if (ret < 0) {
1851 PERROR("send discarded events");
1852 goto error_fatal;
1853 }
1854
1855 break;
1856 }
1857 case LTTNG_CONSUMER_LOST_PACKETS:
1858 {
1859 int ret;
1860 uint64_t lost_packets;
1861 struct lttng_ht_iter iter;
1862 struct lttng_ht *ht;
1863 struct lttng_consumer_stream *stream;
1864 uint64_t id = msg.u.lost_packets.session_id;
1865 uint64_t key = msg.u.lost_packets.channel_key;
1866
1867 DBG("UST consumer lost packets command for session id %"
1868 PRIu64, id);
1869 rcu_read_lock();
1870 pthread_mutex_lock(&consumer_data.lock);
1871
1872 ht = consumer_data.stream_list_ht;
1873
1874 /*
1875 * We only need a reference to the channel, but they are not
1876 * directly indexed, so we just use the first matching stream
1877 * to extract the information we need, we default to 0 if not
1878 * found (no packets lost if the channel is not yet in use).
1879 */
1880 lost_packets = 0;
1881 cds_lfht_for_each_entry_duplicate(ht->ht,
1882 ht->hash_fct(&id, lttng_ht_seed),
1883 ht->match_fct, &id,
1884 &iter.iter, stream, node_session_id.node) {
1885 if (stream->chan->key == key) {
1886 lost_packets = stream->chan->lost_packets;
1887 break;
1888 }
1889 }
1890 pthread_mutex_unlock(&consumer_data.lock);
1891 rcu_read_unlock();
1892
1893 DBG("UST consumer lost packets command for session id %"
1894 PRIu64 ", channel key %" PRIu64, id, key);
1895
1896 health_code_update();
1897
1898 /* Send back returned value to session daemon */
1899 ret = lttcomm_send_unix_sock(sock, &lost_packets,
1900 sizeof(lost_packets));
1901 if (ret < 0) {
1902 PERROR("send lost packets");
1903 goto error_fatal;
1904 }
1905
1906 break;
1907 }
1908 default:
1909 break;
1910 }
1911
1912 end_nosignal:
1913 rcu_read_unlock();
1914
1915 health_code_update();
1916
1917 /*
1918 * Return 1 to indicate success since the 0 value can be a socket
1919 * shutdown during the recv() or send() call.
1920 */
1921 return 1;
1922
1923 end_msg_sessiond:
1924 /*
1925 * The returned value here is not useful since either way we'll return 1 to
1926 * the caller because the session daemon socket management is done
1927 * elsewhere. Returning a negative code or 0 will shutdown the consumer.
1928 */
1929 ret = consumer_send_status_msg(sock, ret_code);
1930 if (ret < 0) {
1931 goto error_fatal;
1932 }
1933 rcu_read_unlock();
1934
1935 health_code_update();
1936
1937 return 1;
1938 end_channel_error:
1939 if (channel) {
1940 /*
1941 * Free channel here since no one has a reference to it. We don't
1942 * free after that because a stream can store this pointer.
1943 */
1944 destroy_channel(channel);
1945 }
1946 /* We have to send a status channel message indicating an error. */
1947 ret = consumer_send_status_channel(sock, NULL);
1948 if (ret < 0) {
1949 /* Stop everything if session daemon can not be notified. */
1950 goto error_fatal;
1951 }
1952 rcu_read_unlock();
1953
1954 health_code_update();
1955
1956 return 1;
1957 error_fatal:
1958 rcu_read_unlock();
1959 /* This will issue a consumer stop. */
1960 return -1;
1961 }
1962
1963 void lttng_ustctl_flush_buffer(struct lttng_consumer_stream *stream,
1964 int producer_active)
1965 {
1966 assert(stream);
1967 assert(stream->ustream);
1968
1969 ustctl_flush_buffer(stream->ustream, producer_active);
1970 }
1971
1972 /*
1973 * Take a snapshot for a specific fd
1974 *
1975 * Returns 0 on success, < 0 on error
1976 */
1977 int lttng_ustconsumer_take_snapshot(struct lttng_consumer_stream *stream)
1978 {
1979 assert(stream);
1980 assert(stream->ustream);
1981
1982 return ustctl_snapshot(stream->ustream);
1983 }
1984
1985 /*
1986 * Get the produced position
1987 *
1988 * Returns 0 on success, < 0 on error
1989 */
1990 int lttng_ustconsumer_get_produced_snapshot(
1991 struct lttng_consumer_stream *stream, unsigned long *pos)
1992 {
1993 assert(stream);
1994 assert(stream->ustream);
1995 assert(pos);
1996
1997 return ustctl_snapshot_get_produced(stream->ustream, pos);
1998 }
1999
2000 /*
2001 * Get the consumed position
2002 *
2003 * Returns 0 on success, < 0 on error
2004 */
2005 int lttng_ustconsumer_get_consumed_snapshot(
2006 struct lttng_consumer_stream *stream, unsigned long *pos)
2007 {
2008 assert(stream);
2009 assert(stream->ustream);
2010 assert(pos);
2011
2012 return ustctl_snapshot_get_consumed(stream->ustream, pos);
2013 }
2014
2015 void lttng_ustconsumer_flush_buffer(struct lttng_consumer_stream *stream,
2016 int producer)
2017 {
2018 assert(stream);
2019 assert(stream->ustream);
2020
2021 ustctl_flush_buffer(stream->ustream, producer);
2022 }
2023
2024 int lttng_ustconsumer_get_current_timestamp(
2025 struct lttng_consumer_stream *stream, uint64_t *ts)
2026 {
2027 assert(stream);
2028 assert(stream->ustream);
2029 assert(ts);
2030
2031 return ustctl_get_current_timestamp(stream->ustream, ts);
2032 }
2033
2034 int lttng_ustconsumer_get_sequence_number(
2035 struct lttng_consumer_stream *stream, uint64_t *seq)
2036 {
2037 assert(stream);
2038 assert(stream->ustream);
2039 assert(seq);
2040
2041 return ustctl_get_sequence_number(stream->ustream, seq);
2042 }
2043
2044 /*
2045 * Called when the stream signals the consumer that it has hung up.
2046 */
2047 void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream *stream)
2048 {
2049 assert(stream);
2050 assert(stream->ustream);
2051
2052 pthread_mutex_lock(&stream->lock);
2053 if (!stream->quiescent) {
2054 ustctl_flush_buffer(stream->ustream, 0);
2055 stream->quiescent = true;
2056 }
2057 pthread_mutex_unlock(&stream->lock);
2058 stream->hangup_flush_done = 1;
2059 }
2060
2061 void lttng_ustconsumer_del_channel(struct lttng_consumer_channel *chan)
2062 {
2063 int i;
2064
2065 assert(chan);
2066 assert(chan->uchan);
2067
2068 if (chan->switch_timer_enabled == 1) {
2069 consumer_timer_switch_stop(chan);
2070 }
2071 for (i = 0; i < chan->nr_stream_fds; i++) {
2072 int ret;
2073
2074 ret = close(chan->stream_fds[i]);
2075 if (ret) {
2076 PERROR("close");
2077 }
2078 if (chan->shm_path[0]) {
2079 char shm_path[PATH_MAX];
2080
2081 ret = get_stream_shm_path(shm_path, chan->shm_path, i);
2082 if (ret) {
2083 ERR("Cannot get stream shm path");
2084 }
2085 ret = run_as_unlink(shm_path, chan->uid, chan->gid);
2086 if (ret) {
2087 PERROR("unlink %s", shm_path);
2088 }
2089 }
2090 }
2091 }
2092
2093 void lttng_ustconsumer_free_channel(struct lttng_consumer_channel *chan)
2094 {
2095 assert(chan);
2096 assert(chan->uchan);
2097
2098 consumer_metadata_cache_destroy(chan);
2099 ustctl_destroy_channel(chan->uchan);
2100 /* Try to rmdir all directories under shm_path root. */
2101 if (chan->root_shm_path[0]) {
2102 (void) run_as_recursive_rmdir(chan->root_shm_path,
2103 chan->uid, chan->gid);
2104 }
2105 free(chan->stream_fds);
2106 }
2107
2108 void lttng_ustconsumer_del_stream(struct lttng_consumer_stream *stream)
2109 {
2110 assert(stream);
2111 assert(stream->ustream);
2112
2113 if (stream->chan->switch_timer_enabled == 1) {
2114 consumer_timer_switch_stop(stream->chan);
2115 }
2116 ustctl_destroy_stream(stream->ustream);
2117 }
2118
2119 int lttng_ustconsumer_get_wakeup_fd(struct lttng_consumer_stream *stream)
2120 {
2121 assert(stream);
2122 assert(stream->ustream);
2123
2124 return ustctl_stream_get_wakeup_fd(stream->ustream);
2125 }
2126
2127 int lttng_ustconsumer_close_wakeup_fd(struct lttng_consumer_stream *stream)
2128 {
2129 assert(stream);
2130 assert(stream->ustream);
2131
2132 return ustctl_stream_close_wakeup_fd(stream->ustream);
2133 }
2134
2135 /*
2136 * Populate index values of a UST stream. Values are set in big endian order.
2137 *
2138 * Return 0 on success or else a negative value.
2139 */
2140 static int get_index_values(struct ctf_packet_index *index,
2141 struct ustctl_consumer_stream *ustream)
2142 {
2143 int ret;
2144
2145 ret = ustctl_get_timestamp_begin(ustream, &index->timestamp_begin);
2146 if (ret < 0) {
2147 PERROR("ustctl_get_timestamp_begin");
2148 goto error;
2149 }
2150 index->timestamp_begin = htobe64(index->timestamp_begin);
2151
2152 ret = ustctl_get_timestamp_end(ustream, &index->timestamp_end);
2153 if (ret < 0) {
2154 PERROR("ustctl_get_timestamp_end");
2155 goto error;
2156 }
2157 index->timestamp_end = htobe64(index->timestamp_end);
2158
2159 ret = ustctl_get_events_discarded(ustream, &index->events_discarded);
2160 if (ret < 0) {
2161 PERROR("ustctl_get_events_discarded");
2162 goto error;
2163 }
2164 index->events_discarded = htobe64(index->events_discarded);
2165
2166 ret = ustctl_get_content_size(ustream, &index->content_size);
2167 if (ret < 0) {
2168 PERROR("ustctl_get_content_size");
2169 goto error;
2170 }
2171 index->content_size = htobe64(index->content_size);
2172
2173 ret = ustctl_get_packet_size(ustream, &index->packet_size);
2174 if (ret < 0) {
2175 PERROR("ustctl_get_packet_size");
2176 goto error;
2177 }
2178 index->packet_size = htobe64(index->packet_size);
2179
2180 ret = ustctl_get_stream_id(ustream, &index->stream_id);
2181 if (ret < 0) {
2182 PERROR("ustctl_get_stream_id");
2183 goto error;
2184 }
2185 index->stream_id = htobe64(index->stream_id);
2186
2187 ret = ustctl_get_instance_id(ustream, &index->stream_instance_id);
2188 if (ret < 0) {
2189 PERROR("ustctl_get_instance_id");
2190 goto error;
2191 }
2192 index->stream_instance_id = htobe64(index->stream_instance_id);
2193
2194 ret = ustctl_get_sequence_number(ustream, &index->packet_seq_num);
2195 if (ret < 0) {
2196 PERROR("ustctl_get_sequence_number");
2197 goto error;
2198 }
2199 index->packet_seq_num = htobe64(index->packet_seq_num);
2200
2201 error:
2202 return ret;
2203 }
2204
2205 static
2206 void metadata_stream_reset_cache(struct lttng_consumer_stream *stream,
2207 struct consumer_metadata_cache *cache)
2208 {
2209 DBG("Metadata stream update to version %" PRIu64,
2210 cache->version);
2211 stream->ust_metadata_pushed = 0;
2212 stream->metadata_version = cache->version;
2213 stream->reset_metadata_flag = 1;
2214 }
2215
2216 /*
2217 * Check if the version of the metadata stream and metadata cache match.
2218 * If the cache got updated, reset the metadata stream.
2219 * The stream lock and metadata cache lock MUST be held.
2220 * Return 0 on success, a negative value on error.
2221 */
2222 static
2223 int metadata_stream_check_version(struct lttng_consumer_stream *stream)
2224 {
2225 int ret = 0;
2226 struct consumer_metadata_cache *cache = stream->chan->metadata_cache;
2227
2228 if (cache->version == stream->metadata_version) {
2229 goto end;
2230 }
2231 metadata_stream_reset_cache(stream, cache);
2232
2233 end:
2234 return ret;
2235 }
2236
2237 /*
2238 * Write up to one packet from the metadata cache to the channel.
2239 *
2240 * Returns the number of bytes pushed in the cache, or a negative value
2241 * on error.
2242 */
2243 static
2244 int commit_one_metadata_packet(struct lttng_consumer_stream *stream)
2245 {
2246 ssize_t write_len;
2247 int ret;
2248
2249 pthread_mutex_lock(&stream->chan->metadata_cache->lock);
2250 ret = metadata_stream_check_version(stream);
2251 if (ret < 0) {
2252 goto end;
2253 }
2254 if (stream->chan->metadata_cache->max_offset
2255 == stream->ust_metadata_pushed) {
2256 ret = 0;
2257 goto end;
2258 }
2259
2260 write_len = ustctl_write_one_packet_to_channel(stream->chan->uchan,
2261 &stream->chan->metadata_cache->data[stream->ust_metadata_pushed],
2262 stream->chan->metadata_cache->max_offset
2263 - stream->ust_metadata_pushed);
2264 assert(write_len != 0);
2265 if (write_len < 0) {
2266 ERR("Writing one metadata packet");
2267 ret = -1;
2268 goto end;
2269 }
2270 stream->ust_metadata_pushed += write_len;
2271
2272 assert(stream->chan->metadata_cache->max_offset >=
2273 stream->ust_metadata_pushed);
2274 ret = write_len;
2275
2276 end:
2277 pthread_mutex_unlock(&stream->chan->metadata_cache->lock);
2278 return ret;
2279 }
2280
2281
2282 /*
2283 * Sync metadata meaning request them to the session daemon and snapshot to the
2284 * metadata thread can consumer them.
2285 *
2286 * Metadata stream lock is held here, but we need to release it when
2287 * interacting with sessiond, else we cause a deadlock with live
2288 * awaiting on metadata to be pushed out.
2289 *
2290 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
2291 * is empty or a negative value on error.
2292 */
2293 int lttng_ustconsumer_sync_metadata(struct lttng_consumer_local_data *ctx,
2294 struct lttng_consumer_stream *metadata)
2295 {
2296 int ret;
2297 int retry = 0;
2298
2299 assert(ctx);
2300 assert(metadata);
2301
2302 pthread_mutex_unlock(&metadata->lock);
2303 /*
2304 * Request metadata from the sessiond, but don't wait for the flush
2305 * because we locked the metadata thread.
2306 */
2307 ret = lttng_ustconsumer_request_metadata(ctx, metadata->chan, 0, 0);
2308 pthread_mutex_lock(&metadata->lock);
2309 if (ret < 0) {
2310 goto end;
2311 }
2312
2313 ret = commit_one_metadata_packet(metadata);
2314 if (ret <= 0) {
2315 goto end;
2316 } else if (ret > 0) {
2317 retry = 1;
2318 }
2319
2320 ustctl_flush_buffer(metadata->ustream, 1);
2321 ret = ustctl_snapshot(metadata->ustream);
2322 if (ret < 0) {
2323 if (errno != EAGAIN) {
2324 ERR("Sync metadata, taking UST snapshot");
2325 goto end;
2326 }
2327 DBG("No new metadata when syncing them.");
2328 /* No new metadata, exit. */
2329 ret = ENODATA;
2330 goto end;
2331 }
2332
2333 /*
2334 * After this flush, we still need to extract metadata.
2335 */
2336 if (retry) {
2337 ret = EAGAIN;
2338 }
2339
2340 end:
2341 return ret;
2342 }
2343
2344 /*
2345 * Return 0 on success else a negative value.
2346 */
2347 static int notify_if_more_data(struct lttng_consumer_stream *stream,
2348 struct lttng_consumer_local_data *ctx)
2349 {
2350 int ret;
2351 struct ustctl_consumer_stream *ustream;
2352
2353 assert(stream);
2354 assert(ctx);
2355
2356 ustream = stream->ustream;
2357
2358 /*
2359 * First, we are going to check if there is a new subbuffer available
2360 * before reading the stream wait_fd.
2361 */
2362 /* Get the next subbuffer */
2363 ret = ustctl_get_next_subbuf(ustream);
2364 if (ret) {
2365 /* No more data found, flag the stream. */
2366 stream->has_data = 0;
2367 ret = 0;
2368 goto end;
2369 }
2370
2371 ret = ustctl_put_subbuf(ustream);
2372 assert(!ret);
2373
2374 /* This stream still has data. Flag it and wake up the data thread. */
2375 stream->has_data = 1;
2376
2377 if (stream->monitor && !stream->hangup_flush_done && !ctx->has_wakeup) {
2378 ssize_t writelen;
2379
2380 writelen = lttng_pipe_write(ctx->consumer_wakeup_pipe, "!", 1);
2381 if (writelen < 0 && errno != EAGAIN && errno != EWOULDBLOCK) {
2382 ret = writelen;
2383 goto end;
2384 }
2385
2386 /* The wake up pipe has been notified. */
2387 ctx->has_wakeup = 1;
2388 }
2389 ret = 0;
2390
2391 end:
2392 return ret;
2393 }
2394
2395 static
2396 int update_stream_stats(struct lttng_consumer_stream *stream)
2397 {
2398 int ret;
2399 uint64_t seq, discarded;
2400
2401 ret = ustctl_get_sequence_number(stream->ustream, &seq);
2402 if (ret < 0) {
2403 PERROR("ustctl_get_sequence_number");
2404 goto end;
2405 }
2406 /*
2407 * Start the sequence when we extract the first packet in case we don't
2408 * start at 0 (for example if a consumer is not connected to the
2409 * session immediately after the beginning).
2410 */
2411 if (stream->last_sequence_number == -1ULL) {
2412 stream->last_sequence_number = seq;
2413 } else if (seq > stream->last_sequence_number) {
2414 stream->chan->lost_packets += seq -
2415 stream->last_sequence_number - 1;
2416 } else {
2417 /* seq <= last_sequence_number */
2418 ERR("Sequence number inconsistent : prev = %" PRIu64
2419 ", current = %" PRIu64,
2420 stream->last_sequence_number, seq);
2421 ret = -1;
2422 goto end;
2423 }
2424 stream->last_sequence_number = seq;
2425
2426 ret = ustctl_get_events_discarded(stream->ustream, &discarded);
2427 if (ret < 0) {
2428 PERROR("kernctl_get_events_discarded");
2429 goto end;
2430 }
2431 if (discarded < stream->last_discarded_events) {
2432 /*
2433 * Overflow has occurred. We assume only one wrap-around
2434 * has occurred.
2435 */
2436 stream->chan->discarded_events +=
2437 (1ULL << (CAA_BITS_PER_LONG - 1)) -
2438 stream->last_discarded_events + discarded;
2439 } else {
2440 stream->chan->discarded_events += discarded -
2441 stream->last_discarded_events;
2442 }
2443 stream->last_discarded_events = discarded;
2444 ret = 0;
2445
2446 end:
2447 return ret;
2448 }
2449
2450 /*
2451 * Read subbuffer from the given stream.
2452 *
2453 * Stream lock MUST be acquired.
2454 *
2455 * Return 0 on success else a negative value.
2456 */
2457 int lttng_ustconsumer_read_subbuffer(struct lttng_consumer_stream *stream,
2458 struct lttng_consumer_local_data *ctx)
2459 {
2460 unsigned long len, subbuf_size, padding;
2461 int err, write_index = 1;
2462 long ret = 0;
2463 struct ustctl_consumer_stream *ustream;
2464 struct ctf_packet_index index;
2465 const char *subbuf_addr;
2466 struct lttng_buffer_view subbuf_view;
2467
2468 assert(stream);
2469 assert(stream->ustream);
2470 assert(ctx);
2471
2472 DBG("In UST read_subbuffer (wait_fd: %d, name: %s)", stream->wait_fd,
2473 stream->name);
2474
2475 /* Ease our life for what's next. */
2476 ustream = stream->ustream;
2477
2478 /*
2479 * We can consume the 1 byte written into the wait_fd by UST. Don't trigger
2480 * error if we cannot read this one byte (read returns 0), or if the error
2481 * is EAGAIN or EWOULDBLOCK.
2482 *
2483 * This is only done when the stream is monitored by a thread, before the
2484 * flush is done after a hangup and if the stream is not flagged with data
2485 * since there might be nothing to consume in the wait fd but still have
2486 * data available flagged by the consumer wake up pipe.
2487 */
2488 if (stream->monitor && !stream->hangup_flush_done && !stream->has_data) {
2489 char dummy;
2490 ssize_t readlen;
2491
2492 readlen = lttng_read(stream->wait_fd, &dummy, 1);
2493 if (readlen < 0 && errno != EAGAIN && errno != EWOULDBLOCK) {
2494 ret = readlen;
2495 goto end;
2496 }
2497 }
2498
2499 retry:
2500 /* Get the next subbuffer */
2501 err = ustctl_get_next_subbuf(ustream);
2502 if (err != 0) {
2503 /*
2504 * Populate metadata info if the existing info has
2505 * already been read.
2506 */
2507 if (stream->metadata_flag) {
2508 ret = commit_one_metadata_packet(stream);
2509 if (ret <= 0) {
2510 goto end;
2511 }
2512 ustctl_flush_buffer(stream->ustream, 1);
2513 goto retry;
2514 }
2515
2516 ret = err; /* ustctl_get_next_subbuf returns negative, caller expect positive. */
2517 /*
2518 * This is a debug message even for single-threaded consumer,
2519 * because poll() have more relaxed criterions than get subbuf,
2520 * so get_subbuf may fail for short race windows where poll()
2521 * would issue wakeups.
2522 */
2523 DBG("Reserving sub buffer failed (everything is normal, "
2524 "it is due to concurrency) [ret: %d]", err);
2525 goto end;
2526 }
2527 assert(stream->chan->output == CONSUMER_CHANNEL_MMAP);
2528
2529 if (!stream->metadata_flag) {
2530 index.offset = htobe64(stream->out_fd_offset);
2531 ret = get_index_values(&index, ustream);
2532 if (ret < 0) {
2533 err = ustctl_put_subbuf(ustream);
2534 assert(err == 0);
2535 goto end;
2536 }
2537
2538 /* Update the stream's sequence and discarded events count. */
2539 ret = update_stream_stats(stream);
2540 if (ret < 0) {
2541 PERROR("kernctl_get_events_discarded");
2542 err = ustctl_put_subbuf(ustream);
2543 assert(err == 0);
2544 goto end;
2545 }
2546 } else {
2547 write_index = 0;
2548 }
2549
2550 /* Get the full padded subbuffer size */
2551 err = ustctl_get_padded_subbuf_size(ustream, &len);
2552 assert(err == 0);
2553
2554 /* Get subbuffer data size (without padding) */
2555 err = ustctl_get_subbuf_size(ustream, &subbuf_size);
2556 assert(err == 0);
2557
2558 /* Make sure we don't get a subbuffer size bigger than the padded */
2559 assert(len >= subbuf_size);
2560
2561 padding = len - subbuf_size;
2562
2563 ret = get_current_subbuf_addr(stream, &subbuf_addr);
2564 if (ret) {
2565 write_index = 0;
2566 goto error_put_subbuf;
2567 }
2568
2569 subbuf_view = lttng_buffer_view_init(subbuf_addr, 0, len);
2570
2571 /* write the subbuffer to the tracefile */
2572 ret = lttng_consumer_on_read_subbuffer_mmap(
2573 ctx, stream, &subbuf_view, padding, &index);
2574 /*
2575 * The mmap operation should write subbuf_size amount of data when
2576 * network streaming or the full padding (len) size when we are _not_
2577 * streaming.
2578 */
2579 if ((ret != subbuf_size && stream->relayd_id != (uint64_t) -1ULL) ||
2580 (ret != len && stream->relayd_id == (uint64_t) -1ULL)) {
2581 /*
2582 * Display the error but continue processing to try to release the
2583 * subbuffer. This is a DBG statement since any unexpected kill or
2584 * signal, the application gets unregistered, relayd gets closed or
2585 * anything that affects the buffer lifetime will trigger this error.
2586 * So, for the sake of the user, don't print this error since it can
2587 * happen and it is OK with the code flow.
2588 */
2589 DBG("Error writing to tracefile "
2590 "(ret: %ld != len: %lu != subbuf_size: %lu)",
2591 ret, len, subbuf_size);
2592 write_index = 0;
2593 }
2594 error_put_subbuf:
2595 err = ustctl_put_next_subbuf(ustream);
2596 assert(err == 0);
2597
2598 /*
2599 * This will consumer the byte on the wait_fd if and only if there is not
2600 * next subbuffer to be acquired.
2601 */
2602 if (!stream->metadata_flag) {
2603 ret = notify_if_more_data(stream, ctx);
2604 if (ret < 0) {
2605 goto end;
2606 }
2607 }
2608
2609 /* Write index if needed. */
2610 if (!write_index) {
2611 goto end;
2612 }
2613
2614 if (stream->chan->live_timer_interval && !stream->metadata_flag) {
2615 /*
2616 * In live, block until all the metadata is sent.
2617 */
2618 pthread_mutex_lock(&stream->metadata_timer_lock);
2619 assert(!stream->missed_metadata_flush);
2620 stream->waiting_on_metadata = true;
2621 pthread_mutex_unlock(&stream->metadata_timer_lock);
2622
2623 err = consumer_stream_sync_metadata(ctx, stream->session_id);
2624
2625 pthread_mutex_lock(&stream->metadata_timer_lock);
2626 stream->waiting_on_metadata = false;
2627 if (stream->missed_metadata_flush) {
2628 stream->missed_metadata_flush = false;
2629 pthread_mutex_unlock(&stream->metadata_timer_lock);
2630 (void) consumer_flush_ust_index(stream);
2631 } else {
2632 pthread_mutex_unlock(&stream->metadata_timer_lock);
2633 }
2634
2635 if (err < 0) {
2636 goto end;
2637 }
2638 }
2639
2640 assert(!stream->metadata_flag);
2641 err = consumer_stream_write_index(stream, &index);
2642 if (err < 0) {
2643 goto end;
2644 }
2645
2646 end:
2647 return ret;
2648 }
2649
2650 /*
2651 * Called when a stream is created.
2652 *
2653 * Return 0 on success or else a negative value.
2654 */
2655 int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream *stream)
2656 {
2657 int ret;
2658
2659 assert(stream);
2660
2661 /* Don't create anything if this is set for streaming. */
2662 if (stream->relayd_id == (uint64_t) -1ULL && stream->chan->monitor) {
2663 ret = utils_create_stream_file(stream->chan->pathname, stream->name,
2664 stream->chan->tracefile_size, stream->tracefile_count_current,
2665 stream->uid, stream->gid, NULL);
2666 if (ret < 0) {
2667 goto error;
2668 }
2669 stream->out_fd = ret;
2670 stream->tracefile_size_current = 0;
2671
2672 if (!stream->metadata_flag) {
2673 struct lttng_index_file *index_file;
2674
2675 index_file = lttng_index_file_create(stream->chan->pathname,
2676 stream->name, stream->uid, stream->gid,
2677 stream->chan->tracefile_size,
2678 stream->tracefile_count_current,
2679 CTF_INDEX_MAJOR, CTF_INDEX_MINOR);
2680 if (!index_file) {
2681 goto error;
2682 }
2683 stream->index_file = index_file;
2684 }
2685 }
2686 ret = 0;
2687
2688 error:
2689 return ret;
2690 }
2691
2692 /*
2693 * Check if data is still being extracted from the buffers for a specific
2694 * stream. Consumer data lock MUST be acquired before calling this function
2695 * and the stream lock.
2696 *
2697 * Return 1 if the traced data are still getting read else 0 meaning that the
2698 * data is available for trace viewer reading.
2699 */
2700 int lttng_ustconsumer_data_pending(struct lttng_consumer_stream *stream)
2701 {
2702 int ret;
2703
2704 assert(stream);
2705 assert(stream->ustream);
2706
2707 DBG("UST consumer checking data pending");
2708
2709 if (stream->endpoint_status != CONSUMER_ENDPOINT_ACTIVE) {
2710 ret = 0;
2711 goto end;
2712 }
2713
2714 if (stream->chan->type == CONSUMER_CHANNEL_TYPE_METADATA) {
2715 uint64_t contiguous, pushed;
2716
2717 /* Ease our life a bit. */
2718 contiguous = stream->chan->metadata_cache->max_offset;
2719 pushed = stream->ust_metadata_pushed;
2720
2721 /*
2722 * We can simply check whether all contiguously available data
2723 * has been pushed to the ring buffer, since the push operation
2724 * is performed within get_next_subbuf(), and because both
2725 * get_next_subbuf() and put_next_subbuf() are issued atomically
2726 * thanks to the stream lock within
2727 * lttng_ustconsumer_read_subbuffer(). This basically means that
2728 * whetnever ust_metadata_pushed is incremented, the associated
2729 * metadata has been consumed from the metadata stream.
2730 */
2731 DBG("UST consumer metadata pending check: contiguous %" PRIu64 " vs pushed %" PRIu64,
2732 contiguous, pushed);
2733 assert(((int64_t) (contiguous - pushed)) >= 0);
2734 if ((contiguous != pushed) ||
2735 (((int64_t) contiguous - pushed) > 0 || contiguous == 0)) {
2736 ret = 1; /* Data is pending */
2737 goto end;
2738 }
2739 } else {
2740 ret = ustctl_get_next_subbuf(stream->ustream);
2741 if (ret == 0) {
2742 /*
2743 * There is still data so let's put back this
2744 * subbuffer.
2745 */
2746 ret = ustctl_put_subbuf(stream->ustream);
2747 assert(ret == 0);
2748 ret = 1; /* Data is pending */
2749 goto end;
2750 }
2751 }
2752
2753 /* Data is NOT pending so ready to be read. */
2754 ret = 0;
2755
2756 end:
2757 return ret;
2758 }
2759
2760 /*
2761 * Stop a given metadata channel timer if enabled and close the wait fd which
2762 * is the poll pipe of the metadata stream.
2763 *
2764 * This MUST be called with the metadata channel acquired.
2765 */
2766 void lttng_ustconsumer_close_metadata(struct lttng_consumer_channel *metadata)
2767 {
2768 int ret;
2769
2770 assert(metadata);
2771 assert(metadata->type == CONSUMER_CHANNEL_TYPE_METADATA);
2772
2773 DBG("Closing metadata channel key %" PRIu64, metadata->key);
2774
2775 if (metadata->switch_timer_enabled == 1) {
2776 consumer_timer_switch_stop(metadata);
2777 }
2778
2779 if (!metadata->metadata_stream) {
2780 goto end;
2781 }
2782
2783 /*
2784 * Closing write side so the thread monitoring the stream wakes up if any
2785 * and clean the metadata stream.
2786 */
2787 if (metadata->metadata_stream->ust_metadata_poll_pipe[1] >= 0) {
2788 ret = close(metadata->metadata_stream->ust_metadata_poll_pipe[1]);
2789 if (ret < 0) {
2790 PERROR("closing metadata pipe write side");
2791 }
2792 metadata->metadata_stream->ust_metadata_poll_pipe[1] = -1;
2793 }
2794
2795 end:
2796 return;
2797 }
2798
2799 /*
2800 * Close every metadata stream wait fd of the metadata hash table. This
2801 * function MUST be used very carefully so not to run into a race between the
2802 * metadata thread handling streams and this function closing their wait fd.
2803 *
2804 * For UST, this is used when the session daemon hangs up. Its the metadata
2805 * producer so calling this is safe because we are assured that no state change
2806 * can occur in the metadata thread for the streams in the hash table.
2807 */
2808 void lttng_ustconsumer_close_all_metadata(struct lttng_ht *metadata_ht)
2809 {
2810 struct lttng_ht_iter iter;
2811 struct lttng_consumer_stream *stream;
2812
2813 assert(metadata_ht);
2814 assert(metadata_ht->ht);
2815
2816 DBG("UST consumer closing all metadata streams");
2817
2818 rcu_read_lock();
2819 cds_lfht_for_each_entry(metadata_ht->ht, &iter.iter, stream,
2820 node.node) {
2821
2822 health_code_update();
2823
2824 pthread_mutex_lock(&stream->chan->lock);
2825 lttng_ustconsumer_close_metadata(stream->chan);
2826 pthread_mutex_unlock(&stream->chan->lock);
2827
2828 }
2829 rcu_read_unlock();
2830 }
2831
2832 void lttng_ustconsumer_close_stream_wakeup(struct lttng_consumer_stream *stream)
2833 {
2834 int ret;
2835
2836 ret = ustctl_stream_close_wakeup_fd(stream->ustream);
2837 if (ret < 0) {
2838 ERR("Unable to close wakeup fd");
2839 }
2840 }
2841
2842 /*
2843 * Please refer to consumer-timer.c before adding any lock within this
2844 * function or any of its callees. Timers have a very strict locking
2845 * semantic with respect to teardown. Failure to respect this semantic
2846 * introduces deadlocks.
2847 *
2848 * DON'T hold the metadata lock when calling this function, else this
2849 * can cause deadlock involving consumer awaiting for metadata to be
2850 * pushed out due to concurrent interaction with the session daemon.
2851 */
2852 int lttng_ustconsumer_request_metadata(struct lttng_consumer_local_data *ctx,
2853 struct lttng_consumer_channel *channel, int timer, int wait)
2854 {
2855 struct lttcomm_metadata_request_msg request;
2856 struct lttcomm_consumer_msg msg;
2857 enum lttcomm_return_code ret_code = LTTCOMM_CONSUMERD_SUCCESS;
2858 uint64_t len, key, offset, version;
2859 int ret;
2860
2861 assert(channel);
2862 assert(channel->metadata_cache);
2863
2864 memset(&request, 0, sizeof(request));
2865
2866 /* send the metadata request to sessiond */
2867 switch (consumer_data.type) {
2868 case LTTNG_CONSUMER64_UST:
2869 request.bits_per_long = 64;
2870 break;
2871 case LTTNG_CONSUMER32_UST:
2872 request.bits_per_long = 32;
2873 break;
2874 default:
2875 request.bits_per_long = 0;
2876 break;
2877 }
2878
2879 request.session_id = channel->session_id;
2880 request.session_id_per_pid = channel->session_id_per_pid;
2881 /*
2882 * Request the application UID here so the metadata of that application can
2883 * be sent back. The channel UID corresponds to the user UID of the session
2884 * used for the rights on the stream file(s).
2885 */
2886 request.uid = channel->ust_app_uid;
2887 request.key = channel->key;
2888
2889 DBG("Sending metadata request to sessiond, session id %" PRIu64
2890 ", per-pid %" PRIu64 ", app UID %u and channel key %" PRIu64,
2891 request.session_id, request.session_id_per_pid, request.uid,
2892 request.key);
2893
2894 pthread_mutex_lock(&ctx->metadata_socket_lock);
2895
2896 health_code_update();
2897
2898 ret = lttcomm_send_unix_sock(ctx->consumer_metadata_socket, &request,
2899 sizeof(request));
2900 if (ret < 0) {
2901 ERR("Asking metadata to sessiond");
2902 goto end;
2903 }
2904
2905 health_code_update();
2906
2907 /* Receive the metadata from sessiond */
2908 ret = lttcomm_recv_unix_sock(ctx->consumer_metadata_socket, &msg,
2909 sizeof(msg));
2910 if (ret != sizeof(msg)) {
2911 DBG("Consumer received unexpected message size %d (expects %zu)",
2912 ret, sizeof(msg));
2913 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_CMD);
2914 /*
2915 * The ret value might 0 meaning an orderly shutdown but this is ok
2916 * since the caller handles this.
2917 */
2918 goto end;
2919 }
2920
2921 health_code_update();
2922
2923 if (msg.cmd_type == LTTNG_ERR_UND) {
2924 /* No registry found */
2925 (void) consumer_send_status_msg(ctx->consumer_metadata_socket,
2926 ret_code);
2927 ret = 0;
2928 goto end;
2929 } else if (msg.cmd_type != LTTNG_CONSUMER_PUSH_METADATA) {
2930 ERR("Unexpected cmd_type received %d", msg.cmd_type);
2931 ret = -1;
2932 goto end;
2933 }
2934
2935 len = msg.u.push_metadata.len;
2936 key = msg.u.push_metadata.key;
2937 offset = msg.u.push_metadata.target_offset;
2938 version = msg.u.push_metadata.version;
2939
2940 assert(key == channel->key);
2941 if (len == 0) {
2942 DBG("No new metadata to receive for key %" PRIu64, key);
2943 }
2944
2945 health_code_update();
2946
2947 /* Tell session daemon we are ready to receive the metadata. */
2948 ret = consumer_send_status_msg(ctx->consumer_metadata_socket,
2949 LTTCOMM_CONSUMERD_SUCCESS);
2950 if (ret < 0 || len == 0) {
2951 /*
2952 * Somehow, the session daemon is not responding anymore or there is
2953 * nothing to receive.
2954 */
2955 goto end;
2956 }
2957
2958 health_code_update();
2959
2960 ret = lttng_ustconsumer_recv_metadata(ctx->consumer_metadata_socket,
2961 key, offset, len, version, channel, timer, wait);
2962 if (ret >= 0) {
2963 /*
2964 * Only send the status msg if the sessiond is alive meaning a positive
2965 * ret code.
2966 */
2967 (void) consumer_send_status_msg(ctx->consumer_metadata_socket, ret);
2968 }
2969 ret = 0;
2970
2971 end:
2972 health_code_update();
2973
2974 pthread_mutex_unlock(&ctx->metadata_socket_lock);
2975 return ret;
2976 }
2977
2978 /*
2979 * Return the ustctl call for the get stream id.
2980 */
2981 int lttng_ustconsumer_get_stream_id(struct lttng_consumer_stream *stream,
2982 uint64_t *stream_id)
2983 {
2984 assert(stream);
2985 assert(stream_id);
2986
2987 return ustctl_get_stream_id(stream->ustream, stream_id);
2988 }
This page took 0.101021 seconds and 5 git commands to generate.