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