1 /* $Id: yaz-proxy-main.cpp,v 1.12 2005-02-21 14:27:32 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>
46 #include <yaz/options.h>
48 #include <yaz++/socket-manager.h>
49 #include <yaz++/pdu-assoc.h>
50 #include <yazproxy/proxy.h>
53 #include <libxml/parser.h>
54 #include <libxml/tree.h>
55 #include <libxslt/xsltutils.h>
56 #include <libxslt/transform.h>
59 void usage(char *prog)
61 fprintf (stderr, "%s: [-c config] [-l log] [-a log] [-v level] [-t target] "
62 "[-u uid] [-p pidfile] @:port\n", prog);
66 static char *pid_fname = 0;
68 static char *log_file = 0;
70 static int no_limit_files = 0;
72 int args(Yaz_Proxy *proxy, int argc, char **argv)
79 while ((ret = options("o:a:t:v:c:u:i:m:l:T:p:n:X",
80 argv, argc, &arg)) != -2)
94 err = proxy->set_config(arg);
97 fprintf(stderr, "Config file support not enabled (not using libxslt & libxml2)\n");
102 fprintf(stderr, "Bad or missing file %s\n", arg);
107 proxy->set_APDU_log(arg);
110 proxy->set_default_target(arg);
113 proxy->option("optimize", arg);
116 yaz_log_init_level (yaz_log_mask_str(arg));
119 yaz_log_init_file (arg);
120 log_file = xstrdup(arg);
123 proxy->set_max_clients(atoi(arg));
126 proxy->set_client_idletime(atoi(arg));
129 proxy->set_target_idletime(atoi(arg));
132 no_limit_files = atoi(arg);
135 proxy->set_debug_mode(1);
140 pid_fname = xstrdup(arg);
153 if (proxy->server(addr))
155 yaz_log(YLOG_FATAL|YLOG_ERRNO, "listen %s", addr);
167 static Yaz_Proxy *static_yaz_proxy = 0;
168 static void sighup_handler(int num)
172 signal(SIGHUP, sighup_handler);
174 if (static_yaz_proxy)
175 static_yaz_proxy->reconfig();
179 static void proxy_xml_error_handler(void *ctx, const char *fmt, ...)
187 vsprintf(buf, fmt, ap);
189 vsnprintf(buf, sizeof(buf), fmt, ap);
191 yaz_log(YLOG_WARN, "%s: %s", (char*) ctx, buf);
197 static void child_run(Yaz_SocketManager *m, int run)
201 signal(SIGHUP, sighup_handler);
205 xmlSetGenericErrorFunc((void *) "XML", proxy_xml_error_handler);
206 xsltSetGenericErrorFunc((void *) "XSLT", proxy_xml_error_handler);
210 yaz_log(YLOG_LOG, "0 proxy run=%d pid=%ld", run, (long) getpid());
215 struct rlimit limit_data;
216 limit_data.rlim_cur = no_limit_files;
217 limit_data.rlim_max = no_limit_files;
219 yaz_log(YLOG_LOG, "0 setrlimit NOFILE cur=%ld max=%ld",
220 (long) limit_data.rlim_cur, (long) limit_data.rlim_max);
221 if (setrlimit(RLIMIT_NOFILE, &limit_data))
222 yaz_log(YLOG_ERRNO|YLOG_WARN, "setrlimit");
224 yaz_log(YLOG_WARN, "setrlimit unavablable. Option -n ignored");
231 FILE *f = fopen(pid_fname, "w");
234 yaz_log(YLOG_ERRNO|YLOG_FATAL, "Couldn't create %s", pid_fname);
237 fprintf(f, "%ld", (long) getpid());
245 if (!(pw = getpwnam(uid)))
247 yaz_log(YLOG_FATAL, "%s: Unknown user", uid);
252 chown(log_file, pw->pw_uid, pw->pw_gid);
255 if (setuid(pw->pw_uid) < 0)
257 yaz_log(YLOG_FATAL|YLOG_ERRNO, "setuid");
264 struct rlimit limit_data;
265 getrlimit(RLIMIT_NOFILE, &limit_data);
266 yaz_log(YLOG_LOG, "0 getrlimit NOFILE cur=%ld max=%ld",
267 (long) limit_data.rlim_cur, (long) limit_data.rlim_max);
270 while (m->processEvent() > 0)
276 int main(int argc, char **argv)
285 Yaz_SocketManager mySocketManager;
286 Yaz_Proxy proxy(new Yaz_PDU_Assoc(&mySocketManager));
288 static_yaz_proxy = &proxy;
290 args(&proxy, argc, argv);
293 child_run(&mySocketManager, run);
297 child_run(&mySocketManager, run);
305 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fork");
310 child_run(&mySocketManager, run);
320 yaz_log(YLOG_FATAL, "p1=%d != p=%d", p1, p);
323 if (WIFSIGNALED(status))
325 switch(WTERMSIG(status)) {
327 yaz_log(YLOG_WARN, "Received SIGILL from child %ld", (long) p);
331 yaz_log(YLOG_WARN, "Received SIGABRT from child %ld", (long) p);
335 yaz_log(YLOG_WARN, "Received SIGSEGV from child %ld", (long) p);
339 yaz_log(YLOG_WARN, "Received SIGBUS from child %ld", (long) p);
343 yaz_log(YLOG_LOG, "Received SIGTERM from child %ld",
348 yaz_log(YLOG_WARN, "Received SIG %d from child %ld",
349 WTERMSIG(status), (long) p);
353 else if (status == 0)
357 yaz_log(YLOG_LOG, "Exit %d from child %ld", status, (long) p);