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