From: ueno Date: Wed, 17 Oct 2001 04:26:25 +0000 (+0000) Subject: * starttls.c (main): Use poll() instead of select() if available. X-Git-Tag: starttls-0_9~25 X-Git-Url: http://git.chise.org/gitweb/?a=commitdiff_plain;h=ba66bf399aa539536340a33cd8de5841fbdc6cfc;p=elisp%2Fstarttls.git * starttls.c (main): Use poll() instead of select() if available. --- diff --git a/starttls.c b/starttls.c index f777d9b..15fc5a0 100644 --- a/starttls.c +++ b/starttls.c @@ -57,6 +57,9 @@ #include #include #include +#ifdef HAVE_POLL_H +#include +#endif #define _GNU_SOURCE #include @@ -202,7 +205,11 @@ main (argc, argv) { int in = fileno (stdin), out = fileno (stdout), nbuffer, wrote; +#ifdef HAVE_POLL + struct pollfd readfds[2], writefds[1]; +#else fd_set readfds, writefds; +#endif char buffer[BUFSIZ], *retry; struct sigaction act; @@ -258,18 +265,34 @@ main (argc, argv) act.sa_flags = SA_RESTART|SA_RESETHAND; sigaction (SIGALRM, &act, NULL); +#ifdef HAVE_POLL + readfds[0].fd = in; + readfds[1].fd = tls_fd; + readfds[0].events = POLLIN; + readfds[1].events = POLLIN; + writefds[0].events = POLLOUT; +#endif + while (1) { +#ifdef HAVE_POLL + if (poll (readfds, 2, -1) == -1 && errno != EINTR) +#else FD_ZERO (&readfds); FD_SET (tls_fd, &readfds); FD_SET (in, &readfds); if (select (tls_fd+1, &readfds, NULL, NULL, NULL) == -1 && errno != EINTR ) +#endif { - perror ("select"); + perror ("poll"); return 1; } +#ifdef HAVE_POLL + if (readfds[0].revents & POLLIN) +#else if (FD_ISSET (in, &readfds)) +#endif { nbuffer = read (in, buffer, sizeof buffer -1); @@ -277,11 +300,16 @@ main (argc, argv) goto finish; for (retry = buffer; nbuffer > 0; nbuffer -= wrote, retry += wrote) { +#ifdef HAVE_POLL + writefds[0].fd = tls_fd; + if (poll (writefds, 1, -1) == -1) +#else FD_ZERO (&writefds); FD_SET (tls_fd, &writefds); if (select (tls_fd+1, NULL, &writefds, NULL, NULL) == -1) +#endif { - perror ("select"); + perror ("poll"); return 1; } if (tls_conn) @@ -291,7 +319,11 @@ main (argc, argv) if (wrote < 0) goto finish; } } +#ifdef HAVE_POLL + if (readfds[1].revents & POLLIN) +#else if (FD_ISSET (tls_fd, &readfds)) +#endif { if (tls_conn) nbuffer = SSL_read (tls_conn, buffer, sizeof buffer -1); @@ -301,11 +333,16 @@ main (argc, argv) goto finish; for (retry = buffer; nbuffer > 0; nbuffer -= wrote, retry += wrote) { +#ifdef HAVE_POLL + writefds[0].fd = out; + if (poll (writefds, 1, -1) == -1) +#else FD_ZERO (&writefds); FD_SET (out, &writefds); if (select (out+1, NULL, &writefds, NULL, NULL) == -1) +#endif { - perror ("select"); + perror ("poll"); return 1; } wrote = write (out, retry, nbuffer);