Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright 2000-2009 Niels Provos <provos@citi.umich.edu> |
michael@0 | 3 | * Copyright 2009-2012 Niels Provos and Nick Mathewson |
michael@0 | 4 | * |
michael@0 | 5 | * Redistribution and use in source and binary forms, with or without |
michael@0 | 6 | * modification, are permitted provided that the following conditions |
michael@0 | 7 | * are met: |
michael@0 | 8 | * 1. Redistributions of source code must retain the above copyright |
michael@0 | 9 | * notice, this list of conditions and the following disclaimer. |
michael@0 | 10 | * 2. Redistributions in binary form must reproduce the above copyright |
michael@0 | 11 | * notice, this list of conditions and the following disclaimer in the |
michael@0 | 12 | * documentation and/or other materials provided with the distribution. |
michael@0 | 13 | * 3. The name of the author may not be used to endorse or promote products |
michael@0 | 14 | * derived from this software without specific prior written permission. |
michael@0 | 15 | * |
michael@0 | 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
michael@0 | 17 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
michael@0 | 18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
michael@0 | 19 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
michael@0 | 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
michael@0 | 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 22 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 23 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
michael@0 | 25 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 26 | */ |
michael@0 | 27 | #include "event2/event-config.h" |
michael@0 | 28 | |
michael@0 | 29 | #include <sys/types.h> |
michael@0 | 30 | #include <sys/resource.h> |
michael@0 | 31 | #ifdef _EVENT_HAVE_SYS_TIME_H |
michael@0 | 32 | #include <sys/time.h> |
michael@0 | 33 | #endif |
michael@0 | 34 | #include <sys/queue.h> |
michael@0 | 35 | #include <sys/devpoll.h> |
michael@0 | 36 | #include <signal.h> |
michael@0 | 37 | #include <stdio.h> |
michael@0 | 38 | #include <stdlib.h> |
michael@0 | 39 | #include <string.h> |
michael@0 | 40 | #include <unistd.h> |
michael@0 | 41 | #include <fcntl.h> |
michael@0 | 42 | #include <errno.h> |
michael@0 | 43 | |
michael@0 | 44 | #include "event2/event.h" |
michael@0 | 45 | #include "event2/event_struct.h" |
michael@0 | 46 | #include "event2/thread.h" |
michael@0 | 47 | #include "event-internal.h" |
michael@0 | 48 | #include "evsignal-internal.h" |
michael@0 | 49 | #include "log-internal.h" |
michael@0 | 50 | #include "evmap-internal.h" |
michael@0 | 51 | #include "evthread-internal.h" |
michael@0 | 52 | |
michael@0 | 53 | struct devpollop { |
michael@0 | 54 | struct pollfd *events; |
michael@0 | 55 | int nevents; |
michael@0 | 56 | int dpfd; |
michael@0 | 57 | struct pollfd *changes; |
michael@0 | 58 | int nchanges; |
michael@0 | 59 | }; |
michael@0 | 60 | |
michael@0 | 61 | static void *devpoll_init(struct event_base *); |
michael@0 | 62 | static int devpoll_add(struct event_base *, int fd, short old, short events, void *); |
michael@0 | 63 | static int devpoll_del(struct event_base *, int fd, short old, short events, void *); |
michael@0 | 64 | static int devpoll_dispatch(struct event_base *, struct timeval *); |
michael@0 | 65 | static void devpoll_dealloc(struct event_base *); |
michael@0 | 66 | |
michael@0 | 67 | const struct eventop devpollops = { |
michael@0 | 68 | "devpoll", |
michael@0 | 69 | devpoll_init, |
michael@0 | 70 | devpoll_add, |
michael@0 | 71 | devpoll_del, |
michael@0 | 72 | devpoll_dispatch, |
michael@0 | 73 | devpoll_dealloc, |
michael@0 | 74 | 1, /* need reinit */ |
michael@0 | 75 | EV_FEATURE_FDS|EV_FEATURE_O1, |
michael@0 | 76 | 0 |
michael@0 | 77 | }; |
michael@0 | 78 | |
michael@0 | 79 | #define NEVENT 32000 |
michael@0 | 80 | |
michael@0 | 81 | static int |
michael@0 | 82 | devpoll_commit(struct devpollop *devpollop) |
michael@0 | 83 | { |
michael@0 | 84 | /* |
michael@0 | 85 | * Due to a bug in Solaris, we have to use pwrite with an offset of 0. |
michael@0 | 86 | * Write is limited to 2GB of data, until it will fail. |
michael@0 | 87 | */ |
michael@0 | 88 | if (pwrite(devpollop->dpfd, devpollop->changes, |
michael@0 | 89 | sizeof(struct pollfd) * devpollop->nchanges, 0) == -1) |
michael@0 | 90 | return (-1); |
michael@0 | 91 | |
michael@0 | 92 | devpollop->nchanges = 0; |
michael@0 | 93 | return (0); |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | static int |
michael@0 | 97 | devpoll_queue(struct devpollop *devpollop, int fd, int events) { |
michael@0 | 98 | struct pollfd *pfd; |
michael@0 | 99 | |
michael@0 | 100 | if (devpollop->nchanges >= devpollop->nevents) { |
michael@0 | 101 | /* |
michael@0 | 102 | * Change buffer is full, must commit it to /dev/poll before |
michael@0 | 103 | * adding more |
michael@0 | 104 | */ |
michael@0 | 105 | if (devpoll_commit(devpollop) != 0) |
michael@0 | 106 | return (-1); |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | pfd = &devpollop->changes[devpollop->nchanges++]; |
michael@0 | 110 | pfd->fd = fd; |
michael@0 | 111 | pfd->events = events; |
michael@0 | 112 | pfd->revents = 0; |
michael@0 | 113 | |
michael@0 | 114 | return (0); |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | static void * |
michael@0 | 118 | devpoll_init(struct event_base *base) |
michael@0 | 119 | { |
michael@0 | 120 | int dpfd, nfiles = NEVENT; |
michael@0 | 121 | struct rlimit rl; |
michael@0 | 122 | struct devpollop *devpollop; |
michael@0 | 123 | |
michael@0 | 124 | if (!(devpollop = mm_calloc(1, sizeof(struct devpollop)))) |
michael@0 | 125 | return (NULL); |
michael@0 | 126 | |
michael@0 | 127 | if (getrlimit(RLIMIT_NOFILE, &rl) == 0 && |
michael@0 | 128 | rl.rlim_cur != RLIM_INFINITY) |
michael@0 | 129 | nfiles = rl.rlim_cur; |
michael@0 | 130 | |
michael@0 | 131 | /* Initialize the kernel queue */ |
michael@0 | 132 | if ((dpfd = evutil_open_closeonexec("/dev/poll", O_RDWR, 0)) == -1) { |
michael@0 | 133 | event_warn("open: /dev/poll"); |
michael@0 | 134 | mm_free(devpollop); |
michael@0 | 135 | return (NULL); |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | devpollop->dpfd = dpfd; |
michael@0 | 139 | |
michael@0 | 140 | /* Initialize fields */ |
michael@0 | 141 | /* FIXME: allocating 'nfiles' worth of space here can be |
michael@0 | 142 | * expensive and unnecessary. See how epoll.c does it instead. */ |
michael@0 | 143 | devpollop->events = mm_calloc(nfiles, sizeof(struct pollfd)); |
michael@0 | 144 | if (devpollop->events == NULL) { |
michael@0 | 145 | mm_free(devpollop); |
michael@0 | 146 | close(dpfd); |
michael@0 | 147 | return (NULL); |
michael@0 | 148 | } |
michael@0 | 149 | devpollop->nevents = nfiles; |
michael@0 | 150 | |
michael@0 | 151 | devpollop->changes = mm_calloc(nfiles, sizeof(struct pollfd)); |
michael@0 | 152 | if (devpollop->changes == NULL) { |
michael@0 | 153 | mm_free(devpollop->events); |
michael@0 | 154 | mm_free(devpollop); |
michael@0 | 155 | close(dpfd); |
michael@0 | 156 | return (NULL); |
michael@0 | 157 | } |
michael@0 | 158 | |
michael@0 | 159 | evsig_init(base); |
michael@0 | 160 | |
michael@0 | 161 | return (devpollop); |
michael@0 | 162 | } |
michael@0 | 163 | |
michael@0 | 164 | static int |
michael@0 | 165 | devpoll_dispatch(struct event_base *base, struct timeval *tv) |
michael@0 | 166 | { |
michael@0 | 167 | struct devpollop *devpollop = base->evbase; |
michael@0 | 168 | struct pollfd *events = devpollop->events; |
michael@0 | 169 | struct dvpoll dvp; |
michael@0 | 170 | int i, res, timeout = -1; |
michael@0 | 171 | |
michael@0 | 172 | if (devpollop->nchanges) |
michael@0 | 173 | devpoll_commit(devpollop); |
michael@0 | 174 | |
michael@0 | 175 | if (tv != NULL) |
michael@0 | 176 | timeout = tv->tv_sec * 1000 + (tv->tv_usec + 999) / 1000; |
michael@0 | 177 | |
michael@0 | 178 | dvp.dp_fds = devpollop->events; |
michael@0 | 179 | dvp.dp_nfds = devpollop->nevents; |
michael@0 | 180 | dvp.dp_timeout = timeout; |
michael@0 | 181 | |
michael@0 | 182 | EVBASE_RELEASE_LOCK(base, th_base_lock); |
michael@0 | 183 | |
michael@0 | 184 | res = ioctl(devpollop->dpfd, DP_POLL, &dvp); |
michael@0 | 185 | |
michael@0 | 186 | EVBASE_ACQUIRE_LOCK(base, th_base_lock); |
michael@0 | 187 | |
michael@0 | 188 | if (res == -1) { |
michael@0 | 189 | if (errno != EINTR) { |
michael@0 | 190 | event_warn("ioctl: DP_POLL"); |
michael@0 | 191 | return (-1); |
michael@0 | 192 | } |
michael@0 | 193 | |
michael@0 | 194 | return (0); |
michael@0 | 195 | } |
michael@0 | 196 | |
michael@0 | 197 | event_debug(("%s: devpoll_wait reports %d", __func__, res)); |
michael@0 | 198 | |
michael@0 | 199 | for (i = 0; i < res; i++) { |
michael@0 | 200 | int which = 0; |
michael@0 | 201 | int what = events[i].revents; |
michael@0 | 202 | |
michael@0 | 203 | if (what & POLLHUP) |
michael@0 | 204 | what |= POLLIN | POLLOUT; |
michael@0 | 205 | else if (what & POLLERR) |
michael@0 | 206 | what |= POLLIN | POLLOUT; |
michael@0 | 207 | |
michael@0 | 208 | if (what & POLLIN) |
michael@0 | 209 | which |= EV_READ; |
michael@0 | 210 | if (what & POLLOUT) |
michael@0 | 211 | which |= EV_WRITE; |
michael@0 | 212 | |
michael@0 | 213 | if (!which) |
michael@0 | 214 | continue; |
michael@0 | 215 | |
michael@0 | 216 | /* XXX(niels): not sure if this works for devpoll */ |
michael@0 | 217 | evmap_io_active(base, events[i].fd, which); |
michael@0 | 218 | } |
michael@0 | 219 | |
michael@0 | 220 | return (0); |
michael@0 | 221 | } |
michael@0 | 222 | |
michael@0 | 223 | |
michael@0 | 224 | static int |
michael@0 | 225 | devpoll_add(struct event_base *base, int fd, short old, short events, void *p) |
michael@0 | 226 | { |
michael@0 | 227 | struct devpollop *devpollop = base->evbase; |
michael@0 | 228 | int res; |
michael@0 | 229 | (void)p; |
michael@0 | 230 | |
michael@0 | 231 | /* |
michael@0 | 232 | * It's not necessary to OR the existing read/write events that we |
michael@0 | 233 | * are currently interested in with the new event we are adding. |
michael@0 | 234 | * The /dev/poll driver ORs any new events with the existing events |
michael@0 | 235 | * that it has cached for the fd. |
michael@0 | 236 | */ |
michael@0 | 237 | |
michael@0 | 238 | res = 0; |
michael@0 | 239 | if (events & EV_READ) |
michael@0 | 240 | res |= POLLIN; |
michael@0 | 241 | if (events & EV_WRITE) |
michael@0 | 242 | res |= POLLOUT; |
michael@0 | 243 | |
michael@0 | 244 | if (devpoll_queue(devpollop, fd, res) != 0) |
michael@0 | 245 | return (-1); |
michael@0 | 246 | |
michael@0 | 247 | return (0); |
michael@0 | 248 | } |
michael@0 | 249 | |
michael@0 | 250 | static int |
michael@0 | 251 | devpoll_del(struct event_base *base, int fd, short old, short events, void *p) |
michael@0 | 252 | { |
michael@0 | 253 | struct devpollop *devpollop = base->evbase; |
michael@0 | 254 | int res; |
michael@0 | 255 | (void)p; |
michael@0 | 256 | |
michael@0 | 257 | res = 0; |
michael@0 | 258 | if (events & EV_READ) |
michael@0 | 259 | res |= POLLIN; |
michael@0 | 260 | if (events & EV_WRITE) |
michael@0 | 261 | res |= POLLOUT; |
michael@0 | 262 | |
michael@0 | 263 | /* |
michael@0 | 264 | * The only way to remove an fd from the /dev/poll monitored set is |
michael@0 | 265 | * to use POLLREMOVE by itself. This removes ALL events for the fd |
michael@0 | 266 | * provided so if we care about two events and are only removing one |
michael@0 | 267 | * we must re-add the other event after POLLREMOVE. |
michael@0 | 268 | */ |
michael@0 | 269 | |
michael@0 | 270 | if (devpoll_queue(devpollop, fd, POLLREMOVE) != 0) |
michael@0 | 271 | return (-1); |
michael@0 | 272 | |
michael@0 | 273 | if ((res & (POLLIN|POLLOUT)) != (POLLIN|POLLOUT)) { |
michael@0 | 274 | /* |
michael@0 | 275 | * We're not deleting all events, so we must resubmit the |
michael@0 | 276 | * event that we are still interested in if one exists. |
michael@0 | 277 | */ |
michael@0 | 278 | |
michael@0 | 279 | if ((res & POLLIN) && (old & EV_WRITE)) { |
michael@0 | 280 | /* Deleting read, still care about write */ |
michael@0 | 281 | devpoll_queue(devpollop, fd, POLLOUT); |
michael@0 | 282 | } else if ((res & POLLOUT) && (old & EV_READ)) { |
michael@0 | 283 | /* Deleting write, still care about read */ |
michael@0 | 284 | devpoll_queue(devpollop, fd, POLLIN); |
michael@0 | 285 | } |
michael@0 | 286 | } |
michael@0 | 287 | |
michael@0 | 288 | return (0); |
michael@0 | 289 | } |
michael@0 | 290 | |
michael@0 | 291 | static void |
michael@0 | 292 | devpoll_dealloc(struct event_base *base) |
michael@0 | 293 | { |
michael@0 | 294 | struct devpollop *devpollop = base->evbase; |
michael@0 | 295 | |
michael@0 | 296 | evsig_dealloc(base); |
michael@0 | 297 | if (devpollop->events) |
michael@0 | 298 | mm_free(devpollop->events); |
michael@0 | 299 | if (devpollop->changes) |
michael@0 | 300 | mm_free(devpollop->changes); |
michael@0 | 301 | if (devpollop->dpfd >= 0) |
michael@0 | 302 | close(devpollop->dpfd); |
michael@0 | 303 | |
michael@0 | 304 | memset(devpollop, 0, sizeof(struct devpollop)); |
michael@0 | 305 | mm_free(devpollop); |
michael@0 | 306 | } |