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