#![cfg_attr(not(feature = "alloc"), allow(dead_code, unused_imports))]
use core::ffi::{c_char, c_int, c_void};
use core::time::Duration;
use crate::sys::{cvt, errno, last_error};
use crate::{Error, ErrorKind, Result};
#[repr(C)]
pub(crate) struct Sigevent {
notify: c_int,
signo: c_int,
value: *mut c_void,
notify_function: *mut c_void,
notify_attributes: *mut c_void,
}
#[repr(C)]
pub(crate) struct Aiocb {
fildes: c_int,
offset: i64,
buf: *mut c_void,
nbytes: usize,
reqprio: c_int,
sigevent: Sigevent,
lio_opcode: c_int,
}
const _: () = assert!(size_of::<Sigevent>() == 32 && align_of::<Sigevent>() == 8);
const _: () = assert!(size_of::<Aiocb>() == 80 && align_of::<Aiocb>() == 8);
const _: () = assert!(core::mem::offset_of!(Aiocb, offset) == 8);
const _: () = assert!(core::mem::offset_of!(Aiocb, buf) == 16);
const _: () = assert!(core::mem::offset_of!(Aiocb, nbytes) == 24);
const _: () = assert!(core::mem::offset_of!(Aiocb, reqprio) == 32);
const _: () = assert!(core::mem::offset_of!(Aiocb, sigevent) == 40);
const _: () = assert!(core::mem::offset_of!(Aiocb, lio_opcode) == 72);
impl Aiocb {
pub(crate) const fn new() -> Self {
Self {
fildes: -1,
offset: 0,
buf: core::ptr::null_mut(),
nbytes: 0,
reqprio: 0,
sigevent: Sigevent::new(None),
lio_opcode: 0,
}
}
pub(crate) fn set(&mut self, fd: c_int, buf: *mut u8, len: usize, offset: i64) {
self.fildes = fd;
self.offset = offset;
self.buf = buf.cast();
self.nbytes = len.min(MAX_LEN);
}
pub(crate) fn set_notice(&mut self, signal: Option<c_int>) {
self.sigevent = Sigevent::new(signal);
}
pub(crate) fn set_listed(&mut self, request: Request) {
self.lio_opcode = match request {
Request::Read => LIO_READ,
Request::Write => LIO_WRITE,
Request::Sync | Request::SyncData => LIO_NOP,
};
}
}
impl Sigevent {
const fn new(signal: Option<c_int>) -> Self {
let (notify, signo) = match signal {
Some(signal) => (SIGEV_SIGNAL, signal),
None => (SIGEV_NONE, 0),
};
Self {
notify,
signo,
value: core::ptr::null_mut(),
notify_function: core::ptr::null_mut(),
notify_attributes: core::ptr::null_mut(),
}
}
}
const MAX_LEN: usize = c_int::MAX as usize;
const SIGEV_NONE: c_int = 0;
const SIGEV_SIGNAL: c_int = 1;
const LIO_NOP: c_int = 0;
const LIO_READ: c_int = 1;
const LIO_WRITE: c_int = 2;
const LIO_NOWAIT: c_int = 1;
const LIO_WAIT: c_int = 2;
pub(crate) const LISTIO_MAX: usize = 16;
const AIO_CANCELED: c_int = 2;
const O_SYNC: c_int = 0x80;
const O_DSYNC: c_int = 0x40_0000;
#[repr(C)]
struct Timespec {
sec: i64,
nsec: i64,
}
unsafe extern "C" {
fn aio_read(cb: *mut Aiocb) -> c_int;
fn aio_write(cb: *mut Aiocb) -> c_int;
fn aio_fsync(op: c_int, cb: *mut Aiocb) -> c_int;
fn aio_error(cb: *const Aiocb) -> c_int;
fn aio_return(cb: *mut Aiocb) -> isize;
fn aio_cancel(fd: c_int, cb: *mut Aiocb) -> c_int;
fn aio_suspend(list: *const *const Aiocb, count: c_int, timeout: *const Timespec) -> c_int;
fn lio_listio(
mode: c_int,
list: *const *mut Aiocb,
count: c_int,
notice: *mut Sigevent,
) -> c_int;
fn sysctlbyname(
name: *const c_char,
old: *mut c_void,
old_len: *mut usize,
new: *mut c_void,
new_len: usize,
) -> c_int;
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) enum Request {
Read,
Write,
Sync,
SyncData,
}
pub(crate) unsafe fn submit(cb: *mut Aiocb, request: Request) -> Result<()> {
cvt(unsafe {
match request {
Request::Read => aio_read(cb),
Request::Write => aio_write(cb),
Request::Sync => aio_fsync(O_SYNC, cb),
Request::SyncData => aio_fsync(O_DSYNC, cb),
}
})?;
Ok(())
}
pub(crate) unsafe fn submit_list(
list: &[*mut Aiocb],
wait: bool,
notice: Option<c_int>,
) -> Result<()> {
let count = c_int::try_from(list.len()).map_err(|_| Error::from(ErrorKind::InvalidInput))?;
let mut notice = notice
.filter(|_| !wait)
.map(|signal| Sigevent::new(Some(signal)));
let notice = notice
.as_mut()
.map_or(core::ptr::null_mut(), |notice| notice as *mut _);
let mode = if wait { LIO_WAIT } else { LIO_NOWAIT };
cvt(unsafe { lio_listio(mode, list.as_ptr(), count, notice) })?;
Ok(())
}
pub(crate) unsafe fn submitted(cb: *const Aiocb) -> bool {
unsafe { aio_error(cb) != -1 }
}
pub(crate) unsafe fn take(cb: *mut Aiocb) -> Option<Result<usize>> {
let error = unsafe { aio_error(cb) };
if error == errno::EINPROGRESS {
return None;
}
if error == -1 {
return Some(Err(last_error()));
}
let count = unsafe { aio_return(cb) };
Some(if error != 0 {
Err(Error::from_raw_os_error(error))
} else if count < 0 {
Err(last_error())
} else {
Ok(count as usize)
})
}
pub(crate) unsafe fn cancel(fd: c_int, cb: *mut Aiocb) -> Result<bool> {
let answer = cvt(unsafe { aio_cancel(fd, cb) })?;
Ok(answer == AIO_CANCELED)
}
pub(crate) unsafe fn suspend(list: &[*const Aiocb], timeout: Option<Duration>) -> Result<bool> {
let timeout = timeout.map(|timeout| Timespec {
sec: i64::try_from(timeout.as_secs()).unwrap_or(i64::MAX),
nsec: timeout.subsec_nanos().into(),
});
let timeout = timeout
.as_ref()
.map_or(core::ptr::null(), |t| t as *const _);
let count = c_int::try_from(list.len()).map_err(|_| Error::from(ErrorKind::InvalidInput))?;
match cvt(unsafe { aio_suspend(list.as_ptr(), count, timeout) }) {
Ok(_) => Ok(true),
Err(error) if error.raw_os_error() == errno::EAGAIN => Ok(false),
Err(error) => Err(error),
}
}
fn limit(name: &core::ffi::CStr) -> Result<u32> {
let mut value: c_int = 0;
let mut len = size_of::<c_int>();
cvt(unsafe {
sysctlbyname(
name.as_ptr(),
(&raw mut value).cast(),
&raw mut len,
core::ptr::null_mut(),
0,
)
})?;
u32::try_from(value).map_err(|_| ErrorKind::Other.into())
}
pub(crate) fn limits() -> Result<(u32, u32, u32)> {
Ok((
limit(c"kern.aiomax")?,
limit(c"kern.aioprocmax")?,
limit(c"kern.aiothreads")?,
))
}