Fix: leak of streams
[babeltrace.git] / formats / lttng-live / lttng-live-plugin.c
index 1d0e0aa02f0c1700b8a75aad1f1640d3b39b2527..a10a1b21235acad04b7ad85c1de16081ec77c621 100644 (file)
 #include <glib.h>
 #include <unistd.h>
 #include <stdlib.h>
+#include <signal.h>
 #include "lttng-live.h"
 
+static volatile int should_quit;
+
+int lttng_live_should_quit(void)
+{
+       return should_quit;
+}
+
+static
+void sighandler(int sig)
+{
+       switch (sig) {
+       case SIGTERM:
+       case SIGINT:
+               should_quit = 1;
+               break;
+       default:
+               break;
+       }
+}
+
 /*
- * hostname parameter needs to hold NAME_MAX chars.
+ * TODO: Eventually, this signal handler setup should be done at the
+ * plugin manager level, rather than within this plugin. Beware, we are
+ * not cleaning up the signal handler after plugin execution.
+ */
+static
+int setup_sighandler(void)
+{
+       struct sigaction sa;
+       sigset_t sigset;
+       int ret;
+
+       if ((ret = sigemptyset(&sigset)) < 0) {
+               perror("sigemptyset");
+               return ret;
+       }
+       sa.sa_handler = sighandler;
+       sa.sa_mask = sigset;
+       sa.sa_flags = 0;
+       if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
+               perror("sigaction");
+               return ret;
+       }
+       if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
+               perror("sigaction");
+               return ret;
+       }
+       return 0;
+}
+
+/*
+ * hostname parameter needs to hold MAXNAMLEN chars.
  */
 static
 int parse_url(const char *path, struct lttng_live_ctx *ctx)
 {
-       char remain[3][NAME_MAX];
+       char remain[3][MAXNAMLEN];
        int ret = -1, proto, proto_offset = 0;
        size_t path_len = strlen(path); /* not accounting \0 */
 
@@ -53,7 +104,7 @@ int parse_url(const char *path, struct lttng_live_ctx *ctx)
         * against a size defined by a macro. Test it beforehand on the
         * input. We know the output is always <= than the input length.
         */
-       if (path_len >= NAME_MAX) {
+       if (path_len >= MAXNAMLEN) {
                goto end;
        }
        ret = sscanf(path, "net%d://", &proto);
@@ -68,6 +119,10 @@ int parse_url(const char *path, struct lttng_live_ctx *ctx)
        if (proto_offset > path_len) {
                goto end;
        }
+       if (proto == 6) {
+               fprintf(stderr, "[error] IPv6 is currently unsupported by lttng-live\n");
+               goto end;
+       }
        /* TODO : parse for IPv6 as well */
        /* Parse the hostname or IP */
        ret = sscanf(&path[proto_offset], "%[a-zA-Z.0-9%-]%s",
@@ -84,6 +139,10 @@ int parse_url(const char *path, struct lttng_live_ctx *ctx)
                                if (ret < 0) {
                                        goto end;
                                }
+                       } else if (ret == 0) {
+                               fprintf(stderr, "[error] Missing port number after delimitor ':'\n");
+                               ret = -1;
+                               goto end;
                        }
                        break;
                case '/':
@@ -102,8 +161,9 @@ int parse_url(const char *path, struct lttng_live_ctx *ctx)
                }
        }
 
