~sircmpwn/gmnisrv

95138564acbc347c0a5f4f86d9280e3ab1484fd2 — Remy Noulin 1 year, 7 months ago 132f2ec
Close some files when too many are open
1 files changed, 11 insertions(+), 0 deletions(-)

M src/server.c
M src/server.c => src/server.c +11 -0
@@ 115,8 115,19 @@ accept_client(struct gmnisrv_server *server, int fd)
	int sockfd = accept(fd, &addr, &addrlen);
	if (sockfd == -1) {
		server_error("accept error: %s", strerror(errno));
		if (errno == EMFILE) {
			for (int i = 1; i < 4; i++) {
				struct gmnisrv_client *client = &server->clients[server->nclients-i];
				disconnect_client(server, client);
			}
			sockfd = accept(fd, &addr, &addrlen);
			if (sockfd != -1) {
				goto accepted;
			}
		}
		return;
	}
accepted:;

	int flags = fcntl(fd, F_GETFL);
	int r = fcntl(fd, F_SETFL, flags | O_NONBLOCK);