aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2023-07-04 16:43:52 +0200
committerEgor Tensin <Egor.Tensin@gmail.com>2023-07-04 16:47:08 +0200
commit4a0b974b85f36011fb1a8c17bf59bc272d96a522 (patch)
treefc05115a4e0f1b37cfd1733acdf1981c3005ae03
parentsqlite: store new runs in SQLite (diff)
downloadcimple-4a0b974b85f36011fb1a8c17bf59bc272d96a522.tar.gz
cimple-4a0b974b85f36011fb1a8c17bf59bc272d96a522.zip
tcp_server: always clean up connection descriptors
-rw-r--r--src/file.c14
-rw-r--r--src/file.h1
-rw-r--r--src/server.c6
-rw-r--r--src/tcp_server.c6
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 <fcntl.h>
#include <ftw.h>
#include <stdio.h>
#include <stdlib.h>
@@ -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;
}