Capture SIGTERM in main prog and destroy filters
[metaproxy-moved-to-github.git] / src / metaproxy_prog.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 "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 "router_flexml.hpp"
33 #include "factory_static.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::RouterFleXML *routerp = 0;
48
49 static void sig_term_handler(int s)
50 {
51     if (routerp)
52     {
53         delete routerp;
54     }
55     exit(0);
56 }
57
58 static void handler(void *data)
59 {
60     routerp = (mp::RouterFleXML*) data;
61     
62     signal(SIGTERM, sig_term_handler);
63     mp::Package pack;
64     pack.router(*routerp).move();
65 }
66     
67 static int sc_main(
68     yaz_sc_t s,
69     int argc, char **argv)
70 {
71     try 
72     {
73         const char *fname = 0;
74         int ret;
75         char *arg;
76         unsigned mode = 0;
77         const char *pidfile = 0;
78         const char *uid = 0;
79
80         while ((ret = options("c{config}:Dh{help}l:p:u:V{version}w:X", 
81                               argv, argc, &arg)) != -2)
82         {
83             switch (ret)
84             {
85             case 'c':
86                 fname = arg;
87                 break;
88             case 'D':
89                 mode = YAZ_DAEMON_FORK|YAZ_DAEMON_KEEPALIVE;
90                 break;
91             case 'h':
92                 std::cerr << "metaproxy\n"
93                     " -h|--help     help\n"
94                     " -V|--version  version\n"
95                     " -c|--config f config filename\n"
96                     " -D            daemon and keepalive operation\n"
97                     " -l f          log file f\n"
98                     " -p f          pid file f\n"
99                     " -u id         change uid to id\n"
100                     " -w dir        changes working directory to dir\n"
101                     " -X            debug mode (no fork/daemon mode)\n"
102 #ifdef WIN32
103                     " -install      install windows service\n"
104                     " -remove       remove windows service\n"
105 #endif
106
107                           << std::endl;
108                 break;
109             case 'l':
110                 yaz_log_init_file(arg);
111                 break;
112             case 'p':
113                 pidfile = arg;
114                 break;
115             case 'u':
116                 uid = arg;
117                 break;
118             case 'V':
119                 std::cout << VERSION "\n";
120                 return 0;
121                 break;
122             case 'w':
123                 if (
124 #ifdef WIN32
125                     _chdir(arg)
126 #else
127                     chdir(arg)
128 #endif
129                    ) 
130                 {
131                     std::cerr << "chdir " << arg << " failed" << std::endl;
132                     return 1;
133                 }
134             case 'X':
135                 mode = YAZ_DAEMON_DEBUG;
136                 break;
137             case -1:
138                 std::cerr << "bad option: " << arg << std::endl;
139                 return 1;
140             }
141         }
142         if (!fname)
143         {
144             std::cerr << "No configuration given; use -h for help\n";
145             return 1;
146         }
147
148         yaz_log(YLOG_LOG, "Metaproxy " VERSION " started");
149         xmlDocPtr doc = xmlReadFile(fname,
150                                     NULL, 
151                                     XML_PARSE_XINCLUDE + XML_PARSE_NOBLANKS
152                                     + XML_PARSE_NSCLEAN + XML_PARSE_NONET );
153         
154         if (!doc)
155         {
156             yaz_log (YLOG_FATAL,"XML parsing failed");
157             return 1;
158         }
159         // and perform Xinclude then
160         int r = xmlXIncludeProcess(doc);
161         if (r == -1)
162         {
163             yaz_log(YLOG_FATAL, "XInclude processing failed");
164             return 1;
165         }
166         WRBUF base_path = wrbuf_alloc();
167         const char *last_p = strrchr(fname,
168 #ifdef WIN32
169                                      '\\'
170 #else
171                                      '/'
172 #endif
173             );
174         if (last_p)
175             wrbuf_write(base_path, fname, last_p - fname);
176         
177         mp::FactoryStatic factory;
178         mp::RouterFleXML *router =
179             new mp::RouterFleXML(doc, factory, false, wrbuf_cstr(base_path));
180         wrbuf_destroy(base_path);
181
182         yaz_sc_running(s);
183
184         yaz_daemon("metaproxy", mode, handler, router, pidfile, uid);
185     }
186     catch (std::logic_error &e) {
187         yaz_log (YLOG_FATAL,"std::logic error: %s" , e.what() );
188         return 1;
189     }
190     catch (std::runtime_error &e) {
191         yaz_log(YLOG_FATAL, "std::runtime error: %s" , e.what() );
192         return 1;
193     }
194     catch ( ... ) {
195         yaz_log(YLOG_FATAL, "Unknown Exception");
196         return 1;
197     }
198     return 0;
199 }
200
201 static void sc_stop(yaz_sc_t s)
202 {
203     return;
204 }
205
206 int main(int argc, char **argv)
207 {
208     int ret;
209     yaz_sc_t s = yaz_sc_create("metaproxy", "metaproxy");
210
211     ret = yaz_sc_program(s, argc, argv, sc_main, sc_stop);
212
213     yaz_sc_destroy(&s);
214     exit(ret);
215 }
216
217
218
219 /*
220  * Local variables:
221  * c-basic-offset: 4
222  * c-file-style: "Stroustrup"
223  * indent-tabs-mode: nil
224  * End:
225  * vim: shiftwidth=4 tabstop=8 expandtab
226  */
227