Added state-handle and some support for asynchronous activities.
[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.10  1995-03-29 15:40:16  quinn
8  * Ongoing work. Statserv is now dynamic by default
9  *
10  * Revision 1.9  1995/03/27  08:34:30  quinn
11  * Added dynamic server functionality.
12  * Released bindings to session.c (is now redundant)
13  *
14  * Revision 1.8  1995/03/20  09:46:26  quinn
15  * Added osi support.
16  *
17  * Revision 1.7  1995/03/16  13:29:04  quinn
18  * Partitioned server.
19  *
20  * Revision 1.6  1995/03/15  15:18:52  quinn
21  * Little changes to better support nonblocking I/O
22  * Added backend.h
23  *
24  * Revision 1.5  1995/03/15  08:37:45  quinn
25  * Now we're pretty much set for nonblocking I/O.
26  *
27  * Revision 1.4  1995/03/14  16:59:48  quinn
28  * Bug-fixes
29  *
30  * Revision 1.3  1995/03/14  11:30:15  quinn
31  * Works better now.
32  *
33  * Revision 1.2  1995/03/14  10:28:03  quinn
34  * More work on demo server.
35  *
36  * Revision 1.1  1995/03/10  18:22:45  quinn
37  * The rudiments of an asynchronous server.
38  *
39  */
40
41 /*
42  * Simple, static server. I wouldn't advise a static server unless you
43  * really have to, but it's great for debugging memory management.  :)
44  */
45
46 #include <stdio.h>
47 #include <unistd.h>
48 #include <fcntl.h>
49 #include <sys/wait.h>
50 #include <signal.h>
51 #include <errno.h>
52
53 #include <options.h>
54 #include <eventl.h>
55 #include <session.h>
56 #include <eventl.h>
57 #include <comstack.h>
58 #include <tcpip.h>
59 #include <xmosi.h>
60 #include <dmalloc.h>
61
62 static char *me = "";
63 static int dynamic = 1;   /* fork on incoming connection */
64
65 #define DEFAULT_LISTENER "tcp:localhost:9999"
66
67 /*
68  * handle incoming connect requests.
69  * The dynamic mode is a bit tricky mostly because we want to avoid
70  * doing all of the listening and accepting in the parent - it's
71  * safer that way.
72  */
73 static void listener(IOCHAN h, int event)
74 {
75     COMSTACK line = (COMSTACK) iochan_getdata(h);
76     association *newas;
77     static int hand[2];
78     static int child = 0;
79     int res;
80
81     if (event == EVENT_INPUT)
82     {
83         if (dynamic && !child) 
84         {
85             int res;
86
87             if (pipe(hand) < 0)
88             {
89                 perror("pipe");
90                 exit(1);
91             }
92             if ((res = fork()) < 0)
93             {
94                 perror("fork");
95                 exit(1);
96             }
97             else if (res == 0) /* child */
98             {
99                 close(hand[0]);
100                 child = 1;
101             }
102             else /* parent */
103             {
104                 close(hand[1]);
105                 /* wait for child to take the call */
106                 for (;;)
107                 {
108                     char dummy[1];
109                     int res;
110                     
111                     if ((res = read(hand[0], dummy, 1)) < 0 && errno != EINTR)
112                     {
113                         perror("handshake read");
114                         exit(1);
115                     }
116                     else if (res >= 0)
117                         break;
118                 }
119                 fprintf(stderr, "P: Child has taken the call\n");
120                 close(hand[0]);
121                 return;
122             }
123         }
124         if ((res = cs_listen(line, 0, 0)) < 0)
125         {
126             fprintf(stderr, "cs_listen failed.\n");
127             return;
128         }
129         else if (res == 1)
130             return;
131         iochan_setevent(h, EVENT_OUTPUT);
132         iochan_setflags(h, EVENT_OUTPUT | EVENT_EXCEPT); /* set up for acpt */
133     }
134     /* in dynamic mode, only the child ever comes down here */
135     else if (event == EVENT_OUTPUT)
136     {
137         COMSTACK new_line;
138         IOCHAN new_chan;
139
140         if (!(new_line = cs_accept(line)))
141         {
142             fprintf(stderr, "Accept failed.\n");
143             iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
144             return;
145         }
146         if (dynamic)
147         {
148             IOCHAN pp;
149             /* close our half of the listener sockets */
150             for (pp = iochan_getchan(); pp; pp = iochan_getnext(pp))
151             {
152                 COMSTACK l = iochan_getdata(pp);
153                 cs_close(l);
154                 iochan_destroy(pp);
155             }
156             /* release dad */
157             fprintf(stderr, "Releasing parent\n");
158             close(hand[1]);
159             fprintf(stderr, "New fd is %d\n", cs_fileno(new_line));
160         }
161         else
162             iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
163
164         if (!(new_chan = iochan_create(cs_fileno(new_line), ir_session,
165             EVENT_INPUT)))
166         {
167             fprintf(stderr, "Failed to create iochan\n");
168             exit(1);
169         }
170         if (!(newas = create_association(new_chan, new_line)))
171         {
172             fprintf(stderr, "Failed to create new assoc.\n");
173             exit(1);
174         }
175         iochan_setdata(new_chan, newas);
176     }
177     else
178     {
179         fprintf(stderr, "Bad event on listener.\n");
180         exit(1);
181     }
182 }
183
184 /*
185  * Set up a listening endpoint, and give it to the event-handler.
186  */
187 static void add_listener(char *where, int what)
188 {
189     COMSTACK l;
190     CS_TYPE type;
191     char mode[100], addr[100];
192     void *ap;
193     IOCHAN lst;
194
195     fprintf(stderr, "Adding %s %s listener on %s\n",
196         dynamic ? "dynamic" : "static",
197         what == PROTO_SR ? "SR" : "Z3950", where);
198     if (!where || sscanf(where, "%[^:]:%s", mode, addr) != 2)
199     {
200         fprintf(stderr, "%s: Address format: ('tcp'|'osi')':'<address>.\n",
201             me);
202         exit(1);
203     }
204     if (!strcmp(mode, "tcp"))
205     {
206         if (!(ap = tcpip_strtoaddr(addr)))
207         {
208             fprintf(stderr, "Address resolution failed for TCP.\n");
209             exit(1);
210         }
211         type = tcpip_type;
212     }
213     else if (!strcmp(mode, "osi"))
214     {
215         if (!(ap = mosi_strtoaddr(addr)))
216         {
217             fprintf(stderr, "Address resolution failed for TCP.\n");
218             exit(1);
219         }
220         type = mosi_type;
221     }
222     else
223     {
224         fprintf(stderr, "You must specify either 'osi:' or 'tcp:'.\n");
225         exit(1);
226     }
227     if (!(l = cs_create(type, 0, what)))
228     {
229         fprintf(stderr, "Failed to create listener\n");
230         exit(1);
231     }
232     if (cs_bind(l, ap, CS_SERVER) < 0)
233     {
234         fprintf(stderr, "Failed to bind.\n");
235         perror(where);
236         exit(1);
237     }
238     if (!(lst = iochan_create(cs_fileno(l), listener, EVENT_INPUT |
239          EVENT_EXCEPT)))
240     {
241         fprintf(stderr, "Failed to create IOCHAN-type\n");
242         exit(1);
243     }
244     iochan_setdata(lst, l);
245 }
246
247 static void catchchld(int num)
248 {
249     while (waitpid(-1, 0, WNOHANG) > 0);
250     signal(SIGCHLD, catchchld);
251 }
252
253 int statserv_main(int argc, char **argv)
254 {
255     int ret, listeners = 0;
256     char *arg;
257     int protocol = CS_Z3950;;
258
259     me = argv[0];
260     while ((ret = options("szSl:", argv, argc, &arg)) != -2)
261         switch (ret)
262         {
263             case 0:
264                 add_listener(arg, protocol);
265                 listeners++;
266                 break;
267             case 'z': protocol = CS_Z3950; break;
268             case 's': protocol = CS_SR; break;
269             case 'S': dynamic = 0; break;
270             default:
271                 fprintf(stderr, "Usage: %s [ -zsS <listener-addr> ... ]\n", me);
272                 exit(1);
273         }
274     if (dynamic)
275         signal(SIGCHLD, catchchld);
276     if (!listeners)
277         add_listener(DEFAULT_LISTENER, protocol);
278     return event_loop();
279 }