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