Fix: don't fail on push metadata if no channel
[lttng-tools.git] / src / common / ust-consumer / ust-consumer.c
CommitLineData
3bd1e081
MD
1/*
2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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
20#include <assert.h>
f02e1e8a 21#include <lttng/ust-ctl.h>
3bd1e081
MD
22#include <poll.h>
23#include <pthread.h>
24#include <stdlib.h>
25#include <string.h>
26#include <sys/mman.h>
27#include <sys/socket.h>
dbb5dfe6 28#include <sys/stat.h>
3bd1e081 29#include <sys/types.h>
77c7c900 30#include <inttypes.h>
3bd1e081 31#include <unistd.h>
ffe60014 32#include <urcu/list.h>
331744e3 33#include <signal.h>
0857097f 34
990570ed 35#include <common/common.h>
10a8a223 36#include <common/sessiond-comm/sessiond-comm.h>
00e2e675 37#include <common/relayd/relayd.h>
dbb5dfe6 38#include <common/compat/fcntl.h>
331744e3
JD
39#include <common/consumer-metadata-cache.h>
40#include <common/consumer-timer.h>
fe4477ee 41#include <common/utils.h>
10a8a223
DG
42
43#include "ust-consumer.h"
3bd1e081
MD
44
45extern struct lttng_consumer_global_data consumer_data;
46extern int consumer_poll_timeout;
47extern volatile int consumer_quit;
48
49/*
ffe60014
DG
50 * Free channel object and all streams associated with it. This MUST be used
51 * only and only if the channel has _NEVER_ been added to the global channel
52 * hash table.
3bd1e081 53 */
ffe60014 54static void destroy_channel(struct lttng_consumer_channel *channel)
3bd1e081 55{
ffe60014
DG
56 struct lttng_consumer_stream *stream, *stmp;
57
58 assert(channel);
59
60 DBG("UST consumer cleaning stream list");
61
62 cds_list_for_each_entry_safe(stream, stmp, &channel->streams.head,
63 send_node) {
64 cds_list_del(&stream->send_node);
65 ustctl_destroy_stream(stream->ustream);
66 free(stream);
67 }
68
69 /*
70 * If a channel is available meaning that was created before the streams
71 * were, delete it.
72 */
73 if (channel->uchan) {
74 lttng_ustconsumer_del_channel(channel);
75 }
76 free(channel);
77}
3bd1e081
MD
78
79/*
ffe60014 80 * Add channel to internal consumer state.
3bd1e081 81 *
ffe60014 82 * Returns 0 on success or else a negative value.
3bd1e081 83 */
ffe60014
DG
84static int add_channel(struct lttng_consumer_channel *channel,
85 struct lttng_consumer_local_data *ctx)
3bd1e081
MD
86{
87 int ret = 0;
88
ffe60014
DG
89 assert(channel);
90 assert(ctx);
91
92 if (ctx->on_recv_channel != NULL) {
93 ret = ctx->on_recv_channel(channel);
94 if (ret == 0) {
d8ef542d 95 ret = consumer_add_channel(channel, ctx);
ffe60014
DG
96 } else if (ret < 0) {
97 /* Most likely an ENOMEM. */
98 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
99 goto error;
100 }
101 } else {
d8ef542d 102 ret = consumer_add_channel(channel, ctx);
3bd1e081
MD
103 }
104
d88aee68 105 DBG("UST consumer channel added (key: %" PRIu64 ")", channel->key);
ffe60014
DG
106
107error:
3bd1e081
MD
108 return ret;
109}
110
111/*
ffe60014
DG
112 * Allocate and return a consumer channel object.
113 */
114static struct lttng_consumer_channel *allocate_channel(uint64_t session_id,
115 const char *pathname, const char *name, uid_t uid, gid_t gid,
98cf381a 116 uint64_t relayd_id, uint64_t key, enum lttng_event_output output,
cb7c6909
JD
117 uint64_t tracefile_size, uint64_t tracefile_count,
118 uint64_t session_id_per_pid)
ffe60014
DG
119{
120 assert(pathname);
121 assert(name);
122
cb7c6909
JD
123 return consumer_allocate_channel(key, session_id, pathname, name, uid,
124 gid, relayd_id, output, tracefile_size,
125 tracefile_count, session_id_per_pid);
ffe60014
DG
126}
127
128/*
129 * Allocate and return a consumer stream object. If _alloc_ret is not NULL, the
130 * error value if applicable is set in it else it is kept untouched.
3bd1e081 131 *
ffe60014 132 * Return NULL on error else the newly allocated stream object.
3bd1e081 133 */
ffe60014
DG
134static struct lttng_consumer_stream *allocate_stream(int cpu, int key,
135 struct lttng_consumer_channel *channel,
136 struct lttng_consumer_local_data *ctx, int *_alloc_ret)
137{
138 int alloc_ret;
139 struct lttng_consumer_stream *stream = NULL;
140
141 assert(channel);
142 assert(ctx);
143
144 stream = consumer_allocate_stream(channel->key,
145 key,
146 LTTNG_CONSUMER_ACTIVE_STREAM,
147 channel->name,
148 channel->uid,
149 channel->gid,
150 channel->relayd_id,
151 channel->session_id,
152 cpu,
153 &alloc_ret,
154 channel->type);
155 if (stream == NULL) {
156 switch (alloc_ret) {
157 case -ENOENT:
158 /*
159 * We could not find the channel. Can happen if cpu hotplug
160 * happens while tearing down.
161 */
162 DBG3("Could not find channel");
163 break;
164 case -ENOMEM:
165 case -EINVAL:
166 default:
167 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
168 break;
169 }
170 goto error;
171 }
172
173 stream->chan = channel;
174
175error:
176 if (_alloc_ret) {
177 *_alloc_ret = alloc_ret;
178 }
179 return stream;
180}
181
182/*
183 * Send the given stream pointer to the corresponding thread.
184 *
185 * Returns 0 on success else a negative value.
186 */
187static int send_stream_to_thread(struct lttng_consumer_stream *stream,
188 struct lttng_consumer_local_data *ctx)
189{
dae10966
DG
190 int ret;
191 struct lttng_pipe *stream_pipe;
ffe60014
DG
192
193 /* Get the right pipe where the stream will be sent. */
194 if (stream->metadata_flag) {
155e9bb8
MD
195 ret = consumer_add_metadata_stream(stream);
196 if (ret) {
197 ERR("Consumer add metadata stream %" PRIu64 " failed.",
198 stream->key);
199 goto error;
200 }
dae10966 201 stream_pipe = ctx->consumer_metadata_pipe;
ffe60014 202 } else {
155e9bb8
MD
203 ret = consumer_add_data_stream(stream);
204 if (ret) {
205 ERR("Consumer add stream %" PRIu64 " failed.",
206 stream->key);
207 goto error;
208 }
dae10966 209 stream_pipe = ctx->consumer_data_pipe;
ffe60014
DG
210 }
211
dae10966 212 ret = lttng_pipe_write(stream_pipe, &stream, sizeof(stream));
ffe60014 213 if (ret < 0) {
dae10966
DG
214 ERR("Consumer write %s stream to pipe %d",
215 stream->metadata_flag ? "metadata" : "data",
216 lttng_pipe_get_writefd(stream_pipe));
155e9bb8
MD
217 if (stream->metadata_flag) {
218 consumer_del_stream_for_metadata(stream);
219 } else {
220 consumer_del_stream_for_data(stream);
221 }
ffe60014 222 }
155e9bb8 223error:
ffe60014
DG
224 return ret;
225}
226
227/*
228 * Search for a relayd object related to the stream. If found, send the stream
229 * to the relayd.
230 *
231 * On success, returns 0 else a negative value.
232 */
233static int send_stream_to_relayd(struct lttng_consumer_stream *stream)
234{
235 int ret = 0;
236 struct consumer_relayd_sock_pair *relayd;
237
238 assert(stream);
239
240 relayd = consumer_find_relayd(stream->net_seq_idx);
241 if (relayd != NULL) {
242 pthread_mutex_lock(&relayd->ctrl_sock_mutex);
243 /* Add stream on the relayd */
244 ret = relayd_add_stream(&relayd->control_sock, stream->name,
0f907de1
JD
245 stream->chan->pathname, &stream->relayd_stream_id,
246 stream->chan->tracefile_size,
247 stream->chan->tracefile_count);
ffe60014
DG
248 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
249 if (ret < 0) {
250 goto error;
251 }
d88aee68
DG
252 } else if (stream->net_seq_idx != (uint64_t) -1ULL) {
253 ERR("Network sequence index %" PRIu64 " unknown. Not adding stream.",
ffe60014
DG
254 stream->net_seq_idx);
255 ret = -1;
256 goto error;
257 }
258
259error:
260 return ret;
261}
262
d88aee68
DG
263/*
264 * Create streams for the given channel using liblttng-ust-ctl.
265 *
266 * Return 0 on success else a negative value.
267 */
ffe60014
DG
268static int create_ust_streams(struct lttng_consumer_channel *channel,
269 struct lttng_consumer_local_data *ctx)
270{
271 int ret, cpu = 0;
272 struct ustctl_consumer_stream *ustream;
273 struct lttng_consumer_stream *stream;
274
275 assert(channel);
276 assert(ctx);
277
278 /*
279 * While a stream is available from ustctl. When NULL is returned, we've
280 * reached the end of the possible stream for the channel.
281 */
282 while ((ustream = ustctl_create_stream(channel->uchan, cpu))) {
283 int wait_fd;
284
749d339a 285 wait_fd = ustctl_stream_get_wait_fd(ustream);
ffe60014
DG
286
287 /* Allocate consumer stream object. */
288 stream = allocate_stream(cpu, wait_fd, channel, ctx, &ret);
289 if (!stream) {
290 goto error_alloc;
291 }
292 stream->ustream = ustream;
293 /*
294 * Store it so we can save multiple function calls afterwards since
295 * this value is used heavily in the stream threads. This is UST
296 * specific so this is why it's done after allocation.
297 */
298 stream->wait_fd = wait_fd;
299
b31398bb
DG
300 /*
301 * Increment channel refcount since the channel reference has now been
302 * assigned in the allocation process above.
303 */
304 uatomic_inc(&stream->chan->refcount);
305
ffe60014
DG
306 /*
307 * Order is important this is why a list is used. On error, the caller
308 * should clean this list.
309 */
310 cds_list_add_tail(&stream->send_node, &channel->streams.head);
311
312 ret = ustctl_get_max_subbuf_size(stream->ustream,
313 &stream->max_sb_size);
314 if (ret < 0) {
315 ERR("ustctl_get_max_subbuf_size failed for stream %s",
316 stream->name);
317 goto error;
318 }
319
320 /* Do actions once stream has been received. */
321 if (ctx->on_recv_stream) {
322 ret = ctx->on_recv_stream(stream);
323 if (ret < 0) {
324 goto error;
325 }
326 }
327
d88aee68 328 DBG("UST consumer add stream %s (key: %" PRIu64 ") with relayd id %" PRIu64,
ffe60014
DG
329 stream->name, stream->key, stream->relayd_stream_id);
330
331 /* Set next CPU stream. */
332 channel->streams.count = ++cpu;
d88aee68
DG
333
334 /* Keep stream reference when creating metadata. */
335 if (channel->type == CONSUMER_CHANNEL_TYPE_METADATA) {
336 channel->metadata_stream = stream;
337 }
ffe60014
DG
338 }
339
340 return 0;
341
342error:
343error_alloc:
344 return ret;
345}
346
347/*
348 * Create an UST channel with the given attributes and send it to the session
349 * daemon using the ust ctl API.
350 *
351 * Return 0 on success or else a negative value.
352 */
353static int create_ust_channel(struct ustctl_consumer_channel_attr *attr,
354 struct ustctl_consumer_channel **chanp)
355{
356 int ret;
357 struct ustctl_consumer_channel *channel;
358
359 assert(attr);
360 assert(chanp);
361
362 DBG3("Creating channel to ustctl with attr: [overwrite: %d, "
363 "subbuf_size: %" PRIu64 ", num_subbuf: %" PRIu64 ", "
364 "switch_timer_interval: %u, read_timer_interval: %u, "
365 "output: %d, type: %d", attr->overwrite, attr->subbuf_size,
366 attr->num_subbuf, attr->switch_timer_interval,
367 attr->read_timer_interval, attr->output, attr->type);
368
369 channel = ustctl_create_channel(attr);
370 if (!channel) {
371 ret = -1;
372 goto error_create;
373 }
374
375 *chanp = channel;
376
377 return 0;
378
379error_create:
380 return ret;
381}
382
d88aee68
DG
383/*
384 * Send a single given stream to the session daemon using the sock.
385 *
386 * Return 0 on success else a negative value.
387 */
ffe60014
DG
388static int send_sessiond_stream(int sock, struct lttng_consumer_stream *stream)
389{
390 int ret;
391
392 assert(stream);
393 assert(sock >= 0);
394
7e448fde 395 DBG("UST consumer sending stream %" PRIu64 " to sessiond", stream->key);
ffe60014
DG
396
397 /* Send stream to session daemon. */
398 ret = ustctl_send_stream_to_sessiond(sock, stream->ustream);
399 if (ret < 0) {
400 goto error;
401 }
402
ffe60014
DG
403error:
404 return ret;
405}
406
407/*
408 * Send channel to sessiond.
409 *
d88aee68 410 * Return 0 on success or else a negative value.
ffe60014
DG
411 */
412static int send_sessiond_channel(int sock,
413 struct lttng_consumer_channel *channel,
414 struct lttng_consumer_local_data *ctx, int *relayd_error)
415{
f2a444f1 416 int ret, ret_code = LTTNG_OK;
ffe60014
DG
417 struct lttng_consumer_stream *stream;
418
419 assert(channel);
420 assert(ctx);
421 assert(sock >= 0);
422
423 DBG("UST consumer sending channel %s to sessiond", channel->name);
424
ffe60014
DG
425 cds_list_for_each_entry(stream, &channel->streams.head, send_node) {
426 /* Try to send the stream to the relayd if one is available. */
427 ret = send_stream_to_relayd(stream);
428 if (ret < 0) {
429 /*
430 * Flag that the relayd was the problem here probably due to a
431 * communicaton error on the socket.
432 */
433 if (relayd_error) {
434 *relayd_error = 1;
435 }
f2a444f1 436 ret_code = LTTNG_ERR_RELAYD_CONNECT_FAIL;
ffe60014 437 }
f2a444f1 438 }
ffe60014 439
f2a444f1
DG
440 /* Inform sessiond that we are about to send channel and streams. */
441 ret = consumer_send_status_msg(sock, ret_code);
442 if (ret < 0 || ret_code != LTTNG_OK) {
443 /*
444 * Either the session daemon is not responding or the relayd died so we
445 * stop now.
446 */
447 goto error;
448 }
449
450 /* Send channel to sessiond. */
451 ret = ustctl_send_channel_to_sessiond(sock, channel->uchan);
452 if (ret < 0) {
453 goto error;
454 }
455
456 ret = ustctl_channel_close_wakeup_fd(channel->uchan);
457 if (ret < 0) {
458 goto error;
459 }
460
461 /* The channel was sent successfully to the sessiond at this point. */
462 cds_list_for_each_entry(stream, &channel->streams.head, send_node) {
ffe60014
DG
463 /* Send stream to session daemon. */
464 ret = send_sessiond_stream(sock, stream);
465 if (ret < 0) {
466 goto error;
467 }
468 }
469
470 /* Tell sessiond there is no more stream. */
471 ret = ustctl_send_stream_to_sessiond(sock, NULL);
472 if (ret < 0) {
473 goto error;
474 }
475
476 DBG("UST consumer NULL stream sent to sessiond");
477
478 return 0;
479
480error:
f2a444f1
DG
481 if (ret_code != LTTNG_OK) {
482 ret = -1;
483 }
ffe60014
DG
484 return ret;
485}
486
487/*
488 * Creates a channel and streams and add the channel it to the channel internal
489 * state. The created stream must ONLY be sent once the GET_CHANNEL command is
490 * received.
491 *
492 * Return 0 on success or else, a negative value is returned and the channel
493 * MUST be destroyed by consumer_del_channel().
494 */
495static int ask_channel(struct lttng_consumer_local_data *ctx, int sock,
496 struct lttng_consumer_channel *channel,
497 struct ustctl_consumer_channel_attr *attr)
3bd1e081
MD
498{
499 int ret;
500
ffe60014
DG
501 assert(ctx);
502 assert(channel);
503 assert(attr);
504
505 /*
506 * This value is still used by the kernel consumer since for the kernel,
507 * the stream ownership is not IN the consumer so we need to have the
508 * number of left stream that needs to be initialized so we can know when
509 * to delete the channel (see consumer.c).
510 *
511 * As for the user space tracer now, the consumer creates and sends the
512 * stream to the session daemon which only sends them to the application
513 * once every stream of a channel is received making this value useless
514 * because we they will be added to the poll thread before the application
515 * receives them. This ensures that a stream can not hang up during
516 * initilization of a channel.
517 */
518 channel->nb_init_stream_left = 0;
519
520 /* The reply msg status is handled in the following call. */
521 ret = create_ust_channel(attr, &channel->uchan);
522 if (ret < 0) {
523 goto error;
3bd1e081
MD
524 }
525
d8ef542d
MD
526 channel->wait_fd = ustctl_channel_get_wait_fd(channel->uchan);
527
ffe60014
DG
528 /* Open all streams for this channel. */
529 ret = create_ust_streams(channel, ctx);
530 if (ret < 0) {
531 goto error;
532 }
533
534error:
3bd1e081
MD
535 return ret;
536}
537
d88aee68
DG
538/*
539 * Send all stream of a channel to the right thread handling it.
540 *
541 * On error, return a negative value else 0 on success.
542 */
543static int send_streams_to_thread(struct lttng_consumer_channel *channel,
544 struct lttng_consumer_local_data *ctx)
545{
546 int ret = 0;
547 struct lttng_consumer_stream *stream, *stmp;
548
549 assert(channel);
550 assert(ctx);
551
552 /* Send streams to the corresponding thread. */
553 cds_list_for_each_entry_safe(stream, stmp, &channel->streams.head,
554 send_node) {
555 /* Sending the stream to the thread. */
556 ret = send_stream_to_thread(stream, ctx);
557 if (ret < 0) {
558 /*
559 * If we are unable to send the stream to the thread, there is
560 * a big problem so just stop everything.
561 */
155e9bb8
MD
562 /* Remove node from the channel stream list. */
563 cds_list_del(&stream->send_node);
d88aee68
DG
564 goto error;
565 }
566
567 /* Remove node from the channel stream list. */
568 cds_list_del(&stream->send_node);
569 }
570
571error:
572 return ret;
573}
574
575/*
576 * Write metadata to the given channel using ustctl to convert the string to
577 * the ringbuffer.
331744e3
JD
578 * Called only from consumer_metadata_cache_write.
579 * The metadata cache lock MUST be acquired to write in the cache.
d88aee68
DG
580 *
581 * Return 0 on success else a negative value.
582 */
331744e3 583int lttng_ustconsumer_push_metadata(struct lttng_consumer_channel *metadata,
d88aee68
DG
584 const char *metadata_str, uint64_t target_offset, uint64_t len)
585{
586 int ret;
587
588 assert(metadata);
589 assert(metadata_str);
590
591 DBG("UST consumer writing metadata to channel %s", metadata->name);
592
73811ecc
DG
593 if (!metadata->metadata_stream) {
594 ret = 0;
595 goto error;
596 }
597
331744e3
JD
598 assert(target_offset <= metadata->metadata_cache->max_offset);
599 ret = ustctl_write_metadata_to_channel(metadata->uchan,
600 metadata_str + target_offset, len);
d88aee68 601 if (ret < 0) {
8fd623e0 602 ERR("ustctl write metadata fail with ret %d, len %" PRIu64, ret, len);
d88aee68
DG
603 goto error;
604 }
d88aee68
DG
605
606 ustctl_flush_buffer(metadata->metadata_stream->ustream, 1);
607
608error:
609 return ret;
610}
611
7972aab2
DG
612/*
613 * Flush channel's streams using the given key to retrieve the channel.
614 *
615 * Return 0 on success else an LTTng error code.
616 */
617static int flush_channel(uint64_t chan_key)
618{
619 int ret = 0;
620 struct lttng_consumer_channel *channel;
621 struct lttng_consumer_stream *stream;
622 struct lttng_ht *ht;
623 struct lttng_ht_iter iter;
624
8fd623e0 625 DBG("UST consumer flush channel key %" PRIu64, chan_key);
7972aab2 626
a500c257 627 rcu_read_lock();
7972aab2
DG
628 channel = consumer_find_channel(chan_key);
629 if (!channel) {
8fd623e0 630 ERR("UST consumer flush channel %" PRIu64 " not found", chan_key);
7972aab2
DG
631 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
632 goto error;
633 }
634
635 ht = consumer_data.stream_per_chan_id_ht;
636
637 /* For each stream of the channel id, flush it. */
7972aab2
DG
638 cds_lfht_for_each_entry_duplicate(ht->ht,
639 ht->hash_fct(&channel->key, lttng_ht_seed), ht->match_fct,
640 &channel->key, &iter.iter, stream, node_channel_id.node) {
f1d912ca 641 ustctl_flush_buffer(stream->ustream, 1);
7972aab2 642 }
7972aab2 643error:
a500c257 644 rcu_read_unlock();
7972aab2
DG
645 return ret;
646}
647
d88aee68
DG
648/*
649 * Close metadata stream wakeup_fd using the given key to retrieve the channel.
a500c257 650 * RCU read side lock MUST be acquired before calling this function.
d88aee68
DG
651 *
652 * Return 0 on success else an LTTng error code.
653 */
654static int close_metadata(uint64_t chan_key)
655{
ea88ca2a 656 int ret = 0;
d88aee68
DG
657 struct lttng_consumer_channel *channel;
658
8fd623e0 659 DBG("UST consumer close metadata key %" PRIu64, chan_key);
d88aee68
DG
660
661 channel = consumer_find_channel(chan_key);
662 if (!channel) {
84cc9aa0
DG
663 /*
664 * This is possible if the metadata thread has issue a delete because
665 * the endpoint point of the stream hung up. There is no way the
666 * session daemon can know about it thus use a DBG instead of an actual
667 * error.
668 */
669 DBG("UST consumer close metadata %" PRIu64 " not found", chan_key);
d88aee68
DG
670 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
671 goto error;
672 }
673
ea88ca2a 674 pthread_mutex_lock(&consumer_data.lock);
72c3879d 675 pthread_mutex_lock(&channel->lock);
73811ecc
DG
676
677 if (cds_lfht_is_node_deleted(&channel->node.node)) {
678 goto error_unlock;
679 }
680
681 if (channel->switch_timer_enabled == 1) {
682 DBG("Deleting timer on metadata channel");
683 consumer_timer_switch_stop(channel);
684 }
685
686 if (channel->metadata_stream) {
ea88ca2a
MD
687 ret = ustctl_stream_close_wakeup_fd(channel->metadata_stream->ustream);
688 if (ret < 0) {
689 ERR("UST consumer unable to close fd of metadata (ret: %d)", ret);
690 ret = LTTCOMM_CONSUMERD_ERROR_METADATA;
691 goto error_unlock;
692 }
331744e3 693 }
d88aee68 694
ea88ca2a 695error_unlock:
72c3879d 696 pthread_mutex_unlock(&channel->lock);
ea88ca2a 697 pthread_mutex_unlock(&consumer_data.lock);
d88aee68
DG
698error:
699 return ret;
700}
701
702/*
703 * RCU read side lock MUST be acquired before calling this function.
704 *
705 * Return 0 on success else an LTTng error code.
706 */
707static int setup_metadata(struct lttng_consumer_local_data *ctx, uint64_t key)
708{
709 int ret;
710 struct lttng_consumer_channel *metadata;
711
8fd623e0 712 DBG("UST consumer setup metadata key %" PRIu64, key);
d88aee68
DG
713
714 metadata = consumer_find_channel(key);
715 if (!metadata) {
716 ERR("UST consumer push metadata %" PRIu64 " not found", key);
717 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
f2a444f1 718 goto error_find;
d88aee68
DG
719 }
720
721 /*
722 * Send metadata stream to relayd if one available. Availability is
723 * known if the stream is still in the list of the channel.
724 */
725 if (cds_list_empty(&metadata->streams.head)) {
726 ERR("Metadata channel key %" PRIu64 ", no stream available.", key);
727 ret = LTTCOMM_CONSUMERD_ERROR_METADATA;
728 goto error;
729 }
730
731 /* Send metadata stream to relayd if needed. */
732 ret = send_stream_to_relayd(metadata->metadata_stream);
733 if (ret < 0) {
734 ret = LTTCOMM_CONSUMERD_ERROR_METADATA;
735 goto error;
736 }
737
738 ret = send_streams_to_thread(metadata, ctx);
739 if (ret < 0) {
740 /*
741 * If we are unable to send the stream to the thread, there is
742 * a big problem so just stop everything.
743 */
744 ret = LTTCOMM_CONSUMERD_FATAL;
745 goto error;
746 }
747 /* List MUST be empty after or else it could be reused. */
748 assert(cds_list_empty(&metadata->streams.head));
749
f2a444f1 750 return 0;
d88aee68
DG
751
752error:
f2a444f1
DG
753 /*
754 * Delete metadata channel on error. At this point, the metadata stream can
755 * NOT be monitored by the metadata thread thus having the guarantee that
756 * the stream is still in the local stream list of the channel. This call
757 * will make sure to clean that list.
758 */
759 consumer_del_channel(metadata);
760error_find:
d88aee68
DG
761 return ret;
762}
763
331744e3
JD
764/*
765 * Receive the metadata updates from the sessiond.
766 */
767int lttng_ustconsumer_recv_metadata(int sock, uint64_t key, uint64_t offset,
f0e1c785
MD
768 uint64_t len, struct lttng_consumer_channel *channel,
769 int timer)
331744e3
JD
770{
771 int ret, ret_code = LTTNG_OK;
772 char *metadata_str;
773
8fd623e0 774 DBG("UST consumer push metadata key %" PRIu64 " of len %" PRIu64, key, len);
331744e3
JD
775
776 metadata_str = zmalloc(len * sizeof(char));
777 if (!metadata_str) {
778 PERROR("zmalloc metadata string");
779 ret_code = LTTCOMM_CONSUMERD_ENOMEM;
780 goto end;
781 }
782
783 /* Receive metadata string. */
784 ret = lttcomm_recv_unix_sock(sock, metadata_str, len);
785 if (ret < 0) {
786 /* Session daemon is dead so return gracefully. */
787 ret_code = ret;
788 goto end_free;
789 }
790
f0e1c785
MD
791 if (!timer) {
792 pthread_mutex_lock(&channel->lock);
793 }
6ca1a6b0 794 pthread_mutex_lock(&channel->timer_lock);
331744e3
JD
795 pthread_mutex_lock(&channel->metadata_cache->lock);
796 ret = consumer_metadata_cache_write(channel, offset, len, metadata_str);
797 if (ret < 0) {
798 /* Unable to handle metadata. Notify session daemon. */
799 ret_code = LTTCOMM_CONSUMERD_ERROR_METADATA;
a32bd775
DG
800 /*
801 * Skip metadata flush on write error since the offset and len might
802 * not have been updated which could create an infinite loop below when
803 * waiting for the metadata cache to be flushed.
804 */
805 pthread_mutex_unlock(&channel->metadata_cache->lock);
6ca1a6b0 806 pthread_mutex_unlock(&channel->timer_lock);
f0e1c785
MD
807 if (!timer) {
808 pthread_mutex_unlock(&channel->lock);
809 }
a32bd775 810 goto end_free;
331744e3
JD
811 }
812 pthread_mutex_unlock(&channel->metadata_cache->lock);
6ca1a6b0 813 pthread_mutex_unlock(&channel->timer_lock);
f0e1c785
MD
814 if (!timer) {
815 pthread_mutex_unlock(&channel->lock);
816 }
331744e3 817
f0e1c785 818 while (consumer_metadata_cache_flushed(channel, offset + len, timer)) {
331744e3
JD
819 DBG("Waiting for metadata to be flushed");
820 usleep(DEFAULT_METADATA_AVAILABILITY_WAIT_TIME);
821 }
822
823end_free:
824 free(metadata_str);
825end:
826 return ret_code;
827}
828
4cbc1a04
DG
829/*
830 * Receive command from session daemon and process it.
831 *
832 * Return 1 on success else a negative value or 0.
833 */
3bd1e081
MD
834int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
835 int sock, struct pollfd *consumer_sockpoll)
836{
837 ssize_t ret;
f50f23d9 838 enum lttng_error_code ret_code = LTTNG_OK;
3bd1e081 839 struct lttcomm_consumer_msg msg;
ffe60014 840 struct lttng_consumer_channel *channel = NULL;
3bd1e081
MD
841
842 ret = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg));
843 if (ret != sizeof(msg)) {
173af62f
DG
844 DBG("Consumer received unexpected message size %zd (expects %zu)",
845 ret, sizeof(msg));
3be74084
DG
846 /*
847 * The ret value might 0 meaning an orderly shutdown but this is ok
848 * since the caller handles this.
849 */
489f70e9 850 if (ret > 0) {
563da294 851 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_CMD);
489f70e9
MD
852 ret = -1;
853 }
3bd1e081
MD
854 return ret;
855 }
856 if (msg.cmd_type == LTTNG_CONSUMER_STOP) {
f50f23d9
DG
857 /*
858 * Notify the session daemon that the command is completed.
859 *
860 * On transport layer error, the function call will print an error
861 * message so handling the returned code is a bit useless since we
862 * return an error code anyway.
863 */
864 (void) consumer_send_status_msg(sock, ret_code);
3bd1e081
MD
865 return -ENOENT;
866 }
867
3f8e211f 868 /* relayd needs RCU read-side lock */
b0b335c8
MD
869 rcu_read_lock();
870
3bd1e081 871 switch (msg.cmd_type) {
00e2e675
DG
872 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET:
873 {
f50f23d9 874 /* Session daemon status message are handled in the following call. */
7735ef9e
DG
875 ret = consumer_add_relayd_socket(msg.u.relayd_sock.net_index,
876 msg.u.relayd_sock.type, ctx, sock, consumer_sockpoll,
46e6455f 877 &msg.u.relayd_sock.sock, msg.u.relayd_sock.session_id);
00e2e675
DG
878 goto end_nosignal;
879 }
173af62f
DG
880 case LTTNG_CONSUMER_DESTROY_RELAYD:
881 {
a6ba4fe1 882 uint64_t index = msg.u.destroy_relayd.net_seq_idx;
173af62f
DG
883 struct consumer_relayd_sock_pair *relayd;
884
a6ba4fe1 885 DBG("UST consumer destroying relayd %" PRIu64, index);
173af62f
DG
886
887 /* Get relayd reference if exists. */
a6ba4fe1 888 relayd = consumer_find_relayd(index);
173af62f 889 if (relayd == NULL) {
3448e266 890 DBG("Unable to find relayd %" PRIu64, index);
f50f23d9 891 ret_code = LTTNG_ERR_NO_CONSUMER;
173af62f
DG
892 }
893
a6ba4fe1
DG
894 /*
895 * Each relayd socket pair has a refcount of stream attached to it
896 * which tells if the relayd is still active or not depending on the
897 * refcount value.
898 *
899 * This will set the destroy flag of the relayd object and destroy it
900 * if the refcount reaches zero when called.
901 *
902 * The destroy can happen either here or when a stream fd hangs up.
903 */
f50f23d9
DG
904 if (relayd) {
905 consumer_flag_relayd_for_destroy(relayd);
906 }
907
d88aee68 908 goto end_msg_sessiond;
173af62f 909 }
3bd1e081
MD
910 case LTTNG_CONSUMER_UPDATE_STREAM:
911 {
3f8e211f 912 rcu_read_unlock();
7ad0a0cb 913 return -ENOSYS;
3bd1e081 914 }
6d805429 915 case LTTNG_CONSUMER_DATA_PENDING:
53632229 916 {
3be74084 917 int ret, is_data_pending;
6d805429 918 uint64_t id = msg.u.data_pending.session_id;
ca22feea 919
6d805429 920 DBG("UST consumer data pending command for id %" PRIu64, id);
ca22feea 921
3be74084 922 is_data_pending = consumer_data_pending(id);
ca22feea
DG
923
924 /* Send back returned value to session daemon */
3be74084
DG
925 ret = lttcomm_send_unix_sock(sock, &is_data_pending,
926 sizeof(is_data_pending));
ca22feea 927 if (ret < 0) {
3be74084 928 DBG("Error when sending the data pending ret code: %d", ret);
489f70e9 929 goto error_fatal;
ca22feea 930 }
f50f23d9
DG
931
932 /*
933 * No need to send back a status message since the data pending
934 * returned value is the response.
935 */
ca22feea 936 break;
53632229 937 }
ffe60014
DG
938 case LTTNG_CONSUMER_ASK_CHANNEL_CREATION:
939 {
940 int ret;
941 struct ustctl_consumer_channel_attr attr;
942
943 /* Create a plain object and reserve a channel key. */
944 channel = allocate_channel(msg.u.ask_channel.session_id,
945 msg.u.ask_channel.pathname, msg.u.ask_channel.name,
946 msg.u.ask_channel.uid, msg.u.ask_channel.gid,
947 msg.u.ask_channel.relayd_id, msg.u.ask_channel.key,
1624d5b7
JD
948 (enum lttng_event_output) msg.u.ask_channel.output,
949 msg.u.ask_channel.tracefile_size,
cb7c6909
JD
950 msg.u.ask_channel.tracefile_count,
951 msg.u.ask_channel.session_id_per_pid);
ffe60014
DG
952 if (!channel) {
953 goto end_channel_error;
954 }
955
956 /* Build channel attributes from received message. */
957 attr.subbuf_size = msg.u.ask_channel.subbuf_size;
958 attr.num_subbuf = msg.u.ask_channel.num_subbuf;
959 attr.overwrite = msg.u.ask_channel.overwrite;
960 attr.switch_timer_interval = msg.u.ask_channel.switch_timer_interval;
961 attr.read_timer_interval = msg.u.ask_channel.read_timer_interval;
7972aab2 962 attr.chan_id = msg.u.ask_channel.chan_id;
ffe60014
DG
963 memcpy(attr.uuid, msg.u.ask_channel.uuid, sizeof(attr.uuid));
964
965 /* Translate the event output type to UST. */
966 switch (channel->output) {
967 case LTTNG_EVENT_SPLICE:
968 /* Splice not supported so fallback on mmap(). */
969 case LTTNG_EVENT_MMAP:
970 default:
971 attr.output = CONSUMER_CHANNEL_MMAP;
972 break;
973 };
974
975 /* Translate and save channel type. */
976 switch (msg.u.ask_channel.type) {
977 case LTTNG_UST_CHAN_PER_CPU:
978 channel->type = CONSUMER_CHANNEL_TYPE_DATA;
979 attr.type = LTTNG_UST_CHAN_PER_CPU;
8633d6e3
MD
980 /*
981 * Set refcount to 1 for owner. Below, we will
982 * pass ownership to the
983 * consumer_thread_channel_poll() thread.
984 */
985 channel->refcount = 1;
ffe60014
DG
986 break;
987 case LTTNG_UST_CHAN_METADATA:
988 channel->type = CONSUMER_CHANNEL_TYPE_METADATA;
989 attr.type = LTTNG_UST_CHAN_METADATA;
990 break;
991 default:
992 assert(0);
993 goto error_fatal;
994 };
995
996 ret = ask_channel(ctx, sock, channel, &attr);
997 if (ret < 0) {
998 goto end_channel_error;
999 }
1000
fc643247
MD
1001 if (msg.u.ask_channel.type == LTTNG_UST_CHAN_METADATA) {
1002 ret = consumer_metadata_cache_allocate(channel);
1003 if (ret < 0) {
1004 ERR("Allocating metadata cache");
1005 goto end_channel_error;
1006 }
1007 consumer_timer_switch_start(channel, attr.switch_timer_interval);
1008 attr.switch_timer_interval = 0;
1009 }
1010
ffe60014
DG
1011 /*
1012 * Add the channel to the internal state AFTER all streams were created
1013 * and successfully sent to session daemon. This way, all streams must
1014 * be ready before this channel is visible to the threads.
fc643247
MD
1015 * If add_channel succeeds, ownership of the channel is
1016 * passed to consumer_thread_channel_poll().
ffe60014
DG
1017 */
1018 ret = add_channel(channel, ctx);
1019 if (ret < 0) {
ea88ca2a
MD
1020 if (msg.u.ask_channel.type == LTTNG_UST_CHAN_METADATA) {
1021 if (channel->switch_timer_enabled == 1) {
1022 consumer_timer_switch_stop(channel);
1023 }
1024 consumer_metadata_cache_destroy(channel);
1025 }
ffe60014
DG
1026 goto end_channel_error;
1027 }
1028
1029 /*
1030 * Channel and streams are now created. Inform the session daemon that
1031 * everything went well and should wait to receive the channel and
1032 * streams with ustctl API.
1033 */
1034 ret = consumer_send_status_channel(sock, channel);
1035 if (ret < 0) {
1036 /*
489f70e9 1037 * There is probably a problem on the socket.
ffe60014 1038 */
489f70e9 1039 goto error_fatal;
ffe60014
DG
1040 }
1041
1042 break;
1043 }
1044 case LTTNG_CONSUMER_GET_CHANNEL:
1045 {
1046 int ret, relayd_err = 0;
d88aee68 1047 uint64_t key = msg.u.get_channel.key;
ffe60014 1048 struct lttng_consumer_channel *channel;
ffe60014
DG
1049
1050 channel = consumer_find_channel(key);
1051 if (!channel) {
8fd623e0 1052 ERR("UST consumer get channel key %" PRIu64 " not found", key);
ffe60014
DG
1053 ret_code = LTTNG_ERR_UST_CHAN_NOT_FOUND;
1054 goto end_msg_sessiond;
1055 }
1056
ffe60014
DG
1057 /* Send everything to sessiond. */
1058 ret = send_sessiond_channel(sock, channel, ctx, &relayd_err);
1059 if (ret < 0) {
1060 if (relayd_err) {
1061 /*
1062 * We were unable to send to the relayd the stream so avoid
1063 * sending back a fatal error to the thread since this is OK
f2a444f1
DG
1064 * and the consumer can continue its work. The above call
1065 * has sent the error status message to the sessiond.
ffe60014 1066 */
f2a444f1 1067 goto end_nosignal;
ffe60014
DG
1068 }
1069 /*
1070 * The communicaton was broken hence there is a bad state between
1071 * the consumer and sessiond so stop everything.
1072 */
1073 goto error_fatal;
1074 }
1075
d88aee68
DG
1076 ret = send_streams_to_thread(channel, ctx);
1077 if (ret < 0) {
1078 /*
1079 * If we are unable to send the stream to the thread, there is
1080 * a big problem so just stop everything.
1081 */
1082 goto error_fatal;
ffe60014 1083 }
ffe60014
DG
1084 /* List MUST be empty after or else it could be reused. */
1085 assert(cds_list_empty(&channel->streams.head));
1086
d88aee68
DG
1087 goto end_msg_sessiond;
1088 }
1089 case LTTNG_CONSUMER_DESTROY_CHANNEL:
1090 {
1091 uint64_t key = msg.u.destroy_channel.key;
d88aee68 1092
a0cbdd2e
MD
1093 /*
1094 * Only called if streams have not been sent to stream
1095 * manager thread. However, channel has been sent to
1096 * channel manager thread.
1097 */
1098 notify_thread_del_channel(ctx, key);
d88aee68 1099 goto end_msg_sessiond;
ffe60014 1100 }
d88aee68
DG
1101 case LTTNG_CONSUMER_CLOSE_METADATA:
1102 {
1103 int ret;
1104
1105 ret = close_metadata(msg.u.close_metadata.key);
1106 if (ret != 0) {
1107 ret_code = ret;
1108 }
1109
1110 goto end_msg_sessiond;
1111 }
7972aab2
DG
1112 case LTTNG_CONSUMER_FLUSH_CHANNEL:
1113 {
1114 int ret;
1115
1116 ret = flush_channel(msg.u.flush_channel.key);
1117 if (ret != 0) {
1118 ret_code = ret;
1119 }
1120
1121 goto end_msg_sessiond;
1122 }
d88aee68 1123 case LTTNG_CONSUMER_PUSH_METADATA:
ffe60014
DG
1124 {
1125 int ret;
d88aee68 1126 uint64_t len = msg.u.push_metadata.len;
d88aee68 1127 uint64_t key = msg.u.push_metadata.key;
331744e3 1128 uint64_t offset = msg.u.push_metadata.target_offset;
ffe60014
DG
1129 struct lttng_consumer_channel *channel;
1130
8fd623e0
DG
1131 DBG("UST consumer push metadata key %" PRIu64 " of len %" PRIu64, key,
1132 len);
ffe60014
DG
1133
1134 channel = consumer_find_channel(key);
1135 if (!channel) {
890d27f4
DG
1136 /*
1137 * This is possible if the metadata creation on the consumer side
1138 * is in flight vis-a-vis a concurrent push metadata from the
1139 * session daemon. Simply return that the channel failed and the
1140 * session daemon will handle that message correctly considering
1141 * that this race is acceptable thus the DBG() statement here.
1142 */
1143 DBG("UST consumer push metadata %" PRIu64 " not found", key);
1144 ret_code = LTTCOMM_CONSUMERD_CHANNEL_FAIL;
4a2eb0ca 1145 goto end_msg_sessiond;
d88aee68
DG
1146 }
1147
1148 /* Tell session daemon we are ready to receive the metadata. */
ffe60014
DG
1149 ret = consumer_send_status_msg(sock, LTTNG_OK);
1150 if (ret < 0) {
1151 /* Somehow, the session daemon is not responding anymore. */
d88aee68
DG
1152 goto error_fatal;
1153 }
1154
1155 /* Wait for more data. */
1156 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
489f70e9 1157 goto error_fatal;
d88aee68
DG
1158 }
1159
331744e3 1160 ret = lttng_ustconsumer_recv_metadata(sock, key, offset,
f0e1c785 1161 len, channel, 0);
d88aee68 1162 if (ret < 0) {
331744e3 1163 /* error receiving from sessiond */
489f70e9 1164 goto error_fatal;
331744e3
JD
1165 } else {
1166 ret_code = ret;
d88aee68
DG
1167 goto end_msg_sessiond;
1168 }
d88aee68
DG
1169 }
1170 case LTTNG_CONSUMER_SETUP_METADATA:
1171 {
1172 int ret;
1173
1174 ret = setup_metadata(ctx, msg.u.setup_metadata.key);
1175 if (ret) {
1176 ret_code = ret;
1177 }
1178 goto end_msg_sessiond;
ffe60014 1179 }
3bd1e081
MD
1180 default:
1181 break;
1182 }
3f8e211f 1183
3bd1e081 1184end_nosignal:
b0b335c8 1185 rcu_read_unlock();
4cbc1a04
DG
1186
1187 /*
1188 * Return 1 to indicate success since the 0 value can be a socket
1189 * shutdown during the recv() or send() call.
1190 */
1191 return 1;
ffe60014
DG
1192
1193end_msg_sessiond:
1194 /*
1195 * The returned value here is not useful since either way we'll return 1 to
1196 * the caller because the session daemon socket management is done
1197 * elsewhere. Returning a negative code or 0 will shutdown the consumer.
1198 */
489f70e9
MD
1199 ret = consumer_send_status_msg(sock, ret_code);
1200 if (ret < 0) {
1201 goto error_fatal;
1202 }
ffe60014
DG
1203 rcu_read_unlock();
1204 return 1;
1205end_channel_error:
1206 if (channel) {
1207 /*
1208 * Free channel here since no one has a reference to it. We don't
1209 * free after that because a stream can store this pointer.
1210 */
1211 destroy_channel(channel);
1212 }
1213 /* We have to send a status channel message indicating an error. */
1214 ret = consumer_send_status_channel(sock, NULL);
1215 if (ret < 0) {
1216 /* Stop everything if session daemon can not be notified. */
1217 goto error_fatal;
1218 }
1219 rcu_read_unlock();
1220 return 1;
1221error_fatal:
1222 rcu_read_unlock();
1223 /* This will issue a consumer stop. */
1224 return -1;
3bd1e081
MD
1225}
1226
ffe60014
DG
1227/*
1228 * Wrapper over the mmap() read offset from ust-ctl library. Since this can be
1229 * compiled out, we isolate it in this library.
1230 */
1231int lttng_ustctl_get_mmap_read_offset(struct lttng_consumer_stream *stream,
1232 unsigned long *off)
3bd1e081 1233{
ffe60014
DG
1234 assert(stream);
1235 assert(stream->ustream);
b5c5fc29 1236
ffe60014 1237 return ustctl_get_mmap_read_offset(stream->ustream, off);
3bd1e081
MD
1238}
1239
ffe60014
DG
1240/*
1241 * Wrapper over the mmap() read offset from ust-ctl library. Since this can be
1242 * compiled out, we isolate it in this library.
1243 */
1244void *lttng_ustctl_get_mmap_base(struct lttng_consumer_stream *stream)
d056b477 1245{
ffe60014
DG
1246 assert(stream);
1247 assert(stream->ustream);
1248
1249 return ustctl_get_mmap_base(stream->ustream);
d056b477
MD
1250}
1251
ffe60014
DG
1252/*
1253 * Take a snapshot for a specific fd
1254 *
1255 * Returns 0 on success, < 0 on error
1256 */
1257int lttng_ustconsumer_take_snapshot(struct lttng_consumer_stream *stream)
3bd1e081 1258{
ffe60014
DG
1259 assert(stream);
1260 assert(stream->ustream);
1261
1262 return ustctl_snapshot(stream->ustream);
3bd1e081
MD
1263}
1264
ffe60014
DG
1265/*
1266 * Get the produced position
1267 *
1268 * Returns 0 on success, < 0 on error
1269 */
1270int lttng_ustconsumer_get_produced_snapshot(
1271 struct lttng_consumer_stream *stream, unsigned long *pos)
3bd1e081 1272{
ffe60014
DG
1273 assert(stream);
1274 assert(stream->ustream);
1275 assert(pos);
7a57cf92 1276
ffe60014
DG
1277 return ustctl_snapshot_get_produced(stream->ustream, pos);
1278}
7a57cf92 1279
ffe60014
DG
1280/*
1281 * Called when the stream signal the consumer that it has hang up.
1282 */
1283void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream *stream)
1284{
1285 assert(stream);
1286 assert(stream->ustream);
2c1dd183 1287
ffe60014
DG
1288 ustctl_flush_buffer(stream->ustream, 0);
1289 stream->hangup_flush_done = 1;
1290}
ee77a7b0 1291
ffe60014
DG
1292void lttng_ustconsumer_del_channel(struct lttng_consumer_channel *chan)
1293{
1294 assert(chan);
1295 assert(chan->uchan);
e316aad5 1296
ea88ca2a
MD
1297 if (chan->switch_timer_enabled == 1) {
1298 consumer_timer_switch_stop(chan);
1299 }
1300 consumer_metadata_cache_destroy(chan);
ffe60014 1301 ustctl_destroy_channel(chan->uchan);
3bd1e081
MD
1302}
1303
1304void lttng_ustconsumer_del_stream(struct lttng_consumer_stream *stream)
1305{
ffe60014
DG
1306 assert(stream);
1307 assert(stream->ustream);
d41f73b7 1308
ea88ca2a
MD
1309 if (stream->chan->switch_timer_enabled == 1) {
1310 consumer_timer_switch_stop(stream->chan);
1311 }
ffe60014
DG
1312 ustctl_destroy_stream(stream->ustream);
1313}
d41f73b7
MD
1314
1315int lttng_ustconsumer_read_subbuffer(struct lttng_consumer_stream *stream,
1316 struct lttng_consumer_local_data *ctx)
1317{
1d4dfdef 1318 unsigned long len, subbuf_size, padding;
d41f73b7
MD
1319 int err;
1320 long ret = 0;
d41f73b7 1321 char dummy;
ffe60014
DG
1322 struct ustctl_consumer_stream *ustream;
1323
1324 assert(stream);
1325 assert(stream->ustream);
1326 assert(ctx);
d41f73b7 1327
7e448fde 1328 DBG("In UST read_subbuffer (wait_fd: %d, name: %s)", stream->wait_fd,
ffe60014
DG
1329 stream->name);
1330
1331 /* Ease our life for what's next. */
1332 ustream = stream->ustream;
d41f73b7
MD
1333
1334 /* We can consume the 1 byte written into the wait_fd by UST */
effcf122 1335 if (!stream->hangup_flush_done) {
c617c0c6
MD
1336 ssize_t readlen;
1337
effcf122
MD
1338 do {
1339 readlen = read(stream->wait_fd, &dummy, 1);
87dc6a9c 1340 } while (readlen == -1 && errno == EINTR);
effcf122
MD
1341 if (readlen == -1) {
1342 ret = readlen;
1343 goto end;
1344 }
d41f73b7
MD
1345 }
1346
d41f73b7 1347 /* Get the next subbuffer */
ffe60014 1348 err = ustctl_get_next_subbuf(ustream);
d41f73b7 1349 if (err != 0) {
1d4dfdef 1350 ret = err; /* ustctl_get_next_subbuf returns negative, caller expect positive. */
d41f73b7
MD
1351 /*
1352 * This is a debug message even for single-threaded consumer,
1353 * because poll() have more relaxed criterions than get subbuf,
1354 * so get_subbuf may fail for short race windows where poll()
1355 * would issue wakeups.
1356 */
1357 DBG("Reserving sub buffer failed (everything is normal, "
ffe60014 1358 "it is due to concurrency) [ret: %d]", err);
d41f73b7
MD
1359 goto end;
1360 }
ffe60014 1361 assert(stream->chan->output == CONSUMER_CHANNEL_MMAP);
1d4dfdef 1362 /* Get the full padded subbuffer size */
ffe60014 1363 err = ustctl_get_padded_subbuf_size(ustream, &len);
effcf122 1364 assert(err == 0);
1d4dfdef
DG
1365
1366 /* Get subbuffer data size (without padding) */
ffe60014 1367 err = ustctl_get_subbuf_size(ustream, &subbuf_size);
1d4dfdef
DG
1368 assert(err == 0);
1369
1370 /* Make sure we don't get a subbuffer size bigger than the padded */
1371 assert(len >= subbuf_size);
1372
1373 padding = len - subbuf_size;
d41f73b7 1374 /* write the subbuffer to the tracefile */
1d4dfdef 1375 ret = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, subbuf_size, padding);
91dfef6e
DG
1376 /*
1377 * The mmap operation should write subbuf_size amount of data when network
1378 * streaming or the full padding (len) size when we are _not_ streaming.
1379 */
d88aee68
DG
1380 if ((ret != subbuf_size && stream->net_seq_idx != (uint64_t) -1ULL) ||
1381 (ret != len && stream->net_seq_idx == (uint64_t) -1ULL)) {
d41f73b7 1382 /*
91dfef6e 1383 * Display the error but continue processing to try to release the
c5c45efa
DG
1384 * subbuffer. This is a DBG statement since any unexpected kill or
1385 * signal, the application gets unregistered, relayd gets closed or
1386 * anything that affects the buffer lifetime will trigger this error.
1387 * So, for the sake of the user, don't print this error since it can
1388 * happen and it is OK with the code flow.
d41f73b7 1389 */
c5c45efa 1390 DBG("Error writing to tracefile "
8fd623e0 1391 "(ret: %ld != len: %lu != subbuf_size: %lu)",
91dfef6e 1392 ret, len, subbuf_size);
d41f73b7 1393 }
ffe60014 1394 err = ustctl_put_next_subbuf(ustream);
effcf122 1395 assert(err == 0);
331744e3 1396
d41f73b7
MD
1397end:
1398 return ret;
1399}
1400
ffe60014
DG
1401/*
1402 * Called when a stream is created.
fe4477ee
JD
1403 *
1404 * Return 0 on success or else a negative value.
ffe60014 1405 */
d41f73b7
MD
1406int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream *stream)
1407{
fe4477ee
JD
1408 int ret;
1409
1410 /* Don't create anything if this is set for streaming. */
1411 if (stream->net_seq_idx == (uint64_t) -1ULL) {
1412 ret = utils_create_stream_file(stream->chan->pathname, stream->name,
1413 stream->chan->tracefile_size, stream->tracefile_count_current,
1414 stream->uid, stream->gid);
1415 if (ret < 0) {
1416 goto error;
1417 }
1418 stream->out_fd = ret;
1419 stream->tracefile_size_current = 0;
1420 }
1421 ret = 0;
1422
1423error:
1424 return ret;
d41f73b7 1425}
ca22feea
DG
1426
1427/*
1428 * Check if data is still being extracted from the buffers for a specific
4e9a4686
DG
1429 * stream. Consumer data lock MUST be acquired before calling this function
1430 * and the stream lock.
ca22feea 1431 *
6d805429 1432 * Return 1 if the traced data are still getting read else 0 meaning that the
ca22feea
DG
1433 * data is available for trace viewer reading.
1434 */
6d805429 1435int lttng_ustconsumer_data_pending(struct lttng_consumer_stream *stream)
ca22feea
DG
1436{
1437 int ret;
1438
1439 assert(stream);
ffe60014 1440 assert(stream->ustream);
ca22feea 1441
6d805429 1442 DBG("UST consumer checking data pending");
c8f59ee5 1443
0b0e08f7
MD
1444 if (stream->endpoint_status != CONSUMER_ENDPOINT_ACTIVE) {
1445 ret = 0;
1446 goto end;
1447 }
1448
ffe60014 1449 ret = ustctl_get_next_subbuf(stream->ustream);
ca22feea
DG
1450 if (ret == 0) {
1451 /* There is still data so let's put back this subbuffer. */
ffe60014 1452 ret = ustctl_put_subbuf(stream->ustream);
ca22feea 1453 assert(ret == 0);
6d805429 1454 ret = 1; /* Data is pending */
4e9a4686 1455 goto end;
ca22feea
DG
1456 }
1457
6d805429
DG
1458 /* Data is NOT pending so ready to be read. */
1459 ret = 0;
ca22feea 1460
6efae65e
DG
1461end:
1462 return ret;
ca22feea 1463}
d88aee68
DG
1464
1465/*
1466 * Close every metadata stream wait fd of the metadata hash table. This
1467 * function MUST be used very carefully so not to run into a race between the
1468 * metadata thread handling streams and this function closing their wait fd.
1469 *
1470 * For UST, this is used when the session daemon hangs up. Its the metadata
1471 * producer so calling this is safe because we are assured that no state change
1472 * can occur in the metadata thread for the streams in the hash table.
1473 */
1474void lttng_ustconsumer_close_metadata(struct lttng_ht *metadata_ht)
1475{
1476 int ret;
1477 struct lttng_ht_iter iter;
1478 struct lttng_consumer_stream *stream;
1479
1480 assert(metadata_ht);
1481 assert(metadata_ht->ht);
1482
1483 DBG("UST consumer closing all metadata streams");
1484
1485 rcu_read_lock();
1486 cds_lfht_for_each_entry(metadata_ht->ht, &iter.iter, stream,
1487 node.node) {
1488 int fd = stream->wait_fd;
1489
1490 /*
1491 * Whatever happens here we have to continue to try to close every
1492 * streams. Let's report at least the error on failure.
1493 */
1494 ret = ustctl_stream_close_wakeup_fd(stream->ustream);
1495 if (ret) {
1496 ERR("Unable to close metadata stream fd %d ret %d", fd, ret);
1497 }
1498 DBG("Metadata wait fd %d closed", fd);
1499 }
1500 rcu_read_unlock();
1501}
d8ef542d
MD
1502
1503void lttng_ustconsumer_close_stream_wakeup(struct lttng_consumer_stream *stream)
1504{
1505 int ret;
1506
1507 ret = ustctl_stream_close_wakeup_fd(stream->ustream);
1508 if (ret < 0) {
1509 ERR("Unable to close wakeup fd");
1510 }
1511}
331744e3 1512
b011a087
MD
1513/*
1514 * Please refer to consumer-timer.c before adding any lock within this
1515 * function or any of its callees. Timers have a very strict locking
1516 * semantic with respect to teardown. Failure to respect this semantic
1517 * introduces deadlocks.
1518 */
331744e3 1519int lttng_ustconsumer_request_metadata(struct lttng_consumer_local_data *ctx,
f0e1c785 1520 struct lttng_consumer_channel *channel, int timer)
331744e3
JD
1521{
1522 struct lttcomm_metadata_request_msg request;
1523 struct lttcomm_consumer_msg msg;
1524 enum lttng_error_code ret_code = LTTNG_OK;
1525 uint64_t len, key, offset;
1526 int ret;
1527
1528 assert(channel);
1529 assert(channel->metadata_cache);
1530
1531 /* send the metadata request to sessiond */
1532 switch (consumer_data.type) {
1533 case LTTNG_CONSUMER64_UST:
1534 request.bits_per_long = 64;
1535 break;
1536 case LTTNG_CONSUMER32_UST:
1537 request.bits_per_long = 32;
1538 break;
1539 default:
1540 request.bits_per_long = 0;
1541 break;
1542 }
1543
1544 request.session_id = channel->session_id;
cb7c6909 1545 request.session_id_per_pid = channel->session_id_per_pid;
331744e3
JD
1546 request.uid = channel->uid;
1547 request.key = channel->key;
cb7c6909
JD
1548 DBG("Sending metadata request to sessiond, session id %" PRIu64
1549 ", per-pid %" PRIu64,
1550 channel->session_id,
1551 channel->session_id_per_pid);
331744e3
JD
1552
1553 ret = lttcomm_send_unix_sock(ctx->consumer_metadata_socket, &request,
1554 sizeof(request));
1555 if (ret < 0) {
1556 ERR("Asking metadata to sessiond");
1557 goto end;
1558 }
1559
1560 /* Receive the metadata from sessiond */
1561 ret = lttcomm_recv_unix_sock(ctx->consumer_metadata_socket, &msg,
1562 sizeof(msg));
1563 if (ret != sizeof(msg)) {
8fd623e0 1564 DBG("Consumer received unexpected message size %d (expects %zu)",
331744e3
JD
1565 ret, sizeof(msg));
1566 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_CMD);
1567 /*
1568 * The ret value might 0 meaning an orderly shutdown but this is ok
1569 * since the caller handles this.
1570 */
1571 goto end;
1572 }
1573
1574 if (msg.cmd_type == LTTNG_ERR_UND) {
1575 /* No registry found */
1576 (void) consumer_send_status_msg(ctx->consumer_metadata_socket,
1577 ret_code);
1578 ret = 0;
1579 goto end;
1580 } else if (msg.cmd_type != LTTNG_CONSUMER_PUSH_METADATA) {
1581 ERR("Unexpected cmd_type received %d", msg.cmd_type);
1582 ret = -1;
1583 goto end;
1584 }
1585
1586 len = msg.u.push_metadata.len;
1587 key = msg.u.push_metadata.key;
1588 offset = msg.u.push_metadata.target_offset;
1589
1590 assert(key == channel->key);
1591 if (len == 0) {
1592 DBG("No new metadata to receive for key %" PRIu64, key);
1593 }
1594
1595 /* Tell session daemon we are ready to receive the metadata. */
1596 ret = consumer_send_status_msg(ctx->consumer_metadata_socket,
1597 LTTNG_OK);
1598 if (ret < 0 || len == 0) {
1599 /*
1600 * Somehow, the session daemon is not responding anymore or there is
1601 * nothing to receive.
1602 */
1603 goto end;
1604 }
1605
27cb7317 1606 ret = lttng_ustconsumer_recv_metadata(ctx->consumer_metadata_socket,
f0e1c785 1607 key, offset, len, channel, timer);
27cb7317 1608 if (ret >= 0) {
f2a444f1
DG
1609 /*
1610 * Only send the status msg if the sessiond is alive meaning a positive
1611 * ret code.
1612 */
27cb7317 1613 (void) consumer_send_status_msg(ctx->consumer_metadata_socket, ret);
f2a444f1 1614 }
331744e3
JD
1615 ret = 0;
1616
1617end:
1618 return ret;
1619}
This page took 0.114903 seconds and 4 git commands to generate.