2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.14 1995-03-31 09:18:58 quinn
10 * Revision 1.13 1995/03/30 16:08:39 quinn
13 * Revision 1.12 1995/03/30 13:29:02 quinn
16 * Revision 1.11 1995/03/30 12:18:17 quinn
19 * Revision 1.10 1995/03/29 15:40:16 quinn
20 * Ongoing work. Statserv is now dynamic by default
22 * Revision 1.9 1995/03/27 08:34:30 quinn
23 * Added dynamic server functionality.
24 * Released bindings to session.c (is now redundant)
26 * Revision 1.8 1995/03/20 09:46:26 quinn
29 * Revision 1.7 1995/03/16 13:29:04 quinn
32 * Revision 1.6 1995/03/15 15:18:52 quinn
33 * Little changes to better support nonblocking I/O
36 * Revision 1.5 1995/03/15 08:37:45 quinn
37 * Now we're pretty much set for nonblocking I/O.
39 * Revision 1.4 1995/03/14 16:59:48 quinn
42 * Revision 1.3 1995/03/14 11:30:15 quinn
45 * Revision 1.2 1995/03/14 10:28:03 quinn
46 * More work on demo server.
48 * Revision 1.1 1995/03/10 18:22:45 quinn
49 * The rudiments of an asynchronous server.
54 * Simple, static server. I wouldn't advise a static server unless you
55 * really have to, but it's great for debugging memory management. :)
78 static int dynamic = 1; /* fork on incoming connection */
80 #define DEFAULT_LISTENER "tcp:localhost:9999"
83 * handle incoming connect requests.
84 * The dynamic mode is a bit tricky mostly because we want to avoid
85 * doing all of the listening and accepting in the parent - it's
88 static void listener(IOCHAN h, int event)
90 COMSTACK line = (COMSTACK) iochan_getdata(h);
96 if (event == EVENT_INPUT)
98 if (dynamic && !child)
104 logf(LOG_FATAL|LOG_ERRNO, "pipe");
107 if ((res = fork()) < 0)
109 logf(LOG_FATAL|LOG_ERRNO, "fork");
112 else if (res == 0) /* child */
120 /* wait for child to take the call */
126 if ((res = read(hand[0], dummy, 1)) < 0 && errno != EINTR)
128 logf(LOG_FATAL|LOG_ERRNO, "handshake read");
134 logf(LOG_DEBUG, "P: Child has taken the call");
139 if ((res = cs_listen(line, 0, 0)) < 0)
141 logf(LOG_FATAL, "cs_listen failed.");
146 iochan_setevent(h, EVENT_OUTPUT);
147 iochan_setflags(h, EVENT_OUTPUT | EVENT_EXCEPT); /* set up for acpt */
149 /* in dynamic mode, only the child ever comes down here */
150 else if (event == EVENT_OUTPUT)
155 if (!(new_line = cs_accept(line)))
157 logf(LOG_FATAL, "Accept failed.");
158 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
164 /* close our half of the listener sockets */
165 for (pp = iochan_getchan(); pp; pp = iochan_getnext(pp))
167 COMSTACK l = iochan_getdata(pp);
172 logf(LOG_DEBUG, "Releasing parent");
176 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
178 if (!(new_chan = iochan_create(cs_fileno(new_line), ir_session,
181 logf(LOG_FATAL, "Failed to create iochan");
184 if (!(newas = create_association(new_chan, new_line)))
186 logf(LOG_FATAL, "Failed to create new assoc.");
189 iochan_setdata(new_chan, newas);
193 logf(LOG_FATAL, "Bad event on listener.");
199 * Set up a listening endpoint, and give it to the event-handler.
201 static void add_listener(char *where, int what)
205 char mode[100], addr[100];
209 logf(LOG_LOG, "Adding %s %s listener on %s",
210 dynamic ? "dynamic" : "static",
211 what == PROTO_SR ? "SR" : "Z3950", where);
212 if (!where || sscanf(where, "%[^:]:%s", mode, addr) != 2)
214 fprintf(stderr, "%s: Address format: ('tcp'|'osi')':'<address>.\n",
218 if (!strcmp(mode, "tcp"))
220 if (!(ap = tcpip_strtoaddr(addr)))
222 fprintf(stderr, "Address resolution failed for TCP.\n");
228 else if (!strcmp(mode, "osi"))
230 if (!(ap = mosi_strtoaddr(addr)))
232 fprintf(stderr, "Address resolution failed for TCP.\n");
240 fprintf(stderr, "You must specify either 'osi:' or 'tcp:'.\n");
243 if (!(l = cs_create(type, 0, what)))
245 fprintf(stderr, "Failed to create listener\n");
248 if (cs_bind(l, ap, CS_SERVER) < 0)
250 fprintf(stderr, "Failed to bind.\n");
251 logf(LOG_FATAL|LOG_ERRNO, where);
254 if (!(lst = iochan_create(cs_fileno(l), listener, EVENT_INPUT |
257 logf(LOG_FATAL, "Failed to create IOCHAN-type\n");
260 iochan_setdata(lst, l);
263 static void catchchld(int num)
265 while (waitpid(-1, 0, WNOHANG) > 0);
266 signal(SIGCHLD, catchchld);
269 int statserv_main(int argc, char **argv)
271 int ret, listeners = 0;
273 int protocol = CS_Z3950;
275 int loglevel = LOG_DEFAULT_LEVEL;
278 while ((ret = options("szSl:v:", argv, argc, &arg)) != -2)
282 add_listener(arg, protocol);
285 case 'z': protocol = CS_Z3950; break;
286 case 's': protocol = CS_SR; break;
287 case 'S': dynamic = 0; break;
290 log_init(loglevel, me, logfile);
293 loglevel = log_mask_str(arg);
294 log_init(loglevel, me, logfile);
297 fprintf(stderr, "Usage: %s [ -v <loglevel> -l <logfile> -zsS <listener-addr> ... ]\n", me);
301 signal(SIGCHLD, catchchld);
303 add_listener(DEFAULT_LISTENER, protocol);
304 logf(LOG_LOG, "Entering event loop.");