Fix: sessiond: metadata not created on app unregistration during start
[lttng-tools.git] / src / bin / lttng-sessiond / ust-app.c
index ea3f860398d3ca35edb16e515f55f14cbebcdcf7..f4e4bedb594e89946907dc56b151f72fd4fa6665 100644 (file)
@@ -7,7 +7,6 @@
  */
 
 #define _LGPL_SOURCE
-#include <errno.h>
 #include <inttypes.h>
 #include <pthread.h>
 #include <stdio.h>
@@ -19,6 +18,7 @@
 #include <urcu/compiler.h>
 #include <signal.h>
 
+#include <common/compat/errno.h>
 #include <common/common.h>
 #include <common/sessiond-comm/sessiond-comm.h>
 
@@ -245,7 +245,7 @@ static struct ust_registry_session *get_session_registry(
        {
                struct buffer_reg_uid *reg_uid = buffer_reg_uid_find(
                                ua_sess->tracing_id, ua_sess->bits_per_long,
-                               ua_sess->real_credentials.uid);
+                               lttng_credentials_get_uid(&ua_sess->real_credentials));
                if (!reg_uid) {
                        goto error;
                }
@@ -1143,7 +1143,7 @@ struct ust_app_ctx *alloc_ust_app_ctx(struct lttng_ust_context_attr *uctx)
        if (uctx) {
                memcpy(&ua_ctx->ctx, uctx, sizeof(ua_ctx->ctx));
                if (uctx->ctx == LTTNG_UST_CONTEXT_APP_CONTEXT) {
-                       char *provider_name = NULL, *ctx_name = NULL;
+                       char *provider_name = NULL, *ctx_name = NULL;
 
                        provider_name = strdup(uctx->u.app_ctx.provider_name);
                        ctx_name = strdup(uctx->u.app_ctx.ctx_name);
@@ -1847,10 +1847,10 @@ static void shadow_copy_session(struct ust_app_session *ua_sess,
 
        ua_sess->tracing_id = usess->id;
        ua_sess->id = get_next_session_id();
-       ua_sess->real_credentials.uid = app->uid;
-       ua_sess->real_credentials.gid = app->gid;
-       ua_sess->effective_credentials.uid = usess->uid;
-       ua_sess->effective_credentials.gid = usess->gid;
+       LTTNG_OPTIONAL_SET(&ua_sess->real_credentials.uid, app->uid);
+       LTTNG_OPTIONAL_SET(&ua_sess->real_credentials.gid, app->gid);
+       LTTNG_OPTIONAL_SET(&ua_sess->effective_credentials.uid, usess->uid);
+       LTTNG_OPTIONAL_SET(&ua_sess->effective_credentials.gid, usess->gid);
        ua_sess->buffer_type = usess->buffer_type;
        ua_sess->bits_per_long = app->bits_per_long;
 
@@ -1872,7 +1872,7 @@ static void shadow_copy_session(struct ust_app_session *ua_sess,
        case LTTNG_BUFFER_PER_UID:
                ret = snprintf(ua_sess->path, sizeof(ua_sess->path),
                                DEFAULT_UST_TRACE_UID_PATH,
-                               ua_sess->real_credentials.uid,
+                               lttng_credentials_get_uid(&ua_sess->real_credentials),
                                app->bits_per_long);
                break;
        default:
@@ -1995,8 +1995,9 @@ static int setup_buffer_reg_pid(struct ust_app_session *ua_sess,
                        app->uint64_t_alignment, app->long_alignment,
                        app->byte_order, app->version.major, app->version.minor,
                        reg_pid->root_shm_path, reg_pid->shm_path,
-                       ua_sess->effective_credentials.uid,
-                       ua_sess->effective_credentials.gid, ua_sess->tracing_id,
+                       lttng_credentials_get_uid(&ua_sess->effective_credentials),
+                       lttng_credentials_get_gid(&ua_sess->effective_credentials),
+                       ua_sess->tracing_id,
                        app->uid);
        if (ret < 0) {
                /*
@@ -2296,7 +2297,7 @@ end:
  */
 static
 int create_ust_app_channel_context(struct ust_app_channel *ua_chan,
-               struct lttng_ust_context_attr *uctx,
+               struct lttng_ust_context_attr *uctx,
                struct ust_app *app)
 {
        int ret = 0;
@@ -2887,8 +2888,9 @@ static int create_channel_per_uid(struct ust_app *app,
 
        notification_ret = notification_thread_command_add_channel(
                        notification_thread_handle, session->name,
-                       ua_sess->effective_credentials.uid,
-                       ua_sess->effective_credentials.gid, ua_chan->name,
+                       lttng_credentials_get_uid(&ua_sess->effective_credentials),
+                       lttng_credentials_get_gid(&ua_sess->effective_credentials),
+                       ua_chan->name,
                        ua_chan->key, LTTNG_DOMAIN_UST,
                        ua_chan->attr.subbuf_size * ua_chan->attr.num_subbuf);
        if (notification_ret != LTTNG_OK) {
@@ -2987,8 +2989,9 @@ static int create_channel_per_pid(struct ust_app *app,
 
        cmd_ret = notification_thread_command_add_channel(
                        notification_thread_handle, session->name,
-                       ua_sess->effective_credentials.uid,
-                       ua_sess->effective_credentials.gid, ua_chan->name,
+                       lttng_credentials_get_uid(&ua_sess->effective_credentials),
+                       lttng_credentials_get_gid(&ua_sess->effective_credentials),
+                       ua_chan->name,
                        ua_chan->key, LTTNG_DOMAIN_UST,
                        ua_chan->attr.subbuf_size * ua_chan->attr.num_subbuf);
        if (cmd_ret != LTTNG_OK) {
@@ -4112,11 +4115,14 @@ int ust_app_channel_create(struct ltt_ust_session *usess,
                ret = ust_app_channel_allocate(ua_sess, uchan,
                        LTTNG_UST_CHAN_PER_CPU, usess,
                        &ua_chan);
-               if (ret == 0) {
-                       ret = ust_app_channel_send(app, usess,
-                               ua_sess, ua_chan);
-               } else {
-                       goto end;
+               if (ret < 0) {
+                       goto error;
+               }
+
+               ret = ust_app_channel_send(app, usess,
+                       ua_sess, ua_chan);
+               if (ret) {
+                       goto error;
                }
 
                /* Add contexts. */
@@ -4124,10 +4130,12 @@ int ust_app_channel_create(struct ltt_ust_session *usess,
                        ret = create_ust_app_channel_context(ua_chan,
                                &uctx->ctx, app);
                        if (ret) {
-                               goto end;
+                               goto error;
                        }
                }
        }
+
+error:
        if (ret < 0) {
                switch (ret) {
                case -ENOTCONN:
@@ -4143,7 +4151,7 @@ int ust_app_channel_create(struct ltt_ust_session *usess,
                        break;
                }
        }
-end:
+
        if (ret == 0 && _ua_chan) {
                /*
                 * Only return the application's channel on success. Note
@@ -4351,15 +4359,6 @@ int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app)
                goto skip_setup;
        }
 
-       /*
-        * Create the metadata for the application. This returns gracefully if a
-        * metadata was already set for the session.
-        */
-       ret = create_ust_app_metadata(ua_sess, app, usess->consumer);
-       if (ret < 0) {
-               goto error_unlock;
-       }
-
        health_code_update();
 
 skip_setup:
@@ -4996,7 +4995,7 @@ end:
 
 /*
  * The caller must ensure that the application is compatible and is tracked
- * by the PID tracker.
+ * by the process attribute trackers.
  */
 static
 void ust_app_synchronize(struct ltt_ust_session *usess,
@@ -5027,6 +5026,7 @@ void ust_app_synchronize(struct ltt_ust_session *usess,
        }
 
        rcu_read_lock();
+
        cds_lfht_for_each_entry(usess->domain_global.channels->ht, &uchan_iter,
                        uchan, node.node) {
                struct ust_app_channel *ua_chan;
@@ -5040,7 +5040,7 @@ void ust_app_synchronize(struct ltt_ust_session *usess,
                 * allocated (if necessary) and sent to the application, and
                 * all enabled contexts will be added to the channel.
                 */
-               ret = find_or_create_ust_app_channel(usess, ua_sess,
+               ret = find_or_create_ust_app_channel(usess, ua_sess,
                        app, uchan, &ua_chan);
                if (ret) {
                        /* Tracer is probably gone or ENOMEM. */
@@ -5070,6 +5070,21 @@ void ust_app_synchronize(struct ltt_ust_session *usess,
                        }
                }
        }
+
+       /*
+        * Create the metadata for the application. This returns gracefully if a
+        * metadata was already set for the session.
+        *
+        * The metadata channel must be created after the data channels as the
+        * consumer daemon assumes this ordering. When interacting with a relay
+        * daemon, the consumer will use this assumption to send the
+        * "STREAMS_SENT" message to the relay daemon.
+        */
+       ret = create_ust_app_metadata(ua_sess, app, usess->consumer);
+       if (ret < 0) {
+               goto error_unlock;
+       }
+
        rcu_read_unlock();
 
 end:
@@ -5892,7 +5907,7 @@ enum lttng_error_code ust_app_snapshot_record(
                                status = LTTNG_ERR_INVALID;
                                goto error;
                        }
-                        /* Add the UST default trace dir to path. */
+                       /* Add the UST default trace dir to path. */
                        cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter,
                                        reg_chan, node.node) {
                                status = consumer_snapshot_channel(socket,
@@ -5956,14 +5971,12 @@ enum lttng_error_code ust_app_snapshot_record(
                                status = LTTNG_ERR_INVALID;
                                goto error;
                        }
-                        cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter,
+                       cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter,
                                        ua_chan, node.node) {
                                status = consumer_snapshot_channel(socket,
                                                ua_chan->key, output, 0,
-                                               ua_sess->effective_credentials
-                                                               .uid,
-                                               ua_sess->effective_credentials
-                                                               .gid,
+                                               lttng_credentials_get_uid(&ua_sess->effective_credentials),
+                                               lttng_credentials_get_gid(&ua_sess->effective_credentials),
                                                &trace_path[consumer_path_offset], wait,
                                                nb_packets_per_stream);
                                switch (status) {
@@ -5983,8 +5996,8 @@ enum lttng_error_code ust_app_snapshot_record(
                        }
                        status = consumer_snapshot_channel(socket,
                                        registry->metadata_key, output, 1,
-                                       ua_sess->effective_credentials.uid,
-                                       ua_sess->effective_credentials.gid,
+                                       lttng_credentials_get_uid(&ua_sess->effective_credentials),
+                                       lttng_credentials_get_gid(&ua_sess->effective_credentials),
                                        &trace_path[consumer_path_offset], wait, 0);
                        switch (status) {
                        case LTTNG_OK:
@@ -6340,10 +6353,8 @@ enum lttng_error_code ust_app_rotate_session(struct ltt_session *session)
                                        ua_chan, node.node) {
                                ret = consumer_rotate_channel(socket,
                                                ua_chan->key,
-                                               ua_sess->effective_credentials
-                                                               .uid,
-                                               ua_sess->effective_credentials
-                                                               .gid,
+                                               lttng_credentials_get_uid(&ua_sess->effective_credentials),
+                                               lttng_credentials_get_gid(&ua_sess->effective_credentials),
                                                ua_sess->consumer,
                                                /* is_metadata_channel */ false);
                                if (ret < 0) {
@@ -6359,8 +6370,8 @@ enum lttng_error_code ust_app_rotate_session(struct ltt_session *session)
                        (void) push_metadata(registry, usess->consumer);
                        ret = consumer_rotate_channel(socket,
                                        registry->metadata_key,
-                                       ua_sess->effective_credentials.uid,
-                                       ua_sess->effective_credentials.gid,
+                                       lttng_credentials_get_uid(&ua_sess->effective_credentials),
+                                       lttng_credentials_get_gid(&ua_sess->effective_credentials),
                                        ua_sess->consumer,
                                        /* is_metadata_channel */ true);
                        if (ret < 0) {
@@ -6639,3 +6650,126 @@ end:
        rcu_read_unlock();
        return cmd_ret;
 }
+
+/*
+ * This function skips the metadata channel as the begin/end timestamps of a
+ * metadata packet are useless.
+ *
+ * Moreover, opening a packet after a "clear" will cause problems for live
+ * sessions as it will introduce padding that was not part of the first trace
+ * chunk. The relay daemon expects the content of the metadata stream of
+ * successive metadata trace chunks to be strict supersets of one another.
+ *
+ * For example, flushing a packet at the beginning of the metadata stream of
+ * a trace chunk resulting from a "clear" session command will cause the
+ * size of the metadata stream of the new trace chunk to not match the size of
+ * the metadata stream of the original chunk. This will confuse the relay
+ * daemon as the same "offset" in a metadata stream will no longer point
+ * to the same content.
+ */
+enum lttng_error_code ust_app_open_packets(struct ltt_session *session)
+{
+       enum lttng_error_code ret = LTTNG_OK;
+       struct lttng_ht_iter iter;
+       struct ltt_ust_session *usess = session->ust_session;
+
+       assert(usess);
+
+       rcu_read_lock();
+
+       switch (usess->buffer_type) {
+       case LTTNG_BUFFER_PER_UID:
+       {
+               struct buffer_reg_uid *reg;
+
+               cds_list_for_each_entry (
+                               reg, &usess->buffer_reg_uid_list, lnode) {
+                       struct buffer_reg_channel *reg_chan;
+                       struct consumer_socket *socket;
+
+                       socket = consumer_find_socket_by_bitness(
+                                       reg->bits_per_long, usess->consumer);
+                       if (!socket) {
+                               ret = LTTNG_ERR_FATAL;
+                               goto error;
+                       }
+
+                       cds_lfht_for_each_entry(reg->registry->channels->ht,
+                                       &iter.iter, reg_chan, node.node) {
+                               const int open_ret =
+                                               consumer_open_channel_packets(
+                                                       socket,
+                                                       reg_chan->consumer_key);
+
+                               if (open_ret < 0) {
+                                       ret = LTTNG_ERR_UNK;
+                                       goto error;
+                               }
+                       }
+               }
+               break;
+       }
+       case LTTNG_BUFFER_PER_PID:
+       {
+               struct ust_app *app;
+
+               cds_lfht_for_each_entry (
+                               ust_app_ht->ht, &iter.iter, app, pid_n.node) {
+                       struct consumer_socket *socket;
+                       struct lttng_ht_iter chan_iter;
+                       struct ust_app_channel *ua_chan;
+                       struct ust_app_session *ua_sess;
+                       struct ust_registry_session *registry;
+
+                       ua_sess = lookup_session_by_app(usess, app);
+                       if (!ua_sess) {
+                               /* Session not associated with this app. */
+                               continue;
+                       }
+
+                       /* Get the right consumer socket for the application. */
+                       socket = consumer_find_socket_by_bitness(
+                                       app->bits_per_long, usess->consumer);
+                       if (!socket) {
+                               ret = LTTNG_ERR_FATAL;
+                               goto error;
+                       }
+
+                       registry = get_session_registry(ua_sess);
+                       if (!registry) {
+                               DBG("Application session is being torn down. Skip application.");
+                               continue;
+                       }
+
+                       cds_lfht_for_each_entry(ua_sess->channels->ht,
+                                       &chan_iter.iter, ua_chan, node.node) {
+                               const int open_ret =
+                                               consumer_open_channel_packets(
+                                                       socket,
+                                                       ua_chan->key);
+
+                               if (open_ret < 0) {
+                                       /*
+                                        * Per-PID buffer and application going
+                                        * away.
+                                        */
+                                       if (open_ret == -LTTNG_ERR_CHAN_NOT_FOUND) {
+                                               continue;
+                                       }
+
+                                       ret = LTTNG_ERR_UNK;
+                                       goto error;
+                               }
+                       }
+               }
+               break;
+       }
+       default:
+               abort();
+               break;
+       }
+
+error:
+       rcu_read_unlock();
+       return ret;
+}
This page took 0.028788 seconds and 4 git commands to generate.