-       if (ctx->port < 0)
+       if (ctx->port < 0) {
                ctx->port = LTTNG_DEFAULT_NETWORK_VIEWER_PORT;
+       }
 
        if (strlen(remain[2]) == 0) {
                printf_verbose("Connecting to hostname : %s, port : %d, "
@@ -131,6 +191,41 @@ end:
        return ret;
 }
 
+static
+guint g_uint64p_hash(gconstpointer key)
+{
+       uint64_t v = *(uint64_t *) key;
+
+       if (sizeof(gconstpointer) == sizeof(uint64_t)) {
+               return g_direct_hash((gconstpointer) (unsigned long) v);
+       } else {
+               return g_direct_hash((gconstpointer) (unsigned long) (v >> 32))
+                       ^ g_direct_hash((gconstpointer) (unsigned long) v);
+       }
+}
+
+static
+gboolean g_uint64p_equal(gconstpointer a, gconstpointer b)
+{
+       uint64_t va = *(uint64_t *) a;
+       uint64_t vb = *(uint64_t *) b;
+
+       if (va != vb)
+               return FALSE;
+       return TRUE;
+}
+
+static void free_session_streams(struct lttng_live_session *lsession)
+{
+       struct lttng_live_viewer_stream *lvstream, *tmp;
+
+       bt_list_for_each_entry_safe(lvstream, tmp, &lsession->stream_list,
+                       stream_node) {
+               bt_list_del(&lvstream->stream_node);
+               g_free(lvstream);
+       }
+}
+
 static int lttng_live_open_trace_read(const char *path)
 {
        int ret = 0;
@@ -139,12 +234,14 @@ static int lttng_live_open_trace_read(const char *path)
        ctx = g_new0(struct lttng_live_ctx, 1);
        ctx->session = g_new0(struct lttng_live_session, 1);
 
+       BT_INIT_LIST_HEAD(&ctx->session->stream_list);
+
        /* We need a pointer to the context from the packet_seek function. */
        ctx->session->ctx = ctx;
 
        /* HT to store the CTF traces. */
-       ctx->session->ctf_traces = g_hash_table_new(g_direct_hash,
-                       g_direct_equal);
+       ctx->session->ctf_traces = g_hash_table_new(g_uint64p_hash,
+                       g_uint64p_equal);
        ctx->port = -1;
        ctx->session_ids = g_array_new(FALSE, TRUE, sizeof(uint64_t));
 
@@ -152,10 +249,12 @@ static int lttng_live_open_trace_read(const char *path)
        if (ret < 0) {
                goto end_free;
        }
-
+       ret = setup_sighandler();
+       if (ret < 0) {
+               goto end_free;
+       }
        ret = lttng_live_connect_viewer(ctx);
        if (ret < 0) {
-               fprintf(stderr, "[error] Connection failed\n");
                goto end_free;
        }
        printf_verbose("LTTng-live connected to relayd\n");
@@ -168,18 +267,22 @@ static int lttng_live_open_trace_read(const char *path)
        printf_verbose("Listing sessions\n");
        ret = lttng_live_list_sessions(ctx, path);
        if (ret < 0) {
-               fprintf(stderr, "[error] List error\n");
                goto end_free;
        }
 
-       if (ctx->session_ids->len > 0)
-               lttng_live_read(ctx);
+       if (ctx->session_ids->len > 0) {
+               ret = lttng_live_read(ctx);
+       }
 
 end_free:
        g_hash_table_destroy(ctx->session->ctf_traces);
+       free_session_streams(ctx->session);
        g_free(ctx->session);
-       g_free(ctx->session->streams);
        g_free(ctx);
+
+       if (lttng_live_should_quit()) {
+               ret = 0;
+       }
        return ret;
 }
 
@@ -206,7 +309,15 @@ struct bt_trace_descriptor *lttng_live_open_trace(const char *path, int flags,
        pos->parent.rw_table = NULL;
        pos->parent.event_cb = NULL;
        pos->parent.trace = &pos->trace_descriptor;
-       lttng_live_open_trace_read(path);
+       /*
+        * Since we do *everything* in this function, we are skipping
+        * the output plugin handling that is part of Babeltrace 1.x.
+        * Therefore, don't expect the --output cmd line option to work.
+        * This limits the output of lttng-live to stderr and stdout.
+        */
+       if (lttng_live_open_trace_read(path) < 0) {
+               goto error;
+       }
        return &pos->trace_descriptor;
 
 error:
@@ -219,7 +330,7 @@ int lttng_live_close_trace(struct bt_trace_descriptor *td)
        struct ctf_text_stream_pos *pos =
                container_of(td, struct ctf_text_stream_pos,
                        trace_descriptor);
-       free(pos);
+       g_free(pos);
        return 0;
 }
 
This page took 0.025442 seconds and 4 git commands to generate.