From 4a0b974b85f36011fb1a8c17bf59bc272d96a522 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Tue, 4 Jul 2023 16:43:52 +0200 Subject: tcp_server: always clean up connection descriptors --- src/file.c | 14 ++++++++++++++ src/file.h | 1 + src/server.c | 6 ++++++ src/tcp_server.c | 6 +++--- 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/file.c b/src/file.c index eb7135a..27709be 100644 --- a/src/file.c +++ b/src/file.c @@ -9,6 +9,7 @@ #include "compiler.h" #include "log.h" +#include #include #include #include @@ -98,6 +99,19 @@ free: return NULL; } +int file_dup(int fd) +{ + int ret = 0; + + ret = fcntl(fd, F_DUPFD_CLOEXEC, 0); + if (ret < 0) { + log_errno("fcntl"); + return ret; + } + + return ret; +} + void file_close(int fd) { log_errno_if(close(fd), "close"); diff --git a/src/file.h b/src/file.h index c25ef40..b4bba62 100644 --- a/src/file.h +++ b/src/file.h @@ -15,6 +15,7 @@ int rm_rf(const char *dir); int my_chdir(const char *dir, char **old); char *my_readlink(const char *path); +int file_dup(int fd); void file_close(int fd); int file_exists(const char *path); diff --git a/src/server.c b/src/server.c index 678f1c9..ee605c0 100644 --- a/src/server.c +++ b/src/server.c @@ -10,6 +10,7 @@ #include "compiler.h" #include "const.h" #include "event_loop.h" +#include "file.h" #include "log.h" #include "msg.h" #include "run_queue.h" @@ -221,6 +222,11 @@ static int server_handle_cmd_new_worker(UNUSED const struct msg *request, struct worker *worker = NULL; int ret = 0; + ret = file_dup(client_fd); + if (ret < 0) + return ret; + client_fd = ret; + ret = worker_create(&worker, client_fd); if (ret < 0) return ret; diff --git a/src/tcp_server.c b/src/tcp_server.c index fe55e4f..221b27e 100644 --- a/src/tcp_server.c +++ b/src/tcp_server.c @@ -72,13 +72,13 @@ static void *connection_thread(void *_ctx) * handled in the main thread. */ ret = signal_block_sigterms(); if (ret < 0) - goto free_ctx; + goto close; ctx->conn_handler(ctx->fd, ctx->arg); -free_ctx: +close: + net_close(ctx->fd); free(ctx); - return NULL; } -- cgit v1.2.3