Fix: consumerd: crash occurs when taking snapshot of ust channel
[lttng-tools.git] / src / common / consumer / consumer-metadata-cache.c
index 3211ec1e84878d7735fe2280891719fd9fd308d6..3fffe53397b368f57e9ddc1e81257bfb50342e94 100644 (file)
@@ -89,7 +89,7 @@ void metadata_cache_reset(struct consumer_metadata_cache *cache)
  */
 static
 int metadata_cache_check_version(struct consumer_metadata_cache *cache,
-               struct lttng_consumer_channel *channel, uint64_t version)
+               uint64_t version)
 {
        int ret = 0;
 
@@ -105,6 +105,41 @@ end:
        return ret;
 }
 
+/*
+ * Write a character on the metadata poll pipe to wake the metadata thread.
+ * Returns 0 on success, -1 on error.
+ */
+int consumer_metadata_wakeup_pipe(const struct lttng_consumer_channel *channel)
+{
+       int ret = 0;
+       const char dummy = 'c';
+
+       if (channel->monitor && channel->metadata_stream) {
+               ssize_t write_ret;
+
+               write_ret = lttng_write(channel->metadata_stream->ust_metadata_poll_pipe[1],
+                               &dummy, 1);
+               if (write_ret < 1) {
+                       if (errno == EWOULDBLOCK) {
+                               /*
+                                * This is fine, the metadata poll thread
+                                * is having a hard time keeping-up, but
+                                * it will eventually wake-up and consume
+                                * the available data.
+                                */
+                               ret = 0;
+                        } else {
+                               PERROR("Wake-up UST metadata pipe");
+                               ret = -1;
+                               goto end;
+                        }
+                }
+       }
+
+end:
+       return ret;
+}
+
 /*
  * Write metadata to the cache, extend the cache if necessary. We support
  * overlapping updates, but they need to be contiguous. Send the
@@ -118,7 +153,6 @@ int consumer_metadata_cache_write(struct lttng_consumer_channel *channel,
                char *data)
 {
        int ret = 0;
-       int size_ret;
        struct consumer_metadata_cache *cache;
 
        assert(channel);
@@ -126,7 +160,7 @@ int consumer_metadata_cache_write(struct lttng_consumer_channel *channel,
 
        cache = channel->metadata_cache;
 
-       ret = metadata_cache_check_version(cache, channel, version);
+       ret = metadata_cache_check_version(cache, version);
        if (ret < 0) {
                goto end;
        }
@@ -144,18 +178,8 @@ int consumer_metadata_cache_write(struct lttng_consumer_channel *channel,
 
        memcpy(cache->data + offset, data, len);
        if (offset + len > cache->max_offset) {
-               char dummy = 'c';
-
                cache->max_offset = offset + len;
-               if (channel->monitor) {
-                       size_ret = lttng_write(channel->metadata_stream->ust_metadata_poll_pipe[1],
-                                       &dummy, 1);
-                       if (size_ret < 1) {
-                               ERR("Wakeup UST metadata pipe");
-                               ret = -1;
-                               goto end;
-                       }
-               }
+               ret = consumer_metadata_wakeup_pipe(channel);
        }
 
 end:
@@ -249,17 +273,20 @@ int consumer_metadata_cache_flushed(struct lttng_consumer_channel *channel,
                pthread_mutex_lock(&channel->lock);
        }
        pthread_mutex_lock(&channel->timer_lock);
-       pthread_mutex_lock(&channel->metadata_cache->lock);
-
        metadata_stream = channel->metadata_stream;
-
        if (!metadata_stream) {
                /*
                 * Having no metadata stream means the channel is being destroyed so there
                 * is no cache to flush anymore.
                 */
                ret = 0;
-       } else if (metadata_stream->ust_metadata_pushed >= offset) {
+               goto end_unlock_channel;
+       }
+
+       pthread_mutex_lock(&metadata_stream->lock);
+       pthread_mutex_lock(&channel->metadata_cache->lock);
+
+       if (metadata_stream->ust_metadata_pushed >= offset) {
                ret = 0;
        } else if (channel->metadata_stream->endpoint_status !=
                        CONSUMER_ENDPOINT_ACTIVE) {
@@ -271,6 +298,8 @@ int consumer_metadata_cache_flushed(struct lttng_consumer_channel *channel,
        }
 
        pthread_mutex_unlock(&channel->metadata_cache->lock);
+       pthread_mutex_unlock(&metadata_stream->lock);
+end_unlock_channel:
        pthread_mutex_unlock(&channel->timer_lock);
        if (!timer) {
                pthread_mutex_unlock(&channel->lock);
This page took 0.026383 seconds and 5 git commands to generate.