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