1 /* $Id: yaz-proxy-main.cpp,v 1.9 2005-01-16 21:50:25 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>
45 #include <yaz/options.h>
47 #include <yaz++/socket-manager.h>
48 #include <yaz++/pdu-assoc.h>
49 #include <yazproxy/proxy.h>
52 #include <libxml/parser.h>
53 #include <libxml/tree.h>
54 #include <libxslt/xsltutils.h>
55 #include <libxslt/transform.h>
58 void usage(char *prog)
60 fprintf (stderr, "%s: [-c config] [-l log] [-a log] [-v level] [-t target] "
61 "[-u uid] [-p pidfile] @:port\n", prog);
65 static char *pid_fname = 0;
67 static char *log_file = 0;
69 static int no_limit_files = 0;
71 int args(Yaz_Proxy *proxy, int argc, char **argv)
78 while ((ret = options("o:a:t:v:c:u:i:m:l:T:p:U:n:X",
79 argv, argc, &arg)) != -2)
93 err = proxy->set_config(arg);
96 fprintf(stderr, "Config file support not enabled (proxy not compiled with libxml2 support)\n");
101 fprintf(stderr, "Bad or missing file %s\n", arg);
106 proxy->set_APDU_log(arg);
109 proxy->set_default_target(arg);
112 proxy->set_proxy_authentication(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);
141 pid_fname = xstrdup(arg);
154 if (proxy->server(addr))
156 yaz_log(YLOG_FATAL|YLOG_ERRNO, "listen %s", addr);
168 static Yaz_Proxy *static_yaz_proxy = 0;
169 static void sighup_handler(int num)
173 signal(SIGHUP, sighup_handler);
175 if (static_yaz_proxy)
176 static_yaz_proxy->reconfig();
180 static void proxy_xml_error_handler(void *ctx, const char *fmt, ...)
188 vsprintf(buf, fmt, ap);
190 vsnprintf(buf, sizeof(buf), fmt, ap);
192 yaz_log(YLOG_WARN, "%s: %s", (char*) ctx, buf);
198 static void child_run(Yaz_SocketManager *m, int run)
202 signal(SIGHUP, sighup_handler);
206 xmlSetGenericErrorFunc((void *) "XML", proxy_xml_error_handler);
207 xsltSetGenericErrorFunc((void *) "XSLT", proxy_xml_error_handler);
211 yaz_log(YLOG_LOG, "0 proxy run=%d pid=%ld", run, (long) getpid());
216 struct rlimit limit_data;
217 limit_data.rlim_cur = no_limit_files;
218 limit_data.rlim_max = no_limit_files;
220 yaz_log(YLOG_LOG, "0 setrlimit NOFILE cur=%ld max=%ld",
221 (long) limit_data.rlim_cur, (long) limit_data.rlim_max);
222 if (setrlimit(RLIMIT_NOFILE, &limit_data))
223 yaz_log(YLOG_ERRNO|YLOG_WARN, "setrlimit");
225 yaz_log(YLOG_WARN, "setrlimit unavablable. Option -n ignored");
232 FILE *f = fopen(pid_fname, "w");
235 yaz_log(YLOG_ERRNO|YLOG_FATAL, "Couldn't create %s", pid_fname);
238 fprintf(f, "%ld", (long) getpid());
246 if (!(pw = getpwnam(uid)))
248 yaz_log(YLOG_FATAL, "%s: Unknown user", uid);
253 chown(log_file, pw->pw_uid, pw->pw_gid);
256 if (setuid(pw->pw_uid) < 0)
258 yaz_log(YLOG_FATAL|YLOG_ERRNO, "setuid");
265 struct rlimit limit_data;
266 getrlimit(RLIMIT_NOFILE, &limit_data);
267 yaz_log(YLOG_LOG, "0 getrlimit NOFILE cur=%ld max=%ld",
268 (long) limit_data.rlim_cur, (long) limit_data.rlim_max);
271 while (m->processEvent() > 0)
277 int main(int argc, char **argv)
286 Yaz_SocketManager mySocketManager;
287 Yaz_Proxy proxy(new Yaz_PDU_Assoc(&mySocketManager));
289 static_yaz_proxy = &proxy;
291 args(&proxy, argc, argv);
294 child_run(&mySocketManager, run);
298 child_run(&mySocketManager, run);
306 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fork");
311 child_run(&mySocketManager, run);
321 yaz_log(YLOG_FATAL, "p1=%d != p=%d", p1, p);
324 if (WIFSIGNALED(status))
326 switch(WTERMSIG(status)) {
328 yaz_log(YLOG_WARN, "Received SIGILL from child %ld", (long) p);
332 yaz_log(YLOG_WARN, "Received SIGABRT from child %ld", (long) p);
336 yaz_log(YLOG_WARN, "Received SIGSEGV from child %ld", (long) p);
340 yaz_log(YLOG_WARN, "Received SIGBUS from child %ld", (long) p);
344 yaz_log(YLOG_LOG, "Received SIGTERM from child %ld",
349 yaz_log(YLOG_WARN, "Received SIG %d from child %ld",
350 WTERMSIG(status), (long) p);
354 else if (status == 0)
358 yaz_log(YLOG_LOG, "Exit %d from child %ld", status, (long) p);