-#ifdef HAVE_SYS_WAIT_H
-#include <sys/wait.h>
-#endif
-
-#ifndef SO_REUSEPORT
-#define SO_REUSEPORT SO_REUSEADDR
-#endif
-
-#define TEST_PORT 12345
-
-
-/* Minimal testing infrastructure. */
-
-static int failures;
-
-static void
-failed (const char *reason)
-{
- if (++failures > 1)
- printf (" ");
- printf ("failed (%s)\n", reason);
-}
-
-static int
-test (void (*fn) (void), const char *msg)
-{
- failures = 0;
- printf ("%s... ", msg);
- fflush (stdout);
- fn ();
-
- if (!failures)
- printf ("passed\n");
-
- return failures;
-}
-
-
-/* Funny socket code. */
-
-static int
-open_server_socket ()
-{
- int s, x;
- struct sockaddr_in ia;
-
- s = socket (AF_INET, SOCK_STREAM, 0);
-
- memset (&ia, 0, sizeof (ia));
- ia.sin_family = AF_INET;
- inet_pton (AF_INET, "127.0.0.1", &ia.sin_addr);
- ia.sin_port = htons (TEST_PORT);
- if (bind (s, (struct sockaddr *) &ia, sizeof (ia)) < 0)
- {
- perror ("bind");
- exit (77);
- }
-
- x = 1;
- setsockopt (s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof (x));
-
- if (listen (s, 1) < 0)
- {
- perror ("listen");
- exit (77);
- }
-
- return s;
-}
-
-static int
-connect_to_socket (int blocking)
-{
- int s;
- struct sockaddr_in ia;
-
- s = socket (AF_INET, SOCK_STREAM, 0);
-
- memset (&ia, 0, sizeof (ia));
- ia.sin_family = AF_INET;
- inet_pton (AF_INET, "127.0.0.1", &ia.sin_addr);
- ia.sin_port = htons (TEST_PORT);
-
- if (!blocking)
- {
-#ifdef WIN32_NATIVE
- unsigned long iMode = 1;
- ioctl (s, FIONBIO, (char *) &iMode);
-
-#elif defined F_GETFL
- int oldflags = fcntl (s, F_GETFL, NULL);
-
- if (!(oldflags & O_NONBLOCK))
- fcntl (s, F_SETFL, oldflags | O_NONBLOCK);
-#endif
- }
-
- if (connect (s, (struct sockaddr *) &ia, sizeof (ia)) < 0
- && (blocking || errno != EINPROGRESS))
- {
- perror ("connect");
- exit (77);
- }
-
- return s;
-}
-
-
-/* A slightly more convenient interface to select(2). */
-
-static int
-do_select (int fd, int ev, struct timeval *tv)
-{
- fd_set rfds, wfds, xfds;
- int r, rev;
-
- FD_ZERO (&rfds);
- FD_ZERO (&wfds);
- FD_ZERO (&xfds);
- if (ev & SEL_IN)
- FD_SET (fd, &rfds);
- if (ev & SEL_OUT)
- FD_SET (fd, &wfds);
- if (ev & SEL_EXC)
- FD_SET (fd, &xfds);
- r = select (fd + 1, &rfds, &wfds, &xfds, tv);
- if (r < 0)
- return r;
-
- rev = 0;
- if (FD_ISSET (fd, &rfds))
- rev |= SEL_IN;
- if (FD_ISSET (fd, &wfds))
- rev |= SEL_OUT;
- if (FD_ISSET (fd, &xfds))
- rev |= SEL_EXC;
- if (rev && r == 0)
- failed ("select returned 0");
- if (rev & ~ev)
- failed ("select returned unrequested events");
-
- return rev;
-}
-
-static int
-do_select_nowait (int fd, int ev)
-{
- static struct timeval tv0;
- return do_select (fd, ev, &tv0);
-}
-
-static int
-do_select_wait (int fd, int ev)
-{
- return do_select (fd, ev, NULL);
-}
-
-
-/* Test poll(2) for TTYs. */
-
-#ifdef INTERACTIVE
-static void
-test_tty (void)
-{
- if (do_select_nowait (0, SEL_IN) != 0)
- failed ("can read");
- if (do_select_nowait (0, SEL_OUT) == 0)
- failed ("cannot write");
-
- if (do_select_wait (0, SEL_IN) == 0)
- failed ("return with infinite timeout");
-
- getchar ();
- if (do_select_nowait (0, SEL_IN) != 0)
- failed ("can read after getc");
-}