relayd fix: trace chunk is reclaimed before close command
authorJérémie Galarneau <jeremie.galarneau@efficios.com>
Fri, 9 Aug 2019 15:18:47 +0000 (11:18 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Thu, 5 Sep 2019 20:39:15 +0000 (16:39 -0400)
The relay daemon control protocol defines a trace chunk close command
which allows the tracer to express the 'end' time bound of a trace
chunk.

However, in the event of a session rotation, the last reference to
such a trace chunk can be released before the close command is
received. This prevents the trace chunk from being renamed and moved
at the completion of a rotation.

A reference to a 'pending closure' trace chunk is kept as part of the
relay session object until its 'close' command is received.

Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
src/bin/lttng-relayd/main.c
src/bin/lttng-relayd/session.c
src/bin/lttng-relayd/session.h

index 82e2603544c9b770ec0f60a1344f456835ddb76e..a3e2509d2865f733fc81675c1272a92136eae4c3 100644 (file)
@@ -2423,11 +2423,22 @@ static int relay_create_trace_chunk(const struct lttcomm_relayd_hdr *recv_hdr,
        }
 
        pthread_mutex_lock(&conn->session->lock);
-       lttng_trace_chunk_put(conn->session->current_trace_chunk);
+       if (conn->session->pending_closure_trace_chunk) {
+               /*
+                * Invalid; this means a second create_trace_chunk command was
+                * received before a close_trace_chunk.
+                */
+               ERR("Invalid trace chunk close command received; a trace chunk is already waiting for a trace chunk close command");
+               reply_code = LTTNG_ERR_INVALID_PROTOCOL;
+               ret = -1;
+               goto end_unlock_session;
+       }
+       conn->session->pending_closure_trace_chunk =
+                       conn->session->current_trace_chunk;
        conn->session->current_trace_chunk = published_chunk;
        published_chunk = NULL;
+end_unlock_session:
        pthread_mutex_unlock(&conn->session->lock);
-
 end:
        reply.ret_code = htobe32((uint32_t) reply_code);
        send_ret = conn->sock->ops->sendmsg(conn->sock,
@@ -2512,13 +2523,23 @@ static int relay_close_trace_chunk(const struct lttcomm_relayd_hdr *recv_hdr,
                goto end;
        }
 
+       pthread_mutex_lock(&session->lock);
+       if (session->pending_closure_trace_chunk &&
+                       session->pending_closure_trace_chunk != chunk) {
+               ERR("Trace chunk close command for session \"%s\" does not target the trace chunk pending closure",
+                               session->session_name);
+               reply_code = LTTNG_ERR_INVALID_PROTOCOL;
+               ret = -1;
+               goto end_unlock_session;
+       }
+
        chunk_status = lttng_trace_chunk_set_close_timestamp(
                        chunk, close_timestamp);
        if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
                ERR("Failed to set trace chunk close timestamp");
                ret = -1;
                reply_code = LTTNG_ERR_UNK;
-               goto end;
+               goto end_unlock_session;
        }
 
        if (close_command.is_set) {
@@ -2527,11 +2548,10 @@ static int relay_close_trace_chunk(const struct lttcomm_relayd_hdr *recv_hdr,
                if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
                        ret = -1;
                        reply_code = LTTNG_ERR_INVALID;
-                       goto end;
+                       goto end_unlock_session;
                }
        }
 
-       pthread_mutex_lock(&session->lock);
        if (session->current_trace_chunk == chunk) {
                /*
                 * After a trace chunk close command, no new streams
@@ -2544,6 +2564,9 @@ static int relay_close_trace_chunk(const struct lttcomm_relayd_hdr *recv_hdr,
                lttng_trace_chunk_put(session->current_trace_chunk);
                session->current_trace_chunk = NULL;
        }
+       lttng_trace_chunk_put(session->pending_closure_trace_chunk);
+       session->pending_closure_trace_chunk = NULL;
+end_unlock_session:
        pthread_mutex_unlock(&session->lock);
 
 end:
index 0997fdd6579469098f0db94eb8d31fbfc0ef183c..28e75f1dc1804c3f3e3740bca3a7a4e3e9fd9548 100644 (file)
@@ -245,6 +245,8 @@ static void destroy_session(struct relay_session *session)
        assert(!ret);
        lttng_trace_chunk_put(session->current_trace_chunk);
        session->current_trace_chunk = NULL;
+       lttng_trace_chunk_put(session->pending_closure_trace_chunk);
+       session->pending_closure_trace_chunk = NULL;
        ret = sessiond_trace_chunk_registry_session_destroyed(
                        sessiond_trace_chunk_registry, session->sessiond_uuid);
        assert(!ret);
index 8b8ae1f2118d33e31ae989b72488aa3c515b6f10..6442c17ee4522f92a08d82d31aaa820b8cc5ff52 100644 (file)
@@ -120,6 +120,7 @@ struct relay_session {
         */
        struct cds_list_head viewer_session_node;
        struct lttng_trace_chunk *current_trace_chunk;
+       struct lttng_trace_chunk *pending_closure_trace_chunk;
        struct rcu_head rcu_node;       /* For call_rcu teardown. */
 };
 
This page took 0.035559 seconds and 4 git commands to generate.