1 /* $Id: yaz-proxy-main.cpp,v 1.6 2004-11-30 21:10:45 adam Exp $
2 Copyright (c) 1998-2004, 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
28 #include <sys/types.h>
34 #include <sys/resource.h>
38 #include <yaz/options.h>
40 #include <yaz++/socket-manager.h>
41 #include <yaz++/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>
51 void usage(char *prog)
53 fprintf (stderr, "%s: [-c config] [-l log] [-a log] [-v level] [-t target] "
54 "[-u uid] [-p pidfile] @:port\n", prog);
58 static char *pid_fname = 0;
60 static char *log_file = 0;
62 static int no_limit_files = 0;
64 int args(Yaz_Proxy *proxy, int argc, char **argv)
71 while ((ret = options("o:a:t:v:c:u:i:m:l:T:p:U:n:X",
72 argv, argc, &arg)) != -2)
86 err = proxy->set_config(arg);
89 fprintf(stderr, "Config file support not enabled (proxy not compiled with libxml2 support)\n");
94 fprintf(stderr, "Bad or missing file %s\n", arg);
99 proxy->set_APDU_log(arg);
102 proxy->set_default_target(arg);
105 proxy->set_proxy_authentication(arg);
108 proxy->option("optimize", arg);
111 yaz_log_init_level (yaz_log_mask_str(arg));
114 yaz_log_init_file (arg);
115 log_file = xstrdup(arg);
118 proxy->set_max_clients(atoi(arg));
121 proxy->set_client_idletime(atoi(arg));
124 proxy->set_target_idletime(atoi(arg));
127 no_limit_files = atoi(arg);
134 pid_fname = xstrdup(arg);
147 if (proxy->server(addr))
149 yaz_log(YLOG_FATAL|YLOG_ERRNO, "listen %s", addr);
161 static Yaz_Proxy *static_yaz_proxy = 0;
162 static void sighup_handler(int num)
166 signal(SIGHUP, sighup_handler);
168 if (static_yaz_proxy)
169 static_yaz_proxy->reconfig();
173 static void proxy_xml_error_handler(void *ctx, const char *fmt, ...)
181 vsprintf(buf, fmt, ap);
183 vsnprintf(buf, sizeof(buf), fmt, ap);
185 yaz_log(YLOG_WARN, "%s: %s", (char*) ctx, buf);
191 static void child_run(Yaz_SocketManager *m, int run)
195 signal(SIGHUP, sighup_handler);
199 xmlSetGenericErrorFunc((void *) "XML", proxy_xml_error_handler);
200 xsltSetGenericErrorFunc((void *) "XSLT", proxy_xml_error_handler);
204 yaz_log(YLOG_LOG, "0 proxy run=%d pid=%ld", run, (long) getpid());
209 struct rlimit limit_data;
210 limit_data.rlim_cur = no_limit_files;
211 limit_data.rlim_max = no_limit_files;
213 yaz_log(YLOG_LOG, "0 setrlimit NOFILE cur=%ld max=%ld",
214 (long) limit_data.rlim_cur, (long) limit_data.rlim_max);
215 if (setrlimit(RLIMIT_NOFILE, &limit_data))
216 yaz_log(YLOG_ERRNO|YLOG_WARN, "setrlimit");
218 yaz_log(YLOG_WARN, "setrlimit unavablable. Option -n ignored");
225 FILE *f = fopen(pid_fname, "w");
228 yaz_log(YLOG_ERRNO|YLOG_FATAL, "Couldn't create %s", pid_fname);
231 fprintf(f, "%ld", (long) getpid());
239 if (!(pw = getpwnam(uid)))
241 yaz_log(YLOG_FATAL, "%s: Unknown user", uid);
246 chown(log_file, pw->pw_uid, pw->pw_gid);
249 if (setuid(pw->pw_uid) < 0)
251 yaz_log(YLOG_FATAL|YLOG_ERRNO, "setuid");
258 struct rlimit limit_data;
259 getrlimit(RLIMIT_NOFILE, &limit_data);
260 yaz_log(YLOG_LOG, "0 getrlimit NOFILE cur=%ld max=%ld",
261 (long) limit_data.rlim_cur, (long) limit_data.rlim_max);
264 while (m->processEvent() > 0)
270 int main(int argc, char **argv)
279 Yaz_SocketManager mySocketManager;
280 Yaz_Proxy proxy(new Yaz_PDU_Assoc(&mySocketManager));
282 static_yaz_proxy = &proxy;
284 args(&proxy, argc, argv);
287 child_run(&mySocketManager, run);
291 child_run(&mySocketManager, run);
299 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fork");
304 child_run(&mySocketManager, run);
314 yaz_log(YLOG_FATAL, "p1=%d != p=%d", p1, p);
317 if (WIFSIGNALED(status))
319 switch(WTERMSIG(status)) {
321 yaz_log(YLOG_WARN, "Received SIGILL from child %ld", (long) p);
325 yaz_log(YLOG_WARN, "Received SIGABRT from child %ld", (long) p);
329 yaz_log(YLOG_WARN, "Received SIGSEGV from child %ld", (long) p);
333 yaz_log(YLOG_WARN, "Received SIGBUS from child %ld", (long) p);
337 yaz_log(YLOG_LOG, "Received SIGTERM from child %ld",
342 yaz_log(YLOG_WARN, "Received SIG %d from child %ld",
343 WTERMSIG(status), (long) p);
347 else if (status == 0)
351 yaz_log(YLOG_LOG, "Exit %d from child %ld", status, (long) p);