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