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