1*41fbaed0Stron /*	$NetBSD: select_bug.c,v 1.1.1.1 2009/06/23 10:09:00 tron Exp $	*/
2*41fbaed0Stron 
3*41fbaed0Stron /*++
4*41fbaed0Stron /* NAME
5*41fbaed0Stron /*	select_bug 1
6*41fbaed0Stron /* SUMMARY
7*41fbaed0Stron /*	select test program
8*41fbaed0Stron /* SYNOPSIS
9*41fbaed0Stron /*	select_bug
10*41fbaed0Stron /* DESCRIPTION
11*41fbaed0Stron /*	select_bug forks child processes that perform select()
12*41fbaed0Stron /*	on a shared socket, and sees if a wakeup affects other
13*41fbaed0Stron /*	processes selecting on a different socket or stdin.
14*41fbaed0Stron /* DIAGNOSTICS
15*41fbaed0Stron /*	Problems are reported to the standard error stream.
16*41fbaed0Stron /* LICENSE
17*41fbaed0Stron /* .ad
18*41fbaed0Stron /* .fi
19*41fbaed0Stron /*	The Secure Mailer license must be distributed with this software.
20*41fbaed0Stron /* AUTHOR(S)
21*41fbaed0Stron /*	Wietse Venema
22*41fbaed0Stron /*	IBM T.J. Watson Research
23*41fbaed0Stron /*	P.O. Box 704
24*41fbaed0Stron /*	Yorktown Heights, NY 10598, USA
25*41fbaed0Stron /*--*/
26*41fbaed0Stron 
27*41fbaed0Stron /* System library. */
28*41fbaed0Stron 
29*41fbaed0Stron #include <sys_defs.h>
30*41fbaed0Stron #include <sys/time.h>
31*41fbaed0Stron #include <sys/socket.h>
32*41fbaed0Stron #include <sys/wait.h>
33*41fbaed0Stron #include <unistd.h>
34*41fbaed0Stron #include <stdlib.h>
35*41fbaed0Stron #include <string.h>			/* bzero() prototype for 44BSD */
36*41fbaed0Stron 
37*41fbaed0Stron /* Utility library. */
38*41fbaed0Stron 
39*41fbaed0Stron #include <msg.h>
40*41fbaed0Stron #include <vstream.h>
41*41fbaed0Stron #include <msg_vstream.h>
42*41fbaed0Stron 
fork_and_read_select(const char * what,int delay,int fd)43*41fbaed0Stron static pid_t fork_and_read_select(const char *what, int delay, int fd)
44*41fbaed0Stron {
45*41fbaed0Stron     struct timeval tv;
46*41fbaed0Stron     pid_t   pid;
47*41fbaed0Stron     fd_set  readfds;
48*41fbaed0Stron 
49*41fbaed0Stron     switch (pid = fork()) {
50*41fbaed0Stron     case -1:
51*41fbaed0Stron 	msg_fatal("fork: %m");
52*41fbaed0Stron     case 0:
53*41fbaed0Stron 	tv.tv_sec = delay;
54*41fbaed0Stron 	tv.tv_usec = 0;
55*41fbaed0Stron 	FD_ZERO(&readfds);
56*41fbaed0Stron 	FD_SET(fd, &readfds);
57*41fbaed0Stron 	switch (select(fd + 1, &readfds, (fd_set *) 0, &readfds, &tv)) {
58*41fbaed0Stron 	case -1:
59*41fbaed0Stron 	    msg_fatal("select: %m");
60*41fbaed0Stron 	case 0:
61*41fbaed0Stron 	    msg_info("%s select timed out", what);
62*41fbaed0Stron 	    exit(0);
63*41fbaed0Stron 	default:
64*41fbaed0Stron 	    msg_info("%s select wakeup", what);
65*41fbaed0Stron 	    exit(0);
66*41fbaed0Stron 	}
67*41fbaed0Stron     default:
68*41fbaed0Stron 	return (pid);
69*41fbaed0Stron     }
70*41fbaed0Stron }
71*41fbaed0Stron 
main(int argc,char ** argv)72*41fbaed0Stron int     main(int argc, char **argv)
73*41fbaed0Stron {
74*41fbaed0Stron     int     pair1[2];
75*41fbaed0Stron     int     pair2[2];
76*41fbaed0Stron 
77*41fbaed0Stron     msg_vstream_init(argv[0], VSTREAM_ERR);
78*41fbaed0Stron 
79*41fbaed0Stron #define DELAY 1
80*41fbaed0Stron 
81*41fbaed0Stron     if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair1) < 0)
82*41fbaed0Stron 	msg_fatal("socketpair: %m");
83*41fbaed0Stron     if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair2) < 0)
84*41fbaed0Stron 	msg_fatal("socketpair: %m");
85*41fbaed0Stron 
86*41fbaed0Stron     vstream_printf("Doing multiple select on socket1, then write to it...\n");
87*41fbaed0Stron     vstream_fflush(VSTREAM_OUT);
88*41fbaed0Stron     fork_and_read_select("socket1", DELAY, pair1[0]);	/* one */
89*41fbaed0Stron     fork_and_read_select("socket1", DELAY, pair1[0]);	/* two */
90*41fbaed0Stron     fork_and_read_select("socket2", DELAY, pair2[0]);
91*41fbaed0Stron     fork_and_read_select("stdin", DELAY, 0);
92*41fbaed0Stron     if (write(pair1[1], "", 1) != 1)
93*41fbaed0Stron 	msg_fatal("write: %m");
94*41fbaed0Stron     while (wait((int *) 0) >= 0)
95*41fbaed0Stron 	 /* void */ ;
96*41fbaed0Stron     return (0);
97*41fbaed0Stron }
98