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

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