dirfd: Avoid link error on AIX 7.1.
[gnulib.git] / tests / test-poll.c
1 /* Test of poll() function.
2    Copyright (C) 2008-2010 Free Software Foundation, Inc.
3
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)
7    any later version.
8
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.
13
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.  */
17
18 /* Written by Paolo Bonzini.  */
19
20 #include <config.h>
21
22 #include <poll.h>
23
24 #include "signature.h"
25 SIGNATURE_CHECK (poll, int, (struct pollfd[], nfds_t, int));
26
27 #include <stdio.h>
28 #include <string.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
32 #include <fcntl.h>
33 #include <stdlib.h>
34 #include <stdbool.h>
35 #include <sys/ioctl.h>
36 #include <errno.h>
37
38 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
39 # define WIN32_NATIVE
40 #endif
41
42 #ifdef WIN32_NATIVE
43 #include <io.h>
44 #define pipe(x) _pipe(x, 256, O_BINARY)
45 #endif
46 #ifdef HAVE_UNISTD_H
47 #include <unistd.h>
48 #endif
49 #ifdef HAVE_SYS_WAIT_H
50 #include <sys/wait.h>
51 #endif
52
53 #ifndef SO_REUSEPORT
54 #define SO_REUSEPORT    SO_REUSEADDR
55 #endif
56
57 #define TEST_PORT       12345
58
59
60 /* Minimal testing infrastructure.  */
61
62 static int failures;
63
64 static void
65 failed (const char *reason)
66 {
67   if (++failures > 1)
68     printf ("  ");
69   printf ("failed (%s)\n", reason);
70 }
71
72 static int
73 test (void (*fn) (void), const char *msg)
74 {
75   failures = 0;
76   printf ("%s... ", msg);
77   fflush (stdout);
78   fn ();
79
80   if (!failures)
81     printf ("passed\n");
82
83   return failures;
84 }
85
86
87 /* Funny socket code.  */
88
89 static int
90 open_server_socket ()
91 {
92   int s, x;
93   struct sockaddr_in ia;
94
95   s = socket (AF_INET, SOCK_STREAM, 0);
96
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)
102     {
103       perror ("bind");
104       exit (77);
105     }
106
107   x = 1;
108   setsockopt (s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof (x));
109
110   if (listen (s, 1) < 0)
111     {
112       perror ("listen");
113       exit (77);
114     }
115
116   return s;
117 }
118
119 static int
120 connect_to_socket (int blocking)
121 {
122   int s;
123   struct sockaddr_in ia;
124
125   s = socket (AF_INET, SOCK_STREAM, 0);
126
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);
131
132   if (!blocking)
133     {
134 #ifdef WIN32_NATIVE
135       unsigned long iMode = 1;
136       ioctl (s, FIONBIO, (char *) &iMode);
137
138 #elif defined F_GETFL
139       int oldflags = fcntl (s, F_GETFL, NULL);
140
141       if (!(oldflags & O_NONBLOCK))
142         fcntl (s, F_SETFL, oldflags | O_NONBLOCK);
143 #endif
144     }
145
146   if (connect (s, (struct sockaddr *) &ia, sizeof (ia)) < 0
147       && (blocking || errno != EINPROGRESS))
148     {
149       perror ("connect");
150       exit (77);
151     }
152
153   return s;
154 }
155
156
157 /* A slightly more convenient interface to poll(2).  */
158
159 static int
160 poll1 (int fd, int ev, int time)
161 {
162   struct pollfd pfd;
163   int r;
164
165   pfd.fd = fd;
166   pfd.events = ev;
167   pfd.revents = 0;
168   r = poll (&pfd, 1, time);
169   if (r < 0)
170     return r;
171
172   if (pfd.revents & ~(POLLHUP | POLLERR | POLLNVAL | ev))
173     failed ("invalid flag combination (unrequested events)");
174
175   return pfd.revents;
176 }
177
178 static int
179 poll1_nowait (int fd, int ev)
180 {
181   return poll1 (fd, ev, 0);
182 }
183
184 static int
185 poll1_wait (int fd, int ev)
186 {
187   return poll1 (fd, ev, -1);
188 }
189
190
191 /* Test poll(2) for TTYs.  */
192
193 #ifdef INTERACTIVE
194 static void
195 test_tty (void)
196 {
197   if (poll1_nowait (0, POLLIN | POLLRDNORM) != 0)
198     failed ("can read");
199   if (poll1_nowait (0, POLLOUT) == 0)
200     failed ("cannot write");
201
202   if (poll1_wait (0, POLLIN | POLLRDNORM) == 0)
203     failed ("return with infinite timeout");
204
205   getchar ();
206   if (poll1_nowait (0, POLLIN | POLLRDNORM) != 0)
207     failed ("can read after getc");
208 }
209 #endif
210
211
212 /* Test poll(2) for unconnected nonblocking sockets.  */
213
214 static void
215 test_connect_first (void)
216 {
217   int s = open_server_socket ();
218   struct sockaddr_in ia;
219   socklen_t addrlen;
220
221   int c1, c2;
222
223   if (poll1_nowait (s, POLLIN | POLLRDNORM | POLLRDBAND) != 0)
224     failed ("can read, socket not connected");
225
226   c1 = connect_to_socket (false);
227
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");
234
235   addrlen = sizeof (ia);
236   c2 = accept (s, (struct sockaddr *) &ia, &addrlen);
237   close (s);
238   close (c1);
239   close (c2);
240 }
241
242
243 /* Test poll(2) for unconnected blocking sockets.  */
244
245 static void
246 test_accept_first (void)
247 {
248 #ifndef WIN32_NATIVE
249   int s = open_server_socket ();
250   struct sockaddr_in ia;
251   socklen_t addrlen;
252   char buf[3];
253   int c, pid;
254
255   pid = fork ();
256   if (pid < 0)
257     return;
258
259   if (pid == 0)
260     {
261       addrlen = sizeof (ia);
262       c = accept (s, (struct sockaddr *) &ia, &addrlen);
263       close (s);
264       write (c, "foo", 3);
265       read (c, buf, 3);
266       shutdown (c, SHUT_RD);
267       close (c);
268       exit (0);
269     }
270   else
271     {
272       close (s);
273       c = connect_to_socket (true);
274       if (poll1_nowait (c, POLLOUT | POLLWRNORM | POLLRDBAND)
275           != (POLLOUT | POLLWRNORM))
276         failed ("cannot write after blocking connect");
277       write (c, "foo", 3);
278       wait (&pid);
279       if (poll1_wait (c, POLLIN) != POLLIN)
280         failed ("cannot read data left in the socket by closed process");
281       read (c, buf, 3);
282       write (c, "foo", 3);
283       if ((poll1_wait (c, POLLIN | POLLOUT) & (POLLHUP | POLLERR)) == 0)
284         failed ("expecting POLLHUP after shutdown");
285       close (c);
286     }
287 #endif
288 }
289
290
291 /* Common code for pipes and connected sockets.  */
292
293 static void
294 test_pair (int rd, int wd)
295 {
296   char buf[3];
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)
303       != POLLWRNORM)
304     failed ("expecting POLLWRNORM before writing");
305
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");
313
314   read (rd, buf, 3);
315 }
316
317
318 /* Test poll(2) on connected sockets.  */
319
320 static void
321 test_socket_pair (void)
322 {
323   struct sockaddr_in ia;
324
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);
329
330   close (s);
331
332   test_pair (c1, c2);
333   close (c1);
334   write (c2, "foo", 3);
335   if ((poll1_nowait (c2, POLLIN | POLLOUT) & (POLLHUP | POLLERR)) == 0)
336     failed ("expecting POLLHUP after shutdown");
337
338   close (c2);
339 }
340
341
342 /* Test poll(2) on pipes.  */
343
344 static void
345 test_pipe (void)
346 {
347   int fd[2];
348
349   pipe (fd);
350   test_pair (fd[0], fd[1]);
351   close (fd[0]);
352   if ((poll1_wait (fd[1], POLLIN | POLLOUT) & (POLLHUP | POLLERR)) == 0)
353     failed ("expecting POLLHUP after shutdown");
354
355   close (fd[1]);
356 }
357
358
359 /* Do them all.  */
360
361 int
362 main ()
363 {
364   int result;
365
366 #ifdef INTERACTIVE
367   printf ("Please press Enter\n");
368   test (test_tty, "TTY");
369 #endif
370
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");
375
376   exit (result);
377 }