1 /* This file is part of YAZ proxy
2 Copyright (C) 1998-2008 Index Data
4 YAZ proxy 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 YAZ proxy 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
24 #include <sys/types.h>
29 #if HAVE_SYS_RESOURCE_H
30 #include <sys/resource.h>
37 #include <yaz/options.h>
38 #include <yaz/daemon.h>
40 #include <yazpp/socket-manager.h>
41 #include <yazpp/pdu-assoc.h>
42 #include <yazproxy/proxy.h>
45 #include <libxml/parser.h>
46 #include <libxml/tree.h>
47 #include <libxslt/xsltutils.h>
48 #include <libxslt/transform.h>
52 #include <libexslt/exslt.h>
55 using namespace yazpp_1;
57 void usage(char *prog)
59 fprintf (stderr, "%s: [-a log] [-c config] [-D]\n"
60 " [-i sec] [-l log] [-m num] [-n num] [-p pidfile]"
61 " [-t target] [-T sec] [-u uid]\n"
62 " [-v level] [-X] [-V] @:port\n", prog);
66 static char *pid_fname = 0;
68 static char *log_file = 0;
69 static unsigned int daemon_flags = YAZ_DAEMON_KEEPALIVE;
70 static int no_limit_files = 0;
72 int args(Yaz_Proxy *proxy, int argc, char **argv)
79 while ((ret = options("o:a:Dt:v:c:u:i:m:l:T:p:n:VX",
80 argv, argc, &arg)) != -2)
94 proxy->set_APDU_log(arg);
97 err = proxy->set_config(arg);
100 fprintf(stderr, "Config file support not enabled (not using libxslt & libxml2)\n");
105 fprintf(stderr, "Bad or missing file %s\n", arg);
110 daemon_flags |= YAZ_DAEMON_FORK;
113 proxy->set_client_idletime(atoi(arg));
116 yaz_log_init_file (arg);
117 log_file = xstrdup(arg);
120 proxy->set_max_clients(atoi(arg));
123 no_limit_files = atoi(arg);
126 proxy->option("optimize", arg);
130 pid_fname = xstrdup(arg);
133 proxy->set_default_target(arg);
136 proxy->set_target_idletime(atoi(arg));
143 yaz_log_init_level (yaz_log_mask_str(arg));
155 proxy->set_debug_mode(1);
156 daemon_flags = YAZ_DAEMON_DEBUG;
165 if (proxy->server(addr))
167 yaz_log(YLOG_FATAL|YLOG_ERRNO, "listen %s", addr);
179 static Yaz_Proxy *static_yaz_proxy = 0;
180 static void sighup_handler(int num)
184 signal(SIGHUP, sighup_handler);
186 if (static_yaz_proxy)
187 static_yaz_proxy->reconfig();
191 static void proxy_xml_error_handler(void *ctx, const char *fmt, ...)
199 vsprintf(buf, fmt, ap);
201 vsnprintf(buf, sizeof(buf), fmt, ap);
203 yaz_log(YLOG_WARN, "%s: %s", (char*) ctx, buf);
209 static void child_run(void *data)
211 SocketManager *m = (SocketManager *) data;
214 signal(SIGHUP, sighup_handler);
218 xmlSetGenericErrorFunc((void *) "XML", proxy_xml_error_handler);
219 xsltSetGenericErrorFunc((void *) "XSLT", proxy_xml_error_handler);
227 yaz_log(YLOG_LOG, "0 proxy pid=%ld", (long) getpid());
232 struct rlimit limit_data;
233 limit_data.rlim_cur = no_limit_files;
234 limit_data.rlim_max = no_limit_files;
236 yaz_log(YLOG_LOG, "0 setrlimit NOFILE cur=%ld max=%ld",
237 (long) limit_data.rlim_cur, (long) limit_data.rlim_max);
238 if (setrlimit(RLIMIT_NOFILE, &limit_data))
239 yaz_log(YLOG_ERRNO|YLOG_WARN, "setrlimit");
241 yaz_log(YLOG_WARN, "setrlimit unavablable. Option -n ignored");
245 struct rlimit limit_data;
246 getrlimit(RLIMIT_NOFILE, &limit_data);
247 yaz_log(YLOG_LOG, "0 getrlimit NOFILE cur=%ld max=%ld",
248 (long) limit_data.rlim_cur, (long) limit_data.rlim_max);
251 while (m->processEvent() > 0)
257 int main(int argc, char **argv)
264 SocketManager mySocketManager;
265 Yaz_Proxy proxy(new PDU_Assoc(&mySocketManager), &mySocketManager);
267 static_yaz_proxy = &proxy;
269 args(&proxy, argc, argv);
271 yaz_daemon("yazproxy", daemon_flags,
272 child_run, &mySocketManager, pid_fname, uid);
279 * indent-tabs-mode: nil
281 * vim: shiftwidth=4 tabstop=8 expandtab