1 /* This file is part of Metaproxy.
2 Copyright (C) Index Data
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
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
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
22 #include <yaz/options.h>
23 #include <yaz/daemon.h>
26 #include <yaz/backtrace.h>
29 #include <libxml/xinclude.h>
31 #include <metaproxy/filter.hpp>
32 #include <metaproxy/package.hpp>
33 #include <metaproxy/util.hpp>
34 #include <metaproxy/router_xml.hpp>
46 namespace mp = metaproxy_1;
48 mp::RouterXML *routerp = 0;
50 static void set_log_prefix(void)
55 sprintf(str, "%lld", (long long) getpid());
56 yaz_log_init_prefix(str);
61 static pid_t process_group = 0;
62 static int sig_received = 0;
63 static pid_t my_pid = 0;
65 static void sig_x_handler(int signo)
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.
76 kill(-process_group, SIGTERM); /* kill all children processes as well */
80 static void work_common(void *data)
84 process_group = getpgid(0); // save process group ID
87 signal(SIGTERM, sig_x_handler);
88 signal(SIGUSR1, sig_x_handler);
90 routerp = (mp::RouterXML*) data;
94 pack.router(*routerp).move();
95 yaz_log(YLOG_LOG, "metaproxy stop");
101 static void work_debug(void *data)
106 static void work_normal(void *data)
109 /* make the current working process group leader */
117 int argc, char **argv)
119 bool test_config = false;
120 const char *fname = 0;
124 const char *pidfile = 0;
127 yaz_enable_panic_backtrace(argv[0]);
130 while ((ret = options("c{config}:Dh{help}l:m:p:tu:v:V{version}w:X",
131 argv, argc, &arg)) != -2)
139 mode = YAZ_DAEMON_FORK|YAZ_DAEMON_KEEPALIVE;
142 std::cerr << "metaproxy\n"
144 " -V|--version version\n"
146 " -c|--config f config filename\n"
147 " -D daemon and keepalive operation\n"
149 " -m logformat log time format (strftime)\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"
156 " -install install windows service\n"
157 " -remove remove windows service\n"
163 yaz_log_init_file(arg);
166 yaz_log_time_format(arg);
178 yaz_log_init_level(yaz_log_mask_str(arg));
181 std::cout << VERSION;
183 std::cout << " " VERSION_SHA1;
197 std::cerr << "chdir " << arg << " failed" << std::endl;
201 mode = YAZ_DAEMON_DEBUG;
204 std::cerr << "bad option: " << arg << std::endl;
210 std::cerr << "No configuration given; use -h for help\n";
214 yaz_log(YLOG_LOG, "metaproxy %s " VERSION
218 , test_config ? "test" : "start"
224 yaz_log_xml_errors(0, YLOG_LOG);
225 xmlDocPtr doc = xmlReadFile(fname,
227 XML_PARSE_XINCLUDE + XML_PARSE_NOBLANKS
228 + XML_PARSE_NSCLEAN + XML_PARSE_NONET );
232 yaz_log(YLOG_FATAL,"XML parsing failed");
235 // and perform Xinclude then
236 int r = xmlXIncludeProcess(doc);
239 yaz_log(YLOG_FATAL, "XInclude processing failed");
243 const char *last_p = strrchr(fname,
251 wrbuf_write(base_path, fname, last_p - fname);
253 wrbuf_puts(base_path, ".");
256 mp::RouterXML *router =
257 new mp::RouterXML(doc, test_config, wrbuf_cstr(base_path));
263 yaz_daemon("metaproxy", mode | YAZ_DAEMON_LOG_REOPEN,
264 mode == YAZ_DAEMON_DEBUG ? work_debug : work_normal,
265 router, pidfile, uid);
269 catch (std::logic_error &e) {
270 yaz_log(YLOG_FATAL,"std::logic error: %s" , e.what() );
273 catch (std::runtime_error &e) {
274 yaz_log(YLOG_FATAL, "std::runtime error: %s" , e.what() );
278 yaz_log(YLOG_FATAL, "Unknown Exception");
283 yaz_log(YLOG_LOG, "metaproxy test exit code %d", ret);
287 static void sc_stop(yaz_sc_t s)
292 int main(int argc, char **argv)
295 yaz_sc_t s = yaz_sc_create("metaproxy", "metaproxy");
297 ret = yaz_sc_program(s, argc, argv, sc_main, sc_stop);
306 * c-file-style: "Stroustrup"
307 * indent-tabs-mode: nil
309 * vim: shiftwidth=4 tabstop=8 expandtab