| 6 |
| * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 17 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 19 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 22 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 23 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 25 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 |
| 28 | #include "config.h" |
| 29 |
| 30 | #include <sys/types.h> |
| 31 | #include <sys/resource.h> |
| 32 | #ifdef HAVE_SYS_TIME_H |
| 33 | #include <sys/time.h> |
| 34 | #else |
| 35 | #include <sys/_libevent_time.h> |
| 36 | #endif |
| 37 | #include <sys/queue.h> |
| 38 | #include <sys/devpoll.h> |
| 39 | #include <signal.h> |
| 40 | #include <stdio.h> |
| 41 | #include <stdlib.h> |
| 42 | #include <string.h> |
| 43 | #include <unistd.h> |
| 44 | #include <fcntl.h> |
| 45 | #include <errno.h> |
| 46 | #include <assert.h> |
| 47 |
| 48 | #include "event.h" |
| 49 | #include "event-internal.h" |
| 50 | #include "evsignal.h" |
| 51 | #include "log.h" |
| 52 |
| 53 | /* due to limitations in the devpoll interface, we need to keep track of |
| 54 | * all file descriptors outself. |
| 55 | */ |
| 56 | struct evdevpoll { |
| 57 | struct event *evread; |
| 58 | struct event *evwrite; |
| 59 | }; |
| 60 |
| 61 | struct devpollop { |
| 62 | struct evdevpoll *fds; |
| 63 | int nfds; |
| 64 | struct pollfd *events; |
| 65 | int nevents; |
| 66 | int dpfd; |
| 67 | struct pollfd *changes; |
| 68 | int nchanges; |
| 69 | }; |
| 70 |
| 71 | static void *devpoll_init (struct event_base *); |
| 72 | static int devpoll_add (void *, struct event *); |
| 73 | static int devpoll_del (void *, struct event *); |
| 74 | static int devpoll_dispatch (struct event_base *, void *, struct timeval *); |
| 75 | static void devpoll_dealloc (struct event_base *, void *); |
| 76 |
| 77 | const struct eventop devpollops = { |
| 78 | "devpoll", |
| 79 | devpoll_init, |
| 80 | devpoll_add, |
| 81 | devpoll_del, |
| 82 | devpoll_dispatch, |
| 83 | devpoll_dealloc, |
| 84 | 1 /* need reinit */ |
| 85 | }; |
| 86 |
| 87 | #define NEVENT 32000 |
| 88 |
| 89 | static int |
| 90 | devpoll_commit(struct devpollop *devpollop) |
| 91 | { |
| 92 | /* |
| 93 | * Due to a bug in Solaris, we have to use pwrite with an offset of 0. |
| 94 | * Write is limited to 2GB of data, until it will fail. |
| 95 | */ |
| 96 | if (pwrite(devpollop->dpfd, devpollop->changes, |
| 97 | sizeof(struct pollfd) * devpollop->nchanges, 0) == -1) |
| 98 | return(-1); |
| 99 |
| 100 | devpollop->nchanges = 0; |
| 101 | return(0); |
| 102 | } |
| 103 |
| 104 | static int |
| 105 | devpoll_queue(struct devpollop *devpollop, int fd, int events) { |
| 106 | struct pollfd *pfd; |
| 107 |
| 108 | if (devpollop->nchanges >= devpollop->nevents) { |
| 109 | /* |
| 110 | * Change buffer is full, must commit it to /dev/poll before |
| 111 | * adding more |
| 112 | */ |
| 113 | if (devpoll_commit(devpollop) != 0) |
| 114 | return(-1); |
| 115 | } |
| 116 |
| 117 | pfd = &devpollop->changes[devpollop->nchanges++]; |
| 118 | pfd->fd = fd; |
| 119 | pfd->events = events; |
| 120 | pfd->revents = 0; |
| 121 |
| 122 | return(0); |
| 123 | } |
| 124 |
| 125 | static void * |
| 126 | devpoll_init(struct event_base *base) |
| 127 | { |
| 128 | int dpfd, nfiles = NEVENT; |
| 129 | struct rlimit rl; |
| 130 | struct devpollop *devpollop; |
| 131 |
| 132 | /* Disable devpoll when this environment variable is set */ |
| 133 | if (evutil_getenv("EVENT_NODEVPOLL")) |
| 134 | return (NULL); |
| 135 |
| 136 | if (!(devpollop = calloc(1, sizeof(struct devpollop)))) |
| 137 | return (NULL); |
| 138 |
| 139 | if (getrlimit(RLIMIT_NOFILE, &rl) == 0 && |
| 140 | rl.rlim_cur != RLIM_INFINITY) |
| 141 | nfiles = rl.rlim_cur; |
| 142 |
| 143 | /* Initialize the kernel queue */ |
| 144 | if ((dpfd = open("/dev/poll", O_RDWR)) == -1) { |
| 145 | event_warn("open: /dev/poll"); |
| 146 | free(devpollop); |
| 147 | return (NULL); |
| 148 | } |
| 149 |
| 150 | devpollop->dpfd = dpfd; |
| 151 |
| 152 | /* Initialize fields */ |
| 153 | devpollop->events = calloc(nfiles, sizeof(struct pollfd)); |
| 154 | if (devpollop->events == NULL) { |
| 155 | free(devpollop); |
| 156 | close(dpfd); |
| 157 | return (NULL); |
| 158 | } |
| 159 | devpollop->nevents = nfiles; |
| 160 |
| 161 | devpollop->fds = calloc(nfiles, sizeof(struct evdevpoll)); |
| 162 | if (devpollop->fds == NULL) { |
| 163 | free(devpollop->events); |
| 164 | free(devpollop); |
| 165 | close(dpfd); |
| 166 | return (NULL); |
| 167 | } |
| 168 | devpollop->nfds = nfiles; |
| 169 |
| 170 | devpollop->changes = calloc(nfiles, sizeof(struct pollfd)); |
| 171 | if (devpollop->changes == NULL) { |
| 172 | free(devpollop->fds); |
| 173 | free(devpollop->events); |
| 174 | free(devpollop); |
| 175 | close(dpfd); |
| 176 | return (NULL); |
| 177 | } |
| 178 |
| 179 | evsignal_init(base); |
| 180 |
| 181 | return (devpollop); |
| 182 | } |
| 183 |
| 184 | static int |
| 185 | devpoll_recalc(struct event_base *base, void *arg, int max) |
| 186 | { |
| 187 | struct devpollop *devpollop = arg; |
| 188 |
| 189 | if (max >= devpollop->nfds) { |
| 190 | struct evdevpoll *fds; |
| 191 | int nfds; |
| 192 |
| 193 | nfds = devpollop->nfds; |
| 194 | while (nfds <= max) |
| 195 | nfds <<= 1; |
| 196 |
| 197 | fds = realloc(devpollop->fds, nfds * sizeof(struct evdevpoll)); |
| 198 | if (fds == NULL) { |
| 199 | event_warn("realloc"); |
| 200 | return (-1); |
| 201 | } |
| 202 | devpollop->fds = fds; |
| 203 | memset(fds + devpollop->nfds, 0, |
| 204 | (nfds - devpollop->nfds) * sizeof(struct evdevpoll)); |
| 205 | devpollop->nfds = nfds; |
| 206 | } |
| 207 |
| 208 | return (0); |
| 209 | } |
| 210 |
| 211 | static int |
| 212 | devpoll_dispatch(struct event_base *base, void *arg, struct timeval *tv) |
| 213 | { |
| 214 | struct devpollop *devpollop = arg; |
| 215 | struct pollfd *events = devpollop->events; |
| 216 | struct dvpoll dvp; |
| 217 | struct evdevpoll *evdp; |
| 218 | int i, res, timeout = -1; |
| 219 |
| 220 | if (devpollop->nchanges) |
| 221 | devpoll_commit(devpollop); |
| 222 |
| 223 | if (tv != NULL) |
| 224 | timeout = tv->tv_sec * 1000 + (tv->tv_usec + 999) / 1000; |
| 225 |
| 226 | dvp.dp_fds = devpollop->events; |
| 227 | dvp.dp_nfds = devpollop->nevents; |
| 228 | dvp.dp_timeout = timeout; |
| 229 |
| 230 | res = ioctl(devpollop->dpfd, DP_POLL, &dvp); |
| 231 |
| 232 | if (res == -1) { |
| 233 | if (errno != EINTR) { |
| 234 | event_warn("ioctl: DP_POLL"); |
| 235 | return (-1); |
| 236 | } |
| 237 |
| 238 | evsignal_process(base); |
| 239 | return (0); |
| 240 | } else if (base->sig.evsignal_caught) { |
| 241 | evsignal_process(base); |
| 242 | } |
| 243 |
| 244 | event_debug(("%s: devpoll_wait reports %d", __func__, res)); |
| 245 |
| 246 | for (i = 0; i < res; i++) { |
| 247 | int which = 0; |
| 248 | int what = events[i].revents; |
| 249 | struct event *evread = NULL, *evwrite = NULL; |
| 250 |
| 251 | assert(events[i].fd < devpollop->nfds); |
| 252 | evdp = &devpollop->fds[events[i].fd]; |
| 253 |
| 254 | if (what & POLLHUP) |
| 255 | what |= POLLIN | POLLOUT; |
| 256 | else if (what & POLLERR) |
| 257 | what |= POLLIN | POLLOUT; |
| 258 |
| 259 | if (what & POLLIN) { |
| 260 | evread = evdp->evread; |
| 261 | which |= EV_READ; |
| 262 | } |
| 263 |
| 264 | if (what & POLLOUT) { |
| 265 | evwrite = evdp->evwrite; |
| 266 | which |= EV_WRITE; |
| 267 | } |
| 268 |
| 269 | if (!which) |
| 270 | continue; |
| 271 |
| 272 | if (evread != NULL && !(evread->ev_events & EV_PERSIST)) |
| 273 | event_del(evread); |
| 274 | if (evwrite != NULL && evwrite != evread && |
| 275 | !(evwrite->ev_events & EV_PERSIST)) |
| 276 | event_del(evwrite); |
| 277 |
| 278 | if (evread != NULL) |
| 279 | event_active(evread, EV_READ, 1); |
| 280 | if (evwrite != NULL) |
| 281 | event_active(evwrite, EV_WRITE, 1); |
| 282 | } |
| 283 |
| 284 | return (0); |
| 285 | } |
| 286 |
| 287 |
| 288 | static int |
| 289 | devpoll_add(void *arg, struct event *ev) |
| 290 | { |
| 291 | struct devpollop *devpollop = arg; |
| 292 | struct evdevpoll *evdp; |
| 293 | int fd, events; |
| 294 |
| 295 | if (ev->ev_events & EV_SIGNAL) |
| 296 | return (evsignal_add(ev)); |
| 297 |
| 298 | fd = ev->ev_fd; |
| 299 | if (fd >= devpollop->nfds) { |
| 300 | /* Extend the file descriptor array as necessary */ |
| 301 | if (devpoll_recalc(ev->ev_base, devpollop, fd) == -1) |
| 302 | return (-1); |
| 303 | } |
| 304 | evdp = &devpollop->fds[fd]; |
| 305 |
| 306 | /* |
| 307 | * It's not necessary to OR the existing read/write events that we |
| 308 | * are currently interested in with the new event we are adding. |
| 309 | * The /dev/poll driver ORs any new events with the existing events |
| 310 | * that it has cached for the fd. |
| 311 | */ |
| 312 |
| 313 | events = 0; |
| 314 | if (ev->ev_events & EV_READ) { |
| 315 | if (evdp->evread && evdp->evread != ev) { |
| 316 | /* There is already a different read event registered */ |
| 317 | return(-1); |
| 318 | } |
| 319 | events |= POLLIN; |
| 320 | } |
| 321 |
| 322 | if (ev->ev_events & EV_WRITE) { |
| 323 | if (evdp->evwrite && evdp->evwrite != ev) { |
| 324 | /* There is already a different write event registered */ |
| 325 | return(-1); |
| 326 | } |
| 327 | events |= POLLOUT; |
| 328 | } |
| 329 |
| 330 | if (devpoll_queue(devpollop, fd, events) != 0) |
| 331 | return(-1); |
| 332 |
| 333 | /* Update events responsible */ |
| 334 | if (ev->ev_events & EV_READ) |
| 335 | evdp->evread = ev; |
| 336 | if (ev->ev_events & EV_WRITE) |
| 337 | evdp->evwrite = ev; |
| 338 |
| 339 | return (0); |
| 340 | } |
| 341 |
| 342 | static int |
| 343 | devpoll_del(void *arg, struct event *ev) |
| 344 | { |
| 345 | struct devpollop *devpollop = arg; |
| 346 | struct evdevpoll *evdp; |
| 347 | int fd, events; |
| 348 | int needwritedelete = 1, needreaddelete = 1; |
| 349 |
| 350 | if (ev->ev_events & EV_SIGNAL) |
| 351 | return (evsignal_del(ev)); |
| 352 |
| 353 | fd = ev->ev_fd; |
| 354 | if (fd >= devpollop->nfds) |
| 355 | return (0); |
| 356 | evdp = &devpollop->fds[fd]; |
| 357 |
| 358 | events = 0; |
| 359 | if (ev->ev_events & EV_READ) |
| 360 | events |= POLLIN; |
| 361 | if (ev->ev_events & EV_WRITE) |
| 362 | events |= POLLOUT; |
| 363 |
| 364 | /* |
| 365 | * The only way to remove an fd from the /dev/poll monitored set is |
| 366 | * to use POLLREMOVE by itself. This removes ALL events for the fd |
| 367 | * provided so if we care about two events and are only removing one |
| 368 | * we must re-add the other event after POLLREMOVE. |
| 369 | */ |
| 370 |
| 371 | if (devpoll_queue(devpollop, fd, POLLREMOVE) != 0) |
| 372 | return(-1); |
| 373 |
| 374 | if ((events & (POLLIN|POLLOUT)) != (POLLIN|POLLOUT)) { |
| 375 | /* |
| 376 | * We're not deleting all events, so we must resubmit the |
| 377 | * event that we are still interested in if one exists. |
| 378 | */ |
| 379 |
| 380 | if ((events & POLLIN) && evdp->evwrite != NULL) { |
| 381 | /* Deleting read, still care about write */ |
| 382 | devpoll_queue(devpollop, fd, POLLOUT); |
| 383 | needwritedelete = 0; |
| 384 | } else if ((events & POLLOUT) && evdp->evread != NULL) { |
| 385 | /* Deleting write, still care about read */ |
| 386 | devpoll_queue(devpollop, fd, POLLIN); |
| 387 | needreaddelete = 0; |
| 388 | } |
| 389 | } |
| 390 |
| 391 | if (needreaddelete) |
| 392 | evdp->evread = NULL; |
| 393 | if (needwritedelete) |
| 394 | evdp->evwrite = NULL; |
| 395 |
| 396 | return (0); |
| 397 | } |
| 398 |
| 399 | static void |
| 400 | devpoll_dealloc(struct event_base *base, void *arg) |
| 401 | { |
| 402 | struct devpollop *devpollop = arg; |
| 403 |
| 404 | evsignal_dealloc(base); |
| 405 | if (devpollop->fds) |
| 406 | free(devpollop->fds); |
| 407 | if (devpollop->events) |
| 408 | free(devpollop->events); |
| 409 | if (devpollop->changes) |
| 410 | free(devpollop->changes); |
| 411 | if (devpollop->dpfd >= 0) |
| 412 | close(devpollop->dpfd); |
| 413 |
| 414 | memset(devpollop, 0, sizeof(struct devpollop)); |
| 415 | free(devpollop); |
| 416 | } |
| 417 |