git: 9fefa628025c - stable/15 - fget_remote(): return fcaps and fde_flags if requested

Konstantin Belousov <[email protected]>
Newsgroups gmane.os.freebsd.devel.cvs.src
Message-ID <6a812495.4448f.713ae546__48615.4947125883$1786848594$gmane$org@gitrepo.freebsd.org>
The branch stable/15 has been updated by kib:

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

commit 9fefa628025cbd0f44bab388607b61adf14167d8
Author:     Konstantin Belousov <[email protected]>
AuthorDate: 2026-05-23 07:03:17 +0000
Commit:     Konstantin Belousov <[email protected]>
CommitDate: 2026-08-16 02:41:25 +0000

    fget_remote(): return fcaps and fde_flags if requested
    
    (cherry picked from commit 77b6adbe5e351d1907e14d21c49291ff40ba879a)
---
 sys/kern/kern_descrip.c | 19 ++++++++++++++++---
 sys/kern/kern_event.c   |  2 +-
 sys/kern/sys_generic.c  |  4 ++--
 sys/sys/file.h          |  3 ++-
 4 files changed, 21 insertions(+), 7 deletions(-)

diff --git a/sys/kern/kern_descrip.c b/sys/kern/kern_descrip.c
index ec9fbdae2fa7..b10b7f4f90bb 100644
--- a/sys/kern/kern_descrip.c
+++ b/sys/kern/kern_descrip.c
@@ -3128,13 +3128,20 @@ fget_cap(struct thread *td, int fd, const cap_rights_t *needrightsp,
 #endif
 
 int
-fget_remote(struct thread *td, struct proc *p, int fd, struct file **fpp)
+fget_remote(struct thread *td, struct proc *p, int fd, struct filecaps *fcaps,
+    uint8_t *fd_flags, struct file **fpp)
 {
 	struct filedesc *fdp;
 	struct file *fp;
 	int error;
 
-	if (p == td->td_proc)	/* curproc */
+	/*
+	 * Both fcaps and fd_flags must be either requested together,
+	 * or not at all.
+	 */
+	MPASS((!(fcaps == NULL) ^ (fd_flags == NULL)));
+
+	if (p == td->td_proc && fcaps == NULL)	/* curproc */
 		return (fget_unlocked(td, fd, &cap_no_rights, fpp));
 
 	PROC_LOCK(p);
@@ -3147,6 +3154,12 @@ fget_remote(struct thread *td, struct proc *p, int fd, struct file **fpp)
 		fp = fget_noref(fdp, fd);
 		if (fp != NULL && fhold(fp)) {
 			*fpp = fp;
+			if (fd_flags != NULL) {
+				*fd_flags = fde_to_fd_flags(fdp->fd_ofiles[fd].
+				    fde_flags);
+			}
+			if (fcaps != NULL)
+				*fcaps = fdp->fd_ofiles[fd].fde_caps;
 			error = 0;
 		} else {
 			error = EBADF;
@@ -3187,7 +3200,7 @@ fget_remote_foreach(struct thread *td, struct proc *p,
 	}
 
 	for (fd = 0; fd <= highfd; fd++) {
-		error1 = fget_remote(td, p, fd, &fp);
+		error1 = fget_remote(td, p, fd, NULL, NULL, &fp);
 		if (error1 != 0)
 			continue;
 		error = fn(p, fd, fp, arg);
diff --git a/sys/kern/kern_event.c b/sys/kern/kern_event.c
index 62c449e59f68..456eddfaea9f 100644
--- a/sys/kern/kern_event.c
+++ b/sys/kern/kern_event.c
@@ -3391,7 +3391,7 @@ sysctl_kern_proc_kqueue_one(struct thread *td, struct sbuf *s, struct proc *p,
 	struct kqueue *kq;
 	int error;
 
-	error = fget_remote(td, p, kq_fd, &fp);
+	error = fget_remote(td, p, kq_fd, NULL, NULL, &fp);
 	if (error == 0) {
 		if (fp->f_type != DTYPE_KQUEUE) {
 			error = EINVAL;
diff --git a/sys/kern/sys_generic.c b/sys/kern/sys_generic.c
index b829a70f26aa..c6aa06e62be6 100644
--- a/sys/kern/sys_generic.c
+++ b/sys/kern/sys_generic.c
@@ -2208,9 +2208,9 @@ kern_kcmp(struct thread *td, pid_t pid1, pid_t pid2, int type,
 	switch (type) {
 	case KCMP_FILE:
 	case KCMP_FILEOBJ:
-		error = fget_remote(td, p1, idx1, &fp1);
+		error = fget_remote(td, p1, idx1, NULL, NULL, &fp1);
 		if (error == 0) {
-			error = fget_remote(td, p2, idx2, &fp2);
+			error = fget_remote(td, p2, idx2, NULL, NULL, &fp2);
 			if (error == 0) {
 				if (type == KCMP_FILEOBJ)
 					res = fo_cmp(fp1, fp2, td);
diff --git a/sys/sys/file.h b/sys/sys/file.h
index bc529df82048..055e6b38483d 100644
--- a/sys/sys/file.h
+++ b/sys/sys/file.h
@@ -283,7 +283,8 @@ int fget_write(struct thread *td, int fd, const cap_rights_t *rightsp,
 int fget_fcntl(struct thread *td, int fd, const 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);
+int fget_remote(struct thread *td, struct proc *p, int fd,
+    struct filecaps *fcaps, uint8_t *fd_flags, struct file **fpp);
 int fget_remote_foreach(struct thread *td, struct proc *p,
     int (*fn)(struct proc *, int, struct file *, void *), void *arg);
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.