diff --git a/ChangeLog b/ChangeLog index 6dd6b354..2b811087 100644 --- a/ChangeLog +++ b/ChangeLog @@ -27,4 +27,5 @@ Changes in current version: o Make the test.sh script run unit tests for the evpoll method. o Make the entire evdns.h header enclosed in "extern C" as appropriate. o Fix implementation of strsep on platforms that lack it - o Fix implementation of getaddrinfo on platforms that lack it; mainly, this will make Windows http.c work better. Original patch by Lubomir Marinov. \ No newline at end of file + o Fix implementation of getaddrinfo on platforms that lack it; mainly, this will make Windows http.c work better. Original patch by Lubomir Marinov. + o Fix evport implementation: port_disassociate called on unassociated events resulting in bogus errors; more efficient memory management; from Trond Norbye and Prakash Sangappa diff --git a/evport.c b/evport.c index 8bb3c8cf..ea82e495 100644 --- a/evport.c +++ b/evport.c @@ -12,20 +12,20 @@ * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * THIS SOFTWARE IS PROVIDED BY SUN MICROSYSTEMS, INC. ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL SUN MICROSYSTEMS, INC. BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /* - * Copyright (c) 2006 Sun Microsystems. All rights reserved. + * Copyright (c) 2007 Sun Microsystems. All rights reserved. * Use is subject to license terms. */ @@ -55,11 +55,7 @@ #include "config.h" #endif -#ifdef HAVE_SYS_TIME_H #include -#else -#include -#endif #include #include #include @@ -118,7 +114,7 @@ struct evport_data { int ed_nevents; /* number of allocated fdi's */ struct fd_info *ed_fds; /* allocated fdi table */ /* fdi's that we need to reassoc */ - struct fd_info *ed_pending[EVENTS_PER_GETN]; + int ed_pending[EVENTS_PER_GETN]; /* fd's with pending events */ }; static void* evport_init (struct event_base *); @@ -146,6 +142,7 @@ static void* evport_init(struct event_base *base) { struct evport_data *evpd; + int i; /* * Disable event ports when this environment variable is set */ @@ -170,7 +167,8 @@ evport_init(struct event_base *base) return (NULL); } evpd->ed_nevents = DEFAULT_NFDS; - memset(&evpd->ed_pending, 0, EVENTS_PER_GETN * sizeof(struct fd_info*)); + for (i = 0; i < EVENTS_PER_GETN; i++) + evpd->ed_pending[i] = -1; evsignal_init(base); @@ -254,15 +252,6 @@ grow(struct evport_data *epdp, int factor) epdp->ed_fds = tmp; memset((char*) (epdp->ed_fds + oldsize), 0, (newsize - oldsize)*sizeof(struct fd_info)); - - /* The ev_pending array contains pointers into the released array. */ - for (ii = 0; ii < EVENTS_PER_GETN; ++ii) { - if (epdp->ed_pending[ii] != 0) { - int offset = epdp->ed_pending[ii] - old; - epdp->ed_pending[ii] = epdp->ed_fds + offset; - } - } - epdp->ed_nevents = newsize; check_evportop(epdp); @@ -281,14 +270,9 @@ reassociate(struct evport_data *epdp, struct fd_info *fdip, int fd) int sysevents = FDI_TO_SYSEVENTS(fdip); if (sysevents != 0) { - if ((-1 == port_associate(epdp->ed_port, PORT_SOURCE_FD, - fd, sysevents, NULL))) { - perror("port_associate"); - return (-1); - } - } else { - if (-1 == port_dissociate(epdp->ed_port, PORT_SOURCE_FD, fd)) { - perror("port_dissociate"); + if (port_associate(epdp->ed_port, PORT_SOURCE_FD, + fd, sysevents, NULL) == -1) { + event_warn("port_associate"); return (-1); } } @@ -337,13 +321,16 @@ evport_dispatch(struct event_base *base, void *arg, struct timeval *tv) * loop below. */ for (i = 0; i < EVENTS_PER_GETN; ++i) { - struct fd_info *fdi = epdp->ed_pending[i]; + struct fd_info *fdi = NULL; + if (epdp->ed_pending[i] != -1) { + fdi = &(epdp->ed_fds[epdp->ed_pending[i]]); + } if (fdi != NULL && FDI_HAS_EVENTS(fdi)) { int fd = FDI_HAS_READ(fdi) ? fdi->fdi_revt->ev_fd : fdi->fdi_wevt->ev_fd; reassociate(epdp, fdi, fd); - epdp->ed_pending[i] = NULL; + epdp->ed_pending[i] = -1; } } @@ -356,7 +343,7 @@ evport_dispatch(struct event_base *base, void *arg, struct timeval *tv) if (nevents == 0) return (0); } else { - perror("port_getn"); + event_warn("port_getn"); return (-1); } } else if (base->sig.evsignal_caught) { @@ -373,6 +360,7 @@ evport_dispatch(struct event_base *base, void *arg, struct timeval *tv) check_evportop(epdp); check_event(pevt); + epdp->ed_pending[i] = fd; /* * Figure out what kind of event it was @@ -405,19 +393,6 @@ evport_dispatch(struct event_base *base, void *arg, struct timeval *tv) event_del(ev); event_active(ev, res, 1); } - - /* - * If there are still events (they haven't been deleted), then - * we must reassociate the port, since the event port interface - * dissociates them automatically. - * - * But we can't do it right away, because the event hasn't - * handled this event yet, so of course there's still data - * waiting! - */ - if(FDI_HAS_EVENTS(fdi)) { - epdp->ed_pending[i] = fdi; - } } /* end of all events gotten */ check_evportop(epdp); @@ -491,6 +466,8 @@ evport_del(void *arg, struct event *ev) { struct evport_data *evpd = arg; struct fd_info *fdi; + int i; + int associated = 1; check_evportop(evpd); @@ -505,6 +482,12 @@ evport_del(void *arg, struct event *ev) return (-1); } + for (i = 0; i < EVENTS_PER_GETN; ++i) { + if (evpd->ed_pending[i] == ev->ev_fd) { + associated = 0; + break; + } + } fdi = &evpd->ed_fds[ev->ev_fd]; if (ev->ev_events & EV_READ) @@ -512,7 +495,29 @@ evport_del(void *arg, struct event *ev) if (ev->ev_events & EV_WRITE) fdi->fdi_wevt = NULL; - return reassociate(evpd, fdi, ev->ev_fd); + if (associated) { + if (!FDI_HAS_EVENTS(fdi) && + port_dissociate(evpd->ed_port, PORT_SOURCE_FD, + ev->ev_fd) == -1) { + /* + * Ignre EBADFD error the fd could have been closed + * before event_del() was called. + */ + if (errno != EBADFD) { + event_warn("port_dissociate"); + return (-1); + } + } else { + if (FDI_HAS_EVENTS(fdi)) { + return (reassociate(evpd, fdi, ev->ev_fd)); + } + } + } else { + if (fdi->fdi_revt == NULL && fdi->fdi_wevt == NULL) { + evpd->ed_pending[i] = -1; + } + } + return 0; }