1 /* $Id: yaz-proxy-main.cpp,v 1.1 2004-04-11 11:36:47 adam Exp $
2 Copyright (c) 1998-2004, Index Data.
4 This file is part of the yaz-proxy.
6 Zebra 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 Zebra 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 Zebra; see the file LICENSE.proxy. 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 <yaz++/proxy/proxy.h>
44 void usage(char *prog)
46 fprintf (stderr, "%s: [-c config] [-l log] [-a log] [-v level] [-t target] "
47 "[-u uid] [-p pidfile] @:port\n", prog);
51 static char *pid_fname = 0;
53 static char *log_file = 0;
55 static int no_limit_files = 0;
57 int args(Yaz_Proxy *proxy, int argc, char **argv)
64 while ((ret = options("o:a:t:v:c:u:i:m:l:T:p:U:n:X",
65 argv, argc, &arg)) != -2)
79 err = proxy->set_config(arg);
82 fprintf(stderr, "Config file support not enabled (proxy not compiled with libxml2 support)\n");
87 fprintf(stderr, "Bad or missing file %s\n", arg);
92 proxy->set_APDU_log(arg);
95 proxy->set_default_target(arg);
98 proxy->set_proxy_authentication(arg);
101 proxy->option("optimize", arg);
104 yaz_log_init_level (yaz_log_mask_str(arg));
107 yaz_log_init_file (arg);
108 log_file = xstrdup(arg);
111 proxy->set_max_clients(atoi(arg));
114 proxy->set_client_idletime(atoi(arg));
117 proxy->set_target_idletime(atoi(arg));
120 no_limit_files = atoi(arg);
127 pid_fname = xstrdup(arg);
140 if (proxy->server(addr))
142 yaz_log(LOG_FATAL|LOG_ERRNO, "listen %s", addr);
154 static Yaz_Proxy *static_yaz_proxy = 0;
155 static void sighup_handler(int num)
159 signal(SIGHUP, sighup_handler);
161 if (static_yaz_proxy)
162 static_yaz_proxy->reconfig();
166 static void proxy_xml_error_handler(void *ctx, const char *fmt, ...)
174 vsprintf(buf, fmt, ap);
176 vsnprintf(buf, sizeof(buf), fmt, ap);
178 yaz_log(LOG_WARN, "%s: %s", (char*) ctx, buf);
184 static void child_run(Yaz_SocketManager *m, int run)
188 signal(SIGHUP, sighup_handler);
192 xmlSetGenericErrorFunc((void *) "XML", proxy_xml_error_handler);
193 xsltSetGenericErrorFunc((void *) "XSLT", proxy_xml_error_handler);
197 yaz_log(LOG_LOG, "0 proxy run=%d pid=%ld", run, (long) getpid());
202 struct rlimit limit_data;
203 limit_data.rlim_cur = no_limit_files;
204 limit_data.rlim_max = no_limit_files;
206 yaz_log(LOG_LOG, "0 setrlimit NOFILE cur=%d max=%d",
207 limit_data.rlim_cur, limit_data.rlim_max);
208 if (setrlimit(RLIMIT_NOFILE, &limit_data))
209 yaz_log(LOG_ERRNO|LOG_WARN, "setrlimit");
211 yaz_log(LOG_WARN, "setrlimit unavablable. Option -n ignored");
218 FILE *f = fopen(pid_fname, "w");
221 yaz_log(LOG_ERRNO|LOG_FATAL, "Couldn't create %s", pid_fname);
224 fprintf(f, "%ld", (long) getpid());
232 if (!(pw = getpwnam(uid)))
234 yaz_log(LOG_FATAL, "%s: Unknown user", uid);
239 chown(log_file, pw->pw_uid, pw->pw_gid);
242 if (setuid(pw->pw_uid) < 0)
244 yaz_log(LOG_FATAL|LOG_ERRNO, "setuid");
251 struct rlimit limit_data;
252 getrlimit(RLIMIT_NOFILE, &limit_data);
253 yaz_log(LOG_LOG, "0 getrlimit NOFILE cur=%d max=%d",
254 limit_data.rlim_cur, limit_data.rlim_max);
257 while (m->processEvent() > 0)
263 int main(int argc, char **argv)
272 Yaz_SocketManager mySocketManager;
273 Yaz_Proxy proxy(new Yaz_PDU_Assoc(&mySocketManager));
275 static_yaz_proxy = &proxy;
277 args(&proxy, argc, argv);
280 child_run(&mySocketManager, run);
284 child_run(&mySocketManager, run);
292 yaz_log(LOG_FATAL|LOG_ERRNO, "fork");
297 child_run(&mySocketManager, run);
307 yaz_log(LOG_FATAL, "p1=%d != p=%d", p1, p);
310 if (WIFSIGNALED(status))
312 switch(WTERMSIG(status)) {
314 yaz_log(LOG_WARN, "Received SIGILL from child %ld", (long) p);
318 yaz_log(LOG_WARN, "Received SIGABRT from child %ld", (long) p);
322 yaz_log(LOG_WARN, "Received SIGSEGV from child %ld", (long) p);
326 yaz_log(LOG_WARN, "Received SIGBUS from child %ld", (long) p);
330 yaz_log(LOG_LOG, "Received SIGTERM from child %ld",
335 yaz_log(LOG_WARN, "Received SIG %d from child %ld",
336 WTERMSIG(status), (long) p);
340 else if (status == 0)
344 yaz_log(LOG_LOG, "Exit %d from child %ld", status, (long) p);