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