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