From 5715fb7ba63866f4d9e423b4a8b864519ccd543f Mon Sep 17 00:00:00 2001
From: Jeremy Soller <jeremy@system76.com>
Date: Sun, 10 Mar 2019 13:03:00 -0600
Subject: [PATCH] Work on switching to epoll as backend for select and poll

---
 src/header/poll/mod.rs       |  92 +++++++++++-
 src/header/sys_epoll/mod.rs  |  41 ++++--
 src/header/sys_select/mod.rs | 185 +++++++++++++++++++++++-
 src/platform/linux/epoll.rs  |  19 +++
 src/platform/linux/mod.rs    |  17 +--
 src/platform/mod.rs          |   2 +-
 src/platform/pal/epoll.rs    |  10 ++
 src/platform/pal/mod.rs      |  16 +--
 src/platform/redox/mod.rs    | 263 -----------------------------------
 9 files changed, 334 insertions(+), 311 deletions(-)
 create mode 100644 src/platform/linux/epoll.rs
 create mode 100644 src/platform/pal/epoll.rs

diff --git a/src/header/poll/mod.rs b/src/header/poll/mod.rs
index 08bb87ab2..4d6f9e41e 100644
--- a/src/header/poll/mod.rs
+++ b/src/header/poll/mod.rs
@@ -1,7 +1,16 @@
 //! poll implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xsh/poll.h.html
 
+use core::{mem, slice};
+
+use fs::File;
+use header::sys_epoll::{
+    epoll_create1, EPOLL_CLOEXEC,
+    epoll_ctl, EPOLL_CTL_ADD,
+    epoll_wait,
+    EPOLLIN, EPOLLPRI, EPOLLOUT, EPOLLERR, EPOLLHUP, EPOLLNVAL,
+    epoll_data, epoll_event
+};
 use platform::types::*;
-use platform::{Pal, Sys};
 
 pub const POLLIN: c_short = 0x001;
 pub const POLLPRI: c_short = 0x002;
@@ -19,10 +28,87 @@ pub struct pollfd {
     pub revents: c_short,
 }
 
+pub fn poll_epoll(fds: &mut [pollfd], timeout: c_int) -> c_int {
+    let event_map = [
+        (POLLIN, EPOLLIN),
+        (POLLPRI, EPOLLPRI),
+        (POLLOUT, EPOLLOUT),
+        (POLLERR, EPOLLERR),
+        (POLLHUP, EPOLLHUP),
+        (POLLNVAL, EPOLLNVAL)
+    ];
+
+    let ep = {
+        let epfd = epoll_create1(EPOLL_CLOEXEC);
+        if epfd < 0 {
+            return -1;
+        }
+        File::new(epfd)
+    };
+
+    for i in 0..fds.len() {
+        let mut pfd = &mut fds[i];
+
+        let mut event = epoll_event {
+            events: 0,
+            data: epoll_data {
+                u64: i as u64,
+            },
+        };
+
+        for (p, ep) in event_map.iter() {
+            if pfd.events & p > 0 {
+                event.events |= ep;
+            }
+        }
+
+        pfd.revents = 0;
+
+        if epoll_ctl(*ep, EPOLL_CTL_ADD, pfd.fd, &mut event) < 0 {
+            return -1;
+        }
+    }
+
+    let mut events: [epoll_event; 32] = unsafe { mem::zeroed() };
+    let res = epoll_wait(
+        *ep,
+        events.as_mut_ptr(),
+        events.len() as c_int,
+        timeout
+    );
+    if res < 0 {
+        return -1;
+    }
+
+    for i in 0..res as usize {
+        let event = &events[i];
+        let pi = unsafe { event.data.u64 as usize };
+        // TODO: Error status when fd does not match?
+        if let Some(pfd) = fds.get_mut(pi) {
+            for (p, ep) in event_map.iter() {
+                if event.events & ep > 0 {
+                    pfd.revents |= p;
+                }
+            }
+        }
+    }
+
+    let mut count = 0;
+    for pfd in fds.iter() {
+        if pfd.revents > 0 {
+            count += 1;
+        }
+    }
+    count
+}
+
 #[no_mangle]
