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