git: 1cdfd599002b - stable/15 - sys_procdesc: extract procdesc_alloc()

Konstantin Belousov <[email protected]>
Newsgroups gmane.os.freebsd.devel.cvs.src
Message-ID <6a812487.423ce.13e9b6fe__2482.63817500032$1786848503$gmane$org@gitrepo.freebsd.org>
The branch stable/15 has been updated by kib:

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

commit 1cdfd599002bb5ae6c4407a4dddc4b10930e07fb
Author:     Konstantin Belousov <[email protected]>
AuthorDate: 2026-05-20 02:02:54 +0000
Commit:     Konstantin Belousov <[email protected]>
CommitDate: 2026-08-16 02:41:20 +0000

    sys_procdesc: extract procdesc_alloc()
    
    (cherry picked from commit 599d021224f2af3fc0befdbd6b804a328dd556f9)
---
 sys/kern/sys_procdesc.c | 36 ++++++++++++++++++++++++------------
 1 file changed, 24 insertions(+), 12 deletions(-)

diff --git a/sys/kern/sys_procdesc.c b/sys/kern/sys_procdesc.c
index d29675b82b57..edf98b7f69d9 100644
--- a/sys/kern/sys_procdesc.c
+++ b/sys/kern/sys_procdesc.c
@@ -201,23 +201,15 @@ sys_pdgetpid(struct thread *td, struct pdgetpid_args *uap)
 	return (error);
 }
 
-/*
- * When a new process is forked by pdfork(), a file descriptor is allocated
- * by the fork code first, then the process is forked, and then we get a
- * chance to set up the process descriptor.  Failure is not permitted at this
- * point, so procdesc_new() must succeed.
- */
-void
-procdesc_new(struct proc *p, int flags)
+static struct procdesc *
+procdesc_alloc(int flags)
 {
 	struct procdesc *pd;
 
 	pd = malloc(sizeof(*pd), M_PROCDESC, M_WAITOK | M_ZERO);
-	pd->pd_proc = p;
-	pd->pd_pid = p->p_pid;
-	p->p_procdesc = pd;
 	pd->pd_flags = 0;
-	if (flags & PD_DAEMON)
+	pd->pd_pid = -1;
+	if ((flags & PD_DAEMON) != 0)
 		pd->pd_flags |= PDF_DAEMON;
 	PROCDESC_LOCK_INIT(pd);
 	knlist_init_mtx(&pd->pd_selinfo.si_note, &pd->pd_lock);
@@ -227,6 +219,26 @@ procdesc_new(struct proc *p, int flags)
 	 * struct file, and the other from their struct proc.
 	 */
 	refcount_init(&pd->pd_refcount, 2);
+
+	return (pd);
+}
+
+/*
+ * When a new process is forked by pdfork(), a file descriptor is allocated
+ * by the fork code first, then the process is forked, and then we get a
+ * chance to set up the process descriptor.  Failure is not permitted at this
+ * point, so procdesc_new() must succeed.
+ */
+void
+procdesc_new(struct proc *p, int flags)
+{
+	struct procdesc *pd;
+
+	pd = procdesc_alloc(flags);
+	pd->pd_proc = p;
+	pd->pd_pid = p->p_pid;
+	MPASS(p->p_procdesc == NULL);
+	p->p_procdesc = pd;
 }
 
 /*
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.