git: c0e1201aaba8 - main - procdesc: make PD_DAEMON per-file

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Tue, 07 Jul 2026 01:57:16 UTC
The branch main has been updated by kib:

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

commit c0e1201aaba8860bdcfd754e35024a85ceb1580a
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2026-05-25 17:48:03 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2026-07-07 01:24:28 +0000

    procdesc: make PD_DAEMON per-file
    
    Reviewed by:    markj
    Tested by:      pho
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
    Differential revision:  https://reviews.freebsd.org/D57124
---
 sys/kern/sys_procdesc.c | 26 +++++++++++++++++---------
 sys/sys/file.h          |  1 +
 sys/sys/procdesc.h      |  7 ++++++-
 3 files changed, 24 insertions(+), 10 deletions(-)

diff --git a/sys/kern/sys_procdesc.c b/sys/kern/sys_procdesc.c
index c5be98f66749..0d57a8cc7b2c 100644
--- a/sys/kern/sys_procdesc.c
+++ b/sys/kern/sys_procdesc.c
@@ -209,8 +209,6 @@ procdesc_alloc(int flags)
 	pd = malloc(sizeof(*pd), M_PROCDESC, M_WAITOK | M_ZERO);
 	pd->pd_flags = 0;
 	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);
 
@@ -260,7 +258,12 @@ int
 procdesc_falloc(struct thread *td, struct file **resultfp, int *resultfd,
     int flags, struct filecaps *fcaps)
 {
-	return (falloc_caps(td, resultfp, resultfd, pdtofdflags(flags), fcaps));
+	int error;
+
+	error = falloc_caps(td, resultfp, resultfd, pdtofdflags(flags), fcaps);
+	if (error == 0 && (flags & PD_DAEMON) != 0)
+		(*resultfp)->f_pdflags |= F_PD_NOKILL;
+	return (error);
 }
 
 /*
@@ -369,6 +372,15 @@ procdesc_reap(struct proc *p)
 	procdesc_free(pd);
 }
 
+static void
+procdesc_close_tail(struct file *fp, struct proc *p)
+{
+	if ((fp->f_pdflags & F_PD_NOKILL) == 0)
+		kern_psignal(p, SIGKILL);
+	PROC_UNLOCK(p);
+	sx_xunlock(&proctree_lock);
+}
+
 /*
  * procdesc_close() - last close on a process descriptor.  If the process is
  * still running, terminate with SIGKILL (unless PDF_DAEMON is set) and let
@@ -436,13 +448,9 @@ procdesc_close(struct file *fp, struct thread *td)
 				p->p_oppid = p->p_reaper->p_pid;
 				proc_add_orphan(p, p->p_reaper);
 			}
-			if ((pd->pd_flags & PDF_DAEMON) == 0)
-				kern_psignal(p, SIGKILL);
-			PROC_UNLOCK(p);
-			sx_xunlock(&proctree_lock);
+			procdesc_close_tail(fp, p);
 		} else {
-			PROC_UNLOCK(p);
-			sx_xunlock(&proctree_lock);
+			procdesc_close_tail(fp, p);
 		}
 	}
 
diff --git a/sys/sys/file.h b/sys/sys/file.h
index f5d0778453d1..a5125d0559b1 100644
--- a/sys/sys/file.h
+++ b/sys/sys/file.h
@@ -213,6 +213,7 @@ struct file {
 	union {
 		int16_t	f_seqcount[2];	/* (a) Count of seq. reads and writes. */
 		int	f_pipegen;
+		int	f_pdflags;	/* Per-file flags for procdesc. */
 	};
 	off_t		f_nextoff[2];	/* next expected read/write offset. */
 	union {
diff --git a/sys/sys/procdesc.h b/sys/sys/procdesc.h
index 08b563828b95..5fbd43f1ac84 100644
--- a/sys/sys/procdesc.h
+++ b/sys/sys/procdesc.h
@@ -89,7 +89,12 @@ struct procdesc {
  * Flags for the pd_flags field.
  */
 #define	PDF_EXITED	0x00000004	/* Process exited. */
-#define	PDF_DAEMON	0x00000008	/* Don't exit when procdesc closes. */
+
+/*
+ * Flags for file f_pdflags.
+ */
+#define	F_PD_NOKILL	0x00000001	/* Opened with PD_DAEMON. Don't send
+					   SIGKILL when file closes. */
 
 /*
  * In-kernel interfaces to process descriptors.