Listening sockets now closed when forking child processes, preventing lock of ports when children aren't killed.

This commit is contained in:
Thulinma 2016-04-20 15:00:09 +02:00
parent 61b66e349e
commit fd123c8c1f
3 changed files with 11 additions and 0 deletions

View file

@ -32,6 +32,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <fstream> #include <fstream>
#include <dirent.h> //for getMyExec #include <dirent.h> //for getMyExec
#include "procs.h"
bool Util::Config::is_active = false; bool Util::Config::is_active = false;
unsigned int Util::Config::printDebugLevel = DEBUG;// 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 &)) { 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()) { while (is_active && server_socket.connected()) {
Socket::Connection S = server_socket.accept(); Socket::Connection S = server_socket.accept();
if (S.connected()) { //check if the new connection is valid 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::sleep(10); //sleep 10ms
} }
} }
Util::Procs::socketList.erase(server_socket.getSocket());
server_socket.close(); server_socket.close();
return 0; return 0;
} }
int Util::Config::forkServer(Socket::Server & server_socket, int (*callback)(Socket::Connection &)) { 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()) { while (is_active && server_socket.connected()) {
Socket::Connection S = server_socket.accept(); Socket::Connection S = server_socket.accept();
if (S.connected()) { //check if the new connection is valid 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::sleep(10); //sleep 10ms
} }
} }
Util::Procs::socketList.erase(server_socket.getSocket());
server_socket.close(); server_socket.close();
return 0; return 0;
} }

View file

@ -22,6 +22,7 @@
#include "timing.h" #include "timing.h"
std::set<pid_t> Util::Procs::plist; std::set<pid_t> Util::Procs::plist;
std::set<int> Util::Procs::socketList;
bool Util::Procs::handler_set = false; 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(); pid = fork();
if (pid == 0) { //child if (pid == 0) { //child
//Close all sockets in the socketList
for (std::set<int>::iterator it = Util::Procs::socketList.begin(); it != Util::Procs::socketList.end(); ++it){
close(*it);
}
if (!fdin) { if (!fdin) {
dup2(devnull, STDIN_FILENO); dup2(devnull, STDIN_FILENO);
} else if (*fdin == -1) { } else if (*fdin == -1) {

View file

@ -32,6 +32,7 @@ namespace Util {
static int Count(); static int Count();
static bool isActive(pid_t name); static bool isActive(pid_t name);
static bool isRunning(pid_t pid); static bool isRunning(pid_t pid);
static std::set<int> socketList; ///< Holds sockets that should be closed before forking
}; };
} }