2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.11 1995-03-30 12:18:17 quinn
10 * Revision 1.10 1995/03/29 15:40:16 quinn
11 * Ongoing work. Statserv is now dynamic by default
13 * Revision 1.9 1995/03/27 08:34:30 quinn
14 * Added dynamic server functionality.
15 * Released bindings to session.c (is now redundant)
17 * Revision 1.8 1995/03/20 09:46:26 quinn
20 * Revision 1.7 1995/03/16 13:29:04 quinn
23 * Revision 1.6 1995/03/15 15:18:52 quinn
24 * Little changes to better support nonblocking I/O
27 * Revision 1.5 1995/03/15 08:37:45 quinn
28 * Now we're pretty much set for nonblocking I/O.
30 * Revision 1.4 1995/03/14 16:59:48 quinn
33 * Revision 1.3 1995/03/14 11:30:15 quinn
36 * Revision 1.2 1995/03/14 10:28:03 quinn
37 * More work on demo server.
39 * Revision 1.1 1995/03/10 18:22:45 quinn
40 * The rudiments of an asynchronous server.
45 * Simple, static server. I wouldn't advise a static server unless you
46 * really have to, but it's great for debugging memory management. :)
67 static int dynamic = 1; /* fork on incoming connection */
69 #define DEFAULT_LISTENER "tcp:localhost:9999"
72 * handle incoming connect requests.
73 * The dynamic mode is a bit tricky mostly because we want to avoid
74 * doing all of the listening and accepting in the parent - it's
77 static void listener(IOCHAN h, int event)
79 COMSTACK line = (COMSTACK) iochan_getdata(h);
85 if (event == EVENT_INPUT)
87 if (dynamic && !child)
96 if ((res = fork()) < 0)
101 else if (res == 0) /* child */
109 /* wait for child to take the call */
115 if ((res = read(hand[0], dummy, 1)) < 0 && errno != EINTR)
117 perror("handshake read");
123 fprintf(stderr, "P: Child has taken the call\n");
128 if ((res = cs_listen(line, 0, 0)) < 0)
130 fprintf(stderr, "cs_listen failed.\n");
135 iochan_setevent(h, EVENT_OUTPUT);
136 iochan_setflags(h, EVENT_OUTPUT | EVENT_EXCEPT); /* set up for acpt */
138 /* in dynamic mode, only the child ever comes down here */
139 else if (event == EVENT_OUTPUT)
144 if (!(new_line = cs_accept(line)))
146 fprintf(stderr, "Accept failed.\n");
147 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
153 /* close our half of the listener sockets */
154 for (pp = iochan_getchan(); pp; pp = iochan_getnext(pp))
156 COMSTACK l = iochan_getdata(pp);
161 fprintf(stderr, "Releasing parent\n");
163 fprintf(stderr, "New fd is %d\n", cs_fileno(new_line));
166 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
168 if (!(new_chan = iochan_create(cs_fileno(new_line), ir_session,
171 fprintf(stderr, "Failed to create iochan\n");
174 if (!(newas = create_association(new_chan, new_line)))
176 fprintf(stderr, "Failed to create new assoc.\n");
179 iochan_setdata(new_chan, newas);
183 fprintf(stderr, "Bad event on listener.\n");
189 * Set up a listening endpoint, and give it to the event-handler.
191 static void add_listener(char *where, int what)
195 char mode[100], addr[100];
199 fprintf(stderr, "Adding %s %s listener on %s\n",
200 dynamic ? "dynamic" : "static",
201 what == PROTO_SR ? "SR" : "Z3950", where);
202 if (!where || sscanf(where, "%[^:]:%s", mode, addr) != 2)
204 fprintf(stderr, "%s: Address format: ('tcp'|'osi')':'<address>.\n",
208 if (!strcmp(mode, "tcp"))
210 if (!(ap = tcpip_strtoaddr(addr)))
212 fprintf(stderr, "Address resolution failed for TCP.\n");
217 else if (!strcmp(mode, "osi"))
219 if (!(ap = mosi_strtoaddr(addr)))
221 fprintf(stderr, "Address resolution failed for TCP.\n");
228 fprintf(stderr, "You must specify either 'osi:' or 'tcp:'.\n");
231 if (!(l = cs_create(type, 0, what)))
233 fprintf(stderr, "Failed to create listener\n");
236 if (cs_bind(l, ap, CS_SERVER) < 0)
238 fprintf(stderr, "Failed to bind.\n");
242 if (!(lst = iochan_create(cs_fileno(l), listener, EVENT_INPUT |
245 fprintf(stderr, "Failed to create IOCHAN-type\n");
248 iochan_setdata(lst, l);
251 static void catchchld(int num)
253 while (waitpid(-1, 0, WNOHANG) > 0);
254 signal(SIGCHLD, catchchld);
257 int statserv_main(int argc, char **argv)
259 int ret, listeners = 0;
261 int protocol = CS_Z3950;
263 int loglevel = LOG_DEFAULT_LEVEL;
266 while ((ret = options("szSl:v:", argv, argc, &arg)) != -2)
270 add_listener(arg, protocol);
273 case 'z': protocol = CS_Z3950; break;
274 case 's': protocol = CS_SR; break;
275 case 'S': dynamic = 0; break;
278 log_init(loglevel, me, logfile);
281 loglevel = log_mask_str(arg);
282 log_init(loglevel, me, logfile);
285 fprintf(stderr, "Usage: %s [ -v <loglevel> -l <logfile> -zsS <listener-addr> ... ]\n", me);
289 signal(SIGCHLD, catchchld);
291 add_listener(DEFAULT_LISTENER, protocol);