1 /* Test of poll() function.
2 Copyright (C) 2008 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, or (at your option)
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, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* Written by Paolo Bonzini. */
24 #include <sys/socket.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
36 #define pipe(x) _pipe(x, 256, O_BINARY)
41 #ifdef HAVE_SYS_WAIT_H
46 #define SO_REUSEPORT SO_REUSEADDR
49 #define TEST_PORT 12345
52 /* Minimal testing infrastructure. */
57 failed (const char *reason)
61 printf ("failed (%s)\n", reason);
65 test (void (*fn) (void), const char *msg)
68 printf ("%s... ", msg);
79 /* Funny socket code. */
85 struct sockaddr_in ia;
87 s = socket (AF_INET, SOCK_STREAM, 0);
89 memset (&ia, 0, sizeof (ia));
90 ia.sin_family = AF_INET;
91 inet_pton (AF_INET, "127.0.0.1", &ia.sin_addr);
92 ia.sin_port = htons (TEST_PORT);
93 if (bind (s, (struct sockaddr *) &ia, sizeof (ia)) < 0)
100 setsockopt (s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof (x));
102 if (listen (s, 1) < 0)
112 connect_to_socket (int blocking)
115 struct sockaddr_in ia;
117 s = socket (AF_INET, SOCK_STREAM, 0);
119 memset (&ia, 0, sizeof (ia));
120 ia.sin_family = AF_INET;
121 inet_pton (AF_INET, "127.0.0.1", &ia.sin_addr);
122 ia.sin_port = htons (TEST_PORT);
127 unsigned long iMode = 1;
128 ioctlsocket (s, FIONBIO, (void *) &iMode);
130 #elif defined F_GETFL
131 int oldflags = fcntl (s, F_GETFL, NULL);
133 if (!(oldflags & O_NONBLOCK))
134 fcntl (s, F_SETFL, oldflags | O_NONBLOCK);
138 if (connect (s, (struct sockaddr *) &ia, sizeof (ia)) < 0)
140 if (errno != EINPROGRESS)
147 failed ("huh, connect succeeded?");
153 /* A slightly more convenient interface to poll(2). */
156 poll1 (int fd, int ev, int time)
164 r = poll (&pfd, 1, time);
168 if (pfd.revents & ~(POLLHUP | POLLERR | POLLNVAL | ev))
169 failed ("invalid flag combination (unrequested events)");
175 poll1_nowait (int fd, int ev)
177 return poll1 (fd, ev, 0);
181 poll1_wait (int fd, int ev)
183 return poll1 (fd, ev, -1);
187 /* Test poll(2) for TTYs. */
193 if (poll1_nowait (0, POLLIN | POLLRDNORM) != 0)
195 if (poll1_nowait (0, POLLOUT) == 0)
196 failed ("cannot write");
198 if (poll1_wait (0, POLLIN | POLLRDNORM) == 0)
199 failed ("return with infinite timeout");
202 if (poll1_nowait (0, POLLIN | POLLRDNORM) != 0)
203 failed ("can read after getc");
208 /* Test poll(2) for unconnected nonblocking sockets. */
211 test_connect_first (void)
213 int s = open_server_socket ();
214 struct sockaddr_in ia;
219 if (poll1_nowait (s, POLLIN | POLLRDNORM | POLLRDBAND) != 0)
220 failed ("can read, socket not connected");
222 c1 = connect_to_socket (false);
224 if (poll1_wait (s, POLLIN | POLLRDNORM | POLLRDBAND) != (POLLIN | POLLRDNORM))
225 failed ("expecting POLLIN | POLLRDNORM on passive socket");
226 if (poll1_nowait (s, POLLIN | POLLRDBAND) != POLLIN)
227 failed ("expecting POLLIN on passive socket");
228 if (poll1_nowait (s, POLLRDNORM | POLLRDBAND) != POLLRDNORM)
229 failed ("expecting POLLRDNORM on passive socket");
231 addrlen = sizeof (ia);
232 c2 = accept (s, (struct sockaddr *) &ia, &addrlen);
239 /* Test poll(2) for unconnected blocking sockets. */
242 test_accept_first (void)
245 int s = open_server_socket ();
246 struct sockaddr_in ia;
257 addrlen = sizeof (ia);
258 c = accept (s, (struct sockaddr *) &ia, &addrlen);
262 shutdown (c, SHUT_RD);
269 c = connect_to_socket (true);
270 if (poll1_nowait (c, POLLOUT | POLLWRNORM | POLLRDBAND)
271 != (POLLOUT | POLLWRNORM))
272 failed ("cannot write after blocking connect");
275 if (poll1_wait (c, POLLIN) != POLLIN)
276 failed ("cannot read data left in the socket by closed process");
279 if ((poll1_wait (c, POLLIN | POLLOUT) & (POLLHUP | POLLERR)) == 0)
280 failed ("expecting POLLHUP after shutdown");
287 /* Common code for pipes and connected sockets. */
290 test_pair (int rd, int wd)
293 if (poll1_wait (wd, POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM | POLLRDBAND)
294 != (POLLOUT | POLLWRNORM))
295 failed ("expecting POLLOUT | POLLWRNORM before writing");
296 if (poll1_nowait (wd, POLLIN | POLLRDNORM | POLLOUT | POLLRDBAND) != POLLOUT)
297 failed ("expecting POLLOUT before writing");
298 if (poll1_nowait (wd, POLLIN | POLLRDNORM | POLLWRNORM | POLLRDBAND)
300 failed ("expecting POLLWRNORM before writing");
302 write (wd, "foo", 3);
303 if (poll1_wait (rd, POLLIN | POLLRDNORM) != (POLLIN | POLLRDNORM))
304 failed ("expecting POLLIN | POLLRDNORM after writing");
305 if (poll1_nowait (rd, POLLIN) != POLLIN)
306 failed ("expecting POLLIN after writing");
307 if (poll1_nowait (rd, POLLRDNORM) != POLLRDNORM)
308 failed ("expecting POLLRDNORM after writing");
314 /* Test poll(2) on connected sockets. */
317 test_socket_pair (void)
319 struct sockaddr_in ia;
321 socklen_t addrlen = sizeof (ia);
322 int s = open_server_socket ();
323 int c1 = connect_to_socket (false);
324 int c2 = accept (s, (struct sockaddr *) &ia, &addrlen);
330 write (c2, "foo", 3);
331 if ((poll1_nowait (c2, POLLIN | POLLOUT) & (POLLHUP | POLLERR)) == 0)
332 failed ("expecting POLLHUP after shutdown");
338 /* Test poll(2) on pipes. */
346 test_pair (fd[0], fd[1]);
348 if ((poll1_wait (fd[1], POLLIN | POLLOUT) & (POLLHUP | POLLERR)) == 0)
349 failed ("expecting POLLHUP after shutdown");
362 gl_sockets_startup (SOCKETS_2_0);
365 printf ("Please press Enter\n");
366 test (test_tty, "TTY");
369 result = test (test_connect_first, "Unconnected socket test");
370 result += test (test_socket_pair, "Connected sockets test");
371 result += test (test_accept_first, "General socket test with fork");
372 result += test (test_pipe, "Pipe test");