Asynchronous facilities. Restructuring of seshigh code.
[yaz-moved-to-github.git] / server / statserv.c
1 /*
2  * Copyright (C) 1994, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: statserv.c,v $
7  * Revision 1.17  1995-05-15 11:56:42  quinn
8  * Asynchronous facilities. Restructuring of seshigh code.
9  *
10  * Revision 1.16  1995/04/10  10:23:40  quinn
11  * Some work to add scan and other things.
12  *
13  * Revision 1.15  1995/03/31  10:16:51  quinn
14  * Fixed logging.
15  *
16  * Revision 1.14  1995/03/31  09:18:58  quinn
17  * Added logging.
18  *
19  * Revision 1.13  1995/03/30  16:08:39  quinn
20  * Little mods.
21  *
22  * Revision 1.12  1995/03/30  13:29:02  quinn
23  * Smallish
24  *
25  * Revision 1.11  1995/03/30  12:18:17  quinn
26  * Fixed bug.
27  *
28  * Revision 1.10  1995/03/29  15:40:16  quinn
29  * Ongoing work. Statserv is now dynamic by default
30  *
31  * Revision 1.9  1995/03/27  08:34:30  quinn
32  * Added dynamic server functionality.
33  * Released bindings to session.c (is now redundant)
34  *
35  * Revision 1.8  1995/03/20  09:46:26  quinn
36  * Added osi support.
37  *
38  * Revision 1.7  1995/03/16  13:29:04  quinn
39  * Partitioned server.
40  *
41  * Revision 1.6  1995/03/15  15:18:52  quinn
42  * Little changes to better support nonblocking I/O
43  * Added backend.h
44  *
45  * Revision 1.5  1995/03/15  08:37:45  quinn
46  * Now we're pretty much set for nonblocking I/O.
47  *
48  * Revision 1.4  1995/03/14  16:59:48  quinn
49  * Bug-fixes
50  *
51  * Revision 1.3  1995/03/14  11:30:15  quinn
52  * Works better now.
53  *
54  * Revision 1.2  1995/03/14  10:28:03  quinn
55  * More work on demo server.
56  *
57  * Revision 1.1  1995/03/10  18:22:45  quinn
58  * The rudiments of an asynchronous server.
59  *
60  */
61
62 /*
63  * Simple, static server. I wouldn't advise a static server unless you
64  * really have to, but it's great for debugging memory management.  :)
65  */
66
67 #include <stdio.h>
68 #include <unistd.h>
69 #include <fcntl.h>
70 #include <sys/wait.h>
71 #include <signal.h>
72 #include <errno.h>
73
74 #include <options.h>
75 #include <eventl.h>
76 #include <session.h>
77 #include <eventl.h>
78 #include <comstack.h>
79 #include <tcpip.h>
80 #ifdef USE_XTIMOSI
81 #include <xmosi.h>
82 #endif
83 #include <dmalloc.h>
84 #include <log.h>
85 #include <statserv.h>
86
87 static char *me = "statserver";
88 /*
89  * default behavior.
90  */
91 static statserv_options_block control_block = {
92     1,                          /* dynamic mode */
93     LOG_DEFAULT_LEVEL,          /* log level */
94     "",                         /* no PDUs */
95     "",                         /* diagnostic output to stderr */
96     "tcp:localhost:9999",       /* default listener port */
97     PROTO_Z3950,                /* application protocol */
98     60,                         /* idle timeout (minutes) */
99     1024*1024*4,                /* maximum PDU size (approx.) to allow */
100     "default-config"            /* configuration name to pass to backend */
101 };
102
103 #define DEFAULT_LISTENER "tcp:localhost:9999"
104
105 /*
106  * handle incoming connect requests.
107  * The dynamic mode is a bit tricky mostly because we want to avoid
108  * doing all of the listening and accepting in the parent - it's
109  * safer that way.
110  */
111 static void listener(IOCHAN h, int event)
112 {
113     COMSTACK line = (COMSTACK) iochan_getdata(h);
114     association *newas;
115     static int hand[2];
116     static int child = 0;
117     int res;
118
119     if (event == EVENT_INPUT)
120     {
121         if (control_block.dynamic && !child) 
122         {
123             int res;
124
125             if (pipe(hand) < 0)
126             {
127                 logf(LOG_FATAL|LOG_ERRNO, "pipe");
128                 exit(1);
129             }
130             if ((res = fork()) < 0)
131             {
132                 logf(LOG_FATAL|LOG_ERRNO, "fork");
133                 exit(1);
134             }
135             else if (res == 0) /* child */
136             {
137                 char nbuf[100];
138
139                 close(hand[0]);
140                 child = 1;
141                 sprintf(nbuf, "%s(%d)", me, getpid());
142                 log_init(control_block.loglevel, nbuf, 0);
143             }
144             else /* parent */
145             {
146                 close(hand[1]);
147                 /* wait for child to take the call */
148                 for (;;)
149                 {
150                     char dummy[1];
151                     int res;
152                     
153                     if ((res = read(hand[0], dummy, 1)) < 0 && errno != EINTR)
154                     {
155                         logf(LOG_FATAL|LOG_ERRNO, "handshake read");
156                         exit(1);
157                     }
158                     else if (res >= 0)
159                         break;
160                 }
161                 logf(LOG_DEBUG, "P: Child has taken the call");
162                 close(hand[0]);
163                 return;
164             }
165         }
166         if ((res = cs_listen(line, 0, 0)) < 0)
167         {
168             logf(LOG_FATAL, "cs_listen failed.");
169             return;
170         }
171         else if (res == 1)
172             return;
173         logf(LOG_DEBUG, "listen ok");
174         iochan_setevent(h, EVENT_OUTPUT);
175         iochan_setflags(h, EVENT_OUTPUT | EVENT_EXCEPT); /* set up for acpt */
176     }
177     /* in dynamic mode, only the child ever comes down here */
178     else if (event == EVENT_OUTPUT)
179     {
180         COMSTACK new_line;
181         IOCHAN new_chan;
182
183         if (!(new_line = cs_accept(line)))
184         {
185             logf(LOG_FATAL, "Accept failed.");
186             iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
187             return;
188         }
189         logf(LOG_DEBUG, "accept ok");
190         if (control_block.dynamic)
191         {
192             IOCHAN pp;
193             /* close our half of the listener sockets */
194             for (pp = iochan_getchan(); pp; pp = iochan_getnext(pp))
195             {
196                 COMSTACK l = iochan_getdata(pp);
197                 cs_close(l);
198                 iochan_destroy(pp);
199             }
200             /* release dad */
201             logf(LOG_DEBUG, "Releasing parent");
202             close(hand[1]);
203         }
204         else
205             iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
206
207         if (!(new_chan = iochan_create(cs_fileno(new_line), ir_session,
208             EVENT_INPUT)))
209         {
210             logf(LOG_FATAL, "Failed to create iochan");
211             exit(1);
212         }
213         if (!(newas = create_association(new_chan, new_line)))
214         {
215             logf(LOG_FATAL, "Failed to create new assoc.");
216             exit(1);
217         }
218         iochan_setdata(new_chan, newas);
219         logf(LOG_LOG, "accepted connection");
220     }
221     else
222     {
223         logf(LOG_FATAL, "Bad event on listener.");
224         exit(1);
225     }
226 }
227
228 /*
229  * Set up a listening endpoint, and give it to the event-handler.
230  */
231 static void add_listener(char *where, int what)
232 {
233     COMSTACK l;
234     CS_TYPE type;
235     char mode[100], addr[100];
236     void *ap;
237     IOCHAN lst;
238
239     if (!where || sscanf(where, "%[^:]:%s", mode, addr) != 2)
240     {
241         fprintf(stderr, "%s: Address format: ('tcp'|'osi')':'<address>.\n",
242             me);
243         exit(1);
244     }
245     if (!strcmp(mode, "tcp"))
246     {
247         if (!(ap = tcpip_strtoaddr(addr)))
248         {
249             fprintf(stderr, "Address resolution failed for TCP.\n");
250             exit(1);
251         }
252         type = tcpip_type;
253     }
254 #ifdef USE_XTIMOSI
255     else if (!strcmp(mode, "osi"))
256     {
257         if (!(ap = mosi_strtoaddr(addr)))
258         {
259             fprintf(stderr, "Address resolution failed for TCP.\n");
260             exit(1);
261         }
262         type = mosi_type;
263     }
264 #endif
265     else
266     {
267         fprintf(stderr, "You must specify either 'osi:' or 'tcp:'.\n");
268         exit(1);
269     }
270     logf(LOG_LOG, "Adding %s %s listener on %s",
271         control_block.dynamic ? "dynamic" : "static",
272         what == PROTO_SR ? "SR" : "Z3950", where);
273     if (!(l = cs_create(type, 0, what)))
274     {
275         logf(LOG_FATAL|LOG_ERRNO, "Failed to create listener");
276         exit(1);
277     }
278     if (cs_bind(l, ap, CS_SERVER) < 0)
279     {
280         logf(LOG_FATAL|LOG_ERRNO, "Failed to bind to %s", where);
281         exit(1);
282     }
283     if (!(lst = iochan_create(cs_fileno(l), listener, EVENT_INPUT |
284          EVENT_EXCEPT)))
285     {
286         logf(LOG_FATAL|LOG_ERRNO, "Failed to create IOCHAN-type");
287         exit(1);
288     }
289     iochan_setdata(lst, l);
290 }
291
292 static void catchchld(int num)
293 {
294     while (waitpid(-1, 0, WNOHANG) > 0);
295     signal(SIGCHLD, catchchld);
296 }
297
298 statserv_options_block *statserv_getcontrol(void)
299 {
300     static statserv_options_block cb;
301
302     memcpy(&cb, &control_block, sizeof(cb));
303     return &cb;
304 }
305
306 void statserv_setcontrol(statserv_options_block *block)
307 {
308     memcpy(&control_block, block, sizeof(*block));
309 }
310
311 int statserv_main(int argc, char **argv)
312 {
313     int ret, listeners = 0;
314     char *arg;
315     int protocol = control_block.default_proto;
316
317     me = argv[0];
318     while ((ret = options("a:szSl:v:", argv, argc, &arg)) != -2)
319         switch (ret)
320         {
321             case 0:
322                 add_listener(arg, protocol);
323                 listeners++;
324                 break;
325             case 'z': protocol = CS_Z3950; break;
326             case 's': protocol = CS_SR; break;
327             case 'S': control_block.dynamic = 0; break;
328             case 'l':
329                 strcpy(control_block.logfile, arg);
330                 log_init(control_block.loglevel, me, control_block.logfile);
331                 break;
332             case 'v':
333                 control_block.loglevel = log_mask_str(arg);
334                 log_init(control_block.loglevel, me, control_block.logfile);
335                 break;
336             case 'a':
337                 strcpy(control_block.apdufile, arg); break;
338             default:
339                 fprintf(stderr, "Usage: %s [ -v <loglevel> -l <logfile> -zsS <listener-addr> ... ]\n", me);
340                 exit(1);
341         }
342     if (control_block.dynamic)
343         signal(SIGCHLD, catchchld);
344     if (!listeners && *control_block.default_listen)
345         add_listener(control_block.default_listen, protocol);
346     logf(LOG_LOG, "Entering event loop.");
347             
348     return event_loop();
349 }