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