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