Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.sbin/faithd use poll(2) instead of select(2). based on ...



details:   https://anonhg.NetBSD.org/src/rev/379f67c6bb6a
branches:  trunk
changeset: 551255:379f67c6bb6a
user:      itojun <itojun%NetBSD.org@localhost>
date:      Tue Sep 02 22:57:29 2003 +0000

description:
use poll(2) instead of select(2).  based on patch from deraadt@openbsd, via kame

diffstat:

 usr.sbin/faithd/faithd.c |   34 +++++-------
 usr.sbin/faithd/ftp.c    |  119 +++++++++++++++++++++++-----------------------
 2 files changed, 74 insertions(+), 79 deletions(-)

diffs (truncated from 317 to 300 lines):

diff -r 960d11842e3c -r 379f67c6bb6a usr.sbin/faithd/faithd.c
--- a/usr.sbin/faithd/faithd.c  Tue Sep 02 22:56:11 2003 +0000
+++ b/usr.sbin/faithd/faithd.c  Tue Sep 02 22:57:29 2003 +0000
@@ -1,5 +1,5 @@
-/*     $NetBSD: faithd.c,v 1.29 2003/05/15 00:23:54 itojun Exp $       */
-/*     $KAME: faithd.c,v 1.60 2003/05/15 00:21:08 itojun Exp $ */
+/*     $NetBSD: faithd.c,v 1.30 2003/09/02 22:57:29 itojun Exp $       */
+/*     $KAME: faithd.c,v 1.62 2003/08/19 21:20:33 itojun Exp $ */
 
 /*
  * Copyright (C) 1997 and 1998 WIDE Project.
@@ -45,10 +45,8 @@
 #include <sys/stat.h>
 #include <sys/time.h>
 #include <sys/ioctl.h>
-#ifdef __FreeBSD__
-#include <libutil.h>
-#endif
 
+#include <poll.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdarg.h>
@@ -349,9 +347,8 @@
        socklen_t len;
        int s_src;
        pid_t child_pid;
-       fd_set rfds;
+       struct pollfd pfd[2];
        int error;
-       int maxfd;
 
        /*
         * Wait, accept, fork, faith....
@@ -359,21 +356,18 @@
 again:
        setproctitle("%s", procname);
 
-       FD_ZERO(&rfds);
-       if (s_wld >= FD_SETSIZE)
-               exit_failure("descriptor too big");
-       FD_SET(s_wld, &rfds);
-       maxfd = s_wld;
+       pfd[0].fd = s_wld;
+       pfd[0].events = POLLIN;
+       pfd[1].fd = -1;
+       pfd[1].revents = 0;
 #ifdef USE_ROUTE
        if (sockfd) {
-               if (sockfd >= FD_SETSIZE)
-                       exit_failure("descriptor too big");
-               FD_SET(sockfd, &rfds);
-               maxfd = (maxfd < sockfd) ? sockfd : maxfd;
+               pfd[1].fd = sockfd;
+               pfd[1].events = POLLIN;
        }
 #endif
 
-       error = select(maxfd + 1, &rfds, NULL, NULL, NULL);
+       error = poll(pfd, sizeof(pfd)/sizeof(pfd[0]), 0);
        if (error < 0) {
                if (errno == EINTR)
                        goto again;
@@ -382,11 +376,13 @@
        }
 
 #ifdef USE_ROUTE
-       if (FD_ISSET(sockfd, &rfds)) {
+       if (pfd[1].revents & POLLIN)
+       {
                update_myaddrs();
        }
 #endif
-       if (FD_ISSET(s_wld, &rfds)) {
+       if (pfd[0].revents & POLLIN)
+       {
                len = sizeof(srcaddr);
                s_src = accept(s_wld, (struct sockaddr *)&srcaddr, &len);
                if (s_src < 0) {
diff -r 960d11842e3c -r 379f67c6bb6a usr.sbin/faithd/ftp.c
--- a/usr.sbin/faithd/ftp.c     Tue Sep 02 22:56:11 2003 +0000
+++ b/usr.sbin/faithd/ftp.c     Tue Sep 02 22:57:29 2003 +0000
@@ -1,5 +1,5 @@
-/*     $NetBSD: ftp.c,v 1.12 2002/09/08 01:41:12 itojun Exp $  */
-/*     $KAME: ftp.c,v 1.20 2002/09/08 01:12:30 itojun Exp $    */
+/*     $NetBSD: ftp.c,v 1.13 2003/09/02 22:57:30 itojun Exp $  */
+/*     $KAME: ftp.c,v 1.23 2003/08/19 21:20:33 itojun Exp $    */
 
 /*
  * Copyright (C) 1997 and 1998 WIDE Project.
@@ -41,6 +41,7 @@
 #include <string.h>
 #include <syslog.h>
 #include <unistd.h>
+#include <poll.h>
 #include <errno.h>
 #include <ctype.h>
 
@@ -72,7 +73,7 @@
 void
 ftp_relay(int ctl6, int ctl4)
 {
-       fd_set readfds;
+       struct pollfd pfd[6];
        int error;
        enum state state = NONE;
        struct timeval tv;
@@ -80,49 +81,42 @@
        syslog(LOG_INFO, "starting ftp control connection");
 
        for (;;) {
-               int maxfd = 0;
-
-               FD_ZERO(&readfds);
-               if (ctl4 >= FD_SETSIZE)
-                       exit_failure("descriptor too big");
-               FD_SET(ctl4, &readfds);
-               maxfd = ctl4;
-               if (ctl6 >= FD_SETSIZE)
-                       exit_failure("descriptor too big");
-               FD_SET(ctl6, &readfds);
-               maxfd = (ctl6 > maxfd) ? ctl6 : maxfd;
+               pfd[0].fd = ctl4;
+               pfd[0].events = POLLIN;
+               pfd[1].fd = ctl6;
+               pfd[1].events = POLLIN;
                if (0 <= port4) {
-                       if (port4 >= FD_SETSIZE)
-                               exit_failure("descriptor too big");
-                       FD_SET(port4, &readfds);
-                       maxfd = (port4 > maxfd) ? port4 : maxfd;
-               }
+                       pfd[2].fd = port4;
+                       pfd[2].events = POLLIN;
+               } else
+                       pfd[2].fd = -1;
                if (0 <= port6) {
-                       if (port6 >= FD_SETSIZE)
-                               exit_failure("descriptor too big");
-                       FD_SET(port6, &readfds);
-                       maxfd = (port6 > maxfd) ? port6 : maxfd;
-               }
+                       pfd[3].fd = port6;
+                       pfd[3].events = POLLIN;
+               } else
+                       pfd[3].fd = -1;
 #if 0
                if (0 <= wport4) {
-                       if (wport4 >= FD_SETSIZE)
-                               exit_failure("descriptor too big");
-                       FD_SET(wport4, &readfds);
-                       maxfd = (wport4 > maxfd) ? wport4 : maxfd;
-               }
+                       pfd[4].fd = wport4;
+                       pfd[4].events = POLLIN;
+               } else
+                       pfd[4].fd = -1;
                if (0 <= wport6) {
-                       if (wport6 >= FD_SETSIZE)
-                               exit_failure("descriptor too big");
-                       FD_SET(wport6, &readfds);
-                       maxfd = (wport6 > maxfd) ? wport6 : maxfd;
-               }
+                       pfd[5].fd = wport4;
+                       pfd[5].events = POLLIN;
+               } else
+                       pfd[5].fd = -1;
+#else
+               pfd[4].fd = pfd[5].fd = -1;
+               pfd[4].events = pfd[5].events = 0;
 #endif
                tv.tv_sec = FAITH_TIMEOUT;
                tv.tv_usec = 0;
 
-               error = select(maxfd + 1, &readfds, NULL, NULL, &tv);
-               if (error == -1)
-                       exit_failure("select: %s", strerror(errno));
+               error = poll(pfd, sizeof(pfd)/sizeof(pfd[0]), tv.tv_sec * 1000);
+               if (error == -1) {
+                       exit_failure("poll: %s", strerror(errno));
+               }
                else if (error == 0)
                        exit_failure("connection timeout");
 
@@ -132,7 +126,8 @@
                 * otherwise some of the pipe may become full and we cannot
                 * relay correctly.
                 */
