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