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