nc: use poll() instead of select()
authorDenys Vlasenko <vda.linux@googlemail.com>
Thu, 16 Feb 2017 16:17:17 +0000 (17:17 +0100)
committerDenys Vlasenko <vda.linux@googlemail.com>
Thu, 16 Feb 2017 16:17:17 +0000 (17:17 +0100)
function                                             old     new   delta
nc_main                                              943     866     -77

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
networking/nc.c

index 1b70434ac3f3d1de0e9bd02f85b4a055151419c4..d2b1ddcce53ba179b3f1a3edfd642cfaf9acbf39 100644 (file)
@@ -117,7 +117,7 @@ int nc_main(int argc, char **argv)
        IF_NOT_NC_EXTRA (const) unsigned delay = 0;
        IF_NOT_NC_EXTRA (const int execparam = 0;)
        IF_NC_EXTRA     (char **execparam = NULL;)
-       fd_set readfds, testfds;
+       struct pollfd pfds[2];
        int opt; /* must be signed (getopt returns -1) */
 
        if (ENABLE_NC_SERVER || ENABLE_NC_EXTRA) {
@@ -235,29 +235,28 @@ int nc_main(int argc, char **argv)
                IF_NC_EXTRA(bb_perror_msg_and_die("can't execute '%s'", execparam[0]);)
        }
 
-       /* Select loop copying stdin to cfd, and cfd to stdout */
+       /* loop copying stdin to cfd, and cfd to stdout */
 
-       FD_ZERO(&readfds);
-       FD_SET(cfd, &readfds);
-       FD_SET(STDIN_FILENO, &readfds);
+       pfds[0].fd = STDIN_FILENO;
+       pfds[0].events = POLLIN;
+       pfds[1].fd = cfd;
+       pfds[1].events = POLLIN;
 
 #define iobuf bb_common_bufsiz1
        setup_common_bufsiz();
        for (;;) {
-               int fd;
+               int fdidx;
                int ofd;
                int nread;
 
-               testfds = readfds;
+               if (safe_poll(pfds, 2, -1) < 0)
+                       bb_perror_msg_and_die("poll");
 
-               if (select(cfd + 1, &testfds, NULL, NULL, NULL) < 0)
-                       bb_perror_msg_and_die("select");
-
-               fd = STDIN_FILENO;
+               fdidx = 0;
                while (1) {
-                       if (FD_ISSET(fd, &testfds)) {
-                               nread = safe_read(fd, iobuf, COMMON_BUFSIZE);
-                               if (fd == cfd) {
+                       if (pfds[fdidx].revents) {
+                               nread = safe_read(pfds[fdidx].fd, iobuf, COMMON_BUFSIZE);
+                               if (fdidx != 0) {
                                        if (nread < 1)
                                                exit(EXIT_SUCCESS);
                                        ofd = STDOUT_FILENO;
@@ -266,7 +265,7 @@ int nc_main(int argc, char **argv)
                                                /* Close outgoing half-connection so they get EOF,
                                                 * but leave incoming alone so we can see response */
                                                shutdown(cfd, SHUT_WR);
-                                               FD_CLR(STDIN_FILENO, &readfds);
+                                               pfds[0].fd = -1;
                                        }
                                        ofd = cfd;
                                }
@@ -274,9 +273,9 @@ int nc_main(int argc, char **argv)
                                if (delay > 0)
                                        sleep(delay);
                        }
-                       if (fd == cfd)
+                       if (fdidx == 1)
                                break;
-                       fd = cfd;
+                       fdidx++;
                }
        }
 }