2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.15 1995-03-31 10:16:51 quinn
10 * Revision 1.14 1995/03/31 09:18:58 quinn
13 * Revision 1.13 1995/03/30 16:08:39 quinn
16 * Revision 1.12 1995/03/30 13:29:02 quinn
19 * Revision 1.11 1995/03/30 12:18:17 quinn
22 * Revision 1.10 1995/03/29 15:40:16 quinn
23 * Ongoing work. Statserv is now dynamic by default
25 * Revision 1.9 1995/03/27 08:34:30 quinn
26 * Added dynamic server functionality.
27 * Released bindings to session.c (is now redundant)
29 * Revision 1.8 1995/03/20 09:46:26 quinn
32 * Revision 1.7 1995/03/16 13:29:04 quinn
35 * Revision 1.6 1995/03/15 15:18:52 quinn
36 * Little changes to better support nonblocking I/O
39 * Revision 1.5 1995/03/15 08:37:45 quinn
40 * Now we're pretty much set for nonblocking I/O.
42 * Revision 1.4 1995/03/14 16:59:48 quinn
45 * Revision 1.3 1995/03/14 11:30:15 quinn
48 * Revision 1.2 1995/03/14 10:28:03 quinn
49 * More work on demo server.
51 * Revision 1.1 1995/03/10 18:22:45 quinn
52 * The rudiments of an asynchronous server.
57 * Simple, static server. I wouldn't advise a static server unless you
58 * really have to, but it's great for debugging memory management. :)
80 static char *me = "statserver";
81 static int dynamic = 1; /* fork on incoming connection */
82 static int loglevel = LOG_DEFAULT_LEVEL;
84 #define DEFAULT_LISTENER "tcp:localhost:9999"
87 * handle incoming connect requests.
88 * The dynamic mode is a bit tricky mostly because we want to avoid
89 * doing all of the listening and accepting in the parent - it's
92 static void listener(IOCHAN h, int event)
94 COMSTACK line = (COMSTACK) iochan_getdata(h);
100 if (event == EVENT_INPUT)
102 if (dynamic && !child)
108 logf(LOG_FATAL|LOG_ERRNO, "pipe");
111 if ((res = fork()) < 0)
113 logf(LOG_FATAL|LOG_ERRNO, "fork");
116 else if (res == 0) /* child */
122 sprintf(nbuf, "%s(%d)", me, getpid());
123 log_init(loglevel, nbuf, 0);
128 /* wait for child to take the call */
134 if ((res = read(hand[0], dummy, 1)) < 0 && errno != EINTR)
136 logf(LOG_FATAL|LOG_ERRNO, "handshake read");
142 logf(LOG_DEBUG, "P: Child has taken the call");
147 if ((res = cs_listen(line, 0, 0)) < 0)
149 logf(LOG_FATAL, "cs_listen failed.");
154 logf(LOG_DEBUG, "listen ok");
155 iochan_setevent(h, EVENT_OUTPUT);
156 iochan_setflags(h, EVENT_OUTPUT | EVENT_EXCEPT); /* set up for acpt */
158 /* in dynamic mode, only the child ever comes down here */
159 else if (event == EVENT_OUTPUT)
164 if (!(new_line = cs_accept(line)))
166 logf(LOG_FATAL, "Accept failed.");
167 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
170 logf(LOG_DEBUG, "accept ok");
174 /* close our half of the listener sockets */
175 for (pp = iochan_getchan(); pp; pp = iochan_getnext(pp))
177 COMSTACK l = iochan_getdata(pp);
182 logf(LOG_DEBUG, "Releasing parent");
186 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
188 if (!(new_chan = iochan_create(cs_fileno(new_line), ir_session,
191 logf(LOG_FATAL, "Failed to create iochan");
194 if (!(newas = create_association(new_chan, new_line)))
196 logf(LOG_FATAL, "Failed to create new assoc.");
199 iochan_setdata(new_chan, newas);
200 logf(LOG_LOG, "accepted connection");
204 logf(LOG_FATAL, "Bad event on listener.");
210 * Set up a listening endpoint, and give it to the event-handler.
212 static void add_listener(char *where, int what)
216 char mode[100], addr[100];
220 if (!where || sscanf(where, "%[^:]:%s", mode, addr) != 2)
222 fprintf(stderr, "%s: Address format: ('tcp'|'osi')':'<address>.\n",
226 if (!strcmp(mode, "tcp"))
228 if (!(ap = tcpip_strtoaddr(addr)))
230 fprintf(stderr, "Address resolution failed for TCP.\n");
236 else if (!strcmp(mode, "osi"))
238 if (!(ap = mosi_strtoaddr(addr)))
240 fprintf(stderr, "Address resolution failed for TCP.\n");
248 fprintf(stderr, "You must specify either 'osi:' or 'tcp:'.\n");
251 logf(LOG_LOG, "Adding %s %s listener on %s",
252 dynamic ? "dynamic" : "static",
253 what == PROTO_SR ? "SR" : "Z3950", where);
254 if (!(l = cs_create(type, 0, what)))
256 logf(LOG_FATAL|LOG_ERRNO, "Failed to create listener");
259 if (cs_bind(l, ap, CS_SERVER) < 0)
261 logf(LOG_FATAL|LOG_ERRNO, "Failed to bind to %s", where);
264 if (!(lst = iochan_create(cs_fileno(l), listener, EVENT_INPUT |
267 logf(LOG_FATAL|LOG_ERRNO, "Failed to create IOCHAN-type");
270 iochan_setdata(lst, l);
273 static void catchchld(int num)
275 while (waitpid(-1, 0, WNOHANG) > 0);
276 signal(SIGCHLD, catchchld);
279 int statserv_main(int argc, char **argv)
281 int ret, listeners = 0;
283 int protocol = CS_Z3950;
287 while ((ret = options("szSl:v:", argv, argc, &arg)) != -2)
291 add_listener(arg, protocol);
294 case 'z': protocol = CS_Z3950; break;
295 case 's': protocol = CS_SR; break;
296 case 'S': dynamic = 0; break;
299 log_init(loglevel, me, logfile);
302 loglevel = log_mask_str(arg);
303 log_init(loglevel, me, logfile);
306 fprintf(stderr, "Usage: %s [ -v <loglevel> -l <logfile> -zsS <listener-addr> ... ]\n", me);
310 signal(SIGCHLD, catchchld);
312 add_listener(DEFAULT_LISTENER, protocol);
313 logf(LOG_LOG, "Entering event loop.");