1 /* Test of select() substitute.
2 Copyright (C) 2008-2009 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>
48 #include <sys/ioctl.h>
51 enum { SEL_IN = 1, SEL_OUT = 2, SEL_EXC = 4 };
53 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
59 #define pipe(x) _pipe(x, 256, O_BINARY)
64 #ifdef HAVE_SYS_WAIT_H
69 #define SO_REUSEPORT SO_REUSEADDR
72 #define TEST_PORT 12345
75 /* Minimal testing infrastructure. */
80 failed (const char *reason)
84 printf ("failed (%s)\n", reason);
88 test (void (*fn) (void), const char *msg)
91 printf ("%s... ", msg);
102 /* Funny socket code. */
105 open_server_socket (void)
108 struct sockaddr_in ia;
110 s = socket (AF_INET, SOCK_STREAM, 0);
112 memset (&ia, 0, sizeof (ia));
113 ia.sin_family = AF_INET;
114 inet_pton (AF_INET, "127.0.0.1", &ia.sin_addr);
115 ia.sin_port = htons (TEST_PORT);
116 if (bind (s, (struct sockaddr *) &ia, sizeof (ia)) < 0)
123 setsockopt (s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof (x));
125 if (listen (s, 1) < 0)
135 connect_to_socket (int blocking)
138 struct sockaddr_in ia;
140 s = socket (AF_INET, SOCK_STREAM, 0);
142 memset (&ia, 0, sizeof (ia));
143 ia.sin_family = AF_INET;
144 inet_pton (AF_INET, "127.0.0.1", &ia.sin_addr);
145 ia.sin_port = htons (TEST_PORT);
150 unsigned long iMode = 1;
151 ioctl (s, FIONBIO, (char *) &iMode);
153 #elif defined F_GETFL
154 int oldflags = fcntl (s, F_GETFL, NULL);
156 if (!(oldflags & O_NONBLOCK))
157 fcntl (s, F_SETFL, oldflags | O_NONBLOCK);
161 if (connect (s, (struct sockaddr *) &ia, sizeof (ia)) < 0
162 && (blocking || errno != EINPROGRESS))
172 /* A slightly more convenient interface to select(2). */
175 do_select (int fd, int ev, struct timeval *tv)
177 fd_set rfds, wfds, xfds;
189 r = select (fd + 1, &rfds, &wfds, &xfds, tv);
194 if (FD_ISSET (fd, &rfds))
196 if (FD_ISSET (fd, &wfds))
198 if (FD_ISSET (fd, &xfds))
201 failed ("select returned 0");
203 failed ("select returned unrequested events");
209 do_select_nowait (int fd, int ev)
211 static struct timeval tv0;
212 return do_select (fd, ev, &tv0);
216 do_select_wait (int fd, int ev)
218 return do_select (fd, ev, NULL);
222 /* Test poll(2) for TTYs. */
228 if (do_select_nowait (0, SEL_IN) != 0)
230 if (do_select_nowait (0, SEL_OUT) == 0)
231 failed ("cannot write");
233 if (do_select_wait (0, SEL_IN) == 0)
234 failed ("return with infinite timeout");
237 if (do_select_nowait (0, SEL_IN) != 0)
238 failed ("can read after getc");
243 /* Test poll(2) for unconnected nonblocking sockets. */
246 test_connect_first (void)
248 int s = open_server_socket ();
249 struct sockaddr_in ia;
254 if (do_select_nowait (s, SEL_IN | SEL_EXC) != 0)
255 failed ("can read, socket not connected");
257 c1 = connect_to_socket (false);
259 if (do_select_wait (s, SEL_IN | SEL_EXC) != SEL_IN)
260 failed ("expecting readability on passive socket");
261 if (do_select_nowait (s, SEL_IN | SEL_EXC) != SEL_IN)
262 failed ("expecting readability on passive socket");
264 addrlen = sizeof (ia);
265 c2 = accept (s, (struct sockaddr *) &ia, &addrlen);
272 /* Test poll(2) for unconnected blocking sockets. */
275 test_accept_first (void)
278 int s = open_server_socket ();
279 struct sockaddr_in ia;
290 addrlen = sizeof (ia);
291 c = accept (s, (struct sockaddr *) &ia, &addrlen);
295 shutdown (c, SHUT_RD);
302 c = connect_to_socket (true);
303 if (do_select_nowait (c, SEL_OUT) != SEL_OUT)
304 failed ("cannot write after blocking connect");
307 if (do_select_wait (c, SEL_IN) != SEL_IN)
308 failed ("cannot read data left in the socket by closed process");
317 /* Common code for pipes and connected sockets. */
320 test_pair (int rd, int wd)
323 if (do_select_wait (wd, SEL_IN | SEL_OUT | SEL_EXC) != SEL_OUT)
324 failed ("expecting writability before writing");
325 if (do_select_nowait (wd, SEL_IN | SEL_OUT | SEL_EXC) != SEL_OUT)
326 failed ("expecting writability before writing");
328 write (wd, "foo", 3);
329 if (do_select_wait (rd, SEL_IN) != SEL_IN)
330 failed ("expecting readability after writing");
331 if (do_select_nowait (rd, SEL_IN) != SEL_IN)
332 failed ("expecting readability after writing");
338 /* Test poll(2) on connected sockets. */
341 test_socket_pair (void)
343 struct sockaddr_in ia;
345 socklen_t addrlen = sizeof (ia);
346 int s = open_server_socket ();
347 int c1 = connect_to_socket (false);
348 int c2 = accept (s, (struct sockaddr *) &ia, &addrlen);
354 write (c2, "foo", 3);
359 /* Test poll(2) on pipes. */
367 test_pair (fd[0], fd[1]);
381 printf ("Please press Enter\n");
382 test (test_tty, "TTY");
385 result = test (test_connect_first, "Unconnected socket test");
386 result += test (test_socket_pair, "Connected sockets test");
387 result += test (test_accept_first, "General socket test with fork");
388 result += test (test_pipe, "Pipe test");