diff --git a/src/libos/src/fs/events.rs b/src/libos/src/fs/events.rs index 55ce6e3a..c2c2ce6a 100644 --- a/src/libos/src/fs/events.rs +++ b/src/libos/src/fs/events.rs @@ -2,13 +2,37 @@ use crate::events::{Event, EventFilter, Notifier, Observer}; use crate::prelude::*; bitflags! { + /// I/O Events + #[rustfmt::skip] pub struct IoEvents: u32 { - const IN = 0x001; // = POLLIN - const OUT = 0x004; // = POLLOUT - const PRI = 0x002; // = POLLPRI - const ERR = 0x008; // = POLLERR + const IN = 0x0001; // = POLLIN + const PRI = 0x0002; // = POLLPRI + const OUT = 0x0004; // = POLLOUT + const ERR = 0x0008; // = POLLERR + const HUP = 0x0010; // = POLLHUP + const NVAL = 0x0020; // = POLLNVAL const RDHUP = 0x2000; // = POLLRDHUP - const HUP = 0x010; // = POLLHUP + } +} + +impl IoEvents { + pub fn from_raw(raw: u32) -> Self { + if Self::contains_unrecognizable_bits(raw) { + warn!("contain unknow flags: {:#x}", raw); + } + Self::from_bits_truncate(raw) + } + + fn contains_unrecognizable_bits(raw: u32) -> bool { + // Help to detect four valid but mostly useless flags that we do not + // handle, yet: POLLRDNORM, POLLRDBAND, POLLWRNORM, annd POLLWRBAND. + + let all_raw = Self::all().to_raw(); + (raw & !all_raw) != 0 + } + + pub fn to_raw(&self) -> u32 { + self.bits() } } diff --git a/src/libos/src/net/io_multiplexing/epoll/host_file_epoller.rs b/src/libos/src/net/io_multiplexing/epoll/host_file_epoller.rs index ce65d0fc..8980681b 100644 --- a/src/libos/src/net/io_multiplexing/epoll/host_file_epoller.rs +++ b/src/libos/src/net/io_multiplexing/epoll/host_file_epoller.rs @@ -156,7 +156,7 @@ impl HostFileEpoller { let mut host_files = self.host_files.lock().unwrap(); for raw_event in &raw_events[..count] { let raw_event = unsafe { raw_event.assume_init() }; - let io_events = IoEvents::from_bits_truncate(raw_event.events as u32); + let io_events = IoEvents::from_raw(raw_event.events as u32); let host_fd = raw_event.u64 as u32; let host_file = match host_files.get(&host_fd) { diff --git a/src/libos/src/net/io_multiplexing/epoll/mod.rs b/src/libos/src/net/io_multiplexing/epoll/mod.rs index f1f8a6d8..8e8f0a23 100644 --- a/src/libos/src/net/io_multiplexing/epoll/mod.rs +++ b/src/libos/src/net/io_multiplexing/epoll/mod.rs @@ -55,7 +55,7 @@ impl EpollEvent { } pub fn from_c(c_event: &libc::epoll_event) -> Self { - let mask = IoEvents::from_bits_truncate(c_event.events as u32); + let mask = IoEvents::from_raw(c_event.events as u32); let user_data = c_event.u64; Self { mask, user_data } }