From e05896356f15f0df2a0931e01e7cdf640246c37d Mon Sep 17 00:00:00 2001 From: Nate Choe Date: Sun, 30 Jan 2022 12:43:10 -0600 Subject: [PATCH] Made sockets nonblocking (I'm a fool who didn't do this and spent several hours trying to figure out why I couldn't handle a second request if firefox was on the page I'm so dumb I hate myself I hate coding I hate the internet computers were a mistake) --- example/logs | 102 ++++++++++++++++++++++++++++++++++++++++++ example/run.sh | 2 +- example/sitefile | 2 +- src/connections.c | 5 +-- src/include/sockets.h | 2 +- src/main.c | 6 +-- src/responses.c | 1 + src/sockets.c | 5 ++- 8 files changed, 115 insertions(+), 10 deletions(-) diff --git a/example/logs b/example/logs index 9e6d259..bd77930 100644 --- a/example/logs +++ b/example/logs @@ -183,3 +183,105 @@ [2022-01-30T18:05:53Z] swebs started [2022-01-30T18:06:10Z] swebs started [2022-01-30T18:06:27Z] swebs started +[2022-01-30T18:11:39Z] swebs started +[2022-01-30T18:11:44Z] Accepting a stream failed +[2022-01-30T18:11:44Z] Accepting a stream failed +[2022-01-30T18:11:44Z] Accepting a stream failed +[2022-01-30T18:11:44Z] Accepting a stream failed +[2022-01-30T18:11:44Z] Accepting a stream failed +[2022-01-30T18:11:44Z] Accepting a stream failed +[2022-01-30T18:11:44Z] Accepting a stream failed +[2022-01-30T18:11:44Z] Accepting a stream failed +[2022-01-30T18:11:44Z] Accepting a stream failed +[2022-01-30T18:11:44Z] Accepting a stream failed +[2022-01-30T18:11:46Z] Accepting a stream failed +[2022-01-30T18:11:46Z] Accepting a stream failed +[2022-01-30T18:11:46Z] Accepting a stream failed +[2022-01-30T18:11:46Z] Accepting a stream failed +[2022-01-30T18:11:46Z] Accepting a stream failed +[2022-01-30T18:11:46Z] Accepting a stream failed +[2022-01-30T18:11:46Z] Accepting a stream failed +[2022-01-30T18:11:46Z] Accepting a stream failed +[2022-01-30T18:11:46Z] Accepting a stream failed +[2022-01-30T18:11:46Z] Accepting a stream failed +[2022-01-30T18:11:46Z] Accepting a stream failed +[2022-01-30T18:11:46Z] Accepting a stream failed +[2022-01-30T18:11:46Z] Accepting a stream failed +[2022-01-30T18:11:46Z] Accepting a stream failed +[2022-01-30T18:11:46Z] Accepting a stream failed +[2022-01-30T18:11:46Z] Accepting a stream failed +[2022-01-30T18:11:46Z] Accepting a stream failed +[2022-01-30T18:11:46Z] Accepting a stream failed +[2022-01-30T18:11:46Z] Accepting a stream failed +[2022-01-30T18:11:46Z] Accepting a stream failed +[2022-01-30T18:11:46Z] Accepting a stream failed +[2022-01-30T18:11:46Z] Accepting a stream failed +[2022-01-30T18:11:46Z] Accepting a stream failed +[2022-01-30T18:11:46Z] Accepting a stream failed +[2022-01-30T18:11:46Z] Accepting a stream failed +[2022-01-30T18:11:46Z] Accepting a stream failed +[2022-01-30T18:11:46Z] Accepting a stream failed +[2022-01-30T18:11:46Z] Accepting a stream failed +[2022-01-30T18:11:46Z] Accepting a stream failed +[2022-01-30T18:11:46Z] Accepting a stream failed +[2022-01-30T18:11:47Z] Accepting a stream failed +[2022-01-30T18:11:47Z] Accepting a stream failed +[2022-01-30T18:11:47Z] Accepting a stream failed +[2022-01-30T18:11:47Z] Accepting a stream failed +[2022-01-30T18:11:47Z] Accepting a stream failed +[2022-01-30T18:11:47Z] Accepting a stream failed +[2022-01-30T18:11:47Z] Accepting a stream failed +[2022-01-30T18:11:47Z] Accepting a stream failed +[2022-01-30T18:11:47Z] Accepting a stream failed +[2022-01-30T18:11:47Z] Accepting a stream failed +[2022-01-30T18:11:48Z] Accepting a stream failed +[2022-01-30T18:11:48Z] Accepting a stream failed +[2022-01-30T18:11:48Z] Accepting a stream failed +[2022-01-30T18:11:48Z] Accepting a stream failed +[2022-01-30T18:11:48Z] Accepting a stream failed +[2022-01-30T18:11:48Z] Accepting a stream failed +[2022-01-30T18:11:48Z] Accepting a stream failed +[2022-01-30T18:11:48Z] Accepting a stream failed +[2022-01-30T18:11:48Z] Accepting a stream failed +[2022-01-30T18:11:48Z] Accepting a stream failed +[2022-01-30T18:11:55Z] swebs started +[2022-01-30T18:12:05Z] Accepting a stream failed +[2022-01-30T18:12:15Z] swebs started +[2022-01-30T18:13:00Z] swebs started +[2022-01-30T18:13:37Z] swebs started +[2022-01-30T18:13:46Z] swebs started +[2022-01-30T18:15:42Z] swebs started +[2022-01-30T18:15:56Z] swebs started +[2022-01-30T18:16:10Z] swebs started +[2022-01-30T18:17:19Z] swebs started +[2022-01-30T18:17:35Z] swebs started +[2022-01-30T18:18:41Z] swebs started +[2022-01-30T18:19:01Z] swebs started +[2022-01-30T18:19:14Z] swebs started +[2022-01-30T18:20:30Z] swebs started +[2022-01-30T18:21:20Z] swebs started +[2022-01-30T18:23:10Z] swebs started +[2022-01-30T18:24:01Z] swebs started +[2022-01-30T18:25:19Z] swebs started +[2022-01-30T18:25:39Z] swebs started +[2022-01-30T18:26:13Z] swebs started +[2022-01-30T18:27:14Z] swebs started +[2022-01-30T18:27:30Z] swebs started +[2022-01-30T18:27:59Z] swebs started +[2022-01-30T18:28:03Z] swebs started +[2022-01-30T18:30:10Z] swebs started +[2022-01-30T18:30:17Z] swebs started +[2022-01-30T18:32:21Z] swebs started +[2022-01-30T18:32:24Z] swebs started +[2022-01-30T18:32:54Z] swebs started +[2022-01-30T18:32:59Z] swebs started +[2022-01-30T18:33:18Z] swebs started +[2022-01-30T18:33:28Z] swebs started +[2022-01-30T18:34:35Z] swebs started +[2022-01-30T18:35:17Z] swebs started +[2022-01-30T18:35:30Z] swebs started +[2022-01-30T18:37:09Z] swebs started +[2022-01-30T18:37:40Z] swebs started +[2022-01-30T18:38:20Z] swebs started +[2022-01-30T18:38:49Z] swebs started +[2022-01-30T18:38:54Z] swebs started diff --git a/example/run.sh b/example/run.sh index c7a8e50..7b38903 100755 --- a/example/run.sh +++ b/example/run.sh @@ -1,3 +1,3 @@ #!/bin/sh -../build/swebs -j 2 -s sitefile -o logs -p 8000 +../build/swebs -s sitefile -o logs -p 8000 -b 100 diff --git a/example/sitefile b/example/sitefile index 7f2233f..9bdbd0e 100644 --- a/example/sitefile +++ b/example/sitefile @@ -1,4 +1,4 @@ -define transport TLS +#define transport TLS define key domain.key define cert domain.crt define timeout 2000 diff --git a/src/connections.c b/src/connections.c index 08ed7b0..87681a4 100644 --- a/src/connections.c +++ b/src/connections.c @@ -202,7 +202,7 @@ static int processChar(Connection *conn, char c, Sitefile *site) { return 0; } -long diff(struct timespec *t1, struct timespec *t2) { +static long diff(struct timespec *t1, struct timespec *t2) { return (t2->tv_sec - t1->tv_sec) * 1000 + (t2->tv_nsec - t1->tv_nsec) / 1000000; } @@ -220,12 +220,11 @@ int updateConnection(Connection *conn, Sitefile *site) { if (received < 0) return errno != EAGAIN; if (received == 0) - break; + return 0; memcpy(&conn->lastdata, ¤tTime, sizeof(struct timespec)); for (unsigned long i = 0; i < received; i++) { if (processChar(conn, buff[i], site)) return 1; } } - return 0; } diff --git a/src/include/sockets.h b/src/include/sockets.h index f4b1c2b..942ce05 100644 --- a/src/include/sockets.h +++ b/src/include/sockets.h @@ -44,7 +44,7 @@ Listener *createListener(SocketType type, uint16_t port, int backlog, ...); //extra arguments depend on type (similar to fcntl): //tcp: (void) //tls: (char *keyfile, char *certfile, char *ocspfile) -Stream *acceptStream(Listener *listener); +Stream *acceptStream(Listener *listener, int flags); //returns 1 on error, accepts fcntl flags void freeListener(Listener *listener); diff --git a/src/main.c b/src/main.c index 944c8be..5d42d78 100644 --- a/src/main.c +++ b/src/main.c @@ -34,7 +34,7 @@ int main(int argc, char **argv) { char *logout = "/var/log/swebs.log"; char *sitefile = NULL; - int processes = 8; + int processes = sysconf(_SC_NPROCESSORS_ONLN) + 1; uint16_t port = 443; int backlog = 100; for (;;) { @@ -76,7 +76,7 @@ int main(int argc, char **argv) { printf( "Usage: swebs [options]\n" " -o [out] Set the log file (default: /var/log/swebs.log)\n" -" -j [cores] Use that many cores (default: 8)\n" +" -j [cores] Use that many cores (default: $(nproc)+1)\n" " -s [site file] Use that site file (required)\n" " -p [port] Set the port (default: 443)\n" " -b [backlog] Set the socket backlog (default: 100)\n" @@ -145,7 +145,7 @@ int main(int argc, char **argv) { createLog("swebs started"); for (;;) { - Stream *stream = acceptStream(listener); + Stream *stream = acceptStream(listener, O_NONBLOCK); if (stream == NULL) { createLog("Accepting a stream failed"); continue; diff --git a/src/responses.c b/src/responses.c index 931d278..2e9c8bb 100644 --- a/src/responses.c +++ b/src/responses.c @@ -142,6 +142,7 @@ int sendResponse(Connection *conn, Sitefile *site) { readResponse(conn, site->content[i].arg); goto end; default: + sendErrorResponse(conn, ERROR_500); return 1; } } diff --git a/src/sockets.c b/src/sockets.c index af74802..84b8f02 100644 --- a/src/sockets.c +++ b/src/sockets.c @@ -91,7 +91,7 @@ error: return NULL; } -Stream *acceptStream(Listener *listener) { +Stream *acceptStream(Listener *listener, int flags) { Stream *ret = malloc(sizeof(Stream)); if (ret == NULL) return NULL; @@ -104,6 +104,9 @@ Stream *acceptStream(Listener *listener) { return NULL; } + int oldflags = fcntl(ret->fd, F_GETFL); + fcntl(ret->fd, F_SETFL, oldflags | flags); + switch (listener->type) { case TCP: default: break;