Fix: don't destroy the sockets if the snapshot was successful
[lttng-tools.git] / src / bin / lttng-sessiond / cmd.c
index 17091de09ec66d9bd5b6028bbf04b90c7a99d5ff..58506b2b225e5e0e306d9fad8682013e52a0723d 100644 (file)
@@ -16,7 +16,9 @@
  */
 
 #define _GNU_SOURCE
+#define _LGPL_SOURCE
 #include <assert.h>
+#include <string.h>
 #include <inttypes.h>
 #include <urcu/list.h>
 #include <urcu/uatomic.h>
@@ -219,7 +221,9 @@ static int list_lttng_agent_events(struct agent *agt,
 
        DBG3("Listing agent events");
 
+       rcu_read_lock();
        nb_event = lttng_ht_get_count(agt->events);
+       rcu_read_unlock();
        if (nb_event == 0) {
                ret = nb_event;
                goto error;
@@ -936,11 +940,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();
@@ -1021,6 +1035,7 @@ int cmd_enable_channel(struct ltt_session *session,
 
 error:
        rcu_read_unlock();
+end:
        return ret;
 }
 
@@ -1039,7 +1054,8 @@ int cmd_disable_event(struct ltt_session *session, int domain,
 
        event_name = event->name;
 
-       if (event->loglevel_type || event->loglevel || event->enabled
+       /* Error out on unhandled search criteria */
+       if (event->loglevel_type || event->loglevel != -1 || event->enabled
                        || event->pid || event->filter || event->exclusion) {
                return LTTNG_ERR_UNK;
        }
@@ -1385,6 +1401,7 @@ end:
 
 /*
  * 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,
@@ -1536,6 +1553,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;
                }
@@ -1598,8 +1619,30 @@ int cmd_enable_event(struct ltt_session *session, struct lttng_domain *domain,
                        assert(0);
                }
 
-               ret = cmd_enable_event(session, &tmp_dom, (char *) default_chan_name,
-                       &uevent, filter_expression, filter, NULL, wpipe);
+               {
+                       struct lttng_filter_bytecode *filter_copy = NULL;
+
+                       if (filter) {
+                               filter_copy = zmalloc(
+                                       sizeof(struct lttng_filter_bytecode)
+                                       + filter->len);
+                               if (!filter_copy) {
+                                       goto error;
+                               }
+
+                               memcpy(filter_copy, filter,
+                                       sizeof(struct lttng_filter_bytecode)
+                                       + filter->len);
+                       }
+
+                       ret = cmd_enable_event(session, &tmp_dom,
+                                       (char *) default_chan_name,
+                                       &uevent, filter_expression, filter_copy,
+                                       NULL, wpipe);
+                       /* We have passed ownership */
+                       filter_expression = NULL;
+               }
+
                if (ret != LTTNG_OK && ret != LTTNG_ERR_UST_EVENT_ENABLED) {
                        goto error;
                }
@@ -1607,8 +1650,10 @@ int cmd_enable_event(struct ltt_session *session, struct lttng_domain *domain,
                /* The wild card * means that everything should be enabled. */
                if (strncmp(event->name, "*", 1) == 0 && strlen(event->name) == 1) {
                        ret = event_agent_enable_all(usess, agt, event, filter);
+                       filter = NULL;
                } else {
                        ret = event_agent_enable(usess, agt, event, filter);
+                       filter = NULL;
                }
                if (ret != LTTNG_OK) {
                        goto error;
@@ -1629,6 +1674,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;
 }
@@ -1848,7 +1896,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;
@@ -1969,13 +2019,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);
@@ -1987,41 +2036,41 @@ 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).
@@ -2029,7 +2078,9 @@ int cmd_set_consumer_uri(int domain, struct ltt_session *session,
        session->output_traces = 1;
        if (ksess) {
                ksess->output_traces = 1;
-       } else if (usess) {
+       }
+
+       if (usess) {
                usess->output_traces = 1;
        }
 
@@ -2091,7 +2142,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;
                }
@@ -2380,12 +2431,14 @@ ssize_t cmd_list_domains(struct ltt_session *session,
                DBG3("Listing domains found UST global domain");
                nb_dom++;
 
+               rcu_read_lock();
                cds_lfht_for_each_entry(session->ust_session->agents->ht, &iter.iter,
                                agt, node.node) {
                        if (agt->being_used) {
                                nb_dom++;
                        }
                }
+               rcu_read_unlock();
        }
 
        if (!nb_dom) {
@@ -2408,6 +2461,7 @@ ssize_t cmd_list_domains(struct ltt_session *session,
                (*domains)[index].buf_type = session->ust_session->buffer_type;
                index++;
 
+               rcu_read_lock();
                cds_lfht_for_each_entry(session->ust_session->agents->ht, &iter.iter,
                                agt, node.node) {
                        if (agt->being_used) {
@@ -2416,6 +2470,7 @@ ssize_t cmd_list_domains(struct ltt_session *session,
                                index++;
                        }
                }
+               rcu_read_unlock();
        }
 end:
        return nb_dom;
@@ -2447,8 +2502,10 @@ 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) {
@@ -2509,10 +2566,12 @@ ssize_t cmd_list_events(int domain, struct ltt_session *session,
                        struct lttng_ht_iter iter;
                        struct agent *agt;
 
+                       rcu_read_lock();
                        cds_lfht_for_each_entry(session->ust_session->agents->ht,
                                        &iter.iter, agt, node.node) {
                                nb_event = list_lttng_agent_events(agt, events);
                        }
+                       rcu_read_unlock();
                }
                break;
        default:
@@ -2761,7 +2820,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;
 
@@ -2775,7 +2834,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;
        }
 
@@ -2786,11 +2845,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);
@@ -2805,28 +2865,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;
 }
 
 /*
@@ -2919,11 +2979,13 @@ static int record_kernel_snapshot(struct ltt_kernel_session *ksess,
        }
 
        ret = LTTNG_OK;
+       goto end;
 
 error_snapshot:
        /* Clean up copied sockets so this output can use some other later on. */
        consumer_destroy_output_sockets(output->consumer);
 error:
+end:
        return ret;
 }
 
@@ -3019,12 +3081,14 @@ static uint64_t get_session_max_subbuf_size(struct ltt_session *session)
                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;
This page took 0.030853 seconds and 5 git commands to generate.