| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * 3. The name of the author may not be used to endorse or promote products |
| 13 | * derived from this software without specific prior written permission. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY SUN MICROSYSTEMS, INC. ``AS IS'' AND ANY |
| 16 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 18 | * DISCLAIMED. IN NO EVENT SHALL SUN MICROSYSTEMS, INC. BE LIABLE FOR ANY |
| 19 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | */ |
| 26 |
| 27 | /* |
| 28 | * Copyright (c) 2007 Sun Microsystems. All rights reserved. |
| 29 | * Use is subject to license terms. |
| 30 | */ |
| 31 |
| 32 | /* |
| 33 | * evport.c: event backend using Solaris 10 event ports. See port_create(3C). |
| 34 | * This implementation is loosely modeled after the one used for select(2) (in |
| 35 | * select.c). |
| 36 | * |
| 37 | * The outstanding events are tracked in a data structure called evport_data. |
| 38 | * Each entry in the ed_fds array corresponds to a file descriptor, and contains |
| 39 | * pointers to the read and write events that correspond to that fd. (That is, |
| 40 | * when the file is readable, the "read" event should handle it, etc.) |
| 41 | * |
| 42 | * evport_add and evport_del update this data structure. evport_dispatch uses it |
| 43 | * to determine where to callback when an event occurs (which it gets from |
| 44 | * port_getn). |
| 45 | * |
| 46 | * Helper functions are used: grow() grows the file descriptor array as |
| 47 | * necessary when large fd's come in. reassociate() takes care of maintaining |
| 48 | * the proper file-descriptor/event-port associations. |
| 49 | * |
| 50 | * As in the select(2) implementation, signals are handled by evsignal. |
| 51 | */ |
| 52 |
| 53 | #include "config.h" |
| 54 |
| 55 | #include <sys/time.h> |
| 56 | #include <assert.h> |
| 57 | #include <sys/queue.h> |
| 58 | #include <errno.h> |
| 59 | #include <poll.h> |
| 60 | #include <port.h> |
| 61 | #include <signal.h> |
| 62 | #include <stdio.h> |
| 63 | #include <stdlib.h> |
| 64 | #include <string.h> |
| 65 | #include <time.h> |
| 66 | #include <unistd.h> |
| 67 | #ifdef CHECK_INVARIANTS |
| 68 | #include <assert.h> |
| 69 | #endif |
| 70 |
| 71 | #include "event.h" |
| 72 | #include "event-internal.h" |
| 73 | #include "log.h" |
| 74 | #include "evsignal.h" |
| 75 |
| 76 |
| 77 | /* |
| 78 | * Default value for ed_nevents, which is the maximum file descriptor number we |
| 79 | * can handle. If an event comes in for a file descriptor F > nevents, we will |
| 80 | * grow the array of file descriptors, doubling its size. |
| 81 | */ |
| 82 | #define DEFAULT_NFDS 16 |
| 83 |
| 84 |
| 85 | /* |
| 86 | * EVENTS_PER_GETN is the maximum number of events to retrieve from port_getn on |
| 87 | * any particular call. You can speed things up by increasing this, but it will |
| 88 | * (obviously) require more memory. |
| 89 | */ |
| 90 | #define EVENTS_PER_GETN 8 |
| 91 |
| 92 | /* |
| 93 | * Per-file-descriptor information about what events we're subscribed to. These |
| 94 | * fields are NULL if no event is subscribed to either of them. |
| 95 | */ |
| 96 |
| 97 | struct fd_info { |
| 98 | struct event* fdi_revt; /* the event responsible for the "read" */ |
| 99 | struct event* fdi_wevt; /* the event responsible for the "write" */ |
| 100 | }; |
| 101 |
| 102 | #define FDI_HAS_READ(fdi) ((fdi)->fdi_revt != NULL) |
| 103 | #define FDI_HAS_WRITE(fdi) ((fdi)->fdi_wevt != NULL) |
| 104 | #define FDI_HAS_EVENTS(fdi) (FDI_HAS_READ(fdi) || FDI_HAS_WRITE(fdi)) |
| 105 | #define FDI_TO_SYSEVENTS(fdi) (FDI_HAS_READ(fdi) ? POLLIN : 0) | \ |
| 106 | (FDI_HAS_WRITE(fdi) ? POLLOUT : 0) |
| 107 |
| 108 | struct evport_data { |
| 109 | int ed_port; /* event port for system events */ |
| 110 | int ed_nevents; /* number of allocated fdi's */ |
| 111 | struct fd_info *ed_fds; /* allocated fdi table */ |
| 112 | /* fdi's that we need to reassoc */ |
| 113 | int ed_pending[EVENTS_PER_GETN]; /* fd's with pending events */ |
| 114 | }; |
| 115 |
| 116 | static void* evport_init (struct event_base *); |
| 117 | static int evport_add (void *, struct event *); |
| 118 | static int evport_del (void *, struct event *); |
| 119 | static int evport_dispatch (struct event_base *, void *, struct timeval *); |
| 120 | static void evport_dealloc (struct event_base *, void *); |
| 121 |
| 122 | const struct eventop evportops = { |
| 123 | "evport", |
| 124 | evport_init, |
| 125 | evport_add, |
| 126 | evport_del, |
| 127 | evport_dispatch, |
| 128 | evport_dealloc, |
| 129 | 1 /* need reinit */ |
| 130 | }; |
| 131 |
| 132 | /* |
| 133 | * Initialize the event port implementation. |
| 134 | */ |
| 135 |
| 136 | static void* |
| 137 | evport_init(struct event_base *base) |
| 138 | { |
| 139 | struct evport_data *evpd; |
| 140 | int i; |
| 141 | /* |
| 142 | * Disable event ports when this environment variable is set |
| 143 | */ |
| 144 | if (evutil_getenv("EVENT_NOEVPORT")) |
| 145 | return (NULL); |
| 146 |
| 147 | if (!(evpd = calloc(1, sizeof(struct evport_data)))) |
| 148 | return (NULL); |
| 149 |
| 150 | if ((evpd->ed_port = port_create()) == -1) { |
| 151 | free(evpd); |
| 152 | return (NULL); |
| 153 | } |
| 154 |
| 155 | /* |
| 156 | * Initialize file descriptor structure |
| 157 | */ |
| 158 | evpd->ed_fds = calloc(DEFAULT_NFDS, sizeof(struct fd_info)); |
| 159 | if (evpd->ed_fds == NULL) { |
| 160 | close(evpd->ed_port); |
| 161 | free(evpd); |
| 162 | return (NULL); |
| 163 | } |
| 164 | evpd->ed_nevents = DEFAULT_NFDS; |
| 165 | for (i = 0; i < EVENTS_PER_GETN; i++) |
| 166 | evpd->ed_pending[i] = -1; |
| 167 |
| 168 | evsignal_init(base); |
| 169 |
| 170 | return (evpd); |
| 171 | } |
| 172 |
| 173 | #ifdef CHECK_INVARIANTS |
| 174 | /* |
| 175 | * Checks some basic properties about the evport_data structure. Because it |
| 176 | * checks all file descriptors, this function can be expensive when the maximum |
| 177 | * file descriptor ever used is rather large. |
| 178 | */ |
| 179 |
| 180 | static void |
| 181 | check_evportop(struct evport_data *evpd) |
| 182 | { |
| 183 | assert(evpd); |
| 184 | assert(evpd->ed_nevents > 0); |
| 185 | assert(evpd->ed_port > 0); |
| 186 | assert(evpd->ed_fds > 0); |
| 187 |
| 188 | /* |
| 189 | * Verify the integrity of the fd_info struct as well as the events to |
| 190 | * which it points (at least, that they're valid references and correct |
| 191 | * for their position in the structure). |
| 192 | */ |
| 193 | int i; |
| 194 | for (i = 0; i < evpd->ed_nevents; ++i) { |
| 195 | struct event *ev; |
| 196 | struct fd_info *fdi; |
| 197 |
| 198 | fdi = &evpd->ed_fds[i]; |
| 199 | if ((ev = fdi->fdi_revt) != NULL) { |
| 200 | assert(ev->ev_fd == i); |
| 201 | } |
| 202 | if ((ev = fdi->fdi_wevt) != NULL) { |
| 203 | assert(ev->ev_fd == i); |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 |
| 208 | /* |
| 209 | * Verifies very basic integrity of a given port_event. |
| 210 | */ |
| 211 | static void |
| 212 | check_event(port_event_t* pevt) |
| 213 | { |
| 214 | /* |
| 215 | * We've only registered for PORT_SOURCE_FD events. The only |
| 216 | * other thing we can legitimately receive is PORT_SOURCE_ALERT, |
| 217 | * but since we're not using port_alert either, we can assume |
| 218 | * PORT_SOURCE_FD. |
| 219 | */ |
| 220 | assert(pevt->portev_source == PORT_SOURCE_FD); |
| 221 | assert(pevt->portev_user == NULL); |
| 222 | } |
| 223 |
| 224 | #else |
| 225 | #define check_evportop(epop) |
| 226 | #define check_event(pevt) |
| 227 | #endif /* CHECK_INVARIANTS */ |
| 228 |
| 229 | /* |
| 230 | * Doubles the size of the allocated file descriptor array. |
| 231 | */ |
| 232 | static int |
| 233 | grow(struct evport_data *epdp, int factor) |
| 234 | { |
| 235 | struct fd_info *tmp; |
| 236 | int oldsize = epdp->ed_nevents; |
| 237 | int newsize = factor * oldsize; |
| 238 | assert(factor > 1); |
| 239 |
| 240 | check_evportop(epdp); |
| 241 |
| 242 | tmp = realloc(epdp->ed_fds, sizeof(struct fd_info) * newsize); |
| 243 | if (NULL == tmp) |
| 244 | return -1; |
| 245 | epdp->ed_fds = tmp; |
| 246 | memset((char*) (epdp->ed_fds + oldsize), 0, |
| 247 | (newsize - oldsize)*sizeof(struct fd_info)); |
| 248 | epdp->ed_nevents = newsize; |
| 249 |
| 250 | check_evportop(epdp); |
| 251 |
| 252 | return 0; |
| 253 | } |
| 254 |
| 255 |
| 256 | /* |
| 257 | * (Re)associates the given file descriptor with the event port. The OS events |
| 258 | * are specified (implicitly) from the fd_info struct. |
| 259 | */ |
| 260 | static int |
| 261 | reassociate(struct evport_data *epdp, struct fd_info *fdip, int fd) |
| 262 | { |
| 263 | int sysevents = FDI_TO_SYSEVENTS(fdip); |
| 264 |
| 265 | if (sysevents != 0) { |
| 266 | if (port_associate(epdp->ed_port, PORT_SOURCE_FD, |
| 267 | fd, sysevents, NULL) == -1) { |
| 268 | event_warn("port_associate"); |
| 269 | return (-1); |
| 270 | } |
| 271 | } |
| 272 |
| 273 | check_evportop(epdp); |
| 274 |
| 275 | return (0); |
| 276 | } |
| 277 |
| 278 | /* |
| 279 | * Main event loop - polls port_getn for some number of events, and processes |
| 280 | * them. |
| 281 | */ |
| 282 |
| 283 | static int |
| 284 | evport_dispatch(struct event_base *base, void *arg, struct timeval *tv) |
| 285 | { |
| 286 | int i, res; |
| 287 | struct evport_data *epdp = arg; |
| 288 | port_event_t pevtlist[EVENTS_PER_GETN]; |
| 289 |
| 290 | /* |
| 291 | * port_getn will block until it has at least nevents events. It will |
| 292 | * also return how many it's given us (which may be more than we asked |
| 293 | * for, as long as it's less than our maximum (EVENTS_PER_GETN)) in |
| 294 | * nevents. |
| 295 | */ |
| 296 | int nevents = 1; |
| 297 |
| 298 | /* |
| 299 | * We have to convert a struct timeval to a struct timespec |
| 300 | * (only difference is nanoseconds vs. microseconds). If no time-based |
| 301 | * events are active, we should wait for I/O (and tv == NULL). |
| 302 | */ |
| 303 | struct timespec ts; |
| 304 | struct timespec *ts_p = NULL; |
| 305 | if (tv != NULL) { |
| 306 | ts.tv_sec = tv->tv_sec; |
| 307 | ts.tv_nsec = tv->tv_usec * 1000; |
| 308 | ts_p = &ts; |
| 309 | } |
| 310 |
| 311 | /* |
| 312 | * Before doing anything else, we need to reassociate the events we hit |
| 313 | * last time which need reassociation. See comment at the end of the |
| 314 | * loop below. |
| 315 | */ |
| 316 | for (i = 0; i < EVENTS_PER_GETN; ++i) { |
| 317 | struct fd_info *fdi = NULL; |
| 318 | if (epdp->ed_pending[i] != -1) { |
| 319 | fdi = &(epdp->ed_fds[epdp->ed_pending[i]]); |
| 320 | } |
| 321 |
| 322 | if (fdi != NULL && FDI_HAS_EVENTS(fdi)) { |
| 323 | int fd = FDI_HAS_READ(fdi) ? fdi->fdi_revt->ev_fd : |
| 324 | fdi->fdi_wevt->ev_fd; |
| 325 | reassociate(epdp, fdi, fd); |
| 326 | epdp->ed_pending[i] = -1; |
| 327 | } |
| 328 | } |
| 329 |
| 330 | if ((res = port_getn(epdp->ed_port, pevtlist, EVENTS_PER_GETN, |
| 331 | (unsigned int *) &nevents, ts_p)) == -1) { |
| 332 | if (errno == EINTR || errno == EAGAIN) { |
| 333 | evsignal_process(base); |
| 334 | return (0); |
| 335 | } else if (errno == ETIME) { |
| 336 | if (nevents == 0) |
| 337 | return (0); |
| 338 | } else { |
| 339 | event_warn("port_getn"); |
| 340 | return (-1); |
| 341 | } |
| 342 | } else if (base->sig.evsignal_caught) { |
| 343 | evsignal_process(base); |
| 344 | } |
| 345 |
| 346 | event_debug(("%s: port_getn reports %d events", __func__, nevents)); |
| 347 |
| 348 | for (i = 0; i < nevents; ++i) { |
| 349 | struct event *ev; |
| 350 | struct fd_info *fdi; |
| 351 | port_event_t *pevt = &pevtlist[i]; |
| 352 | int fd = (int) pevt->portev_object; |
| 353 |
| 354 | check_evportop(epdp); |
| 355 | check_event(pevt); |
| 356 | epdp->ed_pending[i] = fd; |
| 357 |
| 358 | /* |
| 359 | * Figure out what kind of event it was |
| 360 | * (because we have to pass this to the callback) |
| 361 | */ |
| 362 | res = 0; |
| 363 | if (pevt->portev_events & POLLIN) |
| 364 | res |= EV_READ; |
| 365 | if (pevt->portev_events & POLLOUT) |
| 366 | res |= EV_WRITE; |
| 367 |
| 368 | /* |
| 369 | * Check for the error situations or a hangup situation |
| 370 | */ |
| 371 | if (pevt->portev_events & (POLLERR|POLLHUP|POLLNVAL)) |
| 372 | res |= EV_READ|EV_WRITE; |
| 373 |
| 374 | assert(epdp->ed_nevents > fd); |
| 375 | fdi = &(epdp->ed_fds[fd]); |
| 376 |
| 377 | /* |
| 378 | * We now check for each of the possible events (READ |
| 379 | * or WRITE). Then, we activate the event (which will |
| 380 | * cause its callback to be executed). |
| 381 | */ |
| 382 |
| 383 | if ((res & EV_READ) && ((ev = fdi->fdi_revt) != NULL)) { |
| 384 | event_active(ev, res, 1); |
| 385 | } |
| 386 |
| 387 | if ((res & EV_WRITE) && ((ev = fdi->fdi_wevt) != NULL)) { |
| 388 | event_active(ev, res, 1); |
| 389 | } |
| 390 | } /* end of all events gotten */ |
| 391 |
| 392 | check_evportop(epdp); |
| 393 |
| 394 | return (0); |
| 395 | } |
| 396 |
| 397 |
| 398 | /* |
| 399 | * Adds the given event (so that you will be notified when it happens via |
| 400 | * the callback function). |
| 401 | */ |
| 402 |
| 403 | static int |
| 404 | evport_add(void *arg, struct event *ev) |
| 405 | { |
| 406 | struct evport_data *evpd = arg; |
| 407 | struct fd_info *fdi; |
| 408 | int factor; |
| 409 |
| 410 | check_evportop(evpd); |
| 411 |
| 412 | /* |
| 413 | * Delegate, if it's not ours to handle. |
| 414 | */ |
| 415 | if (ev->ev_events & EV_SIGNAL) |
| 416 | return (evsignal_add(ev)); |
| 417 |
| 418 | /* |
| 419 | * If necessary, grow the file descriptor info table |
| 420 | */ |
| 421 |
| 422 | factor = 1; |
| 423 | while (ev->ev_fd >= factor * evpd->ed_nevents) |
| 424 | factor *= 2; |
| 425 |
| 426 | if (factor > 1) { |
| 427 | if (-1 == grow(evpd, factor)) { |
| 428 | return (-1); |
| 429 | } |
| 430 | } |
| 431 |
| 432 | fdi = &evpd->ed_fds[ev->ev_fd]; |
| 433 | if (ev->ev_events & EV_READ) |
| 434 | fdi->fdi_revt = ev; |
| 435 | if (ev->ev_events & EV_WRITE) |
| 436 | fdi->fdi_wevt = ev; |
| 437 |
| 438 | return reassociate(evpd, fdi, ev->ev_fd); |
| 439 | } |
| 440 |
| 441 | /* |
| 442 | * Removes the given event from the list of events to wait for. |
| 443 | */ |
| 444 |
| 445 | static int |
| 446 | evport_del(void *arg, struct event *ev) |
| 447 | { |
| 448 | struct evport_data *evpd = arg; |
| 449 | struct fd_info *fdi; |
| 450 | int i; |
| 451 | int associated = 1; |
| 452 |
| 453 | check_evportop(evpd); |
| 454 |
| 455 | /* |
| 456 | * Delegate, if it's not ours to handle |
| 457 | */ |
| 458 | if (ev->ev_events & EV_SIGNAL) { |
| 459 | return (evsignal_del(ev)); |
| 460 | } |
| 461 |
| 462 | if (evpd->ed_nevents < ev->ev_fd) { |
| 463 | return (-1); |
| 464 | } |
| 465 |
| 466 | for (i = 0; i < EVENTS_PER_GETN; ++i) { |
| 467 | if (evpd->ed_pending[i] == ev->ev_fd) { |
| 468 | associated = 0; |
| 469 | break; |
| 470 | } |
| 471 | } |
| 472 |
| 473 | fdi = &evpd->ed_fds[ev->ev_fd]; |
| 474 | if (ev->ev_events & EV_READ) |
| 475 | fdi->fdi_revt = NULL; |
| 476 | if (ev->ev_events & EV_WRITE) |
| 477 | fdi->fdi_wevt = NULL; |
| 478 |
| 479 | if (associated) { |
| 480 | if (!FDI_HAS_EVENTS(fdi) && |
| 481 | port_dissociate(evpd->ed_port, PORT_SOURCE_FD, |
| 482 | ev->ev_fd) == -1) { |
| 483 | /* |
| 484 | * Ignre EBADFD error the fd could have been closed |
| 485 | * before event_del() was called. |
| 486 | */ |
| 487 | if (errno != EBADFD) { |
| 488 | event_warn("port_dissociate"); |
| 489 | return (-1); |
| 490 | } |
| 491 | } else { |
| 492 | if (FDI_HAS_EVENTS(fdi)) { |
| 493 | return (reassociate(evpd, fdi, ev->ev_fd)); |
| 494 | } |
| 495 | } |
| 496 | } else { |
| 497 | if (fdi->fdi_revt == NULL && fdi->fdi_wevt == NULL) { |
| 498 | evpd->ed_pending[i] = -1; |
| 499 | } |
| 500 | } |
| 501 | return 0; |
| 502 | } |
| 503 |
| 504 |
| 505 | static void |
| 506 | evport_dealloc(struct event_base *base, void *arg) |
| 507 | { |
| 508 | struct evport_data *evpd = arg; |
| 509 |
| 510 | evsignal_dealloc(base); |
| 511 |
| 512 | close(evpd->ed_port); |
| 513 |
| 514 | if (evpd->ed_fds) |
| 515 | free(evpd->ed_fds); |
| 516 | free(evpd); |
| 517 | } |
| 518 |