kqueue's EVFILT_PROC and unreaped children

Kelly Yancey kelly at nttmcl.com
Thu Apr 10 19:01:35 PDT 2003


  The attached patch, against -STABLE, closes a window where a process
can fork/exec a child and then register a NOTE_EXIT note for the child
only to get ESRCH even if the child is still a zombie. Conceivably,
something similar should be done for NOTE_EXEC, but I don't know a simple
way to do that (I'm not saying there isn't one...just that I don't know it
:) ).

  Anyway, I'de appreciate feedback before I adapt the patch to -CURRENT
and commit.  Thanks,

  Kelly

--
Kelly Yancey -- kbyanc@{posi.net,FreeBSD.org} -- kelly at nttmcl.com
-------------- next part --------------
Index: sys/kern/kern_event.c
===================================================================
RCS file: /home/ncvs/src/sys/kern/kern_event.c,v
retrieving revision 1.2.2.8
diff -u -p -r1.2.2.8 kern_event.c
--- sys/kern/kern_event.c	14 Dec 2001 19:24:42 -0000	1.2.2.8
+++ sys/kern/kern_event.c	11 Apr 2003 00:55:48 -0000
@@ -179,8 +179,14 @@ static int
 filt_procattach(struct knote *kn)
 {
 	struct proc *p;
+	int immediate;
 
+	immediate = 0;
 	p = pfind(kn->kn_id);
+	if (p == NULL && kn->kn_sfflags & NOTE_EXIT) {
+		p = zpfind(kn->kn_id);
+		immediate = 1;
+	}
 	if (p == NULL)
 		return (ESRCH);
 	if (! PRISON_CHECK(curproc, p))
@@ -200,6 +206,14 @@ filt_procattach(struct knote *kn)
 
 	/* XXX lock the proc here while adding to the list? */
 	SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
+
+	/*
+	 * Immediately activate the knote if it is an exit note for a zombie
+	 * process.  This is necessary to handle the case where a child dies
+	 * before the kevent is registered.
+	 */
+	if (immediate && filt_proc(kn, NOTE_EXIT))
+		KNOTE_ACTIVATE(kn);
 
 	return (0);
 }
Index: sys/kern/kern_proc.c
===================================================================
RCS file: /home/ncvs/src/sys/kern/kern_proc.c,v
retrieving revision 1.63.2.8
diff -u -p -r1.63.2.8 kern_proc.c
--- sys/kern/kern_proc.c	1 May 2001 13:39:06 -0000	1.63.2.8
+++ sys/kern/kern_proc.c	10 Apr 2003 23:50:55 -0000
@@ -31,7 +31,7 @@
  * SUCH DAMAGE.
  *
  *	@(#)kern_proc.c	8.7 (Berkeley) 2/14/95
- * $FreeBSD: src/sys/kern/kern_proc.c,v 1.63.2.7 2000/09/07 19:13:36 truckman Exp $
+ * $FreeBSD: src/sys/kern/kern_proc.c,v 1.63.2.8 2001/05/01 13:39:06 dwmalone Exp $
  */
 
 #include <sys/param.h>
@@ -403,7 +403,7 @@ fill_eproc(p, ep)
 	}
 }
 
-static struct proc *
+struct proc *
 zpfind(pid_t pid)
 {
 	struct proc *p;
Index: sys/sys/proc.h
===================================================================
RCS file: /home/ncvs/src/sys/sys/proc.h,v
retrieving revision 1.99.2.7
diff -u -p -r1.99.2.7 proc.h
--- sys/sys/proc.h	31 Jan 2002 18:40:29 -0000	1.99.2.7
+++ sys/sys/proc.h	10 Apr 2003 23:50:55 -0000
@@ -36,7 +36,7 @@
  * SUCH DAMAGE.
  *
  *	@(#)proc.h	8.15 (Berkeley) 5/19/95
- * $FreeBSD$
+ * $FreeBSD: src/sys/sys/proc.h,v 1.99.2.7 2002/01/31 18:40:29 dillon Exp $
  */
 
 #ifndef _SYS_PROC_H_
@@ -417,6 +417,7 @@ extern	u_long ps_arg_cache_limit;
 extern	int ps_argsopen;
 
 struct proc *pfind __P((pid_t));	/* Find process by id. */
+struct proc *zpfind __P((pid_t));	/* Find zombie process by id. */
 struct pgrp *pgfind __P((pid_t));	/* Find process group by id. */
 
 struct vm_zone;


More information about the freebsd-arch mailing list