2 * Copyright (c) 1995, Index Data
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.26 1995-08-29 14:44:51 quinn
10 * Revision 1.25 1995/08/29 11:18:02 quinn
11 * Added code to receive close
13 * Revision 1.24 1995/06/16 10:31:39 quinn
14 * Added session timeout.
16 * Revision 1.23 1995/06/15 12:30:48 quinn
19 * Revision 1.22 1995/06/15 07:45:17 quinn
22 * Revision 1.21 1995/06/06 08:15:40 quinn
25 * Revision 1.20 1995/05/29 08:12:09 quinn
28 * Revision 1.19 1995/05/16 09:37:27 quinn
31 * Revision 1.18 1995/05/16 08:51:09 quinn
32 * License, documentation, and memory fixes
34 * Revision 1.17 1995/05/15 11:56:42 quinn
35 * Asynchronous facilities. Restructuring of seshigh code.
37 * Revision 1.16 1995/04/10 10:23:40 quinn
38 * Some work to add scan and other things.
40 * Revision 1.15 1995/03/31 10:16:51 quinn
43 * Revision 1.14 1995/03/31 09:18:58 quinn
46 * Revision 1.13 1995/03/30 16:08:39 quinn
49 * Revision 1.12 1995/03/30 13:29:02 quinn
52 * Revision 1.11 1995/03/30 12:18:17 quinn
55 * Revision 1.10 1995/03/29 15:40:16 quinn
56 * Ongoing work. Statserv is now dynamic by default
58 * Revision 1.9 1995/03/27 08:34:30 quinn
59 * Added dynamic server functionality.
60 * Released bindings to session.c (is now redundant)
62 * Revision 1.8 1995/03/20 09:46:26 quinn
65 * Revision 1.7 1995/03/16 13:29:04 quinn
68 * Revision 1.6 1995/03/15 15:18:52 quinn
69 * Little changes to better support nonblocking I/O
72 * Revision 1.5 1995/03/15 08:37:45 quinn
73 * Now we're pretty much set for nonblocking I/O.
75 * Revision 1.4 1995/03/14 16:59:48 quinn
78 * Revision 1.3 1995/03/14 11:30:15 quinn
81 * Revision 1.2 1995/03/14 10:28:03 quinn
82 * More work on demo server.
84 * Revision 1.1 1995/03/10 18:22:45 quinn
85 * The rudiments of an asynchronous server.
90 * Simple, static server. I wouldn't advise a static server unless you
91 * really have to, but it's great for debugging memory management. :)
100 #include <sys/types.h>
102 #include <sys/time.h>
108 #include <comstack.h>
115 #include <statserv.h>
117 static char *me = "statserver";
121 static statserv_options_block control_block = {
122 1, /* dynamic mode */
123 LOG_DEFAULT_LEVEL, /* log level */
125 "", /* diagnostic output to stderr */
126 "tcp:@:9999", /* default listener port */
127 PROTO_Z3950, /* default application protocol */
128 2*60, /* idle timeout (minutes) */
129 1024*1024, /* maximum PDU size (approx.) to allow */
130 "default-config", /* configuration name to pass to backend */
134 #define DEFAULT_LISTENER "tcp:localhost:9999"
137 * handle incoming connect requests.
138 * The dynamic mode is a bit tricky mostly because we want to avoid
139 * doing all of the listening and accepting in the parent - it's
142 static void listener(IOCHAN h, int event)
144 COMSTACK line = (COMSTACK) iochan_getdata(h);
147 static int child = 0;
150 if (event == EVENT_INPUT)
152 if (control_block.dynamic && !child)
158 logf(LOG_FATAL|LOG_ERRNO, "pipe");
161 if ((res = fork()) < 0)
163 logf(LOG_FATAL|LOG_ERRNO, "fork");
166 else if (res == 0) /* child */
172 sprintf(nbuf, "%s(%d)", me, getpid());
173 log_init(control_block.loglevel, nbuf, 0);
178 /* wait for child to take the call */
184 if ((res = read(hand[0], dummy, 1)) < 0 && errno != EINTR)
186 logf(LOG_FATAL|LOG_ERRNO, "handshake read");
192 logf(LOG_DEBUG, "P: Child has taken the call");
197 if ((res = cs_listen(line, 0, 0)) < 0)
199 logf(LOG_FATAL, "cs_listen failed.");
204 logf(LOG_DEBUG, "listen ok");
205 iochan_setevent(h, EVENT_OUTPUT);
206 iochan_setflags(h, EVENT_OUTPUT | EVENT_EXCEPT); /* set up for acpt */
208 /* in dynamic mode, only the child ever comes down here */
209 else if (event == EVENT_OUTPUT)
214 if (!(new_line = cs_accept(line)))
216 logf(LOG_FATAL, "Accept failed.");
217 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
220 logf(LOG_DEBUG, "accept ok");
221 if (control_block.dynamic)
224 /* close our half of the listener sockets */
225 for (pp = iochan_getchan(); pp; pp = iochan_getnext(pp))
227 COMSTACK l = iochan_getdata(pp);
232 logf(LOG_DEBUG, "Releasing parent");
236 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
238 if (!(new_chan = iochan_create(cs_fileno(new_line), ir_session,
241 logf(LOG_FATAL, "Failed to create iochan");
244 if (!(newas = create_association(new_chan, new_line)))
246 logf(LOG_FATAL, "Failed to create new assoc.");
249 iochan_setdata(new_chan, newas);
250 iochan_settimeout(new_chan, control_block.idle_timeout * 60);
251 logf(LOG_LOG, "accepted connection");
255 logf(LOG_FATAL, "Bad event on listener.");
261 * Set up a listening endpoint, and give it to the event-handler.
263 static void add_listener(char *where, int what)
267 char mode[100], addr[100];
271 if (!where || sscanf(where, "%[^:]:%s", mode, addr) != 2)
273 fprintf(stderr, "%s: Address format: ('tcp'|'osi')':'<address>.\n",
277 if (!strcmp(mode, "tcp"))
279 if (!(ap = tcpip_strtoaddr(addr)))
281 fprintf(stderr, "Address resolution failed for TCP.\n");
286 else if (!strcmp(mode, "osi"))
289 if (!(ap = mosi_strtoaddr(addr)))
291 fprintf(stderr, "Address resolution failed for TCP.\n");
296 fprintf(stderr, "OSI Transport not allowed by configuration.\n");
302 fprintf(stderr, "You must specify either 'osi:' or 'tcp:'.\n");
305 logf(LOG_LOG, "Adding %s %s listener on %s",
306 control_block.dynamic ? "dynamic" : "static",
307 what == PROTO_SR ? "SR" : "Z3950", where);
308 if (!(l = cs_create(type, 0, what)))
310 logf(LOG_FATAL|LOG_ERRNO, "Failed to create listener");
313 if (cs_bind(l, ap, CS_SERVER) < 0)
315 logf(LOG_FATAL|LOG_ERRNO, "Failed to bind to %s", where);
318 if (!(lst = iochan_create(cs_fileno(l), listener, EVENT_INPUT |
321 logf(LOG_FATAL|LOG_ERRNO, "Failed to create IOCHAN-type");
324 iochan_setdata(lst, l);
327 static void catchchld(int num)
329 while (waitpid(-1, 0, WNOHANG) > 0)
331 signal(SIGCHLD, catchchld);
334 statserv_options_block *statserv_getcontrol(void)
336 static statserv_options_block cb;
338 memcpy(&cb, &control_block, sizeof(cb));
342 void statserv_setcontrol(statserv_options_block *block)
344 memcpy(&control_block, block, sizeof(*block));
347 int statserv_main(int argc, char **argv)
349 int ret, listeners = 0;
351 int protocol = control_block.default_proto;
354 while ((ret = options("a:szSl:v:u:", argv, argc, &arg)) != -2)
359 add_listener(arg, protocol);
362 case 'z': protocol = PROTO_Z3950; break;
363 case 's': protocol = PROTO_SR; break;
364 case 'S': control_block.dynamic = 0; break;
366 strcpy(control_block.logfile, arg ? arg : "");
367 log_init(control_block.loglevel, me, control_block.logfile);
370 control_block.loglevel = log_mask_str(arg);
371 log_init(control_block.loglevel, me, control_block.logfile);
374 strcpy(control_block.apdufile, arg ? arg : ""); break;
376 strcpy(control_block.setuid, arg ? arg : ""); break;
378 fprintf(stderr, "Usage: %s [ -a <pdufile> -v <loglevel> -l <logfile> -u <user> -zsS <listener-addr> ... ]\n", me);
382 if (control_block.dynamic)
383 signal(SIGCHLD, catchchld);
384 if (!listeners && *control_block.default_listen)
385 add_listener(control_block.default_listen, protocol);
386 if (*control_block.setuid)
390 if (!(pw = getpwnam(control_block.setuid)))
392 logf(LOG_FATAL, "%s: Unknown user", control_block.setuid);
395 if (setuid(pw->pw_uid) < 0)
397 logf(LOG_FATAL|LOG_ERRNO, "setuid");
401 logf(LOG_LOG, "Entering event loop.");