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