/bitcoin/depends/work/build/x86_64-pc-linux-gnu/libevent/2.1.12-stable-7656baec08e/poll.c
Line | Count | Source |
1 | | /* $OpenBSD: poll.c,v 1.2 2002/06/25 15:50:15 mickey Exp $ */ |
2 | | |
3 | | /* |
4 | | * Copyright 2000-2007 Niels Provos <provos@citi.umich.edu> |
5 | | * Copyright 2007-2012 Niels Provos and Nick Mathewson |
6 | | * |
7 | | * Redistribution and use in source and binary forms, with or without |
8 | | * modification, are permitted provided that the following conditions |
9 | | * are met: |
10 | | * 1. Redistributions of source code must retain the above copyright |
11 | | * notice, this list of conditions and the following disclaimer. |
12 | | * 2. Redistributions in binary form must reproduce the above copyright |
13 | | * notice, this list of conditions and the following disclaimer in the |
14 | | * documentation and/or other materials provided with the distribution. |
15 | | * 3. The name of the author may not be used to endorse or promote products |
16 | | * derived from this software without specific prior written permission. |
17 | | * |
18 | | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
19 | | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
20 | | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
21 | | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
22 | | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
23 | | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
24 | | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
25 | | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
26 | | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
27 | | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | | */ |
29 | | #include "event2/event-config.h" |
30 | | #include "evconfig-private.h" |
31 | | |
32 | | #ifdef EVENT__HAVE_POLL |
33 | | |
34 | | #include <sys/types.h> |
35 | | #ifdef EVENT__HAVE_SYS_TIME_H |
36 | | #include <sys/time.h> |
37 | | #endif |
38 | | #include <sys/queue.h> |
39 | | #include <poll.h> |
40 | | #include <signal.h> |
41 | | #include <limits.h> |
42 | | #include <stdio.h> |
43 | | #include <stdlib.h> |
44 | | #include <string.h> |
45 | | #include <unistd.h> |
46 | | #include <errno.h> |
47 | | |
48 | | #include "event-internal.h" |
49 | | #include "evsignal-internal.h" |
50 | | #include "log-internal.h" |
51 | | #include "evmap-internal.h" |
52 | | #include "event2/thread.h" |
53 | | #include "evthread-internal.h" |
54 | | #include "time-internal.h" |
55 | | |
56 | | /* Since Linux 2.6.17, poll is able to report about peer half-closed connection |
57 | | using special POLLRDHUP flag on a read event. |
58 | | */ |
59 | | #if !defined(POLLRDHUP) |
60 | | #define POLLRDHUP 0 |
61 | | #define EARLY_CLOSE_IF_HAVE_RDHUP 0 |
62 | | #else |
63 | | #define EARLY_CLOSE_IF_HAVE_RDHUP EV_FEATURE_EARLY_CLOSE |
64 | | #endif |
65 | | |
66 | | |
67 | | struct pollidx { |
68 | | int idxplus1; |
69 | | }; |
70 | | |
71 | | struct pollop { |
72 | | int event_count; /* Highest number alloc */ |
73 | | int nfds; /* Highest number used */ |
74 | | int realloc_copy; /* True iff we must realloc |
75 | | * event_set_copy */ |
76 | | struct pollfd *event_set; |
77 | | struct pollfd *event_set_copy; |
78 | | }; |
79 | | |
80 | | static void *poll_init(struct event_base *); |
81 | | static int poll_add(struct event_base *, int, short old, short events, void *idx); |
82 | | static int poll_del(struct event_base *, int, short old, short events, void *idx); |
83 | | static int poll_dispatch(struct event_base *, struct timeval *); |
84 | | static void poll_dealloc(struct event_base *); |
85 | | |
86 | | const struct eventop pollops = { |
87 | | "poll", |
88 | | poll_init, |
89 | | poll_add, |
90 | | poll_del, |
91 | | poll_dispatch, |
92 | | poll_dealloc, |
93 | | 1, /* need_reinit */ |
94 | | EV_FEATURE_FDS|EARLY_CLOSE_IF_HAVE_RDHUP, |
95 | | sizeof(struct pollidx), |
96 | | }; |
97 | | |
98 | | static void * |
99 | | poll_init(struct event_base *base) |
100 | 0 | { |
101 | 0 | struct pollop *pollop; |
102 | |
|
103 | 0 | if (!(pollop = mm_calloc(1, sizeof(struct pollop)))) Branch (103:6): [True: 0, False: 0]
|
104 | 0 | return (NULL); |
105 | | |
106 | 0 | evsig_init_(base); |
107 | |
|
108 | 0 | evutil_weakrand_seed_(&base->weakrand_seed, 0); |
109 | |
|
110 | 0 | return (pollop); |
111 | 0 | } |
112 | | |
113 | | #ifdef CHECK_INVARIANTS |
114 | | static void |
115 | | poll_check_ok(struct pollop *pop) |
116 | | { |
117 | | int i, idx; |
118 | | struct event *ev; |
119 | | |
120 | | for (i = 0; i < pop->fd_count; ++i) { |
121 | | idx = pop->idxplus1_by_fd[i]-1; |
122 | | if (idx < 0) |
123 | | continue; |
124 | | EVUTIL_ASSERT(pop->event_set[idx].fd == i); |
125 | | } |
126 | | for (i = 0; i < pop->nfds; ++i) { |
127 | | struct pollfd *pfd = &pop->event_set[i]; |
128 | | EVUTIL_ASSERT(pop->idxplus1_by_fd[pfd->fd] == i+1); |
129 | | } |
130 | | } |
131 | | #else |
132 | | #define poll_check_ok(pop) |
133 | | #endif |
134 | | |
135 | | static int |
136 | | poll_dispatch(struct event_base *base, struct timeval *tv) |
137 | 0 | { |
138 | 0 | int res, i, j, nfds; |
139 | 0 | long msec = -1; |
140 | 0 | struct pollop *pop = base->evbase; |
141 | 0 | struct pollfd *event_set; |
142 | |
|
143 | 0 | poll_check_ok(pop); |
144 | |
|
145 | 0 | nfds = pop->nfds; |
146 | |
|
147 | 0 | #ifndef EVENT__DISABLE_THREAD_SUPPORT |
148 | 0 | if (base->th_base_lock) { Branch (148:6): [True: 0, False: 0]
|
149 | | /* If we're using this backend in a multithreaded setting, |
150 | | * then we need to work on a copy of event_set, so that we can |
151 | | * let other threads modify the main event_set while we're |
152 | | * polling. If we're not multithreaded, then we'll skip the |
153 | | * copy step here to save memory and time. */ |
154 | 0 | if (pop->realloc_copy) { Branch (154:7): [True: 0, False: 0]
|
155 | 0 | struct pollfd *tmp = mm_realloc(pop->event_set_copy, |
156 | 0 | pop->event_count * sizeof(struct pollfd)); |
157 | 0 | if (tmp == NULL) { Branch (157:8): [True: 0, False: 0]
|
158 | 0 | event_warn("realloc"); |
159 | 0 | return -1; |
160 | 0 | } |
161 | 0 | pop->event_set_copy = tmp; |
162 | 0 | pop->realloc_copy = 0; |
163 | 0 | } |
164 | 0 | memcpy(pop->event_set_copy, pop->event_set, |
165 | 0 | sizeof(struct pollfd)*nfds); |
166 | 0 | event_set = pop->event_set_copy; |
167 | 0 | } else { |
168 | 0 | event_set = pop->event_set; |
169 | 0 | } |
170 | | #else |
171 | | event_set = pop->event_set; |
172 | | #endif |
173 | | |
174 | 0 | if (tv != NULL) { Branch (174:6): [True: 0, False: 0]
|
175 | 0 | msec = evutil_tv_to_msec_(tv); |
176 | 0 | if (msec < 0 || msec > INT_MAX) Branch (176:7): [True: 0, False: 0]
Branch (176:19): [True: 0, False: 0]
|
177 | 0 | msec = INT_MAX; |
178 | 0 | } |
179 | |
|
180 | 0 | EVBASE_RELEASE_LOCK(base, th_base_lock); |
181 | |
|
182 | 0 | res = poll(event_set, nfds, msec); |
183 | |
|
184 | 0 | EVBASE_ACQUIRE_LOCK(base, th_base_lock); |
185 | |
|
186 | 0 | if (res == -1) { Branch (186:6): [True: 0, False: 0]
|
187 | 0 | if (errno != EINTR) { Branch (187:7): [True: 0, False: 0]
|
188 | 0 | event_warn("poll"); |
189 | 0 | return (-1); |
190 | 0 | } |
191 | | |
192 | 0 | return (0); |
193 | 0 | } |
194 | | |
195 | 0 | event_debug(("%s: poll reports %d", __func__, res)); |
196 | |
|
197 | 0 | if (res == 0 || nfds == 0) Branch (197:6): [True: 0, False: 0]
Branch (197:18): [True: 0, False: 0]
|
198 | 0 | return (0); |
199 | | |
200 | 0 | i = evutil_weakrand_range_(&base->weakrand_seed, nfds); |
201 | 0 | for (j = 0; j < nfds; j++) { Branch (201:14): [True: 0, False: 0]
|
202 | 0 | int what; |
203 | 0 | if (++i == nfds) Branch (203:7): [True: 0, False: 0]
|
204 | 0 | i = 0; |
205 | 0 | what = event_set[i].revents; |
206 | 0 | if (!what) Branch (206:7): [True: 0, False: 0]
|
207 | 0 | continue; |
208 | | |
209 | 0 | res = 0; |
210 | | |
211 | | /* If the file gets closed notify */ |
212 | 0 | if (what & (POLLHUP|POLLERR|POLLNVAL)) Branch (212:7): [True: 0, False: 0]
|
213 | 0 | what |= POLLIN|POLLOUT; |
214 | 0 | if (what & POLLIN) Branch (214:7): [True: 0, False: 0]
|
215 | 0 | res |= EV_READ; |
216 | 0 | if (what & POLLOUT) Branch (216:7): [True: 0, False: 0]
|
217 | 0 | res |= EV_WRITE; |
218 | 0 | if (what & POLLRDHUP) Branch (218:7): [True: 0, False: 0]
|
219 | 0 | res |= EV_CLOSED; |
220 | 0 | if (res == 0) Branch (220:7): [True: 0, False: 0]
|
221 | 0 | continue; |
222 | | |
223 | 0 | evmap_io_active_(base, event_set[i].fd, res); |
224 | 0 | } |
225 | |
|
226 | 0 | return (0); |
227 | 0 | } |
228 | | |
229 | | static int |
230 | | poll_add(struct event_base *base, int fd, short old, short events, void *idx_) |
231 | 0 | { |
232 | 0 | struct pollop *pop = base->evbase; |
233 | 0 | struct pollfd *pfd = NULL; |
234 | 0 | struct pollidx *idx = idx_; |
235 | 0 | int i; |
236 | |
|
237 | 0 | EVUTIL_ASSERT((events & EV_SIGNAL) == 0); |
238 | 0 | if (!(events & (EV_READ|EV_WRITE|EV_CLOSED))) Branch (238:6): [True: 0, False: 0]
|
239 | 0 | return (0); |
240 | | |
241 | 0 | poll_check_ok(pop); |
242 | 0 | if (pop->nfds + 1 >= pop->event_count) { Branch (242:6): [True: 0, False: 0]
|
243 | 0 | struct pollfd *tmp_event_set; |
244 | 0 | int tmp_event_count; |
245 | |
|
246 | 0 | if (pop->event_count < 32) Branch (246:7): [True: 0, False: 0]
|
247 | 0 | tmp_event_count = 32; |
248 | 0 | else |
249 | 0 | tmp_event_count = pop->event_count * 2; |
250 | | |
251 | | /* We need more file descriptors */ |
252 | 0 | tmp_event_set = mm_realloc(pop->event_set, |
253 | 0 | tmp_event_count * sizeof(struct pollfd)); |
254 | 0 | if (tmp_event_set == NULL) { Branch (254:7): [True: 0, False: 0]
|
255 | 0 | event_warn("realloc"); |
256 | 0 | return (-1); |
257 | 0 | } |
258 | 0 | pop->event_set = tmp_event_set; |
259 | |
|
260 | 0 | pop->event_count = tmp_event_count; |
261 | 0 | pop->realloc_copy = 1; |
262 | 0 | } |
263 | | |
264 | 0 | i = idx->idxplus1 - 1; |
265 | |
|
266 | 0 | if (i >= 0) { Branch (266:6): [True: 0, False: 0]
|
267 | 0 | pfd = &pop->event_set[i]; |
268 | 0 | } else { |
269 | 0 | i = pop->nfds++; |
270 | 0 | pfd = &pop->event_set[i]; |
271 | 0 | pfd->events = 0; |
272 | 0 | pfd->fd = fd; |
273 | 0 | idx->idxplus1 = i + 1; |
274 | 0 | } |
275 | |
|
276 | 0 | pfd->revents = 0; |
277 | 0 | if (events & EV_WRITE) Branch (277:6): [True: 0, False: 0]
|
278 | 0 | pfd->events |= POLLOUT; |
279 | 0 | if (events & EV_READ) Branch (279:6): [True: 0, False: 0]
|
280 | 0 | pfd->events |= POLLIN; |
281 | 0 | if (events & EV_CLOSED) Branch (281:6): [True: 0, False: 0]
|
282 | 0 | pfd->events |= POLLRDHUP; |
283 | 0 | poll_check_ok(pop); |
284 | |
|
285 | 0 | return (0); |
286 | 0 | } |
287 | | |
288 | | /* |
289 | | * Nothing to be done here. |
290 | | */ |
291 | | |
292 | | static int |
293 | | poll_del(struct event_base *base, int fd, short old, short events, void *idx_) |
294 | 0 | { |
295 | 0 | struct pollop *pop = base->evbase; |
296 | 0 | struct pollfd *pfd = NULL; |
297 | 0 | struct pollidx *idx = idx_; |
298 | 0 | int i; |
299 | |
|
300 | 0 | EVUTIL_ASSERT((events & EV_SIGNAL) == 0); |
301 | 0 | if (!(events & (EV_READ|EV_WRITE|EV_CLOSED))) Branch (301:6): [True: 0, False: 0]
|
302 | 0 | return (0); |
303 | | |
304 | 0 | poll_check_ok(pop); |
305 | 0 | i = idx->idxplus1 - 1; |
306 | 0 | if (i < 0) Branch (306:6): [True: 0, False: 0]
|
307 | 0 | return (-1); |
308 | | |
309 | | /* Do we still want to read or write? */ |
310 | 0 | pfd = &pop->event_set[i]; |
311 | 0 | if (events & EV_READ) Branch (311:6): [True: 0, False: 0]
|
312 | 0 | pfd->events &= ~POLLIN; |
313 | 0 | if (events & EV_WRITE) Branch (313:6): [True: 0, False: 0]
|
314 | 0 | pfd->events &= ~POLLOUT; |
315 | 0 | if (events & EV_CLOSED) Branch (315:6): [True: 0, False: 0]
|
316 | 0 | pfd->events &= ~POLLRDHUP; |
317 | 0 | poll_check_ok(pop); |
318 | 0 | if (pfd->events) Branch (318:6): [True: 0, False: 0]
|
319 | | /* Another event cares about that fd. */ |
320 | 0 | return (0); |
321 | | |
322 | | /* Okay, so we aren't interested in that fd anymore. */ |
323 | 0 | idx->idxplus1 = 0; |
324 | |
|
325 | 0 | --pop->nfds; |
326 | 0 | if (i != pop->nfds) { Branch (326:6): [True: 0, False: 0]
|
327 | | /* |
328 | | * Shift the last pollfd down into the now-unoccupied |
329 | | * position. |
330 | | */ |
331 | 0 | memcpy(&pop->event_set[i], &pop->event_set[pop->nfds], |
332 | 0 | sizeof(struct pollfd)); |
333 | 0 | idx = evmap_io_get_fdinfo_(&base->io, pop->event_set[i].fd); |
334 | 0 | EVUTIL_ASSERT(idx); |
335 | 0 | EVUTIL_ASSERT(idx->idxplus1 == pop->nfds + 1); |
336 | 0 | idx->idxplus1 = i + 1; |
337 | 0 | } |
338 | | |
339 | 0 | poll_check_ok(pop); |
340 | 0 | return (0); |
341 | 0 | } |
342 | | |
343 | | static void |
344 | | poll_dealloc(struct event_base *base) |
345 | 0 | { |
346 | 0 | struct pollop *pop = base->evbase; |
347 | |
|
348 | 0 | evsig_dealloc_(base); |
349 | 0 | if (pop->event_set) Branch (349:6): [True: 0, False: 0]
|
350 | 0 | mm_free(pop->event_set); |
351 | 0 | if (pop->event_set_copy) Branch (351:6): [True: 0, False: 0]
|
352 | 0 | mm_free(pop->event_set_copy); |
353 | |
|
354 | 0 | memset(pop, 0, sizeof(struct pollop)); |
355 | 0 | mm_free(pop); |
356 | 0 | } |
357 | | |
358 | | #endif /* EVENT__HAVE_POLL */ |