metaproxy 1.22.4
metaproxy_prog.cpp
Go to the documentation of this file.
1/* This file is part of Metaproxy.
2 Copyright (C) Index Data
3
4Metaproxy is free software; you can redistribute it and/or modify it under
5the terms of the GNU General Public License as published by the Free
6Software Foundation; either version 2, or (at your option) any later
7version.
8
9Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10WARRANTY; without even the implied warranty of MERCHANTABILITY or
11FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program; if not, write to the Free Software
16Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17*/
18
19#include "config.hpp"
20
21#if HAVE_GETRLIMIT
22#include <sys/resource.h>
23#endif
24
25#include <yaz/log.h>
26#include <yaz/options.h>
27#include <yaz/daemon.h>
28
29#include <yaz/sc.h>
30#include <yaz/backtrace.h>
31#include <iostream>
32#include <stdexcept>
33#include <libxml/xinclude.h>
34
35#include <metaproxy/filter.hpp>
36#include <metaproxy/package.hpp>
37#include <metaproxy/util.hpp>
38#include <metaproxy/router_xml.hpp>
39
40#include <stdlib.h>
41#if HAVE_UNISTD_H
42#include <unistd.h>
43#endif
44#include <signal.h>
45#ifdef WIN32
46#include <direct.h>
47#include <io.h>
48#include <process.h>
49#endif
50
51namespace mp = metaproxy_1;
52
53mp::RouterXML *routerp = 0;
54
55static void set_log_prefix(void)
56{
57#if HAVE_UNISTD_H
58 char str[80];
59 snprintf(str, sizeof str, "%lld", (long long) getpid());
60 yaz_log_init_prefix(str);
61#endif
62}
63
64#if HAVE_UNISTD_H
65static pid_t process_group = 0;
66static int sig_received = 0;
67static pid_t my_pid = 0;
68
69static void sig_x_handler(int signo)
70{
71 if (sig_received)
72 return;
73 if ( getpid() != my_pid ) // can happen in unlikely cases
74 return; // when the sig hits a child just after fork(), before it
75 // clears its sighandler. MP-559.
76 sig_received = signo;
77 if (routerp)
78 routerp->stop(signo);
79 if (signo == SIGTERM)
80 kill(-process_group, SIGTERM); /* kill all children processes as well */
81}
82#endif
83
84static void work_common(void *data)
85{
87#if HAVE_UNISTD_H
88 process_group = getpgid(0); // save process group ID
89 my_pid = getpid();
90
91 signal(SIGTERM, sig_x_handler);
92 signal(SIGUSR1, sig_x_handler);
93#endif
94 routerp = (mp::RouterXML*) data;
95 routerp->start();
96
97 mp::Package pack;
98 pack.router(*routerp).move();
99 yaz_log(YLOG_LOG, "metaproxy stop");
100 delete routerp;
101 routerp = 0;
102 _exit(0);
103}
104
105static void work_debug(void *data)
106{
107 work_common(data);
108}
109
110static void work_normal(void *data)
111{
112#if HAVE_UNISTD_H
113 /* make the current working process group leader */
114 setpgid(0, 0);
115#endif
116 work_common(data);
117}
118
119static int sc_main(
120 yaz_sc_t s,
121 int argc, char **argv)
122{
123 bool test_config = false;
124 const char *fname = 0;
125 int ret;
126 char *arg;
127 unsigned mode = 0;
128 const char *pidfile = 0;
129 const char *uid = 0;
130#if HAVE_GETRLIMIT
131 const char *socket_limit = 0;
132#endif
133
134 yaz_enable_panic_backtrace(argv[0]);
136
137 while ((ret = options("c{config}:Dh{help}l:m:p:s:tu:v:V{version}w:X",
138 argv, argc, &arg)) != -2)
139 {
140 switch (ret)
141 {
142 case 'c':
143 fname = arg;
144 break;
145 case 'D':
146 mode = YAZ_DAEMON_FORK|YAZ_DAEMON_KEEPALIVE;
147 break;
148 case 'h':
149 std::cerr << "metaproxy\n"
150 " -h|--help help\n"
151 " -V|--version version\n"
152 " -v level\n"
153 " -c|--config f config filename\n"
154 " -D daemon and keepalive operation\n"
155 " -l f log file f\n"
156 " -m logformat log time format (strftime)\n"
157 " -p f pid file f\n"
158 " -s n socket limit\n"
159 " -t test configuration\n"
160 " -u id change uid to id\n"
161 " -w dir changes working directory to dir\n"
162 " -X debug mode (no fork/daemon mode)\n"
163#ifdef WIN32
164 " -install install windows service\n"
165 " -remove remove windows service\n"
166#endif
167
168 << std::endl;
169 break;
170 case 'l':
171 yaz_log_init_file(arg);
172 break;
173 case 'm':
174 yaz_log_time_format(arg);
175 break;
176 case 'p':
177 pidfile = arg;
178 break;
179 case 's':
180#if HAVE_GETRLIMIT
181 socket_limit = arg;
182#else
183 yaz_log(YLOG_WARN, "Option -s unsuppoted on this platform");
184#endif
185 break;
186 case 't':
187 test_config = true;
188 break;
189 case 'u':
190 uid = arg;
191 break;
192 case 'v':
193 yaz_log_init_level(yaz_log_mask_str(arg));
194 break;
195 case 'V':
196 std::cout << VERSION;
197#ifdef VERSION_SHA1
198 std::cout << " " VERSION_SHA1;
199#endif
200 std::cout << "\n";
201 return 0;
202 break;
203 case 'w':
204 if (
205#ifdef WIN32
206 _chdir(arg)
207#else
208 chdir(arg)
209#endif
210 )
211 {
212 std::cerr << "chdir " << arg << " failed" << std::endl;
213 return 1;
214 }
215 case 'X':
216 mode = YAZ_DAEMON_DEBUG;
217 break;
218 case -1:
219 std::cerr << "bad option: " << arg << std::endl;
220 return 1;
221 }
222 }
223 if (!fname)
224 {
225 std::cerr << "No configuration given; use -h for help\n";
226 return 1;
227 }
228
229 yaz_log(YLOG_LOG, "metaproxy %s " VERSION
230#ifdef VERSION_SHA1
231 " " VERSION_SHA1
232#endif
233 , test_config ? "test" : "start"
234 );
235
236 char yaz_version_str[20];
237 char yaz_sha1_str[41];
238 yaz_version(yaz_version_str, yaz_sha1_str);
239 yaz_log(YLOG_LOG, "YAZ %s %s", yaz_version_str, yaz_sha1_str);
240
241 xmlInitParser();
242 LIBXML_TEST_VERSION
243
244 yaz_log_xml_errors(0, YLOG_LOG);
245 xmlDocPtr doc = xmlReadFile(fname,
246 NULL,
247 XML_PARSE_XINCLUDE + XML_PARSE_NOBLANKS
248 + XML_PARSE_NSCLEAN + XML_PARSE_NONET );
249
250 if (!doc)
251 {
252 yaz_log(YLOG_FATAL,"XML parsing failed");
253 return 1;
254 }
255 // and perform Xinclude then
256 int r = xmlXIncludeProcess(doc);
257 if (r == -1)
258 {
259 yaz_log(YLOG_FATAL, "XInclude processing failed");
260 return 1;
261 }
262 mp::wrbuf base_path;
263 const char *last_p = strrchr(fname,
264#ifdef WIN32
265 '\\'
266#else
267 '/'
268#endif
269 );
270 if (last_p)
271 wrbuf_write(base_path, fname, last_p - fname);
272 else
273 wrbuf_puts(base_path, ".");
274 ret = 0;
275 try {
276 mp::RouterXML *router =
277 new mp::RouterXML(doc, test_config, wrbuf_cstr(base_path));
278 if (!test_config)
279 {
280#if HAVE_GETRLIMIT
281 if (socket_limit)
282 {
283 struct rlimit limit_data;
284 limit_data.rlim_cur = atoi(socket_limit);
285 limit_data.rlim_max = atoi(socket_limit);
286 int r = setrlimit(RLIMIT_NOFILE, &limit_data);
287 if (r)
288 {
289 yaz_log(YLOG_FATAL,"setrlimit: %s", strerror(errno));
290 return 1;
291 }
292
293 }
294#endif
295 yaz_sc_running(s);
296
297 yaz_daemon("metaproxy", mode | YAZ_DAEMON_LOG_REOPEN,
298 (mode & YAZ_DAEMON_FORK) ? work_normal : work_debug,
299 router, pidfile, uid);
300 }
301 delete router;
302 }
303 catch (std::logic_error &e) {
304 yaz_log(YLOG_FATAL,"std::logic error: %s" , e.what() );
305 ret = 1;
306 }
307 catch (std::runtime_error &e) {
308 yaz_log(YLOG_FATAL, "std::runtime error: %s" , e.what() );
309 ret = 1;
310 }
311 catch ( ... ) {
312 yaz_log(YLOG_FATAL, "Unknown Exception");
313 ret = 1;
314 }
315 xmlFreeDoc(doc);
316 if (test_config)
317 yaz_log(YLOG_LOG, "metaproxy test exit code %d", ret);
318 return ret;
319}
320
321static void sc_stop(yaz_sc_t s)
322{
323 return;
324}
325
326int main(int argc, char **argv)
327{
328 int ret;
329 yaz_sc_t s = yaz_sc_create("metaproxy", "metaproxy");
330
331 ret = yaz_sc_program(s, argc, argv, sc_main, sc_stop);
332
333 yaz_sc_destroy(&s);
334 exit(ret);
335}
336
337/*
338 * Local variables:
339 * c-basic-offset: 4
340 * c-file-style: "Stroustrup"
341 * indent-tabs-mode: nil
342 * End:
343 * vim: shiftwidth=4 tabstop=8 expandtab
344 */
#define VERSION_SHA1
Definition config.hpp:91
#define VERSION
Definition config.hpp:88
static void work_normal(void *data)
int main(int argc, char **argv)
static void work_common(void *data)
static void set_log_prefix(void)
mp::RouterXML * routerp
static int sc_main(yaz_sc_t s, int argc, char **argv)
static void sc_stop(yaz_sc_t s)
static void work_debug(void *data)