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>
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;
61 static int sig_received = 0;
63 static void sig_usr1_handler(int signo)
70 static void sig_term_handler(int signo)
80 static void work_common(void *data)
84 process_group = getpgid(0); // save process group ID
86 signal(SIGTERM, sig_term_handler);
87 signal(SIGUSR1, sig_usr1_handler);
89 routerp = (mp::RouterXML*) data;
93 pack.router(*routerp).move();
98 yaz_log(YLOG_LOG, "metaproxy received SIGTERM");
101 yaz_log(YLOG_LOG, "metaproxy received SIGUSR1");
106 yaz_log(YLOG_LOG, "metaproxy received signo=%d", sig_received);
109 yaz_log(YLOG_LOG, "metaproxy stop");
113 kill(-process_group, SIGTERM); /* kill all children processes as well */
118 static void work_debug(void *data)
123 static void work_normal(void *data)
126 /* make the current working process group leader */
134 int argc, char **argv)
136 bool test_config = false;
137 const char *fname = 0;
141 const char *pidfile = 0;
146 while ((ret = options("c{config}:Dh{help}l:m:p:tu:v:V{version}w:X",
147 argv, argc, &arg)) != -2)
155 mode = YAZ_DAEMON_FORK|YAZ_DAEMON_KEEPALIVE;
158 std::cerr << "metaproxy\n"
160 " -V|--version version\n"
162 " -c|--config f config filename\n"
163 " -D daemon and keepalive operation\n"
165 " -m logformat log time format (strftime)\n"
167 " -t test configuration\n"
168 " -u id change uid to id\n"
169 " -w dir changes working directory to dir\n"
170 " -X debug mode (no fork/daemon mode)\n"
172 " -install install windows service\n"
173 " -remove remove windows service\n"
179 yaz_log_init_file(arg);
182 yaz_log_time_format(arg);
194 yaz_log_init_level(yaz_log_mask_str(arg));
197 std::cout << VERSION;
199 std::cout << " " VERSION_SHA1;
213 std::cerr << "chdir " << arg << " failed" << std::endl;
217 mode = YAZ_DAEMON_DEBUG;
220 std::cerr << "bad option: " << arg << std::endl;
226 std::cerr << "No configuration given; use -h for help\n";
230 yaz_log(YLOG_LOG, "metaproxy %s " VERSION
234 , test_config ? "test" : "start"
237 yaz_log_xml_errors(0, YLOG_LOG);
238 xmlDocPtr doc = xmlReadFile(fname,
240 XML_PARSE_XINCLUDE + XML_PARSE_NOBLANKS
241 + XML_PARSE_NSCLEAN + XML_PARSE_NONET );
245 yaz_log(YLOG_FATAL,"XML parsing failed");
248 // and perform Xinclude then
249 int r = xmlXIncludeProcess(doc);
252 yaz_log(YLOG_FATAL, "XInclude processing failed");
256 const char *last_p = strrchr(fname,
264 wrbuf_write(base_path, fname, last_p - fname);
266 wrbuf_puts(base_path, ".");
269 mp::RouterXML *router =
270 new mp::RouterXML(doc, test_config, wrbuf_cstr(base_path));
276 yaz_daemon("metaproxy", mode, mode == YAZ_DAEMON_DEBUG ?
277 work_debug : work_normal, router, pidfile, uid);
280 catch (std::logic_error &e) {
281 yaz_log(YLOG_FATAL,"std::logic error: %s" , e.what() );
284 catch (std::runtime_error &e) {
285 yaz_log(YLOG_FATAL, "std::runtime error: %s" , e.what() );
289 yaz_log(YLOG_FATAL, "Unknown Exception");
294 yaz_log(YLOG_LOG, "metaproxy test exit code %d", ret);
298 static void sc_stop(yaz_sc_t s)
303 int main(int argc, char **argv)
306 yaz_sc_t s = yaz_sc_create("metaproxy", "metaproxy");
308 ret = yaz_sc_program(s, argc, argv, sc_main, sc_stop);
317 * c-file-style: "Stroustrup"
318 * indent-tabs-mode: nil
320 * vim: shiftwidth=4 tabstop=8 expandtab