1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2012 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>
28 #include <libxml/xinclude.h>
30 #include <metaproxy/filter.hpp>
31 #include <metaproxy/package.hpp>
32 #include <metaproxy/util.hpp>
33 #include "router_flexml.hpp"
34 #include "factory_static.hpp"
46 namespace mp = metaproxy_1;
48 mp::RouterFleXML *routerp = 0;
51 static pid_t process_group = 0;
53 static void sig_usr1_handler(int s)
55 yaz_log(YLOG_LOG, "metaproxy received SIGUSR1");
59 static void sig_term_handler(int s)
61 yaz_log(YLOG_LOG, "metaproxy received SIGTERM");
62 yaz_log(YLOG_LOG, "metaproxy stop");
63 kill(-process_group, SIGTERM); /* kill all children processes as well */
68 static void work_common(void *data)
71 process_group = getpgid(0); // save process group ID
73 signal(SIGTERM, sig_term_handler);
74 signal(SIGUSR1, sig_usr1_handler);
76 routerp = (mp::RouterFleXML*) data;
80 pack.router(*routerp).move();
81 /* this only exits if graceful stop is received (sig_usr1_handler) */
82 yaz_log(YLOG_LOG, "metaproxy stop");
83 kill(-process_group, SIGTERM); /* kill all children processes as well */
87 static void work_debug(void *data)
92 static void work_normal(void *data)
95 /* make the current working process group leader */
103 int argc, char **argv)
105 bool test_config = false;
106 const char *fname = 0;
110 const char *pidfile = 0;
113 while ((ret = options("c{config}:Dh{help}l:p:tu:V{version}w:X",
114 argv, argc, &arg)) != -2)
122 mode = YAZ_DAEMON_FORK|YAZ_DAEMON_KEEPALIVE;
125 std::cerr << "metaproxy\n"
127 " -V|--version version\n"
128 " -c|--config f config filename\n"
129 " -D daemon and keepalive operation\n"
132 " -t test configuration\n"
133 " -u id change uid to id\n"
134 " -w dir changes working directory to dir\n"
135 " -X debug mode (no fork/daemon mode)\n"
137 " -install install windows service\n"
138 " -remove remove windows service\n"
144 yaz_log_init_file(arg);
156 std::cout << VERSION;
158 std::cout << " " VERSION_SHA1;
172 std::cerr << "chdir " << arg << " failed" << std::endl;
176 mode = YAZ_DAEMON_DEBUG;
179 std::cerr << "bad option: " << arg << std::endl;
185 std::cerr << "No configuration given; use -h for help\n";
189 yaz_log(YLOG_LOG, "metaproxy start " VERSION
195 xmlDocPtr doc = xmlReadFile(fname,
197 XML_PARSE_XINCLUDE + XML_PARSE_NOBLANKS
198 + XML_PARSE_NSCLEAN + XML_PARSE_NONET );
202 yaz_log (YLOG_FATAL,"XML parsing failed");
205 // and perform Xinclude then
206 int r = xmlXIncludeProcess(doc);
209 yaz_log(YLOG_FATAL, "XInclude processing failed");
213 const char *last_p = strrchr(fname,
221 wrbuf_write(base_path, fname, last_p - fname);
223 wrbuf_puts(base_path, ".");
226 mp::FactoryStatic factory;
227 mp::RouterFleXML *router =
228 new mp::RouterFleXML(doc, factory, test_config, wrbuf_cstr(base_path));
234 yaz_daemon("metaproxy", mode, mode == YAZ_DAEMON_DEBUG ?
235 work_debug : work_normal, router, pidfile, uid);
238 catch (std::logic_error &e) {
239 yaz_log (YLOG_FATAL,"std::logic error: %s" , e.what() );
242 catch (std::runtime_error &e) {
243 yaz_log(YLOG_FATAL, "std::runtime error: %s" , e.what() );
247 yaz_log(YLOG_FATAL, "Unknown Exception");
254 static void sc_stop(yaz_sc_t s)
259 int main(int argc, char **argv)
262 yaz_sc_t s = yaz_sc_create("metaproxy", "metaproxy");
264 ret = yaz_sc_program(s, argc, argv, sc_main, sc_stop);
275 * c-file-style: "Stroustrup"
276 * indent-tabs-mode: nil
278 * vim: shiftwidth=4 tabstop=8 expandtab