rlogin(1): Replace select(2) with poll(2).

Obtanied from:	NetBSD (CVS Rev. 1.27 - 1.28)
This commit is contained in:
Pedro F. Giffuni 2016-02-26 20:02:01 +00:00
parent 563a01a1da
commit 2e0050835a
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=296109

View File

@ -76,6 +76,7 @@ __FBSDID("$FreeBSD$");
#include <errno.h> #include <errno.h>
#include <libutil.h> #include <libutil.h>
#include <paths.h> #include <paths.h>
#include <poll.h>
#include <pwd.h> #include <pwd.h>
#include <syslog.h> #include <syslog.h>
#include <stdio.h> #include <stdio.h>
@ -350,34 +351,27 @@ protocol(int f, int p)
nfd = f + 1; nfd = f + 1;
else else
nfd = p + 1; nfd = p + 1;
if (nfd > FD_SETSIZE) {
syslog(LOG_ERR, "select mask too small, increase FD_SETSIZE");
fatal(f, "internal error (select mask too small)", 0);
}
for (;;) { for (;;) {
fd_set ibits, obits, ebits, *omask; struct pollfd set[2];
FD_ZERO(&ebits); set[0].fd = p;
FD_ZERO(&ibits); set[0].events = POLLPRI;
FD_ZERO(&obits); set[1].fd = f;
omask = (fd_set *)NULL; set[1].events = 0;
if (fcc) { if (fcc)
FD_SET(p, &obits); set[0].events |= POLLOUT;
omask = &obits; else
} else set[1].events |= POLLIN;
FD_SET(f, &ibits);
if (pcc >= 0) { if (pcc >= 0) {
if (pcc) { if (pcc)
FD_SET(f, &obits); set[1].events |= POLLOUT;
omask = &obits; else
} else set[0].events |= POLLIN;
FD_SET(p, &ibits);
} }
FD_SET(p, &ebits); if ((n = poll(set, 2, INFTIM)) < 0) {
if ((n = select(nfd, &ibits, omask, &ebits, 0)) < 0) {
if (errno == EINTR) if (errno == EINTR)
continue; continue;
fatal(f, "select", 1); fatal(f, "poll", 1);
} }
if (n == 0) { if (n == 0) {
/* shouldn't happen... */ /* shouldn't happen... */
@ -385,18 +379,16 @@ protocol(int f, int p)
continue; continue;
} }
#define pkcontrol(c) ((c)&(TIOCPKT_FLUSHWRITE|TIOCPKT_NOSTOP|TIOCPKT_DOSTOP)) #define pkcontrol(c) ((c)&(TIOCPKT_FLUSHWRITE|TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))
if (FD_ISSET(p, &ebits)) { if (set[0].revents & POLLPRI) {
cc = read(p, &cntl, 1); cc = read(p, &cntl, 1);
if (cc == 1 && pkcontrol(cntl)) { if (cc == 1 && pkcontrol(cntl)) {
cntl |= oobdata[0]; cntl |= oobdata[0];
send(f, &cntl, 1, MSG_OOB); send(f, &cntl, 1, MSG_OOB);
if (cntl & TIOCPKT_FLUSHWRITE) { if (cntl & TIOCPKT_FLUSHWRITE)
pcc = 0; pcc = 0;
FD_CLR(p, &ibits);
}
} }
} }
if (FD_ISSET(f, &ibits)) { if (set[1].revents & POLLIN) {
fcc = read(f, fibuf, sizeof(fibuf)); fcc = read(f, fibuf, sizeof(fibuf));
if (fcc < 0 && errno == EWOULDBLOCK) if (fcc < 0 && errno == EWOULDBLOCK)
fcc = 0; fcc = 0;
@ -422,11 +414,10 @@ protocol(int f, int p)
goto top; /* n^2 */ goto top; /* n^2 */
} }
} }
FD_SET(p, &obits); /* try write */
} }
} }
if (FD_ISSET(p, &obits) && fcc > 0) { if (set[0].revents & POLLOUT && fcc > 0) {
cc = write(p, fbp, fcc); cc = write(p, fbp, fcc);
if (cc > 0) { if (cc > 0) {
fcc -= cc; fcc -= cc;
@ -434,7 +425,7 @@ protocol(int f, int p)
} }
} }
if (FD_ISSET(p, &ibits)) { if (set[0].revents & POLLIN) {
pcc = read(p, pibuf, sizeof (pibuf)); pcc = read(p, pibuf, sizeof (pibuf));
pbp = pibuf; pbp = pibuf;
if (pcc < 0 && errno == EWOULDBLOCK) if (pcc < 0 && errno == EWOULDBLOCK)
@ -443,7 +434,6 @@ protocol(int f, int p)
break; break;
else if (pibuf[0] == 0) { else if (pibuf[0] == 0) {
pbp++, pcc--; pbp++, pcc--;
FD_SET(f, &obits); /* try write */
} else { } else {
if (pkcontrol(pibuf[0])) { if (pkcontrol(pibuf[0])) {
pibuf[0] |= oobdata[0]; pibuf[0] |= oobdata[0];
@ -452,18 +442,8 @@ protocol(int f, int p)
pcc = 0; pcc = 0;
} }
} }
if ((FD_ISSET(f, &obits)) && pcc > 0) { if (set[1].revents & POLLOUT && pcc > 0) {
cc = write(f, pbp, pcc); cc = write(f, pbp, pcc);
if (cc < 0 && errno == EWOULDBLOCK) {
/*
* This happens when we try write after read
* from p, but some old kernels balk at large
* writes even when select returns true.
*/
if (!FD_ISSET(p, &ibits))
sleep(5);
continue;
}
if (cc > 0) { if (cc > 0) {
pcc -= cc; pcc -= cc;
pbp += cc; pbp += cc;