-pub extern "C" fn poll(fds: *mut pollfd, nfds: nfds_t, timeout: c_int) -> c_int {
+pub unsafe extern "C" fn poll(fds: *mut pollfd, nfds: nfds_t, timeout: c_int) -> c_int {
     trace_expr!(
-        Sys::poll(fds, nfds, timeout),
+        poll_epoll(
+            slice::from_raw_parts_mut(fds, nfds as usize),
+            timeout
+        ),
         "poll({:p}, {}, {})",
         fds,
         nfds,
diff --git a/src/header/sys_epoll/mod.rs b/src/header/sys_epoll/mod.rs
index 47b9e2352..06893bc0a 100644
--- a/src/header/sys_epoll/mod.rs
+++ b/src/header/sys_epoll/mod.rs
@@ -3,6 +3,7 @@
 use core::ptr;
 
 use header::signal::sigset_t;
+use platform::{PalEpoll, Sys};
 use platform::types::*;
 
 pub use self::sys::*;
@@ -34,16 +35,16 @@ pub const EPOLLRDHUP: u32 =  0x2000;
 
 #[repr(C)]
 pub union epoll_data {
-    ptr: *mut c_void,
-    fd: c_int,
-    u32: u32,
-    u64: u64,
+    pub ptr: *mut c_void,
+    pub fd: c_int,
+    pub u32: u32,
+    pub u64: u64,
 }
 
 #[repr(C)]
 pub struct epoll_event {
-    events: u32,
-    data: epoll_data,
+    pub events: u32,
+    pub data: epoll_data,
 }
 
 #[no_mangle]
@@ -53,14 +54,23 @@ pub extern "C" fn epoll_create(_size: c_int) -> c_int {
 
 #[no_mangle]
 pub extern "C" fn epoll_create1(flags: c_int) -> c_int {
-    unimplemented!()
-    //Sys::epoll_create1(flags)
+    trace_expr!(
+        Sys::epoll_create1(flags),
+        "epoll_create1({:#x})",
+        flags
+    )
 }
 
 #[no_mangle]
 pub extern "C" fn epoll_ctl(epfd: c_int, op: c_int, fd: c_int, event: *mut epoll_event) -> c_int {
-    unimplemented!()
-    //Sys::epoll_ctl(epfd, op, fd, event)
+    trace_expr!(
+        Sys::epoll_ctl(epfd, op, fd, event),
+        "epoll_ctl({}, {}, {}, {:p})",
+        epfd,
+        op,
+        fd,
+        event
+    )
 }
 
 #[no_mangle]
@@ -70,6 +80,13 @@ pub extern "C" fn epoll_wait(epfd: c_int, events: *mut epoll_event, maxevents: c
 
 #[no_mangle]
 pub extern "C" fn epoll_pwait(epfd: c_int, events: *mut epoll_event, maxevents: c_int, timeout: c_int, sigmask: *const sigset_t) -> c_int {
-    unimplemented!()
-    //Sys::epoll_pwait(epfd, events, maxevents, timeout, sigmask)
+    trace_expr!(
+        Sys::epoll_pwait(epfd, events, maxevents, timeout, sigmask),
+        "epoll_pwait({}, {:p}, {}, {}, {:p})",
+        epfd,
+        events,
+        maxevents,
+        timeout,
+        sigmask
+    )
 }
diff --git a/src/header/sys_select/mod.rs b/src/header/sys_select/mod.rs
index 6565ce402..bf77b819e 100644
--- a/src/header/sys_select/mod.rs
+++ b/src/header/sys_select/mod.rs
@@ -1,9 +1,19 @@
 //! sys/select.h implementation
 
 use core::mem;
+
+use fs::File;
+use header::errno;
+use header::sys_epoll::{
+    epoll_create1, EPOLL_CLOEXEC,
+    epoll_ctl, EPOLL_CTL_ADD,
+    epoll_wait,
+    EPOLLIN, EPOLLOUT, EPOLLERR,
+    epoll_data, epoll_event
+};
 use header::sys_time::timeval;
+use platform;
 use platform::types::*;
-use platform::{Pal, Sys};
 
 // fd_set is also defined in C because cbindgen is incompatible with mem::size_of booo
 
@@ -14,8 +24,155 @@ pub struct fd_set {
     pub fds_bits: [c_ulong; FD_SETSIZE / (8 * mem::size_of::<c_ulong>())],
 }
 
+impl fd_set {
+    fn index(fd: c_int) -> usize {
+        (fd as usize) / (8 * mem::size_of::<c_ulong>())
+    }
+
+    fn bitmask(fd: c_int) -> c_ulong {
+        1 << ((fd as usize) & (8 * mem::size_of::<c_ulong>() - 1)) as c_ulong
+    }
+
+    fn zero(&mut self) {
+        for i in 0..self.fds_bits.len() {
+            self.fds_bits[i] = 0;
+        }
+    }
+
+    fn set(&mut self, fd: c_int) {
+        self.fds_bits[Self::index(fd)] |= Self::bitmask(fd);
+    }
+
+    fn clr(&mut self, fd: c_int) {
+        self.fds_bits[Self::index(fd)] &= !Self::bitmask(fd);
+    }
+
+    fn isset(&self, fd: c_int) -> bool {
+        self.fds_bits[Self::index(fd)] & Self::bitmask(fd) > 0
+    }
+}
+
+pub fn select_epoll(
+    nfds: c_int,
+    mut readfds: Option<&mut fd_set>,
+    mut writefds: Option<&mut fd_set>,
+    mut exceptfds: Option<&mut fd_set>,
+    timeout: Option<&mut timeval>
+) -> c_int {
+    if nfds < 0 || nfds > FD_SETSIZE as i32 {
+        unsafe { platform::errno = errno::EINVAL };
+        return -1;
+    };
+
+    let ep = {
+        let epfd = epoll_create1(EPOLL_CLOEXEC);
+        if epfd < 0 {
+            return -1;
+        }
+        File::new(epfd)
+    };
+
+    for fd in 0..nfds {
+        if let Some(ref fd_set) = readfds {
+            if fd_set.isset(fd) {
+                let mut event = epoll_event {
+                    events: EPOLLIN,
+                    data: epoll_data {
+                        fd: fd,
+                    },
+                };
+                if epoll_ctl(*ep, EPOLL_CTL_ADD, fd, &mut event) < 0 {
+                    return -1;
+                }
+            }
+        }
+        if let Some(ref fd_set) = writefds {
+            if fd_set.isset(fd) {
+                let mut event = epoll_event {
+                    events: EPOLLOUT,
+                    data: epoll_data {
+                        fd: fd,
+                    },
+                };
+                if epoll_ctl(*ep, EPOLL_CTL_ADD, fd, &mut event) < 0 {
+                    return -1;
+                }
+            }
+        }
+        if let Some(ref fd_set) = exceptfds {
+            if fd_set.isset(fd) {
+                let mut event = epoll_event {
+                    events: EPOLLERR,
+                    data: epoll_data {
+                        fd: fd,
+                    },
+                };
+                if epoll_ctl(*ep, EPOLL_CTL_ADD, fd, &mut event) < 0 {
+                    return -1;
+                }
+            }
+        }
+    }
+
+    let mut events: [epoll_event; 32] = unsafe { mem::zeroed() };
+    let res = epoll_wait(
+        *ep,
+        events.as_mut_ptr(),
+        events.len() as c_int,
+        match timeout {
+            Some(timeout) => {
+                //TODO: Check for overflow
+                ((timeout.tv_sec as c_int) * 1000) +
+                ((timeout.tv_usec as c_int) / 1000)
+            },
+            None => -1
+        }
+    );
+    if res < 0 {
+        return -1;
+    }
+
+    if let Some(ref mut fd_set) = readfds {
+        fd_set.zero();
+    }
+    if let Some(ref mut fd_set) = writefds {
+        fd_set.zero();
+    }
+    if let Some(ref mut fd_set) = exceptfds {
+        fd_set.zero();
+    }
+
+    let mut count = 0;
+    for i in 0..res as usize {
+        let event = &events[i];
+        let fd = unsafe { event.data.fd };
+        // TODO: Error status when fd does not match?
+        if fd >= 0 && fd <= FD_SETSIZE as c_int {
+            if event.events & EPOLLIN > 0 {
+                if let Some(ref mut fd_set) = readfds {
+                    fd_set.set(fd);
+                    count += 1;
+                }
+            }
+            if event.events & EPOLLOUT > 0 {
+                if let Some(ref mut fd_set) = writefds {
+                    fd_set.set(fd);
+                    count += 1;
+                }
+            }
+            if event.events & EPOLLERR > 0 {
+                if let Some(ref mut fd_set) = exceptfds {
+                    fd_set.set(fd);
+                    count += 1;
+                }
+            }
+        }
+    }
+    count
+}
+
 #[no_mangle]
-pub extern "C" fn select(
+pub unsafe extern "C" fn select(
     nfds: c_int,
     readfds: *mut fd_set,
     writefds: *mut fd_set,
@@ -23,7 +180,29 @@ pub extern "C" fn select(
     timeout: *mut timeval,
 ) -> c_int {
     trace_expr!(
-        Sys::select(nfds, readfds, writefds, exceptfds, timeout),
+        select_epoll(
+            nfds,
+            if readfds.is_null() {
+                None
+            } else {
+                Some(&mut *readfds)
+            },
+            if writefds.is_null() {
+                None
+            } else {
+                Some(&mut *writefds)
+            },
+            if exceptfds.is_null() {
+                None
+            } else {
+                Some(&mut *exceptfds)
+            },
+            if timeout.is_null() {
+                None
+            } else {
+                Some(&mut *timeout)
+            }
+        ),
         "select({}, {:p}, {:p}, {:p}, {:p})",
         nfds,
         readfds,
diff --git a/src/platform/linux/epoll.rs b/src/platform/linux/epoll.rs
new file mode 100644
index 000000000..e83a97ab8
--- /dev/null
+++ b/src/platform/linux/epoll.rs
@@ -0,0 +1,19 @@
+use super::super::types::*;
+use super::super::PalEpoll;
+use super::{e, Sys};
+use header::signal::sigset_t;
+use header::sys_epoll::epoll_event;
+
+impl PalEpoll for Sys {
+    fn epoll_create1(flags: c_int) -> c_int {
+        unsafe { e(syscall!(EPOLL_CREATE1, flags)) as c_int }
+    }
+
+    fn epoll_ctl(epfd: c_int, op: c_int, fd: c_int, event: *mut epoll_event) -> c_int {
+        unsafe { e(syscall!(EPOLL_CTL, epfd, op, fd, event)) as c_int }
+    }
+
+    fn epoll_pwait(epfd: c_int, events: *mut epoll_event, maxevents: c_int, timeout: c_int, sigmask: *const sigset_t) -> c_int {
+        unsafe { e(syscall!(EPOLL_PWAIT, epfd, events, maxevents, timeout, sigmask)) as c_int }
+    }
+}
diff --git a/src/platform/linux/mod.rs b/src/platform/linux/mod.rs
index 70c6bf178..7d0d5186b 100644
--- a/src/platform/linux/mod.rs
+++ b/src/platform/linux/mod.rs
@@ -5,10 +5,8 @@ use super::types::*;
 use super::{errno, Pal};
 use c_str::CStr;
 use header::dirent::dirent;
-use header::poll::{nfds_t, pollfd};
 use header::signal::SIGCHLD;
 // use header::sys_resource::rusage;
-use header::sys_select::fd_set;
 use header::sys_stat::stat;
 use header::sys_statvfs::statvfs;
 use header::sys_time::{timeval, timezone};
@@ -16,6 +14,7 @@ use header::sys_time::{timeval, timezone};
 use header::sys_utsname::utsname;
 use header::time::timespec;
 
+mod epoll;
 mod signal;
 mod socket;
 
@@ -322,10 +321,6 @@ impl Pal for Sys {
         e(unsafe { syscall!(PIPE2, fildes.as_mut_ptr(), 0) }) as c_int
     }
 
-    fn poll(fds: *mut pollfd, nfds: nfds_t, timeout: c_int) -> c_int {
-        e(unsafe { syscall!(POLL, fds, nfds, timeout) }) as c_int
-    }
-
     fn pte_clone() -> pid_t {
         e(unsafe {
             syscall!(
@@ -367,16 +362,6 @@ impl Pal for Sys {
         e(unsafe { syscall!(SCHED_YIELD) }) as c_int
     }
 
-    fn select(
-        nfds: c_int,
-        readfds: *mut fd_set,
-        writefds: *mut fd_set,
-        exceptfds: *mut fd_set,
-        timeout: *mut timeval,
-    ) -> c_int {
-        e(unsafe { syscall!(SELECT, nfds, readfds, writefds, exceptfds, timeout) }) as c_int
-    }
-
     fn setpgid(pid: pid_t, pgid: pid_t) -> c_int {
         e(unsafe { syscall!(SETPGID, pid, pgid) }) as c_int
     }
diff --git a/src/platform/mod.rs b/src/platform/mod.rs
index 37da18e64..8dfaa1f89 100644
--- a/src/platform/mod.rs
+++ b/src/platform/mod.rs
@@ -12,7 +12,7 @@ mod allocator;
 #[path = "allocator/ralloc.rs"]
 mod allocator;
 
-pub use self::pal::{Pal, PalSignal, PalSocket};
+pub use self::pal::{Pal, PalEpoll, PalSignal, PalSocket};
 
 mod pal;
 
diff --git a/src/platform/pal/epoll.rs b/src/platform/pal/epoll.rs
new file mode 100644
index 000000000..deace670f
--- /dev/null
+++ b/src/platform/pal/epoll.rs
@@ -0,0 +1,10 @@
+use super::super::types::*;
+use super::super::Pal;
+use header::signal::sigset_t;
+use header::sys_epoll::epoll_event;
+
+pub trait PalEpoll: Pal {
+    fn epoll_create1(flags: c_int) -> c_int;
+    fn epoll_ctl(epfd: c_int, op: c_int, fd: c_int, event: *mut epoll_event) -> c_int;
+    fn epoll_pwait(epfd: c_int, events: *mut epoll_event, maxevents: c_int, timeout: c_int, sigmask: *const sigset_t) -> c_int;
+}
diff --git a/src/platform/pal/mod.rs b/src/platform/pal/mod.rs
index 754108343..9984a5359 100644
--- a/src/platform/pal/mod.rs
+++ b/src/platform/pal/mod.rs
@@ -1,14 +1,15 @@
 use super::types::*;
 use c_str::CStr;
 use header::dirent::dirent;
-use header::poll::{nfds_t, pollfd};
-use header::sys_select::fd_set;
 use header::sys_stat::stat;
 use header::sys_statvfs::statvfs;
 use header::sys_time::{timeval, timezone};
 use header::sys_utsname::utsname;
 use header::time::timespec;
 
+pub use self::epoll::PalEpoll;
+mod epoll;
+
 pub use self::signal::PalSignal;
 mod signal;
 
@@ -115,8 +116,6 @@ pub trait Pal {
 
     fn pipe(fildes: &mut [c_int]) -> c_int;
 
-    fn poll(fds: *mut pollfd, nfds: nfds_t, timeout: c_int) -> c_int;
-
     fn pte_clone() -> pid_t;
 
     fn read(fildes: c_int, buf: &mut [u8]) -> ssize_t;
@@ -129,15 +128,6 @@ pub trait Pal {
 
     fn sched_yield() -> c_int;
 
-    //TODO: Deprecate in favor of poll
-    fn select(
-        nfds: c_int,
-        readfds: *mut fd_set,
-        writefds: *mut fd_set,
-        exceptfds: *mut fd_set,
-        timeout: *mut timeval,
-    ) -> c_int;
-
     fn setpgid(pid: pid_t, pgid: pid_t) -> c_int;
 
     fn setregid(rgid: gid_t, egid: gid_t) -> c_int;
diff --git a/src/platform/redox/mod.rs b/src/platform/redox/mod.rs
index a1b801a3a..41d4b6e4d 100644
--- a/src/platform/redox/mod.rs
+++ b/src/platform/redox/mod.rs
@@ -14,9 +14,7 @@ use fs::File;
 use header::dirent::dirent;
 use header::errno::{EINVAL, EIO, EPERM};
 use header::fcntl;
-use header::poll::{self, nfds_t, pollfd};
 use header::sys_mman::MAP_ANON;
-use header::sys_select::fd_set;
 use header::sys_stat::stat;
 use header::sys_statvfs::statvfs;
 use header::sys_time::{timeval, timezone};
@@ -691,130 +689,6 @@ impl Pal for Sys {
         res as c_int
     }
 
-    fn poll(fds: *mut pollfd, nfds: nfds_t, timeout: c_int) -> c_int {
-        let fds = unsafe { slice::from_raw_parts_mut(fds, nfds as usize) };
-
-        let event_path = c_str!("event:");
-        let mut event_file = match File::open(event_path, fcntl::O_RDWR | fcntl::O_CLOEXEC) {
-            Ok(file) => file,
-            Err(_) => return -1,
-        };
-
-        for fd in fds.iter_mut() {
-            let mut flags = 0;
-
-            if fd.events & poll::POLLIN > 0 {
-                flags |= syscall::EVENT_READ;
-            }
-
-            if fd.events & poll::POLLOUT > 0 {
-                flags |= syscall::EVENT_WRITE;
-            }
-
-            fd.revents = 0;
-
-            if fd.fd >= 0 && flags > 0 {
-                if event_file
-                    .write(&syscall::Event {
-                        id: fd.fd as usize,
-                        flags: flags,
-                        data: 0,
-                    })
-                    .is_err()
-                {
-                    return -1;
-                }
-            }
-        }
-
-        const TIMEOUT_TOKEN: usize = 1;
-
-        let timeout_file = if timeout < 0 {
-            None
-        } else {
-            let timeout_path = unsafe {
-                CString::from_vec_unchecked(
-                    format!("time:{}", syscall::CLOCK_MONOTONIC).into_bytes(),
-                )
-            };
-            let mut timeout_file = match File::open(&timeout_path, fcntl::O_RDWR | fcntl::O_CLOEXEC)
-            {
-                Ok(file) => file,
-                Err(_) => return -1,
-            };
-
-            if event_file
-                .write(&syscall::Event {
-                    id: *timeout_file as usize,
-                    flags: syscall::EVENT_READ,
-                    data: TIMEOUT_TOKEN,
-                })
-                .is_err()
-            {
-                return -1;
-            }
-
-            let mut time = syscall::TimeSpec::default();
-            if timeout_file.read(&mut time).is_err() {
-                return -1;
-            }
-
-            time.tv_nsec += timeout * 1000000;
-            while time.tv_nsec >= 1000000000 {
-                time.tv_sec += 1;
-                time.tv_nsec -= 1000000000;
-            }
-
-            if timeout_file.write(&time).is_err() {
-                return -1;
-            }
-
-            Some(timeout_file)
-        };
-
-        let mut events = [syscall::Event::default(); 32];
-        let read = {
-            let mut events = unsafe {
-                slice::from_raw_parts_mut(
-                    &mut events as *mut _ as *mut u8,
-                    mem::size_of::<syscall::Event>() * events.len(),
-                )
-            };
-            match event_file.read(&mut events) {
-                Ok(i) => i / mem::size_of::<syscall::Event>(),
-                Err(_) => return -1,
-            }
-        };
-
-        for event in &events[..read] {
-            if event.data == TIMEOUT_TOKEN {
-                continue;
-            }
-
-            for fd in fds.iter_mut() {
-                if event.id == fd.fd as usize {
-                    if event.flags & syscall::EVENT_READ > 0 {
-                        fd.revents |= poll::POLLIN;
-                    }
-
-                    if event.flags & syscall::EVENT_WRITE > 0 {
-                        fd.revents |= poll::POLLOUT;
-                    }
-                }
-            }
-        }
-
-        let mut total = 0;
-
-        for fd in fds.iter_mut() {
-            if fd.revents > 0 {
-                total += 1;
-            }
-        }
-
-        total
-    }
-
     fn pte_clone() -> pid_t {
         e(unsafe {
             syscall::clone(
@@ -872,143 +746,6 @@ impl Pal for Sys {
         e(syscall::sched_yield()) as c_int
     }
 
-    fn select(
-        nfds: c_int,
-        readfds: *mut fd_set,
-        writefds: *mut fd_set,
-        exceptfds: *mut fd_set,
-        timeout: *mut timeval,
-    ) -> c_int {
-        let mut readfds = unsafe { readfds.as_mut() }.map(|s| BitSet::from_ref(&mut s.fds_bits));
-        let mut writefds = unsafe { writefds.as_mut() }.map(|s| BitSet::from_ref(&mut s.fds_bits));
-        let mut exceptfds =
-            unsafe { exceptfds.as_mut() }.map(|s| BitSet::from_ref(&mut s.fds_bits));
-
-        let event_path = c_str!("event:");
-        let mut event_file = match File::open(event_path, fcntl::O_RDWR | fcntl::O_CLOEXEC) {
-            Ok(file) => file,
-            Err(_) => return -1,
-        };
-
-        for fd in 0..nfds as usize {
-            macro_rules! register {
-                ($fd:expr, $flags:expr) => {
-                    if event_file
-                        .write(&syscall::Event {
-                            id: $fd,
-                            flags: $flags,
-                            data: 0,
-                        })
-                        .is_err()
-                    {
-                        return -1;
-                    }
-                };
-            }
-            if readfds.as_mut().map(|s| s.contains(fd)).unwrap_or(false) {
-                register!(fd, syscall::EVENT_READ);
-            }
-            if writefds.as_mut().map(|s| s.contains(fd)).unwrap_or(false) {
-                register!(fd, syscall::EVENT_WRITE);
-            }
-        }
-
-        const TIMEOUT_TOKEN: usize = 1;
-
-        let timeout_file = if timeout.is_null() {
-            None
-        } else {
-            let timeout = unsafe { &*timeout };
-
-            let timeout_path = unsafe {
-                CString::from_vec_unchecked(
-                    format!("time:{}", syscall::CLOCK_MONOTONIC).into_bytes(),
-                )
-            };
-            let mut timeout_file = match File::open(&timeout_path, fcntl::O_RDWR | fcntl::O_CLOEXEC)
-            {
-                Ok(file) => file,
-                Err(_) => return -1,
-            };
-
-            if event_file
-                .write(&syscall::Event {
-                    id: *timeout_file as usize,
-                    flags: syscall::EVENT_READ,
-                    data: TIMEOUT_TOKEN,
-                })
-                .is_err()
-            {
-                return -1;
-            }
-
-            let mut time = syscall::TimeSpec::default();
-            if timeout_file.read(&mut time).is_err() {
-                return -1;
-            }
-
-            time.tv_sec += timeout.tv_sec;
-            time.tv_nsec += timeout.tv_usec * 1000;
-            while time.tv_nsec >= 1000000000 {
-                time.tv_sec += 1;
-                time.tv_nsec -= 1000000000;
-            }
-
-            if timeout_file.write(&time).is_err() {
-                return -1;
-            }
-
-            Some(timeout_file)
-        };
-
-        let mut events = [syscall::Event::default(); 32];
-        let read = {
-            let mut events = unsafe {
-                slice::from_raw_parts_mut(
-                    &mut events as *mut _ as *mut u8,
-                    mem::size_of::<syscall::Event>() * events.len(),
-                )
-            };
-            match event_file.read(&mut events) {
-                Ok(i) => i / mem::size_of::<syscall::Event>(),
-                Err(_) => return -1,
-            }
-        };
-
-        let mut total = 0;
-
-        if let Some(ref mut set) = readfds {
-            set.clear();
-        }
-        if let Some(ref mut set) = writefds {
-            set.clear();
-        }
-        if let Some(ref mut set) = exceptfds {
-            set.clear();
-        }
-
-        for event in &events[..read] {
-            if event.data == TIMEOUT_TOKEN {
-                continue;
-            }
-
-            if event.flags & syscall::EVENT_READ == syscall::EVENT_READ {
-                if let Some(ref mut set) = readfds {
-                    set.insert(event.id);
-                }
-                total += 1;
-            }
-            if event.flags & syscall::EVENT_WRITE == syscall::EVENT_WRITE {
-                if let Some(ref mut set) = writefds {
-                    set.insert(event.id);
-                }
-                total += 1;
-            }
-        }
-
-        total
-    }
-
     fn setpgid(pid: pid_t, pgid: pid_t) -> c_int {
         e(syscall::setpgid(pid as usize, pgid as usize)) as c_int
     }
-- 
GitLab