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