Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unix pipe. #91

Merged
merged 9 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions compio-driver/src/iour/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl<T: IoBufMut> OpCode for ReadAt<T> {
let fd = Fd(self.fd);
let slice = self.buffer.as_uninit_slice();
opcode::Read::new(fd, slice.as_mut_ptr() as _, slice.len() as _)
.offset(self.offset as _)
.offset(self.offset())
.build()
}
}
Expand All @@ -30,7 +30,7 @@ impl<T: IoBuf> OpCode for WriteAt<T> {
fn create_entry(self: Pin<&mut Self>) -> Entry {
let slice = self.buffer.as_slice();
opcode::Write::new(Fd(self.fd), slice.as_ptr(), slice.len() as _)
.offset(self.offset as _)
.offset(self.offset())
.build()
}
}
Expand Down
15 changes: 4 additions & 11 deletions compio-driver/src/poll/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl<T: IoBufMut> OpCode for ReadAt<T> {
fd,
slice.as_mut_ptr() as _,
slice.len() as _,
self.offset as _
self.offset()
))? as _))
} else {
Ok(Decision::wait_readable(self.fd))
Expand All @@ -33,14 +33,7 @@ impl<T: IoBufMut> OpCode for ReadAt<T> {
let fd = self.fd;
let slice = self.buffer.as_uninit_slice();

syscall!(
break pread(
fd,
slice.as_mut_ptr() as _,
slice.len() as _,
self.offset as _
)
)
syscall!(break pread(fd, slice.as_mut_ptr() as _, slice.len() as _, self.offset()))
}
}

Expand All @@ -52,7 +45,7 @@ impl<T: IoBuf> OpCode for WriteAt<T> {
self.fd,
slice.as_ptr() as _,
slice.len() as _,
self.offset as _
self.offset()
))? as _))
} else {
Ok(Decision::wait_writable(self.fd))
Expand All @@ -69,7 +62,7 @@ impl<T: IoBuf> OpCode for WriteAt<T> {
self.fd,
slice.as_ptr() as _,
slice.len() as _,
self.offset as _
self.offset()
)
)
}
Expand Down
24 changes: 21 additions & 3 deletions compio-driver/src/unix/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,27 @@ use compio_buf::{IntoInner, IoBuf, IoBufMut, IoVectoredBuf, IoVectoredBufMut};
use libc::{sockaddr_storage, socklen_t};
use socket2::SockAddr;

#[cfg(doc)]
use crate::op::*;
use crate::RawFd;
use crate::{op::*, RawFd};

impl<T: IoBufMut> ReadAt<T> {
pub(crate) fn offset(&self) -> u64 {
if self.offset == usize::MAX {
u64::MAX
} else {
self.offset as _
}
}
}

impl<T: IoBuf> WriteAt<T> {
pub(crate) fn offset(&self) -> u64 {
if self.offset == usize::MAX {
u64::MAX
} else {
self.offset as _
}
}
}

