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