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