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