1 /* Test of select() substitute.
2 Copyright (C) 2008-2011 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 <http://www.gnu.org/licenses/>. */
17 /* Written by Paolo Bonzini, 2008. */
21 #include <sys/select.h>
23 #include "signature.h"
24 SIGNATURE_CHECK (select, int, (int, fd_set *, fd_set *, fd_set *,
26 /* The following may be macros without underlying functions, so only
27 check signature if they are not macros. */
29 SIGNATURE_CHECK (FD_CLR, void, (int, fd_set *));
32 SIGNATURE_CHECK (FD_ISSET, void, (int, fd_set *));
35 SIGNATURE_CHECK (FD_SET, int, (int, fd_set *));
38 SIGNATURE_CHECK (FD_ZERO, void, (fd_set *));
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
49 #include <sys/ioctl.h>
54 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
58 #ifdef HAVE_SYS_WAIT_H
59 # include <sys/wait.h>
63 # define SO_REUSEPORT SO_REUSEADDR
66 #define TEST_PORT 12345
69 /* Minimal testing infrastructure. */
74 failed (const char *reason)
78 printf ("failed (%s)\n", reason);
82 test (void (*fn) (void), const char *msg)
85 printf ("%s... ", msg);
96 /* Funny socket code. */
99 open_server_socket (void)
102 struct sockaddr_in ia;
104 s = socket (AF_INET, SOCK_STREAM, 0);
106 memset (&ia, 0, sizeof (ia));
107 ia.sin_family = AF_INET;
108 inet_pton (AF_INET, "127.0.0.1", &ia.sin_addr);
109 ia.sin_port = htons (TEST_PORT);
110 if (bind (s, (struct sockaddr *) &ia, sizeof (ia)) < 0)
117 setsockopt (s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof (x));
119 if (listen (s, 1) < 0)
129 connect_to_socket (bool blocking)
132 struct sockaddr_in ia;
134 s = socket (AF_INET, SOCK_STREAM, 0);
136 memset (&ia, 0, sizeof (ia));
137 ia.sin_family = AF_INET;
138 inet_pton (AF_INET, "127.0.0.1", &ia.sin_addr);
139 ia.sin_port = htons (TEST_PORT);
144 unsigned long iMode = 1;
145 ioctl (s, FIONBIO, (char *) &iMode);
147 #elif defined F_GETFL
148 int oldflags = fcntl (s, F_GETFL, NULL);
150 if (!(oldflags & O_NONBLOCK))
151 fcntl (s, F_SETFL, oldflags | O_NONBLOCK);
155 if (connect (s, (struct sockaddr *) &ia, sizeof (ia)) < 0
156 && (blocking || errno != EINPROGRESS))
166 /* A slightly more convenient interface to select(2).
167 Waits until a specific event occurs on a file descriptor FD.
168 EV is a bit mask of events to look for:
169 SEL_IN - input can be polled without blocking,
170 SEL_OUT - output can be provided without blocking,
171 SEL_EXC - an exception occurred,
172 A maximum wait time is specified by TIMEOUT.
173 *TIMEOUT = { 0, 0 } means to return immediately,
174 TIMEOUT = NULL means to wait indefinitely. */
176 enum { SEL_IN = 1, SEL_OUT = 2, SEL_EXC = 4 };
179 do_select (int fd, int ev, struct timeval *timeout)
181 fd_set rfds, wfds, xfds;
193 r = select (fd + 1, &rfds, &wfds, &xfds, timeout);
198 if (FD_ISSET (fd, &rfds))
200 if (FD_ISSET (fd, &wfds))
202 if (FD_ISSET (fd, &xfds))
205 failed ("select returned 0");
207 failed ("select returned unrequested events");
213 do_select_nowait (int fd, int ev)
218 return do_select (fd, ev, &tv0);
222 do_select_wait (int fd, int ev)
224 return do_select (fd, ev, NULL);
228 /* Test select(2) for TTYs. */
234 if (do_select_nowait (0, SEL_IN) != 0)
236 if (do_select_nowait (0, SEL_OUT) == 0)
237 failed ("cannot write");
239 if (do_select_wait (0, SEL_IN) == 0)
240 failed ("return with infinite timeout");
243 if (do_select_nowait (0, SEL_IN) != 0)
244 failed ("can read after getc");
249 /* Test select(2) for unconnected nonblocking sockets. */
252 test_connect_first (void)
254 int s = open_server_socket ();
255 struct sockaddr_in ia;
260 if (do_select_nowait (s, SEL_IN | SEL_EXC) != 0)
261 failed ("can read, socket not connected");
263 c1 = connect_to_socket (false);
265 if (do_select_wait (s, SEL_IN | SEL_EXC) != SEL_IN)
266 failed ("expecting readability on passive socket");
267 if (do_select_nowait (s, SEL_IN | SEL_EXC) != SEL_IN)
268 failed ("expecting readability on passive socket");
270 addrlen = sizeof (ia);
271 c2 = accept (s, (struct sockaddr *) &ia, &addrlen);
272 ASSERT (close (s) == 0);
273 ASSERT (close (c1) == 0);
274 ASSERT (close (c2) == 0);
278 /* Test select(2) for unconnected blocking sockets. */
281 test_accept_first (void)
284 int s = open_server_socket ();
285 struct sockaddr_in ia;
296 addrlen = sizeof (ia);
297 c = accept (s, (struct sockaddr *) &ia, &addrlen);
298 ASSERT (close (s) == 0);
299 ASSERT (write (c, "foo", 3) == 3);
300 ASSERT (read (c, buf, 3) == 3);
301 shutdown (c, SHUT_RD);
302 ASSERT (close (c) == 0);
307 ASSERT (close (s) == 0);
308 c = connect_to_socket (true);
309 if (do_select_nowait (c, SEL_OUT) != SEL_OUT)
310 failed ("cannot write after blocking connect");
311 ASSERT (write (c, "foo", 3) == 3);
313 if (do_select_wait (c, SEL_IN) != SEL_IN)
314 failed ("cannot read data left in the socket by closed process");
315 ASSERT (read (c, buf, 3) == 3);
316 ASSERT (write (c, "foo", 3) == 3);
317 (void) close (c); /* may fail with errno = ECONNRESET */
323 /* Common code for pipes and connected sockets. */
326 test_pair (int rd, int wd)
329 if (do_select_wait (wd, SEL_IN | SEL_OUT | SEL_EXC) != SEL_OUT)
330 failed ("expecting writability before writing");
331 if (do_select_nowait (wd, SEL_IN | SEL_OUT | SEL_EXC) != SEL_OUT)
332 failed ("expecting writability before writing");
334 ASSERT (write (wd, "foo", 3) == 3);
335 if (do_select_wait (rd, SEL_IN) != SEL_IN)
336 failed ("expecting readability after writing");
337 if (do_select_nowait (rd, SEL_IN) != SEL_IN)
338 failed ("expecting readability after writing");
340 ASSERT (read (rd, buf, 3) == 3);
344 /* Test select(2) on connected sockets. */
347 test_socket_pair (void)
349 struct sockaddr_in ia;
351 socklen_t addrlen = sizeof (ia);
352 int s = open_server_socket ();
353 int c1 = connect_to_socket (false);
354 int c2 = accept (s, (struct sockaddr *) &ia, &addrlen);
356 ASSERT (close (s) == 0);
359 ASSERT (close (c1) == 0);
360 ASSERT (write (c2, "foo", 3) == 3);
361 (void) close (c2); /* may fail with errno = ECONNRESET */
365 /* Test select(2) on pipes. */
372 ASSERT (pipe (fd) == 0);
373 test_pair (fd[0], fd[1]);
374 ASSERT (close (fd[0]) == 0);
375 ASSERT (close (fd[1]) == 0);
387 printf ("Please press Enter\n");
388 test (test_tty, "TTY");
391 result = test (test_connect_first, "Unconnected socket test");
392 result += test (test_socket_pair, "Connected sockets test");
393 result += test (test_accept_first, "General socket test with fork");
394 result += test (test_pipe, "Pipe test");