2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 /* selserver.c tests the IOMGR_Select interface. Specifically it verifies
11 * that the read/write/exception lists work correctly with file descriptors
12 * larger than 31. Generally, the calls to IOMGR_Select pass in all the
13 * read, write and exception fd_sets. When handling is complete, the file
14 * descriptor is cleared from the set. Then if fd_set is checked to make sure
15 * there are no other bits set. The fd_sets which should have had nothing set
18 * The client can send one of the following:
19 * -end - shoots the selserver.
20 * -delay n - The selserver thread handling this request should sleep for
21 * n seconds before accepting data. Used in conjuction with -write
22 * so that the write STREAM can fill. This tests the IOMGR_Select
24 * -write n - writes n bytes to the selserver. If -delay is not set, a default
25 * of 5 seconds is used.
26 * -soob - Send an out-of-band message to selserver. Tests the exception
30 /* Typical test scanario:
31 * selclient -soob : used to test exception fd's on selserver.
32 * selclient -delay 20 -write 150000 : used to test read/write fd's.
33 * Adjust delay to 40 seconds if running loopback. Times and sizes derived
34 * on IRIX 6.2 and 6.4.
37 #include <afsconfig.h>
38 #include <afs/param.h>
46 #include <sys/select.h>
47 #include <sys/types.h>
48 #include <sys/socket.h>
51 #include <netinet/in.h>
54 #include <sys/ioctl.h>
62 extern int IOMGR_Select(int, fd_set *, fd_set *, fd_set *, struct timeval *);
65 void sendTest(int, int, int);
66 void handleRequest(char *);
77 struct sockaddr_in ch_addr;
78 fd_set ch_read, ch_write, ch_except;
82 void handleWrite(clientHandle_t *, selcmd_t *);
84 clientHandle_t clientHandles[MAX_THREADS];
90 for (i = 0; i < MAX_THREADS; i++) {
91 if (clientHandles[i].ch_state == CH_FREE) {
92 clientHandles[i].ch_state = CH_INUSE;
94 return &clientHandles[i];
97 Die(1, "No free client handles!\n");
99 return NULL; /* quiet compiler. */
112 printf("Usage: selserver [-fd n] [-syssel] port\n");
113 printf("\t-fd n\tUse file descriptor n for socket.\n");
119 main(int ac, char **av)
123 short port = -1; /* host order. */
125 struct sockaddr_in saddr;
127 clientHandle_t *clientHandle;
131 fd_set *rfds, *wfds, *efds;
141 signal(SIGIO, sigIO);
144 for (i = 1; i < ac; i++) {
145 if (!strcmp("-fd", av[i])) {
147 printf("Missing number for -fd option.\n");
152 printf("%d: file descriptor must be at least 3.\n", setFD);
159 printf("%s: port must be at least 1\n", av[i]);
163 printf("%s: Unknown argument.\n", av[i]);
169 printf("Missing port.\n");
175 printf("Using default socket of %d.\n", setFD);
182 /* Setup server processes */
183 for (i = 0; i < MAX_THREADS; i++) {
184 if (LWP_CreateProcess
185 (handleRequest, 32768, LWP_NORMAL_PRIORITY,
186 (char *)&clientHandles[i], "HandleRequestThread", &pid) < 0) {
187 printf("%s: Failed to start all LWP's\n", program);
190 clientHandles[i].ch_pid = pid;
194 sockFD = socket(AF_INET, SOCK_STREAM, 0);
199 Log("Using socket at file descriptor %d.\n", sockFD);
201 if (setsockopt(sockFD, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on))
203 perror("setsockopt: ");
207 memset((void *)&saddr, 0, sizeof(saddr));
209 saddr.sin_family = AF_INET;
210 saddr.sin_port = ntohs(port);
211 saddr.sin_addr.s_addr = htonl(INADDR_ANY);
213 if (bind(sockFD, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) {
219 rfds = IOMGR_AllocFDSet();
220 wfds = IOMGR_AllocFDSet();
221 efds = IOMGR_AllocFDSet();
222 if (!rfds || !wfds || !efds) {
223 printf("main: Could not alloc fd_set's.\n");
234 FD_SET(sockFD, rfds);
235 FD_SET(sockFD, efds);
237 Log("Main - going to select.\n");
239 IOMGR_Select(sockFD + 1, rfds, wfds, efds, (struct timeval *)0);
241 case 0: /* Timer, can't happen here. */
244 Log("Oops! select returns %d!\n", code);
247 if (FD_ISSET(sockFD, efds)) {
249 assertNullFDSet(sockFD, rfds);
250 assertNullFDSet(-1, wfds);
251 assertNullFDSet(sockFD, efds);
253 if (FD_ISSET(sockFD, rfds)) {
254 while (nThreads > MAX_THREADS) {
258 clientHandle = getClientHandle();
260 addr_len = sizeof(clientHandle->ch_addr);
261 clientHandle->ch_fd = accept(sockFD, (struct sockaddr *)
262 &clientHandle->ch_addr,
264 if (clientHandle->ch_fd < 0) {
269 Log("Main - signalling LWP 0x%x\n", &clientHandle->ch_state);
270 LWP_NoYieldSignal(&clientHandle->ch_state);
271 assertNullFDSet(sockFD, rfds);
272 assertNullFDSet(-1, wfds);
273 assertNullFDSet(-1, efds);
276 Die(1, "(main) No data to read.\n");
282 handleRequest(char *arg)
284 clientHandle_t *ch = (clientHandle_t *) arg;
291 Log("(handleRequest) going to sleep on 0x%x\n", &ch->ch_state);
292 LWP_WaitProcess(&ch->ch_state);
293 assert(ch->ch_state == CH_INUSE);
295 FD_ZERO(&(ch->ch_read));
296 FD_ZERO(&(ch->ch_write));
297 FD_ZERO(&(ch->ch_except));
298 FD_SET(ch->ch_fd, &(ch->ch_read));
299 FD_SET(ch->ch_fd, &(ch->ch_except));
301 IOMGR_Select(ch->ch_fd + 1, &(ch->ch_read), &(ch->ch_write),
302 &(ch->ch_except), NULL);
303 if (FD_ISSET(ch->ch_fd, &(ch->ch_except))) {
304 Log("Received expception. Read fd_set shows %d\n",
305 FD_ISSET(ch->ch_fd, &(ch->ch_read)));
306 assertNullFDSet(ch->ch_fd, &(ch->ch_read));
307 assertNullFDSet(-1, &(ch->ch_write));
308 assertNullFDSet(ch->ch_fd, &(ch->ch_except));
313 if (read(ch->ch_fd, (char *)&sc, sizeof(sc)) != sizeof(sc)) {
314 Die(1, "(handleRequest) read command");
317 Log("(handleRequest)cmd=%d\n", sc.sc_cmd);
322 Log("Probed from client at %s\n",
323 inet_ntoa(ch->ch_addr.sin_addr));
329 ch->ch_state = CH_FREE;
333 handleWrite(ch, &sc);
336 Log("Ending ungracefully in server.\n");
339 Log("%d: bad command to handleRequest.\n", sc.sc_cmd);
344 /* We're done now, so we can be re-used. */
348 ch->ch_state = CH_FREE;
354 handleWrite(clientHandle_t * ch, selcmd_t * sc)
357 fd_set *rfds, *wfds, *efds;
364 rfds = IOMGR_AllocFDSet();
365 wfds = IOMGR_AllocFDSet();
366 efds = IOMGR_AllocFDSet();
367 assert(rfds && wfds && efds);
369 if (sc->sc_delay > 0) {
370 IOMGR_Sleep(sc->sc_delay);
373 Log("(handleWrite 0x%x) waking after %d second sleep.\n", ch->ch_pid,
376 if (sc->sc_flags & SC_WAIT_OOB)
379 buf = malloc(sc->sc_info);
385 FD_SET(ch->ch_fd, rfds);
387 FD_SET(ch->ch_fd, efds);
390 IOMGR_Select(ch->ch_fd + 1, rfds, wfds, efds,
391 (struct timeval *)0);
394 if (FD_ISSET(ch->ch_fd, rfds)) {
396 assert(i < sc->sc_info);
398 code = read(ch->ch_fd, &buf[i], 1);
402 Log("code =%d\n", code);
406 /* Test for valid fds */
407 assertNullFDSet(ch->ch_fd, rfds);
408 assertNullFDSet(-1, wfds);
409 assertNullFDSet(-1, efds);
410 if (c == END_DATA || i >= sc->sc_info) {
415 Log("Read %d bytes of data.\n", sc->sc_info);
416 nbytes = write(ch->ch_fd, buf, sc->sc_info);
417 assert(nbytes == sc->sc_info);
418 Log("Wrote data back to client.\n");
419 IOMGR_FreeFDSet(rfds);
420 IOMGR_FreeFDSet(wfds);
421 IOMGR_FreeFDSet(efds);