From 4af9d1bd3ea8dd638bddeb2ae2a8ccea6d11b249 Mon Sep 17 00:00:00 2001 From: William Findlay Date: Fri, 22 Oct 2021 11:33:38 -0400 Subject: [PATCH] aya: move mmap from perf_buffer.rs to sys/mod.rs mmap() is needed for the ring buffer implementation, so move it to a common module --- aya/src/maps/perf/perf_buffer.rs | 23 ++--------------------- aya/src/sys/mod.rs | 18 +++++++++++++++++- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/aya/src/maps/perf/perf_buffer.rs b/aya/src/maps/perf/perf_buffer.rs index c3248ab9..c50bbb86 100644 --- a/aya/src/maps/perf/perf_buffer.rs +++ b/aya/src/maps/perf/perf_buffer.rs @@ -1,5 +1,5 @@ use std::{ - ffi::{c_int, c_void}, + ffi::c_void, io, mem, os::fd::{AsFd, AsRawFd, BorrowedFd, OwnedFd, RawFd}, ptr, slice, @@ -15,7 +15,7 @@ use crate::{ perf_event_header, perf_event_mmap_page, perf_event_type::{PERF_RECORD_LOST, PERF_RECORD_SAMPLE}, }, - sys::{perf_event_ioctl, perf_event_open_bpf, SysResult}, + sys::{mmap, perf_event_ioctl, perf_event_open_bpf, SysResult}, PERF_EVENT_IOC_DISABLE, PERF_EVENT_IOC_ENABLE, }; @@ -282,25 +282,6 @@ impl Drop for PerfBuffer { } } -#[cfg_attr(test, allow(unused_variables))] -unsafe fn mmap( - addr: *mut c_void, - len: usize, - prot: c_int, - flags: c_int, - fd: BorrowedFd<'_>, - offset: libc::off_t, -) -> *mut c_void { - #[cfg(not(test))] - return libc::mmap(addr, len, prot, flags, fd.as_raw_fd(), offset); - - #[cfg(test)] - use crate::sys::TEST_MMAP_RET; - - #[cfg(test)] - TEST_MMAP_RET.with(|ret| *ret.borrow()) -} - #[derive(Debug)] #[repr(C)] struct Sample { diff --git a/aya/src/sys/mod.rs b/aya/src/sys/mod.rs index b7aac263..0b22463a 100644 --- a/aya/src/sys/mod.rs +++ b/aya/src/sys/mod.rs @@ -6,7 +6,7 @@ mod perf_event; mod fake; use std::{ - ffi::{c_int, c_long}, + ffi::{c_int, c_long, c_void}, io, mem, os::fd::{AsRawFd as _, BorrowedFd}, }; @@ -115,3 +115,19 @@ fn syscall(call: Syscall<'_>) -> SysResult { ret => Err((ret, io::Error::last_os_error())), } } + +#[cfg_attr(test, allow(unused_variables))] +pub(crate) unsafe fn mmap( + addr: *mut c_void, + len: usize, + prot: c_int, + flags: c_int, + fd: BorrowedFd<'_>, + offset: libc::off_t, +) -> *mut c_void { + #[cfg(not(test))] + return libc::mmap(addr, len, prot, flags, fd.as_raw_fd(), offset); + + #[cfg(test)] + TEST_MMAP_RET.with(|ret| *ret.borrow()) +}