1 /* $Id: yaz-proxy-main.cpp,v 1.22 2008-02-21 09:33:23 adam Exp $
2 Copyright (c) 1998-2006, Index Data.
4 This file is part of the yazproxy.
6 YAZ proxy is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
11 YAZ proxy is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with YAZ proxy; see the file LICENSE. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
27 #include <sys/types.h>
32 #if HAVE_SYS_RESOURCE_H
33 #include <sys/resource.h>
40 #include <yaz/options.h>
41 #include <yaz/daemon.h>
43 #include <yazpp/socket-manager.h>
44 #include <yazpp/pdu-assoc.h>
45 #include <yazproxy/proxy.h>
48 #include <libxml/parser.h>
49 #include <libxml/tree.h>
50 #include <libxslt/xsltutils.h>
51 #include <libxslt/transform.h>
55 #include <libexslt/exslt.h>
58 using namespace yazpp_1;
60 void usage(char *prog)
62 fprintf (stderr, "%s: [-a log] [-c config] [-D]\n"
63 " [-i sec] [-l log] [-m num] [-n num] [-p pidfile]"
64 " [-t target] [-T sec] [-u uid]\n"
65 " [-v level] [-X] @:port\n", prog);
69 static char *pid_fname = 0;
71 static char *log_file = 0;
72 static unsigned int daemon_flags = YAZ_DAEMON_KEEPALIVE;
73 static int no_limit_files = 0;
75 int args(Yaz_Proxy *proxy, int argc, char **argv)
82 while ((ret = options("o:a:Dt:v:c:u:i:m:l:T:p:n:X",
83 argv, argc, &arg)) != -2)
97 proxy->set_APDU_log(arg);
100 err = proxy->set_config(arg);
103 fprintf(stderr, "Config file support not enabled (not using libxslt & libxml2)\n");
108 fprintf(stderr, "Bad or missing file %s\n", arg);
113 daemon_flags |= YAZ_DAEMON_FORK;
116 proxy->set_client_idletime(atoi(arg));
119 yaz_log_init_file (arg);
120 log_file = xstrdup(arg);
123 proxy->set_max_clients(atoi(arg));
126 no_limit_files = atoi(arg);
129 proxy->option("optimize", arg);
133 pid_fname = xstrdup(arg);
136 proxy->set_default_target(arg);
139 proxy->set_target_idletime(atoi(arg));
146 yaz_log_init_level (yaz_log_mask_str(arg));
149 proxy->set_debug_mode(1);
150 daemon_flags = YAZ_DAEMON_DEBUG;
159 if (proxy->server(addr))
161 yaz_log(YLOG_FATAL|YLOG_ERRNO, "listen %s", addr);
173 static Yaz_Proxy *static_yaz_proxy = 0;
174 static void sighup_handler(int num)
178 signal(SIGHUP, sighup_handler);
180 if (static_yaz_proxy)
181 static_yaz_proxy->reconfig();
185 static void proxy_xml_error_handler(void *ctx, const char *fmt, ...)
193 vsprintf(buf, fmt, ap);
195 vsnprintf(buf, sizeof(buf), fmt, ap);
197 yaz_log(YLOG_WARN, "%s: %s", (char*) ctx, buf);
203 static void child_run(void *data)
205 SocketManager *m = (SocketManager *) data;
208 signal(SIGHUP, sighup_handler);
212 xmlSetGenericErrorFunc((void *) "XML", proxy_xml_error_handler);
213 xsltSetGenericErrorFunc((void *) "XSLT", proxy_xml_error_handler);
221 yaz_log(YLOG_LOG, "0 proxy pid=%ld", (long) getpid());
226 struct rlimit limit_data;
227 limit_data.rlim_cur = no_limit_files;
228 limit_data.rlim_max = no_limit_files;
230 yaz_log(YLOG_LOG, "0 setrlimit NOFILE cur=%ld max=%ld",
231 (long) limit_data.rlim_cur, (long) limit_data.rlim_max);
232 if (setrlimit(RLIMIT_NOFILE, &limit_data))
233 yaz_log(YLOG_ERRNO|YLOG_WARN, "setrlimit");
235 yaz_log(YLOG_WARN, "setrlimit unavablable. Option -n ignored");
239 struct rlimit limit_data;
240 getrlimit(RLIMIT_NOFILE, &limit_data);
241 yaz_log(YLOG_LOG, "0 getrlimit NOFILE cur=%ld max=%ld",
242 (long) limit_data.rlim_cur, (long) limit_data.rlim_max);
245 while (m->processEvent() > 0)
251 int main(int argc, char **argv)
258 SocketManager mySocketManager;
259 Yaz_Proxy proxy(new PDU_Assoc(&mySocketManager), &mySocketManager);
261 static_yaz_proxy = &proxy;
263 args(&proxy, argc, argv);
265 yaz_daemon("yazproxy", daemon_flags,
266 child_run, &mySocketManager, pid_fname, uid);
273 * indent-tabs-mode: nil
275 * vim: shiftwidth=4 tabstop=8 expandtab