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