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