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