From 50ddd3fe5966385926090142d6708d22a769c8fe Mon Sep 17 00:00:00 2001 From: JJJ Date: Thu, 10 Sep 2020 19:53:18 +0200 Subject: [PATCH 1/5] Fix for the fact that windows file descriptors do not follow possix numbering. --- src/main/main.c | 87 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 59 insertions(+), 28 deletions(-) diff --git a/src/main/main.c b/src/main/main.c index b90c139da..34f4c1ee9 100644 --- a/src/main/main.c +++ b/src/main/main.c @@ -83,15 +83,17 @@ enum { #endif }; +/** File descriptor handler struct */ +struct fhs { + int fd; /**< File Descriptor */ + int flags; /**< Polling flags (Read, Write, etc.) */ + fd_h* fh; /**< Event handler */ + void* arg; /**< Handler argument */ +}; /** Polling loop data */ struct re { - /** File descriptor handler set */ - struct { - int flags; /**< Polling flags (Read, Write, etc.) */ - fd_h *fh; /**< Event handler */ - void *arg; /**< Handler argument */ - } *fhs; + struct fhs *fhs; /** File descriptor handler set */ int maxfds; /**< Maximum number of polling fds */ int nfds; /**< Number of active file descriptors */ enum poll_method method; /**< The current polling method */ @@ -221,6 +223,19 @@ static struct re *re_get(void) #endif +#ifdef WIN32 +static int lookup_fd_index(struct re* re, int fd) { + int i = 0; + int lim = fd == 0 ? re->maxfds : re->nfds; + + for (i = 0; i < lim; i++) { + if (re->fhs[i].fd == fd) + return i; + } + + return -1; +} +#endif #if MAIN_DEBUG /** @@ -230,21 +245,21 @@ static struct re *re_get(void) * @param fd File descriptor * @param flags Event flags */ -static void fd_handler(struct re *re, int fd, int flags) +static void fd_handler(struct re *re, int i, int flags) { const uint64_t tick = tmr_jiffies(); uint32_t diff; - DEBUG_INFO("event on fd=%d (flags=0x%02x)...\n", fd, flags); + DEBUG_INFO("event on fd=%d (flags=0x%02x)...\n", re->fhs[i].fd, flags); - re->fhs[fd].fh(flags, re->fhs[fd].arg); + re->fhs[i].fh(flags, re->fhs[i].arg); diff = (uint32_t)(tmr_jiffies() - tick); if (diff > MAX_BLOCKING) { DEBUG_WARNING("long async blocking: %u>%u ms (h=%p arg=%p)\n", diff, MAX_BLOCKING, - re->fhs[fd].fh, re->fhs[fd].arg); + re->fhs[i].fh, re->fhs[i].arg); } } #endif @@ -565,6 +580,7 @@ int fd_listen(int fd, int flags, fd_h *fh, void *arg) { struct re *re = re_get(); int err = 0; + int i = fd; DEBUG_INFO("fd_listen: fd=%d flags=0x%02x\n", fd, flags); @@ -579,7 +595,18 @@ int fd_listen(int fd, int flags, fd_h *fh, void *arg) return err; } - if (fd >= re->maxfds) { +#ifdef WIN32 + // Windows file descriptors do not follow possix standard ranges. + // This code emulates possix numbering. There is no locking, so zero thread-safety. + // FRirst a linear search through the list of file handles to find existing descriptor. + i = lookup_fd_index(re, fd); + if (i == -1) { + // And if nothing is found a linear search for the first zeroed handler + i = lookup_fd_index(re, 0); + } +#endif // #ifdef WIN32 + + if (i >= re->maxfds) { if (flags) { DEBUG_WARNING("fd_listen: fd=%d flags=0x%02x" " - Max %d fds\n", @@ -590,12 +617,13 @@ int fd_listen(int fd, int flags, fd_h *fh, void *arg) /* Update fh set */ if (re->fhs) { - re->fhs[fd].flags = flags; - re->fhs[fd].fh = fh; - re->fhs[fd].arg = arg; + re->fhs[i].fd = fd; + re->fhs[i].flags = flags; + re->fhs[i].fh = fh; + re->fhs[i].arg = arg; } - re->nfds = max(re->nfds, fd+1); + re->nfds = max(re->nfds, i+1); switch (re->method) { @@ -683,15 +711,16 @@ static int fd_poll(struct re *re) FD_ZERO(&efds); for (i=0; infds; i++) { + int fd = re->fhs[i].fd; if (!re->fhs[i].fh) continue; if (re->fhs[i].flags & FD_READ) - FD_SET(i, &rfds); + FD_SET(fd, &rfds); if (re->fhs[i].flags & FD_WRITE) - FD_SET(i, &wfds); + FD_SET(fd, &wfds); if (re->fhs[i].flags & FD_EXCEPT) - FD_SET(i, &efds); + FD_SET(fd, &efds); } #ifdef WIN32 @@ -767,13 +796,15 @@ static int fd_poll(struct re *re) #endif #ifdef HAVE_SELECT case METHOD_SELECT: - fd = i; - if (FD_ISSET(fd, &rfds)) - flags |= FD_READ; - if (FD_ISSET(fd, &wfds)) - flags |= FD_WRITE; - if (FD_ISSET(fd, &efds)) - flags |= FD_EXCEPT; + fd = re->fhs[i].fd; + if (fd) { + if (FD_ISSET(fd, &rfds)) + flags |= FD_READ; + if (FD_ISSET(fd, &wfds)) + flags |= FD_WRITE; + if (FD_ISSET(fd, &efds)) + flags |= FD_EXCEPT; + } break; #endif #ifdef HAVE_EPOLL @@ -838,11 +869,11 @@ static int fd_poll(struct re *re) if (!flags) continue; - if (re->fhs[fd].fh) { + if (re->fhs[i].fh) { #if MAIN_DEBUG - fd_handler(re, fd, flags); + fd_handler(re, i, flags); #else - re->fhs[fd].fh(flags, re->fhs[fd].arg); + re->fhs[i].fh(flags, re->fhs[i].arg); #endif } From 4d299b107e0f1b5aef423734f9fa1a905bfb51cb Mon Sep 17 00:00:00 2001 From: JJJ Date: Sun, 13 Sep 2020 21:01:02 +0200 Subject: [PATCH 2/5] Moving more lookup logic into fd_lookup_index. Moving lookup before range checks. --- src/main/main.c | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/main/main.c b/src/main/main.c index 34f4c1ee9..b12ec8f76 100644 --- a/src/main/main.c +++ b/src/main/main.c @@ -224,15 +224,22 @@ static struct re *re_get(void) #endif #ifdef WIN32 +// This code emulates possix numbering. There is no locking, so zero thread-safety. +// First a linear search through the list of file handles to find existing descriptor. static int lookup_fd_index(struct re* re, int fd) { int i = 0; - int lim = fd == 0 ? re->maxfds : re->nfds; - for (i = 0; i < lim; i++) { + for (i = 0; i < re->nfds; i++) { if (re->fhs[i].fd == fd) return i; } + // And if nothing is found a linear search for the first zeroed handler + for (i = 0; i < re->maxfds; i++) { + if (re->fhs[i].fd == 0) + return i; + } + return -1; } #endif @@ -584,6 +591,12 @@ int fd_listen(int fd, int flags, fd_h *fh, void *arg) DEBUG_INFO("fd_listen: fd=%d flags=0x%02x\n", fd, flags); + +#ifdef WIN32 + // Windows file descriptors do not follow possix standard ranges. + i = lookup_fd_index(re, fd); +#endif // #ifdef WIN32 + if (fd < 0) { DEBUG_WARNING("fd_listen: corrupt fd %d\n", fd); return EBADF; @@ -595,17 +608,6 @@ int fd_listen(int fd, int flags, fd_h *fh, void *arg) return err; } -#ifdef WIN32 - // Windows file descriptors do not follow possix standard ranges. - // This code emulates possix numbering. There is no locking, so zero thread-safety. - // FRirst a linear search through the list of file handles to find existing descriptor. - i = lookup_fd_index(re, fd); - if (i == -1) { - // And if nothing is found a linear search for the first zeroed handler - i = lookup_fd_index(re, 0); - } -#endif // #ifdef WIN32 - if (i >= re->maxfds) { if (flags) { DEBUG_WARNING("fd_listen: fd=%d flags=0x%02x" From cf7848b2aeffa1864b4fc773adb009c9fb1d8831 Mon Sep 17 00:00:00 2001 From: johnjuuljensen Date: Mon, 14 Sep 2020 21:51:43 +0200 Subject: [PATCH 3/5] Style fixes --- src/main/main.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/main/main.c b/src/main/main.c index b12ec8f76..5107872c4 100644 --- a/src/main/main.c +++ b/src/main/main.c @@ -224,17 +224,19 @@ static struct re *re_get(void) #endif #ifdef WIN32 -// This code emulates possix numbering. There is no locking, so zero thread-safety. -// First a linear search through the list of file handles to find existing descriptor. +/** + * This code emulates POSIX numbering. There is no locking, so zero thread-safety. + */ static int lookup_fd_index(struct re* re, int fd) { int i = 0; + /* First a linear search through the list of file handles to find existing descriptor. */ for (i = 0; i < re->nfds; i++) { if (re->fhs[i].fd == fd) return i; } - // And if nothing is found a linear search for the first zeroed handler + /* And if nothing is found a linear search for the first zeroed handler */ for (i = 0; i < re->maxfds; i++) { if (re->fhs[i].fd == 0) return i; @@ -593,9 +595,9 @@ int fd_listen(int fd, int flags, fd_h *fh, void *arg) #ifdef WIN32 - // Windows file descriptors do not follow possix standard ranges. + /* Windows file descriptors do not follow POSIX standard ranges. */ i = lookup_fd_index(re, fd); -#endif // #ifdef WIN32 +#endif if (fd < 0) { DEBUG_WARNING("fd_listen: corrupt fd %d\n", fd); From d723b29fc9439394e0d92d82b595d0032d384969 Mon Sep 17 00:00:00 2001 From: johnjuuljensen Date: Mon, 14 Sep 2020 22:31:53 +0200 Subject: [PATCH 4/5] More style fixing --- src/main/main.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main/main.c b/src/main/main.c index 5107872c4..6e7c71fa0 100644 --- a/src/main/main.c +++ b/src/main/main.c @@ -225,18 +225,21 @@ static struct re *re_get(void) #ifdef WIN32 /** - * This code emulates POSIX numbering. There is no locking, so zero thread-safety. + * This code emulates POSIX numbering. There is no locking, + * so zero thread-safety. */ static int lookup_fd_index(struct re* re, int fd) { int i = 0; - /* First a linear search through the list of file handles to find existing descriptor. */ + /* First a linear search through the list of file handles + * to find existing descriptor. */ for (i = 0; i < re->nfds; i++) { if (re->fhs[i].fd == fd) return i; } - /* And if nothing is found a linear search for the first zeroed handler */ + /* And if nothing is found a linear search for the first + * zeroed handler */ for (i = 0; i < re->maxfds; i++) { if (re->fhs[i].fd == 0) return i; From e980aff3b95da16e9ae3af6faa7e1404d56acfff Mon Sep 17 00:00:00 2001 From: jjj Date: Mon, 14 Sep 2020 22:38:53 +0200 Subject: [PATCH 5/5] more styles --- src/main/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/main.c b/src/main/main.c index 6e7c71fa0..04453e58e 100644 --- a/src/main/main.c +++ b/src/main/main.c @@ -231,7 +231,7 @@ static struct re *re_get(void) static int lookup_fd_index(struct re* re, int fd) { int i = 0; - /* First a linear search through the list of file handles + /* First a linear search through the list of file handles * to find existing descriptor. */ for (i = 0; i < re->nfds; i++) { if (re->fhs[i].fd == fd)