Proper TLS handling for poll(). Make poll() default on linux

This commit is contained in:
Sergey Lyubka 2022-05-26 08:55:28 +01:00
parent 252e4715d4
commit 10596a8bdc
4 changed files with 38 additions and 28 deletions

View File

@ -4396,7 +4396,7 @@ static void mg_iotest(struct mg_mgr *mgr, int ms) {
#elif MG_ENABLE_POLL
size_t i = 0, n = 0;
for (struct mg_connection *c = mgr->conns; c != NULL; c = c->next) n++;
struct pollfd fds[n == 0 ? 1 : n]; // Avoid zero-length VLA
struct pollfd fds[n == 0 ? 1 : n]; // Avoid zero-length VLA
memset(fds, 0, sizeof(fds));
for (struct mg_connection *c = mgr->conns; c != NULL; c = c->next, i++) {
@ -4406,24 +4406,25 @@ static void mg_iotest(struct mg_mgr *mgr, int ms) {
fds[i].fd = FD(c);
fds[i].events |= POLLIN;
if (c->is_connecting || (c->send.len > 0 && c->is_tls_hs == 0)) {
fds[i].events |= POLLOUT;
fds[i].events |= POLLOUT;
}
if (mg_tls_pending(c) > 0) ms = 0; // Don't wait if TLS is ready
}
}
if (poll(fds, n, ms) < 0) {
MG_ERROR(("poll failed, errno: %d", MG_SOCK_ERRNO));
return;
}
i = 0;
for (struct mg_connection *c = mgr->conns; c != NULL; c = c->next, i++) {
if (c->is_closing || c->is_resolving || FD(c) == INVALID_SOCKET) {
// Socket not valid, ignore
} else {
c->is_readable = (unsigned)(fds[i].revents & POLLIN ? true : false);
c->is_writable = (unsigned)(fds[i].revents & POLLOUT ? true : false);
fds[i].revents = 0;
} else {
i = 0;
for (struct mg_connection *c = mgr->conns; c != NULL; c = c->next, i++) {
if (c->is_closing || c->is_resolving || FD(c) == INVALID_SOCKET) {
// Socket not valid, ignore
} else {
c->is_readable = (unsigned) (fds[i].revents & POLLIN ? true : false);
c->is_writable = (unsigned) (fds[i].revents & POLLOUT ? true : false);
fds[i].revents = 0;
if (mg_tls_pending(c) > 0) c->is_readable = 1;
}
}
}
#else

View File

@ -418,6 +418,10 @@ extern int SockSet(SOCKET hSock, int Type, int Prop, void *pbuf, int size);
#define _DARWIN_UNLIMITED_SELECT 1 // No limit on file descriptors
#if !defined(MG_ENABLE_POLL) && defined(__linux__)
#define MG_ENABLE_POLL 1
#endif
#include <arpa/inet.h>
#include <ctype.h>
#include <dirent.h>
@ -436,7 +440,7 @@ extern int SockSet(SOCKET hSock, int Type, int Prop, void *pbuf, int size);
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(MG_ENABLE_POLL) && MG_ENABLE_POLL
#if MG_ENABLE_POLL
#include <poll.h>
#else
#include <sys/select.h>

View File

@ -4,6 +4,10 @@
#define _DARWIN_UNLIMITED_SELECT 1 // No limit on file descriptors
#if !defined(MG_ENABLE_POLL) && defined(__linux__)
#define MG_ENABLE_POLL 1
#endif
#include <arpa/inet.h>
#include <ctype.h>
#include <dirent.h>
@ -22,7 +26,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(MG_ENABLE_POLL) && MG_ENABLE_POLL
#if MG_ENABLE_POLL
#include <poll.h>
#else
#include <sys/select.h>

View File

@ -478,7 +478,7 @@ static void mg_iotest(struct mg_mgr *mgr, int ms) {
#elif MG_ENABLE_POLL
size_t i = 0, n = 0;
for (struct mg_connection *c = mgr->conns; c != NULL; c = c->next) n++;
struct pollfd fds[n == 0 ? 1 : n]; // Avoid zero-length VLA
struct pollfd fds[n == 0 ? 1 : n]; // Avoid zero-length VLA
memset(fds, 0, sizeof(fds));
for (struct mg_connection *c = mgr->conns; c != NULL; c = c->next, i++) {
@ -488,24 +488,25 @@ static void mg_iotest(struct mg_mgr *mgr, int ms) {
fds[i].fd = FD(c);
fds[i].events |= POLLIN;
if (c->is_connecting || (c->send.len > 0 && c->is_tls_hs == 0)) {
fds[i].events |= POLLOUT;
fds[i].events |= POLLOUT;
}
if (mg_tls_pending(c) > 0) ms = 0; // Don't wait if TLS is ready
}
}
if (poll(fds, n, ms) < 0) {
MG_ERROR(("poll failed, errno: %d", MG_SOCK_ERRNO));
return;
}
i = 0;
for (struct mg_connection *c = mgr->conns; c != NULL; c = c->next, i++) {
if (c->is_closing || c->is_resolving || FD(c) == INVALID_SOCKET) {
// Socket not valid, ignore
} else {
c->is_readable = (unsigned)(fds[i].revents & POLLIN ? true : false);
c->is_writable = (unsigned)(fds[i].revents & POLLOUT ? true : false);
fds[i].revents = 0;
} else {
i = 0;
for (struct mg_connection *c = mgr->conns; c != NULL; c = c->next, i++) {
if (c->is_closing || c->is_resolving || FD(c) == INVALID_SOCKET) {
// Socket not valid, ignore
} else {
c->is_readable = (unsigned) (fds[i].revents & POLLIN ? true : false);
c->is_writable = (unsigned) (fds[i].revents & POLLOUT ? true : false);
fds[i].revents = 0;
if (mg_tls_pending(c) > 0) c->is_readable = 1;
}
}
}
#else