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