Fix: Missing rcu_read_lock in cmd_list_channels()
[lttng-tools.git] / src / bin / lttng-sessiond / cmd.c
index d1bf6f0c034d7a96e266c06fc6dbf6c9925e4fbc..4b7ef62b600b3cc41f8d47f040b1f949b7b6b093 100644 (file)
@@ -1303,6 +1303,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,
@@ -1424,6 +1425,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;
                }
@@ -1460,15 +1465,18 @@ int cmd_enable_event(struct ltt_session *session, struct lttng_domain *domain,
 
                ret = cmd_enable_event(session, &tmp_dom, DEFAULT_JUL_CHANNEL_NAME,
                        &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;
@@ -1489,6 +1497,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;
 }
@@ -1689,7 +1700,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;
                }
@@ -1813,7 +1824,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;
@@ -2400,16 +2413,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;
        }
@@ -2422,10 +2437,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;
@@ -2711,7 +2722,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;
 
@@ -2725,7 +2736,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;
        }
 
@@ -2736,11 +2747,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);
@@ -2755,28 +2767,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;
 }
 
 /*
@@ -2832,7 +2844,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;
 
@@ -2863,7 +2875,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;
        }
@@ -2884,7 +2896,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;
 
@@ -2915,7 +2927,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:
@@ -2940,11 +2952,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;
 
@@ -2978,6 +3032,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);
 
@@ -3017,17 +3072,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;
                        }
@@ -3051,6 +3132,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,
@@ -3060,7 +3148,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;
@@ -3076,7 +3164,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;
                        }
@@ -3100,6 +3188,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,
@@ -3109,7 +3204,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.028544 seconds and 4 git commands to generate.