From 893f9f44a22d4c78d86107cdac1204c4da65938a Mon Sep 17 00:00:00 2001 From: Andrew Stoycos Date: Mon, 3 Oct 2022 12:28:51 -0400 Subject: [PATCH] Implement Copy for MapData Implement Copy for MapData so that when `take_map` is used we create a 1 to 1 mapping of MapData to internal FileDescriptor. This will ensure that when MapData is used in multiple tasks that we don't drop the FD before all tasks are done using it. Signed-off-by: Andrew Stoycos --- aya/src/maps/mod.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/aya/src/maps/mod.rs b/aya/src/maps/mod.rs index 9e69e3af..4abcf3af 100644 --- a/aya/src/maps/mod.rs +++ b/aya/src/maps/mod.rs @@ -413,7 +413,7 @@ impl_try_from_map_generic_key_and_value!(HashMap, PerCpuHashMap, LpmTrie); /// A generic handle to a BPF map. /// /// You should never need to use this unless you're implementing a new map type. -#[derive(Debug, Clone)] +#[derive(Debug)] pub struct MapData { pub(crate) obj: obj::Map, pub(crate) fd: Option, @@ -577,6 +577,22 @@ impl Drop for MapData { } } +impl Clone for MapData { + fn clone(&self) -> MapData { + MapData { + obj: self.obj.clone(), + fd: { + if let Some(fd) = self.fd { + unsafe { Some(libc::dup(fd)) }; + } + None + }, + btf_fd: self.btf_fd, + pinned: self.pinned, + } + } +} + /// An iterable map pub trait IterableMap { /// Get a generic map handle