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