diff --git a/aya/src/bpf.rs b/aya/src/bpf.rs index b812f6f27..e0025f497 100644 --- a/aya/src/bpf.rs +++ b/aya/src/bpf.rs @@ -1,11 +1,9 @@ use std::{ borrow::Cow, collections::{HashMap, HashSet}, + ffi::c_int, fs, io, - os::{ - fd::{AsFd as _, AsRawFd as _}, - raw::c_int, - }, + os::fd::{AsFd as _, AsRawFd as _}, path::{Path, PathBuf}, sync::{Arc, LazyLock}, }; diff --git a/aya/src/maps/bloom_filter.rs b/aya/src/maps/bloom_filter.rs index 8e6e521e1..cbb5b02b4 100644 --- a/aya/src/maps/bloom_filter.rs +++ b/aya/src/maps/bloom_filter.rs @@ -78,7 +78,7 @@ impl, V: Pod> BloomFilter { #[cfg(test)] mod tests { - use std::io; + use std::{ffi::c_long, io}; use assert_matches::assert_matches; use libc::{EFAULT, ENOENT}; @@ -101,7 +101,7 @@ mod tests { test_utils::new_obj_map::(BPF_MAP_TYPE_BLOOM_FILTER) } - fn sys_error(value: i32) -> SysResult { + fn sys_error(value: i32) -> SysResult { Err((-1, io::Error::from_raw_os_error(value))) } diff --git a/aya/src/maps/hash_map/hash_map.rs b/aya/src/maps/hash_map/hash_map.rs index becac6c4b..307d1a513 100644 --- a/aya/src/maps/hash_map/hash_map.rs +++ b/aya/src/maps/hash_map/hash_map.rs @@ -103,7 +103,7 @@ impl, K: Pod, V: Pod> IterableMap for HashMap #[cfg(test)] mod tests { - use std::io; + use std::{ffi::c_long, io}; use assert_matches::assert_matches; use libc::{EFAULT, ENOENT}; @@ -126,7 +126,7 @@ mod tests { test_utils::new_obj_map::(BPF_MAP_TYPE_HASH) } - fn sys_error(value: i32) -> SysResult { + fn sys_error(value: i32) -> SysResult { Err((-1, io::Error::from_raw_os_error(value))) } @@ -332,7 +332,7 @@ mod tests { assert_matches!(keys, Ok(ks) if ks.is_empty()) } - fn get_next_key(attr: &bpf_attr) -> SysResult { + fn get_next_key(attr: &bpf_attr) -> SysResult { match bpf_key(attr) { None => set_next_key(attr, 10), Some(10) => set_next_key(attr, 20), @@ -344,7 +344,7 @@ mod tests { Ok(1) } - fn lookup_elem(attr: &bpf_attr) -> SysResult { + fn lookup_elem(attr: &bpf_attr) -> SysResult { match bpf_key(attr) { Some(10) => set_ret(attr, 100), Some(20) => set_ret(attr, 200), diff --git a/aya/src/maps/lpm_trie.rs b/aya/src/maps/lpm_trie.rs index 328758665..f46c8776f 100644 --- a/aya/src/maps/lpm_trie.rs +++ b/aya/src/maps/lpm_trie.rs @@ -195,7 +195,7 @@ impl, K: Pod, V: Pod> IterableMap, V> for LpmTrie>(BPF_MAP_TYPE_LPM_TRIE) } - fn sys_error(value: i32) -> SysResult { + fn sys_error(value: i32) -> SysResult { Err((-1, io::Error::from_raw_os_error(value))) } diff --git a/aya/src/maps/mod.rs b/aya/src/maps/mod.rs index f5115522b..8d756478e 100644 --- a/aya/src/maps/mod.rs +++ b/aya/src/maps/mod.rs @@ -49,7 +49,7 @@ //! implement the [Pod] trait. use std::{ borrow::Borrow, - ffi::CString, + ffi::{c_long, CString}, fmt, io, marker::PhantomData, mem, @@ -127,7 +127,7 @@ pub enum MapError { /// Map name name: String, /// Error code - code: i64, + code: c_long, #[source] /// Original io::Error io_error: io::Error, diff --git a/aya/src/programs/uprobe.rs b/aya/src/programs/uprobe.rs index c45fda84d..07a27a38a 100644 --- a/aya/src/programs/uprobe.rs +++ b/aya/src/programs/uprobe.rs @@ -2,11 +2,11 @@ use std::{ borrow::Cow, error::Error, - ffi::{CStr, OsStr, OsString}, + ffi::{c_char, CStr, OsStr, OsString}, fs, io::{self, BufRead, Cursor, Read}, mem, - os::{fd::AsFd as _, raw::c_char, unix::ffi::OsStrExt}, + os::{fd::AsFd as _, unix::ffi::OsStrExt}, path::{Path, PathBuf}, sync::LazyLock, }; diff --git a/aya/src/sys/bpf.rs b/aya/src/sys/bpf.rs index 701206f82..2502fde57 100644 --- a/aya/src/sys/bpf.rs +++ b/aya/src/sys/bpf.rs @@ -1,6 +1,6 @@ use std::{ cmp, - ffi::{c_char, CStr, CString}, + ffi::{c_char, c_long, CStr, CString}, io, iter, mem::{self, MaybeUninit}, os::fd::{AsFd as _, AsRawFd as _, BorrowedFd, FromRawFd as _, RawFd}, @@ -108,7 +108,7 @@ pub(crate) fn bpf_create_map( unsafe { fd_sys_bpf(bpf_cmd::BPF_MAP_CREATE, &mut attr) } } -pub(crate) fn bpf_pin_object(fd: BorrowedFd<'_>, path: &CStr) -> SysResult { +pub(crate) fn bpf_pin_object(fd: BorrowedFd<'_>, path: &CStr) -> SysResult { let mut attr = unsafe { mem::zeroed::() }; let u = unsafe { &mut attr.__bindgen_anon_4 }; u.bpf_fd = fd.as_raw_fd() as u32; @@ -289,7 +289,7 @@ pub(crate) fn bpf_map_update_elem( key: Option<&K>, value: &V, flags: u64, -) -> SysResult { +) -> SysResult { let mut attr = unsafe { mem::zeroed::() }; let u = unsafe { &mut attr.__bindgen_anon_2 }; @@ -307,7 +307,7 @@ pub(crate) fn bpf_map_push_elem( fd: BorrowedFd<'_>, value: &V, flags: u64, -) -> SysResult { +) -> SysResult { let mut attr = unsafe { mem::zeroed::() }; let u = unsafe { &mut attr.__bindgen_anon_2 }; @@ -323,7 +323,7 @@ pub(crate) fn bpf_map_update_elem_ptr( key: *const K, value: *mut V, flags: u64, -) -> SysResult { +) -> SysResult { let mut attr = unsafe { mem::zeroed::() }; let u = unsafe { &mut attr.__bindgen_anon_2 }; @@ -340,12 +340,12 @@ pub(crate) fn bpf_map_update_elem_per_cpu( key: &K, values: &PerCpuValues, flags: u64, -) -> SysResult { +) -> SysResult { let mut mem = values.build_kernel_mem().map_err(|e| (-1, e))?; bpf_map_update_elem_ptr(fd, key, mem.as_mut_ptr(), flags) } -pub(crate) fn bpf_map_delete_elem(fd: BorrowedFd<'_>, key: &K) -> SysResult { +pub(crate) fn bpf_map_delete_elem(fd: BorrowedFd<'_>, key: &K) -> SysResult { let mut attr = unsafe { mem::zeroed::() }; let u = unsafe { &mut attr.__bindgen_anon_2 }; @@ -377,7 +377,7 @@ pub(crate) fn bpf_map_get_next_key( } // since kernel 5.2 -pub(crate) fn bpf_map_freeze(fd: BorrowedFd<'_>) -> SysResult { +pub(crate) fn bpf_map_freeze(fd: BorrowedFd<'_>) -> SysResult { let mut attr = unsafe { mem::zeroed::() }; let u = unsafe { &mut attr.__bindgen_anon_2 }; u.map_fd = fd.as_raw_fd() as u32; @@ -453,7 +453,7 @@ pub(crate) fn bpf_link_update( new_prog_fd: BorrowedFd<'_>, old_prog_fd: Option, flags: u32, -) -> SysResult { +) -> SysResult { let mut attr = unsafe { mem::zeroed::() }; attr.link_update.link_fd = link_fd.as_raw_fd() as u32; @@ -528,7 +528,7 @@ pub(crate) fn bpf_prog_query( prog_ids: &mut [u32], prog_cnt: &mut u32, revision: &mut u64, -) -> SysResult { +) -> SysResult { let mut attr = unsafe { mem::zeroed::() }; match target { @@ -1164,7 +1164,7 @@ fn bpf_prog_load(attr: &mut bpf_attr) -> SysResult { unsafe { fd_sys_bpf(bpf_cmd::BPF_PROG_LOAD, attr) } } -fn sys_bpf(cmd: bpf_cmd, attr: &mut bpf_attr) -> SysResult { +fn sys_bpf(cmd: bpf_cmd, attr: &mut bpf_attr) -> SysResult { syscall(Syscall::Ebpf { cmd, attr }) } diff --git a/aya/src/sys/fake.rs b/aya/src/sys/fake.rs index 0e0cd3fe2..470199265 100644 --- a/aya/src/sys/fake.rs +++ b/aya/src/sys/fake.rs @@ -1,8 +1,12 @@ -use std::{cell::RefCell, ffi::c_void, io, ptr}; +use std::{ + cell::RefCell, + ffi::{c_long, c_void}, + io, ptr, +}; use super::{SysResult, Syscall}; -type SyscallFn = unsafe fn(Syscall<'_>) -> SysResult; +type SyscallFn = unsafe fn(Syscall<'_>) -> SysResult; #[cfg(test)] thread_local! { @@ -11,11 +15,11 @@ thread_local! { } #[cfg(test)] -unsafe fn test_syscall(_call: Syscall<'_>) -> SysResult { +unsafe fn test_syscall(_call: Syscall<'_>) -> SysResult { Err((-1, io::Error::from_raw_os_error(libc::EINVAL))) } #[cfg(test)] -pub(crate) fn override_syscall(call: unsafe fn(Syscall<'_>) -> SysResult) { +pub(crate) fn override_syscall(call: unsafe fn(Syscall<'_>) -> SysResult) { TEST_SYSCALL.with(|test_impl| *test_impl.borrow_mut() = call); } diff --git a/aya/src/sys/mod.rs b/aya/src/sys/mod.rs index 86f197abc..05f054034 100644 --- a/aya/src/sys/mod.rs +++ b/aya/src/sys/mod.rs @@ -8,7 +8,7 @@ mod perf_event; mod fake; use std::{ - ffi::{c_int, c_void}, + ffi::{c_int, c_long, c_void}, io, mem, os::fd::{AsRawFd as _, BorrowedFd, OwnedFd}, }; @@ -16,7 +16,7 @@ use std::{ pub(crate) use bpf::*; #[cfg(test)] pub(crate) use fake::*; -use libc::{pid_t, SYS_bpf, SYS_perf_event_open}; +use libc::{pid_t, SYS_bpf, SYS_ioctl, SYS_perf_event_open}; #[doc(hidden)] pub use netlink::netlink_set_link_up; pub(crate) use netlink::*; @@ -25,7 +25,7 @@ use thiserror::Error; use crate::generated::{bpf_attr, bpf_cmd, perf_event_attr}; -pub(crate) type SysResult = Result; +pub(crate) type SysResult = Result; pub(crate) enum Syscall<'a> { Ebpf { @@ -89,7 +89,7 @@ impl std::fmt::Debug for Syscall<'_> { } } -fn syscall(call: Syscall<'_>) -> SysResult { +fn syscall(call: Syscall<'_>) -> SysResult { #[cfg(test)] return TEST_SYSCALL.with(|test_impl| unsafe { test_impl.borrow()(call) }); @@ -108,22 +108,13 @@ fn syscall(call: Syscall<'_>) -> SysResult { flags, } => libc::syscall(SYS_perf_event_open, &attr, pid, cpu, group, flags), Syscall::PerfEventIoctl { fd, request, arg } => { - // The type of integer taken by `ioctl` is different in glibc (i64) and - // musl (i32). musl builds would complain about useless conversion. - #[allow(clippy::useless_conversion)] - let request = request.try_into().unwrap(); - let ret = libc::ioctl(fd.as_raw_fd(), request, arg); - // `libc::ioctl` returns i32 on x86_64 while `libc::syscall` returns i64. - #[allow(clippy::useless_conversion)] - ret.into() + libc::syscall(SYS_ioctl, fd.as_raw_fd(), request, arg) } } }; - // `libc::syscall` returns i32 on armv7. - #[allow(clippy::useless_conversion)] - match ret.into() { - ret @ 0.. => Ok(ret), + match ret { + 0.. => Ok(ret), ret => Err((ret, io::Error::last_os_error())), } } diff --git a/aya/src/sys/perf_event.rs b/aya/src/sys/perf_event.rs index ec8b14207..508e0c5b3 100644 --- a/aya/src/sys/perf_event.rs +++ b/aya/src/sys/perf_event.rs @@ -1,5 +1,5 @@ use std::{ - ffi::{c_int, CString, OsStr}, + ffi::{c_int, c_long, CString, OsStr}, io, mem, os::fd::{BorrowedFd, FromRawFd as _}, }; @@ -104,7 +104,11 @@ pub(crate) fn perf_event_open_trace_point( perf_event_sys(attr, pid, cpu, PERF_FLAG_FD_CLOEXEC) } -pub(crate) fn perf_event_ioctl(fd: BorrowedFd<'_>, request: c_int, arg: c_int) -> SysResult { +pub(crate) fn perf_event_ioctl( + fd: BorrowedFd<'_>, + request: c_int, + arg: c_int, +) -> SysResult { let call = Syscall::PerfEventIoctl { fd, request, arg }; #[cfg(not(test))] return syscall(call); diff --git a/xtask/public-api/aya.txt b/xtask/public-api/aya.txt index 1d77394d4..4a87a3a43 100644 --- a/xtask/public-api/aya.txt +++ b/xtask/public-api/aya.txt @@ -1336,7 +1336,7 @@ impl core::convert::From for aya::maps::Map pub fn aya::maps::Map::from(t: T) -> T pub enum aya::maps::MapError pub aya::maps::MapError::CreateError -pub aya::maps::MapError::CreateError::code: i64 +pub aya::maps::MapError::CreateError::code: core::ffi::c_long pub aya::maps::MapError::CreateError::io_error: std::io::error::Error pub aya::maps::MapError::CreateError::name: alloc::string::String pub aya::maps::MapError::ElementNotFound