1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2013 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 <metaproxy/router_xml.hpp>
45 namespace mp = metaproxy_1;
47 mp::RouterXML *routerp = 0;
49 static void set_log_prefix(void)
54 sprintf(str, "%lld", (long long) getpid());
55 yaz_log_init_prefix(str);
60 static pid_t process_group = 0;
62 static void sig_usr1_handler(int s)
64 yaz_log(YLOG_LOG, "metaproxy received SIGUSR1");
68 static void sig_term_handler(int s)
70 yaz_log(YLOG_LOG, "metaproxy received SIGTERM");
71 yaz_log(YLOG_LOG, "metaproxy stop");
72 kill(-process_group, SIGTERM); /* kill all children processes as well */
77 static void work_common(void *data)
81 process_group = getpgid(0); // save process group ID
83 signal(SIGTERM, sig_term_handler);
84 signal(SIGUSR1, sig_usr1_handler);
86 routerp = (mp::RouterXML*) data;
90 pack.router(*routerp).move();
91 yaz_log(YLOG_LOG, "metaproxy stop"); /* only for graceful stop */
93 kill(-process_group, SIGTERM); /* kill all children processes as well */
98 static void work_debug(void *data)
103 static void work_normal(void *data)
106 /* make the current working process group leader */
114 int argc, char **argv)
116 bool test_config = false;
117 const char *fname = 0;
121 const char *pidfile = 0;
126 while ((ret = options("c{config}:Dh{help}l:m:p:tu:v:V{version}w:X",
127 argv, argc, &arg)) != -2)
135 mode = YAZ_DAEMON_FORK|YAZ_DAEMON_KEEPALIVE;
138 std::cerr << "metaproxy\n"
140 " -V|--version version\n"
142 " -c|--config f config filename\n"
143 " -D daemon and keepalive operation\n"
145 " -m logformat log time format (strftime)\n"
147 " -t test configuration\n"
148 " -u id change uid to id\n"
149 " -w dir changes working directory to dir\n"
150 " -X debug mode (no fork/daemon mode)\n"
152 " -install install windows service\n"
153 " -remove remove windows service\n"
159 yaz_log_init_file(arg);
162 yaz_log_time_format(arg);
174 yaz_log_init_level(yaz_log_mask_str(arg));
177 std::cout << VERSION;
179 std::cout << " " VERSION_SHA1;
193 std::cerr << "chdir " << arg << " failed" << std::endl;
197 mode = YAZ_DAEMON_DEBUG;
200 std::cerr << "bad option: " << arg << std::endl;
206 std::cerr << "No configuration given; use -h for help\n";
210 yaz_log(YLOG_LOG, "metaproxy %s " VERSION
214 , test_config ? "test" : "start"
217 yaz_log_xml_errors(0, YLOG_LOG);
218 xmlDocPtr doc = xmlReadFile(fname,
220 XML_PARSE_XINCLUDE + XML_PARSE_NOBLANKS
221 + XML_PARSE_NSCLEAN + XML_PARSE_NONET );
225 yaz_log(YLOG_FATAL,"XML parsing failed");
228 // and perform Xinclude then
229 int r = xmlXIncludeProcess(doc);
232 yaz_log(YLOG_FATAL, "XInclude processing failed");
236 const char *last_p = strrchr(fname,
244 wrbuf_write(base_path, fname, last_p - fname);
246 wrbuf_puts(base_path, ".");
249 mp::RouterXML *router =
250 new mp::RouterXML(doc, test_config, wrbuf_cstr(base_path));
256 yaz_daemon("metaproxy", mode, mode == YAZ_DAEMON_DEBUG ?
257 work_debug : work_normal, router, pidfile, uid);
260 catch (std::logic_error &e) {
261 yaz_log(YLOG_FATAL,"std::logic error: %s" , e.what() );
264 catch (std::runtime_error &e) {
265 yaz_log(YLOG_FATAL, "std::runtime error: %s" , e.what() );
269 yaz_log(YLOG_FATAL, "Unknown Exception");
274 yaz_log(YLOG_LOG, "metaproxy test exit code %d", ret);
278 static void sc_stop(yaz_sc_t s)
283 int main(int argc, char **argv)
286 yaz_sc_t s = yaz_sc_create("metaproxy", "metaproxy");
288 ret = yaz_sc_program(s, argc, argv, sc_main, sc_stop);
297 * c-file-style: "Stroustrup"
298 * indent-tabs-mode: nil
300 * vim: shiftwidth=4 tabstop=8 expandtab