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