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"
26 SIGNATURE_CHECK (pselect, int,
27 (int, fd_set *restrict, fd_set *restrict, fd_set *restrict,
28 struct timespec const *restrict, const sigset_t *restrict));
30 SIGNATURE_CHECK (select, int, (int, fd_set *, fd_set *, fd_set *,
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
42 #include <sys/ioctl.h>
47 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
51 #ifdef HAVE_SYS_WAIT_H
52 # include <sys/wait.h>
56 # define SO_REUSEPORT SO_REUSEADDR
59 #define TEST_PORT 12345
62 /* Minimal testing infrastructure. */
67 failed (const char *reason)
71 printf ("failed (%s)\n", reason);
75 test (void (*fn) (void), const char *msg)
78 printf ("%s... ", msg);
89 /* Funny socket code. */
92 open_server_socket (void)
95 struct sockaddr_in ia;
97 s = socket (AF_INET, SOCK_STREAM, 0);
99 memset (&ia, 0, sizeof (ia));
100 ia.sin_family = AF_INET;
101 inet_pton (AF_INET, "127.0.0.1", &ia.sin_addr);
102 ia.sin_port = htons (TEST_PORT);
103 if (bind (s, (struct sockaddr *) &ia, sizeof (ia)) < 0)
110 setsockopt (s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof (x));
112 if (listen (s, 1) < 0)
122 connect_to_socket (bool blocking)
125 struct sockaddr_in ia;
127 s = socket (AF_INET, SOCK_STREAM, 0);
129 memset (&ia, 0, sizeof (ia));
130 ia.sin_family = AF_INET;
131 inet_pton (AF_INET, "127.0.0.1", &ia.sin_addr);
132 ia.sin_port = htons (TEST_PORT);
137 unsigned long iMode = 1;
138 ioctl (s, FIONBIO, (char *) &iMode);
140 #elif defined F_GETFL
141 int oldflags = fcntl (s, F_GETFL, NULL);
143 if (!(oldflags & O_NONBLOCK))
144 fcntl (s, F_SETFL, oldflags | O_NONBLOCK);
148 if (connect (s, (struct sockaddr *) &ia, sizeof (ia)) < 0
149 && (blocking || errno != EINPROGRESS))
159 /* A slightly more convenient interface to select(2).
160 Waits until a specific event occurs on a file descriptor FD.
161 EV is a bit mask of events to look for:
162 SEL_IN - input can be polled without blocking,
163 SEL_OUT - output can be provided without blocking,
164 SEL_EXC - an exception occurred,
165 A maximum wait time is specified by TIMEOUT.
166 *TIMEOUT = { 0, 0 } means to return immediately,
167 TIMEOUT = NULL means to wait indefinitely. */
169 enum { SEL_IN = 1, SEL_OUT = 2, SEL_EXC = 4 };
172 do_select (int fd, int ev, struct timeval *timeout)
174 fd_set rfds, wfds, xfds;
188 struct timespec ts, *pts = NULL;
191 ts.tv_sec = timeout->tv_sec;
192 ts.tv_nsec = timeout->tv_usec * 1000;
195 r = pselect (fd + 1, &rfds, &wfds, &xfds, pts, NULL);
198 r = select (fd + 1, &rfds, &wfds, &xfds, timeout);
204 if (FD_ISSET (fd, &rfds))
206 if (FD_ISSET (fd, &wfds))
208 if (FD_ISSET (fd, &xfds))
211 failed ("select returned 0");
213 failed ("select returned unrequested events");
219 do_select_nowait (int fd, int ev)
224 return do_select (fd, ev, &tv0);
228 do_select_wait (int fd, int ev)
230 return do_select (fd, ev, NULL);
234 /* Test select(2) for TTYs. */
240 if (do_select_nowait (0, SEL_IN) != 0)
242 if (do_select_nowait (0, SEL_OUT) == 0)
243 failed ("cannot write");
245 if (do_select_wait (0, SEL_IN) == 0)
246 failed ("return with infinite timeout");
249 if (do_select_nowait (0, SEL_IN) != 0)
250 failed ("can read after getc");
255 /* Test select(2) for unconnected nonblocking sockets. */
258 test_connect_first (void)
260 int s = open_server_socket ();
261 struct sockaddr_in ia;
266 if (do_select_nowait (s, SEL_IN | SEL_EXC) != 0)
267 failed ("can read, socket not connected");
269 c1 = connect_to_socket (false);
271 if (do_select_wait (s, SEL_IN | SEL_EXC) != SEL_IN)
272 failed ("expecting readability on passive socket");
273 if (do_select_nowait (s, SEL_IN | SEL_EXC) != SEL_IN)
274 failed ("expecting readability on passive socket");
276 addrlen = sizeof (ia);
277 c2 = accept (s, (struct sockaddr *) &ia, &addrlen);
278 ASSERT (close (s) == 0);
279 ASSERT (close (c1) == 0);
280 ASSERT (close (c2) == 0);
284 /* Test select(2) for unconnected blocking sockets. */
287 test_accept_first (void)
290 int s = open_server_socket ();
291 struct sockaddr_in ia;
302 addrlen = sizeof (ia);
303 c = accept (s, (struct sockaddr *) &ia, &addrlen);
304 ASSERT (close (s) == 0);
305 ASSERT (write (c, "foo", 3) == 3);
306 ASSERT (read (c, buf, 3) == 3);
307 shutdown (c, SHUT_RD);
308 ASSERT (close (c) == 0);
313 ASSERT (close (s) == 0);
314 c = connect_to_socket (true);
315 if (do_select_nowait (c, SEL_OUT) != SEL_OUT)
316 failed ("cannot write after blocking connect");
317 ASSERT (write (c, "foo", 3) == 3);
319 if (do_select_wait (c, SEL_IN) != SEL_IN)
320 failed ("cannot read data left in the socket by closed process");
321 ASSERT (read (c, buf, 3) == 3);
322 ASSERT (write (c, "foo", 3) == 3);
323 (void) close (c); /* may fail with errno = ECONNRESET */
329 /* Common code for pipes and connected sockets. */
332 test_pair (int rd, int wd)
335 if (do_select_wait (wd, SEL_IN | SEL_OUT | SEL_EXC) != SEL_OUT)
336 failed ("expecting writability before writing");
337 if (do_select_nowait (wd, SEL_IN | SEL_OUT | SEL_EXC) != SEL_OUT)
338 failed ("expecting writability before writing");
340 ASSERT (write (wd, "foo", 3) == 3);
341 if (do_select_wait (rd, SEL_IN) != SEL_IN)
342 failed ("expecting readability after writing");
343 if (do_select_nowait (rd, SEL_IN) != SEL_IN)
344 failed ("expecting readability after writing");
346 ASSERT (read (rd, buf, 3) == 3);
350 /* Test select(2) on connected sockets. */
353 test_socket_pair (void)
355 struct sockaddr_in ia;
357 socklen_t addrlen = sizeof (ia);
358 int s = open_server_socket ();
359 int c1 = connect_to_socket (false);
360 int c2 = accept (s, (struct sockaddr *) &ia, &addrlen);
362 ASSERT (close (s) == 0);
365 ASSERT (close (c1) == 0);
366 ASSERT (write (c2, "foo", 3) == 3);
367 (void) close (c2); /* may fail with errno = ECONNRESET */
371 /* Test select(2) on pipes. */
378 ASSERT (pipe (fd) == 0);
379 test_pair (fd[0], fd[1]);
380 ASSERT (close (fd[0]) == 0);
381 ASSERT (close (fd[1]) == 0);
393 printf ("Please press Enter\n");
394 test (test_tty, "TTY");
397 result = test (test_connect_first, "Unconnected socket test");
398 result += test (test_socket_pair, "Connected sockets test");
399 result += test (test_accept_first, "General socket test with fork");
400 result += test (test_pipe, "Pipe test");