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

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