Ignore generated file cdetails
[yazproxy-moved-to-github.git] / src / yaz-proxy-main.cpp
1 /* $Id: yaz-proxy-main.cpp,v 1.12 2005-02-21 14:27:32 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: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 'o':
113             proxy->option("optimize", arg);
114             break;
115         case 'v':
116             yaz_log_init_level (yaz_log_mask_str(arg));
117             break;
118         case 'l':
119             yaz_log_init_file (arg);
120             log_file = xstrdup(arg);
121             break;
122         case 'm':
123             proxy->set_max_clients(atoi(arg));
124             break;
125         case 'i':
126             proxy->set_client_idletime(atoi(arg));
127             break;
128         case 'T':
129             proxy->set_target_idletime(atoi(arg));
130             break;
131         case 'n':
132             no_limit_files = atoi(arg);
133             break;
134         case 'X':
135             proxy->set_debug_mode(1);
136             debug = 1;
137             break;
138         case 'p':
139             if (!pid_fname)
140                 pid_fname = xstrdup(arg);
141             break;
142         case 'u':
143             if (!uid)
144                 uid = xstrdup(arg);
145             break;
146         default:
147             usage(prog);
148             return 1;
149         }
150     }
151     if (addr)
152     {
153         if (proxy->server(addr))
154         {
155             yaz_log(YLOG_FATAL|YLOG_ERRNO, "listen %s", addr);
156             exit(1);
157         }
158     }
159     else
160     {
161         usage(prog);
162         return 1;
163     }
164     return 0;
165 }
166
167 static Yaz_Proxy *static_yaz_proxy = 0;
168 static void sighup_handler(int num)
169 {
170 #if WIN32
171 #else
172     signal(SIGHUP, sighup_handler);
173 #endif
174     if (static_yaz_proxy)
175         static_yaz_proxy->reconfig();
176 }
177
178 #if HAVE_XSLT
179 static void proxy_xml_error_handler(void *ctx, const char *fmt, ...)
180 {
181     char buf[1024];
182
183     va_list ap;
184     va_start(ap, fmt);
185
186 #ifdef WIN32
187     vsprintf(buf, fmt, ap);
188 #else
189     vsnprintf(buf, sizeof(buf), fmt, ap);
190 #endif
191     yaz_log(YLOG_WARN, "%s: %s", (char*) ctx, buf);
192
193     va_end (ap);
194 }
195 #endif
196
197 static void child_run(Yaz_SocketManager *m, int run)
198 {
199 #ifdef WIN32
200 #else
201     signal(SIGHUP, sighup_handler);
202 #endif
203
204 #if HAVE_XSLT
205     xmlSetGenericErrorFunc((void *) "XML", proxy_xml_error_handler);
206     xsltSetGenericErrorFunc((void *) "XSLT", proxy_xml_error_handler);
207 #endif
208 #ifdef WIN32
209 #else
210     yaz_log(YLOG_LOG, "0 proxy run=%d pid=%ld", run, (long) getpid());
211 #endif
212     if (no_limit_files)
213     {
214 #if HAVE_SETRLIMIT
215         struct rlimit limit_data;
216         limit_data.rlim_cur = no_limit_files;
217         limit_data.rlim_max = no_limit_files;
218         
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");
223 #else
224         yaz_log(YLOG_WARN, "setrlimit unavablable. Option -n ignored");
225 #endif
226     }
227 #ifdef WIN32
228 #else
229     if (pid_fname)
230     {
231         FILE *f = fopen(pid_fname, "w");
232         if (!f)
233         {
234             yaz_log(YLOG_ERRNO|YLOG_FATAL, "Couldn't create %s", pid_fname);
235             exit(0);
236         }
237         fprintf(f, "%ld", (long) getpid());
238         fclose(f);
239         xfree(pid_fname);
240     }
241     if (uid)
242     {
243         struct passwd *pw;
244
245         if (!(pw = getpwnam(uid)))
246         {
247             yaz_log(YLOG_FATAL, "%s: Unknown user", uid);
248             exit(3);
249         }
250         if (log_file)
251         {
252             chown(log_file, pw->pw_uid,  pw->pw_gid);
253             xfree(log_file);
254         }
255         if (setuid(pw->pw_uid) < 0)
256         {
257             yaz_log(YLOG_FATAL|YLOG_ERRNO, "setuid");
258             exit(4);
259         }
260         xfree(uid);
261     }
262 #endif
263 #if HAVE_GETRLIMIT
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);
268 #endif
269     
270     while (m->processEvent() > 0)
271         ;
272
273     exit (0);
274 }
275
276 int main(int argc, char **argv)
277 {
278 #if HAVE_XSLT
279     xmlInitMemory();
280     
281     LIBXML_TEST_VERSION
282 #endif
283     int cont = 1;
284     int run = 1;
285     Yaz_SocketManager mySocketManager;
286     Yaz_Proxy proxy(new Yaz_PDU_Assoc(&mySocketManager));
287
288     static_yaz_proxy = &proxy;
289
290     args(&proxy, argc, argv);
291
292 #ifdef WIN32
293     child_run(&mySocketManager, run);
294 #else
295     if (debug)
296     {
297         child_run(&mySocketManager, run);
298         exit(0);
299     }
300     while (cont)
301     {
302         pid_t p = fork();
303         if (p == (pid_t) -1)
304         {
305             yaz_log(YLOG_FATAL|YLOG_ERRNO, "fork");
306             exit(1);
307         }
308         else if (p == 0)
309         {
310             child_run(&mySocketManager, run);
311         }
312         pid_t p1;
313         int status;
314         p1 = wait(&status);
315
316         yaz_log_reopen();
317
318         if (p1 != p)
319         {
320             yaz_log(YLOG_FATAL, "p1=%d != p=%d", p1, p);
321             exit(1);
322         }
323         if (WIFSIGNALED(status))
324         {
325             switch(WTERMSIG(status)) {
326             case SIGILL:
327                 yaz_log(YLOG_WARN, "Received SIGILL from child %ld", (long) p);
328                 cont = 1;
329                 break;
330             case SIGABRT:
331                 yaz_log(YLOG_WARN, "Received SIGABRT from child %ld", (long) p);
332                 cont = 1;
333                 break ;
334             case SIGSEGV:
335                 yaz_log(YLOG_WARN, "Received SIGSEGV from child %ld", (long) p);
336                 cont = 1;
337                 break;
338             case SIGBUS:        
339                 yaz_log(YLOG_WARN, "Received SIGBUS from child %ld", (long) p);
340                 cont = 1;
341                 break;
342             case SIGTERM:
343                 yaz_log(YLOG_LOG, "Received SIGTERM from child %ld",
344                         (long) p);
345                 cont = 0;
346                 break;
347             default:
348                 yaz_log(YLOG_WARN, "Received SIG %d from child %ld",
349                         WTERMSIG(status), (long) p);
350                 cont = 0;
351             }
352         }
353         else if (status == 0)
354             cont = 0;
355         else
356         {
357             yaz_log(YLOG_LOG, "Exit %d from child %ld", status, (long) p);
358             cont = 1;
359         }
360         if (cont)
361             sleep(1 + run/5);
362         run++;
363     }
364 #endif
365     exit (0);
366     return 0;
367 }