#![doc(
html_logo_url = "https://aya-rs.dev/assets/images/crabby.svg",
html_favicon_url = "https://aya-rs.dev/assets/images/crabby.svg"
)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![deny(missing_docs)]
mod bpf;
pub mod maps;
pub mod pin;
pub mod programs;
pub mod sys;
pub mod util;
use std::os::fd::{AsFd, AsRawFd, BorrowedFd, FromRawFd, OwnedFd, RawFd};
pub use aya_obj::btf::{Btf, BtfError};
pub use bpf::*;
pub use object::Endianness;
#[doc(hidden)]
pub use sys::netlink_set_link_up;
#[derive(Debug)]
struct MockableFd {
#[cfg(not(test))]
fd: OwnedFd,
#[cfg(test)]
fd: Option<OwnedFd>,
}
impl MockableFd {
#[cfg(test)]
const fn mock_signed_fd() -> i32 {
1337
}
#[cfg(test)]
const fn mock_unsigned_fd() -> u32 {
1337
}
#[cfg(not(test))]
const fn from_fd(fd: OwnedFd) -> Self {
Self { fd }
}
#[cfg(test)]
const fn from_fd(fd: OwnedFd) -> Self {
let fd = Some(fd);
Self { fd }
}
#[cfg(not(test))]
const fn inner(&self) -> &OwnedFd {
let Self { fd } = self;
fd
}
#[cfg(test)]
const fn inner(&self) -> &OwnedFd {
let Self { fd } = self;
fd.as_ref().unwrap()
}
#[cfg(not(test))]
fn into_inner(self) -> OwnedFd {
self.fd
}
#[cfg(test)]
fn into_inner(mut self) -> OwnedFd {
self.fd.take().unwrap()
}
fn try_clone(&self) -> std::io::Result<Self> {
let fd = self.inner();
let fd = fd.try_clone()?;
Ok(Self::from_fd(fd))
}
}
impl<T> From<T> for MockableFd
where
OwnedFd: From<T>,
{
fn from(value: T) -> Self {
let fd = OwnedFd::from(value);
Self::from_fd(fd)
}
}
impl AsFd for MockableFd {
fn as_fd(&self) -> BorrowedFd<'_> {
self.inner().as_fd()
}
}
impl AsRawFd for MockableFd {
fn as_raw_fd(&self) -> RawFd {
self.inner().as_raw_fd()
}
}
impl FromRawFd for MockableFd {
unsafe fn from_raw_fd(fd: RawFd) -> Self {
let fd = unsafe { OwnedFd::from_raw_fd(fd) };
Self::from_fd(fd)
}
}
#[cfg(test)]
impl Drop for MockableFd {
fn drop(&mut self) {
use std::os::fd::{AsRawFd as _, IntoRawFd as _};
let Self { fd } = self;
let fd = fd.take().unwrap();
if fd.as_raw_fd() < Self::mock_signed_fd() {
drop(fd)
} else {
let _raw_fd = fd.into_raw_fd();
}
}
}