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

Use rustix instead of direct calls to libc. #76

Merged
merged 4 commits into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ jobs:
matrix:
# When updating this, the reminder to update the minimum supported
# Rust version in Cargo.toml.
rust: ['1.46']
rust: ['1.48']
steps:
- uses: actions/checkout@v3
- name: Install Rust
Expand Down
10 changes: 2 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ name = "async-io"
version = "1.12.0"
authors = ["Stjepan Glavina <stjepang@gmail.com>"]
edition = "2018"
rust-version = "1.46"
rust-version = "1.48"
description = "Async I/O and timers"
license = "Apache-2.0 OR MIT"
repository = "https://github.com/smol-rs/async-io"
Expand All @@ -25,19 +25,14 @@ futures-lite = "1.11.0"
log = "0.4.11"
parking = "2.0.0"
polling = "2.0.0"
rustix = { version = "0.36.0", default-features = false, features = ["std", "fs"] }
slab = "0.4.2"
socket2 = { version = "0.4.2", features = ["all"] }
waker-fn = "1.1.0"

[build-dependencies]
autocfg = "1"

[target."cfg(unix)".dependencies]
libc = "0.2.77"

[target.'cfg(windows)'.dependencies]
windows-sys = { version = "0.42", features = ["Win32_Networking_WinSock"] }

[dev-dependencies]
async-channel = "1"
async-net = "1"
Expand All @@ -49,7 +44,6 @@ tempfile = "3"

[target.'cfg(target_os = "linux")'.dev-dependencies]
inotify = { version = "0.10", default-features = false }
nix = { version = "0.25", default-features = false }
timerfd = "1"

[target.'cfg(windows)'.dev-dependencies]
Expand Down
10 changes: 9 additions & 1 deletion examples/linux-timerfd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ fn main() -> std::io::Result<()> {

use async_io::Async;
use futures_lite::future;
use rustix::fd::BorrowedFd;
use timerfd::{SetTimeFlags, TimerFd, TimerState};

/// Sleeps using an OS timer.
Expand All @@ -24,7 +25,14 @@ fn main() -> std::io::Result<()> {

// When the OS timer fires, a 64-bit integer can be read from it.
Async::new(timer)?
.read_with(|t| nix::unistd::read(t.as_raw_fd(), &mut [0u8; 8]).map_err(io::Error::from))
.read_with(|t| {
// Safety: We assume `as_raw_fd()` returns a valid fd. When we
// can depend on Rust >= 1.63, where `AsFd` is stabilized, and
// when `TimerFd` implements it, we can remove this unsafe and
// simplify this.
let fd = unsafe { BorrowedFd::borrow_raw(t.as_raw_fd()) };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AsFd is stabilized now, so I think we can eliminate this unsafe.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost, but async-io still has an MSRV of 1.48, and I/O safety is stabilized in Rust 1.63. I've now added more to these comments to mention that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't run the tests on the MSRV build, so it's alright.

rustix::io::read(fd, &mut [0u8; 8]).map_err(io::Error::from)
})
.await?;
Ok(())
}
Expand Down
36 changes: 16 additions & 20 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,21 +598,19 @@ impl<T: AsRawFd> Async<T> {
/// # std::io::Result::Ok(()) });
/// ```
pub fn new(io: T) -> io::Result<Async<T>> {
let fd = io.as_raw_fd();
let raw = io.as_raw_fd();

// Put the file descriptor in non-blocking mode.
unsafe {
let mut res = libc::fcntl(fd, libc::F_GETFL);
if res != -1 {
res = libc::fcntl(fd, libc::F_SETFL, res | libc::O_NONBLOCK);
}
if res == -1 {
return Err(io::Error::last_os_error());
}
}
//
// Safety: We assume `as_raw_fd()` returns a valid fd. When we can
// depend on Rust >= 1.63, where `AsFd` is stabilized, and when
// `TimerFd` implements it, we can remove this unsafe and simplify this.
let fd = unsafe { rustix::fd::BorrowedFd::borrow_raw(raw) };
notgull marked this conversation as resolved.
Show resolved Hide resolved
let flags = rustix::fs::fcntl_getfl(fd)?;
rustix::fs::fcntl_setfl(fd, flags | rustix::fs::OFlags::NONBLOCK)?;

Ok(Async {
source: Reactor::get().insert_io(fd)?,
source: Reactor::get().insert_io(raw)?,
io: Some(io),
})
}
Expand Down Expand Up @@ -678,16 +676,14 @@ impl<T: AsRawSocket> Async<T> {
/// ```
pub fn new(io: T) -> io::Result<Async<T>> {
let sock = io.as_raw_socket();
let borrowed = unsafe { rustix::fd::BorrowedFd::borrow_raw(sock) };

// Put the socket in non-blocking mode.

use windows_sys::Win32::Networking::WinSock;

let mut nonblocking = true as _;
let res = unsafe { WinSock::ioctlsocket(sock as _, WinSock::FIONBIO, &mut nonblocking) };
if res != 0 {
return Err(io::Error::last_os_error());
}
//
// Safety: We assume `as_raw_socket()` returns a valid fd. When we can
// depend on Rust >= 1.63, where `AsFd` is stabilized, and when
// `TimerFd` implements it, we can remove this unsafe and simplify this.
rustix::io::ioctl_fionbio(borrowed, true)?;

Ok(Async {
source: Reactor::get().insert_io(sock)?,
Expand Down Expand Up @@ -1895,7 +1891,7 @@ fn connect(addr: SockAddr, domain: Domain, protocol: Option<Protocol>) -> io::Re
match socket.connect(&addr) {
Ok(_) => {}
#[cfg(unix)]
Err(err) if err.raw_os_error() == Some(libc::EINPROGRESS) => {}
Err(err) if err.raw_os_error() == Some(rustix::io::Errno::INPROGRESS.raw_os_error()) => {}
Err(err) if err.kind() == io::ErrorKind::WouldBlock => {}
Err(err) => return Err(err),
}
Expand Down