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

Cleanup epoll_create fallback #1594

Merged
merged 2 commits into from
Jul 17, 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 Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Targets available via Rustup that are supported.
TARGETS ?= "aarch64-apple-ios" "aarch64-linux-android" "x86_64-apple-darwin" "x86_64-pc-windows-msvc" "x86_64-unknown-freebsd" "x86_64-unknown-illumos" "x86_64-unknown-linux-gnu" "x86_64-unknown-netbsd"
TARGETS ?= aarch64-apple-ios aarch64-linux-android x86_64-apple-darwin x86_64-pc-windows-msvc x86_64-unknown-freebsd x86_64-unknown-illumos x86_64-unknown-linux-gnu x86_64-unknown-netbsd

test:
cargo test --all-features
Expand Down
72 changes: 34 additions & 38 deletions src/sys/unix/selector/epoll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,40 @@ pub struct Selector {

impl Selector {
pub fn new() -> io::Result<Selector> {
let ep = new_epoll_fd()?;
#[cfg(not(target_os = "android"))]
let res = syscall!(epoll_create1(libc::O_CLOEXEC));

// On Android < API level 16 `epoll_create1` is not defined, so use a
// raw system call.
// According to libuv, `EPOLL_CLOEXEC` is not defined on Android API <
// 21. But `EPOLL_CLOEXEC` is an alias for `O_CLOEXEC` on that platform,
// so we use it instead.
#[cfg(target_os = "android")]
let res = syscall!(syscall(libc::SYS_epoll_create1, libc::EPOLL_CLOEXEC));
Copy link
Contributor

Choose a reason for hiding this comment

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

It was EPOLL_CLOEXEC on non-android and O_CLOEXEC fallback on android, but after this PR it's reversed. I think it should be O_CLOEXEC on this line and EPOLL_CLOEXEC above (for epoll_create1).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

You're right, created #1595.


let ep = match res {
Ok(ep) => ep as RawFd,
Err(err) => {
// When `epoll_create1` is not available fall back to use
// `epoll_create` followed by `fcntl`.
if let Some(libc::ENOSYS) = err.raw_os_error() {
match syscall!(epoll_create(1024)) {
Ok(ep) => match syscall!(fcntl(ep, libc::F_SETFD, libc::FD_CLOEXEC)) {
Ok(ep) => ep as RawFd,
Err(err) => {
// `fcntl` failed, cleanup `ep`.
let _ = unsafe { libc::close(ep) };
return Err(err);
}
},
Err(err) => return Err(err),
}
} else {
return Err(err);
}
}
};

Ok(Selector {
#[cfg(debug_assertions)]
id: NEXT_ID.fetch_add(1, Ordering::Relaxed),
Expand Down Expand Up @@ -126,43 +159,6 @@ impl Drop for Selector {
}
}

/// Creates a new epoll file descriptor with close-on-exec flag set.
///
/// close-on-exec is set atomically with `epoll_create1()` if possible,
/// otherwise `epoll_create()` and `fcntl()` calls are used as a fallback.
fn new_epoll_fd() -> io::Result<libc::c_int> {
// According to libuv, `EPOLL_CLOEXEC` is not defined on Android API <
// 21. But `EPOLL_CLOEXEC` is an alias for `O_CLOEXEC` on that platform,
// so we use it instead.
#[cfg(target_os = "android")]
let flag = libc::O_CLOEXEC;
#[cfg(not(target_os = "android"))]
let flag = libc::EPOLL_CLOEXEC;

#[cfg(not(target_os = "android"))]
let ep = syscall!(epoll_create1(flag))?;

// On Android try to use epoll_create1 syscall with an epoll_create fallback
// to support Android API level 16 which does not define epoll_create1 function.
#[cfg(target_os = "android")]
let ep = syscall!(syscall(libc::SYS_epoll_create1, flag))
.map(|fd| fd as libc::c_int)
.or_else(|e| {
match e.raw_os_error() {
Some(libc::ENOSYS) => {
// Using epoll_create() followed by fcntl() instead of epoll_create1() with EPOLL_CLOEXEC
// flag for backwards compatibility.
let ep = syscall!(epoll_create(1024))?;

syscall!(fcntl(ep, libc::F_SETFD, libc::FD_CLOEXEC)).map(|_| ep)
}
_ => Err(e),
}
})?;

Ok(ep)
}

fn interests_to_epoll(interests: Interest) -> u32 {
let mut kind = EPOLLET;

Expand Down