/// Accept a connection.
pub struct Accept {
Expand Down
1 change: 1 addition & 0 deletions compio-fs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ windows-sys = { version = "0.48", features = [
# Unix specific dependencies
[target.'cfg(unix)'.dependencies]
libc = "0.2"
os_pipe = "1"

# Shared dev dependencies for all platforms
[dev-dependencies]
Expand Down
41 changes: 17 additions & 24 deletions compio-fs/src/pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use std::{io, os::unix::fs::FileTypeExt, path::Path};

use compio_driver::{impl_raw_fd, syscall, AsRawFd, FromRawFd};
use compio_driver::{impl_raw_fd, syscall, AsRawFd, FromRawFd, IntoRawFd};
#[cfg(feature = "runtime")]
use {
compio_buf::{BufResult, IoBuf, IoBufMut},
Expand All @@ -14,21 +14,20 @@ use crate::File;
/// Creates a pair of anonymous pipe.
///
/// ```
/// use compio_fs::pipe::pipe;
/// use compio_fs::pipe::anon_pipe;
///
/// # compio_runtime::block_on(async {
/// let (rx, tx) = pipe().unwrap();
/// let (rx, tx) = anon_pipe().unwrap();
///
/// tx.write_all("Hello world!").await.unwrap();
/// let (_, buf) = rx.read_exact(Vec::with_capacity(12)).await.unwrap();
/// assert_eq!(&buf, b"Hello world!");
/// # });
/// ```
pub fn pipe() -> io::Result<(Receiver, Sender)> {
let mut fds = [-1, -1];
syscall!(pipe(fds.as_mut_ptr()))?;
let receiver = unsafe { Receiver::from_raw_fd(fds[0]) };
let sender = unsafe { Sender::from_raw_fd(fds[1]) };
pub fn anon_pipe() -> io::Result<(Receiver, Sender)> {
Berrysoft marked this conversation as resolved.
Show resolved Hide resolved
let (receiver, sender) = os_pipe::pipe()?;
let receiver = Receiver::from_file(unsafe { File::from_raw_fd(receiver.into_raw_fd()) })?;
let sender = Sender::from_file(unsafe { File::from_raw_fd(sender.into_raw_fd()) })?;
Ok((receiver, sender))
}

Expand Down Expand Up @@ -315,9 +314,7 @@ pub struct Sender {

impl Sender {
pub(crate) fn from_file(file: File) -> io::Result<Sender> {
if cfg!(not(all(target_os = "linux", feature = "io-uring"))) {
set_nonblocking(&file)?;
}
set_nonblocking(&file)?;
Ok(Sender { file })
}

Expand Down Expand Up @@ -413,9 +410,7 @@ pub struct Receiver {

impl Receiver {
pub(crate) fn from_file(file: File) -> io::Result<Receiver> {
if cfg!(not(all(target_os = "linux", feature = "io-uring"))) {
set_nonblocking(&file)?;
}
set_nonblocking(&file)?;
Ok(Receiver { file })
}

Expand Down Expand Up @@ -444,16 +439,14 @@ fn is_fifo(file: &File) -> io::Result<bool> {
}

/// Sets file's flags with O_NONBLOCK by fcntl.
fn set_nonblocking(file: &File) -> io::Result<()> {
let fd = file.as_raw_fd();

let current_flags = syscall!(fcntl(fd, libc::F_GETFL))?;

let flags = current_flags | libc::O_NONBLOCK;

if flags != current_flags {
syscall!(fcntl(fd, libc::F_SETFL, flags))?;
fn set_nonblocking(file: &impl AsRawFd) -> io::Result<()> {
if cfg!(not(all(target_os = "linux", feature = "io-uring"))) {
let fd = file.as_raw_fd();
let current_flags = syscall!(fcntl(fd, libc::F_GETFL))?;
let flags = current_flags | libc::O_NONBLOCK;
if flags != current_flags {
syscall!(fcntl(fd, libc::F_SETFL, flags))?;
}
}

Ok(())
}
1 change: 1 addition & 0 deletions compio-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ windows-sys = { version = "0.48", features = ["Win32_System_IO"] }

# Unix specific dependencies
[target.'cfg(unix)'.dependencies]
os_pipe = "1"
libc = "0.2"

[features]
Expand Down
2 changes: 1 addition & 1 deletion compio-runtime/src/event/eventfd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub struct Event {
impl Event {
/// Create [`Event`].
pub fn new() -> io::Result<Self> {
let fd = syscall!(eventfd(0, 0))?;
let fd = syscall!(eventfd(0, libc::EFD_CLOEXEC))?;
let fd = unsafe { OwnedFd::from_raw_fd(fd) };
Ok(Self {
fd,
Expand Down
11 changes: 4 additions & 7 deletions compio-runtime/src/event/pipe.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{
io,
os::fd::{AsRawFd, FromRawFd, OwnedFd, RawFd},
os::fd::{AsRawFd, FromRawFd, IntoRawFd, OwnedFd, RawFd},
};

use compio_buf::{arrayvec::ArrayVec, BufResult};
Expand All @@ -20,14 +20,11 @@ pub struct Event {
impl Event {
/// Create [`Event`].
pub fn new() -> io::Result<Self> {
let mut fds = [-1, -1];
syscall!(pipe(fds.as_mut_ptr()))?;
let receiver = unsafe { OwnedFd::from_raw_fd(fds[0]) };
let sender = unsafe { OwnedFd::from_raw_fd(fds[1]) };
let (receiver, sender) = os_pipe::pipe()?;
let receiver = unsafe { OwnedFd::from_raw_fd(receiver.into_raw_fd()) };
let sender = unsafe { OwnedFd::from_raw_fd(sender.into_raw_fd()) };

syscall!(fcntl(receiver.as_raw_fd(), libc::F_SETFD, libc::FD_CLOEXEC))?;
syscall!(fcntl(receiver.as_raw_fd(), libc::F_SETFL, libc::O_NONBLOCK))?;
syscall!(fcntl(sender.as_raw_fd(), libc::F_SETFD, libc::FD_CLOEXEC))?;
Ok(Self {
sender,
receiver,
Expand Down
Loading