From 623579a47f1fd169ba9503bd71550c3fcce76b21 Mon Sep 17 00:00:00 2001 From: Dave Tucker Date: Sat, 18 Jun 2022 13:32:50 +0100 Subject: [PATCH] aya: Add Map::fd() function to return a MapFd Signed-off-by: Dave Tucker --- aya/src/maps/mod.rs | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/aya/src/maps/mod.rs b/aya/src/maps/mod.rs index dcbca570..d717121f 100644 --- a/aya/src/maps/mod.rs +++ b/aya/src/maps/mod.rs @@ -33,8 +33,15 @@ //! versa. Because of that, all map values must be plain old data and therefore //! implement the [Pod] trait. use std::{ - convert::TryFrom, ffi::CString, io, marker::PhantomData, mem, ops::Deref, os::unix::io::RawFd, - path::Path, ptr, + convert::TryFrom, + ffi::CString, + io, + marker::PhantomData, + mem, + ops::Deref, + os::unix::{io::RawFd, prelude::AsRawFd}, + path::Path, + ptr, }; use thiserror::Error; @@ -206,6 +213,15 @@ pub enum MapError { }, } +/// A map file descriptor. +pub struct MapFd(RawFd); + +impl AsRawFd for MapFd { + fn as_raw_fd(&self) -> RawFd { + self.0 + } +} + /// A generic handle to a BPF map. /// /// You should never need to use this unless you're implementing a new map type. @@ -295,6 +311,13 @@ impl Map { self.pinned = true; Ok(()) } + + /// Returns the file descriptor of the map. + /// + /// Can be converted to [`RawFd`] using [`AsRawFd`]. + pub fn fd(&self) -> Option { + self.fd.map(MapFd) + } } impl Drop for Map {