1 /* $Id: yaz-proxy-main.cpp,v 1.1 2004-03-29 22:48:31 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
25 #include <sys/types.h>
30 #include <sys/resource.h>
34 #include <yaz/options.h>
36 #include <yaz++/socket-manager.h>
37 #include <yaz++/pdu-assoc.h>
38 #include <yaz++/proxy/proxy.h>
40 void usage(char *prog)
42 fprintf (stderr, "%s: [-c config] [-l log] [-a log] [-v level] [-t target] "
43 "[-u uid] [-p pidfile] @:port\n", prog);
47 static char *pid_fname = 0;
49 static char *log_file = 0;
51 static int no_limit_files = 0;
53 int args(Yaz_Proxy *proxy, int argc, char **argv)
60 while ((ret = options("o:a:t:v:c:u:i:m:l:T:p:U:n:X",
61 argv, argc, &arg)) != -2)
75 err = proxy->set_config(arg);
78 fprintf(stderr, "Config file support not enabled (proxy not compiled with libxml2 support)\n");
83 fprintf(stderr, "Bad or missing file %s\n", arg);
88 proxy->set_APDU_log(arg);
91 proxy->set_default_target(arg);
94 proxy->set_proxy_authentication(arg);
97 proxy->option("optimize", arg);
100 yaz_log_init_level (yaz_log_mask_str(arg));
103 yaz_log_init_file (arg);
104 log_file = xstrdup(arg);
107 proxy->set_max_clients(atoi(arg));
110 proxy->set_client_idletime(atoi(arg));
113 proxy->set_target_idletime(atoi(arg));
116 no_limit_files = atoi(arg);
123 pid_fname = xstrdup(arg);
136 if (proxy->server(addr))
138 yaz_log(LOG_FATAL|LOG_ERRNO, "listen %s", addr);
150 static Yaz_Proxy *static_yaz_proxy = 0;
151 static void sighup_handler(int num)
153 signal(SIGHUP, sighup_handler);
154 if (static_yaz_proxy)
155 static_yaz_proxy->reconfig();
159 static void proxy_xml_error_handler(void *ctx, const char *fmt, ...)
166 vsnprintf(buf, sizeof(buf), fmt, ap);
168 yaz_log(LOG_WARN, "%s: %s", (char*) ctx, buf);
174 static void child_run(Yaz_SocketManager *m, int run)
176 signal(SIGHUP, sighup_handler);
179 xmlSetGenericErrorFunc((void *) "XML", proxy_xml_error_handler);
180 xsltSetGenericErrorFunc((void *) "XSLT", proxy_xml_error_handler);
182 yaz_log(LOG_LOG, "0 proxy run=%d pid=%ld", run, (long) getpid());
187 struct rlimit limit_data;
188 limit_data.rlim_cur = no_limit_files;
189 limit_data.rlim_max = no_limit_files;
191 yaz_log(LOG_LOG, "0 setrlimit NOFILE cur=%d max=%d",
192 limit_data.rlim_cur, limit_data.rlim_max);
193 if (setrlimit(RLIMIT_NOFILE, &limit_data))
194 yaz_log(LOG_ERRNO|LOG_WARN, "setrlimit");
196 yaz_log(LOG_WARN, "setrlimit unavablable. Option -n ignored");
201 FILE *f = fopen(pid_fname, "w");
204 yaz_log(LOG_ERRNO|LOG_FATAL, "Couldn't create %s", pid_fname);
207 fprintf(f, "%ld", (long) getpid());
215 if (!(pw = getpwnam(uid)))
217 yaz_log(LOG_FATAL, "%s: Unknown user", uid);
222 chown(log_file, pw->pw_uid, pw->pw_gid);
225 if (setuid(pw->pw_uid) < 0)
227 yaz_log(LOG_FATAL|LOG_ERRNO, "setuid");
233 struct rlimit limit_data;
234 getrlimit(RLIMIT_NOFILE, &limit_data);
235 yaz_log(LOG_LOG, "0 getrlimit NOFILE cur=%d max=%d",
236 limit_data.rlim_cur, limit_data.rlim_max);
239 while (m->processEvent() > 0)
245 int main(int argc, char **argv)
254 Yaz_SocketManager mySocketManager;
255 Yaz_Proxy proxy(new Yaz_PDU_Assoc(&mySocketManager));
257 static_yaz_proxy = &proxy;
259 args(&proxy, argc, argv);
263 child_run(&mySocketManager, run);
271 yaz_log(LOG_FATAL|LOG_ERRNO, "fork");
276 child_run(&mySocketManager, run);
286 yaz_log(LOG_FATAL, "p1=%d != p=%d", p1, p);
289 if (WIFSIGNALED(status))
291 switch(WTERMSIG(status)) {
293 yaz_log(LOG_WARN, "Received SIGILL from child %ld", (long) p);
297 yaz_log(LOG_WARN, "Received SIGABRT from child %ld", (long) p);
301 yaz_log(LOG_WARN, "Received SIGSEGV from child %ld", (long) p);
305 yaz_log(LOG_WARN, "Received SIGBUS from child %ld", (long) p);
309 yaz_log(LOG_LOG, "Received SIGTERM from child %ld",
314 yaz_log(LOG_WARN, "Received SIG %d from child %ld",
315 WTERMSIG(status), (long) p);
319 else if (status == 0)
323 yaz_log(LOG_LOG, "Exit %d from child %ld", status, (long) p);