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