b30fc0bf9223920f46b478f32ea463d65158dc81
[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     yaz_log(YLOG_LOG, "metaproxy stop"); /* only for graceful stop */
82     _exit(0);
83 }
84
85 static void work_debug(void *data)
86 {
87     work_common(data);
88 }
89
90 static void work_normal(void *data)
91 {
92 #if HAVE_UNISTD_H
93     /* make the current working process group leader */
94     setpgid(0, 0);
95 #endif
96     work_common(data);
97 }
98
99 static int sc_main(
100     yaz_sc_t s,
101     int argc, char **argv)
102 {
103     bool test_config = false;
104     const char *fname = 0;
105     int ret;
106     char *arg;
107     unsigned mode = 0;
108     const char *pidfile = 0;
109     const char *uid = 0;
110
111     while ((ret = options("c{config}:Dh{help}l:p:tu:V{version}w:X",
112                           argv, argc, &arg)) != -2)
113     {
114         switch (ret)
115         {
116         case 'c':
117             fname = arg;
118             break;
119         case 'D':
120             mode = YAZ_DAEMON_FORK|YAZ_DAEMON_KEEPALIVE;
121             break;
122         case 'h':
123             std::cerr << "metaproxy\n"
124                 " -h|--help     help\n"
125                 " -V|--version  version\n"
126                 " -c|--config f config filename\n"
127                 " -D            daemon and keepalive operation\n"
128                 " -l f          log file f\n"
129                 " -p f          pid file f\n"
130                 " -t            test configuration\n"
131                 " -u id         change uid to id\n"
132                 " -w dir        changes working directory to dir\n"
133                 " -X            debug mode (no fork/daemon mode)\n"
134 #ifdef WIN32
135                 " -install      install windows service\n"
136                 " -remove       remove windows service\n"
137 #endif
138
139                       << std::endl;
140             break;
141         case 'l':
142             yaz_log_init_file(arg);
143             break;
144         case 'p':
145             pidfile = arg;
146             break;
147         case 't':
148             test_config = true;
149             break;
150         case 'u':
151             uid = arg;
152             break;
153         case 'V':
154             std::cout << VERSION;
155 #ifdef VERSION_SHA1
156             std::cout << " " VERSION_SHA1;
157 #endif
158             std::cout << "\n";
159             return 0;
160             break;
161         case 'w':
162             if (
163 #ifdef WIN32
164                 _chdir(arg)
165 #else
166                 chdir(arg)
167 #endif
168                 )
169             {
170                 std::cerr << "chdir " << arg << " failed" << std::endl;
171                 return 1;
172             }
173         case 'X':
174             mode = YAZ_DAEMON_DEBUG;
175             break;
176         case -1:
177             std::cerr << "bad option: " << arg << std::endl;
178             return 1;
179         }
180     }
181     if (!fname)
182     {
183         std::cerr << "No configuration given; use -h for help\n";
184         return 1;
185     }
186
187     if (!test_config)
188         yaz_log(YLOG_LOG, "metaproxy start " VERSION
189 #ifdef VERSION_SHA1
190                 " " VERSION_SHA1
191 #endif
192             );
193
194     yaz_log_xml_errors(0, YLOG_LOG);
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  * Local variables:
272  * c-basic-offset: 4
273  * c-file-style: "Stroustrup"
274  * indent-tabs-mode: nil
275  * End:
276  * vim: shiftwidth=4 tabstop=8 expandtab
277  */
278