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