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