lio 0.4.1

A platform-independent async I/O library with native support for io_uring (Linux), IOCP (Windows), and kqueue (macOS)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
use super::super::{Interest, ReadinessPoll};
use super::NOTIFY_KEY;
use std::marker::PhantomData;
use std::mem::MaybeUninit;
use std::os::fd::{AsFd, BorrowedFd};
use std::os::fd::{AsRawFd, FromRawFd, OwnedFd, RawFd};
use std::time::Duration;
use std::{io, ptr};

/// Wrapper around an epoll file descriptor
///
/// This type is intentionally `!Send` to ensure it's only used from a single thread.
pub struct OsPoller {
  /// File descriptor for the epoll instance.
  epoll_fd: OwnedFd,
  // /// Notifier used to wake up epoll.
  notifier: Notifier,

  /// File descriptor for the timerfd that produces timeouts.
  ///
  /// Redox does not support timerfd.
  #[cfg(not(target_os = "redox"))]
  timer_fd: Option<OwnedFd>,

  /// Marker to make this type `!Send`
  _not_send: PhantomData<*const ()>,
}

impl OsPoller {
  /// Create a new epoll instance
  pub fn new() -> io::Result<Self> {
    // Create epoll instance with CLOEXEC
    let epoll_fd = {
      let fd = syscall!(epoll_create1(libc::EPOLL_CLOEXEC))?;
      // SAFETY: fd is valid, just returned successfully from epoll_create1
      unsafe { OwnedFd::from_raw_fd(fd) }
    };

    // // Create notifier (uses pipe for portability)
    let notifier = Notifier::new()?;

    #[cfg(not(target_os = "redox"))]
    let timer_fd = {
      let fd: RawFd =
        syscall!(timerfd_create(libc::CLOCK_MONOTONIC, libc::TFD_CLOEXEC))?;
      // SAFETY: fd is valid, just returned successfully from timerfd_create
      unsafe { OwnedFd::from_raw_fd(fd) }
    };

    let epoll = Self {
      epoll_fd,
      notifier,

      #[cfg(not(target_os = "redox"))]
      timer_fd: Some(timer_fd),

      _not_send: PhantomData,
    };

    #[cfg(not(target_os = "redox"))]
    if let Some(ref timer_fd) = epoll.timer_fd {
      epoll.add(timer_fd.as_raw_fd(), NOTIFY_KEY, Interest::NONE)?;
    }

    epoll.add(
      epoll.notifier.as_fd().as_raw_fd(),
      NOTIFY_KEY,
      Interest::READ,
    )?;

    Ok(epoll)
  }
}
impl Drop for OsPoller {
  fn drop(&mut self) {
    #[cfg(not(target_os = "redox"))]
    if let Some(timer_fd) = self.timer_fd.take() {
      let _ = self.delete(timer_fd.as_fd().as_raw_fd());
    }
    let _ = self.delete(self.notifier.as_fd().as_raw_fd());
  }
}

impl ReadinessPoll for OsPoller {
  type NativeEvent = libc::epoll_event;

  fn add(&self, fd: RawFd, key: u64, interest: Interest) -> io::Result<()> {
    let mut events = 0u32;

    if interest.is_readable() {
      events |= libc::EPOLLIN as u32;
    }
    if interest.is_writable() {
      events |= libc::EPOLLOUT as u32;
    }

    // Use EPOLLONESHOT for consistency with kqueue's EV_ONESHOT behavior
    events |= libc::EPOLLONESHOT as u32;
    // Note: EPOLLHUP and EPOLLERR are always reported by the kernel regardless of registration

    let mut event = libc::epoll_event { events, u64: key as u64 };

    syscall!(epoll_ctl(
      self.epoll_fd.as_raw_fd(),
      libc::EPOLL_CTL_ADD,
      fd,
      &mut event as *mut libc::epoll_event,
    ))?;

    // Postcondition: if successful, fd is now registered
    // (no way to verify without tracking state, rely on kernel)
    Ok(())
  }

  fn modify(&self, fd: RawFd, key: u64, interest: Interest) -> io::Result<()> {
    let mut events = 0u32;

    if interest.is_readable() {
      events |= libc::EPOLLIN as u32;
    }
    if interest.is_writable() {
      events |= libc::EPOLLOUT as u32;
    }

    // Use EPOLLONESHOT for consistency with kqueue's EV_ONESHOT behavior
    events |= libc::EPOLLONESHOT as u32;
    // Note: EPOLLHUP and EPOLLERR are always reported by the kernel regardless of registration

    let mut event = libc::epoll_event { events, u64: key as u64 };

    syscall!(epoll_ctl(
      self.epoll_fd.as_raw_fd(),
      libc::EPOLL_CTL_MOD,
      fd,
      &mut event as *mut libc::epoll_event,
    ))?;

    Ok(())
  }

