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