Skip to content

Commit 08b68f6

Browse files
d-e-s-odanielocfb
authored andcommitted
Add AsFd impl for Link type
This change introduces an AsFd implementation for the Link type, similar to what we already have for Map and Program. Signed-off-by: Daniel Müller <[email protected]>
1 parent a00cf5d commit 08b68f6

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

libbpf-rs/src/link.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
use std::fmt::Debug;
2+
use std::os::unix::io::AsFd;
3+
use std::os::unix::io::BorrowedFd;
24
use std::path::Path;
35
use std::path::PathBuf;
46
use std::ptr::NonNull;
@@ -108,6 +110,17 @@ impl Link {
108110
}
109111
}
110112

113+
impl AsFd for Link {
114+
#[inline]
115+
fn as_fd(&self) -> BorrowedFd<'_> {
116+
let fd = unsafe { libbpf_sys::bpf_link__fd(self.ptr.as_ptr()) };
117+
// SAFETY: `bpf_link__fd` always returns a valid fd and the underlying
118+
// libbpf object is not destroyed until the object is dropped,
119+
// which means the fd remains valid as well.
120+
unsafe { BorrowedFd::borrow_raw(fd) }
121+
}
122+
}
123+
111124
impl Drop for Link {
112125
fn drop(&mut self) {
113126
let _ = unsafe { libbpf_sys::bpf_link__destroy(self.ptr.as_ptr()) };

0 commit comments

Comments
 (0)