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