Fix: use a free running channel key between sessiond and kernel consumer
[lttng-tools.git] / src / bin / lttng-sessiond / cmd.c
index 1edcffe79ce13552d688638162068f8085016a1a..68c013bd308097bf71fc07af79fdb85fc3270233 100644 (file)
@@ -51,6 +51,7 @@
 #include "buffer-registry.h"
 #include "notification-thread.h"
 #include "notification-thread-commands.h"
+#include "agent-thread.h"
 
 #include "cmd.h"
 
@@ -167,14 +168,14 @@ static int get_kernel_runtime_stats(struct ltt_session *session,
                goto end;
        }
 
-       ret = consumer_get_discarded_events(session->id, kchan->fd,
+       ret = consumer_get_discarded_events(session->id, kchan->key,
                        session->kernel_session->consumer,
                        discarded_events);
        if (ret < 0) {
                goto end;
        }
 
-       ret = consumer_get_lost_packets(session->id, kchan->fd,
+       ret = consumer_get_lost_packets(session->id, kchan->key,
                        session->kernel_session->consumer,
                        lost_packets);
        if (ret < 0) {
@@ -877,8 +878,11 @@ static int create_connect_relayd(struct lttng_uri *uri,
 
                /* Check relayd version */
                ret = relayd_version_check(rsock);
-               if (ret < 0) {
-                       ret = LTTNG_ERR_RELAYD_VERSION_FAIL;
+               if (ret == LTTNG_ERR_RELAYD_VERSION_FAIL) {
+                       goto close_sock;
+               } else if (ret < 0) {
+                       ERR("Unable to reach lttng-relayd");
+                       ret = LTTNG_ERR_RELAYD_CONNECT_FAIL;
                        goto close_sock;
                }
                consumer->relay_major_version = rsock->major;
@@ -907,6 +911,8 @@ error:
 
 /*
  * Connect to the relayd using URI and send the socket to the right consumer.
+ *
+ * The consumer socket lock must be held by the caller.
  */
 static int send_consumer_relayd_socket(enum lttng_domain_type domain,
                unsigned int session_id, struct lttng_uri *relayd_uri,
@@ -979,6 +985,8 @@ relayd_comm_error:
  * Send both relayd sockets to a specific consumer and domain.  This is a
  * helper function to facilitate sending the information to the consumer for a
  * session.
+ *
+ * The consumer socket lock must be held by the caller.
  */
 static int send_consumer_relayd_sockets(enum lttng_domain_type domain,
                unsigned int session_id, struct consumer_output *consumer,
@@ -1373,9 +1381,15 @@ int cmd_enable_channel(struct ltt_session *session,
                break;
        }
        case LTTNG_DOMAIN_UST:
+               break;
        case LTTNG_DOMAIN_JUL:
        case LTTNG_DOMAIN_LOG4J:
        case LTTNG_DOMAIN_PYTHON:
+               if (!agent_tracing_is_enabled()) {
+                       DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
+                       ret = LTTNG_ERR_AGENT_TRACING_DISABLED;
+                       goto error;
+               }
                break;
        default:
                ret = LTTNG_ERR_UNKNOWN_DOMAIN;
@@ -1650,6 +1664,16 @@ int cmd_add_context(struct ltt_session *session, enum lttng_domain_type domain,
        int ret, chan_kern_created = 0, chan_ust_created = 0;
        char *app_ctx_provider_name = NULL, *app_ctx_name = NULL;
 
+       /*
+        * Don't try to add a context if the session has been started at
+        * some point in time before. The tracer does not allow it and would
+        * result in a corrupted trace.
+        */
+       if (session->has_been_started) {
+               ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
+               goto end;
+       }
+
        if (ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
                app_ctx_provider_name = ctx->u.app_ctx.provider_name;
                app_ctx_name = ctx->u.app_ctx.ctx_name;
@@ -2073,6 +2097,12 @@ static int _cmd_enable_event(struct ltt_session *session,
 
                assert(usess);
 
+               if (!agent_tracing_is_enabled()) {
+                       DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
+                       ret = LTTNG_ERR_AGENT_TRACING_DISABLED;
+                       goto error;
+               }
+
                agt = trace_ust_find_agent(usess, domain->type);
                if (!agt) {
                        agt = agent_create(domain->type);
@@ -3403,10 +3433,12 @@ static
 int clear_metadata_file(int fd)
 {
        int ret;
+       off_t lseek_ret;
 
-       ret = lseek(fd, 0, SEEK_SET);
-       if (ret < 0) {
+       lseek_ret = lseek(fd, 0, SEEK_SET);
+       if (lseek_ret < 0) {
                PERROR("lseek");
+               ret = -1;
                goto end;
        }
 
@@ -3706,10 +3738,12 @@ static int set_relayd_for_snapshot(struct consumer_output *consumer,
        rcu_read_lock();
        cds_lfht_for_each_entry(snap_output->consumer->socks->ht, &iter.iter,
                        socket, node.node) {
+               pthread_mutex_lock(socket->lock);
                ret = send_consumer_relayd_sockets(0, session->id,
                                snap_output->consumer, socket,
                                session->name, session->hostname,
                                session->live_timer);
+               pthread_mutex_unlock(socket->lock);
                if (ret != LTTNG_OK) {
                        rcu_read_unlock();
                        goto error;
@@ -3812,11 +3846,13 @@ static int record_ust_snapshot(struct ltt_ust_session *usess,
        }
 
        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;
 }
 
This page took 0.026449 seconds and 5 git commands to generate.