  fn delete(&self, fd: RawFd) -> io::Result<()> {
    // For EPOLL_CTL_DEL, event pointer can be NULL in Linux 2.6.9+
    match syscall!(epoll_ctl(
      self.epoll_fd.as_raw_fd(),
      libc::EPOLL_CTL_DEL,
      fd,
      ptr::null_mut(),
    )) {
      Ok(_) => Ok(()),
      Err(err) => Err(match err.raw_os_error() {
        Some(libc::EBADF) => io::Error::from_raw_os_error(libc::ENOENT),
        _ => err,
      }),
    }
  }

  fn delete_timer(&self, _key: u64) -> io::Result<()> {
    // epoll doesn't use this - timers on Linux use timerfd which is a regular fd
    // and gets deleted via delete() instead
    Err(io::Error::from_raw_os_error(libc::ENOENT))
  }

  /// Returns [`libc::EINVAL`] if events.is_empty()
  fn wait(
    &self,
    events: &mut [Self::NativeEvent],
    timeout: Option<Duration>,
  ) -> io::Result<usize> {
    /// `timespec` value that equals zero.
    #[cfg(not(target_os = "redox"))]
    // SAFETY: All-zeros is a valid representation of timespec (all integer fields)
    const TS_ZERO: libc::timespec = unsafe {
      std::mem::transmute([0u8; std::mem::size_of::<libc::timespec>()])
    };
    // SAFETY: All-zeros is a valid representation of itimerspec (contains timespec fields)
    #[allow(dead_code)]
    const ITS_ZERO: libc::itimerspec = unsafe {
      std::mem::transmute([0u8; std::mem::size_of::<libc::itimerspec>()])
    };

    #[cfg(not(target_os = "redox"))]
    if let Some(ref timer_fd) = self.timer_fd {
      // Configure the timeout using timerfd.
      let mut new_val = libc::itimerspec {
        it_interval: TS_ZERO,
        it_value: match timeout {
          None => TS_ZERO,
          Some(t) => {
            let mut ts = TS_ZERO;
            ts.tv_sec = t.as_secs() as _;
            ts.tv_nsec = t.subsec_nanos() as _;
            ts
          }
        },
      };

      let mut result = MaybeUninit::<libc::itimerspec>::uninit();
      syscall!(timerfd_settime(
        timer_fd.as_raw_fd(),
        0,
        &mut new_val as *mut _,
        result.as_mut_ptr()
      ))?;

      // Set interest in timerfd.
      self.modify(timer_fd.as_raw_fd(), NOTIFY_KEY, Interest::READ)?;
    }

    #[cfg(not(target_os = "redox"))]
    let timer_fd = &self.timer_fd;
    #[cfg(target_os = "redox")]
    let timer_fd: Option<core::convert::Infallible> = None;

    // Timeout for epoll. In case of overflow, use no timeout.
    let timeout = match (timer_fd, timeout) {
      (_, Some(t)) if t == Duration::from_secs(0) => Some(TS_ZERO),
      (None, Some(t)) => Some(libc::timespec {
        tv_sec: t.as_secs() as i64,
        tv_nsec: t.subsec_nanos() as i64,
      }),
      _ => None,
    };

    // Wait for I/O events.
    let n = syscall!(epoll_pwait2(
      self.epoll_fd.as_raw_fd(),
      events.as_mut_ptr(),
      events.len() as i32,
      timeout.as_ref().map(|v| v as *const _).unwrap_or(ptr::null()),
      ptr::null_mut()
    ))?;

    // Clear the notification (if received) and re-register interest in it.
    self.notifier.clear();
    self.modify(
      self.notifier.as_fd().as_raw_fd(),
      NOTIFY_KEY,
      Interest::READ,
    )?;

    // Filter out internal NOTIFY_KEY events before returning to caller
    // This includes both the notifier and timerfd events
    let mut write_idx = 0;
    for read_idx in 0..n as usize {
      if events[read_idx].u64 != NOTIFY_KEY {
        if write_idx != read_idx {
          events[write_idx] = events[read_idx];
        }
        write_idx += 1;
      }
    }

    Ok(write_idx)
  }

  fn notify(&self) -> io::Result<()> {
    self.notifier.notify();
    Ok(())
  }

  fn event_key(event: &Self::NativeEvent) -> u64 {
    event.u64
  }

  fn event_interest(event: &Self::NativeEvent) -> Interest {
    // epoll can return both read and write in a single event
    let readable = (event.events & libc::EPOLLIN as u32) != 0;
    let writable = (event.events & libc::EPOLLOUT as u32) != 0;

    // EPOLLHUP, EPOLLERR, and EPOLLRDHUP should be treated as making the fd readable
    // so that operations can be attempted and will return appropriate errors
    let is_hup = (event.events & libc::EPOLLHUP as u32) != 0;
    let is_err = (event.events & libc::EPOLLERR as u32) != 0;
    let is_rdhup = (event.events & libc::EPOLLRDHUP as u32) != 0;

    let error_or_hup = is_hup || is_err || is_rdhup;
    let effective_readable = readable || error_or_hup;

    match (effective_readable, writable) {
      (true, true) => Interest::READ_AND_WRITE,
      (true, false) => Interest::READ,
      (false, true) => Interest::WRITE,
      (false, false) => Interest::READ, // Fallback, shouldn't happen
    }
  }
}

