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