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