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