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