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