diff options
author | Toni Uhlig <matzeton@googlemail.com> | 2023-10-04 15:21:55 +0200 |
---|---|---|
committer | Toni Uhlig <matzeton@googlemail.com> | 2023-10-05 12:32:45 +0200 |
commit | 73b8c378f2bbfe1f59c3bfac3479f351063194a7 (patch) | |
tree | 39ca9ed6a87ab6040a8e9175f702f0eb509be367 /nio.c | |
parent | a0e0611c56e9bd291e3ae7f534ab279fc9143369 (diff) |
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 <matzeton@googlemail.com>
Diffstat (limited to 'nio.c')
-rw-r--r-- | nio.c | 43 |
1 files changed, 34 insertions, 9 deletions
@@ -5,11 +5,12 @@ #ifdef ENABLE_EPOLL #include <sys/epoll.h> #endif +#include <unistd.h> 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); |