nc: use poll() instead of select()

function                                             old     new   delta
nc_main                                              943     866     -77

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2017-02-16 17:17:17 +01:00
parent 0ec4d08ea3
commit 5b3b468ec0

View 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) unsigned delay = 0;
IF_NOT_NC_EXTRA (const int execparam = 0;) IF_NOT_NC_EXTRA (const int execparam = 0;)
IF_NC_EXTRA (char **execparam = NULL;) IF_NC_EXTRA (char **execparam = NULL;)
fd_set readfds, testfds; struct pollfd pfds[2];
int opt; /* must be signed (getopt returns -1) */ int opt; /* must be signed (getopt returns -1) */
if (ENABLE_NC_SERVER || ENABLE_NC_EXTRA) { 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]);) 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); pfds[0].fd = STDIN_FILENO;
FD_SET(cfd, &readfds); pfds[0].events = POLLIN;
FD_SET(STDIN_FILENO, &readfds); pfds[1].fd = cfd;
pfds[1].events = POLLIN;
#define iobuf bb_common_bufsiz1 #define iobuf bb_common_bufsiz1
setup_common_bufsiz(); setup_common_bufsiz();
for (;;) { for (;;) {
int fd; int fdidx;
int ofd; int ofd;
int nread; 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) fdidx = 0;
bb_perror_msg_and_die("select");
fd = STDIN_FILENO;
while (1) { while (1) {
if (FD_ISSET(fd, &testfds)) { if (pfds[fdidx].revents) {
nread = safe_read(fd, iobuf, COMMON_BUFSIZE); nread = safe_read(pfds[fdidx].fd, iobuf, COMMON_BUFSIZE);
if (fd == cfd) { if (fdidx != 0) {
if (nread < 1) if (nread < 1)
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
ofd = STDOUT_FILENO; ofd = STDOUT_FILENO;
@ -266,7 +265,7 @@ int nc_main(int argc, char **argv)
/* Close outgoing half-connection so they get EOF, /* Close outgoing half-connection so they get EOF,
* but leave incoming alone so we can see response */ * but leave incoming alone so we can see response */
shutdown(cfd, SHUT_WR); shutdown(cfd, SHUT_WR);
FD_CLR(STDIN_FILENO, &readfds); pfds[0].fd = -1;
} }
ofd = cfd; ofd = cfd;
} }
@ -274,9 +273,9 @@ int nc_main(int argc, char **argv)
if (delay > 0) if (delay > 0)
sleep(delay); sleep(delay);
} }
if (fd == cfd) if (fdidx == 1)
break; break;
fd = cfd; fdidx++;
} }
} }
} }