1b3d1c6a8SNiels Provos /* $OpenBSD: poll.c,v 1.2 2002/06/25 15:50:15 mickey Exp $ */
2b3d1c6a8SNiels Provos
3b3d1c6a8SNiels Provos /*
4b85b710cSNick Mathewson * Copyright 2000-2007 Niels Provos <[email protected]>
5e49e2891SNick Mathewson * Copyright 2007-2012 Niels Provos and Nick Mathewson
6b3d1c6a8SNiels Provos *
7b3d1c6a8SNiels Provos * Redistribution and use in source and binary forms, with or without
8b3d1c6a8SNiels Provos * modification, are permitted provided that the following conditions
9b3d1c6a8SNiels Provos * are met:
10b3d1c6a8SNiels Provos * 1. Redistributions of source code must retain the above copyright
11b3d1c6a8SNiels Provos * notice, this list of conditions and the following disclaimer.
12b3d1c6a8SNiels Provos * 2. Redistributions in binary form must reproduce the above copyright
13b3d1c6a8SNiels Provos * notice, this list of conditions and the following disclaimer in the
14b3d1c6a8SNiels Provos * documentation and/or other materials provided with the distribution.
15c3f496c7SNiels Provos * 3. The name of the author may not be used to endorse or promote products
16b3d1c6a8SNiels Provos * derived from this software without specific prior written permission.
17b3d1c6a8SNiels Provos *
18b3d1c6a8SNiels Provos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19b3d1c6a8SNiels Provos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20b3d1c6a8SNiels Provos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21b3d1c6a8SNiels Provos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22b3d1c6a8SNiels Provos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23b3d1c6a8SNiels Provos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24b3d1c6a8SNiels Provos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25b3d1c6a8SNiels Provos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26b3d1c6a8SNiels Provos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27b3d1c6a8SNiels Provos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28b3d1c6a8SNiels Provos */
29ec347b92SNick Mathewson #include "event2/event-config.h"
300915ca0aSKevin Bowling #include "evconfig-private.h"
31b3d1c6a8SNiels Provos
3268120d9bSNick Mathewson #ifdef EVENT__HAVE_POLL
3376d4c929SRoss Lagerwall
34b3d1c6a8SNiels Provos #include <sys/types.h>
3568120d9bSNick Mathewson #ifdef EVENT__HAVE_SYS_TIME_H
36b3d1c6a8SNiels Provos #include <sys/time.h>
37b3d1c6a8SNiels Provos #endif
38b3d1c6a8SNiels Provos #include <sys/queue.h>
39b3d1c6a8SNiels Provos #include <poll.h>
40b3d1c6a8SNiels Provos #include <signal.h>
41850c3ff2SChristopher Davis #include <limits.h>
42b3d1c6a8SNiels Provos #include <stdio.h>
43b3d1c6a8SNiels Provos #include <stdlib.h>
44b3d1c6a8SNiels Provos #include <string.h>
45b3d1c6a8SNiels Provos #include <unistd.h>
46b3d1c6a8SNiels Provos #include <errno.h>
47b3d1c6a8SNiels Provos
488773c4c9SNiels Provos #include "event-internal.h"
49169321c9SNick Mathewson #include "evsignal-internal.h"
50169321c9SNick Mathewson #include "log-internal.h"
51169321c9SNick Mathewson #include "evmap-internal.h"
526b22e74aSNick Mathewson #include "event2/thread.h"
536b22e74aSNick Mathewson #include "evthread-internal.h"
5471bca50fSNick Mathewson #include "time-internal.h"
55b3d1c6a8SNiels Provos
562530e7c6SAzat Khuzhin /* Since Linux 2.6.17, poll is able to report about peer half-closed connection
572530e7c6SAzat Khuzhin using special POLLRDHUP flag on a read event.
582530e7c6SAzat Khuzhin */
592530e7c6SAzat Khuzhin #if !defined(POLLRDHUP)
602530e7c6SAzat Khuzhin #define POLLRDHUP 0
612530e7c6SAzat Khuzhin #define EARLY_CLOSE_IF_HAVE_RDHUP 0
622530e7c6SAzat Khuzhin #else
632530e7c6SAzat Khuzhin #define EARLY_CLOSE_IF_HAVE_RDHUP EV_FEATURE_EARLY_CLOSE
642530e7c6SAzat Khuzhin #endif
652530e7c6SAzat Khuzhin
662530e7c6SAzat Khuzhin
67554e1493SNick Mathewson struct pollidx {
68554e1493SNick Mathewson int idxplus1;
69554e1493SNick Mathewson };
70554e1493SNick Mathewson
71b3d1c6a8SNiels Provos struct pollop {
72b3d1c6a8SNiels Provos int event_count; /* Highest number alloc */
736b22e74aSNick Mathewson int nfds; /* Highest number used */
746b22e74aSNick Mathewson int realloc_copy; /* True iff we must realloc
756b22e74aSNick Mathewson * event_set_copy */
76b3d1c6a8SNiels Provos struct pollfd *event_set;
776b22e74aSNick Mathewson struct pollfd *event_set_copy;
783ba224dbSNiels Provos };
79b3d1c6a8SNiels Provos
80ca42671aSNiels Provos static void *poll_init(struct event_base *);
81946b5841SNick Mathewson static int poll_add(struct event_base *, int, short old, short events, void *idx);
82946b5841SNick Mathewson static int poll_del(struct event_base *, int, short old, short events, void *idx);
8302b2b4d1SNiels Provos static int poll_dispatch(struct event_base *, struct timeval *);
8402b2b4d1SNiels Provos static void poll_dealloc(struct event_base *);
85b3d1c6a8SNiels Provos
867517ef2aSNiels Provos const struct eventop pollops = {
87b3d1c6a8SNiels Provos "poll",
88b3d1c6a8SNiels Provos poll_init,
89b3d1c6a8SNiels Provos poll_add,
90b3d1c6a8SNiels Provos poll_del,
912e8051f5SNiels Provos poll_dispatch,
92ce4ee418SNick Mathewson poll_dealloc,
93*1c9cc07bSAzat Khuzhin 1, /* need_reinit */
942530e7c6SAzat Khuzhin EV_FEATURE_FDS|EARLY_CLOSE_IF_HAVE_RDHUP,
95554e1493SNick Mathewson sizeof(struct pollidx),
96b3d1c6a8SNiels Provos };
97b3d1c6a8SNiels Provos
98ca42671aSNiels Provos static void *
poll_init(struct event_base * base)9941b7cbc3SNiels Provos poll_init(struct event_base *base)
100b3d1c6a8SNiels Provos {
1013ba224dbSNiels Provos struct pollop *pollop;
1023ba224dbSNiels Provos
10349868b61SNick Mathewson if (!(pollop = mm_calloc(1, sizeof(struct pollop))))
1043ba224dbSNiels Provos return (NULL);
105b3d1c6a8SNiels Provos
1068ac3c4c2SNick Mathewson evsig_init_(base);
107b3d1c6a8SNiels Provos
1083aa44159SNick Mathewson evutil_weakrand_seed_(&base->weakrand_seed, 0);
1093aa44159SNick Mathewson
1103ba224dbSNiels Provos return (pollop);
111b3d1c6a8SNiels Provos }
112b3d1c6a8SNiels Provos
113c15db034SNiels Provos #ifdef CHECK_INVARIANTS
114c15db034SNiels Provos static void
poll_check_ok(struct pollop * pop)115c15db034SNiels Provos poll_check_ok(struct pollop *pop)
116c15db034SNiels Provos {
117c15db034SNiels Provos int i, idx;
118c15db034SNiels Provos struct event *ev;
119c15db034SNiels Provos
120c15db034SNiels Provos for (i = 0; i < pop->fd_count; ++i) {
121c15db034SNiels Provos idx = pop->idxplus1_by_fd[i]-1;
122c15db034SNiels Provos if (idx < 0)
123c15db034SNiels Provos continue;
1242e36dbe1SNick Mathewson EVUTIL_ASSERT(pop->event_set[idx].fd == i);
125c15db034SNiels Provos }
126c15db034SNiels Provos for (i = 0; i < pop->nfds; ++i) {
127c15db034SNiels Provos struct pollfd *pfd = &pop->event_set[i];
1282e36dbe1SNick Mathewson EVUTIL_ASSERT(pop->idxplus1_by_fd[pfd->fd] == i+1);
129c15db034SNiels Provos }
130c15db034SNiels Provos }
131c15db034SNiels Provos #else
132c15db034SNiels Provos #define poll_check_ok(pop)
133c15db034SNiels Provos #endif
134c15db034SNiels Provos
135ca42671aSNiels Provos static int
poll_dispatch(struct event_base * base,struct timeval * tv)13602b2b4d1SNiels Provos poll_dispatch(struct event_base *base, struct timeval *tv)
137b3d1c6a8SNiels Provos {
138850c3ff2SChristopher Davis int res, i, j, nfds;
139850c3ff2SChristopher Davis long msec = -1;
14002b2b4d1SNiels Provos struct pollop *pop = base->evbase;
1416b22e74aSNick Mathewson struct pollfd *event_set;
142b3d1c6a8SNiels Provos
1439938aaf5SNiels Provos poll_check_ok(pop);
1443ad6b47eSNiels Provos
1456b22e74aSNick Mathewson nfds = pop->nfds;
1466b22e74aSNick Mathewson
14768120d9bSNick Mathewson #ifndef EVENT__DISABLE_THREAD_SUPPORT
1486b22e74aSNick Mathewson if (base->th_base_lock) {
1496b22e74aSNick Mathewson /* If we're using this backend in a multithreaded setting,
1506b22e74aSNick Mathewson * then we need to work on a copy of event_set, so that we can
1516b22e74aSNick Mathewson * let other threads modify the main event_set while we're
1526b22e74aSNick Mathewson * polling. If we're not multithreaded, then we'll skip the
1536b22e74aSNick Mathewson * copy step here to save memory and time. */
1546b22e74aSNick Mathewson if (pop->realloc_copy) {
1556b22e74aSNick Mathewson struct pollfd *tmp = mm_realloc(pop->event_set_copy,
1566b22e74aSNick Mathewson pop->event_count * sizeof(struct pollfd));
1576b22e74aSNick Mathewson if (tmp == NULL) {
1586b22e74aSNick Mathewson event_warn("realloc");
1596b22e74aSNick Mathewson return -1;
1606b22e74aSNick Mathewson }
1616b22e74aSNick Mathewson pop->event_set_copy = tmp;
1626b22e74aSNick Mathewson pop->realloc_copy = 0;
1636b22e74aSNick Mathewson }
1646b22e74aSNick Mathewson memcpy(pop->event_set_copy, pop->event_set,
1656b22e74aSNick Mathewson sizeof(struct pollfd)*nfds);
1666b22e74aSNick Mathewson event_set = pop->event_set_copy;
1676b22e74aSNick Mathewson } else {
1686b22e74aSNick Mathewson event_set = pop->event_set;
1696b22e74aSNick Mathewson }
170767eb70fSNick Mathewson #else
171767eb70fSNick Mathewson event_set = pop->event_set;
172767eb70fSNick Mathewson #endif
1736b22e74aSNick Mathewson
174850c3ff2SChristopher Davis if (tv != NULL) {
1758ac3c4c2SNick Mathewson msec = evutil_tv_to_msec_(tv);
176850c3ff2SChristopher Davis if (msec < 0 || msec > INT_MAX)
177850c3ff2SChristopher Davis msec = INT_MAX;
178850c3ff2SChristopher Davis }
1793ad6b47eSNiels Provos
18076cd2b70SNick Mathewson EVBASE_RELEASE_LOCK(base, th_base_lock);
1816b22e74aSNick Mathewson
1826b22e74aSNick Mathewson res = poll(event_set, nfds, msec);
1836b22e74aSNick Mathewson
18476cd2b70SNick Mathewson EVBASE_ACQUIRE_LOCK(base, th_base_lock);
185b3d1c6a8SNiels Provos
186b3d1c6a8SNiels Provos if (res == -1) {
187b3d1c6a8SNiels Provos if (errno != EINTR) {
188fbdaf3abSNiels Provos event_warn("poll");
189b3d1c6a8SNiels Provos return (-1);
190b3d1c6a8SNiels Provos }
191b3d1c6a8SNiels Provos
192b3d1c6a8SNiels Provos return (0);
19341b7cbc3SNiels Provos }
194b3d1c6a8SNiels Provos
195fbdaf3abSNiels Provos event_debug(("%s: poll reports %d", __func__, res));
196b3d1c6a8SNiels Provos
197cdaca02cSNick Mathewson if (res == 0 || nfds == 0)
198b3d1c6a8SNiels Provos return (0);
199b3d1c6a8SNiels Provos
200e86af4b7SNicholas Marriott i = evutil_weakrand_range_(&base->weakrand_seed, nfds);
201cdaca02cSNick Mathewson for (j = 0; j < nfds; j++) {
202cdaca02cSNick Mathewson int what;
203cdaca02cSNick Mathewson if (++i == nfds)
204cdaca02cSNick Mathewson i = 0;
2056b22e74aSNick Mathewson what = event_set[i].revents;
206c15db034SNiels Provos if (!what)
207c15db034SNiels Provos continue;
208e1cd86d7SNiels Provos
209b3d1c6a8SNiels Provos res = 0;
210e1cd86d7SNiels Provos
211e506eaf7SNiels Provos /* If the file gets closed notify */
212675974ceSTim Hentenaar if (what & (POLLHUP|POLLERR|POLLNVAL))
213e1cd86d7SNiels Provos what |= POLLIN|POLLOUT;
21402b2b4d1SNiels Provos if (what & POLLIN)
215f08bf532SNiels Provos res |= EV_READ;
21602b2b4d1SNiels Provos if (what & POLLOUT)
217f08bf532SNiels Provos res |= EV_WRITE;
2182530e7c6SAzat Khuzhin if (what & POLLRDHUP)
2192530e7c6SAzat Khuzhin res |= EV_CLOSED;
220b3d1c6a8SNiels Provos if (res == 0)
221b3d1c6a8SNiels Provos continue;
222b3d1c6a8SNiels Provos
2238ac3c4c2SNick Mathewson evmap_io_active_(base, event_set[i].fd, res);
224b3d1c6a8SNiels Provos }
225b3d1c6a8SNiels Provos
226b3d1c6a8SNiels Provos return (0);
227b3d1c6a8SNiels Provos }
228b3d1c6a8SNiels Provos
229ca42671aSNiels Provos static int
poll_add(struct event_base * base,int fd,short old,short events,void * idx_)230946b5841SNick Mathewson poll_add(struct event_base *base, int fd, short old, short events, void *idx_)
231b3d1c6a8SNiels Provos {
23202b2b4d1SNiels Provos struct pollop *pop = base->evbase;
233c15db034SNiels Provos struct pollfd *pfd = NULL;
234946b5841SNick Mathewson struct pollidx *idx = idx_;
235c15db034SNiels Provos int i;
236b3d1c6a8SNiels Provos
2372e36dbe1SNick Mathewson EVUTIL_ASSERT((events & EV_SIGNAL) == 0);
2382530e7c6SAzat Khuzhin if (!(events & (EV_READ|EV_WRITE|EV_CLOSED)))
239c15db034SNiels Provos return (0);
240c15db034SNiels Provos
241c15db034SNiels Provos poll_check_ok(pop);
242c15db034SNiels Provos if (pop->nfds + 1 >= pop->event_count) {
243025b009bSNiels Provos struct pollfd *tmp_event_set;
244025b009bSNiels Provos int tmp_event_count;
245025b009bSNiels Provos
246c15db034SNiels Provos if (pop->event_count < 32)
247025b009bSNiels Provos tmp_event_count = 32;
248c15db034SNiels Provos else
249025b009bSNiels Provos tmp_event_count = pop->event_count * 2;
250c15db034SNiels Provos
251c15db034SNiels Provos /* We need more file descriptors */
25249868b61SNick Mathewson tmp_event_set = mm_realloc(pop->event_set,
253025b009bSNiels Provos tmp_event_count * sizeof(struct pollfd));
254025b009bSNiels Provos if (tmp_event_set == NULL) {
255c15db034SNiels Provos event_warn("realloc");
256c15db034SNiels Provos return (-1);
257c15db034SNiels Provos }
258025b009bSNiels Provos pop->event_set = tmp_event_set;
259025b009bSNiels Provos
260025b009bSNiels Provos pop->event_count = tmp_event_count;
2616b22e74aSNick Mathewson pop->realloc_copy = 1;
262c15db034SNiels Provos }
263c15db034SNiels Provos
264554e1493SNick Mathewson i = idx->idxplus1 - 1;
265554e1493SNick Mathewson
266c15db034SNiels Provos if (i >= 0) {
267c15db034SNiels Provos pfd = &pop->event_set[i];
268c15db034SNiels Provos } else {
269c15db034SNiels Provos i = pop->nfds++;
270c15db034SNiels Provos pfd = &pop->event_set[i];
271c15db034SNiels Provos pfd->events = 0;
27202b2b4d1SNiels Provos pfd->fd = fd;
273554e1493SNick Mathewson idx->idxplus1 = i + 1;
274c15db034SNiels Provos }
275c15db034SNiels Provos
276c15db034SNiels Provos pfd->revents = 0;
27702b2b4d1SNiels Provos if (events & EV_WRITE)
278c15db034SNiels Provos pfd->events |= POLLOUT;
27902b2b4d1SNiels Provos if (events & EV_READ)
280c15db034SNiels Provos pfd->events |= POLLIN;
2812530e7c6SAzat Khuzhin if (events & EV_CLOSED)
2822530e7c6SAzat Khuzhin pfd->events |= POLLRDHUP;
283c15db034SNiels Provos poll_check_ok(pop);
284b3d1c6a8SNiels Provos
285b3d1c6a8SNiels Provos return (0);
286b3d1c6a8SNiels Provos }
287b3d1c6a8SNiels Provos
288b3d1c6a8SNiels Provos /*
289b3d1c6a8SNiels Provos * Nothing to be done here.
290b3d1c6a8SNiels Provos */
291b3d1c6a8SNiels Provos
292ca42671aSNiels Provos static int
poll_del(struct event_base * base,int fd,short old,short events,void * idx_)293946b5841SNick Mathewson poll_del(struct event_base *base, int fd, short old, short events, void *idx_)
294b3d1c6a8SNiels Provos {
29502b2b4d1SNiels Provos struct pollop *pop = base->evbase;
296c15db034SNiels Provos struct pollfd *pfd = NULL;
297946b5841SNick Mathewson struct pollidx *idx = idx_;
298c15db034SNiels Provos int i;
299b3d1c6a8SNiels Provos
3002e36dbe1SNick Mathewson EVUTIL_ASSERT((events & EV_SIGNAL) == 0);
3012530e7c6SAzat Khuzhin if (!(events & (EV_READ|EV_WRITE|EV_CLOSED)))
302b3d1c6a8SNiels Provos return (0);
303b3d1c6a8SNiels Provos
304c15db034SNiels Provos poll_check_ok(pop);
305554e1493SNick Mathewson i = idx->idxplus1 - 1;
306c15db034SNiels Provos if (i < 0)
307c15db034SNiels Provos return (-1);
308c15db034SNiels Provos
309c15db034SNiels Provos /* Do we still want to read or write? */
310c15db034SNiels Provos pfd = &pop->event_set[i];
31102b2b4d1SNiels Provos if (events & EV_READ)
312c15db034SNiels Provos pfd->events &= ~POLLIN;
31302b2b4d1SNiels Provos if (events & EV_WRITE)
314c15db034SNiels Provos pfd->events &= ~POLLOUT;
3152530e7c6SAzat Khuzhin if (events & EV_CLOSED)
3162530e7c6SAzat Khuzhin pfd->events &= ~POLLRDHUP;
317c15db034SNiels Provos poll_check_ok(pop);
318c15db034SNiels Provos if (pfd->events)
319c15db034SNiels Provos /* Another event cares about that fd. */
320c15db034SNiels Provos return (0);
321c15db034SNiels Provos
322c15db034SNiels Provos /* Okay, so we aren't interested in that fd anymore. */
323554e1493SNick Mathewson idx->idxplus1 = 0;
324c15db034SNiels Provos
325c15db034SNiels Provos --pop->nfds;
326c15db034SNiels Provos if (i != pop->nfds) {
327c15db034SNiels Provos /*
328c15db034SNiels Provos * Shift the last pollfd down into the now-unoccupied
329c15db034SNiels Provos * position.
330c15db034SNiels Provos */
331c15db034SNiels Provos memcpy(&pop->event_set[i], &pop->event_set[pop->nfds],
332c15db034SNiels Provos sizeof(struct pollfd));
3338ac3c4c2SNick Mathewson idx = evmap_io_get_fdinfo_(&base->io, pop->event_set[i].fd);
3342e36dbe1SNick Mathewson EVUTIL_ASSERT(idx);
3352e36dbe1SNick Mathewson EVUTIL_ASSERT(idx->idxplus1 == pop->nfds + 1);
336554e1493SNick Mathewson idx->idxplus1 = i + 1;
337c15db034SNiels Provos }
338c15db034SNiels Provos
339c15db034SNiels Provos poll_check_ok(pop);
340c15db034SNiels Provos return (0);
341b3d1c6a8SNiels Provos }
3422e8051f5SNiels Provos
343ca42671aSNiels Provos static void
poll_dealloc(struct event_base * base)34402b2b4d1SNiels Provos poll_dealloc(struct event_base *base)
3452e8051f5SNiels Provos {
34602b2b4d1SNiels Provos struct pollop *pop = base->evbase;
3472e8051f5SNiels Provos
3488ac3c4c2SNick Mathewson evsig_dealloc_(base);
3492e8051f5SNiels Provos if (pop->event_set)
35049868b61SNick Mathewson mm_free(pop->event_set);
3516b22e74aSNick Mathewson if (pop->event_set_copy)
3526b22e74aSNick Mathewson mm_free(pop->event_set_copy);
3532e8051f5SNiels Provos
3542e8051f5SNiels Provos memset(pop, 0, sizeof(struct pollop));
35549868b61SNick Mathewson mm_free(pop);
3562e8051f5SNiels Provos }
35776d4c929SRoss Lagerwall
35868120d9bSNick Mathewson #endif /* EVENT__HAVE_POLL */
359