Blame SOURCES/0004-thin_dump-Fix-leaked-shared-object-handle.patch

85793c
From 7fbd79cab2e548eb388bc782dd39f1cde027173b Mon Sep 17 00:00:00 2001
85793c
From: Ming-Hung Tsai <mtsai@redhat.com>
85793c
Date: Tue, 1 Jun 2021 23:37:36 +0800
85793c
Subject: [PATCH 04/10] [thin_dump] Fix leaked shared object handle
85793c
85793c
---
85793c
 thin-provisioning/shared_library_emitter.cc | 113 ++++++++++++++++++++++++----
85793c
 1 file changed, 100 insertions(+), 13 deletions(-)
85793c
85793c
diff --git a/thin-provisioning/shared_library_emitter.cc b/thin-provisioning/shared_library_emitter.cc
85793c
index 58f12d2..2e845f3 100644
85793c
--- a/thin-provisioning/shared_library_emitter.cc
85793c
+++ b/thin-provisioning/shared_library_emitter.cc
85793c
@@ -8,22 +8,109 @@ using namespace thin_provisioning;
85793c
 
85793c
 //----------------------------------------------------------------
85793c
 
85793c
-emitter::ptr
85793c
-thin_provisioning::create_custom_emitter(string const &shared_lib, ostream &out)
85793c
-{
85793c
-	emitter::ptr (*create_fn)(ostream &out;;
85793c
-	void *handle = dlopen(shared_lib.c_str(), RTLD_LAZY);
85793c
-	if (!handle)
85793c
-		throw runtime_error(dlerror());
85793c
+struct shared_object {
85793c
+public:
85793c
+	shared_object(const char *shared_lib) {
85793c
+		handle_ = dlopen(shared_lib, RTLD_LAZY);
85793c
+		if (!handle_)
85793c
+			throw runtime_error(dlerror());
85793c
+
85793c
+		dlerror();    // Clear any existing error
85793c
+	}
85793c
+
85793c
+	virtual ~shared_object() {
85793c
+		dlclose(handle_);
85793c
+	}
85793c
+
85793c
+	void *get_symbol(const char *symbol) {
85793c
+		void *sym = dlsym(handle_, symbol);
85793c
+
85793c
+		char *error = dlerror();
85793c
+		if (error)
85793c
+			throw runtime_error(error);
85793c
+
85793c
+		return sym;
85793c
+	}
85793c
+
85793c
+	void *handle_;
85793c
+};
85793c
+
85793c
+class shared_emitter : public emitter {
85793c
+public:
85793c
+	shared_emitter(const char *shared_lib, ostream &out): sobj_(shared_lib) {
85793c
+		emitter::ptr (*create_fn)(ostream &out;;
85793c
+		create_fn = reinterpret_cast<emitter::ptr (*)(ostream &)>(
85793c
+				sobj_.get_symbol("create_emitter"));
85793c
+		inner_ = create_fn(out);
85793c
+	}
85793c
+
85793c
+	virtual ~shared_emitter() {
85793c
+	}
85793c
+
85793c
+	void begin_superblock(std::string const &uuid,
85793c
+			      uint64_t time,
85793c
+			      uint64_t trans_id,
85793c
+			      boost::optional<uint32_t> flags,
85793c
+			      boost::optional<uint32_t> version,
85793c
+			      uint32_t data_block_size,
85793c
+			      uint64_t nr_data_blocks,
85793c
+			      boost::optional<uint64_t> metadata_snap) {
85793c
+		inner_->begin_superblock(uuid,
85793c
+					 time,
85793c
+					 trans_id,
85793c
+					 flags,
85793c
+					 version,
85793c
+					 data_block_size,
85793c
+					 nr_data_blocks,
85793c
+					 metadata_snap);
85793c
+	}
85793c
 
85793c
-	dlerror();    // Clear any existing error
85793c
-	create_fn = reinterpret_cast<emitter::ptr (*)(ostream &)>(dlsym(handle, "create_emitter"));
85793c
+	void end_superblock() {
85793c
+		inner_->end_superblock();
85793c
+	}
85793c
 
85793c
-	char *error = dlerror();
85793c
-	if (error)
85793c
-		throw runtime_error(error);
85793c
+	void begin_device(uint32_t dev_id,
85793c
+			  uint64_t mapped_blocks,
85793c
+			  uint64_t trans_id,
85793c
+			  uint64_t creation_time,
85793c
+			  uint64_t snap_time) {
85793c
+		inner_->begin_device(dev_id, mapped_blocks, trans_id, creation_time, snap_time);
85793c
+	}
85793c
 
85793c
-	return create_fn(out);
85793c
+	void end_device() {
85793c
+		inner_->end_device();
85793c
+	}
85793c
+
85793c
+	void begin_named_mapping(std::string const &name) {
85793c
+		inner_->begin_named_mapping(name);
85793c
+	}
85793c
+
85793c
+	void end_named_mapping() {
85793c
+		inner_->end_named_mapping();
85793c
+	}
85793c
+
85793c
+	void identifier(std::string const &name) {
85793c
+		inner_->identifier(name);
85793c
+	}
85793c
+
85793c
+	void range_map(uint64_t origin_begin, uint64_t data_begin, uint32_t time, uint64_t len) {
85793c
+		inner_->range_map(origin_begin, data_begin, time, len);
85793c
+	}
85793c
+
85793c
+	void single_map(uint64_t origin_block, uint64_t data_block, uint32_t time) {
85793c
+		inner_->single_map(origin_block, data_block, time);
85793c
+	}
85793c
+
85793c
+	shared_object sobj_;
85793c
+	emitter::ptr inner_;
85793c
+};
85793c
+
85793c
+//----------------------------------------------------------------
85793c
+
85793c
+emitter::ptr
85793c
+thin_provisioning::create_custom_emitter(string const &shared_lib, ostream &out)
85793c
+{
85793c
+	return emitter::ptr(new shared_emitter(shared_lib.c_str(), out));
85793c
 }
85793c
 
85793c
 //----------------------------------------------------------------
85793c
-- 
85793c
1.8.3.1
85793c