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