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