[git commit] nc: use poll() instead of select()

Denys Vlasenko vda.linux at googlemail.com
Thu Feb 16 16:17:17 UTC 2017


commit: https://git.busybox.net/busybox/commit/?id=5b3b468ec0c6e6dfe772722dbd6b2c57cef817b5
branch: https://git.busybox.net/busybox/commit/?id=refs/heads/master

function                                             old     new   delta
nc_main                                              943     866     -77

Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 networking/nc.c | 33 ++++++++++++++++-----------------
 1 file changed, 16 insertions(+), 17 deletions(-)

diff --git a/networking/nc.c b/networking/nc.c
index 1b70434..d2b1ddc 100644
--- a/networking/nc.c
+++ b/networking/nc.c
@@ -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++;
 		}
 	}
 }


More information about the busybox-cvs mailing list