Fix: streamline ret/errno of run_as()
[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>
4 *
d14d33bf
AM
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
3bd1e081
MD
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
d14d33bf
AM
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
3bd1e081
MD
17 */
18
19#define _GNU_SOURCE
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>
0857097f 35
51a9e1c7 36#include <bin/lttng-consumerd/health-consumerd.h>
990570ed 37#include <common/common.h>
10a8a223 38#include <common/sessiond-comm/sessiond-comm.h>
00e2e675 39#include <common/relayd/relayd.h>
dbb5dfe6 40#include <common/compat/fcntl.h>
f263b7fd 41#include <common/compat/endian.h>
331744e3 42#include <common/consumer-metadata-cache.h>
10a50311 43#include <common/consumer-stream.h>
331744e3 44#include <common/consumer-timer.h>
fe4477ee 45#include <common/utils.h>
309167d2 46#include <common/index/index.h>
10a8a223
DG
47
48#include "ust-consumer.h"
3bd1e081 49
4628484a
MD
50#define UINT_MAX_STR_LEN 11 /* includes \0 */
51
3bd1e081
MD
52extern struct lttng_consumer_global_data consumer_data;
53extern int consumer_poll_timeout;
54extern volatile int consumer_quit;
55
56/*
ffe60014
DG
57 * Free channel object and all streams associated with it. This MUST be used
58 * only and only if the channel has _NEVER_ been added to the global channel
59 * hash table.
3bd1e081 60 */
ffe60014 61static void destroy_channel(struct lttng_consumer_channel *channel)
3bd1e081 62{
ffe60014
DG
63 struct lttng_consumer_stream *stream, *stmp;
64
65 assert(channel);
66
67 DBG("UST consumer cleaning stream list");
68
69 cds_list_for_each_entry_safe(stream, stmp, &channel->streams.head,
70 send_node) {
9ce5646a
MD
71
72 health_code_update();
73
ffe60014
DG
74 cds_list_del(&stream->send_node);
75 ustctl_destroy_stream(stream->ustream);
76 free(stream);
77 }
78
79 /*
80 * If a channel is available meaning that was created before the streams
81 * were, delete it.
82 */
83 if (channel->uchan) {
84 lttng_ustconsumer_del_channel(channel);
85 }
86 free(channel);
87}
3bd1e081
MD
88
89/*
ffe60014 90 * Add channel to internal consumer state.
3bd1e081 91 *
ffe60014 92 * Returns 0 on success or else a negative value.
3bd1e081 93 */
ffe60014
DG
94static int add_channel(struct lttng_consumer_channel *channel,
95 struct lttng_consumer_local_data *ctx)
3bd1e081
MD
96{
97 int ret = 0;
98
ffe60014
DG
99 assert(channel);
100 assert(ctx);
101
102 if (ctx->on_recv_channel != NULL) {
103 ret = ctx->on_recv_channel(channel);
104 if (ret == 0) {
d8ef542d 105 ret = consumer_add_channel(channel, ctx);
ffe60014
DG
106 } else if (ret < 0) {
107 /* Most likely an ENOMEM. */
108 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
109 goto error;
110 }
111 } else {
d8ef542d 112 ret = consumer_add_channel(channel, ctx);
3bd1e081
MD
113 }
114
d88aee68 115 DBG("UST consumer channel added (key: %" PRIu64 ")", channel->key);
ffe60014
DG
116
117error:
3bd1e081
MD
118 return ret;
119}
120
121/*
ffe60014
DG
122 * Allocate and return a consumer channel object.
123 */
124static struct lttng_consumer_channel *allocate_channel(uint64_t session_id,
125 const char *pathname, const char *name, uid_t uid, gid_t gid,
da009f2c 126 uint64_t relayd_id, uint64_t key, enum lttng_event_output output,
2bba9e53 127 uint64_t tracefile_size, uint64_t tracefile_count,
ecc48a90 128 uint64_t session_id_per_pid, unsigned int monitor,
d7ba1388 129 unsigned int live_timer_interval,
3d071855 130 const char *root_shm_path, const char *shm_path)
ffe60014
DG
131{
132 assert(pathname);
133 assert(name);
134
1950109e
JD
135 return consumer_allocate_channel(key, session_id, pathname, name, uid,
136 gid, relayd_id, output, tracefile_size,
d7ba1388 137 tracefile_count, session_id_per_pid, monitor,
3d071855 138 live_timer_interval, root_shm_path, shm_path);
ffe60014
DG
139}
140
141/*
142 * Allocate and return a consumer stream object. If _alloc_ret is not NULL, the
143 * error value if applicable is set in it else it is kept untouched.
3bd1e081 144 *
ffe60014 145 * Return NULL on error else the newly allocated stream object.
3bd1e081 146 */
ffe60014
DG
147static struct lttng_consumer_stream *allocate_stream(int cpu, int key,
148 struct lttng_consumer_channel *channel,
149 struct lttng_consumer_local_data *ctx, int *_alloc_ret)
150{
151 int alloc_ret;
152 struct lttng_consumer_stream *stream = NULL;
153
154 assert(channel);
155 assert(ctx);
156
157 stream = consumer_allocate_stream(channel->key,
158 key,
159 LTTNG_CONSUMER_ACTIVE_STREAM,
160 channel->name,
161 channel->uid,
162 channel->gid,
163 channel->relayd_id,
164 channel->session_id,
165 cpu,
166 &alloc_ret,
4891ece8
DG
167 channel->type,
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
187 stream->chan = channel;
188
189error:
190 if (_alloc_ret) {
191 *_alloc_ret = alloc_ret;
192 }
193 return stream;
194}
195
196/*
197 * Send the given stream pointer to the corresponding thread.
198 *
199 * Returns 0 on success else a negative value.
200 */
201static int send_stream_to_thread(struct lttng_consumer_stream *stream,
202 struct lttng_consumer_local_data *ctx)
203{
dae10966
DG
204 int ret;
205 struct lttng_pipe *stream_pipe;
ffe60014
DG
206
207 /* Get the right pipe where the stream will be sent. */
208 if (stream->metadata_flag) {
5ab66908
MD
209 ret = consumer_add_metadata_stream(stream);
210 if (ret) {
211 ERR("Consumer add metadata stream %" PRIu64 " failed.",
212 stream->key);
213 goto error;
214 }
dae10966 215 stream_pipe = ctx->consumer_metadata_pipe;
ffe60014 216 } else {
5ab66908
MD
217 ret = consumer_add_data_stream(stream);
218 if (ret) {
219 ERR("Consumer add stream %" PRIu64 " failed.",
220 stream->key);
221 goto error;
222 }
dae10966 223 stream_pipe = ctx->consumer_data_pipe;
ffe60014
DG
224 }
225
5ab66908
MD
226 /*
227 * From this point on, the stream's ownership has been moved away from
228 * the channel and becomes globally visible.
229 */
230 stream->globally_visible = 1;
231
dae10966 232 ret = lttng_pipe_write(stream_pipe, &stream, sizeof(stream));
ffe60014 233 if (ret < 0) {
dae10966
DG
234 ERR("Consumer write %s stream to pipe %d",
235 stream->metadata_flag ? "metadata" : "data",
236 lttng_pipe_get_writefd(stream_pipe));
5ab66908
MD
237 if (stream->metadata_flag) {
238 consumer_del_stream_for_metadata(stream);
239 } else {
240 consumer_del_stream_for_data(stream);
241 }
ffe60014 242 }
5ab66908 243error:
ffe60014
DG
244 return ret;
245}
246
4628484a
MD
247static
248int get_stream_shm_path(char *stream_shm_path, const char *shm_path, int cpu)
249{
250 char cpu_nr[UINT_MAX_STR_LEN]; /* unsigned int max len */
251 int ret;
252
253 strncpy(stream_shm_path, shm_path, PATH_MAX);
254 stream_shm_path[PATH_MAX - 1] = '\0';
255 ret = snprintf(cpu_nr, UINT_MAX_STR_LEN, "%u", cpu);
256 if (ret != 1) {
257 ret = -1;
258 goto end;
259 }
260 strncat(stream_shm_path, cpu_nr,
261 PATH_MAX - strlen(stream_shm_path) - 1);
262 ret = 0;
263end:
264 return ret;
265}
266
d88aee68
DG
267/*
268 * Create streams for the given channel using liblttng-ust-ctl.
269 *
270 * Return 0 on success else a negative value.
271 */
ffe60014
DG
272static int create_ust_streams(struct lttng_consumer_channel *channel,
273 struct lttng_consumer_local_data *ctx)
274{
275 int ret, cpu = 0;
276 struct ustctl_consumer_stream *ustream;
277 struct lttng_consumer_stream *stream;
278
279 assert(channel);
280 assert(ctx);
281
282 /*
283 * While a stream is available from ustctl. When NULL is returned, we've
284 * reached the end of the possible stream for the channel.
285 */
286 while ((ustream = ustctl_create_stream(channel->uchan, cpu))) {
287 int wait_fd;
04ef1097 288 int ust_metadata_pipe[2];
ffe60014 289
9ce5646a
MD
290 health_code_update();
291
04ef1097
MD
292 if (channel->type == CONSUMER_CHANNEL_TYPE_METADATA && channel->monitor) {
293 ret = utils_create_pipe_cloexec_nonblock(ust_metadata_pipe);
294 if (ret < 0) {
295 ERR("Create ust metadata poll pipe");
296 goto error;
297 }
298 wait_fd = ust_metadata_pipe[0];
299 } else {
300 wait_fd = ustctl_stream_get_wait_fd(ustream);
301 }
ffe60014
DG
302
303 /* Allocate consumer stream object. */
304 stream = allocate_stream(cpu, wait_fd, channel, ctx, &ret);
305 if (!stream) {
306 goto error_alloc;
307 }
308 stream->ustream = ustream;
309 /*
310 * Store it so we can save multiple function calls afterwards since
311 * this value is used heavily in the stream threads. This is UST
312 * specific so this is why it's done after allocation.
313 */
314 stream->wait_fd = wait_fd;
315
b31398bb
DG
316 /*
317 * Increment channel refcount since the channel reference has now been
318 * assigned in the allocation process above.
319 */
10a50311
JD
320 if (stream->chan->monitor) {
321 uatomic_inc(&stream->chan->refcount);
322 }
b31398bb 323
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 }
ffe60014
DG
362 }
363
364 return 0;
365
366error:
367error_alloc:
368 return ret;
369}
370
4628484a
MD
371/*
372 * create_posix_shm is never called concurrently within a process.
373 */
374static
375int create_posix_shm(void)
376{
377 char tmp_name[NAME_MAX];
378 int shmfd, ret;
379
380 ret = snprintf(tmp_name, NAME_MAX, "/ust-shm-consumer-%d", getpid());
381 if (ret < 0) {
382 PERROR("snprintf");
383 return -1;
384 }
385 /*
386 * Allocate shm, and immediately unlink its shm oject, keeping
387 * only the file descriptor as a reference to the object.
388 * We specifically do _not_ use the / at the beginning of the
389 * pathname so that some OS implementations can keep it local to
390 * the process (POSIX leaves this implementation-defined).
391 */
392 shmfd = shm_open(tmp_name, O_CREAT | O_EXCL | O_RDWR, 0700);
393 if (shmfd < 0) {
394 PERROR("shm_open");
395 goto error_shm_open;
396 }
397 ret = shm_unlink(tmp_name);
398 if (ret < 0 && errno != ENOENT) {
399 PERROR("shm_unlink");
400 goto error_shm_release;
401 }
402 return shmfd;
403
404error_shm_release:
405 ret = close(shmfd);
406 if (ret) {
407 PERROR("close");
408 }
409error_shm_open:
410 return -1;
411}
412
413static int open_ust_stream_fd(struct lttng_consumer_channel *channel,
414 struct ustctl_consumer_channel_attr *attr,
415 int cpu)
416{
417 char shm_path[PATH_MAX];
418 int ret;
419
420 if (!channel->shm_path[0]) {
421 return create_posix_shm();
422 }
423 ret = get_stream_shm_path(shm_path, channel->shm_path, cpu);
424 if (ret) {
425 goto error_shm_path;
426 }
427 return run_as_open(shm_path,
428 O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR,
429 channel->uid, channel->gid);
430
431error_shm_path:
432 return -1;
433}
434
ffe60014
DG
435/*
436 * Create an UST channel with the given attributes and send it to the session
437 * daemon using the ust ctl API.
438 *
439 * Return 0 on success or else a negative value.
440 */
4628484a
MD
441static int create_ust_channel(struct lttng_consumer_channel *channel,
442 struct ustctl_consumer_channel_attr *attr,
443 struct ustctl_consumer_channel **ust_chanp)
ffe60014 444{
4628484a
MD
445 int ret, nr_stream_fds, i, j;
446 int *stream_fds;
447 struct ustctl_consumer_channel *ust_channel;
ffe60014 448
4628484a 449 assert(channel);
ffe60014 450 assert(attr);
4628484a 451 assert(ust_chanp);
ffe60014
DG
452
453 DBG3("Creating channel to ustctl with attr: [overwrite: %d, "
454 "subbuf_size: %" PRIu64 ", num_subbuf: %" PRIu64 ", "
455 "switch_timer_interval: %u, read_timer_interval: %u, "
456 "output: %d, type: %d", attr->overwrite, attr->subbuf_size,
457 attr->num_subbuf, attr->switch_timer_interval,
458 attr->read_timer_interval, attr->output, attr->type);
459
4628484a
MD
460 if (channel->type == CONSUMER_CHANNEL_TYPE_METADATA)
461 nr_stream_fds = 1;
462 else
463 nr_stream_fds = ustctl_get_nr_stream_per_channel();
464 stream_fds = zmalloc(nr_stream_fds * sizeof(*stream_fds));
465 if (!stream_fds) {
466 ret = -1;
467 goto error_alloc;
468 }
469 for (i = 0; i < nr_stream_fds; i++) {
470 stream_fds[i] = open_ust_stream_fd(channel, attr, i);
471 if (stream_fds[i] < 0) {
472 ret = -1;
473 goto error_open;
474 }
475 }
476 ust_channel = ustctl_create_channel(attr, stream_fds, nr_stream_fds);
477 if (!ust_channel) {
ffe60014
DG
478 ret = -1;
479 goto error_create;
480 }
4628484a
MD
481 channel->nr_stream_fds = nr_stream_fds;
482 channel->stream_fds = stream_fds;
483 *ust_chanp = ust_channel;
ffe60014
DG
484
485 return 0;
486
487error_create:
4628484a
MD
488error_open:
489 for (j = i - 1; j >= 0; j--) {
490 int closeret;
491
492 closeret = close(stream_fds[j]);
493 if (closeret) {
494 PERROR("close");
495 }
496 if (channel->shm_path[0]) {
497 char shm_path[PATH_MAX];
498
499 closeret = get_stream_shm_path(shm_path,
500 channel->shm_path, j);
501 if (closeret) {
502 ERR("Cannot get stream shm path");
503 }
504 closeret = run_as_unlink(shm_path,
505 channel->uid, channel->gid);
506 if (closeret) {
4628484a
MD
507 PERROR("unlink %s", shm_path);
508 }
509 }
510 }
511 /* Try to rmdir all directories under shm_path root. */
512 if (channel->root_shm_path[0]) {
513 (void) run_as_recursive_rmdir(channel->root_shm_path,
514 channel->uid, channel->gid);
515 }
516 free(stream_fds);
517error_alloc:
ffe60014
DG
518 return ret;
519}
520
d88aee68
DG
521/*
522 * Send a single given stream to the session daemon using the sock.
523 *
524 * Return 0 on success else a negative value.
525 */
ffe60014
DG
526static int send_sessiond_stream(int sock, struct lttng_consumer_stream *stream)
527{
528 int ret;
529
530 assert(stream);
531 assert(sock >= 0);
532
3eb914c0 533 DBG("UST consumer sending stream %" PRIu64 " to sessiond", stream->key);
ffe60014
DG
534
535 /* Send stream to session daemon. */
536 ret = ustctl_send_stream_to_sessiond(sock, stream->ustream);
537 if (ret < 0) {
538 goto error;
539 }
540
ffe60014
DG
541error:
542 return ret;
543}
544
545/*
546 * Send channel to sessiond.
547 *
d88aee68 548 * Return 0 on success or else a negative value.
ffe60014
DG
549 */
550static int send_sessiond_channel(int sock,
551 struct lttng_consumer_channel *channel,
552 struct lttng_consumer_local_data *ctx, int *relayd_error)
553{
0c759fc9 554 int ret, ret_code = LTTCOMM_CONSUMERD_SUCCESS;
ffe60014 555 struct lttng_consumer_stream *stream;
a4baae1b 556 uint64_t net_seq_idx = -1ULL;
ffe60014
DG
557
558 assert(channel);
559 assert(ctx);
560 assert(sock >= 0);
561
562 DBG("UST consumer sending channel %s to sessiond", channel->name);
563
62285ea4
DG
564 if (channel->relayd_id != (uint64_t) -1ULL) {
565 cds_list_for_each_entry(stream, &channel->streams.head, send_node) {
9ce5646a
MD
566
567 health_code_update();
568
62285ea4
DG
569 /* Try to send the stream to the relayd if one is available. */
570 ret = consumer_send_relayd_stream(stream, stream->chan->pathname);
571 if (ret < 0) {
572 /*
573 * Flag that the relayd was the problem here probably due to a
574 * communicaton error on the socket.
575 */
576 if (relayd_error) {
577 *relayd_error = 1;
578 }
725d28b2 579 ret_code = LTTCOMM_CONSUMERD_RELAYD_FAIL;
ffe60014 580 }
a4baae1b
JD
581 if (net_seq_idx == -1ULL) {
582 net_seq_idx = stream->net_seq_idx;
583 }
584 }
f2a444f1 585 }
ffe60014 586
f2a444f1
DG
587 /* Inform sessiond that we are about to send channel and streams. */
588 ret = consumer_send_status_msg(sock, ret_code);
0c759fc9 589 if (ret < 0 || ret_code != LTTCOMM_CONSUMERD_SUCCESS) {
f2a444f1
DG
590 /*
591 * Either the session daemon is not responding or the relayd died so we
592 * stop now.
593 */
594 goto error;
595 }
596
597 /* Send channel to sessiond. */
598 ret = ustctl_send_channel_to_sessiond(sock, channel->uchan);
599 if (ret < 0) {
600 goto error;
601 }
602
603 ret = ustctl_channel_close_wakeup_fd(channel->uchan);
604 if (ret < 0) {
605 goto error;
606 }
607
608 /* The channel was sent successfully to the sessiond at this point. */
609 cds_list_for_each_entry(stream, &channel->streams.head, send_node) {
9ce5646a
MD
610
611 health_code_update();
612
ffe60014
DG
613 /* Send stream to session daemon. */
614 ret = send_sessiond_stream(sock, stream);
615 if (ret < 0) {
616 goto error;
617 }
618 }
619
620 /* Tell sessiond there is no more stream. */
621 ret = ustctl_send_stream_to_sessiond(sock, NULL);
622 if (ret < 0) {
623 goto error;
624 }
625
626 DBG("UST consumer NULL stream sent to sessiond");
627
628 return 0;
629
630error:
0c759fc9 631 if (ret_code != LTTCOMM_CONSUMERD_SUCCESS) {
f2a444f1
DG
632 ret = -1;
633 }
ffe60014
DG
634 return ret;
635}
636
637/*
638 * Creates a channel and streams and add the channel it to the channel internal
639 * state. The created stream must ONLY be sent once the GET_CHANNEL command is
640 * received.
641 *
642 * Return 0 on success or else, a negative value is returned and the channel
643 * MUST be destroyed by consumer_del_channel().
644 */
645static int ask_channel(struct lttng_consumer_local_data *ctx, int sock,
646 struct lttng_consumer_channel *channel,
647 struct ustctl_consumer_channel_attr *attr)
3bd1e081
MD
648{
649 int ret;
650
ffe60014
DG
651 assert(ctx);
652 assert(channel);
653 assert(attr);
654
655 /*
656 * This value is still used by the kernel consumer since for the kernel,
657 * the stream ownership is not IN the consumer so we need to have the
658 * number of left stream that needs to be initialized so we can know when
659 * to delete the channel (see consumer.c).
660 *
661 * As for the user space tracer now, the consumer creates and sends the
662 * stream to the session daemon which only sends them to the application
663 * once every stream of a channel is received making this value useless
664 * because we they will be added to the poll thread before the application
665 * receives them. This ensures that a stream can not hang up during
666 * initilization of a channel.
667 */
668 channel->nb_init_stream_left = 0;
669
670 /* The reply msg status is handled in the following call. */
4628484a 671 ret = create_ust_channel(channel, attr, &channel->uchan);
ffe60014 672 if (ret < 0) {
10a50311 673 goto end;
3bd1e081
MD
674 }
675
d8ef542d
MD
676 channel->wait_fd = ustctl_channel_get_wait_fd(channel->uchan);
677
10a50311
JD
678 /*
679 * For the snapshots (no monitor), we create the metadata streams
680 * on demand, not during the channel creation.
681 */
682 if (channel->type == CONSUMER_CHANNEL_TYPE_METADATA && !channel->monitor) {
683 ret = 0;
684 goto end;
685 }
686
ffe60014
DG
687 /* Open all streams for this channel. */
688 ret = create_ust_streams(channel, ctx);
689 if (ret < 0) {
10a50311 690 goto end;
ffe60014
DG
691 }
692
10a50311 693end:
3bd1e081
MD
694 return ret;
695}
696
d88aee68
DG
697/*
698 * Send all stream of a channel to the right thread handling it.
699 *
700 * On error, return a negative value else 0 on success.
701 */
702static int send_streams_to_thread(struct lttng_consumer_channel *channel,
703 struct lttng_consumer_local_data *ctx)
704{
705 int ret = 0;
706 struct lttng_consumer_stream *stream, *stmp;
707
708 assert(channel);
709 assert(ctx);
710
711 /* Send streams to the corresponding thread. */
712 cds_list_for_each_entry_safe(stream, stmp, &channel->streams.head,
713 send_node) {
9ce5646a
MD
714
715 health_code_update();
716
d88aee68
DG
717 /* Sending the stream to the thread. */
718 ret = send_stream_to_thread(stream, ctx);
719 if (ret < 0) {
720 /*
721 * If we are unable to send the stream to the thread, there is
722 * a big problem so just stop everything.
723 */
5ab66908
MD
724 /* Remove node from the channel stream list. */
725 cds_list_del(&stream->send_node);
d88aee68
DG
726 goto error;
727 }
728
729 /* Remove node from the channel stream list. */
730 cds_list_del(&stream->send_node);
4891ece8 731
d88aee68
DG
732 }
733
734error:
735 return ret;
736}
737
7972aab2
DG
738/*
739 * Flush channel's streams using the given key to retrieve the channel.
740 *
741 * Return 0 on success else an LTTng error code.
742 */
743static int flush_channel(uint64_t chan_key)
744{
745 int ret = 0;
746 struct lttng_consumer_channel *channel;
747 struct lttng_consumer_stream *stream;
748 struct lttng_ht *ht;
749 struct lttng_ht_iter iter;
750
8fd623e0 751 DBG("UST consumer flush channel key %" PRIu64, chan_key);
7972aab2 752
a500c257 753 rcu_read_lock();
7972aab2
DG
754 channel = consumer_find_channel(chan_key);
755 if (!channel) {
8fd623e0 756 ERR("UST consumer flush channel %" PRIu64 " not found", chan_key);
7972aab2
DG
757 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
758 goto error;
759 }
760
761 ht = consumer_data.stream_per_chan_id_ht;
762
763 /* For each stream of the channel id, flush it. */
7972aab2
DG
764 cds_lfht_for_each_entry_duplicate(ht->ht,
765 ht->hash_fct(&channel->key, lttng_ht_seed), ht->match_fct,
766 &channel->key, &iter.iter, stream, node_channel_id.node) {
9ce5646a
MD
767
768 health_code_update();
769
b8086166 770 ustctl_flush_buffer(stream->ustream, 1);
7972aab2 771 }
7972aab2 772error:
a500c257 773 rcu_read_unlock();
7972aab2
DG
774 return ret;
775}
776
d88aee68
DG
777/*
778 * Close metadata stream wakeup_fd using the given key to retrieve the channel.
a500c257 779 * RCU read side lock MUST be acquired before calling this function.
d88aee68
DG
780 *
781 * Return 0 on success else an LTTng error code.
782 */
783static int close_metadata(uint64_t chan_key)
784{
ea88ca2a 785 int ret = 0;
d88aee68
DG
786 struct lttng_consumer_channel *channel;
787
8fd623e0 788 DBG("UST consumer close metadata key %" PRIu64, chan_key);
d88aee68
DG
789
790 channel = consumer_find_channel(chan_key);
791 if (!channel) {
84cc9aa0
DG
792 /*
793 * This is possible if the metadata thread has issue a delete because
794 * the endpoint point of the stream hung up. There is no way the
795 * session daemon can know about it thus use a DBG instead of an actual
796 * error.
797 */
798 DBG("UST consumer close metadata %" PRIu64 " not found", chan_key);
d88aee68
DG
799 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
800 goto error;
801 }
802
ea88ca2a 803 pthread_mutex_lock(&consumer_data.lock);
a9838785 804 pthread_mutex_lock(&channel->lock);
73811ecc
DG
805
806 if (cds_lfht_is_node_deleted(&channel->node.node)) {
807 goto error_unlock;
808 }
809
6d574024 810 lttng_ustconsumer_close_metadata(channel);
d88aee68 811
ea88ca2a 812error_unlock:
a9838785 813 pthread_mutex_unlock(&channel->lock);
ea88ca2a 814 pthread_mutex_unlock(&consumer_data.lock);
d88aee68
DG
815error:
816 return ret;
817}
818
819/*
820 * RCU read side lock MUST be acquired before calling this function.
821 *
822 * Return 0 on success else an LTTng error code.
823 */
824static int setup_metadata(struct lttng_consumer_local_data *ctx, uint64_t key)
825{
826 int ret;
827 struct lttng_consumer_channel *metadata;
828
8fd623e0 829 DBG("UST consumer setup metadata key %" PRIu64, key);
d88aee68
DG
830
831 metadata = consumer_find_channel(key);
832 if (!metadata) {
833 ERR("UST consumer push metadata %" PRIu64 " not found", key);
834 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
10a50311
JD
835 goto end;
836 }
837
838 /*
839 * In no monitor mode, the metadata channel has no stream(s) so skip the
840 * ownership transfer to the metadata thread.
841 */
842 if (!metadata->monitor) {
843 DBG("Metadata channel in no monitor");
844 ret = 0;
845 goto end;
d88aee68
DG
846 }
847
848 /*
849 * Send metadata stream to relayd if one available. Availability is
850 * known if the stream is still in the list of the channel.
851 */
852 if (cds_list_empty(&metadata->streams.head)) {
853 ERR("Metadata channel key %" PRIu64 ", no stream available.", key);
854 ret = LTTCOMM_CONSUMERD_ERROR_METADATA;
f5a0c9cf 855 goto error_no_stream;
d88aee68
DG
856 }
857
858 /* Send metadata stream to relayd if needed. */
62285ea4
DG
859 if (metadata->metadata_stream->net_seq_idx != (uint64_t) -1ULL) {
860 ret = consumer_send_relayd_stream(metadata->metadata_stream,
861 metadata->pathname);
862 if (ret < 0) {
863 ret = LTTCOMM_CONSUMERD_ERROR_METADATA;
864 goto error;
865 }
601262d6
JD
866 ret = consumer_send_relayd_streams_sent(
867 metadata->metadata_stream->net_seq_idx);
868 if (ret < 0) {
869 ret = LTTCOMM_CONSUMERD_RELAYD_FAIL;
870 goto error;
871 }
d88aee68
DG
872 }
873
874 ret = send_streams_to_thread(metadata, ctx);
875 if (ret < 0) {
876 /*
877 * If we are unable to send the stream to the thread, there is
878 * a big problem so just stop everything.
879 */
880 ret = LTTCOMM_CONSUMERD_FATAL;
881 goto error;
882 }
883 /* List MUST be empty after or else it could be reused. */
884 assert(cds_list_empty(&metadata->streams.head));
885
10a50311
JD
886 ret = 0;
887 goto end;
d88aee68
DG
888
889error:
f2a444f1
DG
890 /*
891 * Delete metadata channel on error. At this point, the metadata stream can
892 * NOT be monitored by the metadata thread thus having the guarantee that
893 * the stream is still in the local stream list of the channel. This call
894 * will make sure to clean that list.
895 */
f5a0c9cf 896 consumer_stream_destroy(metadata->metadata_stream, NULL);
212d67a2
DG
897 cds_list_del(&metadata->metadata_stream->send_node);
898 metadata->metadata_stream = NULL;
f5a0c9cf 899error_no_stream:
10a50311
JD
900end:
901 return ret;
902}
903
904/*
905 * Snapshot the whole metadata.
906 *
907 * Returns 0 on success, < 0 on error
908 */
909static int snapshot_metadata(uint64_t key, char *path, uint64_t relayd_id,
910 struct lttng_consumer_local_data *ctx)
911{
912 int ret = 0;
10a50311
JD
913 struct lttng_consumer_channel *metadata_channel;
914 struct lttng_consumer_stream *metadata_stream;
915
916 assert(path);
917 assert(ctx);
918
919 DBG("UST consumer snapshot metadata with key %" PRIu64 " at path %s",
920 key, path);
921
922 rcu_read_lock();
923
924 metadata_channel = consumer_find_channel(key);
925 if (!metadata_channel) {
6a00837f
MD
926 ERR("UST snapshot metadata channel not found for key %" PRIu64,
927 key);
10a50311
JD
928 ret = -1;
929 goto error;
930 }
931 assert(!metadata_channel->monitor);
932
9ce5646a
MD
933 health_code_update();
934
10a50311
JD
935 /*
936 * Ask the sessiond if we have new metadata waiting and update the
937 * consumer metadata cache.
938 */
94d49140 939 ret = lttng_ustconsumer_request_metadata(ctx, metadata_channel, 0, 1);
10a50311
JD
940 if (ret < 0) {
941 goto error;
942 }
943
9ce5646a
MD
944 health_code_update();
945
10a50311
JD
946 /*
947 * The metadata stream is NOT created in no monitor mode when the channel
948 * is created on a sessiond ask channel command.
949 */
950 ret = create_ust_streams(metadata_channel, ctx);
951 if (ret < 0) {
952 goto error;
953 }
954
955 metadata_stream = metadata_channel->metadata_stream;
956 assert(metadata_stream);
957
958 if (relayd_id != (uint64_t) -1ULL) {
959 metadata_stream->net_seq_idx = relayd_id;
960 ret = consumer_send_relayd_stream(metadata_stream, path);
961 if (ret < 0) {
962 goto error_stream;
963 }
964 } else {
965 ret = utils_create_stream_file(path, metadata_stream->name,
966 metadata_stream->chan->tracefile_size,
967 metadata_stream->tracefile_count_current,
309167d2 968 metadata_stream->uid, metadata_stream->gid, NULL);
10a50311
JD
969 if (ret < 0) {
970 goto error_stream;
971 }
972 metadata_stream->out_fd = ret;
973 metadata_stream->tracefile_size_current = 0;
974 }
975
04ef1097 976 do {
9ce5646a
MD
977 health_code_update();
978
10a50311
JD
979 ret = lttng_consumer_read_subbuffer(metadata_stream, ctx);
980 if (ret < 0) {
94d49140 981 goto error_stream;
10a50311 982 }
04ef1097 983 } while (ret > 0);
10a50311 984
10a50311
JD
985error_stream:
986 /*
987 * Clean up the stream completly because the next snapshot will use a new
988 * metadata stream.
989 */
10a50311 990 consumer_stream_destroy(metadata_stream, NULL);
212d67a2 991 cds_list_del(&metadata_stream->send_node);
10a50311
JD
992 metadata_channel->metadata_stream = NULL;
993
994error:
995 rcu_read_unlock();
996 return ret;
997}
998
999/*
1000 * Take a snapshot of all the stream of a channel.
1001 *
1002 * Returns 0 on success, < 0 on error
1003 */
1004static int snapshot_channel(uint64_t key, char *path, uint64_t relayd_id,
d07ceecd 1005 uint64_t nb_packets_per_stream, struct lttng_consumer_local_data *ctx)
10a50311
JD
1006{
1007 int ret;
1008 unsigned use_relayd = 0;
1009 unsigned long consumed_pos, produced_pos;
1010 struct lttng_consumer_channel *channel;
1011 struct lttng_consumer_stream *stream;
1012
1013 assert(path);
1014 assert(ctx);
1015
1016 rcu_read_lock();
1017
1018 if (relayd_id != (uint64_t) -1ULL) {
1019 use_relayd = 1;
1020 }
1021
1022 channel = consumer_find_channel(key);
1023 if (!channel) {
6a00837f 1024 ERR("UST snapshot channel not found for key %" PRIu64, key);
10a50311
JD
1025 ret = -1;
1026 goto error;
1027 }
1028 assert(!channel->monitor);
6a00837f 1029 DBG("UST consumer snapshot channel %" PRIu64, key);
10a50311
JD
1030
1031 cds_list_for_each_entry(stream, &channel->streams.head, send_node) {
9ce5646a
MD
1032
1033 health_code_update();
1034
10a50311
JD
1035 /* Lock stream because we are about to change its state. */
1036 pthread_mutex_lock(&stream->lock);
1037 stream->net_seq_idx = relayd_id;
1038
1039 if (use_relayd) {
1040 ret = consumer_send_relayd_stream(stream, path);
1041 if (ret < 0) {
1042 goto error_unlock;
1043 }
1044 } else {
1045 ret = utils_create_stream_file(path, stream->name,
1046 stream->chan->tracefile_size,
1047 stream->tracefile_count_current,
309167d2 1048 stream->uid, stream->gid, NULL);
10a50311
JD
1049 if (ret < 0) {
1050 goto error_unlock;
1051 }
1052 stream->out_fd = ret;
1053 stream->tracefile_size_current = 0;
1054
1055 DBG("UST consumer snapshot stream %s/%s (%" PRIu64 ")", path,
1056 stream->name, stream->key);
1057 }
a4baae1b
JD
1058 if (relayd_id != -1ULL) {
1059 ret = consumer_send_relayd_streams_sent(relayd_id);
1060 if (ret < 0) {
1061 goto error_unlock;
1062 }
1063 }
10a50311
JD
1064
1065 ustctl_flush_buffer(stream->ustream, 1);
1066
1067 ret = lttng_ustconsumer_take_snapshot(stream);
1068 if (ret < 0) {
1069 ERR("Taking UST snapshot");
1070 goto error_unlock;
1071 }
1072
1073 ret = lttng_ustconsumer_get_produced_snapshot(stream, &produced_pos);
1074 if (ret < 0) {
1075 ERR("Produced UST snapshot position");
1076 goto error_unlock;
1077 }
1078
1079 ret = lttng_ustconsumer_get_consumed_snapshot(stream, &consumed_pos);
1080 if (ret < 0) {
1081 ERR("Consumerd UST snapshot position");
1082 goto error_unlock;
1083 }
1084
5c786ded
JD
1085 /*
1086 * The original value is sent back if max stream size is larger than
d07ceecd 1087 * the possible size of the snapshot. Also, we assume that the session
5c786ded
JD
1088 * daemon should never send a maximum stream size that is lower than
1089 * subbuffer size.
1090 */
d07ceecd
MD
1091 consumed_pos = consumer_get_consume_start_pos(consumed_pos,
1092 produced_pos, nb_packets_per_stream,
1093 stream->max_sb_size);
5c786ded 1094
10a50311
JD
1095 while (consumed_pos < produced_pos) {
1096 ssize_t read_len;
1097 unsigned long len, padded_len;
1098
9ce5646a
MD
1099 health_code_update();
1100
10a50311
JD
1101 DBG("UST consumer taking snapshot at pos %lu", consumed_pos);
1102
1103 ret = ustctl_get_subbuf(stream->ustream, &consumed_pos);
1104 if (ret < 0) {
1105 if (ret != -EAGAIN) {
1106 PERROR("ustctl_get_subbuf snapshot");
1107 goto error_close_stream;
1108 }
1109 DBG("UST consumer get subbuf failed. Skipping it.");
1110 consumed_pos += stream->max_sb_size;
1111 continue;
1112 }
1113
1114 ret = ustctl_get_subbuf_size(stream->ustream, &len);
1115 if (ret < 0) {
1116 ERR("Snapshot ustctl_get_subbuf_size");
1117 goto error_put_subbuf;
1118 }
1119
1120 ret = ustctl_get_padded_subbuf_size(stream->ustream, &padded_len);
1121 if (ret < 0) {
1122 ERR("Snapshot ustctl_get_padded_subbuf_size");
1123 goto error_put_subbuf;
1124 }
1125
1126 read_len = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, len,
309167d2 1127 padded_len - len, NULL);
10a50311
JD
1128 if (use_relayd) {
1129 if (read_len != len) {
56591bac 1130 ret = -EPERM;
10a50311
JD
1131 goto error_put_subbuf;
1132 }
1133 } else {
1134 if (read_len != padded_len) {
56591bac 1135 ret = -EPERM;
10a50311
JD
1136 goto error_put_subbuf;
1137 }
1138 }
1139
1140 ret = ustctl_put_subbuf(stream->ustream);
1141 if (ret < 0) {
1142 ERR("Snapshot ustctl_put_subbuf");
1143 goto error_close_stream;
1144 }
1145 consumed_pos += stream->max_sb_size;
1146 }
1147
1148 /* Simply close the stream so we can use it on the next snapshot. */
1149 consumer_stream_close(stream);
1150 pthread_mutex_unlock(&stream->lock);
1151 }
1152
1153 rcu_read_unlock();
1154 return 0;
1155
1156error_put_subbuf:
1157 if (ustctl_put_subbuf(stream->ustream) < 0) {
1158 ERR("Snapshot ustctl_put_subbuf");
1159 }
1160error_close_stream:
1161 consumer_stream_close(stream);
1162error_unlock:
1163 pthread_mutex_unlock(&stream->lock);
1164error:
1165 rcu_read_unlock();
d88aee68
DG
1166 return ret;
1167}
1168
331744e3
JD
1169/*
1170 * Receive the metadata updates from the sessiond.
1171 */
1172int lttng_ustconsumer_recv_metadata(int sock, uint64_t key, uint64_t offset,
5e41ebe1 1173 uint64_t len, struct lttng_consumer_channel *channel,
94d49140 1174 int timer, int wait)
331744e3 1175{
0c759fc9 1176 int ret, ret_code = LTTCOMM_CONSUMERD_SUCCESS;
331744e3
JD
1177 char *metadata_str;
1178
8fd623e0 1179 DBG("UST consumer push metadata key %" PRIu64 " of len %" PRIu64, key, len);
331744e3
JD
1180
1181 metadata_str = zmalloc(len * sizeof(char));
1182 if (!metadata_str) {
1183 PERROR("zmalloc metadata string");
1184 ret_code = LTTCOMM_CONSUMERD_ENOMEM;
1185 goto end;
1186 }
1187
9ce5646a
MD
1188 health_code_update();
1189
331744e3
JD
1190 /* Receive metadata string. */
1191 ret = lttcomm_recv_unix_sock(sock, metadata_str, len);
1192 if (ret < 0) {
1193 /* Session daemon is dead so return gracefully. */
1194 ret_code = ret;
1195 goto end_free;
1196 }
1197
9ce5646a
MD
1198 health_code_update();
1199
331744e3
JD
1200 pthread_mutex_lock(&channel->metadata_cache->lock);
1201 ret = consumer_metadata_cache_write(channel, offset, len, metadata_str);
1202 if (ret < 0) {
1203 /* Unable to handle metadata. Notify session daemon. */
1204 ret_code = LTTCOMM_CONSUMERD_ERROR_METADATA;
a32bd775
DG
1205 /*
1206 * Skip metadata flush on write error since the offset and len might
1207 * not have been updated which could create an infinite loop below when
1208 * waiting for the metadata cache to be flushed.
1209 */
1210 pthread_mutex_unlock(&channel->metadata_cache->lock);
a32bd775 1211 goto end_free;
331744e3
JD
1212 }
1213 pthread_mutex_unlock(&channel->metadata_cache->lock);
1214
94d49140
JD
1215 if (!wait) {
1216 goto end_free;
1217 }
5e41ebe1 1218 while (consumer_metadata_cache_flushed(channel, offset + len, timer)) {
331744e3 1219 DBG("Waiting for metadata to be flushed");
9ce5646a
MD
1220
1221 health_code_update();
1222
331744e3
JD
1223 usleep(DEFAULT_METADATA_AVAILABILITY_WAIT_TIME);
1224 }
1225
1226end_free:
1227 free(metadata_str);
1228end:
1229 return ret_code;
1230}
1231
4cbc1a04
DG
1232/*
1233 * Receive command from session daemon and process it.
1234 *
1235 * Return 1 on success else a negative value or 0.
1236 */
3bd1e081
MD
1237int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
1238 int sock, struct pollfd *consumer_sockpoll)
1239{
1240 ssize_t ret;
0c759fc9 1241 enum lttcomm_return_code ret_code = LTTCOMM_CONSUMERD_SUCCESS;
3bd1e081 1242 struct lttcomm_consumer_msg msg;
ffe60014 1243 struct lttng_consumer_channel *channel = NULL;
3bd1e081 1244
9ce5646a
MD
1245 health_code_update();
1246
3bd1e081
MD
1247 ret = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg));
1248 if (ret != sizeof(msg)) {
173af62f
DG
1249 DBG("Consumer received unexpected message size %zd (expects %zu)",
1250 ret, sizeof(msg));
3be74084
DG
1251 /*
1252 * The ret value might 0 meaning an orderly shutdown but this is ok
1253 * since the caller handles this.
1254 */
489f70e9 1255 if (ret > 0) {
c6857fcf 1256 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_CMD);
489f70e9
MD
1257 ret = -1;
1258 }
3bd1e081
MD
1259 return ret;
1260 }
9ce5646a
MD
1261
1262 health_code_update();
1263
84382d49
MD
1264 /* deprecated */
1265 assert(msg.cmd_type != LTTNG_CONSUMER_STOP);
3bd1e081 1266
9ce5646a
MD
1267 health_code_update();
1268
3f8e211f 1269 /* relayd needs RCU read-side lock */
b0b335c8
MD
1270 rcu_read_lock();
1271
3bd1e081 1272 switch (msg.cmd_type) {
00e2e675
DG
1273 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET:
1274 {
f50f23d9 1275 /* Session daemon status message are handled in the following call. */
7735ef9e
DG
1276 ret = consumer_add_relayd_socket(msg.u.relayd_sock.net_index,
1277 msg.u.relayd_sock.type, ctx, sock, consumer_sockpoll,
d3e2ba59
JD
1278 &msg.u.relayd_sock.sock, msg.u.relayd_sock.session_id,
1279 msg.u.relayd_sock.relayd_session_id);
00e2e675
DG
1280 goto end_nosignal;
1281 }
173af62f
DG
1282 case LTTNG_CONSUMER_DESTROY_RELAYD:
1283 {
a6ba4fe1 1284 uint64_t index = msg.u.destroy_relayd.net_seq_idx;
173af62f
DG
1285 struct consumer_relayd_sock_pair *relayd;
1286
a6ba4fe1 1287 DBG("UST consumer destroying relayd %" PRIu64, index);
173af62f
DG
1288
1289 /* Get relayd reference if exists. */
a6ba4fe1 1290 relayd = consumer_find_relayd(index);
173af62f 1291 if (relayd == NULL) {
3448e266 1292 DBG("Unable to find relayd %" PRIu64, index);
e462382a 1293 ret_code = LTTCOMM_CONSUMERD_RELAYD_FAIL;
173af62f
DG
1294 }
1295
a6ba4fe1
DG
1296 /*
1297 * Each relayd socket pair has a refcount of stream attached to it
1298 * which tells if the relayd is still active or not depending on the
1299 * refcount value.
1300 *
1301 * This will set the destroy flag of the relayd object and destroy it
1302 * if the refcount reaches zero when called.
1303 *
1304 * The destroy can happen either here or when a stream fd hangs up.
1305 */
f50f23d9
DG
1306 if (relayd) {
1307 consumer_flag_relayd_for_destroy(relayd);
1308 }
1309
d88aee68 1310 goto end_msg_sessiond;
173af62f 1311 }
3bd1e081
MD
1312 case LTTNG_CONSUMER_UPDATE_STREAM:
1313 {
3f8e211f 1314 rcu_read_unlock();
7ad0a0cb 1315 return -ENOSYS;
3bd1e081 1316 }
6d805429 1317 case LTTNG_CONSUMER_DATA_PENDING:
53632229 1318 {
3be74084 1319 int ret, is_data_pending;
6d805429 1320 uint64_t id = msg.u.data_pending.session_id;
ca22feea 1321
6d805429 1322 DBG("UST consumer data pending command for id %" PRIu64, id);
ca22feea 1323
3be74084 1324 is_data_pending = consumer_data_pending(id);
ca22feea
DG
1325
1326 /* Send back returned value to session daemon */
3be74084
DG
1327 ret = lttcomm_send_unix_sock(sock, &is_data_pending,
1328 sizeof(is_data_pending));
ca22feea 1329 if (ret < 0) {
3be74084 1330 DBG("Error when sending the data pending ret code: %d", ret);
489f70e9 1331 goto error_fatal;
ca22feea 1332 }
f50f23d9
DG
1333
1334 /*
1335 * No need to send back a status message since the data pending
1336 * returned value is the response.
1337 */
ca22feea 1338 break;
53632229 1339 }
ffe60014
DG
1340 case LTTNG_CONSUMER_ASK_CHANNEL_CREATION:
1341 {
1342 int ret;
1343 struct ustctl_consumer_channel_attr attr;
1344
1345 /* Create a plain object and reserve a channel key. */
1346 channel = allocate_channel(msg.u.ask_channel.session_id,
1347 msg.u.ask_channel.pathname, msg.u.ask_channel.name,
1348 msg.u.ask_channel.uid, msg.u.ask_channel.gid,
1349 msg.u.ask_channel.relayd_id, msg.u.ask_channel.key,
1624d5b7
JD
1350 (enum lttng_event_output) msg.u.ask_channel.output,
1351 msg.u.ask_channel.tracefile_size,
2bba9e53 1352 msg.u.ask_channel.tracefile_count,
1950109e 1353 msg.u.ask_channel.session_id_per_pid,
ecc48a90 1354 msg.u.ask_channel.monitor,
d7ba1388 1355 msg.u.ask_channel.live_timer_interval,
3d071855 1356 msg.u.ask_channel.root_shm_path,
d7ba1388 1357 msg.u.ask_channel.shm_path);
ffe60014
DG
1358 if (!channel) {
1359 goto end_channel_error;
1360 }
1361
567eb353
DG
1362 /*
1363 * Assign UST application UID to the channel. This value is ignored for
1364 * per PID buffers. This is specific to UST thus setting this after the
1365 * allocation.
1366 */
1367 channel->ust_app_uid = msg.u.ask_channel.ust_app_uid;
1368
ffe60014
DG
1369 /* Build channel attributes from received message. */
1370 attr.subbuf_size = msg.u.ask_channel.subbuf_size;
1371 attr.num_subbuf = msg.u.ask_channel.num_subbuf;
1372 attr.overwrite = msg.u.ask_channel.overwrite;
1373 attr.switch_timer_interval = msg.u.ask_channel.switch_timer_interval;
1374 attr.read_timer_interval = msg.u.ask_channel.read_timer_interval;
7972aab2 1375 attr.chan_id = msg.u.ask_channel.chan_id;
ffe60014
DG
1376 memcpy(attr.uuid, msg.u.ask_channel.uuid, sizeof(attr.uuid));
1377
0c759fc9
DG
1378 /* Match channel buffer type to the UST abi. */
1379 switch (msg.u.ask_channel.output) {
1380 case LTTNG_EVENT_MMAP:
1381 default:
1382 attr.output = LTTNG_UST_MMAP;
1383 break;
1384 }
1385
ffe60014
DG
1386 /* Translate and save channel type. */
1387 switch (msg.u.ask_channel.type) {
1388 case LTTNG_UST_CHAN_PER_CPU:
1389 channel->type = CONSUMER_CHANNEL_TYPE_DATA;
1390 attr.type = LTTNG_UST_CHAN_PER_CPU;
8633d6e3
MD
1391 /*
1392 * Set refcount to 1 for owner. Below, we will
1393 * pass ownership to the
1394 * consumer_thread_channel_poll() thread.
1395 */
1396 channel->refcount = 1;
ffe60014
DG
1397 break;
1398 case LTTNG_UST_CHAN_METADATA:
1399 channel->type = CONSUMER_CHANNEL_TYPE_METADATA;
1400 attr.type = LTTNG_UST_CHAN_METADATA;
1401 break;
1402 default:
1403 assert(0);
1404 goto error_fatal;
1405 };
1406
9ce5646a
MD
1407 health_code_update();
1408
ffe60014
DG
1409 ret = ask_channel(ctx, sock, channel, &attr);
1410 if (ret < 0) {
1411 goto end_channel_error;
1412 }
1413
fc643247
MD
1414 if (msg.u.ask_channel.type == LTTNG_UST_CHAN_METADATA) {
1415 ret = consumer_metadata_cache_allocate(channel);
1416 if (ret < 0) {
1417 ERR("Allocating metadata cache");
1418 goto end_channel_error;
1419 }
1420 consumer_timer_switch_start(channel, attr.switch_timer_interval);
1421 attr.switch_timer_interval = 0;
94d49140
JD
1422 } else {
1423 consumer_timer_live_start(channel,
1424 msg.u.ask_channel.live_timer_interval);
fc643247
MD
1425 }
1426
9ce5646a
MD
1427 health_code_update();
1428
ffe60014
DG
1429 /*
1430 * Add the channel to the internal state AFTER all streams were created
1431 * and successfully sent to session daemon. This way, all streams must
1432 * be ready before this channel is visible to the threads.
fc643247
MD
1433 * If add_channel succeeds, ownership of the channel is
1434 * passed to consumer_thread_channel_poll().
ffe60014
DG
1435 */
1436 ret = add_channel(channel, ctx);
1437 if (ret < 0) {
ea88ca2a
MD
1438 if (msg.u.ask_channel.type == LTTNG_UST_CHAN_METADATA) {
1439 if (channel->switch_timer_enabled == 1) {
1440 consumer_timer_switch_stop(channel);
1441 }
1442 consumer_metadata_cache_destroy(channel);
1443 }
d3e2ba59
JD
1444 if (channel->live_timer_enabled == 1) {
1445 consumer_timer_live_stop(channel);
1446 }
ffe60014
DG
1447 goto end_channel_error;
1448 }
1449
9ce5646a
MD
1450 health_code_update();
1451
ffe60014
DG
1452 /*
1453 * Channel and streams are now created. Inform the session daemon that
1454 * everything went well and should wait to receive the channel and
1455 * streams with ustctl API.
1456 */
1457 ret = consumer_send_status_channel(sock, channel);
1458 if (ret < 0) {
1459 /*
489f70e9 1460 * There is probably a problem on the socket.
ffe60014 1461 */
489f70e9 1462 goto error_fatal;
ffe60014
DG
1463 }
1464
1465 break;
1466 }
1467 case LTTNG_CONSUMER_GET_CHANNEL:
1468 {
1469 int ret, relayd_err = 0;
d88aee68 1470 uint64_t key = msg.u.get_channel.key;
ffe60014 1471 struct lttng_consumer_channel *channel;
ffe60014
DG
1472
1473 channel = consumer_find_channel(key);
1474 if (!channel) {
8fd623e0 1475 ERR("UST consumer get channel key %" PRIu64 " not found", key);
e462382a 1476 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
ffe60014
DG
1477 goto end_msg_sessiond;
1478 }
1479
9ce5646a
MD
1480 health_code_update();
1481
ffe60014
DG
1482 /* Send everything to sessiond. */
1483 ret = send_sessiond_channel(sock, channel, ctx, &relayd_err);
1484 if (ret < 0) {
1485 if (relayd_err) {
1486 /*
1487 * We were unable to send to the relayd the stream so avoid
1488 * sending back a fatal error to the thread since this is OK
f2a444f1
DG
1489 * and the consumer can continue its work. The above call
1490 * has sent the error status message to the sessiond.
ffe60014 1491 */
f2a444f1 1492 goto end_nosignal;
ffe60014
DG
1493 }
1494 /*
1495 * The communicaton was broken hence there is a bad state between
1496 * the consumer and sessiond so stop everything.
1497 */
1498 goto error_fatal;
1499 }
1500
9ce5646a
MD
1501 health_code_update();
1502
10a50311
JD
1503 /*
1504 * In no monitor mode, the streams ownership is kept inside the channel
1505 * so don't send them to the data thread.
1506 */
1507 if (!channel->monitor) {
1508 goto end_msg_sessiond;
1509 }
1510
d88aee68
DG
1511 ret = send_streams_to_thread(channel, ctx);
1512 if (ret < 0) {
1513 /*
1514 * If we are unable to send the stream to the thread, there is
1515 * a big problem so just stop everything.
1516 */
1517 goto error_fatal;
ffe60014 1518 }
ffe60014
DG
1519 /* List MUST be empty after or else it could be reused. */
1520 assert(cds_list_empty(&channel->streams.head));
d88aee68
DG
1521 goto end_msg_sessiond;
1522 }
1523 case LTTNG_CONSUMER_DESTROY_CHANNEL:
1524 {
1525 uint64_t key = msg.u.destroy_channel.key;
d88aee68 1526
a0cbdd2e
MD
1527 /*
1528 * Only called if streams have not been sent to stream
1529 * manager thread. However, channel has been sent to
1530 * channel manager thread.
1531 */
1532 notify_thread_del_channel(ctx, key);
d88aee68 1533 goto end_msg_sessiond;
ffe60014 1534 }
d88aee68
DG
1535 case LTTNG_CONSUMER_CLOSE_METADATA:
1536 {
1537 int ret;
1538
1539 ret = close_metadata(msg.u.close_metadata.key);
1540 if (ret != 0) {
1541 ret_code = ret;
1542 }
1543
1544 goto end_msg_sessiond;
1545 }
7972aab2
DG
1546 case LTTNG_CONSUMER_FLUSH_CHANNEL:
1547 {
1548 int ret;
1549
1550 ret = flush_channel(msg.u.flush_channel.key);
1551 if (ret != 0) {
1552 ret_code = ret;
1553 }
1554
1555 goto end_msg_sessiond;
1556 }
d88aee68 1557 case LTTNG_CONSUMER_PUSH_METADATA:
ffe60014
DG
1558 {
1559 int ret;
d88aee68 1560 uint64_t len = msg.u.push_metadata.len;
d88aee68 1561 uint64_t key = msg.u.push_metadata.key;
331744e3 1562 uint64_t offset = msg.u.push_metadata.target_offset;
ffe60014
DG
1563 struct lttng_consumer_channel *channel;
1564
8fd623e0
DG
1565 DBG("UST consumer push metadata key %" PRIu64 " of len %" PRIu64, key,
1566 len);
ffe60014
DG
1567
1568 channel = consumer_find_channel(key);
1569 if (!channel) {
000baf6a
DG
1570 /*
1571 * This is possible if the metadata creation on the consumer side
1572 * is in flight vis-a-vis a concurrent push metadata from the
1573 * session daemon. Simply return that the channel failed and the
1574 * session daemon will handle that message correctly considering
1575 * that this race is acceptable thus the DBG() statement here.
1576 */
1577 DBG("UST consumer push metadata %" PRIu64 " not found", key);
1578 ret_code = LTTCOMM_CONSUMERD_CHANNEL_FAIL;
4a2eb0ca 1579 goto end_msg_sessiond;
d88aee68
DG
1580 }
1581
9ce5646a
MD
1582 health_code_update();
1583
d88aee68 1584 /* Tell session daemon we are ready to receive the metadata. */
0c759fc9 1585 ret = consumer_send_status_msg(sock, LTTCOMM_CONSUMERD_SUCCESS);
ffe60014
DG
1586 if (ret < 0) {
1587 /* Somehow, the session daemon is not responding anymore. */
d88aee68
DG
1588 goto error_fatal;
1589 }
1590
9ce5646a
MD
1591 health_code_update();
1592
d88aee68 1593 /* Wait for more data. */
9ce5646a
MD
1594 health_poll_entry();
1595 ret = lttng_consumer_poll_socket(consumer_sockpoll);
1596 health_poll_exit();
84382d49 1597 if (ret) {
489f70e9 1598 goto error_fatal;
d88aee68
DG
1599 }
1600
9ce5646a
MD
1601 health_code_update();
1602
331744e3 1603 ret = lttng_ustconsumer_recv_metadata(sock, key, offset,
94d49140 1604 len, channel, 0, 1);
d88aee68 1605 if (ret < 0) {
331744e3 1606 /* error receiving from sessiond */
489f70e9 1607 goto error_fatal;
331744e3
JD
1608 } else {
1609 ret_code = ret;
d88aee68
DG
1610 goto end_msg_sessiond;
1611 }
d88aee68
DG
1612 }
1613 case LTTNG_CONSUMER_SETUP_METADATA:
1614 {
1615 int ret;
1616
1617 ret = setup_metadata(ctx, msg.u.setup_metadata.key);
1618 if (ret) {
1619 ret_code = ret;
1620 }
1621 goto end_msg_sessiond;
ffe60014 1622 }
6dc3064a
DG
1623 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL:
1624 {
10a50311
JD
1625 if (msg.u.snapshot_channel.metadata) {
1626 ret = snapshot_metadata(msg.u.snapshot_channel.key,
1627 msg.u.snapshot_channel.pathname,
1628 msg.u.snapshot_channel.relayd_id,
1629 ctx);
1630 if (ret < 0) {
1631 ERR("Snapshot metadata failed");
e462382a 1632 ret_code = LTTCOMM_CONSUMERD_ERROR_METADATA;
10a50311
JD
1633 }
1634 } else {
1635 ret = snapshot_channel(msg.u.snapshot_channel.key,
1636 msg.u.snapshot_channel.pathname,
1637 msg.u.snapshot_channel.relayd_id,
d07ceecd 1638 msg.u.snapshot_channel.nb_packets_per_stream,
10a50311
JD
1639 ctx);
1640 if (ret < 0) {
1641 ERR("Snapshot channel failed");
e462382a 1642 ret_code = LTTCOMM_CONSUMERD_CHANNEL_FAIL;
10a50311
JD
1643 }
1644 }
1645
9ce5646a 1646 health_code_update();
6dc3064a
DG
1647 ret = consumer_send_status_msg(sock, ret_code);
1648 if (ret < 0) {
1649 /* Somehow, the session daemon is not responding anymore. */
1650 goto end_nosignal;
1651 }
9ce5646a 1652 health_code_update();
6dc3064a
DG
1653 break;
1654 }
3bd1e081
MD
1655 default:
1656 break;
1657 }
3f8e211f 1658
3bd1e081 1659end_nosignal:
b0b335c8 1660 rcu_read_unlock();
4cbc1a04 1661
9ce5646a
MD
1662 health_code_update();
1663
4cbc1a04
DG
1664 /*
1665 * Return 1 to indicate success since the 0 value can be a socket
1666 * shutdown during the recv() or send() call.
1667 */
1668 return 1;
ffe60014
DG
1669
1670end_msg_sessiond:
1671 /*
1672 * The returned value here is not useful since either way we'll return 1 to
1673 * the caller because the session daemon socket management is done
1674 * elsewhere. Returning a negative code or 0 will shutdown the consumer.
1675 */
489f70e9
MD
1676 ret = consumer_send_status_msg(sock, ret_code);
1677 if (ret < 0) {
1678 goto error_fatal;
1679 }
ffe60014 1680 rcu_read_unlock();
9ce5646a
MD
1681
1682 health_code_update();
1683
ffe60014
DG
1684 return 1;
1685end_channel_error:
1686 if (channel) {
1687 /*
1688 * Free channel here since no one has a reference to it. We don't
1689 * free after that because a stream can store this pointer.
1690 */
1691 destroy_channel(channel);
1692 }
1693 /* We have to send a status channel message indicating an error. */
1694 ret = consumer_send_status_channel(sock, NULL);
1695 if (ret < 0) {
1696 /* Stop everything if session daemon can not be notified. */
1697 goto error_fatal;
1698 }
1699 rcu_read_unlock();
9ce5646a
MD
1700
1701 health_code_update();
1702
ffe60014
DG
1703 return 1;
1704error_fatal:
1705 rcu_read_unlock();
1706 /* This will issue a consumer stop. */
1707 return -1;
3bd1e081
MD
1708}
1709
ffe60014
DG
1710/*
1711 * Wrapper over the mmap() read offset from ust-ctl library. Since this can be
1712 * compiled out, we isolate it in this library.
1713 */
1714int lttng_ustctl_get_mmap_read_offset(struct lttng_consumer_stream *stream,
1715 unsigned long *off)
3bd1e081 1716{
ffe60014
DG
1717 assert(stream);
1718 assert(stream->ustream);
b5c5fc29 1719
ffe60014 1720 return ustctl_get_mmap_read_offset(stream->ustream, off);
3bd1e081
MD
1721}
1722
ffe60014
DG
1723/*
1724 * Wrapper over the mmap() read offset from ust-ctl library. Since this can be
1725 * compiled out, we isolate it in this library.
1726 */
1727void *lttng_ustctl_get_mmap_base(struct lttng_consumer_stream *stream)
d056b477 1728{
ffe60014
DG
1729 assert(stream);
1730 assert(stream->ustream);
1731
1732 return ustctl_get_mmap_base(stream->ustream);
d056b477
MD
1733}
1734
ffe60014
DG
1735/*
1736 * Take a snapshot for a specific fd
1737 *
1738 * Returns 0 on success, < 0 on error
1739 */
1740int lttng_ustconsumer_take_snapshot(struct lttng_consumer_stream *stream)
3bd1e081 1741{
ffe60014
DG
1742 assert(stream);
1743 assert(stream->ustream);
1744
1745 return ustctl_snapshot(stream->ustream);
3bd1e081
MD
1746}
1747
ffe60014
DG
1748/*
1749 * Get the produced position
1750 *
1751 * Returns 0 on success, < 0 on error
1752 */
1753int lttng_ustconsumer_get_produced_snapshot(
1754 struct lttng_consumer_stream *stream, unsigned long *pos)
3bd1e081 1755{
ffe60014
DG
1756 assert(stream);
1757 assert(stream->ustream);
1758 assert(pos);
7a57cf92 1759
ffe60014
DG
1760 return ustctl_snapshot_get_produced(stream->ustream, pos);
1761}
7a57cf92 1762
10a50311
JD
1763/*
1764 * Get the consumed position
1765 *
1766 * Returns 0 on success, < 0 on error
1767 */
1768int lttng_ustconsumer_get_consumed_snapshot(
1769 struct lttng_consumer_stream *stream, unsigned long *pos)
1770{
1771 assert(stream);
1772 assert(stream->ustream);
1773 assert(pos);
1774
1775 return ustctl_snapshot_get_consumed(stream->ustream, pos);
1776}
1777
84a182ce
DG
1778void lttng_ustconsumer_flush_buffer(struct lttng_consumer_stream *stream,
1779 int producer)
1780{
1781 assert(stream);
1782 assert(stream->ustream);
1783
1784 ustctl_flush_buffer(stream->ustream, producer);
1785}
1786
1787int lttng_ustconsumer_get_current_timestamp(
1788 struct lttng_consumer_stream *stream, uint64_t *ts)
1789{
1790 assert(stream);
1791 assert(stream->ustream);
1792 assert(ts);
1793
1794 return ustctl_get_current_timestamp(stream->ustream, ts);
1795}
1796
ffe60014
DG
1797/*
1798 * Called when the stream signal the consumer that it has hang up.
1799 */
1800void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream *stream)
1801{
1802 assert(stream);
1803 assert(stream->ustream);
2c1dd183 1804
ffe60014
DG
1805 ustctl_flush_buffer(stream->ustream, 0);
1806 stream->hangup_flush_done = 1;
1807}
ee77a7b0 1808
ffe60014
DG
1809void lttng_ustconsumer_del_channel(struct lttng_consumer_channel *chan)
1810{
4628484a
MD
1811 int i;
1812
ffe60014
DG
1813 assert(chan);
1814 assert(chan->uchan);
e316aad5 1815
ea88ca2a
MD
1816 if (chan->switch_timer_enabled == 1) {
1817 consumer_timer_switch_stop(chan);
1818 }
1819 consumer_metadata_cache_destroy(chan);
ffe60014 1820 ustctl_destroy_channel(chan->uchan);
4628484a
MD
1821 for (i = 0; i < chan->nr_stream_fds; i++) {
1822 int ret;
1823
1824 ret = close(chan->stream_fds[i]);
1825 if (ret) {
1826 PERROR("close");
1827 }
1828 if (chan->shm_path[0]) {
1829 char shm_path[PATH_MAX];
1830
1831 ret = get_stream_shm_path(shm_path, chan->shm_path, i);
1832 if (ret) {
1833 ERR("Cannot get stream shm path");
1834 }
1835 ret = run_as_unlink(shm_path, chan->uid, chan->gid);
1836 if (ret) {
4628484a
MD
1837 PERROR("unlink %s", shm_path);
1838 }
1839 }
1840 }
1841 free(chan->stream_fds);
3d071855
MD
1842 /* Try to rmdir all directories under shm_path root. */
1843 if (chan->root_shm_path[0]) {
4628484a
MD
1844 (void) run_as_recursive_rmdir(chan->root_shm_path,
1845 chan->uid, chan->gid);
3d071855 1846 }
3bd1e081
MD
1847}
1848
1849void lttng_ustconsumer_del_stream(struct lttng_consumer_stream *stream)
1850{
ffe60014
DG
1851 assert(stream);
1852 assert(stream->ustream);
d41f73b7 1853
ea88ca2a
MD
1854 if (stream->chan->switch_timer_enabled == 1) {
1855 consumer_timer_switch_stop(stream->chan);
1856 }
ffe60014
DG
1857 ustctl_destroy_stream(stream->ustream);
1858}
d41f73b7 1859
6d574024
DG
1860int lttng_ustconsumer_get_wakeup_fd(struct lttng_consumer_stream *stream)
1861{
1862 assert(stream);
1863 assert(stream->ustream);
1864
1865 return ustctl_stream_get_wakeup_fd(stream->ustream);
1866}
1867
1868int lttng_ustconsumer_close_wakeup_fd(struct lttng_consumer_stream *stream)
1869{
1870 assert(stream);
1871 assert(stream->ustream);
1872
1873 return ustctl_stream_close_wakeup_fd(stream->ustream);
1874}
1875
309167d2
JD
1876/*
1877 * Populate index values of a UST stream. Values are set in big endian order.
1878 *
1879 * Return 0 on success or else a negative value.
1880 */
50adc264 1881static int get_index_values(struct ctf_packet_index *index,
309167d2
JD
1882 struct ustctl_consumer_stream *ustream)
1883{
1884 int ret;
1885
1886 ret = ustctl_get_timestamp_begin(ustream, &index->timestamp_begin);
1887 if (ret < 0) {
1888 PERROR("ustctl_get_timestamp_begin");
1889 goto error;
1890 }
1891 index->timestamp_begin = htobe64(index->timestamp_begin);
1892
1893 ret = ustctl_get_timestamp_end(ustream, &index->timestamp_end);
1894 if (ret < 0) {
1895 PERROR("ustctl_get_timestamp_end");
1896 goto error;
1897 }
1898 index->timestamp_end = htobe64(index->timestamp_end);
1899
1900 ret = ustctl_get_events_discarded(ustream, &index->events_discarded);
1901 if (ret < 0) {
1902 PERROR("ustctl_get_events_discarded");
1903 goto error;
1904 }
1905 index->events_discarded = htobe64(index->events_discarded);
1906
1907 ret = ustctl_get_content_size(ustream, &index->content_size);
1908 if (ret < 0) {
1909 PERROR("ustctl_get_content_size");
1910 goto error;
1911 }
1912 index->content_size = htobe64(index->content_size);
1913
1914 ret = ustctl_get_packet_size(ustream, &index->packet_size);
1915 if (ret < 0) {
1916 PERROR("ustctl_get_packet_size");
1917 goto error;
1918 }
1919 index->packet_size = htobe64(index->packet_size);
1920
1921 ret = ustctl_get_stream_id(ustream, &index->stream_id);
1922 if (ret < 0) {
1923 PERROR("ustctl_get_stream_id");
1924 goto error;
1925 }
1926 index->stream_id = htobe64(index->stream_id);
1927
1928error:
1929 return ret;
1930}
1931
94d49140
JD
1932/*
1933 * Write up to one packet from the metadata cache to the channel.
1934 *
1935 * Returns the number of bytes pushed in the cache, or a negative value
1936 * on error.
1937 */
1938static
1939int commit_one_metadata_packet(struct lttng_consumer_stream *stream)
1940{
1941 ssize_t write_len;
1942 int ret;
1943
1944 pthread_mutex_lock(&stream->chan->metadata_cache->lock);
1945 if (stream->chan->metadata_cache->contiguous
1946 == stream->ust_metadata_pushed) {
1947 ret = 0;
1948 goto end;
1949 }
1950
1951 write_len = ustctl_write_one_packet_to_channel(stream->chan->uchan,
1952 &stream->chan->metadata_cache->data[stream->ust_metadata_pushed],
1953 stream->chan->metadata_cache->contiguous
1954 - stream->ust_metadata_pushed);
1955 assert(write_len != 0);
1956 if (write_len < 0) {
1957 ERR("Writing one metadata packet");
1958 ret = -1;
1959 goto end;
1960 }
1961 stream->ust_metadata_pushed += write_len;
1962
1963 assert(stream->chan->metadata_cache->contiguous >=
1964 stream->ust_metadata_pushed);
1965 ret = write_len;
1966
1967end:
1968 pthread_mutex_unlock(&stream->chan->metadata_cache->lock);
1969 return ret;
1970}
1971
309167d2 1972
94d49140
JD
1973/*
1974 * Sync metadata meaning request them to the session daemon and snapshot to the
1975 * metadata thread can consumer them.
1976 *
1977 * Metadata stream lock MUST be acquired.
1978 *
1979 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
1980 * is empty or a negative value on error.
1981 */
1982int lttng_ustconsumer_sync_metadata(struct lttng_consumer_local_data *ctx,
1983 struct lttng_consumer_stream *metadata)
1984{
1985 int ret;
1986 int retry = 0;
1987
1988 assert(ctx);
1989 assert(metadata);
1990
1991 /*
1992 * Request metadata from the sessiond, but don't wait for the flush
1993 * because we locked the metadata thread.
1994 */
1995 ret = lttng_ustconsumer_request_metadata(ctx, metadata->chan, 0, 0);
1996 if (ret < 0) {
1997 goto end;
1998 }
1999
2000 ret = commit_one_metadata_packet(metadata);
2001 if (ret <= 0) {
2002 goto end;
2003 } else if (ret > 0) {
2004 retry = 1;
2005 }
2006
2007 ustctl_flush_buffer(metadata->ustream, 1);
2008 ret = ustctl_snapshot(metadata->ustream);
2009 if (ret < 0) {
2010 if (errno != EAGAIN) {
2011 ERR("Sync metadata, taking UST snapshot");
2012 goto end;
2013 }
2014 DBG("No new metadata when syncing them.");
2015 /* No new metadata, exit. */
2016 ret = ENODATA;
2017 goto end;
2018 }
2019
2020 /*
2021 * After this flush, we still need to extract metadata.
2022 */
2023 if (retry) {
2024 ret = EAGAIN;
2025 }
2026
2027end:
2028 return ret;
2029}
2030
02b3d176
DG
2031/*
2032 * Return 0 on success else a negative value.
2033 */
2034static int notify_if_more_data(struct lttng_consumer_stream *stream,
2035 struct lttng_consumer_local_data *ctx)
2036{
2037 int ret;
2038 struct ustctl_consumer_stream *ustream;
2039
2040 assert(stream);
2041 assert(ctx);
2042
2043 ustream = stream->ustream;
2044
2045 /*
2046 * First, we are going to check if there is a new subbuffer available
2047 * before reading the stream wait_fd.
2048 */
2049 /* Get the next subbuffer */
2050 ret = ustctl_get_next_subbuf(ustream);
2051 if (ret) {
2052 /* No more data found, flag the stream. */
2053 stream->has_data = 0;
2054 ret = 0;
2055 goto end;
2056 }
2057
5420e5db 2058 ret = ustctl_put_subbuf(ustream);
02b3d176
DG
2059 assert(!ret);
2060
2061 /* This stream still has data. Flag it and wake up the data thread. */
2062 stream->has_data = 1;
2063
2064 if (stream->monitor && !stream->hangup_flush_done && !ctx->has_wakeup) {
2065 ssize_t writelen;
2066
2067 writelen = lttng_pipe_write(ctx->consumer_wakeup_pipe, "!", 1);
2068 if (writelen < 0 && errno != EAGAIN && errno != EWOULDBLOCK) {
2069 ret = writelen;
2070 goto end;
2071 }
2072
2073 /* The wake up pipe has been notified. */
2074 ctx->has_wakeup = 1;
2075 }
2076 ret = 0;
2077
2078end:
2079 return ret;
2080}
2081
94d49140
JD
2082/*
2083 * Read subbuffer from the given stream.
2084 *
2085 * Stream lock MUST be acquired.
2086 *
2087 * Return 0 on success else a negative value.
2088 */
d41f73b7
MD
2089int lttng_ustconsumer_read_subbuffer(struct lttng_consumer_stream *stream,
2090 struct lttng_consumer_local_data *ctx)
2091{
1d4dfdef 2092 unsigned long len, subbuf_size, padding;
1c20f0e2 2093 int err, write_index = 1;
d41f73b7 2094 long ret = 0;
ffe60014 2095 struct ustctl_consumer_stream *ustream;
50adc264 2096 struct ctf_packet_index index;
ffe60014
DG
2097
2098 assert(stream);
2099 assert(stream->ustream);
2100 assert(ctx);
d41f73b7 2101
3eb914c0 2102 DBG("In UST read_subbuffer (wait_fd: %d, name: %s)", stream->wait_fd,
ffe60014
DG
2103 stream->name);
2104
2105 /* Ease our life for what's next. */
2106 ustream = stream->ustream;
d41f73b7 2107
6cd525e8 2108 /*
02b3d176
DG
2109 * We can consume the 1 byte written into the wait_fd by UST. Don't trigger
2110 * error if we cannot read this one byte (read returns 0), or if the error
2111 * is EAGAIN or EWOULDBLOCK.
2112 *
2113 * This is only done when the stream is monitored by a thread, before the
2114 * flush is done after a hangup and if the stream is not flagged with data
2115 * since there might be nothing to consume in the wait fd but still have
2116 * data available flagged by the consumer wake up pipe.
6cd525e8 2117 */
02b3d176
DG
2118 if (stream->monitor && !stream->hangup_flush_done && !stream->has_data) {
2119 char dummy;
c617c0c6
MD
2120 ssize_t readlen;
2121
6cd525e8
MD
2122 readlen = lttng_read(stream->wait_fd, &dummy, 1);
2123 if (readlen < 0 && errno != EAGAIN && errno != EWOULDBLOCK) {
effcf122
MD
2124 ret = readlen;
2125 goto end;
2126 }
d41f73b7
MD
2127 }
2128
04ef1097 2129retry:
d41f73b7 2130 /* Get the next subbuffer */
ffe60014 2131 err = ustctl_get_next_subbuf(ustream);
d41f73b7 2132 if (err != 0) {
04ef1097
MD
2133 /*
2134 * Populate metadata info if the existing info has
2135 * already been read.
2136 */
2137 if (stream->metadata_flag) {
94d49140
JD
2138 ret = commit_one_metadata_packet(stream);
2139 if (ret <= 0) {
04ef1097
MD
2140 goto end;
2141 }
04ef1097
MD
2142 ustctl_flush_buffer(stream->ustream, 1);
2143 goto retry;
2144 }
2145
1d4dfdef 2146 ret = err; /* ustctl_get_next_subbuf returns negative, caller expect positive. */
d41f73b7
MD
2147 /*
2148 * This is a debug message even for single-threaded consumer,
2149 * because poll() have more relaxed criterions than get subbuf,
2150 * so get_subbuf may fail for short race windows where poll()
2151 * would issue wakeups.
2152 */
2153 DBG("Reserving sub buffer failed (everything is normal, "
ffe60014 2154 "it is due to concurrency) [ret: %d]", err);
d41f73b7
MD
2155 goto end;
2156 }
ffe60014 2157 assert(stream->chan->output == CONSUMER_CHANNEL_MMAP);
309167d2 2158
1c20f0e2 2159 if (!stream->metadata_flag) {
309167d2
JD
2160 index.offset = htobe64(stream->out_fd_offset);
2161 ret = get_index_values(&index, ustream);
2162 if (ret < 0) {
2163 goto end;
2164 }
1c20f0e2
JD
2165 } else {
2166 write_index = 0;
309167d2
JD
2167 }
2168
1d4dfdef 2169 /* Get the full padded subbuffer size */
ffe60014 2170 err = ustctl_get_padded_subbuf_size(ustream, &len);
effcf122 2171 assert(err == 0);
1d4dfdef
DG
2172
2173 /* Get subbuffer data size (without padding) */
ffe60014 2174 err = ustctl_get_subbuf_size(ustream, &subbuf_size);
1d4dfdef
DG
2175 assert(err == 0);
2176
2177 /* Make sure we don't get a subbuffer size bigger than the padded */
2178 assert(len >= subbuf_size);
2179
2180 padding = len - subbuf_size;
d41f73b7 2181 /* write the subbuffer to the tracefile */
309167d2 2182 ret = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, subbuf_size, padding, &index);
91dfef6e
DG
2183 /*
2184 * The mmap operation should write subbuf_size amount of data when network
2185 * streaming or the full padding (len) size when we are _not_ streaming.
2186 */
d88aee68
DG
2187 if ((ret != subbuf_size && stream->net_seq_idx != (uint64_t) -1ULL) ||
2188 (ret != len && stream->net_seq_idx == (uint64_t) -1ULL)) {
d41f73b7 2189 /*
91dfef6e 2190 * Display the error but continue processing to try to release the
c5c45efa
DG
2191 * subbuffer. This is a DBG statement since any unexpected kill or
2192 * signal, the application gets unregistered, relayd gets closed or
2193 * anything that affects the buffer lifetime will trigger this error.
2194 * So, for the sake of the user, don't print this error since it can
2195 * happen and it is OK with the code flow.
d41f73b7 2196 */
c5c45efa 2197 DBG("Error writing to tracefile "
8fd623e0 2198 "(ret: %ld != len: %lu != subbuf_size: %lu)",
91dfef6e 2199 ret, len, subbuf_size);
309167d2 2200 write_index = 0;
d41f73b7 2201 }
ffe60014 2202 err = ustctl_put_next_subbuf(ustream);
effcf122 2203 assert(err == 0);
331744e3 2204
02b3d176
DG
2205 /*
2206 * This will consumer the byte on the wait_fd if and only if there is not
2207 * next subbuffer to be acquired.
2208 */
2209 if (!stream->metadata_flag) {
2210 ret = notify_if_more_data(stream, ctx);
2211 if (ret < 0) {
2212 goto end;
2213 }
2214 }
2215
309167d2 2216 /* Write index if needed. */
1c20f0e2
JD
2217 if (!write_index) {
2218 goto end;
2219 }
2220
94d49140
JD
2221 if (stream->chan->live_timer_interval && !stream->metadata_flag) {
2222 /*
2223 * In live, block until all the metadata is sent.
2224 */
2225 err = consumer_stream_sync_metadata(ctx, stream->session_id);
2226 if (err < 0) {
2227 goto end;
2228 }
2229 }
2230
1c20f0e2
JD
2231 assert(!stream->metadata_flag);
2232 err = consumer_stream_write_index(stream, &index);
2233 if (err < 0) {
2234 goto end;
309167d2
JD
2235 }
2236
d41f73b7
MD
2237end:
2238 return ret;
2239}
2240
ffe60014
DG
2241/*
2242 * Called when a stream is created.
fe4477ee
JD
2243 *
2244 * Return 0 on success or else a negative value.
ffe60014 2245 */
d41f73b7
MD
2246int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream *stream)
2247{
fe4477ee
JD
2248 int ret;
2249
10a50311
JD
2250 assert(stream);
2251
fe4477ee 2252 /* Don't create anything if this is set for streaming. */
10a50311 2253 if (stream->net_seq_idx == (uint64_t) -1ULL && stream->chan->monitor) {
fe4477ee
JD
2254 ret = utils_create_stream_file(stream->chan->pathname, stream->name,
2255 stream->chan->tracefile_size, stream->tracefile_count_current,
309167d2 2256 stream->uid, stream->gid, NULL);
fe4477ee
JD
2257 if (ret < 0) {
2258 goto error;
2259 }
2260 stream->out_fd = ret;
2261 stream->tracefile_size_current = 0;
309167d2
JD
2262
2263 if (!stream->metadata_flag) {
2264 ret = index_create_file(stream->chan->pathname,
2265 stream->name, stream->uid, stream->gid,
2266 stream->chan->tracefile_size,
2267 stream->tracefile_count_current);
2268 if (ret < 0) {
2269 goto error;
2270 }
2271 stream->index_fd = ret;
2272 }
fe4477ee
JD
2273 }
2274 ret = 0;
2275
2276error:
2277 return ret;
d41f73b7 2278}
ca22feea
DG
2279
2280/*
2281 * Check if data is still being extracted from the buffers for a specific
4e9a4686
DG
2282 * stream. Consumer data lock MUST be acquired before calling this function
2283 * and the stream lock.
ca22feea 2284 *
6d805429 2285 * Return 1 if the traced data are still getting read else 0 meaning that the
ca22feea
DG
2286 * data is available for trace viewer reading.
2287 */
6d805429 2288int lttng_ustconsumer_data_pending(struct lttng_consumer_stream *stream)
ca22feea
DG
2289{
2290 int ret;
2291
2292 assert(stream);
ffe60014 2293 assert(stream->ustream);
ca22feea 2294
6d805429 2295 DBG("UST consumer checking data pending");
c8f59ee5 2296
ca6b395f
MD
2297 if (stream->endpoint_status != CONSUMER_ENDPOINT_ACTIVE) {
2298 ret = 0;
2299 goto end;
2300 }
2301
04ef1097 2302 if (stream->chan->type == CONSUMER_CHANNEL_TYPE_METADATA) {
e6ee4eab
DG
2303 uint64_t contiguous, pushed;
2304
2305 /* Ease our life a bit. */
2306 contiguous = stream->chan->metadata_cache->contiguous;
2307 pushed = stream->ust_metadata_pushed;
2308
04ef1097
MD
2309 /*
2310 * We can simply check whether all contiguously available data
2311 * has been pushed to the ring buffer, since the push operation
2312 * is performed within get_next_subbuf(), and because both
2313 * get_next_subbuf() and put_next_subbuf() are issued atomically
2314 * thanks to the stream lock within
2315 * lttng_ustconsumer_read_subbuffer(). This basically means that
2316 * whetnever ust_metadata_pushed is incremented, the associated
2317 * metadata has been consumed from the metadata stream.
2318 */
2319 DBG("UST consumer metadata pending check: contiguous %" PRIu64 " vs pushed %" PRIu64,
e6ee4eab 2320 contiguous, pushed);
aa01b94c 2321 assert(((int64_t) (contiguous - pushed)) >= 0);
e6ee4eab 2322 if ((contiguous != pushed) ||
6acdf328 2323 (((int64_t) contiguous - pushed) > 0 || contiguous == 0)) {
04ef1097
MD
2324 ret = 1; /* Data is pending */
2325 goto end;
2326 }
2327 } else {
2328 ret = ustctl_get_next_subbuf(stream->ustream);
2329 if (ret == 0) {
2330 /*
2331 * There is still data so let's put back this
2332 * subbuffer.
2333 */
2334 ret = ustctl_put_subbuf(stream->ustream);
2335 assert(ret == 0);
2336 ret = 1; /* Data is pending */
2337 goto end;
2338 }
ca22feea
DG
2339 }
2340
6d805429
DG
2341 /* Data is NOT pending so ready to be read. */
2342 ret = 0;
ca22feea 2343
6efae65e
DG
2344end:
2345 return ret;
ca22feea 2346}
d88aee68 2347
6d574024
DG
2348/*
2349 * Stop a given metadata channel timer if enabled and close the wait fd which
2350 * is the poll pipe of the metadata stream.
2351 *
2352 * This MUST be called with the metadata channel acquired.
2353 */
2354void lttng_ustconsumer_close_metadata(struct lttng_consumer_channel *metadata)
2355{
2356 int ret;
2357
2358 assert(metadata);
2359 assert(metadata->type == CONSUMER_CHANNEL_TYPE_METADATA);
2360
2361 DBG("Closing metadata channel key %" PRIu64, metadata->key);
2362
2363 if (metadata->switch_timer_enabled == 1) {
2364 consumer_timer_switch_stop(metadata);
2365 }
2366
2367 if (!metadata->metadata_stream) {
2368 goto end;
2369 }
2370
2371 /*
2372 * Closing write side so the thread monitoring the stream wakes up if any
2373 * and clean the metadata stream.
2374 */
2375 if (metadata->metadata_stream->ust_metadata_poll_pipe[1] >= 0) {
2376 ret = close(metadata->metadata_stream->ust_metadata_poll_pipe[1]);
2377 if (ret < 0) {
2378 PERROR("closing metadata pipe write side");
2379 }
2380 metadata->metadata_stream->ust_metadata_poll_pipe[1] = -1;
2381 }
2382
2383end:
2384 return;
2385}
2386
d88aee68
DG
2387/*
2388 * Close every metadata stream wait fd of the metadata hash table. This
2389 * function MUST be used very carefully so not to run into a race between the
2390 * metadata thread handling streams and this function closing their wait fd.
2391 *
2392 * For UST, this is used when the session daemon hangs up. Its the metadata
2393 * producer so calling this is safe because we are assured that no state change
2394 * can occur in the metadata thread for the streams in the hash table.
2395 */
6d574024 2396void lttng_ustconsumer_close_all_metadata(struct lttng_ht *metadata_ht)
d88aee68 2397{
d88aee68
DG
2398 struct lttng_ht_iter iter;
2399 struct lttng_consumer_stream *stream;
2400
2401 assert(metadata_ht);
2402 assert(metadata_ht->ht);
2403
2404 DBG("UST consumer closing all metadata streams");
2405
2406 rcu_read_lock();
2407 cds_lfht_for_each_entry(metadata_ht->ht, &iter.iter, stream,
2408 node.node) {
9ce5646a
MD
2409
2410 health_code_update();
2411
be2b50c7 2412 pthread_mutex_lock(&stream->chan->lock);
6d574024 2413 lttng_ustconsumer_close_metadata(stream->chan);
be2b50c7
DG
2414 pthread_mutex_unlock(&stream->chan->lock);
2415
d88aee68
DG
2416 }
2417 rcu_read_unlock();
2418}
d8ef542d
MD
2419
2420void lttng_ustconsumer_close_stream_wakeup(struct lttng_consumer_stream *stream)
2421{
2422 int ret;
2423
2424 ret = ustctl_stream_close_wakeup_fd(stream->ustream);
2425 if (ret < 0) {
2426 ERR("Unable to close wakeup fd");
2427 }
2428}
331744e3 2429
f666ae70
MD
2430/*
2431 * Please refer to consumer-timer.c before adding any lock within this
2432 * function or any of its callees. Timers have a very strict locking
2433 * semantic with respect to teardown. Failure to respect this semantic
2434 * introduces deadlocks.
2435 */
331744e3 2436int lttng_ustconsumer_request_metadata(struct lttng_consumer_local_data *ctx,
94d49140 2437 struct lttng_consumer_channel *channel, int timer, int wait)
331744e3
JD
2438{
2439 struct lttcomm_metadata_request_msg request;
2440 struct lttcomm_consumer_msg msg;
0c759fc9 2441 enum lttcomm_return_code ret_code = LTTCOMM_CONSUMERD_SUCCESS;
331744e3
JD
2442 uint64_t len, key, offset;
2443 int ret;
2444
2445 assert(channel);
2446 assert(channel->metadata_cache);
2447
53efb85a
MD
2448 memset(&request, 0, sizeof(request));
2449
331744e3
JD
2450 /* send the metadata request to sessiond */
2451 switch (consumer_data.type) {
2452 case LTTNG_CONSUMER64_UST:
2453 request.bits_per_long = 64;
2454 break;
2455 case LTTNG_CONSUMER32_UST:
2456 request.bits_per_long = 32;
2457 break;
2458 default:
2459 request.bits_per_long = 0;
2460 break;
2461 }
2462
2463 request.session_id = channel->session_id;
1950109e 2464 request.session_id_per_pid = channel->session_id_per_pid;
567eb353
DG
2465 /*
2466 * Request the application UID here so the metadata of that application can
2467 * be sent back. The channel UID corresponds to the user UID of the session
2468 * used for the rights on the stream file(s).
2469 */
2470 request.uid = channel->ust_app_uid;
331744e3 2471 request.key = channel->key;
567eb353 2472
1950109e 2473 DBG("Sending metadata request to sessiond, session id %" PRIu64
567eb353
DG
2474 ", per-pid %" PRIu64 ", app UID %u and channek key %" PRIu64,
2475 request.session_id, request.session_id_per_pid, request.uid,
2476 request.key);
331744e3 2477
75d83e50 2478 pthread_mutex_lock(&ctx->metadata_socket_lock);
9ce5646a
MD
2479
2480 health_code_update();
2481
331744e3
JD
2482 ret = lttcomm_send_unix_sock(ctx->consumer_metadata_socket, &request,
2483 sizeof(request));
2484 if (ret < 0) {
2485 ERR("Asking metadata to sessiond");
2486 goto end;
2487 }
2488
9ce5646a
MD
2489 health_code_update();
2490
331744e3
JD
2491 /* Receive the metadata from sessiond */
2492 ret = lttcomm_recv_unix_sock(ctx->consumer_metadata_socket, &msg,
2493 sizeof(msg));
2494 if (ret != sizeof(msg)) {
8fd623e0 2495 DBG("Consumer received unexpected message size %d (expects %zu)",
331744e3
JD
2496 ret, sizeof(msg));
2497 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_CMD);
2498 /*
2499 * The ret value might 0 meaning an orderly shutdown but this is ok
2500 * since the caller handles this.
2501 */
2502 goto end;
2503 }
2504
9ce5646a
MD
2505 health_code_update();
2506
331744e3
JD
2507 if (msg.cmd_type == LTTNG_ERR_UND) {
2508 /* No registry found */
2509 (void) consumer_send_status_msg(ctx->consumer_metadata_socket,
2510 ret_code);
2511 ret = 0;
2512 goto end;
2513 } else if (msg.cmd_type != LTTNG_CONSUMER_PUSH_METADATA) {
2514 ERR("Unexpected cmd_type received %d", msg.cmd_type);
2515 ret = -1;
2516 goto end;
2517 }
2518
2519 len = msg.u.push_metadata.len;
2520 key = msg.u.push_metadata.key;
2521 offset = msg.u.push_metadata.target_offset;
2522
2523 assert(key == channel->key);
2524 if (len == 0) {
2525 DBG("No new metadata to receive for key %" PRIu64, key);
2526 }
2527
9ce5646a
MD
2528 health_code_update();
2529
331744e3
JD
2530 /* Tell session daemon we are ready to receive the metadata. */
2531 ret = consumer_send_status_msg(ctx->consumer_metadata_socket,
0c759fc9 2532 LTTCOMM_CONSUMERD_SUCCESS);
331744e3
JD
2533 if (ret < 0 || len == 0) {
2534 /*
2535 * Somehow, the session daemon is not responding anymore or there is
2536 * nothing to receive.
2537 */
2538 goto end;
2539 }
2540
9ce5646a
MD
2541 health_code_update();
2542
1eb682be 2543 ret = lttng_ustconsumer_recv_metadata(ctx->consumer_metadata_socket,
94d49140 2544 key, offset, len, channel, timer, wait);
1eb682be 2545 if (ret >= 0) {
f2a444f1
DG
2546 /*
2547 * Only send the status msg if the sessiond is alive meaning a positive
2548 * ret code.
2549 */
1eb682be 2550 (void) consumer_send_status_msg(ctx->consumer_metadata_socket, ret);
f2a444f1 2551 }
331744e3
JD
2552 ret = 0;
2553
2554end:
9ce5646a
MD
2555 health_code_update();
2556
75d83e50 2557 pthread_mutex_unlock(&ctx->metadata_socket_lock);
331744e3
JD
2558 return ret;
2559}
70190e1c
DG
2560
2561/*
2562 * Return the ustctl call for the get stream id.
2563 */
2564int lttng_ustconsumer_get_stream_id(struct lttng_consumer_stream *stream,
2565 uint64_t *stream_id)
2566{
2567 assert(stream);
2568 assert(stream_id);
2569
2570 return ustctl_get_stream_id(stream->ustream, stream_id);
2571}
This page took 0.223408 seconds and 5 git commands to generate.