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