enum Notifier {
  /// The primary notifier, using eventfd.
  #[cfg(not(target_os = "redox"))]
  EventFd(OwnedFd),

  /// The fallback notifier, using a pipe.
  Pipe {
    /// The read end of the pipe.
    read_pipe: OwnedFd,

    /// The write end of the pipe.
    write_pipe: OwnedFd,
  },
}

impl AsFd for Notifier {
  fn as_fd(&self) -> BorrowedFd<'_> {
    match self {
      #[cfg(not(target_os = "redox"))]
      Notifier::EventFd(fd) => fd.as_fd(),
      Notifier::Pipe { read_pipe: read, .. } => read.as_fd(),
    }
  }
}

fn pipe() -> io::Result<(OwnedFd, OwnedFd)> {
  let mut result = MaybeUninit::<[OwnedFd; 2]>::uninit();
  match syscall!(pipe2(result.as_mut_ptr().cast::<_>(), libc::O_CLOEXEC)) {
    Ok(_) => {
      // SAFETY: pipe2 succeeded, so result is now initialized with two valid fds
      let [read, write] = unsafe { result.assume_init() };
      Ok((read, write))
    }
    Err(_) => {
      use libc::{F_GETFD, F_SETFD, FD_CLOEXEC};
      syscall!(pipe(result.as_mut_ptr().cast::<_>()))?;
      // SAFETY: pipe succeeded, so result is now initialized with two valid fds
      let [read, write] = unsafe { result.assume_init() };

      let flags = syscall!(fcntl(read.as_raw_fd(), F_GETFD))?;
      syscall!(fcntl(read.as_raw_fd(), F_SETFD, flags | FD_CLOEXEC))?;

      let flags = syscall!(fcntl(write.as_raw_fd(), F_GETFD))?;
      syscall!(fcntl(write.as_raw_fd(), F_SETFD, flags | FD_CLOEXEC))?;

      Ok((read, write))
    }
  }
}

impl Notifier {
  fn new() -> io::Result<Self> {
    // Skip eventfd for testing if necessary.
    #[cfg(not(target_os = "redox"))]
    {
      // Try to create an eventfd.
      match syscall!(eventfd(0, libc::EFD_CLOEXEC | libc::EFD_NONBLOCK)) {
        Ok(fd) => {
          // SAFETY: fd is valid, just returned successfully from eventfd
          let owned = unsafe { OwnedFd::from_raw_fd(fd) };
          return Ok(Notifier::EventFd(owned));
        }

        Err(_err) => {}
      }
    }

    let (read, write) = pipe()?;
    let flags = syscall!(fcntl(read.as_raw_fd(), libc::F_GETFL))?;
    syscall!(fcntl(read.as_raw_fd(), libc::F_SETFL, flags | libc::O_NONBLOCK))?;

    Ok(Notifier::Pipe { read_pipe: read, write_pipe: write })
  }

  pub fn notify(&self) {
    match self {
      #[cfg(not(target_os = "redox"))]
      Self::EventFd(fd) => {
        let buf: [u8; 8] = 1u64.to_ne_bytes();
        let _ = syscall!(write(
          fd.as_raw_fd(),
          buf.as_ptr().cast::<libc::c_void>(),
          buf.len()
        ));
      }

      Self::Pipe { write_pipe, .. } => {
        let buf = [0; 1];
        syscall!(write(
          write_pipe.as_raw_fd(),
          buf.as_ptr().cast::<libc::c_void>(),
          buf.len()
        ))
        .ok();
      }
    }
  }

  /// Clear the notification.
  fn clear(&self) {
    match self {
      #[cfg(not(target_os = "redox"))]
      Self::EventFd(fd) => {
        const SIZE: usize = 8;
        let mut buf = [0u8; SIZE];
        let _ = syscall!(read(
          fd.as_raw_fd(),
          buf.as_mut_ptr().cast::<libc::c_void>(),
          SIZE
        ));
      }

      Self::Pipe { read_pipe, .. } => {
        const SIZE: usize = 1024;
        while syscall!(read(
          read_pipe.as_raw_fd(),
          [0u8; SIZE].as_mut_ptr().cast::<libc::c_void>(),
          SIZE
        ))
        .is_ok()
        {}
      }
    }
  }
}

#[cfg(test)]
mod tests {
  use super::*;
  crate::generate_tests!(OsPoller::new().unwrap());
}