Skip to content

Commit 236fc31

Browse files
jastaThomasdezeeuw
authored andcommitted
Add ESP-IDF framework support
Mostly fixing compiler errors but also working around nonblocking issues with accept, eventfd, etc. Closes #1691
1 parent efba7ef commit 236fc31

File tree

6 files changed

+28
-7
lines changed

6 files changed

+28
-7
lines changed

src/sys/unix/net.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,20 @@ pub(crate) fn new_socket(domain: libc::c_int, socket_type: libc::c_int) -> io::R
4444
return Err(err);
4545
}
4646

47-
// Darwin doesn't have SOCK_NONBLOCK or SOCK_CLOEXEC.
47+
// Darwin (and others) doesn't have SOCK_NONBLOCK or SOCK_CLOEXEC.
4848
#[cfg(any(
4949
target_os = "ios",
5050
target_os = "macos",
5151
target_os = "tvos",
5252
target_os = "watchos",
53+
target_os = "espidf",
5354
))]
5455
{
5556
if let Err(err) = syscall!(fcntl(socket, libc::F_SETFL, libc::O_NONBLOCK)) {
5657
let _ = syscall!(close(socket));
5758
return Err(err);
5859
}
60+
#[cfg(not(target_os = "espidf"))]
5961
if let Err(err) = syscall!(fcntl(socket, libc::F_SETFD, libc::FD_CLOEXEC)) {
6062
let _ = syscall!(close(socket));
6163
return Err(err);
@@ -105,6 +107,7 @@ pub(crate) fn socket_addr(addr: &SocketAddr) -> (SocketAddrCRepr, libc::socklen_
105107
target_os = "openbsd",
106108
target_os = "tvos",
107109
target_os = "watchos",
110+
target_os = "espidf",
108111
))]
109112
sin_len: 0,
110113
};
@@ -131,6 +134,7 @@ pub(crate) fn socket_addr(addr: &SocketAddr) -> (SocketAddrCRepr, libc::socklen_
131134
target_os = "openbsd",
132135
target_os = "tvos",
133136
target_os = "watchos",
137+
target_os = "espidf",
134138
))]
135139
sin6_len: 0,
136140
#[cfg(target_os = "illumos")]

src/sys/unix/pipe.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ pub fn new() -> io::Result<(Sender, Receiver)> {
168168
target_os = "macos",
169169
target_os = "tvos",
170170
target_os = "watchos",
171+
target_os = "espidf",
171172
))]
172173
unsafe {
173174
// For platforms that don't have `pipe2(2)` we need to manually set the
@@ -202,6 +203,7 @@ pub fn new() -> io::Result<(Sender, Receiver)> {
202203
target_os = "redox",
203204
target_os = "tvos",
204205
target_os = "watchos",
206+
target_os = "espidf",
205207
)))]
206208
compile_error!("unsupported target for `mio::unix::pipe`");
207209

