Don't call filter destructors on SIGTERM CP-3770
[metaproxy-moved-to-github.git] / src / metaproxy_prog.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 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 "config.hpp"
20
21 #include <yaz/log.h>
22 #include <yaz/options.h>
23 #include <yaz/daemon.h>
24
25 #include <yaz/sc.h>
26 #include <iostream>
27 #include <stdexcept>
28 #include <libxml/xinclude.h>
29
30 #include <metaproxy/filter.hpp>
31 #include <metaproxy/package.hpp>
32 #include <metaproxy/util.hpp>
33 #include <metaproxy/router_xml.hpp>
34
35 #if HAVE_UNISTD_H
36 #include <unistd.h>
37 #endif
38 #include <signal.h>
39 #ifdef WIN32
40 #include <direct.h>
41 #include <io.h>
42 #include <process.h>
43 #endif
44
45 namespace mp = metaproxy_1;
46
47 mp::RouterXML *routerp = 0;
48
49 static void set_log_prefix(void)
50 {
51 #if HAVE_UNISTD_H
52     char str[80];
53
54     sprintf(str, "%lld", (long long) getpid());
55     yaz_log_init_prefix(str);
56 #endif
57 }
58
59 #if HAVE_UNISTD_H
60 static pid_t process_group = 0;
61 static int sig_received = 0;
62
63 static void sig_x_handler(int signo)
64 {
65     if (sig_received)
66         return;
67     sig_received = signo;
68     if (routerp)
69         routerp->stop(signo);
70     if (signo == SIGTERM)
71         kill(-process_group, SIGTERM); /* kill all children processes as well */
72 }
73 #endif
74
75 static void work_common(void *data)
76 {
77     set_log_prefix();
78 #if HAVE_UNISTD_H
79     process_group = getpgid(0); // save process group ID
80
81     signal(SIGTERM, sig_x_handler);
82     signal(SIGUSR1, sig_x_handler);
83 #endif
84     routerp = (mp::RouterXML*) data;
85     routerp->start();
86
87     mp::Package pack;
88     pack.router(*routerp).move();
89     yaz_log(YLOG_LOG, "metaproxy stop");
90
91     if (sig_received != SIGTERM)
92     {
93         delete routerp;
94         routerp = 0;
95     }
96     _exit(0);
97 }
98
99 static void work_debug(void *data)
100 {
101     work_common(data);
102 }
103
104 static void work_normal(void *data)
105 {
106 #if HAVE_UNISTD_H
107     /* make the current working process group leader */
108     setpgid(0, 0);
109 #endif
110     work_common(data);
111 }
112
113 static int sc_main(
114     yaz_sc_t s,
115     int argc, char **argv)
116 {
117     bool test_config = false;
118     const char *fname = 0;
119     int ret;
120     char *arg;
121     unsigned mode = 0;
122     const char *pidfile = 0;
123     const char *uid = 0;
124
125     set_log_prefix();
126
127     while ((ret = options("c{config}:Dh{help}l:m:p:tu:v:V{version}w:X",
128                           argv, argc, &arg)) != -2)
129     {
130         switch (ret)
131         {
132         case 'c':
133             fname = arg;
134             break;
135         case 'D':
136             mode = YAZ_DAEMON_FORK|YAZ_DAEMON_KEEPALIVE;
137             break;
138         case 'h':
139             std::cerr << "metaproxy\n"
140                 " -h|--help     help\n"
141                 " -V|--version  version\n"
142                 " -v level\n"
143                 " -c|--config f config filename\n"
144                 " -D            daemon and keepalive operation\n"
145                 " -l f          log file f\n"
146                 " -m logformat  log time format (strftime)\n"
147                 " -p f          pid file f\n"
148                 " -t            test configuration\n"
149                 " -u id         change uid to id\n"
150                 " -w dir        changes working directory to dir\n"
151                 " -X            debug mode (no fork/daemon mode)\n"
152 #ifdef WIN32
153                 " -install      install windows service\n"
154                 " -remove       remove windows service\n"
155 #endif
156
157                       << std::endl;
158             break;
159         case 'l':
160             yaz_log_init_file(arg);
161             break;
162         case 'm':
163             yaz_log_time_format(arg);
164             break;
165         case 'p':
166             pidfile = arg;
167             break;
168         case 't':
169             test_config = true;
170             break;
171         case 'u':
172             uid = arg;
173             break;
174         case 'v':
175             yaz_log_init_level(yaz_log_mask_str(arg));
176             break;
177         case 'V':
178             std::cout << VERSION;
179 #ifdef VERSION_SHA1
180             std::cout << " " VERSION_SHA1;
181 #endif
182             std::cout << "\n";
183             return 0;
184             break;
185         case 'w':
186             if (
187 #ifdef WIN32
188                 _chdir(arg)
189 #else
190                 chdir(arg)
191 #endif
192                 )
193             {
194                 std::cerr << "chdir " << arg << " failed" << std::endl;
195                 return 1;
196             }
197         case 'X':
198             mode = YAZ_DAEMON_DEBUG;
199             break;
200         case -1:
201             std::cerr << "bad option: " << arg << std::endl;
202             return 1;
203         }
204     }
205     if (!fname)
206     {
207         std::cerr << "No configuration given; use -h for help\n";
208         return 1;
209     }
210
211     yaz_log(YLOG_LOG, "metaproxy %s " VERSION
212 #ifdef VERSION_SHA1
213                 " " VERSION_SHA1
214 #endif
215         , test_config ? "test" : "start"
216             );
217
218     yaz_log_xml_errors(0, YLOG_LOG);
219     xmlDocPtr doc = xmlReadFile(fname,
220                                 NULL,
221                                 XML_PARSE_XINCLUDE + XML_PARSE_NOBLANKS
222                                 + XML_PARSE_NSCLEAN + XML_PARSE_NONET );
223
224     if (!doc)
225     {
226         yaz_log(YLOG_FATAL,"XML parsing failed");
227         return 1;
228     }
229     // and perform Xinclude then
230     int r = xmlXIncludeProcess(doc);
231     if (r == -1)
232     {
233         yaz_log(YLOG_FATAL, "XInclude processing failed");
234         return 1;
235     }
236     mp::wrbuf base_path;
237     const char *last_p = strrchr(fname,
238 #ifdef WIN32
239                                  '\\'
240 #else
241                                  '/'
242 #endif
243         );
244     if (last_p)
245         wrbuf_write(base_path, fname, last_p - fname);
246     else
247         wrbuf_puts(base_path, ".");
248     ret = 0;
249     try {
250         mp::RouterXML *router =
251             new mp::RouterXML(doc, test_config, wrbuf_cstr(base_path));
252         if (!test_config)
253         {
254
255             yaz_sc_running(s);
256
257             yaz_daemon("metaproxy", mode, mode == YAZ_DAEMON_DEBUG ?
258                        work_debug : work_normal, router, pidfile, uid);
259         }
260     }
261     catch (std::logic_error &e) {
262         yaz_log(YLOG_FATAL,"std::logic error: %s" , e.what() );
263         ret = 1;
264     }
265     catch (std::runtime_error &e) {
266         yaz_log(YLOG_FATAL, "std::runtime error: %s" , e.what() );
267         ret = 1;
268     }
269     catch ( ... ) {
270         yaz_log(YLOG_FATAL, "Unknown Exception");
271         ret = 1;
272     }
273     xmlFreeDoc(doc);
274     if (test_config)
275         yaz_log(YLOG_LOG, "metaproxy test exit code %d", ret);
276     return ret;
277 }
278
279 static void sc_stop(yaz_sc_t s)
280 {
281     return;
282 }
283
284 int main(int argc, char **argv)
285 {
286     int ret;
287     yaz_sc_t s = yaz_sc_create("metaproxy", "metaproxy");
288
289     ret = yaz_sc_program(s, argc, argv, sc_main, sc_stop);
290
291     yaz_sc_destroy(&s);
292     exit(ret);
293 }
294
295 /*
296  * Local variables:
297  * c-basic-offset: 4
298  * c-file-style: "Stroustrup"
299  * indent-tabs-mode: nil
300  * End:
301  * vim: shiftwidth=4 tabstop=8 expandtab
302  */
303