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