src/sys/unix/tcp.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ pub(crate) fn accept(listener: &net::TcpListener) -> io::Result<(net::TcpStream,
8787
target_os = "redox",
8888
target_os = "tvos",
8989
target_os = "watchos",
90+
target_os = "espidf",
9091
all(target_arch = "x86", target_os = "android"),
9192
))]
9293
let stream = {
@@ -97,10 +98,11 @@ pub(crate) fn accept(listener: &net::TcpListener) -> io::Result<(net::TcpStream,
9798
))
9899
.map(|socket| unsafe { net::TcpStream::from_raw_fd(socket) })
99100
.and_then(|s| {
101+
#[cfg(not(target_os = "espidf"))]
100102
syscall!(fcntl(s.as_raw_fd(), libc::F_SETFD, libc::FD_CLOEXEC))?;
101103

102104
// See https://github.com/tokio-rs/mio/issues/1450
103-
#[cfg(all(target_arch = "x86", target_os = "android"))]
105+
#[cfg(any(all(target_arch = "x86", target_os = "android"), target_os = "espidf",))]
104106
syscall!(fcntl(s.as_raw_fd(), libc::F_SETFL, libc::O_NONBLOCK))?;
105107

106108
Ok(s)

src/sys/unix/uds/listener.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ pub(crate) fn accept(listener: &net::UnixListener) -> io::Result<(UnixStream, So
4141
target_os = "redox",
4242
target_os = "tvos",
4343
target_os = "watchos",
44+
target_os = "espidf",
4445
// Android x86's seccomp profile forbids calls to `accept4(2)`
4546
// See https://github.com/tokio-rs/mio/issues/1445 for details
4647
all(target_arch = "x86", target_os = "android"),
@@ -63,6 +64,7 @@ pub(crate) fn accept(listener: &net::UnixListener) -> io::Result<(UnixStream, So
6364
target_os = "redox",
6465
target_os = "tvos",
6566
target_os = "watchos",
67+
target_os = "espidf",
6668
all(target_arch = "x86", target_os = "android")
6769
))]
6870
let socket = syscall!(accept(
@@ -74,10 +76,11 @@ pub(crate) fn accept(listener: &net::UnixListener) -> io::Result<(UnixStream, So
7476
// Ensure the socket is closed if either of the `fcntl` calls
7577
// error below.
7678
let s = unsafe { net::UnixStream::from_raw_fd(socket) };
79+
#[cfg(not(target_os = "espidf"))]
7780
syscall!(fcntl(socket, libc::F_SETFD, libc::FD_CLOEXEC))?;
7881

7982
// See https://github.com/tokio-rs/mio/issues/1450
80-
#[cfg(all(target_arch = "x86", target_os = "android"))]
83+
#[cfg(any(all(target_arch = "x86", target_os = "android"), target_os = "espidf",))]
8184
syscall!(fcntl(socket, libc::F_SETFL, libc::O_NONBLOCK))?;
8285

8386
Ok(s)

src/sys/unix/uds/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,15 @@ cfg_os_poll! {
8282
target_os = "macos",
8383
target_os = "tvos",
8484
target_os = "watchos",
85+
target_os = "espidf",
8586
)))]
8687
let flags = flags | libc::SOCK_NONBLOCK | libc::SOCK_CLOEXEC;
8788

8889
let mut fds = [-1; 2];
8990
syscall!(socketpair(libc::AF_UNIX, flags, 0, fds.as_mut_ptr()))?;
9091
let pair = unsafe { (T::from_raw_fd(fds[0]), T::from_raw_fd(fds[1])) };
9192

92-
// Darwin doesn't have SOCK_NONBLOCK or SOCK_CLOEXEC.
93+
// Darwin (and others) doesn't have SOCK_NONBLOCK or SOCK_CLOEXEC.
9394
//
9495
// In order to set those flags, additional `fcntl` sys calls must be
9596
// performed. If a `fnctl` fails after the sockets have been created,
@@ -100,11 +101,14 @@ cfg_os_poll! {
100101
target_os = "macos",
101102
target_os = "tvos",
102103
target_os = "watchos",
104+
target_os = "espidf",
103105
))]
104106
{
105107
syscall!(fcntl(fds[0], libc::F_SETFL, libc::O_NONBLOCK))?;
108+
#[cfg(not(target_os = "espidf"))]
106109
syscall!(fcntl(fds[0], libc::F_SETFD, libc::FD_CLOEXEC))?;
107110
syscall!(fcntl(fds[1], libc::F_SETFL, libc::O_NONBLOCK))?;
111+
#[cfg(not(target_os = "espidf"))]
108112
syscall!(fcntl(fds[1], libc::F_SETFD, libc::FD_CLOEXEC))?;
109113
}
110114
Ok(pair)

src/sys/unix/waker.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pub use self::fdbased::Waker;
6666

6767
#[cfg(all(
6868
not(mio_unsupported_force_waker_pipe),
69-
any(target_os = "linux", target_os = "android")
69+
any(target_os = "linux", target_os = "android", target_os = "espidf")
7070
))]
7171
mod eventfd {
7272
use std::fs::File;
@@ -86,7 +86,13 @@ mod eventfd {
8686

8787
impl WakerInternal {
8888
pub fn new() -> io::Result<WakerInternal> {
89-
let fd = syscall!(eventfd(0, libc::EFD_CLOEXEC | libc::EFD_NONBLOCK))?;
89+
#[cfg(not(target_os = "espidf"))]
90+
let flags = libc::EFD_CLOEXEC | libc::EFD_NONBLOCK;
91+
// ESP-IDF is EFD_NONBLOCK by default and errors if you try to pass this flag.
92+
#[cfg(target_os = "espidf")]
93+
let flags = 0;
94+
let fd = syscall!(eventfd(0, flags))?;
95+
9096
let file = unsafe { File::from_raw_fd(fd) };
9197
Ok(WakerInternal { fd: file })
9298
}
@@ -133,7 +139,7 @@ mod eventfd {
133139
#[cfg(all(
134140
mio_unsupported_force_poll_poll,
135141
not(mio_unsupported_force_waker_pipe),
136-
any(target_os = "linux", target_os = "android")
142+
any(target_os = "linux", target_os = "android", target_os = "espidf")
137143
))]
138144
pub(crate) use self::eventfd::WakerInternal;
139145

0 commit comments

Comments
 (0)