- return ret;
-}
-
-ssize_t read_subbuffer(struct lttng_consumer_stream *kconsumerd_fd,
- struct lttng_consumer_local_data *ctx)
-{
- unsigned long len;
- int err;
- long ret = 0;
- int infd = helper_get_lttng_consumer_stream_wait_fd(kconsumerd_fd);
-
- if (helper_get_lttng_consumer_stream_output(kconsumerd_fd) == LTTNG_EVENT_SPLICE) {
- /* Get the next subbuffer */
- err = helper_kernctl_get_next_subbuf(infd);
- if (err != 0) {
- ret = errno;
- perror("Reserving sub buffer failed (everything is normal, "
- "it is due to concurrency)");
- goto end;
- }
- /* read the whole subbuffer */
- err = helper_kernctl_get_padded_subbuf_size(infd, &len);
- if (err != 0) {
- ret = errno;
- perror("Getting sub-buffer len failed.");
- goto end;
- }
-
- /* splice the subbuffer to the tracefile */
- ret = helper_lttng_consumer_on_read_subbuffer_splice(ctx, kconsumerd_fd, len);
- if (ret < 0) {
- /*
- * display the error but continue processing to try
- * to release the subbuffer
- */
- fprintf(stderr,"Error splicing to tracefile\n");
- }
- err = helper_kernctl_put_next_subbuf(infd);
- if (err != 0) {
- ret = errno;
- perror("Reserving sub buffer failed (everything is normal, "
- "it is due to concurrency)");
- goto end;
- }
- sem_post(&metadata_available);
- }
-
-end:
- return 0;
-}
-
-int on_update_fd(int key, uint32_t state)
-{
- /* let the lib handle the metadata FD */
- if (key == sessiond_metadata)
- return 0;
- return 1;
-}
-
-int on_recv_fd(struct lttng_consumer_stream *kconsumerd_fd)
-{
- int ret;
- struct mmap_stream *new_mmap_stream;
-
- /* Opening the tracefile in write mode */
- if (helper_get_lttng_consumer_stream_path_name(kconsumerd_fd) != NULL) {
- ret = open(helper_get_lttng_consumer_stream_path_name(kconsumerd_fd),
- O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU|S_IRWXG|S_IRWXO);
- if (ret < 0) {
- perror("open");
- goto end;
- }
- helper_set_lttng_consumer_stream_out_fd(kconsumerd_fd, ret);
- }
-
- if (helper_get_lttng_consumer_stream_output(kconsumerd_fd) == LTTNG_EVENT_MMAP) {
- new_mmap_stream = malloc(sizeof(struct mmap_stream));
- new_mmap_stream->fd = helper_get_lttng_consumer_stream_wait_fd(
- kconsumerd_fd);
- bt_list_add(&new_mmap_stream->list, &mmap_list.head);
-
- g_ptr_array_add(lttng_consumer_stream_array, kconsumerd_fd);
- /* keep mmap FDs internally */
- ret = 1;
- } else {
- consumerd_metadata = helper_get_lttng_consumer_stream_wait_fd(kconsumerd_fd);
- sessiond_metadata = helper_get_lttng_consumer_stream_key(kconsumerd_fd);
- ret = 0;
- }
-
- reload_trace = 1;
-
-end:
- return ret;
-}
-
-void live_consume(struct bt_context **bt_ctx)
-{
- int ret;
- FILE *metadata_fp;
-
- sem_wait(&metadata_available);
- if (access("/tmp/livesession/kernel/metadata", F_OK) != 0) {
- fprintf(stderr,"no metadata\n");
- goto end;
- }
- metadata_fp = fopen("/tmp/livesession/kernel/metadata", "r");
-
- *bt_ctx = bt_context_create();
- ret = bt_context_add_trace(*bt_ctx, NULL, "ctf",
- lttngtop_ctf_packet_seek, &mmap_list, metadata_fp);
- if (ret < 0) {
- printf("Error adding trace\n");
- goto end;
- }
-
-end:
- return;
-}
-
-int setup_consumer(char *command_sock_path, pthread_t *threads,
- struct lttng_consumer_local_data *ctx)
-{
- int ret = 0;
-
- ctx = helper_lttng_consumer_create(HELPER_LTTNG_CONSUMER_KERNEL,
- read_subbuffer, NULL, on_recv_fd, on_update_fd);
- if (!ctx)
- goto end;
-
- unlink(command_sock_path);
- helper_lttng_consumer_set_command_sock_path(ctx, command_sock_path);
- helper_lttng_consumer_init();
-
- /* Create the thread to manage the receive of fd */
- ret = pthread_create(&threads[0], NULL, helper_lttng_consumer_thread_receive_fds,
- (void *) ctx);
- if (ret != 0) {
- perror("pthread_create receive fd");
- goto end;
- }
- /* Create thread to manage the polling/writing of traces */
- ret = pthread_create(&threads[1], NULL, helper_lttng_consumer_thread_poll_fds,
- (void *) ctx);
- if (ret != 0) {
- perror("pthread_create poll fd");
- goto end;
- }
-