diff --git a/lib/config.cpp b/lib/config.cpp index 0da7c689..d6aae576 100644 --- a/lib/config.cpp +++ b/lib/config.cpp @@ -32,6 +32,7 @@ #include #include #include //for getMyExec +#include "procs.h" bool Util::Config::is_active = false; unsigned int Util::Config::printDebugLevel = DEBUG;// @@ -355,6 +356,7 @@ static void callThreadCallback(void * cDataArg) { } int Util::Config::threadServer(Socket::Server & server_socket, int (*callback)(Socket::Connection &)) { + Util::Procs::socketList.insert(server_socket.getSocket()); while (is_active && server_socket.connected()) { Socket::Connection S = server_socket.accept(); if (S.connected()) { //check if the new connection is valid @@ -370,11 +372,13 @@ int Util::Config::threadServer(Socket::Server & server_socket, int (*callback)(S Util::sleep(10); //sleep 10ms } } + Util::Procs::socketList.erase(server_socket.getSocket()); server_socket.close(); return 0; } int Util::Config::forkServer(Socket::Server & server_socket, int (*callback)(Socket::Connection &)) { + Util::Procs::socketList.insert(server_socket.getSocket()); while (is_active && server_socket.connected()) { Socket::Connection S = server_socket.accept(); if (S.connected()) { //check if the new connection is valid @@ -390,6 +394,7 @@ int Util::Config::forkServer(Socket::Server & server_socket, int (*callback)(Soc Util::sleep(10); //sleep 10ms } } + Util::Procs::socketList.erase(server_socket.getSocket()); server_socket.close(); return 0; } diff --git a/lib/procs.cpp b/lib/procs.cpp index afca81d2..263ae8ae 100644 --- a/lib/procs.cpp +++ b/lib/procs.cpp @@ -22,6 +22,7 @@ #include "timing.h" std::set Util::Procs::plist; +std::set Util::Procs::socketList; bool Util::Procs::handler_set = false; @@ -283,6 +284,10 @@ pid_t Util::Procs::StartPiped(char * const * argv, int * fdin, int * fdout, int } pid = fork(); if (pid == 0) { //child + //Close all sockets in the socketList + for (std::set::iterator it = Util::Procs::socketList.begin(); it != Util::Procs::socketList.end(); ++it){ + close(*it); + } if (!fdin) { dup2(devnull, STDIN_FILENO); } else if (*fdin == -1) { diff --git a/lib/procs.h b/lib/procs.h index d7f7637c..033ae55c 100644 --- a/lib/procs.h +++ b/lib/procs.h @@ -32,6 +32,7 @@ namespace Util { static int Count(); static bool isActive(pid_t name); static bool isRunning(pid_t pid); + static std::set socketList; ///< Holds sockets that should be closed before forking }; }