git: 3e3b87ea6e69 - stable/14 - Add fget_remote()

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Wed, 31 Jan 2024 00:46:06 UTC
The branch stable/14 has been updated by kib:

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

commit 3e3b87ea6e6936c4757391477075d487b6745a12
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2024-01-22 22:31:55 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2024-01-30 20:24:42 +0000

    Add fget_remote()
    
    (cherry picked from commit 58d3171698341c664d7c676541b86385a924ae93)
---
 sys/kern/kern_descrip.c | 32 ++++++++++++++++++++++++++++++++
 sys/sys/file.h          |  1 +
 2 files changed, 33 insertions(+)

diff --git a/sys/kern/kern_descrip.c b/sys/kern/kern_descrip.c
index fe6928e421db..7bb392eee6b2 100644
--- a/sys/kern/kern_descrip.c
+++ b/sys/kern/kern_descrip.c
@@ -2962,6 +2962,38 @@ fget_cap(struct thread *td, int fd, cap_rights_t *needrightsp,
 }
 #endif
 
+int
+fget_remote(struct thread *td, struct proc *p, int fd, struct file **fpp)
+{
+	struct filedesc *fdp;
+	struct file *fp;
+	int error;
+
+	if (p == td->td_proc)	/* curproc */
+		return (fget_unlocked(td, fd, &cap_no_rights, fpp));
+
+	PROC_LOCK(p);
+	fdp = fdhold(p);
+	PROC_UNLOCK(p);
+	if (fdp == NULL)
+		return (ENOENT);
+	FILEDESC_SLOCK(fdp);
+	if (refcount_load(&fdp->fd_refcnt) != 0) {
+		fp = fget_noref(fdp, fd);
+		if (fp != NULL && fhold(fp)) {
+			*fpp = fp;
+			error = 0;
+		} else {
+			error = EBADF;
+		}
+	} else {
+		error = ENOENT;
+	}
+	FILEDESC_SUNLOCK(fdp);
+	fddrop(fdp);
+	return (error);
+}
+
 #ifdef CAPABILITIES
 int
 fgetvp_lookup_smr(struct nameidata *ndp, struct vnode **vpp, bool *fsearch)
diff --git a/sys/sys/file.h b/sys/sys/file.h
index cf2544ee70cf..ab8aefbb32f1 100644
--- a/sys/sys/file.h
+++ b/sys/sys/file.h
@@ -263,6 +263,7 @@ int fget_write(struct thread *td, int fd, cap_rights_t *rightsp,
 int fget_fcntl(struct thread *td, int fd, cap_rights_t *rightsp,
     int needfcntl, struct file **fpp);
 int _fdrop(struct file *fp, struct thread *td);
+int fget_remote(struct thread *td, struct proc *p, int fd, struct file **fpp);
 
 fo_rdwr_t	invfo_rdwr;
 fo_truncate_t	invfo_truncate;