From 720a68f0caf459b5251357526f46ed0c67402fb2 Mon Sep 17 00:00:00 2001 From: mycroft Date: Wed, 18 Sep 2002 20:58:56 +0000 Subject: [PATCH] select() -> poll() --- libexec/telnetd/defs.h | 3 +- libexec/telnetd/telnetd.c | 72 +++++++++++++++++---------------------- libexec/telnetd/utility.c | 18 ++++------ usr.sbin/apmd/apmd.c | 31 ++++++++--------- 4 files changed, 56 insertions(+), 68 deletions(-) diff --git a/libexec/telnetd/defs.h b/libexec/telnetd/defs.h index b2e479bace91..a37d845447e1 100644 --- a/libexec/telnetd/defs.h +++ b/libexec/telnetd/defs.h @@ -1,4 +1,4 @@ -/* $NetBSD: defs.h,v 1.11 2002/05/26 00:02:08 wiz Exp $ */ +/* $NetBSD: defs.h,v 1.12 2002/09/18 20:58:56 mycroft Exp $ */ /* * Copyright (c) 1989, 1993 @@ -72,6 +72,7 @@ #include #include #include +#include #ifndef FILIO_H #include #else diff --git a/libexec/telnetd/telnetd.c b/libexec/telnetd/telnetd.c index 5533384b8bfe..8c4bae601666 100644 --- a/libexec/telnetd/telnetd.c +++ b/libexec/telnetd/telnetd.c @@ -1,4 +1,4 @@ -/* $NetBSD: telnetd.c,v 1.33 2002/08/20 13:58:22 christos Exp $ */ +/* $NetBSD: telnetd.c,v 1.34 2002/09/18 20:58:56 mycroft Exp $ */ /* * Copyright (C) 1997 and 1998 WIDE Project. @@ -69,7 +69,7 @@ __COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\ #if 0 static char sccsid[] = "@(#)telnetd.c 8.4 (Berkeley) 5/30/95"; #else -__RCSID("$NetBSD: telnetd.c,v 1.33 2002/08/20 13:58:22 christos Exp $"); +__RCSID("$NetBSD: telnetd.c,v 1.34 2002/09/18 20:58:56 mycroft Exp $"); #endif #endif /* not lint */ @@ -1137,7 +1137,7 @@ telnet(f, p, host) (void) ioctl(p, FIONBIO, (char *)&on); #if defined(SO_OOBINLINE) - (void) setsockopt(net, SOL_SOCKET, SO_OOBINLINE, + (void) setsockopt(f, SOL_SOCKET, SO_OOBINLINE, (char *)&on, sizeof on); #endif /* defined(SO_OOBINLINE) */ @@ -1231,40 +1231,32 @@ telnet(f, p, host) nfd = ((f > p) ? f : p) + 1; for (;;) { - fd_set ibits, obits, xbits; + struct pollfd set[2]; register int c; if (ncc < 0 && pcc < 0) break; - FD_ZERO(&ibits); - FD_ZERO(&obits); - FD_ZERO(&xbits); - - if (f >= FD_SETSIZE) - fatal(net, "fd too large"); - if (p >= FD_SETSIZE) - fatal(net, "fd too large"); - /* * Never look for input if there's still * stuff in the corresponding output buffer */ - if (nfrontp - nbackp || pcc > 0) { - FD_SET(f, &obits); - } else { - FD_SET(p, &ibits); - } - if (pfrontp - pbackp || ncc > 0) { - FD_SET(p, &obits); - } else { - FD_SET(f, &ibits); - } - if (!SYNCHing) { - FD_SET(f, &xbits); - } - if ((c = select(nfd, &ibits, &obits, &xbits, - (struct timeval *)0)) < 1) { + set[0].fd = f; + set[0].events = 0; + set[1].fd = p; + set[1].events = 0; + if (nfrontp - nbackp || pcc > 0) + set[0].events |= POLLOUT; + else + set[1].events |= POLLIN; + if (pfrontp - pbackp || ncc > 0) + set[1].events |= POLLOUT; + else + set[0].events |= POLLIN; + if (!SYNCHing) + set[0].events |= POLLPRI; + + if ((c = poll(set, 2, INFTIM)) < 1) { if (c == -1) { if (errno == EINTR) { continue; @@ -1277,14 +1269,14 @@ telnet(f, p, host) /* * Any urgent data? */ - if (FD_ISSET(net, &xbits)) { + if (set[0].revents & POLLPRI) { SYNCHing = 1; } /* * Something to read from the network... */ - if (FD_ISSET(net, &ibits)) { + if (set[0].revents && POLLIN) { #if !defined(SO_OOBINLINE) /* * In 4.2 (and 4.3 beta) systems, the @@ -1323,24 +1315,24 @@ telnet(f, p, host) if (SYNCHing) { int atmark; - (void) ioctl(net, SIOCATMARK, (char *)&atmark); + (void) ioctl(f, SIOCATMARK, (char *)&atmark); if (atmark) { - ncc = recv(net, netibuf, sizeof (netibuf), MSG_OOB); + ncc = recv(f, netibuf, sizeof (netibuf), MSG_OOB); if ((ncc == -1) && (errno == EINVAL)) { - ncc = read(net, netibuf, sizeof (netibuf)); + ncc = read(f, netibuf, sizeof (netibuf)); if (sequenceIs(didnetreceive, gotDM)) { - SYNCHing = stilloob(net); + SYNCHing = stilloob(f); } } } else { - ncc = read(net, netibuf, sizeof (netibuf)); + ncc = read(f, netibuf, sizeof (netibuf)); } } else { - ncc = read(net, netibuf, sizeof (netibuf)); + ncc = read(f, netibuf, sizeof (netibuf)); } settimer(didnetreceive); #else /* !defined(SO_OOBINLINE)) */ - ncc = read(net, netibuf, sizeof (netibuf)); + ncc = read(f, netibuf, sizeof (netibuf)); #endif /* !defined(SO_OOBINLINE)) */ if (ncc < 0 && errno == EWOULDBLOCK) ncc = 0; @@ -1358,7 +1350,7 @@ telnet(f, p, host) /* * Something to read from the pty... */ - if (FD_ISSET(p, &ibits)) { + if (set[1].revents & POLLIN) { #ifndef STREAMSPTY pcc = read(p, ptyibuf, BUFSIZ); #else @@ -1443,11 +1435,11 @@ telnet(f, p, host) } } - if (FD_ISSET(f, &obits) && (nfrontp - nbackp) > 0) + if (set[0].revents & POLLOUT && (nfrontp - nbackp) > 0) netflush(); if (ncc > 0) telrcv(); - if (FD_ISSET(p, &obits) && (pfrontp - pbackp) > 0) + if (set[1].revents & POLLOUT && (pfrontp - pbackp) > 0) ptyflush(); } cleanup(0); diff --git a/libexec/telnetd/utility.c b/libexec/telnetd/utility.c index 08363b8fb450..945728472582 100644 --- a/libexec/telnetd/utility.c +++ b/libexec/telnetd/utility.c @@ -1,4 +1,4 @@ -/* $NetBSD: utility.c,v 1.19 2002/08/12 09:19:00 abs Exp $ */ +/* $NetBSD: utility.c,v 1.20 2002/09/18 20:58:57 mycroft Exp $ */ /* * Copyright (c) 1989, 1993 @@ -38,7 +38,7 @@ #if 0 static char sccsid[] = "@(#)utility.c 8.4 (Berkeley) 5/30/95"; #else -__RCSID("$NetBSD: utility.c,v 1.19 2002/08/12 09:19:00 abs Exp $"); +__RCSID("$NetBSD: utility.c,v 1.20 2002/09/18 20:58:57 mycroft Exp $"); #endif #endif /* not lint */ @@ -96,23 +96,19 @@ ttloop() stilloob(s) int s; /* socket number */ { - static struct timeval timeout = { 0 }; - fd_set excepts; + struct pollfd set[1]; int value; - if (s >= FD_SETSIZE) - fatal(pty, "fd too large"); - + set[0].fd = net; + set[0].events = POLLPRI; do { - FD_ZERO(&excepts); - FD_SET(s, &excepts); - value = select(s+1, (fd_set *)0, (fd_set *)0, &excepts, &timeout); + value = poll(set, 1, 0); } while ((value == -1) && (errno == EINTR)); if (value < 0) { fatalperror(pty, "select"); } - if (FD_ISSET(s, &excepts)) { + if (set[0].revents & POLLPRI) { return 1; } else { return 0; diff --git a/usr.sbin/apmd/apmd.c b/usr.sbin/apmd/apmd.c index 014d5af49f8a..b86f994c2191 100644 --- a/usr.sbin/apmd/apmd.c +++ b/usr.sbin/apmd/apmd.c @@ -1,4 +1,4 @@ -/* $NetBSD: apmd.c,v 1.22 2002/01/11 04:35:52 itojun Exp $ */ +/* $NetBSD: apmd.c,v 1.23 2002/09/18 21:06:39 mycroft Exp $ */ /*- * Copyright (c) 1996, 2000 The NetBSD Foundation, Inc. @@ -54,6 +54,7 @@ #include #include #include +#include #include #include #include "pathnames.h" @@ -309,15 +310,14 @@ main(int argc, char *argv[]) const char *fname = apmdev; int ctl_fd, sock_fd, ch, ready; int statonly = 0; - fd_set devfds; - fd_set selcopy; + struct pollfd set[2]; struct apm_event_info apmevent; int suspends, standbys, resumes; int ac_is_off; int noacsleep = 0; int lowbattsleep = 0; mode_t mode = 0660; - struct timeval tv = {TIMO, 0}, stv; + unsigned long timeout = TIMO; const char *sockname = sockfile; char *user, *group; char *scratch; @@ -350,8 +350,8 @@ main(int argc, char *argv[]) sockname = optarg; break; case 't': - tv.tv_sec = strtoul(optarg, 0, 0); - if (tv.tv_sec == 0) + timeout = strtoul(optarg, 0, 0); + if (timeout == 0) usage(); break; case 'm': @@ -427,16 +427,15 @@ main(int argc, char *argv[]) sock_fd = bind_socket(sockname, mode, uid, gid); - FD_ZERO(&devfds); - FD_SET(ctl_fd, &devfds); - FD_SET(sock_fd, &devfds); - + set[0].fd = ctl_fd; + set[0].events = POLLIN; + set[1].fd = sock_fd; + set[1].events = POLLIN; - for (selcopy = devfds, errno = 0, stv = tv; - (ready = select(MAX(ctl_fd,sock_fd)+1, &selcopy, 0, 0, &stv)) >= 0 || - errno == EINTR; - selcopy = devfds, errno = 0, stv = tv) { + for (errno = 0; + (ready = poll(set, 2, timeout * 1000)) >= 0 || errno == EINTR; + errno = 0) { if (errno == EINTR) continue; if (ready == 0) { @@ -453,7 +452,7 @@ main(int argc, char *argv[]) suspend(ctl_fd); } } - if (FD_ISSET(ctl_fd, &selcopy)) { + if (set[0].revents & POLLIN) { suspends = standbys = resumes = 0; while (ioctl(ctl_fd, APM_IOC_NEXTEVENT, &apmevent) == 0) { if (debug) @@ -515,7 +514,7 @@ main(int argc, char *argv[]) } if (ready == 0) continue; - if (FD_ISSET(sock_fd, &selcopy)) { + if (set[1].revents & POLLIN) { switch (handle_client(sock_fd, ctl_fd)) { case NORMAL: break;