Fix: comparison of unsigned enum expression >= 0
[lttng-tools.git] / src / common / ust-consumer / ust-consumer.c
1 /*
2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
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.
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 *
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.
17 */
18
19 #define _GNU_SOURCE
20 #include <assert.h>
21 #include <lttng/ust-ctl.h>
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>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <inttypes.h>
31 #include <unistd.h>
32 #include <urcu/list.h>
33 #include <signal.h>
34
35 #include <common/common.h>
36 #include <common/sessiond-comm/sessiond-comm.h>
37 #include <common/relayd/relayd.h>
38 #include <common/compat/fcntl.h>
39 #include <common/consumer-metadata-cache.h>
40 #include <common/consumer-timer.h>
41 #include <common/utils.h>
42
43 #include "ust-consumer.h"
44
45 extern struct lttng_consumer_global_data consumer_data;
46 extern int consumer_poll_timeout;
47 extern volatile int consumer_quit;
48
49 /*
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.
53 */
54 static void destroy_channel(struct lttng_consumer_channel *channel)
55 {
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 }
78
79 /*
80 * Add channel to internal consumer state.
81 *
82 * Returns 0 on success or else a negative value.
83 */
84 static int add_channel(struct lttng_consumer_channel *channel,
85 struct lttng_consumer_local_data *ctx)
86 {
87 int ret = 0;
88
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) {
95 ret = consumer_add_channel(channel, ctx);
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 {
102 ret = consumer_add_channel(channel, ctx);
103 }
104
105 DBG("UST consumer channel added (key: %" PRIu64 ")", channel->key);
106
107 error:
108 return ret;
109 }
110
111 /*
112 * Allocate and return a consumer channel object.
113 */
114 static struct lttng_consumer_channel *allocate_channel(uint64_t session_id,
115 const char *pathname, const char *name, uid_t uid, gid_t gid,
116 uint64_t relayd_id, uint64_t key, enum lttng_event_output output,
117 uint64_t tracefile_size, uint64_t tracefile_count,
118 uint64_t session_id_per_pid)
119 {
120 assert(pathname);
121 assert(name);
122
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);
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.
131 *
132 * Return NULL on error else the newly allocated stream object.
133 */
134 static 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
175 error:
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 */
187 static int send_stream_to_thread(struct lttng_consumer_stream *stream,
188 struct lttng_consumer_local_data *ctx)
189 {
190 int ret;
191 struct lttng_pipe *stream_pipe;
192
193 /* Get the right pipe where the stream will be sent. */
194 if (stream->metadata_flag) {
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 }
201 stream_pipe = ctx->consumer_metadata_pipe;
202 } else {
203 ret = consumer_add_data_stream(stream);
204 if (ret) {
205 ERR("Consumer add stream %" PRIu64 " failed.",
206 stream->key);
207 goto error;
208 }
209 stream_pipe = ctx->consumer_data_pipe;
210 }
211
212 ret = lttng_pipe_write(stream_pipe, &stream, sizeof(stream));
213 if (ret < 0) {
214 ERR("Consumer write %s stream to pipe %d",
215 stream->metadata_flag ? "metadata" : "data",
216 lttng_pipe_get_writefd(stream_pipe));
217 if (stream->metadata_flag) {
218 consumer_del_stream_for_metadata(stream);
219 } else {
220 consumer_del_stream_for_data(stream);
221 }
222 }
223 error:
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 */
233 static 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,
245 stream->chan->pathname, &stream->relayd_stream_id,
246 stream->chan->tracefile_size,
247 stream->chan->tracefile_count);
248 pthread_mutex_unlock(&relayd->ctrl_sock_mutex);
249 if (ret < 0) {
250 goto error;
251 }
252 } else if (stream->net_seq_idx != (uint64_t) -1ULL) {
253 ERR("Network sequence index %" PRIu64 " unknown. Not adding stream.",
254 stream->net_seq_idx);
255 ret = -1;
256 goto error;
257 }
258
259 error:
260 return ret;
261 }
262
263 /*
264 * Create streams for the given channel using liblttng-ust-ctl.
265 *
266 * Return 0 on success else a negative value.
267 */
268 static 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
285 wait_fd = ustctl_stream_get_wait_fd(ustream);
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
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
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
328 DBG("UST consumer add stream %s (key: %" PRIu64 ") with relayd id %" PRIu64,
329 stream->name, stream->key, stream->relayd_stream_id);
330
331 /* Set next CPU stream. */
332 channel->streams.count = ++cpu;
333
334 /* Keep stream reference when creating metadata. */
335 if (channel->type == CONSUMER_CHANNEL_TYPE_METADATA) {
336 channel->metadata_stream = stream;
337 }
338 }
339
340 return 0;
341
342 error:
343 error_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 */
353 static 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
379 error_create:
380 return ret;
381 }
382
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 */
388 static int send_sessiond_stream(int sock, struct lttng_consumer_stream *stream)
389 {
390 int ret;
391
392 assert(stream);
393 assert(sock >= 0);
394
395 DBG("UST consumer sending stream %" PRIu64 " to sessiond", stream->key);
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
403 error:
404 return ret;
405 }
406
407 /*
408 * Send channel to sessiond.
409 *
410 * Return 0 on success or else a negative value.
411 */
412 static int send_sessiond_channel(int sock,
413 struct lttng_consumer_channel *channel,
414 struct lttng_consumer_local_data *ctx, int *relayd_error)
415 {
416 int ret, ret_code = LTTNG_OK;
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
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 }
436 ret_code = LTTNG_ERR_RELAYD_CONNECT_FAIL;
437 }
438 }
439
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) {
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
480 error:
481 if (ret_code != LTTNG_OK) {
482 ret = -1;
483 }
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 */
495 static int ask_channel(struct lttng_consumer_local_data *ctx, int sock,
496 struct lttng_consumer_channel *channel,
497 struct ustctl_consumer_channel_attr *attr)
498 {
499 int ret;
500
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;
524 }
525
526 channel->wait_fd = ustctl_channel_get_wait_fd(channel->uchan);
527
528 /* Open all streams for this channel. */
529 ret = create_ust_streams(channel, ctx);
530 if (ret < 0) {
531 goto error;
532 }
533
534 error:
535 return ret;
536 }
537
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 */
543 static 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 */
562 /* Remove node from the channel stream list. */
563 cds_list_del(&stream->send_node);
564 goto error;
565 }
566
567 /* Remove node from the channel stream list. */
568 cds_list_del(&stream->send_node);
569 }
570
571 error:
572 return ret;
573 }
574
575 /*
576 * Write metadata to the given channel using ustctl to convert the string to
577 * the ringbuffer.
578 * Called only from consumer_metadata_cache_write.
579 * The metadata cache lock MUST be acquired to write in the cache.
580 *
581 * Return 0 on success else a negative value.
582 */
583 int lttng_ustconsumer_push_metadata(struct lttng_consumer_channel *metadata,
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
593 if (!metadata->metadata_stream) {
594 ret = 0;
595 goto error;
596 }
597
598 assert(target_offset <= metadata->metadata_cache->max_offset);
599 ret = ustctl_write_metadata_to_channel(metadata->uchan,
600 metadata_str + target_offset, len);
601 if (ret < 0) {
602 ERR("ustctl write metadata fail with ret %d, len %" PRIu64, ret, len);
603 goto error;
604 }
605
606 ustctl_flush_buffer(metadata->metadata_stream->ustream, 1);
607
608 error:
609 return ret;
610 }
611
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 */
617 static 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
625 DBG("UST consumer flush channel key %" PRIu64, chan_key);
626
627 rcu_read_lock();
628 channel = consumer_find_channel(chan_key);
629 if (!channel) {
630 ERR("UST consumer flush channel %" PRIu64 " not found", chan_key);
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. */
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) {
641 ustctl_flush_buffer(stream->ustream, 1);
642 }
643 error:
644 rcu_read_unlock();
645 return ret;
646 }
647
648 /*
649 * Close metadata stream wakeup_fd using the given key to retrieve the channel.
650 * RCU read side lock MUST be acquired before calling this function.
651 *
652 * Return 0 on success else an LTTng error code.
653 */
654 static int close_metadata(uint64_t chan_key)
655 {
656 int ret = 0;
657 struct lttng_consumer_channel *channel;
658
659 DBG("UST consumer close metadata key %" PRIu64, chan_key);
660
661 channel = consumer_find_channel(chan_key);
662 if (!channel) {
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);
670 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
671 goto error;
672 }
673
674 pthread_mutex_lock(&consumer_data.lock);
675 pthread_mutex_lock(&channel->lock);
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) {
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 }
693 }
694
695 error_unlock:
696 pthread_mutex_unlock(&channel->lock);
697 pthread_mutex_unlock(&consumer_data.lock);
698 error:
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 */
707 static int setup_metadata(struct lttng_consumer_local_data *ctx, uint64_t key)
708 {
709 int ret;
710 struct lttng_consumer_channel *metadata;
711
712 DBG("UST consumer setup metadata key %" PRIu64, key);
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;
718 goto error_find;
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
750 return 0;
751
752 error:
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);
760 error_find:
761 return ret;
762 }
763
764 /*
765 * Receive the metadata updates from the sessiond.
766 */
767 int lttng_ustconsumer_recv_metadata(int sock, uint64_t key, uint64_t offset,
768 uint64_t len, struct lttng_consumer_channel *channel,
769 int timer)
770 {
771 int ret, ret_code = LTTNG_OK;
772 char *metadata_str;
773
774 DBG("UST consumer push metadata key %" PRIu64 " of len %" PRIu64, key, len);
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
791 if (!timer) {
792 pthread_mutex_lock(&channel->lock);
793 }
794 pthread_mutex_lock(&channel->timer_lock);
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;
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);
806 pthread_mutex_unlock(&channel->timer_lock);
807 if (!timer) {
808 pthread_mutex_unlock(&channel->lock);
809 }
810 goto end_free;
811 }
812 pthread_mutex_unlock(&channel->metadata_cache->lock);
813 pthread_mutex_unlock(&channel->timer_lock);
814 if (!timer) {
815 pthread_mutex_unlock(&channel->lock);
816 }
817
818 while (consumer_metadata_cache_flushed(channel, offset + len, timer)) {
819 DBG("Waiting for metadata to be flushed");
820 usleep(DEFAULT_METADATA_AVAILABILITY_WAIT_TIME);
821 }
822
823 end_free:
824 free(metadata_str);
825 end:
826 return ret_code;
827 }
828
829 /*
830 * Receive command from session daemon and process it.
831 *
832 * Return 1 on success else a negative value or 0.
833 */
834 int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
835 int sock, struct pollfd *consumer_sockpoll)
836 {
837 ssize_t ret;
838 enum lttng_error_code ret_code = LTTNG_OK;
839 struct lttcomm_consumer_msg msg;
840 struct lttng_consumer_channel *channel = NULL;
841
842 ret = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg));
843 if (ret != sizeof(msg)) {
844 DBG("Consumer received unexpected message size %zd (expects %zu)",
845 ret, sizeof(msg));
846 /*
847 * The ret value might 0 meaning an orderly shutdown but this is ok
848 * since the caller handles this.
849 */
850 if (ret > 0) {
851 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_CMD);
852 ret = -1;
853 }
854 return ret;
855 }
856 if (msg.cmd_type == LTTNG_CONSUMER_STOP) {
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);
865 return -ENOENT;
866 }
867
868 /* relayd needs RCU read-side lock */
869 rcu_read_lock();
870
871 switch (msg.cmd_type) {
872 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET:
873 {
874 /* Session daemon status message are handled in the following call. */
875 ret = consumer_add_relayd_socket(msg.u.relayd_sock.net_index,
876 msg.u.relayd_sock.type, ctx, sock, consumer_sockpoll,
877 &msg.u.relayd_sock.sock, msg.u.relayd_sock.session_id);
878 goto end_nosignal;
879 }
880 case LTTNG_CONSUMER_DESTROY_RELAYD:
881 {
882 uint64_t index = msg.u.destroy_relayd.net_seq_idx;
883 struct consumer_relayd_sock_pair *relayd;
884
885 DBG("UST consumer destroying relayd %" PRIu64, index);
886
887 /* Get relayd reference if exists. */
888 relayd = consumer_find_relayd(index);
889 if (relayd == NULL) {
890 DBG("Unable to find relayd %" PRIu64, index);
891 ret_code = LTTNG_ERR_NO_CONSUMER;
892 }
893
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 */
904 if (relayd) {
905 consumer_flag_relayd_for_destroy(relayd);
906 }
907
908 goto end_msg_sessiond;
909 }
910 case LTTNG_CONSUMER_UPDATE_STREAM:
911 {
912 rcu_read_unlock();
913 return -ENOSYS;
914 }
915 case LTTNG_CONSUMER_DATA_PENDING:
916 {
917 int ret, is_data_pending;
918 uint64_t id = msg.u.data_pending.session_id;
919
920 DBG("UST consumer data pending command for id %" PRIu64, id);
921
922 is_data_pending = consumer_data_pending(id);
923
924 /* Send back returned value to session daemon */
925 ret = lttcomm_send_unix_sock(sock, &is_data_pending,
926 sizeof(is_data_pending));
927 if (ret < 0) {
928 DBG("Error when sending the data pending ret code: %d", ret);
929 goto error_fatal;
930 }
931
932 /*
933 * No need to send back a status message since the data pending
934 * returned value is the response.
935 */
936 break;
937 }
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,
948 (enum lttng_event_output) msg.u.ask_channel.output,
949 msg.u.ask_channel.tracefile_size,
950 msg.u.ask_channel.tracefile_count,
951 msg.u.ask_channel.session_id_per_pid);
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;
962 attr.chan_id = msg.u.ask_channel.chan_id;
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;
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;
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
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
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.
1015 * If add_channel succeeds, ownership of the channel is
1016 * passed to consumer_thread_channel_poll().
1017 */
1018 ret = add_channel(channel, ctx);
1019 if (ret < 0) {
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 }
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 /*
1037 * There is probably a problem on the socket.
1038 */
1039 goto error_fatal;
1040 }
1041
1042 break;
1043 }
1044 case LTTNG_CONSUMER_GET_CHANNEL:
1045 {
1046 int ret, relayd_err = 0;
1047 uint64_t key = msg.u.get_channel.key;
1048 struct lttng_consumer_channel *channel;
1049
1050 channel = consumer_find_channel(key);
1051 if (!channel) {
1052 ERR("UST consumer get channel key %" PRIu64 " not found", key);
1053 ret_code = LTTNG_ERR_UST_CHAN_NOT_FOUND;
1054 goto end_msg_sessiond;
1055 }
1056
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
1064 * and the consumer can continue its work. The above call
1065 * has sent the error status message to the sessiond.
1066 */
1067 goto end_nosignal;
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
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;
1083 }
1084 /* List MUST be empty after or else it could be reused. */
1085 assert(cds_list_empty(&channel->streams.head));
1086
1087 goto end_msg_sessiond;
1088 }
1089 case LTTNG_CONSUMER_DESTROY_CHANNEL:
1090 {
1091 uint64_t key = msg.u.destroy_channel.key;
1092
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);
1099 goto end_msg_sessiond;
1100 }
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 }
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 }
1123 case LTTNG_CONSUMER_PUSH_METADATA:
1124 {
1125 int ret;
1126 uint64_t len = msg.u.push_metadata.len;
1127 uint64_t key = msg.u.push_metadata.key;
1128 uint64_t offset = msg.u.push_metadata.target_offset;
1129 struct lttng_consumer_channel *channel;
1130
1131 DBG("UST consumer push metadata key %" PRIu64 " of len %" PRIu64, key,
1132 len);
1133
1134 channel = consumer_find_channel(key);
1135 if (!channel) {
1136 ERR("UST consumer push metadata %" PRIu64 " not found", key);
1137 ret_code = LTTNG_ERR_UST_CHAN_NOT_FOUND;
1138 goto end_msg_sessiond;
1139 }
1140
1141 /* Tell session daemon we are ready to receive the metadata. */
1142 ret = consumer_send_status_msg(sock, LTTNG_OK);
1143 if (ret < 0) {
1144 /* Somehow, the session daemon is not responding anymore. */
1145 goto error_fatal;
1146 }
1147
1148 /* Wait for more data. */
1149 if (lttng_consumer_poll_socket(consumer_sockpoll) < 0) {
1150 goto error_fatal;
1151 }
1152
1153 ret = lttng_ustconsumer_recv_metadata(sock, key, offset,
1154 len, channel, 0);
1155 if (ret < 0) {
1156 /* error receiving from sessiond */
1157 goto error_fatal;
1158 } else {
1159 ret_code = ret;
1160 goto end_msg_sessiond;
1161 }
1162 }
1163 case LTTNG_CONSUMER_SETUP_METADATA:
1164 {
1165 int ret;
1166
1167 ret = setup_metadata(ctx, msg.u.setup_metadata.key);
1168 if (ret) {
1169 ret_code = ret;
1170 }
1171 goto end_msg_sessiond;
1172 }
1173 default:
1174 break;
1175 }
1176
1177 end_nosignal:
1178 rcu_read_unlock();
1179
1180 /*
1181 * Return 1 to indicate success since the 0 value can be a socket
1182 * shutdown during the recv() or send() call.
1183 */
1184 return 1;
1185
1186 end_msg_sessiond:
1187 /*
1188 * The returned value here is not useful since either way we'll return 1 to
1189 * the caller because the session daemon socket management is done
1190 * elsewhere. Returning a negative code or 0 will shutdown the consumer.
1191 */
1192 ret = consumer_send_status_msg(sock, ret_code);
1193 if (ret < 0) {
1194 goto error_fatal;
1195 }
1196 rcu_read_unlock();
1197 return 1;
1198 end_channel_error:
1199 if (channel) {
1200 /*
1201 * Free channel here since no one has a reference to it. We don't
1202 * free after that because a stream can store this pointer.
1203 */
1204 destroy_channel(channel);
1205 }
1206 /* We have to send a status channel message indicating an error. */
1207 ret = consumer_send_status_channel(sock, NULL);
1208 if (ret < 0) {
1209 /* Stop everything if session daemon can not be notified. */
1210 goto error_fatal;
1211 }
1212 rcu_read_unlock();
1213 return 1;
1214 error_fatal:
1215 rcu_read_unlock();
1216 /* This will issue a consumer stop. */
1217 return -1;
1218 }
1219
1220 /*
1221 * Wrapper over the mmap() read offset from ust-ctl library. Since this can be
1222 * compiled out, we isolate it in this library.
1223 */
1224 int lttng_ustctl_get_mmap_read_offset(struct lttng_consumer_stream *stream,
1225 unsigned long *off)
1226 {
1227 assert(stream);
1228 assert(stream->ustream);
1229
1230 return ustctl_get_mmap_read_offset(stream->ustream, off);
1231 }
1232
1233 /*
1234 * Wrapper over the mmap() read offset from ust-ctl library. Since this can be
1235 * compiled out, we isolate it in this library.
1236 */
1237 void *lttng_ustctl_get_mmap_base(struct lttng_consumer_stream *stream)
1238 {
1239 assert(stream);
1240 assert(stream->ustream);
1241
1242 return ustctl_get_mmap_base(stream->ustream);
1243 }
1244
1245 /*
1246 * Take a snapshot for a specific fd
1247 *
1248 * Returns 0 on success, < 0 on error
1249 */
1250 int lttng_ustconsumer_take_snapshot(struct lttng_consumer_stream *stream)
1251 {
1252 assert(stream);
1253 assert(stream->ustream);
1254
1255 return ustctl_snapshot(stream->ustream);
1256 }
1257
1258 /*
1259 * Get the produced position
1260 *
1261 * Returns 0 on success, < 0 on error
1262 */
1263 int lttng_ustconsumer_get_produced_snapshot(
1264 struct lttng_consumer_stream *stream, unsigned long *pos)
1265 {
1266 assert(stream);
1267 assert(stream->ustream);
1268 assert(pos);
1269
1270 return ustctl_snapshot_get_produced(stream->ustream, pos);
1271 }
1272
1273 /*
1274 * Called when the stream signal the consumer that it has hang up.
1275 */
1276 void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream *stream)
1277 {
1278 assert(stream);
1279 assert(stream->ustream);
1280
1281 ustctl_flush_buffer(stream->ustream, 0);
1282 stream->hangup_flush_done = 1;
1283 }
1284
1285 void lttng_ustconsumer_del_channel(struct lttng_consumer_channel *chan)
1286 {
1287 assert(chan);
1288 assert(chan->uchan);
1289
1290 if (chan->switch_timer_enabled == 1) {
1291 consumer_timer_switch_stop(chan);
1292 }
1293 consumer_metadata_cache_destroy(chan);
1294 ustctl_destroy_channel(chan->uchan);
1295 }
1296
1297 void lttng_ustconsumer_del_stream(struct lttng_consumer_stream *stream)
1298 {
1299 assert(stream);
1300 assert(stream->ustream);
1301
1302 if (stream->chan->switch_timer_enabled == 1) {
1303 consumer_timer_switch_stop(stream->chan);
1304 }
1305 ustctl_destroy_stream(stream->ustream);
1306 }
1307
1308 int lttng_ustconsumer_read_subbuffer(struct lttng_consumer_stream *stream,
1309 struct lttng_consumer_local_data *ctx)
1310 {
1311 unsigned long len, subbuf_size, padding;
1312 int err;
1313 long ret = 0;
1314 char dummy;
1315 struct ustctl_consumer_stream *ustream;
1316
1317 assert(stream);
1318 assert(stream->ustream);
1319 assert(ctx);
1320
1321 DBG("In UST read_subbuffer (wait_fd: %d, name: %s)", stream->wait_fd,
1322 stream->name);
1323
1324 /* Ease our life for what's next. */
1325 ustream = stream->ustream;
1326
1327 /* We can consume the 1 byte written into the wait_fd by UST */
1328 if (!stream->hangup_flush_done) {
1329 ssize_t readlen;
1330
1331 do {
1332 readlen = read(stream->wait_fd, &dummy, 1);
1333 } while (readlen == -1 && errno == EINTR);
1334 if (readlen == -1) {
1335 ret = readlen;
1336 goto end;
1337 }
1338 }
1339
1340 /* Get the next subbuffer */
1341 err = ustctl_get_next_subbuf(ustream);
1342 if (err != 0) {
1343 ret = err; /* ustctl_get_next_subbuf returns negative, caller expect positive. */
1344 /*
1345 * This is a debug message even for single-threaded consumer,
1346 * because poll() have more relaxed criterions than get subbuf,
1347 * so get_subbuf may fail for short race windows where poll()
1348 * would issue wakeups.
1349 */
1350 DBG("Reserving sub buffer failed (everything is normal, "
1351 "it is due to concurrency) [ret: %d]", err);
1352 goto end;
1353 }
1354 assert(stream->chan->output == CONSUMER_CHANNEL_MMAP);
1355 /* Get the full padded subbuffer size */
1356 err = ustctl_get_padded_subbuf_size(ustream, &len);
1357 assert(err == 0);
1358
1359 /* Get subbuffer data size (without padding) */
1360 err = ustctl_get_subbuf_size(ustream, &subbuf_size);
1361 assert(err == 0);
1362
1363 /* Make sure we don't get a subbuffer size bigger than the padded */
1364 assert(len >= subbuf_size);
1365
1366 padding = len - subbuf_size;
1367 /* write the subbuffer to the tracefile */
1368 ret = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, subbuf_size, padding);
1369 /*
1370 * The mmap operation should write subbuf_size amount of data when network
1371 * streaming or the full padding (len) size when we are _not_ streaming.
1372 */
1373 if ((ret != subbuf_size && stream->net_seq_idx != (uint64_t) -1ULL) ||
1374 (ret != len && stream->net_seq_idx == (uint64_t) -1ULL)) {
1375 /*
1376 * Display the error but continue processing to try to release the
1377 * subbuffer. This is a DBG statement since any unexpected kill or
1378 * signal, the application gets unregistered, relayd gets closed or
1379 * anything that affects the buffer lifetime will trigger this error.
1380 * So, for the sake of the user, don't print this error since it can
1381 * happen and it is OK with the code flow.
1382 */
1383 DBG("Error writing to tracefile "
1384 "(ret: %ld != len: %lu != subbuf_size: %lu)",
1385 ret, len, subbuf_size);
1386 }
1387 err = ustctl_put_next_subbuf(ustream);
1388 assert(err == 0);
1389
1390 end:
1391 return ret;
1392 }
1393
1394 /*
1395 * Called when a stream is created.
1396 *
1397 * Return 0 on success or else a negative value.
1398 */
1399 int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream *stream)
1400 {
1401 int ret;
1402
1403 /* Don't create anything if this is set for streaming. */
1404 if (stream->net_seq_idx == (uint64_t) -1ULL) {
1405 ret = utils_create_stream_file(stream->chan->pathname, stream->name,
1406 stream->chan->tracefile_size, stream->tracefile_count_current,
1407 stream->uid, stream->gid);
1408 if (ret < 0) {
1409 goto error;
1410 }
1411 stream->out_fd = ret;
1412 stream->tracefile_size_current = 0;
1413 }
1414 ret = 0;
1415
1416 error:
1417 return ret;
1418 }
1419
1420 /*
1421 * Check if data is still being extracted from the buffers for a specific
1422 * stream. Consumer data lock MUST be acquired before calling this function
1423 * and the stream lock.
1424 *
1425 * Return 1 if the traced data are still getting read else 0 meaning that the
1426 * data is available for trace viewer reading.
1427 */
1428 int lttng_ustconsumer_data_pending(struct lttng_consumer_stream *stream)
1429 {
1430 int ret;
1431
1432 assert(stream);
1433 assert(stream->ustream);
1434
1435 DBG("UST consumer checking data pending");
1436
1437 if (stream->endpoint_status != CONSUMER_ENDPOINT_ACTIVE) {
1438 ret = 0;
1439 goto end;
1440 }
1441
1442 ret = ustctl_get_next_subbuf(stream->ustream);
1443 if (ret == 0) {
1444 /* There is still data so let's put back this subbuffer. */
1445 ret = ustctl_put_subbuf(stream->ustream);
1446 assert(ret == 0);
1447 ret = 1; /* Data is pending */
1448 goto end;
1449 }
1450
1451 /* Data is NOT pending so ready to be read. */
1452 ret = 0;
1453
1454 end:
1455 return ret;
1456 }
1457
1458 /*
1459 * Close every metadata stream wait fd of the metadata hash table. This
1460 * function MUST be used very carefully so not to run into a race between the
1461 * metadata thread handling streams and this function closing their wait fd.
1462 *
1463 * For UST, this is used when the session daemon hangs up. Its the metadata
1464 * producer so calling this is safe because we are assured that no state change
1465 * can occur in the metadata thread for the streams in the hash table.
1466 */
1467 void lttng_ustconsumer_close_metadata(struct lttng_ht *metadata_ht)
1468 {
1469 int ret;
1470 struct lttng_ht_iter iter;
1471 struct lttng_consumer_stream *stream;
1472
1473 assert(metadata_ht);
1474 assert(metadata_ht->ht);
1475
1476 DBG("UST consumer closing all metadata streams");
1477
1478 rcu_read_lock();
1479 cds_lfht_for_each_entry(metadata_ht->ht, &iter.iter, stream,
1480 node.node) {
1481 int fd = stream->wait_fd;
1482
1483 /*
1484 * Whatever happens here we have to continue to try to close every
1485 * streams. Let's report at least the error on failure.
1486 */
1487 ret = ustctl_stream_close_wakeup_fd(stream->ustream);
1488 if (ret) {
1489 ERR("Unable to close metadata stream fd %d ret %d", fd, ret);
1490 }
1491 DBG("Metadata wait fd %d closed", fd);
1492 }
1493 rcu_read_unlock();
1494 }
1495
1496 void lttng_ustconsumer_close_stream_wakeup(struct lttng_consumer_stream *stream)
1497 {
1498 int ret;
1499
1500 ret = ustctl_stream_close_wakeup_fd(stream->ustream);
1501 if (ret < 0) {
1502 ERR("Unable to close wakeup fd");
1503 }
1504 }
1505
1506 /*
1507 * Please refer to consumer-timer.c before adding any lock within this
1508 * function or any of its callees. Timers have a very strict locking
1509 * semantic with respect to teardown. Failure to respect this semantic
1510 * introduces deadlocks.
1511 */
1512 int lttng_ustconsumer_request_metadata(struct lttng_consumer_local_data *ctx,
1513 struct lttng_consumer_channel *channel, int timer)
1514 {
1515 struct lttcomm_metadata_request_msg request;
1516 struct lttcomm_consumer_msg msg;
1517 enum lttng_error_code ret_code = LTTNG_OK;
1518 uint64_t len, key, offset;
1519 int ret;
1520
1521 assert(channel);
1522 assert(channel->metadata_cache);
1523
1524 /* send the metadata request to sessiond */
1525 switch (consumer_data.type) {
1526 case LTTNG_CONSUMER64_UST:
1527 request.bits_per_long = 64;
1528 break;
1529 case LTTNG_CONSUMER32_UST:
1530 request.bits_per_long = 32;
1531 break;
1532 default:
1533 request.bits_per_long = 0;
1534 break;
1535 }
1536
1537 request.session_id = channel->session_id;
1538 request.session_id_per_pid = channel->session_id_per_pid;
1539 request.uid = channel->uid;
1540 request.key = channel->key;
1541 DBG("Sending metadata request to sessiond, session id %" PRIu64
1542 ", per-pid %" PRIu64,
1543 channel->session_id,
1544 channel->session_id_per_pid);
1545
1546 ret = lttcomm_send_unix_sock(ctx->consumer_metadata_socket, &request,
1547 sizeof(request));
1548 if (ret < 0) {
1549 ERR("Asking metadata to sessiond");
1550 goto end;
1551 }
1552
1553 /* Receive the metadata from sessiond */
1554 ret = lttcomm_recv_unix_sock(ctx->consumer_metadata_socket, &msg,
1555 sizeof(msg));
1556 if (ret != sizeof(msg)) {
1557 DBG("Consumer received unexpected message size %d (expects %zu)",
1558 ret, sizeof(msg));
1559 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_CMD);
1560 /*
1561 * The ret value might 0 meaning an orderly shutdown but this is ok
1562 * since the caller handles this.
1563 */
1564 goto end;
1565 }
1566
1567 if (msg.cmd_type == LTTNG_ERR_UND) {
1568 /* No registry found */
1569 (void) consumer_send_status_msg(ctx->consumer_metadata_socket,
1570 ret_code);
1571 ret = 0;
1572 goto end;
1573 } else if (msg.cmd_type != LTTNG_CONSUMER_PUSH_METADATA) {
1574 ERR("Unexpected cmd_type received %d", msg.cmd_type);
1575 ret = -1;
1576 goto end;
1577 }
1578
1579 len = msg.u.push_metadata.len;
1580 key = msg.u.push_metadata.key;
1581 offset = msg.u.push_metadata.target_offset;
1582
1583 assert(key == channel->key);
1584 if (len == 0) {
1585 DBG("No new metadata to receive for key %" PRIu64, key);
1586 }
1587
1588 /* Tell session daemon we are ready to receive the metadata. */
1589 ret = consumer_send_status_msg(ctx->consumer_metadata_socket,
1590 LTTNG_OK);
1591 if (ret < 0 || len == 0) {
1592 /*
1593 * Somehow, the session daemon is not responding anymore or there is
1594 * nothing to receive.
1595 */
1596 goto end;
1597 }
1598
1599 ret = lttng_ustconsumer_recv_metadata(ctx->consumer_metadata_socket,
1600 key, offset, len, channel, timer);
1601 if (ret >= 0) {
1602 /*
1603 * Only send the status msg if the sessiond is alive meaning a positive
1604 * ret code.
1605 */
1606 (void) consumer_send_status_msg(ctx->consumer_metadata_socket, ret);
1607 }
1608 ret = 0;
1609
1610 end:
1611 return ret;
1612 }
This page took 0.064221 seconds and 4 git commands to generate.