1 /* Test of select() substitute.
2 Copyright (C) 2008-2022 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 of the License, or
7 (at your option) 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, see <https://www.gnu.org/licenses/>. */
16
17 /* Written by Paolo Bonzini, 2008. */
18
19 #include <stdio.h>
20 #include <string.h>
21 #include <sys/socket.h>
22 #include <netinet/in.h>
23 #include <arpa/inet.h>
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <stdlib.h>
27 #include <sys/ioctl.h>
28 #include <errno.h>
29
30 #include "macros.h"
31
32 #if defined _WIN32 && ! defined __CYGWIN__
33 # define WINDOWS_NATIVE
34 #endif
35
36 #ifdef HAVE_SYS_WAIT_H
37 # include <sys/wait.h>
38 #endif
39
40 #define TEST_PORT 12345
41
42
43 typedef int (*select_fn) (int, fd_set *, fd_set *, fd_set *, struct timeval *);
44
45
46 /* Minimal testing infrastructure. */
47
48 static int failures;
49
50 static void
51 failed (const char *reason)
52 {
53 if (++failures > 1)
54 printf (" ");
55 printf ("failed (%s)\n", reason);
56 }
57
58 static int
59 test (void (*fn) (select_fn), select_fn my_select, const char *msg)
60 {
61 failures = 0;
62 printf ("%s... ", msg);
63 fflush (stdout);
64 fn (my_select);
65
66 if (!failures)
67 printf ("passed\n");
68
69 return failures;
70 }
71
72
73 /* Funny socket code. */
74
75 static int
76 open_server_socket (void)
77 {
78 int s, x;
79 struct sockaddr_in ia;
80
81 s = socket (AF_INET, SOCK_STREAM, 0);
82
83 x = 1;
84 setsockopt (s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof (x));
85
86 memset (&ia, 0, sizeof (ia));
87 ia.sin_family = AF_INET;
88 inet_pton (AF_INET, "127.0.0.1", &ia.sin_addr);
89 ia.sin_port = htons (TEST_PORT);
90 if (bind (s, (struct sockaddr *) &ia, sizeof (ia)) < 0)
91 {
92 perror ("bind");
93 exit (77);
94 }
95
96 if (listen (s, 1) < 0)
97 {
98 perror ("listen");
99 exit (77);
100 }
101
102 return s;
103 }
104
105 static int
106 connect_to_socket (bool blocking)
107 {
108 int s;
109 struct sockaddr_in ia;
110
111 s = socket (AF_INET, SOCK_STREAM, 0);
112
113 memset (&ia, 0, sizeof (ia));
114 ia.sin_family = AF_INET;
115 inet_pton (AF_INET, "127.0.0.1", &ia.sin_addr);
116 ia.sin_port = htons (TEST_PORT);
117
118 if (!blocking)
119 {
120 #ifdef WINDOWS_NATIVE
121 unsigned long iMode = 1;
122 ioctl (s, FIONBIO, (char *) &iMode);
123
124 #elif defined F_GETFL
125 int oldflags = fcntl (s, F_GETFL, NULL);
126
127 if (!(oldflags & O_NONBLOCK))
128 fcntl (s, F_SETFL, oldflags | O_NONBLOCK);
129 #endif
130 }
131
132 if (connect (s, (struct sockaddr *) &ia, sizeof (ia)) < 0
133 && (blocking || errno != EINPROGRESS))
134 {
135 perror ("connect");
136 exit (77);
137 }
138
139 return s;
140 }
141
142
143 /* A slightly more convenient interface to select(2).
144 Waits until a specific event occurs on a file descriptor FD.
145 EV is a bit mask of events to look for:
146 SEL_IN - input can be polled without blocking,
147 SEL_OUT - output can be provided without blocking,
148 SEL_EXC - an exception occurred,
149 A maximum wait time is specified by TIMEOUT.
150 *TIMEOUT = { 0, 0 } means to return immediately,
151 TIMEOUT = NULL means to wait indefinitely. */
152
153 enum { SEL_IN = 1, SEL_OUT = 2, SEL_EXC = 4 };
154
155 static int
156 do_select (int fd, int ev, struct timeval *timeout, select_fn my_select)
157 {
158 fd_set rfds, wfds, xfds;
159 int r, rev;
160
161 FD_ZERO (&rfds);
162 FD_ZERO (&wfds);
163 FD_ZERO (&xfds);
164 if (ev & SEL_IN)
165 FD_SET (fd, &rfds);
166 if (ev & SEL_OUT)
167 FD_SET (fd, &wfds);
168 if (ev & SEL_EXC)
169 FD_SET (fd, &xfds);
170 r = my_select (fd + 1, &rfds, &wfds, &xfds, timeout);
171 if (r < 0)
172 return r;
173
174 rev = 0;
175 if (FD_ISSET (fd, &rfds))
176 rev |= SEL_IN;
177 if (FD_ISSET (fd, &wfds))
178 rev |= SEL_OUT;
179 if (FD_ISSET (fd, &xfds))
180 rev |= SEL_EXC;
181 if (rev && r == 0)
182 failed ("select returned 0");
183 if (rev & ~ev)
184 failed ("select returned unrequested events");
185
186 return rev;
187 }
188
189 static int
190 do_select_nowait (int fd, int ev, select_fn my_select)
191 {
192 struct timeval tv0;
193 tv0.tv_sec = 0;
194 tv0.tv_usec = 0;
195 return do_select (fd, ev, &tv0, my_select);
196 }
197
198 static int
199 do_select_wait (int fd, int ev, select_fn my_select)
200 {
201 return do_select (fd, ev, NULL, my_select);
202 }
203
204
205 /* Test select(2) for TTYs. */
206
207 #ifdef INTERACTIVE
208 static void
209 test_tty (select_fn my_select)
210 {
211 if (do_select_nowait (0, SEL_IN, my_select) != 0)
212 failed ("can read");
213 if (do_select_nowait (0, SEL_OUT, my_select) == 0)
214 failed ("cannot write");
215
216 if (do_select_wait (0, SEL_IN, my_select) == 0)
217 failed ("return with infinite timeout");
218
219 getchar ();
220 if (do_select_nowait (0, SEL_IN, my_select) != 0)
221 failed ("can read after getc");
222 }
223 #endif
224
225
226 static int
227 do_select_bad_nfd_nowait (int nfd, select_fn my_select)
228 {
229 struct timeval tv0;
230 tv0.tv_sec = 0;
231 tv0.tv_usec = 0;
232 errno = 0;
233 return my_select (nfd, NULL, NULL, NULL, &tv0);
234 }
235
236 static void
237 test_bad_nfd (select_fn my_select)
238 {
239 if (do_select_bad_nfd_nowait (-1, my_select) != -1 || errno != EINVAL)
240 failed ("invalid errno after negative nfds");
241 /* Can't test FD_SETSIZE + 1 for EINVAL, since some systems allow
242 dynamically larger set size by redefining FD_SETSIZE anywhere up
243 to the actual maximum fd. */
244 #if 0
245 if (do_select_bad_nfd_nowait (FD_SETSIZE + 1, my_select) != -1
246 || errno != EINVAL)
247 failed ("invalid errno after bogus nfds");
248 #endif
249 }
250
251 /* Test select(2) on invalid file descriptors. */
252
253 static int
254 do_select_bad_fd (int fd, int ev, struct timeval *timeout, select_fn my_select)
255 {
256 fd_set rfds, wfds, xfds;
257
258 FD_ZERO (&rfds);
259 FD_ZERO (&wfds);
260 FD_ZERO (&xfds);
261 if (ev & SEL_IN)
262 FD_SET (fd, &rfds);
263 if (ev & SEL_OUT)
264 FD_SET (fd, &wfds);
265 if (ev & SEL_EXC)
266 FD_SET (fd, &xfds);
267 errno = 0;
268 return my_select (fd + 1, &rfds, &wfds, &xfds, timeout);
269 /* In this case, when fd is invalid, on some platforms, the bit for fd
270 is left alone in the fd_set, whereas on other platforms it is cleared.
271 So, don't check the bit for fd here. */
272 }
273
274 static int
275 do_select_bad_fd_nowait (int fd, int ev, select_fn my_select)
276 {
277 struct timeval tv0;
278 tv0.tv_sec = 0;
279 tv0.tv_usec = 0;
280 return do_select_bad_fd (fd, ev, &tv0, my_select);
281 }
282
283 static void
284 test_bad_fd (select_fn my_select)
285 {
286 /* This tests fails on OSF/1 and native Windows, even with fd = 16. */
287 #if !(defined __osf__ || defined WINDOWS_NATIVE)
288 int fd;
289
290 /* On Linux, Mac OS X, *BSD, values of fd like 99 or 399 are discarded
291 by the kernel early and therefore do *not* lead to EBADF, as required
292 by POSIX. */
293 # if defined __linux__ || (defined __APPLE__ && defined __MACH__) || (defined __FreeBSD__ || defined __DragonFly__) || defined __OpenBSD__ || defined __NetBSD__
294 fd = 14;
295 # else
296 fd = 99;
297 # endif
298 /* Even on the best POSIX compliant platforms, values of fd >= FD_SETSIZE
299 require an nfds argument that is > FD_SETSIZE and thus may lead to EINVAL,
300 not EBADF. */
301 if (fd >= FD_SETSIZE)
302 fd = FD_SETSIZE - 1;
303 close (fd);
304
305 if (do_select_bad_fd_nowait (fd, SEL_IN, my_select) == 0 || errno != EBADF)
306 failed ("invalid fd among rfds");
307 if (do_select_bad_fd_nowait (fd, SEL_OUT, my_select) == 0 || errno != EBADF)
308 failed ("invalid fd among wfds");
309 if (do_select_bad_fd_nowait (fd, SEL_EXC, my_select) == 0 || errno != EBADF)
310 failed ("invalid fd among xfds");
311 #endif
312 }
313
314
315 /* Test select(2) for unconnected nonblocking sockets. */
316
317 static void
318 test_connect_first (select_fn my_select)
319 {
320 int s = open_server_socket ();
321 struct sockaddr_in ia;
322 socklen_t addrlen;
323
324 int c1, c2;
325
326 if (do_select_nowait (s, SEL_IN | SEL_EXC, my_select) != 0)
327 failed ("can read, socket not connected");
328
329 c1 = connect_to_socket (false);
330
331 if (do_select_wait (s, SEL_IN | SEL_EXC, my_select) != SEL_IN)
332 failed ("expecting readability on passive socket");
333 if (do_select_nowait (s, SEL_IN | SEL_EXC, my_select) != SEL_IN)
334 failed ("expecting readability on passive socket");
335
336 addrlen = sizeof (ia);
337 c2 = accept (s, (struct sockaddr *) &ia, &addrlen);
338 ASSERT (close (s) == 0);
339 ASSERT (close (c1) == 0);
340 ASSERT (close (c2) == 0);
341 }
342
343
344 /* Test select(2) for unconnected blocking sockets. */
345
346 static void
347 test_accept_first (select_fn my_select)
348 {
349 #ifndef WINDOWS_NATIVE
350 int s = open_server_socket ();
351 struct sockaddr_in ia;
352 socklen_t addrlen;
353 char buf[3];
354 int c, pid;
355
356 pid = fork ();
357 if (pid < 0)
358 return;
359
360 if (pid == 0)
361 {
362 addrlen = sizeof (ia);
363 c = accept (s, (struct sockaddr *) &ia, &addrlen);
364 ASSERT (close (s) == 0);
365 ASSERT (write (c, "foo", 3) == 3);
366 ASSERT (read (c, buf, 3) == 3);
367 shutdown (c, SHUT_RD);
368 ASSERT (close (c) == 0);
369 exit (0);
370 }
371 else
372 {
373 ASSERT (close (s) == 0);
374 c = connect_to_socket (true);
375 if (do_select_nowait (c, SEL_OUT, my_select) != SEL_OUT)
376 failed ("cannot write after blocking connect");
377 ASSERT (write (c, "foo", 3) == 3);
378 wait (&pid);
379 if (do_select_wait (c, SEL_IN, my_select) != SEL_IN)
380 failed ("cannot read data left in the socket by closed process");
381 ASSERT (read (c, buf, 3) == 3);
382 ASSERT (write (c, "foo", 3) == 3);
383 (void) close (c); /* may fail with errno = ECONNRESET */
384 }
385 #endif
386 }
387
388
389 /* Common code for pipes and connected sockets. */
390
391 static void
392 test_pair (int rd, int wd, select_fn my_select)
393 {
394 char buf[3];
395 if (do_select_wait (wd, SEL_IN | SEL_OUT | SEL_EXC, my_select) != SEL_OUT)
396 failed ("expecting writability before writing");
397 if (do_select_nowait (wd, SEL_IN | SEL_OUT | SEL_EXC, my_select) != SEL_OUT)
398 failed ("expecting writability before writing");
399
400 ASSERT (write (wd, "foo", 3) == 3);
401 if (do_select_wait (rd, SEL_IN, my_select) != SEL_IN)
402 failed ("expecting readability after writing");
403 if (do_select_nowait (rd, SEL_IN, my_select) != SEL_IN)
404 failed ("expecting readability after writing");
405
406 ASSERT (read (rd, buf, 3) == 3);
407 }
408
409
410 /* Test select(2) on connected sockets. */
411
412 static void
413 test_socket_pair (select_fn my_select)
414 {
415 struct sockaddr_in ia;
416
417 socklen_t addrlen = sizeof (ia);
418 int s = open_server_socket ();
419 int c1 = connect_to_socket (false);
420 int c2 = accept (s, (struct sockaddr *) &ia, &addrlen);
421
422 ASSERT (close (s) == 0);
423
424 test_pair (c1, c2, my_select);
425 ASSERT (close (c1) == 0);
426 ASSERT (write (c2, "foo", 3) == 3);
427 (void) close (c2); /* may fail with errno = ECONNRESET */
428 }
429
430
431 /* Test select(2) on pipes. */
432
433 static void
434 test_pipe (select_fn my_select)
435 {
436 int fd[2];
437
438 ASSERT (pipe (fd) == 0);
439 test_pair (fd[0], fd[1], my_select);
440 ASSERT (close (fd[0]) == 0);
441 ASSERT (close (fd[1]) == 0);
442 }
443
444
445 /* Do them all. */
446
447 static int
448 test_function (select_fn my_select)
449 {
450 int result = 0;
451
452 #ifdef INTERACTIVE
453 printf ("Please press Enter\n");
454 test (test_tty, "TTY", my_select);
455 #endif
456
457 result += test (test_bad_nfd, my_select, "Invalid nfd test");
458 result += test (test_bad_fd, my_select, "Invalid fd test");
459 result += test (test_connect_first, my_select, "Unconnected socket test");
460 result += test (test_socket_pair, my_select, "Connected sockets test");
461 result += test (test_accept_first, my_select, "General socket test with fork");
462 result += test (test_pipe, my_select, "Pipe test");
463
464 return result;
465 }