1 /* Test of poll() function.
2 Copyright (C) 2008-2010 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 "signature.h"
25 SIGNATURE_CHECK (poll, int, (struct pollfd[], nfds_t, int));
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
35 #include <sys/ioctl.h>
38 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
44 #define pipe(x) _pipe(x, 256, O_BINARY)
49 #ifdef HAVE_SYS_WAIT_H
54 #define SO_REUSEPORT SO_REUSEADDR
57 #define TEST_PORT 12345
60 /* Minimal testing infrastructure. */
65 failed (const char *reason)
69 printf ("failed (%s)\n", reason);
73 test (void (*fn) (void), const char *msg)
76 printf ("%s... ", msg);
87 /* Funny socket code. */
93 struct sockaddr_in ia;
95 s = socket (AF_INET, SOCK_STREAM, 0);
97 memset (&ia, 0, sizeof (ia));
98 ia.sin_family = AF_INET;
99 inet_pton (AF_INET, "127.0.0.1", &ia.sin_addr);
100 ia.sin_port = htons (TEST_PORT);
101 if (bind (s, (struct sockaddr *) &ia, sizeof (ia)) < 0)
108 setsockopt (s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof (x));
110 if (listen (s, 1) < 0)
120 connect_to_socket (int blocking)
123 struct sockaddr_in ia;
125 s = socket (AF_INET, SOCK_STREAM, 0);
127 memset (&ia, 0, sizeof (ia));
128 ia.sin_family = AF_INET;
129 inet_pton (AF_INET, "127.0.0.1", &ia.sin_addr);
130 ia.sin_port = htons (TEST_PORT);
135 unsigned long iMode = 1;
136 ioctl (s, FIONBIO, (char *) &iMode);
138 #elif defined F_GETFL
139 int oldflags = fcntl (s, F_GETFL, NULL);
141 if (!(oldflags & O_NONBLOCK))
142 fcntl (s, F_SETFL, oldflags | O_NONBLOCK);
146 if (connect (s, (struct sockaddr *) &ia, sizeof (ia)) < 0
147 && (blocking || errno != EINPROGRESS))
157 /* A slightly more convenient interface to poll(2). */
160 poll1 (int fd, int ev, int time)
168 r = poll (&pfd, 1, time);
172 if (pfd.revents & ~(POLLHUP | POLLERR | POLLNVAL | ev))
173 failed ("invalid flag combination (unrequested events)");
179 poll1_nowait (int fd, int ev)
181 return poll1 (fd, ev, 0);
185 poll1_wait (int fd, int ev)
187 return poll1 (fd, ev, -1);
191 /* Test poll(2) for TTYs. */
197 if (poll1_nowait (0, POLLIN | POLLRDNORM) != 0)
199 if (poll1_nowait (0, POLLOUT) == 0)
200 failed ("cannot write");
202 if (poll1_wait (0, POLLIN | POLLRDNORM) == 0)
203 failed ("return with infinite timeout");
206 if (poll1_nowait (0, POLLIN | POLLRDNORM) != 0)
207 failed ("can read after getc");
212 /* Test poll(2) for unconnected nonblocking sockets. */
215 test_connect_first (void)
217 int s = open_server_socket ();
218 struct sockaddr_in ia;
223 if (poll1_nowait (s, POLLIN | POLLRDNORM | POLLRDBAND) != 0)
224 failed ("can read, socket not connected");
226 c1 = connect_to_socket (false);
228 if (poll1_wait (s, POLLIN | POLLRDNORM | POLLRDBAND) != (POLLIN | POLLRDNORM))
229 failed ("expecting POLLIN | POLLRDNORM on passive socket");
230 if (poll1_nowait (s, POLLIN | POLLRDBAND) != POLLIN)
231 failed ("expecting POLLIN on passive socket");
232 if (poll1_nowait (s, POLLRDNORM | POLLRDBAND) != POLLRDNORM)
233 failed ("expecting POLLRDNORM on passive socket");
235 addrlen = sizeof (ia);
236 c2 = accept (s, (struct sockaddr *) &ia, &addrlen);
243 /* Test poll(2) for unconnected blocking sockets. */
246 test_accept_first (void)
249 int s = open_server_socket ();
250 struct sockaddr_in ia;
261 addrlen = sizeof (ia);
262 c = accept (s, (struct sockaddr *) &ia, &addrlen);
266 shutdown (c, SHUT_RD);
273 c = connect_to_socket (true);
274 if (poll1_nowait (c, POLLOUT | POLLWRNORM | POLLRDBAND)
275 != (POLLOUT | POLLWRNORM))
276 failed ("cannot write after blocking connect");
279 if (poll1_wait (c, POLLIN) != POLLIN)
280 failed ("cannot read data left in the socket by closed process");
283 if ((poll1_wait (c, POLLIN | POLLOUT) & (POLLHUP | POLLERR)) == 0)
284 failed ("expecting POLLHUP after shutdown");
291 /* Common code for pipes and connected sockets. */
294 test_pair (int rd, int wd)
297 if (poll1_wait (wd, POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM | POLLRDBAND)
298 != (POLLOUT | POLLWRNORM))
299 failed ("expecting POLLOUT | POLLWRNORM before writing");
300 if (poll1_nowait (wd, POLLIN | POLLRDNORM | POLLOUT | POLLRDBAND) != POLLOUT)
301 failed ("expecting POLLOUT before writing");
302 if (poll1_nowait (wd, POLLIN | POLLRDNORM | POLLWRNORM | POLLRDBAND)
304 failed ("expecting POLLWRNORM before writing");
306 write (wd, "foo", 3);
307 if (poll1_wait (rd, POLLIN | POLLRDNORM) != (POLLIN | POLLRDNORM))
308 failed ("expecting POLLIN | POLLRDNORM after writing");
309 if (poll1_nowait (rd, POLLIN) != POLLIN)
310 failed ("expecting POLLIN after writing");
311 if (poll1_nowait (rd, POLLRDNORM) != POLLRDNORM)
312 failed ("expecting POLLRDNORM after writing");
318 /* Test poll(2) on connected sockets. */
321 test_socket_pair (void)
323 struct sockaddr_in ia;
325 socklen_t addrlen = sizeof (ia);
326 int s = open_server_socket ();
327 int c1 = connect_to_socket (false);
328 int c2 = accept (s, (struct sockaddr *) &ia, &addrlen);
334 write (c2, "foo", 3);
335 if ((poll1_nowait (c2, POLLIN | POLLOUT) & (POLLHUP | POLLERR)) == 0)
336 failed ("expecting POLLHUP after shutdown");
342 /* Test poll(2) on pipes. */
350 test_pair (fd[0], fd[1]);
352 if ((poll1_wait (fd[1], POLLIN | POLLOUT) & (POLLHUP | POLLERR)) == 0)
353 failed ("expecting POLLHUP after shutdown");
367 printf ("Please press Enter\n");
368 test (test_tty, "TTY");
371 result = test (test_connect_first, "Unconnected socket test");
372 result += test (test_socket_pair, "Connected sockets test");
373 result += test (test_accept_first, "General socket test with fork");
374 result += test (test_pipe, "Pipe test");