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