Fix: illegal memory access in list_lttng_channels
[lttng-tools.git] / src / bin / lttng-sessiond / cmd.c
index b54e1d03c90b9e31023cc4b021f7974595c99fba..5e6a0d0877dd10b019f66c3ed2947ee3b5cd38ef 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>
@@ -173,7 +174,10 @@ static void list_lttng_channels(int domain, struct ltt_session *session,
                rcu_read_lock();
                cds_lfht_for_each_entry(session->ust_session->domain_global.channels->ht,
                                &iter.iter, uchan, node.node) {
-                       strncpy(channels[i].name, uchan->name, LTTNG_SYMBOL_NAME_LEN);
+                       if (lttng_strncpy(channels[i].name, uchan->name,
+                                       LTTNG_SYMBOL_NAME_LEN)) {
+                               break;
+                       }
                        channels[i].attr.overwrite = uchan->attr.overwrite;
                        channels[i].attr.subbuf_size = uchan->attr.subbuf_size;
                        channels[i].attr.num_subbuf = uchan->attr.num_subbuf;
@@ -517,12 +521,15 @@ static int add_uri_to_consumer(struct consumer_output *consumer,
                DBG2("Setting trace directory path from URI to %s", uri->dst.path);
                memset(consumer->dst.trace_path, 0,
                                sizeof(consumer->dst.trace_path));
-               strncpy(consumer->dst.trace_path, uri->dst.path,
-                               sizeof(consumer->dst.trace_path));
+               /* Explicit length checks for strcpy and strcat. */
+               if (strlen(uri->dst.path) + strlen(default_trace_dir)
+                               >= sizeof(consumer->dst.trace_path)) {
+                       ret = LTTNG_ERR_FATAL;
+                       goto error;
+               }
+               strcpy(consumer->dst.trace_path, uri->dst.path);
                /* Append default trace dir */
-               strncat(consumer->dst.trace_path, default_trace_dir,
-                               sizeof(consumer->dst.trace_path) -
-                               strlen(consumer->dst.trace_path) - 1);
+               strcat(consumer->dst.trace_path, default_trace_dir);
                /* Flag consumer as local. */
                consumer->type = CONSUMER_DST_LOCAL;
                break;
@@ -938,11 +945,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();
@@ -1023,6 +1040,7 @@ int cmd_enable_channel(struct ltt_session *session,
 
 error:
        rcu_read_unlock();
+end:
        return ret;
 }
 
@@ -1125,7 +1143,14 @@ int cmd_disable_event(struct ltt_session *session, int domain,
 
                switch (event->type) {
                case LTTNG_EVENT_ALL:
-                       ret = event_ust_disable_tracepoint(usess, uchan, event_name);
+                       if (strlen(event->name) == 1 &&
+                                       !strncmp(event->name, "*", 1)) {
+                               ret = event_ust_disable_all_tracepoints(usess,
+                                               uchan);
+                       } else {
+                               ret = event_ust_disable_tracepoint(usess, uchan,
+                                               event_name);
+                       }
                        if (ret != LTTNG_OK) {
                                goto error;
                        }
@@ -1443,7 +1468,12 @@ int cmd_enable_event(struct ltt_session *session, struct lttng_domain *domain,
                                ret = LTTNG_ERR_FATAL;
                                goto error;
                        }
-                       strncpy(attr->name, channel_name, sizeof(attr->name));
+                       if (lttng_strncpy(attr->name, channel_name,
+                                       sizeof(attr->name))) {
+                               ret = LTTNG_ERR_INVALID;
+                               free(attr);
+                               goto error;
+                       }
 
                        ret = cmd_enable_channel(session, domain, attr, wpipe);
                        if (ret != LTTNG_OK) {
@@ -1521,7 +1551,12 @@ int cmd_enable_event(struct ltt_session *session, struct lttng_domain *domain,
                                ret = LTTNG_ERR_FATAL;
                                goto error;
                        }
-                       strncpy(attr->name, channel_name, sizeof(attr->name));
+                       if (lttng_strncpy(attr->name, channel_name,
+                                       sizeof(attr->name))) {
+                               ret = LTTNG_ERR_INVALID;
+                               free(attr);
+                               goto error;
+                       }
 
                        ret = cmd_enable_channel(session, domain, attr, wpipe);
                        if (ret != LTTNG_OK) {
@@ -1563,7 +1598,7 @@ int cmd_enable_event(struct ltt_session *session, struct lttng_domain *domain,
                if (!agt) {
                        agt = agent_create(domain->type);
                        if (!agt) {
-                               ret = -LTTNG_ERR_NOMEM;
+                               ret = LTTNG_ERR_NOMEM;
                                goto error;
                        }
                        agent_add(agt, usess->agents);
@@ -1575,7 +1610,7 @@ int cmd_enable_event(struct ltt_session *session, struct lttng_domain *domain,
                uevent.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
                default_event_name = event_get_default_agent_ust_name(domain->type);
                if (!default_event_name) {
-                       ret = -LTTNG_ERR_FATAL;
+                       ret = LTTNG_ERR_FATAL;
                        goto error;
                }
                strncpy(uevent.name, default_event_name, sizeof(uevent.name));
@@ -1597,26 +1632,40 @@ int cmd_enable_event(struct ltt_session *session, struct lttng_domain *domain,
 
                {
                        struct lttng_filter_bytecode *filter_copy = NULL;
+                       char *filter_expression_copy = NULL;
 
                        if (filter) {
                                filter_copy = zmalloc(
                                        sizeof(struct lttng_filter_bytecode)
                                        + filter->len);
                                if (!filter_copy) {
+                                       ret = LTTNG_ERR_NOMEM;
                                        goto error;
                                }
 
                                memcpy(filter_copy, filter,
-                                       sizeof(struct lttng_filter_bytecode)
-                                       + filter->len);
+                                      sizeof(struct lttng_filter_bytecode)
+                                      + filter->len);
+                       }
+
+                       if (filter_expression) {
+                               filter_expression_copy =
+                                               strdup(filter_expression);
+                               if (!filter_expression) {
+                                       ret = LTTNG_ERR_NOMEM;
+                                       goto error_free_copy;
+                               }
                        }
 
                        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;
+                                       &uevent, filter_expression_copy,
+                                       filter_copy, NULL, wpipe);
+                       filter_copy = NULL;
+                       filter_expression_copy = NULL;
+error_free_copy:
+                       free(filter_copy);
+                       free(filter_expression_copy);
                }
 
                if (ret != LTTNG_OK && ret != LTTNG_ERR_UST_EVENT_ENABLED) {
@@ -1625,10 +1674,12 @@ 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);
+                       ret = event_agent_enable_all(usess, agt, event, filter,
+                                       filter_expression);
                        filter = NULL;
                } else {
-                       ret = event_agent_enable(usess, agt, event, filter);
+                       ret = event_agent_enable(usess, agt, event, filter,
+                                       filter_expression);
                        filter = NULL;
                }
                if (ret != LTTNG_OK) {
@@ -1994,13 +2045,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);
@@ -2012,41 +2062,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).
@@ -2054,7 +2104,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;
        }
 
@@ -2116,7 +2168,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;
                }
@@ -2154,7 +2206,7 @@ int cmd_create_session_snapshot(char *name, struct lttng_uri *uris,
         * Create session in no output mode with URIs set to NULL. The uris we've
         * received are for a default snapshot output if one.
         */
-       ret = cmd_create_session_uri(name, NULL, 0, creds, -1);
+       ret = cmd_create_session_uri(name, NULL, 0, creds, 0);
        if (ret != LTTNG_OK) {
                goto error;
        }
@@ -2423,6 +2475,10 @@ ssize_t cmd_list_domains(struct ltt_session *session,
 
        if (session->kernel_session != NULL) {
                (*domains)[index].type = LTTNG_DOMAIN_KERNEL;
+
+               /* Kernel session buffer type is always GLOBAL */
+               (*domains)[index].buf_type = LTTNG_BUFFER_GLOBAL;
+
                index++;
        }
 
@@ -2825,10 +2881,18 @@ ssize_t cmd_snapshot_list_outputs(struct ltt_session *session,
                assert(output->consumer);
                list[idx].id = output->id;
                list[idx].max_size = output->max_size;
-               strncpy(list[idx].name, output->name, sizeof(list[idx].name));
+               if (lttng_strncpy(list[idx].name, output->name,
+                               sizeof(list[idx].name))) {
+                       ret = -LTTNG_ERR_INVALID;
+                       goto error;
+               }
                if (output->consumer->type == CONSUMER_DST_LOCAL) {
-                       strncpy(list[idx].ctrl_url, output->consumer->dst.trace_path,
-                                       sizeof(list[idx].ctrl_url));
+                       if (lttng_strncpy(list[idx].ctrl_url,
+                                       output->consumer->dst.trace_path,
+                                       sizeof(list[idx].ctrl_url))) {
+                               ret = -LTTNG_ERR_INVALID;
+                               goto error;
+                       }
                } else {
                        /* Control URI. */
                        ret = uri_to_str_url(&output->consumer->dst.net.control,
@@ -2911,7 +2975,7 @@ error:
  */
 static int record_kernel_snapshot(struct ltt_kernel_session *ksess,
                struct snapshot_output *output, struct ltt_session *session,
-               int wait, uint64_t max_stream_size)
+               int wait, uint64_t nb_packets_per_stream)
 {
        int ret;
 
@@ -2919,13 +2983,6 @@ static int record_kernel_snapshot(struct ltt_kernel_session *ksess,
        assert(output);
        assert(session);
 
-       /* Get the datetime for the snapshot output directory. */
-       ret = utils_get_current_time_str("%Y%m%d-%H%M%S", output->datetime,
-                       sizeof(output->datetime));
-       if (!ret) {
-               ret = LTTNG_ERR_INVALID;
-               goto error;
-       }
 
        /*
         * Copy kernel session sockets so we can communicate with the right
@@ -2942,17 +2999,19 @@ static int record_kernel_snapshot(struct ltt_kernel_session *ksess,
                goto error_snapshot;
        }
 
-       ret = kernel_snapshot_record(ksess, output, wait, max_stream_size);
+       ret = kernel_snapshot_record(ksess, output, wait, nb_packets_per_stream);
        if (ret != LTTNG_OK) {
                goto error_snapshot;
        }
 
        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;
 }
 
@@ -2963,7 +3022,7 @@ error:
  */
 static int record_ust_snapshot(struct ltt_ust_session *usess,
                struct snapshot_output *output, struct ltt_session *session,
-               int wait, uint64_t max_stream_size)
+               int wait, uint64_t nb_packets_per_stream)
 {
        int ret;
 
@@ -2971,14 +3030,6 @@ static int record_ust_snapshot(struct ltt_ust_session *usess,
        assert(output);
        assert(session);
 
-       /* Get the datetime for the snapshot output directory. */
-       ret = utils_get_current_time_str("%Y%m%d-%H%M%S", output->datetime,
-                       sizeof(output->datetime));
-       if (!ret) {
-               ret = LTTNG_ERR_INVALID;
-               goto error;
-       }
-
        /*
         * Copy UST session sockets so we can communicate with the right
         * consumer for the snapshot record command.
@@ -2994,15 +3045,12 @@ static int record_ust_snapshot(struct ltt_ust_session *usess,
                goto error_snapshot;
        }
 
-       ret = ust_app_snapshot_record(usess, output, wait, max_stream_size);
+       ret = ust_app_snapshot_record(usess, output, wait, nb_packets_per_stream);
        if (ret < 0) {
                switch (-ret) {
                case EINVAL:
                        ret = LTTNG_ERR_INVALID;
                        break;
-               case ENODATA:
-                       ret = LTTNG_ERR_SNAPSHOT_NODATA;
-                       break;
                default:
                        ret = LTTNG_ERR_SNAPSHOT_FAIL;
                        break;
@@ -3019,69 +3067,90 @@ 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)
+static
+uint64_t get_session_size_one_more_packet_per_stream(struct ltt_session *session,
+       uint64_t cur_nr_packets)
 {
-       uint64_t max_size = 0;
-
-       assert(session);
+       uint64_t tot_size = 0;
 
        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 (cur_nr_packets >= chan->channel->attr.num_subbuf) {
+                               /*
+                                * Don't take channel into account if we
+                                * already grab all its packets.
+                                */
+                               continue;
                        }
+                       tot_size += chan->channel->attr.subbuf_size
+                               * chan->stream_count;
                }
        }
 
        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();
+               tot_size += ust_app_get_size_one_more_packet_per_stream(usess,
+                               cur_nr_packets);
        }
 
-       return max_size;
+       return tot_size;
 }
 
 /*
- * Returns the total number of streams for a session or a negative value
- * on error.
+ * Calculate the number of packets we can grab from each stream that
+ * fits within the overall snapshot max size.
+ *
+ * Returns -1 on error, 0 means infinite number of packets, else > 0 is
+ * the number of packets per stream.
+ *
+ * TODO: this approach is not perfect: we consider the worse case
+ * (packet filling the sub-buffers) as an upper bound, but we could do
+ * better if we do this calculation while we actually grab the packet
+ * content: we would know how much padding we don't actually store into
+ * the file.
+ *
+ * This algorithm is currently bounded by the number of packets per
+ * stream.
+ *
+ * Since we call this algorithm before actually grabbing the data, it's
+ * an approximation: for instance, applications could appear/disappear
+ * in between this call and actually grabbing data.
  */
-static unsigned int get_session_nb_streams(struct ltt_session *session)
+static
+int64_t get_session_nb_packets_per_stream(struct ltt_session *session, uint64_t max_size)
 {
-       unsigned int total_streams = 0;
-
-       if (session->kernel_session) {
-               struct ltt_kernel_session *ksess = session->kernel_session;
+       int64_t size_left;
+       uint64_t cur_nb_packets = 0;
 
-               total_streams += ksess->stream_count_global;
+       if (!max_size) {
+               return 0;       /* Infinite */
        }
 
-       if (session->ust_session) {
-               struct ltt_ust_session *usess = session->ust_session;
+       size_left = max_size;
+       for (;;) {
+               uint64_t one_more_packet_tot_size;
 
-               total_streams += ust_app_get_nb_stream(usess);
+               one_more_packet_tot_size = get_session_size_one_more_packet_per_stream(session,
+                                       cur_nb_packets);
+               if (!one_more_packet_tot_size) {
+                       /* We are already grabbing all packets. */
+                       break;
+               }
+               size_left -= one_more_packet_tot_size;
+               if (size_left < 0) {
+                       break;
+               }
+               cur_nb_packets++;
        }
-
-       return total_streams;
+       if (!cur_nb_packets) {
+               /* Not enough room to grab one packet of each stream, error. */
+               return -1;
+       }
+       return cur_nb_packets;
 }
 
 /*
@@ -3098,14 +3167,22 @@ int cmd_snapshot_record(struct ltt_session *session,
        int ret = LTTNG_OK;
        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;
+       unsigned int snapshot_success = 0;
+       char datetime[16];
 
        assert(session);
        assert(output);
 
        DBG("Cmd snapshot record for session %s", session->name);
 
+       /* Get the datetime for the snapshot output directory. */
+       ret = utils_get_current_time_str("%Y%m%d-%H%M%S", datetime,
+                       sizeof(datetime));
+       if (!ret) {
+               ret = LTTNG_ERR_INVALID;
+               goto error;
+       }
+
        /*
         * Permission denied to create an output if the session is not
         * set in no output mode.
@@ -3136,151 +3213,104 @@ int cmd_snapshot_record(struct ltt_session *session,
                }
                /* Use the global session count for the temporary snapshot. */
                tmp_output.nb_snapshot = session->snapshot.nb_snapshot;
-               use_tmp_output = 1;
-       }
-
-       /*
-        * 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;
+               /* Use the global datetime */
+               memcpy(tmp_output.datetime, datetime, sizeof(datetime));
+               use_tmp_output = 1;
        }
 
-       DBG3("Snapshot max size is %" PRIu64 " for max stream size of %" PRIu64,
-                       session_max_size, max_stream_size);
+       if (use_tmp_output) {
+               int64_t nb_packets_per_stream;
 
-       /*
-        * If we use a temporary output, check right away if the max size fits else
-        * for each output the max size will be checked.
-        */
-       if (use_tmp_output &&
-                       (tmp_output.max_size != 0 &&
-                       tmp_output.max_size < session_max_size)) {
-               ret = LTTNG_ERR_MAX_SIZE_INVALID;
-               goto error;
-       }
+               nb_packets_per_stream = get_session_nb_packets_per_stream(session,
+                               tmp_output.max_size);
+               if (nb_packets_per_stream < 0) {
+                       ret = LTTNG_ERR_MAX_SIZE_INVALID;
+                       goto error;
+               }
 
-       if (session->kernel_session) {
-               struct ltt_kernel_session *ksess = session->kernel_session;
+               if (session->kernel_session) {
+                       ret = record_kernel_snapshot(session->kernel_session,
+                                       &tmp_output, session,
+                                       wait, nb_packets_per_stream);
+                       if (ret != LTTNG_OK) {
+                               goto error;
+                       }
+               }
 
-               if (use_tmp_output) {
-                       ret = record_kernel_snapshot(ksess, &tmp_output, session,
-                                       wait, max_stream_size);
+               if (session->ust_session) {
+                       ret = record_ust_snapshot(session->ust_session,
+                                       &tmp_output, session,
+                                       wait, nb_packets_per_stream);
                        if (ret != LTTNG_OK) {
                                goto error;
                        }
-                       snapshot_success = 1;
-               } else {
-                       struct snapshot_output *sout;
-                       struct lttng_ht_iter iter;
+               }
 
-                       rcu_read_lock();
-                       cds_lfht_for_each_entry(session->snapshot.output_ht->ht,
-                                       &iter.iter, sout, node.node) {
-                               /*
-                                * Make a local copy of the output and assign the possible
-                                * temporary value given by the caller.
-                                */
-                               memset(&tmp_output, 0, sizeof(tmp_output));
-                               memcpy(&tmp_output, sout, sizeof(tmp_output));
+               snapshot_success = 1;
+       } else {
+               struct snapshot_output *sout;
+               struct lttng_ht_iter iter;
 
-                               /* Use temporary max size. */
-                               if (output->max_size != (uint64_t) -1ULL) {
-                                       tmp_output.max_size = output->max_size;
-                               }
+               rcu_read_lock();
+               cds_lfht_for_each_entry(session->snapshot.output_ht->ht,
+                               &iter.iter, sout, node.node) {
+                       int64_t nb_packets_per_stream;
 
-                               if (tmp_output.max_size != 0 &&
-                                               tmp_output.max_size < session_max_size) {
-                                       rcu_read_unlock();
-                                       ret = LTTNG_ERR_MAX_SIZE_INVALID;
-                                       goto error;
-                               }
+                       /*
+                        * Make a local copy of the output and assign the possible
+                        * temporary value given by the caller.
+                        */
+                       memset(&tmp_output, 0, sizeof(tmp_output));
+                       memcpy(&tmp_output, sout, sizeof(tmp_output));
 
-                               /* Use temporary name. */
-                               if (*output->name != '\0') {
-                                       strncpy(tmp_output.name, output->name,
-                                                       sizeof(tmp_output.name));
-                               }
+                       if (output->max_size != (uint64_t) -1ULL) {
+                               tmp_output.max_size = output->max_size;
+                       }
 
-                               tmp_output.nb_snapshot = session->snapshot.nb_snapshot;
+                       nb_packets_per_stream = get_session_nb_packets_per_stream(session,
+                                       tmp_output.max_size);
+                       if (nb_packets_per_stream < 0) {
+                               ret = LTTNG_ERR_MAX_SIZE_INVALID;
+                               rcu_read_unlock();
+                               goto error;
+                       }
 
-                               ret = record_kernel_snapshot(ksess, &tmp_output,
-                                               session, wait, max_stream_size);
-                               if (ret != LTTNG_OK) {
+                       /* Use temporary name. */
+                       if (*output->name != '\0') {
+                               if (lttng_strncpy(tmp_output.name, output->name,
+                                               sizeof(tmp_output.name))) {
+                                       ret = LTTNG_ERR_INVALID;
                                        rcu_read_unlock();
                                        goto error;
                                }
-                               snapshot_success = 1;
-                       }
-                       rcu_read_unlock();
-               }
-       }
-
-       if (session->ust_session) {
-               struct ltt_ust_session *usess = session->ust_session;
-
-               if (use_tmp_output) {
-                       ret = record_ust_snapshot(usess, &tmp_output, session,
-                                       wait, max_stream_size);
-                       if (ret != LTTNG_OK) {
-                               goto error;
                        }
-                       snapshot_success = 1;
-               } else {
-                       struct snapshot_output *sout;
-                       struct lttng_ht_iter iter;
 
-                       rcu_read_lock();
-                       cds_lfht_for_each_entry(session->snapshot.output_ht->ht,
-                                       &iter.iter, sout, node.node) {
-                               /*
-                                * Make a local copy of the output and assign the possible
-                                * temporary value given by the caller.
-                                */
-                               memset(&tmp_output, 0, sizeof(tmp_output));
-                               memcpy(&tmp_output, sout, sizeof(tmp_output));
-
-                               /* Use temporary max size. */
-                               if (output->max_size != (uint64_t) -1ULL) {
-                                       tmp_output.max_size = output->max_size;
-                               }
+                       tmp_output.nb_snapshot = session->snapshot.nb_snapshot;
+                       memcpy(tmp_output.datetime, datetime, sizeof(datetime));
 
-                               if (tmp_output.max_size != 0 &&
-                                               tmp_output.max_size < session_max_size) {
+                       if (session->kernel_session) {
+                               ret = record_kernel_snapshot(session->kernel_session,
+                                               &tmp_output, session,
+                                               wait, nb_packets_per_stream);
+                               if (ret != LTTNG_OK) {
                                        rcu_read_unlock();
-                                       ret = LTTNG_ERR_MAX_SIZE_INVALID;
                                        goto error;
                                }
+                       }
 
-                               /* Use temporary name. */
-                               if (*output->name != '\0') {
-                                       strncpy(tmp_output.name, output->name,
-                                                       sizeof(tmp_output.name));
-                               }
-
-                               tmp_output.nb_snapshot = session->snapshot.nb_snapshot;
-
-                               ret = record_ust_snapshot(usess, &tmp_output, session,
-                                               wait, max_stream_size);
+                       if (session->ust_session) {
+                               ret = record_ust_snapshot(session->ust_session,
+                                               &tmp_output, session,
+                                               wait, nb_packets_per_stream);
                                if (ret != LTTNG_OK) {
                                        rcu_read_unlock();
                                        goto error;
                                }
-                               snapshot_success = 1;
                        }
-                       rcu_read_unlock();
+                       snapshot_success = 1;
                }
+               rcu_read_unlock();
        }
 
        if (snapshot_success) {
This page took 0.033527 seconds and 4 git commands to generate.