git: f86fd0a1af0b - stable/15 - EVFILT_PROCDESC: support NOTE_FORK

Konstantin Belousov <[email protected]>
Newsgroups gmane.os.freebsd.devel.cvs.src
Message-ID <6a8124a7.44c97.461d2fcc__27756.6470367432$1786848773$gmane$org@gitrepo.freebsd.org>
The branch stable/15 has been updated by kib:

URL: https://cgit.FreeBSD.org/src/commit/?id=f86fd0a1af0b56af991c3740deccca349f0b9466

commit f86fd0a1af0b56af991c3740deccca349f0b9466
Author:     Konstantin Belousov <[email protected]>
AuthorDate: 2026-07-16 00:49:02 +0000
Commit:     Konstantin Belousov <[email protected]>
CommitDate: 2026-08-16 02:41:32 +0000

    EVFILT_PROCDESC: support NOTE_FORK
    
    (cherry picked from commit e8d4d754e1c73b01c89580a26c22f982415c694a)
---
 sys/kern/kern_fork.c    |  9 +++++++++
 sys/kern/sys_procdesc.c | 19 +++++++++++++++++++
 sys/sys/event.h         |  8 +++++---
 sys/sys/procdesc.h      |  2 ++
 4 files changed, 35 insertions(+), 3 deletions(-)

diff --git a/sys/kern/kern_fork.c b/sys/kern/kern_fork.c
index 313fdef86fd8..45052c53d2e3 100644
--- a/sys/kern/kern_fork.c
+++ b/sys/kern/kern_fork.c
@@ -824,6 +824,15 @@ do_fork(struct thread *td, struct fork_req *fr, struct proc *p2, struct thread *
 		sx_xunlock(&proctree_lock);
 	}
 
+	/*
+	 * Activate procdesc NOTE_FORK after we attached the debugger
+	 * to the child.  This guarantees that a debugger which does
+	 * kevent() on the process descriptor to get notifications of
+	 * fork events, can properly observe the child right after the
+	 * notification fired.
+	 */
+	procdesc_fork(p1, p2->p_pid);
+
 	racct_proc_fork_done(p2);
 
 	if ((fr->fr_flags & RFSTOPPED) == 0) {
diff --git a/sys/kern/sys_procdesc.c b/sys/kern/sys_procdesc.c
index 07e431d4c381..610423f49fe9 100644
--- a/sys/kern/sys_procdesc.c
+++ b/sys/kern/sys_procdesc.c
@@ -335,6 +335,22 @@ procdesc_jobstate(struct proc *p)
 	wakeup(&p->p_procdesc);
 }
 
+void
+procdesc_fork(struct proc *p, pid_t child_pid)
+{
+	struct procdesc *pd;
+
+	PROC_LOCK(p);
+	pd = p->p_procdesc;
+	if (pd != NULL) {
+		PROCDESC_LOCK(pd);
+		pd->pd_last_child = child_pid;
+		KNOTE_LOCKED(&pd->pd_selinfo.si_note, NOTE_FORK);
+		PROCDESC_UNLOCK(pd);
+	}
+	PROC_UNLOCK(p);
+}
+
 /*
  * When a process descriptor is reaped, perhaps as a result of close(), release
  * the process's reference on the process descriptor.
@@ -509,6 +525,9 @@ procdesc_kqops_event(struct knote *kn, long hint)
 		return (1);
 	}
 
+	if ((kn->kn_fflags & NOTE_FORK) != 0)
+		kn->kn_data = pd->pd_last_child;
+
 	return (kn->kn_fflags != 0);
 }
 
diff --git a/sys/sys/event.h b/sys/sys/event.h
index dfba8bc83032..a92eecb32575 100644
--- a/sys/sys/event.h
+++ b/sys/sys/event.h
@@ -202,9 +202,11 @@ struct freebsd11_kevent32 {
 /*
  * data/hint flags for EVFILT_PROC and EVFILT_PROCDESC, shared with userspace
  */
-#define	NOTE_EXIT	0x80000000		/* process exited */
-#define	NOTE_FORK	0x40000000		/* process forked */
-#define	NOTE_EXEC	0x20000000		/* process exec'd */
+#define	NOTE_EXIT	0x80000000		/* proc/procdesc: process
+						   exited */
+#define	NOTE_FORK	0x40000000		/* proc/procdesc: process
+						   forked */
+#define	NOTE_EXEC	0x20000000		/* proc: process exec'd */
 #define	NOTE_PDSIGCHLD	0x10000000		/* procdesc: pdwait() info
 						   available */
 #define	NOTE_SIGNAL	0x08000000		/* process received a signal,
diff --git a/sys/sys/procdesc.h b/sys/sys/procdesc.h
index 5ca93d3f4646..ac23dbdcb53b 100644
--- a/sys/sys/procdesc.h
+++ b/sys/sys/procdesc.h
@@ -64,6 +64,7 @@ struct procdesc {
 	 */
 	struct proc	*pd_proc;		/* (t) Process. */
 	pid_t		 pd_pid;		/* (c) Cached pid. */
+	pid_t		 pd_last_child;		/* (p) Pid of the last child. */
 	u_int		 pd_refcount;		/* (r) Reference count. */
 	u_int		 pd_fpcount;		/* (p|t) files referencing me */
 
@@ -102,6 +103,7 @@ struct procdesc {
  * In-kernel interfaces to process descriptors.
  */
 bool	 procdesc_exit(struct proc *);
+void	 procdesc_fork(struct proc *p, pid_t child_pid);
 void	 procdesc_jobstate(struct proc *p);
 int	 kern_pdgetpid(struct thread *, int fd, const cap_rights_t *,
 	    pid_t *pidp);
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.