Fix: channel names are not validated
[lttng-tools.git] / src / bin / lttng-sessiond / cmd.c
index ea25a5e470a690182a2e0daed6f4777d511e4f47..942e770bac92e8fc56f053c378faa8e9ad65e95e 100644 (file)
@@ -17,6 +17,7 @@
 
 #define _GNU_SOURCE
 #include <assert.h>
+#include <string.h>
 #include <inttypes.h>
 #include <urcu/list.h>
 #include <urcu/uatomic.h>
@@ -166,6 +167,8 @@ static void list_lttng_channels(int domain, struct ltt_session *session,
                        channels[i].attr.read_timer_interval =
                                uchan->attr.read_timer_interval;
                        channels[i].enabled = uchan->enabled;
+                       channels[i].attr.tracefile_size = uchan->tracefile_size;
+                       channels[i].attr.tracefile_count = uchan->tracefile_count;
                        switch (uchan->attr.output) {
                        case LTTNG_UST_MMAP:
                        default:
@@ -219,6 +222,8 @@ static int list_lttng_jul_events(struct jul_domain *dom,
                strncpy(tmp_events[i].name, event->name, sizeof(tmp_events[i].name));
                tmp_events[i].name[sizeof(tmp_events[i].name) - 1] = '\0';
                tmp_events[i].enabled = event->enabled;
+               tmp_events[i].loglevel = event->loglevel;
+               tmp_events[i].loglevel_type = event->loglevel_type;
                i++;
        }
        rcu_read_unlock();
@@ -903,11 +908,21 @@ int cmd_enable_channel(struct ltt_session *session,
        int ret;
        struct ltt_ust_session *usess = session->ust_session;
        struct lttng_ht *chan_ht;
+       size_t len;
 
        assert(session);
        assert(attr);
        assert(domain);
 
+       len = strnlen(attr->name, sizeof(attr->name));
+
+       /* Validate channel name */
+       if (attr->name[0] == '.' ||
+               memchr(attr->name, '/', len) != NULL) {
+               ret = LTTNG_ERR_INVALID_CHANNEL_NAME;
+               goto end;
+       }
+
        DBG("Enabling channel %s for session %s", attr->name, session->name);
 
        rcu_read_lock();
@@ -931,6 +946,16 @@ int cmd_enable_channel(struct ltt_session *session,
                attr->attr.switch_timer_interval = 0;
        }
 
+       /*
+        * The ringbuffer (both in user space and kernel) behave badly in overwrite
+        * mode and with less than 2 subbuf so block it right away and send back an
+        * invalid attribute error.
+        */
+       if (attr->attr.overwrite && attr->attr.num_subbuf < 2) {
+               ret = LTTNG_ERR_INVALID;
+               goto error;
+       }
+
        switch (domain->type) {
        case LTTNG_DOMAIN_KERNEL:
        {
@@ -978,6 +1003,7 @@ int cmd_enable_channel(struct ltt_session *session,
 
 error:
        rcu_read_unlock();
+end:
        return ret;
 }
 
@@ -1289,6 +1315,7 @@ error:
 
 /*
  * Command LTTNG_ENABLE_EVENT processed by the client thread.
+ * We own filter, exclusion, and filter_expression.
  */
 int cmd_enable_event(struct ltt_session *session, struct lttng_domain *domain,
                char *channel_name, struct lttng_event *event,
@@ -1410,6 +1437,10 @@ int cmd_enable_event(struct ltt_session *session, struct lttng_domain *domain,
                /* At this point, the session and channel exist on the tracer */
                ret = event_ust_enable_tracepoint(usess, uchan, event,
                                filter_expression, filter, exclusion);
+               /* We have passed ownership */
+               filter_expression = NULL;
+               filter = NULL;
+               exclusion = NULL;
                if (ret != LTTNG_OK) {
                        goto error;
                }
@@ -1445,16 +1476,19 @@ int cmd_enable_event(struct ltt_session *session, struct lttng_domain *domain,
                tmp_dom.type = LTTNG_DOMAIN_UST;
 
                ret = cmd_enable_event(session, &tmp_dom, DEFAULT_JUL_CHANNEL_NAME,
-                       &uevent, NULL, NULL, NULL, wpipe);
+                       &uevent, filter_expression, filter, NULL, wpipe);
+               /* We have passed ownership */
+               filter_expression = NULL;
+               filter = NULL;
                if (ret != LTTNG_OK && ret != LTTNG_ERR_UST_EVENT_ENABLED) {
                        goto error;
                }
 
                /* The wild card * means that everything should be enabled. */
                if (strncmp(event->name, "*", 1) == 0 && strlen(event->name) == 1) {
-                       ret = event_jul_enable_all(usess, event);
+                       ret = event_jul_enable_all(usess, event, filter);
                } else {
-                       ret = event_jul_enable(usess, event);
+                       ret = event_jul_enable(usess, event, filter);
                }
                if (ret != LTTNG_OK) {
                        goto error;
@@ -1475,6 +1509,9 @@ int cmd_enable_event(struct ltt_session *session, struct lttng_domain *domain,
        ret = LTTNG_OK;
 
 error:
+       free(filter_expression);
+       free(filter);
+       free(exclusion);
        rcu_read_unlock();
        return ret;
 }
@@ -1675,7 +1712,7 @@ int cmd_enable_event_all(struct ltt_session *session,
                strncpy(event.name, "*", sizeof(event.name));
                event.name[sizeof(event.name) - 1] = '\0';
 
-               ret = event_jul_enable_all(usess, &event);
+               ret = event_jul_enable_all(usess, &event, filter);
                if (ret != LTTNG_OK) {
                        goto error;
                }
@@ -1799,7 +1836,9 @@ int cmd_start_trace(struct ltt_session *session)
         * possible to enable channel thus inform the client.
         */
        if (usess && usess->domain_global.channels) {
+               rcu_read_lock();
                nb_chan += lttng_ht_get_count(usess->domain_global.channels);
+               rcu_read_unlock();
        }
        if (ksession) {
                nb_chan += ksession->channel_count;
@@ -1920,13 +1959,12 @@ error:
 /*
  * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
  */
-int cmd_set_consumer_uri(int domain, struct ltt_session *session,
-               size_t nb_uri, struct lttng_uri *uris)
+int cmd_set_consumer_uri(struct ltt_session *session, size_t nb_uri,
+               struct lttng_uri *uris)
 {
        int ret, i;
        struct ltt_kernel_session *ksess = session->kernel_session;
        struct ltt_ust_session *usess = session->ust_session;
-       struct consumer_output *consumer = NULL;
 
        assert(session);
        assert(uris);
@@ -1938,41 +1976,54 @@ int cmd_set_consumer_uri(int domain, struct ltt_session *session,
                goto error;
        }
 
-       /*
-        * This case switch makes sure the domain session has a temporary consumer
-        * so the URL can be set.
-        */
-       switch (domain) {
-       case 0:
-               /* Code flow error. A session MUST always have a consumer object */
-               assert(session->consumer);
-               /*
-                * The URL will be added to the tracing session consumer instead of a
-                * specific domain consumer.
-                */
-               consumer = session->consumer;
-               break;
-       case LTTNG_DOMAIN_KERNEL:
-               /* Code flow error if we don't have a kernel session here. */
-               assert(ksess);
-               assert(ksess->consumer);
-               consumer = ksess->consumer;
-               break;
-       case LTTNG_DOMAIN_UST:
-               /* Code flow error if we don't have a kernel session here. */
-               assert(usess);
-               assert(usess->consumer);
-               consumer = usess->consumer;
-               break;
-       }
-
+       /* Set the "global" consumer URIs */
        for (i = 0; i < nb_uri; i++) {
-               ret = add_uri_to_consumer(consumer, &uris[i], domain, session->name);
+               ret = add_uri_to_consumer(session->consumer,
+                               &uris[i], 0, session->name);
                if (ret != LTTNG_OK) {
                        goto error;
                }
        }
 
+       /* Set UST session URIs */
+       if (session->ust_session) {
+               for (i = 0; i < nb_uri; i++) {
+                       ret = add_uri_to_consumer(
+                                       session->ust_session->consumer,
+                                       &uris[i], LTTNG_DOMAIN_UST,
+                                       session->name);
+                       if (ret != LTTNG_OK) {
+                               goto error;
+                       }
+               }
+       }
+
+       /* Set kernel session URIs */
+       if (session->kernel_session) {
+               for (i = 0; i < nb_uri; i++) {
+                       ret = add_uri_to_consumer(
+                                       session->kernel_session->consumer,
+                                       &uris[i], LTTNG_DOMAIN_KERNEL,
+                                       session->name);
+                       if (ret != LTTNG_OK) {
+                               goto error;
+                       }
+               }
+       }
+
+       /*
+        * Make sure to set the session in output mode after we set URI since a
+        * session can be created without URL (thus flagged in no output mode).
+        */
+       session->output_traces = 1;
+       if (ksess) {
+               ksess->output_traces = 1;
+       }
+
+       if (usess) {
+               usess->output_traces = 1;
+       }
+
        /* All good! */
        ret = LTTNG_OK;
 
@@ -2031,7 +2082,7 @@ int cmd_create_session_uri(char *name, struct lttng_uri *uris,
        }
 
        if (uris) {
-               ret = cmd_set_consumer_uri(0, session, nb_uri, uris);
+               ret = cmd_set_consumer_uri(session, nb_uri, uris);
                if (ret != LTTNG_OK) {
                        goto consumer_error;
                }
@@ -2375,16 +2426,18 @@ ssize_t cmd_list_channels(int domain, struct ltt_session *session,
                break;
        case LTTNG_DOMAIN_UST:
                if (session->ust_session != NULL) {
+                       rcu_read_lock();
                        nb_chan = lttng_ht_get_count(
-                                       session->ust_session->domain_global.channels);
+                               session->ust_session->domain_global.channels);
+                       rcu_read_unlock();
                }
                DBG3("Number of UST global channels %zd", nb_chan);
-               if (nb_chan <= 0) {
+               if (nb_chan < 0) {
                        ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
+                       goto error;
                }
                break;
        default:
-               *channels = NULL;
                ret = LTTNG_ERR_UND;
                goto error;
        }
@@ -2397,10 +2450,6 @@ ssize_t cmd_list_channels(int domain, struct ltt_session *session,
                }
 
                list_lttng_channels(domain, session, *channels);
-       } else {
-               *channels = NULL;
-               /* Ret value was set in the domain switch case */
-               goto error;
        }
 
        return nb_chan;
@@ -2686,7 +2735,7 @@ ssize_t cmd_snapshot_list_outputs(struct ltt_session *session,
                struct lttng_snapshot_output **outputs)
 {
        int ret, idx = 0;
-       struct lttng_snapshot_output *list;
+       struct lttng_snapshot_output *list = NULL;
        struct lttng_ht_iter iter;
        struct snapshot_output *output;
 
@@ -2700,7 +2749,7 @@ ssize_t cmd_snapshot_list_outputs(struct ltt_session *session,
         * set in no output mode.
         */
        if (session->output_traces) {
-               ret = LTTNG_ERR_EPERM;
+               ret = -LTTNG_ERR_EPERM;
                goto error;
        }
 
@@ -2711,11 +2760,12 @@ ssize_t cmd_snapshot_list_outputs(struct ltt_session *session,
 
        list = zmalloc(session->snapshot.nb_output * sizeof(*list));
        if (!list) {
-               ret = LTTNG_ERR_NOMEM;
+               ret = -LTTNG_ERR_NOMEM;
                goto error;
        }
 
        /* Copy list from session to the new list object. */
+       rcu_read_lock();
        cds_lfht_for_each_entry(session->snapshot.output_ht->ht, &iter.iter,
                        output, node.node) {
                assert(output->consumer);
@@ -2730,28 +2780,28 @@ ssize_t cmd_snapshot_list_outputs(struct ltt_session *session,
                        ret = uri_to_str_url(&output->consumer->dst.net.control,
                                        list[idx].ctrl_url, sizeof(list[idx].ctrl_url));
                        if (ret < 0) {
-                               ret = LTTNG_ERR_NOMEM;
-                               goto free_error;
+                               ret = -LTTNG_ERR_NOMEM;
+                               goto error;
                        }
 
                        /* Data URI. */
                        ret = uri_to_str_url(&output->consumer->dst.net.data,
                                        list[idx].data_url, sizeof(list[idx].data_url));
                        if (ret < 0) {
-                               ret = LTTNG_ERR_NOMEM;
-                               goto free_error;
+                               ret = -LTTNG_ERR_NOMEM;
+                               goto error;
                        }
                }
                idx++;
        }
 
        *outputs = list;
-       return session->snapshot.nb_output;
-
-free_error:
-       free(list);
+       list = NULL;
+       ret = session->snapshot.nb_output;
 error:
-       return -ret;
+       free(list);
+       rcu_read_unlock();
+       return ret;
 }
 
 /*
@@ -2807,7 +2857,7 @@ error:
  */
 static int record_kernel_snapshot(struct ltt_kernel_session *ksess,
                struct snapshot_output *output, struct ltt_session *session,
-               int wait, int nb_streams)
+               int wait, uint64_t max_stream_size)
 {
        int ret;
 
@@ -2838,7 +2888,7 @@ static int record_kernel_snapshot(struct ltt_kernel_session *ksess,
                goto error_snapshot;
        }
 
-       ret = kernel_snapshot_record(ksess, output, wait, nb_streams);
+       ret = kernel_snapshot_record(ksess, output, wait, max_stream_size);
        if (ret != LTTNG_OK) {
                goto error_snapshot;
        }
@@ -2859,7 +2909,7 @@ error:
  */
 static int record_ust_snapshot(struct ltt_ust_session *usess,
                struct snapshot_output *output, struct ltt_session *session,
-               int wait, int nb_streams)
+               int wait, uint64_t max_stream_size)
 {
        int ret;
 
@@ -2890,7 +2940,7 @@ static int record_ust_snapshot(struct ltt_ust_session *usess,
                goto error_snapshot;
        }
 
-       ret = ust_app_snapshot_record(usess, output, wait, nb_streams);
+       ret = ust_app_snapshot_record(usess, output, wait, max_stream_size);
        if (ret < 0) {
                switch (-ret) {
                case EINVAL:
@@ -2915,11 +2965,53 @@ error:
        return ret;
 }
 
+/*
+ * Return the biggest subbuffer size of all channels in the given session.
+ */
+static uint64_t get_session_max_subbuf_size(struct ltt_session *session)
+{
+       uint64_t max_size = 0;
+
+       assert(session);
+
+       if (session->kernel_session) {
+               struct ltt_kernel_channel *chan;
+               struct ltt_kernel_session *ksess = session->kernel_session;
+
+               /*
+                * For each channel, add to the max size the size of each subbuffer
+                * multiplied by their sized.
+                */
+               cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
+                       if (chan->channel->attr.subbuf_size > max_size) {
+                               max_size = chan->channel->attr.subbuf_size;
+                       }
+               }
+       }
+
+       if (session->ust_session) {
+               struct lttng_ht_iter iter;
+               struct ltt_ust_channel *uchan;
+               struct ltt_ust_session *usess = session->ust_session;
+
+               rcu_read_lock();
+               cds_lfht_for_each_entry(usess->domain_global.channels->ht, &iter.iter,
+                               uchan, node.node) {
+                       if (uchan->attr.subbuf_size > max_size) {
+                               max_size = uchan->attr.subbuf_size;
+                       }
+               }
+               rcu_read_unlock();
+       }
+
+       return max_size;
+}
+
 /*
  * Returns the total number of streams for a session or a negative value
  * on error.
  */
-static unsigned int get_total_nb_stream(struct ltt_session *session)
+static unsigned int get_session_nb_streams(struct ltt_session *session)
 {
        unsigned int total_streams = 0;
 
@@ -2953,6 +3045,7 @@ int cmd_snapshot_record(struct ltt_session *session,
        unsigned int use_tmp_output = 0;
        struct snapshot_output tmp_output;
        unsigned int nb_streams, snapshot_success = 0;
+       uint64_t session_max_size = 0, max_stream_size = 0;
 
        assert(session);
 
@@ -2992,17 +3085,43 @@ int cmd_snapshot_record(struct ltt_session *session,
        }
 
        /*
-        * Get the total number of stream of that session which is used by the
-        * maximum size of the snapshot feature.
+        * Get the session maximum size for a snapshot meaning it will compute the
+        * size of all streams from all domain.
+        */
+       max_stream_size = get_session_max_subbuf_size(session);
+
+       nb_streams = get_session_nb_streams(session);
+       if (nb_streams) {
+               /*
+                * The maximum size of the snapshot is the number of streams multiplied
+                * by the biggest subbuf size of all channels in a session which is the
+                * maximum stream size available for each stream. The session max size
+                * is now checked against the snapshot max size value given by the user
+                * and if lower, an error is returned.
+                */
+               session_max_size = max_stream_size * nb_streams;
+       }
+
+       DBG3("Snapshot max size is %" PRIu64 " for max stream size of %" PRIu64,
+                       session_max_size, max_stream_size);
+
+       /*
+        * If we use a temporary output, check right away if the max size fits else
+        * for each output the max size will be checked.
         */
-       nb_streams = get_total_nb_stream(session);
+       if (use_tmp_output &&
+                       (tmp_output.max_size != 0 &&
+                       tmp_output.max_size < session_max_size)) {
+               ret = LTTNG_ERR_MAX_SIZE_INVALID;
+               goto error;
+       }
 
        if (session->kernel_session) {
                struct ltt_kernel_session *ksess = session->kernel_session;
 
                if (use_tmp_output) {
                        ret = record_kernel_snapshot(ksess, &tmp_output, session,
-                                       wait, nb_streams);
+                                       wait, max_stream_size);
                        if (ret != LTTNG_OK) {
                                goto error;
                        }
@@ -3026,6 +3145,13 @@ int cmd_snapshot_record(struct ltt_session *session,
                                        tmp_output.max_size = output->max_size;
                                }
 
+                               if (tmp_output.max_size != 0 &&
+                                               tmp_output.max_size < session_max_size) {
+                                       rcu_read_unlock();
+                                       ret = LTTNG_ERR_MAX_SIZE_INVALID;
+                                       goto error;
+                               }
+
                                /* Use temporary name. */
                                if (*output->name != '\0') {
                                        strncpy(tmp_output.name, output->name,
@@ -3035,7 +3161,7 @@ int cmd_snapshot_record(struct ltt_session *session,
                                tmp_output.nb_snapshot = session->snapshot.nb_snapshot;
 
                                ret = record_kernel_snapshot(ksess, &tmp_output,
-                                               session, wait, nb_streams);
+                                               session, wait, max_stream_size);
                                if (ret != LTTNG_OK) {
                                        rcu_read_unlock();
                                        goto error;
@@ -3051,7 +3177,7 @@ int cmd_snapshot_record(struct ltt_session *session,
 
                if (use_tmp_output) {
                        ret = record_ust_snapshot(usess, &tmp_output, session,
-                                       wait, nb_streams);
+                                       wait, max_stream_size);
                        if (ret != LTTNG_OK) {
                                goto error;
                        }
@@ -3075,6 +3201,13 @@ int cmd_snapshot_record(struct ltt_session *session,
                                        tmp_output.max_size = output->max_size;
                                }
 
+                               if (tmp_output.max_size != 0 &&
+                                               tmp_output.max_size < session_max_size) {
+                                       rcu_read_unlock();
+                                       ret = LTTNG_ERR_MAX_SIZE_INVALID;
+                                       goto error;
+                               }
+
                                /* Use temporary name. */
                                if (*output->name != '\0') {
                                        strncpy(tmp_output.name, output->name,
@@ -3084,7 +3217,7 @@ int cmd_snapshot_record(struct ltt_session *session,
                                tmp_output.nb_snapshot = session->snapshot.nb_snapshot;
 
                                ret = record_ust_snapshot(usess, &tmp_output, session,
-                                               wait, nb_streams);
+                                               wait, max_stream_size);
                                if (ret != LTTNG_OK) {
                                        rcu_read_unlock();
                                        goto error;
This page took 0.041721 seconds and 4 git commands to generate.