Log XML errors to yaz_log; not to stdout/stderr
[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     yaz_daemon_stop();
58 }
59
60 static void sig_term_handler(int s)
61 {
62     yaz_log(YLOG_LOG, "metaproxy received SIGTERM");
63     yaz_log(YLOG_LOG, "metaproxy stop");
64     kill(-process_group, SIGTERM); /* kill all children processes as well */
65     _exit(0);
66 }
67 #endif
68
69 static void work_common(void *data)
70 {
71 #if HAVE_UNISTD_H
72     process_group = getpgid(0); // save process group ID
73
74     signal(SIGTERM, sig_term_handler);
75     signal(SIGUSR1, sig_usr1_handler);
76 #endif
77     routerp = (mp::RouterFleXML*) data;
78     routerp->start();
79
80     mp::Package pack;
81     pack.router(*routerp).move();
82     yaz_log(YLOG_LOG, "metaproxy stop"); /* only for graceful stop */
83     _exit(0);
84 }
85
86 static void work_debug(void *data)
87 {
88     work_common(data);
89 }
90     
91 static void work_normal(void *data)
92 {
93 #if HAVE_UNISTD_H
94     /* make the current working process group leader */
95     setpgid(0, 0);
96 #endif
97     work_common(data);
98 }
99
100 static int sc_main(
101     yaz_sc_t s,
102     int argc, char **argv)
103 {
104     bool test_config = false;
105     const char *fname = 0;
106     int ret;
107     char *arg;
108     unsigned mode = 0;
109     const char *pidfile = 0;
110     const char *uid = 0;
111     
112     while ((ret = options("c{config}:Dh{help}l:p:tu:V{version}w:X", 
113                           argv, argc, &arg)) != -2)
114     {
115         switch (ret)
116         {
117         case 'c':
118             fname = arg;
119             break;
120         case 'D':
121             mode = YAZ_DAEMON_FORK|YAZ_DAEMON_KEEPALIVE;
122             break;
123         case 'h':
124             std::cerr << "metaproxy\n"
125                 " -h|--help     help\n"
126                 " -V|--version  version\n"
127                 " -c|--config f config filename\n"
128                 " -D            daemon and keepalive operation\n"
129                 " -l f          log file f\n"
130                 " -p f          pid file f\n"
131                 " -t            test configuration\n"
132                 " -u id         change uid to id\n"
133                 " -w dir        changes working directory to dir\n"
134                 " -X            debug mode (no fork/daemon mode)\n"
135 #ifdef WIN32
136                 " -install      install windows service\n"
137                 " -remove       remove windows service\n"
138 #endif
139                 
140                       << std::endl;
141             break;
142         case 'l':
143             yaz_log_init_file(arg);
144             break;
145         case 'p':
146             pidfile = arg;
147             break;
148         case 't':
149             test_config = true;
150             break;
151         case 'u':
152             uid = arg;
153             break;
154         case 'V':
155             std::cout << VERSION;
156 #ifdef VERSION_SHA1
157             std::cout << " " VERSION_SHA1;
158 #endif
159             std::cout << "\n";
160             return 0;
161             break;
162         case 'w':
163             if (
164 #ifdef WIN32
165                 _chdir(arg)
166 #else
167                 chdir(arg)
168 #endif
169                 ) 
170             {
171                 std::cerr << "chdir " << arg << " failed" << std::endl;
172                 return 1;
173             }
174         case 'X':
175             mode = YAZ_DAEMON_DEBUG;
176             break;
177         case -1:
178             std::cerr << "bad option: " << arg << std::endl;
179             return 1;
180         }
181     }
182     if (!fname)
183     {
184         std::cerr << "No configuration given; use -h for help\n";
185         return 1;
186     }
187     
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
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