From 7899ca1d5f34583449e964732c877aa958e1ab51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Canna=C3=B2?= Date: Tue, 13 Jun 2023 14:23:39 +0000 Subject: [PATCH] Close all FDs in Scheduler When scheduler calls ProxySQL_External_Scheduler::run_once() , after fork() we call close_all_non_term_fd() to close all file descriptors. --- lib/ProxySQL_Admin.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lib/ProxySQL_Admin.cpp b/lib/ProxySQL_Admin.cpp index 430b50da5..b5a0aca9b 100644 --- a/lib/ProxySQL_Admin.cpp +++ b/lib/ProxySQL_Admin.cpp @@ -188,6 +188,24 @@ static char * load_file (const char *filename) { } */ + +void close_all_non_term_fd() { + DIR *d; + struct dirent *dir; + d = opendir("/proc/self/fd"); + if (d) { + while ((dir = readdir(d)) != NULL) { + if (strlen(dir->d_name) && dir->d_name[0] != '.') { + int fd = std::stol(std::string(dir->d_name)); + if (fd > 2) { + close(fd); + } + } + } + closedir(d); + } +} + static int round_intv_to_time_interval(int& intv) { if (intv > 300) { intv = 600; @@ -13602,6 +13620,7 @@ unsigned long long ProxySQL_External_Scheduler::run_once() { exit(EXIT_FAILURE); } if (cpid == 0) { + close_all_non_term_fd(); char *newenviron[] = { NULL }; int rc; rc=execve(sr->filename, newargs, newenviron);