Skip to content

Commit

Permalink
Add fget_remote()
Browse files Browse the repository at this point in the history
The function holds and returns struct file for a file descriptor index
in the given process.

Reviewed by:	brooks, markj
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D43518
  • Loading branch information
kostikbel committed Jan 24, 2024
1 parent 24fee97 commit 58d3171
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
32 changes: 32 additions & 0 deletions sys/kern/kern_descrip.c
Original file line number Diff line number Diff line change
Expand Up @@ -2960,6 +2960,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)
Expand Down
1 change: 1 addition & 0 deletions sys/sys/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,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;
Expand Down

0 comments on commit 58d3171

Please sign in to comment.