54ad208c0c7dbb90f303d5d8269a3fde21590a38
[lttng-tools.git] / src / common / ust-consumer / ust-consumer.c
1 /*
2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 *
6 * SPDX-License-Identifier: GPL-2.0-only
7 *
8 */
9
10 #define _LGPL_SOURCE
11 #include <assert.h>
12 #include <lttng/ust-ctl.h>
13 #include <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 metadata_stream->read_subbuffer_ops.lock(metadata_stream);
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 if (ret < 0) {
1017 goto error_stream;
1018 }
1019
1020 do {
1021 health_code_update();
1022 ret = lttng_consumer_read_subbuffer(metadata_stream, ctx, true);
1023 if (ret < 0) {
1024 goto error_stream;
1025 }
1026 } while (ret > 0);
1027
1028 error_stream:
1029 metadata_stream->read_subbuffer_ops.unlock(metadata_stream);
1030 /*
1031 * Clean up the stream completely because the next snapshot will use a
1032 * new metadata stream.
1033 */
1034 consumer_stream_destroy(metadata_stream, NULL);
1035 cds_list_del(&metadata_stream->send_node);
1036 metadata_channel->metadata_stream = NULL;
1037
1038 error:
1039 rcu_read_unlock();
1040 return ret;
1041 }
1042
1043 static
1044 int get_current_subbuf_addr(struct lttng_consumer_stream *stream,
1045 const char **addr)
1046 {
1047 int ret;
1048 unsigned long mmap_offset;
1049 const char *mmap_base;
1050
1051 mmap_base = lttng_ust_ctl_get_mmap_base(stream->ustream);
1052 if (!mmap_base) {
1053 ERR("Failed to get mmap base for stream `%s`",
1054 stream->name);
1055 ret = -EPERM;
1056 goto error;
1057 }
1058
1059 ret = lttng_ust_ctl_get_mmap_read_offset(stream->ustream, &mmap_offset);
1060 if (ret != 0) {
1061 ERR("Failed to get mmap offset for stream `%s`", stream->name);
1062 ret = -EINVAL;
1063 goto error;
1064 }
1065
1066 *addr = mmap_base + mmap_offset;
1067 error:
1068 return ret;
1069
1070 }
1071
1072 /*
1073 * Take a snapshot of all the stream of a channel.
1074 * RCU read-side lock and the channel lock must be held by the caller.
1075 *
1076 * Returns 0 on success, < 0 on error
1077 */
1078 static int snapshot_channel(struct lttng_consumer_channel *channel,
1079 uint64_t key, char *path, uint64_t relayd_id,
1080 uint64_t nb_packets_per_stream,
1081 struct lttng_consumer_local_data *ctx)
1082 {
1083 int ret;
1084 unsigned use_relayd = 0;
1085 unsigned long consumed_pos, produced_pos;
1086 struct lttng_consumer_stream *stream;
1087
1088 assert(path);
1089 assert(ctx);
1090
1091 rcu_read_lock();
1092
1093 if (relayd_id != (uint64_t) -1ULL) {
1094 use_relayd = 1;
1095 }
1096
1097 assert(!channel->monitor);
1098 DBG("UST consumer snapshot channel %" PRIu64, key);
1099
1100 cds_list_for_each_entry(stream, &channel->streams.head, send_node) {
1101 health_code_update();
1102
1103 /* Lock stream because we are about to change its state. */
1104 pthread_mutex_lock(&stream->lock);
1105 assert(channel->trace_chunk);
1106 if (!lttng_trace_chunk_get(channel->trace_chunk)) {
1107 /*
1108 * Can't happen barring an internal error as the channel
1109 * holds a reference to the trace chunk.
1110 */
1111 ERR("Failed to acquire reference to channel's trace chunk");
1112 ret = -1;
1113 goto error_unlock;
1114 }
1115 assert(!stream->trace_chunk);
1116 stream->trace_chunk = channel->trace_chunk;
1117
1118 stream->net_seq_idx = relayd_id;
1119
1120 if (use_relayd) {
1121 ret = consumer_send_relayd_stream(stream, path);
1122 if (ret < 0) {
1123 goto error_unlock;
1124 }
1125 } else {
1126 ret = consumer_stream_create_output_files(stream,
1127 false);
1128 if (ret < 0) {
1129 goto error_unlock;
1130 }
1131 DBG("UST consumer snapshot stream (%" PRIu64 ")",
1132 stream->key);
1133 }
1134
1135 /*
1136 * If tracing is active, we want to perform a "full" buffer flush.
1137 * Else, if quiescent, it has already been done by the prior stop.
1138 */
1139 if (!stream->quiescent) {
1140 ret = lttng_ust_ctl_flush_buffer(stream->ustream, 0);
1141 if (ret < 0) {
1142 ERR("Failed to flush buffer during snapshot of channel: channel key = %" PRIu64 ", channel name = '%s'",
1143 channel->key, channel->name);
1144 goto error_unlock;
1145 }
1146 }
1147
1148 ret = lttng_ustconsumer_take_snapshot(stream);
1149 if (ret < 0) {
1150 ERR("Taking UST snapshot");
1151 goto error_unlock;
1152 }
1153
1154 ret = lttng_ustconsumer_get_produced_snapshot(stream, &produced_pos);
1155 if (ret < 0) {
1156 ERR("Produced UST snapshot position");
1157 goto error_unlock;
1158 }
1159
1160 ret = lttng_ustconsumer_get_consumed_snapshot(stream, &consumed_pos);
1161 if (ret < 0) {
1162 ERR("Consumerd UST snapshot position");
1163 goto error_unlock;
1164 }
1165
1166 /*
1167 * The original value is sent back if max stream size is larger than
1168 * the possible size of the snapshot. Also, we assume that the session
1169 * daemon should never send a maximum stream size that is lower than
1170 * subbuffer size.
1171 */
1172 consumed_pos = consumer_get_consume_start_pos(consumed_pos,
1173 produced_pos, nb_packets_per_stream,
1174 stream->max_sb_size);
1175
1176 while ((long) (consumed_pos - produced_pos) < 0) {
1177 ssize_t read_len;
1178 unsigned long len, padded_len;
1179 const char *subbuf_addr;
1180 struct lttng_buffer_view subbuf_view;
1181
1182 health_code_update();
1183
1184 DBG("UST consumer taking snapshot at pos %lu", consumed_pos);
1185
1186 ret = lttng_ust_ctl_get_subbuf(stream->ustream, &consumed_pos);
1187 if (ret < 0) {
1188 if (ret != -EAGAIN) {
1189 PERROR("lttng_ust_ctl_get_subbuf snapshot");
1190 goto error_close_stream;
1191 }
1192 DBG("UST consumer get subbuf failed. Skipping it.");
1193 consumed_pos += stream->max_sb_size;
1194 stream->chan->lost_packets++;
1195 continue;
1196 }
1197
1198 ret = lttng_ust_ctl_get_subbuf_size(stream->ustream, &len);
1199 if (ret < 0) {
1200 ERR("Snapshot lttng_ust_ctl_get_subbuf_size");
1201 goto error_put_subbuf;
1202 }
1203
1204 ret = lttng_ust_ctl_get_padded_subbuf_size(stream->ustream, &padded_len);
1205 if (ret < 0) {
1206 ERR("Snapshot lttng_ust_ctl_get_padded_subbuf_size");
1207 goto error_put_subbuf;
1208 }
1209
1210 ret = get_current_subbuf_addr(stream, &subbuf_addr);
1211 if (ret) {
1212 goto error_put_subbuf;
1213 }
1214
1215 subbuf_view = lttng_buffer_view_init(
1216 subbuf_addr, 0, padded_len);
1217 read_len = lttng_consumer_on_read_subbuffer_mmap(
1218 stream, &subbuf_view, padded_len - len);
1219 if (use_relayd) {
1220 if (read_len != len) {
1221 ret = -EPERM;
1222 goto error_put_subbuf;
1223 }
1224 } else {
1225 if (read_len != padded_len) {
1226 ret = -EPERM;
1227 goto error_put_subbuf;
1228 }
1229 }
1230
1231 ret = lttng_ust_ctl_put_subbuf(stream->ustream);
1232 if (ret < 0) {
1233 ERR("Snapshot lttng_ust_ctl_put_subbuf");
1234 goto error_close_stream;
1235 }
1236 consumed_pos += stream->max_sb_size;
1237 }
1238
1239 /* Simply close the stream so we can use it on the next snapshot. */
1240 consumer_stream_close(stream);
1241 pthread_mutex_unlock(&stream->lock);
1242 }
1243
1244 rcu_read_unlock();
1245 return 0;
1246
1247 error_put_subbuf:
1248 if (lttng_ust_ctl_put_subbuf(stream->ustream) < 0) {
1249 ERR("Snapshot lttng_ust_ctl_put_subbuf");
1250 }
1251 error_close_stream:
1252 consumer_stream_close(stream);
1253 error_unlock:
1254 pthread_mutex_unlock(&stream->lock);
1255 rcu_read_unlock();
1256 return ret;
1257 }
1258
1259 static
1260 void metadata_stream_reset_cache_consumed_position(
1261 struct lttng_consumer_stream *stream)
1262 {
1263 ASSERT_LOCKED(stream->lock);
1264
1265 DBG("Reset metadata cache of session %" PRIu64,
1266 stream->chan->session_id);
1267 stream->ust_metadata_pushed = 0;
1268 }
1269
1270 /*
1271 * Receive the metadata updates from the sessiond. Supports receiving
1272 * overlapping metadata, but is needs to always belong to a contiguous
1273 * range starting from 0.
1274 * Be careful about the locks held when calling this function: it needs
1275 * the metadata cache flush to concurrently progress in order to
1276 * complete.
1277 */
1278 int lttng_ustconsumer_recv_metadata(int sock, uint64_t key, uint64_t offset,
1279 uint64_t len, uint64_t version,
1280 struct lttng_consumer_channel *channel, int timer, int wait)
1281 {
1282 int ret, ret_code = LTTCOMM_CONSUMERD_SUCCESS;
1283 char *metadata_str;
1284 enum consumer_metadata_cache_write_status cache_write_status;
1285
1286 DBG("UST consumer push metadata key %" PRIu64 " of len %" PRIu64, key, len);
1287
1288 metadata_str = zmalloc(len * sizeof(char));
1289 if (!metadata_str) {
1290 PERROR("zmalloc metadata string");
1291 ret_code = LTTCOMM_CONSUMERD_ENOMEM;
1292 goto end;
1293 }
1294
1295 health_code_update();
1296
1297 /* Receive metadata string. */
1298 ret = lttcomm_recv_unix_sock(sock, metadata_str, len);
1299 if (ret < 0) {
1300 /* Session daemon is dead so return gracefully. */
1301 ret_code = ret;
1302 goto end_free;
1303 }
1304
1305 health_code_update();
1306
1307 pthread_mutex_lock(&channel->metadata_cache->lock);
1308 cache_write_status = consumer_metadata_cache_write(
1309 channel->metadata_cache, offset, len, version,
1310 metadata_str);
1311 pthread_mutex_unlock(&channel->metadata_cache->lock);
1312 switch (cache_write_status) {
1313 case CONSUMER_METADATA_CACHE_WRITE_STATUS_NO_CHANGE:
1314 /*
1315 * The write entirely overlapped with existing contents of the
1316 * same metadata version (same content); there is nothing to do.
1317 */
1318 break;
1319 case CONSUMER_METADATA_CACHE_WRITE_STATUS_INVALIDATED:
1320 /*
1321 * The metadata cache was invalidated (previously pushed
1322 * content has been overwritten). Reset the stream's consumed
1323 * metadata position to ensure the metadata poll thread consumes
1324 * the whole cache.
1325 */
1326
1327 /*
1328 * channel::metadata_stream can be null when the metadata
1329 * channel is under a snapshot session type. No need to update
1330 * the stream position in that scenario.
1331 */
1332 if (channel->metadata_stream != NULL) {
1333 pthread_mutex_lock(&channel->metadata_stream->lock);
1334 metadata_stream_reset_cache_consumed_position(
1335 channel->metadata_stream);
1336 pthread_mutex_unlock(&channel->metadata_stream->lock);
1337 } else {
1338 /* Validate we are in snapshot mode. */
1339 assert(!channel->monitor);
1340 }
1341 /* Fall-through. */
1342 case CONSUMER_METADATA_CACHE_WRITE_STATUS_APPENDED_CONTENT:
1343 /*
1344 * In both cases, the metadata poll thread has new data to
1345 * consume.
1346 */
1347 ret = consumer_metadata_wakeup_pipe(channel);
1348 if (ret) {
1349 ret_code = LTTCOMM_CONSUMERD_ERROR_METADATA;
1350 goto end_free;
1351 }
1352 break;
1353 case CONSUMER_METADATA_CACHE_WRITE_STATUS_ERROR:
1354 /* Unable to handle metadata. Notify session daemon. */
1355 ret_code = LTTCOMM_CONSUMERD_ERROR_METADATA;
1356 /*
1357 * Skip metadata flush on write error since the offset and len might
1358 * not have been updated which could create an infinite loop below when
1359 * waiting for the metadata cache to be flushed.
1360 */
1361 goto end_free;
1362 default:
1363 abort();
1364 }
1365
1366 if (!wait) {
1367 goto end_free;
1368 }
1369 while (consumer_metadata_cache_flushed(channel, offset + len, timer)) {
1370 DBG("Waiting for metadata to be flushed");
1371
1372 health_code_update();
1373
1374 usleep(DEFAULT_METADATA_AVAILABILITY_WAIT_TIME);
1375 }
1376
1377 end_free:
1378 free(metadata_str);
1379 end:
1380 return ret_code;
1381 }
1382
1383 /*
1384 * Receive command from session daemon and process it.
1385 *
1386 * Return 1 on success else a negative value or 0.
1387 */
1388 int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
1389 int sock, struct pollfd *consumer_sockpoll)
1390 {
1391 int ret_func;
1392 enum lttcomm_return_code ret_code = LTTCOMM_CONSUMERD_SUCCESS;
1393 struct lttcomm_consumer_msg msg;
1394 struct lttng_consumer_channel *channel = NULL;
1395
1396 health_code_update();
1397
1398 {
1399 ssize_t ret_recv;
1400
1401 ret_recv = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg));
1402 if (ret_recv != sizeof(msg)) {
1403 DBG("Consumer received unexpected message size %zd (expects %zu)",
1404 ret_recv, sizeof(msg));
1405 /*
1406 * The ret value might 0 meaning an orderly shutdown but this is ok
1407 * since the caller handles this.
1408 */
1409 if (ret_recv > 0) {
1410 lttng_consumer_send_error(ctx,
1411 LTTCOMM_CONSUMERD_ERROR_RECV_CMD);
1412 ret_recv = -1;
1413 }
1414 return ret_recv;
1415 }
1416 }
1417
1418 health_code_update();
1419
1420 /* deprecated */
1421 assert(msg.cmd_type != LTTNG_CONSUMER_STOP);
1422
1423 health_code_update();
1424
1425 /* relayd needs RCU read-side lock */
1426 rcu_read_lock();
1427
1428 switch (msg.cmd_type) {
1429 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET:
1430 {
1431 uint32_t major = msg.u.relayd_sock.major;
1432 uint32_t minor = msg.u.relayd_sock.minor;
1433 enum lttcomm_sock_proto protocol =
1434 (enum lttcomm_sock_proto) msg.u.relayd_sock
1435 .relayd_socket_protocol;
1436
1437 /* Session daemon status message are handled in the following call. */
1438 consumer_add_relayd_socket(msg.u.relayd_sock.net_index,
1439 msg.u.relayd_sock.type, ctx, sock,
1440 consumer_sockpoll, msg.u.relayd_sock.session_id,
1441 msg.u.relayd_sock.relayd_session_id, major,
1442 minor, protocol);
1443 goto end_nosignal;
1444 }
1445 case LTTNG_CONSUMER_DESTROY_RELAYD:
1446 {
1447 uint64_t index = msg.u.destroy_relayd.net_seq_idx;
1448 struct consumer_relayd_sock_pair *relayd;
1449
1450 DBG("UST consumer destroying relayd %" PRIu64, index);
1451
1452 /* Get relayd reference if exists. */
1453 relayd = consumer_find_relayd(index);
1454 if (relayd == NULL) {
1455 DBG("Unable to find relayd %" PRIu64, index);
1456 ret_code = LTTCOMM_CONSUMERD_RELAYD_FAIL;
1457 }
1458
1459 /*
1460 * Each relayd socket pair has a refcount of stream attached to it
1461 * which tells if the relayd is still active or not depending on the
1462 * refcount value.
1463 *
1464 * This will set the destroy flag of the relayd object and destroy it
1465 * if the refcount reaches zero when called.
1466 *
1467 * The destroy can happen either here or when a stream fd hangs up.
1468 */
1469 if (relayd) {
1470 consumer_flag_relayd_for_destroy(relayd);
1471 }
1472
1473 goto end_msg_sessiond;
1474 }
1475 case LTTNG_CONSUMER_UPDATE_STREAM:
1476 {
1477 rcu_read_unlock();
1478 return -ENOSYS;
1479 }
1480 case LTTNG_CONSUMER_DATA_PENDING:
1481 {
1482 int is_data_pending;
1483 ssize_t ret_send;
1484 uint64_t id = msg.u.data_pending.session_id;
1485
1486 DBG("UST consumer data pending command for id %" PRIu64, id);
1487
1488 is_data_pending = consumer_data_pending(id);
1489
1490 /* Send back returned value to session daemon */
1491 ret_send = lttcomm_send_unix_sock(sock, &is_data_pending,
1492 sizeof(is_data_pending));
1493 if (ret_send < 0) {
1494 DBG("Error when sending the data pending ret code: %zd",
1495 ret_send);
1496 goto error_fatal;
1497 }
1498
1499 /*
1500 * No need to send back a status message since the data pending
1501 * returned value is the response.
1502 */
1503 break;
1504 }
1505 case LTTNG_CONSUMER_ASK_CHANNEL_CREATION:
1506 {
1507 int ret_ask_channel, ret_add_channel, ret_send;
1508 struct lttng_ust_ctl_consumer_channel_attr attr;
1509 const uint64_t chunk_id = msg.u.ask_channel.chunk_id.value;
1510 const struct lttng_credentials buffer_credentials = {
1511 .uid = LTTNG_OPTIONAL_INIT_VALUE(msg.u.ask_channel.buffer_credentials.uid),
1512 .gid = LTTNG_OPTIONAL_INIT_VALUE(msg.u.ask_channel.buffer_credentials.gid),
1513 };
1514
1515 /* Create a plain object and reserve a channel key. */
1516 channel = consumer_allocate_channel(
1517 msg.u.ask_channel.key,
1518 msg.u.ask_channel.session_id,
1519 msg.u.ask_channel.chunk_id.is_set ?
1520 &chunk_id : NULL,
1521 msg.u.ask_channel.pathname,
1522 msg.u.ask_channel.name,
1523 msg.u.ask_channel.relayd_id,
1524 (enum lttng_event_output) msg.u.ask_channel.output,
1525 msg.u.ask_channel.tracefile_size,
1526 msg.u.ask_channel.tracefile_count,
1527 msg.u.ask_channel.session_id_per_pid,
1528 msg.u.ask_channel.monitor,
1529 msg.u.ask_channel.live_timer_interval,
1530 msg.u.ask_channel.is_live,
1531 msg.u.ask_channel.root_shm_path,
1532 msg.u.ask_channel.shm_path);
1533 if (!channel) {
1534 goto end_channel_error;
1535 }
1536
1537 LTTNG_OPTIONAL_SET(&channel->buffer_credentials,
1538 buffer_credentials);
1539
1540 /*
1541 * Assign UST application UID to the channel. This value is ignored for
1542 * per PID buffers. This is specific to UST thus setting this after the
1543 * allocation.
1544 */
1545 channel->ust_app_uid = msg.u.ask_channel.ust_app_uid;
1546
1547 /* Build channel attributes from received message. */
1548 attr.subbuf_size = msg.u.ask_channel.subbuf_size;
1549 attr.num_subbuf = msg.u.ask_channel.num_subbuf;
1550 attr.overwrite = msg.u.ask_channel.overwrite;
1551 attr.switch_timer_interval = msg.u.ask_channel.switch_timer_interval;
1552 attr.read_timer_interval = msg.u.ask_channel.read_timer_interval;
1553 attr.chan_id = msg.u.ask_channel.chan_id;
1554 memcpy(attr.uuid, msg.u.ask_channel.uuid, sizeof(attr.uuid));
1555 attr.blocking_timeout= msg.u.ask_channel.blocking_timeout;
1556
1557 /* Match channel buffer type to the UST abi. */
1558 switch (msg.u.ask_channel.output) {
1559 case LTTNG_EVENT_MMAP:
1560 default:
1561 attr.output = LTTNG_UST_ABI_MMAP;
1562 break;
1563 }
1564
1565 /* Translate and save channel type. */
1566 switch (msg.u.ask_channel.type) {
1567 case LTTNG_UST_ABI_CHAN_PER_CPU:
1568 channel->type = CONSUMER_CHANNEL_TYPE_DATA;
1569 attr.type = LTTNG_UST_ABI_CHAN_PER_CPU;
1570 /*
1571 * Set refcount to 1 for owner. Below, we will
1572 * pass ownership to the
1573 * consumer_thread_channel_poll() thread.
1574 */
1575 channel->refcount = 1;
1576 break;
1577 case LTTNG_UST_ABI_CHAN_METADATA:
1578 channel->type = CONSUMER_CHANNEL_TYPE_METADATA;
1579 attr.type = LTTNG_UST_ABI_CHAN_METADATA;
1580 break;
1581 default:
1582 assert(0);
1583 goto error_fatal;
1584 };
1585
1586 health_code_update();
1587
1588 ret_ask_channel = ask_channel(ctx, channel, &attr);
1589 if (ret_ask_channel < 0) {
1590 goto end_channel_error;
1591 }
1592
1593 if (msg.u.ask_channel.type == LTTNG_UST_ABI_CHAN_METADATA) {
1594 int ret_allocate;
1595
1596 ret_allocate = consumer_metadata_cache_allocate(
1597 channel);
1598 if (ret_allocate < 0) {
1599 ERR("Allocating metadata cache");
1600 goto end_channel_error;
1601 }
1602 consumer_timer_switch_start(channel, attr.switch_timer_interval);
1603 attr.switch_timer_interval = 0;
1604 } else {
1605 int monitor_start_ret;
1606
1607 consumer_timer_live_start(channel,
1608 msg.u.ask_channel.live_timer_interval);
1609 monitor_start_ret = consumer_timer_monitor_start(
1610 channel,
1611 msg.u.ask_channel.monitor_timer_interval);
1612 if (monitor_start_ret < 0) {
1613 ERR("Starting channel monitoring timer failed");
1614 goto end_channel_error;
1615 }
1616 }
1617
1618 health_code_update();
1619
1620 /*
1621 * Add the channel to the internal state AFTER all streams were created
1622 * and successfully sent to session daemon. This way, all streams must
1623 * be ready before this channel is visible to the threads.
1624 * If add_channel succeeds, ownership of the channel is
1625 * passed to consumer_thread_channel_poll().
1626 */
1627 ret_add_channel = add_channel(channel, ctx);
1628 if (ret_add_channel < 0) {
1629 if (msg.u.ask_channel.type == LTTNG_UST_ABI_CHAN_METADATA) {
1630 if (channel->switch_timer_enabled == 1) {
1631 consumer_timer_switch_stop(channel);
1632 }
1633 consumer_metadata_cache_destroy(channel);
1634 }
1635 if (channel->live_timer_enabled == 1) {
1636 consumer_timer_live_stop(channel);
1637 }
1638 if (channel->monitor_timer_enabled == 1) {
1639 consumer_timer_monitor_stop(channel);
1640 }
1641 goto end_channel_error;
1642 }
1643
1644 health_code_update();
1645
1646 /*
1647 * Channel and streams are now created. Inform the session daemon that
1648 * everything went well and should wait to receive the channel and
1649 * streams with ustctl API.
1650 */
1651 ret_send = consumer_send_status_channel(sock, channel);
1652 if (ret_send < 0) {
1653 /*
1654 * There is probably a problem on the socket.
1655 */
1656 goto error_fatal;
1657 }
1658
1659 break;
1660 }
1661 case LTTNG_CONSUMER_GET_CHANNEL:
1662 {
1663 int ret, relayd_err = 0;
1664 uint64_t key = msg.u.get_channel.key;
1665 struct lttng_consumer_channel *found_channel;
1666
1667 found_channel = consumer_find_channel(key);
1668 if (!found_channel) {
1669 ERR("UST consumer get channel key %" PRIu64 " not found", key);
1670 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
1671 goto end_get_channel;
1672 }
1673
1674 health_code_update();
1675
1676 /* Send the channel to sessiond (and relayd, if applicable). */
1677 ret = send_channel_to_sessiond_and_relayd(
1678 sock, found_channel, ctx, &relayd_err);
1679 if (ret < 0) {
1680 if (relayd_err) {
1681 /*
1682 * We were unable to send to the relayd the stream so avoid
1683 * sending back a fatal error to the thread since this is OK
1684 * and the consumer can continue its work. The above call
1685 * has sent the error status message to the sessiond.
1686 */
1687 goto end_get_channel_nosignal;
1688 }
1689 /*
1690 * The communicaton was broken hence there is a bad state between
1691 * the consumer and sessiond so stop everything.
1692 */
1693 goto error_get_channel_fatal;
1694 }
1695
1696 health_code_update();
1697
1698 /*
1699 * In no monitor mode, the streams ownership is kept inside the channel
1700 * so don't send them to the data thread.
1701 */
1702 if (!found_channel->monitor) {
1703 goto end_get_channel;
1704 }
1705
1706 ret = send_streams_to_thread(found_channel, ctx);
1707 if (ret < 0) {
1708 /*
1709 * If we are unable to send the stream to the thread, there is
1710 * a big problem so just stop everything.
1711 */
1712 goto error_get_channel_fatal;
1713 }
1714 /* List MUST be empty after or else it could be reused. */
1715 assert(cds_list_empty(&found_channel->streams.head));
1716 end_get_channel:
1717 goto end_msg_sessiond;
1718 error_get_channel_fatal:
1719 goto error_fatal;
1720 end_get_channel_nosignal:
1721 goto end_nosignal;
1722 }
1723 case LTTNG_CONSUMER_DESTROY_CHANNEL:
1724 {
1725 uint64_t key = msg.u.destroy_channel.key;
1726
1727 /*
1728 * Only called if streams have not been sent to stream
1729 * manager thread. However, channel has been sent to
1730 * channel manager thread.
1731 */
1732 notify_thread_del_channel(ctx, key);
1733 goto end_msg_sessiond;
1734 }
1735 case LTTNG_CONSUMER_CLOSE_METADATA:
1736 {
1737 int ret;
1738
1739 ret = close_metadata(msg.u.close_metadata.key);
1740 if (ret != 0) {
1741 ret_code = ret;
1742 }
1743
1744 goto end_msg_sessiond;
1745 }
1746 case LTTNG_CONSUMER_FLUSH_CHANNEL:
1747 {
1748 int ret;
1749
1750 ret = flush_channel(msg.u.flush_channel.key);
1751 if (ret != 0) {
1752 ret_code = ret;
1753 }
1754
1755 goto end_msg_sessiond;
1756 }
1757 case LTTNG_CONSUMER_CLEAR_QUIESCENT_CHANNEL:
1758 {
1759 int ret;
1760
1761 ret = clear_quiescent_channel(
1762 msg.u.clear_quiescent_channel.key);
1763 if (ret != 0) {
1764 ret_code = ret;
1765 }
1766
1767 goto end_msg_sessiond;
1768 }
1769 case LTTNG_CONSUMER_PUSH_METADATA:
1770 {
1771 int ret;
1772 uint64_t len = msg.u.push_metadata.len;
1773 uint64_t key = msg.u.push_metadata.key;
1774 uint64_t offset = msg.u.push_metadata.target_offset;
1775 uint64_t version = msg.u.push_metadata.version;
1776 struct lttng_consumer_channel *found_channel;
1777
1778 DBG("UST consumer push metadata key %" PRIu64 " of len %" PRIu64, key,
1779 len);
1780
1781 found_channel = consumer_find_channel(key);
1782 if (!found_channel) {
1783 /*
1784 * This is possible if the metadata creation on the consumer side
1785 * is in flight vis-a-vis a concurrent push metadata from the
1786 * session daemon. Simply return that the channel failed and the
1787 * session daemon will handle that message correctly considering
1788 * that this race is acceptable thus the DBG() statement here.
1789 */
1790 DBG("UST consumer push metadata %" PRIu64 " not found", key);
1791 ret_code = LTTCOMM_CONSUMERD_CHANNEL_FAIL;
1792 goto end_push_metadata_msg_sessiond;
1793 }
1794
1795 health_code_update();
1796
1797 if (!len) {
1798 /*
1799 * There is nothing to receive. We have simply
1800 * checked whether the channel can be found.
1801 */
1802 ret_code = LTTCOMM_CONSUMERD_SUCCESS;
1803 goto end_push_metadata_msg_sessiond;
1804 }
1805
1806 /* Tell session daemon we are ready to receive the metadata. */
1807 ret = consumer_send_status_msg(sock, LTTCOMM_CONSUMERD_SUCCESS);
1808 if (ret < 0) {
1809 /* Somehow, the session daemon is not responding anymore. */
1810 goto error_push_metadata_fatal;
1811 }
1812
1813 health_code_update();
1814
1815 /* Wait for more data. */
1816 health_poll_entry();
1817 ret = lttng_consumer_poll_socket(consumer_sockpoll);
1818 health_poll_exit();
1819 if (ret) {
1820 goto error_push_metadata_fatal;
1821 }
1822
1823 health_code_update();
1824
1825 ret = lttng_ustconsumer_recv_metadata(sock, key, offset, len,
1826 version, found_channel, 0, 1);
1827 if (ret < 0) {
1828 /* error receiving from sessiond */
1829 goto error_push_metadata_fatal;
1830 } else {
1831 ret_code = ret;
1832 goto end_push_metadata_msg_sessiond;
1833 }
1834 end_push_metadata_msg_sessiond:
1835 goto end_msg_sessiond;
1836 error_push_metadata_fatal:
1837 goto error_fatal;
1838 }
1839 case LTTNG_CONSUMER_SETUP_METADATA:
1840 {
1841 int ret;
1842
1843 ret = setup_metadata(ctx, msg.u.setup_metadata.key);
1844 if (ret) {
1845 ret_code = ret;
1846 }
1847 goto end_msg_sessiond;
1848 }
1849 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL:
1850 {
1851 struct lttng_consumer_channel *found_channel;
1852 uint64_t key = msg.u.snapshot_channel.key;
1853 int ret_send;
1854
1855 found_channel = consumer_find_channel(key);
1856 if (!found_channel) {
1857 DBG("UST snapshot channel not found for key %" PRIu64, key);
1858 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
1859 } else {
1860 if (msg.u.snapshot_channel.metadata) {
1861 int ret_snapshot;
1862
1863 ret_snapshot = snapshot_metadata(found_channel,
1864 key,
1865 msg.u.snapshot_channel.pathname,
1866 msg.u.snapshot_channel.relayd_id,
1867 ctx);
1868 if (ret_snapshot < 0) {
1869 ERR("Snapshot metadata failed");
1870 ret_code = LTTCOMM_CONSUMERD_SNAPSHOT_FAILED;
1871 }
1872 } else {
1873 int ret_snapshot;
1874
1875 ret_snapshot = snapshot_channel(found_channel,
1876 key,
1877 msg.u.snapshot_channel.pathname,
1878 msg.u.snapshot_channel.relayd_id,
1879 msg.u.snapshot_channel
1880 .nb_packets_per_stream,
1881 ctx);
1882 if (ret_snapshot < 0) {
1883 ERR("Snapshot channel failed");
1884 ret_code = LTTCOMM_CONSUMERD_SNAPSHOT_FAILED;
1885 }
1886 }
1887 }
1888 health_code_update();
1889 ret_send = consumer_send_status_msg(sock, ret_code);
1890 if (ret_send < 0) {
1891 /* Somehow, the session daemon is not responding anymore. */
1892 goto end_nosignal;
1893 }
1894 health_code_update();
1895 break;
1896 }
1897 case LTTNG_CONSUMER_DISCARDED_EVENTS:
1898 {
1899 int ret = 0;
1900 uint64_t discarded_events;
1901 struct lttng_ht_iter iter;
1902 struct lttng_ht *ht;
1903 struct lttng_consumer_stream *stream;
1904 uint64_t id = msg.u.discarded_events.session_id;
1905 uint64_t key = msg.u.discarded_events.channel_key;
1906
1907 DBG("UST consumer discarded events command for session id %"
1908 PRIu64, id);
1909 rcu_read_lock();
1910 pthread_mutex_lock(&the_consumer_data.lock);
1911
1912 ht = the_consumer_data.stream_list_ht;
1913
1914 /*
1915 * We only need a reference to the channel, but they are not
1916 * directly indexed, so we just use the first matching stream
1917 * to extract the information we need, we default to 0 if not
1918 * found (no events are dropped if the channel is not yet in
1919 * use).
1920 */
1921 discarded_events = 0;
1922 cds_lfht_for_each_entry_duplicate(ht->ht,
1923 ht->hash_fct(&id, lttng_ht_seed),
1924 ht->match_fct, &id,
1925 &iter.iter, stream, node_session_id.node) {
1926 if (stream->chan->key == key) {
1927 discarded_events = stream->chan->discarded_events;
1928 break;
1929 }
1930 }
1931 pthread_mutex_unlock(&the_consumer_data.lock);
1932 rcu_read_unlock();
1933
1934 DBG("UST consumer discarded events command for session id %"
1935 PRIu64 ", channel key %" PRIu64, id, key);
1936
1937 health_code_update();
1938
1939 /* Send back returned value to session daemon */
1940 ret = lttcomm_send_unix_sock(sock, &discarded_events, sizeof(discarded_events));
1941 if (ret < 0) {
1942 PERROR("send discarded events");
1943 goto error_fatal;
1944 }
1945
1946 break;
1947 }
1948 case LTTNG_CONSUMER_LOST_PACKETS:
1949 {
1950 int ret;
1951 uint64_t lost_packets;
1952 struct lttng_ht_iter iter;
1953 struct lttng_ht *ht;
1954 struct lttng_consumer_stream *stream;
1955 uint64_t id = msg.u.lost_packets.session_id;
1956 uint64_t key = msg.u.lost_packets.channel_key;
1957
1958 DBG("UST consumer lost packets command for session id %"
1959 PRIu64, id);
1960 rcu_read_lock();
1961 pthread_mutex_lock(&the_consumer_data.lock);
1962
1963 ht = the_consumer_data.stream_list_ht;
1964
1965 /*
1966 * We only need a reference to the channel, but they are not
1967 * directly indexed, so we just use the first matching stream
1968 * to extract the information we need, we default to 0 if not
1969 * found (no packets lost if the channel is not yet in use).
1970 */
1971 lost_packets = 0;
1972 cds_lfht_for_each_entry_duplicate(ht->ht,
1973 ht->hash_fct(&id, lttng_ht_seed),
1974 ht->match_fct, &id,
1975 &iter.iter, stream, node_session_id.node) {
1976 if (stream->chan->key == key) {
1977 lost_packets = stream->chan->lost_packets;
1978 break;
1979 }
1980 }
1981 pthread_mutex_unlock(&the_consumer_data.lock);
1982 rcu_read_unlock();
1983
1984 DBG("UST consumer lost packets command for session id %"
1985 PRIu64 ", channel key %" PRIu64, id, key);
1986
1987 health_code_update();
1988
1989 /* Send back returned value to session daemon */
1990 ret = lttcomm_send_unix_sock(sock, &lost_packets,
1991 sizeof(lost_packets));
1992 if (ret < 0) {
1993 PERROR("send lost packets");
1994 goto error_fatal;
1995 }
1996
1997 break;
1998 }
1999 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE:
2000 {
2001 int channel_monitor_pipe, ret_send,
2002 ret_set_channel_monitor_pipe;
2003 ssize_t ret_recv;
2004
2005 ret_code = LTTCOMM_CONSUMERD_SUCCESS;
2006 /* Successfully received the command's type. */
2007 ret_send = consumer_send_status_msg(sock, ret_code);
2008 if (ret_send < 0) {
2009 goto error_fatal;
2010 }
2011
2012 ret_recv = lttcomm_recv_fds_unix_sock(
2013 sock, &channel_monitor_pipe, 1);
2014 if (ret_recv != sizeof(channel_monitor_pipe)) {
2015 ERR("Failed to receive channel monitor pipe");
2016 goto error_fatal;
2017 }
2018
2019 DBG("Received channel monitor pipe (%d)", channel_monitor_pipe);
2020 ret_set_channel_monitor_pipe =
2021 consumer_timer_thread_set_channel_monitor_pipe(
2022 channel_monitor_pipe);
2023 if (!ret_set_channel_monitor_pipe) {
2024 int flags;
2025 int ret_fcntl;
2026
2027 ret_code = LTTCOMM_CONSUMERD_SUCCESS;
2028 /* Set the pipe as non-blocking. */
2029 ret_fcntl = fcntl(channel_monitor_pipe, F_GETFL, 0);
2030 if (ret_fcntl == -1) {
2031 PERROR("fcntl get flags of the channel monitoring pipe");
2032 goto error_fatal;
2033 }
2034 flags = ret_fcntl;
2035
2036 ret_fcntl = fcntl(channel_monitor_pipe, F_SETFL,
2037 flags | O_NONBLOCK);
2038 if (ret_fcntl == -1) {
2039 PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe");
2040 goto error_fatal;
2041 }
2042 DBG("Channel monitor pipe set as non-blocking");
2043 } else {
2044 ret_code = LTTCOMM_CONSUMERD_ALREADY_SET;
2045 }
2046 goto end_msg_sessiond;
2047 }
2048 case LTTNG_CONSUMER_ROTATE_CHANNEL:
2049 {
2050 struct lttng_consumer_channel *found_channel;
2051 uint64_t key = msg.u.rotate_channel.key;
2052 int ret_send_status;
2053
2054 found_channel = consumer_find_channel(key);
2055 if (!found_channel) {
2056 DBG("Channel %" PRIu64 " not found", key);
2057 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
2058 } else {
2059 int rotate_channel;
2060
2061 /*
2062 * Sample the rotate position of all the streams in
2063 * this channel.
2064 */
2065 rotate_channel = lttng_consumer_rotate_channel(
2066 found_channel, key,
2067 msg.u.rotate_channel.relayd_id,
2068 msg.u.rotate_channel.metadata, ctx);
2069 if (rotate_channel < 0) {
2070 ERR("Rotate channel failed");
2071 ret_code = LTTCOMM_CONSUMERD_ROTATION_FAIL;
2072 }
2073
2074 health_code_update();
2075 }
2076
2077 ret_send_status = consumer_send_status_msg(sock, ret_code);
2078 if (ret_send_status < 0) {
2079 /* Somehow, the session daemon is not responding anymore. */
2080 goto end_rotate_channel_nosignal;
2081 }
2082
2083 /*
2084 * Rotate the streams that are ready right now.
2085 * FIXME: this is a second consecutive iteration over the
2086 * streams in a channel, there is probably a better way to
2087 * handle this, but it needs to be after the
2088 * consumer_send_status_msg() call.
2089 */
2090 if (found_channel) {
2091 int ret_rotate_read_streams;
2092
2093 ret_rotate_read_streams =
2094 lttng_consumer_rotate_ready_streams(
2095 found_channel, key,
2096 ctx);
2097 if (ret_rotate_read_streams < 0) {
2098 ERR("Rotate channel failed");
2099 }
2100 }
2101 break;
2102 end_rotate_channel_nosignal:
2103 goto end_nosignal;
2104 }
2105 case LTTNG_CONSUMER_CLEAR_CHANNEL:
2106 {
2107 struct lttng_consumer_channel *found_channel;
2108 uint64_t key = msg.u.clear_channel.key;
2109 int ret_send_status;
2110
2111 found_channel = consumer_find_channel(key);
2112 if (!found_channel) {
2113 DBG("Channel %" PRIu64 " not found", key);
2114 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
2115 } else {
2116 int ret_clear_channel;
2117
2118 ret_clear_channel = lttng_consumer_clear_channel(
2119 found_channel);
2120 if (ret_clear_channel) {
2121 ERR("Clear channel failed key %" PRIu64, key);
2122 ret_code = ret_clear_channel;
2123 }
2124
2125 health_code_update();
2126 }
2127 ret_send_status = consumer_send_status_msg(sock, ret_code);
2128 if (ret_send_status < 0) {
2129 /* Somehow, the session daemon is not responding anymore. */
2130 goto end_nosignal;
2131 }
2132 break;
2133 }
2134 case LTTNG_CONSUMER_INIT:
2135 {
2136 int ret_send_status;
2137
2138 ret_code = lttng_consumer_init_command(ctx,
2139 msg.u.init.sessiond_uuid);
2140 health_code_update();
2141 ret_send_status = consumer_send_status_msg(sock, ret_code);
2142 if (ret_send_status < 0) {
2143 /* Somehow, the session daemon is not responding anymore. */
2144 goto end_nosignal;
2145 }
2146 break;
2147 }
2148 case LTTNG_CONSUMER_CREATE_TRACE_CHUNK:
2149 {
2150 const struct lttng_credentials credentials = {
2151 .uid = LTTNG_OPTIONAL_INIT_VALUE(msg.u.create_trace_chunk.credentials.value.uid),
2152 .gid = LTTNG_OPTIONAL_INIT_VALUE(msg.u.create_trace_chunk.credentials.value.gid),
2153 };
2154 const bool is_local_trace =
2155 !msg.u.create_trace_chunk.relayd_id.is_set;
2156 const uint64_t relayd_id =
2157 msg.u.create_trace_chunk.relayd_id.value;
2158 const char *chunk_override_name =
2159 *msg.u.create_trace_chunk.override_name ?
2160 msg.u.create_trace_chunk.override_name :
2161 NULL;
2162 struct lttng_directory_handle *chunk_directory_handle = NULL;
2163
2164 /*
2165 * The session daemon will only provide a chunk directory file
2166 * descriptor for local traces.
2167 */
2168 if (is_local_trace) {
2169 int chunk_dirfd;
2170 int ret_send_status;
2171 ssize_t ret_recv;
2172
2173 /* Acnowledge the reception of the command. */
2174 ret_send_status = consumer_send_status_msg(
2175 sock, LTTCOMM_CONSUMERD_SUCCESS);
2176 if (ret_send_status < 0) {
2177 /* Somehow, the session daemon is not responding anymore. */
2178 goto end_nosignal;
2179 }
2180
2181 /*
2182 * Receive trace chunk domain dirfd.
2183 */
2184 ret_recv = lttcomm_recv_fds_unix_sock(
2185 sock, &chunk_dirfd, 1);
2186 if (ret_recv != sizeof(chunk_dirfd)) {
2187 ERR("Failed to receive trace chunk domain directory file descriptor");
2188 goto error_fatal;
2189 }
2190
2191 DBG("Received trace chunk domain directory fd (%d)",
2192 chunk_dirfd);
2193 chunk_directory_handle = lttng_directory_handle_create_from_dirfd(
2194 chunk_dirfd);
2195 if (!chunk_directory_handle) {
2196 ERR("Failed to initialize chunk domain directory handle from directory file descriptor");
2197 if (close(chunk_dirfd)) {
2198 PERROR("Failed to close chunk directory file descriptor");
2199 }
2200 goto error_fatal;
2201 }
2202 }
2203
2204 ret_code = lttng_consumer_create_trace_chunk(
2205 !is_local_trace ? &relayd_id : NULL,
2206 msg.u.create_trace_chunk.session_id,
2207 msg.u.create_trace_chunk.chunk_id,
2208 (time_t) msg.u.create_trace_chunk
2209 .creation_timestamp,
2210 chunk_override_name,
2211 msg.u.create_trace_chunk.credentials.is_set ?
2212 &credentials :
2213 NULL,
2214 chunk_directory_handle);
2215 lttng_directory_handle_put(chunk_directory_handle);
2216 goto end_msg_sessiond;
2217 }
2218 case LTTNG_CONSUMER_CLOSE_TRACE_CHUNK:
2219 {
2220 enum lttng_trace_chunk_command_type close_command =
2221 msg.u.close_trace_chunk.close_command.value;
2222 const uint64_t relayd_id =
2223 msg.u.close_trace_chunk.relayd_id.value;
2224 struct lttcomm_consumer_close_trace_chunk_reply reply;
2225 char closed_trace_chunk_path[LTTNG_PATH_MAX] = {};
2226 int ret;
2227
2228 ret_code = lttng_consumer_close_trace_chunk(
2229 msg.u.close_trace_chunk.relayd_id.is_set ?
2230 &relayd_id :
2231 NULL,
2232 msg.u.close_trace_chunk.session_id,
2233 msg.u.close_trace_chunk.chunk_id,
2234 (time_t) msg.u.close_trace_chunk.close_timestamp,
2235 msg.u.close_trace_chunk.close_command.is_set ?
2236 &close_command :
2237 NULL, closed_trace_chunk_path);
2238 reply.ret_code = ret_code;
2239 reply.path_length = strlen(closed_trace_chunk_path) + 1;
2240 ret = lttcomm_send_unix_sock(sock, &reply, sizeof(reply));
2241 if (ret != sizeof(reply)) {
2242 goto error_fatal;
2243 }
2244 ret = lttcomm_send_unix_sock(sock, closed_trace_chunk_path,
2245 reply.path_length);
2246 if (ret != reply.path_length) {
2247 goto error_fatal;
2248 }
2249 goto end_nosignal;
2250 }
2251 case LTTNG_CONSUMER_TRACE_CHUNK_EXISTS:
2252 {
2253 const uint64_t relayd_id =
2254 msg.u.trace_chunk_exists.relayd_id.value;
2255
2256 ret_code = lttng_consumer_trace_chunk_exists(
2257 msg.u.trace_chunk_exists.relayd_id.is_set ?
2258 &relayd_id : NULL,
2259 msg.u.trace_chunk_exists.session_id,
2260 msg.u.trace_chunk_exists.chunk_id);
2261 goto end_msg_sessiond;
2262 }
2263 case LTTNG_CONSUMER_OPEN_CHANNEL_PACKETS:
2264 {
2265 const uint64_t key = msg.u.open_channel_packets.key;
2266 struct lttng_consumer_channel *found_channel =
2267 consumer_find_channel(key);
2268
2269 if (found_channel) {
2270 pthread_mutex_lock(&found_channel->lock);
2271 ret_code = lttng_consumer_open_channel_packets(
2272 found_channel);
2273 pthread_mutex_unlock(&found_channel->lock);
2274 } else {
2275 /*
2276 * The channel could have disappeared in per-pid
2277 * buffering mode.
2278 */
2279 DBG("Channel %" PRIu64 " not found", key);
2280 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
2281 }
2282
2283 health_code_update();
2284 goto end_msg_sessiond;
2285 }
2286 default:
2287 break;
2288 }
2289
2290 end_nosignal:
2291 /*
2292 * Return 1 to indicate success since the 0 value can be a socket
2293 * shutdown during the recv() or send() call.
2294 */
2295 ret_func = 1;
2296 goto end;
2297
2298 end_msg_sessiond:
2299 /*
2300 * The returned value here is not useful since either way we'll return 1 to
2301 * the caller because the session daemon socket management is done
2302 * elsewhere. Returning a negative code or 0 will shutdown the consumer.
2303 */
2304 {
2305 int ret_send_status;
2306
2307 ret_send_status = consumer_send_status_msg(sock, ret_code);
2308 if (ret_send_status < 0) {
2309 goto error_fatal;
2310 }
2311 }
2312
2313 ret_func = 1;
2314 goto end;
2315
2316 end_channel_error:
2317 if (channel) {
2318 /*
2319 * Free channel here since no one has a reference to it. We don't
2320 * free after that because a stream can store this pointer.
2321 */
2322 destroy_channel(channel);
2323 }
2324 /* We have to send a status channel message indicating an error. */
2325 {
2326 int ret_send_status;
2327
2328 ret_send_status = consumer_send_status_channel(sock, NULL);
2329 if (ret_send_status < 0) {
2330 /* Stop everything if session daemon can not be notified. */
2331 goto error_fatal;
2332 }
2333 }
2334
2335 ret_func = 1;
2336 goto end;
2337
2338 error_fatal:
2339 /* This will issue a consumer stop. */
2340 ret_func = -1;
2341 goto end;
2342
2343 end:
2344 rcu_read_unlock();
2345 health_code_update();
2346 return ret_func;
2347 }
2348
2349 int lttng_ust_flush_buffer(struct lttng_consumer_stream *stream,
2350 int producer_active)
2351 {
2352 assert(stream);
2353 assert(stream->ustream);
2354
2355 return lttng_ust_ctl_flush_buffer(stream->ustream, producer_active);
2356 }
2357
2358 /*
2359 * Take a snapshot for a specific stream.
2360 *
2361 * Returns 0 on success, < 0 on error
2362 */
2363 int lttng_ustconsumer_take_snapshot(struct lttng_consumer_stream *stream)
2364 {
2365 assert(stream);
2366 assert(stream->ustream);
2367
2368 return lttng_ust_ctl_snapshot(stream->ustream);
2369 }
2370
2371 /*
2372 * Sample consumed and produced positions for a specific stream.
2373 *
2374 * Returns 0 on success, < 0 on error.
2375 */
2376 int lttng_ustconsumer_sample_snapshot_positions(
2377 struct lttng_consumer_stream *stream)
2378 {
2379 assert(stream);
2380 assert(stream->ustream);
2381
2382 return lttng_ust_ctl_snapshot_sample_positions(stream->ustream);
2383 }
2384
2385 /*
2386 * Get the produced position
2387 *
2388 * Returns 0 on success, < 0 on error
2389 */
2390 int lttng_ustconsumer_get_produced_snapshot(
2391 struct lttng_consumer_stream *stream, unsigned long *pos)
2392 {
2393 assert(stream);
2394 assert(stream->ustream);
2395 assert(pos);
2396
2397 return lttng_ust_ctl_snapshot_get_produced(stream->ustream, pos);
2398 }
2399
2400 /*
2401 * Get the consumed position
2402 *
2403 * Returns 0 on success, < 0 on error
2404 */
2405 int lttng_ustconsumer_get_consumed_snapshot(
2406 struct lttng_consumer_stream *stream, unsigned long *pos)
2407 {
2408 assert(stream);
2409 assert(stream->ustream);
2410 assert(pos);
2411
2412 return lttng_ust_ctl_snapshot_get_consumed(stream->ustream, pos);
2413 }
2414
2415 int lttng_ustconsumer_flush_buffer(struct lttng_consumer_stream *stream,
2416 int producer)
2417 {
2418 assert(stream);
2419 assert(stream->ustream);
2420
2421 return lttng_ust_ctl_flush_buffer(stream->ustream, producer);
2422 }
2423
2424 int lttng_ustconsumer_clear_buffer(struct lttng_consumer_stream *stream)
2425 {
2426 assert(stream);
2427 assert(stream->ustream);
2428
2429 return lttng_ust_ctl_clear_buffer(stream->ustream);
2430 }
2431
2432 int lttng_ustconsumer_get_current_timestamp(
2433 struct lttng_consumer_stream *stream, uint64_t *ts)
2434 {
2435 assert(stream);
2436 assert(stream->ustream);
2437 assert(ts);
2438
2439 return lttng_ust_ctl_get_current_timestamp(stream->ustream, ts);
2440 }
2441
2442 int lttng_ustconsumer_get_sequence_number(
2443 struct lttng_consumer_stream *stream, uint64_t *seq)
2444 {
2445 assert(stream);
2446 assert(stream->ustream);
2447 assert(seq);
2448
2449 return lttng_ust_ctl_get_sequence_number(stream->ustream, seq);
2450 }
2451
2452 /*
2453 * Called when the stream signals the consumer that it has hung up.
2454 */
2455 void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream *stream)
2456 {
2457 assert(stream);
2458 assert(stream->ustream);
2459
2460 pthread_mutex_lock(&stream->lock);
2461 if (!stream->quiescent) {
2462 if (lttng_ust_ctl_flush_buffer(stream->ustream, 0) < 0) {
2463 ERR("Failed to flush buffer on stream hang-up");
2464 } else {
2465 stream->quiescent = true;
2466 }
2467 }
2468 pthread_mutex_unlock(&stream->lock);
2469 stream->hangup_flush_done = 1;
2470 }
2471
2472 void lttng_ustconsumer_del_channel(struct lttng_consumer_channel *chan)
2473 {
2474 int i;
2475
2476 assert(chan);
2477 assert(chan->uchan);
2478 assert(chan->buffer_credentials.is_set);
2479
2480 if (chan->switch_timer_enabled == 1) {
2481 consumer_timer_switch_stop(chan);
2482 }
2483 for (i = 0; i < chan->nr_stream_fds; i++) {
2484 int ret;
2485
2486 ret = close(chan->stream_fds[i]);
2487 if (ret) {
2488 PERROR("close");
2489 }
2490 if (chan->shm_path[0]) {
2491 char shm_path[PATH_MAX];
2492
2493 ret = get_stream_shm_path(shm_path, chan->shm_path, i);
2494 if (ret) {
2495 ERR("Cannot get stream shm path");
2496 }
2497 ret = run_as_unlink(shm_path,
2498 lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(
2499 chan->buffer_credentials)),
2500 lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(
2501 chan->buffer_credentials)));
2502 if (ret) {
2503 PERROR("unlink %s", shm_path);
2504 }
2505 }
2506 }
2507 }
2508
2509 void lttng_ustconsumer_free_channel(struct lttng_consumer_channel *chan)
2510 {
2511 assert(chan);
2512 assert(chan->uchan);
2513 assert(chan->buffer_credentials.is_set);
2514
2515 consumer_metadata_cache_destroy(chan);
2516 lttng_ust_ctl_destroy_channel(chan->uchan);
2517 /* Try to rmdir all directories under shm_path root. */
2518 if (chan->root_shm_path[0]) {
2519 (void) run_as_rmdir_recursive(chan->root_shm_path,
2520 lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(
2521 chan->buffer_credentials)),
2522 lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(
2523 chan->buffer_credentials)),
2524 LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG);
2525 }
2526 free(chan->stream_fds);
2527 }
2528
2529 void lttng_ustconsumer_del_stream(struct lttng_consumer_stream *stream)
2530 {
2531 assert(stream);
2532 assert(stream->ustream);
2533
2534 if (stream->chan->switch_timer_enabled == 1) {
2535 consumer_timer_switch_stop(stream->chan);
2536 }
2537 lttng_ust_ctl_destroy_stream(stream->ustream);
2538 }
2539
2540 int lttng_ustconsumer_get_wakeup_fd(struct lttng_consumer_stream *stream)
2541 {
2542 assert(stream);
2543 assert(stream->ustream);
2544
2545 return lttng_ust_ctl_stream_get_wakeup_fd(stream->ustream);
2546 }
2547
2548 int lttng_ustconsumer_close_wakeup_fd(struct lttng_consumer_stream *stream)
2549 {
2550 assert(stream);
2551 assert(stream->ustream);
2552
2553 return lttng_ust_ctl_stream_close_wakeup_fd(stream->ustream);
2554 }
2555
2556 /*
2557 * Write up to one packet from the metadata cache to the channel.
2558 *
2559 * Returns the number of bytes pushed from the cache into the ring buffer, or a
2560 * negative value on error.
2561 */
2562 static
2563 int commit_one_metadata_packet(struct lttng_consumer_stream *stream)
2564 {
2565 ssize_t write_len;
2566 int ret;
2567
2568 pthread_mutex_lock(&stream->chan->metadata_cache->lock);
2569 if (stream->chan->metadata_cache->contents.size ==
2570 stream->ust_metadata_pushed) {
2571 /*
2572 * In the context of a user space metadata channel, a
2573 * change in version can be detected in two ways:
2574 * 1) During the pre-consume of the `read_subbuffer` loop,
2575 * 2) When populating the metadata ring buffer (i.e. here).
2576 *
2577 * This function is invoked when there is no metadata
2578 * available in the ring-buffer. If all data was consumed
2579 * up to the size of the metadata cache, there is no metadata
2580 * to insert in the ring-buffer.
2581 *
2582 * However, the metadata version could still have changed (a
2583 * regeneration without any new data will yield the same cache
2584 * size).
2585 *
2586 * The cache's version is checked for a version change and the
2587 * consumed position is reset if one occurred.
2588 *
2589 * This check is only necessary for the user space domain as
2590 * it has to manage the cache explicitly. If this reset was not
2591 * performed, no metadata would be consumed (and no reset would
2592 * occur as part of the pre-consume) until the metadata size
2593 * exceeded the cache size.
2594 */
2595 if (stream->metadata_version !=
2596 stream->chan->metadata_cache->version) {
2597 metadata_stream_reset_cache_consumed_position(stream);
2598 consumer_stream_metadata_set_version(stream,
2599 stream->chan->metadata_cache->version);
2600 } else {
2601 ret = 0;
2602 goto end;
2603 }
2604 }
2605
2606 write_len = lttng_ust_ctl_write_one_packet_to_channel(stream->chan->uchan,
2607 &stream->chan->metadata_cache->contents.data[stream->ust_metadata_pushed],
2608 stream->chan->metadata_cache->contents.size -
2609 stream->ust_metadata_pushed);
2610 assert(write_len != 0);
2611 if (write_len < 0) {
2612 ERR("Writing one metadata packet");
2613 ret = write_len;
2614 goto end;
2615 }
2616 stream->ust_metadata_pushed += write_len;
2617
2618 assert(stream->chan->metadata_cache->contents.size >=
2619 stream->ust_metadata_pushed);
2620 ret = write_len;
2621
2622 /*
2623 * Switch packet (but don't open the next one) on every commit of
2624 * a metadata packet. Since the subbuffer is fully filled (with padding,
2625 * if needed), the stream is "quiescent" after this commit.
2626 */
2627 if (lttng_ust_ctl_flush_buffer(stream->ustream, 1)) {
2628 ERR("Failed to flush buffer while commiting one metadata packet");
2629 ret = -EIO;
2630 } else {
2631 stream->quiescent = true;
2632 }
2633 end:
2634 pthread_mutex_unlock(&stream->chan->metadata_cache->lock);
2635 return ret;
2636 }
2637
2638
2639 /*
2640 * Sync metadata meaning request them to the session daemon and snapshot to the
2641 * metadata thread can consumer them.
2642 *
2643 * Metadata stream lock is held here, but we need to release it when
2644 * interacting with sessiond, else we cause a deadlock with live
2645 * awaiting on metadata to be pushed out.
2646 *
2647 * The RCU read side lock must be held by the caller.
2648 */
2649 enum sync_metadata_status lttng_ustconsumer_sync_metadata(
2650 struct lttng_consumer_local_data *ctx,
2651 struct lttng_consumer_stream *metadata_stream)
2652 {
2653 int ret;
2654 enum sync_metadata_status status;
2655 struct lttng_consumer_channel *metadata_channel;
2656
2657 assert(ctx);
2658 assert(metadata_stream);
2659
2660 metadata_channel = metadata_stream->chan;
2661 pthread_mutex_unlock(&metadata_stream->lock);
2662 /*
2663 * Request metadata from the sessiond, but don't wait for the flush
2664 * because we locked the metadata thread.
2665 */
2666 ret = lttng_ustconsumer_request_metadata(ctx, metadata_channel, 0, 0);
2667 pthread_mutex_lock(&metadata_stream->lock);
2668 if (ret < 0) {
2669 status = SYNC_METADATA_STATUS_ERROR;
2670 goto end;
2671 }
2672
2673 /*
2674 * The metadata stream and channel can be deleted while the
2675 * metadata stream lock was released. The streamed is checked
2676 * for deletion before we use it further.
2677 *
2678 * Note that it is safe to access a logically-deleted stream since its
2679 * existence is still guaranteed by the RCU read side lock. However,
2680 * it should no longer be used. The close/deletion of the metadata
2681 * channel and stream already guarantees that all metadata has been
2682 * consumed. Therefore, there is nothing left to do in this function.
2683 */
2684 if (consumer_stream_is_deleted(metadata_stream)) {
2685 DBG("Metadata stream %" PRIu64 " was deleted during the metadata synchronization",
2686 metadata_stream->key);
2687 status = SYNC_METADATA_STATUS_NO_DATA;
2688 goto end;
2689 }
2690
2691 ret = commit_one_metadata_packet(metadata_stream);
2692 if (ret < 0) {
2693 status = SYNC_METADATA_STATUS_ERROR;
2694 goto end;
2695 } else if (ret > 0) {
2696 status = SYNC_METADATA_STATUS_NEW_DATA;
2697 } else /* ret == 0 */ {
2698 status = SYNC_METADATA_STATUS_NO_DATA;
2699 goto end;
2700 }
2701
2702 ret = lttng_ust_ctl_snapshot(metadata_stream->ustream);
2703 if (ret < 0) {
2704 ERR("Failed to take a snapshot of the metadata ring-buffer positions, ret = %d", ret);
2705 status = SYNC_METADATA_STATUS_ERROR;
2706 goto end;
2707 }
2708
2709 end:
2710 return status;
2711 }
2712
2713 /*
2714 * Return 0 on success else a negative value.
2715 */
2716 static int notify_if_more_data(struct lttng_consumer_stream *stream,
2717 struct lttng_consumer_local_data *ctx)
2718 {
2719 int ret;
2720 struct lttng_ust_ctl_consumer_stream *ustream;
2721
2722 assert(stream);
2723 assert(ctx);
2724
2725 ustream = stream->ustream;
2726
2727 /*
2728 * First, we are going to check if there is a new subbuffer available
2729 * before reading the stream wait_fd.
2730 */
2731 /* Get the next subbuffer */
2732 ret = lttng_ust_ctl_get_next_subbuf(ustream);
2733 if (ret) {
2734 /* No more data found, flag the stream. */
2735 stream->has_data = 0;
2736 ret = 0;
2737 goto end;
2738 }
2739
2740 ret = lttng_ust_ctl_put_subbuf(ustream);
2741 assert(!ret);
2742
2743 /* This stream still has data. Flag it and wake up the data thread. */
2744 stream->has_data = 1;
2745
2746 if (stream->monitor && !stream->hangup_flush_done && !ctx->has_wakeup) {
2747 ssize_t writelen;
2748
2749 writelen = lttng_pipe_write(ctx->consumer_wakeup_pipe, "!", 1);
2750 if (writelen < 0 && errno != EAGAIN && errno != EWOULDBLOCK) {
2751 ret = writelen;
2752 goto end;
2753 }
2754
2755 /* The wake up pipe has been notified. */
2756 ctx->has_wakeup = 1;
2757 }
2758 ret = 0;
2759
2760 end:
2761 return ret;
2762 }
2763
2764 static int consumer_stream_ust_on_wake_up(struct lttng_consumer_stream *stream)
2765 {
2766 int ret = 0;
2767
2768 /*
2769 * We can consume the 1 byte written into the wait_fd by
2770 * UST. Don't trigger error if we cannot read this one byte
2771 * (read returns 0), or if the error is EAGAIN or EWOULDBLOCK.
2772 *
2773 * This is only done when the stream is monitored by a thread,
2774 * before the flush is done after a hangup and if the stream
2775 * is not flagged with data since there might be nothing to
2776 * consume in the wait fd but still have data available
2777 * flagged by the consumer wake up pipe.
2778 */
2779 if (stream->monitor && !stream->hangup_flush_done && !stream->has_data) {
2780 char dummy;
2781 ssize_t readlen;
2782
2783 readlen = lttng_read(stream->wait_fd, &dummy, 1);
2784 if (readlen < 0 && errno != EAGAIN && errno != EWOULDBLOCK) {
2785 ret = readlen;
2786 }
2787 }
2788
2789 return ret;
2790 }
2791
2792 static int extract_common_subbuffer_info(struct lttng_consumer_stream *stream,
2793 struct stream_subbuffer *subbuf)
2794 {
2795 int ret;
2796
2797 ret = lttng_ust_ctl_get_subbuf_size(
2798 stream->ustream, &subbuf->info.data.subbuf_size);
2799 if (ret) {
2800 goto end;
2801 }
2802
2803 ret = lttng_ust_ctl_get_padded_subbuf_size(
2804 stream->ustream, &subbuf->info.data.padded_subbuf_size);
2805 if (ret) {
2806 goto end;
2807 }
2808
2809 end:
2810 return ret;
2811 }
2812
2813 static int extract_metadata_subbuffer_info(struct lttng_consumer_stream *stream,
2814 struct stream_subbuffer *subbuf)
2815 {
2816 int ret;
2817
2818 ret = extract_common_subbuffer_info(stream, subbuf);
2819 if (ret) {
2820 goto end;
2821 }
2822
2823 subbuf->info.metadata.version = stream->metadata_version;
2824
2825 end:
2826 return ret;
2827 }
2828
2829 static int extract_data_subbuffer_info(struct lttng_consumer_stream *stream,
2830 struct stream_subbuffer *subbuf)
2831 {
2832 int ret;
2833
2834 ret = extract_common_subbuffer_info(stream, subbuf);
2835 if (ret) {
2836 goto end;
2837 }
2838
2839 ret = lttng_ust_ctl_get_packet_size(
2840 stream->ustream, &subbuf->info.data.packet_size);
2841 if (ret < 0) {
2842 PERROR("Failed to get sub-buffer packet size");
2843 goto end;
2844 }
2845
2846 ret = lttng_ust_ctl_get_content_size(
2847 stream->ustream, &subbuf->info.data.content_size);
2848 if (ret < 0) {
2849 PERROR("Failed to get sub-buffer content size");
2850 goto end;
2851 }
2852
2853 ret = lttng_ust_ctl_get_timestamp_begin(
2854 stream->ustream, &subbuf->info.data.timestamp_begin);
2855 if (ret < 0) {
2856 PERROR("Failed to get sub-buffer begin timestamp");
2857 goto end;
2858 }
2859
2860 ret = lttng_ust_ctl_get_timestamp_end(
2861 stream->ustream, &subbuf->info.data.timestamp_end);
2862 if (ret < 0) {
2863 PERROR("Failed to get sub-buffer end timestamp");
2864 goto end;
2865 }
2866
2867 ret = lttng_ust_ctl_get_events_discarded(
2868 stream->ustream, &subbuf->info.data.events_discarded);
2869 if (ret) {
2870 PERROR("Failed to get sub-buffer events discarded count");
2871 goto end;
2872 }
2873
2874 ret = lttng_ust_ctl_get_sequence_number(stream->ustream,
2875 &subbuf->info.data.sequence_number.value);
2876 if (ret) {
2877 /* May not be supported by older LTTng-modules. */
2878 if (ret != -ENOTTY) {
2879 PERROR("Failed to get sub-buffer sequence number");
2880 goto end;
2881 }
2882 } else {
2883 subbuf->info.data.sequence_number.is_set = true;
2884 }
2885
2886 ret = lttng_ust_ctl_get_stream_id(
2887 stream->ustream, &subbuf->info.data.stream_id);
2888 if (ret < 0) {
2889 PERROR("Failed to get stream id");
2890 goto end;
2891 }
2892
2893 ret = lttng_ust_ctl_get_instance_id(stream->ustream,
2894 &subbuf->info.data.stream_instance_id.value);
2895 if (ret) {
2896 /* May not be supported by older LTTng-modules. */
2897 if (ret != -ENOTTY) {
2898 PERROR("Failed to get stream instance id");
2899 goto end;
2900 }
2901 } else {
2902 subbuf->info.data.stream_instance_id.is_set = true;
2903 }
2904 end:
2905 return ret;
2906 }
2907
2908 static int get_next_subbuffer_common(struct lttng_consumer_stream *stream,
2909 struct stream_subbuffer *subbuffer)
2910 {
2911 int ret;
2912 const char *addr;
2913
2914 ret = stream->read_subbuffer_ops.extract_subbuffer_info(
2915 stream, subbuffer);
2916 if (ret) {
2917 goto end;
2918 }
2919
2920 ret = get_current_subbuf_addr(stream, &addr);
2921 if (ret) {
2922 goto end;
2923 }
2924
2925 subbuffer->buffer.buffer = lttng_buffer_view_init(
2926 addr, 0, subbuffer->info.data.padded_subbuf_size);
2927 assert(subbuffer->buffer.buffer.data != NULL);
2928 end:
2929 return ret;
2930 }
2931
2932 static enum get_next_subbuffer_status get_next_subbuffer(
2933 struct lttng_consumer_stream *stream,
2934 struct stream_subbuffer *subbuffer)
2935 {
2936 int ret;
2937 enum get_next_subbuffer_status status;
2938
2939 ret = lttng_ust_ctl_get_next_subbuf(stream->ustream);
2940 switch (ret) {
2941 case 0:
2942 status = GET_NEXT_SUBBUFFER_STATUS_OK;
2943 break;
2944 case -ENODATA:
2945 case -EAGAIN:
2946 /*
2947 * The caller only expects -ENODATA when there is no data to
2948 * read, but the kernel tracer returns -EAGAIN when there is
2949 * currently no data for a non-finalized stream, and -ENODATA
2950 * when there is no data for a finalized stream. Those can be
2951 * combined into a -ENODATA return value.
2952 */
2953 status = GET_NEXT_SUBBUFFER_STATUS_NO_DATA;
2954 goto end;
2955 default:
2956 status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
2957 goto end;
2958 }
2959
2960 ret = get_next_subbuffer_common(stream, subbuffer);
2961 if (ret) {
2962 status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
2963 goto end;
2964 }
2965 end:
2966 return status;
2967 }
2968
2969 static enum get_next_subbuffer_status get_next_subbuffer_metadata(
2970 struct lttng_consumer_stream *stream,
2971 struct stream_subbuffer *subbuffer)
2972 {
2973 int ret;
2974 bool cache_empty;
2975 bool got_subbuffer;
2976 bool coherent;
2977 bool buffer_empty;
2978 unsigned long consumed_pos, produced_pos;
2979 enum get_next_subbuffer_status status;
2980
2981 do {
2982 ret = lttng_ust_ctl_get_next_subbuf(stream->ustream);
2983 if (ret == 0) {
2984 got_subbuffer = true;
2985 } else {
2986 got_subbuffer = false;
2987 if (ret != -EAGAIN) {
2988 /* Fatal error. */
2989 status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
2990 goto end;
2991 }
2992 }
2993
2994 /*
2995 * Determine if the cache is empty and ensure that a sub-buffer
2996 * is made available if the cache is not empty.
2997 */
2998 if (!got_subbuffer) {
2999 ret = commit_one_metadata_packet(stream);
3000 if (ret < 0 && ret != -ENOBUFS) {
3001 status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
3002 goto end;
3003 } else if (ret == 0) {
3004 /* Not an error, the cache is empty. */
3005 cache_empty = true;
3006 status = GET_NEXT_SUBBUFFER_STATUS_NO_DATA;
3007 goto end;
3008 } else {
3009 cache_empty = false;
3010 }
3011 } else {
3012 pthread_mutex_lock(&stream->chan->metadata_cache->lock);
3013 cache_empty = stream->chan->metadata_cache->contents.size ==
3014 stream->ust_metadata_pushed;
3015 pthread_mutex_unlock(&stream->chan->metadata_cache->lock);
3016 }
3017 } while (!got_subbuffer);
3018
3019 /* Populate sub-buffer infos and view. */
3020 ret = get_next_subbuffer_common(stream, subbuffer);
3021 if (ret) {
3022 status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
3023 goto end;
3024 }
3025
3026 ret = lttng_ustconsumer_sample_snapshot_positions(stream);
3027 if (ret < 0) {
3028 /*
3029 * -EAGAIN is not expected since we got a sub-buffer and haven't
3030 * pushed the consumption position yet (on put_next).
3031 */
3032 PERROR("Failed to take a snapshot of metadata buffer positions");
3033 status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
3034 goto end;
3035 }
3036
3037 ret = lttng_ustconsumer_get_consumed_snapshot(stream, &consumed_pos);
3038 if (ret) {
3039 PERROR("Failed to get metadata consumed position");
3040 status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
3041 goto end;
3042 }
3043
3044 ret = lttng_ustconsumer_get_produced_snapshot(stream, &produced_pos);
3045 if (ret) {
3046 PERROR("Failed to get metadata produced position");
3047 status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
3048 goto end;
3049 }
3050
3051 /* Last sub-buffer of the ring buffer ? */
3052 buffer_empty = (consumed_pos + stream->max_sb_size) == produced_pos;
3053
3054 /*
3055 * The sessiond registry lock ensures that coherent units of metadata
3056 * are pushed to the consumer daemon at once. Hence, if a sub-buffer is
3057 * acquired, the cache is empty, and it is the only available sub-buffer
3058 * available, it is safe to assume that it is "coherent".
3059 */
3060 coherent = got_subbuffer && cache_empty && buffer_empty;
3061
3062 LTTNG_OPTIONAL_SET(&subbuffer->info.metadata.coherent, coherent);
3063 status = GET_NEXT_SUBBUFFER_STATUS_OK;
3064 end:
3065 return status;
3066 }
3067
3068 static int put_next_subbuffer(struct lttng_consumer_stream *stream,
3069 struct stream_subbuffer *subbuffer)
3070 {
3071 const int ret = lttng_ust_ctl_put_next_subbuf(stream->ustream);
3072
3073 assert(ret == 0);
3074 return ret;
3075 }
3076
3077 static int signal_metadata(struct lttng_consumer_stream *stream,
3078 struct lttng_consumer_local_data *ctx)
3079 {
3080 ASSERT_LOCKED(stream->metadata_rdv_lock);
3081 return pthread_cond_broadcast(&stream->metadata_rdv) ? -errno : 0;
3082 }
3083
3084 static int lttng_ustconsumer_set_stream_ops(
3085 struct lttng_consumer_stream *stream)
3086 {
3087 int ret = 0;
3088
3089 stream->read_subbuffer_ops.on_wake_up = consumer_stream_ust_on_wake_up;
3090 if (stream->metadata_flag) {
3091 stream->read_subbuffer_ops.get_next_subbuffer =
3092 get_next_subbuffer_metadata;
3093 stream->read_subbuffer_ops.extract_subbuffer_info =
3094 extract_metadata_subbuffer_info;
3095 stream->read_subbuffer_ops.reset_metadata =
3096 metadata_stream_reset_cache_consumed_position;
3097 if (stream->chan->is_live) {
3098 stream->read_subbuffer_ops.on_sleep = signal_metadata;
3099 ret = consumer_stream_enable_metadata_bucketization(
3100 stream);
3101 if (ret) {
3102 goto end;
3103 }
3104 }
3105 } else {
3106 stream->read_subbuffer_ops.get_next_subbuffer =
3107 get_next_subbuffer;
3108 stream->read_subbuffer_ops.extract_subbuffer_info =
3109 extract_data_subbuffer_info;
3110 stream->read_subbuffer_ops.on_sleep = notify_if_more_data;
3111 if (stream->chan->is_live) {
3112 stream->read_subbuffer_ops.send_live_beacon =
3113 consumer_flush_ust_index;
3114 }
3115 }
3116
3117 stream->read_subbuffer_ops.put_next_subbuffer = put_next_subbuffer;
3118 end:
3119 return ret;
3120 }
3121
3122 /*
3123 * Called when a stream is created.
3124 *
3125 * Return 0 on success or else a negative value.
3126 */
3127 int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream *stream)
3128 {
3129 int ret;
3130
3131 assert(stream);
3132
3133 /*
3134 * Don't create anything if this is set for streaming or if there is
3135 * no current trace chunk on the parent channel.
3136 */
3137 if (stream->net_seq_idx == (uint64_t) -1ULL && stream->chan->monitor &&
3138 stream->chan->trace_chunk) {
3139 ret = consumer_stream_create_output_files(stream, true);
3140 if (ret) {
3141 goto error;
3142 }
3143 }
3144
3145 lttng_ustconsumer_set_stream_ops(stream);
3146 ret = 0;
3147
3148 error:
3149 return ret;
3150 }
3151
3152 /*
3153 * Check if data is still being extracted from the buffers for a specific
3154 * stream. Consumer data lock MUST be acquired before calling this function
3155 * and the stream lock.
3156 *
3157 * Return 1 if the traced data are still getting read else 0 meaning that the
3158 * data is available for trace viewer reading.
3159 */
3160 int lttng_ustconsumer_data_pending(struct lttng_consumer_stream *stream)
3161 {
3162 int ret;
3163
3164 assert(stream);
3165 assert(stream->ustream);
3166 ASSERT_LOCKED(stream->lock);
3167
3168 DBG("UST consumer checking data pending");
3169
3170 if (stream->endpoint_status != CONSUMER_ENDPOINT_ACTIVE) {
3171 ret = 0;
3172 goto end;
3173 }
3174
3175 if (stream->chan->type == CONSUMER_CHANNEL_TYPE_METADATA) {
3176 uint64_t contiguous, pushed;
3177
3178 /* Ease our life a bit. */
3179 pthread_mutex_lock(&stream->chan->metadata_cache->lock);
3180 contiguous = stream->chan->metadata_cache->contents.size;
3181 pthread_mutex_unlock(&stream->chan->metadata_cache->lock);
3182 pushed = stream->ust_metadata_pushed;
3183
3184 /*
3185 * We can simply check whether all contiguously available data
3186 * has been pushed to the ring buffer, since the push operation
3187 * is performed within get_next_subbuf(), and because both
3188 * get_next_subbuf() and put_next_subbuf() are issued atomically
3189 * thanks to the stream lock within
3190 * lttng_ustconsumer_read_subbuffer(). This basically means that
3191 * whetnever ust_metadata_pushed is incremented, the associated
3192 * metadata has been consumed from the metadata stream.
3193 */
3194 DBG("UST consumer metadata pending check: contiguous %" PRIu64 " vs pushed %" PRIu64,
3195 contiguous, pushed);
3196 assert(((int64_t) (contiguous - pushed)) >= 0);
3197 if ((contiguous != pushed) ||
3198 (((int64_t) contiguous - pushed) > 0 || contiguous == 0)) {
3199 ret = 1; /* Data is pending */
3200 goto end;
3201 }
3202 } else {
3203 ret = lttng_ust_ctl_get_next_subbuf(stream->ustream);
3204 if (ret == 0) {
3205 /*
3206 * There is still data so let's put back this
3207 * subbuffer.
3208 */
3209 ret = lttng_ust_ctl_put_subbuf(stream->ustream);
3210 assert(ret == 0);
3211 ret = 1; /* Data is pending */
3212 goto end;
3213 }
3214 }
3215
3216 /* Data is NOT pending so ready to be read. */
3217 ret = 0;
3218
3219 end:
3220 return ret;
3221 }
3222
3223 /*
3224 * Stop a given metadata channel timer if enabled and close the wait fd which
3225 * is the poll pipe of the metadata stream.
3226 *
3227 * This MUST be called with the metadata channel lock acquired.
3228 */
3229 void lttng_ustconsumer_close_metadata(struct lttng_consumer_channel *metadata)
3230 {
3231 int ret;
3232
3233 assert(metadata);
3234 assert(metadata->type == CONSUMER_CHANNEL_TYPE_METADATA);
3235
3236 DBG("Closing metadata channel key %" PRIu64, metadata->key);
3237
3238 if (metadata->switch_timer_enabled == 1) {
3239 consumer_timer_switch_stop(metadata);
3240 }
3241
3242 if (!metadata->metadata_stream) {
3243 goto end;
3244 }
3245
3246 /*
3247 * Closing write side so the thread monitoring the stream wakes up if any
3248 * and clean the metadata stream.
3249 */
3250 if (metadata->metadata_stream->ust_metadata_poll_pipe[1] >= 0) {
3251 ret = close(metadata->metadata_stream->ust_metadata_poll_pipe[1]);
3252 if (ret < 0) {
3253 PERROR("closing metadata pipe write side");
3254 }
3255 metadata->metadata_stream->ust_metadata_poll_pipe[1] = -1;
3256 }
3257
3258 end:
3259 return;
3260 }
3261
3262 /*
3263 * Close every metadata stream wait fd of the metadata hash table. This
3264 * function MUST be used very carefully so not to run into a race between the
3265 * metadata thread handling streams and this function closing their wait fd.
3266 *
3267 * For UST, this is used when the session daemon hangs up. Its the metadata
3268 * producer so calling this is safe because we are assured that no state change
3269 * can occur in the metadata thread for the streams in the hash table.
3270 */
3271 void lttng_ustconsumer_close_all_metadata(struct lttng_ht *metadata_ht)
3272 {
3273 struct lttng_ht_iter iter;
3274 struct lttng_consumer_stream *stream;
3275
3276 assert(metadata_ht);
3277 assert(metadata_ht->ht);
3278
3279 DBG("UST consumer closing all metadata streams");
3280
3281 rcu_read_lock();
3282 cds_lfht_for_each_entry(metadata_ht->ht, &iter.iter, stream,
3283 node.node) {
3284
3285 health_code_update();
3286
3287 pthread_mutex_lock(&stream->chan->lock);
3288 lttng_ustconsumer_close_metadata(stream->chan);
3289 pthread_mutex_unlock(&stream->chan->lock);
3290
3291 }
3292 rcu_read_unlock();
3293 }
3294
3295 void lttng_ustconsumer_close_stream_wakeup(struct lttng_consumer_stream *stream)
3296 {
3297 int ret;
3298
3299 ret = lttng_ust_ctl_stream_close_wakeup_fd(stream->ustream);
3300 if (ret < 0) {
3301 ERR("Unable to close wakeup fd");
3302 }
3303 }
3304
3305 /*
3306 * Please refer to consumer-timer.c before adding any lock within this
3307 * function or any of its callees. Timers have a very strict locking
3308 * semantic with respect to teardown. Failure to respect this semantic
3309 * introduces deadlocks.
3310 *
3311 * DON'T hold the metadata lock when calling this function, else this
3312 * can cause deadlock involving consumer awaiting for metadata to be
3313 * pushed out due to concurrent interaction with the session daemon.
3314 */
3315 int lttng_ustconsumer_request_metadata(struct lttng_consumer_local_data *ctx,
3316 struct lttng_consumer_channel *channel, int timer, int wait)
3317 {
3318 struct lttcomm_metadata_request_msg request;
3319 struct lttcomm_consumer_msg msg;
3320 enum lttcomm_return_code ret_code = LTTCOMM_CONSUMERD_SUCCESS;
3321 uint64_t len, key, offset, version;
3322 int ret;
3323
3324 assert(channel);
3325 assert(channel->metadata_cache);
3326
3327 memset(&request, 0, sizeof(request));
3328
3329 /* send the metadata request to sessiond */
3330 switch (the_consumer_data.type) {
3331 case LTTNG_CONSUMER64_UST:
3332 request.bits_per_long = 64;
3333 break;
3334 case LTTNG_CONSUMER32_UST:
3335 request.bits_per_long = 32;
3336 break;
3337 default:
3338 request.bits_per_long = 0;
3339 break;
3340 }
3341
3342 request.session_id = channel->session_id;
3343 request.session_id_per_pid = channel->session_id_per_pid;
3344 /*
3345 * Request the application UID here so the metadata of that application can
3346 * be sent back. The channel UID corresponds to the user UID of the session
3347 * used for the rights on the stream file(s).
3348 */
3349 request.uid = channel->ust_app_uid;
3350 request.key = channel->key;
3351
3352 DBG("Sending metadata request to sessiond, session id %" PRIu64
3353 ", per-pid %" PRIu64 ", app UID %u and channel key %" PRIu64,
3354 request.session_id, request.session_id_per_pid, request.uid,
3355 request.key);
3356
3357 pthread_mutex_lock(&ctx->metadata_socket_lock);
3358
3359 health_code_update();
3360
3361 ret = lttcomm_send_unix_sock(ctx->consumer_metadata_socket, &request,
3362 sizeof(request));
3363 if (ret < 0) {
3364 ERR("Asking metadata to sessiond");
3365 goto end;
3366 }
3367
3368 health_code_update();
3369
3370 /* Receive the metadata from sessiond */
3371 ret = lttcomm_recv_unix_sock(ctx->consumer_metadata_socket, &msg,
3372 sizeof(msg));
3373 if (ret != sizeof(msg)) {
3374 DBG("Consumer received unexpected message size %d (expects %zu)",
3375 ret, sizeof(msg));
3376 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_CMD);
3377 /*
3378 * The ret value might 0 meaning an orderly shutdown but this is ok
3379 * since the caller handles this.
3380 */
3381 goto end;
3382 }
3383
3384 health_code_update();
3385
3386 if (msg.cmd_type == LTTNG_ERR_UND) {
3387 /* No registry found */
3388 (void) consumer_send_status_msg(ctx->consumer_metadata_socket,
3389 ret_code);
3390 ret = 0;
3391 goto end;
3392 } else if (msg.cmd_type != LTTNG_CONSUMER_PUSH_METADATA) {
3393 ERR("Unexpected cmd_type received %d", msg.cmd_type);
3394 ret = -1;
3395 goto end;
3396 }
3397
3398 len = msg.u.push_metadata.len;
3399 key = msg.u.push_metadata.key;
3400 offset = msg.u.push_metadata.target_offset;
3401 version = msg.u.push_metadata.version;
3402
3403 assert(key == channel->key);
3404 if (len == 0) {
3405 DBG("No new metadata to receive for key %" PRIu64, key);
3406 }
3407
3408 health_code_update();
3409
3410 /* Tell session daemon we are ready to receive the metadata. */
3411 ret = consumer_send_status_msg(ctx->consumer_metadata_socket,
3412 LTTCOMM_CONSUMERD_SUCCESS);
3413 if (ret < 0 || len == 0) {
3414 /*
3415 * Somehow, the session daemon is not responding anymore or there is
3416 * nothing to receive.
3417 */
3418 goto end;
3419 }
3420
3421 health_code_update();
3422
3423 ret = lttng_ustconsumer_recv_metadata(ctx->consumer_metadata_socket,
3424 key, offset, len, version, channel, timer, wait);
3425 if (ret >= 0) {
3426 /*
3427 * Only send the status msg if the sessiond is alive meaning a positive
3428 * ret code.
3429 */
3430 (void) consumer_send_status_msg(ctx->consumer_metadata_socket, ret);
3431 }
3432 ret = 0;
3433
3434 end:
3435 health_code_update();
3436
3437 pthread_mutex_unlock(&ctx->metadata_socket_lock);
3438 return ret;
3439 }
3440
3441 /*
3442 * Return the ustctl call for the get stream id.
3443 */
3444 int lttng_ustconsumer_get_stream_id(struct lttng_consumer_stream *stream,
3445 uint64_t *stream_id)
3446 {
3447 assert(stream);
3448 assert(stream_id);
3449
3450 return lttng_ust_ctl_get_stream_id(stream->ustream, stream_id);
3451 }
3452
3453 void lttng_ustconsumer_sigbus_handle(void *addr)
3454 {
3455 lttng_ust_ctl_sigbus_handle(addr);
3456 }
This page took 0.151364 seconds and 3 git commands to generate.