Page MenuHomeFreeBSD

D58292.id182243.diff
No OneTemporary

D58292.id182243.diff

diff --git a/lib/libsys/kqueue.2 b/lib/libsys/kqueue.2
--- a/lib/libsys/kqueue.2
+++ b/lib/libsys/kqueue.2
@@ -592,6 +592,41 @@
The process has exited.
The exit status will be stored in
.Va data .
+.It Dv NOTE_FORK
+The process has forked.
+The process identifier
+.Pq PID
+of the most recently forked child is returned in the
+.Va data
+field.
+.Pp
+The identifier is advisory;
+if several children were spawned before the event is returned,
+only one of the PIDs is returned.
+Other mechanisms provide reliable reporting of fork events.
+For instance, debugging the process with a subscription for relevant
+events would serialize forks.
+See
+.Xr ptrace 2
+and the description of the
+.Dv PT_FOLLOW_FORK
+request.
+.Pp
+Unlike the
+.Dv EVFILT_PROC
+filter,
+the
+.Dv NOTE_TRACK
+pseudo-event is not supported.
+.Pp
+If both
+.Dv NOTE_EXIT
+and
+.Dv NOTE_FORK
+events are reported, then the
+.Va data
+field contains the exit status of the process,
+and the last child's PID is not returned.
.It Dv NOTE_PDSIGCHLD
Activates on events that are reported through
.Xr pdwait 2
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.
@@ -514,6 +530,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);

File Metadata

Mime Type
text/plain
Expires
Sat, Aug 1, 4:45 AM (10 h, 55 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
35791138
Default Alt Text
D58292.id182243.diff (3 KB)

Event Timeline