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