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