2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.12 1995-03-30 13:29:02 quinn
10 * Revision 1.11 1995/03/30 12:18:17 quinn
13 * Revision 1.10 1995/03/29 15:40:16 quinn
14 * Ongoing work. Statserv is now dynamic by default
16 * Revision 1.9 1995/03/27 08:34:30 quinn
17 * Added dynamic server functionality.
18 * Released bindings to session.c (is now redundant)
20 * Revision 1.8 1995/03/20 09:46:26 quinn
23 * Revision 1.7 1995/03/16 13:29:04 quinn
26 * Revision 1.6 1995/03/15 15:18:52 quinn
27 * Little changes to better support nonblocking I/O
30 * Revision 1.5 1995/03/15 08:37:45 quinn
31 * Now we're pretty much set for nonblocking I/O.
33 * Revision 1.4 1995/03/14 16:59:48 quinn
36 * Revision 1.3 1995/03/14 11:30:15 quinn
39 * Revision 1.2 1995/03/14 10:28:03 quinn
40 * More work on demo server.
42 * Revision 1.1 1995/03/10 18:22:45 quinn
43 * The rudiments of an asynchronous server.
48 * Simple, static server. I wouldn't advise a static server unless you
49 * really have to, but it's great for debugging memory management. :)
70 static int dynamic = 1; /* fork on incoming connection */
72 #define DEFAULT_LISTENER "tcp:localhost:9999"
75 * handle incoming connect requests.
76 * The dynamic mode is a bit tricky mostly because we want to avoid
77 * doing all of the listening and accepting in the parent - it's
80 static void listener(IOCHAN h, int event)
82 COMSTACK line = (COMSTACK) iochan_getdata(h);
88 if (event == EVENT_INPUT)
90 if (dynamic && !child)
99 if ((res = fork()) < 0)
104 else if (res == 0) /* child */
112 /* wait for child to take the call */
118 if ((res = read(hand[0], dummy, 1)) < 0 && errno != EINTR)
120 perror("handshake read");
126 fprintf(stderr, "P: Child has taken the call\n");
131 if ((res = cs_listen(line, 0, 0)) < 0)
133 fprintf(stderr, "cs_listen failed.\n");
138 iochan_setevent(h, EVENT_OUTPUT);
139 iochan_setflags(h, EVENT_OUTPUT | EVENT_EXCEPT); /* set up for acpt */
141 /* in dynamic mode, only the child ever comes down here */
142 else if (event == EVENT_OUTPUT)
147 if (!(new_line = cs_accept(line)))
149 fprintf(stderr, "Accept failed.\n");
150 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
156 /* close our half of the listener sockets */
157 for (pp = iochan_getchan(); pp; pp = iochan_getnext(pp))
159 COMSTACK l = iochan_getdata(pp);
164 fprintf(stderr, "Releasing parent\n");
166 fprintf(stderr, "New fd is %d\n", cs_fileno(new_line));
169 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
171 if (!(new_chan = iochan_create(cs_fileno(new_line), ir_session,
174 fprintf(stderr, "Failed to create iochan\n");
177 if (!(newas = create_association(new_chan, new_line)))
179 fprintf(stderr, "Failed to create new assoc.\n");
182 iochan_setdata(new_chan, newas);
186 fprintf(stderr, "Bad event on listener.\n");
192 * Set up a listening endpoint, and give it to the event-handler.
194 static void add_listener(char *where, int what)
198 char mode[100], addr[100];
202 fprintf(stderr, "Adding %s %s listener on %s\n",
203 dynamic ? "dynamic" : "static",
204 what == PROTO_SR ? "SR" : "Z3950", where);
205 if (!where || sscanf(where, "%[^:]:%s", mode, addr) != 2)
207 fprintf(stderr, "%s: Address format: ('tcp'|'osi')':'<address>.\n",
211 if (!strcmp(mode, "tcp"))
213 if (!(ap = tcpip_strtoaddr(addr)))
215 fprintf(stderr, "Address resolution failed for TCP.\n");
221 else if (!strcmp(mode, "osi"))
223 if (!(ap = mosi_strtoaddr(addr)))
225 fprintf(stderr, "Address resolution failed for TCP.\n");
233 fprintf(stderr, "You must specify either 'osi:' or 'tcp:'.\n");
236 if (!(l = cs_create(type, 0, what)))
238 fprintf(stderr, "Failed to create listener\n");
241 if (cs_bind(l, ap, CS_SERVER) < 0)
243 fprintf(stderr, "Failed to bind.\n");
247 if (!(lst = iochan_create(cs_fileno(l), listener, EVENT_INPUT |
250 fprintf(stderr, "Failed to create IOCHAN-type\n");
253 iochan_setdata(lst, l);
256 static void catchchld(int num)
258 while (waitpid(-1, 0, WNOHANG) > 0);
259 signal(SIGCHLD, catchchld);
262 int statserv_main(int argc, char **argv)
264 int ret, listeners = 0;
266 int protocol = CS_Z3950;
268 int loglevel = LOG_DEFAULT_LEVEL;
271 while ((ret = options("szSl:v:", argv, argc, &arg)) != -2)
275 add_listener(arg, protocol);
278 case 'z': protocol = CS_Z3950; break;
279 case 's': protocol = CS_SR; break;
280 case 'S': dynamic = 0; break;
283 log_init(loglevel, me, logfile);
286 loglevel = log_mask_str(arg);
287 log_init(loglevel, me, logfile);
290 fprintf(stderr, "Usage: %s [ -v <loglevel> -l <logfile> -zsS <listener-addr> ... ]\n", me);
294 signal(SIGCHLD, catchchld);
296 add_listener(DEFAULT_LISTENER, protocol);