Fix: fd leak on trace close
[babeltrace.git] / formats / ctf / ctf.c
index 69917f7fb5d9cfc8dcaf246128f77d93557d7c6d..d60b4353fc2c0d9bbdaaac45fbfb7e1475dd3b1d 100644 (file)
@@ -551,20 +551,15 @@ error:
 void ctf_init_pos(struct ctf_stream_pos *pos, int fd, int open_flags)
 {
        pos->fd = fd;
-       pos->mmap_offset = 0;
-       pos->packet_size = 0;
-       pos->content_size = 0;
-       pos->content_size_loc = NULL;
-       pos->base_mma = NULL;
-       pos->offset = 0;
-       pos->dummy = false;
-       pos->cur_index = 0;
-       pos->packet_real_index = NULL;
-       if (fd >= 0)
+       if (fd >= 0) {
                pos->packet_cycles_index = g_array_new(FALSE, TRUE,
                                                sizeof(struct packet_index));
-       else
+               pos->packet_real_index = g_array_new(FALSE, TRUE,
+                               sizeof(struct packet_index));
+       } else {
                pos->packet_cycles_index = NULL;
+               pos->packet_real_index = NULL;
+       }
        switch (open_flags & O_ACCMODE) {
        case O_RDONLY:
                pos->prot = PROT_READ;
@@ -600,8 +595,10 @@ void ctf_fini_pos(struct ctf_stream_pos *pos)
                        assert(0);
                }
        }
-       (void) g_array_free(pos->packet_cycles_index, TRUE);
-       (void) g_array_free(pos->packet_real_index, TRUE);
+       if (pos->packet_cycles_index)
+               (void) g_array_free(pos->packet_cycles_index, TRUE);
+       if (pos->packet_real_index)
+               (void) g_array_free(pos->packet_real_index, TRUE);
 }
 
 /*
@@ -702,6 +699,9 @@ void ctf_packet_seek(struct stream_pos *stream_pos, size_t index, int whence)
                        break;
                }
                case SEEK_SET:
+                       packet_index = &g_array_index(pos->packet_cycles_index,
+                                       struct packet_index, index);
+                       pos->last_events_discarded = packet_index->events_discarded;
                        pos->cur_index = index;
                        file_stream->parent.prev_real_timestamp = 0;
                        file_stream->parent.prev_real_timestamp_end = 0;
@@ -1011,6 +1011,7 @@ int ctf_open_trace_metadata_read(struct ctf_trace *td,
 
        if (metadata_fp) {
                fp = metadata_fp;
+               metadata_stream->pos.fd = -1;
        } else {
                td->metadata = &metadata_stream->parent;
                metadata_stream->pos.fd = openat(td->dirfd, "metadata", O_RDONLY);
@@ -1090,7 +1091,8 @@ end_packet_read:
                fclose(fp);
        free(buf);
 end_stream:
-       close(metadata_stream->pos.fd);
+       if (metadata_stream->pos.fd >= 0)
+               close(metadata_stream->pos.fd);
        if (ret)
                g_free(metadata_stream);
        return ret;
@@ -1265,6 +1267,7 @@ int create_stream_packet_index(struct ctf_trace *td,
                packet_index.timestamp_begin = 0;
                packet_index.timestamp_end = 0;
                packet_index.events_discarded = 0;
+               packet_index.events_discarded_len = 0;
 
                /* read and check header, set stream id (and check) */
                if (file_stream->parent.trace_packet_header) {
@@ -1518,7 +1521,7 @@ int ctf_open_file_stream_read(struct ctf_trace *td, const char *path, int flags,
        if (ret)
                goto error_def;
        /*
-        * For now, only a single slock is supported.
+        * For now, only a single clock per trace is supported.
         */
        file_stream->parent.current_clock = td->single_clock;
        ret = create_stream_packet_index(td, file_stream);
@@ -1746,6 +1749,11 @@ int ctf_open_mmap_stream_read(struct ctf_trace *td,
        if (ret)
                goto error_index;
 
+       /*
+        * For now, only a single clock per trace is supported.
+        */
+       file_stream->parent.current_clock = td->single_clock;
+
        /* Add stream file to stream class */
        g_ptr_array_add(file_stream->parent.stream_class->streams,
                        &file_stream->parent);
@@ -1848,9 +1856,6 @@ int ctf_convert_index_timestamp(struct trace_descriptor *tdp)
                        cfs = container_of(stream, struct ctf_file_stream,
                                        parent);
                        stream_pos = &cfs->pos;
-                       stream_pos->packet_real_index = g_array_new(FALSE, TRUE,
-                                       sizeof(struct packet_index));
-
                        if (!stream_pos->packet_cycles_index)
                                continue;
 
@@ -1899,35 +1904,14 @@ void ctf_close_trace(struct trace_descriptor *tdp)
                                continue;
                        for (j = 0; j < stream->streams->len; j++) {
                                struct ctf_file_stream *file_stream;
-                               file_stream = container_of(g_ptr_array_index(stream->streams, j), struct ctf_file_stream, parent);
+                               file_stream = container_of(g_ptr_array_index(stream->streams, j),
+                                               struct ctf_file_stream, parent);
                                ctf_close_file_stream(file_stream);
                        }
-
                }
-               g_ptr_array_free(td->streams, TRUE);
-       }
-
-       if (td->event_declarations) {
-               for (i = 0; i < td->event_declarations->len; i++) {
-                       struct bt_ctf_event_decl *event;
-
-                       event = g_ptr_array_index(td->event_declarations, i);
-                       if (event->context_decl)
-                               g_ptr_array_free(event->context_decl, TRUE);
-                       if (event->fields_decl)
-                               g_ptr_array_free(event->fields_decl, TRUE);
-                       if (event->packet_header_decl)
-                               g_ptr_array_free(event->packet_header_decl, TRUE);
-                       if (event->event_context_decl)
-                               g_ptr_array_free(event->event_context_decl, TRUE);
-                       if (event->event_header_decl)
-                               g_ptr_array_free(event->event_header_decl, TRUE);
-                       if (event->packet_context_decl)
-                               g_ptr_array_free(event->packet_context_decl, TRUE);
-                       g_free(event);
-               }
-               g_ptr_array_free(td->event_declarations, TRUE);
        }
+       ctf_destroy_metadata(td);
+       close(td->dirfd);
        closedir(td->dir);
        g_free(td);
 }
@@ -1952,6 +1936,7 @@ void ctf_set_handle(struct trace_descriptor *descriptor,
        td->handle = handle;
 }
 
+static
 void __attribute__((constructor)) ctf_init(void)
 {
        int ret;
@@ -1961,4 +1946,8 @@ void __attribute__((constructor)) ctf_init(void)
        assert(!ret);
 }
 
-/* TODO: finalize */
+static
+void __attribute__((destructor)) ctf_exit(void)
+{
+       bt_unregister_format(&ctf_format);
+}
This page took 0.026134 seconds and 4 git commands to generate.