2 * This file and its contents are supplied under the terms of the
3 * Common Development and Distribution License ("CDDL"), version 1.0.
4 * You may only use this file in accordance with the terms of version
7 * A full copy of the text of the CDDL should have accompanied this
8 * source. A copy of the CDDL is also available via the Internet at
9 * http://www.illumos.org/license/CDDL.
12 /* Copyright 2016, Richard Lowe. */
14 #include <sys/select.h>
17 #include <sys/types.h>
28 diff_sets(fd_set
*a
, fd_set
*b
, size_t size
)
30 for (int i
= 0; i
< size
; i
++) {
31 if (FD_ISSET(i
, a
) != FD_ISSET(i
, b
))
32 printf("fd %d: %d should be %d\n", i
, FD_ISSET(i
, a
),
38 print_set(fd_set
*a
, size_t size
)
40 for (int i
= 0; i
< size
; i
++) {
51 main(int argc
, char **argv
)
53 struct timeval tv
= {0, 0};
55 fd_set
*sread
= NULL
, *swrite
= NULL
, *serr
= NULL
;
56 int null
, zero
, maxfd
= -1, nfds
;
60 errx(1, "usage: select_test <number of fds>");
64 err(1, "couldn't convert %s to int", argv
[1]);
66 if (nfds
> FD_SETSIZE
)
67 errx(1, "can't have more fds than FD_SETSIZE %d", FD_SETSIZE
);
72 switch (arc4random_uniform(3)) {
86 if ((null
= open("/dev/null", O_RDONLY
)) == -1)
87 err(1, "couldn't open /dev/null");
91 if ((zero
= open("/dev/zero", O_RDWR
)) == -1)
92 err(1, "couldn't open /dev/zero");
94 for (int i
= zero
; i
< (zero
+ nfds
); i
++) {
95 int fd
= (serr
!= NULL
) ? null
: zero
;
96 if (arc4random_uniform(100) > 90) {
100 if (dup2(fd
, i
) == -1)
101 err(1, "couldn't dup fd to fd %d", i
);
107 else if (sread
!= NULL
)
109 else if (serr
!= NULL
)
112 print_set(&proto
, 80);
114 memcpy(&check
, &proto
, sizeof (check
));
116 if (select(maxfd
+ 1, sread
, swrite
, serr
, &tv
) == -1)
117 err(1, "select failed");
119 if (memcmp(&check
, &proto
, sizeof (check
)) != 0) {
120 diff_sets(&check
, &proto
, sizeof (check
));
121 warnx("fd set mismatch: check: %p proto: %p", &check
, &proto
);