commit 1e3ef7ab4f803b6309fcddc11dc23ecc5f33be27 from: Omar Polo date: Wed Feb 03 21:14:48 2021 UTC use upper bound given by poll it's a waste to loop through all fds. We know the *exact* number of clients that needs attention, so use that information to limit the looping. commit - e824d03efa218415d3625d14d168dfc954b39ef6 commit + 1e3ef7ab4f803b6309fcddc11dc23ecc5f33be27 blob - 07fb904bc4bcb08f50ab7500cdacf488263b3e7e blob + 8c40bd34cf507b21d8ca9a69230e0e5ecd6559f2 --- server.c +++ server.c @@ -902,7 +902,7 @@ do_accept(int sock, struct tls *ctx, struct pollfd *fd void loop(struct tls *ctx, int sock4, int sock6) { - int i; + int i, n; struct client clients[MAX_USERS]; struct pollfd fds[MAX_USERS]; @@ -918,16 +918,15 @@ loop(struct tls *ctx, int sock4, int sock6) fds[1].fd = sock6; for (;;) { - if (poll(fds, MAX_USERS, INFTIM) == -1) { + if ((n = poll(fds, MAX_USERS, INFTIM)) == -1) { if (errno == EINTR) { - fprintf(stderr, "connected clients: %d\n", + fprintf(stderr, "connected clients: %d\n", connected_clients); - continue; - } - fatal("poll: %s", strerror(errno)); + } else + fatal("poll: %s", strerror(errno)); } - for (i = 0; i < MAX_USERS; i++) { + for (i = 0; i < MAX_USERS && n > 0; i++) { if (fds[i].revents == 0) continue; @@ -935,6 +934,8 @@ loop(struct tls *ctx, int sock4, int sock6) fatal("bad fd %d: %s", fds[i].fd, strerror(errno)); + n--; + if (fds[i].revents & POLLHUP) { /* fds[i] may be the fd of the stdin * of a cgi script that has exited. */