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