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 *,
34 /* The following may be macros without underlying functions, so only
35 check signature if they are not macros. */
37 SIGNATURE_CHECK (FD_CLR, void, (int, fd_set *));
40 SIGNATURE_CHECK (FD_ISSET, void, (int, fd_set *));
43 SIGNATURE_CHECK (FD_SET, int, (int, fd_set *));
46 SIGNATURE_CHECK (FD_ZERO, void, (fd_set *));
51 #include <netinet/in.h>
52 #include <arpa/inet.h>
57 #include <sys/ioctl.h>
62 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
66 #ifdef HAVE_SYS_WAIT_H
67 # include <sys/wait.h>
71 # define SO_REUSEPORT SO_REUSEADDR
74 #define TEST_PORT 12345
77 /* Minimal testing infrastructure. */
82 failed (const char *reason)
86 printf ("failed (%s)\n", reason);
90 test (void (*fn) (void), const char *msg)
93 printf ("%s... ", msg);
104 /* Funny socket code. */
107 open_server_socket (void)
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);
118 if (bind (s, (struct sockaddr *) &ia, sizeof (ia)) < 0)
125 setsockopt (s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof (x));
127 if (listen (s, 1) < 0)
137 connect_to_socket (bool blocking)
140 struct sockaddr_in ia;
142 s = socket (AF_INET, SOCK_STREAM, 0);
144 memset (&ia, 0, sizeof (ia));
145 ia.sin_family = AF_INET;
146 inet_pton (AF_INET, "127.0.0.1", &ia.sin_addr);
147 ia.sin_port = htons (TEST_PORT);
152 unsigned long iMode = 1;
153 ioctl (s, FIONBIO, (char *) &iMode);
155 #elif defined F_GETFL
156 int oldflags = fcntl (s, F_GETFL, NULL);
158 if (!(oldflags & O_NONBLOCK))
159 fcntl (s, F_SETFL, oldflags | O_NONBLOCK);
163 if (connect (s, (struct sockaddr *) &ia, sizeof (ia)) < 0
164 && (blocking || errno != EINPROGRESS))
174 /* A slightly more convenient interface to select(2).
175 Waits until a specific event occurs on a file descriptor FD.
176 EV is a bit mask of events to look for:
177 SEL_IN - input can be polled without blocking,
178 SEL_OUT - output can be provided without blocking,
179 SEL_EXC - an exception occurred,
180 A maximum wait time is specified by TIMEOUT.
181 *TIMEOUT = { 0, 0 } means to return immediately,
182 TIMEOUT = NULL means to wait indefinitely. */
184 enum { SEL_IN = 1, SEL_OUT = 2, SEL_EXC = 4 };
187 do_select (int fd, int ev, struct timeval *timeout)
189 fd_set rfds, wfds, xfds;
203 struct timespec ts, *pts = NULL;
206 ts.tv_sec = timeout->tv_sec;
207 ts.tv_nsec = timeout->tv_usec * 1000;
210 r = pselect (fd + 1, &rfds, &wfds, &xfds, pts, NULL);
213 r = select (fd + 1, &rfds, &wfds, &xfds, timeout);
219 if (FD_ISSET (fd, &rfds))
221 if (FD_ISSET (fd, &wfds))
223 if (FD_ISSET (fd, &xfds))
226 failed ("select returned 0");
228 failed ("select returned unrequested events");
234 do_select_nowait (int fd, int ev)
239 return do_select (fd, ev, &tv0);
243 do_select_wait (int fd, int ev)
245 return do_select (fd, ev, NULL);
249 /* Test select(2) for TTYs. */
255 if (do_select_nowait (0, SEL_IN) != 0)
257 if (do_select_nowait (0, SEL_OUT) == 0)
258 failed ("cannot write");
260 if (do_select_wait (0, SEL_IN) == 0)
261 failed ("return with infinite timeout");
264 if (do_select_nowait (0, SEL_IN) != 0)
265 failed ("can read after getc");
270 /* Test select(2) for unconnected nonblocking sockets. */
273 test_connect_first (void)
275 int s = open_server_socket ();
276 struct sockaddr_in ia;
281 if (do_select_nowait (s, SEL_IN | SEL_EXC) != 0)
282 failed ("can read, socket not connected");
284 c1 = connect_to_socket (false);
286 if (do_select_wait (s, SEL_IN | SEL_EXC) != SEL_IN)
287 failed ("expecting readability on passive socket");
288 if (do_select_nowait (s, SEL_IN | SEL_EXC) != SEL_IN)
289 failed ("expecting readability on passive socket");
291 addrlen = sizeof (ia);
292 c2 = accept (s, (struct sockaddr *) &ia, &addrlen);
293 ASSERT (close (s) == 0);
294 ASSERT (close (c1) == 0);
295 ASSERT (close (c2) == 0);
299 /* Test select(2) for unconnected blocking sockets. */
302 test_accept_first (void)
305 int s = open_server_socket ();
306 struct sockaddr_in ia;
317 addrlen = sizeof (ia);
318 c = accept (s, (struct sockaddr *) &ia, &addrlen);
319 ASSERT (close (s) == 0);
320 ASSERT (write (c, "foo", 3) == 3);
321 ASSERT (read (c, buf, 3) == 3);
322 shutdown (c, SHUT_RD);
323 ASSERT (close (c) == 0);
328 ASSERT (close (s) == 0);
329 c = connect_to_socket (true);
330 if (do_select_nowait (c, SEL_OUT) != SEL_OUT)
331 failed ("cannot write after blocking connect");
332 ASSERT (write (c, "foo", 3) == 3);
334 if (do_select_wait (c, SEL_IN) != SEL_IN)
335 failed ("cannot read data left in the socket by closed process");
336 ASSERT (read (c, buf, 3) == 3);
337 ASSERT (write (c, "foo", 3) == 3);
338 (void) close (c); /* may fail with errno = ECONNRESET */
344 /* Common code for pipes and connected sockets. */
347 test_pair (int rd, int wd)
350 if (do_select_wait (wd, SEL_IN | SEL_OUT | SEL_EXC) != SEL_OUT)
351 failed ("expecting writability before writing");
352 if (do_select_nowait (wd, SEL_IN | SEL_OUT | SEL_EXC) != SEL_OUT)
353 failed ("expecting writability before writing");
355 ASSERT (write (wd, "foo", 3) == 3);
356 if (do_select_wait (rd, SEL_IN) != SEL_IN)
357 failed ("expecting readability after writing");
358 if (do_select_nowait (rd, SEL_IN) != SEL_IN)
359 failed ("expecting readability after writing");
361 ASSERT (read (rd, buf, 3) == 3);
365 /* Test select(2) on connected sockets. */
368 test_socket_pair (void)
370 struct sockaddr_in ia;
372 socklen_t addrlen = sizeof (ia);
373 int s = open_server_socket ();
374 int c1 = connect_to_socket (false);
375 int c2 = accept (s, (struct sockaddr *) &ia, &addrlen);
377 ASSERT (close (s) == 0);
380 ASSERT (close (c1) == 0);
381 ASSERT (write (c2, "foo", 3) == 3);
382 (void) close (c2); /* may fail with errno = ECONNRESET */
386 /* Test select(2) on pipes. */
393 ASSERT (pipe (fd) == 0);
394 test_pair (fd[0], fd[1]);
395 ASSERT (close (fd[0]) == 0);
396 ASSERT (close (fd[1]) == 0);
408 printf ("Please press Enter\n");
409 test (test_tty, "TTY");
412 result = test (test_connect_first, "Unconnected socket test");
413 result += test (test_socket_pair, "Connected sockets test");
414 result += test (test_accept_first, "General socket test with fork");
415 result += test (test_pipe, "Pipe test");