-               if (FD_ISSET(ctl6, &readfds)) {
+               if (pfd[1].revents & POLLIN)
+               {
                        /*
                         * copy control connection from the client.
                         * command translation is necessary.
@@ -148,7 +143,8 @@
                                /*NOTREACHED*/
                        }
                }
-               if (FD_ISSET(ctl4, &readfds)) {
+               if (pfd[0].revents & POLLIN)
+               {
                        /*
                         * copy control connection from the server
                         * translation of result code is necessary.
@@ -164,12 +160,13 @@
                                /*NOTREACHED*/
                        }
                }
-               if (0 <= port4 && 0 <= port6 && FD_ISSET(port4, &readfds)) {
+               if (0 <= port4 && 0 <= port6 && (pfd[2].revents & POLLIN))
+               {
                        /*
                         * copy data connection.
                         * no special treatment necessary.
                         */
-                       if (FD_ISSET(port4, &readfds))
+                       if (pfd[2].revents & POLLIN)
                                error = ftp_copy(port4, port6);
                        switch (error) {
                        case -1:
@@ -184,12 +181,13 @@
                                break;
                        }
                }
-               if (0 <= port4 && 0 <= port6 && FD_ISSET(port6, &readfds)) {
+               if (0 <= port4 && 0 <= port6 && (pfd[3].revents & POLLIN))
+               {
                        /*
                         * copy data connection.
                         * no special treatment necessary.
                         */
-                       if (FD_ISSET(port6, &readfds))
+                       if (pfd[3].revents & POLLIN)
                                error = ftp_copy(port6, port4);
                        switch (error) {
                        case -1:
@@ -205,13 +203,15 @@
                        }
                }
 #if 0
-               if (wport4 && FD_ISSET(wport4, &readfds)) {
+               if (wport4 && (pfd[4].revents & POLLIN))
+               {
                        /*
                         * establish active data connection from the server.
                         */
                        ftp_activeconn();
                }
-               if (wport6 && FD_ISSET(wport6, &readfds)) {
+               if (wport4 && (pfd[5].revents & POLLIN))
+               {
                        /*
                         * establish passive data connection from the client.
                         */
@@ -229,20 +229,19 @@
 {
        socklen_t n;
        int error;
-       fd_set set;
+       struct pollfd pfd[1];
        struct timeval timeout;
        struct sockaddr *sa;
 
        /* get active connection from server */
-       FD_ZERO(&set);
-       if (wport4 >= FD_SETSIZE)
-               exit_failure("descriptor too big");
-       FD_SET(wport4, &set);
+       pfd[0].fd = wport4;
+       pfd[0].events = POLLIN;
        timeout.tv_sec = 120;
-       timeout.tv_usec = -1;
+       timeout.tv_usec = 0;
        n = sizeof(data4);
-       if (select(wport4 + 1, &set, NULL, NULL, &timeout) == 0
-        || (port4 = accept(wport4, (struct sockaddr *)&data4, &n)) < 0) {
+       if (poll(pfd, sizeof(pfd)/sizeof(pfd[0]), timeout.tv_sec * 1000) == 0 ||
+           (port4 = accept(wport4, (struct sockaddr *)&data4, &n)) < 0)
+       {
                close(wport4);
                wport4 = -1;
                syslog(LOG_INFO, "active mode data connection failed");
@@ -278,20 +277,19 @@
 {
        socklen_t len;
        int error;
-       fd_set set;
+       struct pollfd pfd[1];
        struct timeval timeout;
        struct sockaddr *sa;
 
        /* get passive connection from client */
-       FD_ZERO(&set);
-       if (wport6 >= FD_SETSIZE)
-               exit_failure("descriptor too big");
-       FD_SET(wport6, &set);
+       pfd[0].fd = wport6;
+       pfd[0].events = POLLIN;
        timeout.tv_sec = 120;
        timeout.tv_usec = 0;



Home | Main Index | Thread Index | Old Index