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