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