Rephrase comment about missing XML support
[yazproxy-moved-to-github.git] / src / yaz-proxy-main.cpp
1 /* $Id: yaz-proxy-main.cpp,v 1.11 2005-02-11 15:39:55 adam Exp $
2    Copyright (c) 1998-2005, Index Data.
3
4 This file is part of the yaz-proxy.
5
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
9 version.
10
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
14 for more details.
15
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
19 02111-1307, USA.
20  */
21
22 #include <signal.h>
23 #if HAVE_UNISTD_H
24 #include <unistd.h>
25 #endif
26 #if HAVE_SYS_TYPES_H
27 #include <sys/types.h>
28 #endif
29 #if HAVE_SYS_TIME_H
30 #include <sys/time.h>
31 #endif
32 #if HAVE_SYS_WAIT_H
33 #include <sys/wait.h>
34 #endif
35 #if HAVE_SYS_RESOURCE_H
36 #include <sys/resource.h>
37 #endif
38 #if HAVE_PWD_H
39 #include <pwd.h>
40 #endif
41
42 #include <stdarg.h>
43 #include <stdlib.h>
44
45 #include <yaz/log.h>
46 #include <yaz/options.h>
47
48 #include <yaz++/socket-manager.h>
49 #include <yaz++/pdu-assoc.h>
50 #include <yazproxy/proxy.h>
51
52 #if HAVE_XSLT
53 #include <libxml/parser.h>
54 #include <libxml/tree.h>
55 #include <libxslt/xsltutils.h>
56 #include <libxslt/transform.h>
57 #endif
58
59 void usage(char *prog)
60 {
61     fprintf (stderr, "%s: [-c config] [-l log] [-a log] [-v level] [-t target] "
62              "[-u uid] [-p pidfile] @:port\n", prog);
63     exit (1);
64 }
65
66 static char *pid_fname = 0;
67 static char *uid = 0;
68 static char *log_file = 0;
69 static int debug = 0;
70 static int no_limit_files = 0;
71
72 int args(Yaz_Proxy *proxy, int argc, char **argv)
73 {
74     char *addr = 0;
75     char *arg;
76     char *prog = argv[0];
77     int ret;
78
79     while ((ret = options("o:a:t:v:c:u:i:m:l:T:p:U:n:X",
80                           argv, argc, &arg)) != -2)
81     {
82         int err;
83         switch (ret)
84         {
85         case 0:
86             if (addr)
87             {
88                 usage(prog);
89                 return 1;
90             }
91             addr = arg;
92             break;
93         case 'c':
94             err = proxy->set_config(arg);
95             if (err == -2)
96             {
97                 fprintf(stderr, "Config file support not enabled (not using libxslt & libxml2)\n");
98                 exit(1);
99             }
100             else if (err == -1)
101             {
102                 fprintf(stderr, "Bad or missing file %s\n", arg);
103                 exit(1);
104             }
105             break;
106         case 'a':
107             proxy->set_APDU_log(arg);
108             break;
109         case 't':
110             proxy->set_default_target(arg);
111             break;
112         case 'U':
113             proxy->set_proxy_authentication(arg);
114             break;
115         case 'o':
116             proxy->option("optimize", arg);
117             break;
118         case 'v':
119             yaz_log_init_level (yaz_log_mask_str(arg));
120             break;
121         case 'l':
122             yaz_log_init_file (arg);
123             log_file = xstrdup(arg);
124             break;
125         case 'm':
126             proxy->set_max_clients(atoi(arg));
127             break;
128         case 'i':
129             proxy->set_client_idletime(atoi(arg));
130             break;
131         case 'T':
132             proxy->set_target_idletime(atoi(arg));
133             break;
134         case 'n':
135             no_limit_files = atoi(arg);
136             break;
137         case 'X':
138             debug = 1;
139             break;
140         case 'p':
141             if (!pid_fname)
142                 pid_fname = xstrdup(arg);
143             break;
144         case 'u':
145             if (!uid)
146                 uid = xstrdup(arg);
147             break;
148         default:
149             usage(prog);
150             return 1;
151         }
152     }
153     if (addr)
154     {
155         if (proxy->server(addr))
156         {
157             yaz_log(YLOG_FATAL|YLOG_ERRNO, "listen %s", addr);
158             exit(1);
159         }
160     }
161     else
162     {
163         usage(prog);
164         return 1;
165     }
166     return 0;
167 }
168
169 static Yaz_Proxy *static_yaz_proxy = 0;
170 static void sighup_handler(int num)
171 {
172 #if WIN32
173 #else
174     signal(SIGHUP, sighup_handler);
175 #endif
176     if (static_yaz_proxy)
177         static_yaz_proxy->reconfig();
178 }
179
180 #if HAVE_XSLT
181 static void proxy_xml_error_handler(void *ctx, const char *fmt, ...)
182 {
183     char buf[1024];
184
185     va_list ap;
186     va_start(ap, fmt);
187
188 #ifdef WIN32
189     vsprintf(buf, fmt, ap);
190 #else
191     vsnprintf(buf, sizeof(buf), fmt, ap);
192 #endif
193     yaz_log(YLOG_WARN, "%s: %s", (char*) ctx, buf);
194
195     va_end (ap);
196 }
197 #endif
198
199 static void child_run(Yaz_SocketManager *m, int run)
200 {
201 #ifdef WIN32
202 #else
203     signal(SIGHUP, sighup_handler);
204 #endif
205
206 #if HAVE_XSLT
207     xmlSetGenericErrorFunc((void *) "XML", proxy_xml_error_handler);
208     xsltSetGenericErrorFunc((void *) "XSLT", proxy_xml_error_handler);
209 #endif
210 #ifdef WIN32
211 #else
212     yaz_log(YLOG_LOG, "0 proxy run=%d pid=%ld", run, (long) getpid());
213 #endif
214     if (no_limit_files)
215     {
216 #if HAVE_SETRLIMIT
217         struct rlimit limit_data;
218         limit_data.rlim_cur = no_limit_files;
219         limit_data.rlim_max = no_limit_files;
220         
221         yaz_log(YLOG_LOG, "0 setrlimit NOFILE cur=%ld max=%ld",
222                 (long) limit_data.rlim_cur, (long) limit_data.rlim_max);
223         if (setrlimit(RLIMIT_NOFILE, &limit_data))
224             yaz_log(YLOG_ERRNO|YLOG_WARN, "setrlimit");
225 #else
226         yaz_log(YLOG_WARN, "setrlimit unavablable. Option -n ignored");
227 #endif
228     }
229 #ifdef WIN32
230 #else
231     if (pid_fname)
232     {
233         FILE *f = fopen(pid_fname, "w");
234         if (!f)
235         {
236             yaz_log(YLOG_ERRNO|YLOG_FATAL, "Couldn't create %s", pid_fname);
237             exit(0);
238         }
239         fprintf(f, "%ld", (long) getpid());
240         fclose(f);
241         xfree(pid_fname);
242     }
243     if (uid)
244     {
245         struct passwd *pw;
246
247         if (!(pw = getpwnam(uid)))
248         {
249             yaz_log(YLOG_FATAL, "%s: Unknown user", uid);
250             exit(3);
251         }
252         if (log_file)
253         {
254             chown(log_file, pw->pw_uid,  pw->pw_gid);
255             xfree(log_file);
256         }
257         if (setuid(pw->pw_uid) < 0)
258         {
259             yaz_log(YLOG_FATAL|YLOG_ERRNO, "setuid");
260             exit(4);
261         }
262         xfree(uid);
263     }
264 #endif
265 #if HAVE_GETRLIMIT
266     struct rlimit limit_data;
267     getrlimit(RLIMIT_NOFILE, &limit_data);
268     yaz_log(YLOG_LOG, "0 getrlimit NOFILE cur=%ld max=%ld",
269             (long) limit_data.rlim_cur, (long) limit_data.rlim_max);
270 #endif
271     
272     while (m->processEvent() > 0)
273         ;
274
275     exit (0);
276 }
277
278 int main(int argc, char **argv)
279 {
280 #if HAVE_XSLT
281     xmlInitMemory();
282     
283     LIBXML_TEST_VERSION
284 #endif
285     int cont = 1;
286     int run = 1;
287     Yaz_SocketManager mySocketManager;
288     Yaz_Proxy proxy(new Yaz_PDU_Assoc(&mySocketManager));
289
290     static_yaz_proxy = &proxy;
291
292     args(&proxy, argc, argv);
293
294 #ifdef WIN32
295     child_run(&mySocketManager, run);
296 #else
297     if (debug)
298     {
299         child_run(&mySocketManager, run);
300         exit(0);
301     }
302     while (cont)
303     {
304         pid_t p = fork();
305         if (p == (pid_t) -1)
306         {
307             yaz_log(YLOG_FATAL|YLOG_ERRNO, "fork");
308             exit(1);
309         }
310         else if (p == 0)
311         {
312             child_run(&mySocketManager, run);
313         }
314         pid_t p1;
315         int status;
316         p1 = wait(&status);
317
318         yaz_log_reopen();
319
320         if (p1 != p)
321         {
322             yaz_log(YLOG_FATAL, "p1=%d != p=%d", p1, p);
323             exit(1);
324         }
325         if (WIFSIGNALED(status))
326         {
327             switch(WTERMSIG(status)) {
328             case SIGILL:
329                 yaz_log(YLOG_WARN, "Received SIGILL from child %ld", (long) p);
330                 cont = 1;
331                 break;
332             case SIGABRT:
333                 yaz_log(YLOG_WARN, "Received SIGABRT from child %ld", (long) p);
334                 cont = 1;
335                 break ;
336             case SIGSEGV:
337                 yaz_log(YLOG_WARN, "Received SIGSEGV from child %ld", (long) p);
338                 cont = 1;
339                 break;
340             case SIGBUS:        
341                 yaz_log(YLOG_WARN, "Received SIGBUS from child %ld", (long) p);
342                 cont = 1;
343                 break;
344             case SIGTERM:
345                 yaz_log(YLOG_LOG, "Received SIGTERM from child %ld",
346                         (long) p);
347                 cont = 0;
348                 break;
349             default:
350                 yaz_log(YLOG_WARN, "Received SIG %d from child %ld",
351                         WTERMSIG(status), (long) p);
352                 cont = 0;
353             }
354         }
355         else if (status == 0)
356             cont = 0;
357         else
358         {
359             yaz_log(YLOG_LOG, "Exit %d from child %ld", status, (long) p);
360             cont = 1;
361         }
362         if (cont)
363             sleep(1 + run/5);
364         run++;
365     }
366 #endif
367     exit (0);
368     return 0;
369 }