]>
Commit | Line | Data |
---|---|---|
6d39a5c4 | 1 | /* $Id$ */ |
2 | ||
3 | /* | |
4 | * Copyright (c) 2004, 2005, 2007 Darren Tucker (dtucker at zip com au). | |
5 | * | |
6 | * Permission to use, copy, modify, and distribute this software for any | |
7 | * purpose with or without fee is hereby granted, provided that the above | |
8 | * copyright notice and this permission notice appear in all copies. | |
9 | * | |
10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
17 | */ | |
18 | ||
19 | #include "includes.h" | |
6ce84f4f | 20 | #if !defined(HAVE_POLL) |
6d39a5c4 | 21 | |
22 | #ifdef HAVE_SYS_SELECT_H | |
23 | # include <sys/select.h> | |
24 | #endif | |
25 | ||
490c3105 | 26 | #include <stdlib.h> |
6d39a5c4 | 27 | #include <errno.h> |
28 | #include "bsd-poll.h" | |
29 | ||
30 | /* | |
31 | * A minimal implementation of poll(2), built on top of select(2). | |
32 | * | |
33 | * Only supports POLLIN and POLLOUT flags in pfd.events, and POLLIN, POLLOUT | |
34 | * and POLLERR flags in revents. | |
35 | * | |
36 | * Supports pfd.fd = -1 meaning "unused" although it's not standard. | |
37 | */ | |
38 | ||
39 | int | |
40 | poll(struct pollfd *fds, nfds_t nfds, int timeout) | |
41 | { | |
42 | nfds_t i; | |
43 | int saved_errno, ret, fd, maxfd = 0; | |
44 | fd_set *readfds = NULL, *writefds = NULL, *exceptfds = NULL; | |
45 | size_t nmemb; | |
46 | struct timeval tv, *tvp = NULL; | |
47 | ||
48 | for (i = 0; i < nfds; i++) { | |
49 | if (fd >= FD_SETSIZE) { | |
50 | errno = EINVAL; | |
51 | return -1; | |
52 | } | |
53 | maxfd = MAX(maxfd, fds[i].fd); | |
54 | } | |
55 | ||
56 | nmemb = howmany(maxfd + 1 , NFDBITS); | |
57 | if ((readfds = calloc(nmemb, sizeof(fd_mask))) == NULL || | |
58 | (writefds = calloc(nmemb, sizeof(fd_mask))) == NULL || | |
59 | (exceptfds = calloc(nmemb, sizeof(fd_mask))) == NULL) { | |
60 | saved_errno = ENOMEM; | |
61 | ret = -1; | |
62 | goto out; | |
63 | } | |
64 | ||
65 | /* populate event bit vectors for the events we're interested in */ | |
66 | for (i = 0; i < nfds; i++) { | |
67 | fd = fds[i].fd; | |
68 | if (fd == -1) | |
69 | continue; | |
70 | if (fds[i].events & POLLIN) { | |
71 | FD_SET(fd, readfds); | |
72 | FD_SET(fd, exceptfds); | |
73 | } | |
74 | if (fds[i].events & POLLOUT) { | |
75 | FD_SET(fd, writefds); | |
76 | FD_SET(fd, exceptfds); | |
77 | } | |
78 | } | |
79 | ||
80 | /* poll timeout is msec, select is timeval (sec + usec) */ | |
81 | if (timeout >= 0) { | |
82 | tv.tv_sec = timeout / 1000; | |
83 | tv.tv_usec = (timeout % 1000) * 1000; | |
84 | tvp = &tv; | |
85 | } | |
86 | ||
87 | ret = select(maxfd + 1, readfds, writefds, exceptfds, tvp); | |
88 | saved_errno = errno; | |
89 | ||
90 | /* scan through select results and set poll() flags */ | |
91 | for (i = 0; i < nfds; i++) { | |
92 | fd = fds[i].fd; | |
93 | fds[i].revents = 0; | |
94 | if (fd == -1) | |
95 | continue; | |
96 | if (FD_ISSET(fd, readfds)) { | |
97 | fds[i].revents |= POLLIN; | |
98 | } | |
99 | if (FD_ISSET(fd, writefds)) { | |
100 | fds[i].revents |= POLLOUT; | |
101 | } | |
102 | if (FD_ISSET(fd, exceptfds)) { | |
103 | fds[i].revents |= POLLERR; | |
104 | } | |
105 | } | |
106 | ||
107 | out: | |
108 | if (readfds != NULL) | |
109 | free(readfds); | |
110 | if (writefds != NULL) | |
111 | free(writefds); | |
112 | if (exceptfds != NULL) | |
113 | free(exceptfds); | |
114 | if (ret == -1) | |
115 | errno = saved_errno; | |
116 | return ret; | |
117 | } | |
118 | #endif |