This patch fix the following problems:

* port_disassociate was sometimes called on events not associated in the
event port resulting in bogus error messages being printed out

* Use event_warn instead of perror

* Modified the implementation of the ed_pending array to contain the
index of the entry that needs to be reassociated instead of a pointer to
the element (so we don't need to relocate all of the pointers when the
array is relocated)

Trond Norbye

Index: evport.c
===================================================================
--- evport.c	(revision 463)
+++ evport.c	(working copy)
@@ -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 <sys/time.h>
-#else
-#include <sys/_time.h>
-#endif
 #include <assert.h>
 #include <sys/queue.h>
 #include <sys/tree.h>
@@ -118,7 +114,7 @@
 	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 @@
 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 @@
 		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 @@
 	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,16 +270,11 @@
 	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");
+		if (port_associate(epdp->ed_port, PORT_SOURCE_FD,
+				   fd, sysevents, NULL) == -1) {
+			event_warn("port_associate");
 			return (-1);
 		}
-	} else {
-		if (-1 == port_dissociate(epdp->ed_port, PORT_SOURCE_FD, fd)) {
-			perror("port_dissociate");
-			return (-1);
-		}
 	}
 
 	check_evportop(epdp);
@@ -337,13 +321,16 @@
 	 * 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 @@
 			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 @@
 
 		check_evportop(epdp);
 		check_event(pevt);
+		epdp->ed_pending[i] = fd;
 
 		/*
 		 * Figure out what kind of event it was 
@@ -405,19 +393,6 @@
 				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 @@
 {
 	struct evport_data *evpd = arg;
 	struct fd_info *fdi;
+	int i;
+	int associated = 1;
 
 	check_evportop(evpd);
 
@@ -505,6 +482,12 @@
 		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 @@
 	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;
 }
 
 
_______________________________________________
Libevent-users mailing list
[email protected]
http://monkey.org/mailman/listinfo/libevent-users

Reply via email to