From 10684c98b1ceadb16be96dd5e2ad9a5b22282155 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Wed, 17 Mar 2010 15:06:54 +0100 Subject: [PATCH] CGI filter destructor kills active child processes --- src/filter_cgi.cpp | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/src/filter_cgi.cpp b/src/filter_cgi.cpp index 3412ca8..a89d03e 100644 --- a/src/filter_cgi.cpp +++ b/src/filter_cgi.cpp @@ -43,16 +43,30 @@ namespace metaproxy_1 { class CGI::Rep { friend class CGI; std::list exec_map; + std::map children; + boost::mutex m_mutex; + public: + ~Rep(); }; } } yf::CGI::CGI() : m_p(new Rep) { + +} + +yf::CGI::Rep::~Rep() +{ + std::map::const_iterator it; + boost::mutex::scoped_lock lock(m_mutex); + + for (it = children.begin(); it != children.end(); it++) + kill(it->second, SIGTERM); } yf::CGI::~CGI() -{ // must have a destructor because of boost::scoped_ptr +{ } void yf::CGI::process(mp::Package &package) const @@ -90,14 +104,11 @@ void yf::CGI::process(mp::Package &package) const int r; pid_t pid; int status; - int fd; pid = ::fork(); switch (pid) { case 0: /* child */ - for (fd = 3; fd <= 1023; fd++) - close(fd); setenv("PATH_INFO", path_info.c_str(), 1); setenv("QUERY_STRING", query_string.c_str(), 1); r = execl(it->program.c_str(), it->program.c_str(), (char *) 0); @@ -111,8 +122,18 @@ void yf::CGI::process(mp::Package &package) const package.response() = zgdu_res; break; default: /* parent */ + if (pid) + { + boost::mutex::scoped_lock lock(m_p->m_mutex); + m_p->children[pid] = pid; + } waitpid(pid, &status, 0); + if (pid) + { + boost::mutex::scoped_lock lock(m_p->m_mutex); + m_p->children.erase(pid); + } zgdu_res = odr.create_HTTP_Response( package.session(), zgdu_req->u.HTTP_Request, 200); package.response() = zgdu_res; -- 1.7.10.4