1 /* $Id: yaz-proxy-main.cpp,v 1.8 2005-01-11 20:50:28 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>
32 #if HAVE_SYS_RESOURCE_H
33 #include <sys/resource.h>
41 #include <yaz/options.h>
43 #include <yaz++/socket-manager.h>
44 #include <yaz++/pdu-assoc.h>
45 #include <yazproxy/proxy.h>
48 #include <libxml/parser.h>
49 #include <libxml/tree.h>
50 #include <libxslt/xsltutils.h>
51 #include <libxslt/transform.h>
54 void usage(char *prog)
56 fprintf (stderr, "%s: [-c config] [-l log] [-a log] [-v level] [-t target] "
57 "[-u uid] [-p pidfile] @:port\n", prog);
61 static char *pid_fname = 0;
63 static char *log_file = 0;
65 static int no_limit_files = 0;
67 int args(Yaz_Proxy *proxy, int argc, char **argv)
74 while ((ret = options("o:a:t:v:c:u:i:m:l:T:p:U:n:X",
75 argv, argc, &arg)) != -2)
89 err = proxy->set_config(arg);
92 fprintf(stderr, "Config file support not enabled (proxy not compiled with libxml2 support)\n");
97 fprintf(stderr, "Bad or missing file %s\n", arg);
102 proxy->set_APDU_log(arg);
105 proxy->set_default_target(arg);
108 proxy->set_proxy_authentication(arg);
111 proxy->option("optimize", arg);
114 yaz_log_init_level (yaz_log_mask_str(arg));
117 yaz_log_init_file (arg);
118 log_file = xstrdup(arg);
121 proxy->set_max_clients(atoi(arg));
124 proxy->set_client_idletime(atoi(arg));
127 proxy->set_target_idletime(atoi(arg));
130 no_limit_files = atoi(arg);
137 pid_fname = xstrdup(arg);
150 if (proxy->server(addr))
152 yaz_log(YLOG_FATAL|YLOG_ERRNO, "listen %s", addr);
164 static Yaz_Proxy *static_yaz_proxy = 0;
165 static void sighup_handler(int num)
169 signal(SIGHUP, sighup_handler);
171 if (static_yaz_proxy)
172 static_yaz_proxy->reconfig();
176 static void proxy_xml_error_handler(void *ctx, const char *fmt, ...)
184 vsprintf(buf, fmt, ap);
186 vsnprintf(buf, sizeof(buf), fmt, ap);
188 yaz_log(YLOG_WARN, "%s: %s", (char*) ctx, buf);
194 static void child_run(Yaz_SocketManager *m, int run)
198 signal(SIGHUP, sighup_handler);
202 xmlSetGenericErrorFunc((void *) "XML", proxy_xml_error_handler);
203 xsltSetGenericErrorFunc((void *) "XSLT", proxy_xml_error_handler);
207 yaz_log(YLOG_LOG, "0 proxy run=%d pid=%ld", run, (long) getpid());
212 struct rlimit limit_data;
213 limit_data.rlim_cur = no_limit_files;
214 limit_data.rlim_max = no_limit_files;
216 yaz_log(YLOG_LOG, "0 setrlimit NOFILE cur=%ld max=%ld",
217 (long) limit_data.rlim_cur, (long) limit_data.rlim_max);
218 if (setrlimit(RLIMIT_NOFILE, &limit_data))
219 yaz_log(YLOG_ERRNO|YLOG_WARN, "setrlimit");
221 yaz_log(YLOG_WARN, "setrlimit unavablable. Option -n ignored");
228 FILE *f = fopen(pid_fname, "w");
231 yaz_log(YLOG_ERRNO|YLOG_FATAL, "Couldn't create %s", pid_fname);
234 fprintf(f, "%ld", (long) getpid());
242 if (!(pw = getpwnam(uid)))
244 yaz_log(YLOG_FATAL, "%s: Unknown user", uid);
249 chown(log_file, pw->pw_uid, pw->pw_gid);
252 if (setuid(pw->pw_uid) < 0)
254 yaz_log(YLOG_FATAL|YLOG_ERRNO, "setuid");
261 struct rlimit limit_data;
262 getrlimit(RLIMIT_NOFILE, &limit_data);
263 yaz_log(YLOG_LOG, "0 getrlimit NOFILE cur=%ld max=%ld",
264 (long) limit_data.rlim_cur, (long) limit_data.rlim_max);
267 while (m->processEvent() > 0)
273 int main(int argc, char **argv)
282 Yaz_SocketManager mySocketManager;
283 Yaz_Proxy proxy(new Yaz_PDU_Assoc(&mySocketManager));
285 static_yaz_proxy = &proxy;
287 args(&proxy, argc, argv);
290 child_run(&mySocketManager, run);
294 child_run(&mySocketManager, run);
302 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fork");
307 child_run(&mySocketManager, run);
317 yaz_log(YLOG_FATAL, "p1=%d != p=%d", p1, p);
320 if (WIFSIGNALED(status))
322 switch(WTERMSIG(status)) {
324 yaz_log(YLOG_WARN, "Received SIGILL from child %ld", (long) p);
328 yaz_log(YLOG_WARN, "Received SIGABRT from child %ld", (long) p);
332 yaz_log(YLOG_WARN, "Received SIGSEGV from child %ld", (long) p);
336 yaz_log(YLOG_WARN, "Received SIGBUS from child %ld", (long) p);
340 yaz_log(YLOG_LOG, "Received SIGTERM from child %ld",
345 yaz_log(YLOG_WARN, "Received SIG %d from child %ld",
346 WTERMSIG(status), (long) p);
350 else if (status == 0)
354 yaz_log(YLOG_LOG, "Exit %d from child %ld", status, (long) p);