1 /* Test of poll() function.
2 Copyright (C) 2008-2020 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, see <https://www.gnu.org/licenses/>. */
17 /* 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>
40 #if defined _WIN32 && ! defined __CYGWIN__
41 # define WINDOWS_NATIVE
46 #define pipe(x) _pipe(x, 256, O_BINARY)
51 #ifdef HAVE_SYS_WAIT_H
55 #define TEST_PORT 12345
58 /* Minimal testing infrastructure. */
63 failed (const char *reason
)
67 printf ("failed (%s)\n", reason
);
71 test (void (*fn
) (void), const char *msg
)
74 printf ("%s... ", msg
);
85 /* Funny socket code. */
91 struct sockaddr_in ia
;
93 s
= socket (AF_INET
, SOCK_STREAM
, 0);
96 setsockopt (s
, SOL_SOCKET
, SO_REUSEPORT
, &x
, sizeof (x
));
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)
108 if (listen (s
, 1) < 0)
118 connect_to_socket (int blocking
)
121 struct sockaddr_in ia
;
123 s
= socket (AF_INET
, SOCK_STREAM
, 0);
125 memset (&ia
, 0, sizeof (ia
));
126 ia
.sin_family
= AF_INET
;
127 inet_pton (AF_INET
, "127.0.0.1", &ia
.sin_addr
);
128 ia
.sin_port
= htons (TEST_PORT
);
132 #ifdef WINDOWS_NATIVE
133 unsigned long iMode
= 1;
134 ioctl (s
, FIONBIO
, (char *) &iMode
);
136 #elif defined F_GETFL
137 int oldflags
= fcntl (s
, F_GETFL
, NULL
);
139 if (!(oldflags
& O_NONBLOCK
))
140 fcntl (s
, F_SETFL
, oldflags
| O_NONBLOCK
);
144 if (connect (s
, (struct sockaddr
*) &ia
, sizeof (ia
)) < 0
145 && (blocking
|| errno
!= EINPROGRESS
))
155 /* A slightly more convenient interface to poll(2). */
158 poll1 (int fd
, int ev
, int time
)
166 r
= poll (&pfd
, 1, time
);
170 if (pfd
.revents
& ~(POLLHUP
| POLLERR
| POLLNVAL
| ev
))
171 failed ("invalid flag combination (unrequested events)");
177 poll1_nowait (int fd
, int ev
)
179 return poll1 (fd
, ev
, 0);
183 poll1_wait (int fd
, int ev
)
185 return poll1 (fd
, ev
, -1);
189 /* Test poll(2) for TTYs. */
195 if (poll1_nowait (0, POLLIN
| POLLRDNORM
) != 0)
197 if (poll1_nowait (0, POLLOUT
) == 0)
198 failed ("cannot write");
200 if (poll1_wait (0, POLLIN
| POLLRDNORM
) == 0)
201 failed ("return with infinite timeout");
204 if (poll1_nowait (0, POLLIN
| POLLRDNORM
) != 0)
205 failed ("can read after getc");
210 /* Test poll(2) for unconnected nonblocking sockets. */
213 test_connect_first (void)
215 int s
= open_server_socket ();
216 struct sockaddr_in ia
;
221 if (poll1_nowait (s
, POLLIN
| POLLRDNORM
| POLLRDBAND
) != 0)
222 failed ("can read, socket not connected");
224 c1
= connect_to_socket (false);
226 if (poll1_wait (s
, POLLIN
| POLLRDNORM
| POLLRDBAND
) != (POLLIN
| POLLRDNORM
))
227 failed ("expecting POLLIN | POLLRDNORM on passive socket");
228 if (poll1_nowait (s
, POLLIN
| POLLRDBAND
) != POLLIN
)
229 failed ("expecting POLLIN on passive socket");
230 if (poll1_nowait (s
, POLLRDNORM
| POLLRDBAND
) != POLLRDNORM
)
231 failed ("expecting POLLRDNORM on passive socket");
233 addrlen
= sizeof (ia
);
234 c2
= accept (s
, (struct sockaddr
*) &ia
, &addrlen
);
241 /* Test poll(2) for unconnected blocking sockets. */
244 test_accept_first (void)
246 #ifndef WINDOWS_NATIVE
247 int s
= open_server_socket ();
248 struct sockaddr_in ia
;
259 addrlen
= sizeof (ia
);
260 c
= accept (s
, (struct sockaddr
*) &ia
, &addrlen
);
263 ASSERT (write (c
, "foo", 3) == 3);
264 ASSERT (read (c
, buf
, 3) == 3);
265 shutdown (c
, SHUT_RD
);
272 c
= connect_to_socket (true);
274 if (poll1_nowait (c
, POLLOUT
| POLLWRNORM
| POLLRDBAND
)
275 != (POLLOUT
| POLLWRNORM
))
276 failed ("cannot write after blocking connect");
277 ASSERT (write (c
, "foo", 3) == 3);
279 if (poll1_wait (c
, POLLIN
) != POLLIN
)
280 failed ("cannot read data left in the socket by closed process");
281 ASSERT (read (c
, buf
, 3) == 3);
282 ASSERT (write (c
, "foo", 3) == 3);
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 ASSERT (write (wd
, "foo", 3) == 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");
314 ASSERT (read (rd
, buf
, 3) == 3);
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
);
337 ASSERT (write (c2
, "foo", 3) == 3);
338 if ((poll1_nowait (c2
, POLLIN
| POLLOUT
) & (POLLHUP
| POLLERR
)) == 0)
339 failed ("expecting POLLHUP after shutdown");
345 /* Test poll(2) on pipes. */
352 ASSERT (pipe (fd
) >= 0);
353 test_pair (fd
[0], fd
[1]);
355 if ((poll1_wait (fd
[1], POLLIN
| POLLOUT
) & (POLLHUP
| POLLERR
)) == 0)
356 failed ("expecting POLLHUP after shutdown");
370 printf ("Please press Enter\n");
371 test (test_tty
, "TTY");
374 result
= test (test_connect_first
, "Unconnected socket test");
375 result
+= test (test_socket_pair
, "Connected sockets test");
376 result
+= test (test_accept_first
, "General socket test with fork");
377 result
+= test (test_pipe
, "Pipe test");