2 * Copyright (c) 1995, Index Data
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.22 1995-06-15 07:45:17 quinn
10 * Revision 1.21 1995/06/06 08:15:40 quinn
13 * Revision 1.20 1995/05/29 08:12:09 quinn
16 * Revision 1.19 1995/05/16 09:37:27 quinn
19 * Revision 1.18 1995/05/16 08:51:09 quinn
20 * License, documentation, and memory fixes
22 * Revision 1.17 1995/05/15 11:56:42 quinn
23 * Asynchronous facilities. Restructuring of seshigh code.
25 * Revision 1.16 1995/04/10 10:23:40 quinn
26 * Some work to add scan and other things.
28 * Revision 1.15 1995/03/31 10:16:51 quinn
31 * Revision 1.14 1995/03/31 09:18:58 quinn
34 * Revision 1.13 1995/03/30 16:08:39 quinn
37 * Revision 1.12 1995/03/30 13:29:02 quinn
40 * Revision 1.11 1995/03/30 12:18:17 quinn
43 * Revision 1.10 1995/03/29 15:40:16 quinn
44 * Ongoing work. Statserv is now dynamic by default
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)
50 * Revision 1.8 1995/03/20 09:46:26 quinn
53 * Revision 1.7 1995/03/16 13:29:04 quinn
56 * Revision 1.6 1995/03/15 15:18:52 quinn
57 * Little changes to better support nonblocking I/O
60 * Revision 1.5 1995/03/15 08:37:45 quinn
61 * Now we're pretty much set for nonblocking I/O.
63 * Revision 1.4 1995/03/14 16:59:48 quinn
66 * Revision 1.3 1995/03/14 11:30:15 quinn
69 * Revision 1.2 1995/03/14 10:28:03 quinn
70 * More work on demo server.
72 * Revision 1.1 1995/03/10 18:22:45 quinn
73 * The rudiments of an asynchronous server.
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. :)
88 #include <sys/types.h>
102 #include <statserv.h>
104 static char *me = "statserver";
108 static statserv_options_block control_block = {
109 1, /* dynamic mode */
110 LOG_DEFAULT_LEVEL, /* log level */
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 */
121 #define DEFAULT_LISTENER "tcp:localhost:9999"
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
129 static void listener(IOCHAN h, int event)
131 COMSTACK line = (COMSTACK) iochan_getdata(h);
134 static int child = 0;
137 if (event == EVENT_INPUT)
139 if (control_block.dynamic && !child)
145 logf(LOG_FATAL|LOG_ERRNO, "pipe");
148 if ((res = fork()) < 0)
150 logf(LOG_FATAL|LOG_ERRNO, "fork");
153 else if (res == 0) /* child */
159 sprintf(nbuf, "%s(%d)", me, getpid());
160 log_init(control_block.loglevel, nbuf, 0);
165 /* wait for child to take the call */
171 if ((res = read(hand[0], dummy, 1)) < 0 && errno != EINTR)
173 logf(LOG_FATAL|LOG_ERRNO, "handshake read");
179 logf(LOG_DEBUG, "P: Child has taken the call");
184 if ((res = cs_listen(line, 0, 0)) < 0)
186 logf(LOG_FATAL, "cs_listen failed.");
191 logf(LOG_DEBUG, "listen ok");
192 iochan_setevent(h, EVENT_OUTPUT);
193 iochan_setflags(h, EVENT_OUTPUT | EVENT_EXCEPT); /* set up for acpt */
195 /* in dynamic mode, only the child ever comes down here */
196 else if (event == EVENT_OUTPUT)
201 if (!(new_line = cs_accept(line)))
203 logf(LOG_FATAL, "Accept failed.");
204 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
207 logf(LOG_DEBUG, "accept ok");
208 if (control_block.dynamic)
211 /* close our half of the listener sockets */
212 for (pp = iochan_getchan(); pp; pp = iochan_getnext(pp))
214 COMSTACK l = iochan_getdata(pp);
219 logf(LOG_DEBUG, "Releasing parent");
223 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
225 if (!(new_chan = iochan_create(cs_fileno(new_line), ir_session,
228 logf(LOG_FATAL, "Failed to create iochan");
231 if (!(newas = create_association(new_chan, new_line)))
233 logf(LOG_FATAL, "Failed to create new assoc.");
236 iochan_setdata(new_chan, newas);
237 logf(LOG_LOG, "accepted connection");
241 logf(LOG_FATAL, "Bad event on listener.");
247 * Set up a listening endpoint, and give it to the event-handler.
249 static void add_listener(char *where, int what)
253 char mode[100], addr[100];
257 if (!where || sscanf(where, "%[^:]:%s", mode, addr) != 2)
259 fprintf(stderr, "%s: Address format: ('tcp'|'osi')':'<address>.\n",
263 if (!strcmp(mode, "tcp"))
265 if (!(ap = tcpip_strtoaddr(addr)))
267 fprintf(stderr, "Address resolution failed for TCP.\n");
273 else if (!strcmp(mode, "osi"))
275 if (!(ap = mosi_strtoaddr(addr)))
277 fprintf(stderr, "Address resolution failed for TCP.\n");
285 fprintf(stderr, "You must specify either 'osi:' or 'tcp:'.\n");
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)))
293 logf(LOG_FATAL|LOG_ERRNO, "Failed to create listener");
296 if (cs_bind(l, ap, CS_SERVER) < 0)
298 logf(LOG_FATAL|LOG_ERRNO, "Failed to bind to %s", where);
301 if (!(lst = iochan_create(cs_fileno(l), listener, EVENT_INPUT |
304 logf(LOG_FATAL|LOG_ERRNO, "Failed to create IOCHAN-type");
307 iochan_setdata(lst, l);
310 static void catchchld(int num)
312 while (waitpid(-1, 0, WNOHANG) > 0)
314 signal(SIGCHLD, catchchld);
317 statserv_options_block *statserv_getcontrol(void)
319 static statserv_options_block cb;
321 memcpy(&cb, &control_block, sizeof(cb));
325 void statserv_setcontrol(statserv_options_block *block)
327 memcpy(&control_block, block, sizeof(*block));
330 int statserv_main(int argc, char **argv)
332 int ret, listeners = 0;
334 int protocol = control_block.default_proto;
337 while ((ret = options("a:szSl:v:u:", argv, argc, &arg)) != -2)
342 add_listener(arg, protocol);
345 case 'z': protocol = PROTO_Z3950; break;
346 case 's': protocol = PROTO_SR; break;
347 case 'S': control_block.dynamic = 0; break;
349 strcpy(control_block.logfile, arg ? arg : "");
350 log_init(control_block.loglevel, me, control_block.logfile);
353 control_block.loglevel = log_mask_str(arg);
354 log_init(control_block.loglevel, me, control_block.logfile);
357 strcpy(control_block.apdufile, arg ? arg : ""); break;
359 strcpy(control_block.setuid, arg ? arg : ""); break;
361 fprintf(stderr, "Usage: %s [ -a <pdufile> -v <loglevel> -l <logfile> -u <user> -zsS <listener-addr> ... ]\n", me);
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)
373 if (!(pw = getpwnam(control_block.setuid)))
375 logf(LOG_FATAL|LOG_ERRNO, "%s", control_block.setuid);
378 if (setuid(pw->pw_uid) < 0)
380 logf(LOG_FATAL|LOG_ERRNO, "setuid");
384 logf(LOG_LOG, "Entering event loop.");