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