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