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