xref: /minix3/minix/tests/common-socket.c (revision 9f81acbc21316bcbade46d78b67a5f2e58d11bb9)
1 #include <assert.h>
2 #include <ctype.h>
3 #include <errno.h>
4 #include <fcntl.h>
5 #include <signal.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <sys/socket.h>
10 #include <sys/stat.h>
11 #include <sys/wait.h>
12 #include <time.h>
13 #include <unistd.h>
14 
15 #include "common.h"
16 #include "common-socket.h"
17 
18 #define ISO8601_FORMAT "%Y-%m-%dT%H:%M:%S"
19 
20 /* timestamps for debug and error logs */
21 static char *get_timestamp(void)
22 {
23 	struct tm *tm;
24 	time_t t;
25 	size_t len;
26 	char *s;
27 
28 	len = sizeof(char) * 32;
29 
30 	t = time(NULL);
31 	if (t == -1) {
32 		return NULL;
33 	}
34 	tm = gmtime(&t);
35 	if (tm == NULL) {
36 		return NULL;
37 	}
38 
39 	s = (char *) malloc(len);
40 	if (!s) {
41 		perror("malloc");
42 		return NULL;
43 	}
44 	memset(s, '\0', len);
45 
46 	strftime(s, len - 1, ISO8601_FORMAT, tm);
47 	return s;
48 }
49 
50 void test_fail_fl(char *msg, char *file, int line)
51 {
52 	char *timestamp;
53 	int e;
54 	e = errno;
55 	timestamp = get_timestamp();
56 	if (errct == 0) fprintf(stderr, "\n");
57 	errno = e;
58 	fprintf(stderr, "[ERROR][%s] (%s Line %d) %s [pid=%d:errno=%d:%s]\n",
59 	    timestamp, file, line, msg, getpid(), errno, strerror(errno));
60 	fflush(stderr);
61 	if (timestamp != NULL) {
62 		free(timestamp);
63 		timestamp = NULL;
64 	}
65 	errno = e;
66 	e(7);
67 }
68 
69 #if DEBUG == 1
70 void debug_fl(char *msg, char *file, int line)
71 {
72 	char *timestamp;
73 	timestamp = get_timestamp();
74 	fprintf(stdout,"[DEBUG][%s] (%s:%d) %s [pid=%d]\n",
75 		timestamp, __FILE__, __LINE__, msg, getpid());
76 	fflush(stdout);
77 	if (timestamp != NULL) {
78 		free(timestamp);
79 		timestamp = NULL;
80 	}
81 }
82 #endif
83 
84 void test_socket(const struct socket_test_info *info)
85 {
86 	struct stat statbuf, statbuf2;
87 	int sd, sd2;
88 	int rc;
89 	int i;
90 
91 	debug("entering test_socket()");
92 
93 	debug("Test socket() with an unsupported address family");
94 
95 	errno = 0;
96 	sd = socket(-1, info->type, 0);
97 	if (!(sd == -1 && errno == EAFNOSUPPORT)) {
98 		test_fail("socket");
99 		if (sd != -1) {
100 			CLOSE(sd);
101 		}
102 	}
103 
104 	debug("Test socket() with all available FDs open by this process");
105 
106 	for (i = 3; i < getdtablesize(); i++) {
107 		rc = open("/dev/null", O_RDONLY);
108 		if (rc == -1) {
109 			test_fail("we couldn't open /dev/null for read");
110 		}
111 	}
112 
113 	errno = 0;
114 	sd = socket(info->domain, info->type, 0);
115 	if (!(sd == -1 && errno == EMFILE)) {
116 		test_fail("socket() call with all fds open should fail");
117 		if (sd != -1) {
118 			CLOSE(sd);
119 		}
120 	}
121 
122 	for (i = 3; i < getdtablesize(); i++) {
123 		CLOSE(i);
124 	}
125 
126 	debug("Test socket() with an mismatched protocol");
127 
128 	errno = 0;
129 	sd = socket(info->domain, info->type, 4);
130 	if (!(sd == -1 && errno == EPROTONOSUPPORT)) {
131 		test_fail("socket() should fail with errno = EPROTONOSUPPORT");
132 		if (sd != -1) {
133 			CLOSE(sd);
134 		}
135 	}
136 
137 	debug("Test socket() success");
138 
139 	/*
140 	 * open 2 sockets at once and *then* close them.
141 	 * This will test that /dev/uds is cloning properly.
142 	 */
143 
144 	SOCKET(sd, info->domain, info->type, 0);
145 	SOCKET(sd2, info->domain, info->type, 0);
146 
147 	rc = fstat(sd, &statbuf);
148 	if (rc == -1) {
149 		test_fail("fstat failed on sd");
150 	}
151 
152 	rc = fstat(sd2, &statbuf2);
153 	if (rc == -1) {
154 		test_fail("fstat failed on sd2");
155 	}
156 
157 
158 	if (statbuf.st_dev == statbuf2.st_dev) {
159 		test_fail("/dev/uds isn't being cloned");
160 	}
161 
162 	CLOSE(sd2);
163 	CLOSE(sd);
164 
165 	debug("leaving test_socket()");
166 }
167 
168 void test_getsockname(const struct socket_test_info *info)
169 {
170 	int sd;
171 	int rc;
172 	int on;
173 	struct sockaddr_storage sock_addr;
174 	socklen_t sock_addr_len;
175 
176 	SOCKET(sd, info->domain, info->type, 0);
177 
178 	on = 1;
179 	(void)setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
180 
181 	rc = bind(sd, info->serveraddr, info->serveraddrlen);
182 	if (rc == -1) {
183 		test_fail("bind() should have worked");
184 	}
185 
186 	debug("Test getsockname() success");
187 
188 	memset(&sock_addr, '\0', sizeof(sock_addr));
189 	sock_addr_len = sizeof(sock_addr);
190 
191 	rc = getsockname(sd, (struct sockaddr *) &sock_addr, &sock_addr_len);
192 	if (rc == -1) {
193 		test_fail("getsockname() should have worked");
194 	}
195 
196 	info->callback_check_sockaddr((struct sockaddr *) &sock_addr,
197 		sock_addr_len, "getsockname", 1);
198 
199 	CLOSE(sd);
200 }
201 
202 void test_bind(const struct socket_test_info *info)
203 {
204 	struct sockaddr_storage sock_addr;
205 	socklen_t sock_addr_len;
206 	int sd;
207 	int sd2;
208 	int rc;
209 	int on;
210 
211 	debug("entering test_bind()");
212 	info->callback_cleanup();
213 
214 	debug("Test bind() success");
215 
216 	SOCKET(sd, info->domain, info->type, 0);
217 
218 	on = 1;
219 	(void)setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
220 
221 	rc = bind(sd, info->serveraddr, info->serveraddrlen);
222 	if (rc == -1) {
223 		test_fail("bind() should have worked");
224 	}
225 
226 	debug("Test getsockname() success");
227 
228 	memset(&sock_addr, '\0', sizeof(sock_addr));
229 	sock_addr_len = sizeof(sock_addr);
230 
231 	rc = getsockname(sd, (struct sockaddr *) &sock_addr, &sock_addr_len);
232 	if (rc == -1) {
233 		test_fail("getsockname() should have worked");
234 	}
235 
236 	info->callback_check_sockaddr((struct sockaddr *) &sock_addr,
237 		sock_addr_len, "getsockname", 1);
238 
239 	debug("Test bind() with a address that has already been bind()'d");
240 
241 	SOCKET(sd2, info->domain, info->type, 0);
242 	errno = 0;
243 	rc = bind(sd2, info->serveraddr, info->serveraddrlen);
244 	if (!((rc == -1) && (errno == EADDRINUSE))) {
245 		test_fail("bind() should have failed with EADDRINUSE");
246 	}
247 	CLOSE(sd2);
248 	CLOSE(sd);
249 	info->callback_cleanup();
250 
251 	debug("Test bind() with a NULL address");
252 
253 	SOCKET(sd, info->domain, info->type, 0);
254 	errno = 0;
255 	rc = bind(sd, (struct sockaddr *) NULL,
256 		sizeof(struct sockaddr_storage));
257 	if (!((rc == -1) && (errno == EFAULT))) {
258 		test_fail("bind() should have failed with EFAULT");
259 	}
260 	CLOSE(sd);
261 
262 	debug("leaving test_bind()");
263 }
264 
265 void test_listen(const struct socket_test_info *info)
266 {
267 	int rc;
268 
269 	debug("entering test_listen()");
270 
271 	debug("Test listen() with a bad file descriptor");
272 
273 	errno = 0;
274 	rc = listen(-1, 0);
275 	if (!(rc == -1 && errno == EBADF)) {
276 		test_fail("listen(-1, 0) should have failed");
277 	}
278 
279 	debug("Test listen() with a non-socket file descriptor");
280 
281 	errno = 0;
282 	rc = listen(0, 0);
283 	/* Test on errno disabled here: there's currently no telling what this
284 	 * will return. POSIX says it should be ENOTSOCK, MINIX3 libc returns
285 	 * ENOSYS, and we used to test for ENOTTY here..
286 	 */
287 	if (!(rc == -1)) {
288 		test_fail("listen(0, 0) should have failed");
289 	}
290 
291 	debug("leaving test_listen()");
292 }
293 
294 void test_shutdown(const struct socket_test_info *info)
295 {
296 	int how[3] = { SHUT_RD, SHUT_WR, SHUT_RDWR };
297 	int sd;
298 	int rc;
299 	int i;
300 
301 	debug("entering test_shutdown()");
302 
303 	/* test for each direction (read, write, read-write) */
304 	for (i = 0; i < 3; i++) {
305 
306 		debug("test shutdown() with an invalid descriptor");
307 
308 		errno = 0;
309 		rc = shutdown(-1, how[i]);
310 		if (!(rc == -1 && errno == EBADF)) {
311 			test_fail("shutdown(-1, how[i]) should have failed");
312 		}
313 
314 		debug("test shutdown() with a non-socket descriptor");
315 
316 		errno = 0;
317 		rc = shutdown(0, how[i]);
318 		if (!(rc == -1 && errno == ENOTSOCK)) {
319 			test_fail("shutdown() should have failed with "
320 			    "ENOTSOCK");
321 		}
322 
323 		debug("test shutdown() with a socket that is not connected");
324 
325 		SOCKET(sd, info->domain, info->type, 0);
326 		errno = 0;
327 		rc = shutdown(sd, how[i]);
328 		if (rc != 0 && !(rc == -1 && errno == ENOTCONN)) {
329 			test_fail("shutdown() should have failed");
330 		}
331 		CLOSE(sd);
332 	}
333 
334 	SOCKET(sd, info->domain, info->type, 0);
335 	errno = 0;
336 	rc = shutdown(sd, -1);
337 	if (!(rc == -1 && errno == EINVAL)) {
338 		test_fail("shutdown(sd, -1) should have failed with EINVAL");
339 	}
340 	CLOSE(sd);
341 
342 	debug("leaving test_shutdown()");
343 }
344 
345 void test_close(const struct socket_test_info *info)
346 {
347 	int sd, sd2;
348 	int rc, i, on;
349 
350 	debug("entering test_close()");
351 
352 	info->callback_cleanup();
353 
354 	debug("Test close() success");
355 
356 	SOCKET(sd, info->domain, info->type, 0);
357 
358 	on = 1;
359 	(void)setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
360 
361 	rc = bind(sd, info->serveraddr, info->serveraddrlen);
362 	if (rc != 0) {
363 		test_fail("bind() should have worked");
364 	}
365 
366 	CLOSE(sd);
367 
368 	debug("Close an already closed file descriptor");
369 
370 	errno = 0;
371 	rc = close(sd);
372 	if (!(rc == -1 && errno == EBADF)) {
373 		test_fail("close(sd) should have failed with EBADF");
374 	}
375 
376 	info->callback_cleanup();
377 
378 	debug("dup()'ing a file descriptor and closing both should work");
379 
380 	SOCKET(sd, info->domain, info->type, 0);
381 
382 	on = 1;
383 	(void)setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
384 
385 	rc = bind(sd, info->serveraddr, info->serveraddrlen);
386 	if (rc != 0) {
387 		test_fail("bind() should have worked");
388 	}
389 
390 	errno = 0;
391 	sd2 = dup(sd);
392 	if (sd2 == -1) {
393 		test_fail("dup(sd) should have worked");
394 	} else {
395 		CLOSE(sd2);
396 		CLOSE(sd);
397 	}
398 
399 	info->callback_cleanup();
400 
401 	/* Create and close a socket a bunch of times.
402 	 * If the implementation doesn't properly free the
403 	 * socket during close(), eventually socket() will
404 	 * fail when the internal descriptor table is full.
405 	 */
406 	for (i = 0; i < 1024; i++) {
407 		SOCKET(sd, info->domain, info->type, 0);
408 		CLOSE(sd);
409 	}
410 
411 	debug("leaving test_close()");
412 }
413 
414 void test_sockopts(const struct socket_test_info *info)
415 {
416 	int i;
417 	int rc;
418 	int sd;
419 	int option_value;
420 	socklen_t option_len;
421 
422 	debug("entering test_sockopts()");
423 
424 	for (i = 0; i < info->typecount; i++) {
425 
426 		SOCKET(sd, info->domain, info->types[i], 0);
427 
428 		debug("Test setsockopt() works");
429 
430 		option_value = 0;
431 		option_len = sizeof(option_value);
432 		errno = 0;
433 		rc = getsockopt(sd, SOL_SOCKET, SO_TYPE, &option_value,
434 							&option_len);
435 		if (rc != 0) {
436 			test_fail("setsockopt() should have worked");
437 		}
438 
439 		if (option_value != info->types[i]) {
440 			test_fail("SO_TYPE didn't seem to work.");
441 		}
442 
443 		CLOSE(sd);
444 	}
445 
446 	SOCKET(sd, info->domain, info->type, 0);
447 
448 	debug("Test setsockopt() works");
449 
450 	option_value = 0;
451 	option_len = sizeof(option_value);
452 	errno = 0;
453 	rc = getsockopt(sd, SOL_SOCKET, SO_SNDBUF, &option_value, &option_len);
454 
455 	if (info->expected_sndbuf >= 0 &&
456 		option_value != info->expected_sndbuf) {
457 		test_fail("SO_SNDBUF didn't seem to work.");
458 	}
459 
460 	CLOSE(sd);
461 
462 
463 	SOCKET(sd, info->domain, info->type, 0);
464 
465 	debug("Test setsockopt() works");
466 
467 	option_value = 0;
468 	option_len = sizeof(option_value);
469 	errno = 0;
470 	rc = getsockopt(sd, SOL_SOCKET, SO_RCVBUF, &option_value, &option_len);
471 	if (rc != 0) {
472 		test_fail("getsockopt() should have worked");
473 	}
474 
475 	if (info->expected_rcvbuf >= 0 &&
476 		option_value != info->expected_rcvbuf) {
477 		test_fail("SO_RCVBUF didn't seem to work.");
478 	}
479 
480 	CLOSE(sd);
481 
482 
483 	debug("leaving test_sockopts()");
484 }
485 
486 void test_read(const struct socket_test_info *info)
487 {
488 	int rc;
489 	int fd;
490 	char buf[BUFSIZE];
491 
492 	debug("entering test_read()");
493 
494 	errno = 0;
495 	rc = read(-1, buf, sizeof(buf));
496 	if (!(rc == -1 && errno == EBADF)) {
497 		test_fail("read() should have failed with EBADF");
498 	}
499 
500 	fd = open("/tmp", O_RDONLY);
501 	if (fd == -1) {
502 		test_fail("open(\"/tmp\", O_RDONLY) should have worked");
503 	}
504 
505 	CLOSE(fd);
506 
507 	debug("leaving test_read()");
508 }
509 
510 void test_write(const struct socket_test_info *info)
511 {
512 	int rc;
513 	char buf[BUFSIZE];
514 
515 	debug("entering test_write()");
516 
517 	errno = 0;
518 	rc = write(-1, buf, sizeof(buf));
519 	if (!(rc == -1 && errno == EBADF)) {
520 		test_fail("write() should have failed with EBADF");
521 	}
522 
523 	debug("leaving test_write()");
524 }
525 
526 void test_dup(const struct socket_test_info *info)
527 {
528 	struct stat info1;
529 	struct stat info2;
530 	int sd, sd2;
531 	int rc;
532 	int i, on;
533 
534 	debug("entering test_dup()");
535 
536 	info->callback_cleanup();
537 
538 	debug("Test dup()");
539 
540 	SOCKET(sd, info->domain, info->type, 0);
541 
542 	on = 1;
543 	(void)setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
544 
545 	rc = bind(sd, info->serveraddr, info->serveraddrlen);
546 	if (rc != 0) {
547 		test_fail("bind() should have worked");
548 	}
549 
550 	errno = 0;
551 	sd2 = dup(sd);
552 	if (sd2 == -1) {
553 		test_fail("dup(sd) should have worked");
554 	}
555 
556 	rc = fstat(sd, &info1);
557 	if (rc == -1) {
558 		test_fail("fstat(fd, &info1) failed");
559 	}
560 
561 	rc = fstat(sd2, &info2);
562 	if (rc == -1) {
563 		test_fail("fstat(sd, &info2) failed");
564 	}
565 
566 	if (info1.st_ino != info2.st_ino) {
567 		test_fail("dup() failed info1.st_ino != info2.st_ino");
568 	}
569 
570 	CLOSE(sd);
571 	CLOSE(sd2);
572 
573 	debug("Test dup() with a closed socket");
574 
575 	errno = 0;
576 	rc = dup(sd);
577 	if (!(rc == -1 && errno == EBADF)) {
578 		test_fail("dup(sd) on a closed socket shouldn't have worked");
579 	}
580 
581 	debug("Test dup() with socket descriptor of -1");
582 
583 	errno = 0;
584 	rc = dup(-1);
585 	if (!(rc == -1 && errno == EBADF)) {
586 		test_fail("dup(-1) shouldn't have worked");
587 	}
588 
589 	debug("Test dup() when all of the file descriptors are taken");
590 
591 	SOCKET(sd, info->domain, info->type, 0);
592 
593 	for (i = 4; i < getdtablesize(); i++) {
594 		rc = open("/dev/null", O_RDONLY);
595 		if (rc == -1) {
596 			test_fail("we couldn't open /dev/null for read");
597 		}
598 	}
599 
600 	errno = 0;
601 	sd2 = dup(sd);
602 	if (!(sd2 == -1 && errno == EMFILE)) {
603 		test_fail("dup(sd) should have failed with errno = EMFILE");
604 	}
605 
606 	for (i = 3; i < getdtablesize(); i++) {
607 		CLOSE(i);
608 	}
609 
610 	info->callback_cleanup();
611 
612 	debug("leaving test_dup()");
613 }
614 
615 void test_dup2(const struct socket_test_info *info)
616 {
617 	struct stat info1;
618 	struct stat info2;
619 	int sd;
620 	int fd;
621 	int rc;
622 	int on;
623 
624 	debug("entering test_dup2()");
625 	info->callback_cleanup();
626 
627 	SOCKET(sd, info->domain, info->type, 0);
628 
629 	on = 1;
630 	(void)setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
631 
632 	rc = bind(sd, info->serveraddr, info->serveraddrlen);
633 	if (rc != 0) {
634 		test_fail("bind() should have worked");
635 	}
636 
637 	fd = open("/dev/null", O_RDONLY);
638 	if (fd == -1) {
639 		test_fail("open(\"/dev/null\", O_RDONLY) failed");
640 	}
641 
642 	fd = dup2(sd, fd);
643 	if (fd == -1) {
644 		test_fail("dup2(sd, fd) failed.");
645 	}
646 
647 	memset(&info1, '\0', sizeof(struct stat));
648 	memset(&info2, '\0', sizeof(struct stat));
649 
650 	rc = fstat(fd, &info1);
651 	if (rc == -1) {
652 		test_fail("fstat(fd, &info1) failed");
653 	}
654 
655 	rc = fstat(sd, &info2);
656 	if (rc == -1) {
657 		test_fail("fstat(sd, &info2) failed");
658 	}
659 
660 	if (!(info1.st_ino == info2.st_ino &&
661 		major(info1.st_dev) == major(info2.st_dev) &&
662 		minor(info1.st_dev) == minor(info2.st_dev))) {
663 
664 		test_fail("dup2() failed");
665 	}
666 
667 	CLOSE(fd);
668 	CLOSE(sd);
669 
670 	info->callback_cleanup();
671 	debug("leaving test_dup2()");
672 
673 }
674 
675 /*
676  * A toupper() server. This toy server converts a string to upper case.
677  */
678 static void test_xfer_server(const struct socket_test_info *info, pid_t pid)
679 {
680 	int i;
681 	struct timeval tv;
682 	fd_set readfds;
683 	int status;
684 	int rc;
685 	int sd;
686 	int on;
687 	unsigned char buf[BUFSIZE];
688 	socklen_t client_addr_size;
689 	int client_sd;
690 	struct sockaddr_storage client_addr;
691 
692 	status = 0;
693 	rc = 0;
694 	sd = 0;
695 	client_sd = 0;
696 	client_addr_size = sizeof(struct sockaddr_storage);
697 
698 	memset(&buf, '\0', sizeof(buf));
699 	memset(&client_addr, '\0', sizeof(client_addr));
700 
701 	SOCKET(sd, info->domain, info->type, 0);
702 
703 	on = 1;
704 	(void)setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
705 
706 	rc = bind(sd, info->serveraddr, info->serveraddrlen);
707 	if (rc == -1) {
708 		test_fail("bind() should have worked");
709 	}
710 
711 	rc = listen(sd, 8);
712 	if (rc == -1) {
713 		test_fail("listen(sd, 8) should have worked");
714 	}
715 
716 	/* we're ready for connections, time to tell the client to start
717 	 * the test
718 	 */
719 	kill(pid, SIGUSR1);
720 
721 	tv.tv_sec = 10;
722 	tv.tv_usec = 0;
723 
724 	FD_ZERO(&readfds);
725 	FD_SET(sd, &readfds);
726 
727 	/* use select() in case the client is really broken and never
728 	 * attempts to connect (we don't want to block on accept()
729 	 * forever).
730 	 */
731 	rc = select(sd + 1, &readfds, NULL, NULL, &tv);
732 	if (rc == -1) {
733 		test_fail("[server] select() should not have failed");
734 	}
735 
736 	if (rc != 1) {
737 		test_fail("[server] select() should have returned 1");
738 		printf("[server] select returned %d\n", rc);
739 	}
740 
741 	if (!(FD_ISSET(sd, &readfds))) {
742 		test_fail("[server] client didn't connect within 10 seconds");
743 		kill(pid, SIGKILL);
744 		return;
745 	}
746 
747 	client_sd = accept(sd, (struct sockaddr *) &client_addr,
748 						&client_addr_size);
749 
750 	if (client_sd == -1) {
751 		test_fail("accept() should have worked");
752 		kill(pid, SIGKILL);
753 		return;
754 	} else {
755 		debug("[server] client accept()'d");
756 	}
757 
758 	debug("[server] Reading message");
759 	rc = read(client_sd, buf, sizeof(buf));
760 	if (rc == -1) {
761 		test_fail("read() failed unexpectedly");
762 		kill(pid, SIGKILL);
763 		return;
764 	}
765 	debug("[server] we got the following message:");
766 	debug(buf);
767 
768 	for (i = 0; i < rc && i < 127; i++) {
769 		buf[i] = toupper(buf[i]);
770 	}
771 
772 	debug("[server] Writing message...");
773 	rc = write(client_sd, buf, sizeof(buf));
774 	if (rc == -1) {
775 		test_fail("write(client_sd, buf, sizeof(buf)) failed");
776 		kill(pid, SIGKILL);
777 		return;
778 	}
779 
780 	if (rc < strlen((char *)buf)) {
781 		test_fail("[server] write didn't write all the bytes");
782 	}
783 
784 	memset(&buf, '\0', sizeof(buf));
785 
786 	debug("[server] Recv message");
787 	rc = recv(client_sd, buf, sizeof(buf), 0);
788 	if (rc == -1) {
789 		test_fail("recv() failed unexpectedly");
790 		kill(pid, SIGKILL);
791 		return;
792 	}
793 	debug("[server] we got the following message:");
794 	debug(buf);
795 
796 	for (i = 0; i < rc && i < 127; i++) {
797 		buf[i] = toupper(buf[i]);
798 	}
799 
800 	debug("[server] Sending message...");
801 	rc = send(client_sd, buf, sizeof(buf), 0);
802 	if (rc == -1) {
803 		test_fail("send(client_sd, buf, sizeof(buf), 0) failed");
804 		kill(pid, SIGKILL);
805 		return;
806 	}
807 
808 	if (rc < strlen((char *)buf)) {
809 		test_fail("[server] write didn't write all the bytes");
810 	}
811 
812 	memset(&buf, '\0', sizeof(buf));
813 
814 	debug("[server] Recvfrom message");
815 	rc = recvfrom(client_sd, buf, sizeof(buf), 0, NULL, 0);
816 	if (rc == -1) {
817 		test_fail("recvfrom() failed unexpectedly");
818 		kill(pid, SIGKILL);
819 		return;
820 	}
821 	debug("[server] we got the following message:");
822 	debug(buf);
823 
824 	for (i = 0; i < rc && i < 127; i++) {
825 		buf[i] = toupper(buf[i]);
826 	}
827 
828 	debug("[server] Sendto message...");
829 	rc = sendto(client_sd, buf, sizeof(buf), 0, NULL, 0);
830 	if (rc == -1) {
831 		test_fail("sendto() failed");
832 		kill(pid, SIGKILL);
833 		return;
834 	}
835 
836 	if (rc < strlen((char *)buf)) {
837 		test_fail("[server] write didn't write all the bytes");
838 	}
839 
840 	shutdown(client_sd, SHUT_RDWR);
841 	CLOSE(client_sd);
842 
843 	shutdown(sd, SHUT_RDWR);
844 	CLOSE(sd);
845 
846 	/* wait for client to exit */
847 	do {
848 		errno = 0;
849 		rc = waitpid(pid, &status, 0);
850 	} while (rc == -1 && errno == EINTR);
851 
852 	/* we use the exit status to get its error count */
853 	errct += WEXITSTATUS(status);
854 }
855 
856 int server_ready = 0;
857 
858 /* signal handler for the client */
859 void test_xfer_sighdlr(int sig)
860 {
861 	debug("entering signal handler");
862 	switch (sig) {
863 		/* the server will send SIGUSR1 when it is time for us
864 		 * to start the tests
865 		 */
866 	case SIGUSR1:
867 		server_ready = 1;
868 		debug("got SIGUSR1, the server is ready for the client");
869 		break;
870 	default:
871 		debug("didn't get SIGUSR1");
872 	}
873 	debug("leaving signal handler");
874 }
875 
876 /*
877  * A toupper() client.
878  */
879 static void test_xfer_client(const struct socket_test_info *info)
880 {
881 	struct timeval tv;
882 	fd_set readfds;
883 	struct sockaddr_storage peer_addr;
884 	socklen_t peer_addr_len;
885 	int sd;
886 	int rc;
887 	char buf[BUFSIZE];
888 
889 	debug("[client] entering test_xfer_client()");
890 	errct = 0;	/* reset error count */
891 	memset(&buf, '\0', sizeof(buf));
892 
893 	while (server_ready == 0) {
894 		debug("[client] waiting for the server to signal");
895 		sleep(1);
896 	}
897 
898 	peer_addr_len = sizeof(peer_addr);
899 
900 
901 	if (info->callback_xfer_prepclient) info->callback_xfer_prepclient();
902 
903 	debug("[client] creating client socket");
904 	SOCKET(sd, info->domain, info->type, 0);
905 
906 	debug("[client] connecting to server through the symlink");
907 	rc = connect(sd, info->clientaddrsym, info->clientaddrsymlen);
908 	if (rc == -1) {
909 		test_fail("[client] connect() should have worked");
910 	} else {
911 		debug("[client] connected");
912 	}
913 
914 	debug("[client] testing getpeername()");
915 	memset(&peer_addr, '\0', sizeof(peer_addr));
916 	rc = getpeername(sd, (struct sockaddr *) &peer_addr, &peer_addr_len);
917 	if (rc == -1) {
918 		test_fail("[client] getpeername() should have worked");
919 	}
920 
921 
922 	info->callback_check_sockaddr((struct sockaddr *) &peer_addr,
923 		peer_addr_len, "getpeername", 1);
924 
925 	strncpy(buf, "Hello, World!", sizeof(buf) - 1);
926 	debug("[client] send to server");
927 	rc = write(sd, buf, sizeof(buf));
928 	if (rc == -1) {
929 		test_fail("[client] write() failed unexpectedly");
930 	}
931 
932 	memset(buf, '\0', sizeof(buf));
933 	debug("[client] read from server");
934 	rc = read(sd, buf, sizeof(buf));
935 	if (rc == -1) {
936 		test_fail("[client] read() failed unexpectedly");
937 	} else {
938 		debug("[client] we got the following message:");
939 		debug(buf);
940 	}
941 
942 	if (strncmp(buf, "HELLO, WORLD!", sizeof(buf)) != 0) {
943 		test_fail("[client] We didn't get the correct response");
944 	}
945 
946 	memset(&buf, '\0', sizeof(buf));
947 	strncpy(buf, "Bonjour!", sizeof(buf) - 1);
948 
949 	debug("[client] send to server");
950 	rc = send(sd, buf, sizeof(buf), 0);
951 	if (rc == -1) {
952 		test_fail("[client] send() failed unexpectedly");
953 	}
954 
955 	if (info->callback_xfer_peercred) info->callback_xfer_peercred(sd);
956 
957 	debug("Testing select()");
958 
959 	tv.tv_sec = 2;
960 	tv.tv_usec = 500000;
961 
962 	FD_ZERO(&readfds);
963 	FD_SET(sd, &readfds);
964 
965 	rc = select(sd + 1, &readfds, NULL, NULL, &tv);
966 	if (rc == -1) {
967 		test_fail("[client] select() should not have failed");
968 	}
969 
970 	if (rc != 1) {
971 		test_fail("[client] select() should have returned 1");
972 	}
973 
974 	if (!(FD_ISSET(sd, &readfds))) {
975 		test_fail("The server didn't respond within 2.5 seconds");
976 	}
977 
978 	memset(buf, '\0', sizeof(buf));
979 	debug("[client] recv from server");
980 	rc = recv(sd, buf, sizeof(buf), 0);
981 	if (rc == -1) {
982 		test_fail("[client] recv() failed unexpectedly");
983 	} else {
984 		debug("[client] we got the following message:");
985 		debug(buf);
986 	}
987 
988 	if (strncmp(buf, "BONJOUR!", sizeof(buf)) != 0) {
989 		test_fail("[client] We didn't get the right response.");
990 	}
991 
992 	memset(&buf, '\0', sizeof(buf));
993 	strncpy(buf, "Hola!", sizeof(buf) - 1);
994 
995 	debug("[client] sendto to server");
996 	rc = sendto(sd, buf, sizeof(buf), 0, NULL, 0);
997 	if (rc == -1) {
998 		test_fail("[client] sendto() failed");
999 	}
1000 
1001 	debug("Testing select()");
1002 
1003 	tv.tv_sec = 2;
1004 	tv.tv_usec = 500000;
1005 
1006 	FD_ZERO(&readfds);
1007 	FD_SET(sd, &readfds);
1008 
1009 	rc = select(sd + 1, &readfds, NULL, NULL, &tv);
1010 	if (rc == -1) {
1011 		test_fail("[client] select() should not have failed");
1012 	}
1013 
1014 	if (rc != 1) {
1015 		test_fail("[client] select() should have returned 1");
1016 	}
1017 
1018 	if (!(FD_ISSET(sd, &readfds))) {
1019 		test_fail("[client] The server didn't respond in 2.5 seconds");
1020 	}
1021 
1022 	memset(buf, '\0', sizeof(buf));
1023 	debug("[client] recvfrom from server");
1024 	rc = recvfrom(sd, buf, sizeof(buf), 0, NULL, 0);
1025 	if (rc == -1) {
1026 		test_fail("[cleint] recvfrom() failed unexpectedly");
1027 	} else {
1028 		debug("[client] we got the following message:");
1029 		debug(buf);
1030 	}
1031 
1032 	if (strncmp(buf, "HOLA!", sizeof(buf)) != 0) {
1033 		test_fail("[client] We didn't get the right response.");
1034 	}
1035 
1036 	debug("[client] closing socket");
1037 	CLOSE(sd);
1038 
1039 	debug("[client] leaving test_xfer_client()");
1040 	exit(errct);
1041 }
1042 
1043 void test_xfer(const struct socket_test_info *info)
1044 {
1045 	pid_t pid;
1046 
1047 	debug("entering test_xfer()");
1048 	info->callback_cleanup();
1049 
1050 	/* the signal handler is only used by the client, but we have to
1051 	 * install it now. if we don't the server may signal the client
1052 	 * before the handler is installed.
1053 	 */
1054 	debug("installing signal handler");
1055 	if (signal(SIGUSR1, test_xfer_sighdlr) == SIG_ERR) {
1056 		test_fail("signal(SIGUSR1, test_xfer_sighdlr) failed");
1057 	}
1058 
1059 	debug("signal handler installed");
1060 
1061 	server_ready = 0;
1062 
1063 	pid = fork();
1064 	if (pid == -1) {
1065 		test_fail("fork() failed");
1066 		return;
1067 	} else if (pid == 0) {
1068 		debug("child");
1069 		errct = 0;
1070 		test_xfer_client(info);
1071 		test_fail("we should never get here");
1072 		exit(1);
1073 	} else {
1074 		debug("parent");
1075 		test_xfer_server(info, pid);
1076 		debug("parent done");
1077 	}
1078 
1079 	info->callback_cleanup();
1080 	debug("leaving test_xfer()");
1081 }
1082 
1083 static void test_simple_client(const struct socket_test_info *info, int type)
1084 {
1085 	char buf[BUFSIZE];
1086 	int sd, rc;
1087 
1088 	sd = socket(info->domain, type, 0);
1089 	if (sd == -1) {
1090 		test_fail("socket");
1091 		exit(errct);
1092 	}
1093 
1094 	while (server_ready == 0) {
1095 		debug("[client] waiting for the server");
1096 		sleep(1);
1097 	}
1098 
1099 	bzero(buf, BUFSIZE);
1100 	snprintf(buf, BUFSIZE-1, "Hello, My Name is Client.");
1101 
1102 	if (type == SOCK_DGRAM) {
1103 
1104 		rc = sendto(sd, buf, strlen(buf) + 1, 0,
1105 			info->clientaddr, info->clientaddrlen);
1106 		if (rc == -1) {
1107 			test_fail("sendto");
1108 			exit(errct);
1109 		}
1110 
1111 	} else {
1112 
1113 		rc = connect(sd, info->clientaddr, info->clientaddrlen);
1114 		if (rc == -1) {
1115 			test_fail("connect");
1116 			exit(errct);
1117 		}
1118 
1119 		rc = write(sd, buf, strlen(buf) + 1);
1120 
1121 		if (rc == -1) {
1122 			test_fail("write");
1123 		}
1124 
1125 		memset(buf, '\0', BUFSIZE);
1126 		rc = read(sd, buf, BUFSIZE);
1127 		if (rc == -1) {
1128 			test_fail("read");
1129 		}
1130 
1131 		if (strcmp("Hello, My Name is Server.", buf) != 0) {
1132 			test_fail("didn't read the correct string");
1133 		}
1134 	}
1135 
1136 	rc = close(sd);
1137 	if (rc == -1) {
1138 		test_fail("close");
1139 	}
1140 
1141 	exit(errct);
1142 }
1143 
1144 static void test_simple_server(const struct socket_test_info *info, int type,
1145 	pid_t pid)
1146 {
1147 	char buf[BUFSIZE];
1148 	int sd, rc, client_sd, status, on;
1149 	struct sockaddr_storage addr;
1150 	socklen_t addr_len;
1151 
1152 	addr_len = info->clientaddrlen;
1153 
1154 	sd = socket(info->domain, type, 0);
1155 	if (sd == -1) {
1156 		test_fail("socket");
1157 	}
1158 
1159 	on = 1;
1160 	(void)setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
1161 
1162 	assert(info->clientaddrlen <= sizeof(addr));
1163 	memcpy(&addr, info->clientaddr, info->clientaddrlen);
1164 
1165 	rc = bind(sd, info->serveraddr, info->serveraddrlen);
1166 	if (rc == -1) {
1167 		test_fail("bind");
1168 	}
1169 
1170 	if (type == SOCK_DGRAM) {
1171 
1172 		/* ready for client */
1173 		kill(pid, SIGUSR1);
1174 
1175 		rc = recvfrom(sd, buf, BUFSIZE, 0,
1176 				(struct sockaddr *) &addr, &addr_len);
1177 		if (rc == -1) {
1178 			test_fail("recvfrom");
1179 		}
1180 
1181 	} else {
1182 
1183 		rc = listen(sd, 5);
1184 		if (rc == -1) {
1185 			test_fail("listen");
1186 		}
1187 
1188 		/* we're ready for connections, time to tell the client
1189 		 * to start the test
1190 		 */
1191 		kill(pid, SIGUSR1);
1192 
1193 		client_sd = accept(sd, (struct sockaddr *) &addr, &addr_len);
1194 		if (client_sd == -1) {
1195 			test_fail("accept");
1196 		}
1197 
1198 		memset(buf, '\0', BUFSIZE);
1199 		rc = read(client_sd, buf, BUFSIZE);
1200 		if (rc == -1) {
1201 			test_fail("read");
1202 		}
1203 
1204 		if (strcmp("Hello, My Name is Client.", buf) != 0) {
1205 			test_fail("didn't read the correct string");
1206 		}
1207 
1208 		/* added for extra fun to make the client block on read() */
1209 		sleep(1);
1210 
1211 		bzero(buf, BUFSIZE);
1212 		snprintf(buf, BUFSIZE-1, "Hello, My Name is Server.");
1213 
1214 		rc = write(client_sd, buf, strlen(buf) + 1);
1215 		if (rc == -1) {
1216 			test_fail("write");
1217 		}
1218 		rc = close(client_sd);
1219 		if (rc == -1) {
1220 			test_fail("close");
1221 		}
1222 	}
1223 
1224 	rc = close(sd);
1225 	if (rc == -1) {
1226 		test_fail("close");
1227 	}
1228 
1229 	/* wait for client to exit */
1230 	do {
1231 		errno = 0;
1232 		rc = waitpid(pid, &status, 0);
1233 	} while (rc == -1 && errno == EINTR);
1234 
1235 	/* we use the exit status to get its error count */
1236 	errct += WEXITSTATUS(status);
1237 }
1238 
1239 static void test_abort_client(const struct socket_test_info *info,
1240 	int abort_type);
1241 static void test_abort_server(const struct socket_test_info *info,
1242 	pid_t pid, int abort_type);
1243 
1244 void test_abort_client_server(const struct socket_test_info *info,
1245 	int abort_type)
1246 {
1247 	pid_t pid;
1248 
1249 	debug("test_simple_client_server()");
1250 
1251 	info->callback_cleanup();
1252 
1253 	/* the signal handler is only used by the client, but we have to
1254 	 * install it now. if we don't the server may signal the client
1255 	 * before the handler is installed.
1256 	 */
1257 	debug("installing signal handler");
1258 	if (signal(SIGUSR1, test_xfer_sighdlr) == SIG_ERR) {
1259 		test_fail("signal(SIGUSR1, test_xfer_sighdlr) failed");
1260 	}
1261 
1262 	debug("signal handler installed");
1263 
1264 	server_ready = 0;
1265 
1266 	pid = fork();
1267 	if (pid == -1) {
1268 		test_fail("fork() failed");
1269 		return;
1270 	} else if (pid == 0) {
1271 		debug("child");
1272 		errct = 0;
1273 		test_abort_client(info, abort_type);
1274 		test_fail("we should never get here");
1275 		exit(1);
1276 	} else {
1277 		debug("parent");
1278 		test_abort_server(info, pid, abort_type);
1279 		debug("parent done");
1280 	}
1281 
1282 	info->callback_cleanup();
1283 }
1284 
1285 static void test_abort_client(const struct socket_test_info *info,
1286 	int abort_type)
1287 {
1288 	char buf[BUFSIZE];
1289 	int sd, rc;
1290 
1291 	sd = socket(info->domain, info->type, 0);
1292 	if (sd == -1) {
1293 		test_fail("socket");
1294 		exit(errct);
1295 	}
1296 
1297 	while (server_ready == 0) {
1298 		debug("[client] waiting for the server");
1299 		sleep(1);
1300 	}
1301 
1302 	bzero(buf, BUFSIZE);
1303 	snprintf(buf, BUFSIZE-1, "Hello, My Name is Client.");
1304 
1305 	rc = connect(sd, info->clientaddr, info->clientaddrlen);
1306 	if (rc == -1) {
1307 		test_fail("connect");
1308 		exit(errct);
1309 	}
1310 
1311 	if (abort_type == 2) {
1312 		/* Give server a chance to close connection */
1313 		sleep(2);
1314 		rc = write(sd, buf, strlen(buf) + 1);
1315 		if (rc != -1) {
1316 			if (!info->ignore_write_conn_reset) {
1317 				test_fail("write should have failed\n");
1318 			}
1319 		} else if (errno != EPIPE && errno != ECONNRESET) {
1320 			test_fail("errno should've been EPIPE/ECONNRESET\n");
1321 		}
1322 	}
1323 
1324 	rc = close(sd);
1325 	if (rc == -1) {
1326 		test_fail("close");
1327 	}
1328 
1329 	exit(errct);
1330 }
1331 
1332 static void test_abort_server(const struct socket_test_info *info,
1333 	pid_t pid, int abort_type)
1334 {
1335 	char buf[BUFSIZE];
1336 	int sd, rc, client_sd, status, on;
1337 	struct sockaddr_storage addr;
1338 	socklen_t addr_len;
1339 
1340 	addr_len = info->clientaddrlen;
1341 
1342 	sd = socket(info->domain, info->type, 0);
1343 	if (sd == -1) {
1344 		test_fail("socket");
1345 	}
1346 
1347 	on = 1;
1348 	(void)setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
1349 
1350 	assert(sizeof(addr) >= info->clientaddrlen);
1351 	memcpy(&addr, info->clientaddr, info->clientaddrlen);
1352 
1353 	rc = bind(sd, info->serveraddr, info->serveraddrlen);
1354 	if (rc == -1) {
1355 		test_fail("bind");
1356 	}
1357 
1358 	rc = listen(sd, 5);
1359 	if (rc == -1) {
1360 		test_fail("listen");
1361 	}
1362 
1363 	/* we're ready for connections, time to tell the client
1364 	 * to start the test
1365 	 */
1366 	kill(pid, SIGUSR1);
1367 
1368 	client_sd = accept(sd, (struct sockaddr *) &addr, &addr_len);
1369 	if (client_sd == -1) {
1370 		test_fail("accept");
1371 	}
1372 
1373 	if (abort_type == 1) {
1374 		memset(buf, '\0', BUFSIZE);
1375 		rc = read(client_sd, buf, BUFSIZE);
1376 		if (rc != 0 && rc != -1) {
1377 			test_fail("read should've failed or returned zero\n");
1378 		}
1379 		if (rc != 0 && errno != ECONNRESET) {
1380 			test_fail("errno should've been ECONNRESET\n");
1381 		}
1382 	} /* else if (abort_type == 2) { */
1383 		rc = close(client_sd);
1384 		if (rc == -1) {
1385 			test_fail("close");
1386 		}
1387 	/* } */
1388 
1389 	rc = close(sd);
1390 	if (rc == -1) {
1391 		test_fail("close");
1392 	}
1393 
1394 	/* wait for client to exit */
1395 	do {
1396 		errno = 0;
1397 		rc = waitpid(pid, &status, 0);
1398 	} while (rc == -1 && errno == EINTR);
1399 
1400 	/* we use the exit status to get its error count */
1401 	errct += WEXITSTATUS(status);
1402 }
1403 
1404 void test_simple_client_server(const struct socket_test_info *info, int type)
1405 {
1406 	pid_t pid;
1407 
1408 	debug("entering test_simple_client_server()");
1409 
1410 	info->callback_cleanup();
1411 
1412 	/* the signal handler is only used by the client, but we have to
1413 	 * install it now. if we don't the server may signal the client
1414 	 * before the handler is installed.
1415 	 */
1416 	debug("installing signal handler");
1417 	if (signal(SIGUSR1, test_xfer_sighdlr) == SIG_ERR) {
1418 		test_fail("signal(SIGUSR1, test_xfer_sighdlr) failed");
1419 	}
1420 
1421 	debug("signal handler installed");
1422 
1423 	server_ready = 0;
1424 
1425 	pid = fork();
1426 	if (pid == -1) {
1427 		test_fail("fork() failed");
1428 		return;
1429 	} else if (pid == 0) {
1430 		debug("child");
1431 		errct = 0;
1432 		test_simple_client(info, type);
1433 		test_fail("we should never get here");
1434 		exit(1);
1435 	} else {
1436 		debug("parent");
1437 		test_simple_server(info, type, pid);
1438 		debug("parent done");
1439 	}
1440 
1441 	info->callback_cleanup();
1442 	debug("leaving test_simple_client_server()");
1443 }
1444 
1445 void test_msg_dgram(const struct socket_test_info *info)
1446 {
1447 	int rc;
1448 	int src;
1449 	int dst;
1450 	struct sockaddr_storage addr;
1451 	struct iovec iov[3];
1452 	struct msghdr msg1;
1453 	struct msghdr msg2;
1454 	char buf1[BUFSIZE];
1455 	char buf2[BUFSIZE];
1456 	char buf3[BUFSIZE];
1457 
1458 	debug("entering test_msg_dgram");
1459 
1460 	info->callback_cleanup();
1461 
1462 	src = socket(info->domain, SOCK_DGRAM, 0);
1463 	if (src == -1) {
1464 		test_fail("socket");
1465 	}
1466 
1467 	dst = socket(info->domain, SOCK_DGRAM, 0);
1468 	if (dst == -1) {
1469 		test_fail("socket");
1470 	}
1471 
1472 	rc = bind(src, info->serveraddr2, info->serveraddr2len);
1473 	if (rc == -1) {
1474 		test_fail("bind");
1475 	}
1476 
1477 	assert(info->clientaddrlen <= sizeof(addr));
1478 	memcpy(&addr, info->clientaddr, info->clientaddrlen);
1479 
1480 	rc = bind(dst, info->serveraddr, info->serveraddrlen);
1481 	if (rc == -1) {
1482 		test_fail("bind");
1483 	}
1484 
1485 	memset(&buf1, '\0', BUFSIZE);
1486 	memset(&buf2, '\0', BUFSIZE);
1487 	memset(&buf3, '\0', BUFSIZE);
1488 
1489 	strncpy(buf1, "Minix ", BUFSIZE-1);
1490 	strncpy(buf2, "is ", BUFSIZE-1);
1491 	strncpy(buf3, "great!", BUFSIZE-1);
1492 
1493 	iov[0].iov_base = buf1;
1494 	iov[0].iov_len  = 6;
1495 	iov[1].iov_base = buf2;
1496 	iov[1].iov_len  = 3;
1497 	iov[2].iov_base = buf3;
1498 	iov[2].iov_len  = 32;
1499 
1500 	memset(&msg1, '\0', sizeof(struct msghdr));
1501 	msg1.msg_name = &addr;
1502 	msg1.msg_namelen = info->clientaddrlen;
1503 	msg1.msg_iov = iov;
1504 	msg1.msg_iovlen = 3;
1505 	msg1.msg_control = NULL;
1506 	msg1.msg_controllen = 0;
1507 	msg1.msg_flags = 0;
1508 
1509 	rc = sendmsg(src, &msg1, 0);
1510 	if (rc == -1) {
1511 		test_fail("sendmsg");
1512 	}
1513 
1514 	memset(&buf1, '\0', BUFSIZE);
1515 	memset(&buf2, '\0', BUFSIZE);
1516 
1517 	iov[0].iov_base = buf1;
1518 	iov[0].iov_len  = 9;
1519 	iov[1].iov_base = buf2;
1520 	iov[1].iov_len  = 32;
1521 
1522 	memset(&addr, '\0', sizeof(addr));
1523 	memset(&msg2, '\0', sizeof(struct msghdr));
1524 	msg2.msg_name = &addr;
1525 	msg2.msg_namelen = sizeof(addr);
1526 	msg2.msg_iov = iov;
1527 	msg2.msg_iovlen = 2;
1528 	msg2.msg_control = NULL;
1529 	msg2.msg_controllen = 0;
1530 	msg2.msg_flags = 0;
1531 
1532 	rc = recvmsg(dst, &msg2, 0);
1533 	if (rc == -1) {
1534 		test_fail("recvmsg");
1535 	}
1536 
1537 	if (strncmp(buf1, "Minix is ", 9) || strncmp(buf2, "great!", 6)) {
1538 		test_fail("recvmsg");
1539 	}
1540 
1541 	info->callback_check_sockaddr((struct sockaddr *) &addr,
1542 		msg2.msg_namelen, "recvmsg", 2);
1543 
1544 	rc = close(dst);
1545 	if (rc == -1) {
1546 		test_fail("close");
1547 	}
1548 
1549 	rc = close(src);
1550 	if (rc == -1) {
1551 		test_fail("close");
1552 	}
1553 
1554 	info->callback_cleanup();
1555 	debug("leaving test_msg_dgram");
1556 }
1557 
1558 #define check_select(sd, rd, wr, block) \
1559 	check_select_internal(sd, rd, wr, block, 1, __LINE__)
1560 #define check_select_cond(sd, rd, wr, block, allchecks) \
1561 	check_select_internal(sd, rd, wr, block, allchecks, __LINE__)
1562 
1563 static void
1564 check_select_internal(int sd, int rd, int wr, int block, int allchecks, int line)
1565 {
1566 	fd_set read_set, write_set;
1567 	struct timeval tv;
1568 
1569 	FD_ZERO(&read_set);
1570 	if (rd != -1)
1571 		FD_SET(sd, &read_set);
1572 
1573 	FD_ZERO(&write_set);
1574 	if (wr != -1)
1575 		FD_SET(sd, &write_set);
1576 
1577 	tv.tv_sec = block ? 2 : 0;
1578 	tv.tv_usec = 0;
1579 
1580 	errno = 0;
1581 	if (select(sd + 1, &read_set, &write_set, NULL, &tv) < 0)
1582 		test_fail_fl("select() failed unexpectedly", __FILE__, line);
1583 
1584 	if (rd != -1 && !!FD_ISSET(sd, &read_set) != rd && allchecks)
1585 		test_fail_fl("select() mismatch on read operation",
1586 			__FILE__, line);
1587 
1588 	if (wr != -1 && !!FD_ISSET(sd, &write_set) != wr && allchecks)
1589 		test_fail_fl("select() mismatch on write operation",
1590 			__FILE__, line);
1591 }
1592 
1593 /*
1594  * Verify that:
1595  * - a nonblocking connecting socket for which there is no accepter, will
1596  *   return EINPROGRESS and complete in the background later;
1597  * - a nonblocking listening socket will return EAGAIN on accept;
1598  * - connecting a connecting socket yields EALREADY;
1599  * - connecting a connected socket yields EISCONN;
1600  * - selecting for read and write on a connecting socket will only satisfy the
1601  *   write only once it is connected;
1602  * - doing a nonblocking write on a connecting socket yields EAGAIN;
1603  * - doing a nonblocking read on a connected socket with no pending data yields
1604  *   EAGAIN.
1605  */
1606 void
1607 test_nonblock(const struct socket_test_info *info)
1608 {
1609 	char buf[BUFSIZE];
1610 	socklen_t len;
1611 	int server_sd, client_sd;
1612 	struct sockaddr_storage addr;
1613 	int status, on;
1614 
1615 	debug("entering test_nonblock()");
1616 	memset(buf, 0, sizeof(buf));
1617 
1618 	SOCKET(server_sd, info->domain, info->type, 0);
1619 
1620 	on = 1;
1621 	(void)setsockopt(server_sd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
1622 
1623 	if (bind(server_sd, info->serveraddr, info->serveraddrlen) == -1)
1624 		test_fail("bind() should have worked");
1625 
1626 	if (info->callback_set_listen_opt != NULL)
1627 		info->callback_set_listen_opt(server_sd);
1628 
1629 	if (listen(server_sd, 8) == -1)
1630 		test_fail("listen() should have worked");
1631 
1632 	fcntl(server_sd, F_SETFL, fcntl(server_sd, F_GETFL) | O_NONBLOCK);
1633 
1634 	check_select(server_sd, 0 /*read*/, 1 /*write*/, 0 /*block*/);
1635 
1636 	len = sizeof(addr);
1637 	if (accept(server_sd, (struct sockaddr *) &addr, &len) != -1 ||
1638 	    errno != EAGAIN)
1639 		test_fail("accept() should have yielded EAGAIN");
1640 
1641 	SOCKET(client_sd, info->domain, info->type, 0);
1642 
1643 	fcntl(client_sd, F_SETFL, fcntl(client_sd, F_GETFL) | O_NONBLOCK);
1644 
1645 	if (connect(client_sd, info->clientaddr, info->clientaddrlen) != -1) {
1646 		test_fail("connect() should have failed");
1647 	} else if (errno != EINPROGRESS) {
1648 		test_fail("connect() should have yielded EINPROGRESS");
1649 	}
1650 
1651 	check_select_cond(client_sd, 0 /*read*/, 0 /*write*/, 0 /*block*/,
1652 		!info->ignore_select_delay);
1653 
1654 	if (connect(client_sd, info->clientaddr, info->clientaddrlen) != -1) {
1655 		test_fail("connect() should have failed");
1656 	} else if (errno != EALREADY && errno != EISCONN) {
1657 		test_fail("connect() should have yielded EALREADY");
1658 	}
1659 
1660 	if (recv(client_sd, buf, sizeof(buf), 0) != -1 || errno != EAGAIN)
1661 		test_fail("recv() should have yielded EAGAIN");
1662 
1663 	/* This may be an implementation aspect, or even plain wrong (?). */
1664 	if (!info->ignore_send_waiting) {
1665 		if (send(client_sd, buf, sizeof(buf), 0) != -1) {
1666 			test_fail("send() should have failed");
1667 		} else if (errno != EAGAIN) {
1668 			test_fail("send() should have yielded EAGAIN");
1669 		}
1670 	}
1671 
1672 	switch (fork()) {
1673 	case 0:
1674 		errct = 0;
1675 		close(client_sd);
1676 
1677 		check_select(server_sd, 1 /*read*/, 1 /*write*/, 0 /*block*/);
1678 
1679 		len = sizeof(addr);
1680 		client_sd = accept(server_sd, (struct sockaddr *) &addr, &len);
1681 		if (client_sd == -1)
1682 			test_fail("accept() should have succeeded");
1683 
1684 		check_select(server_sd, 0 /*read*/, 1 /*write*/, 0 /*block*/);
1685 
1686 		close(server_sd);
1687 
1688 		/* Let the socket become writable in the parent process. */
1689 		sleep(1);
1690 
1691 		if (write(client_sd, buf, 1) != 1)
1692 			test_fail("write() should have succeeded");
1693 
1694 		/* Wait for the client side to close. */
1695 		check_select_cond(client_sd, 0 /*read*/, 1 /*write*/,
1696 			0 /*block*/, !info->ignore_select_delay /*allchecks*/);
1697 		check_select(client_sd, 1 /*read*/, -1 /*write*/, 1 /*block*/);
1698 		check_select(client_sd, 1 /*read*/, 1 /*write*/, 0 /*block*/);
1699 
1700 		exit(errct);
1701 	case -1:
1702 		test_fail("can't fork");
1703 	default:
1704 		break;
1705 	}
1706 
1707 	close(server_sd);
1708 
1709 	check_select(client_sd, 0 /*read*/, 1 /*write*/, 1 /*block*/);
1710 	check_select(client_sd, 0 /*read*/, 1 /*write*/, 0 /*block*/);
1711 
1712 	if (connect(client_sd, info->clientaddr, info->clientaddrlen) != -1 ||
1713 		errno != EISCONN)
1714 		test_fail("connect() should have yielded EISCONN");
1715 
1716 	check_select(client_sd, 1 /*read*/, -1 /*write*/, 1 /*block*/);
1717 	check_select(client_sd, 1 /*read*/, 1 /*write*/, 0 /*block*/);
1718 
1719 	if (read(client_sd, buf, 1) != 1)
1720 		test_fail("read() should have succeeded");
1721 
1722 	check_select(client_sd, 0 /*read*/, 1 /*write*/, 0 /*block*/);
1723 
1724 	if (read(client_sd, buf, 1) != -1 || errno != EAGAIN)
1725 		test_fail("read() should have yielded EAGAIN");
1726 
1727 	/* Let the child process block on the select waiting for the close. */
1728 	sleep(1);
1729 
1730 	close(client_sd);
1731 
1732 	errno = 0;
1733 	if (wait(&status) <= 0)
1734 		test_fail("wait() should have succeeded");
1735 	if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
1736 		test_fail("child process failed the test");
1737 
1738 	info->callback_cleanup();
1739 	debug("leaving test_nonblock()");
1740 }
1741 
1742 /*
1743  * Verify that a nonblocking connect for which there is an accepter, succeeds
1744  * immediately.  A pretty lame test, only here for completeness.
1745  */
1746 void
1747 test_connect_nb(const struct socket_test_info *info)
1748 {
1749 	socklen_t len;
1750 	int server_sd, client_sd;
1751 	struct sockaddr_storage addr;
1752 	int status, on;
1753 
1754 	debug("entering test_connect_nb()");
1755 	SOCKET(server_sd, info->domain, info->type, 0);
1756 
1757 	on = 1;
1758 	(void)setsockopt(server_sd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
1759 
1760 	if (bind(server_sd, info->serveraddr, info->serveraddrlen) == -1)
1761 		test_fail("bind() should have worked");
1762 
1763 	if (listen(server_sd, 8) == -1)
1764 		test_fail("listen() should have worked");
1765 
1766 	switch (fork()) {
1767 	case 0:
1768 		errct = 0;
1769 		len = sizeof(addr);
1770 		if (accept(server_sd, (struct sockaddr *) &addr, &len) == -1)
1771 			test_fail("accept() should have succeeded");
1772 
1773 		exit(errct);
1774 	case -1:
1775 		test_fail("can't fork");
1776 	default:
1777 		break;
1778 	}
1779 
1780 	close(server_sd);
1781 
1782 	sleep(1);
1783 
1784 	SOCKET(client_sd, info->domain, info->type, 0);
1785 
1786 	fcntl(client_sd, F_SETFL, fcntl(client_sd, F_GETFL) | O_NONBLOCK);
1787 
1788 	if (connect(client_sd, info->clientaddr, info->clientaddrlen) != 0) {
1789 		if (!info->ignore_connect_delay) {
1790 			test_fail("connect() should have succeeded");
1791 		} else if (errno != EINPROGRESS) {
1792 			test_fail("connect() should have succeeded or "
1793 				"failed with EINPROGRESS");
1794 		}
1795 	}
1796 
1797 	close(client_sd);
1798 
1799 	if (wait(&status) <= 0)
1800 		test_fail("wait() should have succeeded");
1801 	if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
1802 		test_fail("child process failed the test");
1803 
1804 	info->callback_cleanup();
1805 	debug("leaving test_connect_nb()");
1806 }
1807 
1808 static void
1809 dummy_handler(int sig)
1810 {
1811 	/* Nothing. */
1812 }
1813 
1814 /*
1815  * Verify that:
1816  * - interrupting a blocking connect will return EINTR but complete in the
1817  *   background later;
1818  * - doing a blocking write on an asynchronously connecting socket succeeds
1819  *   once the socket is connected.
1820  * - doing a nonblocking write on a connected socket with lots of pending data
1821  *   yields EAGAIN.
1822  */
1823 void
1824 test_intr(const struct socket_test_info *info)
1825 {
1826 	struct sigaction act, oact;
1827 	char buf[BUFSIZE];
1828 	int isconn;
1829 	socklen_t len;
1830 	int server_sd, client_sd;
1831 	struct sockaddr_storage addr;
1832 	int r, status, on;
1833 
1834 	debug("entering test_intr()");
1835 	memset(buf, 0, sizeof(buf));
1836 
1837 	SOCKET(server_sd, info->domain, info->type, 0);
1838 
1839 	on = 1;
1840 	(void)setsockopt(server_sd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
1841 
1842 	if (bind(server_sd, info->serveraddr, info->serveraddrlen) == -1)
1843 		test_fail("bind() should have worked");
1844 
1845 	if (info->callback_set_listen_opt != NULL)
1846 		info->callback_set_listen_opt(server_sd);
1847 
1848 	if (listen(server_sd, 8) == -1)
1849 		test_fail("listen() should have worked");
1850 
1851 	SOCKET(client_sd, info->domain, info->type, 0);
1852 
1853 	memset(&act, 0, sizeof(act));
1854 	act.sa_handler = dummy_handler;
1855 	if (sigaction(SIGALRM, &act, &oact) == -1)
1856 		test_fail("sigaction() should have succeeded");
1857 
1858 	if (info->domain != PF_INET) alarm(1);
1859 
1860 	isconn = 0;
1861 	if (connect(client_sd, info->clientaddr, info->clientaddrlen) != -1) {
1862 		if (!info->ignore_connect_unaccepted) {
1863 			test_fail("connect() should have failed");
1864 		}
1865 		isconn = 1;
1866 	} else if (errno != EINTR) {
1867 		test_fail("connect() should have yielded EINTR");
1868 	}
1869 
1870 	alarm(0);
1871 
1872 	check_select(client_sd, 0 /*read*/, isconn /*write*/, 0 /*block*/);
1873 
1874 	switch (fork()) {
1875 	case 0:
1876 		errct = 0;
1877 		close(client_sd);
1878 
1879 		/* Ensure that the parent is blocked on the send(). */
1880 		sleep(1);
1881 
1882 		check_select(server_sd, 1 /*read*/, 1 /*write*/, 0 /*block*/);
1883 
1884 		len = sizeof(addr);
1885 		client_sd = accept(server_sd, (struct sockaddr *) &addr, &len);
1886 		if (client_sd == -1)
1887 			test_fail("accept() should have succeeded");
1888 
1889 		check_select(server_sd, 0 /*read*/, 1 /*write*/, 0 /*block*/);
1890 
1891 		close(server_sd);
1892 
1893 		check_select(client_sd, 1 /*read*/, -1 /*write*/, 1 /*block*/);
1894 		check_select(client_sd, 1 /*read*/, 1 /*write*/, 0 /*block*/);
1895 
1896 		if (recv(client_sd, buf, sizeof(buf), 0) != sizeof(buf))
1897 			test_fail("recv() should have yielded bytes");
1898 
1899 		/* No partial transfers should be happening. */
1900 		check_select(client_sd, 0 /*read*/, 1 /*write*/, 0 /*block*/);
1901 
1902 		sleep(1);
1903 
1904 		fcntl(client_sd, F_SETFL, fcntl(client_sd, F_GETFL) |
1905 		    O_NONBLOCK);
1906 
1907 		/* We can only test nonblocking writes by filling the pipe. */
1908 		while ((r = write(client_sd, buf, sizeof(buf))) > 0);
1909 
1910 		if (r != -1) {
1911 			test_fail("write() should have failed");
1912 		} else if (errno != EAGAIN) {
1913 			test_fail("write() should have yielded EAGAIN");
1914 		}
1915 
1916 		check_select(client_sd, 0 /*read*/, 0 /*write*/, 0 /*block*/);
1917 
1918 		if (write(client_sd, buf, 1) != -1) {
1919 			test_fail("write() should have failed");
1920 		} else if (errno != EAGAIN) {
1921 			test_fail("write() should have yielded EAGAIN");
1922 		}
1923 
1924 		exit(errct);
1925 	case -1:
1926 		test_fail("can't fork");
1927 	default:
1928 		break;
1929 	}
1930 
1931 	close(server_sd);
1932 
1933 	if (send(client_sd, buf, sizeof(buf), 0) != sizeof(buf))
1934 		test_fail("send() should have succeded");
1935 
1936 	check_select(client_sd, 0 /*read*/, 1 /*write*/, 0 /*block*/);
1937 
1938 	if (wait(&status) <= 0)
1939 		test_fail("wait() should have succeeded");
1940 	if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
1941 		test_fail("child process failed the test");
1942 
1943 	check_select(client_sd, 1 /*read*/, 1 /*write*/, 0 /*block*/);
1944 
1945 	close(client_sd);
1946 
1947 	sigaction(SIGALRM, &oact, NULL);
1948 
1949 	info->callback_cleanup();
1950 	debug("leaving test_intr()");
1951 }
1952 
1953 /*
1954  * Verify that closing a connecting socket before it is accepted will result in
1955  * no activity on the accepting side later.
1956  */
1957 void
1958 test_connect_close(const struct socket_test_info *info)
1959 {
1960 	int server_sd, client_sd, sd, on;
1961 	struct sockaddr_storage addr;
1962 	socklen_t len;
1963 
1964 	debug("entering test_connect_close()");
1965 	SOCKET(server_sd, info->domain, info->type, 0);
1966 
1967 	on = 1;
1968 	(void)setsockopt(server_sd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
1969 
1970 	if (bind(server_sd, info->serveraddr, info->serveraddrlen) == -1)
1971 		test_fail("bind() should have worked");
1972 
1973 	if (info->callback_set_listen_opt != NULL)
1974 		info->callback_set_listen_opt(server_sd);
1975 
1976 	if (listen(server_sd, 8) == -1)
1977 		test_fail("listen() should have worked");
1978 
1979 	fcntl(server_sd, F_SETFL, fcntl(server_sd, F_GETFL) | O_NONBLOCK);
1980 
1981 	check_select(server_sd, 0 /*read*/, 1 /*write*/, 0 /*block*/);
1982 
1983 	SOCKET(client_sd, info->domain, info->type, 0);
1984 
1985 	fcntl(client_sd, F_SETFL, fcntl(client_sd, F_GETFL) | O_NONBLOCK);
1986 
1987 	if (connect(client_sd, info->clientaddr, info->clientaddrlen) != -1 ||
1988 		errno != EINPROGRESS)
1989 		test_fail("connect() should have yielded EINPROGRESS");
1990 
1991 	check_select_cond(client_sd, 0 /*read*/, 0 /*write*/, 0 /*block*/,
1992 		!info->ignore_select_delay);
1993 	check_select_cond(server_sd, 1 /*read*/, 1 /*write*/, 0 /*block*/,
1994 		!info->ignore_select_delay);
1995 
1996 	close(client_sd);
1997 
1998 	check_select_cond(server_sd, 0 /*read*/, 1 /*write*/, 0 /*block*/,
1999 		!info->ignore_select_delay);
2000 
2001 	len = sizeof(addr);
2002 	errno = 0;
2003 	if ((sd = accept(server_sd, (struct sockaddr *) &addr, &len)) != -1) {
2004 		if (!info->ignore_accept_delay) {
2005 			test_fail("accept() should have failed");
2006 		}
2007 		close(sd);
2008 	} else if (errno != EAGAIN) {
2009 		test_fail("accept() should have yielded EAGAIN");
2010 	}
2011 	close(server_sd);
2012 
2013 	info->callback_cleanup();
2014 	debug("leaving test_connect_close()");
2015 }
2016 
2017 /*
2018  * Verify that closing a listening socket will cause a blocking connect to fail
2019  * with ECONNRESET, and that a subsequent write will yield EPIPE.  This test
2020  * works only if the connect(2) does not succeed before accept(2) is called at
2021  * all, which means it is limited to UDS with LOCAL_CONNWAIT right now.
2022  */
2023 void
2024 test_listen_close(const struct socket_test_info *info)
2025 {
2026 	int server_sd, client_sd;
2027 	int status, on;
2028 	char byte;
2029 
2030 	debug("entering test_listen_close()");
2031 	SOCKET(server_sd, info->domain, info->type, 0);
2032 
2033 	on = 1;
2034 	(void)setsockopt(server_sd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
2035 
2036 	if (bind(server_sd, info->serveraddr, info->serveraddrlen) == -1)
2037 		test_fail("bind() should have worked");
2038 
2039 	if (info->callback_set_listen_opt != NULL)
2040 		info->callback_set_listen_opt(server_sd);
2041 
2042 	if (listen(server_sd, 8) == -1)
2043 		test_fail("listen() should have worked");
2044 
2045 	switch (fork()) {
2046 	case 0:
2047 		sleep(1);
2048 
2049 		exit(0);
2050 	case -1:
2051 		test_fail("can't fork");
2052 	default:
2053 		break;
2054 	}
2055 
2056 	close(server_sd);
2057 
2058 	SOCKET(client_sd, info->domain, info->type, 0);
2059 
2060 	byte = 0;
2061 	if (write(client_sd, &byte, 1) != -1 || errno != ENOTCONN)
2062 		test_fail("write() should have yielded ENOTCONN");
2063 
2064 	if (connect(client_sd, info->clientaddr, info->clientaddrlen) != -1) {
2065 		test_fail("connect() should have failed");
2066 	} else if (errno != ECONNRESET) {
2067 		test_fail("connect() should have yielded ECONNRESET");
2068 	}
2069 
2070 	/*
2071 	 * The error we get on the next write() depends on whether the socket
2072 	 * may be reused after a failed connect.  For UDS, it may be, so we get
2073 	 * ENOTCONN.  Otherwise we would expect EPIPE.
2074 	 */
2075 	if (write(client_sd, &byte, 1) != -1 || errno != ENOTCONN)
2076 		test_fail("write() should have yielded ENOTCONN");
2077 
2078 	close(client_sd);
2079 
2080 	if (wait(&status) <= 0)
2081 		test_fail("wait() should have succeeded");
2082 	if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
2083 		test_fail("child process failed the test");
2084 
2085 	info->callback_cleanup();
2086 	debug("leaving test_listen_close()");
2087 }
2088 
2089 /*
2090  * Verify that closing a listening socket will cause a nonblocking connect to
2091  * result in the socket becoming readable and writable, and yielding ECONNRESET
2092  * and EPIPE on the next two writes, respectively.
2093  */
2094 void
2095 test_listen_close_nb(const struct socket_test_info *info)
2096 {
2097 	int server_sd, client_sd;
2098 	int status, on;
2099 	char byte;
2100 
2101 	debug("entering test_listen_close_nb()");
2102 	SOCKET(server_sd, info->domain, info->type, 0);
2103 
2104 	on = 1;
2105 	(void)setsockopt(server_sd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
2106 
2107 	if (bind(server_sd, info->serveraddr, info->serveraddrlen) == -1)
2108 		test_fail("bind() should have worked");
2109 
2110 	if (info->callback_set_listen_opt != NULL)
2111 		info->callback_set_listen_opt(server_sd);
2112 
2113 	if (listen(server_sd, 8) == -1)
2114 		test_fail("listen() should have worked");
2115 
2116 	switch (fork()) {
2117 	case 0:
2118 		sleep(1);
2119 
2120 		exit(0);
2121 	case -1:
2122 		test_fail("can't fork");
2123 	default:
2124 		break;
2125 	}
2126 
2127 	close(server_sd);
2128 
2129 	SOCKET(client_sd, info->domain, info->type, 0);
2130 
2131 	fcntl(client_sd, F_SETFL, fcntl(client_sd, F_GETFL) | O_NONBLOCK);
2132 
2133 	if (connect(client_sd, info->clientaddr, info->clientaddrlen) != -1 ||
2134 		errno != EINPROGRESS)
2135 		test_fail("connect() should have yielded EINPROGRESS");
2136 
2137 	check_select_cond(client_sd, 0 /*read*/, 0 /*write*/, 0 /*block*/,
2138 		!info->ignore_select_delay);
2139 	check_select_cond(client_sd, 1 /*read*/, 1 /*write*/, 1 /*block*/,
2140 		!info->ignore_select_delay);
2141 
2142 	byte = 0;
2143 	if (write(client_sd, &byte, 1) != -1) {
2144 		if (!info->ignore_write_conn_reset) {
2145 			test_fail("write() should have failed");
2146 		}
2147 	} else if (errno != ECONNRESET) {
2148 		test_fail("write() should have yielded ECONNRESET");
2149 	}
2150 
2151 	check_select_cond(client_sd, 1 /*read*/, 1 /*write*/, 0 /*block*/,
2152 		!info->ignore_select_delay);
2153 
2154 	close(client_sd);
2155 
2156 	if (wait(&status) <= 0)
2157 		test_fail("wait() should have succeeded");
2158 	if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
2159 		test_fail("child process failed the test");
2160 
2161 	info->callback_cleanup();
2162 	debug("leaving test_listen_close_nb()");
2163 }
2164