forked from andybalholm/spamass-milter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoll.c
65 lines (60 loc) · 1.9 KB
/
poll.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "config.h"
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
#include "subst_poll.h"
/* This function pulled from Markus Gutschke's "wy60" package */
/* $Id: poll.c,v 1.4 2003/06/09 15:57:35 dnelson Exp $ */
int poll(struct pollfd *fds, unsigned long nfds, int timeout) {
// This emulation function is somewhat limited. Most notably, it will never
// report POLLERR, POLLHUP, or POLLNVAL. The calling code has to detect
// these error conditions by some other means (typically by read() or write()
// reporting end-of-file).
fd_set readFds, writeFds, exceptionFds;
struct timeval *timeoutPtr, timeoutStruct;
int i, rc, fd;
FD_ZERO(&readFds);
FD_ZERO(&writeFds);
FD_ZERO(&exceptionFds);
fd = -1;
for (i = nfds; i--; ) {
if (fds[i].events & POLLIN)
FD_SET(fds[i].fd, &readFds);
if (fds[i].events & POLLOUT)
FD_SET(fds[i].fd, &writeFds);
if (fds[i].events & POLLPRI)
FD_SET(fds[i].fd, &exceptionFds);
if (fds[i].fd > fd)
fd = fds[i].fd;
fds[i].revents = 0;
}
if (timeout < 0)
timeoutPtr = NULL;
else {
timeoutStruct.tv_sec = timeout/1000;
timeoutStruct.tv_usec = (timeout%1000) * 1000;
timeoutPtr = &timeoutStruct;
}
i = select(fd + 1, &readFds, &writeFds, &exceptionFds,
timeoutPtr);
if (i <= 0)
rc = i;
else {
rc = 0;
for (i = nfds; i--; ) {
if (FD_ISSET(fds[i].fd, &readFds))
fds[i].revents |= POLLIN;
if (FD_ISSET(fds[i].fd, &writeFds))
fds[i].revents |= POLLOUT;
if (FD_ISSET(fds[i].fd, &exceptionFds))
fds[i].revents |= POLLPRI;
if (fds[i].revents)
rc++;
}
}
return(rc);
}