nc_bloaty: use poll() instead of select()

function                                             old     new   delta
readwrite                                            829     715    -114

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2017-02-17 17:48:59 +01:00
parent 52a515d187
commit 94dcfd8cc0

View File

@ -582,11 +582,10 @@ void oprint(int direction, unsigned char *p, unsigned bc);
#endif #endif
/* readwrite: /* readwrite:
handle stdin/stdout/network I/O. Bwahaha!! -- the select loop from hell. handle stdin/stdout/network I/O. Bwahaha!! -- the i/o loop from hell.
In this instance, return what might become our exit status. */ In this instance, return what might become our exit status. */
static int readwrite(void) static int readwrite(void)
{ {
int rr;
char *zp = zp; /* gcc */ /* stdin buf ptr */ char *zp = zp; /* gcc */ /* stdin buf ptr */
char *np = np; /* net-in buf ptr */ char *np = np; /* net-in buf ptr */
unsigned rzleft; unsigned rzleft;
@ -594,45 +593,41 @@ static int readwrite(void)
unsigned netretry; /* net-read retry counter */ unsigned netretry; /* net-read retry counter */
unsigned fds_open; unsigned fds_open;
/* if you don't have all this FD_* macro hair in sys/types.h, you'll have to struct pollfd pfds[2];
either find it or do your own bit-bashing: *ding1 |= (1 << fd), etc... */ pfds[0].fd = STDIN_FILENO;
fd_set ding1; /* for select loop */ pfds[0].events = POLLIN;
fd_set ding2; pfds[1].fd = netfd;
FD_ZERO(&ding1); pfds[1].events = POLLIN;
FD_SET(netfd, &ding1);
FD_SET(STDIN_FILENO, &ding1);
fds_open = 2;
fds_open = 2;
netretry = 2; netretry = 2;
rzleft = rnleft = 0; rzleft = rnleft = 0;
if (o_interval) if (o_interval)
sleep(o_interval); /* pause *before* sending stuff, too */ sleep(o_interval); /* pause *before* sending stuff, too */
/* and now the big ol' select shoveling loop ... */ /* and now the big ol' shoveling loop ... */
/* nc 1.10 has "while (FD_ISSET(netfd)" here */ /* nc 1.10 has "while (FD_ISSET(netfd)" here */
while (fds_open) { while (fds_open) {
int rr;
int poll_tmout_ms;
unsigned wretry = 8200; /* net-write sanity counter */ unsigned wretry = 8200; /* net-write sanity counter */
ding2 = ding1; /* FD_COPY ain't portable... */ poll_tmout_ms = -1;
/* some systems, notably linux, crap into their select timers on return, so
we create a expendable copy and give *that* to select. */
if (o_wait) { if (o_wait) {
struct timeval tmp_timer; poll_tmout_ms = INT_MAX;
tmp_timer.tv_sec = o_wait; if (o_wait < INT_MAX / 1000)
tmp_timer.tv_usec = 0; poll_tmout_ms = o_wait * 1000;
/* highest possible fd is netfd (3) */ }
rr = select(netfd+1, &ding2, NULL, NULL, &tmp_timer); rr = poll(pfds, 2, poll_tmout_ms);
} else
rr = select(netfd+1, &ding2, NULL, NULL, NULL);
if (rr < 0 && errno != EINTR) { /* might have gotten ^Zed, etc */ if (rr < 0 && errno != EINTR) { /* might have gotten ^Zed, etc */
holler_perror("select"); holler_perror("poll");
close(netfd); close(netfd);
return 1; return 1;
} }
/* if we have a timeout AND stdin is closed AND we haven't heard anything /* if we have a timeout AND stdin is closed AND we haven't heard anything
from the net during that time, assume it's dead and close it too. */ from the net during that time, assume it's dead and close it too. */
if (rr == 0) { if (rr == 0) {
if (!FD_ISSET(STDIN_FILENO, &ding1)) { if (!pfds[0].revents) {
netretry--; /* we actually try a coupla times. */ netretry--; /* we actually try a coupla times. */
if (!netretry) { if (!netretry) {
if (o_verbose > 1) /* normally we don't care */ if (o_verbose > 1) /* normally we don't care */
@ -641,19 +636,17 @@ static int readwrite(void)
return 0; /* not an error! */ return 0; /* not an error! */
} }
} }
} /* select timeout */ } /* timeout */
/* xxx: should we check the exception fds too? The read fds seem to give
us the right info, and none of the examples I found bothered. */
/* Ding!! Something arrived, go check all the incoming hoppers, net first */ /* Ding!! Something arrived, go check all the incoming hoppers, net first */
if (FD_ISSET(netfd, &ding2)) { /* net: ding! */ if (pfds[1].revents) { /* net: ding! */
rr = read(netfd, bigbuf_net, BIGSIZ); rr = read(netfd, bigbuf_net, BIGSIZ);
if (rr <= 0) { if (rr <= 0) {
if (rr < 0 && o_verbose > 1) { if (rr < 0 && o_verbose > 1) {
/* nc 1.10 doesn't do this */ /* nc 1.10 doesn't do this */
bb_perror_msg("net read"); bb_perror_msg("net read");
} }
FD_CLR(netfd, &ding1); /* net closed */ pfds[1].fd = -1; /* don't poll for netfd anymore */
fds_open--; fds_open--;
rzleft = 0; /* can't write anymore: broken pipe */ rzleft = 0; /* can't write anymore: broken pipe */
} else { } else {
@ -669,12 +662,12 @@ Debug("got %d from the net, errno %d", rr, errno);
goto shovel; goto shovel;
/* okay, suck more stdin */ /* okay, suck more stdin */
if (FD_ISSET(STDIN_FILENO, &ding2)) { /* stdin: ding! */ if (pfds[0].revents) { /* stdin: ding! */
rr = read(STDIN_FILENO, bigbuf_in, BIGSIZ); rr = read(STDIN_FILENO, bigbuf_in, BIGSIZ);
/* Considered making reads here smaller for UDP mode, but 8192-byte /* Considered making reads here smaller for UDP mode, but 8192-byte
mobygrams are kinda fun and exercise the reassembler. */ mobygrams are kinda fun and exercise the reassembler. */
if (rr <= 0) { /* at end, or fukt, or ... */ if (rr <= 0) { /* at end, or fukt, or ... */
FD_CLR(STDIN_FILENO, &ding1); /* disable stdin */ pfds[0].fd = -1; /* disable stdin */
/*close(STDIN_FILENO); - not really necessary */ /*close(STDIN_FILENO); - not really necessary */
/* Let peer know we have no more data */ /* Let peer know we have no more data */
/* nc 1.10 doesn't do this: */ /* nc 1.10 doesn't do this: */
@ -718,7 +711,7 @@ Debug("wrote %d to net, errno %d", rr, errno);
} /* rzleft */ } /* rzleft */
if (o_interval) { /* cycle between slow lines, or ... */ if (o_interval) { /* cycle between slow lines, or ... */
sleep(o_interval); sleep(o_interval);
continue; /* ...with hairy select loop... */ continue; /* ...with hairy loop... */
} }
if (rzleft || rnleft) { /* shovel that shit till they ain't */ if (rzleft || rnleft) { /* shovel that shit till they ain't */
wretry--; /* none left, and get another load */ wretry--; /* none left, and get another load */