2 * Copyright (c) 1995, Index Data
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.35 1996-05-29 10:03:28 quinn
10 * Revision 1.34 1996/02/21 13:12:07 quinn
11 * *** empty log message ***
13 * Revision 1.33 1996/02/10 12:23:49 quinn
14 * Enable inetd operations fro TCP/IP stack
16 * Revision 1.32 1996/01/19 15:41:52 quinn
17 * *** empty log message ***
19 * Revision 1.31 1995/11/17 11:09:39 adam
20 * Added new option '-c' to specify configuration name in control block.
22 * Revision 1.30 1995/11/01 13:54:59 quinn
25 * Revision 1.29 1995/10/30 12:41:29 quinn
26 * Added hostname lookup for server.
28 * Revision 1.28 1995/09/29 17:12:30 quinn
31 * Revision 1.27 1995/09/27 15:03:02 quinn
32 * Modified function heads & prototypes.
34 * Revision 1.26 1995/08/29 14:44:51 quinn
37 * Revision 1.25 1995/08/29 11:18:02 quinn
38 * Added code to receive close
40 * Revision 1.24 1995/06/16 10:31:39 quinn
41 * Added session timeout.
43 * Revision 1.23 1995/06/15 12:30:48 quinn
46 * Revision 1.22 1995/06/15 07:45:17 quinn
49 * Revision 1.21 1995/06/06 08:15:40 quinn
52 * Revision 1.20 1995/05/29 08:12:09 quinn
55 * Revision 1.19 1995/05/16 09:37:27 quinn
58 * Revision 1.18 1995/05/16 08:51:09 quinn
59 * License, documentation, and memory fixes
61 * Revision 1.17 1995/05/15 11:56:42 quinn
62 * Asynchronous facilities. Restructuring of seshigh code.
64 * Revision 1.16 1995/04/10 10:23:40 quinn
65 * Some work to add scan and other things.
67 * Revision 1.15 1995/03/31 10:16:51 quinn
70 * Revision 1.14 1995/03/31 09:18:58 quinn
73 * Revision 1.13 1995/03/30 16:08:39 quinn
76 * Revision 1.12 1995/03/30 13:29:02 quinn
79 * Revision 1.11 1995/03/30 12:18:17 quinn
82 * Revision 1.10 1995/03/29 15:40:16 quinn
83 * Ongoing work. Statserv is now dynamic by default
85 * Revision 1.9 1995/03/27 08:34:30 quinn
86 * Added dynamic server functionality.
87 * Released bindings to session.c (is now redundant)
89 * Revision 1.8 1995/03/20 09:46:26 quinn
92 * Revision 1.7 1995/03/16 13:29:04 quinn
95 * Revision 1.6 1995/03/15 15:18:52 quinn
96 * Little changes to better support nonblocking I/O
99 * Revision 1.5 1995/03/15 08:37:45 quinn
100 * Now we're pretty much set for nonblocking I/O.
102 * Revision 1.4 1995/03/14 16:59:48 quinn
105 * Revision 1.3 1995/03/14 11:30:15 quinn
108 * Revision 1.2 1995/03/14 10:28:03 quinn
109 * More work on demo server.
111 * Revision 1.1 1995/03/10 18:22:45 quinn
112 * The rudiments of an asynchronous server.
117 * Simple, static server. I wouldn't advise a static server unless you
118 * really have to, but it's great for debugging memory management. :)
124 #include <sys/wait.h>
127 #include <sys/types.h>
129 #include <sys/time.h>
135 #include <comstack.h>
141 #include <statserv.h>
143 static char *me = "statserver";
147 static statserv_options_block control_block = {
148 1, /* dynamic mode */
149 LOG_DEFAULT_LEVEL, /* log level */
151 "", /* diagnostic output to stderr */
152 "tcp:@:9999", /* default listener port */
153 PROTO_Z3950, /* default application protocol */
154 60, /* idle timeout (minutes) */
155 1024*1024, /* maximum PDU size (approx.) to allow */
156 "default-config", /* configuration name to pass to backend */
161 * handle incoming connect requests.
162 * The dynamic mode is a bit tricky mostly because we want to avoid
163 * doing all of the listening and accepting in the parent - it's
166 static void listener(IOCHAN h, int event)
168 COMSTACK line = (COMSTACK) iochan_getdata(h);
171 static int child = 0;
174 if (event == EVENT_INPUT)
176 if (control_block.dynamic && !child)
182 logf(LOG_FATAL|LOG_ERRNO, "pipe");
185 if ((res = fork()) < 0)
187 logf(LOG_FATAL|LOG_ERRNO, "fork");
190 else if (res == 0) /* child */
197 for (pp = iochan_getchan(); pp; pp = iochan_getnext(pp))
201 COMSTACK l = iochan_getdata(pp);
206 sprintf(nbuf, "%s(%d)", me, getpid());
207 log_init(control_block.loglevel, nbuf, 0);
212 /* wait for child to take the call */
218 if ((res = read(hand[0], dummy, 1)) < 0 && errno != EINTR)
220 logf(LOG_FATAL|LOG_ERRNO, "handshake read");
226 logf(LOG_DEBUG, "P: Child has taken the call");
231 if ((res = cs_listen(line, 0, 0)) < 0)
233 logf(LOG_FATAL, "cs_listen failed.");
238 logf(LOG_DEBUG, "listen ok");
239 iochan_setevent(h, EVENT_OUTPUT);
240 iochan_setflags(h, EVENT_OUTPUT | EVENT_EXCEPT); /* set up for acpt */
242 /* in dynamic mode, only the child ever comes down here */
243 else if (event == EVENT_OUTPUT)
249 if (!(new_line = cs_accept(line)))
251 logf(LOG_FATAL, "Accept failed.");
252 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
255 logf(LOG_DEBUG, "accept ok");
256 if (control_block.dynamic)
259 /* close our half of the listener socket */
260 for (pp = iochan_getchan(); pp; pp = iochan_getnext(pp))
262 COMSTACK l = iochan_getdata(pp);
267 logf(LOG_DEBUG, "Releasing parent");
271 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
273 if (!(new_chan = iochan_create(cs_fileno(new_line), ir_session,
276 logf(LOG_FATAL, "Failed to create iochan");
279 if (!(newas = create_association(new_chan, new_line)))
281 logf(LOG_FATAL, "Failed to create new assoc.");
284 iochan_setdata(new_chan, newas);
285 iochan_settimeout(new_chan, control_block.idle_timeout * 60);
286 a = cs_addrstr(new_line);
287 logf(LOG_LOG, "Accepted connection from %s", a ? a : "[Unknown]");
291 logf(LOG_FATAL, "Bad event on listener.");
296 static void inetd_connection(int what)
303 if (!(line = cs_createbysocket(0, tcpip_type, 0, what)))
305 logf(LOG_ERRNO|LOG_FATAL, "Failed to create comstack on socket 0");
308 if (!(chan = iochan_create(cs_fileno(line), ir_session, EVENT_INPUT)))
310 logf(LOG_FATAL, "Failed to create iochan");
313 if (!(assoc = create_association(chan, line)))
315 logf(LOG_FATAL, "Failed to create association structure");
318 iochan_setdata(chan, assoc);
319 iochan_settimeout(chan, control_block.idle_timeout * 60);
320 addr = cs_addrstr(line);
321 logf(LOG_LOG, "Inetd association from %s", addr ? addr : "[UNKNOWN]");
325 * Set up a listening endpoint, and give it to the event-handler.
327 static void add_listener(char *where, int what)
331 char mode[100], addr[100];
335 if (!where || sscanf(where, "%[^:]:%s", mode, addr) != 2)
337 fprintf(stderr, "%s: Address format: ('tcp'|'osi')':'<address>.\n",
341 if (!strcmp(mode, "tcp"))
343 if (!(ap = tcpip_strtoaddr(addr)))
345 fprintf(stderr, "Address resolution failed for TCP.\n");
350 else if (!strcmp(mode, "osi"))
353 if (!(ap = mosi_strtoaddr(addr)))
355 fprintf(stderr, "Address resolution failed for TCP.\n");
360 fprintf(stderr, "OSI Transport not allowed by configuration.\n");
366 fprintf(stderr, "You must specify either 'osi:' or 'tcp:'.\n");
369 logf(LOG_LOG, "Adding %s %s listener on %s",
370 control_block.dynamic ? "dynamic" : "static",
371 what == PROTO_SR ? "SR" : "Z3950", where);
372 if (!(l = cs_create(type, 0, what)))
374 logf(LOG_FATAL|LOG_ERRNO, "Failed to create listener");
377 if (cs_bind(l, ap, CS_SERVER) < 0)
379 logf(LOG_FATAL|LOG_ERRNO, "Failed to bind to %s", where);
382 if (!(lst = iochan_create(cs_fileno(l), listener, EVENT_INPUT |
385 logf(LOG_FATAL|LOG_ERRNO, "Failed to create IOCHAN-type");
388 iochan_setdata(lst, l);
391 static void catchchld(int num)
393 while (waitpid(-1, 0, WNOHANG) > 0)
395 signal(SIGCHLD, catchchld);
398 statserv_options_block *statserv_getcontrol(void)
400 static statserv_options_block cb;
402 memcpy(&cb, &control_block, sizeof(cb));
406 void statserv_setcontrol(statserv_options_block *block)
408 memcpy(&control_block, block, sizeof(*block));
411 int statserv_main(int argc, char **argv)
413 int ret, listeners = 0, inetd = 0, r;
415 int protocol = control_block.default_proto;
418 while ((ret = options("a:iszSl:v:u:c:w:t:k:", argv, argc, &arg)) != -2)
423 add_listener(arg, protocol);
426 case 'z': protocol = PROTO_Z3950; break;
427 case 's': protocol = PROTO_SR; break;
428 case 'S': control_block.dynamic = 0; break;
430 strcpy(control_block.logfile, arg ? arg : "");
431 log_init(control_block.loglevel, me, control_block.logfile);
434 control_block.loglevel = log_mask_str(arg);
435 log_init(control_block.loglevel, me, control_block.logfile);
438 strcpy(control_block.apdufile, arg ? arg : ""); break;
440 strcpy(control_block.setuid, arg ? arg : ""); break;
442 strcpy(control_block.configname, arg ? arg : ""); break;
444 if (!arg || !(r = atoi(arg)))
446 fprintf(stderr, "%s: Specify positive timeout for -t.\n",
450 control_block.idle_timeout = r;
453 if (!arg || !(r = atoi(arg)))
455 fprintf(stderr, "%s: Specify positive timeout for -t.\n",
459 control_block.maxrecordsize = r * 1024;
471 fprintf(stderr, "Usage: %s [ -i -a <pdufile> -v <loglevel>"
472 " -l <logfile> -u <user> -c <config> -t <minutes>"
474 " -zsS <listener-addr> -w <directory> ... ]\n", me);
479 inetd_connection(protocol);
482 if (control_block.dynamic)
483 signal(SIGCHLD, catchchld);
484 if (!listeners && *control_block.default_listen)
485 add_listener(control_block.default_listen, protocol);
487 if (*control_block.setuid)
491 if (!(pw = getpwnam(control_block.setuid)))
493 logf(LOG_FATAL, "%s: Unknown user", control_block.setuid);
496 if (setuid(pw->pw_uid) < 0)
498 logf(LOG_FATAL|LOG_ERRNO, "setuid");
502 logf(LOG_LOG, "Entering event loop.");