Don't add kevents to closing file descriptors (from riastradh) trunk
authorchristos <christos@NetBSD.org>
Sat, 11 Nov 2017 03:58:01 +0000
branchtrunk
changeset 273207 df2c5eec156e
parent 273206 905a75de3bfd
child 273208 e0509729ccc1
Don't add kevents to closing file descriptors (from riastradh)
sys/kern/kern_event.c
--- a/sys/kern/kern_event.c	Fri Nov 10 22:54:20 2017 +0000
+++ b/sys/kern/kern_event.c	Sat Nov 11 03:58:01 2017 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_event.c,v 1.97 2017/11/07 18:35:57 christos Exp $	*/
+/*	$NetBSD: kern_event.c,v 1.98 2017/11/11 03:58:01 christos Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
@@ -58,7 +58,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_event.c,v 1.97 2017/11/07 18:35:57 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_event.c,v 1.98 2017/11/11 03:58:01 christos Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -982,6 +982,10 @@
 		}
 		mutex_enter(&fdp->fd_lock);
 		ff = fdp->fd_dt->dt_ff[fd];
+		if (ff->ff_refcnt & FR_CLOSING) {
+			error = EBADF;
+			goto doneunlock;
+		}
 		if (fd <= fdp->fd_lastkqfile) {
 			SLIST_FOREACH(kn, &ff->ff_knlist, kn_link) {
 				if (kq == kn->kn_kq &&
@@ -1096,8 +1100,7 @@
 	} else {
 		if (kn == NULL) {
 			error = ENOENT;
-		 	mutex_exit(&fdp->fd_lock);
-			goto done;
+			goto doneunlock;
 		}
 		if (kev->flags & EV_DELETE) {
 			/* knote_detach() drops fdp->fd_lock */
@@ -1118,6 +1121,7 @@
 	if ((kev->flags & EV_ENABLE)) {
 		knote_enqueue(kn);
 	}
+doneunlock:
 	mutex_exit(&fdp->fd_lock);
  done:
 	rw_exit(&kqueue_filter_lock);