2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.13 1995-03-30 16:08:39 quinn
10 * Revision 1.12 1995/03/30 13:29:02 quinn
13 * Revision 1.11 1995/03/30 12:18:17 quinn
16 * Revision 1.10 1995/03/29 15:40:16 quinn
17 * Ongoing work. Statserv is now dynamic by default
19 * Revision 1.9 1995/03/27 08:34:30 quinn
20 * Added dynamic server functionality.
21 * Released bindings to session.c (is now redundant)
23 * Revision 1.8 1995/03/20 09:46:26 quinn
26 * Revision 1.7 1995/03/16 13:29:04 quinn
29 * Revision 1.6 1995/03/15 15:18:52 quinn
30 * Little changes to better support nonblocking I/O
33 * Revision 1.5 1995/03/15 08:37:45 quinn
34 * Now we're pretty much set for nonblocking I/O.
36 * Revision 1.4 1995/03/14 16:59:48 quinn
39 * Revision 1.3 1995/03/14 11:30:15 quinn
42 * Revision 1.2 1995/03/14 10:28:03 quinn
43 * More work on demo server.
45 * Revision 1.1 1995/03/10 18:22:45 quinn
46 * The rudiments of an asynchronous server.
51 * Simple, static server. I wouldn't advise a static server unless you
52 * really have to, but it's great for debugging memory management. :)
75 static int dynamic = 1; /* fork on incoming connection */
77 #define DEFAULT_LISTENER "tcp:localhost:9999"
80 * handle incoming connect requests.
81 * The dynamic mode is a bit tricky mostly because we want to avoid
82 * doing all of the listening and accepting in the parent - it's
85 static void listener(IOCHAN h, int event)
87 COMSTACK line = (COMSTACK) iochan_getdata(h);
93 if (event == EVENT_INPUT)
95 if (dynamic && !child)
104 if ((res = fork()) < 0)
109 else if (res == 0) /* child */
117 /* wait for child to take the call */
123 if ((res = read(hand[0], dummy, 1)) < 0 && errno != EINTR)
125 perror("handshake read");
131 fprintf(stderr, "P: Child has taken the call\n");
136 if ((res = cs_listen(line, 0, 0)) < 0)
138 fprintf(stderr, "cs_listen failed.\n");
143 iochan_setevent(h, EVENT_OUTPUT);
144 iochan_setflags(h, EVENT_OUTPUT | EVENT_EXCEPT); /* set up for acpt */
146 /* in dynamic mode, only the child ever comes down here */
147 else if (event == EVENT_OUTPUT)
152 if (!(new_line = cs_accept(line)))
154 fprintf(stderr, "Accept failed.\n");
155 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
161 /* close our half of the listener sockets */
162 for (pp = iochan_getchan(); pp; pp = iochan_getnext(pp))
164 COMSTACK l = iochan_getdata(pp);
169 fprintf(stderr, "Releasing parent\n");
171 fprintf(stderr, "New fd is %d\n", cs_fileno(new_line));
174 iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
176 if (!(new_chan = iochan_create(cs_fileno(new_line), ir_session,
179 fprintf(stderr, "Failed to create iochan\n");
182 if (!(newas = create_association(new_chan, new_line)))
184 fprintf(stderr, "Failed to create new assoc.\n");
187 iochan_setdata(new_chan, newas);
191 fprintf(stderr, "Bad event on listener.\n");
197 * Set up a listening endpoint, and give it to the event-handler.
199 static void add_listener(char *where, int what)
203 char mode[100], addr[100];
207 fprintf(stderr, "Adding %s %s listener on %s\n",
208 dynamic ? "dynamic" : "static",
209 what == PROTO_SR ? "SR" : "Z3950", where);
210 if (!where || sscanf(where, "%[^:]:%s", mode, addr) != 2)
212 fprintf(stderr, "%s: Address format: ('tcp'|'osi')':'<address>.\n",
216 if (!strcmp(mode, "tcp"))
218 if (!(ap = tcpip_strtoaddr(addr)))
220 fprintf(stderr, "Address resolution failed for TCP.\n");
226 else if (!strcmp(mode, "osi"))
228 if (!(ap = mosi_strtoaddr(addr)))
230 fprintf(stderr, "Address resolution failed for TCP.\n");
238 fprintf(stderr, "You must specify either 'osi:' or 'tcp:'.\n");
241 if (!(l = cs_create(type, 0, what)))
243 fprintf(stderr, "Failed to create listener\n");
246 if (cs_bind(l, ap, CS_SERVER) < 0)
248 fprintf(stderr, "Failed to bind.\n");
252 if (!(lst = iochan_create(cs_fileno(l), listener, EVENT_INPUT |
255 fprintf(stderr, "Failed to create IOCHAN-type\n");
258 iochan_setdata(lst, l);
261 static void catchchld(int num)
263 while (waitpid(-1, 0, WNOHANG) > 0);
264 signal(SIGCHLD, catchchld);
267 int statserv_main(int argc, char **argv)
269 int ret, listeners = 0;
271 int protocol = CS_Z3950;
273 int loglevel = LOG_DEFAULT_LEVEL;
276 while ((ret = options("szSl:v:", argv, argc, &arg)) != -2)
280 add_listener(arg, protocol);
283 case 'z': protocol = CS_Z3950; break;
284 case 's': protocol = CS_SR; break;
285 case 'S': dynamic = 0; break;
288 log_init(loglevel, me, logfile);
291 loglevel = log_mask_str(arg);
292 log_init(loglevel, me, logfile);
295 fprintf(stderr, "Usage: %s [ -v <loglevel> -l <logfile> -zsS <listener-addr> ... ]\n", me);
299 signal(SIGCHLD, catchchld);
301 add_listener(DEFAULT_LISTENER, protocol);