1 /* $Id: yaz-proxy-main.cpp,v 1.21 2007-10-08 11:47:21 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>
35 #if HAVE_SYS_RESOURCE_H
36 #include <sys/resource.h>
46 #include <yaz/options.h>
48 #include <yazpp/socket-manager.h>
49 #include <yazpp/pdu-assoc.h>
50 #include <yazproxy/proxy.h>
53 #include <libxml/parser.h>
54 #include <libxml/tree.h>
55 #include <libxslt/xsltutils.h>
56 #include <libxslt/transform.h>
60 #include <libexslt/exslt.h>
63 using namespace yazpp_1;
65 void usage(char *prog)
67 fprintf (stderr, "%s: [-a log] [-c config]\n"
68 " [-i sec] [-l log] [-m num] [-n num] [-p pidfile]"
69 " [-t target] [-T sec] [-u uid]\n"
70 " [-v level] [-X] @:port\n", prog);
74 static char *pid_fname = 0;
76 static char *log_file = 0;
78 static int no_limit_files = 0;
80 int args(Yaz_Proxy *proxy, int argc, char **argv)
87 while ((ret = options("o:a:t:v:c:u:i:m:l:T:p:n:X",
88 argv, argc, &arg)) != -2)
102 proxy->set_APDU_log(arg);
105 err = proxy->set_config(arg);
108 fprintf(stderr, "Config file support not enabled (not using libxslt & libxml2)\n");
113 fprintf(stderr, "Bad or missing file %s\n", arg);
118 proxy->set_client_idletime(atoi(arg));
121 yaz_log_init_file (arg);
122 log_file = xstrdup(arg);
125 proxy->set_max_clients(atoi(arg));
128 no_limit_files = atoi(arg);
131 proxy->option("optimize", arg);
135 pid_fname = xstrdup(arg);
138 proxy->set_default_target(arg);
141 proxy->set_target_idletime(atoi(arg));
148 yaz_log_init_level (yaz_log_mask_str(arg));
151 proxy->set_debug_mode(1);
161 if (proxy->server(addr))
163 yaz_log(YLOG_FATAL|YLOG_ERRNO, "listen %s", addr);
175 static Yaz_Proxy *static_yaz_proxy = 0;
176 static void sighup_handler(int num)
180 signal(SIGHUP, sighup_handler);
182 if (static_yaz_proxy)
183 static_yaz_proxy->reconfig();
187 static void proxy_xml_error_handler(void *ctx, const char *fmt, ...)
195 vsprintf(buf, fmt, ap);
197 vsnprintf(buf, sizeof(buf), fmt, ap);
199 yaz_log(YLOG_WARN, "%s: %s", (char*) ctx, buf);
205 static void child_run(SocketManager *m, int run)
209 signal(SIGHUP, sighup_handler);
213 xmlSetGenericErrorFunc((void *) "XML", proxy_xml_error_handler);
214 xsltSetGenericErrorFunc((void *) "XSLT", proxy_xml_error_handler);
222 yaz_log(YLOG_LOG, "0 proxy run=%d pid=%ld", run, (long) getpid());
227 struct rlimit limit_data;
228 limit_data.rlim_cur = no_limit_files;
229 limit_data.rlim_max = no_limit_files;
231 yaz_log(YLOG_LOG, "0 setrlimit NOFILE cur=%ld max=%ld",
232 (long) limit_data.rlim_cur, (long) limit_data.rlim_max);
233 if (setrlimit(RLIMIT_NOFILE, &limit_data))
234 yaz_log(YLOG_ERRNO|YLOG_WARN, "setrlimit");
236 yaz_log(YLOG_WARN, "setrlimit unavablable. Option -n ignored");
243 FILE *f = fopen(pid_fname, "w");
246 yaz_log(YLOG_ERRNO|YLOG_FATAL, "Couldn't create %s", pid_fname);
249 fprintf(f, "%ld", (long) getpid());
257 if (!(pw = getpwnam(uid)))
259 yaz_log(YLOG_FATAL, "%s: Unknown user", uid);
264 chown(log_file, pw->pw_uid, pw->pw_gid);
267 if (setuid(pw->pw_uid) < 0)
269 yaz_log(YLOG_FATAL|YLOG_ERRNO, "setuid");
276 struct rlimit limit_data;
277 getrlimit(RLIMIT_NOFILE, &limit_data);
278 yaz_log(YLOG_LOG, "0 getrlimit NOFILE cur=%ld max=%ld",
279 (long) limit_data.rlim_cur, (long) limit_data.rlim_max);
282 while (m->processEvent() > 0)
288 int main(int argc, char **argv)
297 SocketManager mySocketManager;
298 Yaz_Proxy proxy(new PDU_Assoc(&mySocketManager), &mySocketManager);
300 static_yaz_proxy = &proxy;
302 args(&proxy, argc, argv);
305 child_run(&mySocketManager, run);
309 child_run(&mySocketManager, run);
317 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fork");
322 child_run(&mySocketManager, run);
332 yaz_log(YLOG_FATAL, "p1=%d != p=%d", p1, p);
335 if (WIFSIGNALED(status))
337 switch(WTERMSIG(status)) {
339 yaz_log(YLOG_WARN, "Received SIGILL from child %ld", (long) p);
343 yaz_log(YLOG_WARN, "Received SIGABRT from child %ld", (long) p);
347 yaz_log(YLOG_WARN, "Received SIGSEGV from child %ld", (long) p);
351 yaz_log(YLOG_WARN, "Received SIGBUS from child %ld", (long) p);
355 yaz_log(YLOG_LOG, "Received SIGTERM from child %ld",
360 yaz_log(YLOG_WARN, "Received SIG %d from child %ld",
361 WTERMSIG(status), (long) p);
365 else if (status == 0)
369 yaz_log(YLOG_LOG, "Exit %d from child %ld", status, (long) p);
383 * indent-tabs-mode: nil
385 * vim: shiftwidth=4 tabstop=8 expandtab