1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2010 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 "router_flexml.hpp"
33 #include "factory_static.hpp"
45 namespace mp = metaproxy_1;
47 mp::RouterFleXML *routerp = 0;
50 static pid_t process_group = 0;
52 static void sig_term_handler(int s)
54 kill(-process_group, SIGTERM); /* kill all children processes as well */
59 static void handler(void *data)
61 routerp = (mp::RouterFleXML*) data;
64 /* make the current working process group leader */
66 process_group = getpgid(0); // save process group ID
68 signal(SIGTERM, sig_term_handler);
72 pack.router(*routerp).move(); /* should never exit */
77 int argc, char **argv)
81 const char *fname = 0;
85 const char *pidfile = 0;
88 while ((ret = options("c{config}:Dh{help}l:p:u:V{version}w:X",
89 argv, argc, &arg)) != -2)
97 mode = YAZ_DAEMON_FORK|YAZ_DAEMON_KEEPALIVE;
100 std::cerr << "metaproxy\n"
102 " -V|--version version\n"
103 " -c|--config f config filename\n"
104 " -D daemon and keepalive operation\n"
107 " -u id change uid to id\n"
108 " -w dir changes working directory to dir\n"
109 " -X debug mode (no fork/daemon mode)\n"
111 " -install install windows service\n"
112 " -remove remove windows service\n"
118 yaz_log_init_file(arg);
127 std::cout << VERSION;
129 std::cout << " " VERSION_SHA1;
143 std::cerr << "chdir " << arg << " failed" << std::endl;
147 mode = YAZ_DAEMON_DEBUG;
150 std::cerr << "bad option: " << arg << std::endl;
156 std::cerr << "No configuration given; use -h for help\n";
160 yaz_log(YLOG_LOG, "Metaproxy start " VERSION
166 xmlDocPtr doc = xmlReadFile(fname,
168 XML_PARSE_XINCLUDE + XML_PARSE_NOBLANKS
169 + XML_PARSE_NSCLEAN + XML_PARSE_NONET );
173 yaz_log (YLOG_FATAL,"XML parsing failed");
176 // and perform Xinclude then
177 int r = xmlXIncludeProcess(doc);
180 yaz_log(YLOG_FATAL, "XInclude processing failed");
183 WRBUF base_path = wrbuf_alloc();
184 const char *last_p = strrchr(fname,
192 wrbuf_write(base_path, fname, last_p - fname);
194 mp::FactoryStatic factory;
195 mp::RouterFleXML *router =
196 new mp::RouterFleXML(doc, factory, false, wrbuf_cstr(base_path));
197 wrbuf_destroy(base_path);
201 yaz_daemon("metaproxy", mode, handler, router, pidfile, uid);
203 catch (std::logic_error &e) {
204 yaz_log (YLOG_FATAL,"std::logic error: %s" , e.what() );
207 catch (std::runtime_error &e) {
208 yaz_log(YLOG_FATAL, "std::runtime error: %s" , e.what() );
212 yaz_log(YLOG_FATAL, "Unknown Exception");
218 static void sc_stop(yaz_sc_t s)
223 int main(int argc, char **argv)
226 yaz_sc_t s = yaz_sc_create("metaproxy", "metaproxy");
228 ret = yaz_sc_program(s, argc, argv, sc_main, sc_stop);
239 * c-file-style: "Stroustrup"
240 * indent-tabs-mode: nil
242 * vim: shiftwidth=4 tabstop=8 expandtab