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