1 /* $Id: yaz-proxy-main.cpp,v 1.15 2005-06-08 13:29:03 adam Exp $
2 Copyright (c) 1998-2005, Index Data.
4 This file is part of the yaz-proxy.
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 <yaz++/socket-manager.h>
49 #include <yaz++/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>
59 using namespace yazpp_1;
61 void usage(char *prog)
63 fprintf (stderr, "%s: [-c config] [-l log] [-a log] [-v level] [-t target] "
64 "[-u uid] [-p pidfile] @:port\n", prog);
68 static char *pid_fname = 0;
70 static char *log_file = 0;
72 static int no_limit_files = 0;
74 int args(Yaz_Proxy *proxy, int argc, char **argv)
81 while ((ret = options("o:a:t:v:c:u:i:m:l:T:p:n:X",
82 argv, argc, &arg)) != -2)
96 err = proxy->set_config(arg);
99 fprintf(stderr, "Config file support not enabled (not using libxslt & libxml2)\n");
104 fprintf(stderr, "Bad or missing file %s\n", arg);
109 proxy->set_APDU_log(arg);
112 proxy->set_default_target(arg);
115 proxy->option("optimize", arg);
118 yaz_log_init_level (yaz_log_mask_str(arg));
121 yaz_log_init_file (arg);
122 log_file = xstrdup(arg);
125 proxy->set_max_clients(atoi(arg));
128 proxy->set_client_idletime(atoi(arg));
131 proxy->set_target_idletime(atoi(arg));
134 no_limit_files = atoi(arg);
137 proxy->set_debug_mode(1);
142 pid_fname = xstrdup(arg);
155 if (proxy->server(addr))
157 yaz_log(YLOG_FATAL|YLOG_ERRNO, "listen %s", addr);
169 static Yaz_Proxy *static_yaz_proxy = 0;
170 static void sighup_handler(int num)
174 signal(SIGHUP, sighup_handler);
176 if (static_yaz_proxy)
177 static_yaz_proxy->reconfig();
181 static void proxy_xml_error_handler(void *ctx, const char *fmt, ...)
189 vsprintf(buf, fmt, ap);
191 vsnprintf(buf, sizeof(buf), fmt, ap);
193 yaz_log(YLOG_WARN, "%s: %s", (char*) ctx, buf);
199 static void child_run(SocketManager *m, int run)
203 signal(SIGHUP, sighup_handler);
207 xmlSetGenericErrorFunc((void *) "XML", proxy_xml_error_handler);
208 xsltSetGenericErrorFunc((void *) "XSLT", proxy_xml_error_handler);
212 yaz_log(YLOG_LOG, "0 proxy run=%d pid=%ld", run, (long) getpid());
217 struct rlimit limit_data;
218 limit_data.rlim_cur = no_limit_files;
219 limit_data.rlim_max = no_limit_files;
221 yaz_log(YLOG_LOG, "0 setrlimit NOFILE cur=%ld max=%ld",
222 (long) limit_data.rlim_cur, (long) limit_data.rlim_max);
223 if (setrlimit(RLIMIT_NOFILE, &limit_data))
224 yaz_log(YLOG_ERRNO|YLOG_WARN, "setrlimit");
226 yaz_log(YLOG_WARN, "setrlimit unavablable. Option -n ignored");
233 FILE *f = fopen(pid_fname, "w");
236 yaz_log(YLOG_ERRNO|YLOG_FATAL, "Couldn't create %s", pid_fname);
239 fprintf(f, "%ld", (long) getpid());
247 if (!(pw = getpwnam(uid)))
249 yaz_log(YLOG_FATAL, "%s: Unknown user", uid);
254 chown(log_file, pw->pw_uid, pw->pw_gid);
257 if (setuid(pw->pw_uid) < 0)
259 yaz_log(YLOG_FATAL|YLOG_ERRNO, "setuid");
266 struct rlimit limit_data;
267 getrlimit(RLIMIT_NOFILE, &limit_data);
268 yaz_log(YLOG_LOG, "0 getrlimit NOFILE cur=%ld max=%ld",
269 (long) limit_data.rlim_cur, (long) limit_data.rlim_max);
272 while (m->processEvent() > 0)
278 int main(int argc, char **argv)
287 SocketManager mySocketManager;
288 Yaz_Proxy proxy(new PDU_Assoc(&mySocketManager), &mySocketManager);
290 static_yaz_proxy = &proxy;
292 args(&proxy, argc, argv);
295 child_run(&mySocketManager, run);
299 child_run(&mySocketManager, run);
307 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fork");
312 child_run(&mySocketManager, run);
322 yaz_log(YLOG_FATAL, "p1=%d != p=%d", p1, p);
325 if (WIFSIGNALED(status))
327 switch(WTERMSIG(status)) {
329 yaz_log(YLOG_WARN, "Received SIGILL from child %ld", (long) p);
333 yaz_log(YLOG_WARN, "Received SIGABRT from child %ld", (long) p);
337 yaz_log(YLOG_WARN, "Received SIGSEGV from child %ld", (long) p);
341 yaz_log(YLOG_WARN, "Received SIGBUS from child %ld", (long) p);
345 yaz_log(YLOG_LOG, "Received SIGTERM from child %ld",
350 yaz_log(YLOG_WARN, "Received SIG %d from child %ld",
351 WTERMSIG(status), (long) p);
355 else if (status == 0)
359 yaz_log(YLOG_LOG, "Exit %d from child %ld", status, (long) p);