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