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