Fix: build failure with -fno-common
[lttng-tools.git] / src / bin / lttng-sessiond / ust-app.c
index 856de63a3f24152d03944c003bfe28c890f4847a..d3c28883c74bea2416d9afb8a5f0db7c87c03f91 100644 (file)
 #include "notification-thread-commands.h"
 #include "rotate.h"
 
+struct lttng_ht *ust_app_ht;
+struct lttng_ht *ust_app_ht_by_sock;
+struct lttng_ht *ust_app_ht_by_notify_sock;
+
 static
 int ust_app_flush_app_session(struct ust_app *app, struct ust_app_session *ua_sess);
 
@@ -1901,12 +1905,12 @@ static void shadow_copy_session(struct ust_app_session *ua_sess,
                switch (ua_sess->buffer_type) {
                case LTTNG_BUFFER_PER_PID:
                        ret = snprintf(tmp_shm_path, sizeof(tmp_shm_path),
-                                       DEFAULT_UST_TRACE_PID_PATH "/%s-%d-%s",
+                                       "/" DEFAULT_UST_TRACE_PID_PATH "/%s-%d-%s",
                                        app->name, app->pid, datetime);
                        break;
                case LTTNG_BUFFER_PER_UID:
                        ret = snprintf(tmp_shm_path, sizeof(tmp_shm_path),
-                                       DEFAULT_UST_TRACE_UID_PATH,
+                                       "/" DEFAULT_UST_TRACE_UID_PATH,
                                        app->uid, app->bits_per_long);
                        break;
                default:
@@ -4161,75 +4165,6 @@ end:
        return ret;
 }
 
-/*
- * For a specific UST session, create the channel for all registered apps.
- */
-int ust_app_create_channel_glb(struct ltt_ust_session *usess,
-               struct ltt_ust_channel *uchan)
-{
-       int ret = 0;
-       struct cds_lfht_iter iter;
-       struct ust_app *app;
-
-       assert(usess);
-       assert(usess->active);
-       assert(uchan);
-
-       DBG2("UST app adding channel %s to UST domain for session id %" PRIu64,
-                       uchan->name, usess->id);
-
-       rcu_read_lock();
-       /* For every registered applications */
-       cds_lfht_for_each_entry(ust_app_ht->ht, &iter, app, pid_n.node) {
-               struct ust_app_session *ua_sess;
-               int session_was_created = 0;
-
-               if (!app->compatible ||
-                               !trace_ust_pid_tracker_lookup(usess, app->pid)) {
-                       goto error_rcu_unlock;
-               }
-
-               /*
-                * Create session on the tracer side and add it to app session HT. Note
-                * that if session exist, it will simply return a pointer to the ust
-                * app session.
-                */
-               ret = find_or_create_ust_app_session(usess, app, &ua_sess,
-                               &session_was_created);
-               if (ret < 0) {
-                       switch (ret) {
-                       case -ENOTCONN:
-                               /*
-                                * The application's socket is not valid. Either a bad
-                                * socket or a timeout on it. We can't inform the caller
-                                * that for a specific app, the session failed so lets
-                                * continue here; it is not an error.
-                                */
-                               ret = 0;
-                               goto error_rcu_unlock;
-                       case -ENOMEM:
-                       default:
-                               goto error_rcu_unlock;
-                       }
-               }
-
-               if (ua_sess->deleted) {
-                       continue;
-               }
-               ret = ust_app_channel_create(usess, ua_sess, uchan, app, NULL);
-               if (ret) {
-                       if (session_was_created) {
-                               destroy_app_session(app, ua_sess);
-                       }
-                       /* Continue to the next application. */
-               }
-       }
-
-error_rcu_unlock:
-       rcu_read_unlock();
-       return ret;
-}
-
 /*
  * Enable event for a specific session and channel on the tracer.
  */
@@ -4416,6 +4351,11 @@ int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app)
                goto end;
        }
 
