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