1 /* Test of poll() function.
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, 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. */
25 #include "signature.h"
26 SIGNATURE_CHECK (poll, int, (struct pollfd[], nfds_t, int));
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
36 #include <sys/ioctl.h>
39 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
45 #define pipe(x) _pipe(x, 256, O_BINARY)
50 #ifdef HAVE_SYS_WAIT_H
55 #define SO_REUSEPORT SO_REUSEADDR
58 #define TEST_PORT 12345
61 /* Minimal testing infrastructure. */
66 failed (const char *reason)
70 printf ("failed (%s)\n", reason);
74 test (void (*fn) (void), const char *msg)
77 printf ("%s... ", msg);
88 /* Funny socket code. */
94 struct sockaddr_in ia;
96 s = socket (AF_INET, SOCK_STREAM, 0);
98 memset (&ia, 0, sizeof (ia));
99 ia.sin_family = AF_INET;
100 inet_pton (AF_INET, "127.0.0.1", &ia.sin_addr);
101 ia.sin_port = htons (TEST_PORT);
102 if (bind (s, (struct sockaddr *) &ia, sizeof (ia)) < 0)
109 setsockopt (s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof (x));
111 if (listen (s, 1) < 0)
121 connect_to_socket (int blocking)
124 struct sockaddr_in ia;
126 s = socket (AF_INET, SOCK_STREAM, 0);
128 memset (&ia, 0, sizeof (ia));
129 ia.sin_family = AF_INET;
130 inet_pton (AF_INET, "127.0.0.1", &ia.sin_addr);
131 ia.sin_port = htons (TEST_PORT);
136 unsigned long iMode = 1;
137 ioctl (s, FIONBIO, (char *) &iMode);
139 #elif defined F_GETFL
140 int oldflags = fcntl (s, F_GETFL, NULL);
142 if (!(oldflags & O_NONBLOCK))
143 fcntl (s, F_SETFL, oldflags | O_NONBLOCK);
147 if (connect (s, (struct sockaddr *) &ia, sizeof (ia)) < 0
148 && (blocking || errno != EINPROGRESS))
158 /* A slightly more convenient interface to poll(2). */
161 poll1 (int fd, int ev, int time)
169 r = poll (&pfd, 1, time);
173 if (pfd.revents & ~(POLLHUP | POLLERR | POLLNVAL | ev))
174 failed ("invalid flag combination (unrequested events)");
180 poll1_nowait (int fd, int ev)
182 return poll1 (fd, ev, 0);
186 poll1_wait (int fd, int ev)
188 return poll1 (fd, ev, -1);
192 /* Test poll(2) for TTYs. */
198 if (poll1_nowait (0, POLLIN | POLLRDNORM) != 0)
200 if (poll1_nowait (0, POLLOUT) == 0)
201 failed ("cannot write");
203 if (poll1_wait (0, POLLIN | POLLRDNORM) == 0)
204 failed ("return with infinite timeout");
207 if (poll1_nowait (0, POLLIN | POLLRDNORM) != 0)
208 failed ("can read after getc");
213 /* Test poll(2) for unconnected nonblocking sockets. */
216 test_connect_first (void)
218 int s = open_server_socket ();
219 struct sockaddr_in ia;
224 if (poll1_nowait (s, POLLIN | POLLRDNORM | POLLRDBAND) != 0)
225 failed ("can read, socket not connected");
227 c1 = connect_to_socket (false);
229 if (poll1_wait (s, POLLIN | POLLRDNORM | POLLRDBAND) != (POLLIN | POLLRDNORM))
230 failed ("expecting POLLIN | POLLRDNORM on passive socket");
231 if (poll1_nowait (s, POLLIN | POLLRDBAND) != POLLIN)
232 failed ("expecting POLLIN on passive socket");
233 if (poll1_nowait (s, POLLRDNORM | POLLRDBAND) != POLLRDNORM)
234 failed ("expecting POLLRDNORM on passive socket");
236 addrlen = sizeof (ia);
237 c2 = accept (s, (struct sockaddr *) &ia, &addrlen);
244 /* Test poll(2) for unconnected blocking sockets. */
247 test_accept_first (void)
250 int s = open_server_socket ();
251 struct sockaddr_in ia;
262 addrlen = sizeof (ia);
263 c = accept (s, (struct sockaddr *) &ia, &addrlen);
267 shutdown (c, SHUT_RD);
274 c = connect_to_socket (true);
275 if (poll1_nowait (c, POLLOUT | POLLWRNORM | POLLRDBAND)
276 != (POLLOUT | POLLWRNORM))
277 failed ("cannot write after blocking connect");
280 if (poll1_wait (c, POLLIN) != POLLIN)
281 failed ("cannot read data left in the socket by closed process");
284 if ((poll1_wait (c, POLLIN | POLLOUT) & (POLLHUP | POLLERR)) == 0)
285 failed ("expecting POLLHUP after shutdown");
292 /* Common code for pipes and connected sockets. */
295 test_pair (int rd, int wd)
298 if (poll1_wait (wd, POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM | POLLRDBAND)
299 != (POLLOUT | POLLWRNORM))
300 failed ("expecting POLLOUT | POLLWRNORM before writing");
301 if (poll1_nowait (wd, POLLIN | POLLRDNORM | POLLOUT | POLLRDBAND) != POLLOUT)
302 failed ("expecting POLLOUT before writing");
303 if (poll1_nowait (wd, POLLIN | POLLRDNORM | POLLWRNORM | POLLRDBAND)
305 failed ("expecting POLLWRNORM before writing");
307 write (wd, "foo", 3);
308 if (poll1_wait (rd, POLLIN | POLLRDNORM) != (POLLIN | POLLRDNORM))
309 failed ("expecting POLLIN | POLLRDNORM after writing");
310 if (poll1_nowait (rd, POLLIN) != POLLIN)
311 failed ("expecting POLLIN after writing");
312 if (poll1_nowait (rd, POLLRDNORM) != POLLRDNORM)
313 failed ("expecting POLLRDNORM after writing");
319 /* Test poll(2) on connected sockets. */
322 test_socket_pair (void)
324 struct sockaddr_in ia;
326 socklen_t addrlen = sizeof (ia);
327 int s = open_server_socket ();
328 int c1 = connect_to_socket (false);
329 int c2 = accept (s, (struct sockaddr *) &ia, &addrlen);
335 write (c2, "foo", 3);
336 if ((poll1_nowait (c2, POLLIN | POLLOUT) & (POLLHUP | POLLERR)) == 0)
337 failed ("expecting POLLHUP after shutdown");
343 /* Test poll(2) on pipes. */
351 test_pair (fd[0], fd[1]);
353 if ((poll1_wait (fd[1], POLLIN | POLLOUT) & (POLLHUP | POLLERR)) == 0)
354 failed ("expecting POLLHUP after shutdown");
368 printf ("Please press Enter\n");
369 test (test_tty, "TTY");
372 result = test (test_connect_first, "Unconnected socket test");
373 result += test (test_socket_pair, "Connected sockets test");
374 result += test (test_accept_first, "General socket test with fork");
375 result += test (test_pipe, "Pipe test");