Adapt to lttng-ust ust-abi.h naming prefix update
[lttng-tools.git] / src / common / ust-consumer / ust-consumer.c
1 /*
2 * Copyright (C) 2011 Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 *
6 * SPDX-License-Identifier: GPL-2.0-only
7 *
8 */
9
10 #define _LGPL_SOURCE
11 #include <assert.h>
12 #include <lttng/ust-ctl.h>
13 #include <poll.h>
14 #include <pthread.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <sys/mman.h>
18 #include <sys/socket.h>
19 #include <sys/stat.h>
20 #include <sys/types.h>
21 #include <inttypes.h>
22 #include <unistd.h>
23 #include <urcu/list.h>
24 #include <signal.h>
25 #include <stdbool.h>
26 #include <stdint.h>
27
28 #include <bin/lttng-consumerd/health-consumerd.h>
29 #include <common/common.h>
30 #include <common/sessiond-comm/sessiond-comm.h>
31 #include <common/relayd/relayd.h>
32 #include <common/compat/fcntl.h>
33 #include <common/compat/endian.h>
34 #include <common/consumer/consumer-metadata-cache.h>
35 #include <common/consumer/consumer-stream.h>
36 #include <common/consumer/consumer-timer.h>
37 #include <common/utils.h>
38 #include <common/index/index.h>
39 #include <common/consumer/consumer.h>
40 #include <common/optional.h>
41
42 #include "ust-consumer.h"
43
44 #define INT_MAX_STR_LEN 12 /* includes \0 */
45
46 extern struct lttng_consumer_global_data consumer_data;
47 extern int consumer_poll_timeout;
48
49 /*
50 * Free channel object and all streams associated with it. This MUST be used
51 * only and only if the channel has _NEVER_ been added to the global channel
52 * hash table.
53 */
54 static void destroy_channel(struct lttng_consumer_channel *channel)
55 {
56 struct lttng_consumer_stream *stream, *stmp;
57
58 assert(channel);
59
60 DBG("UST consumer cleaning stream list");
61
62 cds_list_for_each_entry_safe(stream, stmp, &channel->streams.head,
63 send_node) {
64
65 health_code_update();
66
67 cds_list_del(&stream->send_node);
68 ustctl_destroy_stream(stream->ustream);
69 lttng_trace_chunk_put(stream->trace_chunk);
70 free(stream);
71 }
72
73 /*
74 * If a channel is available meaning that was created before the streams
75 * were, delete it.
76 */
77 if (channel->uchan) {
78 lttng_ustconsumer_del_channel(channel);
79 lttng_ustconsumer_free_channel(channel);
80 }
81
82 if (channel->trace_chunk) {
83 lttng_trace_chunk_put(channel->trace_chunk);
84 }
85
86 free(channel);
87 }
88
89 /*
90 * Add channel to internal consumer state.
91 *
92 * Returns 0 on success or else a negative value.
93 */
94 static int add_channel(struct lttng_consumer_channel *channel,
95 struct lttng_consumer_local_data *ctx)
96 {
97 int ret = 0;
98
99 assert(channel);
100 assert(ctx);
101
102 if (ctx->on_recv_channel != NULL) {
103 ret = ctx->on_recv_channel(channel);
104 if (ret == 0) {
105 ret = consumer_add_channel(channel, ctx);
106 } else if (ret < 0) {
107 /* Most likely an ENOMEM. */
108 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
109 goto error;
110 }
111 } else {
112 ret = consumer_add_channel(channel, ctx);
113 }
114
115 DBG("UST consumer channel added (key: %" PRIu64 ")", channel->key);
116
117 error:
118 return ret;
119 }
120
121 /*
122 * Allocate and return a consumer stream object. If _alloc_ret is not NULL, the
123 * error value if applicable is set in it else it is kept untouched.
124 *
125 * Return NULL on error else the newly allocated stream object.
126 */
127 static struct lttng_consumer_stream *allocate_stream(int cpu, int key,
128 struct lttng_consumer_channel *channel,
129 struct lttng_consumer_local_data *ctx, int *_alloc_ret)
130 {
131 int alloc_ret;
132 struct lttng_consumer_stream *stream = NULL;
133
134 assert(channel);
135 assert(ctx);
136
137 stream = consumer_stream_create(
138 channel,
139 channel->key,
140 key,
141 channel->name,
142 channel->relayd_id,
143 channel->session_id,
144 channel->trace_chunk,
145 cpu,
146 &alloc_ret,
147 channel->type,
148 channel->monitor);
149 if (stream == NULL) {
150 switch (alloc_ret) {
151 case -ENOENT:
152 /*
153 * We could not find the channel. Can happen if cpu hotplug
154 * happens while tearing down.
155 */
156 DBG3("Could not find channel");
157 break;
158 case -ENOMEM:
159 case -EINVAL:
160 default:
161 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
162 break;
163 }
164 goto error;
165 }
166
167 consumer_stream_update_channel_attributes(stream, channel);
168
169 error:
170 if (_alloc_ret) {
171 *_alloc_ret = alloc_ret;
172 }
173 return stream;
174 }
175
176 /*
177 * Send the given stream pointer to the corresponding thread.
178 *
179 * Returns 0 on success else a negative value.
180 */
181 static int send_stream_to_thread(struct lttng_consumer_stream *stream,
182 struct lttng_consumer_local_data *ctx)
183 {
184 int ret;
185 struct lttng_pipe *stream_pipe;
186
187 /* Get the right pipe where the stream will be sent. */
188 if (stream->metadata_flag) {
189 consumer_add_metadata_stream(stream);
190 stream_pipe = ctx->consumer_metadata_pipe;
191 } else {
192 consumer_add_data_stream(stream);
193 stream_pipe = ctx->consumer_data_pipe;
194 }
195
196 /*
197 * From this point on, the stream's ownership has been moved away from
198 * the channel and it becomes globally visible. Hence, remove it from
199 * the local stream list to prevent the stream from being both local and
200 * global.
201 */
202 stream->globally_visible = 1;
203 cds_list_del(&stream->send_node);
204
205 ret = lttng_pipe_write(stream_pipe, &stream, sizeof(stream));
206 if (ret < 0) {
207 ERR("Consumer write %s stream to pipe %d",
208 stream->metadata_flag ? "metadata" : "data",
209 lttng_pipe_get_writefd(stream_pipe));
210 if (stream->metadata_flag) {
211 consumer_del_stream_for_metadata(stream);
212 } else {
213 consumer_del_stream_for_data(stream);
214 }
215 goto error;
216 }
217
218 error:
219 return ret;
220 }
221
222 static
223 int get_stream_shm_path(char *stream_shm_path, const char *shm_path, int cpu)
224 {
225 char cpu_nr[INT_MAX_STR_LEN]; /* int max len */
226 int ret;
227
228 strncpy(stream_shm_path, shm_path, PATH_MAX);
229 stream_shm_path[PATH_MAX - 1] = '\0';
230 ret = snprintf(cpu_nr, INT_MAX_STR_LEN, "%i", cpu);
231 if (ret < 0) {
232 PERROR("snprintf");
233 goto end;
234 }
235 strncat(stream_shm_path, cpu_nr,
236 PATH_MAX - strlen(stream_shm_path) - 1);
237 ret = 0;
238 end:
239 return ret;
240 }
241
242 /*
243 * Create streams for the given channel using liblttng-ust-ctl.
244 * The channel lock must be acquired by the caller.
245 *
246 * Return 0 on success else a negative value.
247 */
248 static int create_ust_streams(struct lttng_consumer_channel *channel,
249 struct lttng_consumer_local_data *ctx)
250 {
251 int ret, cpu = 0;
252 struct ustctl_consumer_stream *ustream;
253 struct lttng_consumer_stream *stream;
254 pthread_mutex_t *current_stream_lock = NULL;
255
256 assert(channel);
257 assert(ctx);
258
259 /*
260 * While a stream is available from ustctl. When NULL is returned, we've
261 * reached the end of the possible stream for the channel.
262 */
263 while ((ustream = ustctl_create_stream(channel->uchan, cpu))) {
264 int wait_fd;
265 int ust_metadata_pipe[2];
266
267 health_code_update();
268
269 if (channel->type == CONSUMER_CHANNEL_TYPE_METADATA && channel->monitor) {
270 ret = utils_create_pipe_cloexec_nonblock(ust_metadata_pipe);
271 if (ret < 0) {
272 ERR("Create ust metadata poll pipe");
273 goto error;
274 }
275 wait_fd = ust_metadata_pipe[0];
276 } else {
277 wait_fd = ustctl_stream_get_wait_fd(ustream);
278 }
279
280 /* Allocate consumer stream object. */
281 stream = allocate_stream(cpu, wait_fd, channel, ctx, &ret);
282 if (!stream) {
283 goto error_alloc;
284 }
285 stream->ustream = ustream;
286 /*
287 * Store it so we can save multiple function calls afterwards since
288 * this value is used heavily in the stream threads. This is UST
289 * specific so this is why it's done after allocation.
290 */
291 stream->wait_fd = wait_fd;
292
293 /*
294 * Increment channel refcount since the channel reference has now been
295 * assigned in the allocation process above.
296 */
297 if (stream->chan->monitor) {
298 uatomic_inc(&stream->chan->refcount);
299 }
300
301 pthread_mutex_lock(&stream->lock);
302 current_stream_lock = &stream->lock;
303 /*
304 * Order is important this is why a list is used. On error, the caller
305 * should clean this list.
306 */
307 cds_list_add_tail(&stream->send_node, &channel->streams.head);
308
309 ret = ustctl_get_max_subbuf_size(stream->ustream,
310 &stream->max_sb_size);
311 if (ret < 0) {
312 ERR("ustctl_get_max_subbuf_size failed for stream %s",
313 stream->name);
314 goto error;
315 }
316
317 /* Do actions once stream has been received. */
318 if (ctx->on_recv_stream) {
319 ret = ctx->on_recv_stream(stream);
320 if (ret < 0) {
321 goto error;
322 }
323 }
324
325 DBG("UST consumer add stream %s (key: %" PRIu64 ") with relayd id %" PRIu64,
326 stream->name, stream->key, stream->relayd_stream_id);
327
328 /* Set next CPU stream. */
329 channel->streams.count = ++cpu;
330
331 /* Keep stream reference when creating metadata. */
332 if (channel->type == CONSUMER_CHANNEL_TYPE_METADATA) {
333 channel->metadata_stream = stream;
334 if (channel->monitor) {
335 /* Set metadata poll pipe if we created one */
336 memcpy(stream->ust_metadata_poll_pipe,
337 ust_metadata_pipe,
338 sizeof(ust_metadata_pipe));
339 }
340 }
341 pthread_mutex_unlock(&stream->lock);
342 current_stream_lock = NULL;
343 }
344
345 return 0;
346
347 error:
348 error_alloc:
349 if (current_stream_lock) {
350 pthread_mutex_unlock(current_stream_lock);
351 }
352 return ret;
353 }
354
355 /*
356 * create_posix_shm is never called concurrently within a process.
357 */
358 static
359 int create_posix_shm(void)
360 {
361 char tmp_name[NAME_MAX];
362 int shmfd, ret;
363
364 ret = snprintf(tmp_name, NAME_MAX, "/ust-shm-consumer-%d", getpid());
365 if (ret < 0) {
366 PERROR("snprintf");
367 return -1;
368 }
369 /*
370 * Allocate shm, and immediately unlink its shm oject, keeping
371 * only the file descriptor as a reference to the object.
372 * We specifically do _not_ use the / at the beginning of the
373 * pathname so that some OS implementations can keep it local to
374 * the process (POSIX leaves this implementation-defined).
375 */
376 shmfd = shm_open(tmp_name, O_CREAT | O_EXCL | O_RDWR, 0700);
377 if (shmfd < 0) {
378 PERROR("shm_open");
379 goto error_shm_open;
380 }
381 ret = shm_unlink(tmp_name);
382 if (ret < 0 && errno != ENOENT) {
383 PERROR("shm_unlink");
384 goto error_shm_release;
385 }
386 return shmfd;
387
388 error_shm_release:
389 ret = close(shmfd);
390 if (ret) {
391 PERROR("close");
392 }
393 error_shm_open:
394 return -1;
395 }
396
397 static int open_ust_stream_fd(struct lttng_consumer_channel *channel, int cpu,
398 const struct lttng_credentials *session_credentials)
399 {
400 char shm_path[PATH_MAX];
401 int ret;
402
403 if (!channel->shm_path[0]) {
404 return create_posix_shm();
405 }
406 ret = get_stream_shm_path(shm_path, channel->shm_path, cpu);
407 if (ret) {
408 goto error_shm_path;
409 }
410 return run_as_open(shm_path,
411 O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR,
412 lttng_credentials_get_uid(session_credentials),
413 lttng_credentials_get_gid(session_credentials));
414
415 error_shm_path:
416 return -1;
417 }
418
419 /*
420 * Create an UST channel with the given attributes and send it to the session
421 * daemon using the ust ctl API.
422 *
423 * Return 0 on success or else a negative value.
424 */
425 static int create_ust_channel(struct lttng_consumer_channel *channel,
426 struct ustctl_consumer_channel_attr *attr,
427 struct ustctl_consumer_channel **ust_chanp)
428 {
429 int ret, nr_stream_fds, i, j;
430 int *stream_fds;
431 struct ustctl_consumer_channel *ust_channel;
432
433 assert(channel);
434 assert(attr);
435 assert(ust_chanp);
436 assert(channel->buffer_credentials.is_set);
437
438 DBG3("Creating channel to ustctl with attr: [overwrite: %d, "
439 "subbuf_size: %" PRIu64 ", num_subbuf: %" PRIu64 ", "
440 "switch_timer_interval: %u, read_timer_interval: %u, "
441 "output: %d, type: %d", attr->overwrite, attr->subbuf_size,
442 attr->num_subbuf, attr->switch_timer_interval,
443 attr->read_timer_interval, attr->output, attr->type);
444
445 if (channel->type == CONSUMER_CHANNEL_TYPE_METADATA)
446 nr_stream_fds = 1;
447 else
448 nr_stream_fds = ustctl_get_nr_stream_per_channel();
449 stream_fds = zmalloc(nr_stream_fds * sizeof(*stream_fds));
450 if (!stream_fds) {
451 ret = -1;
452 goto error_alloc;
453 }
454 for (i = 0; i < nr_stream_fds; i++) {
455 stream_fds[i] = open_ust_stream_fd(channel, i,
456 &channel->buffer_credentials.value);
457 if (stream_fds[i] < 0) {
458 ret = -1;
459 goto error_open;
460 }
461 }
462 ust_channel = ustctl_create_channel(attr, stream_fds, nr_stream_fds);
463 if (!ust_channel) {
464 ret = -1;
465 goto error_create;
466 }
467 channel->nr_stream_fds = nr_stream_fds;
468 channel->stream_fds = stream_fds;
469 *ust_chanp = ust_channel;
470
471 return 0;
472
473 error_create:
474 error_open:
475 for (j = i - 1; j >= 0; j--) {
476 int closeret;
477
478 closeret = close(stream_fds[j]);
479 if (closeret) {
480 PERROR("close");
481 }
482 if (channel->shm_path[0]) {
483 char shm_path[PATH_MAX];
484
485 closeret = get_stream_shm_path(shm_path,
486 channel->shm_path, j);
487 if (closeret) {
488 ERR("Cannot get stream shm path");
489 }
490 closeret = run_as_unlink(shm_path,
491 lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(
492 channel->buffer_credentials)),
493 lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(
494 channel->buffer_credentials)));
495 if (closeret) {
496 PERROR("unlink %s", shm_path);
497 }
498 }
499 }
500 /* Try to rmdir all directories under shm_path root. */
501 if (channel->root_shm_path[0]) {
502 (void) run_as_rmdir_recursive(channel->root_shm_path,
503 lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(
504 channel->buffer_credentials)),
505 lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(
506 channel->buffer_credentials)),
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 static
1287 void metadata_stream_reset_cache_consumed_position(
1288 struct lttng_consumer_stream *stream)
1289 {
1290 ASSERT_LOCKED(stream->lock);
1291
1292 DBG("Reset metadata cache of session %" PRIu64,
1293 stream->chan->session_id);
1294 stream->ust_metadata_pushed = 0;
1295 }
1296
1297 /*
1298 * Receive the metadata updates from the sessiond. Supports receiving
1299 * overlapping metadata, but is needs to always belong to a contiguous
1300 * range starting from 0.
1301 * Be careful about the locks held when calling this function: it needs
1302 * the metadata cache flush to concurrently progress in order to
1303 * complete.
1304 */
1305 int lttng_ustconsumer_recv_metadata(int sock, uint64_t key, uint64_t offset,
1306 uint64_t len, uint64_t version,
1307 struct lttng_consumer_channel *channel, int timer, int wait)
1308 {
1309 int ret, ret_code = LTTCOMM_CONSUMERD_SUCCESS;
1310 char *metadata_str;
1311 enum consumer_metadata_cache_write_status cache_write_status;
1312
1313 DBG("UST consumer push metadata key %" PRIu64 " of len %" PRIu64, key, len);
1314
1315 metadata_str = zmalloc(len * sizeof(char));
1316 if (!metadata_str) {
1317 PERROR("zmalloc metadata string");
1318 ret_code = LTTCOMM_CONSUMERD_ENOMEM;
1319 goto end;
1320 }
1321
1322 health_code_update();
1323
1324 /* Receive metadata string. */
1325 ret = lttcomm_recv_unix_sock(sock, metadata_str, len);
1326 if (ret < 0) {
1327 /* Session daemon is dead so return gracefully. */
1328 ret_code = ret;
1329 goto end_free;
1330 }
1331
1332 health_code_update();
1333
1334 pthread_mutex_lock(&channel->metadata_cache->lock);
1335 cache_write_status = consumer_metadata_cache_write(
1336 channel->metadata_cache, offset, len, version,
1337 metadata_str);
1338 pthread_mutex_unlock(&channel->metadata_cache->lock);
1339 switch (cache_write_status) {
1340 case CONSUMER_METADATA_CACHE_WRITE_STATUS_NO_CHANGE:
1341 /*
1342 * The write entirely overlapped with existing contents of the
1343 * same metadata version (same content); there is nothing to do.
1344 */
1345 break;
1346 case CONSUMER_METADATA_CACHE_WRITE_STATUS_INVALIDATED:
1347 /*
1348 * The metadata cache was invalidated (previously pushed
1349 * content has been overwritten). Reset the stream's consumed
1350 * metadata position to ensure the metadata poll thread consumes
1351 * the whole cache.
1352 */
1353 pthread_mutex_lock(&channel->metadata_stream->lock);
1354 metadata_stream_reset_cache_consumed_position(
1355 channel->metadata_stream);
1356 pthread_mutex_unlock(&channel->metadata_stream->lock);
1357 /* Fall-through. */
1358 case CONSUMER_METADATA_CACHE_WRITE_STATUS_APPENDED_CONTENT:
1359 /*
1360 * In both cases, the metadata poll thread has new data to
1361 * consume.
1362 */
1363 ret = consumer_metadata_wakeup_pipe(channel);
1364 if (ret) {
1365 ret_code = LTTCOMM_CONSUMERD_ERROR_METADATA;
1366 goto end_free;
1367 }
1368 break;
1369 case CONSUMER_METADATA_CACHE_WRITE_STATUS_ERROR:
1370 /* Unable to handle metadata. Notify session daemon. */
1371 ret_code = LTTCOMM_CONSUMERD_ERROR_METADATA;
1372 /*
1373 * Skip metadata flush on write error since the offset and len might
1374 * not have been updated which could create an infinite loop below when
1375 * waiting for the metadata cache to be flushed.
1376 */
1377 goto end_free;
1378 default:
1379 abort();
1380 }
1381
1382 if (!wait) {
1383 goto end_free;
1384 }
1385 while (consumer_metadata_cache_flushed(channel, offset + len, timer)) {
1386 DBG("Waiting for metadata to be flushed");
1387
1388 health_code_update();
1389
1390 usleep(DEFAULT_METADATA_AVAILABILITY_WAIT_TIME);
1391 }
1392
1393 end_free:
1394 free(metadata_str);
1395 end:
1396 return ret_code;
1397 }
1398
1399 /*
1400 * Receive command from session daemon and process it.
1401 *
1402 * Return 1 on success else a negative value or 0.
1403 */
1404 int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
1405 int sock, struct pollfd *consumer_sockpoll)
1406 {
1407 ssize_t ret;
1408 enum lttcomm_return_code ret_code = LTTCOMM_CONSUMERD_SUCCESS;
1409 struct lttcomm_consumer_msg msg;
1410 struct lttng_consumer_channel *channel = NULL;
1411
1412 health_code_update();
1413
1414 ret = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg));
1415 if (ret != sizeof(msg)) {
1416 DBG("Consumer received unexpected message size %zd (expects %zu)",
1417 ret, sizeof(msg));
1418 /*
1419 * The ret value might 0 meaning an orderly shutdown but this is ok
1420 * since the caller handles this.
1421 */
1422 if (ret > 0) {
1423 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_CMD);
1424 ret = -1;
1425 }
1426 return ret;
1427 }
1428
1429 health_code_update();
1430
1431 /* deprecated */
1432 assert(msg.cmd_type != LTTNG_CONSUMER_STOP);
1433
1434 health_code_update();
1435
1436 /* relayd needs RCU read-side lock */
1437 rcu_read_lock();
1438
1439 switch (msg.cmd_type) {
1440 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET:
1441 {
1442 /* Session daemon status message are handled in the following call. */
1443 consumer_add_relayd_socket(msg.u.relayd_sock.net_index,
1444 msg.u.relayd_sock.type, ctx, sock, consumer_sockpoll,
1445 &msg.u.relayd_sock.sock, msg.u.relayd_sock.session_id,
1446 msg.u.relayd_sock.relayd_session_id);
1447 goto end_nosignal;
1448 }
1449 case LTTNG_CONSUMER_DESTROY_RELAYD:
1450 {
1451 uint64_t index = msg.u.destroy_relayd.net_seq_idx;
1452 struct consumer_relayd_sock_pair *relayd;
1453
1454 DBG("UST consumer destroying relayd %" PRIu64, index);
1455
1456 /* Get relayd reference if exists. */
1457 relayd = consumer_find_relayd(index);
1458 if (relayd == NULL) {
1459 DBG("Unable to find relayd %" PRIu64, index);
1460 ret_code = LTTCOMM_CONSUMERD_RELAYD_FAIL;
1461 }
1462
1463 /*
1464 * Each relayd socket pair has a refcount of stream attached to it
1465 * which tells if the relayd is still active or not depending on the
1466 * refcount value.
1467 *
1468 * This will set the destroy flag of the relayd object and destroy it
1469 * if the refcount reaches zero when called.
1470 *
1471 * The destroy can happen either here or when a stream fd hangs up.
1472 */
1473 if (relayd) {
1474 consumer_flag_relayd_for_destroy(relayd);
1475 }
1476
1477 goto end_msg_sessiond;
1478 }
1479 case LTTNG_CONSUMER_UPDATE_STREAM:
1480 {
1481 rcu_read_unlock();
1482 return -ENOSYS;
1483 }
1484 case LTTNG_CONSUMER_DATA_PENDING:
1485 {
1486 int ret, is_data_pending;
1487 uint64_t id = msg.u.data_pending.session_id;
1488
1489 DBG("UST consumer data pending command for id %" PRIu64, id);
1490
1491 is_data_pending = consumer_data_pending(id);
1492
1493 /* Send back returned value to session daemon */
1494 ret = lttcomm_send_unix_sock(sock, &is_data_pending,
1495 sizeof(is_data_pending));
1496 if (ret < 0) {
1497 DBG("Error when sending the data pending ret code: %d", ret);
1498 goto error_fatal;
1499 }
1500
1501 /*
1502 * No need to send back a status message since the data pending
1503 * returned value is the response.
1504 */
1505 break;
1506 }
1507 case LTTNG_CONSUMER_ASK_CHANNEL_CREATION:
1508 {
1509 int ret;
1510 struct ustctl_consumer_channel_attr attr;
1511 const uint64_t chunk_id = msg.u.ask_channel.chunk_id.value;
1512 const struct lttng_credentials buffer_credentials = {
1513 .uid = LTTNG_OPTIONAL_INIT_VALUE(msg.u.ask_channel.buffer_credentials.uid),
1514 .gid = LTTNG_OPTIONAL_INIT_VALUE(msg.u.ask_channel.buffer_credentials.gid),
1515 };
1516
1517 /* Create a plain object and reserve a channel key. */
1518 channel = consumer_allocate_channel(
1519 msg.u.ask_channel.key,
1520 msg.u.ask_channel.session_id,
1521 msg.u.ask_channel.chunk_id.is_set ?
1522 &chunk_id : NULL,
1523 msg.u.ask_channel.pathname,
1524 msg.u.ask_channel.name,
1525 msg.u.ask_channel.relayd_id,
1526 (enum lttng_event_output) msg.u.ask_channel.output,
1527 msg.u.ask_channel.tracefile_size,
1528 msg.u.ask_channel.tracefile_count,
1529 msg.u.ask_channel.session_id_per_pid,
1530 msg.u.ask_channel.monitor,
1531 msg.u.ask_channel.live_timer_interval,
1532 msg.u.ask_channel.is_live,
1533 msg.u.ask_channel.root_shm_path,
1534 msg.u.ask_channel.shm_path);
1535 if (!channel) {
1536 goto end_channel_error;
1537 }
1538
1539 LTTNG_OPTIONAL_SET(&channel->buffer_credentials,
1540 buffer_credentials);
1541
1542 /*
1543 * Assign UST application UID to the channel. This value is ignored for
1544 * per PID buffers. This is specific to UST thus setting this after the
1545 * allocation.
1546 */
1547 channel->ust_app_uid = msg.u.ask_channel.ust_app_uid;
1548
1549 /* Build channel attributes from received message. */
1550 attr.subbuf_size = msg.u.ask_channel.subbuf_size;
1551 attr.num_subbuf = msg.u.ask_channel.num_subbuf;
1552 attr.overwrite = msg.u.ask_channel.overwrite;
1553 attr.switch_timer_interval = msg.u.ask_channel.switch_timer_interval;
1554 attr.read_timer_interval = msg.u.ask_channel.read_timer_interval;
1555 attr.chan_id = msg.u.ask_channel.chan_id;
1556 memcpy(attr.uuid, msg.u.ask_channel.uuid, sizeof(attr.uuid));
1557 attr.blocking_timeout= msg.u.ask_channel.blocking_timeout;
1558
1559 /* Match channel buffer type to the UST abi. */
1560 switch (msg.u.ask_channel.output) {
1561 case LTTNG_EVENT_MMAP:
1562 default:
1563 attr.output = LTTNG_UST_ABI_MMAP;
1564 break;
1565 }
1566
1567 /* Translate and save channel type. */
1568 switch (msg.u.ask_channel.type) {
1569 case LTTNG_UST_ABI_CHAN_PER_CPU:
1570 channel->type = CONSUMER_CHANNEL_TYPE_DATA;
1571 attr.type = LTTNG_UST_ABI_CHAN_PER_CPU;
1572 /*
1573 * Set refcount to 1 for owner. Below, we will
1574 * pass ownership to the
1575 * consumer_thread_channel_poll() thread.
1576 */
1577 channel->refcount = 1;
1578 break;
1579 case LTTNG_UST_ABI_CHAN_METADATA:
1580 channel->type = CONSUMER_CHANNEL_TYPE_METADATA;
1581 attr.type = LTTNG_UST_ABI_CHAN_METADATA;
1582 break;
1583 default:
1584 assert(0);
1585 goto error_fatal;
1586 };
1587
1588 health_code_update();
1589
1590 ret = ask_channel(ctx, channel, &attr);
1591 if (ret < 0) {
1592 goto end_channel_error;
1593 }
1594
1595 if (msg.u.ask_channel.type == LTTNG_UST_ABI_CHAN_METADATA) {
1596 ret = consumer_metadata_cache_allocate(channel);
1597 if (ret < 0) {
1598 ERR("Allocating metadata cache");
1599 goto end_channel_error;
1600 }
1601 consumer_timer_switch_start(channel, attr.switch_timer_interval);
1602 attr.switch_timer_interval = 0;
1603 } else {
1604 int monitor_start_ret;
1605
1606 consumer_timer_live_start(channel,
1607 msg.u.ask_channel.live_timer_interval);
1608 monitor_start_ret = consumer_timer_monitor_start(
1609 channel,
1610 msg.u.ask_channel.monitor_timer_interval);
1611 if (monitor_start_ret < 0) {
1612 ERR("Starting channel monitoring timer failed");
1613 goto end_channel_error;
1614 }
1615 }
1616
1617 health_code_update();
1618
1619 /*
1620 * Add the channel to the internal state AFTER all streams were created
1621 * and successfully sent to session daemon. This way, all streams must
1622 * be ready before this channel is visible to the threads.
1623 * If add_channel succeeds, ownership of the channel is
1624 * passed to consumer_thread_channel_poll().
1625 */
1626 ret = add_channel(channel, ctx);
1627 if (ret < 0) {
1628 if (msg.u.ask_channel.type == LTTNG_UST_ABI_CHAN_METADATA) {
1629 if (channel->switch_timer_enabled == 1) {
1630 consumer_timer_switch_stop(channel);
1631 }
1632 consumer_metadata_cache_destroy(channel);
1633 }
1634 if (channel->live_timer_enabled == 1) {
1635 consumer_timer_live_stop(channel);
1636 }
1637 if (channel->monitor_timer_enabled == 1) {
1638 consumer_timer_monitor_stop(channel);
1639 }
1640 goto end_channel_error;
1641 }
1642
1643 health_code_update();
1644
1645 /*
1646 * Channel and streams are now created. Inform the session daemon that
1647 * everything went well and should wait to receive the channel and
1648 * streams with ustctl API.
1649 */
1650 ret = consumer_send_status_channel(sock, channel);
1651 if (ret < 0) {
1652 /*
1653 * There is probably a problem on the socket.
1654 */
1655 goto error_fatal;
1656 }
1657
1658 break;
1659 }
1660 case LTTNG_CONSUMER_GET_CHANNEL:
1661 {
1662 int ret, relayd_err = 0;
1663 uint64_t key = msg.u.get_channel.key;
1664 struct lttng_consumer_channel *channel;
1665
1666 channel = consumer_find_channel(key);
1667 if (!channel) {
1668 ERR("UST consumer get channel key %" PRIu64 " not found", key);
1669 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
1670 goto end_get_channel;
1671 }
1672
1673 health_code_update();
1674
1675 /* Send the channel to sessiond (and relayd, if applicable). */
1676 ret = send_channel_to_sessiond_and_relayd(sock, channel, ctx,
1677 &relayd_err);
1678 if (ret < 0) {
1679 if (relayd_err) {
1680 /*
1681 * We were unable to send to the relayd the stream so avoid
1682 * sending back a fatal error to the thread since this is OK
1683 * and the consumer can continue its work. The above call
1684 * has sent the error status message to the sessiond.
1685 */
1686 goto end_get_channel_nosignal;
1687 }
1688 /*
1689 * The communicaton was broken hence there is a bad state between
1690 * the consumer and sessiond so stop everything.
1691 */
1692 goto error_get_channel_fatal;
1693 }
1694
1695 health_code_update();
1696
1697 /*
1698 * In no monitor mode, the streams ownership is kept inside the channel
1699 * so don't send them to the data thread.
1700 */
1701 if (!channel->monitor) {
1702 goto end_get_channel;
1703 }
1704
1705 ret = send_streams_to_thread(channel, ctx);
1706 if (ret < 0) {
1707 /*
1708 * If we are unable to send the stream to the thread, there is
1709 * a big problem so just stop everything.
1710 */
1711 goto error_get_channel_fatal;
1712 }
1713 /* List MUST be empty after or else it could be reused. */
1714 assert(cds_list_empty(&channel->streams.head));
1715 end_get_channel:
1716 goto end_msg_sessiond;
1717 error_get_channel_fatal:
1718 goto error_fatal;
1719 end_get_channel_nosignal:
1720 goto end_nosignal;
1721 }
1722 case LTTNG_CONSUMER_DESTROY_CHANNEL:
1723 {
1724 uint64_t key = msg.u.destroy_channel.key;
1725
1726 /*
1727 * Only called if streams have not been sent to stream
1728 * manager thread. However, channel has been sent to
1729 * channel manager thread.
1730 */
1731 notify_thread_del_channel(ctx, key);
1732 goto end_msg_sessiond;
1733 }
1734 case LTTNG_CONSUMER_CLOSE_METADATA:
1735 {
1736 int ret;
1737
1738 ret = close_metadata(msg.u.close_metadata.key);
1739 if (ret != 0) {
1740 ret_code = ret;
1741 }
1742
1743 goto end_msg_sessiond;
1744 }
1745 case LTTNG_CONSUMER_FLUSH_CHANNEL:
1746 {
1747 int ret;
1748
1749 ret = flush_channel(msg.u.flush_channel.key);
1750 if (ret != 0) {
1751 ret_code = ret;
1752 }
1753
1754 goto end_msg_sessiond;
1755 }
1756 case LTTNG_CONSUMER_CLEAR_QUIESCENT_CHANNEL:
1757 {
1758 int ret;
1759
1760 ret = clear_quiescent_channel(
1761 msg.u.clear_quiescent_channel.key);
1762 if (ret != 0) {
1763 ret_code = ret;
1764 }
1765
1766 goto end_msg_sessiond;
1767 }
1768 case LTTNG_CONSUMER_PUSH_METADATA:
1769 {
1770 int ret;
1771 uint64_t len = msg.u.push_metadata.len;
1772 uint64_t key = msg.u.push_metadata.key;
1773 uint64_t offset = msg.u.push_metadata.target_offset;
1774 uint64_t version = msg.u.push_metadata.version;
1775 struct lttng_consumer_channel *channel;
1776
1777 DBG("UST consumer push metadata key %" PRIu64 " of len %" PRIu64, key,
1778 len);
1779
1780 channel = consumer_find_channel(key);
1781 if (!channel) {
1782 /*
1783 * This is possible if the metadata creation on the consumer side
1784 * is in flight vis-a-vis a concurrent push metadata from the
1785 * session daemon. Simply return that the channel failed and the
1786 * session daemon will handle that message correctly considering
1787 * that this race is acceptable thus the DBG() statement here.
1788 */
1789 DBG("UST consumer push metadata %" PRIu64 " not found", key);
1790 ret_code = LTTCOMM_CONSUMERD_CHANNEL_FAIL;
1791 goto end_push_metadata_msg_sessiond;
1792 }
1793
1794 health_code_update();
1795
1796 if (!len) {
1797 /*
1798 * There is nothing to receive. We have simply
1799 * checked whether the channel can be found.
1800 */
1801 ret_code = LTTCOMM_CONSUMERD_SUCCESS;
1802 goto end_push_metadata_msg_sessiond;
1803 }
1804
1805 /* Tell session daemon we are ready to receive the metadata. */
1806 ret = consumer_send_status_msg(sock, LTTCOMM_CONSUMERD_SUCCESS);
1807 if (ret < 0) {
1808 /* Somehow, the session daemon is not responding anymore. */
1809 goto error_push_metadata_fatal;
1810 }
1811
1812 health_code_update();
1813
1814 /* Wait for more data. */
1815 health_poll_entry();
1816 ret = lttng_consumer_poll_socket(consumer_sockpoll);
1817 health_poll_exit();
1818 if (ret) {
1819 goto error_push_metadata_fatal;
1820 }
1821
1822 health_code_update();
1823
1824 ret = lttng_ustconsumer_recv_metadata(sock, key, offset,
1825 len, version, channel, 0, 1);
1826 if (ret < 0) {
1827 /* error receiving from sessiond */
1828 goto error_push_metadata_fatal;
1829 } else {
1830 ret_code = ret;
1831 goto end_push_metadata_msg_sessiond;
1832 }
1833 end_push_metadata_msg_sessiond:
1834 goto end_msg_sessiond;
1835 error_push_metadata_fatal:
1836 goto error_fatal;
1837 }
1838 case LTTNG_CONSUMER_SETUP_METADATA:
1839 {
1840 int ret;
1841
1842 ret = setup_metadata(ctx, msg.u.setup_metadata.key);
1843 if (ret) {
1844 ret_code = ret;
1845 }
1846 goto end_msg_sessiond;
1847 }
1848 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL:
1849 {
1850 struct lttng_consumer_channel *channel;
1851 uint64_t key = msg.u.snapshot_channel.key;
1852
1853 channel = consumer_find_channel(key);
1854 if (!channel) {
1855 DBG("UST snapshot channel not found for key %" PRIu64, key);
1856 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
1857 } else {
1858 if (msg.u.snapshot_channel.metadata) {
1859 ret = snapshot_metadata(channel, key,
1860 msg.u.snapshot_channel.pathname,
1861 msg.u.snapshot_channel.relayd_id,
1862 ctx);
1863 if (ret < 0) {
1864 ERR("Snapshot metadata failed");
1865 ret_code = LTTCOMM_CONSUMERD_SNAPSHOT_FAILED;
1866 }
1867 } else {
1868 ret = snapshot_channel(channel, key,
1869 msg.u.snapshot_channel.pathname,
1870 msg.u.snapshot_channel.relayd_id,
1871 msg.u.snapshot_channel.nb_packets_per_stream,
1872 ctx);
1873 if (ret < 0) {
1874 ERR("Snapshot channel failed");
1875 ret_code = LTTCOMM_CONSUMERD_SNAPSHOT_FAILED;
1876 }
1877 }
1878 }
1879 health_code_update();
1880 ret = consumer_send_status_msg(sock, ret_code);
1881 if (ret < 0) {
1882 /* Somehow, the session daemon is not responding anymore. */
1883 goto end_nosignal;
1884 }
1885 health_code_update();
1886 break;
1887 }
1888 case LTTNG_CONSUMER_DISCARDED_EVENTS:
1889 {
1890 int ret = 0;
1891 uint64_t discarded_events;
1892 struct lttng_ht_iter iter;
1893 struct lttng_ht *ht;
1894 struct lttng_consumer_stream *stream;
1895 uint64_t id = msg.u.discarded_events.session_id;
1896 uint64_t key = msg.u.discarded_events.channel_key;
1897
1898 DBG("UST consumer discarded events command for session id %"
1899 PRIu64, id);
1900 rcu_read_lock();
1901 pthread_mutex_lock(&consumer_data.lock);
1902
1903 ht = consumer_data.stream_list_ht;
1904
1905 /*
1906 * We only need a reference to the channel, but they are not
1907 * directly indexed, so we just use the first matching stream
1908 * to extract the information we need, we default to 0 if not
1909 * found (no events are dropped if the channel is not yet in
1910 * use).
1911 */
1912 discarded_events = 0;
1913 cds_lfht_for_each_entry_duplicate(ht->ht,
1914 ht->hash_fct(&id, lttng_ht_seed),
1915 ht->match_fct, &id,
1916 &iter.iter, stream, node_session_id.node) {
1917 if (stream->chan->key == key) {
1918 discarded_events = stream->chan->discarded_events;
1919 break;
1920 }
1921 }
1922 pthread_mutex_unlock(&consumer_data.lock);
1923 rcu_read_unlock();
1924
1925 DBG("UST consumer discarded events command for session id %"
1926 PRIu64 ", channel key %" PRIu64, id, key);
1927
1928 health_code_update();
1929
1930 /* Send back returned value to session daemon */
1931 ret = lttcomm_send_unix_sock(sock, &discarded_events, sizeof(discarded_events));
1932 if (ret < 0) {
1933 PERROR("send discarded events");
1934 goto error_fatal;
1935 }
1936
1937 break;
1938 }
1939 case LTTNG_CONSUMER_LOST_PACKETS:
1940 {
1941 int ret;
1942 uint64_t lost_packets;
1943 struct lttng_ht_iter iter;
1944 struct lttng_ht *ht;
1945 struct lttng_consumer_stream *stream;
1946 uint64_t id = msg.u.lost_packets.session_id;
1947 uint64_t key = msg.u.lost_packets.channel_key;
1948
1949 DBG("UST consumer lost packets command for session id %"
1950 PRIu64, id);
1951 rcu_read_lock();
1952 pthread_mutex_lock(&consumer_data.lock);
1953
1954 ht = consumer_data.stream_list_ht;
1955
1956 /*
1957 * We only need a reference to the channel, but they are not
1958 * directly indexed, so we just use the first matching stream
1959 * to extract the information we need, we default to 0 if not
1960 * found (no packets lost if the channel is not yet in use).
1961 */
1962 lost_packets = 0;
1963 cds_lfht_for_each_entry_duplicate(ht->ht,
1964 ht->hash_fct(&id, lttng_ht_seed),
1965 ht->match_fct, &id,
1966 &iter.iter, stream, node_session_id.node) {
1967 if (stream->chan->key == key) {
1968 lost_packets = stream->chan->lost_packets;
1969 break;
1970 }
1971 }
1972 pthread_mutex_unlock(&consumer_data.lock);
1973 rcu_read_unlock();
1974
1975 DBG("UST consumer lost packets command for session id %"
1976 PRIu64 ", channel key %" PRIu64, id, key);
1977
1978 health_code_update();
1979
1980 /* Send back returned value to session daemon */
1981 ret = lttcomm_send_unix_sock(sock, &lost_packets,
1982 sizeof(lost_packets));
1983 if (ret < 0) {
1984 PERROR("send lost packets");
1985 goto error_fatal;
1986 }
1987
1988 break;
1989 }
1990 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE:
1991 {
1992 int channel_monitor_pipe;
1993
1994 ret_code = LTTCOMM_CONSUMERD_SUCCESS;
1995 /* Successfully received the command's type. */
1996 ret = consumer_send_status_msg(sock, ret_code);
1997 if (ret < 0) {
1998 goto error_fatal;
1999 }
2000
2001 ret = lttcomm_recv_fds_unix_sock(sock, &channel_monitor_pipe,
2002 1);
2003 if (ret != sizeof(channel_monitor_pipe)) {
2004 ERR("Failed to receive channel monitor pipe");
2005 goto error_fatal;
2006 }
2007
2008 DBG("Received channel monitor pipe (%d)", channel_monitor_pipe);
2009 ret = consumer_timer_thread_set_channel_monitor_pipe(
2010 channel_monitor_pipe);
2011 if (!ret) {
2012 int flags;
2013
2014 ret_code = LTTCOMM_CONSUMERD_SUCCESS;
2015 /* Set the pipe as non-blocking. */
2016 ret = fcntl(channel_monitor_pipe, F_GETFL, 0);
2017 if (ret == -1) {
2018 PERROR("fcntl get flags of the channel monitoring pipe");
2019 goto error_fatal;
2020 }
2021 flags = ret;
2022
2023 ret = fcntl(channel_monitor_pipe, F_SETFL,
2024 flags | O_NONBLOCK);
2025 if (ret == -1) {
2026 PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe");
2027 goto error_fatal;
2028 }
2029 DBG("Channel monitor pipe set as non-blocking");
2030 } else {
2031 ret_code = LTTCOMM_CONSUMERD_ALREADY_SET;
2032 }
2033 goto end_msg_sessiond;
2034 }
2035 case LTTNG_CONSUMER_ROTATE_CHANNEL:
2036 {
2037 struct lttng_consumer_channel *channel;
2038 uint64_t key = msg.u.rotate_channel.key;
2039
2040 channel = consumer_find_channel(key);
2041 if (!channel) {
2042 DBG("Channel %" PRIu64 " not found", key);
2043 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
2044 } else {
2045 /*
2046 * Sample the rotate position of all the streams in
2047 * this channel.
2048 */
2049 ret = lttng_consumer_rotate_channel(channel, key,
2050 msg.u.rotate_channel.relayd_id,
2051 msg.u.rotate_channel.metadata,
2052 ctx);
2053 if (ret < 0) {
2054 ERR("Rotate channel failed");
2055 ret_code = LTTCOMM_CONSUMERD_ROTATION_FAIL;
2056 }
2057
2058 health_code_update();
2059 }
2060 ret = consumer_send_status_msg(sock, ret_code);
2061 if (ret < 0) {
2062 /* Somehow, the session daemon is not responding anymore. */
2063 goto end_rotate_channel_nosignal;
2064 }
2065
2066 /*
2067 * Rotate the streams that are ready right now.
2068 * FIXME: this is a second consecutive iteration over the
2069 * streams in a channel, there is probably a better way to
2070 * handle this, but it needs to be after the
2071 * consumer_send_status_msg() call.
2072 */
2073 if (channel) {
2074 ret = lttng_consumer_rotate_ready_streams(
2075 channel, key, ctx);
2076 if (ret < 0) {
2077 ERR("Rotate channel failed");
2078 }
2079 }
2080 break;
2081 end_rotate_channel_nosignal:
2082 goto end_nosignal;
2083 }
2084 case LTTNG_CONSUMER_CLEAR_CHANNEL:
2085 {
2086 struct lttng_consumer_channel *channel;
2087 uint64_t key = msg.u.clear_channel.key;
2088
2089 channel = consumer_find_channel(key);
2090 if (!channel) {
2091 DBG("Channel %" PRIu64 " not found", key);
2092 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
2093 } else {
2094 ret = lttng_consumer_clear_channel(channel);
2095 if (ret) {
2096 ERR("Clear channel failed key %" PRIu64, key);
2097 ret_code = ret;
2098 }
2099
2100 health_code_update();
2101 }
2102 ret = consumer_send_status_msg(sock, ret_code);
2103 if (ret < 0) {
2104 /* Somehow, the session daemon is not responding anymore. */
2105 goto end_nosignal;
2106 }
2107 break;
2108 }
2109 case LTTNG_CONSUMER_INIT:
2110 {
2111 ret_code = lttng_consumer_init_command(ctx,
2112 msg.u.init.sessiond_uuid);
2113 health_code_update();
2114 ret = consumer_send_status_msg(sock, ret_code);
2115 if (ret < 0) {
2116 /* Somehow, the session daemon is not responding anymore. */
2117 goto end_nosignal;
2118 }
2119 break;
2120 }
2121 case LTTNG_CONSUMER_CREATE_TRACE_CHUNK:
2122 {
2123 const struct lttng_credentials credentials = {
2124 .uid = LTTNG_OPTIONAL_INIT_VALUE(msg.u.create_trace_chunk.credentials.value.uid),
2125 .gid = LTTNG_OPTIONAL_INIT_VALUE(msg.u.create_trace_chunk.credentials.value.gid),
2126 };
2127 const bool is_local_trace =
2128 !msg.u.create_trace_chunk.relayd_id.is_set;
2129 const uint64_t relayd_id =
2130 msg.u.create_trace_chunk.relayd_id.value;
2131 const char *chunk_override_name =
2132 *msg.u.create_trace_chunk.override_name ?
2133 msg.u.create_trace_chunk.override_name :
2134 NULL;
2135 struct lttng_directory_handle *chunk_directory_handle = NULL;
2136
2137 /*
2138 * The session daemon will only provide a chunk directory file
2139 * descriptor for local traces.
2140 */
2141 if (is_local_trace) {
2142 int chunk_dirfd;
2143
2144 /* Acnowledge the reception of the command. */
2145 ret = consumer_send_status_msg(sock,
2146 LTTCOMM_CONSUMERD_SUCCESS);
2147 if (ret < 0) {
2148 /* Somehow, the session daemon is not responding anymore. */
2149 goto end_nosignal;
2150 }
2151
2152 /*
2153 * Receive trace chunk domain dirfd.
2154 */
2155 ret = lttcomm_recv_fds_unix_sock(sock, &chunk_dirfd, 1);
2156 if (ret != sizeof(chunk_dirfd)) {
2157 ERR("Failed to receive trace chunk domain directory file descriptor");
2158 goto error_fatal;
2159 }
2160
2161 DBG("Received trace chunk domain directory fd (%d)",
2162 chunk_dirfd);
2163 chunk_directory_handle = lttng_directory_handle_create_from_dirfd(
2164 chunk_dirfd);
2165 if (!chunk_directory_handle) {
2166 ERR("Failed to initialize chunk domain directory handle from directory file descriptor");
2167 if (close(chunk_dirfd)) {
2168 PERROR("Failed to close chunk directory file descriptor");
2169 }
2170 goto error_fatal;
2171 }
2172 }
2173
2174 ret_code = lttng_consumer_create_trace_chunk(
2175 !is_local_trace ? &relayd_id : NULL,
2176 msg.u.create_trace_chunk.session_id,
2177 msg.u.create_trace_chunk.chunk_id,
2178 (time_t) msg.u.create_trace_chunk
2179 .creation_timestamp,
2180 chunk_override_name,
2181 msg.u.create_trace_chunk.credentials.is_set ?
2182 &credentials :
2183 NULL,
2184 chunk_directory_handle);
2185 lttng_directory_handle_put(chunk_directory_handle);
2186 goto end_msg_sessiond;
2187 }
2188 case LTTNG_CONSUMER_CLOSE_TRACE_CHUNK:
2189 {
2190 enum lttng_trace_chunk_command_type close_command =
2191 msg.u.close_trace_chunk.close_command.value;
2192 const uint64_t relayd_id =
2193 msg.u.close_trace_chunk.relayd_id.value;
2194 struct lttcomm_consumer_close_trace_chunk_reply reply;
2195 char closed_trace_chunk_path[LTTNG_PATH_MAX];
2196 int ret;
2197
2198 ret_code = lttng_consumer_close_trace_chunk(
2199 msg.u.close_trace_chunk.relayd_id.is_set ?
2200 &relayd_id :
2201 NULL,
2202 msg.u.close_trace_chunk.session_id,
2203 msg.u.close_trace_chunk.chunk_id,
2204 (time_t) msg.u.close_trace_chunk.close_timestamp,
2205 msg.u.close_trace_chunk.close_command.is_set ?
2206 &close_command :
2207 NULL, closed_trace_chunk_path);
2208 reply.ret_code = ret_code;
2209 reply.path_length = strlen(closed_trace_chunk_path) + 1;
2210 ret = lttcomm_send_unix_sock(sock, &reply, sizeof(reply));
2211 if (ret != sizeof(reply)) {
2212 goto error_fatal;
2213 }
2214 ret = lttcomm_send_unix_sock(sock, closed_trace_chunk_path,
2215 reply.path_length);
2216 if (ret != reply.path_length) {
2217 goto error_fatal;
2218 }
2219 goto end_nosignal;
2220 }
2221 case LTTNG_CONSUMER_TRACE_CHUNK_EXISTS:
2222 {
2223 const uint64_t relayd_id =
2224 msg.u.trace_chunk_exists.relayd_id.value;
2225
2226 ret_code = lttng_consumer_trace_chunk_exists(
2227 msg.u.trace_chunk_exists.relayd_id.is_set ?
2228 &relayd_id : NULL,
2229 msg.u.trace_chunk_exists.session_id,
2230 msg.u.trace_chunk_exists.chunk_id);
2231 goto end_msg_sessiond;
2232 }
2233 case LTTNG_CONSUMER_OPEN_CHANNEL_PACKETS:
2234 {
2235 const uint64_t key = msg.u.open_channel_packets.key;
2236 struct lttng_consumer_channel *channel =
2237 consumer_find_channel(key);
2238
2239 if (channel) {
2240 pthread_mutex_lock(&channel->lock);
2241 ret_code = lttng_consumer_open_channel_packets(channel);
2242 pthread_mutex_unlock(&channel->lock);
2243 } else {
2244 /*
2245 * The channel could have disappeared in per-pid
2246 * buffering mode.
2247 */
2248 DBG("Channel %" PRIu64 " not found", key);
2249 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
2250 }
2251
2252 health_code_update();
2253 goto end_msg_sessiond;
2254 }
2255 default:
2256 break;
2257 }
2258
2259 end_nosignal:
2260 /*
2261 * Return 1 to indicate success since the 0 value can be a socket
2262 * shutdown during the recv() or send() call.
2263 */
2264 ret = 1;
2265 goto end;
2266
2267 end_msg_sessiond:
2268 /*
2269 * The returned value here is not useful since either way we'll return 1 to
2270 * the caller because the session daemon socket management is done
2271 * elsewhere. Returning a negative code or 0 will shutdown the consumer.
2272 */
2273 ret = consumer_send_status_msg(sock, ret_code);
2274 if (ret < 0) {
2275 goto error_fatal;
2276 }
2277 ret = 1;
2278 goto end;
2279
2280 end_channel_error:
2281 if (channel) {
2282 /*
2283 * Free channel here since no one has a reference to it. We don't
2284 * free after that because a stream can store this pointer.
2285 */
2286 destroy_channel(channel);
2287 }
2288 /* We have to send a status channel message indicating an error. */
2289 ret = consumer_send_status_channel(sock, NULL);
2290 if (ret < 0) {
2291 /* Stop everything if session daemon can not be notified. */
2292 goto error_fatal;
2293 }
2294 ret = 1;
2295 goto end;
2296
2297 error_fatal:
2298 /* This will issue a consumer stop. */
2299 ret = -1;
2300 goto end;
2301
2302 end:
2303 rcu_read_unlock();
2304 health_code_update();
2305 return ret;
2306 }
2307
2308 void lttng_ustctl_flush_buffer(struct lttng_consumer_stream *stream,
2309 int producer_active)
2310 {
2311 assert(stream);
2312 assert(stream->ustream);
2313
2314 ustctl_flush_buffer(stream->ustream, producer_active);
2315 }
2316
2317 /*
2318 * Take a snapshot for a specific stream.
2319 *
2320 * Returns 0 on success, < 0 on error
2321 */
2322 int lttng_ustconsumer_take_snapshot(struct lttng_consumer_stream *stream)
2323 {
2324 assert(stream);
2325 assert(stream->ustream);
2326
2327 return ustctl_snapshot(stream->ustream);
2328 }
2329
2330 /*
2331 * Sample consumed and produced positions for a specific stream.
2332 *
2333 * Returns 0 on success, < 0 on error.
2334 */
2335 int lttng_ustconsumer_sample_snapshot_positions(
2336 struct lttng_consumer_stream *stream)
2337 {
2338 assert(stream);
2339 assert(stream->ustream);
2340
2341 return ustctl_snapshot_sample_positions(stream->ustream);
2342 }
2343
2344 /*
2345 * Get the produced position
2346 *
2347 * Returns 0 on success, < 0 on error
2348 */
2349 int lttng_ustconsumer_get_produced_snapshot(
2350 struct lttng_consumer_stream *stream, unsigned long *pos)
2351 {
2352 assert(stream);
2353 assert(stream->ustream);
2354 assert(pos);
2355
2356 return ustctl_snapshot_get_produced(stream->ustream, pos);
2357 }
2358
2359 /*
2360 * Get the consumed position
2361 *
2362 * Returns 0 on success, < 0 on error
2363 */
2364 int lttng_ustconsumer_get_consumed_snapshot(
2365 struct lttng_consumer_stream *stream, unsigned long *pos)
2366 {
2367 assert(stream);
2368 assert(stream->ustream);
2369 assert(pos);
2370
2371 return ustctl_snapshot_get_consumed(stream->ustream, pos);
2372 }
2373
2374 void lttng_ustconsumer_flush_buffer(struct lttng_consumer_stream *stream,
2375 int producer)
2376 {
2377 assert(stream);
2378 assert(stream->ustream);
2379
2380 ustctl_flush_buffer(stream->ustream, producer);
2381 }
2382
2383 void lttng_ustconsumer_clear_buffer(struct lttng_consumer_stream *stream)
2384 {
2385 assert(stream);
2386 assert(stream->ustream);
2387
2388 ustctl_clear_buffer(stream->ustream);
2389 }
2390
2391 int lttng_ustconsumer_get_current_timestamp(
2392 struct lttng_consumer_stream *stream, uint64_t *ts)
2393 {
2394 assert(stream);
2395 assert(stream->ustream);
2396 assert(ts);
2397
2398 return ustctl_get_current_timestamp(stream->ustream, ts);
2399 }
2400
2401 int lttng_ustconsumer_get_sequence_number(
2402 struct lttng_consumer_stream *stream, uint64_t *seq)
2403 {
2404 assert(stream);
2405 assert(stream->ustream);
2406 assert(seq);
2407
2408 return ustctl_get_sequence_number(stream->ustream, seq);
2409 }
2410
2411 /*
2412 * Called when the stream signals the consumer that it has hung up.
2413 */
2414 void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream *stream)
2415 {
2416 assert(stream);
2417 assert(stream->ustream);
2418
2419 pthread_mutex_lock(&stream->lock);
2420 if (!stream->quiescent) {
2421 ustctl_flush_buffer(stream->ustream, 0);
2422 stream->quiescent = true;
2423 }
2424 pthread_mutex_unlock(&stream->lock);
2425 stream->hangup_flush_done = 1;
2426 }
2427
2428 void lttng_ustconsumer_del_channel(struct lttng_consumer_channel *chan)
2429 {
2430 int i;
2431
2432 assert(chan);
2433 assert(chan->uchan);
2434 assert(chan->buffer_credentials.is_set);
2435
2436 if (chan->switch_timer_enabled == 1) {
2437 consumer_timer_switch_stop(chan);
2438 }
2439 for (i = 0; i < chan->nr_stream_fds; i++) {
2440 int ret;
2441
2442 ret = close(chan->stream_fds[i]);
2443 if (ret) {
2444 PERROR("close");
2445 }
2446 if (chan->shm_path[0]) {
2447 char shm_path[PATH_MAX];
2448
2449 ret = get_stream_shm_path(shm_path, chan->shm_path, i);
2450 if (ret) {
2451 ERR("Cannot get stream shm path");
2452 }
2453 ret = run_as_unlink(shm_path,
2454 lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(
2455 chan->buffer_credentials)),
2456 lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(
2457 chan->buffer_credentials)));
2458 if (ret) {
2459 PERROR("unlink %s", shm_path);
2460 }
2461 }
2462 }
2463 }
2464
2465 void lttng_ustconsumer_free_channel(struct lttng_consumer_channel *chan)
2466 {
2467 assert(chan);
2468 assert(chan->uchan);
2469 assert(chan->buffer_credentials.is_set);
2470
2471 consumer_metadata_cache_destroy(chan);
2472 ustctl_destroy_channel(chan->uchan);
2473 /* Try to rmdir all directories under shm_path root. */
2474 if (chan->root_shm_path[0]) {
2475 (void) run_as_rmdir_recursive(chan->root_shm_path,
2476 lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(
2477 chan->buffer_credentials)),
2478 lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(
2479 chan->buffer_credentials)),
2480 LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG);
2481 }
2482 free(chan->stream_fds);
2483 }
2484
2485 void lttng_ustconsumer_del_stream(struct lttng_consumer_stream *stream)
2486 {
2487 assert(stream);
2488 assert(stream->ustream);
2489
2490 if (stream->chan->switch_timer_enabled == 1) {
2491 consumer_timer_switch_stop(stream->chan);
2492 }
2493 ustctl_destroy_stream(stream->ustream);
2494 }
2495
2496 int lttng_ustconsumer_get_wakeup_fd(struct lttng_consumer_stream *stream)
2497 {
2498 assert(stream);
2499 assert(stream->ustream);
2500
2501 return ustctl_stream_get_wakeup_fd(stream->ustream);
2502 }
2503
2504 int lttng_ustconsumer_close_wakeup_fd(struct lttng_consumer_stream *stream)
2505 {
2506 assert(stream);
2507 assert(stream->ustream);
2508
2509 return ustctl_stream_close_wakeup_fd(stream->ustream);
2510 }
2511
2512 /*
2513 * Write up to one packet from the metadata cache to the channel.
2514 *
2515 * Returns the number of bytes pushed from the cache into the ring buffer, or a
2516 * negative value on error.
2517 */
2518 static
2519 int commit_one_metadata_packet(struct lttng_consumer_stream *stream)
2520 {
2521 ssize_t write_len;
2522 int ret;
2523
2524 pthread_mutex_lock(&stream->chan->metadata_cache->lock);
2525 if (stream->chan->metadata_cache->contents.size ==
2526 stream->ust_metadata_pushed) {
2527 /*
2528 * In the context of a user space metadata channel, a
2529 * change in version can be detected in two ways:
2530 * 1) During the pre-consume of the `read_subbuffer` loop,
2531 * 2) When populating the metadata ring buffer (i.e. here).
2532 *
2533 * This function is invoked when there is no metadata
2534 * available in the ring-buffer. If all data was consumed
2535 * up to the size of the metadata cache, there is no metadata
2536 * to insert in the ring-buffer.
2537 *
2538 * However, the metadata version could still have changed (a
2539 * regeneration without any new data will yield the same cache
2540 * size).
2541 *
2542 * The cache's version is checked for a version change and the
2543 * consumed position is reset if one occurred.
2544 *
2545 * This check is only necessary for the user space domain as
2546 * it has to manage the cache explicitly. If this reset was not
2547 * performed, no metadata would be consumed (and no reset would
2548 * occur as part of the pre-consume) until the metadata size
2549 * exceeded the cache size.
2550 */
2551 if (stream->metadata_version !=
2552 stream->chan->metadata_cache->version) {
2553 metadata_stream_reset_cache_consumed_position(stream);
2554 consumer_stream_metadata_set_version(stream,
2555 stream->chan->metadata_cache->version);
2556 } else {
2557 ret = 0;
2558 goto end;
2559 }
2560 }
2561
2562 write_len = ustctl_write_one_packet_to_channel(stream->chan->uchan,
2563 &stream->chan->metadata_cache->contents.data[stream->ust_metadata_pushed],
2564 stream->chan->metadata_cache->contents.size -
2565 stream->ust_metadata_pushed);
2566 assert(write_len != 0);
2567 if (write_len < 0) {
2568 ERR("Writing one metadata packet");
2569 ret = write_len;
2570 goto end;
2571 }
2572 stream->ust_metadata_pushed += write_len;
2573
2574 assert(stream->chan->metadata_cache->contents.size >=
2575 stream->ust_metadata_pushed);
2576 ret = write_len;
2577
2578 /*
2579 * Switch packet (but don't open the next one) on every commit of
2580 * a metadata packet. Since the subbuffer is fully filled (with padding,
2581 * if needed), the stream is "quiescent" after this commit.
2582 */
2583 ustctl_flush_buffer(stream->ustream, 1);
2584 stream->quiescent = true;
2585 end:
2586 pthread_mutex_unlock(&stream->chan->metadata_cache->lock);
2587 return ret;
2588 }
2589
2590
2591 /*
2592 * Sync metadata meaning request them to the session daemon and snapshot to the
2593 * metadata thread can consumer them.
2594 *
2595 * Metadata stream lock is held here, but we need to release it when
2596 * interacting with sessiond, else we cause a deadlock with live
2597 * awaiting on metadata to be pushed out.
2598 *
2599 * The RCU read side lock must be held by the caller.
2600 */
2601 enum sync_metadata_status lttng_ustconsumer_sync_metadata(
2602 struct lttng_consumer_local_data *ctx,
2603 struct lttng_consumer_stream *metadata_stream)
2604 {
2605 int ret;
2606 enum sync_metadata_status status;
2607 struct lttng_consumer_channel *metadata_channel;
2608
2609 assert(ctx);
2610 assert(metadata_stream);
2611
2612 metadata_channel = metadata_stream->chan;
2613 pthread_mutex_unlock(&metadata_stream->lock);
2614 /*
2615 * Request metadata from the sessiond, but don't wait for the flush
2616 * because we locked the metadata thread.
2617 */
2618 ret = lttng_ustconsumer_request_metadata(ctx, metadata_channel, 0, 0);
2619 pthread_mutex_lock(&metadata_stream->lock);
2620 if (ret < 0) {
2621 status = SYNC_METADATA_STATUS_ERROR;
2622 goto end;
2623 }
2624
2625 /*
2626 * The metadata stream and channel can be deleted while the
2627 * metadata stream lock was released. The streamed is checked
2628 * for deletion before we use it further.
2629 *
2630 * Note that it is safe to access a logically-deleted stream since its
2631 * existence is still guaranteed by the RCU read side lock. However,
2632 * it should no longer be used. The close/deletion of the metadata
2633 * channel and stream already guarantees that all metadata has been
2634 * consumed. Therefore, there is nothing left to do in this function.
2635 */
2636 if (consumer_stream_is_deleted(metadata_stream)) {
2637 DBG("Metadata stream %" PRIu64 " was deleted during the metadata synchronization",
2638 metadata_stream->key);
2639 status = SYNC_METADATA_STATUS_NO_DATA;
2640 goto end;
2641 }
2642
2643 ret = commit_one_metadata_packet(metadata_stream);
2644 if (ret < 0) {
2645 status = SYNC_METADATA_STATUS_ERROR;
2646 goto end;
2647 } else if (ret > 0) {
2648 status = SYNC_METADATA_STATUS_NEW_DATA;
2649 } else /* ret == 0 */ {
2650 status = SYNC_METADATA_STATUS_NO_DATA;
2651 goto end;
2652 }
2653
2654 ret = ustctl_snapshot(metadata_stream->ustream);
2655 if (ret < 0) {
2656 ERR("Failed to take a snapshot of the metadata ring-buffer positions, ret = %d", ret);
2657 status = SYNC_METADATA_STATUS_ERROR;
2658 goto end;
2659 }
2660
2661 end:
2662 return status;
2663 }
2664
2665 /*
2666 * Return 0 on success else a negative value.
2667 */
2668 static int notify_if_more_data(struct lttng_consumer_stream *stream,
2669 struct lttng_consumer_local_data *ctx)
2670 {
2671 int ret;
2672 struct ustctl_consumer_stream *ustream;
2673
2674 assert(stream);
2675 assert(ctx);
2676
2677 ustream = stream->ustream;
2678
2679 /*
2680 * First, we are going to check if there is a new subbuffer available
2681 * before reading the stream wait_fd.
2682 */
2683 /* Get the next subbuffer */
2684 ret = ustctl_get_next_subbuf(ustream);
2685 if (ret) {
2686 /* No more data found, flag the stream. */
2687 stream->has_data = 0;
2688 ret = 0;
2689 goto end;
2690 }
2691
2692 ret = ustctl_put_subbuf(ustream);
2693 assert(!ret);
2694
2695 /* This stream still has data. Flag it and wake up the data thread. */
2696 stream->has_data = 1;
2697
2698 if (stream->monitor && !stream->hangup_flush_done && !ctx->has_wakeup) {
2699 ssize_t writelen;
2700
2701 writelen = lttng_pipe_write(ctx->consumer_wakeup_pipe, "!", 1);
2702 if (writelen < 0 && errno != EAGAIN && errno != EWOULDBLOCK) {
2703 ret = writelen;
2704 goto end;
2705 }
2706
2707 /* The wake up pipe has been notified. */
2708 ctx->has_wakeup = 1;
2709 }
2710 ret = 0;
2711
2712 end:
2713 return ret;
2714 }
2715
2716 static int consumer_stream_ust_on_wake_up(struct lttng_consumer_stream *stream)
2717 {
2718 int ret = 0;
2719
2720 /*
2721 * We can consume the 1 byte written into the wait_fd by
2722 * UST. Don't trigger error if we cannot read this one byte
2723 * (read returns 0), or if the error is EAGAIN or EWOULDBLOCK.
2724 *
2725 * This is only done when the stream is monitored by a thread,
2726 * before the flush is done after a hangup and if the stream
2727 * is not flagged with data since there might be nothing to
2728 * consume in the wait fd but still have data available
2729 * flagged by the consumer wake up pipe.
2730 */
2731 if (stream->monitor && !stream->hangup_flush_done && !stream->has_data) {
2732 char dummy;
2733 ssize_t readlen;
2734
2735 readlen = lttng_read(stream->wait_fd, &dummy, 1);
2736 if (readlen < 0 && errno != EAGAIN && errno != EWOULDBLOCK) {
2737 ret = readlen;
2738 }
2739 }
2740
2741 return ret;
2742 }
2743
2744 static int extract_common_subbuffer_info(struct lttng_consumer_stream *stream,
2745 struct stream_subbuffer *subbuf)
2746 {
2747 int ret;
2748
2749 ret = ustctl_get_subbuf_size(
2750 stream->ustream, &subbuf->info.data.subbuf_size);
2751 if (ret) {
2752 goto end;
2753 }
2754
2755 ret = ustctl_get_padded_subbuf_size(
2756 stream->ustream, &subbuf->info.data.padded_subbuf_size);
2757 if (ret) {
2758 goto end;
2759 }
2760
2761 end:
2762 return ret;
2763 }
2764
2765 static int extract_metadata_subbuffer_info(struct lttng_consumer_stream *stream,
2766 struct stream_subbuffer *subbuf)
2767 {
2768 int ret;
2769
2770 ret = extract_common_subbuffer_info(stream, subbuf);
2771 if (ret) {
2772 goto end;
2773 }
2774
2775 subbuf->info.metadata.version = stream->metadata_version;
2776
2777 end:
2778 return ret;
2779 }
2780
2781 static int extract_data_subbuffer_info(struct lttng_consumer_stream *stream,
2782 struct stream_subbuffer *subbuf)
2783 {
2784 int ret;
2785
2786 ret = extract_common_subbuffer_info(stream, subbuf);
2787 if (ret) {
2788 goto end;
2789 }
2790
2791 ret = ustctl_get_packet_size(
2792 stream->ustream, &subbuf->info.data.packet_size);
2793 if (ret < 0) {
2794 PERROR("Failed to get sub-buffer packet size");
2795 goto end;
2796 }
2797
2798 ret = ustctl_get_content_size(
2799 stream->ustream, &subbuf->info.data.content_size);
2800 if (ret < 0) {
2801 PERROR("Failed to get sub-buffer content size");
2802 goto end;
2803 }
2804
2805 ret = ustctl_get_timestamp_begin(
2806 stream->ustream, &subbuf->info.data.timestamp_begin);
2807 if (ret < 0) {
2808 PERROR("Failed to get sub-buffer begin timestamp");
2809 goto end;
2810 }
2811
2812 ret = ustctl_get_timestamp_end(
2813 stream->ustream, &subbuf->info.data.timestamp_end);
2814 if (ret < 0) {
2815 PERROR("Failed to get sub-buffer end timestamp");
2816 goto end;
2817 }
2818
2819 ret = ustctl_get_events_discarded(
2820 stream->ustream, &subbuf->info.data.events_discarded);
2821 if (ret) {
2822 PERROR("Failed to get sub-buffer events discarded count");
2823 goto end;
2824 }
2825
2826 ret = ustctl_get_sequence_number(stream->ustream,
2827 &subbuf->info.data.sequence_number.value);
2828 if (ret) {
2829 /* May not be supported by older LTTng-modules. */
2830 if (ret != -ENOTTY) {
2831 PERROR("Failed to get sub-buffer sequence number");
2832 goto end;
2833 }
2834 } else {
2835 subbuf->info.data.sequence_number.is_set = true;
2836 }
2837
2838 ret = ustctl_get_stream_id(
2839 stream->ustream, &subbuf->info.data.stream_id);
2840 if (ret < 0) {
2841 PERROR("Failed to get stream id");
2842 goto end;
2843 }
2844
2845 ret = ustctl_get_instance_id(stream->ustream,
2846 &subbuf->info.data.stream_instance_id.value);
2847 if (ret) {
2848 /* May not be supported by older LTTng-modules. */
2849 if (ret != -ENOTTY) {
2850 PERROR("Failed to get stream instance id");
2851 goto end;
2852 }
2853 } else {
2854 subbuf->info.data.stream_instance_id.is_set = true;
2855 }
2856 end:
2857 return ret;
2858 }
2859
2860 static int get_next_subbuffer_common(struct lttng_consumer_stream *stream,
2861 struct stream_subbuffer *subbuffer)
2862 {
2863 int ret;
2864 const char *addr;
2865
2866 ret = stream->read_subbuffer_ops.extract_subbuffer_info(
2867 stream, subbuffer);
2868 if (ret) {
2869 goto end;
2870 }
2871
2872 ret = get_current_subbuf_addr(stream, &addr);
2873 if (ret) {
2874 goto end;
2875 }
2876
2877 subbuffer->buffer.buffer = lttng_buffer_view_init(
2878 addr, 0, subbuffer->info.data.padded_subbuf_size);
2879 assert(subbuffer->buffer.buffer.data != NULL);
2880 end:
2881 return ret;
2882 }
2883
2884 static int get_next_subbuffer(struct lttng_consumer_stream *stream,
2885 struct stream_subbuffer *subbuffer)
2886 {
2887 int ret;
2888
2889 ret = ustctl_get_next_subbuf(stream->ustream);
2890 if (ret) {
2891 goto end;
2892 }
2893
2894 ret = get_next_subbuffer_common(stream, subbuffer);
2895 if (ret) {
2896 goto end;
2897 }
2898 end:
2899 return ret;
2900 }
2901
2902 static int get_next_subbuffer_metadata(struct lttng_consumer_stream *stream,
2903 struct stream_subbuffer *subbuffer)
2904 {
2905 int ret;
2906 bool cache_empty;
2907 bool got_subbuffer;
2908 bool coherent;
2909 bool buffer_empty;
2910 unsigned long consumed_pos, produced_pos;
2911
2912 do {
2913 ret = ustctl_get_next_subbuf(stream->ustream);
2914 if (ret == 0) {
2915 got_subbuffer = true;
2916 } else {
2917 got_subbuffer = false;
2918 if (ret != -EAGAIN) {
2919 /* Fatal error. */
2920 goto end;
2921 }
2922 }
2923
2924 /*
2925 * Determine if the cache is empty and ensure that a sub-buffer
2926 * is made available if the cache is not empty.
2927 */
2928 if (!got_subbuffer) {
2929 ret = commit_one_metadata_packet(stream);
2930 if (ret < 0 && ret != -ENOBUFS) {
2931 goto end;
2932 } else if (ret == 0) {
2933 /* Not an error, the cache is empty. */
2934 cache_empty = true;
2935 ret = -ENODATA;
2936 goto end;
2937 } else {
2938 cache_empty = false;
2939 }
2940 } else {
2941 pthread_mutex_lock(&stream->chan->metadata_cache->lock);
2942 cache_empty = stream->chan->metadata_cache->contents.size ==
2943 stream->ust_metadata_pushed;
2944 pthread_mutex_unlock(&stream->chan->metadata_cache->lock);
2945 }
2946 } while (!got_subbuffer);
2947
2948 /* Populate sub-buffer infos and view. */
2949 ret = get_next_subbuffer_common(stream, subbuffer);
2950 if (ret) {
2951 goto end;
2952 }
2953
2954 ret = lttng_ustconsumer_sample_snapshot_positions(stream);
2955 if (ret < 0) {
2956 /*
2957 * -EAGAIN is not expected since we got a sub-buffer and haven't
2958 * pushed the consumption position yet (on put_next).
2959 */
2960 PERROR("Failed to take a snapshot of metadata buffer positions");
2961 goto end;
2962 }
2963
2964 ret = lttng_ustconsumer_get_consumed_snapshot(stream, &consumed_pos);
2965 if (ret) {
2966 PERROR("Failed to get metadata consumed position");
2967 goto end;
2968 }
2969
2970 ret = lttng_ustconsumer_get_produced_snapshot(stream, &produced_pos);
2971 if (ret) {
2972 PERROR("Failed to get metadata produced position");
2973 goto end;
2974 }
2975
2976 /* Last sub-buffer of the ring buffer ? */
2977 buffer_empty = (consumed_pos + stream->max_sb_size) == produced_pos;
2978
2979 /*
2980 * The sessiond registry lock ensures that coherent units of metadata
2981 * are pushed to the consumer daemon at once. Hence, if a sub-buffer is
2982 * acquired, the cache is empty, and it is the only available sub-buffer
2983 * available, it is safe to assume that it is "coherent".
2984 */
2985 coherent = got_subbuffer && cache_empty && buffer_empty;
2986
2987 LTTNG_OPTIONAL_SET(&subbuffer->info.metadata.coherent, coherent);
2988 end:
2989 return ret;
2990 }
2991
2992 static int put_next_subbuffer(struct lttng_consumer_stream *stream,
2993 struct stream_subbuffer *subbuffer)
2994 {
2995 const int ret = ustctl_put_next_subbuf(stream->ustream);
2996
2997 assert(ret == 0);
2998 return ret;
2999 }
3000
3001 static int signal_metadata(struct lttng_consumer_stream *stream,
3002 struct lttng_consumer_local_data *ctx)
3003 {
3004 ASSERT_LOCKED(stream->metadata_rdv_lock);
3005 return pthread_cond_broadcast(&stream->metadata_rdv) ? -errno : 0;
3006 }
3007
3008 static int lttng_ustconsumer_set_stream_ops(
3009 struct lttng_consumer_stream *stream)
3010 {
3011 int ret = 0;
3012
3013 stream->read_subbuffer_ops.on_wake_up = consumer_stream_ust_on_wake_up;
3014 if (stream->metadata_flag) {
3015 stream->read_subbuffer_ops.get_next_subbuffer =
3016 get_next_subbuffer_metadata;
3017 stream->read_subbuffer_ops.extract_subbuffer_info =
3018 extract_metadata_subbuffer_info;
3019 stream->read_subbuffer_ops.reset_metadata =
3020 metadata_stream_reset_cache_consumed_position;
3021 if (stream->chan->is_live) {
3022 stream->read_subbuffer_ops.on_sleep = signal_metadata;
3023 ret = consumer_stream_enable_metadata_bucketization(
3024 stream);
3025 if (ret) {
3026 goto end;
3027 }
3028 }
3029 } else {
3030 stream->read_subbuffer_ops.get_next_subbuffer =
3031 get_next_subbuffer;
3032 stream->read_subbuffer_ops.extract_subbuffer_info =
3033 extract_data_subbuffer_info;
3034 stream->read_subbuffer_ops.on_sleep = notify_if_more_data;
3035 if (stream->chan->is_live) {
3036 stream->read_subbuffer_ops.send_live_beacon =
3037 consumer_flush_ust_index;
3038 }
3039 }
3040
3041 stream->read_subbuffer_ops.put_next_subbuffer = put_next_subbuffer;
3042 end:
3043 return ret;
3044 }
3045
3046 /*
3047 * Called when a stream is created.
3048 *
3049 * Return 0 on success or else a negative value.
3050 */
3051 int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream *stream)
3052 {
3053 int ret;
3054
3055 assert(stream);
3056
3057 /*
3058 * Don't create anything if this is set for streaming or if there is
3059 * no current trace chunk on the parent channel.
3060 */
3061 if (stream->net_seq_idx == (uint64_t) -1ULL && stream->chan->monitor &&
3062 stream->chan->trace_chunk) {
3063 ret = consumer_stream_create_output_files(stream, true);
3064 if (ret) {
3065 goto error;
3066 }
3067 }
3068
3069 lttng_ustconsumer_set_stream_ops(stream);
3070 ret = 0;
3071
3072 error:
3073 return ret;
3074 }
3075
3076 /*
3077 * Check if data is still being extracted from the buffers for a specific
3078 * stream. Consumer data lock MUST be acquired before calling this function
3079 * and the stream lock.
3080 *
3081 * Return 1 if the traced data are still getting read else 0 meaning that the
3082 * data is available for trace viewer reading.
3083 */
3084 int lttng_ustconsumer_data_pending(struct lttng_consumer_stream *stream)
3085 {
3086 int ret;
3087
3088 assert(stream);
3089 assert(stream->ustream);
3090 ASSERT_LOCKED(stream->lock);
3091
3092 DBG("UST consumer checking data pending");
3093
3094 if (stream->endpoint_status != CONSUMER_ENDPOINT_ACTIVE) {
3095 ret = 0;
3096 goto end;
3097 }
3098
3099 if (stream->chan->type == CONSUMER_CHANNEL_TYPE_METADATA) {
3100 uint64_t contiguous, pushed;
3101
3102 /* Ease our life a bit. */
3103 pthread_mutex_lock(&stream->chan->metadata_cache->lock);
3104 contiguous = stream->chan->metadata_cache->contents.size;
3105 pthread_mutex_unlock(&stream->chan->metadata_cache->lock);
3106 pushed = stream->ust_metadata_pushed;
3107
3108 /*
3109 * We can simply check whether all contiguously available data
3110 * has been pushed to the ring buffer, since the push operation
3111 * is performed within get_next_subbuf(), and because both
3112 * get_next_subbuf() and put_next_subbuf() are issued atomically
3113 * thanks to the stream lock within
3114 * lttng_ustconsumer_read_subbuffer(). This basically means that
3115 * whetnever ust_metadata_pushed is incremented, the associated
3116 * metadata has been consumed from the metadata stream.
3117 */
3118 DBG("UST consumer metadata pending check: contiguous %" PRIu64 " vs pushed %" PRIu64,
3119 contiguous, pushed);
3120 assert(((int64_t) (contiguous - pushed)) >= 0);
3121 if ((contiguous != pushed) ||
3122 (((int64_t) contiguous - pushed) > 0 || contiguous == 0)) {
3123 ret = 1; /* Data is pending */
3124 goto end;
3125 }
3126 } else {
3127 ret = ustctl_get_next_subbuf(stream->ustream);
3128 if (ret == 0) {
3129 /*
3130 * There is still data so let's put back this
3131 * subbuffer.
3132 */
3133 ret = ustctl_put_subbuf(stream->ustream);
3134 assert(ret == 0);
3135 ret = 1; /* Data is pending */
3136 goto end;
3137 }
3138 }
3139
3140 /* Data is NOT pending so ready to be read. */
3141 ret = 0;
3142
3143 end:
3144 return ret;
3145 }
3146
3147 /*
3148 * Stop a given metadata channel timer if enabled and close the wait fd which
3149 * is the poll pipe of the metadata stream.
3150 *
3151 * This MUST be called with the metadata channel lock acquired.
3152 */
3153 void lttng_ustconsumer_close_metadata(struct lttng_consumer_channel *metadata)
3154 {
3155 int ret;
3156
3157 assert(metadata);
3158 assert(metadata->type == CONSUMER_CHANNEL_TYPE_METADATA);
3159
3160 DBG("Closing metadata channel key %" PRIu64, metadata->key);
3161
3162 if (metadata->switch_timer_enabled == 1) {
3163 consumer_timer_switch_stop(metadata);
3164 }
3165
3166 if (!metadata->metadata_stream) {
3167 goto end;
3168 }
3169
3170 /*
3171 * Closing write side so the thread monitoring the stream wakes up if any
3172 * and clean the metadata stream.
3173 */
3174 if (metadata->metadata_stream->ust_metadata_poll_pipe[1] >= 0) {
3175 ret = close(metadata->metadata_stream->ust_metadata_poll_pipe[1]);
3176 if (ret < 0) {
3177 PERROR("closing metadata pipe write side");
3178 }
3179 metadata->metadata_stream->ust_metadata_poll_pipe[1] = -1;
3180 }
3181
3182 end:
3183 return;
3184 }
3185
3186 /*
3187 * Close every metadata stream wait fd of the metadata hash table. This
3188 * function MUST be used very carefully so not to run into a race between the
3189 * metadata thread handling streams and this function closing their wait fd.
3190 *
3191 * For UST, this is used when the session daemon hangs up. Its the metadata
3192 * producer so calling this is safe because we are assured that no state change
3193 * can occur in the metadata thread for the streams in the hash table.
3194 */
3195 void lttng_ustconsumer_close_all_metadata(struct lttng_ht *metadata_ht)
3196 {
3197 struct lttng_ht_iter iter;
3198 struct lttng_consumer_stream *stream;
3199
3200 assert(metadata_ht);
3201 assert(metadata_ht->ht);
3202
3203 DBG("UST consumer closing all metadata streams");
3204
3205 rcu_read_lock();
3206 cds_lfht_for_each_entry(metadata_ht->ht, &iter.iter, stream,
3207 node.node) {
3208
3209 health_code_update();
3210
3211 pthread_mutex_lock(&stream->chan->lock);
3212 lttng_ustconsumer_close_metadata(stream->chan);
3213 pthread_mutex_unlock(&stream->chan->lock);
3214
3215 }
3216 rcu_read_unlock();
3217 }
3218
3219 void lttng_ustconsumer_close_stream_wakeup(struct lttng_consumer_stream *stream)
3220 {
3221 int ret;
3222
3223 ret = ustctl_stream_close_wakeup_fd(stream->ustream);
3224 if (ret < 0) {
3225 ERR("Unable to close wakeup fd");
3226 }
3227 }
3228
3229 /*
3230 * Please refer to consumer-timer.c before adding any lock within this
3231 * function or any of its callees. Timers have a very strict locking
3232 * semantic with respect to teardown. Failure to respect this semantic
3233 * introduces deadlocks.
3234 *
3235 * DON'T hold the metadata lock when calling this function, else this
3236 * can cause deadlock involving consumer awaiting for metadata to be
3237 * pushed out due to concurrent interaction with the session daemon.
3238 */
3239 int lttng_ustconsumer_request_metadata(struct lttng_consumer_local_data *ctx,
3240 struct lttng_consumer_channel *channel, int timer, int wait)
3241 {
3242 struct lttcomm_metadata_request_msg request;
3243 struct lttcomm_consumer_msg msg;
3244 enum lttcomm_return_code ret_code = LTTCOMM_CONSUMERD_SUCCESS;
3245 uint64_t len, key, offset, version;
3246 int ret;
3247
3248 assert(channel);
3249 assert(channel->metadata_cache);
3250
3251 memset(&request, 0, sizeof(request));
3252
3253 /* send the metadata request to sessiond */
3254 switch (consumer_data.type) {
3255 case LTTNG_CONSUMER64_UST:
3256 request.bits_per_long = 64;
3257 break;
3258 case LTTNG_CONSUMER32_UST:
3259 request.bits_per_long = 32;
3260 break;
3261 default:
3262 request.bits_per_long = 0;
3263 break;
3264 }
3265
3266 request.session_id = channel->session_id;
3267 request.session_id_per_pid = channel->session_id_per_pid;
3268 /*
3269 * Request the application UID here so the metadata of that application can
3270 * be sent back. The channel UID corresponds to the user UID of the session
3271 * used for the rights on the stream file(s).
3272 */
3273 request.uid = channel->ust_app_uid;
3274 request.key = channel->key;
3275
3276 DBG("Sending metadata request to sessiond, session id %" PRIu64
3277 ", per-pid %" PRIu64 ", app UID %u and channel key %" PRIu64,
3278 request.session_id, request.session_id_per_pid, request.uid,
3279 request.key);
3280
3281 pthread_mutex_lock(&ctx->metadata_socket_lock);
3282
3283 health_code_update();
3284
3285 ret = lttcomm_send_unix_sock(ctx->consumer_metadata_socket, &request,
3286 sizeof(request));
3287 if (ret < 0) {
3288 ERR("Asking metadata to sessiond");
3289 goto end;
3290 }
3291
3292 health_code_update();
3293
3294 /* Receive the metadata from sessiond */
3295 ret = lttcomm_recv_unix_sock(ctx->consumer_metadata_socket, &msg,
3296 sizeof(msg));
3297 if (ret != sizeof(msg)) {
3298 DBG("Consumer received unexpected message size %d (expects %zu)",
3299 ret, sizeof(msg));
3300 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_CMD);
3301 /*
3302 * The ret value might 0 meaning an orderly shutdown but this is ok
3303 * since the caller handles this.
3304 */
3305 goto end;
3306 }
3307
3308 health_code_update();
3309
3310 if (msg.cmd_type == LTTNG_ERR_UND) {
3311 /* No registry found */
3312 (void) consumer_send_status_msg(ctx->consumer_metadata_socket,
3313 ret_code);
3314 ret = 0;
3315 goto end;
3316 } else if (msg.cmd_type != LTTNG_CONSUMER_PUSH_METADATA) {
3317 ERR("Unexpected cmd_type received %d", msg.cmd_type);
3318 ret = -1;
3319 goto end;
3320 }
3321
3322 len = msg.u.push_metadata.len;
3323 key = msg.u.push_metadata.key;
3324 offset = msg.u.push_metadata.target_offset;
3325 version = msg.u.push_metadata.version;
3326
3327 assert(key == channel->key);
3328 if (len == 0) {
3329 DBG("No new metadata to receive for key %" PRIu64, key);
3330 }
3331
3332 health_code_update();
3333
3334 /* Tell session daemon we are ready to receive the metadata. */
3335 ret = consumer_send_status_msg(ctx->consumer_metadata_socket,
3336 LTTCOMM_CONSUMERD_SUCCESS);
3337 if (ret < 0 || len == 0) {
3338 /*
3339 * Somehow, the session daemon is not responding anymore or there is
3340 * nothing to receive.
3341 */
3342 goto end;
3343 }
3344
3345 health_code_update();
3346
3347 ret = lttng_ustconsumer_recv_metadata(ctx->consumer_metadata_socket,
3348 key, offset, len, version, channel, timer, wait);
3349 if (ret >= 0) {
3350 /*
3351 * Only send the status msg if the sessiond is alive meaning a positive
3352 * ret code.
3353 */
3354 (void) consumer_send_status_msg(ctx->consumer_metadata_socket, ret);
3355 }
3356 ret = 0;
3357
3358 end:
3359 health_code_update();
3360
3361 pthread_mutex_unlock(&ctx->metadata_socket_lock);
3362 return ret;
3363 }
3364
3365 /*
3366 * Return the ustctl call for the get stream id.
3367 */
3368 int lttng_ustconsumer_get_stream_id(struct lttng_consumer_stream *stream,
3369 uint64_t *stream_id)
3370 {
3371 assert(stream);
3372 assert(stream_id);
3373
3374 return ustctl_get_stream_id(stream->ustream, stream_id);
3375 }
This page took 0.141436 seconds and 5 git commands to generate.