1 /* Test of select() substitute.
2 Copyright (C) 2008-2018 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Paolo Bonzini, 2008. */
21 #include <sys/socket.h>
22 #include <netinet/in.h>
23 #include <arpa/inet.h>
28 #include <sys/ioctl.h>
33 #if defined _WIN32 && ! defined __CYGWIN__
34 # define WINDOWS_NATIVE
37 #ifdef HAVE_SYS_WAIT_H
38 # include <sys/wait.h>
41 #define TEST_PORT 12345
44 typedef int (*select_fn
) (int, fd_set
*, fd_set
*, fd_set
*, struct timeval
*);
47 /* Minimal testing infrastructure. */
52 failed (const char *reason
)
56 printf ("failed (%s)\n", reason
);
60 test (void (*fn
) (select_fn
), select_fn my_select
, const char *msg
)
63 printf ("%s... ", msg
);
74 /* Funny socket code. */
77 open_server_socket (void)
80 struct sockaddr_in ia
;
82 s
= socket (AF_INET
, SOCK_STREAM
, 0);
85 setsockopt (s
, SOL_SOCKET
, SO_REUSEPORT
, &x
, sizeof (x
));
87 memset (&ia
, 0, sizeof (ia
));
88 ia
.sin_family
= AF_INET
;
89 inet_pton (AF_INET
, "127.0.0.1", &ia
.sin_addr
);
90 ia
.sin_port
= htons (TEST_PORT
);
91 if (bind (s
, (struct sockaddr
*) &ia
, sizeof (ia
)) < 0)
97 if (listen (s
, 1) < 0)
107 connect_to_socket (bool blocking
)
110 struct sockaddr_in ia
;
112 s
= socket (AF_INET
, SOCK_STREAM
, 0);
114 memset (&ia
, 0, sizeof (ia
));
115 ia
.sin_family
= AF_INET
;
116 inet_pton (AF_INET
, "127.0.0.1", &ia
.sin_addr
);
117 ia
.sin_port
= htons (TEST_PORT
);
121 #ifdef WINDOWS_NATIVE
122 unsigned long iMode
= 1;
123 ioctl (s
, FIONBIO
, (char *) &iMode
);
125 #elif defined F_GETFL
126 int oldflags
= fcntl (s
, F_GETFL
, NULL
);
128 if (!(oldflags
& O_NONBLOCK
))
129 fcntl (s
, F_SETFL
, oldflags
| O_NONBLOCK
);
133 if (connect (s
, (struct sockaddr
*) &ia
, sizeof (ia
)) < 0
134 && (blocking
|| errno
!= EINPROGRESS
))
144 /* A slightly more convenient interface to select(2).
145 Waits until a specific event occurs on a file descriptor FD.
146 EV is a bit mask of events to look for:
147 SEL_IN - input can be polled without blocking,
148 SEL_OUT - output can be provided without blocking,
149 SEL_EXC - an exception occurred,
150 A maximum wait time is specified by TIMEOUT.
151 *TIMEOUT = { 0, 0 } means to return immediately,
152 TIMEOUT = NULL means to wait indefinitely. */
154 enum { SEL_IN
= 1, SEL_OUT
= 2, SEL_EXC
= 4 };
157 do_select (int fd
, int ev
, struct timeval
*timeout
, select_fn my_select
)
159 fd_set rfds
, wfds
, xfds
;
171 r
= my_select (fd
+ 1, &rfds
, &wfds
, &xfds
, timeout
);
176 if (FD_ISSET (fd
, &rfds
))
178 if (FD_ISSET (fd
, &wfds
))
180 if (FD_ISSET (fd
, &xfds
))
183 failed ("select returned 0");
185 failed ("select returned unrequested events");
191 do_select_nowait (int fd
, int ev
, select_fn my_select
)
196 return do_select (fd
, ev
, &tv0
, my_select
);
200 do_select_wait (int fd
, int ev
, select_fn my_select
)
202 return do_select (fd
, ev
, NULL
, my_select
);
206 /* Test select(2) for TTYs. */
210 test_tty (select_fn my_select
)
212 if (do_select_nowait (0, SEL_IN
, my_select
) != 0)
214 if (do_select_nowait (0, SEL_OUT
, my_select
) == 0)
215 failed ("cannot write");
217 if (do_select_wait (0, SEL_IN
, my_select
) == 0)
218 failed ("return with infinite timeout");
221 if (do_select_nowait (0, SEL_IN
, my_select
) != 0)
222 failed ("can read after getc");
228 do_select_bad_nfd_nowait (int nfd
, select_fn my_select
)
234 return my_select (nfd
, NULL
, NULL
, NULL
, &tv0
);
238 test_bad_nfd (select_fn my_select
)
240 if (do_select_bad_nfd_nowait (-1, my_select
) != -1 || errno
!= EINVAL
)
241 failed ("invalid errno after negative nfds");
242 /* Can't test FD_SETSIZE + 1 for EINVAL, since some systems allow
243 dynamically larger set size by redefining FD_SETSIZE anywhere up
244 to the actual maximum fd. */
245 /* if (do_select_bad_nfd_nowait (FD_SETSIZE + 1, my_select) != -1 */
246 /* || errno != EINVAL) */
247 /* failed ("invalid errno after bogus nfds"); */
250 /* Test select(2) on invalid file descriptors. */
253 do_select_bad_fd (int fd
, int ev
, struct timeval
*timeout
, select_fn my_select
)
255 fd_set rfds
, wfds
, xfds
;
267 return my_select (fd
+ 1, &rfds
, &wfds
, &xfds
, timeout
);
268 /* In this case, when fd is invalid, on some platforms, the bit for fd
269 is left alone in the fd_set, whereas on other platforms it is cleared.
270 So, don't check the bit for fd here. */
274 do_select_bad_fd_nowait (int fd
, int ev
, select_fn my_select
)
279 return do_select_bad_fd (fd
, ev
, &tv0
, my_select
);
283 test_bad_fd (select_fn my_select
)
285 /* This tests fails on OSF/1 and native Windows, even with fd = 16. */
286 #if !(defined __osf__ || defined WINDOWS_NATIVE)
289 /* On Linux, Mac OS X, *BSD, values of fd like 99 or 399 are discarded
290 by the kernel early and therefore do *not* lead to EBADF, as required
292 # if defined __linux__ || (defined __APPLE__ && defined __MACH__) || (defined __FreeBSD__ || defined __DragonFly__) || defined __OpenBSD__ || defined __NetBSD__
299 if (do_select_bad_fd_nowait (fd
, SEL_IN
, my_select
) == 0 || errno
!= EBADF
)
300 failed ("invalid fd among rfds");
301 if (do_select_bad_fd_nowait (fd
, SEL_OUT
, my_select
) == 0 || errno
!= EBADF
)
302 failed ("invalid fd among wfds");
303 if (do_select_bad_fd_nowait (fd
, SEL_EXC
, my_select
) == 0 || errno
!= EBADF
)
304 failed ("invalid fd among xfds");
309 /* Test select(2) for unconnected nonblocking sockets. */
312 test_connect_first (select_fn my_select
)
314 int s
= open_server_socket ();
315 struct sockaddr_in ia
;
320 if (do_select_nowait (s
, SEL_IN
| SEL_EXC
, my_select
) != 0)
321 failed ("can read, socket not connected");
323 c1
= connect_to_socket (false);
325 if (do_select_wait (s
, SEL_IN
| SEL_EXC
, my_select
) != SEL_IN
)
326 failed ("expecting readability on passive socket");
327 if (do_select_nowait (s
, SEL_IN
| SEL_EXC
, my_select
) != SEL_IN
)
328 failed ("expecting readability on passive socket");
330 addrlen
= sizeof (ia
);
331 c2
= accept (s
, (struct sockaddr
*) &ia
, &addrlen
);
332 ASSERT (close (s
) == 0);
333 ASSERT (close (c1
) == 0);
334 ASSERT (close (c2
) == 0);
338 /* Test select(2) for unconnected blocking sockets. */
341 test_accept_first (select_fn my_select
)
343 #ifndef WINDOWS_NATIVE
344 int s
= open_server_socket ();
345 struct sockaddr_in ia
;
356 addrlen
= sizeof (ia
);
357 c
= accept (s
, (struct sockaddr
*) &ia
, &addrlen
);
358 ASSERT (close (s
) == 0);
359 ASSERT (write (c
, "foo", 3) == 3);
360 ASSERT (read (c
, buf
, 3) == 3);
361 shutdown (c
, SHUT_RD
);
362 ASSERT (close (c
) == 0);
367 ASSERT (close (s
) == 0);
368 c
= connect_to_socket (true);
369 if (do_select_nowait (c
, SEL_OUT
, my_select
) != SEL_OUT
)
370 failed ("cannot write after blocking connect");
371 ASSERT (write (c
, "foo", 3) == 3);
373 if (do_select_wait (c
, SEL_IN
, my_select
) != SEL_IN
)
374 failed ("cannot read data left in the socket by closed process");
375 ASSERT (read (c
, buf
, 3) == 3);
376 ASSERT (write (c
, "foo", 3) == 3);
377 (void) close (c
); /* may fail with errno = ECONNRESET */
383 /* Common code for pipes and connected sockets. */
386 test_pair (int rd
, int wd
, select_fn my_select
)
389 if (do_select_wait (wd
, SEL_IN
| SEL_OUT
| SEL_EXC
, my_select
) != SEL_OUT
)
390 failed ("expecting writability before writing");
391 if (do_select_nowait (wd
, SEL_IN
| SEL_OUT
| SEL_EXC
, my_select
) != SEL_OUT
)
392 failed ("expecting writability before writing");
394 ASSERT (write (wd
, "foo", 3) == 3);
395 if (do_select_wait (rd
, SEL_IN
, my_select
) != SEL_IN
)
396 failed ("expecting readability after writing");
397 if (do_select_nowait (rd
, SEL_IN
, my_select
) != SEL_IN
)
398 failed ("expecting readability after writing");
400 ASSERT (read (rd
, buf
, 3) == 3);
404 /* Test select(2) on connected sockets. */
407 test_socket_pair (select_fn my_select
)
409 struct sockaddr_in ia
;
411 socklen_t addrlen
= sizeof (ia
);
412 int s
= open_server_socket ();
413 int c1
= connect_to_socket (false);
414 int c2
= accept (s
, (struct sockaddr
*) &ia
, &addrlen
);
416 ASSERT (close (s
) == 0);
418 test_pair (c1
, c2
, my_select
);
419 ASSERT (close (c1
) == 0);
420 ASSERT (write (c2
, "foo", 3) == 3);
421 (void) close (c2
); /* may fail with errno = ECONNRESET */
425 /* Test select(2) on pipes. */
428 test_pipe (select_fn my_select
)
432 ASSERT (pipe (fd
) == 0);
433 test_pair (fd
[0], fd
[1], my_select
);
434 ASSERT (close (fd
[0]) == 0);
435 ASSERT (close (fd
[1]) == 0);
442 test_function (select_fn my_select
)
447 printf ("Please press Enter\n");
448 test (test_tty
, "TTY", my_select
);
451 result
+= test (test_bad_nfd
, my_select
, "Invalid nfd test");
452 result
+= test (test_bad_fd
, my_select
, "Invalid fd test");
453 result
+= test (test_connect_first
, my_select
, "Unconnected socket test");
454 result
+= test (test_socket_pair
, my_select
, "Connected sockets test");
455 result
+= test (test_accept_first
, my_select
, "General socket test with fork");
456 result
+= test (test_pipe
, my_select
, "Pipe test");