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