The branch main has been updated by kib:

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

commit 77b6adbe5e351d1907e14d21c49291ff40ba879a
Author:     Konstantin Belousov <[email protected]>
AuthorDate: 2026-05-23 07:03:17 +0000
Commit:     Konstantin Belousov <[email protected]>
CommitDate: 2026-07-07 21:54:35 +0000

    fget_remote(): return fcaps and fde_flags if requested
    
    Reviewed by:    markj
    Tested by:      pho
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
    Differential revision:  https://reviews.freebsd.org/D57163
---
 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 349ebb5702a5..351cc1917b04 100644
--- a/sys/kern/kern_descrip.c
+++ b/sys/kern/kern_descrip.c
@@ -3156,13 +3156,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);
@@ -3175,6 +3182,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;
@@ -3215,7 +3228,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 01bd22fbefd1..fa7f8bf7cdb7 100644
--- a/sys/kern/kern_event.c
+++ b/sys/kern/kern_event.c
@@ -3355,7 +3355,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 07b98613e087..079bd7b347d7 100644
--- a/sys/kern/sys_generic.c
+++ b/sys/kern/sys_generic.c
@@ -2204,9 +2204,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 a5125d0559b1..abe211aaa39a 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);
 

Reply via email to