Skip to content

Commit

Permalink
Mark preadv2 a safe function to call
Browse files Browse the repository at this point in the history
Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
  • Loading branch information
NobodyXu committed Apr 25, 2024
1 parent 181591c commit 9087cbe
Showing 1 changed file with 31 additions and 29 deletions.
60 changes: 31 additions & 29 deletions src/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,33 +410,37 @@ mod linux {
}
}

unsafe fn preadv2(fd: c_int, iov: &iovec) -> ssize_t {
fn preadv2(fd: c_int, iov: &iovec) -> ssize_t {
let iovcnt: c_int = 1;

#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
let res = syscall(
SYS_preadv2,
fd,
iov,
iovcnt,
-1 as off_t,
0 as off_t,
RWF_NOWAIT,
);
let res = unsafe {
syscall(
SYS_preadv2,
fd,
iov,
iovcnt,
-1 as off_t,
0 as off_t,
RWF_NOWAIT,
)
};

#[cfg(all(target_arch = "x86_64", target_pointer_width = "32"))]
let res = syscall(SYS_preadv2, fd, iov, iovcnt, -1 as off_t, RWF_NOWAIT);
let res = unsafe { syscall(SYS_preadv2, fd, iov, iovcnt, -1 as off_t, RWF_NOWAIT) };

#[cfg(not(target_arch = "x86_64"))]
let res = syscall(
SYS_preadv2,
fd,
iov,
iovcnt,
-1 as libc::c_long,
((-1 as u64) >> 32) as libc::c_long,
RWF_NOWAIT,
);
let res = unsafe {
syscall(
SYS_preadv2,
fd,
iov,
iovcnt,
-1 as libc::c_long,
((-1 as u64) >> 32) as libc::c_long,
RWF_NOWAIT,
)
};

res.try_into().unwrap()
}
Expand All @@ -448,15 +452,13 @@ mod linux {
return Err(io::ErrorKind::Unsupported.into());
}

match cvt_ssize(unsafe {
preadv2(
fd,
&iovec {
iov_base: buf.as_ptr() as *mut _,
iov_len: buf.len(),
},
)
}) {
match cvt_ssize(preadv2(
fd,
&iovec {
iov_base: buf.as_ptr() as *mut _,
iov_len: buf.len(),
},
)) {
Ok(cnt) => Ok(cnt.try_into().unwrap()),
Err(err) if matches!(err.raw_os_error(), Some(libc::EOPNOTSUPP | libc::ENOSYS)) => {
IS_NONBLOCKING_READ_UNSUPPORTED.store(true, Ordering::Relaxed);
Expand Down

0 comments on commit 9087cbe

Please sign in to comment.