From 73b8c378f2bbfe1f59c3bfac3479f351063194a7 Mon Sep 17 00:00:00 2001 From: Toni Uhlig Date: Wed, 4 Oct 2023 15:21:55 +0200 Subject: nDPId event I/O fixes. * forcibly disable epoll even if available * nDPId-test event I/O selftest * CI event I/O tests Signed-off-by: Toni Uhlig --- nio.c | 43 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 9 deletions(-) (limited to 'nio.c') diff --git a/nio.c b/nio.c index 92e657d47..0e1ea3671 100644 --- a/nio.c +++ b/nio.c @@ -5,11 +5,12 @@ #ifdef ENABLE_EPOLL #include #endif +#include void nio_init(struct nio * io) { io->nready = -1; - io->poll_max_fds = -1; + io->poll_max_fds = 0; io->poll_cur_fds = 0; io->poll_fds = NULL; io->poll_ptrs = NULL; @@ -27,6 +28,11 @@ int nio_use_poll(struct nio * io, nfds_t max_fds) io->poll_fds = (struct pollfd *)calloc(max_fds, sizeof(*io->poll_fds)); io->poll_ptrs = calloc(max_fds, sizeof(*io->poll_ptrs)); + for (size_t i = 0; i < max_fds; ++i) + { + io->poll_fds[i].fd = -1; + } + return io->poll_fds == NULL || io->poll_ptrs == NULL; // return NIO_ERROR_INTERNAL on error } @@ -40,7 +46,7 @@ int nio_use_epoll(struct nio * io, int max_events) io->max_events = max_events; io->events = calloc(max_events, sizeof(struct epoll_event)); - return io->epoll_fd; + return io->events == NULL || io->epoll_fd < 0; #else (void)io; (void)max_events; @@ -76,7 +82,7 @@ int nio_add_fd(struct nio * io, int fd, int event_flags, void * ptr) } else #endif - if (io->poll_max_fds > 0) + if (io->poll_max_fds > 0) { struct pollfd * unused_pollfd = NULL; void ** unused_ptr = NULL; @@ -247,7 +253,7 @@ int nio_check(struct nio * io, int index, int events) return NIO_ERROR_INTERNAL; #ifdef ENABLE_EPOLL - if (io->epoll_fd >= 0 && index >= 0 && index < io->max_events) + if (io->epoll_fd >= 0 && index < io->max_events) { uint32_t epoll_events = 0; @@ -255,18 +261,20 @@ int nio_check(struct nio * io, int index, int events) epoll_events |= EPOLLIN; if ((events & NIO_EVENT_OUTPUT) != 0) epoll_events |= EPOLLOUT; + if ((events & NIO_EVENT_ERROR) != 0) + epoll_events |= EPOLLERR | EPOLLHUP; if (epoll_events == 0) return NIO_ERROR_INTERNAL; struct epoll_event * ee = (struct epoll_event *)io->events; - if ((ee[index].events & epoll_events) != epoll_events) + if ((ee[index].events & epoll_events) == 0) return NIO_ERROR_INTERNAL; return NIO_ERROR_SUCCESS; } else #endif - if (io->poll_max_fds > 0 && index >= 0 && index < (int)io->poll_max_fds) + if (io->poll_max_fds > 0 && index < (int)io->poll_max_fds) { short int poll_events = 0; @@ -274,10 +282,12 @@ int nio_check(struct nio * io, int index, int events) poll_events |= POLLIN; if ((events & NIO_EVENT_OUTPUT) != 0) poll_events |= POLLOUT; + if ((events & NIO_EVENT_ERROR) != 0) + poll_events |= POLLERR | POLLHUP; if (poll_events == 0) return NIO_ERROR_INTERNAL; - if (io->poll_fds[index].revents != poll_events) + if ((io->poll_fds[index].revents & poll_events) == 0) return NIO_ERROR_INTERNAL; return NIO_ERROR_SUCCESS; @@ -292,13 +302,13 @@ int nio_is_valid(struct nio * io, int index) return NIO_ERROR_INTERNAL; #ifdef ENABLE_EPOLL - if (io->epoll_fd >= 0 && index >= 0 && index <= io->max_events) + if (io->epoll_fd >= 0 && index <= io->max_events) { return NIO_ERROR_SUCCESS; } else #endif - if (io->poll_max_fds > 0 && index >= 0 && index < (int)io->poll_max_fds) + if (io->poll_max_fds > 0 && index < (int)io->poll_max_fds) { if (io->poll_fds[index].revents != 0) return NIO_ERROR_SUCCESS; @@ -324,6 +334,21 @@ int nio_has_error(struct nio * io, int index) void nio_free(struct nio * io) { + for (size_t i = 0; i < io->poll_max_fds; ++i) + { + if (io->poll_fds[i].fd >= 0) + { + close(io->poll_fds[i].fd); + io->poll_fds[i].fd = -1; + } + } +#ifdef ENABLE_EPOLL + if (io->epoll_fd >= 0) + { + close(io->epoll_fd); + io->epoll_fd = -1; + } +#endif free(io->poll_fds); free(io->poll_ptrs); free(io->events); -- cgit v1.2.3