diff --git a/sys/kern/kern_fork.c b/sys/kern/kern_fork.c --- a/sys/kern/kern_fork.c +++ b/sys/kern/kern_fork.c @@ -825,6 +825,15 @@ 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 --- a/sys/kern/sys_procdesc.c +++ b/sys/kern/sys_procdesc.c @@ -335,6 +335,22 @@ 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. @@ -506,6 +522,9 @@ 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 --- a/sys/sys/event.h +++ b/sys/sys/event.h @@ -204,7 +204,8 @@ */ #define NOTE_EXIT 0x80000000 /* proc/procdesc: process exited */ -#define NOTE_FORK 0x40000000 /* proc: process forked */ +#define NOTE_FORK 0x40000000 /* proc/procdesc: process + forked */ #define NOTE_EXEC 0x20000000 /* proc: process exec'd */ #define NOTE_PDSIGCHLD 0x10000000 /* procdesc: pdwait() info available */ diff --git a/sys/sys/procdesc.h b/sys/sys/procdesc.h --- a/sys/sys/procdesc.h +++ b/sys/sys/procdesc.h @@ -64,6 +64,7 @@ */ 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 @@ * 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);