+       if (ua_sess->enabled) {
+               pthread_mutex_unlock(&ua_sess->lock);
+               goto end;
+       }
+
        /* Upon restart, we skip the setup, already done */
        if (ua_sess->started) {
                goto skip_setup;
@@ -4456,6 +4396,7 @@ skip_setup:
 
        /* Indicate that the session has been started once */
        ua_sess->started = 1;
+       ua_sess->enabled = 1;
 
        pthread_mutex_unlock(&ua_sess->lock);
 
@@ -4545,6 +4486,7 @@ int ust_app_stop_trace(struct ltt_ust_session *usess, struct ust_app *app)
        }
 
        health_code_update();
+       ua_sess->enabled = 0;
 
        /* Quiescent wait after stopping trace */
        pthread_mutex_lock(&app->sock_lock);
@@ -5184,7 +5126,11 @@ void ust_app_global_update(struct ltt_ust_session *usess, struct ust_app *app)
        if (!app->compatible) {
                return;
        }
-       if (trace_ust_pid_tracker_lookup(usess, app->pid)) {
+       if (trace_ust_id_tracker_lookup(LTTNG_TRACKER_VPID, usess, app->pid) &&
+                       trace_ust_id_tracker_lookup(
+                                       LTTNG_TRACKER_VUID, usess, app->uid) &&
+                       trace_ust_id_tracker_lookup(
+                                       LTTNG_TRACKER_VGID, usess, app->gid)) {
                /*
                 * Synchronize the application's internal tracing configuration
                 * and start tracing.
@@ -5921,6 +5867,7 @@ enum lttng_error_code ust_app_snapshot_record(
                        struct buffer_reg_channel *reg_chan;
                        struct consumer_socket *socket;
                        char pathname[PATH_MAX];
+                       size_t consumer_path_offset = 0;
 
                        if (!reg->registry->reg.ust->metadata_key) {
                                /* Skip since no metadata is present */
@@ -5936,12 +5883,8 @@ enum lttng_error_code ust_app_snapshot_record(
                        }
 
                        memset(pathname, 0, sizeof(pathname));
-                       /*
-                        * DEFAULT_UST_TRACE_UID_PATH already contains a path
-                        * separator.
-                        */
                        ret = snprintf(pathname, sizeof(pathname),
-                                       DEFAULT_UST_TRACE_DIR DEFAULT_UST_TRACE_UID_PATH,
+                                       DEFAULT_UST_TRACE_DIR "/" DEFAULT_UST_TRACE_UID_PATH,
                                        reg->uid, reg->bits_per_long);
                        if (ret < 0) {
                                PERROR("snprintf snapshot path");
@@ -5950,7 +5893,8 @@ enum lttng_error_code ust_app_snapshot_record(
                        }
                        /* Free path allowed on previous iteration. */
                        free(trace_path);
-                       trace_path = setup_channel_trace_path(usess->consumer, pathname);
+                       trace_path = setup_channel_trace_path(usess->consumer, pathname,
+                                               &consumer_path_offset);
                        if (!trace_path) {
                                status = LTTNG_ERR_INVALID;
                                goto error;
@@ -5961,7 +5905,7 @@ enum lttng_error_code ust_app_snapshot_record(
                                status = consumer_snapshot_channel(socket,
                                                reg_chan->consumer_key,
                                                output, 0, usess->uid,
-                                               usess->gid, trace_path, wait,
+                                               usess->gid, &trace_path[consumer_path_offset], wait,
                                                nb_packets_per_stream);
                                if (status != LTTNG_OK) {
                                        goto error;
@@ -5969,7 +5913,8 @@ enum lttng_error_code ust_app_snapshot_record(
                        }
                        status = consumer_snapshot_channel(socket,
                                        reg->registry->reg.ust->metadata_key, output, 1,
-                                       usess->uid, usess->gid, trace_path, wait, 0);
+                                       usess->uid, usess->gid, &trace_path[consumer_path_offset],
+                                       wait, 0);
                        if (status != LTTNG_OK) {
                                goto error;
                        }
@@ -5985,6 +5930,7 @@ enum lttng_error_code ust_app_snapshot_record(
                        struct ust_app_session *ua_sess;
                        struct ust_registry_session *registry;
                        char pathname[PATH_MAX];
+                       size_t consumer_path_offset = 0;
 
                        ua_sess = lookup_session_by_app(usess, app);
                        if (!ua_sess) {
@@ -6002,7 +5948,7 @@ enum lttng_error_code ust_app_snapshot_record(
 
                        /* Add the UST default trace dir to path. */
                        memset(pathname, 0, sizeof(pathname));
-                       ret = snprintf(pathname, sizeof(pathname), DEFAULT_UST_TRACE_DIR "%s",
+                       ret = snprintf(pathname, sizeof(pathname), DEFAULT_UST_TRACE_DIR "/%s",
                                        ua_sess->path);
                        if (ret < 0) {
                                status = LTTNG_ERR_INVALID;
@@ -6011,7 +5957,8 @@ enum lttng_error_code ust_app_snapshot_record(
                        }
                        /* Free path allowed on previous iteration. */
                        free(trace_path);
-                       trace_path = setup_channel_trace_path(usess->consumer, pathname);
+                       trace_path = setup_channel_trace_path(usess->consumer, pathname,
+                                       &consumer_path_offset);
                        if (!trace_path) {
                                status = LTTNG_ERR_INVALID;
                                goto error;
@@ -6024,7 +5971,7 @@ enum lttng_error_code ust_app_snapshot_record(
                                                                .uid,
                                                ua_sess->effective_credentials
                                                                .gid,
-                                               trace_path, wait,
+                                               &trace_path[consumer_path_offset], wait,
                                                nb_packets_per_stream);
                                switch (status) {
                                case LTTNG_OK:
@@ -6045,7 +5992,7 @@ enum lttng_error_code ust_app_snapshot_record(
                                        registry->metadata_key, output, 1,
                                        ua_sess->effective_credentials.uid,
                                        ua_sess->effective_credentials.gid,
-                                       trace_path, wait, 0);
+                                       &trace_path[consumer_path_offset], wait, 0);
                        switch (status) {
                        case LTTNG_OK:
                                break;
@@ -6325,6 +6272,11 @@ enum lttng_error_code ust_app_rotate_session(struct ltt_session *session)
                        struct buffer_reg_channel *reg_chan;
                        struct consumer_socket *socket;
 
+                       if (!reg->registry->reg.ust->metadata_key) {
+                               /* Skip since no metadata is present */
+                               continue;
+                       }
+
                        /* Get consumer socket to use to push the metadata.*/
                        socket = consumer_find_socket_by_bitness(reg->bits_per_long,
                                        usess->consumer);
@@ -6459,7 +6411,7 @@ enum lttng_error_code ust_app_create_channel_subdirectories(
 
                cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) {
                        fmt_ret = asprintf(&pathname_index,
-                                      DEFAULT_UST_TRACE_DIR DEFAULT_UST_TRACE_UID_PATH "/" DEFAULT_INDEX_DIR,
+                                      DEFAULT_UST_TRACE_DIR "/" DEFAULT_UST_TRACE_UID_PATH "/" DEFAULT_INDEX_DIR,
                                       reg->uid, reg->bits_per_long);
                        if (fmt_ret < 0) {
                                ERR("Failed to format channel index directory");
@@ -6486,6 +6438,17 @@ enum lttng_error_code ust_app_create_channel_subdirectories(
        {
                struct ust_app *app;
 
+               /*
+                * Create the toplevel ust/ directory in case no apps are running.
+                */
+               chunk_status = lttng_trace_chunk_create_subdirectory(
+                               usess->current_trace_chunk,
+                               DEFAULT_UST_TRACE_DIR);
+               if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
+                       ret = LTTNG_ERR_CREATE_DIR_FAIL;
+                       goto error;
+               }
+
                cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app,
                                pid_n.node) {
                        struct ust_app_session *ua_sess;
@@ -6504,7 +6467,7 @@ enum lttng_error_code ust_app_create_channel_subdirectories(
                        }
 
                        fmt_ret = asprintf(&pathname_index,
-                                       DEFAULT_UST_TRACE_DIR "%s/" DEFAULT_INDEX_DIR,
+                                       DEFAULT_UST_TRACE_DIR "/%s/" DEFAULT_INDEX_DIR,
                                        ua_sess->path);
                        if (fmt_ret < 0) {
                                ERR("Failed to format channel index directory");
@@ -6535,3 +6498,151 @@ error:
        rcu_read_unlock();
        return ret;
 }
+
+/*
+ * Clear all the channels of a session.
+ *
+ * Return LTTNG_OK on success or else an LTTng error code.
+ */
+enum lttng_error_code ust_app_clear_session(struct ltt_session *session)
+{
+       int ret;
+       enum lttng_error_code cmd_ret = LTTNG_OK;
+       struct lttng_ht_iter iter;
+       struct ust_app *app;
+       struct ltt_ust_session *usess = session->ust_session;
+
+       assert(usess);
+
+       rcu_read_lock();
+
+       if (usess->active) {
+               ERR("Expecting inactive session %s (%" PRIu64 ")", session->name, session->id);
+               cmd_ret = LTTNG_ERR_FATAL;
+               goto end;
+       }
+
+       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;
+
+                       /* Get consumer socket to use to push the metadata.*/
+                       socket = consumer_find_socket_by_bitness(reg->bits_per_long,
+                                       usess->consumer);
+                       if (!socket) {
+                               cmd_ret = LTTNG_ERR_INVALID;
+                               goto error_socket;
+                       }
+
+                       /* Clear the data channels. */
+                       cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter,
+                                       reg_chan, node.node) {
+                               ret = consumer_clear_channel(socket,
+                                               reg_chan->consumer_key);
+                               if (ret < 0) {
+                                       goto error;
+                               }
+                       }
+
+                       (void) push_metadata(reg->registry->reg.ust, usess->consumer);
+
+                       /*
+                        * Clear the metadata channel.
+                        * Metadata channel is not cleared per se but we still need to
+                        * perform a rotation operation on it behind the scene.
+                        */
+                       ret = consumer_clear_channel(socket,
+                                       reg->registry->reg.ust->metadata_key);
+                       if (ret < 0) {
+                               goto error;
+                       }
+               }
+               break;
+       }
+       case LTTNG_BUFFER_PER_PID:
+       {
+               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) {
+                               cmd_ret = LTTNG_ERR_INVALID;
+                               goto error_socket;
+                       }
+
+                       registry = get_session_registry(ua_sess);
+                       if (!registry) {
+                               DBG("Application session is being torn down. Skip application.");
+                               continue;
+                       }
+
+                       /* Clear the data channels. */
+                       cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter,
+                                       ua_chan, node.node) {
+                               ret = consumer_clear_channel(socket, ua_chan->key);
+                               if (ret < 0) {
+                                       /* Per-PID buffer and application going away. */
+                                       if (ret == -LTTNG_ERR_CHAN_NOT_FOUND) {
+                                               continue;
+                                       }
+                                       goto error;
+                               }
+                       }
+
+                       (void) push_metadata(registry, usess->consumer);
+
+                       /*
+                        * Clear the metadata channel.
+                        * Metadata channel is not cleared per se but we still need to
+                        * perform rotation operation on it behind the scene.
+                        */
+                       ret = consumer_clear_channel(socket, registry->metadata_key);
+                       if (ret < 0) {
+                               /* Per-PID buffer and application going away. */
+                               if (ret == -LTTNG_ERR_CHAN_NOT_FOUND) {
+                                       continue;
+                               }
+                               goto error;
+                       }
+               }
+               break;
+       }
+       default:
+               assert(0);
+               break;
+       }
+
+       cmd_ret = LTTNG_OK;
+       goto end;
+
+error:
+       switch (-ret) {
+       case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED:
+               cmd_ret = LTTNG_ERR_CLEAR_RELAY_DISALLOWED;
+               break;
+       default:
+               cmd_ret = LTTNG_ERR_CLEAR_FAIL_CONSUMER;
+       }
+
+error_socket:
+end:
+       rcu_read_unlock();
+       return cmd_ret;
+}
This page took 0.029482 seconds and 5 git commands to generate.