select() -> poll()

This commit is contained in:
mycroft
2002-09-20 14:16:03 +00:00
parent a6819b86f6
commit 27fea982b0

View File

@@ -1,4 +1,4 @@
/* $NetBSD: loop-bsd.c,v 1.5 1999/01/11 22:40:01 kleink Exp $ */ /* $NetBSD: loop-bsd.c,v 1.6 2002/09/20 14:16:03 mycroft Exp $ */
/* /*
* Copyright (c) 1993-95 Mats O Jansson. All rights reserved. * Copyright (c) 1993-95 Mats O Jansson. All rights reserved.
@@ -31,7 +31,7 @@
#include <sys/cdefs.h> #include <sys/cdefs.h>
#ifndef lint #ifndef lint
__RCSID("$NetBSD: loop-bsd.c,v 1.5 1999/01/11 22:40:01 kleink Exp $"); __RCSID("$NetBSD: loop-bsd.c,v 1.6 2002/09/20 14:16:03 mycroft Exp $");
#endif #endif
#include <errno.h> #include <errno.h>
@@ -43,6 +43,8 @@ __RCSID("$NetBSD: loop-bsd.c,v 1.5 1999/01/11 22:40:01 kleink Exp $");
#endif #endif
#include <net/bpf.h> #include <net/bpf.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/poll.h>
#include <assert.h>
#include "os.h" #include "os.h"
#include "common.h" #include "common.h"
@@ -105,10 +107,10 @@ void
Loop() Loop()
{ {
u_char *buf, *bp, *ep; u_char *buf, *bp, *ep;
int cc; int cc, n, m;
fd_set fds, listeners; struct pollfd *set;
int bufsize, maxfd = 0; int bufsize;
struct if_info *ii; struct if_info *ii;
if (iflist == 0) { if (iflist == 0) {
syslog(LOG_ERR, "no interfaces"); syslog(LOG_ERR, "no interfaces");
@@ -129,26 +131,22 @@ Loop()
* Find the highest numbered file descriptor for select(). * Find the highest numbered file descriptor for select().
* Initialize the set of descriptors to listen to. * Initialize the set of descriptors to listen to.
*/ */
FD_ZERO(&fds); for (ii = iflist, n = 0; ii; ii = ii->next, n++)
for (ii = iflist; ii; ii = ii->next) { ;
if (ii->fd != -1) { set = malloc(n * sizeof(*set));
FD_SET(ii->fd, &fds); for (ii = iflist, m = 0; ii; ii = ii->next, m++) {
if (ii->fd > maxfd) assert(ii->fd != -1);
maxfd = ii->fd; set[m].fd = ii->fd;
} set[m].events = POLLIN;
} }
while (1) { for (;;) {
listeners = fds; if (poll(set, n, INFTIM) < 0) {
if (select(maxfd + 1, &listeners, (struct fd_set *) 0, syslog(LOG_ERR, "poll: %m");
(struct fd_set *) 0, (struct timeval *) 0) < 0) {
syslog(LOG_ERR, "select: %m");
exit(0); exit(0);
} }
for (ii = iflist; ii; ii = ii->next) { for (ii = iflist, m = 0; ii; ii = ii->next, m++) {
if (ii->fd != -1) { if (!(set[m].revents & POLLIN))
if (!FD_ISSET(ii->fd, &listeners)) continue;
continue;
}
again: again:
cc = read(ii->fd, (char *) buf, bufsize); cc = read(ii->fd, (char *) buf, bufsize);
/* Don't choke when we get ptraced */ /* Don't choke when we get ptraced */