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