CGI filter destructor kills active child processes
[metaproxy-moved-to-github.git] / src / filter_cgi.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 2005-2010 Index Data
3
4 Metaproxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19 #include "filter_cgi.hpp"
20 #include <metaproxy/package.hpp>
21 #include <metaproxy/util.hpp>
22 #include "gduutil.hpp"
23 #include <yaz/zgdu.h>
24 #include <yaz/log.h>
25
26 #include <unistd.h>
27 #include <sys/wait.h>
28 #include <sstream>
29
30 #include "config.hpp"
31
32 namespace mp = metaproxy_1;
33 namespace yf = mp::filter;
34
35 namespace metaproxy_1 {
36     namespace filter {
37         class CGI::Exec {
38             friend class Rep;
39             friend class CGI;
40             std::string path;
41             std::string program;
42         };
43         class CGI::Rep {
44             friend class CGI;
45             std::list<CGI::Exec> exec_map;
46             std::map<pid_t,pid_t> children;
47             boost::mutex m_mutex;
48         public:
49             ~Rep();
50         };
51     }
52 }
53
54 yf::CGI::CGI() : m_p(new Rep)
55 {
56     
57 }
58
59 yf::CGI::Rep::~Rep()
60 {
61     std::map<pid_t,pid_t>::const_iterator it;
62     boost::mutex::scoped_lock lock(m_mutex);
63     
64     for (it = children.begin(); it != children.end(); it++)
65         kill(it->second, SIGTERM);
66 }
67
68 yf::CGI::~CGI()
69 {
70 }
71
72 void yf::CGI::process(mp::Package &package) const
73 {
74     Z_GDU *zgdu_req = package.request().get();
75     Z_GDU *zgdu_res = 0;
76     
77     if (!zgdu_req)
78         return;
79     
80     if (zgdu_req->which != Z_GDU_HTTP_Request)
81     {
82         package.move();
83         return;
84     }
85     std::string path_info;
86     std::string query_string;
87     const char *path = zgdu_req->u.HTTP_Request->path;
88     yaz_log(YLOG_LOG, "path=%s", path);
89     const char *p_cp = strchr(path, '?');
90     if (p_cp)
91     {
92         path_info.assign(path, p_cp - path);
93         query_string.assign(p_cp+1);
94     }
95     else
96         path_info.assign(path);
97
98     std::list<CGI::Exec>::const_iterator it;
99     metaproxy_1::odr odr;
100     for (it = m_p->exec_map.begin(); it != m_p->exec_map.end(); it++)
101     {
102         if (it->path.compare(path_info) == 0)
103         {
104             int r;
105             pid_t pid;
106             int status;
107             
108             pid = ::fork();
109             switch (pid)
110             {
111             case 0: /* child */
112                 setenv("PATH_INFO", path_info.c_str(), 1);
113                 setenv("QUERY_STRING", query_string.c_str(), 1);
114                 r = execl(it->program.c_str(), it->program.c_str(), (char *) 0);
115                 if (r == -1)
116                     exit(1);
117                 exit(0);
118                 break;
119             case -1: /* error */
120                 zgdu_res = odr.create_HTTP_Response(
121                     package.session(), zgdu_req->u.HTTP_Request, 400);
122                 package.response() = zgdu_res;
123                 break;
124             default: /* parent */
125                 if (pid)
126                 {
127                     boost::mutex::scoped_lock lock(m_p->m_mutex);
128                     m_p->children[pid] = pid;
129                 }
130                 waitpid(pid, &status, 0);
131
132                 if (pid)
133                 {
134                     boost::mutex::scoped_lock lock(m_p->m_mutex);
135                     m_p->children.erase(pid);
136                 }
137                 zgdu_res = odr.create_HTTP_Response(
138                     package.session(), zgdu_req->u.HTTP_Request, 200);
139                 package.response() = zgdu_res;
140                 break;
141             }
142             return;
143         }
144     }
145     package.move();
146 }
147
148 void yf::CGI::configure(const xmlNode *ptr, bool test_only)
149 {
150     for (ptr = ptr->children; ptr; ptr = ptr->next)
151     {
152         if (ptr->type != XML_ELEMENT_NODE)
153             continue;
154         if (!strcmp((const char *) ptr->name, "map"))
155         {
156             CGI::Exec exec;
157
158             const struct _xmlAttr *attr;
159             for (attr = ptr->properties; attr; attr = attr->next)
160             {
161                 if (!strcmp((const char *) attr->name,  "path"))
162                     exec.path = mp::xml::get_text(attr->children);
163                 else if (!strcmp((const char *) attr->name, "exec"))
164                     exec.program = mp::xml::get_text(attr->children);
165                 else
166                     throw mp::filter::FilterException
167                         ("Bad attribute " 
168                          + std::string((const char *) attr->name)
169                          + " in cgi section");
170             }
171             m_p->exec_map.push_back(exec);
172         }
173         else
174         {
175             throw mp::filter::FilterException("Bad element " 
176                                                + std::string((const char *)
177                                                              ptr->name));
178         }
179     }
180 }
181
182 static mp::filter::Base* filter_creator()
183 {
184     return new mp::filter::CGI;
185 }
186
187 extern "C" {
188     struct metaproxy_1_filter_struct metaproxy_1_filter_cgi = {
189         0,
190         "cgi",
191         filter_creator
192     };
193 }
194
195
196 /*
197  * Local variables:
198  * c-basic-offset: 4
199  * c-file-style: "Stroustrup"
200  * indent-tabs-mode: nil
201  * End:
202  * vim: shiftwidth=4 tabstop=8 expandtab
203  */
204