2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.10 1995-03-29 15:40:16 quinn
8 * Ongoing work. Statserv is now dynamic by default
10 * Revision 1.9 1995/03/27 08:34:30 quinn
11 * Added dynamic server functionality.
12 * Released bindings to session.c (is now redundant)
14 * Revision 1.8 1995/03/20 09:46:26 quinn
17 * Revision 1.7 1995/03/16 13:29:04 quinn
20 * Revision 1.6 1995/03/15 15:18:52 quinn
21 * Little changes to better support nonblocking I/O
24 * Revision 1.5 1995/03/15 08:37:45 quinn
25 * Now we're pretty much set for nonblocking I/O.
27 * Revision 1.4 1995/03/14 16:59:48 quinn
30 * Revision 1.3 1995/03/14 11:30:15 quinn
33 * Revision 1.2 1995/03/14 10:28:03 quinn
34 * More work on demo server.
36 * Revision 1.1 1995/03/10 18:22:45 quinn
37 * The rudiments of an asynchronous server.
42 * Simple, static server. I wouldn't advise a static server unless you
43 * really have to, but it's great for debugging memory management. :)
63 static int dynamic = 1; /* fork on incoming connection */
65 #define DEFAULT_LISTENER "tcp:localhost:9999"
68 * handle incoming connect requests.
69 * The dynamic mode is a bit tricky mostly because we want to avoid
70 * doing all of the listening and accepting in the parent - it's
73 static void listener(IOCHAN h, int event)
75 COMSTACK line = (COMSTACK) iochan_getdata(h);
81 if (event == EVENT_INPUT)
83 if (dynamic && !child)
92 if ((res = fork()) < 0)
97 else if (res == 0) /* child */
105 /* wait for child to take the call */
111 if ((res = read(hand[0], dummy, 1)) < 0 && errno != EINTR)
113 perror("handshake read");
119 fprintf(stderr, "P: Child has taken the call\n");
124 if ((res = cs_listen(line, 0, 0)) < 0)
126 fprintf(stderr, "cs_listen failed.\n");
131 iochan_setevent(h, EVENT_OUTPUT);
132 iochan_setflags(h, EVENT_OUTPUT | EVENT_EXCEPT); /* set up for acpt */
134 /* in dynamic mode, only the child ever comes down here */
135 else if (event == EVENT_OUTPUT)
140 if (!(new_line = cs_accept(line)))
142 fprintf(stderr, "Accept failed.\n");
143 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
149 /* close our half of the listener sockets */
150 for (pp = iochan_getchan(); pp; pp = iochan_getnext(pp))
152 COMSTACK l = iochan_getdata(pp);
157 fprintf(stderr, "Releasing parent\n");
159 fprintf(stderr, "New fd is %d\n", cs_fileno(new_line));
162 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
164 if (!(new_chan = iochan_create(cs_fileno(new_line), ir_session,
167 fprintf(stderr, "Failed to create iochan\n");
170 if (!(newas = create_association(new_chan, new_line)))
172 fprintf(stderr, "Failed to create new assoc.\n");
175 iochan_setdata(new_chan, newas);
179 fprintf(stderr, "Bad event on listener.\n");
185 * Set up a listening endpoint, and give it to the event-handler.
187 static void add_listener(char *where, int what)
191 char mode[100], addr[100];
195 fprintf(stderr, "Adding %s %s listener on %s\n",
196 dynamic ? "dynamic" : "static",
197 what == PROTO_SR ? "SR" : "Z3950", where);
198 if (!where || sscanf(where, "%[^:]:%s", mode, addr) != 2)
200 fprintf(stderr, "%s: Address format: ('tcp'|'osi')':'<address>.\n",
204 if (!strcmp(mode, "tcp"))
206 if (!(ap = tcpip_strtoaddr(addr)))
208 fprintf(stderr, "Address resolution failed for TCP.\n");
213 else if (!strcmp(mode, "osi"))
215 if (!(ap = mosi_strtoaddr(addr)))
217 fprintf(stderr, "Address resolution failed for TCP.\n");
224 fprintf(stderr, "You must specify either 'osi:' or 'tcp:'.\n");
227 if (!(l = cs_create(type, 0, what)))
229 fprintf(stderr, "Failed to create listener\n");
232 if (cs_bind(l, ap, CS_SERVER) < 0)
234 fprintf(stderr, "Failed to bind.\n");
238 if (!(lst = iochan_create(cs_fileno(l), listener, EVENT_INPUT |
241 fprintf(stderr, "Failed to create IOCHAN-type\n");
244 iochan_setdata(lst, l);
247 static void catchchld(int num)
249 while (waitpid(-1, 0, WNOHANG) > 0);
250 signal(SIGCHLD, catchchld);
253 int statserv_main(int argc, char **argv)
255 int ret, listeners = 0;
257 int protocol = CS_Z3950;;
260 while ((ret = options("szSl:", argv, argc, &arg)) != -2)
264 add_listener(arg, protocol);
267 case 'z': protocol = CS_Z3950; break;
268 case 's': protocol = CS_SR; break;
269 case 'S': dynamic = 0; break;
271 fprintf(stderr, "Usage: %s [ -zsS <listener-addr> ... ]\n", me);
275 signal(SIGCHLD, catchchld);
277 add_listener(DEFAULT_LISTENER, protocol);