aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKonstantin Belousov <kib@FreeBSD.org>2026-07-16 00:49:02 +0000
committerKonstantin Belousov <kib@FreeBSD.org>2026-07-19 20:39:29 +0000
commite8d4d754e1c73b01c89580a26c22f982415c694a (patch)
tree0d6c4dd1fa6233a897fe333bca5672c4262a3908
parent29d15d658d175139196d821b123c30a5b58e135e (diff)
EVFILT_PROCDESC: support NOTE_FORK
Reviewed by: markj Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D58292
-rw-r--r--sys/kern/kern_fork.c9
-rw-r--r--sys/kern/sys_procdesc.c19
-rw-r--r--sys/sys/event.h3
-rw-r--r--sys/sys/procdesc.h2
4 files changed, 32 insertions, 1 deletions
diff --git a/sys/kern/kern_fork.c b/sys/kern/kern_fork.c
index 26e2510698e2..60d89c03f865 100644
--- a/sys/kern/kern_fork.c
+++ b/sys/kern/kern_fork.c
@@ -825,6 +825,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 152f24c219c9..fd4b660d6afa 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.
@@ -506,6 +522,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 ed215453a325..9b8a3ba2bcf1 100644
--- a/sys/sys/event.h
+++ b/sys/sys/event.h
@@ -204,7 +204,8 @@ struct freebsd11_kevent32 {
*/
#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
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);