diff options
author | Todd C. Miller <millert@cvs.openbsd.org> | 2013-04-21 09:51:25 +0000 |
---|---|---|
committer | Todd C. Miller <millert@cvs.openbsd.org> | 2013-04-21 09:51:25 +0000 |
commit | 2404307084124a76a19c053871bbbfaf4979a202 (patch) | |
tree | 93b153bf1b4f4b885608fa10197e81e8b3406c1e /usr.bin | |
parent | aad99ea5808d938de3e6f5a8f103cd68bc6a7ad9 (diff) |
Convert select() to poll(). OK deraadt@
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/telnet/network.c | 26 | ||||
-rw-r--r-- | usr.bin/telnet/sys_bsd.c | 106 | ||||
-rw-r--r-- | usr.bin/telnet/telnet_locl.h | 4 | ||||
-rw-r--r-- | usr.bin/telnet/tn3270.c | 26 | ||||
-rw-r--r-- | usr.bin/telnet/utilities.c | 24 |
5 files changed, 67 insertions, 119 deletions
diff --git a/usr.bin/telnet/network.c b/usr.bin/telnet/network.c index 3f0af179a06..6aa2f2aebcf 100644 --- a/usr.bin/telnet/network.c +++ b/usr.bin/telnet/network.c @@ -1,4 +1,4 @@ -/* $OpenBSD: network.c,v 1.8 2003/06/03 02:56:18 millert Exp $ */ +/* $OpenBSD: network.c,v 1.9 2013/04/21 09:51:24 millert Exp $ */ /* $NetBSD: network.c,v 1.5 1996/02/28 21:04:06 thorpej Exp $ */ /* @@ -61,34 +61,24 @@ init_network() int stilloob() { - static struct timeval timeout = { 0 }; - fd_set *fdsp; - int fdsn; + struct pollfd pfd[1]; int value; - fdsn = howmany(net+1, NFDBITS) * sizeof(fd_mask); - if ((fdsp = (fd_set *)malloc(fdsn)) == NULL) - err(1, "malloc"); - do { - memset(fdsp, 0, fdsn); - FD_SET(net, fdsp); - value = select(net+1, (fd_set *)0, (fd_set *)0, fdsp, &timeout); + pfd[0].fd = net; + pfd[0].events = POLLRDBAND; + value = poll(pfd, 1, 0); } while ((value == -1) && (errno == EINTR)); if (value < 0) { - perror("select"); - free(fdsp); + perror("poll"); (void) quit(); /* NOTREACHED */ } - if (FD_ISSET(net, fdsp)) { - free(fdsp); + if (pfd[0].revents & POLLRDBAND) return 1; - } else { - free(fdsp); + else return 0; - } } diff --git a/usr.bin/telnet/sys_bsd.c b/usr.bin/telnet/sys_bsd.c index 126742f0589..b9d23eb3eb0 100644 --- a/usr.bin/telnet/sys_bsd.c +++ b/usr.bin/telnet/sys_bsd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: sys_bsd.c,v 1.14 2003/06/03 02:56:18 millert Exp $ */ +/* $OpenBSD: sys_bsd.c,v 1.15 2013/04/21 09:51:24 millert Exp $ */ /* $NetBSD: sys_bsd.c,v 1.11 1996/02/28 21:04:10 thorpej Exp $ */ /* @@ -43,6 +43,11 @@ int tin, /* Input file descriptor */ net; +#define TELNET_FD_TOUT 0 +#define TELNET_FD_TIN 1 +#define TELNET_FD_NET 2 +#define TELNET_FD_NUM 3 + #ifndef USE_TERMIO struct tchars otc = { 0 }, ntc = { 0 }; struct ltchars oltc = { 0 }, nltc = { 0 }; @@ -85,9 +90,6 @@ extern struct termios new_tc; # endif #endif /* USE_TERMIO */ -fd_set *ibitsp, *obitsp, *xbitsp; -int fdsn; - void init_sys() { @@ -961,8 +963,8 @@ sys_telnet_init() */ int -process_rings(netin, netout, netex, ttyin, ttyout, poll) - int poll; /* If 0, then block until something to do */ +process_rings(netin, netout, netex, ttyin, ttyout, dopoll) + int dopoll; /* If 0, then block until something to do */ { int c; /* One wants to be a bit careful about setting returnValue @@ -971,49 +973,34 @@ process_rings(netin, netout, netex, ttyin, ttyout, poll) * time (TN3270 mode only). */ int returnValue = 0; - static struct timeval TimeValue = { 0 }; - int maxfd = -1; - int tmp; - - if ((netout || netin || netex) && net > maxfd) - maxfd = net; - if (ttyout && tout > maxfd) - maxfd = tout; - if (ttyin && tin > maxfd) - maxfd = tin; - tmp = howmany(maxfd+1, NFDBITS) * sizeof(fd_mask); - if (tmp > fdsn) { - if (ibitsp) - free(ibitsp); - if (obitsp) - free(obitsp); - if (xbitsp) - free(xbitsp); - fdsn = tmp; - if ((ibitsp = (fd_set *)malloc(fdsn)) == NULL) - err(1, "malloc"); - if ((obitsp = (fd_set *)malloc(fdsn)) == NULL) - err(1, "malloc"); - if ((xbitsp = (fd_set *)malloc(fdsn)) == NULL) - err(1, "malloc"); - memset(ibitsp, 0, fdsn); - memset(obitsp, 0, fdsn); - memset(xbitsp, 0, fdsn); + struct pollfd pfd[TELNET_FD_NUM]; + + if (ttyout) { + pfd[TELNET_FD_TOUT].fd = tout; + pfd[TELNET_FD_TOUT].events = POLLOUT; + } else { + pfd[TELNET_FD_TOUT].fd = -1; + } + if (ttyin) { + pfd[TELNET_FD_TIN].fd = tin; + pfd[TELNET_FD_TIN].events = POLLIN; + } else { + pfd[TELNET_FD_TIN].fd = -1; + } + if (netout || netin || netex) { + pfd[TELNET_FD_NET].fd = net; + pfd[TELNET_FD_NET].events = 0; + if (netout) + pfd[TELNET_FD_NET].events |= POLLOUT; + if (netin) + pfd[TELNET_FD_NET].events |= POLLIN; + if (netex) + pfd[TELNET_FD_NET].events |= POLLRDBAND; + } else { + pfd[TELNET_FD_NET].fd = -1; } - if (netout) - FD_SET(net, obitsp); - if (ttyout) - FD_SET(tout, obitsp); - if (ttyin) - FD_SET(tin, ibitsp); - if (netin) - FD_SET(net, ibitsp); - if (netex) - FD_SET(net, xbitsp); - - if ((c = select(maxfd+1, ibitsp, obitsp, xbitsp, - (poll == 0)? (struct timeval *)0 : &TimeValue)) < 0) { + if ((c = poll(pfd, TELNET_FD_NUM, dopoll ? 0 : -1)) < 0) { if (c == -1) { /* * we can get EINTR if we are in line mode, @@ -1029,19 +1016,11 @@ process_rings(netin, netout, netex, ttyin, ttyout, poll) * mode, and the transcom process died. */ if (errno == EBADF) { - /* - * zero the bits (even though kernel does it) - * to make sure we are selecting on the right - * ones. - */ - memset(ibitsp, 0, fdsn); - memset(obitsp, 0, fdsn); - memset(xbitsp, 0, fdsn); return 0; } # endif /* defined(TN3270) */ /* I don't like this, does it ever happen? */ - printf("sleep(5) from telnet, after select\r\n"); + printf("sleep(5) from telnet, after poll\r\n"); sleep(5); } return 0; @@ -1050,8 +1029,7 @@ process_rings(netin, netout, netex, ttyin, ttyout, poll) /* * Any urgent data? */ - if (FD_ISSET(net, xbitsp)) { - FD_CLR(net, xbitsp); + if (pfd[TELNET_FD_NET].revents & POLLRDBAND) { SYNCHing = 1; (void) ttyflush(1); /* flush already enqueued data */ } @@ -1059,10 +1037,9 @@ process_rings(netin, netout, netex, ttyin, ttyout, poll) /* * Something to read from the network... */ - if (FD_ISSET(net, ibitsp)) { + if (pfd[TELNET_FD_NET].revents & (POLLIN|POLLHUP)) { int canread; - FD_CLR(net, ibitsp); canread = ring_empty_consecutive(&netiring); #if !defined(SO_OOBINLINE) /* @@ -1172,8 +1149,7 @@ process_rings(netin, netout, netex, ttyin, ttyout, poll) /* * Something to read from the tty... */ - if (FD_ISSET(tin, ibitsp)) { - FD_CLR(tin, ibitsp); + if (pfd[TELNET_FD_TIN].revents & (POLLIN|POLLHUP)) { c = TerminalRead(ttyiring.supply, ring_empty_consecutive(&ttyiring)); if (c < 0 && errno == EIO) c = 0; @@ -1197,12 +1173,10 @@ process_rings(netin, netout, netex, ttyin, ttyout, poll) returnValue = 1; /* did something useful */ } - if (FD_ISSET(net, obitsp)) { - FD_CLR(net, obitsp); + if (pfd[TELNET_FD_NET].revents & POLLOUT) { returnValue |= netflush(); } - if (FD_ISSET(tout, obitsp)) { - FD_CLR(tout, obitsp); + if (pfd[TELNET_FD_TOUT].revents & POLLOUT) { returnValue |= (ttyflush(SYNCHing|flushout) > 0); } diff --git a/usr.bin/telnet/telnet_locl.h b/usr.bin/telnet/telnet_locl.h index 22b6e8c6a01..c51f0075005 100644 --- a/usr.bin/telnet/telnet_locl.h +++ b/usr.bin/telnet/telnet_locl.h @@ -1,4 +1,4 @@ -/* $OpenBSD: telnet_locl.h,v 1.3 2003/05/14 01:46:51 hin Exp $ */ +/* $OpenBSD: telnet_locl.h,v 1.4 2013/04/21 09:51:24 millert Exp $ */ /* $KTH: telnet_locl.h,v 1.13 1997/11/03 21:37:55 assar Exp $ */ /* @@ -56,7 +56,7 @@ #include <fcntl.h> #include <netdb.h> #include <pwd.h> -#include <sys/select.h> +#include <poll.h> #include <sys/time.h> #include <time.h> #include <sys/param.h> diff --git a/usr.bin/telnet/tn3270.c b/usr.bin/telnet/tn3270.c index 2f7c87b2853..40c1b62f939 100644 --- a/usr.bin/telnet/tn3270.c +++ b/usr.bin/telnet/tn3270.c @@ -1,4 +1,4 @@ -/* $OpenBSD: tn3270.c,v 1.6 2003/06/03 02:56:18 millert Exp $ */ +/* $OpenBSD: tn3270.c,v 1.7 2013/04/21 09:51:24 millert Exp $ */ /* $NetBSD: tn3270.c,v 1.5 1996/02/28 21:04:18 thorpej Exp $ */ /* @@ -107,14 +107,13 @@ DataToNetwork(buffer, count, done) while (count) { /* If not enough room for EORs, IACs, etc., wait */ if (NETROOM() < 6) { - fd_set o; + struct pollfd pfd[1]; - FD_ZERO(&o); netflush(); while (NETROOM() < 6) { - FD_SET(net, &o); - (void) select(net+1, (fd_set *) 0, &o, (fd_set *) 0, - (struct timeval *) 0); + pfd[0].fd = net; + pfd[0].events = POLLOUT; + (void) poll(pfd, 1, -1); netflush(); } } @@ -175,7 +174,7 @@ outputPurge() * DataToTerminal - queue up some data to go to terminal. * * Note: there are people who call us and depend on our processing - * *all* the data at one time (thus the select). + * *all* the data at one time (thus the poll). */ int @@ -190,18 +189,13 @@ DataToTerminal(buffer, count) while (count) { if (TTYROOM() == 0) { -#if defined(unix) - fd_set o; + struct pollfd pfd[1]; - FD_ZERO(&o); -#endif /* defined(unix) */ (void) ttyflush(0); while (TTYROOM() == 0) { -#if defined(unix) - FD_SET(tout, &o); - (void) select(tout+1, (fd_set *) 0, &o, (fd_set *) 0, - (struct timeval *) 0); -#endif /* defined(unix) */ + pfd[0].fd = tout; + pfd[0].events = POLLOUT; + (void) poll(pfd, 1, -1); (void) ttyflush(0); } } diff --git a/usr.bin/telnet/utilities.c b/usr.bin/telnet/utilities.c index 8cdea5f1a3e..c59dfccbc71 100644 --- a/usr.bin/telnet/utilities.c +++ b/usr.bin/telnet/utilities.c @@ -1,4 +1,4 @@ -/* $OpenBSD: utilities.c,v 1.10 2007/10/17 20:10:44 chl Exp $ */ +/* $OpenBSD: utilities.c,v 1.11 2013/04/21 09:51:24 millert Exp $ */ /* $NetBSD: utilities.c,v 1.5 1996/02/28 21:04:21 thorpej Exp $ */ /* @@ -847,33 +847,23 @@ printsub(direction, pointer, length) /* EmptyTerminal - called to make sure that the terminal buffer is empty. * Note that we consider the buffer to run all the - * way to the kernel (thus the select). + * way to the kernel (thus the poll). */ void EmptyTerminal() { -#if defined(unix) - fd_set outs; + struct pollfd pfd[1]; - FD_ZERO(&outs); -#endif /* defined(unix) */ + pfd[0].fd = tout; + pfd[0].events = POLLOUT; if (TTYBYTES() == 0) { -#if defined(unix) - FD_SET(tout, &outs); - (void) select(tout+1, (fd_set *) 0, &outs, (fd_set *) 0, - (struct timeval *) 0); /* wait for TTLOWAT */ -#endif /* defined(unix) */ + (void) poll(pfd, 1, -1); /* wait for TTLOWAT */ } else { while (TTYBYTES()) { (void) ttyflush(0); -#if defined(unix) - ttyflush(0); - FD_SET(tout, &outs); - (void) select(tout+1, (fd_set *) 0, &outs, (fd_set *) 0, - (struct timeval *) 0); /* wait for TTLOWAT */ -#endif /* defined(unix) */ + (void) poll(pfd, 1, -1); /* wait for TTLOWAT */ } } } |