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